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