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