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