Added new Last PPI column
[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             $elders_with_yearly_ppi++;
965             $date = $this->db2->f('date');
966             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
967             $completed_data.= "<td align=center>$phone</td>";
968             $completed_data.= "<td align=center>$date</td>";
969             $completed_data.= "<td align=center>$ppi_notes</td>";
970             $completed_data.= '</tr>';
971             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
972             $this->t->set_var('tr_color2',$tr_color2);
973           }
974       }
975
976       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
977       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
978       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
979       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
980       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
981       $completed_header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
982       
983       $elders_width=300; $totals_width=100;
984       $totals_table_width=$elders_width + $totals_width;
985       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
986       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
987       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
988       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
989       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
990       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
991       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
992       $this->t->set_var('tr_color',$tr_color);
993       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
994       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
995       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
996       $totals_data.= "</tr>";
997       
998       $this->t->set_var('table_width',$table_width);
999       $this->t->set_var('header_row',$header_row);
1000       $this->t->set_var('table_data',$table_data);
1001       $this->t->set_var('totals_header_row',$totals_header_row);
1002       $this->t->set_var('totals_table_width',$totals_table_width);
1003       $this->t->set_var('completed_header_row',$completed_header_row);
1004       $this->t->set_var('completed_table_width',$completed_table_width);
1005       $this->t->set_var('completed',$completed_data);
1006       $this->t->set_var('totals',$totals_data);
1007       $this->t->fp('list','elder_list',True);
1008       
1009       $this->t->pfp('out','ppi_sched_t');
1010       $this->save_sessiondata(); 
1011       
1012     }
1013   
1014   function ppi_view()
1015     {
1016       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
1017       $this->t->set_block('ppi_view_t','district_list','list');
1018
1019       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1020       $num_months = get_var('num_months',array('GET','POST'));
1021       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
1022       $this->t->set_var('num_months',$num_months);
1023       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1024       else {  $this->t->set_var('lang_num_months','Months of History'); }
1025       $this->t->set_var('lang_filter','Filter');
1026       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1027       
1028       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1029       $this->t->set_var('ppi_link_title','Hometeaching PPIs');
1030       
1031       $this->t->set_var('eqpres_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view&eqpresppi=1'));
1032       $this->t->set_var('eqpres_ppi_link_title','EQ President Yearly PPIs'); 
1033       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
1034
1035       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1036       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1037       
1038       if($eqpresppi == 1) {
1039         $this->t->set_var('title','EQ President Yearly PPIs');
1040         $num_months = get_var('num_months',array('GET','POST'));
1041         if($num_months == '') { $num_months = $this->default_ppi_num_years; }
1042         $this->t->set_var('num_months',$num_months);
1043         if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
1044         else { $this->t->set_var('lang_num_months','Years of History'); }       
1045       }
1046       else { 
1047         $this->t->set_var('title','Hometeaching PPIs'); 
1048       }
1049
1050       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1051       $this->db->query($sql,__LINE__,__FILE__);
1052       $i=0;
1053       while ($this->db->next_record())
1054         {
1055           if(($eqpresppi == 1) && ($this->db->f('district') == 1)) {
1056             $districts[$i]['district'] = $this->db->f('district');
1057             $districts[$i]['name'] = $this->db->f('name');
1058             $districts[$i]['supervisor'] = $this->db->f('supervisor');
1059           } else if($eqpresppi == 0) {
1060             $districts[$i]['district'] = $this->db->f('district');
1061             $districts[$i]['name'] = $this->db->f('name');
1062             $districts[$i]['supervisor'] = $this->db->f('supervisor');
1063             $i++;
1064           }
1065         }
1066
1067       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
1068       $this->db->query($sql,__LINE__,__FILE__);
1069       $i=0;
1070       while ($this->db->next_record())
1071         {
1072           $elder_id[$i] = $this->db->f('elder');
1073           $elder_name[$i] = $this->db->f('name');
1074           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1075           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1076           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1077           $i++;
1078         }
1079       array_multisort($elder_name, $elder_id);
1080       for($i=0; $i < count($elder_id); $i++) {
1081           $id = $elder_id[$i];
1082           $elders[$id] = $elder_name[$i];
1083       }      
1084
1085       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
1086       $this->db->query($sql,__LINE__,__FILE__);
1087       while ($this->db->next_record())
1088         {
1089           $aaronic_id = $this->db->f('aaronic');
1090           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
1091           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
1092         }
1093       
1094       $total_companionships = 0;
1095       $this->nextmatchs->template_alternate_row_color(&$this->t);
1096       for ($i=0; $i < count($districts); $i++) {
1097         if($eqpresppi == 1) { 
1098           $this->t->set_var('district_number','*');
1099           $this->t->set_var('district_name','EQ President');
1100         } else {
1101           $this->t->set_var('district_number',$districts[$i]['district']);
1102           $this->t->set_var('district_name',$districts[$i]['name']);    
1103         }
1104         $supervisor = $districts[$i]['supervisor'];
1105         $unique_companionships='';
1106                 
1107         // Select all the unique companionship numbers for this district
1108         if($eqpresppi == 1) { 
1109           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1";
1110         } 
1111         else {
1112           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
1113         }
1114         $this->db->query($sql,__LINE__,__FILE__);
1115         $j=0;
1116         while ($this->db->next_record())
1117           {
1118             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1119             $j++;
1120           }
1121         
1122         $comp_width=400; $ppi_width=25; $table_width=$comp_width + $num_months*$ppi_width;
1123         $table_data=""; $num_companionships = $j; $num_elders = 0;
1124         for($m=$num_months; $m >= 0; $m--) { $ppis[$m] = 0; }
1125         for ($j=0; $j < count($unique_companionships); $j++) {
1126           // Select all the companions in each companionship
1127           if($eqpresppi) {
1128             $sql = "SELECT * FROM eq_companionship where valid=1 and aaronic=0 and ".
1129                    "companionship=". $unique_companionships[$j]['companionship'];  
1130           }
1131           else {
1132             $sql = "SELECT * FROM eq_companionship where valid=1 and ".
1133                    "companionship=". $unique_companionships[$j]['companionship'];
1134           }
1135           $this->db->query($sql,__LINE__,__FILE__);
1136           $k=0;
1137           $comp = $unique_companionships[$j]['companionship'];
1138           for($m=$num_months; $m >= 0; $m--) { $ppi_recorded[$comp][$m] = 0; }
1139           while ($this->db->next_record())
1140             {
1141               // Get this companions information
1142               $num_elders++;
1143               $companionship = $this->db->f('companionship');
1144               $elder_id = $this->db->f('elder');
1145               $aaronic_id = $this->db->f('aaronic');
1146               if($elder_id) {
1147                 $name = $elders[$elder_id];
1148                 $phone = $elder_phone[$elder_id];
1149               }
1150               else if($aaronic_id) {
1151                 $name = $aaronic[$aaronic_id]['name'];
1152                 $phone = $aaronic[$aaronic_id]['phone'];
1153               }
1154               $link_data['menuaction'] = 'eq.eq.ppi_update';
1155               $link_data['companionship'] = $companionship;
1156               $link_data['interviewer'] = $supervisor;
1157               $link_data['elder'] = $elder_id;
1158               $link_data['aaronic'] = $aaronic_id;
1159               $link_data['name'] = $name;
1160               $link_data['ppi'] = '';
1161               $link_data['eqpresppi'] = $eqpresppi;
1162               $link_data['action'] = 'add';
1163               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1164               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1165
1166               // Find out how many times PPIs were performed in the past $num_months for this Elder
1167               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
1168               for($m=$num_months; $m >= 0; $m--) {
1169                 if($eqpresppi == 1) {
1170                   $year = date('Y') - $m;
1171                   $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1172                   $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1173                          "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id . " AND eqpresppi=1";
1174                   $this->db2->query($sql,__LINE__,__FILE__);
1175                   $header_row .= "<th width=150><font size=-2>$year</th>"; 
1176                 }
1177                 else {
1178                   $month = $this->current_month - $m;
1179                   $year = $this->current_year;
1180                   if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
1181                   if($month < 10) { $month = "0"."$month"; }
1182                   $month_start = "$year"."-"."$month"."-"."01";
1183                   $month_end = "$year"."-"."$month"."-"."31";
1184                   $month = "$month"."/"."$year";
1185                   $sql = "SELECT * FROM eq_ppi WHERE date >= '$month_start' AND date <= '$month_end' ".
1186                          "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id . " AND eqpresppi=0";
1187                   $this->db2->query($sql,__LINE__,__FILE__);
1188                   $header_row .= "<th width=$ppi_width><font size=-2>$month</th>";
1189                 }
1190                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1191                 if($this->db2->next_record()) {
1192                   if(!$ppi_recorded[$companionship][$m]) {
1193                     $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
1194                   }
1195                   $link_data['menuaction'] = 'eq.eq.ppi_update';
1196                   $link_data['companionship'] = $companionship;
1197                   $link_data['interviewer'] = $this->db2->f('interviewer');
1198                   $link_data['elder'] = $elder_id;
1199                   $link_data['aaronic'] = $aaronic_id;
1200                   $link_data['name'] = $name;
1201                   $link_data['ppi'] = $this->db2->f('ppi');
1202                   $link_data['eqpresppi'] = $eqpresppi;
1203                   $link_data['action'] = 'view';
1204                   $date = $this->db2->f('date');
1205                   $date_array = explode("-",$date);
1206                   $month = $date_array[1];
1207                   $day   = $date_array[2];
1208                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1209                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$month.'-'.$day.'</a></td>';
1210                 }
1211                 else { $table_data .= "<td>&nbsp;</td>"; }
1212               }
1213               $table_data .= "</tr>"; 
1214               $k++;
1215             }
1216           $table_data .= "<tr><td colspan=20><hr></td></tr>";
1217         }
1218         // Now add Elders not assigned to any companionship to the table if we are in eqpresppi mode
1219         if($eqpresppi == 1) {
1220           $table_data .= "<tr bgcolor=\"#c9c9c9\"><td colspan=20><b>Unassigned Potential Home Teachers</b><hr></td></tr>";
1221           foreach($elders as $elder_id => $value) {
1222             $sql = "SELECT * FROM eq_companionship where valid=1 and elder=".$elder_id;
1223             $this->db->query($sql,__LINE__,__FILE__);
1224             if(!$this->db->next_record()) {
1225               // We found an Elder not in a companionship, add them to the table
1226               $num_elders++;
1227               $companionship=0;
1228               $name = $elders[$elder_id];
1229               $link_data['menuaction'] = 'eq.eq.ppi_update';
1230               $link_data['companionship'] = $companionship;
1231               $link_data['interviewer'] = $supervisor;
1232               $link_data['elder'] = $elder_id;
1233               $link_data['name'] = $name;
1234               $link_data['ppi'] = '';
1235               $link_data['eqpresppi'] = $eqpresppi;
1236               $link_data['action'] = 'add';
1237               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1238               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td><a href=$link>$name</a></td>";
1239               for($m=$num_months; $m >= 0; $m--) {
1240                 $year = date('Y') - $m;
1241                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1242                 $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1243                        "AND elder=" . $elder_id . " AND eqpresppi=1";
1244                 $this->db2->query($sql,__LINE__,__FILE__);
1245                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1246                 if($this->db2->next_record()) {
1247                   $ppis[$m]++; $total_ppis[$m]++;
1248                   $link_data['menuaction'] = 'eq.eq.ppi_update';
1249                   $link_data['companionship'] = $companionship;
1250                   $link_data['interviewer'] = $supervisor;
1251                   $link_data['elder'] = $elder_id;
1252                   $link_data['name'] = $name;
1253                   $link_data['ppi'] = $this->db2->f('ppi');
1254                   $link_data['eqpresppi'] = $eqpresppi;
1255                   $link_data['action'] = 'view';
1256                   $date = $this->db2->f('date');
1257                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1258                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif"><br>'.$date.'</a></td>';
1259                 }
1260                 else { $table_data .= "<td>&nbsp;</td>"; }
1261               }
1262               $table_data .= "</tr>"; 
1263             }
1264           }
1265         }
1266         $total_companionships += $num_companionships;
1267         if($eqpresppi == 1) {
1268           $stat_data = "<tr><td><b><font size=-2>$num_elders Elders<br>PPI Totals:</font></b></td>";
1269         } else {
1270           $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>PPI Totals:</font></b></td>";
1271         }
1272         for($m=$num_months; $m >=0; $m--) {
1273           if($eqpresppi == 1) { $percent = ceil(($ppis[$m] / $num_elders)*100); }
1274           else { $percent = ceil(($ppis[$m] / $num_companionships)*100); }
1275           $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
1276         }
1277         $stat_data .= "</tr>";
1278
1279         $this->t->set_var('table_width',$table_width);
1280         $this->t->set_var('header_row',$header_row);
1281         $this->t->set_var('table_data',$table_data);
1282         $this->t->set_var('stat_data',$stat_data);
1283         $this->t->fp('list','district_list',True);
1284       }
1285
1286       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>PPI Totals:</font></b></td>";
1287       for($m=$num_months; $m >=0; $m--) {
1288         $percent = ceil(($total_ppis[$m] / $total_companionships)*100);
1289         $totals .= "<td align=center><font size=-2><b>$total_ppis[$m]<br>$percent%</font></b></td>";
1290       }
1291       $totals .= "</tr>";
1292       
1293       $this->t->set_var('totals',$totals);
1294       $this->t->pfp('out','ppi_view_t');
1295       $this->save_sessiondata(); 
1296     }
1297
1298   function ppi_update()
1299     {
1300       $this->t->set_file(array('form' => 'ppi_update.tpl'));
1301       $this->t->set_block('form','interviewer_list','int_list');
1302       $this->t->set_block('form','add','addhandle');
1303       $this->t->set_block('form','edit','edithandle');
1304       
1305       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1306       $this->t->set_var('readonly','');
1307       $this->t->set_var('disabled','');
1308       
1309       $action = get_var('action',array('GET','POST'));
1310       $companionship = get_var('companionship',array('GET','POST'));
1311       $interviewer = get_var('interviewer',array('GET','POST'));      
1312       $name = get_var('name',array('GET','POST'));
1313       $ppi = get_var('ppi',array('GET','POST'));
1314       $elder = get_var('elder',array('GET','POST'));
1315       $aaronic = get_var('aaronic',array('GET','POST'));
1316       $date = get_var('date',array('GET','POST'));
1317       $notes = get_var('notes',array('GET','POST'));
1318       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
1319       
1320       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1321       $this->db->query($sql,__LINE__,__FILE__);
1322       while ($this->db->next_record())
1323         {
1324           $supervisor = $this->db->f('supervisor');
1325           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
1326           $this->db2->query($sql,__LINE__,__FILE__);
1327           $this->db2->next_record();
1328           $interviewer_name = $this->db2->f('name');
1329           
1330           if($supervisor == $interviewer) { 
1331             $this->t->set_var('interviewer',$supervisor . ' selected');
1332           } else {
1333             $this->t->set_var('interviewer',$interviewer);
1334           }
1335           $this->t->set_var('interviewer_name',$interviewer_name);
1336           $this->t->fp('int_list','interviewer_list',True);
1337         }
1338       
1339       if($action == 'save')
1340         {
1341           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1342           $this->db->query("UPDATE eq_ppi set " .
1343                            "   ppi='" . $ppi . "'" .
1344                     ", interviewer='" . $interviewer . "'" .
1345                           ", elder='" . $elder . "'" .
1346                         ", aaronic='" . $aaronic . "'" .
1347                            ", date='" . $date . "'" .
1348                           ", notes='" . $notes . "'" .
1349                       ", eqpresppi='" . $eqpresppi . "'" .
1350                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
1351           $this->ppi_view();
1352           return false;
1353         }
1354
1355       if($action == 'insert')
1356         {
1357           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1358           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
1359                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
1360                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
1361           $this->ppi_view();
1362           return false;
1363         }
1364       
1365       if($action == 'add')
1366         {
1367           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1368           $this->t->set_var('ppi', '');
1369           $this->t->set_var('interviewer', $interviewer);
1370           $this->t->set_var('name',$name);
1371           $this->t->set_var('elder',$elder);
1372           $this->t->set_var('aaronic',$aaronic);
1373           $this->t->set_var('date','');
1374           $this->t->set_var('notes','');
1375           $this->t->set_var('eqpresppi',$eqpresppi);
1376           $this->t->set_var('lang_done','Cancel');
1377           $this->t->set_var('lang_action','Adding New PPI');
1378           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1379                                                                 . $ppi . '&action=' . 'insert'));
1380         }
1381
1382       if($action == 'edit' || $action == 'view')
1383         {
1384           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
1385           $this->db->query($sql,__LINE__,__FILE__);
1386           $this->db->next_record();
1387           $this->t->set_var('ppi',$ppi);
1388           $this->t->set_var('name',$name);
1389           $this->t->set_var('interviewer', $this->db->f('interviewer'));
1390           $this->t->set_var('elder',$this->db->f('elder'));
1391           $this->t->set_var('aaronic',$this->db->f('aaronic'));
1392           $this->t->set_var('date',$this->db->f('date'));
1393           $this->t->set_var('notes',$this->db->f('notes'));
1394           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
1395         }
1396       
1397       if($action == 'edit')
1398         {
1399           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1400           $this->t->set_var('lang_done','Cancel');
1401           $this->t->set_var('lang_action','Editing PPI');
1402           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1403                                                                 . $ppi . '&action=' . 'save'));
1404         }
1405
1406       if($action == 'view')
1407         {
1408           $date = $this->db->f('date');
1409           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1410           $this->t->set_var('readonly','READONLY');
1411           $this->t->set_var('disabled','DISABLED');
1412           $this->t->set_var('lang_done','Done');
1413           $this->t->set_var('lang_action','Viewing PPI');
1414           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1415                                                                 . $ppi . '&action=' . 'edit'));
1416         }
1417       
1418       $this->t->set_var('lang_reset','Clear Form');
1419       $this->t->set_var('lang_add','Add PPI');
1420       $this->t->set_var('lang_save','Save Changes');
1421       $this->t->set_var('edithandle','');
1422       $this->t->set_var('addhandle','');
1423
1424       $this->t->pfp('out','form');
1425       
1426       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
1427       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1428       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1429      
1430       $this->save_sessiondata(); 
1431     }
1432
1433   function vis_view()
1434     {
1435       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
1436       $this->t->set_block('vis_view_t','visit_list','list1');
1437       $this->t->set_block('vis_view_t','family_list','list2');
1438
1439       $this->t->set_var('lang_name','Family Name');
1440       $this->t->set_var('lang_date','Date');
1441       
1442       $sql = "SELECT * FROM eq_visit WHERE companionship=0 ORDER BY date DESC";
1443       $this->db->query($sql,__LINE__,__FILE__);
1444       $total_records = $this->db->num_rows();
1445
1446       $i = 0;
1447       while ($this->db->next_record())
1448         {
1449           $visit_list[$i]['visit'] = $this->db->f('visit');
1450           $visit_list[$i]['family'] = $this->db->f('family');
1451           $visit_list[$i]['date']  = $this->db->f('date');
1452           $i++;
1453         }
1454             
1455       for ($i=0; $i < count($visit_list); $i++)
1456         {         
1457           $this->nextmatchs->template_alternate_row_color(&$this->t);
1458
1459           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
1460           $this->db->query($sql,__LINE__,__FILE__);
1461           $this->db->next_record();
1462                   
1463           $this->t->set_var('family',$visit_list[$i]['family']);
1464           $this->t->set_var('family_name',$this->db->f('name'));
1465           $this->t->set_var('date',$visit_list[$i]['date']);
1466           
1467           $link_data['menuaction'] = 'eq.eq.vis_update';
1468           $link_data['visit'] = $visit_list[$i]['visit'];
1469           $link_data['name'] = $this->db->f('name');
1470           $link_data['date'] = $visit_list[$i]['date'];
1471           $link_data['action'] = 'view';
1472           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1473           $this->t->set_var('lang_view','View');
1474
1475           $link_data['menuaction'] = 'eq.eq.vis_update';
1476           $link_data['visit'] = $visit_list[$i]['visit'];
1477           $link_data['name'] = $this->db->f('name');
1478           $link_data['date'] = $visit_list[$i]['date'];
1479           $link_data['action'] = 'edit';
1480           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1481           $this->t->set_var('lang_edit','Edit');
1482
1483           $this->t->fp('list1','visit_list',True);
1484         }
1485
1486       // List the families that are available to record a visit against
1487       $sql = "SELECT * FROM eq_family WHERE valid=1";
1488       $this->db->query($sql,__LINE__,__FILE__);
1489       $total_records = $this->db->num_rows();
1490
1491       $i = 0;
1492       while ($this->db->next_record())
1493         {
1494           $family_names[$i] = $this->db->f('name');
1495           $family_ids[$i] = $this->db->f('family');
1496           $i++;
1497         } array_multisort($family_names, $family_ids);
1498       
1499       for ($i=0; $i < count($family_names); $i++)
1500         {
1501           $link_data['menuaction'] = 'eq.eq.vis_update';
1502           $link_data['visit'] = '';
1503           $link_data['family'] = $family_ids[$i];
1504           $link_data['action'] = 'add';
1505           $link_data['name'] = $family_names[$i];
1506           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1507
1508           $this->t->set_var('name',$family_names[$i]);
1509           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
1510           else { $this->t->set_var('table_sep',"</td>"); }
1511           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
1512
1513           $this->t->fp('list2','family_list',True);
1514         }   
1515
1516       $this->t->pfp('out','vis_view_t');
1517       $this->save_sessiondata(); 
1518     }
1519
1520   function vis_update()
1521     {
1522       $this->t->set_file(array('form' => 'vis_update.tpl'));
1523       $this->t->set_block('form','add','addhandle');
1524       $this->t->set_block('form','edit','edithandle');
1525       
1526       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
1527       $this->t->set_var('readonly','');
1528       $this->t->set_var('disabled','');
1529       
1530       $action = get_var('action',array('GET','POST'));
1531       $visit = get_var('visit',array('GET','POST'));
1532       $family = get_var('family',array('GET','POST'));
1533       $name = get_var('name',array('GET','POST'));
1534       $date = get_var('date',array('GET','POST'));
1535       $notes = get_var('notes',array('GET','POST'));
1536       $companionship = 0;
1537       
1538       if($action == 'save')
1539         {
1540           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1541           $this->db->query("UPDATE eq_visit set " .
1542                            "  date='" . $date . "'" .
1543                           ", notes='" . $notes . "'" .
1544                            " WHERE visit=" . $visit,__LINE__,__FILE__);
1545           $this->vis_view();
1546           return false;
1547         }
1548
1549       if($action == 'insert')
1550         {
1551           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1552           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
1553                            . "VALUES ('" . $family . "','" . $companionship . "','"
1554                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
1555           $this->vis_view();
1556           return false;
1557         }
1558       
1559       if($action == 'add')
1560         {
1561           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1562           $this->t->set_var('family', $family);
1563           $this->t->set_var('visit', '');
1564           $this->t->set_var('name', $name);
1565           $this->t->set_var('date','');
1566           $this->t->set_var('notes','');
1567           $this->t->set_var('lang_done','Cancel');
1568           $this->t->set_var('lang_action','Adding New Visit');
1569           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
1570                                                                 . $family . '&action=' . 'insert'));
1571         }
1572
1573       if($action == 'edit' || $action == 'view')
1574         {
1575           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
1576           $this->db->query($sql,__LINE__,__FILE__);
1577           $this->db->next_record();
1578           $this->t->set_var('visit',$visit);
1579           $this->t->set_var('name',$name);
1580           $this->t->set_var('family', $family);
1581           $this->t->set_var('date',$this->db->f('date'));
1582           $this->t->set_var('notes',$this->db->f('notes'));
1583         }
1584       
1585       if($action == 'edit')
1586         {
1587           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
1588           $this->t->set_var('lang_done','Cancel');
1589           $this->t->set_var('lang_action','Editing Visit');
1590           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
1591                                                                 . $visit . '&action=' . 'save'));
1592         }
1593
1594       if($action == 'view')
1595         {
1596           $date = $this->db->f('date');
1597           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
1598           $this->t->set_var('readonly','READONLY');
1599           $this->t->set_var('disabled','DISABLED');
1600           $this->t->set_var('lang_done','Done');
1601           $this->t->set_var('lang_action','Viewing Visit');
1602           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
1603                                                                 . $visit . '&action=' . 'edit'));
1604         }
1605       
1606       $this->t->set_var('lang_reset','Clear Form');
1607       $this->t->set_var('lang_add','Add Visit');
1608       $this->t->set_var('lang_save','Save Changes');
1609       $this->t->set_var('edithandle','');
1610       $this->t->set_var('addhandle','');
1611
1612       $this->t->pfp('out','form');
1613       
1614       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
1615       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
1616       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1617
1618       $this->save_sessiondata(); 
1619     }
1620
1621   function att_view()
1622     {
1623       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
1624       $this->t->set_block('att_view_t','act_list','list');
1625
1626       $this->t->set_block('att_view_t','month_list','list1');
1627       $this->t->set_block('att_view_t','header_list','list2');
1628       $this->t->set_block('att_view_t','elder_list','list3');
1629       
1630       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
1631       $num_months = get_var('num_months',array('GET','POST'));
1632       if($num_months == '') { $num_months = $this->default_att_num_months; }
1633       $this->t->set_var('num_months',$num_months);
1634       $this->t->set_var('lang_filter','Filter');
1635       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1636       else {  $this->t->set_var('lang_num_months','Months of History'); }
1637          
1638       $sql = "SELECT * FROM eq_elder where valid=1";
1639       $this->db->query($sql,__LINE__,__FILE__);
1640       $i=0;
1641       while ($this->db->next_record())
1642         {
1643           $elder_name[$i] = $this->db->f('name');
1644           $elder_id[$i] = $this->db->f('elder');
1645           $i++;
1646         }
1647       array_multisort($elder_name, $elder_id);
1648
1649       
1650       // Create a list of sunday dates for a window of 3 months back and current month
1651       $i=0; 
1652       $last_time = 0; 
1653       $found_sunday = 0;
1654       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, date("m")-$num_months, 1, date("y")));
1655       $last_date = explode("-",$sunday_list[0]['date']);
1656       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1657       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
1658       while($last_time < $time_limit)
1659       {
1660         $day = date("w",$last_time);
1661         if(date("w",$last_time) == 0) {
1662           $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
1663           $last_date = explode("-",$sunday_list[$i]['date']);
1664           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1665           $sunday_list[$i]['day'] = $last_date[2];
1666           $sunday_list[$i]['month'] = date("M",$last_time);
1667           $sunday_list[$i]['year'] = $last_date[0];
1668           $found_sunday = 1; 
1669         }
1670         $last_time += 90000;
1671         if($found_sunday) { $i++; $found_sunday=0; }
1672       }
1673
1674       $total_elders = count($elder_id);
1675       $old_month=$sunday_list[0]['month']; $span=0;
1676       for ($i=0; $i < count($sunday_list); $i++) {
1677         $date = $sunday_list[$i]['date'];
1678         $this->t->set_var('date',$sunday_list[$i]['date']);
1679         $this->t->set_var('day',$sunday_list[$i]['day']);
1680         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
1681           if($i == count($sunday_list)-1) { $span++; }
1682           $cur_month = $sunday_list[$i]['month'];
1683           $old_month = $sunday_list[$i]['month'];         
1684           $link_data['menuaction'] = 'eq.eq.att_update';
1685           $link_data['month'] = $sunday_list[$i-1]['month'];
1686           $link_data['year'] = $sunday_list[$i-1]['year'];
1687           $link_data['action'] = 'update_month';
1688           $cur_month = $sunday_list[$i-1]['month'];
1689           $cur_year = $sunday_list[$i-1]['year'];
1690           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
1691           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1692           $this->t->set_var('month',$sunday_list[$i-1]['month']);
1693           $this->t->set_var('year',$sunday_list[$i-1]['year']);
1694           $this->t->set_var('span',$span); $span=0;
1695           $this->t->fp('list1','month_list',True);
1696         } $span++;
1697       }
1698       $this->t->set_var('total_elders',$total_elders);
1699       $this->t->set_var('header_row',$header_row);
1700       
1701       $elder_width=200; $att_width=25; $total_width=$elder_width; 
1702       for ($i=0; $i < count($sunday_list); $i++) {
1703         $link_data['menuaction'] = 'eq.eq.att_update';
1704         $link_data['month'] = $sunday_list[$i]['month'];
1705         $link_data['year'] = $sunday_list[$i]['year'];
1706         $link_data['day'] = $sunday_list[$i]['day'];
1707         $link_data['date'] = $sunday_list[$i]['date'];
1708         $link_data['action'] = 'update_day';
1709         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1710         $this->t->set_var('date',$sunday_list[$i]['date']);
1711         $this->t->set_var('day',$sunday_list[$i]['day']);
1712         $this->t->set_var('month',$sunday_list[$i]['month']);
1713         $this->t->set_var('year',$sunday_list[$i]['year']);
1714         $this->t->fp('list2','header_list',True);
1715         $total_width += $att_width;
1716       }
1717
1718       for ($i=0; $i < count($elder_id); $i++) {
1719         $att_table = "";
1720         $this->nextmatchs->template_alternate_row_color(&$this->t);
1721         $this->t->set_var('elder_name',$elder_name[$i]);
1722         #print "checking for elder: " . $elder_id[$i] . "<br>";
1723         for ($j=0; $j < count($sunday_list); $j++) {
1724           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
1725           #print "SELECT * FROM eq_attendance WHERE date='"
1726           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
1727           $sql = "SELECT * FROM eq_attendance WHERE date='"
1728              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
1729           $this->db->query($sql,__LINE__,__FILE__);
1730           if($this->db->next_record()) {
1731             $cur_month = $sunday_list[$j]['month'];
1732             if($attended[$i][$cur_month] != 1) { 
1733               $attended[$i][$cur_month]=1;
1734               $attendance[$cur_month]++;
1735             }
1736             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
1737           } else {
1738             $att_table .= '<td>&nbsp;</td>';
1739           }
1740         }
1741         $this->t->set_var('att_table',$att_table);
1742         $this->t->fp('list3','elder_list',True);
1743       }
1744       $this->t->set_var('total_width',$total_width);
1745       $this->t->set_var('elder_width',$elder_width);
1746       $this->t->set_var('att_width',$att_width);
1747
1748       # Now calculate attendance for these months
1749       $attendance_str = "";
1750       $nonattendance_str = "";
1751       $aveattendance_str = "";
1752       $avenonattendance_str = "";
1753       $num_months=0;
1754       $ave_total_attended=0;
1755       foreach($attendance as $att => $value) {
1756         $total_attended = $attendance[$att];
1757         $ave_total_attended += $attendance[$att]; $num_months++;
1758         $percent = ceil(($total_attended / $total_elders)*100);
1759         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
1760         $total_nonattended = $total_elders - $total_attended;
1761         $percent = ceil(($total_nonattended / $total_elders)*100);
1762         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
1763         
1764         $total_attended = ceil(($ave_total_attended / $num_months));
1765         $percent = ceil(($total_attended / $total_elders)*100);
1766         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
1767         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
1768         $percent = ceil(($total_attended / $total_elders)*100);
1769         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
1770       }
1771       
1772       $this->t->set_var('attendance',$attendance_str);
1773       $this->t->set_var('aveattendance',$aveattendance_str);
1774       $this->t->set_var('nonattendance',$nonattendance_str);
1775       $this->t->set_var('avenonattendance',$avenonattendance_str);
1776       
1777       $this->t->pfp('out','att_view_t');
1778       $this->save_sessiondata(); 
1779     }
1780
1781   function att_update()
1782     {
1783       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
1784       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
1785       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
1786
1787       $this->t->set_file(array('form' => 'att_update.tpl'));
1788       $this->t->set_block('form','edit','edithandle');
1789       
1790       $this->t->set_block('form','month_list','list1');
1791       $this->t->set_block('form','header_list','list2');
1792       $this->t->set_block('form','elder_list','list3');
1793
1794       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
1795
1796       $action = get_var('action',array('GET','POST'));
1797       $month = get_var('month',array('GET','POST'));
1798       $year = get_var('year',array('GET','POST'));
1799       $day = get_var('day',array('GET','POST'));
1800       $date = get_var('date',array('GET','POST'));
1801
1802       if($action == 'save_month' || $action == 'save_day')
1803         {
1804            $new_data = get_var('elders_attended',array('POST'));
1805            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
1806
1807            if($action == 'save_month') {        
1808              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
1809            }
1810
1811            if($action == 'save_day') {        
1812              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
1813            }   
1814
1815            foreach ($new_data as $data)
1816            {
1817               $data_array = explode("-",$data);
1818               $elder = $data_array[0];
1819               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
1820               $this->db->query("INSERT INTO eq_attendance (elder,date) "
1821                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
1822            }
1823         
1824          $this->att_view();
1825          return false;    
1826         }
1827
1828       $sql = "SELECT * FROM eq_elder where valid=1";
1829       $this->db->query($sql,__LINE__,__FILE__);
1830       $i=0;
1831       while ($this->db->next_record())
1832         {
1833           $elder_name[$i] = $this->db->f('name');
1834           $elder_id[$i] = $this->db->f('elder');
1835           $i++;
1836         }
1837       array_multisort($elder_name, $elder_id);
1838       
1839       if($action == 'update_month')
1840       {
1841         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
1842         $i=0; 
1843         $last_time = 0; 
1844         $found_sunday = 0;
1845         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
1846         $last_date = explode("-",$sunday_list[0]['date']);
1847         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1848         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
1849         while($last_time <= $time_limit)
1850         {
1851           $day = date("w",$last_time);
1852           if(date("w",$last_time) == 0) { 
1853             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
1854             $last_date = explode("-",$sunday_list[$i]['date']);
1855             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
1856             $sunday_list[$i]['day'] = $last_date[2];
1857             $sunday_list[$i]['month'] = date("M",$last_time);
1858             $sunday_list[$i]['year'] = $last_date[0];
1859             $found_sunday = 1; 
1860           }
1861           $last_time += 90000;
1862           if($found_sunday) { $i++; $found_sunday=0; }
1863         }
1864
1865         $this->t->set_var('span', $i);
1866         $this->t->set_var('month',$sunday_list[$i-1]['month']);
1867         $this->t->set_var('year',$sunday_list[$i-1]['year']);
1868         $this->t->fp('list1','month_list',True);
1869         $elder_width=200; $att_width=25; $total_width=$elder_width;
1870         for ($i=0; $i < count($sunday_list); $i++) {
1871           $link_data['menuaction'] = 'eq.eq.att_update';
1872           $link_data['month'] = $sunday_list[$i]['month'];
1873           $link_data['year'] = $sunday_list[$i]['year'];
1874           $link_data['day'] = $sunday_list[$i]['day'];
1875           $link_data['date'] = $sunday_list[$i]['date'];
1876           $link_data['action'] = 'update_day';
1877           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
1878           $this->t->set_var('date',$sunday_list[$i]['date']);
1879           $this->t->set_var('day',$sunday_list[$i]['day']);
1880           $this->t->set_var('month',$sunday_list[$i]['month']);
1881           $this->t->set_var('year',$sunday_list[$i]['year']);
1882           $this->t->fp('list2','header_list',True);
1883           $total_width += $att_width;
1884         }     
1885       }
1886
1887       if($action == 'update_day')
1888       {
1889         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
1890         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
1891         $this->t->set_var('month',$month);
1892         $this->t->set_var('year',$year);
1893         $this->t->fp('list1','month_list',True);
1894         $this->t->set_var('date',$date);
1895         $this->t->set_var('day',$day);
1896         $this->t->set_var('month',$month);
1897         $this->t->set_var('year',$year);
1898         $this->t->fp('list2','header_list',True);
1899       }           
1900             
1901       for ($i=0; $i < count($elder_id); $i++) {
1902         $att_table = "";
1903         $this->nextmatchs->template_alternate_row_color(&$this->t);
1904         $this->t->set_var('elder_name',$elder_name[$i]);
1905         for ($j=0; $j < count($sunday_list); $j++) {
1906           $sql = "SELECT * FROM eq_attendance WHERE date='"
1907              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
1908           $this->db->query($sql,__LINE__,__FILE__);
1909           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
1910           if($this->db->next_record()) {
1911             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
1912           } else {
1913             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
1914           }
1915         }
1916         $this->t->set_var('att_table',$att_table);
1917         $this->t->fp('list3','elder_list',True);
1918       } 
1919            
1920       $this->t->set_var('lang_done', 'Cancel');
1921       $this->t->set_var('lang_reset','Clear Form');
1922       $this->t->set_var('lang_save','Save Changes');
1923
1924       $this->t->pfp('out','form');
1925       $this->t->pfp('addhandle','edit');
1926
1927       $this->save_sessiondata();       
1928     }
1929
1930   function dir_view()
1931     {
1932       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
1933       $this->t->set_block('dir_view_t','dir_list','list');
1934       
1935       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
1936       $this->db->query($sql,__LINE__,__FILE__);
1937       $i=0;
1938       while ($this->db->next_record())
1939         {
1940           $parent[$i]['id'] = $this->db->f('parent');
1941           $parent[$i]['name'] = $this->db->f('name');
1942           $parent[$i]['phone'] = $this->db->f('phone');
1943           $parent[$i]['address'] = $this->db->f('address');
1944           $i++;
1945         }   
1946       
1947       for ($i=0; $i < count($parent); $i++) 
1948       {
1949         $name = $parent[$i]['name'];
1950         $phone = $parent[$i]['phone'];
1951         $address = $parent[$i]['address'];
1952         $this->t->set_var('name', $name);
1953         $this->t->set_var('address', $address);
1954         $this->t->set_var('phone', $phone);
1955         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1956         $this->t->set_var('tr_color',$tr_color);
1957         $this->t->fp('list','dir_list',True);
1958         //print "$phone $name $address<br>";
1959       }
1960       $this->t->pfp('out','dir_view_t');
1961       $this->save_sessiondata();   
1962     }
1963   
1964   function org_view()
1965     {
1966       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
1967       $this->t->set_block('org_view_t','calling_list','list1');
1968       $this->t->set_block('org_view_t','org_list','list2');
1969
1970       # Display a list ordered alphabetically
1971       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
1972       $this->db->query($sql,__LINE__,__FILE__);
1973       $i=0;
1974       while ($this->db->next_record())
1975         {
1976           $calling[$i]['id'] = $this->db->f('indiv_id');
1977           $calling[$i]['name'] = $this->db->f('name');
1978           $calling[$i]['position'] = $this->db->f('position');
1979           $calling[$i]['sustained'] = $this->db->f('sustained');
1980           $calling[$i]['organization'] = $this->db->f('organization');
1981           $i++;
1982         }   
1983       for ($i=0; $i < count($calling); $i++) 
1984       {
1985         $name = $calling[$i]['name'];
1986         $position = $calling[$i]['position'];
1987         $sustained = $calling[$i]['sustained'];
1988         $organization = $calling[$i]['organization'];
1989         $this->t->set_var('name', $name);
1990         $this->t->set_var('position', $position);
1991         $this->t->set_var('sustained', $sustained);
1992         $this->t->set_var('organization', $organization);
1993         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1994         $this->t->set_var('tr_color',$tr_color);
1995         $this->t->fp('list1','calling_list',True);
1996       }
1997
1998       # Display a list ordered by organization
1999       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
2000       $this->db->query($sql,__LINE__,__FILE__);
2001       $i=0;
2002       while ($this->db->next_record())
2003         {
2004           $calling[$i]['id'] = $this->db->f('indiv_id');
2005           $calling[$i]['name'] = $this->db->f('name');
2006           $calling[$i]['position'] = $this->db->f('position');
2007           $calling[$i]['sustained'] = $this->db->f('sustained');
2008           $calling[$i]['organization'] = $this->db->f('organization');
2009           $i++;
2010         }   
2011       for ($i=0; $i < count($calling); $i++) 
2012       {
2013         $name = $calling[$i]['name'];
2014         $position = $calling[$i]['position'];
2015         $sustained = $calling[$i]['sustained'];
2016         $organization = $calling[$i]['organization'];
2017         $this->t->set_var('name', $name);
2018         $this->t->set_var('position', $position);
2019         $this->t->set_var('sustained', $sustained);
2020         $this->t->set_var('organization', $organization);
2021         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2022         $this->t->set_var('tr_color',$tr_color);
2023         $this->t->fp('list2','org_list',True);
2024       }
2025       
2026       $this->t->pfp('out','org_view_t');
2027       $this->save_sessiondata();   
2028     }
2029
2030   function admin()
2031     {
2032       $this->t->set_file(array('admin_t' => 'admin.tpl'));
2033       $this->t->set_block('admin_t','upload','uploadhandle');
2034       $this->t->set_block('admin_t','admin','adminhandle');
2035       $this->t->set_block('admin_t','cmd','cmdhandle');
2036       
2037       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
2038       
2039       $action = get_var('action',array('GET','POST'));
2040
2041       $this->t->pfp('out','admin_t');
2042       
2043       if($action == 'upload')
2044         {        
2045           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
2046           
2047           if(($_FILES['uploadedfile']['type'] == "application/zip") &&
2048              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
2049             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
2050             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
2051             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
2052             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
2053             $this->t->set_var('uploadstatus',$uploadstatus);
2054             $this->t->pfp('uploadhandle','upload');
2055             $this->t->set_var('uploadhandle','');
2056             print "<table border=1 width=80%><tr><td>\n<pre>";
2057             
2058             # make a directory for this data to be stored in
2059             $date="data_" . date("Y_m_d");
2060             $data_dir = $this->upload_target_path . $date;
2061             print "-> Making the data directory: $date<br>\n";
2062             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
2063             if($return_code != 0) {
2064               print implode('\n',$result) . "<br>";
2065               print "<b><font color=red>";
2066               print "-E- Unable to create the data directory. Aborting import.";
2067               print "</font></b>";
2068               return 0;
2069             }
2070
2071             # move the file uploaded into this directory
2072             print "-> Moving the uploaded file into the data dir<br>\n";
2073             exec('mv ' . $target_path . ' ' . $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 move the uploaded file into the data dir. Aborting import.";
2078               print "</font></b>";
2079               return 0;
2080             }
2081             
2082             # unzip the data into this directory
2083             print "-> Unzipping the data<br>\n";
2084             $data_file = $data_dir . '';
2085             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
2086             if($return_code != 0) {
2087               print implode('\n',$result) . "<br>";
2088               print "<b><font color=red>";
2089               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
2090               print "</font></b>";
2091               return 0;
2092             }
2093             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
2094
2095             # update the data_latest link to point to this new directory
2096             print "-> Updating the latest data dir link<br>\n";
2097             $data_latest = $this->upload_target_path . 'data_latest';
2098             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
2099             if($return_code != 0) {
2100               print implode('\n',$result) . "<br>";
2101               print "<b><font color=red>";
2102               print "-E- Unable to update the data latest link. Aborting import.";
2103               print "</font></b>";
2104               return 0;
2105             }
2106             
2107             # run the import perl script to encorporate it into the DB
2108             ob_start('ob_logstdout', 2);
2109             print "-> Importing the data into the EQ database<br>\n";
2110             ob_flush(); flush(); sleep(1);
2111             $import_log = $this->upload_target_path . '/import.log';
2112             $data_log = $this->upload_target_path . '/data.log';
2113             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
2114             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
2115             #print "import_cmd: $import_cmd<br>";
2116             #print "parse_cmd: $parse_cmd<br>";
2117             ob_start('ob_logstdout', 2);
2118             passthru($import_cmd);
2119             passthru($parse_cmd);
2120             ob_flush(); flush(); sleep(1);
2121
2122             # fix the permissions of the data dir
2123             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
2124             
2125             $this->t->pfp('cmdhandle','cmd');
2126             print "</pre></td></tr></table>";
2127             
2128           } else if($_FILES['uploadedfile']['type'] != "application/zip") {
2129             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
2130             $this->t->set_var('uploadstatus',$uploadstatus);
2131             
2132           } else {
2133             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
2134             $uploadstatus.= ") uploading the file, please try again! </font></b>";
2135             $this->t->set_var('uploadstatus',$uploadstatus);
2136           }
2137         }
2138       else
2139         {
2140           $this->t->set_var('adminhandle','');
2141           $this->t->pfp('adminhandle','admin'); 
2142         }
2143       
2144       $this->save_sessiondata();   
2145     }
2146   
2147 }
2148
2149 ?>