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