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