Changed include syntax to be a full path instead of a relative path.
[eq/.git] / inc / class.eq.inc.php
1 <?php
2   /**************************************************************************\
3   * phpGroupWare - eq                                                        *
4   * http://www.phpgroupware.org                                              *
5   * -----------------------------------------------                          *
6   *  This program is free software; you can redistribute it and/or modify it *
7   *  under the terms of the GNU General Public License as published by the   *
8   *  Free Software Foundation; either version 2 of the License, or (at your  *
9   *  option) any later version.                                              *
10   \**************************************************************************/
11       /* $Id: class.eq.inc.php,v 1.1.1.1 2005/07/20 07:40:32 ajp Exp $ */
12
13 class eq
14 {
15   var $db;
16   var $db2;
17   var $t;
18   var $nextmatchs;
19   var $grants;
20   var $jscal;
21   var $cal_options;  
22   var $default_ht_num_months;
23   var $default_ppi_num_months;
24   var $default_ppi_num_years;
25   var $default_int_num_quarters;
26   var $default_int_num_years;
27   var $default_vis_num_years;
28   var $default_att_num_quarters;
29   var $max_num_districts;
30   var $current_year;
31   var $current_month;
32   var $upload_target_path;
33   var $script_path;
34   var $max_appointments;
35   var $max_presidency_members;
36   
37   var $public_functions = array
38     (
39      'ht_view'    => True,
40      'ht_update'  => True,
41      'act_list'   => True,
42      'act_view'   => True,
43      'act_update' => True,
44      'par_view'   => True,
45      'ppi_view'   => True,
46      'ppi_update' => True,
47      'ppi_sched'  => True,
48      'int_view'   => True,
49      'int_update' => True,
50      'int_sched'  => True,
51      'vis_sched'  => True,
52      'vis_view'   => True,
53      'vis_update' => True,
54      'att_view'   => True,
55      'att_update' => True,
56      'dir_view'   => True,
57      'org_view'   => True,
58      'schedule'   => True,
59      'admin'      => True,
60      'email_appt' => True,
61      'willing_view'   => True,
62      'willing_update' => True,
63      'send_ical_appt' => True,
64      'assign_view'    => True,
65      'assign_update'  => True,
66      'get_time_selection_form' => True,
67      );
68  
69   function eq()
70     {
71       $base_path = $_SERVER['DOCUMENT_ROOT'];
72       if(file_exists("$base_path/eq/setup/eq_config.local")) {
73         include("$base_path/eq/setup/eq_config.local");
74       } else {
75         include("$base_path/eq/setup/eq_config");
76       }
77       
78       $this->script_path = "$this->application_path"."/bin";
79       $this->max_presidency_members = 99;
80       $this->max_appointments = 32768;
81       
82       $this->db         = $GLOBALS['phpgw']->db;
83       $this->db2        = $this->db;
84       $this->db3        = $this->db;
85       $this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
86       $this->t          = $GLOBALS['phpgw']->template;
87       $this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
88       $this->grants     = $GLOBALS['phpgw']->acl->get_grants('eq');
89       $this->grants[$this->account] = PHPGW_ACL_READ + PHPGW_ACL_ADD + PHPGW_ACL_EDIT + PHPGW_ACL_DELETE;
90        
91       $this->jscal = CreateObject('eq.jscalendar');   // before phpgw_header() !!!
92       $this->cal_options = 'daFormat    : "%Y-%m-%d",
93                                 ifFormat    : "%Y-%m-%d",
94                                 mondayFirst : false,
95                                 weekNumbers : false';
96        
97       $GLOBALS['phpgw_info']['flags']['app_header'] = 'Elders Quorum Tools - The 3rd Counselor';
98       $GLOBALS['phpgw']->common->phpgw_header();
99
100       $this->current_day = `date '+%d'`;
101       $this->current_day = $this->current_day-0; // Make it numeric
102       $this->current_month = `date '+%m'`;
103       $this->current_month = $this->current_month-0; // Make it numeric
104       $this->current_year = `date '+%Y'`;
105       $this->current_year = $this->current_year-0; // Make it numeric
106
107       echo parse_navbar();
108       $this->display_app_header();      
109     }
110   
111   function save_sessiondata()
112     {
113       
114     }
115
116   function display_app_header()
117     {
118       $this->t->set_file(array('eq_header' => 'header.tpl'));
119       
120       if (isset($phpgw_info['user']['preferences']['eq']['eq_font']))
121         {
122           $font = $phpgw_info['user']['preferences']['eq']['eq_font'];
123         }
124       else
125         {
126           $font = 'Arial';
127         }
128       
129       $this->t->set_var('bg_color',$phpgw_info['theme']['th_bg']);
130       $this->t->set_var('font',$font);
131       $link_data['menuaction'] = 'eq.eq.ht_view';
132       $this->t->set_var('link_hometeaching',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
133       $this->t->set_var('lang_hometeaching','HomeTeaching');
134       $link_data['menuaction'] = 'eq.eq.act_list';
135       $this->t->set_var('link_activity',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
136       $this->t->set_var('lang_activity','Activities');
137       $link_data['menuaction'] = 'eq.eq.willing_view';
138       $this->t->set_var('link_willing',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
139       $this->t->set_var('lang_willing','Willingness');
140       $link_data['menuaction'] = 'eq.eq.assign_view';
141       $this->t->set_var('link_assignment',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
142       $this->t->set_var('lang_assignment','Assignments');
143       $link_data['menuaction'] = 'eq.eq.par_view';
144       $this->t->set_var('link_participation',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
145       $this->t->set_var('lang_participation','Participation');
146       $link_data['menuaction'] = 'eq.eq.ppi_view';
147       $this->t->set_var('link_ppi',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
148       $this->t->set_var('lang_ppi','PPIs');
149       $link_data['menuaction'] = 'eq.eq.int_view';
150       $this->t->set_var('link_int',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
151       $this->t->set_var('lang_int','Interviews');
152       $link_data['menuaction'] = 'eq.eq.vis_view';
153       $this->t->set_var('link_visit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
154       $this->t->set_var('lang_visit','Visits');
155       $link_data['menuaction'] = 'eq.eq.att_view';      
156       $this->t->set_var('link_attendance',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
157       $this->t->set_var('lang_attendance','Attendance');
158       $link_data['menuaction'] = 'eq.eq.dir_view';      
159       $this->t->set_var('link_dir',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
160       $this->t->set_var('lang_dir','Directory');
161       $link_data['menuaction'] = 'eq.eq.org_view';      
162       $this->t->set_var('link_org',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
163       $this->t->set_var('lang_org','Callings');
164       $link_data['menuaction'] = 'eq.eq.admin'; 
165       $this->t->set_var('link_admin',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
166       $this->t->set_var('lang_admin','Admin');
167       $link_data['menuaction'] = 'eq.eq.schedule';      
168       $this->t->set_var('link_schedule',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
169       $this->t->set_var('lang_schedule','Scheduling');
170                 
171       $this->t->pparse('out','eq_header');
172     }
173
174   function ht_view()
175     {
176       $this->t->set_file(array('ht_view_t' => 'ht_view.tpl'));
177       $this->t->set_block('ht_view_t','district_list','list');
178    
179       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
180       $num_months = get_var('num_months',array('GET','POST'));
181       if($num_months == '') { $num_months = $this->default_ht_num_months; }
182       $this->t->set_var('num_months',$num_months);
183       $this->t->set_var('lang_filter','Filter');
184       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
185       else {  $this->t->set_var('lang_num_months','Months of History'); }
186       
187       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
188       $this->t->set_var('title','Hometeaching'); 
189       
190       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
191       $this->db->query($sql,__LINE__,__FILE__);
192       $i=0;
193       while ($this->db->next_record())
194         {
195           $districts[$i]['district'] = $this->db->f('district');
196           $districts[$i]['name'] = $this->db->f('name');
197           $districts[$i]['supervisor'] = $this->db->f('supervisor');
198           $i++;
199         }
200
201       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
202       $this->db->query($sql,__LINE__,__FILE__);
203       $i=0;
204       while ($this->db->next_record())
205         {
206           $elder_id[$i] = $this->db->f('elder');
207           $elder_name[$i] = $this->db->f('name');
208           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
209           $i++;
210         }
211       array_multisort($elder_name, $elder_id);
212
213       // Make an array mapping elder_ids to elder_names
214       for($i=0; $i < count($elder_id); $i++) {
215           $id = $elder_id[$i];
216           $elders[$id] = $elder_name[$i];
217       }      
218
219       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
220       $this->db->query($sql,__LINE__,__FILE__);
221       while ($this->db->next_record())
222         {
223           $aaronic_id = $this->db->f('aaronic');
224           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
225           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
226         }
227       
228       $total_families = 0;
229       $this->nextmatchs->template_alternate_row_color(&$this->t);
230       for ($i=0; $i < count($districts); $i++) {
231         $this->t->set_var('district_number',$districts[$i]['district']);
232         $this->t->set_var('district_name',$districts[$i]['name']);      
233         $supervisor = $districts[$i]['supervisor'];
234         $unique_companionships='';
235                 
236         // Select all the unique companionship numbers for this district
237         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
238         $this->db->query($sql,__LINE__,__FILE__);
239         $j=0;
240         while ($this->db->next_record())
241           {
242             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
243             $j++;
244           }
245
246         $comp_width=450; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
247         $table_data=""; $num_companionships = 0; $num_families = 0;
248         for($m=$num_months; $m >= 0; $m--) { $visits[$m] = 0; }
249         for ($j=0; $j < count($unique_companionships); $j++) {
250           $companion_table_entry = "";
251           // Select all the companions in each companionship
252           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
253                  "companionship=". $unique_companionships[$j]['companionship'];
254           $this->db->query($sql,__LINE__,__FILE__);
255
256           while ($this->db->next_record())
257             {
258               // Get this companions information
259               if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
260               $companionship = $this->db->f('companionship');
261               $elder_id = $this->db->f('elder');
262               $aaronic_id = $this->db->f('aaronic');
263               if($elder_id) {
264                 $name = $elders[$elder_id];
265                 $phone = $elder_phone[$elder_id];
266               }
267               else if($aaronic_id) {
268                 $name = $aaronic[$aaronic_id]['name'];
269                 $phone = $aaronic[$aaronic_id]['phone'];                
270               }
271               $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
272             }
273           $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
274           
275           // Get the names of the families assigned this home teaching companionship
276           $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
277           $sql = $sql . " ORDER BY name ASC";
278           $this->db->query($sql,__LINE__,__FILE__);
279           $k=0;
280           while ($this->db->next_record())
281             {
282               $num_families++; $total_families++;
283               $family_name = $this->db->f('name');
284               $family_id = $this->db->f('family');
285               $this->nextmatchs->template_alternate_row_color(&$this->t);
286               $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
287               // Find out how many times Visits were performed by this companionship
288               // in the past $num_months for this Family
289               $header_row="<th width=$comp_width><font size=-2>Families</th>";
290               for($m=$num_months; $m >= 0; $m--) {
291                 $month = $this->current_month - $m;
292                 $year = $this->current_year;
293                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
294                 if($month < 10) { $month = "0"."$month"; }
295                 $month_start = "$year"."-"."$month"."-"."01";
296                 $month_end = "$year"."-"."$month"."-"."31";
297                 $month = "$month"."/"."$year";
298                 
299                 //print "m: $m month: $month year: $year month_start: $month_start month_end: $month_end<br>";
300                 // Add this to the query to filter on only visits made by this companionship:
301                 // " AND companionship=" . $unique_companionships[$j]['companionship'].
302
303                 // First check to see if the currently assigned companionship has visited them
304                 $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
305                    " AND companionship=".$unique_companionships[$j]['companionship'].
306                    " AND family=". $family_id;
307                 $query_id = $this->db2->query($sql,__LINE__,__FILE__);
308                 if($this->db2->num_rows($query_id) == 0) {
309                   // We did not find any visits made by the currently assigned companionship,
310                   // look for visits made by any other companionship other than 0. (0 == EQ Presidency Visit)
311                   $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
312                      " AND companionship!=0".
313                      " AND family=". $family_id;
314                   $query_id = $this->db2->query($sql,__LINE__,__FILE__);
315                 }
316                 $this->db2->query($sql,__LINE__,__FILE__);
317                 $link_data['menuaction'] = 'eq.eq.ht_update';
318                 $link_data['date'] = $month_start;
319                 $link_data['month_start'] = $month_start;
320                 $link_data['month_end'] = $month_end;
321                 $link_data['month'] = $month;
322                 $link_data['district'] = $districts[$i]['district'];
323                 $link_data['district_name'] = $districts[$i]['name'];
324                 $link_data['action'] = 'view';
325                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
326                 $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
327                 if(!$total_visits[$m]) { $total_visits[$m] = 0; }
328                 if($this->db2->next_record()) {
329                   if($this->db2->f('visited') == 'y') {
330                     $visits[$m]++; $total_visits[$m]++;           
331                     $table_data .= '<td align=center><a href="'.$link.'"><img src="images/checkmark.gif"></a></td>';
332                   }
333                   else if($this->db2->f('visited') == 'n') {
334                     $table_data .= '<td align=center><a href="'.$link.'"><img src="images/x.gif"></a></td>';
335                   }
336                   else {
337                     $visits[$m]++; $total_visits[$m]++;
338                     $table_data .= "<td>&nbsp;</td>";
339                   }
340                 }
341                 else {
342                   $visits[$m]++; $total_visits[$m]++;
343                   $table_data .= "<td>&nbsp;</td>";
344                 }
345               }
346               $table_data .= "</tr>"; 
347               $k++;
348             }
349           $table_data .= "<tr><td colspan=20></td></tr>";
350         }
351         $table_data .= "<tr><td colspan=20><hr></td></tr>";
352         $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
353
354         for($m=$num_months; $m >=0; $m--) {
355           $percent = ceil(($visits[$m] / $num_families)*100);
356           $stat_data .= "<td align=center><font size=-2><b>$visits[$m]<br>$percent%</font></b></td>";
357         }
358         $stat_data .= "</tr>";
359
360         $this->t->set_var('table_width',$table_width);
361         $this->t->set_var('header_row',$header_row);
362         $this->t->set_var('table_data',$table_data);
363         $this->t->set_var('stat_data',$stat_data);
364         $this->t->fp('list','district_list',True);
365       }
366
367       $totals = "<tr><td><b><font size=-2>$total_families Total Families<br>Visit Totals:</font></b></td>";
368       for($m=$num_months; $m >=0; $m--) {
369         $percent = ceil(($total_visits[$m] / $total_families)*100);
370         $totals .= "<td align=center><font size=-2><b>$total_visits[$m]<br>$percent%</font></b></td>";
371       }
372       $totals .= "</tr>";
373       
374       $this->t->set_var('totals',$totals);
375       
376       $this->t->pfp('out','ht_view_t');
377       $this->save_sessiondata();
378     }
379       
380
381   function ht_update()
382     {
383       $this->t->set_file(array('ht_update_t' => 'ht_update.tpl'));
384       $this->t->set_block('ht_update_t','district_list','list');
385       $this->t->set_block('ht_update_t','save','savehandle');
386       
387       $district = get_var('district',array('GET','POST'));
388       $district_name = get_var('district_name',array('GET','POST'));
389       $date = get_var('date',array('GET','POST'));
390       $month = get_var('month',array('GET','POST'));
391       $month_start = get_var('month_start',array('GET','POST'));
392       $month_end = get_var('month_end',array('GET','POST'));
393       $action = get_var('action',array('GET','POST'));
394       
395       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
396       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_update&action=save'));
397       $this->t->set_var('lang_done','Cancel');
398       $this->t->set_var('district_name',$district_name);
399       $this->t->set_var('district_number',$district);
400       $this->t->set_var('title','Hometeaching Update ' . $month);
401       $this->t->set_var('date',$date);
402       
403       if($action == 'save')
404         {
405           // Get a list of all the companionships in this district
406           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
407           $this->db->query($sql,__LINE__,__FILE__);
408           $j=0;
409           while ($this->db->next_record())
410             {
411               $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
412               $j++;
413             }
414           for ($j=0; $j < count($unique_companionships); $j++)
415             {
416               //$comp=$unique_companionships[$j]['companionship'];
417               //print "deleting from eq_visit where companionship=$comp and date=$date and district=$district<br>";
418               // Delete all the visits that have taken place for all families for this companionsthip for this month
419               $this->db->query("DELETE from eq_visit where companionship=" . $unique_companionships[$j]['companionship'] .
420                                " AND " . "date='" . $date . "'",__LINE__,__FILE__);
421             }
422
423           // Now, add the visits that are checked for this month
424           $new_data = get_var('family_visited',array('POST'));
425           foreach ($new_data as $family)
426            {
427              foreach ($family as $data)
428                {
429                  //print "family_visited: $data <br>";
430                  $data_array = explode("/",$data);
431                  $family_id = $data_array[0];
432                  $companionship = $data_array[1];
433                  $date = $data_array[2];
434                  $visited = $data_array[3];
435                  if($visited == "") { $visited = $data_array[4]; }
436                  //print "family_id: $family_id companionship: $companionship date: $date visited: $visited<br>";
437                  $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes,visited) "
438                                   . "VALUES (" . $family_id .",". $companionship .",'". $date ."','','". $visited ."')",__LINE__,__FILE__);
439                }
440            }
441           $this->ht_view();
442           return false;
443         }
444       
445       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
446       $this->db->query($sql,__LINE__,__FILE__);
447       $i=0;
448       while ($this->db->next_record())
449         {
450           $elder_id[$i] = $this->db->f('elder');
451           $elder_name[$i] = $this->db->f('name');
452           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
453           $i++;
454         }
455       array_multisort($elder_name, $elder_id);
456
457       // Make an array mapping elder_ids to elder_names
458       for($i=0; $i < count($elder_id); $i++) {
459           $id = $elder_id[$i];
460           $elders[$id] = $elder_name[$i];
461       }      
462
463       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
464       $this->db->query($sql,__LINE__,__FILE__);
465       while ($this->db->next_record())
466         {
467           $aaronic_id = $this->db->f('aaronic');
468           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
469           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
470         }
471       
472       // Select all the unique companionship numbers for this district
473       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
474       $this->db->query($sql,__LINE__,__FILE__);
475       $j=0; $unique_companionships='';
476       while ($this->db->next_record())
477         {
478           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
479           $j++;
480         }
481       
482       $comp_width=300; $visit_width=25; $table_width=$comp_width + $visit_width;
483       $table_data=""; $num_companionships = 0; $num_families = 0; $visits=0;
484       for ($j=0; $j < count($unique_companionships); $j++) {
485         $companion_table_entry = "";
486         // Select all the companions in each companionship
487         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
488                "companionship=". $unique_companionships[$j]['companionship'];
489         $this->db->query($sql,__LINE__,__FILE__);
490         
491         while ($this->db->next_record())
492           {
493             // Get this companions information
494             if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
495             $companionship = $this->db->f('companionship');
496             $elder_id = $this->db->f('elder');
497             $aaronic_id = $this->db->f('aaronic');
498             if($elder_id) {
499               $name = $elders[$elder_id];
500               $phone = $elder_phone[$elder_id];
501             }
502             else if($aaronic_id) {
503               $name = $aaronic[$aaronic_id]['name'];
504               $phone = $aaronic[$aaronic_id]['phone'];
505             }
506             $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
507           }
508         $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
509         
510         // Get the names of the families assigned this home teaching companionship
511         $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
512         $sql = $sql . " ORDER BY name ASC";
513         $this->db->query($sql,__LINE__,__FILE__);
514         while ($this->db->next_record())
515           {
516             $num_families++; $total_families++;
517             $family_name = $this->db->f('name');
518             $family_id = $this->db->f('family');
519             $this->nextmatchs->template_alternate_row_color(&$this->t);
520             $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
521             
522             $header_row="<th width=$comp_width><font size=-2>Families</th>";
523
524             // First check to see if the currently assigned companionship has visited them
525             $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
526                    " AND companionship=".$unique_companionships[$j]['companionship'].
527                    " AND family=". $family_id;
528             $query_id = $this->db2->query($sql,__LINE__,__FILE__);
529             if($this->db2->num_rows($query_id) == 0) {
530               // We did not find any visits made by the currently assigned companionship,
531               // look for visits made by any other companionship other than 0. (0 == EQ Presidency Visit)
532               $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
533                    " AND companionship!=0".
534                    " AND family=". $family_id;
535               $query_id = $this->db2->query($sql,__LINE__,__FILE__);
536             }
537             
538             $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
539             $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
540             if(!$total_visits) { $total_visits = 0; }
541             if($this->db2->next_record()) {
542               if($this->db2->f('visited') == 'y') {
543                 $visits++; $total_visits++;
544                 $table_data .= '<td width=100 align=center>';
545                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y" checked>Y';
546                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
547                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/"> ';
548                 $table_data .= '</td>';
549               } else if($this->db2->f('visited') == 'n') {
550                 $table_data .= '<td width=100 align=center>';
551                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
552                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n" checked>N';
553                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/">';
554                 $table_data .= '</td>';
555               } else {
556                 $table_data .= '<td width=100 align=center>';
557                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
558                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
559                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
560                 $table_data .= '</td>';
561               }
562             }
563             else {
564               $value .= "/";
565               $table_data .= '<td width=100 align=center>';
566               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
567               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
568               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
569               $table_data .= '</td>';         
570             }
571           }
572         $table_data .= "</tr>"; 
573         $table_data .= "<tr><td colspan=20></td></tr>";
574       }
575       $table_data .= "<tr><td colspan=20><hr></td></tr>";
576       $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
577       
578       $percent = ceil(($visits / $num_families)*100);
579       $stat_data .= "<td align=center><font size=-2><b>$visits<br>$percent%</font></b></td>";
580       $stat_data .= "</tr>";
581       
582       $this->t->set_var('table_width',$table_width);
583       $this->t->set_var('header_row',$header_row);
584       $this->t->set_var('table_data',$table_data);
585       $this->t->set_var('stat_data',$stat_data);
586       $this->t->fp('list','district_list',True);
587
588       $this->t->set_var('lang_reset','Clear Form');
589       $this->t->set_var('lang_save','Save Changes');
590       $this->t->set_var('savehandle','');
591       
592       $this->t->pfp('out','ht_update_t');
593       $this->t->pfp('addhandle','save');
594       
595       $this->save_sessiondata();
596     }
597
598   function act_list()
599     {
600       $this->t->set_file(array('act_list_t' => 'act_list.tpl'));
601       $this->t->set_block('act_list_t','act_list','list');
602       
603       $this->t->set_var('lang_name','Assignment');
604       $this->t->set_var('lang_date','Date');
605       $this->t->set_var('lang_notes','Description');
606       
607       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
608       $this->db->query($sql,__LINE__,__FILE__);
609       $total_records = $this->db->num_rows();
610
611       $i = 0;
612       while ($this->db->next_record())
613         {
614           $activity_list[$i]['activity']  = $this->db->f('activity');
615           $activity_list[$i]['assignment'] = $this->db->f('assignment');
616           $activity_list[$i]['date']  = $this->db->f('date');
617           $activity_list[$i]['notes']  = $this->db->f('notes');
618
619           $sql = "SELECT * FROM eq_assignment WHERE assignment='" . $activity_list[$i]['assignment'] . "'";
620           $this->db2->query($sql,__LINE__,__FILE__);
621           if($this->db2->next_record())
622             {
623               $activity_list[$i]['name'] = $this->db2->f('name');
624               $activity_list[$i]['code'] = $this->db2->f('code');
625             }
626           $i++;
627         }
628             
629       for ($i=0; $i < count($activity_list); $i++)
630         {         
631           $this->nextmatchs->template_alternate_row_color(&$this->t);
632           $this->t->set_var('name',$activity_list[$i]['name']);
633           $this->t->set_var('date',$activity_list[$i]['date']);
634           $activity_notes = $activity_list[$i]['notes'];
635           if(strlen($activity_notes) > 40) { $activity_notes = substr($activity_notes,0,40) . "..."; }
636           $this->t->set_var('notes',$activity_notes);
637           
638           $link_data['menuaction'] = 'eq.eq.act_view';
639           $link_data['activity'] = $activity_list[$i]['activity'];
640           $link_data['action'] = 'view';
641           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
642           $this->t->set_var('lang_view','View');
643
644           $link_data['menuaction'] = 'eq.eq.act_update';
645           $link_data['activity'] = $activity_list[$i]['activity'];
646           $link_data['action'] = 'edit';
647           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
648           $this->t->set_var('lang_edit','Edit');
649
650           $link_data['menuaction'] = 'eq.eq.act_update';
651           $link_data['activity'] = '0';
652           $link_data['action'] = 'add';
653           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
654                            . '"><input type="submit" name="Add" value="' . 'Add Activity' .'"></font></form>');
655
656           $this->t->fp('list','act_list',True);
657         }
658
659       $this->t->pfp('out','act_list_t');
660       $this->save_sessiondata();
661     }
662
663   function act_view()
664     {
665       $this->t->set_file(array('act_view_t' => 'act_view.tpl'));
666       $this->t->set_block('act_view_t','part_list','list');
667       
668       $sql = "SELECT * FROM eq_activity WHERE activity=" . intval(get_var('activity',array('GET','POST')));
669       $this->db->query($sql,__LINE__,__FILE__);
670       $this->db->next_record();
671       $this->t->set_var('assignment', $this->db->f('assignment'));
672       $this->t->set_var('date', $this->db->f('date'));
673       $this->t->set_var('notes', $this->db->f('notes'));
674       
675       $sql = "SELECT * FROM eq_assignment WHERE assignment='" . $this->db->f('assignment') . "'";
676       $this->db2->query($sql,__LINE__,__FILE__);
677       if($this->db2->next_record())
678         {
679           $this->t->set_var('name', $this->db2->f('name'));
680           $this->t->set_var('code', $this->db2->f('code'));
681         }
682       $this->t->set_var('lang_name','Assignment');
683       $this->t->set_var('lang_date','Date');
684       $this->t->set_var('lang_notes','Description');
685       $this->t->set_var('lang_done','Done');
686       $this->t->set_var('lang_action','View');
687
688       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
689       $this->t->set_var('tr_color',$tr_color);
690             
691       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
692
693       $link_data['menuaction'] = 'eq.eq.act_update';
694       $link_data['activity'] = get_var('activity',array('GET','POST'));
695       $link_data['action'] = 'edit';
696       $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
697       $this->t->set_var('lang_edit','Edit');
698       $this->t->set_var('cal_date',$this->db->f('date'));
699       
700       // Now find out which elders participated in this activity
701       $sql = "SELECT * FROM eq_participation WHERE activity=" . intval(get_var('activity',array('GET','POST')));
702       $this->db->query($sql,__LINE__,__FILE__);
703       $total_records = $this->db->num_rows();
704
705       $i = 0;
706       while ($this->db->next_record())
707         {
708           $part_list[$i]['elder']  = $this->db->f('elder');
709           $i++;
710         }
711       
712       for ($i=0; $i < count($part_list); $i++)
713         {
714           $sql = "SELECT * FROM eq_elder WHERE elder=" . $part_list[$i]['elder'];
715           $this->db->query($sql,__LINE__,__FILE__);
716           $this->db->next_record();
717           $names[$i] = $this->db->f('name');
718         } sort($names);
719       
720       for ($i=0; $i < count($names); $i++)
721         {
722           //$this->nextmatchs->template_alternate_row_color(&$this->t);
723           $this->t->set_var('elder_name',$names[$i]);
724           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
725           else { $this->t->set_var('table_sep',"</td>"); }
726           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
727           $this->t->fp('list','part_list',True);
728         }
729       
730       $this->t->pfp('out','act_view_t');
731       $this->save_sessiondata();
732     }
733
734   function act_update()
735     {
736       $this->t->set_file(array('form' => 'act_update.tpl'));
737       $this->t->set_block('form','elder_list','list');
738       $this->t->set_block('form','add','addhandle');
739       $this->t->set_block('form','edit','edithandle');
740       $this->t->set_var('lang_done','Done');
741
742       $action = get_var('action',array('GET','POST'));
743       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
744       $activity['activity'] = intval(get_var('activity',array('GET','POST')));
745       
746       if($action == 'save')
747         {
748           $activity['assignment'] = get_var('assignment',array('POST'));
749           $activity['date'] = get_var('date',array('POST'));
750           $activity['notes']= get_var('notes',array('POST'));
751           $this->db->query("UPDATE eq_activity set " .
752                            "   assignment='" . $activity['assignment'] .
753                            "', date='" . $activity['date'] . "'" .
754                            ", notes='" . $activity['notes'] . "'" .
755                            " WHERE activity=" . $activity['activity'],__LINE__,__FILE__);
756
757           // Delete all the elders who have particiapted in this activity
758           $this->db->query("DELETE from eq_participation where activity=".$activity['activity'],__LINE__,__FILE__);
759           
760           // Re-add the elders who are checked as having participated in this activity
761           $elders = get_var('elder_name',array('POST'));
762           foreach ($elders as $elder)
763             {
764               $this->db->query("INSERT INTO eq_participation (elder,activity) "
765                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
766             }
767
768           $this->act_list();
769           return false;
770         }
771
772       if($action == 'insert')
773         {
774           $activity['assignment'] = get_var('assignment',array('POST'));
775           $activity['date'] = get_var('date',array('POST'));
776           $activity['notes']= get_var('notes',array('POST'));
777           $this->db->query("INSERT INTO eq_activity (assignment,date,notes) "
778                            . "VALUES ('" . $activity['assignment'] . "','"
779                            . $activity['date'] . "','" . $activity['notes'] . "')",__LINE__,__FILE__);
780
781           $sql = "SELECT * FROM eq_activity WHERE assignment='".$activity['assignment']."' "
782              . " AND date='".$activity['date']."' AND notes='".$activity['notes']."'";
783           $this->db->query($sql,__LINE__,__FILE__);
784           if($this->db->next_record()) {
785             //print "activity: " . $this->db->f('activity') . "<br>";
786             $activity['activity'] = $this->db->f('activity');
787           }
788           
789           $elders = get_var('elder_name',array('POST'));
790           foreach ($elders as $elder)
791             {
792               $this->db->query("INSERT INTO eq_participation (elder,activity) "
793                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
794             }
795           
796           $this->act_list();
797           return false;
798         }
799       
800       if($action == 'add')
801         {
802           $activity['activity'] = 0;
803           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
804           $this->t->set_var('assignment','');
805           $this->t->set_var('date','');
806           $this->t->set_var('notes','');
807           $this->t->set_var('lang_done','Cancel');
808           $this->t->set_var('lang_action','Adding New Activity');
809           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
810                                                                 . $activity['activity'] . '&action=' . 'insert'));
811         }
812
813       if($action == 'edit')
814         {
815           $sql = "SELECT * FROM eq_activity WHERE activity=" . $activity['activity'];
816           $this->db->query($sql,__LINE__,__FILE__);
817           $this->db->next_record();
818           $this->t->set_var('cal_date',$this->jscal->input('date',$this->db->f('date'),'','','','','',$this->cal_options));
819           $this->t->set_var('assignment', $this->db->f('assignment'));
820           $assignment = $this->db->f('assignment');
821           $this->t->set_var('date', $this->db->f('date'));
822           $this->t->set_var('notes', $this->db->f('notes'));
823           $this->t->set_var('lang_done','Cancel');
824           $this->t->set_var('lang_action','Editing Activity');
825           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
826                                                                 . $activity['activity'] . '&action=' . 'save'));
827
828         }
829
830       // Create the assignments drop-down list
831       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
832       $this->db->query($sql,__LINE__,__FILE__);
833       $i = 0;
834       while ($this->db->next_record())
835         {
836           $assignments[$i]['assignment']  = $this->db->f('assignment');
837           $assignments[$i]['name'] = $this->db->f('name');
838           $assignments[$i]['code'] = $this->db->f('code');
839           $i++;
840         }
841       
842       $assignment_data.= '<select name=assignment>';
843       $assignment_data.= '<option value=0></option>';  
844       for ($j=0; $j < count($assignments); $j++) {
845         $id = $assignments[$j]['assignment'];
846         $name = $assignments[$j]['name'];
847         if($assignments[$j]['assignment'] == $assignment) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
848         $assignment_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
849       }
850       $assignment_data.='</select>';
851       $this->t->set_var('assignment_data',$assignment_data);
852       
853       // Create elder selection boxes
854       $sql = "SELECT * FROM eq_elder";
855       $this->db->query($sql,__LINE__,__FILE__);
856       $i=0;
857       while ($this->db->next_record())
858         {
859           if($this->db->f('valid') == 1 || $action != 'add') {
860             $elder_name[$i] = $this->db->f('name');
861             $elder_id[$i] = $this->db->f('elder');
862             $elder_valid[$i] = $this->db->f('valid');
863             $i++;
864           }
865         }
866       array_multisort($elder_name, $elder_id, $elder_valid);
867
868       $j=0;
869       for ($i=0; $i < count($elder_id); $i++)
870         {
871           //$this->nextmatchs->template_alternate_row_color(&$this->t);
872           $sql = "SELECT * FROM eq_participation where activity=". $activity['activity'] . " AND elder=" . $elder_id[$i];
873           $this->db->query($sql,__LINE__,__FILE__);
874           if($this->db->next_record()) { $this->t->set_var('checked','checked'); $checked=1; }
875           else { $this->t->set_var('checked',''); $checked=0; }
876           if($checked || $elder_valid[$i] == 1) {
877             $this->t->set_var('elder_name',$elder_name[$i]);
878             $this->t->set_var('elder',$elder_id[$i]);
879             if(($j+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
880             else { $this->t->set_var('table_sep',"</td>"); }
881             if(($j) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
882             $this->t->fp('list','elder_list',True);
883             $j++;
884           }
885         }
886       
887       $this->t->set_var('lang_reset','Clear Form');
888       $this->t->set_var('lang_add','Add Activity');
889       $this->t->set_var('lang_save','Save Changes');
890       $this->t->set_var('edithandle','');
891       $this->t->set_var('addhandle','');
892       
893       $this->t->pfp('out','form');
894       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
895       if($action == 'add') { $this->t->pfp('addhandle','add'); }
896       
897       $this->save_sessiondata();
898     }
899
900     function assign_view()
901     {
902       $this->t->set_file(array('assign_view_t' => 'assign_view.tpl'));
903       $this->t->set_block('assign_view_t','assign_view','list');
904       
905       $this->t->set_var('lang_name','Assignment Name');
906       $this->t->set_var('lang_code','Code');
907       
908       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
909       $this->db->query($sql,__LINE__,__FILE__);
910       $total_records = $this->db->num_rows();
911
912       $i = 0;
913       while ($this->db->next_record())
914         {
915           $assignment_list[$i]['assignment']  = $this->db->f('assignment');
916           $assignment_list[$i]['name'] = $this->db->f('name');
917           $assignment_list[$i]['code'] = $this->db->f('code');
918           $i++;
919         }
920             
921       for ($i=0; $i < count($assignment_list); $i++)
922         {         
923           $this->nextmatchs->template_alternate_row_color(&$this->t);
924           $this->t->set_var('name',$assignment_list[$i]['name']);
925           $this->t->set_var('code',$assignment_list[$i]['code']);
926           
927           $link_data['menuaction'] = 'eq.eq.assign_update';
928           $link_data['assignment'] = $assignment_list[$i]['assignment'];
929           $link_data['action'] = 'edit';
930           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
931           $this->t->set_var('lang_edit','Edit');
932
933           $link_data['menuaction'] = 'eq.eq.assign_update';
934           $link_data['assignment'] = '0';
935           $link_data['action'] = 'add';
936           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
937                            . '"><input type="submit" name="Add" value="' . 'Add Assignment' .'"></font></form>');
938
939           $this->t->fp('list','assign_view',True);
940         }
941
942       $this->t->pfp('out','assign_view_t');
943       $this->save_sessiondata();
944     }
945
946     function assign_update()
947     {
948       $this->t->set_file(array('form' => 'assign_update.tpl'));
949       $this->t->set_block('form','add','addhandle');
950       $this->t->set_block('form','edit','edithandle');
951       $this->t->set_var('lang_done','Done');
952
953       $action = get_var('action',array('GET','POST'));
954       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.assign_view'));
955       $assignment['assignment'] = intval(get_var('assignment',array('GET','POST')));
956
957       if($action == 'save')
958         {
959           $assignment['name'] = get_var('name',array('POST'));
960           $assignment['code'] = get_var('code',array('POST'));
961           $this->db->query("UPDATE eq_assignment set " .
962                            "  name='" . $assignment['name'] . "'" .
963                            ", code='" . $assignment['code'] . "'" .
964                            " WHERE assignment=" . $assignment['assignment'],__LINE__,__FILE__);
965           
966           $this->assign_view();
967           return false;
968         }
969
970       if($action == 'insert')
971         {
972           $assignment['name'] = get_var('name',array('POST'));
973           $assignment['code'] = get_var('code',array('POST'));
974           $this->db->query("INSERT INTO eq_assignment (name,code) "
975                            . "VALUES ('" . $assignment['name'] . "','"
976                            . $assignment['code'] . "')",__LINE__,__FILE__);
977           $this->assign_view();
978           return false;
979         }
980       
981       if($action == 'add')
982         {
983           $assignment['assignment'] = 0;
984           $this->t->set_var('name','');
985           $this->t->set_var('code','');
986           $this->t->set_var('lang_done','Cancel');
987           $this->t->set_var('lang_action','Adding New Assignment');
988           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.assign_update&assignment='
989                                                                 . $assignment['assignment'] . '&action=' . 'insert'));
990         }
991
992       if($action == 'edit')
993         {
994           $sql = "SELECT * FROM eq_assignment WHERE assignment=" . $assignment['assignment'];
995           $this->db->query($sql,__LINE__,__FILE__);
996           $this->db->next_record();
997           $this->t->set_var('name', $this->db->f('name'));
998           $this->t->set_var('code', $this->db->f('code'));
999           $this->t->set_var('lang_done','Cancel');
1000           $this->t->set_var('lang_action','Editing Assignment');
1001           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.assign_update&assignment='
1002                                                                 . $assignment['assignment'] . '&action=' . 'save'));
1003
1004         }
1005       
1006       $this->t->set_var('lang_reset','Clear Form');
1007       $this->t->set_var('lang_add','Add Assignment');
1008       $this->t->set_var('lang_save','Save Changes');
1009       $this->t->set_var('edithandle','');
1010       $this->t->set_var('addhandle','');
1011       
1012       $this->t->pfp('out','form');
1013       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
1014       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1015       
1016       $this->save_sessiondata();
1017     }
1018
1019   function par_view()
1020     {
1021       $this->t->set_file(array('par_view_t' => 'par_view.tpl'));
1022       $this->t->set_block('par_view_t','header_list','list1');
1023       $this->t->set_block('par_view_t','elder_list','list2');
1024
1025       $sql = "SELECT * FROM eq_elder where valid=1";
1026       $this->db->query($sql,__LINE__,__FILE__);
1027       $i=0;
1028       while ($this->db->next_record())
1029         {
1030           $elder_name[$i] = $this->db->f('name');
1031           $elder_id[$i] = $this->db->f('elder');
1032           $i++;
1033         }
1034       array_multisort($elder_name, $elder_id);
1035
1036       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
1037       $this->db->query($sql,__LINE__,__FILE__);
1038       $total_records = $this->db->num_rows();
1039
1040       $i = 0;
1041       while ($this->db->next_record())
1042         {
1043           $activity_list[$i]['assignment'] = $this->db->f('assignment');
1044           $activity_list[$i]['date'] = $this->db->f('date');
1045           $activity_list[$i]['activity']  = $this->db->f('activity');
1046           $i++;
1047         }
1048
1049       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
1050       $this->db->query($sql,__LINE__,__FILE__);
1051       $i=0;
1052       while($this->db->next_record())
1053         {
1054           $assignment_list[$i]['assignment'] = $this->db->f('assignment');
1055           $assignment_list[$i]['name'] = $this->db->f('name');
1056           $assignment_list[$i]['code'] = $this->db->f('code');
1057           $i++;
1058         }
1059       
1060       $elder_width=300; $part_width=25; $assignment_width=50;
1061       $total_width=$elder_width+$part_width;
1062       for ($i=0; $i < count($assignment_list); $i++) {
1063         $this->t->set_var('assignment_name',$assignment_list[$i]['name']);
1064         $this->t->set_var('assignment_code',$assignment_list[$i]['code']);
1065         $this->t->fp('list1','header_list',True);
1066         $total_width += $assignment_width;
1067       }
1068
1069       for ($i=0; $i < count($elder_id); $i++) {
1070         $participated=0; $part_table = ''; 
1071         $this->nextmatchs->template_alternate_row_color(&$this->t);
1072         $this->t->set_var('elder_name',$elder_name[$i]);
1073         for ($j=0; $j < count($assignment_list); $j++) {
1074           $date = "0000-00-00"; $checkmark=0; $num_matches=0;
1075           for ($k=0; $k < count($activity_list); $k++) {
1076             if($assignment_list[$j]['assignment'] == $activity_list[$k]['assignment']) { 
1077               $sql = "SELECT * FROM eq_participation where "
1078                  . " activity=" . $activity_list[$k]['activity']
1079                  . " AND elder=" . $elder_id[$i];
1080               $this->db->query($sql,__LINE__,__FILE__);
1081               while($this->db->next_record()) {
1082                 if($activity_list[$k]['date'] > $date) { 
1083                   $date = $activity_list[$k]['date'];
1084                 }
1085                 $checkmark=1;
1086                 $num_matches++;
1087                 $participated++;
1088               }
1089             }
1090           }
1091           if($checkmark) {
1092             $part_table .= '<td align=center><img src="images/checkmark.gif">';
1093             $part_table .= '<font size=-2>'.$num_matches.'</font><br>';
1094             $part_table .= '<font size=-2>'.$date.'</font></td>';
1095           } else {
1096             $part_table .= '<td>&nbsp;</td>';
1097           }
1098         }
1099         if($participated) { $part_table .= '<td align=center><img src="images/checkmark.gif">'.$participated.'</td>'; }
1100         else { $part_table .= '<td>&nbsp;</td>'; }
1101         $this->t->set_var('part_table',$part_table);
1102         $this->t->fp('list2','elder_list',True);
1103       }
1104       $this->t->set_var('total_width',$total_width);
1105       $this->t->set_var('elder_width',$elder_width);
1106       $this->t->set_var('part_width',$part_width);
1107       $this->t->set_var('act_width',$act_width);
1108       $this->t->pfp('out','par_view_t');
1109       $this->save_sessiondata(); 
1110     }
1111
1112     function willing_view()
1113     {
1114       $this->t->set_file(array('willing_view_t' => 'willing_view.tpl'));
1115       $this->t->set_block('willing_view_t','header_list','list1');
1116       $this->t->set_block('willing_view_t','elder_list','list2');
1117
1118       $this->t->set_var('lang_filter','Filter');
1119       $this->t->set_var('lang_filter_unwilling','Filter out unwilling Elders:');
1120
1121       $filter_unwilling = get_var('filter_unwilling',array('POST'));
1122       $this->t->set_var('filter_unwilling',$filter_unwilling);
1123       
1124       if($filter_unwilling == 'y' || $filter_unwilling == '') { 
1125         $filter_input = "<input type=\"radio\" name=\"filter_unwilling\" value=\"y\" checked>Y";
1126         $filter_input.= "<input type=\"radio\" name=\"filter_unwilling\" value=\"n\">N";
1127         $filter_input.= "&nbsp;&nbsp;";
1128       } else {
1129         $filter_input = "<input type=\"radio\" name=\"filter_unwilling\" value=\"y\">Y";
1130         $filter_input.= "<input type=\"radio\" name=\"filter_unwilling\" value=\"n\" checked>N";
1131         $filter_input.= "&nbsp;&nbsp;";
1132       }
1133       $this->t->set_var('filter_input',$filter_input);
1134       
1135       $sql = "SELECT * FROM eq_elder where valid=1";
1136       $this->db->query($sql,__LINE__,__FILE__);
1137       $i=0;
1138       while ($this->db->next_record())
1139         {
1140           $elder_name[$i] = $this->db->f('name');
1141           $elder_id[$i] = $this->db->f('elder');
1142           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1143           $i++;
1144         }
1145       array_multisort($elder_name, $elder_id);
1146
1147       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
1148       $this->db->query($sql,__LINE__,__FILE__);
1149       $i=0;
1150       while($this->db->next_record())
1151         {
1152           $assignment_list[$i]['assignment'] = $this->db->f('assignment');
1153           $assignment_list[$i]['name'] = $this->db->f('name');
1154           $assignment_list[$i]['code'] = $this->db->f('code');
1155           $i++;
1156         }
1157
1158       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
1159       $this->db->query($sql,__LINE__,__FILE__);
1160       $total_records = $this->db->num_rows();
1161
1162       $i = 0;
1163       while ($this->db->next_record())
1164         {
1165           $activity_list[$i]['assignment'] = $this->db->f('assignment');
1166           $activity_list[$i]['date'] = $this->db->f('date');
1167           $activity_list[$i]['activity']  = $this->db->f('activity');
1168           $i++;
1169         }
1170       
1171       $elder_width=275; $willing_width=40; $assignment_width=50;
1172       $total_width=$elder_width+$willing_width;
1173       
1174       for ($i=0; $i < count($assignment_list); $i++) {
1175         $this->t->set_var('assignment_name',$assignment_list[$i]['name']);
1176         $this->t->set_var('assignment_code',$assignment_list[$i]['code']);
1177         $this->t->fp('list1','header_list',True);
1178         $total_width += $assignment_width;
1179         $total_willing[$i] = 0;
1180       }
1181
1182       for ($i=0; $i < count($elder_id); $i++) {
1183         $willing_table = ''; $elder_willing=0;
1184         $this->t->set_var('elder_name',$elder_name[$i]);
1185         $this->t->set_var('elder_phone',$elder_phone[$elder_id[$i]]);
1186         $this->t->set_var('editurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_update&elder_id='
1187                                                             . $elder_id[$i] . '&action=' . 'edit'));
1188         for ($j=0; $j < count($assignment_list); $j++) {
1189           $found_willingness=0; 
1190           $sql = "SELECT * FROM eq_willingness where "
1191              . " assignment=" . $assignment_list[$j]['assignment']
1192              . " AND elder=" . $elder_id[$i];
1193           $this->db->query($sql,__LINE__,__FILE__);
1194           while($this->db->next_record()) {
1195             $found_willingness=1;
1196             $date_part="";
1197             $sql = "SELECT * FROM eq_activity where "
1198                . " assignment=". $assignment_list[$j]['assignment']
1199                . " ORDER by date DESC";
1200             $this->db2->query($sql,__LINE__,__FILE__);
1201             if($this->db2->next_record()) {
1202               $activity = $this->db2->f('activity');
1203               $date = $this->db2->f('date');
1204               $sql = "SELECT * FROM eq_participation where "
1205                  . " activity=" . $activity
1206                  . " AND elder=". $elder_id[$i];
1207               $this->db3->query($sql,__LINE__,__FILE__);
1208               if($this->db3->next_record()) {
1209                 $date_part = $date;
1210               } 
1211             }
1212               
1213             if($this->db->f('willing') == 'y') {
1214               $total_willing[$j]++;
1215               $elder_willing=1;
1216               $willing_table .= '<td align=center><img src="images/checkmark.gif"><br><font size=-2>'.$date_part.'</font></td></td>';
1217             }
1218             else if($this->db->f('willing') == 'n') {
1219               $willing_table .= '<td align=center><img src="images/x.gif"></td>';
1220             }
1221             else {
1222               $elder_willing=1;
1223               $willing_table .= "<td>&nbsp;</td>";
1224             }
1225           }
1226           if(!$found_willingness) {
1227             $elder_willing=1;
1228             $willing_table .= "<td>&nbsp;</td>";
1229           }
1230         }
1231         if(($elder_willing == 1) || ($filter_unwilling == 'n')) { 
1232           $this->t->set_var('willing_table',$willing_table);
1233           $this->t->fp('list2','elder_list',True);
1234           $this->nextmatchs->template_alternate_row_color(&$this->t);
1235         } 
1236       }
1237
1238       $stat_table = '<td><b>Total Willing to Serve</b></td>';
1239       for ($j=0; $j < count($assignment_list); $j++) {
1240         $stat_table .= "<td align=center><b>".$total_willing[$j]."</b></td>";
1241       }
1242       $this->t->set_var('stat_table',$stat_table);
1243       
1244       $this->t->set_var('total_width',$total_width);
1245       $this->t->set_var('elder_width',$elder_width);
1246       $this->t->set_var('willing_width',$willing_width);
1247       $this->t->pfp('out','willing_view_t');
1248       $this->save_sessiondata(); 
1249     }
1250     
1251   function willing_update()
1252     {
1253       //print "<font color=red>Willingness Update Under Constrcution</font>";
1254       //$this->willing_view();
1255       //return false;
1256       
1257       $this->t->set_file(array('willing_update_t' => 'willing_update.tpl'));
1258       $this->t->set_block('willing_update_t','assignment_list','list');
1259       $this->t->set_block('willing_update_t','save','savehandle');
1260       
1261       $elder_id = get_var('elder_id',array('GET','POST'));
1262       $this->t->set_var('elder_id',$elder_id);
1263       $action = get_var('action',array('GET','POST'));
1264       
1265       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_view'));
1266       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_update&action=save'));
1267       $this->t->set_var('lang_done','Cancel');
1268       $this->t->set_var('title','Willingness Update ');
1269       
1270       if($action == 'save')
1271         {
1272           // Delete all the previous willingness entries for this elder
1273           $this->db->query("DELETE from eq_willingness where elder=" . $elder_id ,__LINE__,__FILE__);
1274               
1275           // Now, add the assignment willingness that is checked for this elder
1276           $new_data = get_var('willingness',array('POST'));
1277           foreach ($new_data as $data)
1278             {
1279               $data_array = explode("/",$data);
1280               $assignment = $data_array[0];
1281               $willing = $data_array[1];
1282               //print "elder_id: $elder_id assignment: $assignment willing: $willing<br>";
1283               $this->db->query("INSERT INTO eq_willingness (elder,assignment,willing) "
1284                                . "VALUES (" . $elder_id .",". $assignment .",'". $willing . "')",__LINE__,__FILE__);
1285             }      
1286           $this->willing_view();
1287           return false;
1288         }
1289       
1290       $assignment_width=300; $willing_width=25; $table_width=$assignment_width + $willing_width;
1291       $table_data=""; 
1292
1293       // Find out the elder's name
1294       $sql = "SELECT * FROM eq_elder WHERE elder=".$elder_id." AND valid=1";
1295       $this->db->query($sql,__LINE__,__FILE__);
1296       if($this->db->next_record()) {
1297         $elder_name = $this->db->f('name');
1298         $this->t->set_var('elder_name',$elder_name);
1299       }
1300       
1301       // Select all the assignments
1302       $sql = "SELECT * FROM eq_assignment ORDER by name ASC";
1303       $this->db->query($sql,__LINE__,__FILE__);
1304       
1305       while ($this->db->next_record())
1306         {
1307           $assignment = $this->db->f('assignment');
1308           $assignment_name = $this->db->f('name');
1309           $assignment_code = $this->db->f('code');
1310
1311           $this->nextmatchs->template_alternate_row_color(&$this->t);
1312           $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$assignment_name</td>";
1313           
1314           $header_row="<th width=$comp_width><font size=-2>Assignments</th><th>Willingness</th>";
1315           $sql = "SELECT * FROM eq_willingness WHERE elder=".$elder_id." AND assignment=".$assignment;
1316           $this->db2->query($sql,__LINE__,__FILE__);
1317           $value = $assignment;
1318              
1319           if($this->db2->next_record()) {
1320               if($this->db2->f('willing') == 'y') {
1321                 $table_data .= '<td width=100 align=center>';
1322                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y" checked>Y';
1323                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1324                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/"> ';
1325                 $table_data .= '</td>';
1326               } else if($this->db2->f('willing') == 'n') {
1327                 $table_data .= '<td width=100 align=center>';
1328                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1329                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n" checked>N';
1330                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/">';
1331                 $table_data .= '</td>';
1332               } else {
1333                 $table_data .= '<td width=100 align=center>';
1334                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1335                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1336                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/" checked> ';
1337                 $table_data .= '</td>';
1338               }
1339             }
1340           else {
1341             $table_data .= '<td width=100 align=center>';
1342             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1343             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1344             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/" checked> ';
1345             $table_data .= '</td>';
1346           }
1347           
1348           $table_data .= "\n";
1349           $table_data .= "</tr>"; 
1350           $table_data .= "<tr><td colspan=20></td></tr>";
1351         }
1352       
1353       $table_data .= "<tr><td colspan=20><hr></td></tr>";
1354       
1355       $this->t->set_var('table_width',$table_width);
1356       $this->t->set_var('header_row',$header_row);
1357       $this->t->set_var('table_data',$table_data);
1358       $this->t->fp('list','assignment_list',True);
1359
1360       $this->t->set_var('lang_reset','Clear Form');
1361       $this->t->set_var('lang_save','Save Changes');
1362       $this->t->set_var('savehandle','');
1363       
1364       $this->t->pfp('out','willing_update_t');
1365       $this->t->pfp('addhandle','save');
1366       
1367       $this->save_sessiondata();
1368     }
1369     
1370     
1371     function ppi_sched()
1372       {
1373       $this->t->set_file(array('ppi_sched_t' => 'ppi_sched.tpl'));
1374       $this->t->set_block('ppi_sched_t','elder_list','elderlist');
1375       $this->t->set_block('ppi_sched_t','appt_list','apptlist');
1376       $action = get_var('action',array('GET','POST'));
1377
1378       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1379       $this->t->set_var('lang_reset','Clear Changes');
1380       
1381       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1382       $this->t->set_var('ppi_link_title','Yearly PPIs');
1383       
1384       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1385       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1386
1387       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched&action=save'));
1388       $this->t->set_var('title','Yearly PPI Scheduler');
1389
1390       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
1391       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
1392       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1393       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1394       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1395       $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
1396       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1397       $table_data=""; $completed_data=""; $totals_data="";
1398
1399       $year = date('Y');
1400       
1401       if($action == 'save')
1402         {
1403           // Save any changes made to the appointment table
1404           $new_data = get_var('appt_notes',array('POST'));
1405           if($new_data != "") { 
1406             foreach ($new_data as $entry)
1407               {
1408                 $elder = $entry['elder'];
1409                 $appointment = $entry['appointment'];
1410                 
1411                 //print "elder: $elder appointment: $appointment <br>";
1412                 
1413                 //Only perform a database update if we have made a change to this appointment
1414                 $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and elder='$elder'";
1415                 $this->db->query($sql,__LINE__,__FILE__);
1416                 if(!$this->db->next_record()) {
1417                   // Perform database save actions here
1418                   $this->db->query("UPDATE eq_appointment set " .
1419                                    " elder='" . $elder . "'" .
1420                                    " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1421                   // Email the appointment
1422                   $this->email_appt($appointment);
1423                 }
1424                 
1425               }
1426           }
1427           
1428           // Save any changes made to the ppi notes table
1429           $new_data = get_var('ppi_notes',array('POST'));
1430           foreach ($new_data as $entry)
1431            {
1432              $ppi_notes = $entry['notes'];
1433              $elder_id = $entry['elder_id'];
1434              $ppi_pri = $entry['pri'];
1435              
1436              // Perform database save actions here
1437              $this->db->query("UPDATE eq_elder set " .
1438                               " ppi_notes='" . $ppi_notes . "'" .
1439                               ",ppi_pri='" . $ppi_pri . "'" .
1440                               " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1441              
1442            }
1443
1444           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched');
1445           //Header('Location: ' . $take_me_to_url);
1446         }
1447
1448       // Get the EQ President
1449       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
1450       $this->db->query($sql,__LINE__,__FILE__);
1451       if($this->db->next_record()) {
1452         $president_name = $this->db->f('name');
1453         $president_id = $this->db->f('elder');
1454         $presidency_id = $this->db->f('presidency');
1455         $interviewer = $this->db->f('elder');
1456         $district_number = '*';
1457         $district_name = $president_name;
1458       } else {
1459         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
1460         return;
1461       }
1462       
1463       // create the elder id -> elder name mapping
1464       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1465       $this->db->query($sql,__LINE__,__FILE__);
1466       $i=0;
1467       $elder_id = NULL;
1468       $elder_name = NULL;
1469       while ($this->db->next_record())
1470         {
1471           $elder_name[$i] = $this->db->f('name');
1472           $elder_id[$i] = $this->db->f('elder');
1473           $i++;
1474         }
1475       array_multisort($elder_name, $elder_id);
1476
1477       // APPOINTMENT TABLE
1478       $date_width=150; $time_width=100; $elder_width=200;
1479       $appt_table_width=$date_width + $time_width + $elder_width;
1480       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1481       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1482       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1483       $appt_table_data = ""; 
1484
1485       $total_elders=0; $elders_with_yearly_ppi=0;
1486       
1487       // Display a scheduling table for the EQ President
1488       $table_data=""; $appt_table_data="";
1489       $table_title = "District ".$district_number.": ".$district_name.": All Elders with Yearly PPI Not Completed";
1490       $appt_table_title = "District ".$district_number.": ".$district_name.": Yearly PPI Appointment Slots";
1491       $this->t->set_var('table_title',$table_title);
1492       $this->t->set_var('appt_table_title',$appt_table_title);
1493       
1494       // query the database for all the appointments
1495       $sql = "SELECT * FROM eq_appointment where presidency=".$presidency_id." and date>=CURDATE() ORDER BY date ASC, time ASC";
1496       $this->db->query($sql,__LINE__,__FILE__);
1497         
1498       while ($this->db->next_record())
1499         {
1500           $appointment = $this->db->f('appointment');
1501           $elder = $this->db->f('elder');
1502           
1503           $date = $this->db->f('date');
1504           $date_array = explode("-",$date);
1505           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1506           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1507           
1508           $time = $this->db->f('time');
1509           $time_array = explode(":",$time);
1510           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1511           
1512           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1513           $appt_table_data.= "<td align=center>$day_string</td>";
1514           $appt_table_data.= "<td align=center>$time_string</td>";
1515
1516           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1517           $appt_table_data.= '<option value=0></option>';
1518           for ($i=0; $i < count($elder_id); $i++) {
1519             $id = $elder_id[$i];
1520             $name = $elder_name[$i];
1521             if($elder_id[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1522             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1523           }
1524           $appt_table_data.='</select></td>';
1525
1526           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1527           
1528           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1529           $this->t->set_var('tr_color',$tr_color);
1530         }
1531
1532       $this->t->set_var('appt_table_data',$appt_table_data);
1533       $this->t->set_var('appt_header_row',$appt_header_row);
1534       $this->t->set_var('appt_table_width',$appt_table_width);
1535
1536       // PPI SCHEDULING TABLE
1537       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY ppi_pri ASC";
1538       $this->db->query($sql,__LINE__,__FILE__);
1539       
1540       $i=0; 
1541       $elder_id = NULL;
1542       while ($this->db->next_record())
1543         {
1544           $elder_id[$i] = $this->db->f('elder');
1545           $elder_name[$i] = $this->db->f('name');
1546           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1547           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1548           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1549           $i++;
1550           $total_elders++;
1551         }
1552
1553       $max = count($elder_id);
1554       
1555       for($i=0; $i < $max; $i++) {
1556           $id = $elder_id[$i];
1557           $name = $elder_name[$i];
1558           $phone = $elder_phone[$id];
1559           $ppi_pri = $elder_ppi_pri[$id];
1560           $ppi_notes = $elder_ppi_notes[$id];
1561
1562           // If this elder has had a yearly PPI this year, don't show him on the schedule list
1563           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1564           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1565              "AND elder=" . $id;
1566           $this->db2->query($sql,__LINE__,__FILE__);
1567           
1568           if(!$this->db2->next_record()) {
1569             $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1570             $this->db->query($sql,__LINE__,__FILE__);
1571             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1572             $link_data['menuaction'] = 'eq.eq.ppi_update';
1573             $link_data['elder'] = $id;
1574             $link_data['name'] = $name;
1575             $link_data['ppi'] = '';
1576             $link_data['eqpresppi'] = 1;
1577             $link_data['action'] = 'add';
1578             $link_data['interviewer'] = $interviewer;
1579             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1580             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1581             $this->t->set_var('tr_color',$tr_color);
1582             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1583             $table_data.= "<td align=center>$phone</td>";
1584             //$table_data.= "<td align=center>$ppi_pri</td>";
1585             $table_data.= "<td align=center>";
1586             $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
1587             foreach(range(0,6) as $num) {
1588               if($num == 0) { $num = 1; } else {$num = $num*5; }
1589               if($ppi_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1590               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1591             }
1592             $table_data.= '</select></td>';
1593             $table_data.= "<td align=center>$date</td>";
1594             $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
1595             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_id]" value="'.$id.'">';
1596             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_name]" value="'.$name.'">';
1597             $table_data.= '</td>';
1598             $table_data.= '</tr>';
1599           } else {
1600             $link_data['menuaction'] = 'eq.eq.ppi_update';
1601             $link_data['interviewer'] = $this->db2->f('interviewer');
1602             $link_data['elder'] = $this->db2->f('elder');
1603             $link_data['name'] = $name;
1604             $link_data['ppi'] = $this->db2->f('ppi');
1605             $link_data['eqpresppi'] = $this->db2->f('eqpresppi');
1606             $link_data['action'] = 'view';
1607             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1608             $elders_with_yearly_ppi++;
1609             $date = $this->db2->f('date');
1610             $ppi_notes = $this->db2->f('notes');
1611             if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
1612             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1613             $this->t->set_var('tr_color2',$tr_color2);
1614             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1615             $completed_data.= "<td align=center>$phone</td>";
1616             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1617             $completed_data.= "<td align=left>$ppi_notes</td>";
1618             $completed_data.= '</tr>';
1619           }
1620       } // End for Elders Loop
1621
1622       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1623       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1624       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1625       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1626       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1627       $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1628             
1629       $this->t->set_var('table_width',$table_width);
1630       $this->t->set_var('header_row',$header_row);
1631       $this->t->set_var('table_data',$table_data);
1632       $this->t->set_var('completed_header_row',$completed_header_row);
1633       $this->t->set_var('completed_table_width',$completed_table_width);
1634       $this->t->set_var('completed',$completed_data);
1635       $this->t->fp('elderlist','elder_list',True); 
1636       
1637       $elders_width=300; $totals_width=100;
1638       $totals_table_width=$elders_width + $totals_width;
1639       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1640       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1641       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1642       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
1643       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
1644       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
1645       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1646       $this->t->set_var('tr_color',$tr_color);
1647       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1648       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1649       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1650       $totals_data.= "</tr>";
1651
1652       $this->t->set_var('totals',$totals_data);
1653       $this->t->set_var('totals_header_row',$totals_header_row);
1654       $this->t->set_var('totals_table_width',$totals_table_width);
1655       
1656       $this->t->pfp('out','ppi_sched_t');
1657       $this->save_sessiondata(); 
1658       
1659     }
1660   
1661   function int_sched()
1662     {
1663       $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1664       $this->t->set_block('int_sched_t','elder_list','elderlist');
1665       $this->t->set_block('int_sched_t','appt_list','apptlist');
1666       $action = get_var('action',array('GET','POST'));
1667
1668       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1669       $this->t->set_var('lang_reset','Clear Changes');
1670       
1671       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
1672       $this->t->set_var('int_link_title','Hometeaching Interviews');
1673       
1674       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
1675       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
1676
1677       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched&action=save'));
1678       $this->t->set_var('title','Hometeaching Interviews Scheduler');
1679
1680       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
1681       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $int_date_width;
1682       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1683       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1684       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1685       $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
1686       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1687       $table_data=""; $completed_data=""; $totals_data="";
1688
1689       $year = date('Y');
1690       $month = date('m');
1691       $nextyear = $year + 1;
1692       if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
1693       if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
1694       if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
1695       if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
1696       //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
1697       
1698       if($action == 'save')
1699         {
1700           // Save any changes made to the appointment table
1701           $new_data = get_var('appt_notes',array('POST'));
1702           if($new_data != "") { 
1703             foreach ($new_data as $entry)
1704               {
1705                 $elder = $entry['elder'];
1706                 $appointment = $entry['appointment'];
1707                 
1708                 //print "elder: $elder appointment: $appointment <br>";
1709                 //Only perform a database update if we have made a change to this appointment
1710                 $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and elder='$elder'";
1711                 $this->db->query($sql,__LINE__,__FILE__);
1712                 if(!$this->db->next_record()) {
1713                   // Perform database save actions here
1714                   $this->db->query("UPDATE eq_appointment set " .
1715                                    " elder='" . $elder . "'" .
1716                                    " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1717                   
1718                   // Email the appointment
1719                   $this->email_appt($appointment);
1720                 }
1721               }
1722           }
1723           
1724           // Save any changes made to the int notes table
1725           $new_data = get_var('int_notes',array('POST'));
1726           foreach ($new_data as $entry)
1727            {
1728              $int_notes = $entry['notes'];
1729              $elder_id = $entry['elder_id'];
1730              $elder_name = $entry['elder_name'];
1731              $int_pri = $entry['pri'];
1732              $aaronic = $entry['aaronic'];
1733              //print "int_notes: $int_notes elder_name: $elder_name aaronic: $aaronic <Br>";
1734              if($aaronic == 0) { 
1735                // Perform database save actions here
1736                $this->db->query("UPDATE eq_elder set " .
1737                                 " int_notes='" . $int_notes . "'" .
1738                                 ",int_pri='" . $int_pri . "'" .
1739                                 " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1740              } 
1741              
1742            }
1743
1744           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched');
1745           //Header('Location: ' . $take_me_to_url);
1746         }
1747
1748       // Get the Districts
1749       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1750       $this->db->query($sql,__LINE__,__FILE__);
1751       $i=0;
1752       while ($this->db->next_record())
1753         {
1754           $district = $this->db->f('district');
1755           $districts[$i]['district'] = $this->db->f('district');
1756           $districts[$i]['name'] = $this->db->f('name');
1757           $districts[$i]['supervisor'] = $this->db->f('supervisor');
1758           $sql = "SELECT * FROM eq_presidency where district=$district and valid=1";
1759           $this->db2->query($sql,__LINE__,__FILE__);
1760           if($this->db2->next_record()) {
1761             $districts[$i]['presidency'] = $this->db2->f('presidency');
1762           }
1763           $i++;
1764         }
1765       
1766       // create the elder id -> elder name mapping
1767       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1768       $this->db->query($sql,__LINE__,__FILE__);
1769       $i=0;
1770       $elder_id_data = NULL;
1771       $elder_name_data = NULL;
1772       while ($this->db->next_record())
1773         {
1774           $elder_name_data[$i] = $this->db->f('name');
1775           $elder_id_data[$i] = $this->db->f('elder');
1776           $i++;
1777         }
1778       array_multisort($elder_name_data, $elder_id_data);
1779
1780       // APPOINTMENT TABLE
1781       $district = 1;
1782       $date_width=150; $time_width=100; $elder_width=200;
1783       $appt_table_width=$date_width + $time_width + $elder_width;
1784       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1785       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1786       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1787       $appt_table_data = ""; 
1788
1789       $total_comps=0; $comps_with_quarterly_int=0;
1790       
1791       // Display a scheduling table for each district
1792       for ($d=0; $d < count($districts); $d++) {
1793       $table_data=""; $appt_table_data="";
1794       $this->t->set_var('district_number',$districts[$d]['district']);
1795       $this->t->set_var('district_name',$districts[$d]['name']);        
1796       $supervisor = $districts[$d]['supervisor'];
1797       $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Elders with Interviews Not Completed";
1798       $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
1799       $this->t->set_var('table_title',$table_title);
1800       $this->t->set_var('appt_table_title',$appt_table_title);
1801       
1802       // query the database for all the appointments
1803       $sql = "SELECT * FROM eq_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1804       $this->db->query($sql,__LINE__,__FILE__);
1805         
1806       while ($this->db->next_record())
1807         {
1808           $appointment = $this->db->f('appointment');
1809           $elder = $this->db->f('elder');
1810           
1811           $date = $this->db->f('date');
1812           $date_array = explode("-",$date);
1813           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1814           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1815           
1816           $time = $this->db->f('time');
1817           $time_array = explode(":",$time);
1818           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1819           
1820           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1821           $appt_table_data.= "<td align=center>$day_string</td>";
1822           $appt_table_data.= "<td align=center>$time_string</td>";
1823
1824           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1825           $appt_table_data.= '<option value=0></option>';
1826           for ($i=0; $i < count($elder_id_data); $i++) {
1827             $id = $elder_id_data[$i];
1828             $name = $elder_name_data[$i];
1829             if($elder_id_data[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1830             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1831           }
1832           $appt_table_data.='</select></td>';
1833
1834           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1835           
1836           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1837           $this->t->set_var('tr_color',$tr_color);
1838         }
1839
1840       $this->t->set_var('appt_table_data',$appt_table_data);
1841       $this->t->set_var('appt_header_row',$appt_header_row);
1842       $this->t->set_var('appt_table_width',$appt_table_width);
1843
1844       // INTERVIEW SCHEDULING TABLE
1845       
1846       // Select all the unique companionship numbers for this district
1847       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$d]['district'];
1848       $this->db->query($sql,__LINE__,__FILE__);
1849       $j=0;
1850       while ($this->db->next_record())
1851         {
1852           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1853           $j++;
1854         }
1855
1856       $i=0;
1857       for ($j=0; $j < count($unique_companionships); $j++) {
1858         // Select all the companions from each companionship
1859         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
1860            "companionship=". $unique_companionships[$j]['companionship'];
1861         $this->db->query($sql,__LINE__,__FILE__);
1862         $k=0; $int_completed=0;
1863         $comp = $unique_companionships[$j]['companionship'];
1864         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1865         $this->t->set_var('tr_color',$tr_color);
1866         $total_comps++;
1867         while ($this->db->next_record())
1868             {         
1869               // Get this companions information
1870               $elder_id = $this->db->f('elder');
1871               $aaronic_id = $this->db->f('aaronic');
1872              
1873               $sql = "SELECT * FROM eq_elder where elder=$elder_id";
1874               $this->db2->query($sql,__LINE__,__FILE__);        
1875               if($this->db2->next_record())
1876                 {
1877                   $elder_id = $this->db2->f('elder');
1878                   $elder_name = $this->db2->f('name');
1879                   $elder_phone[$elder_id] = $this->db2->f('phone');
1880                   $elder_int_pri[$elder_id] = $this->db2->f('int_pri');
1881                   $elder_int_notes[$elder_id] = $this->db2->f('int_notes');
1882                   $elder_aaronic = 0;
1883                 }
1884               else {
1885                 $sql = "SELECT * FROM eq_aaronic where aaronic=$aaronic_id";
1886                 $this->db2->query($sql,__LINE__,__FILE__);
1887                 if($this->db2->next_record())
1888                   {
1889                     $elder_id = $this->db2->f('aaronic');
1890                     $elder_name = $this->db2->f('name');
1891                     $elder_phone[$elder_id] = $this->db2->f('phone');
1892                     $elder_aaronic = 1;
1893                   }
1894               }
1895
1896               $id = $elder_id;
1897               $name = $elder_name;
1898               $phone = $elder_phone[$id];
1899               $int_pri = $elder_int_pri[$id];
1900               $int_notes = $elder_int_notes[$id];
1901               $aaronic = $elder_aaronic;
1902               
1903               // If the companionship has already had its quarterly interview,
1904               // Skip the other companion in the companionship.
1905               if($int_completed == 1) {
1906                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1907                 $completed_data.= "<td align=center>$phone</td>";
1908                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1909                 $completed_data.= "<td align=left>$int_notes</td>";
1910                 $completed_data.= '</tr>';
1911                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1912                 $this->t->set_var('tr_color2',$tr_color2);
1913                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1914                 $this->t->set_var('tr_color',$tr_color);
1915                 continue;
1916               }
1917               
1918               // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
1919               $sql = "SELECT * FROM eq_ppi WHERE date >= '$quarter_start' AND date < '$quarter_end' ".
1920                  "AND elder=" . $id;
1921               $this->db2->query($sql,__LINE__,__FILE__);
1922               
1923               if(!$this->db2->next_record()) {
1924                 $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1925                 $this->db3->query($sql,__LINE__,__FILE__);
1926                 if($this->db3->next_record()) { $date = $this->db3->f('date'); } else { $date = ""; }
1927                 $link_data['menuaction'] = 'eq.eq.int_update';
1928                 $link_data['elder'] = $id;
1929                 $link_data['aaronic'] = 0;
1930                 $link_data['name'] = $name;
1931                 $link_data['ppi'] = '';
1932                 $link_data['action'] = 'add';
1933                 $link_data['interviewer'] = $districts[$d]['supervisor'];
1934                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1935                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1936                 $table_data.= "<td align=center>$phone</td>";
1937                 $table_data.= "<td align=center>";
1938                 if($aaronic == 0) { 
1939                   $table_data.= '<select name=int_notes['.$i.'][pri]>';
1940                   foreach(range(0,6) as $num) {
1941                     if($num == 0) { $num = 1; } else {$num = $num*5; }
1942                     if($int_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1943                     $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1944                   }
1945                   $table_data.= '</select></td>';
1946                 }
1947                 $table_data.= "<td align=center>$date</td>";
1948                 if($aaronic == 0) { 
1949                   $table_data.= '<td><input type=text size="50" maxlength="128" name="int_notes['.$i.'][notes]" value="'.$int_notes.'">';
1950                 }
1951                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_id]" value="'.$id.'">';
1952                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_name]" value="'.$name.'">';
1953                 $table_data.= '<input type=hidden name="int_notes['.$i.'][aaronic]" value="'.$aaronic.'">';
1954                 $table_data.= '</td>';
1955                 $table_data.= '</tr>'."\n";
1956                 $i++;
1957               } else {
1958                 $link_data['menuaction'] = 'eq.eq.int_update';
1959                 $link_data['interviewer'] = $this->db2->f('interviewer');
1960                 $link_data['elder'] = $this->db2->f('elder');
1961                 $link_data['aaronic'] = $this->db2->f('aaronic');
1962                 $link_data['name'] = $name;
1963                 $link_data['ppi'] = $this->db2->f('ppi');
1964                 $link_data['action'] = 'view';
1965                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1966                 $comps_with_quarterly_int++;
1967                 $int_completed=1;
1968                 $date = $this->db2->f('date');
1969                 $int_notes = $this->db2->f('notes');
1970                 if(strlen($int_notes) > 40) { $int_notes = substr($int_notes,0,40) . "..."; }
1971                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1972                 $completed_data.= "<td align=center>$phone</td>";
1973                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1974                 $completed_data.= "<td align=left>$int_notes</td>";
1975                 $completed_data.= '</tr>';
1976               }
1977             }
1978       }
1979         
1980       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1981       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1982       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1983       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1984       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1985       $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
1986             
1987       $this->t->set_var('table_width',$table_width);
1988       $this->t->set_var('header_row',$header_row);
1989       $this->t->set_var('table_data',$table_data);
1990       $this->t->set_var('completed_header_row',$completed_header_row);
1991       $this->t->set_var('completed_table_width',$completed_table_width);
1992       $this->t->set_var('completed',$completed_data);
1993       $this->t->fp('elderlist','elder_list',True);
1994
1995       } // End for each district loop
1996
1997       
1998       $elders_width=300; $totals_width=100;
1999       $totals_table_width=$elders_width + $totals_width;
2000       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
2001       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2002       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2003       $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
2004       $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
2005       $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
2006       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2007       $this->t->set_var('tr_color',$tr_color);
2008       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2009       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2010       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2011       $totals_data.= "</tr>";
2012
2013       $this->t->set_var('totals',$totals_data);
2014       $this->t->set_var('totals_header_row',$totals_header_row);
2015       $this->t->set_var('totals_table_width',$totals_table_width);
2016       
2017       $this->t->pfp('out','int_sched_t');
2018       $this->save_sessiondata(); 
2019       
2020     }
2021   
2022   function vis_sched()
2023     {
2024       $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
2025       $this->t->set_block('vis_sched_t','family_list','familylist');
2026       $this->t->set_block('vis_sched_t','appt_list','apptlist');
2027       $action = get_var('action',array('GET','POST'));
2028
2029       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
2030       $this->t->set_var('lang_reset','Clear Changes');
2031       
2032       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2033       $this->t->set_var('vis_link_title','View Yearly Visits');
2034       
2035       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2036       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2037
2038       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched&action=save'));
2039       $this->t->set_var('title','EQ Presidency Yearly Visit Scheduler');
2040
2041       $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
2042       $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
2043       $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
2044       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
2045       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
2046       $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
2047       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2048       $table_data=""; $completed_data=""; $totals_data="";
2049
2050       $year = date('Y');
2051       
2052       if($action == 'save')
2053         {
2054           // Save any changes made to the appointment table
2055           $new_data = get_var('appt_notes',array('POST'));
2056           if($new_data != "") { 
2057             foreach ($new_data as $entry)
2058               {
2059                 $family = $entry['family'];
2060                 $appointment = $entry['appointment'];
2061                 
2062                 //Only perform a database update if we have made a change to this appointment
2063                 $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and family='$family'";
2064                 $this->db->query($sql,__LINE__,__FILE__);
2065                 if(!$this->db->next_record()) {
2066                   
2067                   // Perform database save actions here
2068                   $this->db->query("UPDATE eq_appointment set " .
2069                                    " family='" . $family . "'" .
2070                                    " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2071                   
2072                   // Email the appointment
2073                   $this->email_appt($appointment);
2074                 }
2075               }
2076           }
2077           
2078           // Save any changes made to the visit notes table
2079           $new_data = get_var('vis_notes',array('POST'));
2080           foreach ($new_data as $entry)
2081            {
2082              $visit_notes = $entry['notes'];
2083              $family_id = $entry['family_id'];
2084              $visit_pri = $entry['pri'];
2085              
2086              // Perform database save actions here
2087              $this->db->query("UPDATE eq_family set " .
2088                               " visit_notes='" . $visit_notes . "'" .
2089                               ",visit_pri='" . $visit_pri . "'" .
2090                               " WHERE family=" . $family_id,__LINE__,__FILE__);
2091              
2092            }
2093
2094           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched');
2095           //Header('Location: ' . $take_me_to_url);
2096         }
2097
2098       // APPOINTMENT TABLE
2099       $date_width=150; $time_width=100; $family_width=250;
2100       $appt_table_width=$date_width + $time_width + $family_width;
2101       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2102       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2103       $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
2104       $appt_table_data = ""; 
2105
2106       // Find out what the EQ Presidency ID is
2107       $sql = "SELECT * FROM eq_presidency where eqpres=1 and valid=1";
2108       $this->db->query($sql,__LINE__,__FILE__);
2109       if($this->db->next_record()) {
2110         $presidency_name = $this->db->f('name');
2111         $presidency_id = $this->db->f('presidency');
2112       } else {
2113         print "<hr><font color=red><h3>-E- Unable to locate EQ Presidency in eq_presidency table</h3></font></hr>";
2114         return;
2115       }
2116       
2117       // create the family id -> family name mapping
2118       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
2119       $this->db->query($sql,__LINE__,__FILE__);
2120       $i=0;
2121       $family_id = NULL;
2122       while ($this->db->next_record())
2123         {
2124           $family_id[$i] = $this->db->f('family');
2125           $family_name[$i] = $this->db->f('name');
2126           $i++;
2127         }
2128       array_multisort($family_name, $family_id);
2129       
2130       // query the database for all the appointments
2131       $sql = "SELECT * FROM eq_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
2132       $this->db->query($sql,__LINE__,__FILE__);
2133
2134       while ($this->db->next_record())
2135         {
2136           $appointment = $this->db->f('appointment');
2137           $family = $this->db->f('family');
2138
2139           $date = $this->db->f('date');
2140           $date_array = explode("-",$date);
2141           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2142           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2143           
2144           $time = $this->db->f('time');
2145           $time_array = explode(":",$time);
2146           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2147           
2148           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2149           $appt_table_data.= "<td align=center>$day_string</td>";
2150           $appt_table_data.= "<td align=center>$time_string</td>";
2151
2152           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
2153           $appt_table_data.= '<option value=0></option>';
2154           for ($i=0; $i < count($family_id); $i++) {
2155             $id = $family_id[$i];
2156             $name = $family_name[$i];
2157             if($family_id[$i] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
2158             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
2159           }
2160           $appt_table_data.='</select></td>';
2161
2162           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2163           
2164           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2165           $this->t->set_var('tr_color',$tr_color);
2166         }
2167
2168       $this->t->set_var('appt_table_data',$appt_table_data);
2169       $this->t->set_var('appt_header_row',$appt_header_row);
2170       $this->t->set_var('appt_table_width',$appt_table_width);
2171
2172       
2173       // VISIT SCHEDULING TABLE
2174       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY visit_pri ASC";
2175       $this->db->query($sql,__LINE__,__FILE__);
2176
2177       $total_families=0; $families_with_yearly_visit=0;
2178       
2179       $i=0; 
2180       $family_id = NULL;
2181       $family_name = NULL;
2182       $family_phone = NULL;
2183       $family_visit_pri = NULL;
2184       $family_visit_notes = NULL;
2185       while ($this->db->next_record())
2186         {
2187           $family_id[$i] = $this->db->f('family');
2188           $family_name[$i] = $this->db->f('name');
2189           $family_phone[$family_id[$i]] = $family_id[$i] . " ERROR";
2190           $family_visit_pri[$family_id[$i]] = $this->db->f('visit_pri');
2191           $family_visit_notes[$family_id[$i]] = $this->db->f('visit_notes');
2192           $i++;
2193           $total_families++;
2194         }
2195
2196       $sql = "SELECT * FROM eq_parent where valid=1";
2197       $this->db->query($sql,__LINE__,__FILE__);
2198       while ($this->db->next_record())
2199         {
2200           $family = $this->db->f('family');
2201           $phone = $this->db->f('phone');
2202           $family_phone[$family] = $phone;
2203         }
2204       
2205       $max = count($family_id);
2206       
2207       for($i=0; $i < $max; $i++) {
2208           $id = $family_id[$i];
2209           $name = $family_name[$i];
2210           $phone = $family_phone[$id];
2211           $vis_pri = $family_visit_pri[$id];
2212           $vis_notes = $family_visit_notes[$id];
2213
2214           // If this family has had a yearly visit this year, don't show them on the schedule list
2215           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2216           $sql = "SELECT * FROM eq_visit WHERE date > '$year_start' AND date < '$year_end' ".
2217              "AND family=" . $id . " AND companionship=0";
2218           $this->db2->query($sql,__LINE__,__FILE__);
2219           
2220           if(!$this->db2->next_record()) {
2221             $sql = "SELECT * FROM eq_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
2222             $this->db->query($sql,__LINE__,__FILE__);
2223             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
2224             $link_data['menuaction'] = 'eq.eq.vis_update';
2225             $link_data['visit'] = '';
2226             $link_data['family'] = $id;
2227             $link_data['name'] = $name;
2228             $link_data['action'] = 'add';
2229             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2230             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2231             $table_data.= "<td align=center>$phone</td>";
2232             $table_data.= "<td align=center>";
2233             $table_data.= '<select name=vis_notes['.$i.'][pri]>';
2234             foreach(range(0,6) as $num) {
2235               if($num == 0) { $num = 1; } else {$num = $num*5; }
2236               if($vis_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
2237               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2238             }
2239             $table_data.= '</select></td>';
2240             $table_data.= "<td align=center>$date</td>";
2241             $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$i.'][notes]" value="'.$vis_notes.'">';
2242             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_id]" value="'.$id.'">';
2243             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_name]" value="'.$name.'">';
2244             $table_data.= '</td>';
2245             $table_data.= '</tr>';
2246             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2247             $this->t->set_var('tr_color',$tr_color);
2248           } else {
2249             $link_data['menuaction'] = 'eq.eq.vis_update';
2250             $link_data['visit'] = $this->db2->f('visit');
2251             $link_data['family'] = $this->db2->f('family');
2252             $link_data['name'] = $name;
2253             $link_data['date'] = $this->db2->f('date');
2254             $link_data['action'] = 'view';
2255             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
2256             $families_with_yearly_visit++;
2257             $date = $this->db2->f('date');
2258             $vis_notes = $this->db2->f('notes');
2259             if(strlen($vis_notes) > 40) { $vis_notes = stripslashes(substr($vis_notes,0,40) . "..."); }
2260             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2261             $completed_data.= "<td align=center>$phone</td>";
2262             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2263             $completed_data.= "<td align=left>$vis_notes</td>";
2264             $completed_data.= '</tr>';
2265             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2266             $this->t->set_var('tr_color2',$tr_color2);
2267           }
2268       }
2269
2270       $name_width=190; $phone_width=100; $date_width=100; $notes_width=300;
2271       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2272       $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
2273       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2274       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2275       $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
2276       
2277       $family_width=300; $totals_width=100;
2278       $totals_table_width=$family_width + $totals_width;
2279       $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
2280       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2281       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2282       $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
2283       $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
2284       $percent = ceil(($families_with_yearly_visit / $total_families)*100);
2285       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2286       $this->t->set_var('tr_color',$tr_color);
2287       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2288       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2289       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2290       $totals_data.= "</tr>";
2291       
2292       $this->t->set_var('table_width',$table_width);
2293       $this->t->set_var('header_row',$header_row);
2294       $this->t->set_var('table_data',$table_data);
2295       $this->t->set_var('totals_header_row',$totals_header_row);
2296       $this->t->set_var('totals_table_width',$totals_table_width);
2297       $this->t->set_var('completed_header_row',$completed_header_row);
2298       $this->t->set_var('completed_table_width',$completed_table_width);
2299       $this->t->set_var('completed',$completed_data);
2300       $this->t->set_var('totals',$totals_data);
2301       $this->t->fp('familylist','family_list',True);
2302       $this->t->fp('apptlist','appt_list',True);
2303       
2304       $this->t->pfp('out','vis_sched_t');
2305       $this->save_sessiondata(); 
2306       
2307     }
2308   
2309   function ppi_view()
2310     {
2311       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
2312       $this->t->set_block('ppi_view_t','district_list','list');
2313
2314       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2315       $num_months = get_var('num_months',array('GET','POST'));
2316       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
2317       $this->t->set_var('num_months',$num_months);
2318       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
2319       else {  $this->t->set_var('lang_num_months','Months of History'); }
2320       $this->t->set_var('lang_filter','Filter');
2321       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2322             
2323       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2324       $this->t->set_var('ppi_link_title','Yearly PPIs'); 
2325
2326       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
2327       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2328       
2329       $this->t->set_var('title','Yearly PPIs');
2330       $num_months = get_var('num_months',array('GET','POST'));
2331       if($num_months == '') { $num_months = $this->default_ppi_num_years; }
2332       $this->t->set_var('num_months',$num_months);
2333       if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
2334       else { $this->t->set_var('lang_num_months','Years of History'); } 
2335
2336       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
2337       $this->db->query($sql,__LINE__,__FILE__);
2338       if($this->db->next_record()) {
2339         $president_name = $this->db->f('name');
2340         $interviewer = $this->db->f('elder');
2341         $eqpresppi = 1;
2342       } else {
2343         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
2344         return;
2345       }
2346       $this->t->set_var('district_number','*');
2347       $this->t->set_var('district_name',$president_name);
2348
2349       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2350       $this->db->query($sql,__LINE__,__FILE__);
2351       $i=0;
2352       while ($this->db->next_record())
2353         {
2354           $elder_id[$i] = $this->db->f('elder');
2355           $elder_name[$i] = $this->db->f('name');
2356           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2357           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
2358           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
2359           $i++;
2360         }
2361       $total_elders=$i;
2362       array_multisort($elder_name, $elder_id);
2363       //var_dump($elder_name); print "<br><br>"; var_dump($elder_id);
2364       
2365       $header_row="<th width=$comp_width><font size=-2>Elder Name</th>";
2366         
2367       $elder_width=400; $ppi_width=75; $table_width=$elder_width + $num_months*$ppi_width;
2368       $table_data="";
2369       for($m=$num_months; $m >= 0; $m--) {
2370         $year = date('Y') - $m;
2371         $header_row .= "<th width=150><font size=-2>$year</th>"; 
2372         $ppis[$m] = 0;
2373       }
2374       
2375       for ($j=0; $j < count($elder_id); $j++) {
2376         $id = $elder_id[$j];
2377         $name = $elder_name[$j];
2378         $phone = $elder_phone[$id];
2379         
2380         $link_data['menuaction'] = 'eq.eq.ppi_update';
2381         $link_data['interviewer'] = $interviewer;
2382         $link_data['elder'] = $id;
2383         $link_data['name'] = $name;
2384         $link_data['ppi'] = '';
2385         $link_data['eqpresppi'] = $eqpresppi;
2386         $link_data['action'] = 'add';
2387         $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2388         $this->nextmatchs->template_alternate_row_color(&$this->t);
2389         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2390
2391         // Find out how many times PPIs were performed in the past $num_months for this Elder
2392         for($m=$num_months; $m >= 0; $m--) {
2393           $year = date('Y') - $m;
2394           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2395           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
2396              "AND elder=" . $id . " AND eqpresppi=1";
2397           $this->db2->query($sql,__LINE__,__FILE__);
2398                   
2399           if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
2400           if($this->db2->next_record()) {
2401             $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
2402             $link_data['menuaction'] = 'eq.eq.ppi_update';
2403             $link_data['companionship'] = $companionship;
2404             $link_data['interviewer'] = $this->db2->f('interviewer');
2405             $link_data['elder'] = $id;
2406             $link_data['name'] = $name;
2407             $link_data['ppi'] = $this->db2->f('ppi');
2408             $link_data['eqpresppi'] = $eqpresppi;
2409             $link_data['action'] = 'view';
2410             $date = $this->db2->f('date');
2411             $date_array = explode("-",$date);
2412             $month = $date_array[1];
2413             $day   = $date_array[2];
2414             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2415             $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2416           }
2417           else { $table_data .= "<td>&nbsp;</td>"; }
2418         }
2419         $table_data .= "</tr>\n"; 
2420       }
2421       $table_data .= "<tr><td colspan=20><hr></td></tr>";
2422       
2423       $stat_data = "<tr><td><b><font size=-2>$total_elders Elders<br>PPI Totals:</font></b></td>";
2424       for($m=$num_months; $m >=0; $m--) {
2425         $percent = ceil(($ppis[$m] / $total_elders)*100);
2426         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
2427       }
2428       $stat_data .= "</tr>";
2429       
2430       $this->t->set_var('table_width',$table_width);
2431       $this->t->set_var('header_row',$header_row);
2432       $this->t->set_var('table_data',$table_data);
2433       $this->t->set_var('stat_data',$stat_data);
2434       $this->t->pfp('out','ppi_view_t');
2435       $this->save_sessiondata(); 
2436     }
2437
2438   function ppi_update()
2439     {
2440       $this->t->set_file(array('form' => 'ppi_update.tpl'));
2441       $this->t->set_block('form','interviewer_list','int_list');
2442       $this->t->set_block('form','add','addhandle');
2443       $this->t->set_block('form','edit','edithandle');
2444       
2445       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2446       $this->t->set_var('readonly','');
2447       $this->t->set_var('disabled','');
2448       
2449       $action = get_var('action',array('GET','POST'));
2450       $companionship = get_var('companionship',array('GET','POST'));
2451       $interviewer = get_var('interviewer',array('GET','POST'));      
2452       $name = get_var('name',array('GET','POST'));
2453       $ppi = get_var('ppi',array('GET','POST'));
2454       $elder = get_var('elder',array('GET','POST'));
2455       $aaronic = get_var('aaronic',array('GET','POST'));
2456       $date = get_var('date',array('GET','POST'));
2457       $notes = get_var('notes',array('GET','POST'));
2458       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
2459       
2460       $sql = "SELECT * FROM eq_elder WHERE elder=" . $interviewer;
2461       $this->db2->query($sql,__LINE__,__FILE__);
2462       $this->db2->next_record();
2463       $interviewer_name = $this->db2->f('name');
2464       $this->t->set_var('interviewer',$interviewer . ' selected');
2465       $this->t->set_var('interviewer_name',$interviewer_name);
2466       $this->t->set_var('eqpresppi_checked','');
2467       $this->t->fp('int_list','interviewer_list',True);
2468     
2469       if($action == 'save')
2470         {
2471           $notes = get_var('notes',array('POST'));
2472           $this->db->query("UPDATE eq_ppi set " .
2473                            "   ppi='" . $ppi . "'" .
2474                     ", interviewer='" . $interviewer . "'" .
2475                           ", elder='" . $elder . "'" .
2476                         ", aaronic='" . $aaronic . "'" .
2477                            ", date='" . $date . "'" .
2478                           ", notes='" . $notes . "'" .
2479                       ", eqpresppi='" . $eqpresppi . "'" .
2480                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2481           $this->ppi_view();
2482           return false;
2483         }
2484
2485       if($action == 'insert')
2486         {
2487           $notes = get_var('notes',array('POST'));
2488           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
2489                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2490                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
2491           $this->ppi_view();
2492           return false;
2493         }
2494       
2495       if($action == 'add')
2496         {
2497           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2498           $this->t->set_var('ppi', '');
2499           $this->t->set_var('interviewer', $interviewer);
2500           $this->t->set_var('name',$name);
2501           $this->t->set_var('elder',$elder);
2502           $this->t->set_var('date','');
2503           $this->t->set_var('notes','');
2504           $this->t->set_var('eqpresppi',$eqpresppi);
2505           $this->t->set_var('eqpresppi_checked','checked');
2506           $this->t->set_var('lang_done','Cancel');
2507           $this->t->set_var('lang_action','Adding New PPI');
2508           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2509                                                                 . $ppi . '&action=' . 'insert'));
2510         }
2511
2512       if($action == 'edit' || $action == 'view')
2513         {
2514           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2515           $this->db->query($sql,__LINE__,__FILE__);
2516           $this->db->next_record();
2517           $this->t->set_var('ppi',$ppi);
2518           $this->t->set_var('name',$name);
2519           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2520           $this->t->set_var('elder',$this->db->f('elder'));
2521           $this->t->set_var('date',$this->db->f('date'));
2522           $this->t->set_var('notes',$this->db->f('notes'));
2523           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
2524           if($this->db->f('eqpresppi') == 1) { $this->t->set_var('eqpresppi_checked','checked'); }
2525         }
2526       
2527       if($action == 'edit')
2528         {
2529           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2530           $this->t->set_var('lang_done','Cancel');
2531           $this->t->set_var('lang_action','Editing PPI');
2532           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2533                                                                 . $ppi . '&action=' . 'save'));
2534         }
2535
2536       if($action == 'view')
2537         {
2538           $date = $this->db->f('date');
2539           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2540           $this->t->set_var('readonly','READONLY');
2541           $this->t->set_var('disabled','DISABLED');
2542           $this->t->set_var('lang_done','Done');
2543           $this->t->set_var('lang_action','Viewing PPI');
2544           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2545                                                                 . $ppi . '&action=' . 'edit'));
2546         }
2547       
2548       $this->t->set_var('lang_reset','Clear Form');
2549       $this->t->set_var('lang_add','Add PPI');
2550       $this->t->set_var('lang_save','Save Changes');
2551       $this->t->set_var('edithandle','');
2552       $this->t->set_var('addhandle','');
2553
2554       $this->t->pfp('out','form');
2555       
2556       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2557       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2558       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2559      
2560       $this->save_sessiondata(); 
2561     }
2562
2563     function int_view()
2564     {
2565       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2566       $this->t->set_block('int_view_t','district_list','list');
2567
2568       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2569       $num_quarters = get_var('num_quarters',array('GET','POST'));
2570       if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2571       $this->t->set_var('num_quarters',$num_quarters);
2572       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2573       else {  $this->t->set_var('lang_num_quarters','Quarters of History'); }
2574       $this->t->set_var('lang_filter','Filter');
2575       
2576       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2577       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2578       
2579       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2580       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2581       
2582       $this->t->set_var('title','Hometeaching Interviews'); 
2583
2584       $num_months = $num_quarters * 3 - 1;
2585       $current_month = $this->current_month;
2586       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2587       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2588       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2589       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2590
2591       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2592       $this->db->query($sql,__LINE__,__FILE__);
2593       $i=0;
2594       while ($this->db->next_record())
2595         {
2596           $districts[$i]['district'] = $this->db->f('district');
2597           $districts[$i]['name'] = $this->db->f('name');
2598           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2599           $i++;
2600         }
2601
2602       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2603       $this->db->query($sql,__LINE__,__FILE__);
2604       $i=0;
2605       while ($this->db->next_record())
2606         {
2607           $elder_id[$i] = $this->db->f('elder');
2608           $elder_name[$i] = $this->db->f('name');
2609           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2610           $i++;
2611         }
2612       array_multisort($elder_name, $elder_id);
2613       for($i=0; $i < count($elder_id); $i++) {
2614           $id = $elder_id[$i];
2615           $elders[$id] = $elder_name[$i];
2616       }      
2617
2618       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2619       $this->db->query($sql,__LINE__,__FILE__);
2620       while ($this->db->next_record())
2621         {
2622           $aaronic_id = $this->db->f('aaronic');
2623           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2624           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2625         }
2626       
2627       $total_companionships = 0;
2628       $this->nextmatchs->template_alternate_row_color(&$this->t);
2629       for ($i=0; $i < count($districts); $i++) {
2630         $this->t->set_var('district_number',$districts[$i]['district']);
2631         $this->t->set_var('district_name',$districts[$i]['name']);      
2632         $supervisor = $districts[$i]['supervisor'];
2633         $unique_companionships='';
2634                 
2635         // Select all the unique companionship numbers for this district
2636         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2637
2638         $this->db->query($sql,__LINE__,__FILE__);
2639         $j=0;
2640         while ($this->db->next_record())
2641           {
2642             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2643             $j++;
2644           }
2645         
2646         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2647         $table_data=""; $num_companionships = $j; $num_elders = 0;
2648         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2649         for ($j=0; $j < count($unique_companionships); $j++) {
2650           // Select all the companions in each companionship
2651           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2652              "companionship=". $unique_companionships[$j]['companionship'];
2653           $this->db->query($sql,__LINE__,__FILE__);
2654           $k=0;
2655           $comp = $unique_companionships[$j]['companionship'];
2656           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2657           while ($this->db->next_record())
2658             {
2659               // Get this companions information
2660               $num_elders++;
2661               $companionship = $this->db->f('companionship');
2662               $elder_id = $this->db->f('elder');
2663               $aaronic_id = $this->db->f('aaronic');
2664               if($elder_id) {
2665                 $name = $elders[$elder_id];
2666                 $phone = $elder_phone[$elder_id];
2667               }
2668               else if($aaronic_id) {
2669                 $name = $aaronic[$aaronic_id]['name'];
2670                 $phone = $aaronic[$aaronic_id]['phone'];
2671               }
2672               $link_data['menuaction'] = 'eq.eq.int_update';
2673               $link_data['companionship'] = $companionship;
2674               $link_data['interviewer'] = $supervisor;
2675               $link_data['elder'] = $elder_id;
2676               $link_data['aaronic'] = $aaronic_id;
2677               $link_data['name'] = $name;
2678               $link_data['ppi'] = '';
2679               $link_data['action'] = 'add';
2680               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2681               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2682
2683               // Find out how many times Interviews were performed in the past $num_months for this Elder
2684               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2685               for($m=$num_months; $m >= 0; $m--) {
2686                 $month = $current_month - $m;
2687                 $year = $this->current_year;
2688                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2689                 if($month < 10) { $month = "0"."$month"; }
2690                 $month_start = "$year"."-"."$month"."-"."01";
2691                 $month_end = "$year"."-"."$month"."-"."31";
2692                 $month = "$month"."/"."$year";
2693                 $sql = "SELECT * FROM eq_ppi WHERE date >= '$month_start' AND date <= '$month_end' ".
2694                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2695                 $this->db2->query($sql,__LINE__,__FILE__);
2696                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2697               
2698                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2699                 if($this->db2->next_record()) {
2700                   if(!$int_recorded[$companionship][$m]) {
2701                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2702                   }
2703                   $link_data['menuaction'] = 'eq.eq.int_update';
2704                   $link_data['companionship'] = $companionship;
2705                   $link_data['interviewer'] = $this->db2->f('interviewer');
2706                   $link_data['elder'] = $elder_id;
2707                   $link_data['aaronic'] = $aaronic_id;
2708                   $link_data['name'] = $name;
2709                   $link_data['ppi'] = $this->db2->f('ppi');
2710                   $link_data['action'] = 'view';
2711                   $date = $this->db2->f('date');
2712                   $date_array = explode("-",$date);
2713                   $month = $date_array[1];
2714                   $day   = $date_array[2];
2715                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2716                   $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2717                 }
2718                 else { $table_data .= "<td>&nbsp;</td>"; }
2719               }
2720               $table_data .= "</tr>"; 
2721               $k++;
2722             }
2723           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2724         }
2725         $total_companionships += $num_companionships;
2726         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Totals:</font></b></td>";
2727
2728         // Print the hometeaching interview stats
2729         for($m=$num_months; $m >=0; $m--) {
2730           $month = $current_month - $m;
2731           if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2732           $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2733           //print "$month % $this->monthly_hometeaching_interview_stats = $month_begins <br>";
2734           if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2735           if(($month_begins) == 1) { $total = $ints[$m]; }
2736           else { $total += $ints[$m]; }
2737           $percent = ceil(($total / $num_companionships)*100);
2738           $stat_data .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2739         }
2740         $stat_data .= "</tr>";
2741         
2742         $this->t->set_var('table_width',$table_width);
2743         $this->t->set_var('header_row',$header_row);
2744         $this->t->set_var('table_data',$table_data);
2745         $this->t->set_var('stat_data',$stat_data);
2746         $this->t->fp('list','district_list',True);
2747       }
2748
2749       // Display the totals
2750       $total = 0;
2751       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Totals:</font></b></td>";
2752       for($m=$num_months; $m >=0; $m--) {
2753         $month = $current_month - $m;
2754         if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2755         $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2756         if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2757         if(($month_begins) == 1) { $total = $total_ints[$m]; }
2758         else { $total += $total_ints[$m]; }
2759         $percent = ceil(($total / $total_companionships)*100);
2760         $totals .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2761       }
2762       $totals .= "</tr>";
2763           
2764       $this->t->set_var('totals',$totals);
2765       $this->t->pfp('out','int_view_t');
2766       $this->save_sessiondata(); 
2767     }
2768
2769   function int_update()
2770     {
2771       $this->t->set_file(array('form' => 'int_update.tpl'));
2772       $this->t->set_block('form','interviewer_list','int_list');
2773       $this->t->set_block('form','add','addhandle');
2774       $this->t->set_block('form','edit','edithandle');
2775       
2776       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2777       $this->t->set_var('readonly','');
2778       $this->t->set_var('disabled','');
2779       $this->t->set_var('eqpresppi_checked','');
2780       
2781       $action = get_var('action',array('GET','POST'));
2782       $companionship = get_var('companionship',array('GET','POST'));
2783       $interviewer = get_var('interviewer',array('GET','POST'));      
2784       $name = get_var('name',array('GET','POST'));
2785       $ppi = get_var('ppi',array('GET','POST'));
2786       $elder = get_var('elder',array('GET','POST'));
2787       $aaronic = get_var('aaronic',array('GET','POST'));
2788       $date = get_var('date',array('GET','POST'));
2789       $notes = get_var('notes',array('GET','POST'));
2790       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
2791       
2792       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2793       $this->db->query($sql,__LINE__,__FILE__);
2794       while ($this->db->next_record())
2795         {
2796           $supervisor = $this->db->f('supervisor');
2797           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2798           $this->db2->query($sql,__LINE__,__FILE__);
2799           $this->db2->next_record();
2800           $interviewer_name = $this->db2->f('name');
2801           
2802           if($supervisor == $interviewer) { 
2803             $this->t->set_var('interviewer',$supervisor . ' selected');
2804           } else {
2805             $this->t->set_var('interviewer',$interviewer);
2806           }
2807           $this->t->set_var('interviewer_name',$interviewer_name);
2808           $this->t->fp('int_list','interviewer_list',True);
2809         }
2810       
2811       if($action == 'save')
2812         {
2813           $notes = get_var('notes',array('POST'));
2814           $this->db->query("UPDATE eq_ppi set " .
2815                            "   ppi='" . $ppi . "'" .
2816                     ", interviewer='" . $interviewer . "'" .
2817                           ", elder='" . $elder . "'" .
2818                         ", aaronic='" . $aaronic . "'" .
2819                            ", date='" . $date . "'" .
2820                           ", notes='" . $notes . "'" .
2821                       ", eqpresppi='" . $eqpresppi . "'" .
2822                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2823           $this->int_view();
2824           return false;
2825         }
2826
2827       if($action == 'insert')
2828         {
2829           $notes = get_var('notes',array('POST'));
2830           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
2831                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2832                            . $date . "','" . $notes ."','" . $eqpresppi . "')",__LINE__,__FILE__);
2833           $this->int_view();
2834           return false;
2835         }
2836       
2837       if($action == 'add')
2838         {
2839           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2840           $this->t->set_var('ppi', '');
2841           $this->t->set_var('interviewer', $interviewer);
2842           $this->t->set_var('name',$name);
2843           $this->t->set_var('elder',$elder);
2844           $this->t->set_var('aaronic',$aaronic);
2845           $this->t->set_var('date','');
2846           $this->t->set_var('notes','');
2847           $this->t->set_var('lang_done','Cancel');
2848           $this->t->set_var('lang_action','Adding New Interview');
2849           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2850                                                                 . $ppi . '&action=' . 'insert'));
2851         }
2852
2853       if($action == 'edit' || $action == 'view')
2854         {
2855           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2856           $this->db->query($sql,__LINE__,__FILE__);
2857           $this->db->next_record();
2858           $this->t->set_var('ppi',$ppi);
2859           $this->t->set_var('name',$name);
2860           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2861           $this->t->set_var('elder',$this->db->f('elder'));
2862           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2863           $this->t->set_var('date',$this->db->f('date'));
2864           $this->t->set_var('notes',$this->db->f('notes'));
2865           if($this->db->f('eqpresppi') == 1) { $this->t->set_var('eqpresppi_checked','checked'); }
2866         }
2867       
2868       if($action == 'edit')
2869         {
2870           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2871           $this->t->set_var('lang_done','Cancel');
2872           $this->t->set_var('lang_action','Editing Interview');
2873           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2874                                                                 . $ppi . '&action=' . 'save'));
2875         }
2876
2877       if($action == 'view')
2878         {
2879           $date = $this->db->f('date');
2880           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2881           $this->t->set_var('readonly','READONLY');
2882           $this->t->set_var('disabled','DISABLED');
2883           $this->t->set_var('lang_done','Done');
2884           $this->t->set_var('lang_action','Viewing Interview');
2885           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2886                                                                 . $ppi . '&action=' . 'edit'));
2887         }
2888       
2889       $this->t->set_var('lang_reset','Clear Form');
2890       $this->t->set_var('lang_add','Add Interview');
2891       $this->t->set_var('lang_save','Save Changes');
2892       $this->t->set_var('edithandle','');
2893       $this->t->set_var('addhandle','');
2894
2895       $this->t->pfp('out','form');
2896       
2897       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2898       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2899       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2900      
2901       $this->save_sessiondata(); 
2902     }
2903
2904   function vis_view()
2905     {
2906       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2907       $this->t->set_block('vis_view_t','visit_list','list1');
2908       $this->t->set_block('vis_view_t','family_list','list2');
2909
2910       $this->t->set_var('lang_name','Family Name');
2911       $this->t->set_var('lang_date','Date');
2912
2913       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2914       $this->t->set_var('vis_link_title','View Yearly Visits');
2915       
2916       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2917       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2918
2919       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2920       $num_years = get_var('num_years',array('GET','POST'));
2921       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2922       $this->t->set_var('num_years',$num_years);
2923       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2924       else {  $this->t->set_var('lang_num_years','Years of History'); }
2925       $this->t->set_var('lang_filter','Filter');
2926       
2927       $year = date('Y') - $num_years + 1;
2928       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2929       
2930       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2931       $this->db->query($sql,__LINE__,__FILE__);
2932       $total_records = $this->db->num_rows();
2933
2934       $i = 0;
2935       while ($this->db->next_record())
2936         {
2937           $visit_list[$i]['visit'] = $this->db->f('visit');
2938           $visit_list[$i]['family'] = $this->db->f('family');
2939           $visit_list[$i]['date']  = $this->db->f('date');
2940           $i++;
2941         }
2942             
2943       for ($i=0; $i < count($visit_list); $i++)
2944         {         
2945           $this->nextmatchs->template_alternate_row_color(&$this->t);
2946
2947           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2948           $this->db->query($sql,__LINE__,__FILE__);
2949           $this->db->next_record();
2950                   
2951           $this->t->set_var('family',$visit_list[$i]['family']);
2952           $this->t->set_var('family_name',$this->db->f('name'));
2953           $this->t->set_var('date',$visit_list[$i]['date']);
2954           
2955           $link_data['menuaction'] = 'eq.eq.vis_update';
2956           $link_data['visit'] = $visit_list[$i]['visit'];
2957           $link_data['name'] = $this->db->f('name');
2958           $link_data['date'] = $visit_list[$i]['date'];
2959           $link_data['action'] = 'view';
2960           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2961           $this->t->set_var('lang_view','View');
2962
2963           $link_data['menuaction'] = 'eq.eq.vis_update';
2964           $link_data['visit'] = $visit_list[$i]['visit'];
2965           $link_data['name'] = $this->db->f('name');
2966           $link_data['date'] = $visit_list[$i]['date'];
2967           $link_data['action'] = 'edit';
2968           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2969           $this->t->set_var('lang_edit','Edit');
2970
2971           $this->t->fp('list1','visit_list',True);
2972         }
2973
2974       // List the families that are available to record a visit against
2975       $sql = "SELECT * FROM eq_family WHERE valid=1";
2976       $this->db->query($sql,__LINE__,__FILE__);
2977       $total_records = $this->db->num_rows();
2978
2979       $i = 0;
2980       while ($this->db->next_record())
2981         {
2982           $family_names[$i] = $this->db->f('name');
2983           $family_ids[$i] = $this->db->f('family');
2984           $i++;
2985         } array_multisort($family_names, $family_ids);
2986       
2987       for ($i=0; $i < count($family_names); $i++)
2988         {
2989           $link_data['menuaction'] = 'eq.eq.vis_update';
2990           $link_data['visit'] = '';
2991           $link_data['family'] = $family_ids[$i];
2992           $link_data['action'] = 'add';
2993           $link_data['name'] = $family_names[$i];
2994           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2995
2996           $this->t->set_var('name',$family_names[$i]);
2997           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2998           else { $this->t->set_var('table_sep',"</td>"); }
2999           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
3000
3001           $this->t->fp('list2','family_list',True);
3002         }   
3003
3004       $this->t->pfp('out','vis_view_t');
3005       $this->save_sessiondata(); 
3006     }
3007
3008   function vis_update()
3009     {
3010       $this->t->set_file(array('form' => 'vis_update.tpl'));
3011       $this->t->set_block('form','add','addhandle');
3012       $this->t->set_block('form','edit','edithandle');
3013       
3014       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
3015       $this->t->set_var('readonly','');
3016       $this->t->set_var('disabled','');
3017       
3018       $action = get_var('action',array('GET','POST'));
3019       $visit = get_var('visit',array('GET','POST'));
3020       $family = get_var('family',array('GET','POST'));
3021       $name = get_var('name',array('GET','POST'));
3022       $date = get_var('date',array('GET','POST'));
3023       $notes = get_var('notes',array('GET','POST'));
3024       $companionship = 0;
3025       
3026       if($action == 'save')
3027         {
3028           $notes = get_var('notes',array('POST'));
3029           $this->db->query("UPDATE eq_visit set " .
3030                            "  date='" . $date . "'" .
3031                           ", notes='" . $notes . "'" .
3032                            " WHERE visit=" . $visit,__LINE__,__FILE__);
3033           $this->vis_view();
3034           return false;
3035         }
3036
3037       if($action == 'insert')
3038         {
3039           $notes = get_var('notes',array('POST'));
3040           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
3041                            . "VALUES ('" . $family . "','" . $companionship . "','"
3042                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
3043           $this->vis_view();
3044           return false;
3045         }
3046       
3047       if($action == 'add')
3048         {
3049           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3050           $this->t->set_var('family', $family);
3051           $this->t->set_var('visit', '');
3052           $this->t->set_var('name', $name);
3053           $this->t->set_var('date','');
3054           $this->t->set_var('notes','');
3055           $this->t->set_var('lang_done','Cancel');
3056           $this->t->set_var('lang_action','Adding New Visit');
3057           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
3058                                                                 . $family . '&action=' . 'insert'));
3059         }
3060
3061       if($action == 'edit' || $action == 'view')
3062         {
3063           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
3064           $this->db->query($sql,__LINE__,__FILE__);
3065           $this->db->next_record();
3066           $this->t->set_var('visit',$visit);
3067           $this->t->set_var('name',$name);
3068           $this->t->set_var('family', $family);
3069           $this->t->set_var('date',$this->db->f('date'));
3070           $this->t->set_var('notes',$this->db->f('notes'));
3071         }
3072       
3073       if($action == 'edit')
3074         {
3075           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3076           $this->t->set_var('lang_done','Cancel');
3077           $this->t->set_var('lang_action','Editing Visit');
3078           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3079                                                                 . $visit . '&action=' . 'save'));
3080         }
3081
3082       if($action == 'view')
3083         {
3084           $date = $this->db->f('date');
3085           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3086           $this->t->set_var('readonly','READONLY');
3087           $this->t->set_var('disabled','DISABLED');
3088           $this->t->set_var('lang_done','Done');
3089           $this->t->set_var('lang_action','Viewing Visit');
3090           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3091                                                                 . $visit . '&action=' . 'edit'));
3092         }
3093       
3094       $this->t->set_var('lang_reset','Clear Form');
3095       $this->t->set_var('lang_add','Add Visit');
3096       $this->t->set_var('lang_save','Save Changes');
3097       $this->t->set_var('edithandle','');
3098       $this->t->set_var('addhandle','');
3099
3100       $this->t->pfp('out','form');
3101       
3102       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3103       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3104       if($action == 'add') { $this->t->pfp('addhandle','add'); }
3105
3106       $this->save_sessiondata(); 
3107     }
3108
3109   function att_view()
3110     {
3111       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3112       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3113       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3114
3115       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3116       $this->t->set_block('att_view_t','act_list','list');
3117
3118       $this->t->set_block('att_view_t','month_list','list1');
3119       $this->t->set_block('att_view_t','header_list','list2');
3120       $this->t->set_block('att_view_t','elder_list','list3');
3121       
3122       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3123       $num_quarters = get_var('num_quarters',array('GET','POST'));
3124       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3125       $this->t->set_var('num_quarters',$num_quarters);
3126       $this->t->set_var('lang_filter','Filter');
3127       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
3128       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
3129
3130       $num_months = $num_quarters * 3;
3131       $current_month = $this->current_month;
3132       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3133       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3134       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3135       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3136       
3137       $sql = "SELECT * FROM eq_elder where valid=1";
3138       $this->db->query($sql,__LINE__,__FILE__);
3139       $i=0;
3140       while ($this->db->next_record())
3141         {
3142           $elder_name[$i] = $this->db->f('name');
3143           $elder_id[$i] = $this->db->f('elder');
3144           $i++;
3145         }
3146       array_multisort($elder_name, $elder_id);
3147       
3148       // Create a list of sunday dates for a window of 3 months back and current month
3149       $i=0; 
3150       $last_time = 0; 
3151       $found_sunday = 0;
3152       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
3153       $last_date = explode("-",$sunday_list[0]['date']);
3154       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3155       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
3156       while($last_time < $time_limit)
3157       {
3158         $day = date("w",$last_time);
3159         if(date("w",$last_time) == 0) {
3160           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3161           $last_date = explode("-",$sunday_list[$i]['date']);
3162           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3163           $sunday_list[$i]['day'] = $last_date[2];
3164           $sunday_list[$i]['month'] = date("M",$last_time);
3165           $sunday_list[$i]['year'] = $last_date[0];
3166           $found_sunday = 1; 
3167         }
3168         $last_time += 90000;
3169         if($found_sunday) { $i++; $found_sunday=0; }
3170       }
3171
3172       $total_elders = count($elder_id);
3173       $old_month=$sunday_list[0]['month']; $span=0;
3174       for ($i=0; $i < count($sunday_list); $i++) {
3175         $date = $sunday_list[$i]['date'];
3176         $this->t->set_var('date',$sunday_list[$i]['date']);
3177         $this->t->set_var('day',$sunday_list[$i]['day']);
3178         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3179           if($i == count($sunday_list)-1) { $span++; }
3180           $cur_month = $sunday_list[$i]['month'];
3181           $old_month = $sunday_list[$i]['month'];         
3182           $link_data['menuaction'] = 'eq.eq.att_update';
3183           $link_data['month'] = $sunday_list[$i-1]['month'];
3184           $link_data['year'] = $sunday_list[$i-1]['year'];
3185           $link_data['action'] = 'update_month';
3186           $cur_month = $sunday_list[$i-1]['month'];
3187           $cur_year = $sunday_list[$i-1]['year'];
3188           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3189           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3190           $this->t->set_var('month',$sunday_list[$i-1]['month']);
3191           $this->t->set_var('year',$sunday_list[$i-1]['year']);
3192           $this->t->set_var('span',$span); $span=0;
3193           $this->t->fp('list1','month_list',True);
3194         } $span++;
3195       }
3196       $this->t->set_var('total_elders',$total_elders);
3197       $this->t->set_var('header_row',$header_row);
3198       
3199       $elder_width=200; $att_width=25; $total_width=$elder_width; 
3200       for ($i=0; $i < count($sunday_list); $i++) {
3201         $link_data['menuaction'] = 'eq.eq.att_update';
3202         $link_data['month'] = $sunday_list[$i]['month'];
3203         $link_data['year'] = $sunday_list[$i]['year'];
3204         $link_data['day'] = $sunday_list[$i]['day'];
3205         $link_data['date'] = $sunday_list[$i]['date'];
3206         $link_data['action'] = 'update_day';
3207         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3208         $this->t->set_var('date',$sunday_list[$i]['date']);
3209         $this->t->set_var('day',$sunday_list[$i]['day']);
3210         $this->t->set_var('month',$sunday_list[$i]['month']);
3211         $this->t->set_var('year',$sunday_list[$i]['year']);
3212         $this->t->fp('list2','header_list',True);
3213         $total_width += $att_width;
3214         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3215       }
3216
3217       for ($i=0; $i < count($elder_id); $i++) {
3218         $att_table = "";
3219         $this->nextmatchs->template_alternate_row_color(&$this->t);
3220         $this->t->set_var('elder_name',$elder_name[$i]);
3221         #print "checking for elder: " . $elder_id[$i] . "<br>";
3222         for ($j=0; $j < count($sunday_list); $j++) {
3223           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3224           #print "SELECT * FROM eq_attendance WHERE date='"
3225           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
3226           $sql = "SELECT * FROM eq_attendance WHERE date='"
3227              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3228           $this->db->query($sql,__LINE__,__FILE__);
3229           if($this->db->next_record()) {
3230             $cur_month = $sunday_list[$j]['month'];
3231             if($attended[$i][$cur_month] != 1) { 
3232               $attended[$i][$cur_month]=1;
3233               $attendance[$monthnum[$cur_month]]++;
3234             } 
3235             $att_table .= '<td align=center><img src="images/checkmark.gif"></td>';
3236           } else {
3237             $att_table .= '<td>&nbsp;</td>';
3238           }
3239         }
3240         $this->t->set_var('att_table',$att_table);
3241         $this->t->fp('list3','elder_list',True);
3242       }
3243       $this->t->set_var('total_width',$total_width);
3244       $this->t->set_var('elder_width',$elder_width);
3245       $this->t->set_var('att_width',$att_width);
3246
3247       # Now calculate attendance for these months
3248       $attendance_str = "";
3249       $nonattendance_str = "";
3250       $aveattendance_str = "";
3251       $avenonattendance_str = "";
3252       $num_months=0;
3253       $ave_total_attended=0;
3254       ksort($attendance);
3255       foreach($attendance as $att => $value) {
3256         $total_attended = $attendance[$att];
3257         $ave_total_attended += $attendance[$att]; $num_months++;
3258         $percent = ceil(($total_attended / $total_elders)*100);
3259         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3260         $total_nonattended = $total_elders - $total_attended;
3261         $percent = ceil(($total_nonattended / $total_elders)*100);
3262         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3263         
3264         $total_attended = ceil(($ave_total_attended / $num_months));
3265         $percent = ceil(($total_attended / $total_elders)*100);
3266         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3267         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
3268         $percent = ceil(($total_attended / $total_elders)*100);
3269         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3270       }
3271       
3272       $this->t->set_var('attendance',$attendance_str);
3273       $this->t->set_var('aveattendance',$aveattendance_str);
3274       $this->t->set_var('nonattendance',$nonattendance_str);
3275       $this->t->set_var('avenonattendance',$avenonattendance_str);
3276       
3277       $this->t->pfp('out','att_view_t');
3278       $this->save_sessiondata(); 
3279     }
3280
3281   function att_update()
3282     {
3283       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3284       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3285       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3286
3287       $this->t->set_file(array('form' => 'att_update.tpl'));
3288       $this->t->set_block('form','edit','edithandle');
3289       
3290       $this->t->set_block('form','month_list','list1');
3291       $this->t->set_block('form','header_list','list2');
3292       $this->t->set_block('form','elder_list','list3');
3293
3294       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3295
3296       $action = get_var('action',array('GET','POST'));
3297       $month = get_var('month',array('GET','POST'));
3298       $year = get_var('year',array('GET','POST'));
3299       $day = get_var('day',array('GET','POST'));
3300       $date = get_var('date',array('GET','POST'));
3301
3302       if($action == 'save_month' || $action == 'save_day')
3303         {
3304            $new_data = get_var('elders_attended',array('POST'));
3305            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3306
3307            if($action == 'save_month') {        
3308              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3309            }
3310
3311            if($action == 'save_day') {        
3312              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3313            }   
3314
3315            foreach ($new_data as $data)
3316            {
3317               $data_array = explode("-",$data);
3318               $elder = $data_array[0];
3319               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
3320               $this->db->query("INSERT INTO eq_attendance (elder,date) "
3321                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
3322            }
3323         
3324          $this->att_view();
3325          return false;    
3326         }
3327
3328       $sql = "SELECT * FROM eq_elder where valid=1";
3329       $this->db->query($sql,__LINE__,__FILE__);
3330       $i=0;
3331       while ($this->db->next_record())
3332         {
3333           $elder_name[$i] = $this->db->f('name');
3334           $elder_id[$i] = $this->db->f('elder');
3335           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
3336           $i++;
3337         }
3338       array_multisort($elder_name, $elder_id);
3339       
3340       if($action == 'update_month')
3341       {
3342         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
3343         $i=0; 
3344         $last_time = 0; 
3345         $found_sunday = 0;
3346         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3347         $last_date = explode("-",$sunday_list[0]['date']);
3348         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3349         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3350         while($last_time <= $time_limit)
3351         {
3352           $day = date("w",$last_time);
3353           if(date("w",$last_time) == 0) { 
3354             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3355             $last_date = explode("-",$sunday_list[$i]['date']);
3356             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3357             $sunday_list[$i]['day'] = $last_date[2];
3358             $sunday_list[$i]['month'] = date("M",$last_time);
3359             $sunday_list[$i]['year'] = $last_date[0];
3360             $found_sunday = 1; 
3361           }
3362           $last_time += 90000;
3363           if($found_sunday) { $i++; $found_sunday=0; }
3364         }
3365
3366         $this->t->set_var('span', $i);
3367         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3368         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3369         $this->t->fp('list1','month_list',True);
3370         $elder_width=200; $att_width=25; $total_width=$elder_width;
3371         for ($i=0; $i < count($sunday_list); $i++) {
3372           $link_data['menuaction'] = 'eq.eq.att_update';
3373           $link_data['month'] = $sunday_list[$i]['month'];
3374           $link_data['year'] = $sunday_list[$i]['year'];
3375           $link_data['day'] = $sunday_list[$i]['day'];
3376           $link_data['date'] = $sunday_list[$i]['date'];
3377           $link_data['action'] = 'update_day';
3378           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3379           $this->t->set_var('date',$sunday_list[$i]['date']);
3380           $this->t->set_var('day',$sunday_list[$i]['day']);
3381           $this->t->set_var('month',$sunday_list[$i]['month']);
3382           $this->t->set_var('year',$sunday_list[$i]['year']);
3383           $this->t->fp('list2','header_list',True);
3384           $total_width += $att_width;
3385         }     
3386       }
3387
3388       if($action == 'update_day')
3389       {
3390         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
3391         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3392         $this->t->set_var('month',$month);
3393         $this->t->set_var('year',$year);
3394         $this->t->fp('list1','month_list',True);
3395         $this->t->set_var('date',$date);
3396         $this->t->set_var('day',$day);
3397         $this->t->set_var('month',$month);
3398         $this->t->set_var('year',$year);
3399         $this->t->fp('list2','header_list',True);
3400       }           
3401             
3402       for ($i=0; $i < count($elder_id); $i++) {
3403         $att_table = "";
3404         $this->nextmatchs->template_alternate_row_color(&$this->t);
3405         $this->t->set_var('elder_name',$elder_name[$i]);
3406         for ($j=0; $j < count($sunday_list); $j++) {
3407           $sql = "SELECT * FROM eq_attendance WHERE date='"
3408              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3409           $this->db->query($sql,__LINE__,__FILE__);
3410           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
3411           if($this->db->next_record()) {
3412             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3413           } else if($elder_attending[$elder_id[$i]] == 1) {
3414             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3415           } else {
3416             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
3417           }
3418         }
3419         $this->t->set_var('att_table',$att_table);
3420         $this->t->fp('list3','elder_list',True);
3421       } 
3422            
3423       $this->t->set_var('lang_done', 'Cancel');
3424       $this->t->set_var('lang_reset','Clear Form');
3425       $this->t->set_var('lang_save','Save Changes');
3426
3427       $this->t->pfp('out','form');
3428       $this->t->pfp('addhandle','edit');
3429
3430       $this->save_sessiondata();       
3431     }
3432
3433   function dir_view()
3434     {
3435       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3436       $this->t->set_block('dir_view_t','dir_list','list');
3437       
3438       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
3439       $this->db->query($sql,__LINE__,__FILE__);
3440       $i=0;
3441       while ($this->db->next_record())
3442         {
3443           $parent[$i]['id'] = $this->db->f('parent');
3444           $parent[$i]['name'] = $this->db->f('name');
3445           $parent[$i]['phone'] = $this->db->f('phone');
3446           $parent[$i]['address'] = $this->db->f('address');
3447           $i++;
3448         }   
3449       
3450       for ($i=0; $i < count($parent); $i++) 
3451       {
3452         $name = $parent[$i]['name'];
3453         $phone = $parent[$i]['phone'];
3454         $address = $parent[$i]['address'];
3455         $this->t->set_var('name', $name);
3456         $this->t->set_var('address', $address);
3457         $this->t->set_var('phone', $phone);
3458         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3459         $this->t->set_var('tr_color',$tr_color);
3460         $this->t->fp('list','dir_list',True);
3461         //print "$phone $name $address<br>";
3462       }
3463       $this->t->pfp('out','dir_view_t');
3464       $this->save_sessiondata();   
3465     }
3466   
3467   function org_view()
3468     {
3469       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3470       $this->t->set_block('org_view_t','calling_list','list1');
3471       $this->t->set_block('org_view_t','org_list','list2');
3472
3473       # Display a list ordered alphabetically
3474       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
3475       $this->db->query($sql,__LINE__,__FILE__);
3476       $i=0;
3477       while ($this->db->next_record())
3478         {
3479           $calling[$i]['id'] = $this->db->f('indiv_id');
3480           $calling[$i]['name'] = $this->db->f('name');
3481           $calling[$i]['position'] = $this->db->f('position');
3482           $calling[$i]['sustained'] = $this->db->f('sustained');
3483           $calling[$i]['organization'] = $this->db->f('organization');
3484           $i++;
3485         }   
3486       for ($i=0; $i < count($calling); $i++) 
3487       {
3488         $name = $calling[$i]['name'];
3489         $position = $calling[$i]['position'];
3490         $sustained = $calling[$i]['sustained'];
3491         $organization = $calling[$i]['organization'];
3492         $this->t->set_var('name', $name);
3493         $this->t->set_var('position', $position);
3494         $this->t->set_var('sustained', $sustained);
3495         $this->t->set_var('organization', $organization);
3496         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3497         $this->t->set_var('tr_color',$tr_color);
3498         $this->t->fp('list1','calling_list',True);
3499       }
3500
3501       # Display a list ordered by organization
3502       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
3503       $this->db->query($sql,__LINE__,__FILE__);
3504       $i=0;
3505       while ($this->db->next_record())
3506         {
3507           $calling[$i]['id'] = $this->db->f('indiv_id');
3508           $calling[$i]['name'] = $this->db->f('name');
3509           $calling[$i]['position'] = $this->db->f('position');
3510           $calling[$i]['sustained'] = $this->db->f('sustained');
3511           $calling[$i]['organization'] = $this->db->f('organization');
3512           $i++;
3513         }   
3514       for ($i=0; $i < count($calling); $i++) 
3515       {
3516         $name = $calling[$i]['name'];
3517         $position = $calling[$i]['position'];
3518         $sustained = $calling[$i]['sustained'];
3519         $organization = $calling[$i]['organization'];
3520         $this->t->set_var('name', $name);
3521         $this->t->set_var('position', $position);
3522         $this->t->set_var('sustained', $sustained);
3523         $this->t->set_var('organization', $organization);
3524         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3525         $this->t->set_var('tr_color',$tr_color);
3526         $this->t->fp('list2','org_list',True);
3527       }
3528       
3529       $this->t->pfp('out','org_view_t');
3530       $this->save_sessiondata();   
3531     }
3532   
3533   function schedule()
3534     {
3535       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3536       $this->t->set_block('sched_t','presidency_list','list');
3537
3538       $action = get_var('action',array('GET','POST'));
3539       
3540       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3541       $this->t->set_var('title','EQ Scheduling Tool');
3542
3543       $this->t->set_var('lang_save','Save Schedule');
3544       $this->t->set_var('lang_reset','Cancel');
3545       
3546       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3547       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3548
3549       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3550       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3551       
3552       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3553       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3554       
3555       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3556       $table_width=$date_width + $time_width + $elder_width + $family_width;
3557       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3558       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3559       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3560       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3561       $table_data = "";
3562
3563       if($action == 'save')
3564         {
3565           $new_data = get_var('sched',array('POST'));
3566           foreach ($new_data as $presidency_array)
3567            {
3568              foreach ($presidency_array as $entry)
3569                {
3570                  $presidency = $entry['presidency'];
3571                  $appointment = $entry['appointment'];
3572                  $date = $entry['date'];
3573                  $hour = $entry['hour'];
3574                  $minute = $entry['minute'];
3575                  $pm = $entry['pm'];
3576                  $elder = $entry['elder'];
3577                  $family = $entry['family'];
3578                  if($pm) { $hour = $hour + 12; }
3579                  $time = $hour.':'.$minute.':'.'00';
3580                  $uid = 0;
3581
3582                  // Zero out the family or elder if date = NULL
3583                  if($date == "") {
3584                    $elder = 0;
3585                    $family = 0;
3586                  }
3587                  
3588                  // Update an existing appointment
3589                  if($appointment < $this->max_appointments)
3590                    {
3591                      //Only perform a database update if we have made a change to this appointment
3592                      $sql = "SELECT * FROM eq_appointment where " .
3593                         "appointment='$appointment'" .
3594                         " and presidency='$presidency'" .
3595                         " and elder='$elder'" .
3596                         " and family='$family'" .
3597                         " and date='$date'" .
3598                         " and time='$time'";
3599                      $this->db->query($sql,__LINE__,__FILE__);
3600                      if(!$this->db->next_record()) {
3601                        $old_date = $this->db->f('date');
3602                        $old_time = $this->db->f('time');
3603                        $this->db2->query("UPDATE eq_appointment set" .
3604                                         " family=" . $family . 
3605                                         " ,elder=" . $elder . 
3606                                         " ,date='" . $date . "'" .
3607                                         " ,time='" . $time . "'" .
3608                                         " ,presidency='" . $presidency . "'" .
3609                                         " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3610                        
3611                        // Email the appointment
3612                        $this->email_appt($appointment);
3613                      }
3614                    }
3615                  
3616                  // Add a new appointment
3617                  else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3618                    {
3619                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";
3620                      $this->db2->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time,uid) "
3621                            . "VALUES (NULL,'" . $presidency . "','" . $family . "','"
3622                            . $elder . "','" . $date . "','" . $time  . "','" . $uid ."')",__LINE__,__FILE__);
3623
3624                      // Now reselect this entry from the database to see if we need
3625                      // to send an appointment out for it.
3626                      $sql = "SELECT * FROM eq_appointment where " .
3627                         "elder='$elder'" .
3628                         " and family='$family'" .
3629                         " and presidency='$presidency'" .
3630                         " and date='$date'" .
3631                         " and time='$time'" .
3632                         " and uid='$uid'";
3633                      $this->db3->query($sql,__LINE__,__FILE__);
3634                      if($this->db3->next_record()) {
3635                        // Email the appointment if warranted
3636                        if(($date != "") && ($time != "") && (($elder > 0) || $family > 0)) { 
3637                          $this->email_appt($this->db3->f('appointment'));
3638                        }
3639                      }
3640                    }
3641                }
3642            }
3643           
3644           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3645           //Header('Location: ' . $take_me_to_url);
3646         }
3647
3648       $sql = "SELECT * FROM eq_presidency where valid=1";
3649       $this->db->query($sql,__LINE__,__FILE__);
3650       $i=0;
3651       while ($this->db->next_record())
3652         {
3653           $presidency_data[$i]['id'] = $this->db->f('presidency');
3654           $presidency_data[$i]['name'] = $this->db->f('name');
3655           $presidency_data[$i]['elder'] = $this->db->f('elder');
3656           $i++;
3657         }
3658       
3659       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3660       $this->db->query($sql,__LINE__,__FILE__);
3661       $i=0;
3662       while ($this->db->next_record())
3663         {
3664           $elder_id[$i] = $this->db->f('elder');
3665           $elder_name[$i] = $this->db->f('name');
3666           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3667           $i++;
3668         }
3669       array_multisort($elder_name, $elder_id);
3670
3671       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3672       $this->db->query($sql,__LINE__,__FILE__);
3673       $i=0;
3674       while ($this->db->next_record())
3675         {
3676           $family_id[$i] = $this->db->f('family');
3677           $family_name[$i] = $this->db->f('name');
3678           $i++;
3679         }
3680       array_multisort($family_name, $family_id);
3681       
3682       for ($i=0; $i < count($presidency_data); $i++) {
3683         $presidency = $presidency_data[$i]['id'];
3684         $interviewer = $presidency_data[$i]['elder'];
3685         $name = $presidency_data[$i]['name'];
3686         $this->t->set_var('presidency_name',$name);
3687         $table_data="";
3688         
3689         // query the database for all the appointments
3690         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3691         $this->db->query($sql,__LINE__,__FILE__);
3692
3693         // Prefill any existing appointment slots
3694         while ($this->db->next_record())
3695           {
3696             $appointment = $this->db->f('appointment');
3697             $elder = $this->db->f('elder');
3698             $family = $this->db->f('family');
3699             
3700             $date = $this->db->f('date');
3701             $date_array = explode("-",$date);
3702             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3703             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3704             
3705             $time = $this->db->f('time');
3706             $time_array = explode(":",$time);
3707             $hour = $time_array[0];
3708             $minute = $time_array[1];
3709             $pm = 0;
3710             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3711             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3712             
3713             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3714
3715             // Date selection
3716             $table_data.= '<td align=left>';
3717             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3718             $table_data.= '</td>';
3719             
3720             // Hour & Minutes selection
3721             $table_data.= "<td align=center>";
3722             $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
3723             $table_data.= "</td>";
3724             
3725             // Elder drop down list (for PPIs)
3726             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3727             $table_data.= '<option value=0></option>';  
3728             for ($j=0; $j < count($elder_id); $j++) {
3729               $id = $elder_id[$j];
3730               $name = $elder_name[$j];
3731               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3732               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3733             }
3734             $table_data.='</select></td>';
3735
3736             // Family drop down list (for Visits)
3737             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3738             $table_data.= '<option value=0></option>';              
3739             for ($j=0; $j < count($elder_id); $j++) {
3740               $id = $family_id[$j];
3741               $name = $family_name[$j];
3742               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3743               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3744             }
3745             $table_data.='</select></td>';
3746             
3747             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3748             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3749         
3750             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3751             $this->t->set_var('tr_color',$tr_color);
3752             
3753           }
3754
3755         // Create blank appointment slot
3756         for ($b=0; $b < 4; $b++) {
3757           $appointment = $this->max_appointments + $b;
3758           $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3759
3760           // Date selection
3761           $table_data.= '<td align=left>';
3762           $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3763           $table_data.= '</td>';
3764         
3765           // Time selection
3766           $table_data.= "<td align=center>";
3767           $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
3768           $table_data.= "</td>";
3769           
3770           // Elder drop down list
3771           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3772           $table_data.= '<option value=0></option>';  
3773           for ($j=0; $j < count($elder_id); $j++) {
3774             $id = $elder_id[$j];
3775             $name = $elder_name[$j];
3776             $table_data.= '<option value='.$id.'>'.$name.'</option>';
3777           }
3778           $table_data.='</select></td>';
3779           
3780           // Family drop down list
3781           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3782           $table_data.= '<option value=0></option>';        
3783           for ($j=0; $j < count($elder_id); $j++) {
3784             $id = $family_id[$j];
3785             $name = $family_name[$j];
3786             $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3787           }
3788           $table_data.='</select></td>';
3789           
3790           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3791           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3792
3793           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3794           $this->t->set_var('tr_color',$tr_color);
3795         }
3796         
3797         $this->t->set_var('table_data',$table_data);
3798         $this->t->set_var('header_row',$header_row);
3799         $this->t->set_var('table_width',$table_width);
3800         $this->t->fp('list','presidency_list',True);
3801         
3802       }
3803       
3804       $this->t->pfp('out','sched_t');
3805       $this->save_sessiondata();   
3806     }
3807   
3808   function admin()
3809     {
3810       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3811       $this->t->set_block('admin_t','upload','uploadhandle');
3812       $this->t->set_block('admin_t','admin','adminhandle');
3813       $this->t->set_block('admin_t','cmd','cmdhandle');
3814       $this->t->set_block('admin_t','presidency','presidencyhandle');
3815       
3816       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3817       $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=presidency'));
3818       
3819       $action = get_var('action',array('GET','POST'));
3820
3821       $this->t->pfp('out','admin_t');
3822
3823       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3824       $this->db->query($sql,__LINE__,__FILE__);
3825       $i=0;
3826       while ($this->db->next_record())
3827         {
3828           $elder_id[$i] = $this->db->f('elder');
3829           $elder_name[$i] = $this->db->f('name');
3830           $elder2name[$elder_id[$i]] = $elder_name[$i];
3831           $i++;
3832         }
3833       array_multisort($elder_name, $elder_id);
3834
3835       if($action == 'upload')
3836         {        
3837           $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
3838           
3839           if(($_FILES['uploadedfile']['type'] == "application/zip") ||
3840              ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3841              ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3842              ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
3843
3844             if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
3845               $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
3846               $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
3847               $this->t->set_var('uploadstatus',$uploadstatus);
3848               $this->t->pfp('uploadhandle','upload',True);
3849               return 0;
3850             }
3851             
3852             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3853             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3854             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3855             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3856             $this->t->set_var('uploadstatus',$uploadstatus);
3857             $this->t->pfp('uploadhandle','upload');
3858             $this->t->set_var('uploadhandle','');
3859             print "<table border=1 width=80%><tr><td>\n<pre>";
3860             
3861             # make a directory for this data to be stored in
3862             $date="data_" . date("Y_m_d");
3863             $data_dir = $this->upload_target_path . '/' . $date;
3864             print "-> Making the data directory: $date<br>\n";
3865             exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
3866             if($return_code != 0) {
3867               print implode('\n',$result) . "<br>";
3868               print "<b><font color=red>";
3869               print "-E- Unable to create the data directory. Aborting import.";
3870               print "</font></b>";
3871               return 0;
3872             }
3873
3874             # move the file uploaded into this directory
3875             print "-> Moving the uploaded file into the data dir<br>\n";
3876             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3877             if($return_code != 0) {
3878               print implode('\n',$result) . "<br>";
3879               print "<b><font color=red>";
3880               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3881               print "</font></b>";
3882               return 0;
3883             }
3884             
3885             # unzip the data into this directory
3886             print "-> Unzipping the data<br>\n";
3887             exec($this->unzip_path .' -u '. $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3888             if($return_code != 0) {
3889               print implode('\n',$result) . "<br>";
3890               print "<b><font color=red>";
3891               print "-E- Unable to unzip the uploaded file into the data dir: $data_dir. Aborting import.";
3892               print "</font></b>";
3893               return 0;
3894             }
3895             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3896
3897             # update the data_latest link to point to this new directory
3898             print "-> Updating the latest data dir link<br>\n";
3899             $data_latest = $this->upload_target_path . '/data_latest';
3900             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3901             if($return_code != 0) {
3902               print implode('\n',$result) . "<br>";
3903               print "<b><font color=red>";
3904               print "-E- Unable to update the data latest link. Aborting import.";
3905               print "</font></b>";
3906               return 0;
3907             }
3908             
3909             # run the import perl script to encorporate it into the DB
3910             ob_start('ob_logstdout', 2);
3911             print "-> Importing the data into the EQ database<br>\n";
3912             ob_flush(); flush(); sleep(1);
3913             $import_log = $this->upload_target_path . '/import.log';
3914             $data_log = $this->upload_target_path . '/data.log';
3915             $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
3916             $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
3917             #print "import_cmd: $import_cmd<br>";
3918             #print "parse_cmd: $parse_cmd<br>";
3919             ob_start('ob_logstdout', 2);
3920             passthru($import_cmd);
3921             passthru($parse_cmd);
3922             ob_flush(); flush(); sleep(1);
3923
3924             # fix the permissions of the data dir
3925             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3926             
3927             $this->t->pfp('cmdhandle','cmd');
3928             print "</pre></td></tr></table>";
3929             
3930           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3931                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3932                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3933                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3934             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3935             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3936             $this->t->set_var('uploadstatus',$uploadstatus);
3937             $this->t->pfp('uploadhandle','upload',True);
3938           } else {
3939             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3940             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3941             $this->t->set_var('uploadstatus',$uploadstatus);
3942             $this->t->pfp('uploadhandle','upload',True);
3943           }
3944         }
3945       else if($action == "presidency")
3946         {
3947           $new_data = get_var('eqpres',array('POST'));
3948           foreach ($new_data as $entry)
3949            {
3950              $id = $entry['id'];
3951              $email = $entry['email'];
3952              $elder = $entry['elder'];
3953              $name = $entry['name'];
3954              $district = $entry['district'];
3955              $president = $entry['president'];
3956              $counselor = $entry['counselor'];
3957              $secretary = $entry['secretary'];
3958              $eqpresidency = $entry['eqpresidency'];
3959              // Set the elder id to 0 for EQ Presidency tagged entry
3960              if($eqpresidency == 1) { $elder="0"; }
3961              // Re-look up the elder name for the ID if we aren't an EQ Presidency tagged entry
3962              else { $name = $elder2name[$elder]; }
3963              //print "id=$id elder=$elder name=$name email=$email district=$district president=$president ";
3964              //print "counselor=$counselor secretary=$secretary eqpres=$eqpresidency<br>";
3965
3966              if(($elder > 0) || ($name != "")) {
3967                if($id < $this->max_presidency_members) {
3968                  //print "Updating Existing Entry<br>";
3969                  $this->db2->query("UPDATE eq_presidency set" .
3970                                    " elder=" . $elder . 
3971                                    " ,district=" . $district . 
3972                                    " ,name='" . $name . "'" .
3973                                    " ,email='" . $email . "'" .
3974                                    " ,president='" . $president . "'" .
3975                                    " ,counselor='" . $counselor . "'" .
3976                                    " ,secretary='" . $secretary . "'" .
3977                                    " ,eqpres='" . $eqpresidency . "'" .
3978                                    " WHERE presidency=" . $id,__LINE__,__FILE__);
3979                  
3980                } else {
3981                  //print "Adding New Entry<br>";
3982                  $this->db2->query("INSERT INTO eq_presidency (presidency,elder,district,name,"
3983                                    . "email,president,counselor,secretary,eqpres,valid) "
3984                                    . "VALUES (NULL,'" . $elder . "','" . $district . "','"
3985                                    . $name . "','" . $email . "','" . $president  . "','"
3986                                    . $counselor . "','" . $secretary . "','" . $eqpres  . "','1'"
3987                                    .")",__LINE__,__FILE__);
3988                }
3989              } else {
3990                //print "Ignoring Blank Entry<br>";
3991              }
3992            }
3993
3994           // Now update the eq_district table appropriately
3995           
3996           // Delete all the previous district entries from the table
3997           $this->db->query("DELETE from eq_district where valid=1",__LINE__,__FILE__);
3998           $this->db->query("DELETE from eq_district where valid=0",__LINE__,__FILE__);
3999
4000           // Always add a "District 0" assigned to the High Priests Group
4001           $district = 0;
4002           $name = "High Priests";
4003           $elder = 0;
4004           $valid = 0;
4005           $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4006                             . "VALUES ('" . $district . "','" . $name . "','"
4007                             . $elder . "','" . $valid . "'"
4008                             .")",__LINE__,__FILE__);
4009           
4010           
4011           // Requery the eq_presidency table
4012           $sql = "SELECT * FROM eq_presidency where valid=1";
4013           $this->db->query($sql,__LINE__,__FILE__);
4014           while ($this->db->next_record())
4015             {
4016               // Extract the data for each presidency record
4017               $id = $this->db->f('presidency');
4018               $elder = $this->db->f('elder');
4019               $name = $this->db->f('name');
4020               $district = $this->db->f('district');
4021               $name = $this->db->f('name');
4022               $valid = 1;
4023
4024               // If we have a valid district, add it to the district table
4025               if($district > 0) {
4026                 $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4027                                   . "VALUES ('" . $district . "','" . $name . "','"
4028                                   . $elder . "','" . $valid . "'"
4029                                   .")",__LINE__,__FILE__);
4030               }
4031               
4032             }
4033           
4034           $this->t->set_var('adminhandle','');
4035           $this->t->pfp('adminhandle','admin'); 
4036         }
4037       else
4038         {
4039           $this->t->set_var('adminhandle','');
4040           $this->t->pfp('adminhandle','admin'); 
4041         }
4042
4043       // Now save off the data needed for an EQ Presidency Table Update
4044       
4045       $sql = "SELECT * FROM eq_presidency where valid=1";
4046       $this->db->query($sql,__LINE__,__FILE__);
4047       $table_data = "";
4048       $header_row = "<th>Elder</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th><th>EQ Presidency</th>";
4049       while ($this->db->next_record())
4050         {
4051           // Extract the data for each presidency record
4052           $id = $this->db->f('presidency');
4053           $elder = $this->db->f('elder');
4054           $district = $this->db->f('district');
4055           $name = $this->db->f('name');
4056           $email = $this->db->f('email');
4057           $president = $this->db->f('president');
4058           $counselor = $this->db->f('counselor');
4059           $secretary = $this->db->f('secretary');
4060           $eqpresidency = $this->db->f('eqpres');
4061
4062           // Create the forms needed in the table
4063           $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4064           
4065           // Presidency ID
4066           $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4067           
4068           // Elder
4069           if($eqpresidency == 0) { 
4070             $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4071             $table_data.= '<option value=0></option>';  
4072             for ($j=0; $j < count($elder_id); $j++) {
4073               $tmp_id = $elder_id[$j];
4074               $name = $elder_name[$j];
4075               if($elder_id[$j] == $elder) { $eldername = $name; $selected = 'selected="selected"'; } else { $selected = ''; }
4076               $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4077             }
4078             $table_data.='</select></td>';
4079             $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$eldername.'">';
4080           } else {
4081             $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="EQ Presidency"></td>';
4082             $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="EQ Presidency">';
4083           }
4084             
4085           // Email Address
4086           $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4087           
4088           // District
4089           $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4090           $table_data.= '<option value=0></option>';
4091           for ($j=0; $j <= $this->max_num_districts; $j++) {
4092             if($district == $j) { $selected = 'selected="selected"'; } else { $selected = ''; }
4093             $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4094           }
4095           $table_data.='</select></td>';
4096           
4097           // President
4098           $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4099           if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4100           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4101           $table_data.='</select></td>';
4102           
4103           // Counselor
4104           $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4105           if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4106           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4107           $table_data.='</select></td>';
4108
4109           // Secretary
4110           $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4111           if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4112           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4113           $table_data.='</select></td>';
4114
4115           // EQ Presidency
4116           $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4117           if($eqpresidency == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4118           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4119           $table_data.='</select></td>';
4120
4121           // End of ROW
4122           $table_data .= "</tr>\n";
4123           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4124           $this->t->set_var('tr_color',$tr_color);
4125         }
4126
4127       // Now create 1 blank row to always have a line available to add a new elder with
4128       $id = $this->max_presidency_members;
4129       $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4130       // Presidency ID
4131       $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4132       // Elder
4133       $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4134       $table_data.= '<option value=0></option>';  
4135       for ($j=0; $j < count($elder_id); $j++) {
4136         $tmp_id = $elder_id[$j];
4137         $name = $elder_name[$j];
4138         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4139       }
4140       $table_data.='</select></td>';
4141       $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4142       // Email Address
4143       $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4144       // District
4145       $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4146       $table_data.= '<option value=0></option>';
4147       for ($j=0; $j <= $this->max_num_districts; $j++) {
4148         if($j == 0) { $selected = 'selected="selected"'; } else { $selected = ''; }
4149         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4150       }
4151       $table_data.='</select></td>';
4152       // President
4153       $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4154       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4155       $table_data.='</select></td>';
4156       // Counselor
4157       $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4158       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4159       $table_data.='</select></td>';
4160       // Secretary
4161       $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4162       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4163       $table_data.='</select></td>';
4164       // EQ Presidency
4165       $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4166       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4167       $table_data.='</select></td>';
4168       // End of ROW
4169       $table_data .= "</tr>\n";
4170       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4171       $this->t->set_var('tr_color',$tr_color);
4172       
4173       $this->t->set_var('header_row',$header_row);
4174       $this->t->set_var('table_data',$table_data);
4175       $this->t->pfp('presidencyhandle','presidency',True);
4176
4177       $this->save_sessiondata();   
4178     }
4179
4180   function email_appt($appointment)
4181     {
4182       //print "Emailing notification of appointment: $appointment <br>";
4183
4184       $sql = "SELECT * FROM eq_appointment where appointment='$appointment'";
4185       $this->db->query($sql,__LINE__,__FILE__);
4186         
4187       while ($this->db->next_record())
4188         {
4189           $appointment = $this->db->f('appointment');
4190           $presidency = $this->db->f('presidency');
4191           $interviewer = "";
4192           $email = "";
4193           $elder = $this->db->f('elder');
4194           $elder_name = "";
4195           $family = $this->db->f('family');
4196           $family_name = "";
4197           $appt_name = "";
4198           $phone = "";
4199           $location = "";
4200           $uid = $this->db->f('uid');
4201                     
4202           // Extract the year, month, day, hours, minutes, seconds from the appointment time
4203           $appt_date = $this->db->f('date');
4204           $date_array = explode("-",$appt_date);
4205           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4206           $appt_time = $this->db->f('time');
4207           $time_array = explode(":",$appt_time);
4208           $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4209
4210           // Format the appointment time into an iCal UTC equivalent
4211           $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4212           $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4213           $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4214           
4215           // Set the email address of the person making the appointment
4216           $from = $GLOBALS['phpgw_info']['user']['fullname'] . "<" .
4217                   $GLOBALS['phpgw_info']['user']['preferences']['email']['address'] . ">";
4218           
4219           $sql = "SELECT * FROM eq_presidency where presidency='$presidency'";
4220           $this->db2->query($sql,__LINE__,__FILE__);
4221           if($this->db2->next_record()) {
4222             $email = $this->db2->f('email');
4223             $interviewer = $this->db2->f('name');
4224           }
4225
4226           if($elder > 0) { 
4227             $sql = "SELECT * FROM eq_elder where elder='$elder'";
4228             $this->db2->query($sql,__LINE__,__FILE__);
4229             if($this->db2->next_record()) {
4230               $elder_name = $this->db2->f('name');
4231               $phone = $this->db2->f('phone');
4232               $appt_name = $elder_name . " Interview";
4233               $location = "$interviewer"."'s home";
4234               $duration = $this->default_ppi_appt_duration * 60;
4235             }
4236           }
4237
4238           if($family > 0) { 
4239             $sql = "SELECT * FROM eq_family where family='$family'";
4240             $this->db2->query($sql,__LINE__,__FILE__);
4241             if($this->db2->next_record()) {
4242               $family_name = $this->db2->f('name');
4243               $phone = $this->db2->f('phone');
4244               $elder_id = $this->db2->f('elder_id');
4245               $appt_name = $family_name . " Family Visit";
4246               $sql = "SELECT * FROM eq_elder where elder='$elder_id'";
4247               $this->db3->query($sql,__LINE__,__FILE__);
4248               if($this->db3->next_record()) {
4249                 $phone = $this->db3->f('phone');
4250               }
4251               $sql = "SELECT * FROM eq_parent where family='$family'";
4252               $this->db3->query($sql,__LINE__,__FILE__);
4253               if($this->db3->next_record()) {
4254                 $location=$this->db3->f('address');
4255               }
4256               $duration = $this->default_visit_appt_duration * 60;
4257             }
4258           }
4259
4260           $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4261           $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4262           $date = $dtstartstr . "-" . $dtendstr;
4263           $description = "$appt_name : $phone";
4264           
4265           if(($uid == 0) && ($appt_name != "")) { 
4266             // Create a new calendar item for this appointment, since this must be the first time we
4267             // are sending it out.
4268             print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4269             $uid = rand() . rand(); // Generate a random identifier for this appointment
4270             $subject = "Created: $appt_name";
4271             
4272             $this->db->query("UPDATE eq_appointment set" .
4273                              " uid=" . $uid . 
4274                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4275
4276             $action = "PUBLISH";
4277             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4278                                   $dtend, $date, $location, $appt_name, $description, $uid);
4279             
4280           } else if(($uid != 0) && ($appt_name == "")) {
4281             // Remove the calendar item for this appointment since it has already been sent
4282             // and there is no name we have changed it to.
4283             print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4284             $subject = "Canceled: $appt_date $appt_time";
4285             
4286             $this->db->query("UPDATE eq_appointment set" .
4287                              " uid=0" . 
4288                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4289             
4290             $action = "CANCEL";
4291             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4292                                   $dtend, $date, $location, $subject, $subject, $uid);
4293             
4294           } else if($uid != 0) {
4295             // Update the existing appointment since we have changed it
4296             print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4297
4298             $subject = "Canceled: $appt_date $appt_time";
4299             $action = "CANCEL";
4300             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4301                                   $dtend, $date, $location, $subject, $subject, $uid);
4302             
4303             $uid = rand() . rand(); // Generate a random identifier for this appointment
4304             $this->db->query("UPDATE eq_appointment set" .
4305                              " uid=" . $uid .
4306                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4307             
4308             $subject = "Updated: $appt_name";       
4309             $action = "PUBLISH";
4310             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4311                                   $dtend, $date, $location, $appt_name, $description, $uid);
4312           }
4313           
4314         }
4315           
4316       return true;
4317     }
4318
4319   function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4320     {
4321       // Initialize our local variables
4322       $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4323       $message = "";
4324       $headers = "";
4325
4326       // Form the headers for the email message
4327       $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4328       $headers.="Mime-Version: 1.0\n";
4329       $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4330       $headers.="Content-Disposition: inline\n";
4331       $headers.="Reply-To: $from\n";
4332       $headers.="From: $from\n";
4333
4334       // Print the plaintext version of the appointment
4335       $message.="--$boundary\n";
4336       $message.="Content-Type: text/plain; charset=us-ascii\n";
4337       $message.="Content-Disposition: inline\n";
4338       $message.="\n";
4339       $message.="What: $description\n";
4340       $message.="When: $date\n";
4341       $message.="Where: $location\n";
4342       $message.="\n";
4343       
4344       // Print the .ics attachment version of the appointment
4345       $message.="--$boundary\n";
4346       $message.="Content-Type: text/calendar; charset=us-ascii\n";
4347       $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4348       $message.="\n";
4349       $message.="BEGIN:VCALENDAR" . "\n";
4350       $message.="VERSION:2.0" . "\n";
4351       $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4352       $message.="METHOD:$action" . "\n";
4353       $message.="BEGIN:VEVENT" . "\n";
4354       $message.="ORGANIZER:MAILTO:$from". "\n";
4355       $message.="DTSTAMP:$dtstamp" . "\n";
4356       $message.="DTSTART:$dtstart" . "\n";
4357       $message.="DTEND:$dtend" . "\n";
4358       $message.="SUMMARY:$summary" . "\n";
4359       $message.="DESCRIPTION:$description" . "\n";
4360       $message.="LOCATION:$location" . "\n";
4361       $message.="UID:$uid" ."\n";
4362       $message.="TRANSP:OPAQUE" . "\n";
4363       $message.="SEQUENCE:0" . "\n";
4364       $message.="CLASS:PUBLIC" . "\n";
4365       $message.="END:VEVENT" . "\n";
4366       $message.="END:VCALENDAR" . "\n";
4367
4368       // Complete the message
4369       $message.="--$boundary\n";
4370
4371       // Send the message
4372       mail($to, $subject, $message, $headers);
4373       
4374     }
4375
4376   function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4377     {
4378       $form_data = "";
4379       $blank = 0;
4380       
4381       if($hour == 0) { $blank = 1; }
4382
4383       if($this->time_drop_down_lists == 1) {
4384         // Create drop down lists to get the time
4385         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4386         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4387         foreach(range(1,12) as $num) {
4388           if($hour == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4389           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4390         }
4391         $form_data.= '</select>';
4392         $form_data.= '&nbsp;:&nbsp;';
4393         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4394         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4395         $num = 0;
4396         while($num < 60) {
4397           if($num < 10) { $num = "0" . "$num"; }
4398           if($minute == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4399           if($blank == 1) { $selected = ""; }
4400           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4401           $num = $num + $this->time_drop_down_list_inc;
4402         }
4403         $form_data.= '</select>';
4404       } else {
4405         // Use free form text fields to get the time
4406         if($blank == 1) { $hour = ""; $minute = ""; $ampm = ""; }
4407         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4408         $form_data.= ':';
4409         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4410         $form_data.= '&nbsp;';
4411       }
4412       // Always use a drop-down select form for am/pm
4413       $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4414       if($blank == 0) { 
4415         if($pm == 0) { $form_data.= '<option value=0 selected>am</option>'; $form_data.= '<option value=1>pm</option>'; }
4416         if($pm == 1) { $form_data.= '<option value=0>am</option>'; $form_data.= '<option value=1 selected>pm</option>'; }
4417       } else {
4418         $form_data.= '<option value=""></option>';
4419         $form_data.= '<option value=0>am</option>';
4420         $form_data.= '<option value=1>pm</option>';
4421       }
4422       $form_data.= '</select>';
4423       
4424       return $form_data;
4425     }
4426 }
4427
4428 ?>