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