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