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