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