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