Added new Admin page with a data import tool.
[eq/.git] / inc / class.eq.inc.php
1 <?php
2   /**************************************************************************\
3   * phpGroupWare - eq                                                        *
4   * http://www.phpgroupware.org                                              *
5   * -----------------------------------------------                          *
6   *  This program is free software; you can redistribute it and/or modify it *
7   *  under the terms of the GNU General Public License as published by the   *
8   *  Free Software Foundation; either version 2 of the License, or (at your  *
9   *  option) any later version.                                              *
10   \**************************************************************************/
11         /* $Id: class.eq.inc.php,v 1.1.1.1 2001/05/20 07:40:32 seek3r Exp $ */
12
13 class eq
14 {
15   var $db;
16   var $db2;
17   var $t;
18   var $nextmatchs;
19   var $grants;
20   var $jscal;
21   var $cal_options;  
22   var $default_ht_num_months;
23   var $default_ppi_num_months;
24   var $default_ppi_num_years;
25   var $default_att_num_months;
26   var $current_year;
27   var $current_month;
28   var $upload_target_path;
29   var $script_path;
30   
31   var $public_functions = array
32     (
33      'ht_view'    => True,
34      'ht_update'  => True,
35      'act_list'   => True,
36      'act_view'   => True,
37      'act_update' => True,
38      'par_view'   => True,
39      'ppi_view'   => True,
40      'ppi_update' => True,
41      'vis_view'   => True,
42      'vis_update' => True,
43      'att_view'   => True,
44      'att_update' => True,
45      'dir_view'   => True,
46      'org_view'   => True,
47      'admin'      => True
48      );
49  
50   function eq()
51     {
52       $this->default_ht_num_months = 3;
53       $this->default_ppi_num_months = 3;
54       $this->default_ppi_num_years = 0;
55       $this->default_att_num_months = 3;
56       $this->upload_target_path = "/home/users/eqpres/eq_data/";
57       $this->script_path = "/usr/share/phpgroupware/eq/";
58       
59       $this->db         = $GLOBALS['phpgw']->db;
60       $this->db2        = $this->db;
61       $this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
62       $this->t          = $GLOBALS['phpgw']->template;
63       $this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
64       $this->grants     = $GLOBALS['phpgw']->acl->get_grants('eq');
65       $this->grants[$this->account] = PHPGW_ACL_READ + PHPGW_ACL_ADD + PHPGW_ACL_EDIT + PHPGW_ACL_DELETE;
66        
67       $this->jscal = CreateObject('phpgwapi.jscalendar');   // before phpgw_header() !!!
68       $this->cal_options = 'daFormat    : "%Y-%m-%d",
69                                 ifFormat    : "%Y-%m-%d",
70                                 mondayFirst : false,
71                                 weekNumbers : false';
72        
73       $GLOBALS['phpgw_info']['flags']['app_header'] = 'Elders Quorum Tools';
74       $GLOBALS['phpgw']->common->phpgw_header();
75       
76       $this->current_month = `date '+%m'`;
77       $this->current_month = $this->current_month-0; // Make it numeric
78       $this->current_year = `date '+%Y'`;
79       $this->current_year = $this->current_year-0; // Make it numeric
80
81       echo parse_navbar();
82       $this->display_app_header();      
83     }
84   
85   function save_sessiondata()
86     {
87       
88     }
89
90   function display_app_header()
91     {
92       $this->t->set_file(array('eq_header' => 'header.tpl'));
93       
94       if (isset($phpgw_info['user']['preferences']['eq']['eq_font']))
95         {
96           $font = $phpgw_info['user']['preferences']['eq']['eq_font'];
97         }
98       else
99         {
100           $font = 'Arial';
101         }
102       
103       $this->t->set_var('bg_color',$phpgw_info['theme']['th_bg']);
104       $this->t->set_var('font',$font);
105       $link_data['menuaction'] = 'eq.eq.ht_view';
106       $this->t->set_var('link_hometeaching',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
107       $this->t->set_var('lang_hometeaching','HomeTeaching');
108       $link_data['menuaction'] = 'eq.eq.act_list';
109       $this->t->set_var('link_activity',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
110       $this->t->set_var('lang_activity','Activities');
111       $link_data['menuaction'] = 'eq.eq.par_view';
112       $this->t->set_var('link_participation',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
113       $this->t->set_var('lang_participation','Participation');
114       $link_data['menuaction'] = 'eq.eq.ppi_view';
115       $this->t->set_var('link_ppi',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
116       $this->t->set_var('lang_ppi','PPIs');
117       $link_data['menuaction'] = 'eq.eq.vis_view';
118       $this->t->set_var('link_visit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
119       $this->t->set_var('lang_visit','Visits');
120       $link_data['menuaction'] = 'eq.eq.att_view';      
121       $this->t->set_var('link_attendance',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
122       $this->t->set_var('lang_attendance','Attendance');
123       $link_data['menuaction'] = 'eq.eq.dir_view';      
124       $this->t->set_var('link_dir',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
125       $this->t->set_var('lang_dir','Directory');
126       $link_data['menuaction'] = 'eq.eq.org_view';      
127       $this->t->set_var('link_org',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
128       $this->t->set_var('lang_org','Callings');
129       $link_data['menuaction'] = 'eq.eq.admin'; 
130       $this->t->set_var('link_admin',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
131       $this->t->set_var('lang_admin','Admin');
132                 
133       $this->t->pparse('out','eq_header');
134     }
135
136   function ht_view()
137     {
138       $this->t->set_file(array('ht_view_t' => 'ht_view.tpl'));
139       $this->t->set_block('ht_view_t','district_list','list');
140    
141       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
142       $num_months = get_var('num_months',array('GET','POST'));
143       if($num_months == '') { $num_months = $this->default_ht_num_months; }
144       $this->t->set_var('num_months',$num_months);
145       $this->t->set_var('lang_filter','Filter');
146       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
147       else {  $this->t->set_var('lang_num_months','Months of History'); }
148       
149       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
150       $this->t->set_var('title','Hometeaching'); 
151       
152       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
153       $this->db->query($sql,__LINE__,__FILE__);
154       $i=0;
155       while ($this->db->next_record())
156         {
157           $districts[$i]['district'] = $this->db->f('district');
158           $districts[$i]['name'] = $this->db->f('name');
159           $districts[$i]['supervisor'] = $this->db->f('supervisor');
160           $i++;
161         }
162
163       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
164       $this->db->query($sql,__LINE__,__FILE__);
165       $i=0;
166       while ($this->db->next_record())
167         {
168           $elder_id[$i] = $this->db->f('elder');
169           $elder_name[$i] = $this->db->f('name');
170           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
171           $i++;
172         }
173       array_multisort($elder_name, $elder_id);
174
175       // Make an array mapping elder_ids to elder_names
176       for($i=0; $i < count($elder_id); $i++) {
177           $id = $elder_id[$i];
178           $elders[$id] = $elder_name[$i];
179       }      
180
181       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
182       $this->db->query($sql,__LINE__,__FILE__);
183       while ($this->db->next_record())
184         {
185           $aaronic_id = $this->db->f('aaronic');
186           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
187           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
188         }
189       
190       $total_families = 0;
191       $this->nextmatchs->template_alternate_row_color(&$this->t);
192       for ($i=0; $i < count($districts); $i++) {
193         $this->t->set_var('district_number',$districts[$i]['district']);
194         $this->t->set_var('district_name',$districts[$i]['name']);      
195         $supervisor = $districts[$i]['supervisor'];
196         $unique_companionships='';
197                 
198         // Select all the unique companionship numbers for this district
199         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
200         $this->db->query($sql,__LINE__,__FILE__);
201         $j=0;
202         while ($this->db->next_record())
203           {
204             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
205             $j++;
206           }
207
208         $comp_width=450; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
209         $table_data=""; $num_companionships = 0; $num_families = 0;
210         for($m=$num_months; $m >= 0; $m--) { $visits[$m] = 0; }
211         for ($j=0; $j < count($unique_companionships); $j++) {
212           $companion_table_entry = "";
213           // Select all the companions in each companionship
214           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
215                  "companionship=". $unique_companionships[$j]['companionship'];
216           $this->db->query($sql,__LINE__,__FILE__);
217
218           while ($this->db->next_record())
219             {
220               // Get this companions information
221               if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
222               $companionship = $this->db->f('companionship');
223               $elder_id = $this->db->f('elder');
224               $aaronic_id = $this->db->f('aaronic');
225               if($elder_id) {
226                 $name = $elders[$elder_id];
227                 $phone = $elder_phone[$elder_id];
228               }
229               else if($aaronic_id) {
230                 $name = $aaronic[$aaronic_id]['name'];
231                 $phone = $aaronic[$aaronic_id]['phone'];                
232               }
233               $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
234             }
235           $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
236           
237           // Get the names of the families assigned this home teaching companionship
238           $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
239           $sql = $sql . " ORDER BY name ASC";
240           $this->db->query($sql,__LINE__,__FILE__);
241           $k=0;
242           while ($this->db->next_record())
243             {
244               $num_families++; $total_families++;
245               $family_name = $this->db->f('name');
246               $family_id = $this->db->f('family');
247               $this->nextmatchs->template_alternate_row_color(&$this->t);
248               $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
249               // Find out how many times Visits were performed by this companionship
250               // in the past $num_months for this Family
251               $header_row="<th width=$comp_width><font size=-2>Families</th>";
252               for($m=$num_months; $m >= 0; $m--) {
253                 $month = $this->current_month - $m;
254                 $year = $this->current_year;
255                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
256                 if($month < 10) { $month = "0"."$month"; }
257                 $month_start = "$year"."-"."$month"."-"."01";
258                 $month_end = "$year"."-"."$month"."-"."31";
259                 $month = "$month"."/"."$year";
260                 //print "m: $m month: $month year: $year month_start: $month_start month_end: $month_end<br>";
261                 // Add this to the query to filter on only visits made by this companionship:
262                 // " AND companionship=" . $unique_companionships[$j]['companionship'].
263                 $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
264                        " AND companionship!=0".
265                        " AND family=". $family_id;
266                 $this->db2->query($sql,__LINE__,__FILE__);
267                 $link_data['menuaction'] = 'eq.eq.ht_update';
268                 $link_data['date'] = $month_start;
269                 $link_data['month_start'] = $month_start;
270                 $link_data['month_end'] = $month_end;
271                 $link_data['month'] = $month;
272                 $link_data['district'] = $districts[$i]['district'];
273                 $link_data['district_name'] = $districts[$i]['name'];
274                 $link_data['action'] = 'view';
275                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
276                 $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
277                 if(!$total_visits[$m]) { $total_visits[$m] = 0; }
278                 if($this->db2->next_record()) {
279                   if($this->db2->f('visited') == 'y') {
280                     $visits[$m]++; $total_visits[$m]++;           
281                     $table_data .= '<td align=center><a href="'.$link.'"><img src="checkmark.gif"></a></td>';
282                   }
283                   else if($this->db2->f('visited') == 'n') {
284                     $table_data .= '<td align=center><a href="'.$link.'"><img src="x.gif"></a></td>';
285                   }
286                   else {
287                     $visits[$m]++; $total_visits[$m]++;
288                     $table_data .= "<td>&nbsp;</td>";
289                   }
290                 }
291                 else {
292                   $visits[$m]++; $total_visits[$m]++;
293                   $table_data .= "<td>&nbsp;</td>";
294                 }
295               }
296               $table_data .= "</tr>"; 
297               $k++;
298             }
299           $table_data .= "<tr><td colspan=20></td></tr>";
300         }
301         $table_data .= "<tr><td colspan=20><hr></td></tr>";
302         $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
303
304         for($m=$num_months; $m >=0; $m--) {
305           $percent = ceil(($visits[$m] / $num_families)*100);
306           $stat_data .= "<td align=center><font size=-2><b>$visits[$m]<br>$percent%</font></b></td>";
307         }
308         $stat_data .= "</tr>";
309
310         $this->t->set_var('table_width',$table_width);
311         $this->t->set_var('header_row',$header_row);
312         $this->t->set_var('table_data',$table_data);
313         $this->t->set_var('stat_data',$stat_data);
314         $this->t->fp('list','district_list',True);
315       }
316
317       $totals = "<tr><td><b><font size=-2>$total_families Total Families<br>Visit Totals:</font></b></td>";
318       for($m=$num_months; $m >=0; $m--) {
319         $percent = ceil(($total_visits[$m] / $total_families)*100);
320         $totals .= "<td align=center><font size=-2><b>$total_visits[$m]<br>$percent%</font></b></td>";
321       }
322       $totals .= "</tr>";
323       
324       $this->t->set_var('totals',$totals);
325       
326       $this->t->pfp('out','ht_view_t');
327       $this->save_sessiondata();
328     }
329       
330
331   function ht_update()
332     {
333       $this->t->set_file(array('ht_update_t' => 'ht_update.tpl'));
334       $this->t->set_block('ht_update_t','district_list','list');
335       $this->t->set_block('ht_update_t','save','savehandle');
336       
337       $district = get_var('district',array('GET','POST'));
338       $district_name = get_var('district_name',array('GET','POST'));
339       $date = get_var('date',array('GET','POST'));
340       $month = get_var('month',array('GET','POST'));
341       $month_start = get_var('month_start',array('GET','POST'));
342       $month_end = get_var('month_end',array('GET','POST'));
343       $action = get_var('action',array('GET','POST'));
344       
345       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
346       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_update&action=save'));
347       $this->t->set_var('lang_done','Cancel');
348       $this->t->set_var('district_name',$district_name);
349       $this->t->set_var('district_number',$district);
350       $this->t->set_var('title','Hometeaching Update ' . $month);
351       $this->t->set_var('date',$date);
352       
353       if($action == 'save')
354         {
355           // Get a list of all the companionships in this district
356           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
357           $this->db->query($sql,__LINE__,__FILE__);
358           $j=0;
359           while ($this->db->next_record())
360             {
361               $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
362               $j++;
363             }
364           for ($j=0; $j < count($unique_companionships); $j++)
365             {
366               // FIXME: We won't be able to go back and edit history on families that have been
367               // reassigned to a different companionship. The following delete command will not delete
368               // the history of visits under an older companionship, only the ones for the existing
369               // companionship. This will lead to duplicate visits being entered for an older
370               // month for the same family, making it impossible to change the past history once
371               // a family is reassigned. However, you will be able to view the history just fine.
372
373               //$comp=$unique_companionships[$j]['companionship'];
374               //print "deleting from eq_visit where companionship=$comp and date=$date and district=$district<br>";
375               // Delete all the visits that have taken place for all families for this month
376               $this->db->query("DELETE from eq_visit where companionship=" . $unique_companionships[$j]['companionship'] .
377                                " AND " . "date='" . $date . "'",__LINE__,__FILE__);
378             }
379
380           // Now, add the visits that are checked for this month
381           $new_data = get_var('family_visited',array('POST'));
382           foreach ($new_data as $family)
383            {
384              foreach ($family as $data)
385                {
386                  //print "family_visited: $data <br>";
387                  $data_array = explode("/",$data);
388                  $family_id = $data_array[0];
389                  $companionship = $data_array[1];
390                  $date = $data_array[2];
391                  $visited = $data_array[3];
392                  if($visited == "") { $visited = $data_array[4]; }
393                  //print "family_id: $family_id companionship: $companionship date: $date visited: $visited<br>";
394                  $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes,visited) "
395                                   . "VALUES (" . $family_id .",". $companionship .",'". $date ."','','". $visited ."')",__LINE__,__FILE__);
396                }
397            }
398           $this->ht_view();
399           return false;
400         }
401       
402       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
403       $this->db->query($sql,__LINE__,__FILE__);
404       $i=0;
405       while ($this->db->next_record())
406         {
407           $elder_id[$i] = $this->db->f('elder');
408           $elder_name[$i] = $this->db->f('name');
409           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
410           $i++;
411         }
412       array_multisort($elder_name, $elder_id);
413
414       // Make an array mapping elder_ids to elder_names
415       for($i=0; $i < count($elder_id); $i++) {
416           $id = $elder_id[$i];
417           $elders[$id] = $elder_name[$i];
418       }      
419
420       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
421       $this->db->query($sql,__LINE__,__FILE__);
422       while ($this->db->next_record())
423         {
424           $aaronic_id = $this->db->f('aaronic');
425           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
426           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
427         }
428       
429       // Select all the unique companionship numbers for this district
430       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
431       $this->db->query($sql,__LINE__,__FILE__);
432       $j=0; $unique_companionships='';
433       while ($this->db->next_record())
434         {
435           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
436           $j++;
437         }
438       
439       $comp_width=300; $visit_width=25; $table_width=$comp_width + $visit_width;
440       $table_data=""; $num_companionships = 0; $num_families = 0; $visits=0;
441       for ($j=0; $j < count($unique_companionships); $j++) {
442         $companion_table_entry = "";
443         // Select all the companions in each companionship
444         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
445                "companionship=". $unique_companionships[$j]['companionship'];
446         $this->db->query($sql,__LINE__,__FILE__);
447         
448         while ($this->db->next_record())
449           {
450             // Get this companions information
451             if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
452             $companionship = $this->db->f('companionship');
453             $elder_id = $this->db->f('elder');
454             $aaronic_id = $this->db->f('aaronic');
455             if($elder_id) {
456               $name = $elders[$elder_id];
457               $phone = $elder_phone[$elder_id];
458             }
459             else if($aaronic_id) {
460               $name = $aaronic[$aaronic_id]['name'];
461               $phone = $aaronic[$aaronic_id]['phone'];
462             }
463             $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
464           }
465         $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
466         
467         // Get the names of the families assigned this home teaching companionship
468         $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
469         $sql = $sql . " ORDER BY name ASC";
470         $this->db->query($sql,__LINE__,__FILE__);
471         while ($this->db->next_record())
472           {
473             $num_families++; $total_families++;
474             $family_name = $this->db->f('name');
475             $family_id = $this->db->f('family');
476             $this->nextmatchs->template_alternate_row_color(&$this->t);
477             $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
478             
479             $header_row="<th width=$comp_width><font size=-2>Families</th>";
480             $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
481                    " AND companionship!=0".
482                    " AND family=". $family_id;
483             $this->db2->query($sql,__LINE__,__FILE__);
484             $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
485             $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
486             if(!$total_visits) { $total_visits = 0; }
487             if($this->db2->next_record()) {
488               if($this->db2->f('visited') == 'y') {
489                 $visits++; $total_visits++;
490                 $table_data .= '<td width=100 align=center>';
491                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y" checked>Y';
492                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
493                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/"> ';
494                 $table_data .= '</td>';
495               } else if($this->db2->f('visited') == 'n') {
496                 $table_data .= '<td width=100 align=center>';
497                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
498                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n" checked>N';
499                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/">';
500                 $table_data .= '</td>';
501               } else {
502                 $table_data .= '<td width=100 align=center>';
503                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
504                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
505                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
506                 $table_data .= '</td>';
507               }
508             }
509             else {
510               $value .= "/";
511               $table_data .= '<td width=100 align=center>';
512               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
513               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
514               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
515               $table_data .= '</td>';         
516             }
517           }
518         $table_data .= "</tr>"; 
519         $table_data .= "<tr><td colspan=20></td></tr>";
520       }
521       $table_data .= "<tr><td colspan=20><hr></td></tr>";
522       $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
523       
524       $percent = ceil(($visits / $num_families)*100);
525       $stat_data .= "<td align=center><font size=-2><b>$visits<br>$percent%</font></b></td>";
526       $stat_data .= "</tr>";
527       
528       $this->t->set_var('table_width',$table_width);
529       $this->t->set_var('header_row',$header_row);
530       $this->t->set_var('table_data',$table_data);
531       $this->t->set_var('stat_data',$stat_data);
532       $this->t->fp('list','district_list',True);
533
534       $this->t->set_var('lang_reset','Clear Form');
535       $this->t->set_var('lang_save','Save Changes');
536       $this->t->set_var('savehandle','');
537       
538       $this->t->pfp('out','ht_update_t');
539       $this->t->pfp('addhandle','save');
540       
541       $this->save_sessiondata();
542     }
543
544   function act_list()
545     {
546       $this->t->set_file(array('act_list_t' => 'act_list.tpl'));
547       $this->t->set_block('act_list_t','act_list','list');
548       
549       $this->t->set_var('lang_name','Activity Name');
550       $this->t->set_var('lang_date','Date');
551       
552       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
553       $this->db->query($sql,__LINE__,__FILE__);
554       $total_records = $this->db->num_rows();
555
556       $i = 0;
557       while ($this->db->next_record())
558         {
559           $activity_list[$i]['name'] = $this->db->f('name');
560           $activity_list[$i]['date']  = $this->db->f('date');
561           $activity_list[$i]['activity']  = $this->db->f('activity');
562           $i++;
563         }
564             
565       for ($i=0; $i < count($activity_list); $i++)
566         {         
567           $this->nextmatchs->template_alternate_row_color(&$this->t);
568           $this->t->set_var('name',$activity_list[$i]['name']);
569           $this->t->set_var('date',$activity_list[$i]['date']);
570           
571           $link_data['menuaction'] = 'eq.eq.act_view';
572           $link_data['activity'] = $activity_list[$i]['activity'];
573           $link_data['action'] = 'view';
574           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
575           $this->t->set_var('lang_view','View');
576
577           $link_data['menuaction'] = 'eq.eq.act_update';
578           $link_data['activity'] = $activity_list[$i]['activity'];
579           $link_data['action'] = 'edit';
580           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
581           $this->t->set_var('lang_edit','Edit');
582
583           $link_data['menuaction'] = 'eq.eq.act_update';
584           $link_data['activity'] = '0';
585           $link_data['action'] = 'add';
586           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
587                            . '"><input type="submit" name="Add" value="' . 'Add Activity' .'"></font></form>');
588
589           $this->t->fp('list','act_list',True);
590         }
591
592       $this->t->pfp('out','act_list_t');
593       $this->save_sessiondata();
594     }
595
596   function act_view()
597     {
598       $this->t->set_file(array('act_view_t' => 'act_view.tpl'));
599       $this->t->set_block('act_view_t','part_list','list');
600       
601       $sql = "SELECT * FROM eq_activity WHERE activity=" . intval(get_var('activity',array('GET','POST')));
602       $this->db->query($sql,__LINE__,__FILE__);
603       $this->db->next_record();
604       $this->t->set_var('name', $this->db->f('name'));
605       $this->t->set_var('date', $this->db->f('date'));
606       $this->t->set_var('notes', $this->db->f('notes'));
607             
608       $this->t->set_var('lang_name','Activity Name');
609       $this->t->set_var('lang_date','Date');
610       $this->t->set_var('lang_notes','Notes');
611       $this->t->set_var('lang_done','Done');
612       $this->t->set_var('lang_action','View');
613
614       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
615       $this->t->set_var('tr_color',$tr_color);
616             
617       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
618
619       $link_data['menuaction'] = 'eq.eq.act_update';
620       $link_data['activity'] = get_var('activity',array('GET','POST'));
621       $link_data['action'] = 'edit';
622       $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
623       $this->t->set_var('lang_edit','Edit');
624       $this->t->set_var('cal_date',$this->db->f('date'));
625
626       // Now find out which elders participated in this activity
627       $sql = "SELECT * FROM eq_participation WHERE activity=" . intval(get_var('activity',array('GET','POST')));
628       $this->db->query($sql,__LINE__,__FILE__);
629       $total_records = $this->db->num_rows();
630
631       $i = 0;
632       while ($this->db->next_record())
633         {
634           $part_list[$i]['elder']  = $this->db->f('elder');
635           $i++;
636         }
637       
638       for ($i=0; $i < count($part_list); $i++)
639         {
640           $sql = "SELECT * FROM eq_elder WHERE elder=" . $part_list[$i]['elder'];
641           $this->db->query($sql,__LINE__,__FILE__);
642           $this->db->next_record();
643           $names[$i] = $this->db->f('name');
644         } sort($names);
645       
646       for ($i=0; $i < count($names); $i++)
647         {
648           //$this->nextmatchs->template_alternate_row_color(&$this->t);
649           $this->t->set_var('elder_name',$names[$i]);
650           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
651           else { $this->t->set_var('table_sep',"</td>"); }
652           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
653           $this->t->fp('list','part_list',True);
654         }
655       
656       $this->t->pfp('out','act_view_t');
657       $this->save_sessiondata();
658     }
659
660   function act_update()
661     {
662       $this->t->set_file(array('form' => 'act_update.tpl'));
663       $this->t->set_block('form','elder_list','list');
664       $this->t->set_block('form','add','addhandle');
665       $this->t->set_block('form','edit','edithandle');
666       $this->t->set_var('lang_done','Done');
667
668       $action = get_var('action',array('GET','POST'));
669       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
670       $activity['activity'] = intval(get_var('activity',array('GET','POST')));
671
672       if($action == 'save')
673         {
674           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
675           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
676           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
677           $this->db->query("UPDATE eq_activity set " .
678                            "   name='" . $activity['name'] .
679                            "', date='" . $activity['date'] . "'" .
680                            ", notes='" . $activity['notes'] . "'" .
681                            " WHERE activity=" . $activity['activity'],__LINE__,__FILE__);
682
683           // Delete all the elders who have particiapted in this activity
684           $this->db->query("DELETE from eq_participation where activity=".$activity['activity'],__LINE__,__FILE__);
685           
686           // Re-add the elders who are checked as having participated in this activity
687           $elders = get_var('elder_name',array('POST'));
688           foreach ($elders as $elder)
689             {
690               $this->db->query("INSERT INTO eq_participation (elder,activity) "
691                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
692             }
693
694           $this->act_list();
695           return false;
696         }
697
698       if($action == 'insert')
699         {
700           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
701           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
702           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
703           $this->db->query("INSERT INTO eq_activity (name,date,notes) "
704                            . "VALUES ('" . $activity['name'] . "','"
705                            . $activity['date'] . "','" . $activity['notes'] . "')",__LINE__,__FILE__);
706           $this->act_list();
707           return false;
708         }
709       
710       if($action == 'add')
711         {
712           $activity['activity'] = 0;
713           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
714           $this->t->set_var('name','');
715           $this->t->set_var('date','');
716           $this->t->set_var('notes','');
717           $this->t->set_var('lang_done','Cancel');
718           $this->t->set_var('lang_action','Adding New Activity');
719           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
720                                                                 . $activity['activity'] . '&action=' . 'insert'));
721         }
722
723       if($action == 'edit')
724         {
725           $sql = "SELECT * FROM eq_activity WHERE activity=" . $activity['activity'];
726           $this->db->query($sql,__LINE__,__FILE__);
727           $this->db->next_record();
728           $this->t->set_var('cal_date',$this->jscal->input('date',$this->db->f('date'),'','','','','',$this->cal_options));
729           $this->t->set_var('name', $this->db->f('name'));
730           $this->t->set_var('date', $this->db->f('date'));
731           $this->t->set_var('notes', $this->db->f('notes'));
732           $this->t->set_var('lang_done','Cancel');
733           $this->t->set_var('lang_action','Editing Activity');
734           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
735                                                                 . $activity['activity'] . '&action=' . 'save'));
736
737         }
738
739       $sql = "SELECT * FROM eq_elder";
740       $this->db->query($sql,__LINE__,__FILE__);
741       $i=0;
742       while ($this->db->next_record())
743         {
744           if($this->db->f('valid') == 1 || $action != 'add') {
745             $elder_name[$i] = $this->db->f('name');
746             $elder_id[$i] = $this->db->f('elder');
747             $elder_valid[$i] = $this->db->f('valid');
748             $i++;
749           }
750         }
751       array_multisort($elder_name, $elder_id, $elder_valid);
752
753       $j=0;
754       for ($i=0; $i < count($elder_id); $i++)
755         {
756           //$this->nextmatchs->template_alternate_row_color(&$this->t);
757           $sql = "SELECT * FROM eq_participation where activity=". $activity['activity'] . " AND elder=" . $elder_id[$i];
758           $this->db->query($sql,__LINE__,__FILE__);
759           if($this->db->next_record()) { $this->t->set_var('checked','checked'); $checked=1; }
760           else { $this->t->set_var('checked',''); $checked=0; }
761           if($checked || $elder_valid[$i] == 1) {
762             $this->t->set_var('elder_name',$elder_name[$i]);
763             $this->t->set_var('elder',$elder_id[$i]);
764             if(($j+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
765             else { $this->t->set_var('table_sep',"</td>"); }
766             if(($j) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
767             $this->t->fp('list','elder_list',True);
768             $j++;
769           }
770         }
771       
772       $this->t->set_var('lang_reset','Clear Form');
773       $this->t->set_var('lang_add','Add Activity');
774       $this->t->set_var('lang_save','Save Changes');
775       $this->t->set_var('edithandle','');
776       $this->t->set_var('addhandle','');
777       
778       $this->t->pfp('out','form');
779       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
780       if($action == 'add') { $this->t->pfp('addhandle','add'); }
781       
782       $this->save_sessiondata();
783     }
784
785   function par_view()
786     {
787       $this->t->set_file(array('par_view_t' => 'par_view.tpl'));
788       $this->t->set_block('par_view_t','header_list','list1');
789       $this->t->set_block('par_view_t','elder_list','list2');
790
791       $sql = "SELECT * FROM eq_elder where valid=1";
792       $this->db->query($sql,__LINE__,__FILE__);
793       $i=0;
794       while ($this->db->next_record())
795         {
796           $elder_name[$i] = $this->db->f('name');
797           $elder_id[$i] = $this->db->f('elder');
798           $i++;
799         }
800       array_multisort($elder_name, $elder_id);
801
802       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
803       $this->db->query($sql,__LINE__,__FILE__);
804       $total_records = $this->db->num_rows();
805
806       $i = 0;
807       while ($this->db->next_record())
808         {
809           $activity_list[$i]['name'] = $this->db->f('name');
810           $activity_list[$i]['date'] = $this->db->f('date');
811           $activity_list[$i]['activity']  = $this->db->f('activity');
812           if($activity_list[$i]['date'] == '0000-00-00') { $activity_list[$i]['date']=""; }
813           $i++;
814         }
815
816       $elder_width=250; $part_width=25; $act_width=50;
817       $total_width=$elder_width+$part_width;
818       for ($i=0; $i < count($activity_list); $i++) {
819         $this->t->set_var('activity_name',$activity_list[$i]['name']);
820         $this->t->set_var('activity_date',$activity_list[$i]['date']);
821         $this->t->fp('list1','header_list',True);
822         $total_width += $act_width;
823       }
824
825       for ($i=0; $i < count($elder_id); $i++) {
826         $participated=0; $part_table = ''; 
827         $this->nextmatchs->template_alternate_row_color(&$this->t);
828         $this->t->set_var('elder_name',$elder_name[$i]);
829         for ($j=0; $j < count($activity_list); $j++) {
830           $sql = "SELECT * FROM eq_participation where activity="
831              . $activity_list[$j]['activity'] . " AND elder=" . $elder_id[$i];
832           $this->db->query($sql,__LINE__,__FILE__);
833           if($this->db->next_record()) {
834             $part_table .= '<td align=center><img src="checkmark.gif"></td>';
835             $participated++;
836           } else {
837             $part_table .= '<td>&nbsp;</td>';
838           }
839         }
840         if($participated) { $part_table .= '<td align=center><img src="checkmark.gif">'.$participated.'</td>'; }
841         else { $part_table .= '<td>&nbsp;</td>'; }
842         $this->t->set_var('part_table',$part_table);
843         $this->t->fp('list2','elder_list',True);
844       }
845       $this->t->set_var('total_width',$total_width);
846       $this->t->set_var('elder_width',$elder_width);
847       $this->t->set_var('part_width',$part_width);
848       $this->t->set_var('act_width',$act_width);
849       $this->t->pfp('out','par_view_t');
850       $this->save_sessiondata(); 
851     }
852
853   function ppi_view()
854     {
855       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
856       $this->t->set_block('ppi_view_t','district_list','list');
857
858       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
859       $num_months = get_var('num_months',array('GET','POST'));
860       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
861       $this->t->set_var('num_months',$num_months);
862       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
863       else {  $this->t->set_var('lang_num_months','Months of History'); }
864       $this->t->set_var('lang_filter','Filter');
865       
866       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
867       $this->t->set_var('eqpres_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view&eqpresppi=1'));
868       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
869       
870       if($eqpresppi == 1) {
871         $num_months = get_var('num_months',array('GET','POST'));
872         if($num_months == '') { $num_months = $this->default_ppi_num_years; }
873         $this->t->set_var('num_months',$num_months);
874         $this->t->set_var('ppi_link_title','PPIs');
875         $this->t->set_var('title','EQ President Yearly PPIs');
876         if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
877         else { $this->t->set_var('lang_num_months','Years of History'); }
878         $this->t->set_var('eqpres_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
879       }
880       else { 
881         $this->t->set_var('ppi_link_title','EQ President Yearly PPIs'); 
882         $this->t->set_var('title','PPIs'); 
883       }
884
885       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
886       $this->db->query($sql,__LINE__,__FILE__);
887       $i=0;
888       while ($this->db->next_record())
889         {
890           if($eqpresppi == 1 && $this->db->f('district') == 1) {
891             $districts[$i]['district'] = $this->db->f('district');
892             $districts[$i]['name'] = $this->db->f('name');
893             $districts[$i]['supervisor'] = $this->db->f('supervisor');
894           } else if($eqpresppi == 0) {
895             $districts[$i]['district'] = $this->db->f('district');
896             $districts[$i]['name'] = $this->db->f('name');
897             $districts[$i]['supervisor'] = $this->db->f('supervisor');
898             $i++;
899           }
900         }
901
902       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
903       $this->db->query($sql,__LINE__,__FILE__);
904       $i=0;
905       while ($this->db->next_record())
906         {
907           $elder_id[$i] = $this->db->f('elder');
908           $elder_name[$i] = $this->db->f('name');
909           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
910           $i++;
911         }
912       array_multisort($elder_name, $elder_id);
913       for($i=0; $i < count($elder_id); $i++) {
914           $id = $elder_id[$i];
915           $elders[$id] = $elder_name[$i];
916       }      
917
918       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
919       $this->db->query($sql,__LINE__,__FILE__);
920       while ($this->db->next_record())
921         {
922           $aaronic_id = $this->db->f('aaronic');
923           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
924           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
925         }
926       
927       $total_companionships = 0;
928       $this->nextmatchs->template_alternate_row_color(&$this->t);
929       for ($i=0; $i < count($districts); $i++) {
930         if($eqpresppi == 1) { 
931           $this->t->set_var('district_number','*');
932           $this->t->set_var('district_name','EQ President');
933         } else {
934           $this->t->set_var('district_number',$districts[$i]['district']);
935           $this->t->set_var('district_name',$districts[$i]['name']);    
936         }
937         $supervisor = $districts[$i]['supervisor'];
938         $unique_companionships='';
939                 
940         // Select all the unique companionship numbers for this district
941         if($eqpresppi == 1) { 
942           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1";
943         } 
944         else {
945           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
946         }
947         $this->db->query($sql,__LINE__,__FILE__);
948         $j=0;
949         while ($this->db->next_record())
950           {
951             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
952             $j++;
953           }
954         
955         $comp_width=400; $ppi_width=25; $table_width=$comp_width + $num_months*$ppi_width;
956         $table_data=""; $num_companionships = $j; $num_elders = 0;
957         for($m=$num_months; $m >= 0; $m--) { $ppis[$m] = 0; }
958         for ($j=0; $j < count($unique_companionships); $j++) {
959           // Select all the companions in each companionship
960           if($eqpresppi) {
961             $sql = "SELECT * FROM eq_companionship where valid=1 and aaronic=0 and ".
962                    "companionship=". $unique_companionships[$j]['companionship'];  
963           }
964           else {
965             $sql = "SELECT * FROM eq_companionship where valid=1 and ".
966                    "companionship=". $unique_companionships[$j]['companionship'];
967           }
968           $this->db->query($sql,__LINE__,__FILE__);
969           $k=0;
970           $comp = $unique_companionships[$j]['companionship'];
971           for($m=$num_months; $m >= 0; $m--) { $ppi_recorded[$comp][$m] = 0; }
972           while ($this->db->next_record())
973             {
974               // Get this companions information
975               $num_elders++;
976               $companionship = $this->db->f('companionship');
977               $elder_id = $this->db->f('elder');
978               $aaronic_id = $this->db->f('aaronic');
979               if($elder_id) {
980                 $name = $elders[$elder_id];
981                 $phone = $elder_phone[$elder_id];
982               }
983               else if($aaronic_id) {
984                 $name = $aaronic[$aaronic_id]['name'];
985                 $phone = $aaronic[$aaronic_id]['phone'];
986               }
987               $link_data['menuaction'] = 'eq.eq.ppi_update';
988               $link_data['companionship'] = $companionship;
989               $link_data['interviewer'] = $supervisor;
990               $link_data['elder'] = $elder_id;
991               $link_data['aaronic'] = $aaronic_id;
992               $link_data['name'] = $name;
993               $link_data['ppi'] = '';
994               $link_data['eqpresppi'] = $eqpresppi;
995               $link_data['action'] = 'add';
996               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
997               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
998
999               // Find out how many times PPIs were performed in the past $num_months for this Elder
1000               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
1001               for($m=$num_months; $m >= 0; $m--) {
1002                 if($eqpresppi == 1) {
1003                   $year = date('Y') - $m;
1004                   $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1005                   $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1006                          "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id . " AND eqpresppi=1";
1007                   $this->db2->query($sql,__LINE__,__FILE__);
1008                   $header_row .= "<th width=150><font size=-2>$year</th>"; 
1009                 }
1010                 else {
1011                   $month = $this->current_month - $m;
1012                   $year = $this->current_year;
1013                   if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
1014                   if($month < 10) { $month = "0"."$month"; }
1015                   $month_start = "$year"."-"."$month"."-"."01";
1016                   $month_end = "$year"."-"."$month"."-"."31";
1017                   $month = "$month"."/"."$year";
1018                   $sql = "SELECT * FROM eq_ppi WHERE date >= '$month_start' AND date <= '$month_end' ".
1019                          "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id . " AND eqpresppi=0";
1020                   $this->db2->query($sql,__LINE__,__FILE__);
1021                   $header_row .= "<th width=$ppi_width><font size=-2>$month</th>";
1022                 }
1023                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1024                 if($this->db2->next_record()) {
1025                   if(!$ppi_recorded[$companionship][$m]) {
1026                     $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
1027                   }
1028                   $link_data['menuaction'] = 'eq.eq.ppi_update';
1029                   $link_data['companionship'] = $companionship;
1030                   $link_data['interviewer'] = $this->db2->f('interviewer');
1031                   $link_data['elder'] = $elder_id;
1032                   $link_data['aaronic'] = $aaronic_id;
1033                   $link_data['name'] = $name;
1034                   $link_data['ppi'] = $this->db2->f('ppi');
1035                   $link_data['eqpresppi'] = $eqpresppi;
1036                   $link_data['action'] = 'view';
1037                   $date = $this->db2->f('date');
1038                   $date_array = explode("-",$date);
1039                   $month = $date_array[1];
1040                   $day   = $date_array[2];
1041                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1042                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$month.'-'.$day.'</a></td>';
1043                 }
1044                 else { $table_data .= "<td>&nbsp;</td>"; }
1045               }
1046               $table_data .= "</tr>"; 
1047               $k++;
1048             }
1049           $table_data .= "<tr><td colspan=20><hr></td></tr>";
1050         }
1051         // Now add Elders not assigned to any companionship to the table if we are in eqpresppi mode
1052         if($eqpresppi == 1) {
1053           $table_data .= "<tr bgcolor=\"#c9c9c9\"><hr><td colspan=20><b>Unassigned Potential Home Teachers</b><hr></td></tr>";
1054           foreach($elders as $elder_id => $value) {
1055             $sql = "SELECT * FROM eq_companionship where valid=1 and elder=".$elder_id;
1056             $this->db->query($sql,__LINE__,__FILE__);
1057             if(!$this->db->next_record()) {
1058               // We found an Elder not in a companionship, add them to the table
1059               $num_elders++;
1060               $companionship=0;
1061               $name = $elders[$elder_id];
1062               $link_data['menuaction'] = 'eq.eq.ppi_update';
1063               $link_data['companionship'] = $companionship;
1064               $link_data['interviewer'] = $supervisor;
1065               $link_data['elder'] = $elder_id;
1066               $link_data['name'] = $name;
1067               $link_data['ppi'] = '';
1068               $link_data['eqpresppi'] = $eqpresppi;
1069               $link_data['action'] = 'add';
1070               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1071               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td><a href=$link>$name</a></td>";
1072               for($m=$num_months; $m >= 0; $m--) {
1073                 $year = date('Y') - $m;
1074                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1075                 $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1076                        "AND elder=" . $elder_id . " AND eqpresppi=1";
1077                 $this->db2->query($sql,__LINE__,__FILE__);
1078                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1079                 if($this->db2->next_record()) {
1080                   $ppis[$m]++; $total_ppis[$m]++;
1081                   $link_data['menuaction'] = 'eq.eq.ppi_update';
1082                   $link_data['companionship'] = $companionship;
1083                   $link_data['interviewer'] = $supervisor;
1084                   $link_data['elder'] = $elder_id;
1085                   $link_data['name'] = $name;
1086                   $link_data['ppi'] = $this->db2->f('ppi');
1087                   $link_data['eqpresppi'] = $eqpresppi;
1088                   $link_data['action'] = 'view';
1089                   $date = $this->db2->f('date');
1090                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1091                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$date.'</a></td>';
1092                 }
1093                 else { $table_data .= "<td>&nbsp;</td>"; }
1094               }
1095               $table_data .= "</tr>"; 
1096             }
1097           }
1098         }
1099         $total_companionships += $num_companionships;
1100         if($eqpresppi == 1) {
1101           $stat_data = "<tr><td><b><font size=-2>$num_elders Elders<br>PPI Totals:</font></b></td>";
1102         } else {
1103           $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>PPI Totals:</font></b></td>";
1104         }
1105         for($m=$num_months; $m >=0; $m--) {
1106           if($eqpresppi == 1) { $percent = ceil(($ppis[$m] / $num_elders)*100); }
1107           else { $percent = ceil(($ppis[$m] / $num_companionships)*100); }
1108           $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
1109         }
1110         $stat_data .= "</tr>";
1111
1112         $this->t->set_var('table_width',$table_width);
1113         $this->t->set_var('header_row',$header_row);
1114         $this->t->set_var('table_data',$table_data);
1115         $this->t->set_var('stat_data',$stat_data);
1116         $this->t->fp('list','district_list',True);
1117       }
1118
1119       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>PPI Totals:</font></b></td>";
1120       for($m=$num_months; $m >=0; $m--) {
1121         $percent = ceil(($total_ppis[$m] / $total_companionships)*100);
1122         $totals .= "<td align=center><font size=-2><b>$total_ppis[$m]<br>$percent%</font></b></td>";
1123       }
1124       $totals .= "</tr>";
1125       
1126       $this->t->set_var('totals',$totals);
1127       $this->t->pfp('out','ppi_view_t');
1128       $this->save_sessiondata(); 
1129     }
1130
1131   function ppi_update()
1132     {
1133       $this->t->set_file(array('form' => 'ppi_update.tpl'));
1134       $this->t->set_block('form','interviewer_list','int_list');
1135       $this->t->set_block('form','add','addhandle');
1136       $this->t->set_block('form','edit','edithandle');
1137       
1138       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1139       $this->t->set_var('readonly','');
1140       $this->t->set_var('disabled','');
1141       
1142       $action = get_var('action',array('GET','POST'));
1143       $companionship = get_var('companionship',array('GET','POST'));
1144       $interviewer = get_var('interviewer',array('GET','POST'));      
1145       $name = get_var('name',array('GET','POST'));
1146       $ppi = get_var('ppi',array('GET','POST'));
1147       $elder = get_var('elder',array('GET','POST'));
1148       $aaronic = get_var('aaronic',array('GET','POST'));
1149       $date = get_var('date',array('GET','POST'));
1150       $notes = get_var('notes',array('GET','POST'));
1151       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
1152       
1153       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1154       $this->db->query($sql,__LINE__,__FILE__);
1155       while ($this->db->next_record())
1156         {
1157           $supervisor = $this->db->f('supervisor');
1158           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
1159           $this->db2->query($sql,__LINE__,__FILE__);
1160           $this->db2->next_record();
1161           $interviewer_name = $this->db2->f('name');
1162           
1163           if($supervisor == $interviewer) { 
1164             $this->t->set_var('interviewer',$supervisor . ' selected');
1165           } else {
1166             $this->t->set_var('interviewer',$interviewer);
1167           }
1168           $this->t->set_var('interviewer_name',$interviewer_name);
1169           $this->t->fp('int_list','interviewer_list',True);
1170         }
1171       
1172       if($action == 'save')
1173         {
1174           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1175           $this->db->query("UPDATE eq_ppi set " .
1176                            "   ppi='" . $ppi . "'" .
1177                     ", interviewer='" . $interviewer . "'" .
1178                           ", elder='" . $elder . "'" .
1179                         ", aaronic='" . $aaronic . "'" .
1180                            ", date='" . $date . "'" .
1181                           ", notes='" . $notes . "'" .
1182                       ", eqpresppi='" . $eqpresppi . "'" .
1183                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
1184           $this->ppi_view();
1185           return false;
1186         }
1187
1188       if($action == 'insert')
1189         {
1190           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1191           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
1192                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
1193                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
1194           $this->ppi_view();
1195           return false;
1196         }
1197       
1198       if($action == 'add')
1199         {
1200           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1201           $this->t->set_var('ppi', '');
1202           $this->t->set_var('interviewer', $interviewer);
1203           $this->t->set_var('name',$name);
1204           $this->t->set_var('elder',$elder);
1205           $this->t->set_var('aaronic',$aaronic);
1206           $this->t->set_var('date','');
1207           $this->t->set_var('notes','');
1208           $this->t->set_var('eqpresppi',$eqpresppi);
1209           $this->t->set_var('lang_done','Cancel');
1210           $this->t->set_var('lang_action','Adding New PPI');
1211           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1212                                                                 . $ppi . '&action=' . 'insert'));
1213         }
1214
1215       if($action == 'edit' || $action == 'view')
1216         {
1217           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
1218           $this->db->query($sql,__LINE__,__FILE__);
1219           $this->db->next_record();
1220           $this->t->set_var('ppi',$ppi);
1221           $this->t->set_var('name',$name);
1222           $this->t->set_var('interviewer', $this->db->f('interviewer'));
1223           $this->t->set_var('elder',$this->db->f('elder'));
1224           $this->t->set_var('aaronic',$this->db->f('aaronic'));
1225           $this->t->set_var('date',$this->db->f('date'));
1226           $this->t->set_var('notes',$this->db->f('notes'));
1227           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
1228         }
1229       
1230       if($action == 'edit')
1231         {
1232           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1233           $this->t->set_var('lang_done','Cancel');
1234           $this->t->set_var('lang_action','Editing PPI');
1235           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1236                                                                 . $ppi . '&action=' . 'save'));
1237         }
1238
1239       if($action == 'view')
1240         {
1241           $date = $this->db->f('date');
1242           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1243           $this->t->set_var('readonly','READONLY');
1244           $this->t->set_var('disabled','DISABLED');
1245           $this->t->set_var('lang_done','Done');
1246           $this->t->set_var('lang_action','Viewing PPI');
1247           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1248                                                                 . $ppi . '&action=' . 'edit'));
1249         }
1250       
1251       $this->t->set_var('lang_reset','Clear Form');
1252       $this->t->set_var('lang_add','Add PPI');
1253       $this->t->set_var('lang_save','Save Changes');
1254       $this->t->set_var('edithandle','');
1255       $this->t->set_var('addhandle','');
1256
1257       $this->t->pfp('out','form');
1258       
1259       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
1260       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1261       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1262      
1263       $this->save_sessiondata(); 
1264     }
1265
1266   function vis_view()
1267     {
1268       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
1269       $this->t->set_block('vis_view_t','visit_list','list1');
1270       $this->t->set_block('vis_view_t','family_list','list2');
1271
1272       $this->t->set_var('lang_name','Family Name');
1273       $this->t->set_var('lang_date','Date');
1274       
1275       $sql = "SELECT * FROM eq_visit WHERE companionship=0 ORDER BY date DESC";
1276       $this->db->query($sql,__LINE__,__FILE__);
1277       $total_records = $this->db->num_rows();
1278
1279       $i = 0;
1280       while ($this->db->next_record())
1281         {
1282           $visit_list[$i]['visit'] = $this->db->f('visit');
1283           $visit_list[$i]['family'] = $this->db->f('family');
1284           $visit_list[$i]['date']  = $this->db->f('date');
1285           $i++;
1286         }
1287             
1288       for ($i=0; $i < count($visit_list); $i++)
1289         {         
1290           $this->nextmatchs->template_alternate_row_color(&$this->t);
1291
1292           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
1293           $this->db->query($sql,__LINE__,__FILE__);
1294           $this->db->next_record();
1295                   
1296           $this->t->set_var('family',$visit_list[$i]['family']);
1297           $this->t->set_var('family_name',$this->db->f('name'));
1298           $this->t->set_var('date',$visit_list[$i]['date']);
1299           
1300           $link_data['menuaction'] = 'eq.eq.vis_update';
1301           $link_data['visit'] = $visit_list[$i]['visit'];
1302           $link_data['name'] = $this->db->f('name');
1303           $link_data['date'] = $visit_list[$i]['date'];
1304           $link_data['action'] = 'view';
1305           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1306           $this->t->set_var('lang_view','View');
1307
1308           $link_data['menuaction'] = 'eq.eq.vis_update';
1309           $link_data['visit'] = $visit_list[$i]['visit'];
1310           $link_data['name'] = $this->db->f('name');
1311           $link_data['date'] = $visit_list[$i]['date'];
1312           $link_data['action'] = 'edit';
1313           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1314           $this->t->set_var('lang_edit','Edit');
1315
1316           $this->t->fp('list1','visit_list',True);
1317         }
1318
1319       // List the families that are available to record a visit against
1320       $sql = "SELECT * FROM eq_family WHERE valid=1";
1321       $this->db->query($sql,__LINE__,__FILE__);
1322       $total_records = $this->db->num_rows();
1323
1324       $i = 0;
1325       while ($this->db->next_record())
1326         {
1327           $family_names[$i] = $this->db->f('name');
1328           $family_ids[$i] = $this->db->f('family');
1329           $i++;
1330         } array_multisort($family_names, $family_ids);
1331       
1332       for ($i=0; $i < count($family_names); $i++)
1333         {
1334           $link_data['menuaction'] = 'eq.eq.vis_update';
1335           $link_data['visit'] = '';
1336           $link_data['family'] = $family_ids[$i];
1337           $link_data['action'] = 'add';
1338           $link_data['name'] = $family_names[$i];
1339           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1340
1341           $this->t->set_var('name',$family_names[$i]);
1342           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
1343           else { $this->t->set_var('table_sep',"</td>"); }
1344           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
1345
1346           $this->t->fp('list2','family_list',True);
1347         }   
1348
1349       $this->t->pfp('out','vis_view_t');
1350       $this->save_sessiondata(); 
1351     }
1352
1353   function vis_update()
1354     {
1355       $this->t->set_file(array('form' => 'vis_update.tpl'));
1356       $this->t->set_block('form','add','addhandle');
1357       $this->t->set_block('form','edit','edithandle');
1358       
1359       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
1360       $this->t->set_var('readonly','');
1361       $this->t->set_var('disabled','');
1362       
1363       $action = get_var('action',array('GET','POST'));
1364       $visit = get_var('visit',array('GET','POST'));
1365       $family = get_var('family',array('GET','POST'));
1366       $name = get_var('name',array('GET','POST'));
1367       $date = get_var('date',array('GET','POST'));
1368       $notes = get_var('notes',array('GET','POST'));
1369       $companionship = 0;
1370       
1371       if($action == 'save')
1372         {
1373           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1374           $this->db->query("UPDATE eq_visit set " .
1375                            "  date='" . $date . "'" .
1376                           ", notes='" . $notes . "'" .
1377                            " WHERE visit=" . $visit,__LINE__,__FILE__);
1378           $this->vis_view();
1379           return false;
1380         }
1381
1382       if($action == 'insert')
1383         {
1384           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1385           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
1386                            . "VALUES ('" . $family . "','" . $companionship . "','"
1387                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
1388           $this->vis_view();
1389           return false;
1390         }
1391       
1392       if($action == 'add')
1393         {
1394           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1395           $this->t->set_var('family', $family);
1396           $this->t->set_var('visit', '');
1397           $this->t->set_var('name', $name);
1398           $this->t->set_var('date','');
1399           $this->t->set_var('notes','');
1400           $this->t->set_var('lang_done','Cancel');
1401           $this->t->set_var('lang_action','Adding New Visit');
1402           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
1403                                                                 . $family . '&action=' . 'insert'));
1404         }
1405
1406       if($action == 'edit' || $action == 'view')
1407         {
1408           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
1409           $this->db->query($sql,__LINE__,__FILE__);
1410           $this->db->next_record();
1411           $this->t->set_var('visit',$visit);
1412           $this->t->set_var('name',$name);
1413           $this->t->set_var('family', $family);
1414           $this->t->set_var('date',$this->db->f('date'));
1415           $this->t->set_var('notes',$this->db->f('notes'));
1416         }
1417       
1418       if($action == 'edit')
1419         {
1420           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1421           $this->t->set_var('lang_done','Cancel');
1422           $this->t->set_var('lang_action','Editing Visit');
1423           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
1424                                                                 . $visit . '&action=' . 'save'));
1425         }
1426
1427       if($action == 'view')
1428         {
1429           $date = $this->db->f('date');
1430           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1431           $this->t->set_var('readonly','READONLY');
1432           $this->t->set_var('disabled','DISABLED');
1433           $this->t->set_var('lang_done','Done');
1434           $this->t->set_var('lang_action','Viewing Visit');
1435           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
1436                                                                 . $visit . '&action=' . 'edit'));
1437         }
1438       
1439       $this->t->set_var('lang_reset','Clear Form');
1440       $this->t->set_var('lang_add','Add Visit');
1441       $this->t->set_var('lang_save','Save Changes');
1442       $this->t->set_var('edithandle','');
1443       $this->t->set_var('addhandle','');
1444
1445       $this->t->pfp('out','form');
1446       
1447       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
1448       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1449       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1450
1451       $this->save_sessiondata(); 
1452     }
1453
1454   function att_view()
1455     {
1456       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
1457       $this->t->set_block('att_view_t','act_list','list');
1458
1459       $this->t->set_block('att_view_t','month_list','list1');
1460       $this->t->set_block('att_view_t','header_list','list2');
1461       $this->t->set_block('att_view_t','elder_list','list3');
1462       
1463       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
1464       $num_months = get_var('num_months',array('GET','POST'));
1465       if($num_months == '') { $num_months = $this->default_att_num_months; }
1466       $this->t->set_var('num_months',$num_months);
1467       $this->t->set_var('lang_filter','Filter');
1468       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1469       else {  $this->t->set_var('lang_num_months','Months of History'); }
1470          
1471       $sql = "SELECT * FROM eq_elder where valid=1";
1472       $this->db->query($sql,__LINE__,__FILE__);
1473       $i=0;
1474       while ($this->db->next_record())
1475         {
1476           $elder_name[$i] = $this->db->f('name');
1477           $elder_id[$i] = $this->db->f('elder');
1478           $i++;
1479         }
1480       array_multisort($elder_name, $elder_id);
1481
1482       
1483       // Create a list of sunday dates for a window of 3 months back and current month
1484       $i=0; 
1485       $last_time = 0; 
1486       $found_sunday = 0;
1487       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, date("m")-$num_months, 1, date("y")));
1488       $last_date = explode("-",$sunday_list[0]['date']);
1489       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1490       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
1491       while($last_time < $time_limit)
1492       {
1493         $day = date("w",$last_time);
1494         if(date("w",$last_time) == 0) {
1495           $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
1496           $last_date = explode("-",$sunday_list[$i]['date']);
1497           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1498           $sunday_list[$i]['day'] = $last_date[2];
1499           $sunday_list[$i]['month'] = date("M",$last_time);
1500           $sunday_list[$i]['year'] = $last_date[0];
1501           $found_sunday = 1; 
1502         }
1503         $last_time += 90000;
1504         if($found_sunday) { $i++; $found_sunday=0; }
1505       }
1506
1507       $total_elders = count($elder_id);
1508       $old_month=$sunday_list[0]['month']; $span=0;
1509       for ($i=0; $i < count($sunday_list); $i++) {
1510         $date = $sunday_list[$i]['date'];
1511         $this->t->set_var('date',$sunday_list[$i]['date']);
1512         $this->t->set_var('day',$sunday_list[$i]['day']);
1513         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
1514           if($i == count($sunday_list)-1) { $span++; }
1515           $cur_month = $sunday_list[$i]['month'];
1516           $old_month = $sunday_list[$i]['month'];         
1517           $link_data['menuaction'] = 'eq.eq.att_update';
1518           $link_data['month'] = $sunday_list[$i-1]['month'];
1519           $link_data['year'] = $sunday_list[$i-1]['year'];
1520           $link_data['action'] = 'update_month';
1521           $cur_month = $sunday_list[$i-1]['month'];
1522           $cur_year = $sunday_list[$i-1]['year'];
1523           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
1524           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1525           $this->t->set_var('month',$sunday_list[$i-1]['month']);
1526           $this->t->set_var('year',$sunday_list[$i-1]['year']);
1527           $this->t->set_var('span',$span); $span=0;
1528           $this->t->fp('list1','month_list',True);
1529         } $span++;
1530       }
1531       $this->t->set_var('total_elders',$total_elders);
1532       $this->t->set_var('header_row',$header_row);
1533       
1534       $elder_width=200; $att_width=25; $total_width=$elder_width; 
1535       for ($i=0; $i < count($sunday_list); $i++) {
1536         $link_data['menuaction'] = 'eq.eq.att_update';
1537         $link_data['month'] = $sunday_list[$i]['month'];
1538         $link_data['year'] = $sunday_list[$i]['year'];
1539         $link_data['day'] = $sunday_list[$i]['day'];
1540         $link_data['date'] = $sunday_list[$i]['date'];
1541         $link_data['action'] = 'update_day';
1542         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1543         $this->t->set_var('date',$sunday_list[$i]['date']);
1544         $this->t->set_var('day',$sunday_list[$i]['day']);
1545         $this->t->set_var('month',$sunday_list[$i]['month']);
1546         $this->t->set_var('year',$sunday_list[$i]['year']);
1547         $this->t->fp('list2','header_list',True);
1548         $total_width += $att_width;
1549       }
1550
1551       for ($i=0; $i < count($elder_id); $i++) {
1552         $att_table = "";
1553         $this->nextmatchs->template_alternate_row_color(&$this->t);
1554         $this->t->set_var('elder_name',$elder_name[$i]);
1555         #print "checking for elder: " . $elder_id[$i] . "<br>";
1556         for ($j=0; $j < count($sunday_list); $j++) {
1557           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
1558           #print "SELECT * FROM eq_attendance WHERE date='"
1559           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
1560           $sql = "SELECT * FROM eq_attendance WHERE date='"
1561              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
1562           $this->db->query($sql,__LINE__,__FILE__);
1563           if($this->db->next_record()) {
1564             $cur_month = $sunday_list[$j]['month'];
1565             if($attended[$i][$cur_month] != 1) { 
1566               $attended[$i][$cur_month]=1;
1567               $attendance[$cur_month]++;
1568             }
1569             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
1570           } else {
1571             $att_table .= '<td>&nbsp;</td>';
1572           }
1573         }
1574         $this->t->set_var('att_table',$att_table);
1575         $this->t->fp('list3','elder_list',True);
1576       }
1577       $this->t->set_var('total_width',$total_width);
1578       $this->t->set_var('elder_width',$elder_width);
1579       $this->t->set_var('att_width',$att_width);
1580
1581       # Now calculate attendance for these months
1582       $attendance_str = "";
1583       $nonattendance_str = "";
1584       $aveattendance_str = "";
1585       $avenonattendance_str = "";
1586       $num_months=0;
1587       $ave_total_attended=0;
1588       foreach($attendance as $att => $value) {
1589         $total_attended = $attendance[$att];
1590         $ave_total_attended += $attendance[$att]; $num_months++;
1591         $percent = ceil(($total_attended / $total_elders)*100);
1592         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
1593         $total_nonattended = $total_elders - $total_attended;
1594         $percent = ceil(($total_nonattended / $total_elders)*100);
1595         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
1596         
1597         $total_attended = ceil(($ave_total_attended / $num_months));
1598         $percent = ceil(($total_attended / $total_elders)*100);
1599         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
1600         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
1601         $percent = ceil(($total_attended / $total_elders)*100);
1602         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
1603       }
1604       
1605       $this->t->set_var('attendance',$attendance_str);
1606       $this->t->set_var('aveattendance',$aveattendance_str);
1607       $this->t->set_var('nonattendance',$nonattendance_str);
1608       $this->t->set_var('avenonattendance',$avenonattendance_str);
1609       
1610       $this->t->pfp('out','att_view_t');
1611       $this->save_sessiondata(); 
1612     }
1613
1614   function att_update()
1615     {
1616       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
1617       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
1618       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
1619
1620       $this->t->set_file(array('form' => 'att_update.tpl'));
1621       $this->t->set_block('form','edit','edithandle');
1622       
1623       $this->t->set_block('form','month_list','list1');
1624       $this->t->set_block('form','header_list','list2');
1625       $this->t->set_block('form','elder_list','list3');
1626
1627       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
1628
1629       $action = get_var('action',array('GET','POST'));
1630       $month = get_var('month',array('GET','POST'));
1631       $year = get_var('year',array('GET','POST'));
1632       $day = get_var('day',array('GET','POST'));
1633       $date = get_var('date',array('GET','POST'));
1634
1635       if($action == 'save_month' || $action == 'save_day')
1636         {
1637            $new_data = get_var('elders_attended',array('POST'));
1638            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
1639
1640            if($action == 'save_month') {        
1641              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
1642            }
1643
1644            if($action == 'save_day') {        
1645              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
1646            }   
1647
1648            foreach ($new_data as $data)
1649            {
1650               $data_array = explode("-",$data);
1651               $elder = $data_array[0];
1652               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
1653               $this->db->query("INSERT INTO eq_attendance (elder,date) "
1654                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
1655            }
1656         
1657          $this->att_view();
1658          return false;    
1659         }
1660
1661       $sql = "SELECT * FROM eq_elder where valid=1";
1662       $this->db->query($sql,__LINE__,__FILE__);
1663       $i=0;
1664       while ($this->db->next_record())
1665         {
1666           $elder_name[$i] = $this->db->f('name');
1667           $elder_id[$i] = $this->db->f('elder');
1668           $i++;
1669         }
1670       array_multisort($elder_name, $elder_id);
1671       
1672       if($action == 'update_month')
1673       {
1674         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
1675         $i=0; 
1676         $last_time = 0; 
1677         $found_sunday = 0;
1678         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
1679         $last_date = explode("-",$sunday_list[0]['date']);
1680         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1681         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
1682         while($last_time <= $time_limit)
1683         {
1684           $day = date("w",$last_time);
1685           if(date("w",$last_time) == 0) { 
1686             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
1687             $last_date = explode("-",$sunday_list[$i]['date']);
1688             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1689             $sunday_list[$i]['day'] = $last_date[2];
1690             $sunday_list[$i]['month'] = date("M",$last_time);
1691             $sunday_list[$i]['year'] = $last_date[0];
1692             $found_sunday = 1; 
1693           }
1694           $last_time += 90000;
1695           if($found_sunday) { $i++; $found_sunday=0; }
1696         }
1697
1698         $this->t->set_var('span', $i);
1699         $this->t->set_var('month',$sunday_list[$i-1]['month']);
1700         $this->t->set_var('year',$sunday_list[$i-1]['year']);
1701         $this->t->fp('list1','month_list',True);
1702         $elder_width=200; $att_width=25; $total_width=$elder_width;
1703         for ($i=0; $i < count($sunday_list); $i++) {
1704           $link_data['menuaction'] = 'eq.eq.att_update';
1705           $link_data['month'] = $sunday_list[$i]['month'];
1706           $link_data['year'] = $sunday_list[$i]['year'];
1707           $link_data['day'] = $sunday_list[$i]['day'];
1708           $link_data['date'] = $sunday_list[$i]['date'];
1709           $link_data['action'] = 'update_day';
1710           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1711           $this->t->set_var('date',$sunday_list[$i]['date']);
1712           $this->t->set_var('day',$sunday_list[$i]['day']);
1713           $this->t->set_var('month',$sunday_list[$i]['month']);
1714           $this->t->set_var('year',$sunday_list[$i]['year']);
1715           $this->t->fp('list2','header_list',True);
1716           $total_width += $att_width;
1717         }     
1718       }
1719
1720       if($action == 'update_day')
1721       {
1722         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
1723         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
1724         $this->t->set_var('month',$month);
1725         $this->t->set_var('year',$year);
1726         $this->t->fp('list1','month_list',True);
1727         $this->t->set_var('date',$date);
1728         $this->t->set_var('day',$day);
1729         $this->t->set_var('month',$month);
1730         $this->t->set_var('year',$year);
1731         $this->t->fp('list2','header_list',True);
1732       }           
1733             
1734       for ($i=0; $i < count($elder_id); $i++) {
1735         $att_table = "";
1736         $this->nextmatchs->template_alternate_row_color(&$this->t);
1737         $this->t->set_var('elder_name',$elder_name[$i]);
1738         for ($j=0; $j < count($sunday_list); $j++) {
1739           $sql = "SELECT * FROM eq_attendance WHERE date='"
1740              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
1741           $this->db->query($sql,__LINE__,__FILE__);
1742           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
1743           if($this->db->next_record()) {
1744             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
1745           } else {
1746             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
1747           }
1748         }
1749         $this->t->set_var('att_table',$att_table);
1750         $this->t->fp('list3','elder_list',True);
1751       } 
1752            
1753       $this->t->set_var('lang_done', 'Cancel');
1754       $this->t->set_var('lang_reset','Clear Form');
1755       $this->t->set_var('lang_save','Save Changes');
1756
1757       $this->t->pfp('out','form');
1758       $this->t->pfp('addhandle','edit');
1759
1760       $this->save_sessiondata();       
1761     }
1762
1763   function dir_view()
1764     {
1765       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
1766       $this->t->set_block('dir_view_t','dir_list','list');
1767       
1768       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
1769       $this->db->query($sql,__LINE__,__FILE__);
1770       $i=0;
1771       while ($this->db->next_record())
1772         {
1773           $parent[$i]['id'] = $this->db->f('parent');
1774           $parent[$i]['name'] = $this->db->f('name');
1775           $parent[$i]['phone'] = $this->db->f('phone');
1776           $parent[$i]['address'] = $this->db->f('address');
1777           $i++;
1778         }   
1779       
1780       for ($i=0; $i < count($parent); $i++) 
1781       {
1782         $name = $parent[$i]['name'];
1783         $phone = $parent[$i]['phone'];
1784         $address = $parent[$i]['address'];
1785         $this->t->set_var('name', $name);
1786         $this->t->set_var('address', $address);
1787         $this->t->set_var('phone', $phone);
1788         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1789         $this->t->set_var('tr_color',$tr_color);
1790         $this->t->fp('list','dir_list',True);
1791         //print "$phone $name $address<br>";
1792       }
1793       $this->t->pfp('out','dir_view_t');
1794       $this->save_sessiondata();   
1795     }
1796   
1797   function org_view()
1798     {
1799       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
1800       $this->t->set_block('org_view_t','calling_list','list1');
1801       $this->t->set_block('org_view_t','org_list','list2');
1802
1803       # Display a list ordered alphabetically
1804       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
1805       $this->db->query($sql,__LINE__,__FILE__);
1806       $i=0;
1807       while ($this->db->next_record())
1808         {
1809           $calling[$i]['id'] = $this->db->f('indiv_id');
1810           $calling[$i]['name'] = $this->db->f('name');
1811           $calling[$i]['position'] = $this->db->f('position');
1812           $calling[$i]['sustained'] = $this->db->f('sustained');
1813           $calling[$i]['organization'] = $this->db->f('organization');
1814           $i++;
1815         }   
1816       for ($i=0; $i < count($calling); $i++) 
1817       {
1818         $name = $calling[$i]['name'];
1819         $position = $calling[$i]['position'];
1820         $sustained = $calling[$i]['sustained'];
1821         $organization = $calling[$i]['organization'];
1822         $this->t->set_var('name', $name);
1823         $this->t->set_var('position', $position);
1824         $this->t->set_var('sustained', $sustained);
1825         $this->t->set_var('organization', $organization);
1826         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1827         $this->t->set_var('tr_color',$tr_color);
1828         $this->t->fp('list1','calling_list',True);
1829       }
1830
1831       # Display a list ordered by organization
1832       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
1833       $this->db->query($sql,__LINE__,__FILE__);
1834       $i=0;
1835       while ($this->db->next_record())
1836         {
1837           $calling[$i]['id'] = $this->db->f('indiv_id');
1838           $calling[$i]['name'] = $this->db->f('name');
1839           $calling[$i]['position'] = $this->db->f('position');
1840           $calling[$i]['sustained'] = $this->db->f('sustained');
1841           $calling[$i]['organization'] = $this->db->f('organization');
1842           $i++;
1843         }   
1844       for ($i=0; $i < count($calling); $i++) 
1845       {
1846         $name = $calling[$i]['name'];
1847         $position = $calling[$i]['position'];
1848         $sustained = $calling[$i]['sustained'];
1849         $organization = $calling[$i]['organization'];
1850         $this->t->set_var('name', $name);
1851         $this->t->set_var('position', $position);
1852         $this->t->set_var('sustained', $sustained);
1853         $this->t->set_var('organization', $organization);
1854         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1855         $this->t->set_var('tr_color',$tr_color);
1856         $this->t->fp('list2','org_list',True);
1857       }
1858       
1859       $this->t->pfp('out','org_view_t');
1860       $this->save_sessiondata();   
1861     }
1862
1863   function admin()
1864     {
1865       $this->t->set_file(array('admin_t' => 'admin.tpl'));
1866       $this->t->set_block('admin_t','upload','uploadhandle');
1867       $this->t->set_block('admin_t','admin','adminhandle');
1868       $this->t->set_block('admin_t','cmd','cmdhandle');
1869       
1870       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
1871       
1872       $action = get_var('action',array('GET','POST'));
1873
1874       $this->t->pfp('out','admin_t');
1875       
1876       if($action == 'upload')
1877         {        
1878           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
1879           
1880           if(($_FILES['uploadedfile']['type'] == "application/zip") &&
1881              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
1882             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
1883             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
1884             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
1885             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
1886             $this->t->set_var('uploadstatus',$uploadstatus);
1887             $this->t->pfp('uploadhandle','upload');
1888             $this->t->set_var('uploadhandle','');
1889             print "<table border=1 width=80%><tr><td>\n<pre>";
1890             
1891             # make a directory for this data to be stored in
1892             $date="data_" . date("Y_m_d");
1893             $data_dir = $this->upload_target_path . $date;
1894             print "-> Making the data directory: $date<br>\n";
1895             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
1896             if($return_code != 0) {
1897               print implode('\n',$result) . "<br>";
1898               print "<b><font color=red>";
1899               print "-E- Unable to create the data directory. Aborting import.";
1900               print "</font></b>";
1901               return 0;
1902             }
1903
1904             # move the file uploaded into this directory
1905             print "-> Moving the uploaded file into the data dir<br>\n";
1906             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
1907             if($return_code != 0) {
1908               print implode('\n',$result) . "<br>";
1909               print "<b><font color=red>";
1910               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
1911               print "</font></b>";
1912               return 0;
1913             }
1914             
1915             # unzip the data into this directory
1916             print "-> Unzipping the data<br>\n";
1917             $data_file = $data_dir . '';
1918             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
1919             if($return_code != 0) {
1920               print implode('\n',$result) . "<br>";
1921               print "<b><font color=red>";
1922               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
1923               print "</font></b>";
1924               return 0;
1925             }
1926             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
1927
1928             # update the data_latest link to point to this new directory
1929             print "-> Updating the latest data dir link<br>\n";
1930             $data_latest = $this->upload_target_path . 'data_latest';
1931             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
1932             if($return_code != 0) {
1933               print implode('\n',$result) . "<br>";
1934               print "<b><font color=red>";
1935               print "-E- Unable to update the data latest link. Aborting import.";
1936               print "</font></b>";
1937               return 0;
1938             }
1939             
1940             # run the import perl script to encorporate it into the DB
1941             ob_start('ob_logstdout', 2);
1942             print "-> Importing the data into the EQ database<br>\n";
1943             ob_flush(); flush(); sleep(1);
1944             $import_log = $this->upload_target_path . '/import.log';
1945             $data_log = $this->upload_target_path . '/data.log';
1946             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
1947             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
1948             #print "import_cmd: $import_cmd<br>";
1949             #print "parse_cmd: $parse_cmd<br>";
1950             ob_start('ob_logstdout', 2);
1951             passthru($import_cmd);
1952             passthru($parse_cmd);
1953             ob_flush(); flush(); sleep(1);
1954
1955             # fix the permissions of the data dir
1956             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
1957             
1958             $this->t->pfp('cmdhandle','cmd');
1959             print "</pre></td></tr></table>";
1960             
1961           } else if($_FILES['uploadedfile']['type'] != "application/zip") {
1962             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
1963             $this->t->set_var('uploadstatus',$uploadstatus);
1964             
1965           } else {
1966             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
1967             $uploadstatus.= ") uploading the file, please try again! </font></b>";
1968             $this->t->set_var('uploadstatus',$uploadstatus);
1969           }
1970         }
1971       else
1972         {
1973           $this->t->set_var('adminhandle','');
1974           $this->t->pfp('adminhandle','admin'); 
1975         }
1976       
1977       $this->save_sessiondata();   
1978     }
1979   
1980 }
1981
1982 ?>