a20f1561cd580d7dd35c28e72c180a3878655a6d
[eq/.git] / inc / class.eq.inc.php
1 <?php
2   /**************************************************************************\
3   * phpGroupWare - eq                                                        *
4   * http://www.phpgroupware.org                                              *
5   * -----------------------------------------------                          *
6   *  This program is free software; you can redistribute it and/or modify it *
7   *  under the terms of the GNU General Public License as published by the   *
8   *  Free Software Foundation; either version 2 of the License, or (at your  *
9   *  option) any later version.                                              *
10   \**************************************************************************/
11         /* $Id: class.eq.inc.php,v 1.1.1.1 2001/05/20 07:40:32 seek3r Exp $ */
12
13 class eq
14 {
15   var $db;
16   var $db2;
17   var $t;
18   var $nextmatchs;
19   var $grants;
20   var $jscal;
21   var $cal_options;  
22   var $default_ht_num_months;
23   var $default_ppi_num_months;
24   var $default_ppi_num_years;
25   var $default_att_num_months;
26   
27   var $public_functions = array
28     (
29      'ht_view'    => True,
30      'ht_update'  => True,
31      'act_list'   => True,
32      'act_view'   => True,
33      'act_update' => True,
34      'par_view'   => True,
35      'ppi_view'   => True,
36      'ppi_update' => True,
37      'vis_view'   => True,
38      'vis_update' => True,
39      'att_view'   => True,
40      'att_update' => True
41      );
42  
43   function eq()
44     {
45       $this->default_ht_num_months = 1;
46       $this->default_ppi_num_months = 1;
47       $this->default_ppi_num_years = 0;
48       $this->default_att_num_months = 1;
49       
50       $this->db         = $GLOBALS['phpgw']->db;
51       $this->db2        = $this->db;
52       $this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
53       $this->t          = $GLOBALS['phpgw']->template;
54       $this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
55       $this->grants     = $GLOBALS['phpgw']->acl->get_grants('eq');
56       $this->grants[$this->account] = PHPGW_ACL_READ + PHPGW_ACL_ADD + PHPGW_ACL_EDIT + PHPGW_ACL_DELETE;
57        
58       $this->jscal = CreateObject('phpgwapi.jscalendar');   // before phpgw_header() !!!
59       $this->cal_options = 'daFormat    : "%Y-%m-%d",
60                                 ifFormat    : "%Y-%m-%d",
61                                 mondayFirst : false,
62                                 weekNumbers : false';
63        
64       $GLOBALS['phpgw_info']['flags']['app_header'] = 'Elders Quorum Tools';
65       $GLOBALS['phpgw']->common->phpgw_header();
66       echo parse_navbar();
67       $this->display_app_header();      
68     }
69   
70   function save_sessiondata()
71     {
72       
73     }
74
75   function display_app_header()
76     {
77       $this->t->set_file(array('eq_header' => 'header.tpl'));
78       
79       if (isset($phpgw_info['user']['preferences']['eq']['eq_font']))
80         {
81           $font = $phpgw_info['user']['preferences']['eq']['eq_font'];
82         }
83       else
84         {
85           $font = 'Arial';
86         }
87       
88       $this->t->set_var('bg_color',$phpgw_info['theme']['th_bg']);
89       $this->t->set_var('font',$font);
90       $link_data['menuaction'] = 'eq.eq.ht_view';
91       $this->t->set_var('link_hometeaching',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
92       $this->t->set_var('lang_hometeaching','HomeTeaching');
93       $link_data['menuaction'] = 'eq.eq.act_list';
94       $this->t->set_var('link_activity',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
95       $this->t->set_var('lang_activity','Activities');
96       $link_data['menuaction'] = 'eq.eq.par_view';
97       $this->t->set_var('link_participation',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
98       $this->t->set_var('lang_participation','Participation');
99       $link_data['menuaction'] = 'eq.eq.ppi_view';
100       $this->t->set_var('link_ppi',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
101       $this->t->set_var('lang_ppi','PPIs');
102       $link_data['menuaction'] = 'eq.eq.vis_view';
103       $this->t->set_var('link_visit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
104       $this->t->set_var('lang_visit','Visits');
105       $link_data['menuaction'] = 'eq.eq.att_view';      
106       $this->t->set_var('link_attendance',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
107       $this->t->set_var('lang_attendance','Attendance');
108                 
109       $this->t->pparse('out','eq_header');
110     }
111
112   function ht_view()
113     {
114       $this->t->set_file(array('ht_view_t' => 'ht_view.tpl'));
115       $this->t->set_block('ht_view_t','district_list','list');
116    
117       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
118       $num_months = get_var('num_months',array('GET','POST'));
119       if($num_months == '') { $num_months = $this->default_ht_num_months; }
120       $this->t->set_var('num_months',$num_months);
121       $this->t->set_var('lang_filter','Filter');
122       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
123       else {  $this->t->set_var('lang_num_months','Months of History'); }
124       
125       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
126       $this->t->set_var('title','Hometeaching'); 
127
128       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
129       $this->db->query($sql,__LINE__,__FILE__);
130       $i=0;
131       while ($this->db->next_record())
132         {
133           $districts[$i]['district'] = $this->db->f('district');
134           $districts[$i]['name'] = $this->db->f('name');
135           $districts[$i]['supervisor'] = $this->db->f('supervisor');
136           $i++;
137         }
138
139       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
140       $this->db->query($sql,__LINE__,__FILE__);
141       $i=0;
142       while ($this->db->next_record())
143         {
144           $elder_id[$i] = $this->db->f('elder');
145           $elder_name[$i] = $this->db->f('name');
146           $i++;
147         }
148       array_multisort($elder_name, $elder_id);
149
150       // Make an array mapping elder_ids to elder_names
151       for($i=0; $i < count($elder_id); $i++) {
152           $id = $elder_id[$i];
153           $elders[$id] = $elder_name[$i];
154       }      
155
156       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
157       $this->db->query($sql,__LINE__,__FILE__);
158       while ($this->db->next_record())
159         {
160           $aaronic_id = $this->db->f('aaronic');
161           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
162         }
163       
164       $total_families = 0;
165       $this->nextmatchs->template_alternate_row_color(&$this->t);
166       for ($i=0; $i < count($districts); $i++) {
167         $this->t->set_var('district_number',$districts[$i]['district']);
168         $this->t->set_var('district_name',$districts[$i]['name']);      
169         $supervisor = $districts[$i]['supervisor'];
170         $unique_companionships='';
171                 
172         // Select all the unique companionship numbers for this district
173         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
174         $this->db->query($sql,__LINE__,__FILE__);
175         $j=0;
176         while ($this->db->next_record())
177           {
178             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
179             $j++;
180           }
181
182         $comp_width=450; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
183         $table_data=""; $num_companionships = 0; $num_families = 0;
184         for($m=$num_months; $m >= 0; $m--) { $visits[$m] = 0; }
185         for ($j=0; $j < count($unique_companionships); $j++) {
186           $companion_table_entry = "";
187           // Select all the companions in each companionship
188           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
189                  "companionship=". $unique_companionships[$j]['companionship'];
190           $this->db->query($sql,__LINE__,__FILE__);
191
192           while ($this->db->next_record())
193             {
194               // Get this companions information
195               if($companion_table_entry != "") { $companion_table_entry .= "&nbsp;/&nbsp;"; }
196               $companionship = $this->db->f('companionship');
197               $elder_id = $this->db->f('elder');
198               $aaronic_id = $this->db->f('aaronic');
199               if($elder_id) { $name = $elders[$elder_id]; }
200               else if($aaronic_id) { $name = $aaronic[$aaronic_id]['name']; }
201               $companion_table_entry .= "$name";
202             }
203           $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><b>$companion_table_entry<hr></td></tr>";
204           
205           // Get the names of the families assigned this home teaching companionship
206           $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
207           $this->db->query($sql,__LINE__,__FILE__);
208           $k=0;
209           while ($this->db->next_record())
210             {
211               $num_families++; $total_families++;
212               $family_name = $this->db->f('name');
213               $family_id = $this->db->f('family');
214               $this->nextmatchs->template_alternate_row_color(&$this->t);
215               $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
216               // Find out how many times Visits were performed by this companionship
217               // in the past $num_months for this Family
218               $header_row="<th width=$comp_width><font size=-2>Families</th>";
219               for($m=$num_months; $m >= 0; $m--) {
220                 if(`date '+%d'` == 31) { $d = 1; } else { $d = 0; }
221                 $month = date('m/Y', strtotime('-'.$m.' month -'.$d.' day'));
222                 $month_start = date('Y-m', strtotime('-'.$m.' month -'.$d.' day')); $month_start .= "-01";
223                 $month_end = date('Y-m', strtotime('-'.$m.' month -'.$d.' day')); $month_end .= "-31";
224                 $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
225                        " AND companionship=" . $unique_companionships[$j]['companionship'].
226                        " AND family=". $family_id;
227                 $this->db2->query($sql,__LINE__,__FILE__);
228                 $link_data['menuaction'] = 'eq.eq.ht_update';
229                 $link_data['date'] = $month_start;
230                 $link_data['month_start'] = $month_start;
231                 $link_data['month_end'] = $month_end;
232                 $link_data['month'] = $month;
233                 $link_data['district'] = $districts[$i]['district'];
234                 $link_data['district_name'] = $districts[$i]['name'];
235                 $link_data['action'] = 'view';
236                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
237                 $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
238                 if(!$total_visits[$m]) { $total_visits[$m] = 0; }
239                 if($this->db2->next_record()) {
240                   if($this->db2->f('visited') == 'y') {
241                     $visits[$m]++; $total_visits[$m]++;           
242                     $table_data .= '<td align=center><a href="'.$link.'"><img src="checkmark.gif"></a></td>';
243                   }
244                   else if($this->db2->f('visited') == 'n') {
245                     $table_data .= '<td align=center><a href="'.$link.'"><img src="x.gif"></a></td>';
246                   }
247                   else { $table_data .= "<td>&nbsp;</td>"; }
248                 }
249                 else { $table_data .= "<td>&nbsp;</td>"; }
250               }
251               $table_data .= "</tr>"; 
252               $k++;
253             }
254           $table_data .= "<tr><td colspan=20></td></tr>";
255         }
256         $table_data .= "<tr><td colspan=20><hr></td></tr>";
257         $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
258
259         for($m=$num_months; $m >=0; $m--) {
260           $percent = ceil(($visits[$m] / $num_families)*100);
261           $stat_data .= "<td align=center><font size=-2><b>$visits[$m]<br>$percent%</font></b></td>";
262         }
263         $stat_data .= "</tr>";
264
265         $this->t->set_var('table_width',$table_width);
266         $this->t->set_var('header_row',$header_row);
267         $this->t->set_var('table_data',$table_data);
268         $this->t->set_var('stat_data',$stat_data);
269         $this->t->fp('list','district_list',True);
270       }
271
272       $totals = "<tr><td><b><font size=-2>$total_families Total Families<br>Visit Totals:</font></b></td>";
273       for($m=$num_months; $m >=0; $m--) {
274         $percent = ceil(($total_visits[$m] / $total_families)*100);
275         $totals .= "<td align=center><font size=-2><b>$total_visits[$m]<br>$percent%</font></b></td>";
276       }
277       $totals .= "</tr>";
278       
279       $this->t->set_var('totals',$totals);
280       
281       $this->t->pfp('out','ht_view_t');
282       $this->save_sessiondata();
283     }
284       
285
286   function ht_update()
287     {
288       $this->t->set_file(array('ht_update_t' => 'ht_update.tpl'));
289       $this->t->set_block('ht_update_t','district_list','list');
290       $this->t->set_block('ht_update_t','save','savehandle');
291       
292       $district = get_var('district',array('GET','POST'));
293       $district_name = get_var('district_name',array('GET','POST'));
294       $date = get_var('date',array('GET','POST'));
295       $month = get_var('month',array('GET','POST'));
296       $month_start = get_var('month_start',array('GET','POST'));
297       $month_end = get_var('month_end',array('GET','POST'));
298       $action = get_var('action',array('GET','POST'));
299       
300       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
301       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_update&action=save'));
302       $this->t->set_var('lang_done','Cancel');
303       $this->t->set_var('district_name',$district_name);
304       $this->t->set_var('district_number',$district);
305       $this->t->set_var('title','Hometeaching Update ' . $month);
306       $this->t->set_var('date',$date);
307       
308       if($action == 'save')
309         {
310           // Get a list of all the companionships in this district
311           $sql = "SELECT distinct companionship FROM eq_companionship where district=". $district;
312           $this->db->query($sql,__LINE__,__FILE__);
313           $j=0;
314           while ($this->db->next_record())
315             {
316               $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
317               $j++;
318             }
319           for ($j=0; $j < count($unique_companionships); $j++)
320             {
321               // Delete all the visits that have taken place for all families for this month
322               $this->db->query("DELETE from eq_visit where companionship=" . $unique_companionships[$j]['companionship'] .
323                                " AND " . "date='" . $date . "'",__LINE__,__FILE__);
324             }
325
326           // Now, add the visits that are checked for this month
327           $new_data = get_var('family_visited',array('POST'));
328           foreach ($new_data as $family)
329            {
330              foreach ($family as $data)
331                {
332                  //print "family_visited: $data <br>";
333                  $data_array = explode("/",$data);
334                  $family_id = $data_array[0];
335                  $companionship = $data_array[1];
336                  $date = $data_array[2];
337                  $visited = $data_array[3];
338                  if($visited == "") { $visited = $data_array[4]; }
339                  //print "family_id: $family_id companionship: $companionship date: $date visited: $visited<br>";
340                  $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes,visited) "
341                                   . "VALUES (" . $family_id .",". $companionship .",'". $date ."','','". $visited ."')",__LINE__,__FILE__);
342                }
343            }
344           $this->ht_view();
345           return false;
346         }
347       
348       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
349       $this->db->query($sql,__LINE__,__FILE__);
350       $i=0;
351       while ($this->db->next_record())
352         {
353           $elder_id[$i] = $this->db->f('elder');
354           $elder_name[$i] = $this->db->f('name');
355           $i++;
356         }
357       array_multisort($elder_name, $elder_id);
358
359       // Make an array mapping elder_ids to elder_names
360       for($i=0; $i < count($elder_id); $i++) {
361           $id = $elder_id[$i];
362           $elders[$id] = $elder_name[$i];
363       }      
364
365       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
366       $this->db->query($sql,__LINE__,__FILE__);
367       while ($this->db->next_record())
368         {
369           $aaronic_id = $this->db->f('aaronic');
370           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
371         }
372       
373       // Select all the unique companionship numbers for this district
374       $sql = "SELECT distinct companionship FROM eq_companionship where district=". $district;
375       $this->db->query($sql,__LINE__,__FILE__);
376       $j=0; $unique_companionships='';
377       while ($this->db->next_record())
378         {
379           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
380           $j++;
381         }
382       
383       $comp_width=300; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
384       $table_data=""; $num_companionships = 0; $num_families = 0; $visits=0;
385       for ($j=0; $j < count($unique_companionships); $j++) {
386         $companion_table_entry = "";
387         // Select all the companions in each companionship
388         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
389                "companionship=". $unique_companionships[$j]['companionship'];
390         $this->db->query($sql,__LINE__,__FILE__);
391         
392         while ($this->db->next_record())
393           {
394             // Get this companions information
395             if($companion_table_entry != "") { $companion_table_entry .= "&nbsp;/&nbsp;"; }
396             $companionship = $this->db->f('companionship');
397             $elder_id = $this->db->f('elder');
398             $aaronic_id = $this->db->f('aaronic');
399             if($elder_id) { $name = $elders[$elder_id]; }
400             else if($aaronic_id) { $name = $aaronic[$aaronic_id]['name']; }
401             $companion_table_entry .= "$name";
402           }
403         $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><b>$companion_table_entry</b><hr></td></tr>";
404         
405         // Get the names of the families assigned this home teaching companionship
406         $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
407         $this->db->query($sql,__LINE__,__FILE__);
408         while ($this->db->next_record())
409           {
410             $num_families++; $total_families++;
411             $family_name = $this->db->f('name');
412             $family_id = $this->db->f('family');
413             $this->nextmatchs->template_alternate_row_color(&$this->t);
414             $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
415             
416             $header_row="<th width=$comp_width><font size=-2>Families</th>";
417             $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
418                    " AND companionship=" . $unique_companionships[$j]['companionship'] .
419                    " AND family=". $family_id;
420             $this->db2->query($sql,__LINE__,__FILE__);
421             $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
422             $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
423             if(!$total_visits) { $total_visits = 0; }
424             if($this->db2->next_record()) {
425               if($this->db2->f('visited') == 'y') {
426                 $visits++; $total_visits++;
427                 $table_data .= '<td width=100 align=center>';
428                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y" checked>Y';
429                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
430                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/"> ';
431                 $table_data .= '</td>';
432               } else if($this->db2->f('visited') == 'n') {
433                 $table_data .= '<td width=100 align=center>';
434                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
435                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n" checked>N';
436                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/">';
437                 $table_data .= '</td>';
438               } else {
439                 $table_data .= '<td width=100 align=center>';
440                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
441                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
442                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
443                 $table_data .= '</td>';
444               }
445             }
446             else {
447               $value .= "/";
448               $table_data .= '<td width=100 align=center>';
449               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
450               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
451               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
452               $table_data .= '</td>';         
453             }
454           }
455         $table_data .= "</tr>"; 
456         $table_data .= "<tr><td colspan=20></td></tr>";
457       }
458       $table_data .= "<tr><td colspan=20><hr></td></tr>";
459       $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
460       
461       $percent = ceil(($visits / $num_families)*100);
462       $stat_data .= "<td align=center><font size=-2><b>$visits<br>$percent%</font></b></td>";
463       $stat_data .= "</tr>";
464       
465       $this->t->set_var('table_width',$table_width);
466       $this->t->set_var('header_row',$header_row);
467       $this->t->set_var('table_data',$table_data);
468       $this->t->set_var('stat_data',$stat_data);
469       $this->t->fp('list','district_list',True);
470
471       $this->t->set_var('lang_reset','Clear Form');
472       $this->t->set_var('lang_save','Save Changes');
473       $this->t->set_var('savehandle','');
474       
475       $this->t->pfp('out','ht_update_t');
476       $this->t->pfp('addhandle','save');
477       
478       $this->save_sessiondata();
479     }
480
481   function act_list()
482     {
483       $this->t->set_file(array('act_list_t' => 'act_list.tpl'));
484       $this->t->set_block('act_list_t','act_list','list');
485       
486       $this->t->set_var('lang_name','Activity Name');
487       $this->t->set_var('lang_date','Date');
488       
489       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
490       $this->db->query($sql,__LINE__,__FILE__);
491       $total_records = $this->db->num_rows();
492
493       $i = 0;
494       while ($this->db->next_record())
495         {
496           $activity_list[$i]['name'] = $this->db->f('name');
497           $activity_list[$i]['date']  = $this->db->f('date');
498           $activity_list[$i]['activity']  = $this->db->f('activity');
499           $i++;
500         }
501             
502       for ($i=0; $i < count($activity_list); $i++)
503         {         
504           $this->nextmatchs->template_alternate_row_color(&$this->t);
505           $this->t->set_var('name',$activity_list[$i]['name']);
506           $this->t->set_var('date',$activity_list[$i]['date']);
507           
508           $link_data['menuaction'] = 'eq.eq.act_view';
509           $link_data['activity'] = $activity_list[$i]['activity'];
510           $link_data['action'] = 'view';
511           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
512           $this->t->set_var('lang_view','View');
513
514           $link_data['menuaction'] = 'eq.eq.act_update';
515           $link_data['activity'] = $activity_list[$i]['activity'];
516           $link_data['action'] = 'edit';
517           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
518           $this->t->set_var('lang_edit','Edit');
519
520           $link_data['menuaction'] = 'eq.eq.act_update';
521           $link_data['activity'] = '0';
522           $link_data['action'] = 'add';
523           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
524                            . '"><input type="submit" name="Add" value="' . 'Add Activity' .'"></font></form>');
525
526           $this->t->fp('list','act_list',True);
527         }
528
529       $this->t->pfp('out','act_list_t');
530       $this->save_sessiondata();
531     }
532
533   function act_view()
534     {
535       $this->t->set_file(array('act_view_t' => 'act_view.tpl'));
536       $this->t->set_block('act_view_t','part_list','list');
537       
538       $sql = "SELECT * FROM eq_activity WHERE activity=" . intval(get_var('activity',array('GET','POST')));
539       $this->db->query($sql,__LINE__,__FILE__);
540       $this->db->next_record();
541       $this->t->set_var('name', $this->db->f('name'));
542       $this->t->set_var('date', $this->db->f('date'));
543       $this->t->set_var('notes', $this->db->f('notes'));
544             
545       $this->t->set_var('lang_name','Activity Name');
546       $this->t->set_var('lang_date','Date');
547       $this->t->set_var('lang_notes','Notes');
548       $this->t->set_var('lang_done','Done');
549       $this->t->set_var('lang_action','View');
550
551       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
552       $this->t->set_var('tr_color',$tr_color);
553             
554       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
555
556       $link_data['menuaction'] = 'eq.eq.act_update';
557       $link_data['activity'] = get_var('activity',array('GET','POST'));
558       $link_data['action'] = 'edit';
559       $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
560       $this->t->set_var('lang_edit','Edit');
561       $this->t->set_var('cal_date',$this->db->f('date'));
562
563       // Now find out which elders participated in this activity
564       $sql = "SELECT * FROM eq_participation WHERE activity=" . intval(get_var('activity',array('GET','POST')));
565       $this->db->query($sql,__LINE__,__FILE__);
566       $total_records = $this->db->num_rows();
567
568       $i = 0;
569       while ($this->db->next_record())
570         {
571           $part_list[$i]['elder']  = $this->db->f('elder');
572           $i++;
573         }
574       
575       for ($i=0; $i < count($part_list); $i++)
576         {
577           $sql = "SELECT * FROM eq_elder WHERE elder=" . $part_list[$i]['elder'];
578           $this->db->query($sql,__LINE__,__FILE__);
579           $this->db->next_record();
580           $names[$i] = $this->db->f('name');
581         } sort($names);
582       
583       for ($i=0; $i < count($names); $i++)
584         {
585           //$this->nextmatchs->template_alternate_row_color(&$this->t);
586           $this->t->set_var('elder_name',$names[$i]);
587           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
588           else { $this->t->set_var('table_sep',"</td>"); }
589           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
590           $this->t->fp('list','part_list',True);
591         }
592       
593       $this->t->pfp('out','act_view_t');
594       $this->save_sessiondata();
595     }
596
597   function act_update()
598     {
599       $this->t->set_file(array('form' => 'act_update.tpl'));
600       $this->t->set_block('form','elder_list','list');
601       $this->t->set_block('form','add','addhandle');
602       $this->t->set_block('form','edit','edithandle');
603       $this->t->set_var('lang_done','Done');
604
605       $action = get_var('action',array('GET','POST'));
606       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
607       $activity['activity'] = intval(get_var('activity',array('GET','POST')));
608
609       if($action == 'save')
610         {
611           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
612           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
613           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
614           $this->db->query("UPDATE eq_activity set " .
615                            "   name='" . $activity['name'] .
616                            "', date='" . $activity['date'] . "'" .
617                            ", notes='" . $activity['notes'] . "'" .
618                            " WHERE activity=" . $activity['activity'],__LINE__,__FILE__);
619
620           // Delete all the elders who have particiapted in this activity
621           $this->db->query("DELETE from eq_participation where activity=".$activity['activity'],__LINE__,__FILE__);
622           
623           // Re-add the elders who are checked as having participated in this activity
624           $elders = get_var('elder_name',array('POST'));
625           foreach ($elders as $elder)
626             {
627               $this->db->query("INSERT INTO eq_participation (elder,activity) "
628                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
629             }
630
631           $this->act_list();
632           return false;
633         }
634
635       if($action == 'insert')
636         {
637           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
638           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
639           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
640           $this->db->query("INSERT INTO eq_activity (name,date,notes) "
641                            . "VALUES ('" . $activity['name'] . "','"
642                            . $activity['date'] . "','" . $activity['notes'] . "')",__LINE__,__FILE__);
643           $this->act_list();
644           return false;
645         }
646       
647       if($action == 'add')
648         {
649           $activity['activity'] = 0;
650           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
651           $this->t->set_var('name','');
652           $this->t->set_var('date','');
653           $this->t->set_var('notes','');
654           $this->t->set_var('lang_done','Cancel');
655           $this->t->set_var('lang_action','Adding New Activity');
656           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
657                                                                 . $activity['activity'] . '&action=' . 'insert'));
658         }
659
660       if($action == 'edit')
661         {
662           $sql = "SELECT * FROM eq_activity WHERE activity=" . $activity['activity'];
663           $this->db->query($sql,__LINE__,__FILE__);
664           $this->db->next_record();
665           $this->t->set_var('cal_date',$this->jscal->input('date',$this->db->f('date'),'','','','','',$this->cal_options));
666           $this->t->set_var('name', $this->db->f('name'));
667           $this->t->set_var('date', $this->db->f('date'));
668           $this->t->set_var('notes', $this->db->f('notes'));
669           $this->t->set_var('lang_done','Cancel');
670           $this->t->set_var('lang_action','Editing Activity');
671           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
672                                                                 . $activity['activity'] . '&action=' . 'save'));
673
674         }
675
676       $sql = "SELECT * FROM eq_elder";
677       $this->db->query($sql,__LINE__,__FILE__);
678       $i=0;
679       while ($this->db->next_record())
680         {
681           if($this->db->f('valid') == 1 || $action != 'add') {
682             $elder_name[$i] = $this->db->f('name');
683             $elder_id[$i] = $this->db->f('elder');
684             $elder_valid[$i] = $this->db->f('valid');
685             $i++;
686           }
687         }
688       array_multisort($elder_name, $elder_id, $elder_valid);
689
690       $j=0;
691       for ($i=0; $i < count($elder_id); $i++)
692         {
693           //$this->nextmatchs->template_alternate_row_color(&$this->t);
694           $sql = "SELECT * FROM eq_participation where activity=". $activity['activity'] . " AND elder=" . $elder_id[$i];
695           $this->db->query($sql,__LINE__,__FILE__);
696           if($this->db->next_record()) { $this->t->set_var('checked','checked'); $checked=1; }
697           else { $this->t->set_var('checked',''); $checked=0; }
698           if($checked || $elder_valid[$i] == 1) {
699             $this->t->set_var('elder_name',$elder_name[$i]);
700             $this->t->set_var('elder',$elder_id[$i]);
701             if(($j+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
702             else { $this->t->set_var('table_sep',"</td>"); }
703             if(($j) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
704             $this->t->fp('list','elder_list',True);
705             $j++;
706           }
707         }
708       
709       $this->t->set_var('lang_reset','Clear Form');
710       $this->t->set_var('lang_add','Add Activity');
711       $this->t->set_var('lang_save','Save Changes');
712       $this->t->set_var('edithandle','');
713       $this->t->set_var('addhandle','');
714       
715       $this->t->pfp('out','form');
716       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
717       if($action == 'add') { $this->t->pfp('addhandle','add'); }
718       
719       $this->save_sessiondata();
720     }
721
722   function par_view()
723     {
724       $this->t->set_file(array('par_view_t' => 'par_view.tpl'));
725       $this->t->set_block('par_view_t','header_list','list1');
726       $this->t->set_block('par_view_t','elder_list','list2');
727
728       $sql = "SELECT * FROM eq_elder where valid=1";
729       $this->db->query($sql,__LINE__,__FILE__);
730       $i=0;
731       while ($this->db->next_record())
732         {
733           $elder_name[$i] = $this->db->f('name');
734           $elder_id[$i] = $this->db->f('elder');
735           $i++;
736         }
737       array_multisort($elder_name, $elder_id);
738
739       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
740       $this->db->query($sql,__LINE__,__FILE__);
741       $total_records = $this->db->num_rows();
742
743       $i = 0;
744       while ($this->db->next_record())
745         {
746           $activity_list[$i]['name'] = $this->db->f('name');
747           $activity_list[$i]['date'] = $this->db->f('date');
748           $activity_list[$i]['activity']  = $this->db->f('activity');
749           if($activity_list[$i]['date'] == '0000-00-00') { $activity_list[$i]['date']=""; }
750           $i++;
751         }
752
753       $elder_width=250; $part_width=25; $act_width=50;
754       $total_width=$elder_width+$part_width;
755       for ($i=0; $i < count($activity_list); $i++) {
756         $this->t->set_var('activity_name',$activity_list[$i]['name']);
757         $this->t->set_var('activity_date',$activity_list[$i]['date']);
758         $this->t->fp('list1','header_list',True);
759         $total_width += $act_width;
760       }
761
762       for ($i=0; $i < count($elder_id); $i++) {
763         $participated=0; $part_table = ''; 
764         $this->nextmatchs->template_alternate_row_color(&$this->t);
765         $this->t->set_var('elder_name',$elder_name[$i]);
766         for ($j=0; $j < count($activity_list); $j++) {
767           $sql = "SELECT * FROM eq_participation where activity="
768              . $activity_list[$j]['activity'] . " AND elder=" . $elder_id[$i];
769           $this->db->query($sql,__LINE__,__FILE__);
770           if($this->db->next_record()) {
771             $part_table .= '<td align=center><img src="checkmark.gif"></td>';
772             $participated++;
773           } else {
774             $part_table .= '<td>&nbsp;</td>';
775           }
776         }
777         if($participated) { $part_table .= '<td align=center><img src="checkmark.gif">'.$participated.'</td>'; }
778         else { $part_table .= '<td>&nbsp;</td>'; }
779         $this->t->set_var('part_table',$part_table);
780         $this->t->fp('list2','elder_list',True);
781       }
782       $this->t->set_var('total_width',$total_width);
783       $this->t->set_var('elder_width',$elder_width);
784       $this->t->set_var('part_width',$part_width);
785       $this->t->set_var('act_width',$act_width);
786       $this->t->pfp('out','par_view_t');
787       $this->save_sessiondata(); 
788     }
789
790   function ppi_view()
791     {
792       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
793       $this->t->set_block('ppi_view_t','district_list','list');
794
795       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
796       $num_months = get_var('num_months',array('GET','POST'));
797       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
798       $this->t->set_var('num_months',$num_months);
799       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
800       else {  $this->t->set_var('lang_num_months','Months of History'); }
801       $this->t->set_var('lang_filter','Filter');
802       
803       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
804       $this->t->set_var('eqpres_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view&eqpresppi=1'));
805       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
806       
807       if($eqpresppi == 1) {
808         $num_months = get_var('num_months',array('GET','POST'));
809         if($num_months == '') { $num_months = $this->default_ppi_num_years; }
810         $this->t->set_var('num_months',$num_months);
811         $this->t->set_var('ppi_link_title','PPIs');
812         $this->t->set_var('title','EQ President Yearly PPIs');
813         if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
814         else { $this->t->set_var('lang_num_months','Years of History'); }
815         $this->t->set_var('eqpres_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
816       }
817       else { 
818         $this->t->set_var('ppi_link_title','EQ President Yearly PPIs'); 
819         $this->t->set_var('title','PPIs'); 
820       }
821
822       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
823       $this->db->query($sql,__LINE__,__FILE__);
824       $i=0;
825       while ($this->db->next_record())
826         {
827           $districts[$i]['district'] = $this->db->f('district');
828           $districts[$i]['name'] = $this->db->f('name');
829           $districts[$i]['supervisor'] = $this->db->f('supervisor');
830           if($eqpresppi == 0) { $i++; }
831         }
832
833       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
834       $this->db->query($sql,__LINE__,__FILE__);
835       $i=0;
836       while ($this->db->next_record())
837         {
838           $elder_id[$i] = $this->db->f('elder');
839           $elder_name[$i] = $this->db->f('name');
840           $i++;
841         }
842       array_multisort($elder_name, $elder_id);
843       for($i=0; $i < count($elder_id); $i++) {
844           $id = $elder_id[$i];
845           $elders[$id] = $elder_name[$i];
846       }      
847
848       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
849       $this->db->query($sql,__LINE__,__FILE__);
850       while ($this->db->next_record())
851         {
852           $aaronic_id = $this->db->f('aaronic');
853           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
854         }
855       
856       $total_companionships = 0;
857       $this->nextmatchs->template_alternate_row_color(&$this->t);
858       for ($i=0; $i < count($districts); $i++) {
859         if($eqpresppi == 1) { 
860           $this->t->set_var('district_number','*');
861           $this->t->set_var('district_name','EQ President');
862         } else {
863           $this->t->set_var('district_number',$districts[$i]['district']);
864           $this->t->set_var('district_name',$districts[$i]['name']);    
865         }
866         $supervisor = $districts[$i]['supervisor'];
867         $unique_companionships='';
868                 
869         // Select all the unique companionship numbers for this district
870         if($eqpresppi == 1) { 
871           $sql = "SELECT distinct companionship FROM eq_companionship";
872         } 
873         else {
874           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
875         }
876         $this->db->query($sql,__LINE__,__FILE__);
877         $j=0;
878         while ($this->db->next_record())
879           {
880             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
881             $j++;
882           }
883         
884         $comp_width=400; $ppi_width=25; $table_width=$comp_width + $num_months*$ppi_width;
885         $table_data=""; $num_companionships = $j; $num_elders = 0;
886         for($m=$num_months; $m >= 0; $m--) { $ppis[$m] = 0; }
887         for ($j=0; $j < count($unique_companionships); $j++) {
888           // Select all the companions in each companionship
889           if($eqpresppi) {
890             $sql = "SELECT * FROM eq_companionship where valid=1 and aaronic=0 and ".
891                    "companionship=". $unique_companionships[$j]['companionship'];  
892           }
893           else {
894             $sql = "SELECT * FROM eq_companionship where valid=1 and ".
895                    "companionship=". $unique_companionships[$j]['companionship'];
896           }
897           $this->db->query($sql,__LINE__,__FILE__);
898           $k=0;
899           while ($this->db->next_record())
900             {
901               // Get this companions information
902               $num_elders++;
903               $companionship = $this->db->f('companionship');
904               $elder_id = $this->db->f('elder');
905               $aaronic_id = $this->db->f('aaronic');
906               if($elder_id) { $name = $elders[$elder_id]; }
907               else if($aaronic_id) { $name = $aaronic[$aaronic_id]['name']; }
908               $link_data['menuaction'] = 'eq.eq.ppi_update';
909               $link_data['companionship'] = $companionship;
910               $link_data['interviewer'] = $supervisor;
911               $link_data['elder'] = $elder_id;
912               $link_data['name'] = $name;
913               $link_data['ppi'] = '';
914               $link_data['eqpresppi'] = $eqpresppi;
915               $link_data['action'] = 'add';
916               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
917               if($aaronic_id == 0) {
918                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td><a href=$link>$name</a></td>";
919               } else {
920                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$name</td>";
921               }
922               
923               // Find out how many times PPIs were performed in the past $num_months for this Elder
924               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
925               for($m=$num_months; $m >= 0; $m--) {
926                 if($eqpresppi == 1) {
927                   $year = date('Y') - $m;
928                   $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
929                   $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
930                          "AND elder=" . $elder_id . " AND eqpresppi=1";
931                   $this->db2->query($sql,__LINE__,__FILE__);
932                   $header_row .= "<th width=150><font size=-2>$year</th>"; 
933                 }
934                 else {
935                   if(`date '+%d'` == 31) { $d = 1; } else { $d = 0; }
936                   $month = date('m/Y', strtotime('-'.$m.' month -'.$d.' day'));
937                   $month_start = date('Y-m', strtotime('-'.$m.' month -'.$d.' day')); $month_start .= "-01";
938                   $month_end = date('Y-m', strtotime('-'.$m.' month -'.$d.' day')); $month_end .= "-31";
939                   $sql = "SELECT * FROM eq_ppi WHERE date >= '$month_start' AND date <= '$month_end' ".
940                          "AND elder=" . $elder_id . " AND eqpresppi=0";
941                   $this->db2->query($sql,__LINE__,__FILE__);
942                   $header_row .= "<th width=$ppi_width><font size=-2>$month</th>";
943                 }
944                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
945                 if($this->db2->next_record()) {
946                   $ppis[$m]++; $total_ppis[$m]++;
947                   $link_data['menuaction'] = 'eq.eq.ppi_update';
948                   $link_data['companionship'] = $companionship;
949                   $link_data['interviewer'] = $supervisor;
950                   $link_data['elder'] = $elder_id;
951                   $link_data['name'] = $name;
952                   $link_data['ppi'] = $this->db2->f('ppi');
953                   $link_data['eqpresppi'] = $eqpresppi;
954                   $link_data['action'] = 'view';
955                   $date = $this->db2->f('date');
956                   $date_array = explode("-",$date);
957                   $month = $date_array[1];
958                   $day   = $date_array[2];
959                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
960
961                   if($aaronic_id == 0) {
962                     $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$month.'-'.$day.'</a></td>';
963                   } else {
964                     $table_data .= '<td align=center><img src="checkmark.gif"></td>';
965                   }
966                 }
967                 else { $table_data .= "<td>&nbsp;</td>"; }
968               }
969               $table_data .= "</tr>"; 
970               $k++;
971             }
972           $table_data .= "<tr><td colspan=20><hr></td></tr>";
973         }
974         // Now add Elders not assigned to any companionship to the table if we are in eqpresppi mode
975         if($eqpresppi == 1) {
976           $table_data .= "<tr bgcolor=\"#c9c9c9\"><hr><td colspan=20><b>Unassigned Potential Home Teachers</b><hr></td></tr>";
977           foreach($elders as $elder_id => $value) {
978             $sql = "SELECT * FROM eq_companionship where valid=1 and elder=".$elder_id;
979             $this->db->query($sql,__LINE__,__FILE__);
980             if(!$this->db->next_record()) {
981               // We found an Elder not in a companionship, add them to the table
982               $num_elders++;
983               $companionship=0;
984               $name = $elders[$elder_id];
985               $link_data['menuaction'] = 'eq.eq.ppi_update';
986               $link_data['companionship'] = $companionship;
987               $link_data['interviewer'] = $supervisor;
988               $link_data['elder'] = $elder_id;
989               $link_data['name'] = $name;
990               $link_data['ppi'] = '';
991               $link_data['eqpresppi'] = $eqpresppi;
992               $link_data['action'] = 'add';
993               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
994               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td><a href=$link>$name</a></td>";
995               for($m=$num_months; $m >= 0; $m--) {
996                 $year = date('Y') - $m;
997                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
998                 $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
999                        "AND elder=" . $elder_id . " AND eqpresppi=1";
1000                 $this->db2->query($sql,__LINE__,__FILE__);
1001                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1002                 if($this->db2->next_record()) {
1003                   $ppis[$m]++; $total_ppis[$m]++;
1004                   $link_data['menuaction'] = 'eq.eq.ppi_update';
1005                   $link_data['companionship'] = $companionship;
1006                   $link_data['interviewer'] = $supervisor;
1007                   $link_data['elder'] = $elder_id;
1008                   $link_data['name'] = $name;
1009                   $link_data['ppi'] = $this->db2->f('ppi');
1010                   $link_data['eqpresppi'] = $eqpresppi;
1011                   $link_data['action'] = 'view';
1012                   $date = $this->db2->f('date');
1013                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1014                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$date.'</a></td>';
1015                 }
1016                 else { $table_data .= "<td>&nbsp;</td>"; }
1017               }
1018               $table_data .= "</tr>"; 
1019             }
1020           }
1021         }
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 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 ?>