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