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