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