Switched PPIs back to just being done by the EQ President.
[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 2001/05/20 07:40:32 seek3r 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_months;
26   var $default_int_num_years;
27   var $default_att_num_months;
28   var $current_year;
29   var $current_month;
30   var $upload_target_path;
31   var $script_path;
32   
33   var $public_functions = array
34     (
35      'ht_view'    => True,
36      'ht_update'  => True,
37      'act_list'   => True,
38      'act_view'   => True,
39      'act_update' => True,
40      'par_view'   => True,
41      'ppi_view'   => True,
42      'ppi_update' => True,
43      'ppi_sched'  => True,
44      'int_view'   => True,
45      'int_update' => True,
46      'int_sched'  => True,
47      'vis_sched'  => True,
48      'vis_view'   => True,
49      'vis_update' => True,
50      'att_view'   => True,
51      'att_update' => True,
52      'dir_view'   => True,
53      'org_view'   => True,
54      'schedule'   => True,
55      'admin'      => True
56      );
57  
58   function eq()
59     {
60       $this->default_ht_num_months = 3;
61       $this->default_ppi_num_months = 3;
62       $this->default_ppi_num_years = 0;
63       $this->default_int_num_months = 3;
64       $this->default_int_num_years = 0;
65       $this->default_att_num_months = 3;
66       $this->upload_target_path = "/home/users/eqpres/eq_data/";
67       $this->script_path = "/usr/share/phpgroupware/eq/";
68       
69       $this->db         = $GLOBALS['phpgw']->db;
70       $this->db2        = $this->db;
71       $this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
72       $this->t          = $GLOBALS['phpgw']->template;
73       $this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
74       $this->grants     = $GLOBALS['phpgw']->acl->get_grants('eq');
75       $this->grants[$this->account] = PHPGW_ACL_READ + PHPGW_ACL_ADD + PHPGW_ACL_EDIT + PHPGW_ACL_DELETE;
76        
77       $this->jscal = CreateObject('phpgwapi.jscalendar');   // before phpgw_header() !!!
78       $this->cal_options = 'daFormat    : "%Y-%m-%d",
79                                 ifFormat    : "%Y-%m-%d",
80                                 mondayFirst : false,
81                                 weekNumbers : false';
82        
83       $GLOBALS['phpgw_info']['flags']['app_header'] = 'Elders Quorum Tools';
84       $GLOBALS['phpgw']->common->phpgw_header();
85
86       $this->current_day = `date '+%d'`;
87       $this->current_day = $this->current_day-0; // Make it numeric
88       $this->current_month = `date '+%m'`;
89       $this->current_month = $this->current_month-0; // Make it numeric
90       $this->current_year = `date '+%Y'`;
91       $this->current_year = $this->current_year-0; // Make it numeric
92
93       echo parse_navbar();
94       $this->display_app_header();      
95     }
96   
97   function save_sessiondata()
98     {
99       
100     }
101
102   function display_app_header()
103     {
104       $this->t->set_file(array('eq_header' => 'header.tpl'));
105       
106       if (isset($phpgw_info['user']['preferences']['eq']['eq_font']))
107         {
108           $font = $phpgw_info['user']['preferences']['eq']['eq_font'];
109         }
110       else
111         {
112           $font = 'Arial';
113         }
114       
115       $this->t->set_var('bg_color',$phpgw_info['theme']['th_bg']);
116       $this->t->set_var('font',$font);
117       $link_data['menuaction'] = 'eq.eq.ht_view';
118       $this->t->set_var('link_hometeaching',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
119       $this->t->set_var('lang_hometeaching','HomeTeaching');
120       $link_data['menuaction'] = 'eq.eq.act_list';
121       $this->t->set_var('link_activity',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
122       $this->t->set_var('lang_activity','Activities');
123       $link_data['menuaction'] = 'eq.eq.par_view';
124       $this->t->set_var('link_participation',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
125       $this->t->set_var('lang_participation','Participation');
126       $link_data['menuaction'] = 'eq.eq.ppi_view';
127       $this->t->set_var('link_ppi',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
128       $this->t->set_var('lang_ppi','PPIs');
129       $link_data['menuaction'] = 'eq.eq.int_view';
130       $this->t->set_var('link_int',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
131       $this->t->set_var('lang_int','Interviews');
132       $link_data['menuaction'] = 'eq.eq.vis_view';
133       $this->t->set_var('link_visit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
134       $this->t->set_var('lang_visit','Visits');
135       $link_data['menuaction'] = 'eq.eq.att_view';      
136       $this->t->set_var('link_attendance',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
137       $this->t->set_var('lang_attendance','Attendance');
138       $link_data['menuaction'] = 'eq.eq.dir_view';      
139       $this->t->set_var('link_dir',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
140       $this->t->set_var('lang_dir','Directory');
141       $link_data['menuaction'] = 'eq.eq.org_view';      
142       $this->t->set_var('link_org',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
143       $this->t->set_var('lang_org','Callings');
144       $link_data['menuaction'] = 'eq.eq.admin'; 
145       $this->t->set_var('link_admin',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
146       $this->t->set_var('lang_admin','Admin');
147       $link_data['menuaction'] = 'eq.eq.schedule';      
148       $this->t->set_var('link_schedule',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
149       $this->t->set_var('lang_schedule','Scheduling');
150                 
151       $this->t->pparse('out','eq_header');
152     }
153
154   function ht_view()
155     {
156       $this->t->set_file(array('ht_view_t' => 'ht_view.tpl'));
157       $this->t->set_block('ht_view_t','district_list','list');
158    
159       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
160       $num_months = get_var('num_months',array('GET','POST'));
161       if($num_months == '') { $num_months = $this->default_ht_num_months; }
162       $this->t->set_var('num_months',$num_months);
163       $this->t->set_var('lang_filter','Filter');
164       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
165       else {  $this->t->set_var('lang_num_months','Months of History'); }
166       
167       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
168       $this->t->set_var('title','Hometeaching'); 
169       
170       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
171       $this->db->query($sql,__LINE__,__FILE__);
172       $i=0;
173       while ($this->db->next_record())
174         {
175           $districts[$i]['district'] = $this->db->f('district');
176           $districts[$i]['name'] = $this->db->f('name');
177           $districts[$i]['supervisor'] = $this->db->f('supervisor');
178           $i++;
179         }
180
181       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
182       $this->db->query($sql,__LINE__,__FILE__);
183       $i=0;
184       while ($this->db->next_record())
185         {
186           $elder_id[$i] = $this->db->f('elder');
187           $elder_name[$i] = $this->db->f('name');
188           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
189           $i++;
190         }
191       array_multisort($elder_name, $elder_id);
192
193       // Make an array mapping elder_ids to elder_names
194       for($i=0; $i < count($elder_id); $i++) {
195           $id = $elder_id[$i];
196           $elders[$id] = $elder_name[$i];
197       }      
198
199       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
200       $this->db->query($sql,__LINE__,__FILE__);
201       while ($this->db->next_record())
202         {
203           $aaronic_id = $this->db->f('aaronic');
204           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
205           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
206         }
207       
208       $total_families = 0;
209       $this->nextmatchs->template_alternate_row_color(&$this->t);
210       for ($i=0; $i < count($districts); $i++) {
211         $this->t->set_var('district_number',$districts[$i]['district']);
212         $this->t->set_var('district_name',$districts[$i]['name']);      
213         $supervisor = $districts[$i]['supervisor'];
214         $unique_companionships='';
215                 
216         // Select all the unique companionship numbers for this district
217         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
218         $this->db->query($sql,__LINE__,__FILE__);
219         $j=0;
220         while ($this->db->next_record())
221           {
222             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
223             $j++;
224           }
225
226         $comp_width=450; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
227         $table_data=""; $num_companionships = 0; $num_families = 0;
228         for($m=$num_months; $m >= 0; $m--) { $visits[$m] = 0; }
229         for ($j=0; $j < count($unique_companionships); $j++) {
230           $companion_table_entry = "";
231           // Select all the companions in each companionship
232           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
233                  "companionship=". $unique_companionships[$j]['companionship'];
234           $this->db->query($sql,__LINE__,__FILE__);
235
236           while ($this->db->next_record())
237             {
238               // Get this companions information
239               if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
240               $companionship = $this->db->f('companionship');
241               $elder_id = $this->db->f('elder');
242               $aaronic_id = $this->db->f('aaronic');
243               if($elder_id) {
244                 $name = $elders[$elder_id];
245                 $phone = $elder_phone[$elder_id];
246               }
247               else if($aaronic_id) {
248                 $name = $aaronic[$aaronic_id]['name'];
249                 $phone = $aaronic[$aaronic_id]['phone'];                
250               }
251               $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
252             }
253           $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
254           
255           // Get the names of the families assigned this home teaching companionship
256           $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
257           $sql = $sql . " ORDER BY name ASC";
258           $this->db->query($sql,__LINE__,__FILE__);
259           $k=0;
260           while ($this->db->next_record())
261             {
262               $num_families++; $total_families++;
263               $family_name = $this->db->f('name');
264               $family_id = $this->db->f('family');
265               $this->nextmatchs->template_alternate_row_color(&$this->t);
266               $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
267               // Find out how many times Visits were performed by this companionship
268               // in the past $num_months for this Family
269               $header_row="<th width=$comp_width><font size=-2>Families</th>";
270               for($m=$num_months; $m >= 0; $m--) {
271                 $month = $this->current_month - $m;
272                 $year = $this->current_year;
273                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
274                 if($month < 10) { $month = "0"."$month"; }
275                 $month_start = "$year"."-"."$month"."-"."01";
276                 $month_end = "$year"."-"."$month"."-"."31";
277                 $month = "$month"."/"."$year";
278                 //print "m: $m month: $month year: $year month_start: $month_start month_end: $month_end<br>";
279                 // Add this to the query to filter on only visits made by this companionship:
280                 // " AND companionship=" . $unique_companionships[$j]['companionship'].
281                 $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
282                        " AND companionship!=0".
283                        " AND family=". $family_id;
284                 $this->db2->query($sql,__LINE__,__FILE__);
285                 $link_data['menuaction'] = 'eq.eq.ht_update';
286                 $link_data['date'] = $month_start;
287                 $link_data['month_start'] = $month_start;
288                 $link_data['month_end'] = $month_end;
289                 $link_data['month'] = $month;
290                 $link_data['district'] = $districts[$i]['district'];
291                 $link_data['district_name'] = $districts[$i]['name'];
292                 $link_data['action'] = 'view';
293                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
294                 $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
295                 if(!$total_visits[$m]) { $total_visits[$m] = 0; }
296                 if($this->db2->next_record()) {
297                   if($this->db2->f('visited') == 'y') {
298                     $visits[$m]++; $total_visits[$m]++;           
299                     $table_data .= '<td align=center><a href="'.$link.'"><img src="checkmark.gif"></a></td>';
300                   }
301                   else if($this->db2->f('visited') == 'n') {
302                     $table_data .= '<td align=center><a href="'.$link.'"><img src="x.gif"></a></td>';
303                   }
304                   else {
305                     $visits[$m]++; $total_visits[$m]++;
306                     $table_data .= "<td>&nbsp;</td>";
307                   }
308                 }
309                 else {
310                   $visits[$m]++; $total_visits[$m]++;
311                   $table_data .= "<td>&nbsp;</td>";
312                 }
313               }
314               $table_data .= "</tr>"; 
315               $k++;
316             }
317           $table_data .= "<tr><td colspan=20></td></tr>";
318         }
319         $table_data .= "<tr><td colspan=20><hr></td></tr>";
320         $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
321
322         for($m=$num_months; $m >=0; $m--) {
323           $percent = ceil(($visits[$m] / $num_families)*100);
324           $stat_data .= "<td align=center><font size=-2><b>$visits[$m]<br>$percent%</font></b></td>";
325         }
326         $stat_data .= "</tr>";
327
328         $this->t->set_var('table_width',$table_width);
329         $this->t->set_var('header_row',$header_row);
330         $this->t->set_var('table_data',$table_data);
331         $this->t->set_var('stat_data',$stat_data);
332         $this->t->fp('list','district_list',True);
333       }
334
335       $totals = "<tr><td><b><font size=-2>$total_families Total Families<br>Visit Totals:</font></b></td>";
336       for($m=$num_months; $m >=0; $m--) {
337         $percent = ceil(($total_visits[$m] / $total_families)*100);
338         $totals .= "<td align=center><font size=-2><b>$total_visits[$m]<br>$percent%</font></b></td>";
339       }
340       $totals .= "</tr>";
341       
342       $this->t->set_var('totals',$totals);
343       
344       $this->t->pfp('out','ht_view_t');
345       $this->save_sessiondata();
346     }
347       
348
349   function ht_update()
350     {
351       $this->t->set_file(array('ht_update_t' => 'ht_update.tpl'));
352       $this->t->set_block('ht_update_t','district_list','list');
353       $this->t->set_block('ht_update_t','save','savehandle');
354       
355       $district = get_var('district',array('GET','POST'));
356       $district_name = get_var('district_name',array('GET','POST'));
357       $date = get_var('date',array('GET','POST'));
358       $month = get_var('month',array('GET','POST'));
359       $month_start = get_var('month_start',array('GET','POST'));
360       $month_end = get_var('month_end',array('GET','POST'));
361       $action = get_var('action',array('GET','POST'));
362       
363       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
364       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_update&action=save'));
365       $this->t->set_var('lang_done','Cancel');
366       $this->t->set_var('district_name',$district_name);
367       $this->t->set_var('district_number',$district);
368       $this->t->set_var('title','Hometeaching Update ' . $month);
369       $this->t->set_var('date',$date);
370       
371       if($action == 'save')
372         {
373           // Get a list of all the companionships in this district
374           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
375           $this->db->query($sql,__LINE__,__FILE__);
376           $j=0;
377           while ($this->db->next_record())
378             {
379               $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
380               $j++;
381             }
382           for ($j=0; $j < count($unique_companionships); $j++)
383             {
384               // FIXME: We won't be able to go back and edit history on families that have been
385               // reassigned to a different companionship. The following delete command will not delete
386               // the history of visits under an older companionship, only the ones for the existing
387               // companionship. This will lead to duplicate visits being entered for an older
388               // month for the same family, making it impossible to change the past history once
389               // a family is reassigned. However, you will be able to view the history just fine.
390
391               //$comp=$unique_companionships[$j]['companionship'];
392               //print "deleting from eq_visit where companionship=$comp and date=$date and district=$district<br>";
393               // Delete all the visits that have taken place for all families for this month
394               $this->db->query("DELETE from eq_visit where companionship=" . $unique_companionships[$j]['companionship'] .
395                                " AND " . "date='" . $date . "'",__LINE__,__FILE__);
396             }
397
398           // Now, add the visits that are checked for this month
399           $new_data = get_var('family_visited',array('POST'));
400           foreach ($new_data as $family)
401            {
402              foreach ($family as $data)
403                {
404                  //print "family_visited: $data <br>";
405                  $data_array = explode("/",$data);
406                  $family_id = $data_array[0];
407                  $companionship = $data_array[1];
408                  $date = $data_array[2];
409                  $visited = $data_array[3];
410                  if($visited == "") { $visited = $data_array[4]; }
411                  //print "family_id: $family_id companionship: $companionship date: $date visited: $visited<br>";
412                  $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes,visited) "
413                                   . "VALUES (" . $family_id .",". $companionship .",'". $date ."','','". $visited ."')",__LINE__,__FILE__);
414                }
415            }
416           $this->ht_view();
417           return false;
418         }
419       
420       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
421       $this->db->query($sql,__LINE__,__FILE__);
422       $i=0;
423       while ($this->db->next_record())
424         {
425           $elder_id[$i] = $this->db->f('elder');
426           $elder_name[$i] = $this->db->f('name');
427           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
428           $i++;
429         }
430       array_multisort($elder_name, $elder_id);
431
432       // Make an array mapping elder_ids to elder_names
433       for($i=0; $i < count($elder_id); $i++) {
434           $id = $elder_id[$i];
435           $elders[$id] = $elder_name[$i];
436       }      
437
438       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
439       $this->db->query($sql,__LINE__,__FILE__);
440       while ($this->db->next_record())
441         {
442           $aaronic_id = $this->db->f('aaronic');
443           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
444           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
445         }
446       
447       // Select all the unique companionship numbers for this district
448       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
449       $this->db->query($sql,__LINE__,__FILE__);
450       $j=0; $unique_companionships='';
451       while ($this->db->next_record())
452         {
453           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
454           $j++;
455         }
456       
457       $comp_width=300; $visit_width=25; $table_width=$comp_width + $visit_width;
458       $table_data=""; $num_companionships = 0; $num_families = 0; $visits=0;
459       for ($j=0; $j < count($unique_companionships); $j++) {
460         $companion_table_entry = "";
461         // Select all the companions in each companionship
462         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
463                "companionship=". $unique_companionships[$j]['companionship'];
464         $this->db->query($sql,__LINE__,__FILE__);
465         
466         while ($this->db->next_record())
467           {
468             // Get this companions information
469             if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
470             $companionship = $this->db->f('companionship');
471             $elder_id = $this->db->f('elder');
472             $aaronic_id = $this->db->f('aaronic');
473             if($elder_id) {
474               $name = $elders[$elder_id];
475               $phone = $elder_phone[$elder_id];
476             }
477             else if($aaronic_id) {
478               $name = $aaronic[$aaronic_id]['name'];
479               $phone = $aaronic[$aaronic_id]['phone'];
480             }
481             $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
482           }
483         $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
484         
485         // Get the names of the families assigned this home teaching companionship
486         $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
487         $sql = $sql . " ORDER BY name ASC";
488         $this->db->query($sql,__LINE__,__FILE__);
489         while ($this->db->next_record())
490           {
491             $num_families++; $total_families++;
492             $family_name = $this->db->f('name');
493             $family_id = $this->db->f('family');
494             $this->nextmatchs->template_alternate_row_color(&$this->t);
495             $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
496             
497             $header_row="<th width=$comp_width><font size=-2>Families</th>";
498             $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
499                    " AND companionship!=0".
500                    " AND family=". $family_id;
501             $this->db2->query($sql,__LINE__,__FILE__);
502             $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
503             $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
504             if(!$total_visits) { $total_visits = 0; }
505             if($this->db2->next_record()) {
506               if($this->db2->f('visited') == 'y') {
507                 $visits++; $total_visits++;
508                 $table_data .= '<td width=100 align=center>';
509                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y" checked>Y';
510                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
511                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/"> ';
512                 $table_data .= '</td>';
513               } else if($this->db2->f('visited') == 'n') {
514                 $table_data .= '<td width=100 align=center>';
515                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
516                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n" checked>N';
517                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/">';
518                 $table_data .= '</td>';
519               } else {
520                 $table_data .= '<td width=100 align=center>';
521                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
522                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
523                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
524                 $table_data .= '</td>';
525               }
526             }
527             else {
528               $value .= "/";
529               $table_data .= '<td width=100 align=center>';
530               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
531               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
532               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
533               $table_data .= '</td>';         
534             }
535           }
536         $table_data .= "</tr>"; 
537         $table_data .= "<tr><td colspan=20></td></tr>";
538       }
539       $table_data .= "<tr><td colspan=20><hr></td></tr>";
540       $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
541       
542       $percent = ceil(($visits / $num_families)*100);
543       $stat_data .= "<td align=center><font size=-2><b>$visits<br>$percent%</font></b></td>";
544       $stat_data .= "</tr>";
545       
546       $this->t->set_var('table_width',$table_width);
547       $this->t->set_var('header_row',$header_row);
548       $this->t->set_var('table_data',$table_data);
549       $this->t->set_var('stat_data',$stat_data);
550       $this->t->fp('list','district_list',True);
551
552       $this->t->set_var('lang_reset','Clear Form');
553       $this->t->set_var('lang_save','Save Changes');
554       $this->t->set_var('savehandle','');
555       
556       $this->t->pfp('out','ht_update_t');
557       $this->t->pfp('addhandle','save');
558       
559       $this->save_sessiondata();
560     }
561
562   function act_list()
563     {
564       $this->t->set_file(array('act_list_t' => 'act_list.tpl'));
565       $this->t->set_block('act_list_t','act_list','list');
566       
567       $this->t->set_var('lang_name','Activity Name');
568       $this->t->set_var('lang_date','Date');
569       
570       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
571       $this->db->query($sql,__LINE__,__FILE__);
572       $total_records = $this->db->num_rows();
573
574       $i = 0;
575       while ($this->db->next_record())
576         {
577           $activity_list[$i]['name'] = $this->db->f('name');
578           $activity_list[$i]['date']  = $this->db->f('date');
579           $activity_list[$i]['activity']  = $this->db->f('activity');
580           $i++;
581         }
582             
583       for ($i=0; $i < count($activity_list); $i++)
584         {         
585           $this->nextmatchs->template_alternate_row_color(&$this->t);
586           $this->t->set_var('name',$activity_list[$i]['name']);
587           $this->t->set_var('date',$activity_list[$i]['date']);
588           
589           $link_data['menuaction'] = 'eq.eq.act_view';
590           $link_data['activity'] = $activity_list[$i]['activity'];
591           $link_data['action'] = 'view';
592           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
593           $this->t->set_var('lang_view','View');
594
595           $link_data['menuaction'] = 'eq.eq.act_update';
596           $link_data['activity'] = $activity_list[$i]['activity'];
597           $link_data['action'] = 'edit';
598           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
599           $this->t->set_var('lang_edit','Edit');
600
601           $link_data['menuaction'] = 'eq.eq.act_update';
602           $link_data['activity'] = '0';
603           $link_data['action'] = 'add';
604           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
605                            . '"><input type="submit" name="Add" value="' . 'Add Activity' .'"></font></form>');
606
607           $this->t->fp('list','act_list',True);
608         }
609
610       $this->t->pfp('out','act_list_t');
611       $this->save_sessiondata();
612     }
613
614   function act_view()
615     {
616       $this->t->set_file(array('act_view_t' => 'act_view.tpl'));
617       $this->t->set_block('act_view_t','part_list','list');
618       
619       $sql = "SELECT * FROM eq_activity WHERE activity=" . intval(get_var('activity',array('GET','POST')));
620       $this->db->query($sql,__LINE__,__FILE__);
621       $this->db->next_record();
622       $this->t->set_var('name', $this->db->f('name'));
623       $this->t->set_var('date', $this->db->f('date'));
624       $this->t->set_var('notes', $this->db->f('notes'));
625             
626       $this->t->set_var('lang_name','Activity Name');
627       $this->t->set_var('lang_date','Date');
628       $this->t->set_var('lang_notes','Notes');
629       $this->t->set_var('lang_done','Done');
630       $this->t->set_var('lang_action','View');
631
632       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
633       $this->t->set_var('tr_color',$tr_color);
634             
635       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
636
637       $link_data['menuaction'] = 'eq.eq.act_update';
638       $link_data['activity'] = get_var('activity',array('GET','POST'));
639       $link_data['action'] = 'edit';
640       $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
641       $this->t->set_var('lang_edit','Edit');
642       $this->t->set_var('cal_date',$this->db->f('date'));
643
644       // Now find out which elders participated in this activity
645       $sql = "SELECT * FROM eq_participation WHERE activity=" . intval(get_var('activity',array('GET','POST')));
646       $this->db->query($sql,__LINE__,__FILE__);
647       $total_records = $this->db->num_rows();
648
649       $i = 0;
650       while ($this->db->next_record())
651         {
652           $part_list[$i]['elder']  = $this->db->f('elder');
653           $i++;
654         }
655       
656       for ($i=0; $i < count($part_list); $i++)
657         {
658           $sql = "SELECT * FROM eq_elder WHERE elder=" . $part_list[$i]['elder'];
659           $this->db->query($sql,__LINE__,__FILE__);
660           $this->db->next_record();
661           $names[$i] = $this->db->f('name');
662         } sort($names);
663       
664       for ($i=0; $i < count($names); $i++)
665         {
666           //$this->nextmatchs->template_alternate_row_color(&$this->t);
667           $this->t->set_var('elder_name',$names[$i]);
668           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
669           else { $this->t->set_var('table_sep',"</td>"); }
670           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
671           $this->t->fp('list','part_list',True);
672         }
673       
674       $this->t->pfp('out','act_view_t');
675       $this->save_sessiondata();
676     }
677
678   function act_update()
679     {
680       $this->t->set_file(array('form' => 'act_update.tpl'));
681       $this->t->set_block('form','elder_list','list');
682       $this->t->set_block('form','add','addhandle');
683       $this->t->set_block('form','edit','edithandle');
684       $this->t->set_var('lang_done','Done');
685
686       $action = get_var('action',array('GET','POST'));
687       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
688       $activity['activity'] = intval(get_var('activity',array('GET','POST')));
689
690       if($action == 'save')
691         {
692           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
693           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
694           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
695           $this->db->query("UPDATE eq_activity set " .
696                            "   name='" . $activity['name'] .
697                            "', date='" . $activity['date'] . "'" .
698                            ", notes='" . $activity['notes'] . "'" .
699                            " WHERE activity=" . $activity['activity'],__LINE__,__FILE__);
700
701           // Delete all the elders who have particiapted in this activity
702           $this->db->query("DELETE from eq_participation where activity=".$activity['activity'],__LINE__,__FILE__);
703           
704           // Re-add the elders who are checked as having participated in this activity
705           $elders = get_var('elder_name',array('POST'));
706           foreach ($elders as $elder)
707             {
708               $this->db->query("INSERT INTO eq_participation (elder,activity) "
709                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
710             }
711
712           $this->act_list();
713           return false;
714         }
715
716       if($action == 'insert')
717         {
718           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
719           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
720           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
721           $this->db->query("INSERT INTO eq_activity (name,date,notes) "
722                            . "VALUES ('" . $activity['name'] . "','"
723                            . $activity['date'] . "','" . $activity['notes'] . "')",__LINE__,__FILE__);
724           $this->act_list();
725           return false;
726         }
727       
728       if($action == 'add')
729         {
730           $activity['activity'] = 0;
731           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
732           $this->t->set_var('name','');
733           $this->t->set_var('date','');
734           $this->t->set_var('notes','');
735           $this->t->set_var('lang_done','Cancel');
736           $this->t->set_var('lang_action','Adding New Activity');
737           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
738                                                                 . $activity['activity'] . '&action=' . 'insert'));
739         }
740
741       if($action == 'edit')
742         {
743           $sql = "SELECT * FROM eq_activity WHERE activity=" . $activity['activity'];
744           $this->db->query($sql,__LINE__,__FILE__);
745           $this->db->next_record();
746           $this->t->set_var('cal_date',$this->jscal->input('date',$this->db->f('date'),'','','','','',$this->cal_options));
747           $this->t->set_var('name', $this->db->f('name'));
748           $this->t->set_var('date', $this->db->f('date'));
749           $this->t->set_var('notes', $this->db->f('notes'));
750           $this->t->set_var('lang_done','Cancel');
751           $this->t->set_var('lang_action','Editing Activity');
752           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
753                                                                 . $activity['activity'] . '&action=' . 'save'));
754
755         }
756
757       $sql = "SELECT * FROM eq_elder";
758       $this->db->query($sql,__LINE__,__FILE__);
759       $i=0;
760       while ($this->db->next_record())
761         {
762           if($this->db->f('valid') == 1 || $action != 'add') {
763             $elder_name[$i] = $this->db->f('name');
764             $elder_id[$i] = $this->db->f('elder');
765             $elder_valid[$i] = $this->db->f('valid');
766             $i++;
767           }
768         }
769       array_multisort($elder_name, $elder_id, $elder_valid);
770
771       $j=0;
772       for ($i=0; $i < count($elder_id); $i++)
773         {
774           //$this->nextmatchs->template_alternate_row_color(&$this->t);
775           $sql = "SELECT * FROM eq_participation where activity=". $activity['activity'] . " AND elder=" . $elder_id[$i];
776           $this->db->query($sql,__LINE__,__FILE__);
777           if($this->db->next_record()) { $this->t->set_var('checked','checked'); $checked=1; }
778           else { $this->t->set_var('checked',''); $checked=0; }
779           if($checked || $elder_valid[$i] == 1) {
780             $this->t->set_var('elder_name',$elder_name[$i]);
781             $this->t->set_var('elder',$elder_id[$i]);
782             if(($j+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
783             else { $this->t->set_var('table_sep',"</td>"); }
784             if(($j) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
785             $this->t->fp('list','elder_list',True);
786             $j++;
787           }
788         }
789       
790       $this->t->set_var('lang_reset','Clear Form');
791       $this->t->set_var('lang_add','Add Activity');
792       $this->t->set_var('lang_save','Save Changes');
793       $this->t->set_var('edithandle','');
794       $this->t->set_var('addhandle','');
795       
796       $this->t->pfp('out','form');
797       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
798       if($action == 'add') { $this->t->pfp('addhandle','add'); }
799       
800       $this->save_sessiondata();
801     }
802
803   function par_view()
804     {
805       $this->t->set_file(array('par_view_t' => 'par_view.tpl'));
806       $this->t->set_block('par_view_t','header_list','list1');
807       $this->t->set_block('par_view_t','elder_list','list2');
808
809       $sql = "SELECT * FROM eq_elder where valid=1";
810       $this->db->query($sql,__LINE__,__FILE__);
811       $i=0;
812       while ($this->db->next_record())
813         {
814           $elder_name[$i] = $this->db->f('name');
815           $elder_id[$i] = $this->db->f('elder');
816           $i++;
817         }
818       array_multisort($elder_name, $elder_id);
819
820       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
821       $this->db->query($sql,__LINE__,__FILE__);
822       $total_records = $this->db->num_rows();
823
824       $i = 0;
825       while ($this->db->next_record())
826         {
827           $activity_list[$i]['name'] = $this->db->f('name');
828           $activity_list[$i]['date'] = $this->db->f('date');
829           $activity_list[$i]['activity']  = $this->db->f('activity');
830           if($activity_list[$i]['date'] == '0000-00-00') { $activity_list[$i]['date']=""; }
831           $i++;
832         }
833
834       $elder_width=250; $part_width=25; $act_width=50;
835       $total_width=$elder_width+$part_width;
836       for ($i=0; $i < count($activity_list); $i++) {
837         $this->t->set_var('activity_name',$activity_list[$i]['name']);
838         $this->t->set_var('activity_date',$activity_list[$i]['date']);
839         $this->t->fp('list1','header_list',True);
840         $total_width += $act_width;
841       }
842
843       for ($i=0; $i < count($elder_id); $i++) {
844         $participated=0; $part_table = ''; 
845         $this->nextmatchs->template_alternate_row_color(&$this->t);
846         $this->t->set_var('elder_name',$elder_name[$i]);
847         for ($j=0; $j < count($activity_list); $j++) {
848           $sql = "SELECT * FROM eq_participation where activity="
849              . $activity_list[$j]['activity'] . " AND elder=" . $elder_id[$i];
850           $this->db->query($sql,__LINE__,__FILE__);
851           if($this->db->next_record()) {
852             $part_table .= '<td align=center><img src="checkmark.gif"></td>';
853             $participated++;
854           } else {
855             $part_table .= '<td>&nbsp;</td>';
856           }
857         }
858         if($participated) { $part_table .= '<td align=center><img src="checkmark.gif">'.$participated.'</td>'; }
859         else { $part_table .= '<td>&nbsp;</td>'; }
860         $this->t->set_var('part_table',$part_table);
861         $this->t->fp('list2','elder_list',True);
862       }
863       $this->t->set_var('total_width',$total_width);
864       $this->t->set_var('elder_width',$elder_width);
865       $this->t->set_var('part_width',$part_width);
866       $this->t->set_var('act_width',$act_width);
867       $this->t->pfp('out','par_view_t');
868       $this->save_sessiondata(); 
869     }
870   
871   function ppi_sched()
872     {
873       $this->t->set_file(array('ppi_sched_t' => 'ppi_sched.tpl'));
874       $this->t->set_block('ppi_sched_t','elder_list','elderlist');
875       $this->t->set_block('ppi_sched_t','appt_list','apptlist');
876       $action = get_var('action',array('GET','POST'));
877
878       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
879       $this->t->set_var('lang_reset','Clear Changes');
880       
881       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
882       $this->t->set_var('ppi_link_title','Yearly PPIs');
883       
884       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
885       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
886
887       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched&action=save'));
888       $this->t->set_var('title','Yearly PPI Scheduler');
889
890       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
891       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
892       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
893       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
894       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
895       $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
896       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
897       $table_data=""; $completed_data=""; $totals_data="";
898
899       $year = date('Y');
900       
901       if($action == 'save')
902         {
903           // Save any changes made to the appointment table
904           $new_data = get_var('appt_notes',array('POST'));
905           foreach ($new_data as $entry)
906            {
907              $elder = $entry['elder'];
908              $appointment = $entry['appointment'];
909
910              //print "elder: $elder appointment: $appointment <br>";
911              
912              // Perform database save actions here
913              $this->db->query("UPDATE eq_appointment set " .
914                               " elder='" . $elder . "'" .
915                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
916
917            }
918           
919           // Save any changes made to the ppi notes table
920           $new_data = get_var('ppi_notes',array('POST'));
921           foreach ($new_data as $entry)
922            {
923              $ppi_notes = $entry['notes'];
924              $elder_id = $entry['elder_id'];
925              $ppi_pri = $entry['pri'];
926              
927              // Perform database save actions here
928              $this->db->query("UPDATE eq_elder set " .
929                               " ppi_notes='" . $ppi_notes . "'" .
930                               ",ppi_pri='" . $ppi_pri . "'" .
931                               " WHERE elder=" . $elder_id,__LINE__,__FILE__);
932              
933            }
934
935           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched');
936           Header('Location: ' . $take_me_to_url);
937         }
938
939       // Get the EQ President
940       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
941       $this->db->query($sql,__LINE__,__FILE__);
942       if($this->db->next_record()) {
943         $president_name = $this->db->f('name');
944         $president_id = $this->db->f('elder');
945         $presidency_id = $this->db->f('presidency');
946         $district_number = '*';
947         $district_name = $president_name;
948       } else {
949         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
950         return;
951       }
952       
953       // create the elder id -> elder name mapping
954       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
955       $this->db->query($sql,__LINE__,__FILE__);
956       $i=0;
957       $elder_id = NULL;
958       $elder_name = NULL;
959       while ($this->db->next_record())
960         {
961           $elder_name[$i] = $this->db->f('name');
962           $elder_id[$i] = $this->db->f('elder');
963           $i++;
964         }
965       array_multisort($elder_name, $elder_id);
966
967       // APPOINTMENT TABLE
968       $date_width=150; $time_width=100; $elder_width=200;
969       $appt_table_width=$date_width + $time_width + $elder_width;
970       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
971       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
972       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
973       $appt_table_data = ""; 
974
975       $total_elders=0; $elders_with_yearly_ppi=0;
976       
977       // Display a scheduling table for the EQ President
978       $table_data=""; $appt_table_data="";
979       $table_title = "District ".$district_number.": ".$district_name.": All Elders with Yearly PPI Not Completed";
980       $appt_table_title = "District ".$district_number.": ".$district_name.": Yearly PPI Appointment Slots";
981       $this->t->set_var('table_title',$table_title);
982       $this->t->set_var('appt_table_title',$appt_table_title);
983       
984       // query the database for all the appointments
985       $sql = "SELECT * FROM eq_appointment where presidency=".$presidency_id." and date>=CURDATE() ORDER BY date ASC, time ASC";
986       $this->db->query($sql,__LINE__,__FILE__);
987         
988       while ($this->db->next_record())
989         {
990           $appointment = $this->db->f('appointment');
991           $elder = $this->db->f('elder');
992           
993           $date = $this->db->f('date');
994           $date_array = explode("-",$date);
995           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
996           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
997           
998           $time = $this->db->f('time');
999           $time_array = explode(":",$time);
1000           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1001           
1002           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1003           $appt_table_data.= "<td align=center>$day_string</td>";
1004           $appt_table_data.= "<td align=center>$time_string</td>";
1005
1006           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1007           $appt_table_data.= '<option value=0></option>';
1008           for ($i=0; $i < count($elder_id); $i++) {
1009             $id = $elder_id[$i];
1010             $name = $elder_name[$i];
1011             if($elder_id[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1012             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1013           }
1014           $appt_table_data.='</select></td>';
1015
1016           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1017           
1018           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1019           $this->t->set_var('tr_color',$tr_color);
1020         }
1021
1022       $this->t->set_var('appt_table_data',$appt_table_data);
1023       $this->t->set_var('appt_header_row',$appt_header_row);
1024       $this->t->set_var('appt_table_width',$appt_table_width);
1025
1026       // PPI SCHEDULING TABLE
1027       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY ppi_pri ASC";
1028       $this->db->query($sql,__LINE__,__FILE__);
1029       
1030       $i=0; 
1031       $elder_id = NULL;
1032       while ($this->db->next_record())
1033         {
1034           $elder_id[$i] = $this->db->f('elder');
1035           $elder_name[$i] = $this->db->f('name');
1036           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1037           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1038           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1039           $i++;
1040           $total_elders++;
1041         }
1042
1043       $max = count($elder_id);
1044       
1045       for($i=0; $i < $max; $i++) {
1046           $id = $elder_id[$i];
1047           $name = $elder_name[$i];
1048           $phone = $elder_phone[$id];
1049           $ppi_pri = $elder_ppi_pri[$id];
1050           $ppi_notes = $elder_ppi_notes[$id];
1051
1052           // If this elder has had a yearly PPI this year, don't show him on the schedule list
1053           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1054           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1055              "AND elder=" . $id;
1056           $this->db2->query($sql,__LINE__,__FILE__);
1057           
1058           if(!$this->db2->next_record()) {
1059             $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1060             $this->db->query($sql,__LINE__,__FILE__);
1061             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1062             $link_data['menuaction'] = 'eq.eq.ppi_update';
1063             $link_data['elder'] = $id;
1064             $link_data['name'] = $name;
1065             $link_data['ppi'] = '';
1066             $link_data['eqpresppi'] = 1;
1067             $link_data['action'] = 'add';
1068             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1069             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1070             $this->t->set_var('tr_color',$tr_color);
1071             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1072             $table_data.= "<td align=center>$phone</td>";
1073             //$table_data.= "<td align=center>$ppi_pri</td>";
1074             $table_data.= "<td align=center>";
1075             $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
1076             foreach(range(0,6) as $num) {
1077               if($num == 0) { $num = 1; } else {$num = $num*5; }
1078               if($ppi_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1079               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1080             }
1081             $table_data.= '</select></td>';
1082             $table_data.= "<td align=center>$date</td>";
1083             $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
1084             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_id]" value="'.$id.'">';
1085             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_name]" value="'.$name.'">';
1086             $table_data.= '</td>';
1087             $table_data.= '</tr>';
1088           } else {
1089             $link_data['menuaction'] = 'eq.eq.ppi_update';
1090             $link_data['interviewer'] = $this->db2->f('interviewer');
1091             $link_data['elder'] = $this->db2->f('elder');
1092             $link_data['name'] = $name;
1093             $link_data['ppi'] = $this->db2->f('ppi');
1094             $link_data['eqpresppi'] = $this->db2->f('eqpresppi');
1095             $link_data['action'] = 'view';
1096             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1097             $elders_with_yearly_ppi++;
1098             $date = $this->db2->f('date');
1099             $ppi_notes = $this->db2->f('notes');
1100             if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
1101             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1102             $this->t->set_var('tr_color2',$tr_color2);
1103             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1104             $completed_data.= "<td align=center>$phone</td>";
1105             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1106             $completed_data.= "<td align=left>$ppi_notes</td>";
1107             $completed_data.= '</tr>';
1108           }
1109       } // End for Elders Loop
1110
1111       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1112       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1113       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1114       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1115       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1116       $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1117             
1118       $this->t->set_var('table_width',$table_width);
1119       $this->t->set_var('header_row',$header_row);
1120       $this->t->set_var('table_data',$table_data);
1121       $this->t->set_var('completed_header_row',$completed_header_row);
1122       $this->t->set_var('completed_table_width',$completed_table_width);
1123       $this->t->set_var('completed',$completed_data);
1124       $this->t->fp('elderlist','elder_list',True); 
1125       
1126       $elders_width=300; $totals_width=100;
1127       $totals_table_width=$elders_width + $totals_width;
1128       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1129       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1130       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1131       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
1132       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
1133       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
1134       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1135       $this->t->set_var('tr_color',$tr_color);
1136       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1137       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1138       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1139       $totals_data.= "</tr>";
1140
1141       $this->t->set_var('totals',$totals_data);
1142       $this->t->set_var('totals_header_row',$totals_header_row);
1143       $this->t->set_var('totals_table_width',$totals_table_width);
1144       
1145       $this->t->pfp('out','ppi_sched_t');
1146       $this->save_sessiondata(); 
1147       
1148     }
1149   
1150   function int_sched()
1151     {
1152       $this->t->set_file(array('ppi_sched_t' => 'ppi_sched.tpl'));
1153       $this->t->set_block('ppi_sched_t','elder_list','elderlist');
1154       $this->t->set_block('ppi_sched_t','appt_list','apptlist');
1155       $action = get_var('action',array('GET','POST'));
1156
1157       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1158       $this->t->set_var('lang_reset','Clear Changes');
1159       
1160       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1161       $this->t->set_var('ppi_link_title','Yearly PPIs');
1162       
1163       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1164       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1165
1166       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched&action=save'));
1167       $this->t->set_var('title','Yearly PPIs Scheduler');
1168
1169       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
1170       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
1171       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1172       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1173       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1174       $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
1175       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1176       $table_data=""; $completed_data=""; $totals_data="";
1177
1178       $year = date('Y');
1179       
1180       if($action == 'save')
1181         {
1182           // Save any changes made to the appointment table
1183           $new_data = get_var('appt_notes',array('POST'));
1184           foreach ($new_data as $entry)
1185            {
1186              $elder = $entry['elder'];
1187              $appointment = $entry['appointment'];
1188
1189              //print "elder: $elder appointment: $appointment <br>";
1190              
1191              // Perform database save actions here
1192              $this->db->query("UPDATE eq_appointment set " .
1193                               " elder='" . $elder . "'" .
1194                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1195
1196            }
1197           
1198           // Save any changes made to the ppi notes table
1199           $new_data = get_var('ppi_notes',array('POST'));
1200           foreach ($new_data as $entry)
1201            {
1202              $ppi_notes = $entry['notes'];
1203              $elder_id = $entry['elder_id'];
1204              $ppi_pri = $entry['pri'];
1205              
1206              // Perform database save actions here
1207              $this->db->query("UPDATE eq_elder set " .
1208                               " ppi_notes='" . $ppi_notes . "'" .
1209                               ",ppi_pri='" . $ppi_pri . "'" .
1210                               " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1211              
1212            }
1213
1214           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched');
1215           Header('Location: ' . $take_me_to_url);
1216         }
1217
1218       // Get the Districts
1219       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1220       $this->db->query($sql,__LINE__,__FILE__);
1221       $i=0;
1222       while ($this->db->next_record())
1223         {
1224           $districts[$i]['district'] = $this->db->f('district');
1225           $districts[$i]['name'] = $this->db->f('name');
1226           $districts[$i]['supervisor'] = $this->db->f('supervisor');
1227           $i++;
1228         }
1229       $districts[$i]['district'] = $i + 1;
1230       $districts[$i]['name'] = "Unassigned";
1231       $districts[$i]['supervisor'] = "Unassigned";
1232       
1233       // create the elder id -> elder name mapping
1234       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1235       $this->db->query($sql,__LINE__,__FILE__);
1236       $i=0;
1237       $elder_id = NULL;
1238       $elder_name = NULL;
1239       while ($this->db->next_record())
1240         {
1241           $elder_name[$i] = $this->db->f('name');
1242           $elder_id[$i] = $this->db->f('elder');
1243           $i++;
1244         }
1245       array_multisort($elder_name, $elder_id);
1246
1247       // APPOINTMENT TABLE
1248       $district = 1;
1249       $date_width=150; $time_width=100; $elder_width=200;
1250       $appt_table_width=$date_width + $time_width + $elder_width;
1251       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1252       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1253       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1254       $appt_table_data = ""; 
1255
1256       $total_elders=0; $elders_with_yearly_ppi=0;
1257       
1258       // Display a scheduling table for each district
1259       for ($d=0; $d < count($districts); $d++) {
1260       $table_data=""; $appt_table_data="";
1261       $this->t->set_var('district_number',$districts[$d]['district']);
1262       $this->t->set_var('district_name',$districts[$d]['name']);        
1263       $supervisor = $districts[$d]['supervisor'];
1264       $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Elders with Yearly PPI Not Completed";
1265       $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Yearly PPI Appointment Slots";
1266       $this->t->set_var('table_title',$table_title);
1267       $this->t->set_var('appt_table_title',$appt_table_title);
1268       
1269       // query the database for all the appointments
1270       $sql = "SELECT * FROM eq_appointment where district=".$districts[$d]['district']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1271       $this->db->query($sql,__LINE__,__FILE__);
1272         
1273       while ($this->db->next_record())
1274         {
1275           $appointment = $this->db->f('appointment');
1276           $elder = $this->db->f('elder');
1277           
1278           $date = $this->db->f('date');
1279           $date_array = explode("-",$date);
1280           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1281           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1282           
1283           $time = $this->db->f('time');
1284           $time_array = explode(":",$time);
1285           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1286           
1287           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1288           $appt_table_data.= "<td align=center>$day_string</td>";
1289           $appt_table_data.= "<td align=center>$time_string</td>";
1290
1291           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1292           $appt_table_data.= '<option value=0></option>';
1293           for ($i=0; $i < count($elder_id); $i++) {
1294             $id = $elder_id[$i];
1295             $name = $elder_name[$i];
1296             if($elder_id[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1297             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1298           }
1299           $appt_table_data.='</select></td>';
1300
1301           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1302           
1303           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1304           $this->t->set_var('tr_color',$tr_color);
1305         }
1306
1307       $this->t->set_var('appt_table_data',$appt_table_data);
1308       $this->t->set_var('appt_header_row',$appt_header_row);
1309       $this->t->set_var('appt_table_width',$appt_table_width);
1310
1311       // PPI SCHEDULING TABLE
1312       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY ppi_pri ASC";
1313       $this->db->query($sql,__LINE__,__FILE__);
1314       
1315       $i=0; 
1316       $elder_id = NULL;
1317       while ($this->db->next_record())
1318         {
1319           $sql2 = "SELECT * FROM eq_companionship where valid=1 and elder=".$this->db->f('elder');
1320           $this->db2->query($sql2,__LINE__,__FILE__);
1321           
1322           if(((!$this->db2->next_record()) && ($d == count($districts) - 1)) ||
1323              ($this->db2->f('district') == $districts[$d]['district'])) { 
1324             $elder_id[$i] = $this->db->f('elder');
1325             $elder_name[$i] = $this->db->f('name');
1326             $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1327             $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1328             $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1329             $i++;
1330             $total_elders++;
1331           }
1332         }
1333
1334       $max = count($elder_id);
1335       
1336       for($i=0; $i < $max; $i++) {
1337           $id = $elder_id[$i];
1338           $name = $elder_name[$i];
1339           $phone = $elder_phone[$id];
1340           $ppi_pri = $elder_ppi_pri[$id];
1341           $ppi_notes = $elder_ppi_notes[$id];
1342
1343           // If this elder has had a yearly PPI this year, don't show him on the schedule list
1344           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1345           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1346              "AND elder=" . $id;
1347           $this->db2->query($sql,__LINE__,__FILE__);
1348           
1349           if(!$this->db2->next_record()) {
1350             $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1351             $this->db->query($sql,__LINE__,__FILE__);
1352             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1353             $link_data['menuaction'] = 'eq.eq.ppi_update';
1354             $link_data['elder'] = $id;
1355             $link_data['aaronic'] = 0;
1356             $link_data['name'] = $name;
1357             $link_data['ppi'] = '';
1358             $link_data['eqpresppi'] = 1;
1359             $link_data['action'] = 'add';
1360             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1361             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1362             $table_data.= "<td align=center>$phone</td>";
1363             //$table_data.= "<td align=center>$ppi_pri</td>";
1364             $table_data.= "<td align=center>";
1365             $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
1366             foreach(range(0,6) as $num) {
1367               if($num == 0) { $num = 1; } else {$num = $num*5; }
1368               if($ppi_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1369               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1370             }
1371             $table_data.= '</select></td>';
1372             $table_data.= "<td align=center>$date</td>";
1373             $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
1374             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_id]" value="'.$id.'">';
1375             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_name]" value="'.$name.'">';
1376             $table_data.= '</td>';
1377             $table_data.= '</tr>';
1378             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1379             $this->t->set_var('tr_color',$tr_color);
1380           } else {
1381             $link_data['menuaction'] = 'eq.eq.ppi_update';
1382             $link_data['interviewer'] = $this->db2->f('interviewer');
1383             $link_data['elder'] = $this->db2->f('elder');
1384             $link_data['aaronic'] = $this->db2->f('aaronic');
1385             $link_data['name'] = $name;
1386             $link_data['ppi'] = $this->db2->f('ppi');
1387             $link_data['eqpresppi'] = $this->db2->f('eqpresppi');
1388             $link_data['action'] = 'view';
1389             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1390             $elders_with_yearly_ppi++;
1391             $date = $this->db2->f('date');
1392             $ppi_notes = $this->db2->f('notes');
1393             if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
1394             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1395             $completed_data.= "<td align=center>$phone</td>";
1396             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1397             $completed_data.= "<td align=left>$ppi_notes</td>";
1398             $completed_data.= '</tr>';
1399             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1400             $this->t->set_var('tr_color2',$tr_color2);
1401           }
1402       }
1403
1404       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1405       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1406       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1407       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1408       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1409       $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1410             
1411       $this->t->set_var('table_width',$table_width);
1412       $this->t->set_var('header_row',$header_row);
1413       $this->t->set_var('table_data',$table_data);
1414       $this->t->set_var('completed_header_row',$completed_header_row);
1415       $this->t->set_var('completed_table_width',$completed_table_width);
1416       $this->t->set_var('completed',$completed_data);
1417       $this->t->fp('elderlist','elder_list',True);
1418
1419       } // End for each district loop
1420
1421       
1422       $elders_width=300; $totals_width=100;
1423       $totals_table_width=$elders_width + $totals_width;
1424       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1425       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1426       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1427       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
1428       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
1429       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
1430       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1431       $this->t->set_var('tr_color',$tr_color);
1432       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1433       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1434       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1435       $totals_data.= "</tr>";
1436
1437       $this->t->set_var('totals',$totals_data);
1438       $this->t->set_var('totals_header_row',$totals_header_row);
1439       $this->t->set_var('totals_table_width',$totals_table_width);
1440       
1441       $this->t->pfp('out','ppi_sched_t');
1442       $this->save_sessiondata(); 
1443       
1444     }
1445   
1446   function vis_sched()
1447     {
1448       $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
1449       $this->t->set_block('vis_sched_t','family_list','familylist');
1450       $this->t->set_block('vis_sched_t','appt_list','apptlist');
1451       $action = get_var('action',array('GET','POST'));
1452
1453       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1454       $this->t->set_var('lang_reset','Clear Changes');
1455       
1456       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
1457       $this->t->set_var('vis_link_title','View Yearly Visits');
1458       
1459       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
1460       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
1461
1462       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched&action=save'));
1463       $this->t->set_var('title','EQ Presidency Yearly Visit Scheduler');
1464
1465       $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
1466       $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
1467       $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
1468       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1469       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1470       $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
1471       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1472       $table_data=""; $completed_data=""; $totals_data="";
1473
1474       $year = date('Y');
1475       
1476       if($action == 'save')
1477         {
1478           // Save any changes made to the appointment table
1479           $new_data = get_var('appt_notes',array('POST'));
1480           foreach ($new_data as $entry)
1481            {
1482              $family = $entry['family'];
1483              $appointment = $entry['appointment'];
1484
1485              // Perform database save actions here
1486              $this->db->query("UPDATE eq_appointment set " .
1487                               " family='" . $family . "'" .
1488                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1489
1490            }
1491           
1492           // Save any changes made to the visit notes table
1493           $new_data = get_var('vis_notes',array('POST'));
1494           foreach ($new_data as $entry)
1495            {
1496              $visit_notes = $entry['notes'];
1497              $family_id = $entry['family_id'];
1498              $visit_pri = $entry['pri'];
1499              
1500              // Perform database save actions here
1501              $this->db->query("UPDATE eq_family set " .
1502                               " visit_notes='" . $visit_notes . "'" .
1503                               ",visit_pri='" . $visit_pri . "'" .
1504                               " WHERE family=" . $family_id,__LINE__,__FILE__);
1505              
1506            }
1507
1508           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched');
1509           Header('Location: ' . $take_me_to_url);
1510         }
1511
1512       // APPOINTMENT TABLE
1513       $date_width=150; $time_width=100; $family_width=250;
1514       $appt_table_width=$date_width + $time_width + $family_width;
1515       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1516       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1517       $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
1518       $appt_table_data = ""; 
1519
1520       // Find out what the EQ Presidency ID is
1521       $sql = "SELECT * FROM eq_presidency where president=0 and counselor=0 and secretary=0 and valid=1";
1522       $this->db->query($sql,__LINE__,__FILE__);
1523       if($this->db->next_record()) {
1524         $presidency_name = $this->db->f('name');
1525         $presidency_id = $this->db->f('presidency');
1526       } else {
1527         print "<hr><font color=red><h3>-E- Unable to locate EQ Presidency in eq_presidency table</h3></font></hr>";
1528         return;
1529       }
1530       
1531       // create the family id -> family name mapping
1532       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
1533       $this->db->query($sql,__LINE__,__FILE__);
1534       $i=0;
1535       $family_id = NULL;
1536       while ($this->db->next_record())
1537         {
1538           $family_id[$i] = $this->db->f('family');
1539           $family_name[$i] = $this->db->f('name');
1540           $i++;
1541         }
1542       array_multisort($family_name, $family_id);
1543       
1544       // query the database for all the appointments
1545       $sql = "SELECT * FROM eq_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
1546       $this->db->query($sql,__LINE__,__FILE__);
1547
1548       while ($this->db->next_record())
1549         {
1550           $appointment = $this->db->f('appointment');
1551           $family = $this->db->f('family');
1552
1553           $date = $this->db->f('date');
1554           $date_array = explode("-",$date);
1555           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1556           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1557           
1558           $time = $this->db->f('time');
1559           $time_array = explode(":",$time);
1560           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1561           
1562           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1563           $appt_table_data.= "<td align=center>$day_string</td>";
1564           $appt_table_data.= "<td align=center>$time_string</td>";
1565
1566           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
1567           $appt_table_data.= '<option value=0></option>';
1568           for ($i=0; $i < count($family_id); $i++) {
1569             $id = $family_id[$i];
1570             $name = $family_name[$i];
1571             if($family_id[$i] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1572             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
1573           }
1574           $appt_table_data.='</select></td>';
1575
1576           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1577           
1578           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1579           $this->t->set_var('tr_color',$tr_color);
1580         }
1581
1582       $this->t->set_var('appt_table_data',$appt_table_data);
1583       $this->t->set_var('appt_header_row',$appt_header_row);
1584       $this->t->set_var('appt_table_width',$appt_table_width);
1585
1586       
1587       // VISIT SCHEDULING TABLE
1588       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY visit_pri ASC";
1589       $this->db->query($sql,__LINE__,__FILE__);
1590
1591       $total_families=0; $families_with_yearly_visit=0;
1592       
1593       $i=0; 
1594       $family_id = NULL;
1595       $family_name = NULL;
1596       $family_phone = NULL;
1597       $family_visit_pri = NULL;
1598       $family_visit_notes = NULL;
1599       while ($this->db->next_record())
1600         {
1601           $family_id[$i] = $this->db->f('family');
1602           $family_name[$i] = $this->db->f('name');
1603           $family_phone[$family_id[$i]] = $family_id[$i] . " ERROR";
1604           $family_visit_pri[$family_id[$i]] = $this->db->f('visit_pri');
1605           $family_visit_notes[$family_id[$i]] = $this->db->f('visit_notes');
1606           $i++;
1607           $total_families++;
1608         }
1609
1610       $sql = "SELECT * FROM eq_parent where valid=1";
1611       $this->db->query($sql,__LINE__,__FILE__);
1612       while ($this->db->next_record())
1613         {
1614           $family = $this->db->f('family');
1615           $phone = $this->db->f('phone');
1616           $family_phone[$family] = $phone;
1617         }
1618       
1619       $max = count($family_id);
1620       
1621       for($i=0; $i < $max; $i++) {
1622           $id = $family_id[$i];
1623           $name = $family_name[$i];
1624           $phone = $family_phone[$id];
1625           $vis_pri = $family_visit_pri[$id];
1626           $vis_notes = $family_visit_notes[$id];
1627
1628           // If this family has had a yearly visit this year, don't show them on the schedule list
1629           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1630           $sql = "SELECT * FROM eq_visit WHERE date > '$year_start' AND date < '$year_end' ".
1631              "AND family=" . $id . " AND companionship=0";
1632           $this->db2->query($sql,__LINE__,__FILE__);
1633           
1634           if(!$this->db2->next_record()) {
1635             $sql = "SELECT * FROM eq_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
1636             $this->db->query($sql,__LINE__,__FILE__);
1637             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1638             $link_data['menuaction'] = 'eq.eq.vis_update';
1639             $link_data['visit'] = '';
1640             $link_data['family'] = $id;
1641             $link_data['name'] = $name;
1642             $link_data['action'] = 'add';
1643             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1644             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
1645             $table_data.= "<td align=center>$phone</td>";
1646             $table_data.= "<td align=center>";
1647             $table_data.= '<select name=vis_notes['.$i.'][pri]>';
1648             foreach(range(0,6) as $num) {
1649               if($num == 0) { $num = 1; } else {$num = $num*5; }
1650               if($vis_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1651               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1652             }
1653             $table_data.= '</select></td>';
1654             $table_data.= "<td align=center>$date</td>";
1655             $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$i.'][notes]" value="'.$vis_notes.'">';
1656             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_id]" value="'.$id.'">';
1657             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_name]" value="'.$name.'">';
1658             $table_data.= '</td>';
1659             $table_data.= '</tr>';
1660             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1661             $this->t->set_var('tr_color',$tr_color);
1662           } else {
1663             $link_data['menuaction'] = 'eq.eq.vis_update';
1664             $link_data['visit'] = $this->db2->f('visit');
1665             $link_data['family'] = $this->db2->f('family');
1666             $link_data['name'] = $name;
1667             $link_data['date'] = $this->db2->f('date');
1668             $link_data['action'] = 'view';
1669             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1670             $families_with_yearly_visit++;
1671             $date = $this->db2->f('date');
1672             $vis_notes = $this->db2->f('notes');
1673             if(strlen($vis_notes) > 40) { $vis_notes = substr($vis_notes,0,40) . "..."; }
1674             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
1675             $completed_data.= "<td align=center>$phone</td>";
1676             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1677             $completed_data.= "<td align=left>$vis_notes</td>";
1678             $completed_data.= '</tr>';
1679             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1680             $this->t->set_var('tr_color2',$tr_color2);
1681           }
1682       }
1683
1684       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1685       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1686       $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
1687       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1688       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1689       $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
1690       
1691       $family_width=300; $totals_width=100;
1692       $totals_table_width=$family_width + $totals_width;
1693       $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
1694       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1695       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1696       $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
1697       $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
1698       $percent = ceil(($families_with_yearly_visit / $total_families)*100);
1699       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1700       $this->t->set_var('tr_color',$tr_color);
1701       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1702       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1703       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1704       $totals_data.= "</tr>";
1705       
1706       $this->t->set_var('table_width',$table_width);
1707       $this->t->set_var('header_row',$header_row);
1708       $this->t->set_var('table_data',$table_data);
1709       $this->t->set_var('totals_header_row',$totals_header_row);
1710       $this->t->set_var('totals_table_width',$totals_table_width);
1711       $this->t->set_var('completed_header_row',$completed_header_row);
1712       $this->t->set_var('completed_table_width',$completed_table_width);
1713       $this->t->set_var('completed',$completed_data);
1714       $this->t->set_var('totals',$totals_data);
1715       $this->t->fp('familylist','family_list',True);
1716       $this->t->fp('apptlist','appt_list',True);
1717       
1718       $this->t->pfp('out','vis_sched_t');
1719       $this->save_sessiondata(); 
1720       
1721     }
1722   
1723   function ppi_view()
1724     {
1725       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
1726       $this->t->set_block('ppi_view_t','district_list','list');
1727
1728       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1729       $num_months = get_var('num_months',array('GET','POST'));
1730       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
1731       $this->t->set_var('num_months',$num_months);
1732       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1733       else {  $this->t->set_var('lang_num_months','Months of History'); }
1734       $this->t->set_var('lang_filter','Filter');
1735       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1736             
1737       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1738       $this->t->set_var('ppi_link_title','Yearly PPIs'); 
1739       $eqpresppi = 1;
1740
1741       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1742       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1743       
1744       $this->t->set_var('title','Yearly PPIs');
1745       $num_months = get_var('num_months',array('GET','POST'));
1746       if($num_months == '') { $num_months = $this->default_ppi_num_years; }
1747       $this->t->set_var('num_months',$num_months);
1748       if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
1749       else { $this->t->set_var('lang_num_months','Years of History'); } 
1750       
1751       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
1752       $this->db->query($sql,__LINE__,__FILE__);
1753       if($this->db->next_record()) {
1754         $president_name = $this->db->f('name');
1755         $interviewer = $this->db->f('elder');
1756       } else {
1757         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
1758         return;
1759       }
1760       $this->t->set_var('district_number','*');
1761       $this->t->set_var('district_name',$president_name);
1762
1763       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
1764       $this->db->query($sql,__LINE__,__FILE__);
1765       $i=0;
1766       while ($this->db->next_record())
1767         {
1768           $elder_id[$i] = $this->db->f('elder');
1769           $elder_name[$i] = $this->db->f('name');
1770           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1771           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1772           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1773           $i++;
1774         }
1775       $total_elders=$i;
1776       array_multisort($elder_name, $elder_id);
1777       //var_dump($elder_name); print "<br><br>"; var_dump($elder_id);
1778       
1779       $header_row="<th width=$comp_width><font size=-2>Elder Name</th>";
1780         
1781       $elder_width=400; $ppi_width=75; $table_width=$elder_width + $num_months*$ppi_width;
1782       $table_data="";
1783       for($m=$num_months; $m >= 0; $m--) {
1784         $year = date('Y') - $m;
1785         $header_row .= "<th width=150><font size=-2>$year</th>"; 
1786         $ppis[$m] = 0;
1787       }
1788       
1789       for ($j=0; $j < count($elder_id); $j++) {
1790         $id = $elder_id[$j];
1791         $name = $elder_name[$j];
1792         $phone = $elder_phone[$id];
1793         
1794         $link_data['menuaction'] = 'eq.eq.ppi_update';
1795         $link_data['interviewer'] = $interviewer;
1796         $link_data['elder'] = $id;
1797         $link_data['name'] = $name;
1798         $link_data['ppi'] = '';
1799         $link_data['eqpresppi'] = $eqpresppi;
1800         $link_data['action'] = 'add';
1801         $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1802         $this->nextmatchs->template_alternate_row_color(&$this->t);
1803         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1804
1805         // Find out how many times PPIs were performed in the past $num_months for this Elder
1806         for($m=$num_months; $m >= 0; $m--) {
1807           $year = date('Y') - $m;
1808           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1809           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1810              "AND elder=" . $id;
1811           $this->db2->query($sql,__LINE__,__FILE__);
1812                   
1813           if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1814           if($this->db2->next_record()) {
1815             $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
1816             $link_data['menuaction'] = 'eq.eq.ppi_update';
1817             $link_data['companionship'] = $companionship;
1818             $link_data['interviewer'] = $this->db2->f('interviewer');
1819             $link_data['elder'] = $id;
1820             $link_data['name'] = $name;
1821             $link_data['ppi'] = $this->db2->f('ppi');
1822             $link_data['eqpresppi'] = $eqpresppi;
1823             $link_data['action'] = 'view';
1824             $date = $this->db2->f('date');
1825             $date_array = explode("-",$date);
1826             $month = $date_array[1];
1827             $day   = $date_array[2];
1828             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1829             $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
1830           }
1831           else { $table_data .= "<td>&nbsp;</td>"; }
1832         }
1833         $table_data .= "</tr>\n"; 
1834       }
1835       $table_data .= "<tr><td colspan=20><hr></td></tr>";
1836       
1837       $stat_data = "<tr><td><b><font size=-2>$total_elders Elders<br>PPI Totals:</font></b></td>";
1838       for($m=$num_months; $m >=0; $m--) {
1839         $percent = ceil(($ppis[$m] / $total_elders)*100);
1840         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
1841       }
1842       $stat_data .= "</tr>";
1843       
1844       $this->t->set_var('table_width',$table_width);
1845       $this->t->set_var('header_row',$header_row);
1846       $this->t->set_var('table_data',$table_data);
1847       $this->t->set_var('stat_data',$stat_data);
1848       $this->t->pfp('out','ppi_view_t');
1849       $this->save_sessiondata(); 
1850     }
1851
1852   function ppi_update()
1853     {
1854       $this->t->set_file(array('form' => 'ppi_update.tpl'));
1855       $this->t->set_block('form','interviewer_list','int_list');
1856       $this->t->set_block('form','add','addhandle');
1857       $this->t->set_block('form','edit','edithandle');
1858       
1859       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1860       $this->t->set_var('readonly','');
1861       $this->t->set_var('disabled','');
1862       
1863       $action = get_var('action',array('GET','POST'));
1864       $companionship = get_var('companionship',array('GET','POST'));
1865       $interviewer = get_var('interviewer',array('GET','POST'));      
1866       $name = get_var('name',array('GET','POST'));
1867       $ppi = get_var('ppi',array('GET','POST'));
1868       $elder = get_var('elder',array('GET','POST'));
1869       $aaronic = get_var('aaronic',array('GET','POST'));
1870       $date = get_var('date',array('GET','POST'));
1871       $notes = get_var('notes',array('GET','POST'));
1872       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
1873       
1874       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1875       $this->db->query($sql,__LINE__,__FILE__);
1876       while ($this->db->next_record())
1877         {
1878           $supervisor = $this->db->f('supervisor');
1879           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
1880           $this->db2->query($sql,__LINE__,__FILE__);
1881           $this->db2->next_record();
1882           $interviewer_name = $this->db2->f('name');
1883           
1884           if($supervisor == $interviewer) { 
1885             $this->t->set_var('interviewer',$supervisor . ' selected');
1886           } else {
1887             $this->t->set_var('interviewer',$interviewer);
1888           }
1889           $this->t->set_var('interviewer_name',$interviewer_name);
1890           $this->t->fp('int_list','interviewer_list',True);
1891         }
1892       
1893       if($action == 'save')
1894         {
1895           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1896           $this->db->query("UPDATE eq_ppi set " .
1897                            "   ppi='" . $ppi . "'" .
1898                     ", interviewer='" . $interviewer . "'" .
1899                           ", elder='" . $elder . "'" .
1900                            ", date='" . $date . "'" .
1901                           ", notes='" . $notes . "'" .
1902                       ", eqpresppi='" . $eqpresppi . "'" .
1903                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
1904           $this->ppi_view();
1905           return false;
1906         }
1907
1908       if($action == 'insert')
1909         {
1910           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1911           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,date,notes,eqpresppi) "
1912                            . "VALUES ('" . $interviewer . "','" . $elder . "','"
1913                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
1914           $this->ppi_view();
1915           return false;
1916         }
1917       
1918       if($action == 'add')
1919         {
1920           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1921           $this->t->set_var('ppi', '');
1922           $this->t->set_var('interviewer', $interviewer);
1923           $this->t->set_var('name',$name);
1924           $this->t->set_var('elder',$elder);
1925           $this->t->set_var('date','');
1926           $this->t->set_var('notes','');
1927           $this->t->set_var('eqpresppi',$eqpresppi);
1928           $this->t->set_var('lang_done','Cancel');
1929           $this->t->set_var('lang_action','Adding New PPI');
1930           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1931                                                                 . $ppi . '&action=' . 'insert'));
1932         }
1933
1934       if($action == 'edit' || $action == 'view')
1935         {
1936           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
1937           $this->db->query($sql,__LINE__,__FILE__);
1938           $this->db->next_record();
1939           $this->t->set_var('ppi',$ppi);
1940           $this->t->set_var('name',$name);
1941           $this->t->set_var('interviewer', $this->db->f('interviewer'));
1942           $this->t->set_var('elder',$this->db->f('elder'));
1943           $this->t->set_var('date',$this->db->f('date'));
1944           $this->t->set_var('notes',$this->db->f('notes'));
1945           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
1946         }
1947       
1948       if($action == 'edit')
1949         {
1950           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1951           $this->t->set_var('lang_done','Cancel');
1952           $this->t->set_var('lang_action','Editing PPI');
1953           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1954                                                                 . $ppi . '&action=' . 'save'));
1955         }
1956
1957       if($action == 'view')
1958         {
1959           $date = $this->db->f('date');
1960           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1961           $this->t->set_var('readonly','READONLY');
1962           $this->t->set_var('disabled','DISABLED');
1963           $this->t->set_var('lang_done','Done');
1964           $this->t->set_var('lang_action','Viewing PPI');
1965           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1966                                                                 . $ppi . '&action=' . 'edit'));
1967         }
1968       
1969       $this->t->set_var('lang_reset','Clear Form');
1970       $this->t->set_var('lang_add','Add PPI');
1971       $this->t->set_var('lang_save','Save Changes');
1972       $this->t->set_var('edithandle','');
1973       $this->t->set_var('addhandle','');
1974
1975       $this->t->pfp('out','form');
1976       
1977       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
1978       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1979       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1980      
1981       $this->save_sessiondata(); 
1982     }
1983
1984     function int_view()
1985     {
1986       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
1987       $this->t->set_block('int_view_t','district_list','list');
1988
1989       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
1990       $num_months = get_var('num_months',array('GET','POST'));
1991       if($num_months == '') { $num_months = $this->default_int_num_months; }
1992       $this->t->set_var('num_months',$num_months);
1993       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1994       else {  $this->t->set_var('lang_num_months','Months of History'); }
1995       $this->t->set_var('lang_filter','Filter');
1996       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
1997             
1998       $this->t->set_var('title','Hometeaching Interviews'); 
1999
2000       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2001       $this->db->query($sql,__LINE__,__FILE__);
2002       $i=0;
2003       while ($this->db->next_record())
2004         {
2005           $districts[$i]['district'] = $this->db->f('district');
2006           $districts[$i]['name'] = $this->db->f('name');
2007           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2008           $i++;
2009         }
2010
2011       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2012       $this->db->query($sql,__LINE__,__FILE__);
2013       $i=0;
2014       while ($this->db->next_record())
2015         {
2016           $elder_id[$i] = $this->db->f('elder');
2017           $elder_name[$i] = $this->db->f('name');
2018           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2019           $i++;
2020         }
2021       array_multisort($elder_name, $elder_id);
2022       for($i=0; $i < count($elder_id); $i++) {
2023           $id = $elder_id[$i];
2024           $elders[$id] = $elder_name[$i];
2025       }      
2026
2027       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2028       $this->db->query($sql,__LINE__,__FILE__);
2029       while ($this->db->next_record())
2030         {
2031           $aaronic_id = $this->db->f('aaronic');
2032           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2033           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2034         }
2035       
2036       $total_companionships = 0;
2037       $this->nextmatchs->template_alternate_row_color(&$this->t);
2038       for ($i=0; $i < count($districts); $i++) {
2039         $this->t->set_var('district_number',$districts[$i]['district']);
2040         $this->t->set_var('district_name',$districts[$i]['name']);      
2041         $supervisor = $districts[$i]['supervisor'];
2042         $unique_companionships='';
2043                 
2044         // Select all the unique companionship numbers for this district
2045         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2046
2047         $this->db->query($sql,__LINE__,__FILE__);
2048         $j=0;
2049         while ($this->db->next_record())
2050           {
2051             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2052             $j++;
2053           }
2054         
2055         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2056         $table_data=""; $num_companionships = $j; $num_elders = 0;
2057         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2058         for ($j=0; $j < count($unique_companionships); $j++) {
2059           // Select all the companions in each companionship
2060           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2061              "companionship=". $unique_companionships[$j]['companionship'];
2062           $this->db->query($sql,__LINE__,__FILE__);
2063           $k=0;
2064           $comp = $unique_companionships[$j]['companionship'];
2065           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2066           while ($this->db->next_record())
2067             {
2068               // Get this companions information
2069               $num_elders++;
2070               $companionship = $this->db->f('companionship');
2071               $elder_id = $this->db->f('elder');
2072               $aaronic_id = $this->db->f('aaronic');
2073               if($elder_id) {
2074                 $name = $elders[$elder_id];
2075                 $phone = $elder_phone[$elder_id];
2076               }
2077               else if($aaronic_id) {
2078                 $name = $aaronic[$aaronic_id]['name'];
2079                 $phone = $aaronic[$aaronic_id]['phone'];
2080               }
2081               $link_data['menuaction'] = 'eq.eq.int_update';
2082               $link_data['companionship'] = $companionship;
2083               $link_data['interviewer'] = $supervisor;
2084               $link_data['elder'] = $elder_id;
2085               $link_data['aaronic'] = $aaronic_id;
2086               $link_data['name'] = $name;
2087               $link_data['interview'] = '';
2088               $link_data['action'] = 'add';
2089               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2090               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2091
2092               // Find out how many times Interviews were performed in the past $num_months for this Elder
2093               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2094               for($m=$num_months; $m >= 0; $m--) {
2095                 $month = $this->current_month - $m;
2096                 $year = $this->current_year;
2097                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2098                 if($month < 10) { $month = "0"."$month"; }
2099                 $month_start = "$year"."-"."$month"."-"."01";
2100                 $month_end = "$year"."-"."$month"."-"."31";
2101                 $month = "$month"."/"."$year";
2102                 $sql = "SELECT * FROM eq_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2103                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2104                 $this->db2->query($sql,__LINE__,__FILE__);
2105                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2106               
2107                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2108                 if($this->db2->next_record()) {
2109                   if(!$int_recorded[$companionship][$m]) {
2110                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2111                   }
2112                   $link_data['menuaction'] = 'eq.eq.int_update';
2113                   $link_data['companionship'] = $companionship;
2114                   $link_data['interviewer'] = $this->db2->f('interviewer');
2115                   $link_data['elder'] = $elder_id;
2116                   $link_data['aaronic'] = $aaronic_id;
2117                   $link_data['name'] = $name;
2118                   $link_data['interview'] = $this->db2->f('interview');
2119                   $link_data['action'] = 'view';
2120                   $date = $this->db2->f('date');
2121                   $date_array = explode("-",$date);
2122                   $month = $date_array[1];
2123                   $day   = $date_array[2];
2124                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2125                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2126                 }
2127                 else { $table_data .= "<td>&nbsp;</td>"; }
2128               }
2129               $table_data .= "</tr>"; 
2130               $k++;
2131             }
2132           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2133         }
2134         $total_companionships += $num_companionships;
2135         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Quarterly Totals:</font></b></td>";
2136
2137         for($m=$num_months; $m >=0; $m--) {
2138           $month = $this->current_month - $m;
2139           if(($month % 3) == 1) { $quarter_total = $ints[$m]; }
2140           else { $quarter_total += $ints[$m]; }
2141           $percent = ceil(($quarter_total / $num_companionships)*100);
2142           $stat_data .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2143         }
2144         $stat_data .= "</tr>";
2145
2146         $this->t->set_var('table_width',$table_width);
2147         $this->t->set_var('header_row',$header_row);
2148         $this->t->set_var('table_data',$table_data);
2149         $this->t->set_var('stat_data',$stat_data);
2150         $this->t->fp('list','district_list',True);
2151       }
2152
2153       // Display the totals, cummulative per quarter
2154       $quarter_total = 0;
2155       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Quarterly Totals:</font></b></td>";
2156       for($m=$num_months; $m >=0; $m--) {
2157         $month = $this->current_month - $m;
2158         if(($month % 3) == 1) { $quarter_total = $total_ints[$m]; }
2159         else { $quarter_total += $total_ints[$m]; }
2160         $percent = ceil(($quarter_total / $total_companionships)*100);
2161         $totals .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2162       }
2163       $totals .= "</tr>";
2164       
2165       $this->t->set_var('totals',$totals);
2166       $this->t->pfp('out','int_view_t');
2167       $this->save_sessiondata(); 
2168     }
2169
2170   function int_update()
2171     {
2172       $this->t->set_file(array('form' => 'int_update.tpl'));
2173       $this->t->set_block('form','interviewer_list','int_list');
2174       $this->t->set_block('form','add','addhandle');
2175       $this->t->set_block('form','edit','edithandle');
2176       
2177       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2178       $this->t->set_var('readonly','');
2179       $this->t->set_var('disabled','');
2180       
2181       $action = get_var('action',array('GET','POST'));
2182       $companionship = get_var('companionship',array('GET','POST'));
2183       $interviewer = get_var('interviewer',array('GET','POST'));      
2184       $name = get_var('name',array('GET','POST'));
2185       $interview = get_var('interview',array('GET','POST'));
2186       $elder = get_var('elder',array('GET','POST'));
2187       $aaronic = get_var('aaronic',array('GET','POST'));
2188       $date = get_var('date',array('GET','POST'));
2189       $notes = get_var('notes',array('GET','POST'));
2190       
2191       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2192       $this->db->query($sql,__LINE__,__FILE__);
2193       while ($this->db->next_record())
2194         {
2195           $supervisor = $this->db->f('supervisor');
2196           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2197           $this->db2->query($sql,__LINE__,__FILE__);
2198           $this->db2->next_record();
2199           $interviewer_name = $this->db2->f('name');
2200           
2201           if($supervisor == $interviewer) { 
2202             $this->t->set_var('interviewer',$supervisor . ' selected');
2203           } else {
2204             $this->t->set_var('interviewer',$interviewer);
2205           }
2206           $this->t->set_var('interviewer_name',$interviewer_name);
2207           $this->t->fp('int_list','interviewer_list',True);
2208         }
2209       
2210       if($action == 'save')
2211         {
2212           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2213           $this->db->query("UPDATE eq_interview set " .
2214                      "   interview='" . $interview . "'" .
2215                     ", interviewer='" . $interviewer . "'" .
2216                           ", elder='" . $elder . "'" .
2217                         ", aaronic='" . $aaronic . "'" .
2218                            ", date='" . $date . "'" .
2219                           ", notes='" . $notes . "'" .
2220                            " WHERE interview=" . $interview,__LINE__,__FILE__);
2221           $this->int_view();
2222           return false;
2223         }
2224
2225       if($action == 'insert')
2226         {
2227           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2228           $this->db->query("INSERT INTO eq_interview (interviewer,elder,aaronic,date,notes) "
2229                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2230                            . $date . "','" . $notes ."')",__LINE__,__FILE__);
2231           $this->int_view();
2232           return false;
2233         }
2234       
2235       if($action == 'add')
2236         {
2237           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2238           $this->t->set_var('interview', '');
2239           $this->t->set_var('interviewer', $interviewer);
2240           $this->t->set_var('name',$name);
2241           $this->t->set_var('elder',$elder);
2242           $this->t->set_var('aaronic',$aaronic);
2243           $this->t->set_var('date','');
2244           $this->t->set_var('notes','');
2245           $this->t->set_var('lang_done','Cancel');
2246           $this->t->set_var('lang_action','Adding New Interview');
2247           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2248                                                                 . $interview . '&action=' . 'insert'));
2249         }
2250
2251       if($action == 'edit' || $action == 'view')
2252         {
2253           $sql = "SELECT * FROM eq_interview WHERE interview=".$interview;
2254           $this->db->query($sql,__LINE__,__FILE__);
2255           $this->db->next_record();
2256           $this->t->set_var('interview',$interview);
2257           $this->t->set_var('name',$name);
2258           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2259           $this->t->set_var('elder',$this->db->f('elder'));
2260           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2261           $this->t->set_var('date',$this->db->f('date'));
2262           $this->t->set_var('notes',$this->db->f('notes'));
2263         }
2264       
2265       if($action == 'edit')
2266         {
2267           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2268           $this->t->set_var('lang_done','Cancel');
2269           $this->t->set_var('lang_action','Editing Interview');
2270           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2271                                                                 . $interview . '&action=' . 'save'));
2272         }
2273
2274       if($action == 'view')
2275         {
2276           $date = $this->db->f('date');
2277           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2278           $this->t->set_var('readonly','READONLY');
2279           $this->t->set_var('disabled','DISABLED');
2280           $this->t->set_var('lang_done','Done');
2281           $this->t->set_var('lang_action','Viewing Interview');
2282           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2283                                                                 . $interview . '&action=' . 'edit'));
2284         }
2285       
2286       $this->t->set_var('lang_reset','Clear Form');
2287       $this->t->set_var('lang_add','Add Interview');
2288       $this->t->set_var('lang_save','Save Changes');
2289       $this->t->set_var('edithandle','');
2290       $this->t->set_var('addhandle','');
2291
2292       $this->t->pfp('out','form');
2293       
2294       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2295       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2296       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2297      
2298       $this->save_sessiondata(); 
2299     }
2300
2301   function vis_view()
2302     {
2303       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2304       $this->t->set_block('vis_view_t','visit_list','list1');
2305       $this->t->set_block('vis_view_t','family_list','list2');
2306
2307       $this->t->set_var('lang_name','Family Name');
2308       $this->t->set_var('lang_date','Date');
2309
2310       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2311       $this->t->set_var('vis_link_title','View Yearly Visits');
2312       
2313       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2314       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2315       
2316       
2317       $sql = "SELECT * FROM eq_visit WHERE companionship=0 ORDER BY date DESC";
2318       $this->db->query($sql,__LINE__,__FILE__);
2319       $total_records = $this->db->num_rows();
2320
2321       $i = 0;
2322       while ($this->db->next_record())
2323         {
2324           $visit_list[$i]['visit'] = $this->db->f('visit');
2325           $visit_list[$i]['family'] = $this->db->f('family');
2326           $visit_list[$i]['date']  = $this->db->f('date');
2327           $i++;
2328         }
2329             
2330       for ($i=0; $i < count($visit_list); $i++)
2331         {         
2332           $this->nextmatchs->template_alternate_row_color(&$this->t);
2333
2334           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2335           $this->db->query($sql,__LINE__,__FILE__);
2336           $this->db->next_record();
2337                   
2338           $this->t->set_var('family',$visit_list[$i]['family']);
2339           $this->t->set_var('family_name',$this->db->f('name'));
2340           $this->t->set_var('date',$visit_list[$i]['date']);
2341           
2342           $link_data['menuaction'] = 'eq.eq.vis_update';
2343           $link_data['visit'] = $visit_list[$i]['visit'];
2344           $link_data['name'] = $this->db->f('name');
2345           $link_data['date'] = $visit_list[$i]['date'];
2346           $link_data['action'] = 'view';
2347           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2348           $this->t->set_var('lang_view','View');
2349
2350           $link_data['menuaction'] = 'eq.eq.vis_update';
2351           $link_data['visit'] = $visit_list[$i]['visit'];
2352           $link_data['name'] = $this->db->f('name');
2353           $link_data['date'] = $visit_list[$i]['date'];
2354           $link_data['action'] = 'edit';
2355           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2356           $this->t->set_var('lang_edit','Edit');
2357
2358           $this->t->fp('list1','visit_list',True);
2359         }
2360
2361       // List the families that are available to record a visit against
2362       $sql = "SELECT * FROM eq_family WHERE valid=1";
2363       $this->db->query($sql,__LINE__,__FILE__);
2364       $total_records = $this->db->num_rows();
2365
2366       $i = 0;
2367       while ($this->db->next_record())
2368         {
2369           $family_names[$i] = $this->db->f('name');
2370           $family_ids[$i] = $this->db->f('family');
2371           $i++;
2372         } array_multisort($family_names, $family_ids);
2373       
2374       for ($i=0; $i < count($family_names); $i++)
2375         {
2376           $link_data['menuaction'] = 'eq.eq.vis_update';
2377           $link_data['visit'] = '';
2378           $link_data['family'] = $family_ids[$i];
2379           $link_data['action'] = 'add';
2380           $link_data['name'] = $family_names[$i];
2381           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2382
2383           $this->t->set_var('name',$family_names[$i]);
2384           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2385           else { $this->t->set_var('table_sep',"</td>"); }
2386           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2387
2388           $this->t->fp('list2','family_list',True);
2389         }   
2390
2391       $this->t->pfp('out','vis_view_t');
2392       $this->save_sessiondata(); 
2393     }
2394
2395   function vis_update()
2396     {
2397       $this->t->set_file(array('form' => 'vis_update.tpl'));
2398       $this->t->set_block('form','add','addhandle');
2399       $this->t->set_block('form','edit','edithandle');
2400       
2401       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2402       $this->t->set_var('readonly','');
2403       $this->t->set_var('disabled','');
2404       
2405       $action = get_var('action',array('GET','POST'));
2406       $visit = get_var('visit',array('GET','POST'));
2407       $family = get_var('family',array('GET','POST'));
2408       $name = get_var('name',array('GET','POST'));
2409       $date = get_var('date',array('GET','POST'));
2410       $notes = get_var('notes',array('GET','POST'));
2411       $companionship = 0;
2412       
2413       if($action == 'save')
2414         {
2415           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2416           $this->db->query("UPDATE eq_visit set " .
2417                            "  date='" . $date . "'" .
2418                           ", notes='" . $notes . "'" .
2419                            " WHERE visit=" . $visit,__LINE__,__FILE__);
2420           $this->vis_view();
2421           return false;
2422         }
2423
2424       if($action == 'insert')
2425         {
2426           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2427           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
2428                            . "VALUES ('" . $family . "','" . $companionship . "','"
2429                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
2430           $this->vis_view();
2431           return false;
2432         }
2433       
2434       if($action == 'add')
2435         {
2436           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2437           $this->t->set_var('family', $family);
2438           $this->t->set_var('visit', '');
2439           $this->t->set_var('name', $name);
2440           $this->t->set_var('date','');
2441           $this->t->set_var('notes','');
2442           $this->t->set_var('lang_done','Cancel');
2443           $this->t->set_var('lang_action','Adding New Visit');
2444           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
2445                                                                 . $family . '&action=' . 'insert'));
2446         }
2447
2448       if($action == 'edit' || $action == 'view')
2449         {
2450           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
2451           $this->db->query($sql,__LINE__,__FILE__);
2452           $this->db->next_record();
2453           $this->t->set_var('visit',$visit);
2454           $this->t->set_var('name',$name);
2455           $this->t->set_var('family', $family);
2456           $this->t->set_var('date',$this->db->f('date'));
2457           $this->t->set_var('notes',$this->db->f('notes'));
2458         }
2459       
2460       if($action == 'edit')
2461         {
2462           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2463           $this->t->set_var('lang_done','Cancel');
2464           $this->t->set_var('lang_action','Editing Visit');
2465           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2466                                                                 . $visit . '&action=' . 'save'));
2467         }
2468
2469       if($action == 'view')
2470         {
2471           $date = $this->db->f('date');
2472           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2473           $this->t->set_var('readonly','READONLY');
2474           $this->t->set_var('disabled','DISABLED');
2475           $this->t->set_var('lang_done','Done');
2476           $this->t->set_var('lang_action','Viewing Visit');
2477           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2478                                                                 . $visit . '&action=' . 'edit'));
2479         }
2480       
2481       $this->t->set_var('lang_reset','Clear Form');
2482       $this->t->set_var('lang_add','Add Visit');
2483       $this->t->set_var('lang_save','Save Changes');
2484       $this->t->set_var('edithandle','');
2485       $this->t->set_var('addhandle','');
2486
2487       $this->t->pfp('out','form');
2488       
2489       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
2490       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2491       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2492
2493       $this->save_sessiondata(); 
2494     }
2495
2496   function att_view()
2497     {
2498       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2499       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2500       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2501
2502       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
2503       $this->t->set_block('att_view_t','act_list','list');
2504
2505       $this->t->set_block('att_view_t','month_list','list1');
2506       $this->t->set_block('att_view_t','header_list','list2');
2507       $this->t->set_block('att_view_t','elder_list','list3');
2508       
2509       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2510       $num_months = get_var('num_months',array('GET','POST'));
2511       if($num_months == '') { $num_months = $this->default_att_num_months; }
2512       $this->t->set_var('num_months',$num_months);
2513       $this->t->set_var('lang_filter','Filter');
2514       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
2515       else {  $this->t->set_var('lang_num_months','Months of History'); }
2516          
2517       $sql = "SELECT * FROM eq_elder where valid=1";
2518       $this->db->query($sql,__LINE__,__FILE__);
2519       $i=0;
2520       while ($this->db->next_record())
2521         {
2522           $elder_name[$i] = $this->db->f('name');
2523           $elder_id[$i] = $this->db->f('elder');
2524           $i++;
2525         }
2526       array_multisort($elder_name, $elder_id);
2527
2528       
2529       // Create a list of sunday dates for a window of 3 months back and current month
2530       $i=0; 
2531       $last_time = 0; 
2532       $found_sunday = 0;
2533       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, date("m")-$num_months, 1, date("y")));
2534       $last_date = explode("-",$sunday_list[0]['date']);
2535       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2536       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
2537       while($last_time < $time_limit)
2538       {
2539         $day = date("w",$last_time);
2540         if(date("w",$last_time) == 0) {
2541           $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
2542           $last_date = explode("-",$sunday_list[$i]['date']);
2543           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2544           $sunday_list[$i]['day'] = $last_date[2];
2545           $sunday_list[$i]['month'] = date("M",$last_time);
2546           $sunday_list[$i]['year'] = $last_date[0];
2547           $found_sunday = 1; 
2548         }
2549         $last_time += 90000;
2550         if($found_sunday) { $i++; $found_sunday=0; }
2551       }
2552
2553       $total_elders = count($elder_id);
2554       $old_month=$sunday_list[0]['month']; $span=0;
2555       for ($i=0; $i < count($sunday_list); $i++) {
2556         $date = $sunday_list[$i]['date'];
2557         $this->t->set_var('date',$sunday_list[$i]['date']);
2558         $this->t->set_var('day',$sunday_list[$i]['day']);
2559         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
2560           if($i == count($sunday_list)-1) { $span++; }
2561           $cur_month = $sunday_list[$i]['month'];
2562           $old_month = $sunday_list[$i]['month'];         
2563           $link_data['menuaction'] = 'eq.eq.att_update';
2564           $link_data['month'] = $sunday_list[$i-1]['month'];
2565           $link_data['year'] = $sunday_list[$i-1]['year'];
2566           $link_data['action'] = 'update_month';
2567           $cur_month = $sunday_list[$i-1]['month'];
2568           $cur_year = $sunday_list[$i-1]['year'];
2569           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
2570           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2571           $this->t->set_var('month',$sunday_list[$i-1]['month']);
2572           $this->t->set_var('year',$sunday_list[$i-1]['year']);
2573           $this->t->set_var('span',$span); $span=0;
2574           $this->t->fp('list1','month_list',True);
2575         } $span++;
2576       }
2577       $this->t->set_var('total_elders',$total_elders);
2578       $this->t->set_var('header_row',$header_row);
2579       
2580       $elder_width=200; $att_width=25; $total_width=$elder_width; 
2581       for ($i=0; $i < count($sunday_list); $i++) {
2582         $link_data['menuaction'] = 'eq.eq.att_update';
2583         $link_data['month'] = $sunday_list[$i]['month'];
2584         $link_data['year'] = $sunday_list[$i]['year'];
2585         $link_data['day'] = $sunday_list[$i]['day'];
2586         $link_data['date'] = $sunday_list[$i]['date'];
2587         $link_data['action'] = 'update_day';
2588         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2589         $this->t->set_var('date',$sunday_list[$i]['date']);
2590         $this->t->set_var('day',$sunday_list[$i]['day']);
2591         $this->t->set_var('month',$sunday_list[$i]['month']);
2592         $this->t->set_var('year',$sunday_list[$i]['year']);
2593         $this->t->fp('list2','header_list',True);
2594         $total_width += $att_width;
2595         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
2596       }
2597
2598       for ($i=0; $i < count($elder_id); $i++) {
2599         $att_table = "";
2600         $this->nextmatchs->template_alternate_row_color(&$this->t);
2601         $this->t->set_var('elder_name',$elder_name[$i]);
2602         #print "checking for elder: " . $elder_id[$i] . "<br>";
2603         for ($j=0; $j < count($sunday_list); $j++) {
2604           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
2605           #print "SELECT * FROM eq_attendance WHERE date='"
2606           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
2607           $sql = "SELECT * FROM eq_attendance WHERE date='"
2608              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2609           $this->db->query($sql,__LINE__,__FILE__);
2610           if($this->db->next_record()) {
2611             $cur_month = $sunday_list[$j]['month'];
2612             if($attended[$i][$cur_month] != 1) { 
2613               $attended[$i][$cur_month]=1;
2614               $attendance[$monthnum[$cur_month]]++;
2615             } 
2616             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
2617           } else {
2618             $att_table .= '<td>&nbsp;</td>';
2619           }
2620         }
2621         $this->t->set_var('att_table',$att_table);
2622         $this->t->fp('list3','elder_list',True);
2623       }
2624       $this->t->set_var('total_width',$total_width);
2625       $this->t->set_var('elder_width',$elder_width);
2626       $this->t->set_var('att_width',$att_width);
2627
2628       # Now calculate attendance for these months
2629       $attendance_str = "";
2630       $nonattendance_str = "";
2631       $aveattendance_str = "";
2632       $avenonattendance_str = "";
2633       $num_months=0;
2634       $ave_total_attended=0;
2635       ksort($attendance);
2636       foreach($attendance as $att => $value) {
2637         $total_attended = $attendance[$att];
2638         $ave_total_attended += $attendance[$att]; $num_months++;
2639         $percent = ceil(($total_attended / $total_elders)*100);
2640         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2641         $total_nonattended = $total_elders - $total_attended;
2642         $percent = ceil(($total_nonattended / $total_elders)*100);
2643         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
2644         
2645         $total_attended = ceil(($ave_total_attended / $num_months));
2646         $percent = ceil(($total_attended / $total_elders)*100);
2647         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2648         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
2649         $percent = ceil(($total_attended / $total_elders)*100);
2650         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2651       }
2652       
2653       $this->t->set_var('attendance',$attendance_str);
2654       $this->t->set_var('aveattendance',$aveattendance_str);
2655       $this->t->set_var('nonattendance',$nonattendance_str);
2656       $this->t->set_var('avenonattendance',$avenonattendance_str);
2657       
2658       $this->t->pfp('out','att_view_t');
2659       $this->save_sessiondata(); 
2660     }
2661
2662   function att_update()
2663     {
2664       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2665       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2666       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2667
2668       $this->t->set_file(array('form' => 'att_update.tpl'));
2669       $this->t->set_block('form','edit','edithandle');
2670       
2671       $this->t->set_block('form','month_list','list1');
2672       $this->t->set_block('form','header_list','list2');
2673       $this->t->set_block('form','elder_list','list3');
2674
2675       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2676
2677       $action = get_var('action',array('GET','POST'));
2678       $month = get_var('month',array('GET','POST'));
2679       $year = get_var('year',array('GET','POST'));
2680       $day = get_var('day',array('GET','POST'));
2681       $date = get_var('date',array('GET','POST'));
2682
2683       if($action == 'save_month' || $action == 'save_day')
2684         {
2685            $new_data = get_var('elders_attended',array('POST'));
2686            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
2687
2688            if($action == 'save_month') {        
2689              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
2690            }
2691
2692            if($action == 'save_day') {        
2693              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
2694            }   
2695
2696            foreach ($new_data as $data)
2697            {
2698               $data_array = explode("-",$data);
2699               $elder = $data_array[0];
2700               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
2701               $this->db->query("INSERT INTO eq_attendance (elder,date) "
2702                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
2703            }
2704         
2705          $this->att_view();
2706          return false;    
2707         }
2708
2709       $sql = "SELECT * FROM eq_elder where valid=1";
2710       $this->db->query($sql,__LINE__,__FILE__);
2711       $i=0;
2712       while ($this->db->next_record())
2713         {
2714           $elder_name[$i] = $this->db->f('name');
2715           $elder_id[$i] = $this->db->f('elder');
2716           $i++;
2717         }
2718       array_multisort($elder_name, $elder_id);
2719       
2720       if($action == 'update_month')
2721       {
2722         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
2723         $i=0; 
2724         $last_time = 0; 
2725         $found_sunday = 0;
2726         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
2727         $last_date = explode("-",$sunday_list[0]['date']);
2728         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2729         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
2730         while($last_time <= $time_limit)
2731         {
2732           $day = date("w",$last_time);
2733           if(date("w",$last_time) == 0) { 
2734             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
2735             $last_date = explode("-",$sunday_list[$i]['date']);
2736             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2737             $sunday_list[$i]['day'] = $last_date[2];
2738             $sunday_list[$i]['month'] = date("M",$last_time);
2739             $sunday_list[$i]['year'] = $last_date[0];
2740             $found_sunday = 1; 
2741           }
2742           $last_time += 90000;
2743           if($found_sunday) { $i++; $found_sunday=0; }
2744         }
2745
2746         $this->t->set_var('span', $i);
2747         $this->t->set_var('month',$sunday_list[$i-1]['month']);
2748         $this->t->set_var('year',$sunday_list[$i-1]['year']);
2749         $this->t->fp('list1','month_list',True);
2750         $elder_width=200; $att_width=25; $total_width=$elder_width;
2751         for ($i=0; $i < count($sunday_list); $i++) {
2752           $link_data['menuaction'] = 'eq.eq.att_update';
2753           $link_data['month'] = $sunday_list[$i]['month'];
2754           $link_data['year'] = $sunday_list[$i]['year'];
2755           $link_data['day'] = $sunday_list[$i]['day'];
2756           $link_data['date'] = $sunday_list[$i]['date'];
2757           $link_data['action'] = 'update_day';
2758           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2759           $this->t->set_var('date',$sunday_list[$i]['date']);
2760           $this->t->set_var('day',$sunday_list[$i]['day']);
2761           $this->t->set_var('month',$sunday_list[$i]['month']);
2762           $this->t->set_var('year',$sunday_list[$i]['year']);
2763           $this->t->fp('list2','header_list',True);
2764           $total_width += $att_width;
2765         }     
2766       }
2767
2768       if($action == 'update_day')
2769       {
2770         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
2771         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
2772         $this->t->set_var('month',$month);
2773         $this->t->set_var('year',$year);
2774         $this->t->fp('list1','month_list',True);
2775         $this->t->set_var('date',$date);
2776         $this->t->set_var('day',$day);
2777         $this->t->set_var('month',$month);
2778         $this->t->set_var('year',$year);
2779         $this->t->fp('list2','header_list',True);
2780       }           
2781             
2782       for ($i=0; $i < count($elder_id); $i++) {
2783         $att_table = "";
2784         $this->nextmatchs->template_alternate_row_color(&$this->t);
2785         $this->t->set_var('elder_name',$elder_name[$i]);
2786         for ($j=0; $j < count($sunday_list); $j++) {
2787           $sql = "SELECT * FROM eq_attendance WHERE date='"
2788              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2789           $this->db->query($sql,__LINE__,__FILE__);
2790           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
2791           if($this->db->next_record()) {
2792             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
2793           } else {
2794             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
2795           }
2796         }
2797         $this->t->set_var('att_table',$att_table);
2798         $this->t->fp('list3','elder_list',True);
2799       } 
2800            
2801       $this->t->set_var('lang_done', 'Cancel');
2802       $this->t->set_var('lang_reset','Clear Form');
2803       $this->t->set_var('lang_save','Save Changes');
2804
2805       $this->t->pfp('out','form');
2806       $this->t->pfp('addhandle','edit');
2807
2808       $this->save_sessiondata();       
2809     }
2810
2811   function dir_view()
2812     {
2813       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
2814       $this->t->set_block('dir_view_t','dir_list','list');
2815       
2816       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
2817       $this->db->query($sql,__LINE__,__FILE__);
2818       $i=0;
2819       while ($this->db->next_record())
2820         {
2821           $parent[$i]['id'] = $this->db->f('parent');
2822           $parent[$i]['name'] = $this->db->f('name');
2823           $parent[$i]['phone'] = $this->db->f('phone');
2824           $parent[$i]['address'] = $this->db->f('address');
2825           $i++;
2826         }   
2827       
2828       for ($i=0; $i < count($parent); $i++) 
2829       {
2830         $name = $parent[$i]['name'];
2831         $phone = $parent[$i]['phone'];
2832         $address = $parent[$i]['address'];
2833         $this->t->set_var('name', $name);
2834         $this->t->set_var('address', $address);
2835         $this->t->set_var('phone', $phone);
2836         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2837         $this->t->set_var('tr_color',$tr_color);
2838         $this->t->fp('list','dir_list',True);
2839         //print "$phone $name $address<br>";
2840       }
2841       $this->t->pfp('out','dir_view_t');
2842       $this->save_sessiondata();   
2843     }
2844   
2845   function org_view()
2846     {
2847       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
2848       $this->t->set_block('org_view_t','calling_list','list1');
2849       $this->t->set_block('org_view_t','org_list','list2');
2850
2851       # Display a list ordered alphabetically
2852       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
2853       $this->db->query($sql,__LINE__,__FILE__);
2854       $i=0;
2855       while ($this->db->next_record())
2856         {
2857           $calling[$i]['id'] = $this->db->f('indiv_id');
2858           $calling[$i]['name'] = $this->db->f('name');
2859           $calling[$i]['position'] = $this->db->f('position');
2860           $calling[$i]['sustained'] = $this->db->f('sustained');
2861           $calling[$i]['organization'] = $this->db->f('organization');
2862           $i++;
2863         }   
2864       for ($i=0; $i < count($calling); $i++) 
2865       {
2866         $name = $calling[$i]['name'];
2867         $position = $calling[$i]['position'];
2868         $sustained = $calling[$i]['sustained'];
2869         $organization = $calling[$i]['organization'];
2870         $this->t->set_var('name', $name);
2871         $this->t->set_var('position', $position);
2872         $this->t->set_var('sustained', $sustained);
2873         $this->t->set_var('organization', $organization);
2874         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2875         $this->t->set_var('tr_color',$tr_color);
2876         $this->t->fp('list1','calling_list',True);
2877       }
2878
2879       # Display a list ordered by organization
2880       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
2881       $this->db->query($sql,__LINE__,__FILE__);
2882       $i=0;
2883       while ($this->db->next_record())
2884         {
2885           $calling[$i]['id'] = $this->db->f('indiv_id');
2886           $calling[$i]['name'] = $this->db->f('name');
2887           $calling[$i]['position'] = $this->db->f('position');
2888           $calling[$i]['sustained'] = $this->db->f('sustained');
2889           $calling[$i]['organization'] = $this->db->f('organization');
2890           $i++;
2891         }   
2892       for ($i=0; $i < count($calling); $i++) 
2893       {
2894         $name = $calling[$i]['name'];
2895         $position = $calling[$i]['position'];
2896         $sustained = $calling[$i]['sustained'];
2897         $organization = $calling[$i]['organization'];
2898         $this->t->set_var('name', $name);
2899         $this->t->set_var('position', $position);
2900         $this->t->set_var('sustained', $sustained);
2901         $this->t->set_var('organization', $organization);
2902         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2903         $this->t->set_var('tr_color',$tr_color);
2904         $this->t->fp('list2','org_list',True);
2905       }
2906       
2907       $this->t->pfp('out','org_view_t');
2908       $this->save_sessiondata();   
2909     }
2910   
2911   function schedule()
2912     {
2913       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
2914       $this->t->set_block('sched_t','presidency_list','list');
2915
2916       $action = get_var('action',array('GET','POST'));
2917       
2918       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
2919       $this->t->set_var('title','EQ Scheduling Tool');
2920
2921       $this->t->set_var('lang_save','Save Schedule');
2922       $this->t->set_var('lang_reset','Cancel');
2923       
2924       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
2925       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2926
2927       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2928       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2929       
2930       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
2931       $table_width=$date_width + $time_width + $elder_width + $family_width;
2932       $header_row = "<th width=$date_width><font size=-2>Date</th>";
2933       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2934       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
2935       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
2936       $table_data = "";
2937
2938       if($action == 'save')
2939         {
2940           $new_data = get_var('sched',array('POST'));
2941           foreach ($new_data as $presidency_array)
2942            {
2943              foreach ($presidency_array as $entry)
2944                {
2945                  $presidency = $entry['presidency'];
2946                  $appointment = $entry['appointment'];
2947                  $date = $entry['date'];
2948                  $hour = $entry['hour'];
2949                  $minute = $entry['minute'];
2950                  $pm = $entry['pm'];
2951                  $elder = $entry['elder'];
2952                  $family = $entry['family'];
2953                  if($pm) { $hour = $hour + 12; }
2954                  $time = $hour.':'.$minute.':'.'00';
2955
2956                  // Update an existing appointment
2957                  if($appointment != 0)
2958                    {
2959                      $this->db->query("UPDATE eq_appointment set" .
2960                               " family=" . $family . 
2961                               " ,elder=" . $elder . 
2962                               " ,date='" . $date . "'" .
2963                               " ,time='" . $time . "'" .
2964                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2965
2966                      //print "updating entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                  
2967                    }
2968                  
2969                  // Add a new appointment
2970                  else if(($appointment == 0) && ($date != "") && ($time != ""))
2971                    {
2972                      $this->db->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time) "
2973                            . "VALUES ('" . $appointment . "','" . $presidency . "','" . $family . "','"
2974                            . $elder . "','" . $date . "','" . $time  ."')",__LINE__,__FILE__);
2975                      
2976                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                    
2977                    }
2978                }
2979            }
2980           
2981           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
2982           Header('Location: ' . $take_me_to_url);
2983         }
2984
2985       $sql = "SELECT * FROM eq_presidency where valid=1";
2986       $this->db->query($sql,__LINE__,__FILE__);
2987       $i=0;
2988       while ($this->db->next_record())
2989         {
2990           $presidency_data[$i]['id'] = $this->db->f('presidency');
2991           $presidency_data[$i]['name'] = $this->db->f('name');
2992           $presidency_data[$i]['elder'] = $this->db->f('elder');
2993           $i++;
2994         }
2995       
2996       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2997       $this->db->query($sql,__LINE__,__FILE__);
2998       $i=0;
2999       while ($this->db->next_record())
3000         {
3001           $elder_id[$i] = $this->db->f('elder');
3002           $elder_name[$i] = $this->db->f('name');
3003           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3004           $i++;
3005         }
3006       array_multisort($elder_name, $elder_id);
3007
3008       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3009       $this->db->query($sql,__LINE__,__FILE__);
3010       $i=0;
3011       while ($this->db->next_record())
3012         {
3013           $family_id[$i] = $this->db->f('family');
3014           $family_name[$i] = $this->db->f('name');
3015           $i++;
3016         }
3017       array_multisort($family_name, $family_id);
3018       
3019       for ($i=0; $i < count($presidency_data); $i++) {
3020         $presidency = $presidency_data[$i]['id'];
3021         $interviewer = $presidency_data[$i]['elder'];
3022         $name = $presidency_data[$i]['name'];
3023         $this->t->set_var('presidency_name',$name);
3024         $table_data="";
3025         
3026         // query the database for all the appointments
3027         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3028         $this->db->query($sql,__LINE__,__FILE__);
3029
3030         // Prefill any existing appointment slots
3031         while ($this->db->next_record())
3032           {
3033             $appointment = $this->db->f('appointment');
3034             $elder = $this->db->f('elder');
3035             $family = $this->db->f('family');
3036             
3037             $date = $this->db->f('date');
3038             $date_array = explode("-",$date);
3039             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3040             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3041             
3042             $time = $this->db->f('time');
3043             $time_array = explode(":",$time);
3044             $hour = $time_array[0];
3045             $minute = $time_array[1];
3046             $pm = 0;
3047             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3048             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3049             
3050             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3051
3052             // Date selection
3053             $table_data.= '<td align=left>';
3054             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3055             $table_data.= '</td>';
3056             
3057             // Hour & Minutes selection
3058             $table_data.= "<td align=center>";
3059             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3060             foreach(range(1,12) as $num) {
3061               if($hour == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3062               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3063             }
3064             $table_data.= '</select>';
3065             $table_data.= '&nbsp;:&nbsp;';
3066             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3067             foreach(range(0,3) as $num) {
3068               $num = $num * 15; if($num == 0) { $num = "00"; }
3069               if($minute == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3070               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3071             }
3072             $table_data.= '</select>';
3073             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3074             if($pm == 0) { $table_data.= '<option value=0 selected>am</option>'; $table_data.= '<option value=1>pm</option>'; }
3075             else { $table_data.= '<option value=0>am</option>'; $table_data.= '<option value=1 selected>pm</option>'; }
3076             $table_data.= '</select>';
3077             $table_data.= "</td>";
3078             
3079             // Elder drop down list (for PPIs)
3080             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3081             $table_data.= '<option value=0></option>';  
3082             for ($j=0; $j < count($elder_id); $j++) {
3083               $id = $elder_id[$j];
3084               $name = $elder_name[$j];
3085               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3086               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3087             }
3088             $table_data.='</select></td>';
3089
3090             // Family drop down list (for Visits)
3091             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3092             $table_data.= '<option value=0></option>';              
3093             for ($j=0; $j < count($elder_id); $j++) {
3094               $id = $family_id[$j];
3095               $name = $family_name[$j];
3096               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3097               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3098             }
3099             $table_data.='</select></td>';
3100             
3101             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3102             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3103         
3104             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3105             $this->t->set_var('tr_color',$tr_color);
3106             
3107           }
3108
3109         // Create blank appointment slot
3110         $appointment = 0;
3111         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3112
3113         // Date selection
3114         $table_data.= '<td align=left>';
3115         $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3116         $table_data.= '</td>';
3117         
3118         // Time selection
3119         $table_data.= "<td align=center>";
3120         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3121         $table_data.= '<option value=""></option>';
3122         foreach(range(1,12) as $num) {
3123           $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3124         }
3125         $table_data.= '</select>';
3126         $table_data.= '&nbsp;:&nbsp;';
3127         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3128         $table_data.= '<option value=""></option>';
3129         foreach(range(0,3) as $num) {
3130           $num = $num * 15; if($num == 0) { $num = "00"; }
3131           $table_data.= '<option value='.$num.'>'.$num.'</option>';
3132         }
3133         $table_data.= '</select>';
3134         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3135         $table_data.= '<option value=""></option>';
3136         $table_data.= '<option value=0>am</option>';
3137         $table_data.= '<option value=1>pm</option>';
3138         $table_data.= '</select>';
3139         $table_data.= "</td>";
3140         
3141         // Elder drop down list
3142         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3143         $table_data.= '<option value=0></option>';  
3144         for ($j=0; $j < count($elder_id); $j++) {
3145           $id = $elder_id[$j];
3146           $name = $elder_name[$j];
3147           $table_data.= '<option value='.$id.'>'.$name.'</option>';
3148         }
3149         $table_data.='</select></td>';
3150         
3151         // Family drop down list
3152         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3153         $table_data.= '<option value=0></option>';          
3154         for ($j=0; $j < count($elder_id); $j++) {
3155           $id = $family_id[$j];
3156           $name = $family_name[$j];
3157           $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3158         }
3159         $table_data.='</select></td>';
3160
3161         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3162         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3163         
3164         $this->t->set_var('table_data',$table_data);
3165         $this->t->set_var('header_row',$header_row);
3166         $this->t->set_var('table_width',$table_width);
3167         $this->t->fp('list','presidency_list',True);
3168         
3169       }
3170       
3171       $this->t->pfp('out','sched_t');
3172       $this->save_sessiondata();   
3173     }
3174   
3175   function admin()
3176     {
3177       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3178       $this->t->set_block('admin_t','upload','uploadhandle');
3179       $this->t->set_block('admin_t','admin','adminhandle');
3180       $this->t->set_block('admin_t','cmd','cmdhandle');
3181       
3182       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3183       
3184       $action = get_var('action',array('GET','POST'));
3185
3186       $this->t->pfp('out','admin_t');
3187       
3188       if($action == 'upload')
3189         {        
3190           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
3191           
3192           if((($_FILES['uploadedfile']['type'] == "application/zip") ||
3193               ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3194               ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3195               ($_FILES['uploadedfile']['type'] == "application/octet-stream")) &&
3196              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
3197             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3198             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3199             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3200             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3201             $this->t->set_var('uploadstatus',$uploadstatus);
3202             $this->t->pfp('uploadhandle','upload');
3203             $this->t->set_var('uploadhandle','');
3204             print "<table border=1 width=80%><tr><td>\n<pre>";
3205             
3206             # make a directory for this data to be stored in
3207             $date="data_" . date("Y_m_d");
3208             $data_dir = $this->upload_target_path . $date;
3209             print "-> Making the data directory: $date<br>\n";
3210             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
3211             if($return_code != 0) {
3212               print implode('\n',$result) . "<br>";
3213               print "<b><font color=red>";
3214               print "-E- Unable to create the data directory. Aborting import.";
3215               print "</font></b>";
3216               return 0;
3217             }
3218
3219             # move the file uploaded into this directory
3220             print "-> Moving the uploaded file into the data dir<br>\n";
3221             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3222             if($return_code != 0) {
3223               print implode('\n',$result) . "<br>";
3224               print "<b><font color=red>";
3225               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3226               print "</font></b>";
3227               return 0;
3228             }
3229             
3230             # unzip the data into this directory
3231             print "-> Unzipping the data<br>\n";
3232             $data_file = $data_dir . '';
3233             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3234             if($return_code != 0) {
3235               print implode('\n',$result) . "<br>";
3236               print "<b><font color=red>";
3237               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
3238               print "</font></b>";
3239               return 0;
3240             }
3241             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3242
3243             # update the data_latest link to point to this new directory
3244             print "-> Updating the latest data dir link<br>\n";
3245             $data_latest = $this->upload_target_path . 'data_latest';
3246             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3247             if($return_code != 0) {
3248               print implode('\n',$result) . "<br>";
3249               print "<b><font color=red>";
3250               print "-E- Unable to update the data latest link. Aborting import.";
3251               print "</font></b>";
3252               return 0;
3253             }
3254             
3255             # run the import perl script to encorporate it into the DB
3256             ob_start('ob_logstdout', 2);
3257             print "-> Importing the data into the EQ database<br>\n";
3258             ob_flush(); flush(); sleep(1);
3259             $import_log = $this->upload_target_path . '/import.log';
3260             $data_log = $this->upload_target_path . '/data.log';
3261             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
3262             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
3263             #print "import_cmd: $import_cmd<br>";
3264             #print "parse_cmd: $parse_cmd<br>";
3265             ob_start('ob_logstdout', 2);
3266             passthru($import_cmd);
3267             passthru($parse_cmd);
3268             ob_flush(); flush(); sleep(1);
3269
3270             # fix the permissions of the data dir
3271             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3272             
3273             $this->t->pfp('cmdhandle','cmd');
3274             print "</pre></td></tr></table>";
3275             
3276           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3277                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3278                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3279                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3280             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3281             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3282             $this->t->set_var('uploadstatus',$uploadstatus);
3283             $this->t->pfp('uploadhandle','upload',True);
3284           } else {
3285             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3286             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3287             $this->t->set_var('uploadstatus',$uploadstatus);
3288             $this->t->pfp('uploadhandle','upload',True);
3289           }
3290         }
3291       else
3292         {
3293           $this->t->set_var('adminhandle','');
3294           $this->t->pfp('adminhandle','admin'); 
3295         }
3296       
3297       $this->save_sessiondata();   
3298     }
3299   
3300 }
3301
3302 ?>