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