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