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