dd3343c3c7614e4349bdc4fc6c3a9384e2ea8534
[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 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 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 = 0; $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++; $num_companionships++;
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           if($num_companionships % 2 != 0) { $num_companionships++; }
971           $table_data .= "<tr><td colspan=20><hr></td></tr>";
972         }
973         // Now add Elders not assigned to any companionship to the table if we are in eqpresppi mode
974         if($eqpresppi == 1) {
975           $table_data .= "<tr bgcolor=\"#c9c9c9\"><hr><td colspan=20><b>Unassigned Potential Home Teachers</b><hr></td></tr>";
976           foreach($elders as $elder_id => $value) {
977             $sql = "SELECT * FROM eq_companionship where valid=1 and elder=".$elder_id;
978             $this->db->query($sql,__LINE__,__FILE__);
979             if(!$this->db->next_record()) {
980               // We found an Elder not in a companionship, add them to the table
981               $num_elders++; $num_companionships++;
982               $companionship=0;
983               $name = $elders[$elder_id];
984               $link_data['menuaction'] = 'eq.eq.ppi_update';
985               $link_data['companionship'] = $companionship;
986               $link_data['interviewer'] = $supervisor;
987               $link_data['elder'] = $elder_id;
988               $link_data['name'] = $name;
989               $link_data['ppi'] = '';
990               $link_data['eqpresppi'] = $eqpresppi;
991               $link_data['action'] = 'add';
992               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
993               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td><a href=$link>$name</a></td>";
994               for($m=$num_months; $m >= 0; $m--) {
995                 $year = date('Y') - $m;
996                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
997                 $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
998                        "AND elder=" . $elder_id . " AND eqpresppi=1";
999                 $this->db2->query($sql,__LINE__,__FILE__);
1000                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1001                 if($this->db2->next_record()) {
1002                   $ppis[$m]++; $total_ppis[$m]++;
1003                   $link_data['menuaction'] = 'eq.eq.ppi_update';
1004                   $link_data['companionship'] = $companionship;
1005                   $link_data['interviewer'] = $supervisor;
1006                   $link_data['elder'] = $elder_id;
1007                   $link_data['name'] = $name;
1008                   $link_data['ppi'] = $this->db2->f('ppi');
1009                   $link_data['eqpresppi'] = $eqpresppi;
1010                   $link_data['action'] = 'view';
1011                   $date = $this->db2->f('date');
1012                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1013                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$date.'</a></td>';
1014                 }
1015                 else { $table_data .= "<td>&nbsp;</td>"; }
1016               }
1017               $table_data .= "</tr>"; 
1018             }
1019           }
1020         }
1021         $num_companionships = ceil($num_companionships/2);
1022         $total_companionships += $num_companionships;
1023         if($eqpresppi == 1) {
1024           $stat_data = "<tr><td><b><font size=-2>$num_elders Elders<br>PPI Totals:</font></b></td>";
1025         } else {
1026           $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>PPI Totals:</font></b></td>";
1027         }
1028         for($m=$num_months; $m >=0; $m--) {
1029           if($eqpresppi == 1) { $percent = ceil(($ppis[$m] / $num_elders)*100); }
1030           else { $percent = ceil(($ppis[$m] / $num_companionships)*100); }
1031           $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
1032         }
1033         $stat_data .= "</tr>";
1034
1035         $this->t->set_var('table_width',$table_width);
1036         $this->t->set_var('header_row',$header_row);
1037         $this->t->set_var('table_data',$table_data);
1038         $this->t->set_var('stat_data',$stat_data);
1039         $this->t->fp('list','district_list',True);
1040       }
1041
1042       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>PPI Totals:</font></b></td>";
1043       for($m=$num_months; $m >=0; $m--) {
1044         $percent = ceil(($total_ppis[$m] / $total_companionships)*100);
1045         $totals .= "<td align=center><font size=-2><b>$total_ppis[$m]<br>$percent%</font></b></td>";
1046       }
1047       $totals .= "</tr>";
1048       
1049       $this->t->set_var('totals',$totals);
1050       $this->t->pfp('out','ppi_view_t');
1051       $this->save_sessiondata(); 
1052     }
1053
1054   function ppi_update()
1055     {
1056       $this->t->set_file(array('form' => 'ppi_update.tpl'));
1057       $this->t->set_block('form','interviewer_list','int_list');
1058       $this->t->set_block('form','add','addhandle');
1059       $this->t->set_block('form','edit','edithandle');
1060       
1061       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1062       $this->t->set_var('readonly','');
1063       $this->t->set_var('disabled','');
1064       
1065       $action = get_var('action',array('GET','POST'));
1066       $companionship = get_var('companionship',array('GET','POST'));
1067       $interviewer = get_var('interviewer',array('GET','POST'));      
1068       $name = get_var('name',array('GET','POST'));
1069       $ppi = get_var('ppi',array('GET','POST'));
1070       $elder = get_var('elder',array('GET','POST'));
1071       $date = get_var('date',array('GET','POST'));
1072       $notes = get_var('notes',array('GET','POST'));
1073       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
1074
1075       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1076       $this->db->query($sql,__LINE__,__FILE__);
1077       while ($this->db->next_record())
1078         {
1079           $supervisor = $this->db->f('supervisor');
1080           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
1081           $this->db2->query($sql,__LINE__,__FILE__);
1082           $this->db2->next_record();
1083           $interviewer_name = $this->db2->f('name');
1084           
1085           if($supervisor == $interviewer) { 
1086             $this->t->set_var('interviewer',$supervisor . ' selected');
1087           } else {
1088             $this->t->set_var('interviewer',$interviewer);
1089           }
1090           $this->t->set_var('interviewer_name',$interviewer_name);
1091           $this->t->fp('int_list','interviewer_list',True);
1092         }
1093       
1094       if($action == 'save')
1095         {
1096           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1097           $this->db->query("UPDATE eq_ppi set " .
1098                            "   ppi='" . $ppi . "'" .
1099                     ", interviewer='" . $interviewer . "'" .
1100                           ", elder='" . $elder . "'" .
1101                            ", date='" . $date . "'" .
1102                           ", notes='" . $notes . "'" .
1103                       ", eqpresppi='" . $eqpresppi . "'" .
1104                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
1105           $this->ppi_view();
1106           return false;
1107         }
1108
1109       if($action == 'insert')
1110         {
1111           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1112           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,date,notes,eqpresppi) "
1113                            . "VALUES ('" . $interviewer . "','" . $elder . "','"
1114                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
1115           $this->ppi_view();
1116           return false;
1117         }
1118       
1119       if($action == 'add')
1120         {
1121           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1122           $this->t->set_var('ppi', '');
1123           $this->t->set_var('interviewer', $interviewer);
1124           $this->t->set_var('name',$name);
1125           $this->t->set_var('elder',$elder);
1126           $this->t->set_var('date','');
1127           $this->t->set_var('notes','');
1128           $this->t->set_var('eqpresppi',$eqpresppi);
1129           $this->t->set_var('lang_done','Cancel');
1130           $this->t->set_var('lang_action','Adding New PPI');
1131           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1132                                                                 . $ppi . '&action=' . 'insert'));
1133         }
1134
1135       if($action == 'edit' || $action == 'view')
1136         {
1137           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
1138           $this->db->query($sql,__LINE__,__FILE__);
1139           $this->db->next_record();
1140           $this->t->set_var('ppi',$ppi);
1141           $this->t->set_var('name',$name);
1142           $this->t->set_var('interviewer', $this->db->f('interviewer'));
1143           $this->t->set_var('elder',$this->db->f('elder'));
1144           $this->t->set_var('date',$this->db->f('date'));
1145           $this->t->set_var('notes',$this->db->f('notes'));
1146           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
1147         }
1148       
1149       if($action == 'edit')
1150         {
1151           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1152           $this->t->set_var('lang_done','Cancel');
1153           $this->t->set_var('lang_action','Editing PPI');
1154           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1155                                                                 . $ppi . '&action=' . 'save'));
1156         }
1157
1158       if($action == 'view')
1159         {
1160           $date = $this->db->f('date');
1161           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1162           $this->t->set_var('readonly','READONLY');
1163           $this->t->set_var('disabled','DISABLED');
1164           $this->t->set_var('lang_done','Done');
1165           $this->t->set_var('lang_action','Viewing PPI');
1166           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1167                                                                 . $ppi . '&action=' . 'edit'));
1168         }
1169       
1170       $this->t->set_var('lang_reset','Clear Form');
1171       $this->t->set_var('lang_add','Add PPI');
1172       $this->t->set_var('lang_save','Save Changes');
1173       $this->t->set_var('edithandle','');
1174       $this->t->set_var('addhandle','');
1175
1176       $this->t->pfp('out','form');
1177       
1178       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
1179       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1180       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1181      
1182       $this->save_sessiondata(); 
1183     }
1184
1185   function vis_view()
1186     {
1187       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
1188       $this->t->set_block('vis_view_t','visit_list','list1');
1189       $this->t->set_block('vis_view_t','family_list','list2');
1190
1191       $this->t->set_var('lang_name','Family Name');
1192       $this->t->set_var('lang_date','Date');
1193       
1194       $sql = "SELECT * FROM eq_visit WHERE companionship=0 ORDER BY date DESC";
1195       $this->db->query($sql,__LINE__,__FILE__);
1196       $total_records = $this->db->num_rows();
1197
1198       $i = 0;
1199       while ($this->db->next_record())
1200         {
1201           $visit_list[$i]['visit'] = $this->db->f('visit');
1202           $visit_list[$i]['family'] = $this->db->f('family');
1203           $visit_list[$i]['date']  = $this->db->f('date');
1204           $i++;
1205         }
1206             
1207       for ($i=0; $i < count($visit_list); $i++)
1208         {         
1209           $this->nextmatchs->template_alternate_row_color(&$this->t);
1210
1211           $sql = "SELECT * FROM eq_family WHERE valid=1 AND family=".$visit_list[$i]['family'];
1212           $this->db->query($sql,__LINE__,__FILE__);
1213           $this->db->next_record();
1214                   
1215           $this->t->set_var('family',$visit_list[$i]['family']);
1216           $this->t->set_var('family_name',$this->db->f('name'));
1217           $this->t->set_var('date',$visit_list[$i]['date']);
1218           
1219           $link_data['menuaction'] = 'eq.eq.vis_update';
1220           $link_data['visit'] = $visit_list[$i]['visit'];
1221           $link_data['name'] = $this->db->f('name');
1222           $link_data['date'] = $visit_list[$i]['date'];
1223           $link_data['action'] = 'view';
1224           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1225           $this->t->set_var('lang_view','View');
1226
1227           $link_data['menuaction'] = 'eq.eq.vis_update';
1228           $link_data['visit'] = $visit_list[$i]['visit'];
1229           $link_data['name'] = $this->db->f('name');
1230           $link_data['date'] = $visit_list[$i]['date'];
1231           $link_data['action'] = 'edit';
1232           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1233           $this->t->set_var('lang_edit','Edit');
1234
1235           $this->t->fp('list1','visit_list',True);
1236         }
1237
1238       // List the families that are available to record a visit against
1239       $sql = "SELECT * FROM eq_family WHERE valid=1";
1240       $this->db->query($sql,__LINE__,__FILE__);
1241       $total_records = $this->db->num_rows();
1242
1243       $i = 0;
1244       while ($this->db->next_record())
1245         {
1246           $family_names[$i] = $this->db->f('name');
1247           $family_ids[$i] = $this->db->f('family');
1248           $i++;
1249         } array_multisort($family_names, $family_ids);
1250       
1251       for ($i=0; $i < count($family_names); $i++)
1252         {
1253           $link_data['menuaction'] = 'eq.eq.vis_update';
1254           $link_data['visit'] = '';
1255           $link_data['family'] = $family_ids[$i];
1256           $link_data['action'] = 'add';
1257           $link_data['name'] = $family_names[$i];
1258           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1259
1260           $this->t->set_var('name',$family_names[$i]);
1261           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
1262           else { $this->t->set_var('table_sep',"</td>"); }
1263           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
1264
1265           $this->t->fp('list2','family_list',True);
1266         }   
1267
1268       $this->t->pfp('out','vis_view_t');
1269       $this->save_sessiondata(); 
1270     }
1271
1272   function vis_update()
1273     {
1274       $this->t->set_file(array('form' => 'vis_update.tpl'));
1275       $this->t->set_block('form','add','addhandle');
1276       $this->t->set_block('form','edit','edithandle');
1277       
1278       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
1279       $this->t->set_var('readonly','');
1280       $this->t->set_var('disabled','');
1281       
1282       $action = get_var('action',array('GET','POST'));
1283       $visit = get_var('visit',array('GET','POST'));
1284       $family = get_var('family',array('GET','POST'));
1285       $name = get_var('name',array('GET','POST'));
1286       $date = get_var('date',array('GET','POST'));
1287       $notes = get_var('notes',array('GET','POST'));
1288       $companionship = 0;
1289       
1290       if($action == 'save')
1291         {
1292           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1293           $this->db->query("UPDATE eq_visit set " .
1294                            "  date='" . $date . "'" .
1295                           ", notes='" . $notes . "'" .
1296                            " WHERE visit=" . $visit,__LINE__,__FILE__);
1297           $this->vis_view();
1298           return false;
1299         }
1300
1301       if($action == 'insert')
1302         {
1303           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1304           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
1305                            . "VALUES ('" . $family . "','" . $companionship . "','"
1306                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
1307           $this->vis_view();
1308           return false;
1309         }
1310       
1311       if($action == 'add')
1312         {
1313           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1314           $this->t->set_var('family', $family);
1315           $this->t->set_var('visit', '');
1316           $this->t->set_var('name', $name);
1317           $this->t->set_var('date','');
1318           $this->t->set_var('notes','');
1319           $this->t->set_var('lang_done','Cancel');
1320           $this->t->set_var('lang_action','Adding New Visit');
1321           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
1322                                                                 . $family . '&action=' . 'insert'));
1323         }
1324
1325       if($action == 'edit' || $action == 'view')
1326         {
1327           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
1328           $this->db->query($sql,__LINE__,__FILE__);
1329           $this->db->next_record();
1330           $this->t->set_var('visit',$visit);
1331           $this->t->set_var('name',$name);
1332           $this->t->set_var('family', $family);
1333           $this->t->set_var('date',$this->db->f('date'));
1334           $this->t->set_var('notes',$this->db->f('notes'));
1335         }
1336       
1337       if($action == 'edit')
1338         {
1339           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1340           $this->t->set_var('lang_done','Cancel');
1341           $this->t->set_var('lang_action','Editing Visit');
1342           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
1343                                                                 . $visit . '&action=' . 'save'));
1344         }
1345
1346       if($action == 'view')
1347         {
1348           $date = $this->db->f('date');
1349           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1350           $this->t->set_var('readonly','READONLY');
1351           $this->t->set_var('disabled','DISABLED');
1352           $this->t->set_var('lang_done','Done');
1353           $this->t->set_var('lang_action','Viewing Visit');
1354           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
1355                                                                 . $visit . '&action=' . 'edit'));
1356         }
1357       
1358       $this->t->set_var('lang_reset','Clear Form');
1359       $this->t->set_var('lang_add','Add Visit');
1360       $this->t->set_var('lang_save','Save Changes');
1361       $this->t->set_var('edithandle','');
1362       $this->t->set_var('addhandle','');
1363
1364       $this->t->pfp('out','form');
1365       
1366       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
1367       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1368       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1369
1370       $this->save_sessiondata(); 
1371     }
1372
1373   function att_view()
1374     {
1375       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
1376       $this->t->set_block('att_view_t','act_list','list');
1377
1378       $this->t->set_block('att_view_t','month_list','list1');
1379       $this->t->set_block('att_view_t','header_list','list2');
1380       $this->t->set_block('att_view_t','elder_list','list3');
1381       
1382       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
1383       $num_months = get_var('num_months',array('GET','POST'));
1384       if($num_months == '') { $num_months = $this->default_att_num_months; }
1385       $this->t->set_var('num_months',$num_months);
1386       $this->t->set_var('lang_filter','Filter');
1387       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1388       else {  $this->t->set_var('lang_num_months','Months of History'); }
1389          
1390       $sql = "SELECT * FROM eq_elder where valid=1";
1391       $this->db->query($sql,__LINE__,__FILE__);
1392       $i=0;
1393       while ($this->db->next_record())
1394         {
1395           $elder_name[$i] = $this->db->f('name');
1396           $elder_id[$i] = $this->db->f('elder');
1397           $i++;
1398         }
1399       array_multisort($elder_name, $elder_id);
1400
1401       
1402       // Create a list of sunday dates for a window of 3 months back and current month
1403       $i=0; 
1404       $last_time = 0; 
1405       $found_sunday = 0;
1406       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, date("m")-$num_months, 1, date("y")));
1407       $last_date = explode("-",$sunday_list[0]['date']);
1408       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1409       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
1410       while($last_time < $time_limit)
1411       {
1412         $day = date("w",$last_time);
1413         if(date("w",$last_time) == 0) {
1414           $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
1415           $last_date = explode("-",$sunday_list[$i]['date']);
1416           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1417           $sunday_list[$i]['day'] = $last_date[2];
1418           $sunday_list[$i]['month'] = date("M",$last_time);
1419           $sunday_list[$i]['year'] = $last_date[0];
1420           $found_sunday = 1; 
1421         }
1422         $last_time += 90000;
1423         if($found_sunday) { $i++; $found_sunday=0; }
1424       }
1425       
1426       $old_month=$sunday_list[0]['month']; $span=0;
1427       for ($i=0; $i < count($sunday_list); $i++) {
1428         $date = $sunday_list[$i]['date'];
1429         $this->t->set_var('date',$sunday_list[$i]['date']);
1430         $this->t->set_var('day',$sunday_list[$i]['day']);
1431         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
1432           if($i == count($sunday_list)-1) { $span++; }
1433           $cur_month = $sunday_list[$i]['month'];
1434           $old_month = $sunday_list[$i]['month'];         
1435           $link_data['menuaction'] = 'eq.eq.att_update';
1436           $link_data['month'] = $sunday_list[$i-1]['month'];
1437           $link_data['year'] = $sunday_list[$i-1]['year'];
1438           $link_data['action'] = 'update_month';
1439           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1440           $this->t->set_var('month',$sunday_list[$i-1]['month']);
1441           $this->t->set_var('year',$sunday_list[$i-1]['year']);
1442           $this->t->set_var('span',$span); $span=0;
1443           $this->t->fp('list1','month_list',True);
1444         } $span++;
1445       }
1446
1447       $elder_width=200; $att_width=25; $total_width=$elder_width; 
1448       for ($i=0; $i < count($sunday_list); $i++) {
1449         $link_data['menuaction'] = 'eq.eq.att_update';
1450         $link_data['month'] = $sunday_list[$i]['month'];
1451         $link_data['year'] = $sunday_list[$i]['year'];
1452         $link_data['day'] = $sunday_list[$i]['day'];
1453         $link_data['date'] = $sunday_list[$i]['date'];
1454         $link_data['action'] = 'update_day';
1455         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1456         $this->t->set_var('date',$sunday_list[$i]['date']);
1457         $this->t->set_var('day',$sunday_list[$i]['day']);
1458         $this->t->set_var('month',$sunday_list[$i]['month']);
1459         $this->t->set_var('year',$sunday_list[$i]['year']);
1460         $this->t->fp('list2','header_list',True);
1461         $total_width += $att_width;
1462       }
1463
1464       for ($i=0; $i < count($elder_id); $i++) {
1465         $att_table = "";
1466         $this->nextmatchs->template_alternate_row_color(&$this->t);
1467         $this->t->set_var('elder_name',$elder_name[$i]);
1468         #print "checking for elder: " . $elder_id[$i] . "<br>";
1469         for ($j=0; $j < count($sunday_list); $j++) {
1470           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
1471           #print "SELECT * FROM eq_attendance WHERE date='"
1472           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
1473           $sql = "SELECT * FROM eq_attendance WHERE date='"
1474              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
1475           $this->db->query($sql,__LINE__,__FILE__);
1476           if($this->db->next_record()) {
1477             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
1478           } else {
1479             $att_table .= '<td>&nbsp;</td>';
1480           }
1481         }
1482         $this->t->set_var('att_table',$att_table);
1483         $this->t->fp('list3','elder_list',True);
1484       }
1485       $this->t->set_var('total_width',$total_width);
1486       $this->t->set_var('elder_width',$elder_width);
1487       $this->t->set_var('att_width',$att_width);
1488
1489       $this->t->pfp('out','att_view_t');
1490       $this->save_sessiondata(); 
1491     }
1492
1493   function att_update()
1494     {
1495       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
1496       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
1497       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
1498
1499       $this->t->set_file(array('form' => 'att_update.tpl'));
1500       $this->t->set_block('form','edit','edithandle');
1501       
1502       $this->t->set_block('form','month_list','list1');
1503       $this->t->set_block('form','header_list','list2');
1504       $this->t->set_block('form','elder_list','list3');
1505
1506       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
1507
1508       $action = get_var('action',array('GET','POST'));
1509       $month = get_var('month',array('GET','POST'));
1510       $year = get_var('year',array('GET','POST'));
1511       $day = get_var('day',array('GET','POST'));
1512       $date = get_var('date',array('GET','POST'));
1513
1514       if($action == 'save_month' || $action == 'save_day')
1515         {
1516            $new_data = get_var('elders_attended',array('POST'));
1517            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
1518
1519            if($action == 'save_month') {        
1520              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
1521            }
1522
1523            if($action == 'save_day') {        
1524              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
1525            }   
1526
1527            foreach ($new_data as $data)
1528            {
1529               $data_array = explode("-",$data);
1530               $elder = $data_array[0];
1531               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
1532               $this->db->query("INSERT INTO eq_attendance (elder,date) "
1533                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
1534            }
1535         
1536          $this->att_view();
1537          return false;    
1538         }
1539
1540       $sql = "SELECT * FROM eq_elder where valid=1";
1541       $this->db->query($sql,__LINE__,__FILE__);
1542       $i=0;
1543       while ($this->db->next_record())
1544         {
1545           $elder_name[$i] = $this->db->f('name');
1546           $elder_id[$i] = $this->db->f('elder');
1547           $i++;
1548         }
1549       array_multisort($elder_name, $elder_id);
1550       
1551       if($action == 'update_month')
1552       {
1553         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
1554         $i=0; 
1555         $last_time = 0; 
1556         $found_sunday = 0;
1557         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
1558         $last_date = explode("-",$sunday_list[0]['date']);
1559         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1560         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
1561         while($last_time <= $time_limit)
1562         {
1563           $day = date("w",$last_time);
1564           if(date("w",$last_time) == 0) { 
1565             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
1566             $last_date = explode("-",$sunday_list[$i]['date']);
1567             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1568             $sunday_list[$i]['day'] = $last_date[2];
1569             $sunday_list[$i]['month'] = date("M",$last_time);
1570             $sunday_list[$i]['year'] = $last_date[0];
1571             $found_sunday = 1; 
1572           }
1573           $last_time += 90000;
1574           if($found_sunday) { $i++; $found_sunday=0; }
1575         }
1576
1577         $this->t->set_var('span', $i);
1578         $this->t->set_var('month',$sunday_list[$i-1]['month']);
1579         $this->t->set_var('year',$sunday_list[$i-1]['year']);
1580         $this->t->fp('list1','month_list',True);
1581         $elder_width=200; $att_width=25; $total_width=$elder_width;
1582         for ($i=0; $i < count($sunday_list); $i++) {
1583           $link_data['menuaction'] = 'eq.eq.att_update';
1584           $link_data['month'] = $sunday_list[$i]['month'];
1585           $link_data['year'] = $sunday_list[$i]['year'];
1586           $link_data['day'] = $sunday_list[$i]['day'];
1587           $link_data['date'] = $sunday_list[$i]['date'];
1588           $link_data['action'] = 'update_day';
1589           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1590           $this->t->set_var('date',$sunday_list[$i]['date']);
1591           $this->t->set_var('day',$sunday_list[$i]['day']);
1592           $this->t->set_var('month',$sunday_list[$i]['month']);
1593           $this->t->set_var('year',$sunday_list[$i]['year']);
1594           $this->t->fp('list2','header_list',True);
1595           $total_width += $att_width;
1596         }     
1597       }
1598
1599       if($action == 'update_day')
1600       {
1601         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
1602         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
1603         $this->t->set_var('month',$month);
1604         $this->t->set_var('year',$year);
1605         $this->t->fp('list1','month_list',True);
1606         $this->t->set_var('date',$date);
1607         $this->t->set_var('day',$day);
1608         $this->t->set_var('month',$month);
1609         $this->t->set_var('year',$year);
1610         $this->t->fp('list2','header_list',True);
1611       }           
1612             
1613       for ($i=0; $i < count($elder_id); $i++) {
1614         $att_table = "";
1615         $this->nextmatchs->template_alternate_row_color(&$this->t);
1616         $this->t->set_var('elder_name',$elder_name[$i]);
1617         for ($j=0; $j < count($sunday_list); $j++) {
1618           $sql = "SELECT * FROM eq_attendance WHERE date='"
1619              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
1620           $this->db->query($sql,__LINE__,__FILE__);
1621           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
1622           if($this->db->next_record()) {
1623             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
1624           } else {
1625             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
1626           }
1627         }
1628         $this->t->set_var('att_table',$att_table);
1629         $this->t->fp('list3','elder_list',True);
1630       } 
1631            
1632       $this->t->set_var('lang_done', 'Cancel');
1633       $this->t->set_var('lang_reset','Clear Form');
1634       $this->t->set_var('lang_save','Save Changes');
1635
1636       $this->t->pfp('out','form');
1637       $this->t->pfp('addhandle','edit');
1638
1639       $this->save_sessiondata();       
1640     }
1641
1642 }
1643
1644 ?>