Fixed interview tool to encorporate a new interview scheduling tool.
[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_int_num_months;
26   var $default_int_num_years;
27   var $default_att_num_months;
28   var $current_year;
29   var $current_month;
30   var $upload_target_path;
31   var $script_path;
32   
33   var $public_functions = array
34     (
35      'ht_view'    => True,
36      'ht_update'  => True,
37      'act_list'   => True,
38      'act_view'   => True,
39      'act_update' => True,
40      'par_view'   => True,
41      'ppi_view'   => True,
42      'ppi_update' => True,
43      'ppi_sched'  => True,
44      'int_view'   => True,
45      'int_update' => True,
46      'int_sched'  => True,
47      'vis_sched'  => True,
48      'vis_view'   => True,
49      'vis_update' => True,
50      'att_view'   => True,
51      'att_update' => True,
52      'dir_view'   => True,
53      'org_view'   => True,
54      'schedule'   => True,
55      'admin'      => True
56      );
57  
58   function eq()
59     {
60       $this->default_ht_num_months = 3;
61       $this->default_ppi_num_months = 3;
62       $this->default_ppi_num_years = 0;
63       $this->default_int_num_quarters = 1;
64       $this->default_int_num_months = 3;
65       $this->default_int_num_years = 0;
66       $this->default_att_num_quarters = 3;
67       $this->default_att_num_months = 3;
68       $this->upload_target_path = "/home/users/eqpres/eq_data/";
69       $this->script_path = "/usr/share/phpgroupware/eq/";
70       
71       $this->db         = $GLOBALS['phpgw']->db;
72       $this->db2        = $this->db;
73       $this->db3        = $this->db;
74       $this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
75       $this->t          = $GLOBALS['phpgw']->template;
76       $this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
77       $this->grants     = $GLOBALS['phpgw']->acl->get_grants('eq');
78       $this->grants[$this->account] = PHPGW_ACL_READ + PHPGW_ACL_ADD + PHPGW_ACL_EDIT + PHPGW_ACL_DELETE;
79        
80       $this->jscal = CreateObject('phpgwapi.jscalendar');   // before phpgw_header() !!!
81       $this->cal_options = 'daFormat    : "%Y-%m-%d",
82                                 ifFormat    : "%Y-%m-%d",
83                                 mondayFirst : false,
84                                 weekNumbers : false';
85        
86       $GLOBALS['phpgw_info']['flags']['app_header'] = 'Elders Quorum Tools';
87       $GLOBALS['phpgw']->common->phpgw_header();
88
89       $this->current_day = `date '+%d'`;
90       $this->current_day = $this->current_day-0; // Make it numeric
91       $this->current_month = `date '+%m'`;
92       $this->current_month = $this->current_month-0; // Make it numeric
93       $this->current_year = `date '+%Y'`;
94       $this->current_year = $this->current_year-0; // Make it numeric
95
96       echo parse_navbar();
97       $this->display_app_header();      
98     }
99   
100   function save_sessiondata()
101     {
102       
103     }
104
105   function display_app_header()
106     {
107       $this->t->set_file(array('eq_header' => 'header.tpl'));
108       
109       if (isset($phpgw_info['user']['preferences']['eq']['eq_font']))
110         {
111           $font = $phpgw_info['user']['preferences']['eq']['eq_font'];
112         }
113       else
114         {
115           $font = 'Arial';
116         }
117       
118       $this->t->set_var('bg_color',$phpgw_info['theme']['th_bg']);
119       $this->t->set_var('font',$font);
120       $link_data['menuaction'] = 'eq.eq.ht_view';
121       $this->t->set_var('link_hometeaching',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
122       $this->t->set_var('lang_hometeaching','HomeTeaching');
123       $link_data['menuaction'] = 'eq.eq.act_list';
124       $this->t->set_var('link_activity',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
125       $this->t->set_var('lang_activity','Activities');
126       $link_data['menuaction'] = 'eq.eq.par_view';
127       $this->t->set_var('link_participation',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
128       $this->t->set_var('lang_participation','Participation');
129       $link_data['menuaction'] = 'eq.eq.ppi_view';
130       $this->t->set_var('link_ppi',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
131       $this->t->set_var('lang_ppi','PPIs');
132       $link_data['menuaction'] = 'eq.eq.int_view';
133       $this->t->set_var('link_int',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
134       $this->t->set_var('lang_int','Interviews');
135       $link_data['menuaction'] = 'eq.eq.vis_view';
136       $this->t->set_var('link_visit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
137       $this->t->set_var('lang_visit','Visits');
138       $link_data['menuaction'] = 'eq.eq.att_view';      
139       $this->t->set_var('link_attendance',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
140       $this->t->set_var('lang_attendance','Attendance');
141       $link_data['menuaction'] = 'eq.eq.dir_view';      
142       $this->t->set_var('link_dir',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
143       $this->t->set_var('lang_dir','Directory');
144       $link_data['menuaction'] = 'eq.eq.org_view';      
145       $this->t->set_var('link_org',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
146       $this->t->set_var('lang_org','Callings');
147       $link_data['menuaction'] = 'eq.eq.admin'; 
148       $this->t->set_var('link_admin',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
149       $this->t->set_var('lang_admin','Admin');
150       $link_data['menuaction'] = 'eq.eq.schedule';      
151       $this->t->set_var('link_schedule',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
152       $this->t->set_var('lang_schedule','Scheduling');
153                 
154       $this->t->pparse('out','eq_header');
155     }
156
157   function ht_view()
158     {
159       $this->t->set_file(array('ht_view_t' => 'ht_view.tpl'));
160       $this->t->set_block('ht_view_t','district_list','list');
161    
162       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
163       $num_months = get_var('num_months',array('GET','POST'));
164       if($num_months == '') { $num_months = $this->default_ht_num_months; }
165       $this->t->set_var('num_months',$num_months);
166       $this->t->set_var('lang_filter','Filter');
167       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
168       else {  $this->t->set_var('lang_num_months','Months of History'); }
169       
170       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
171       $this->t->set_var('title','Hometeaching'); 
172       
173       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
174       $this->db->query($sql,__LINE__,__FILE__);
175       $i=0;
176       while ($this->db->next_record())
177         {
178           $districts[$i]['district'] = $this->db->f('district');
179           $districts[$i]['name'] = $this->db->f('name');
180           $districts[$i]['supervisor'] = $this->db->f('supervisor');
181           $i++;
182         }
183
184       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
185       $this->db->query($sql,__LINE__,__FILE__);
186       $i=0;
187       while ($this->db->next_record())
188         {
189           $elder_id[$i] = $this->db->f('elder');
190           $elder_name[$i] = $this->db->f('name');
191           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
192           $i++;
193         }
194       array_multisort($elder_name, $elder_id);
195
196       // Make an array mapping elder_ids to elder_names
197       for($i=0; $i < count($elder_id); $i++) {
198           $id = $elder_id[$i];
199           $elders[$id] = $elder_name[$i];
200       }      
201
202       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
203       $this->db->query($sql,__LINE__,__FILE__);
204       while ($this->db->next_record())
205         {
206           $aaronic_id = $this->db->f('aaronic');
207           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
208           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
209         }
210       
211       $total_families = 0;
212       $this->nextmatchs->template_alternate_row_color(&$this->t);
213       for ($i=0; $i < count($districts); $i++) {
214         $this->t->set_var('district_number',$districts[$i]['district']);
215         $this->t->set_var('district_name',$districts[$i]['name']);      
216         $supervisor = $districts[$i]['supervisor'];
217         $unique_companionships='';
218                 
219         // Select all the unique companionship numbers for this district
220         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
221         $this->db->query($sql,__LINE__,__FILE__);
222         $j=0;
223         while ($this->db->next_record())
224           {
225             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
226             $j++;
227           }
228
229         $comp_width=450; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
230         $table_data=""; $num_companionships = 0; $num_families = 0;
231         for($m=$num_months; $m >= 0; $m--) { $visits[$m] = 0; }
232         for ($j=0; $j < count($unique_companionships); $j++) {
233           $companion_table_entry = "";
234           // Select all the companions in each companionship
235           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
236                  "companionship=". $unique_companionships[$j]['companionship'];
237           $this->db->query($sql,__LINE__,__FILE__);
238
239           while ($this->db->next_record())
240             {
241               // Get this companions information
242               if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
243               $companionship = $this->db->f('companionship');
244               $elder_id = $this->db->f('elder');
245               $aaronic_id = $this->db->f('aaronic');
246               if($elder_id) {
247                 $name = $elders[$elder_id];
248                 $phone = $elder_phone[$elder_id];
249               }
250               else if($aaronic_id) {
251                 $name = $aaronic[$aaronic_id]['name'];
252                 $phone = $aaronic[$aaronic_id]['phone'];                
253               }
254               $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
255             }
256           $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
257           
258           // Get the names of the families assigned this home teaching companionship
259           $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
260           $sql = $sql . " ORDER BY name ASC";
261           $this->db->query($sql,__LINE__,__FILE__);
262           $k=0;
263           while ($this->db->next_record())
264             {
265               $num_families++; $total_families++;
266               $family_name = $this->db->f('name');
267               $family_id = $this->db->f('family');
268               $this->nextmatchs->template_alternate_row_color(&$this->t);
269               $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
270               // Find out how many times Visits were performed by this companionship
271               // in the past $num_months for this Family
272               $header_row="<th width=$comp_width><font size=-2>Families</th>";
273               for($m=$num_months; $m >= 0; $m--) {
274                 $month = $this->current_month - $m;
275                 $year = $this->current_year;
276                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
277                 if($month < 10) { $month = "0"."$month"; }
278                 $month_start = "$year"."-"."$month"."-"."01";
279                 $month_end = "$year"."-"."$month"."-"."31";
280                 $month = "$month"."/"."$year";
281                 //print "m: $m month: $month year: $year month_start: $month_start month_end: $month_end<br>";
282                 // Add this to the query to filter on only visits made by this companionship:
283                 // " AND companionship=" . $unique_companionships[$j]['companionship'].
284                 $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
285                        " AND companionship!=0".
286                        " AND family=". $family_id;
287                 $this->db2->query($sql,__LINE__,__FILE__);
288                 $link_data['menuaction'] = 'eq.eq.ht_update';
289                 $link_data['date'] = $month_start;
290                 $link_data['month_start'] = $month_start;
291                 $link_data['month_end'] = $month_end;
292                 $link_data['month'] = $month;
293                 $link_data['district'] = $districts[$i]['district'];
294                 $link_data['district_name'] = $districts[$i]['name'];
295                 $link_data['action'] = 'view';
296                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
297                 $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
298                 if(!$total_visits[$m]) { $total_visits[$m] = 0; }
299                 if($this->db2->next_record()) {
300                   if($this->db2->f('visited') == 'y') {
301                     $visits[$m]++; $total_visits[$m]++;           
302                     $table_data .= '<td align=center><a href="'.$link.'"><img src="checkmark.gif"></a></td>';
303                   }
304                   else if($this->db2->f('visited') == 'n') {
305                     $table_data .= '<td align=center><a href="'.$link.'"><img src="x.gif"></a></td>';
306                   }
307                   else {
308                     $visits[$m]++; $total_visits[$m]++;
309                     $table_data .= "<td>&nbsp;</td>";
310                   }
311                 }
312                 else {
313                   $visits[$m]++; $total_visits[$m]++;
314                   $table_data .= "<td>&nbsp;</td>";
315                 }
316               }
317               $table_data .= "</tr>"; 
318               $k++;
319             }
320           $table_data .= "<tr><td colspan=20></td></tr>";
321         }
322         $table_data .= "<tr><td colspan=20><hr></td></tr>";
323         $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
324
325         for($m=$num_months; $m >=0; $m--) {
326           $percent = ceil(($visits[$m] / $num_families)*100);
327           $stat_data .= "<td align=center><font size=-2><b>$visits[$m]<br>$percent%</font></b></td>";
328         }
329         $stat_data .= "</tr>";
330
331         $this->t->set_var('table_width',$table_width);
332         $this->t->set_var('header_row',$header_row);
333         $this->t->set_var('table_data',$table_data);
334         $this->t->set_var('stat_data',$stat_data);
335         $this->t->fp('list','district_list',True);
336       }
337
338       $totals = "<tr><td><b><font size=-2>$total_families Total Families<br>Visit Totals:</font></b></td>";
339       for($m=$num_months; $m >=0; $m--) {
340         $percent = ceil(($total_visits[$m] / $total_families)*100);
341         $totals .= "<td align=center><font size=-2><b>$total_visits[$m]<br>$percent%</font></b></td>";
342       }
343       $totals .= "</tr>";
344       
345       $this->t->set_var('totals',$totals);
346       
347       $this->t->pfp('out','ht_view_t');
348       $this->save_sessiondata();
349     }
350       
351
352   function ht_update()
353     {
354       $this->t->set_file(array('ht_update_t' => 'ht_update.tpl'));
355       $this->t->set_block('ht_update_t','district_list','list');
356       $this->t->set_block('ht_update_t','save','savehandle');
357       
358       $district = get_var('district',array('GET','POST'));
359       $district_name = get_var('district_name',array('GET','POST'));
360       $date = get_var('date',array('GET','POST'));
361       $month = get_var('month',array('GET','POST'));
362       $month_start = get_var('month_start',array('GET','POST'));
363       $month_end = get_var('month_end',array('GET','POST'));
364       $action = get_var('action',array('GET','POST'));
365       
366       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
367       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_update&action=save'));
368       $this->t->set_var('lang_done','Cancel');
369       $this->t->set_var('district_name',$district_name);
370       $this->t->set_var('district_number',$district);
371       $this->t->set_var('title','Hometeaching Update ' . $month);
372       $this->t->set_var('date',$date);
373       
374       if($action == 'save')
375         {
376           // Get a list of all the companionships in this district
377           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
378           $this->db->query($sql,__LINE__,__FILE__);
379           $j=0;
380           while ($this->db->next_record())
381             {
382               $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
383               $j++;
384             }
385           for ($j=0; $j < count($unique_companionships); $j++)
386             {
387               // FIXME: We won't be able to go back and edit history on families that have been
388               // reassigned to a different companionship. The following delete command will not delete
389               // the history of visits under an older companionship, only the ones for the existing
390               // companionship. This will lead to duplicate visits being entered for an older
391               // month for the same family, making it impossible to change the past history once
392               // a family is reassigned. However, you will be able to view the history just fine.
393
394               //$comp=$unique_companionships[$j]['companionship'];
395               //print "deleting from eq_visit where companionship=$comp and date=$date and district=$district<br>";
396               // Delete all the visits that have taken place for all families for this month
397               $this->db->query("DELETE from eq_visit where companionship=" . $unique_companionships[$j]['companionship'] .
398                                " AND " . "date='" . $date . "'",__LINE__,__FILE__);
399             }
400
401           // Now, add the visits that are checked for this month
402           $new_data = get_var('family_visited',array('POST'));
403           foreach ($new_data as $family)
404            {
405              foreach ($family as $data)
406                {
407                  //print "family_visited: $data <br>";
408                  $data_array = explode("/",$data);
409                  $family_id = $data_array[0];
410                  $companionship = $data_array[1];
411                  $date = $data_array[2];
412                  $visited = $data_array[3];
413                  if($visited == "") { $visited = $data_array[4]; }
414                  //print "family_id: $family_id companionship: $companionship date: $date visited: $visited<br>";
415                  $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes,visited) "
416                                   . "VALUES (" . $family_id .",". $companionship .",'". $date ."','','". $visited ."')",__LINE__,__FILE__);
417                }
418            }
419           $this->ht_view();
420           return false;
421         }
422       
423       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
424       $this->db->query($sql,__LINE__,__FILE__);
425       $i=0;
426       while ($this->db->next_record())
427         {
428           $elder_id[$i] = $this->db->f('elder');
429           $elder_name[$i] = $this->db->f('name');
430           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
431           $i++;
432         }
433       array_multisort($elder_name, $elder_id);
434
435       // Make an array mapping elder_ids to elder_names
436       for($i=0; $i < count($elder_id); $i++) {
437           $id = $elder_id[$i];
438           $elders[$id] = $elder_name[$i];
439       }      
440
441       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
442       $this->db->query($sql,__LINE__,__FILE__);
443       while ($this->db->next_record())
444         {
445           $aaronic_id = $this->db->f('aaronic');
446           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
447           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
448         }
449       
450       // Select all the unique companionship numbers for this district
451       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
452       $this->db->query($sql,__LINE__,__FILE__);
453       $j=0; $unique_companionships='';
454       while ($this->db->next_record())
455         {
456           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
457           $j++;
458         }
459       
460       $comp_width=300; $visit_width=25; $table_width=$comp_width + $visit_width;
461       $table_data=""; $num_companionships = 0; $num_families = 0; $visits=0;
462       for ($j=0; $j < count($unique_companionships); $j++) {
463         $companion_table_entry = "";
464         // Select all the companions in each companionship
465         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
466                "companionship=". $unique_companionships[$j]['companionship'];
467         $this->db->query($sql,__LINE__,__FILE__);
468         
469         while ($this->db->next_record())
470           {
471             // Get this companions information
472             if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
473             $companionship = $this->db->f('companionship');
474             $elder_id = $this->db->f('elder');
475             $aaronic_id = $this->db->f('aaronic');
476             if($elder_id) {
477               $name = $elders[$elder_id];
478               $phone = $elder_phone[$elder_id];
479             }
480             else if($aaronic_id) {
481               $name = $aaronic[$aaronic_id]['name'];
482               $phone = $aaronic[$aaronic_id]['phone'];
483             }
484             $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
485           }
486         $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
487         
488         // Get the names of the families assigned this home teaching companionship
489         $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
490         $sql = $sql . " ORDER BY name ASC";
491         $this->db->query($sql,__LINE__,__FILE__);
492         while ($this->db->next_record())
493           {
494             $num_families++; $total_families++;
495             $family_name = $this->db->f('name');
496             $family_id = $this->db->f('family');
497             $this->nextmatchs->template_alternate_row_color(&$this->t);
498             $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
499             
500             $header_row="<th width=$comp_width><font size=-2>Families</th>";
501             $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
502                    " AND companionship!=0".
503                    " AND family=". $family_id;
504             $this->db2->query($sql,__LINE__,__FILE__);
505             $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
506             $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
507             if(!$total_visits) { $total_visits = 0; }
508             if($this->db2->next_record()) {
509               if($this->db2->f('visited') == 'y') {
510                 $visits++; $total_visits++;
511                 $table_data .= '<td width=100 align=center>';
512                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y" checked>Y';
513                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
514                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/"> ';
515                 $table_data .= '</td>';
516               } else if($this->db2->f('visited') == 'n') {
517                 $table_data .= '<td width=100 align=center>';
518                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
519                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n" checked>N';
520                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/">';
521                 $table_data .= '</td>';
522               } else {
523                 $table_data .= '<td width=100 align=center>';
524                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
525                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
526                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
527                 $table_data .= '</td>';
528               }
529             }
530             else {
531               $value .= "/";
532               $table_data .= '<td width=100 align=center>';
533               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
534               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
535               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
536               $table_data .= '</td>';         
537             }
538           }
539         $table_data .= "</tr>"; 
540         $table_data .= "<tr><td colspan=20></td></tr>";
541       }
542       $table_data .= "<tr><td colspan=20><hr></td></tr>";
543       $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
544       
545       $percent = ceil(($visits / $num_families)*100);
546       $stat_data .= "<td align=center><font size=-2><b>$visits<br>$percent%</font></b></td>";
547       $stat_data .= "</tr>";
548       
549       $this->t->set_var('table_width',$table_width);
550       $this->t->set_var('header_row',$header_row);
551       $this->t->set_var('table_data',$table_data);
552       $this->t->set_var('stat_data',$stat_data);
553       $this->t->fp('list','district_list',True);
554
555       $this->t->set_var('lang_reset','Clear Form');
556       $this->t->set_var('lang_save','Save Changes');
557       $this->t->set_var('savehandle','');
558       
559       $this->t->pfp('out','ht_update_t');
560       $this->t->pfp('addhandle','save');
561       
562       $this->save_sessiondata();
563     }
564
565   function act_list()
566     {
567       $this->t->set_file(array('act_list_t' => 'act_list.tpl'));
568       $this->t->set_block('act_list_t','act_list','list');
569       
570       $this->t->set_var('lang_name','Activity Name');
571       $this->t->set_var('lang_date','Date');
572       
573       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
574       $this->db->query($sql,__LINE__,__FILE__);
575       $total_records = $this->db->num_rows();
576
577       $i = 0;
578       while ($this->db->next_record())
579         {
580           $activity_list[$i]['name'] = $this->db->f('name');
581           $activity_list[$i]['date']  = $this->db->f('date');
582           $activity_list[$i]['activity']  = $this->db->f('activity');
583           $i++;
584         }
585             
586       for ($i=0; $i < count($activity_list); $i++)
587         {         
588           $this->nextmatchs->template_alternate_row_color(&$this->t);
589           $this->t->set_var('name',$activity_list[$i]['name']);
590           $this->t->set_var('date',$activity_list[$i]['date']);
591           
592           $link_data['menuaction'] = 'eq.eq.act_view';
593           $link_data['activity'] = $activity_list[$i]['activity'];
594           $link_data['action'] = 'view';
595           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
596           $this->t->set_var('lang_view','View');
597
598           $link_data['menuaction'] = 'eq.eq.act_update';
599           $link_data['activity'] = $activity_list[$i]['activity'];
600           $link_data['action'] = 'edit';
601           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
602           $this->t->set_var('lang_edit','Edit');
603
604           $link_data['menuaction'] = 'eq.eq.act_update';
605           $link_data['activity'] = '0';
606           $link_data['action'] = 'add';
607           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
608                            . '"><input type="submit" name="Add" value="' . 'Add Activity' .'"></font></form>');
609
610           $this->t->fp('list','act_list',True);
611         }
612
613       $this->t->pfp('out','act_list_t');
614       $this->save_sessiondata();
615     }
616
617   function act_view()
618     {
619       $this->t->set_file(array('act_view_t' => 'act_view.tpl'));
620       $this->t->set_block('act_view_t','part_list','list');
621       
622       $sql = "SELECT * FROM eq_activity WHERE activity=" . intval(get_var('activity',array('GET','POST')));
623       $this->db->query($sql,__LINE__,__FILE__);
624       $this->db->next_record();
625       $this->t->set_var('name', $this->db->f('name'));
626       $this->t->set_var('date', $this->db->f('date'));
627       $this->t->set_var('notes', $this->db->f('notes'));
628             
629       $this->t->set_var('lang_name','Activity Name');
630       $this->t->set_var('lang_date','Date');
631       $this->t->set_var('lang_notes','Notes');
632       $this->t->set_var('lang_done','Done');
633       $this->t->set_var('lang_action','View');
634
635       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
636       $this->t->set_var('tr_color',$tr_color);
637             
638       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
639
640       $link_data['menuaction'] = 'eq.eq.act_update';
641       $link_data['activity'] = get_var('activity',array('GET','POST'));
642       $link_data['action'] = 'edit';
643       $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
644       $this->t->set_var('lang_edit','Edit');
645       $this->t->set_var('cal_date',$this->db->f('date'));
646
647       // Now find out which elders participated in this activity
648       $sql = "SELECT * FROM eq_participation WHERE activity=" . intval(get_var('activity',array('GET','POST')));
649       $this->db->query($sql,__LINE__,__FILE__);
650       $total_records = $this->db->num_rows();
651
652       $i = 0;
653       while ($this->db->next_record())
654         {
655           $part_list[$i]['elder']  = $this->db->f('elder');
656           $i++;
657         }
658       
659       for ($i=0; $i < count($part_list); $i++)
660         {
661           $sql = "SELECT * FROM eq_elder WHERE elder=" . $part_list[$i]['elder'];
662           $this->db->query($sql,__LINE__,__FILE__);
663           $this->db->next_record();
664           $names[$i] = $this->db->f('name');
665         } sort($names);
666       
667       for ($i=0; $i < count($names); $i++)
668         {
669           //$this->nextmatchs->template_alternate_row_color(&$this->t);
670           $this->t->set_var('elder_name',$names[$i]);
671           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
672           else { $this->t->set_var('table_sep',"</td>"); }
673           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
674           $this->t->fp('list','part_list',True);
675         }
676       
677       $this->t->pfp('out','act_view_t');
678       $this->save_sessiondata();
679     }
680
681   function act_update()
682     {
683       $this->t->set_file(array('form' => 'act_update.tpl'));
684       $this->t->set_block('form','elder_list','list');
685       $this->t->set_block('form','add','addhandle');
686       $this->t->set_block('form','edit','edithandle');
687       $this->t->set_var('lang_done','Done');
688
689       $action = get_var('action',array('GET','POST'));
690       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
691       $activity['activity'] = intval(get_var('activity',array('GET','POST')));
692
693       if($action == 'save')
694         {
695           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
696           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
697           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
698           $this->db->query("UPDATE eq_activity set " .
699                            "   name='" . $activity['name'] .
700                            "', date='" . $activity['date'] . "'" .
701                            ", notes='" . $activity['notes'] . "'" .
702                            " WHERE activity=" . $activity['activity'],__LINE__,__FILE__);
703
704           // Delete all the elders who have particiapted in this activity
705           $this->db->query("DELETE from eq_participation where activity=".$activity['activity'],__LINE__,__FILE__);
706           
707           // Re-add the elders who are checked as having participated in this activity
708           $elders = get_var('elder_name',array('POST'));
709           foreach ($elders as $elder)
710             {
711               $this->db->query("INSERT INTO eq_participation (elder,activity) "
712                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
713             }
714
715           $this->act_list();
716           return false;
717         }
718
719       if($action == 'insert')
720         {
721           $activity['name'] = $this->db->db_addslashes(get_var('name',array('POST')));
722           $activity['date'] = $this->db->db_addslashes(get_var('date',array('POST')));
723           $activity['notes']= $this->db->db_addslashes(get_var('notes',array('POST')));
724           $this->db->query("INSERT INTO eq_activity (name,date,notes) "
725                            . "VALUES ('" . $activity['name'] . "','"
726                            . $activity['date'] . "','" . $activity['notes'] . "')",__LINE__,__FILE__);
727           $this->act_list();
728           return false;
729         }
730       
731       if($action == 'add')
732         {
733           $activity['activity'] = 0;
734           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
735           $this->t->set_var('name','');
736           $this->t->set_var('date','');
737           $this->t->set_var('notes','');
738           $this->t->set_var('lang_done','Cancel');
739           $this->t->set_var('lang_action','Adding New Activity');
740           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
741                                                                 . $activity['activity'] . '&action=' . 'insert'));
742         }
743
744       if($action == 'edit')
745         {
746           $sql = "SELECT * FROM eq_activity WHERE activity=" . $activity['activity'];
747           $this->db->query($sql,__LINE__,__FILE__);
748           $this->db->next_record();
749           $this->t->set_var('cal_date',$this->jscal->input('date',$this->db->f('date'),'','','','','',$this->cal_options));
750           $this->t->set_var('name', $this->db->f('name'));
751           $this->t->set_var('date', $this->db->f('date'));
752           $this->t->set_var('notes', $this->db->f('notes'));
753           $this->t->set_var('lang_done','Cancel');
754           $this->t->set_var('lang_action','Editing Activity');
755           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
756                                                                 . $activity['activity'] . '&action=' . 'save'));
757
758         }
759
760       $sql = "SELECT * FROM eq_elder";
761       $this->db->query($sql,__LINE__,__FILE__);
762       $i=0;
763       while ($this->db->next_record())
764         {
765           if($this->db->f('valid') == 1 || $action != 'add') {
766             $elder_name[$i] = $this->db->f('name');
767             $elder_id[$i] = $this->db->f('elder');
768             $elder_valid[$i] = $this->db->f('valid');
769             $i++;
770           }
771         }
772       array_multisort($elder_name, $elder_id, $elder_valid);
773
774       $j=0;
775       for ($i=0; $i < count($elder_id); $i++)
776         {
777           //$this->nextmatchs->template_alternate_row_color(&$this->t);
778           $sql = "SELECT * FROM eq_participation where activity=". $activity['activity'] . " AND elder=" . $elder_id[$i];
779           $this->db->query($sql,__LINE__,__FILE__);
780           if($this->db->next_record()) { $this->t->set_var('checked','checked'); $checked=1; }
781           else { $this->t->set_var('checked',''); $checked=0; }
782           if($checked || $elder_valid[$i] == 1) {
783             $this->t->set_var('elder_name',$elder_name[$i]);
784             $this->t->set_var('elder',$elder_id[$i]);
785             if(($j+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
786             else { $this->t->set_var('table_sep',"</td>"); }
787             if(($j) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
788             $this->t->fp('list','elder_list',True);
789             $j++;
790           }
791         }
792       
793       $this->t->set_var('lang_reset','Clear Form');
794       $this->t->set_var('lang_add','Add Activity');
795       $this->t->set_var('lang_save','Save Changes');
796       $this->t->set_var('edithandle','');
797       $this->t->set_var('addhandle','');
798       
799       $this->t->pfp('out','form');
800       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
801       if($action == 'add') { $this->t->pfp('addhandle','add'); }
802       
803       $this->save_sessiondata();
804     }
805
806   function par_view()
807     {
808       $this->t->set_file(array('par_view_t' => 'par_view.tpl'));
809       $this->t->set_block('par_view_t','header_list','list1');
810       $this->t->set_block('par_view_t','elder_list','list2');
811
812       $sql = "SELECT * FROM eq_elder where valid=1";
813       $this->db->query($sql,__LINE__,__FILE__);
814       $i=0;
815       while ($this->db->next_record())
816         {
817           $elder_name[$i] = $this->db->f('name');
818           $elder_id[$i] = $this->db->f('elder');
819           $i++;
820         }
821       array_multisort($elder_name, $elder_id);
822
823       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
824       $this->db->query($sql,__LINE__,__FILE__);
825       $total_records = $this->db->num_rows();
826
827       $i = 0;
828       while ($this->db->next_record())
829         {
830           $activity_list[$i]['name'] = $this->db->f('name');
831           $activity_list[$i]['date'] = $this->db->f('date');
832           $activity_list[$i]['activity']  = $this->db->f('activity');
833           if($activity_list[$i]['date'] == '0000-00-00') { $activity_list[$i]['date']=""; }
834           $i++;
835         }
836
837       $elder_width=250; $part_width=25; $act_width=50;
838       $total_width=$elder_width+$part_width;
839       for ($i=0; $i < count($activity_list); $i++) {
840         $this->t->set_var('activity_name',$activity_list[$i]['name']);
841         $this->t->set_var('activity_date',$activity_list[$i]['date']);
842         $this->t->fp('list1','header_list',True);
843         $total_width += $act_width;
844       }
845
846       for ($i=0; $i < count($elder_id); $i++) {
847         $participated=0; $part_table = ''; 
848         $this->nextmatchs->template_alternate_row_color(&$this->t);
849         $this->t->set_var('elder_name',$elder_name[$i]);
850         for ($j=0; $j < count($activity_list); $j++) {
851           $sql = "SELECT * FROM eq_participation where activity="
852              . $activity_list[$j]['activity'] . " AND elder=" . $elder_id[$i];
853           $this->db->query($sql,__LINE__,__FILE__);
854           if($this->db->next_record()) {
855             $part_table .= '<td align=center><img src="checkmark.gif"></td>';
856             $participated++;
857           } else {
858             $part_table .= '<td>&nbsp;</td>';
859           }
860         }
861         if($participated) { $part_table .= '<td align=center><img src="checkmark.gif">'.$participated.'</td>'; }
862         else { $part_table .= '<td>&nbsp;</td>'; }
863         $this->t->set_var('part_table',$part_table);
864         $this->t->fp('list2','elder_list',True);
865       }
866       $this->t->set_var('total_width',$total_width);
867       $this->t->set_var('elder_width',$elder_width);
868       $this->t->set_var('part_width',$part_width);
869       $this->t->set_var('act_width',$act_width);
870       $this->t->pfp('out','par_view_t');
871       $this->save_sessiondata(); 
872     }
873   
874   function ppi_sched()
875     {
876       $this->t->set_file(array('ppi_sched_t' => 'ppi_sched.tpl'));
877       $this->t->set_block('ppi_sched_t','elder_list','elderlist');
878       $this->t->set_block('ppi_sched_t','appt_list','apptlist');
879       $action = get_var('action',array('GET','POST'));
880
881       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
882       $this->t->set_var('lang_reset','Clear Changes');
883       
884       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
885       $this->t->set_var('ppi_link_title','Yearly PPIs');
886       
887       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
888       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
889
890       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched&action=save'));
891       $this->t->set_var('title','Yearly PPI Scheduler');
892
893       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
894       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
895       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
896       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
897       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
898       $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
899       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
900       $table_data=""; $completed_data=""; $totals_data="";
901
902       $year = date('Y');
903       
904       if($action == 'save')
905         {
906           // Save any changes made to the appointment table
907           $new_data = get_var('appt_notes',array('POST'));
908           foreach ($new_data as $entry)
909            {
910              $elder = $entry['elder'];
911              $appointment = $entry['appointment'];
912
913              //print "elder: $elder appointment: $appointment <br>";
914              
915              // Perform database save actions here
916              $this->db->query("UPDATE eq_appointment set " .
917                               " elder='" . $elder . "'" .
918                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
919
920            }
921           
922           // Save any changes made to the ppi notes table
923           $new_data = get_var('ppi_notes',array('POST'));
924           foreach ($new_data as $entry)
925            {
926              $ppi_notes = $entry['notes'];
927              $elder_id = $entry['elder_id'];
928              $ppi_pri = $entry['pri'];
929              
930              // Perform database save actions here
931              $this->db->query("UPDATE eq_elder set " .
932                               " ppi_notes='" . $ppi_notes . "'" .
933                               ",ppi_pri='" . $ppi_pri . "'" .
934                               " WHERE elder=" . $elder_id,__LINE__,__FILE__);
935              
936            }
937
938           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched');
939           Header('Location: ' . $take_me_to_url);
940         }
941
942       // Get the EQ President
943       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
944       $this->db->query($sql,__LINE__,__FILE__);
945       if($this->db->next_record()) {
946         $president_name = $this->db->f('name');
947         $president_id = $this->db->f('elder');
948         $presidency_id = $this->db->f('presidency');
949         $district_number = '*';
950         $district_name = $president_name;
951       } else {
952         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
953         return;
954       }
955       
956       // create the elder id -> elder name mapping
957       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
958       $this->db->query($sql,__LINE__,__FILE__);
959       $i=0;
960       $elder_id = NULL;
961       $elder_name = NULL;
962       while ($this->db->next_record())
963         {
964           $elder_name[$i] = $this->db->f('name');
965           $elder_id[$i] = $this->db->f('elder');
966           $i++;
967         }
968       array_multisort($elder_name, $elder_id);
969
970       // APPOINTMENT TABLE
971       $date_width=150; $time_width=100; $elder_width=200;
972       $appt_table_width=$date_width + $time_width + $elder_width;
973       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
974       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
975       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
976       $appt_table_data = ""; 
977
978       $total_elders=0; $elders_with_yearly_ppi=0;
979       
980       // Display a scheduling table for the EQ President
981       $table_data=""; $appt_table_data="";
982       $table_title = "District ".$district_number.": ".$district_name.": All Elders with Yearly PPI Not Completed";
983       $appt_table_title = "District ".$district_number.": ".$district_name.": Yearly PPI Appointment Slots";
984       $this->t->set_var('table_title',$table_title);
985       $this->t->set_var('appt_table_title',$appt_table_title);
986       
987       // query the database for all the appointments
988       $sql = "SELECT * FROM eq_appointment where presidency=".$presidency_id." and date>=CURDATE() ORDER BY date ASC, time ASC";
989       $this->db->query($sql,__LINE__,__FILE__);
990         
991       while ($this->db->next_record())
992         {
993           $appointment = $this->db->f('appointment');
994           $elder = $this->db->f('elder');
995           
996           $date = $this->db->f('date');
997           $date_array = explode("-",$date);
998           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
999           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1000           
1001           $time = $this->db->f('time');
1002           $time_array = explode(":",$time);
1003           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1004           
1005           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1006           $appt_table_data.= "<td align=center>$day_string</td>";
1007           $appt_table_data.= "<td align=center>$time_string</td>";
1008
1009           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1010           $appt_table_data.= '<option value=0></option>';
1011           for ($i=0; $i < count($elder_id); $i++) {
1012             $id = $elder_id[$i];
1013             $name = $elder_name[$i];
1014             if($elder_id[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1015             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1016           }
1017           $appt_table_data.='</select></td>';
1018
1019           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1020           
1021           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1022           $this->t->set_var('tr_color',$tr_color);
1023         }
1024
1025       $this->t->set_var('appt_table_data',$appt_table_data);
1026       $this->t->set_var('appt_header_row',$appt_header_row);
1027       $this->t->set_var('appt_table_width',$appt_table_width);
1028
1029       // PPI SCHEDULING TABLE
1030       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY ppi_pri ASC";
1031       $this->db->query($sql,__LINE__,__FILE__);
1032       
1033       $i=0; 
1034       $elder_id = NULL;
1035       while ($this->db->next_record())
1036         {
1037           $elder_id[$i] = $this->db->f('elder');
1038           $elder_name[$i] = $this->db->f('name');
1039           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1040           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1041           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1042           $i++;
1043           $total_elders++;
1044         }
1045
1046       $max = count($elder_id);
1047       
1048       for($i=0; $i < $max; $i++) {
1049           $id = $elder_id[$i];
1050           $name = $elder_name[$i];
1051           $phone = $elder_phone[$id];
1052           $ppi_pri = $elder_ppi_pri[$id];
1053           $ppi_notes = $elder_ppi_notes[$id];
1054
1055           // If this elder has had a yearly PPI this year, don't show him on the schedule list
1056           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1057           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1058              "AND elder=" . $id;
1059           $this->db2->query($sql,__LINE__,__FILE__);
1060           
1061           if(!$this->db2->next_record()) {
1062             $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1063             $this->db->query($sql,__LINE__,__FILE__);
1064             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1065             $link_data['menuaction'] = 'eq.eq.ppi_update';
1066             $link_data['elder'] = $id;
1067             $link_data['name'] = $name;
1068             $link_data['ppi'] = '';
1069             $link_data['eqpresppi'] = 1;
1070             $link_data['action'] = 'add';
1071             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1072             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1073             $this->t->set_var('tr_color',$tr_color);
1074             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1075             $table_data.= "<td align=center>$phone</td>";
1076             //$table_data.= "<td align=center>$ppi_pri</td>";
1077             $table_data.= "<td align=center>";
1078             $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
1079             foreach(range(0,6) as $num) {
1080               if($num == 0) { $num = 1; } else {$num = $num*5; }
1081               if($ppi_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1082               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1083             }
1084             $table_data.= '</select></td>';
1085             $table_data.= "<td align=center>$date</td>";
1086             $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
1087             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_id]" value="'.$id.'">';
1088             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_name]" value="'.$name.'">';
1089             $table_data.= '</td>';
1090             $table_data.= '</tr>';
1091           } else {
1092             $link_data['menuaction'] = 'eq.eq.ppi_update';
1093             $link_data['interviewer'] = $this->db2->f('interviewer');
1094             $link_data['elder'] = $this->db2->f('elder');
1095             $link_data['name'] = $name;
1096             $link_data['ppi'] = $this->db2->f('ppi');
1097             $link_data['eqpresppi'] = $this->db2->f('eqpresppi');
1098             $link_data['action'] = 'view';
1099             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1100             $elders_with_yearly_ppi++;
1101             $date = $this->db2->f('date');
1102             $ppi_notes = $this->db2->f('notes');
1103             if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
1104             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1105             $this->t->set_var('tr_color2',$tr_color2);
1106             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1107             $completed_data.= "<td align=center>$phone</td>";
1108             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1109             $completed_data.= "<td align=left>$ppi_notes</td>";
1110             $completed_data.= '</tr>';
1111           }
1112       } // End for Elders Loop
1113
1114       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1115       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1116       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1117       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1118       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1119       $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1120             
1121       $this->t->set_var('table_width',$table_width);
1122       $this->t->set_var('header_row',$header_row);
1123       $this->t->set_var('table_data',$table_data);
1124       $this->t->set_var('completed_header_row',$completed_header_row);
1125       $this->t->set_var('completed_table_width',$completed_table_width);
1126       $this->t->set_var('completed',$completed_data);
1127       $this->t->fp('elderlist','elder_list',True); 
1128       
1129       $elders_width=300; $totals_width=100;
1130       $totals_table_width=$elders_width + $totals_width;
1131       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1132       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1133       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1134       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
1135       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
1136       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
1137       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1138       $this->t->set_var('tr_color',$tr_color);
1139       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1140       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1141       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1142       $totals_data.= "</tr>";
1143
1144       $this->t->set_var('totals',$totals_data);
1145       $this->t->set_var('totals_header_row',$totals_header_row);
1146       $this->t->set_var('totals_table_width',$totals_table_width);
1147       
1148       $this->t->pfp('out','ppi_sched_t');
1149       $this->save_sessiondata(); 
1150       
1151     }
1152   
1153   function int_sched()
1154     {
1155       $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1156       $this->t->set_block('int_sched_t','elder_list','elderlist');
1157       $this->t->set_block('int_sched_t','appt_list','apptlist');
1158       $action = get_var('action',array('GET','POST'));
1159
1160       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1161       $this->t->set_var('lang_reset','Clear Changes');
1162       
1163       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
1164       $this->t->set_var('int_link_title','Hometeaching Interviews');
1165       
1166       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
1167       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
1168
1169       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched&action=save'));
1170       $this->t->set_var('title','Hometeaching Interviews Scheduler');
1171
1172       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
1173       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $int_date_width;
1174       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1175       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1176       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1177       $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
1178       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1179       $table_data=""; $completed_data=""; $totals_data="";
1180
1181       $year = date('Y');
1182       $month = date('m');
1183       $nextyear = $year + 1;
1184       if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
1185       if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
1186       if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
1187       if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
1188       //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
1189       
1190       if($action == 'save')
1191         {
1192           // Save any changes made to the appointment table
1193           $new_data = get_var('appt_notes',array('POST'));
1194           foreach ($new_data as $entry)
1195            {
1196              $elder = $entry['elder'];
1197              $appointment = $entry['appointment'];
1198
1199              //print "elder: $elder appointment: $appointment <br>";
1200              
1201              // Perform database save actions here
1202              $this->db->query("UPDATE eq_appointment set " .
1203                               " elder='" . $elder . "'" .
1204                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1205
1206            }
1207           
1208           // Save any changes made to the int notes table
1209           $new_data = get_var('int_notes',array('POST'));
1210           foreach ($new_data as $entry)
1211            {
1212              $int_notes = $entry['notes'];
1213              $elder_id = $entry['elder_id'];
1214              $elder_name = $entry['elder_name'];
1215              $int_pri = $entry['pri'];
1216              $aaronic = $entry['aaronic'];
1217              if($aaronic == 0) { 
1218                // Perform database save actions here
1219                $this->db->query("UPDATE eq_elder set " .
1220                                 " int_notes='" . $int_notes . "'" .
1221                                 ",int_pri='" . $int_pri . "'" .
1222                                 " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1223              } 
1224              
1225            }
1226
1227           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched');
1228           Header('Location: ' . $take_me_to_url);
1229         }
1230
1231       // Get the Districts
1232       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1233       $this->db->query($sql,__LINE__,__FILE__);
1234       $i=0;
1235       while ($this->db->next_record())
1236         {
1237           $district = $this->db->f('district');
1238           $districts[$i]['district'] = $this->db->f('district');
1239           $districts[$i]['name'] = $this->db->f('name');
1240           $districts[$i]['supervisor'] = $this->db->f('supervisor');
1241           $sql = "SELECT * FROM eq_presidency where district=$district and valid=1";
1242           $this->db2->query($sql,__LINE__,__FILE__);
1243           if($this->db2->next_record()) {
1244             $districts[$i]['presidency'] = $this->db2->f('presidency');
1245           }
1246           $i++;
1247         }
1248       
1249       // create the elder id -> elder name mapping
1250       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1251       $this->db->query($sql,__LINE__,__FILE__);
1252       $i=0;
1253       $elder_id_data = NULL;
1254       $elder_name_data = NULL;
1255       while ($this->db->next_record())
1256         {
1257           $elder_name_data[$i] = $this->db->f('name');
1258           $elder_id_data[$i] = $this->db->f('elder');
1259           $i++;
1260         }
1261       array_multisort($elder_name_data, $elder_id_data);
1262
1263       // APPOINTMENT TABLE
1264       $district = 1;
1265       $date_width=150; $time_width=100; $elder_width=200;
1266       $appt_table_width=$date_width + $time_width + $elder_width;
1267       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1268       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1269       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1270       $appt_table_data = ""; 
1271
1272       $total_comps=0; $comps_with_quarterly_int=0;
1273       
1274       // Display a scheduling table for each district
1275       for ($d=0; $d < count($districts); $d++) {
1276       $table_data=""; $appt_table_data="";
1277       $this->t->set_var('district_number',$districts[$d]['district']);
1278       $this->t->set_var('district_name',$districts[$d]['name']);        
1279       $supervisor = $districts[$d]['supervisor'];
1280       $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Elders with Interviews Not Completed";
1281       $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
1282       $this->t->set_var('table_title',$table_title);
1283       $this->t->set_var('appt_table_title',$appt_table_title);
1284       
1285       // query the database for all the appointments
1286       $sql = "SELECT * FROM eq_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1287       $this->db->query($sql,__LINE__,__FILE__);
1288         
1289       while ($this->db->next_record())
1290         {
1291           $appointment = $this->db->f('appointment');
1292           $elder = $this->db->f('elder');
1293           
1294           $date = $this->db->f('date');
1295           $date_array = explode("-",$date);
1296           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1297           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1298           
1299           $time = $this->db->f('time');
1300           $time_array = explode(":",$time);
1301           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1302           
1303           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1304           $appt_table_data.= "<td align=center>$day_string</td>";
1305           $appt_table_data.= "<td align=center>$time_string</td>";
1306
1307           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1308           $appt_table_data.= '<option value=0></option>';
1309           for ($i=0; $i < count($elder_id_data); $i++) {
1310             $id = $elder_id_data[$i];
1311             $name = $elder_name_data[$i];
1312             if($elder_id_data[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1313             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1314           }
1315           $appt_table_data.='</select></td>';
1316
1317           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1318           
1319           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1320           $this->t->set_var('tr_color',$tr_color);
1321         }
1322
1323       $this->t->set_var('appt_table_data',$appt_table_data);
1324       $this->t->set_var('appt_header_row',$appt_header_row);
1325       $this->t->set_var('appt_table_width',$appt_table_width);
1326
1327       // INTERVIEW SCHEDULING TABLE
1328       
1329       // Select all the unique companionship numbers for this district
1330       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$d]['district'];
1331       $this->db->query($sql,__LINE__,__FILE__);
1332       $j=0;
1333       while ($this->db->next_record())
1334         {
1335           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1336           $j++;
1337         }
1338
1339       $i=0;
1340       for ($j=0; $j < count($unique_companionships); $j++) {
1341         // Select all the companions from each companionship
1342         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
1343            "companionship=". $unique_companionships[$j]['companionship'];
1344         $this->db->query($sql,__LINE__,__FILE__);
1345         $k=0; $int_completed=0;
1346         $comp = $unique_companionships[$j]['companionship'];
1347         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1348         $this->t->set_var('tr_color',$tr_color);
1349         $total_comps++;
1350         while ($this->db->next_record())
1351             {         
1352               // Get this companions information
1353               $elder_id = $this->db->f('elder');
1354               $aaronic_id = $this->db->f('aaronic');
1355              
1356               $sql = "SELECT * FROM eq_elder where elder=$elder_id";
1357               $this->db2->query($sql,__LINE__,__FILE__);        
1358               if($this->db2->next_record())
1359                 {
1360                   $elder_id = $this->db2->f('elder');
1361                   $elder_name = $this->db2->f('name');
1362                   $elder_phone[$elder_id] = $this->db2->f('phone');
1363                   $elder_int_pri[$elder_id] = $this->db2->f('int_pri');
1364                   $elder_int_notes[$elder_id] = $this->db2->f('int_notes');
1365                   $elder_aaronic = 0;
1366                 }
1367               else {
1368                 $sql = "SELECT * FROM eq_aaronic where aaronic=$aaronic_id";
1369                 $this->db2->query($sql,__LINE__,__FILE__);
1370                 if($this->db2->next_record())
1371                   {
1372                     $elder_id = $this->db2->f('aaronic');
1373                     $elder_name = $this->db2->f('name');
1374                     $elder_phone[$elder_id] = $this->db2->f('phone');
1375                     $elder_aaronic = 1;
1376                   }
1377               }
1378
1379               $id = $elder_id;
1380               $name = $elder_name;
1381               $phone = $elder_phone[$id];
1382               $int_pri = $elder_int_pri[$id];
1383               $int_notes = $elder_int_notes[$id];
1384               $aaronic = $elder_aaronic;
1385               
1386               // If the companionship has already had its quarterly interview,
1387               // Skip the other companion in the companionship.
1388               if($int_completed == 1) {
1389                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1390                 $completed_data.= "<td align=center>$phone</td>";
1391                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1392                 $completed_data.= "<td align=left>$int_notes</td>";
1393                 $completed_data.= '</tr>';
1394                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1395                 $this->t->set_var('tr_color2',$tr_color2);
1396                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1397                 $this->t->set_var('tr_color',$tr_color);
1398                 continue;
1399               }
1400               
1401               // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
1402               $sql = "SELECT * FROM eq_interview WHERE date >= '$quarter_start' AND date < '$quarter_end' ".
1403                  "AND elder=" . $id;
1404               $this->db2->query($sql,__LINE__,__FILE__);
1405               
1406               if(!$this->db2->next_record()) {
1407                 $sql = "SELECT * FROM eq_interview WHERE elder=" . $id . " ORDER BY date DESC";
1408                 $this->db3->query($sql,__LINE__,__FILE__);
1409                 if($this->db3->next_record()) { $date = $this->db3->f('date'); } else { $date = ""; }
1410                 $link_data['menuaction'] = 'eq.eq.int_update';
1411                 $link_data['elder'] = $id;
1412                 $link_data['aaronic'] = 0;
1413                 $link_data['name'] = $name;
1414                 $link_data['interview'] = '';
1415                 $link_data['action'] = 'add';
1416                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1417                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1418                 $table_data.= "<td align=center>$phone</td>";
1419                 $table_data.= "<td align=center>";
1420                 $table_data.= '<select name=int_notes['.$i.'][pri]>';
1421                 foreach(range(0,6) as $num) {
1422                   if($num == 0) { $num = 1; } else {$num = $num*5; }
1423                   if($int_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1424                   $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1425                 }
1426                 $table_data.= '</select></td>';
1427                 $table_data.= "<td align=center>$date</td>";
1428                 $table_data.= '<td><input type=text size="50" maxlength="128" name="int_notes['.$i.'][notes]" value="'.$int_notes.'">';
1429                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_id]" value="'.$id.'">';
1430                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_name]" value="'.$name.'">';
1431                 $table_data.= '<input type=hidden name="int_notes['.$i.'][aaronic]" value="'.$aaronic.'">';
1432                 $table_data.= '</td>';
1433                 $table_data.= '</tr>';
1434                 $i++;
1435               } else {
1436                 $link_data['menuaction'] = 'eq.eq.int_update';
1437                 $link_data['interviewer'] = $this->db2->f('interviewer');
1438                 $link_data['elder'] = $this->db2->f('elder');
1439                 $link_data['aaronic'] = $this->db2->f('aaronic');
1440                 $link_data['name'] = $name;
1441                 $link_data['interview'] = $this->db2->f('int');
1442                 $link_data['action'] = 'view';
1443                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1444                 $comps_with_quarterly_int++;
1445                 $int_completed=1;
1446                 $date = $this->db2->f('date');
1447                 $int_notes = $this->db2->f('notes');
1448                 if(strlen($int_notes) > 40) { $int_notes = substr($int_notes,0,40) . "..."; }
1449                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1450                 $completed_data.= "<td align=center>$phone</td>";
1451                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1452                 $completed_data.= "<td align=left>$int_notes</td>";
1453                 $completed_data.= '</tr>';
1454               }
1455             }
1456       }
1457         
1458       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1459       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1460       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1461       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1462       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1463       $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
1464             
1465       $this->t->set_var('table_width',$table_width);
1466       $this->t->set_var('header_row',$header_row);
1467       $this->t->set_var('table_data',$table_data);
1468       $this->t->set_var('completed_header_row',$completed_header_row);
1469       $this->t->set_var('completed_table_width',$completed_table_width);
1470       $this->t->set_var('completed',$completed_data);
1471       $this->t->fp('elderlist','elder_list',True);
1472
1473       } // End for each district loop
1474
1475       
1476       $elders_width=300; $totals_width=100;
1477       $totals_table_width=$elders_width + $totals_width;
1478       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1479       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1480       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1481       $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
1482       $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
1483       $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
1484       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1485       $this->t->set_var('tr_color',$tr_color);
1486       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1487       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1488       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1489       $totals_data.= "</tr>";
1490
1491       $this->t->set_var('totals',$totals_data);
1492       $this->t->set_var('totals_header_row',$totals_header_row);
1493       $this->t->set_var('totals_table_width',$totals_table_width);
1494       
1495       $this->t->pfp('out','int_sched_t');
1496       $this->save_sessiondata(); 
1497       
1498     }
1499   
1500   function vis_sched()
1501     {
1502       $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
1503       $this->t->set_block('vis_sched_t','family_list','familylist');
1504       $this->t->set_block('vis_sched_t','appt_list','apptlist');
1505       $action = get_var('action',array('GET','POST'));
1506
1507       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1508       $this->t->set_var('lang_reset','Clear Changes');
1509       
1510       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
1511       $this->t->set_var('vis_link_title','View Yearly Visits');
1512       
1513       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
1514       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
1515
1516       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched&action=save'));
1517       $this->t->set_var('title','EQ Presidency Yearly Visit Scheduler');
1518
1519       $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
1520       $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
1521       $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
1522       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1523       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1524       $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
1525       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1526       $table_data=""; $completed_data=""; $totals_data="";
1527
1528       $year = date('Y');
1529       
1530       if($action == 'save')
1531         {
1532           // Save any changes made to the appointment table
1533           $new_data = get_var('appt_notes',array('POST'));
1534           foreach ($new_data as $entry)
1535            {
1536              $family = $entry['family'];
1537              $appointment = $entry['appointment'];
1538
1539              // Perform database save actions here
1540              $this->db->query("UPDATE eq_appointment set " .
1541                               " family='" . $family . "'" .
1542                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1543
1544            }
1545           
1546           // Save any changes made to the visit notes table
1547           $new_data = get_var('vis_notes',array('POST'));
1548           foreach ($new_data as $entry)
1549            {
1550              $visit_notes = $entry['notes'];
1551              $family_id = $entry['family_id'];
1552              $visit_pri = $entry['pri'];
1553              
1554              // Perform database save actions here
1555              $this->db->query("UPDATE eq_family set " .
1556                               " visit_notes='" . $visit_notes . "'" .
1557                               ",visit_pri='" . $visit_pri . "'" .
1558                               " WHERE family=" . $family_id,__LINE__,__FILE__);
1559              
1560            }
1561
1562           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched');
1563           Header('Location: ' . $take_me_to_url);
1564         }
1565
1566       // APPOINTMENT TABLE
1567       $date_width=150; $time_width=100; $family_width=250;
1568       $appt_table_width=$date_width + $time_width + $family_width;
1569       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1570       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1571       $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
1572       $appt_table_data = ""; 
1573
1574       // Find out what the EQ Presidency ID is
1575       $sql = "SELECT * FROM eq_presidency where president=0 and counselor=0 and secretary=0 and valid=1";
1576       $this->db->query($sql,__LINE__,__FILE__);
1577       if($this->db->next_record()) {
1578         $presidency_name = $this->db->f('name');
1579         $presidency_id = $this->db->f('presidency');
1580       } else {
1581         print "<hr><font color=red><h3>-E- Unable to locate EQ Presidency in eq_presidency table</h3></font></hr>";
1582         return;
1583       }
1584       
1585       // create the family id -> family name mapping
1586       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
1587       $this->db->query($sql,__LINE__,__FILE__);
1588       $i=0;
1589       $family_id = NULL;
1590       while ($this->db->next_record())
1591         {
1592           $family_id[$i] = $this->db->f('family');
1593           $family_name[$i] = $this->db->f('name');
1594           $i++;
1595         }
1596       array_multisort($family_name, $family_id);
1597       
1598       // query the database for all the appointments
1599       $sql = "SELECT * FROM eq_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
1600       $this->db->query($sql,__LINE__,__FILE__);
1601
1602       while ($this->db->next_record())
1603         {
1604           $appointment = $this->db->f('appointment');
1605           $family = $this->db->f('family');
1606
1607           $date = $this->db->f('date');
1608           $date_array = explode("-",$date);
1609           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1610           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1611           
1612           $time = $this->db->f('time');
1613           $time_array = explode(":",$time);
1614           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1615           
1616           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1617           $appt_table_data.= "<td align=center>$day_string</td>";
1618           $appt_table_data.= "<td align=center>$time_string</td>";
1619
1620           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
1621           $appt_table_data.= '<option value=0></option>';
1622           for ($i=0; $i < count($family_id); $i++) {
1623             $id = $family_id[$i];
1624             $name = $family_name[$i];
1625             if($family_id[$i] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1626             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
1627           }
1628           $appt_table_data.='</select></td>';
1629
1630           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1631           
1632           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1633           $this->t->set_var('tr_color',$tr_color);
1634         }
1635
1636       $this->t->set_var('appt_table_data',$appt_table_data);
1637       $this->t->set_var('appt_header_row',$appt_header_row);
1638       $this->t->set_var('appt_table_width',$appt_table_width);
1639
1640       
1641       // VISIT SCHEDULING TABLE
1642       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY visit_pri ASC";
1643       $this->db->query($sql,__LINE__,__FILE__);
1644
1645       $total_families=0; $families_with_yearly_visit=0;
1646       
1647       $i=0; 
1648       $family_id = NULL;
1649       $family_name = NULL;
1650       $family_phone = NULL;
1651       $family_visit_pri = NULL;
1652       $family_visit_notes = NULL;
1653       while ($this->db->next_record())
1654         {
1655           $family_id[$i] = $this->db->f('family');
1656           $family_name[$i] = $this->db->f('name');
1657           $family_phone[$family_id[$i]] = $family_id[$i] . " ERROR";
1658           $family_visit_pri[$family_id[$i]] = $this->db->f('visit_pri');
1659           $family_visit_notes[$family_id[$i]] = $this->db->f('visit_notes');
1660           $i++;
1661           $total_families++;
1662         }
1663
1664       $sql = "SELECT * FROM eq_parent where valid=1";
1665       $this->db->query($sql,__LINE__,__FILE__);
1666       while ($this->db->next_record())
1667         {
1668           $family = $this->db->f('family');
1669           $phone = $this->db->f('phone');
1670           $family_phone[$family] = $phone;
1671         }
1672       
1673       $max = count($family_id);
1674       
1675       for($i=0; $i < $max; $i++) {
1676           $id = $family_id[$i];
1677           $name = $family_name[$i];
1678           $phone = $family_phone[$id];
1679           $vis_pri = $family_visit_pri[$id];
1680           $vis_notes = $family_visit_notes[$id];
1681
1682           // If this family has had a yearly visit this year, don't show them on the schedule list
1683           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1684           $sql = "SELECT * FROM eq_visit WHERE date > '$year_start' AND date < '$year_end' ".
1685              "AND family=" . $id . " AND companionship=0";
1686           $this->db2->query($sql,__LINE__,__FILE__);
1687           
1688           if(!$this->db2->next_record()) {
1689             $sql = "SELECT * FROM eq_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
1690             $this->db->query($sql,__LINE__,__FILE__);
1691             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1692             $link_data['menuaction'] = 'eq.eq.vis_update';
1693             $link_data['visit'] = '';
1694             $link_data['family'] = $id;
1695             $link_data['name'] = $name;
1696             $link_data['action'] = 'add';
1697             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1698             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
1699             $table_data.= "<td align=center>$phone</td>";
1700             $table_data.= "<td align=center>";
1701             $table_data.= '<select name=vis_notes['.$i.'][pri]>';
1702             foreach(range(0,6) as $num) {
1703               if($num == 0) { $num = 1; } else {$num = $num*5; }
1704               if($vis_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1705               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1706             }
1707             $table_data.= '</select></td>';
1708             $table_data.= "<td align=center>$date</td>";
1709             $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$i.'][notes]" value="'.$vis_notes.'">';
1710             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_id]" value="'.$id.'">';
1711             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_name]" value="'.$name.'">';
1712             $table_data.= '</td>';
1713             $table_data.= '</tr>';
1714             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1715             $this->t->set_var('tr_color',$tr_color);
1716           } else {
1717             $link_data['menuaction'] = 'eq.eq.vis_update';
1718             $link_data['visit'] = $this->db2->f('visit');
1719             $link_data['family'] = $this->db2->f('family');
1720             $link_data['name'] = $name;
1721             $link_data['date'] = $this->db2->f('date');
1722             $link_data['action'] = 'view';
1723             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1724             $families_with_yearly_visit++;
1725             $date = $this->db2->f('date');
1726             $vis_notes = $this->db2->f('notes');
1727             if(strlen($vis_notes) > 40) { $vis_notes = substr($vis_notes,0,40) . "..."; }
1728             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
1729             $completed_data.= "<td align=center>$phone</td>";
1730             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1731             $completed_data.= "<td align=left>$vis_notes</td>";
1732             $completed_data.= '</tr>';
1733             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1734             $this->t->set_var('tr_color2',$tr_color2);
1735           }
1736       }
1737
1738       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1739       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1740       $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
1741       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1742       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1743       $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
1744       
1745       $family_width=300; $totals_width=100;
1746       $totals_table_width=$family_width + $totals_width;
1747       $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
1748       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1749       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1750       $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
1751       $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
1752       $percent = ceil(($families_with_yearly_visit / $total_families)*100);
1753       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1754       $this->t->set_var('tr_color',$tr_color);
1755       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1756       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1757       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1758       $totals_data.= "</tr>";
1759       
1760       $this->t->set_var('table_width',$table_width);
1761       $this->t->set_var('header_row',$header_row);
1762       $this->t->set_var('table_data',$table_data);
1763       $this->t->set_var('totals_header_row',$totals_header_row);
1764       $this->t->set_var('totals_table_width',$totals_table_width);
1765       $this->t->set_var('completed_header_row',$completed_header_row);
1766       $this->t->set_var('completed_table_width',$completed_table_width);
1767       $this->t->set_var('completed',$completed_data);
1768       $this->t->set_var('totals',$totals_data);
1769       $this->t->fp('familylist','family_list',True);
1770       $this->t->fp('apptlist','appt_list',True);
1771       
1772       $this->t->pfp('out','vis_sched_t');
1773       $this->save_sessiondata(); 
1774       
1775     }
1776   
1777   function ppi_view()
1778     {
1779       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
1780       $this->t->set_block('ppi_view_t','district_list','list');
1781
1782       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1783       $num_months = get_var('num_months',array('GET','POST'));
1784       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
1785       $this->t->set_var('num_months',$num_months);
1786       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
1787       else {  $this->t->set_var('lang_num_months','Months of History'); }
1788       $this->t->set_var('lang_filter','Filter');
1789       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1790             
1791       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1792       $this->t->set_var('ppi_link_title','Yearly PPIs'); 
1793
1794       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1795       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1796       
1797       $this->t->set_var('title','Yearly PPIs');
1798       $num_months = get_var('num_months',array('GET','POST'));
1799       if($num_months == '') { $num_months = $this->default_ppi_num_years; }
1800       $this->t->set_var('num_months',$num_months);
1801       if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
1802       else { $this->t->set_var('lang_num_months','Years of History'); } 
1803
1804       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
1805       $this->db->query($sql,__LINE__,__FILE__);
1806       if($this->db->next_record()) {
1807         $president_name = $this->db->f('name');
1808         $interviewer = $this->db->f('elder');
1809         $eqpresppi = 1;
1810       } else {
1811         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
1812         return;
1813       }
1814       $this->t->set_var('district_number','*');
1815       $this->t->set_var('district_name',$president_name);
1816
1817       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
1818       $this->db->query($sql,__LINE__,__FILE__);
1819       $i=0;
1820       while ($this->db->next_record())
1821         {
1822           $elder_id[$i] = $this->db->f('elder');
1823           $elder_name[$i] = $this->db->f('name');
1824           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1825           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1826           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1827           $i++;
1828         }
1829       $total_elders=$i;
1830       array_multisort($elder_name, $elder_id);
1831       //var_dump($elder_name); print "<br><br>"; var_dump($elder_id);
1832       
1833       $header_row="<th width=$comp_width><font size=-2>Elder Name</th>";
1834         
1835       $elder_width=400; $ppi_width=75; $table_width=$elder_width + $num_months*$ppi_width;
1836       $table_data="";
1837       for($m=$num_months; $m >= 0; $m--) {
1838         $year = date('Y') - $m;
1839         $header_row .= "<th width=150><font size=-2>$year</th>"; 
1840         $ppis[$m] = 0;
1841       }
1842       
1843       for ($j=0; $j < count($elder_id); $j++) {
1844         $id = $elder_id[$j];
1845         $name = $elder_name[$j];
1846         $phone = $elder_phone[$id];
1847         
1848         $link_data['menuaction'] = 'eq.eq.ppi_update';
1849         $link_data['interviewer'] = $interviewer;
1850         $link_data['elder'] = $id;
1851         $link_data['name'] = $name;
1852         $link_data['ppi'] = '';
1853         $link_data['eqpresppi'] = $eqpresppi;
1854         $link_data['action'] = 'add';
1855         $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1856         $this->nextmatchs->template_alternate_row_color(&$this->t);
1857         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1858
1859         // Find out how many times PPIs were performed in the past $num_months for this Elder
1860         for($m=$num_months; $m >= 0; $m--) {
1861           $year = date('Y') - $m;
1862           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1863           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1864              "AND elder=" . $id;
1865           $this->db2->query($sql,__LINE__,__FILE__);
1866                   
1867           if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
1868           if($this->db2->next_record()) {
1869             $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
1870             $link_data['menuaction'] = 'eq.eq.ppi_update';
1871             $link_data['companionship'] = $companionship;
1872             $link_data['interviewer'] = $this->db2->f('interviewer');
1873             $link_data['elder'] = $id;
1874             $link_data['name'] = $name;
1875             $link_data['ppi'] = $this->db2->f('ppi');
1876             $link_data['eqpresppi'] = $eqpresppi;
1877             $link_data['action'] = 'view';
1878             $date = $this->db2->f('date');
1879             $date_array = explode("-",$date);
1880             $month = $date_array[1];
1881             $day   = $date_array[2];
1882             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1883             $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
1884           }
1885           else { $table_data .= "<td>&nbsp;</td>"; }
1886         }
1887         $table_data .= "</tr>\n"; 
1888       }
1889       $table_data .= "<tr><td colspan=20><hr></td></tr>";
1890       
1891       $stat_data = "<tr><td><b><font size=-2>$total_elders Elders<br>PPI Totals:</font></b></td>";
1892       for($m=$num_months; $m >=0; $m--) {
1893         $percent = ceil(($ppis[$m] / $total_elders)*100);
1894         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
1895       }
1896       $stat_data .= "</tr>";
1897       
1898       $this->t->set_var('table_width',$table_width);
1899       $this->t->set_var('header_row',$header_row);
1900       $this->t->set_var('table_data',$table_data);
1901       $this->t->set_var('stat_data',$stat_data);
1902       $this->t->pfp('out','ppi_view_t');
1903       $this->save_sessiondata(); 
1904     }
1905
1906   function ppi_update()
1907     {
1908       $this->t->set_file(array('form' => 'ppi_update.tpl'));
1909       $this->t->set_block('form','interviewer_list','int_list');
1910       $this->t->set_block('form','add','addhandle');
1911       $this->t->set_block('form','edit','edithandle');
1912       
1913       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1914       $this->t->set_var('readonly','');
1915       $this->t->set_var('disabled','');
1916       
1917       $action = get_var('action',array('GET','POST'));
1918       $companionship = get_var('companionship',array('GET','POST'));
1919       $interviewer = get_var('interviewer',array('GET','POST'));      
1920       $name = get_var('name',array('GET','POST'));
1921       $ppi = get_var('ppi',array('GET','POST'));
1922       $elder = get_var('elder',array('GET','POST'));
1923       $aaronic = get_var('aaronic',array('GET','POST'));
1924       $date = get_var('date',array('GET','POST'));
1925       $notes = get_var('notes',array('GET','POST'));
1926       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
1927       
1928       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1929       $this->db->query($sql,__LINE__,__FILE__);
1930       while ($this->db->next_record())
1931         {
1932           $supervisor = $this->db->f('supervisor');
1933           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
1934           $this->db2->query($sql,__LINE__,__FILE__);
1935           $this->db2->next_record();
1936           $interviewer_name = $this->db2->f('name');
1937           
1938           if($supervisor == $interviewer) { 
1939             $this->t->set_var('interviewer',$supervisor . ' selected');
1940           } else {
1941             $this->t->set_var('interviewer',$interviewer);
1942           }
1943           $this->t->set_var('interviewer_name',$interviewer_name);
1944           $this->t->fp('int_list','interviewer_list',True);
1945         }
1946       
1947       if($action == 'save')
1948         {
1949           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1950           $this->db->query("UPDATE eq_ppi set " .
1951                            "   ppi='" . $ppi . "'" .
1952                     ", interviewer='" . $interviewer . "'" .
1953                           ", elder='" . $elder . "'" .
1954                            ", date='" . $date . "'" .
1955                           ", notes='" . $notes . "'" .
1956                       ", eqpresppi='" . $eqpresppi . "'" .
1957                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
1958           $this->ppi_view();
1959           return false;
1960         }
1961
1962       if($action == 'insert')
1963         {
1964           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
1965           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,date,notes,eqpresppi) "
1966                            . "VALUES ('" . $interviewer . "','" . $elder . "','"
1967                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
1968           $this->ppi_view();
1969           return false;
1970         }
1971       
1972       if($action == 'add')
1973         {
1974           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
1975           $this->t->set_var('ppi', '');
1976           $this->t->set_var('interviewer', $interviewer);
1977           $this->t->set_var('name',$name);
1978           $this->t->set_var('elder',$elder);
1979           $this->t->set_var('date','');
1980           $this->t->set_var('notes','');
1981           $this->t->set_var('eqpresppi',$eqpresppi);
1982           $this->t->set_var('lang_done','Cancel');
1983           $this->t->set_var('lang_action','Adding New PPI');
1984           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
1985                                                                 . $ppi . '&action=' . 'insert'));
1986         }
1987
1988       if($action == 'edit' || $action == 'view')
1989         {
1990           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
1991           $this->db->query($sql,__LINE__,__FILE__);
1992           $this->db->next_record();
1993           $this->t->set_var('ppi',$ppi);
1994           $this->t->set_var('name',$name);
1995           $this->t->set_var('interviewer', $this->db->f('interviewer'));
1996           $this->t->set_var('elder',$this->db->f('elder'));
1997           $this->t->set_var('date',$this->db->f('date'));
1998           $this->t->set_var('notes',$this->db->f('notes'));
1999           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
2000         }
2001       
2002       if($action == 'edit')
2003         {
2004           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2005           $this->t->set_var('lang_done','Cancel');
2006           $this->t->set_var('lang_action','Editing PPI');
2007           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2008                                                                 . $ppi . '&action=' . 'save'));
2009         }
2010
2011       if($action == 'view')
2012         {
2013           $date = $this->db->f('date');
2014           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2015           $this->t->set_var('readonly','READONLY');
2016           $this->t->set_var('disabled','DISABLED');
2017           $this->t->set_var('lang_done','Done');
2018           $this->t->set_var('lang_action','Viewing PPI');
2019           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2020                                                                 . $ppi . '&action=' . 'edit'));
2021         }
2022       
2023       $this->t->set_var('lang_reset','Clear Form');
2024       $this->t->set_var('lang_add','Add PPI');
2025       $this->t->set_var('lang_save','Save Changes');
2026       $this->t->set_var('edithandle','');
2027       $this->t->set_var('addhandle','');
2028
2029       $this->t->pfp('out','form');
2030       
2031       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2032       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2033       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2034      
2035       $this->save_sessiondata(); 
2036     }
2037
2038     function int_view()
2039     {
2040       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2041       $this->t->set_block('int_view_t','district_list','list');
2042
2043       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2044       $num_months = get_var('num_months',array('GET','POST'));
2045       if($num_months == '') { $num_months = $this->default_int_num_months; }
2046       $this->t->set_var('num_months',$num_months);
2047       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
2048       else {  $this->t->set_var('lang_num_months','Months of History'); }
2049       $this->t->set_var('lang_filter','Filter');
2050       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2051
2052       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2053       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2054       
2055       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2056       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2057       
2058       $this->t->set_var('title','Hometeaching Interviews'); 
2059
2060       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2061       $this->db->query($sql,__LINE__,__FILE__);
2062       $i=0;
2063       while ($this->db->next_record())
2064         {
2065           $districts[$i]['district'] = $this->db->f('district');
2066           $districts[$i]['name'] = $this->db->f('name');
2067           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2068           $i++;
2069         }
2070
2071       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2072       $this->db->query($sql,__LINE__,__FILE__);
2073       $i=0;
2074       while ($this->db->next_record())
2075         {
2076           $elder_id[$i] = $this->db->f('elder');
2077           $elder_name[$i] = $this->db->f('name');
2078           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2079           $i++;
2080         }
2081       array_multisort($elder_name, $elder_id);
2082       for($i=0; $i < count($elder_id); $i++) {
2083           $id = $elder_id[$i];
2084           $elders[$id] = $elder_name[$i];
2085       }      
2086
2087       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2088       $this->db->query($sql,__LINE__,__FILE__);
2089       while ($this->db->next_record())
2090         {
2091           $aaronic_id = $this->db->f('aaronic');
2092           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2093           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2094         }
2095       
2096       $total_companionships = 0;
2097       $this->nextmatchs->template_alternate_row_color(&$this->t);
2098       for ($i=0; $i < count($districts); $i++) {
2099         $this->t->set_var('district_number',$districts[$i]['district']);
2100         $this->t->set_var('district_name',$districts[$i]['name']);      
2101         $supervisor = $districts[$i]['supervisor'];
2102         $unique_companionships='';
2103                 
2104         // Select all the unique companionship numbers for this district
2105         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2106
2107         $this->db->query($sql,__LINE__,__FILE__);
2108         $j=0;
2109         while ($this->db->next_record())
2110           {
2111             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2112             $j++;
2113           }
2114         
2115         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2116         $table_data=""; $num_companionships = $j; $num_elders = 0;
2117         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2118         for ($j=0; $j < count($unique_companionships); $j++) {
2119           // Select all the companions in each companionship
2120           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2121              "companionship=". $unique_companionships[$j]['companionship'];
2122           $this->db->query($sql,__LINE__,__FILE__);
2123           $k=0;
2124           $comp = $unique_companionships[$j]['companionship'];
2125           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2126           while ($this->db->next_record())
2127             {
2128               // Get this companions information
2129               $num_elders++;
2130               $companionship = $this->db->f('companionship');
2131               $elder_id = $this->db->f('elder');
2132               $aaronic_id = $this->db->f('aaronic');
2133               if($elder_id) {
2134                 $name = $elders[$elder_id];
2135                 $phone = $elder_phone[$elder_id];
2136               }
2137               else if($aaronic_id) {
2138                 $name = $aaronic[$aaronic_id]['name'];
2139                 $phone = $aaronic[$aaronic_id]['phone'];
2140               }
2141               $link_data['menuaction'] = 'eq.eq.int_update';
2142               $link_data['companionship'] = $companionship;
2143               $link_data['interviewer'] = $supervisor;
2144               $link_data['elder'] = $elder_id;
2145               $link_data['aaronic'] = $aaronic_id;
2146               $link_data['name'] = $name;
2147               $link_data['interview'] = '';
2148               $link_data['action'] = 'add';
2149               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2150               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2151
2152               // Find out how many times Interviews were performed in the past $num_months for this Elder
2153               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2154               for($m=$num_months; $m >= 0; $m--) {
2155                 $month = $this->current_month - $m;
2156                 $year = $this->current_year;
2157                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2158                 if($month < 10) { $month = "0"."$month"; }
2159                 $month_start = "$year"."-"."$month"."-"."01";
2160                 $month_end = "$year"."-"."$month"."-"."31";
2161                 $month = "$month"."/"."$year";
2162                 $sql = "SELECT * FROM eq_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2163                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2164                 $this->db2->query($sql,__LINE__,__FILE__);
2165                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2166               
2167                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2168                 if($this->db2->next_record()) {
2169                   if(!$int_recorded[$companionship][$m]) {
2170                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2171                   }
2172                   $link_data['menuaction'] = 'eq.eq.int_update';
2173                   $link_data['companionship'] = $companionship;
2174                   $link_data['interviewer'] = $this->db2->f('interviewer');
2175                   $link_data['elder'] = $elder_id;
2176                   $link_data['aaronic'] = $aaronic_id;
2177                   $link_data['name'] = $name;
2178                   $link_data['interview'] = $this->db2->f('interview');
2179                   $link_data['action'] = 'view';
2180                   $date = $this->db2->f('date');
2181                   $date_array = explode("-",$date);
2182                   $month = $date_array[1];
2183                   $day   = $date_array[2];
2184                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2185                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2186                 }
2187                 else { $table_data .= "<td>&nbsp;</td>"; }
2188               }
2189               $table_data .= "</tr>"; 
2190               $k++;
2191             }
2192           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2193         }
2194         $total_companionships += $num_companionships;
2195         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Quarterly Totals:</font></b></td>";
2196
2197         for($m=$num_months; $m >=0; $m--) {
2198           $month = $this->current_month - $m;
2199           if(($month % 3) == 1) { $quarter_total = $ints[$m]; }
2200           else { $quarter_total += $ints[$m]; }
2201           $percent = ceil(($quarter_total / $num_companionships)*100);
2202           $stat_data .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2203         }
2204         $stat_data .= "</tr>";
2205
2206         $this->t->set_var('table_width',$table_width);
2207         $this->t->set_var('header_row',$header_row);
2208         $this->t->set_var('table_data',$table_data);
2209         $this->t->set_var('stat_data',$stat_data);
2210         $this->t->fp('list','district_list',True);
2211       }
2212
2213       // Display the totals, cummulative per quarter
2214       $quarter_total = 0;
2215       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Quarterly Totals:</font></b></td>";
2216       for($m=$num_months; $m >=0; $m--) {
2217         $month = $this->current_month - $m;
2218         if(($month % 3) == 1) { $quarter_total = $total_ints[$m]; }
2219         else { $quarter_total += $total_ints[$m]; }
2220         $percent = ceil(($quarter_total / $total_companionships)*100);
2221         $totals .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2222       }
2223       $totals .= "</tr>";
2224       
2225       $this->t->set_var('totals',$totals);
2226       $this->t->pfp('out','int_view_t');
2227       $this->save_sessiondata(); 
2228     }
2229
2230   function int_update()
2231     {
2232       $this->t->set_file(array('form' => 'int_update.tpl'));
2233       $this->t->set_block('form','interviewer_list','int_list');
2234       $this->t->set_block('form','add','addhandle');
2235       $this->t->set_block('form','edit','edithandle');
2236       
2237       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2238       $this->t->set_var('readonly','');
2239       $this->t->set_var('disabled','');
2240       
2241       $action = get_var('action',array('GET','POST'));
2242       $companionship = get_var('companionship',array('GET','POST'));
2243       $interviewer = get_var('interviewer',array('GET','POST'));      
2244       $name = get_var('name',array('GET','POST'));
2245       $interview = get_var('interview',array('GET','POST'));
2246       $elder = get_var('elder',array('GET','POST'));
2247       $aaronic = get_var('aaronic',array('GET','POST'));
2248       $date = get_var('date',array('GET','POST'));
2249       $notes = get_var('notes',array('GET','POST'));
2250       
2251       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2252       $this->db->query($sql,__LINE__,__FILE__);
2253       while ($this->db->next_record())
2254         {
2255           $supervisor = $this->db->f('supervisor');
2256           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2257           $this->db2->query($sql,__LINE__,__FILE__);
2258           $this->db2->next_record();
2259           $interviewer_name = $this->db2->f('name');
2260           
2261           if($supervisor == $interviewer) { 
2262             $this->t->set_var('interviewer',$supervisor . ' selected');
2263           } else {
2264             $this->t->set_var('interviewer',$interviewer);
2265           }
2266           $this->t->set_var('interviewer_name',$interviewer_name);
2267           $this->t->fp('int_list','interviewer_list',True);
2268         }
2269       
2270       if($action == 'save')
2271         {
2272           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2273           $this->db->query("UPDATE eq_interview set " .
2274                      "   interview='" . $interview . "'" .
2275                     ", interviewer='" . $interviewer . "'" .
2276                           ", elder='" . $elder . "'" .
2277                         ", aaronic='" . $aaronic . "'" .
2278                            ", date='" . $date . "'" .
2279                           ", notes='" . $notes . "'" .
2280                            " WHERE interview=" . $interview,__LINE__,__FILE__);
2281           $this->int_view();
2282           return false;
2283         }
2284
2285       if($action == 'insert')
2286         {
2287           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2288           $this->db->query("INSERT INTO eq_interview (interviewer,elder,aaronic,date,notes) "
2289                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2290                            . $date . "','" . $notes ."')",__LINE__,__FILE__);
2291           $this->int_view();
2292           return false;
2293         }
2294       
2295       if($action == 'add')
2296         {
2297           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2298           $this->t->set_var('interview', '');
2299           $this->t->set_var('interviewer', $interviewer);
2300           $this->t->set_var('name',$name);
2301           $this->t->set_var('elder',$elder);
2302           $this->t->set_var('aaronic',$aaronic);
2303           $this->t->set_var('date','');
2304           $this->t->set_var('notes','');
2305           $this->t->set_var('lang_done','Cancel');
2306           $this->t->set_var('lang_action','Adding New Interview');
2307           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2308                                                                 . $interview . '&action=' . 'insert'));
2309         }
2310
2311       if($action == 'edit' || $action == 'view')
2312         {
2313           $sql = "SELECT * FROM eq_interview WHERE interview=".$interview;
2314           $this->db->query($sql,__LINE__,__FILE__);
2315           $this->db->next_record();
2316           $this->t->set_var('interview',$interview);
2317           $this->t->set_var('name',$name);
2318           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2319           $this->t->set_var('elder',$this->db->f('elder'));
2320           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2321           $this->t->set_var('date',$this->db->f('date'));
2322           $this->t->set_var('notes',$this->db->f('notes'));
2323         }
2324       
2325       if($action == 'edit')
2326         {
2327           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2328           $this->t->set_var('lang_done','Cancel');
2329           $this->t->set_var('lang_action','Editing Interview');
2330           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2331                                                                 . $interview . '&action=' . 'save'));
2332         }
2333
2334       if($action == 'view')
2335         {
2336           $date = $this->db->f('date');
2337           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2338           $this->t->set_var('readonly','READONLY');
2339           $this->t->set_var('disabled','DISABLED');
2340           $this->t->set_var('lang_done','Done');
2341           $this->t->set_var('lang_action','Viewing Interview');
2342           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2343                                                                 . $interview . '&action=' . 'edit'));
2344         }
2345       
2346       $this->t->set_var('lang_reset','Clear Form');
2347       $this->t->set_var('lang_add','Add Interview');
2348       $this->t->set_var('lang_save','Save Changes');
2349       $this->t->set_var('edithandle','');
2350       $this->t->set_var('addhandle','');
2351
2352       $this->t->pfp('out','form');
2353       
2354       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2355       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2356       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2357      
2358       $this->save_sessiondata(); 
2359     }
2360
2361   function vis_view()
2362     {
2363       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2364       $this->t->set_block('vis_view_t','visit_list','list1');
2365       $this->t->set_block('vis_view_t','family_list','list2');
2366
2367       $this->t->set_var('lang_name','Family Name');
2368       $this->t->set_var('lang_date','Date');
2369
2370       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2371       $this->t->set_var('vis_link_title','View Yearly Visits');
2372       
2373       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2374       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2375       
2376       
2377       $sql = "SELECT * FROM eq_visit WHERE companionship=0 ORDER BY date DESC";
2378       $this->db->query($sql,__LINE__,__FILE__);
2379       $total_records = $this->db->num_rows();
2380
2381       $i = 0;
2382       while ($this->db->next_record())
2383         {
2384           $visit_list[$i]['visit'] = $this->db->f('visit');
2385           $visit_list[$i]['family'] = $this->db->f('family');
2386           $visit_list[$i]['date']  = $this->db->f('date');
2387           $i++;
2388         }
2389             
2390       for ($i=0; $i < count($visit_list); $i++)
2391         {         
2392           $this->nextmatchs->template_alternate_row_color(&$this->t);
2393
2394           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2395           $this->db->query($sql,__LINE__,__FILE__);
2396           $this->db->next_record();
2397                   
2398           $this->t->set_var('family',$visit_list[$i]['family']);
2399           $this->t->set_var('family_name',$this->db->f('name'));
2400           $this->t->set_var('date',$visit_list[$i]['date']);
2401           
2402           $link_data['menuaction'] = 'eq.eq.vis_update';
2403           $link_data['visit'] = $visit_list[$i]['visit'];
2404           $link_data['name'] = $this->db->f('name');
2405           $link_data['date'] = $visit_list[$i]['date'];
2406           $link_data['action'] = 'view';
2407           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2408           $this->t->set_var('lang_view','View');
2409
2410           $link_data['menuaction'] = 'eq.eq.vis_update';
2411           $link_data['visit'] = $visit_list[$i]['visit'];
2412           $link_data['name'] = $this->db->f('name');
2413           $link_data['date'] = $visit_list[$i]['date'];
2414           $link_data['action'] = 'edit';
2415           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2416           $this->t->set_var('lang_edit','Edit');
2417
2418           $this->t->fp('list1','visit_list',True);
2419         }
2420
2421       // List the families that are available to record a visit against
2422       $sql = "SELECT * FROM eq_family WHERE valid=1";
2423       $this->db->query($sql,__LINE__,__FILE__);
2424       $total_records = $this->db->num_rows();
2425
2426       $i = 0;
2427       while ($this->db->next_record())
2428         {
2429           $family_names[$i] = $this->db->f('name');
2430           $family_ids[$i] = $this->db->f('family');
2431           $i++;
2432         } array_multisort($family_names, $family_ids);
2433       
2434       for ($i=0; $i < count($family_names); $i++)
2435         {
2436           $link_data['menuaction'] = 'eq.eq.vis_update';
2437           $link_data['visit'] = '';
2438           $link_data['family'] = $family_ids[$i];
2439           $link_data['action'] = 'add';
2440           $link_data['name'] = $family_names[$i];
2441           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2442
2443           $this->t->set_var('name',$family_names[$i]);
2444           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2445           else { $this->t->set_var('table_sep',"</td>"); }
2446           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2447
2448           $this->t->fp('list2','family_list',True);
2449         }   
2450
2451       $this->t->pfp('out','vis_view_t');
2452       $this->save_sessiondata(); 
2453     }
2454
2455   function vis_update()
2456     {
2457       $this->t->set_file(array('form' => 'vis_update.tpl'));
2458       $this->t->set_block('form','add','addhandle');
2459       $this->t->set_block('form','edit','edithandle');
2460       
2461       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2462       $this->t->set_var('readonly','');
2463       $this->t->set_var('disabled','');
2464       
2465       $action = get_var('action',array('GET','POST'));
2466       $visit = get_var('visit',array('GET','POST'));
2467       $family = get_var('family',array('GET','POST'));
2468       $name = get_var('name',array('GET','POST'));
2469       $date = get_var('date',array('GET','POST'));
2470       $notes = get_var('notes',array('GET','POST'));
2471       $companionship = 0;
2472       
2473       if($action == 'save')
2474         {
2475           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2476           $this->db->query("UPDATE eq_visit set " .
2477                            "  date='" . $date . "'" .
2478                           ", notes='" . $notes . "'" .
2479                            " WHERE visit=" . $visit,__LINE__,__FILE__);
2480           $this->vis_view();
2481           return false;
2482         }
2483
2484       if($action == 'insert')
2485         {
2486           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2487           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
2488                            . "VALUES ('" . $family . "','" . $companionship . "','"
2489                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
2490           $this->vis_view();
2491           return false;
2492         }
2493       
2494       if($action == 'add')
2495         {
2496           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2497           $this->t->set_var('family', $family);
2498           $this->t->set_var('visit', '');
2499           $this->t->set_var('name', $name);
2500           $this->t->set_var('date','');
2501           $this->t->set_var('notes','');
2502           $this->t->set_var('lang_done','Cancel');
2503           $this->t->set_var('lang_action','Adding New Visit');
2504           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
2505                                                                 . $family . '&action=' . 'insert'));
2506         }
2507
2508       if($action == 'edit' || $action == 'view')
2509         {
2510           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
2511           $this->db->query($sql,__LINE__,__FILE__);
2512           $this->db->next_record();
2513           $this->t->set_var('visit',$visit);
2514           $this->t->set_var('name',$name);
2515           $this->t->set_var('family', $family);
2516           $this->t->set_var('date',$this->db->f('date'));
2517           $this->t->set_var('notes',$this->db->f('notes'));
2518         }
2519       
2520       if($action == 'edit')
2521         {
2522           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2523           $this->t->set_var('lang_done','Cancel');
2524           $this->t->set_var('lang_action','Editing Visit');
2525           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2526                                                                 . $visit . '&action=' . 'save'));
2527         }
2528
2529       if($action == 'view')
2530         {
2531           $date = $this->db->f('date');
2532           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2533           $this->t->set_var('readonly','READONLY');
2534           $this->t->set_var('disabled','DISABLED');
2535           $this->t->set_var('lang_done','Done');
2536           $this->t->set_var('lang_action','Viewing Visit');
2537           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2538                                                                 . $visit . '&action=' . 'edit'));
2539         }
2540       
2541       $this->t->set_var('lang_reset','Clear Form');
2542       $this->t->set_var('lang_add','Add Visit');
2543       $this->t->set_var('lang_save','Save Changes');
2544       $this->t->set_var('edithandle','');
2545       $this->t->set_var('addhandle','');
2546
2547       $this->t->pfp('out','form');
2548       
2549       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
2550       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2551       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2552
2553       $this->save_sessiondata(); 
2554     }
2555
2556   function att_view()
2557     {
2558       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2559       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2560       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2561
2562       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
2563       $this->t->set_block('att_view_t','act_list','list');
2564
2565       $this->t->set_block('att_view_t','month_list','list1');
2566       $this->t->set_block('att_view_t','header_list','list2');
2567       $this->t->set_block('att_view_t','elder_list','list3');
2568       
2569       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2570       $num_months = get_var('num_months',array('GET','POST'));
2571       if($num_months == '') { $num_months = $this->default_att_num_months; }
2572       $this->t->set_var('num_months',$num_months);
2573       $this->t->set_var('lang_filter','Filter');
2574       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
2575       else {  $this->t->set_var('lang_num_months','Months of History'); }
2576          
2577       $sql = "SELECT * FROM eq_elder where valid=1";
2578       $this->db->query($sql,__LINE__,__FILE__);
2579       $i=0;
2580       while ($this->db->next_record())
2581         {
2582           $elder_name[$i] = $this->db->f('name');
2583           $elder_id[$i] = $this->db->f('elder');
2584           $i++;
2585         }
2586       array_multisort($elder_name, $elder_id);
2587
2588       
2589       // Create a list of sunday dates for a window of 3 months back and current month
2590       $i=0; 
2591       $last_time = 0; 
2592       $found_sunday = 0;
2593       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, date("m")-$num_months, 1, date("y")));
2594       $last_date = explode("-",$sunday_list[0]['date']);
2595       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2596       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
2597       while($last_time < $time_limit)
2598       {
2599         $day = date("w",$last_time);
2600         if(date("w",$last_time) == 0) {
2601           $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
2602           $last_date = explode("-",$sunday_list[$i]['date']);
2603           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2604           $sunday_list[$i]['day'] = $last_date[2];
2605           $sunday_list[$i]['month'] = date("M",$last_time);
2606           $sunday_list[$i]['year'] = $last_date[0];
2607           $found_sunday = 1; 
2608         }
2609         $last_time += 90000;
2610         if($found_sunday) { $i++; $found_sunday=0; }
2611       }
2612
2613       $total_elders = count($elder_id);
2614       $old_month=$sunday_list[0]['month']; $span=0;
2615       for ($i=0; $i < count($sunday_list); $i++) {
2616         $date = $sunday_list[$i]['date'];
2617         $this->t->set_var('date',$sunday_list[$i]['date']);
2618         $this->t->set_var('day',$sunday_list[$i]['day']);
2619         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
2620           if($i == count($sunday_list)-1) { $span++; }
2621           $cur_month = $sunday_list[$i]['month'];
2622           $old_month = $sunday_list[$i]['month'];         
2623           $link_data['menuaction'] = 'eq.eq.att_update';
2624           $link_data['month'] = $sunday_list[$i-1]['month'];
2625           $link_data['year'] = $sunday_list[$i-1]['year'];
2626           $link_data['action'] = 'update_month';
2627           $cur_month = $sunday_list[$i-1]['month'];
2628           $cur_year = $sunday_list[$i-1]['year'];
2629           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
2630           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2631           $this->t->set_var('month',$sunday_list[$i-1]['month']);
2632           $this->t->set_var('year',$sunday_list[$i-1]['year']);
2633           $this->t->set_var('span',$span); $span=0;
2634           $this->t->fp('list1','month_list',True);
2635         } $span++;
2636       }
2637       $this->t->set_var('total_elders',$total_elders);
2638       $this->t->set_var('header_row',$header_row);
2639       
2640       $elder_width=200; $att_width=25; $total_width=$elder_width; 
2641       for ($i=0; $i < count($sunday_list); $i++) {
2642         $link_data['menuaction'] = 'eq.eq.att_update';
2643         $link_data['month'] = $sunday_list[$i]['month'];
2644         $link_data['year'] = $sunday_list[$i]['year'];
2645         $link_data['day'] = $sunday_list[$i]['day'];
2646         $link_data['date'] = $sunday_list[$i]['date'];
2647         $link_data['action'] = 'update_day';
2648         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2649         $this->t->set_var('date',$sunday_list[$i]['date']);
2650         $this->t->set_var('day',$sunday_list[$i]['day']);
2651         $this->t->set_var('month',$sunday_list[$i]['month']);
2652         $this->t->set_var('year',$sunday_list[$i]['year']);
2653         $this->t->fp('list2','header_list',True);
2654         $total_width += $att_width;
2655         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
2656       }
2657
2658       for ($i=0; $i < count($elder_id); $i++) {
2659         $att_table = "";
2660         $this->nextmatchs->template_alternate_row_color(&$this->t);
2661         $this->t->set_var('elder_name',$elder_name[$i]);
2662         #print "checking for elder: " . $elder_id[$i] . "<br>";
2663         for ($j=0; $j < count($sunday_list); $j++) {
2664           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
2665           #print "SELECT * FROM eq_attendance WHERE date='"
2666           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
2667           $sql = "SELECT * FROM eq_attendance WHERE date='"
2668              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2669           $this->db->query($sql,__LINE__,__FILE__);
2670           if($this->db->next_record()) {
2671             $cur_month = $sunday_list[$j]['month'];
2672             if($attended[$i][$cur_month] != 1) { 
2673               $attended[$i][$cur_month]=1;
2674               $attendance[$monthnum[$cur_month]]++;
2675             } 
2676             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
2677           } else {
2678             $att_table .= '<td>&nbsp;</td>';
2679           }
2680         }
2681         $this->t->set_var('att_table',$att_table);
2682         $this->t->fp('list3','elder_list',True);
2683       }
2684       $this->t->set_var('total_width',$total_width);
2685       $this->t->set_var('elder_width',$elder_width);
2686       $this->t->set_var('att_width',$att_width);
2687
2688       # Now calculate attendance for these months
2689       $attendance_str = "";
2690       $nonattendance_str = "";
2691       $aveattendance_str = "";
2692       $avenonattendance_str = "";
2693       $num_months=0;
2694       $ave_total_attended=0;
2695       ksort($attendance);
2696       foreach($attendance as $att => $value) {
2697         $total_attended = $attendance[$att];
2698         $ave_total_attended += $attendance[$att]; $num_months++;
2699         $percent = ceil(($total_attended / $total_elders)*100);
2700         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2701         $total_nonattended = $total_elders - $total_attended;
2702         $percent = ceil(($total_nonattended / $total_elders)*100);
2703         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
2704         
2705         $total_attended = ceil(($ave_total_attended / $num_months));
2706         $percent = ceil(($total_attended / $total_elders)*100);
2707         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2708         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
2709         $percent = ceil(($total_attended / $total_elders)*100);
2710         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2711       }
2712       
2713       $this->t->set_var('attendance',$attendance_str);
2714       $this->t->set_var('aveattendance',$aveattendance_str);
2715       $this->t->set_var('nonattendance',$nonattendance_str);
2716       $this->t->set_var('avenonattendance',$avenonattendance_str);
2717       
2718       $this->t->pfp('out','att_view_t');
2719       $this->save_sessiondata(); 
2720     }
2721
2722   function att_update()
2723     {
2724       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2725       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2726       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2727
2728       $this->t->set_file(array('form' => 'att_update.tpl'));
2729       $this->t->set_block('form','edit','edithandle');
2730       
2731       $this->t->set_block('form','month_list','list1');
2732       $this->t->set_block('form','header_list','list2');
2733       $this->t->set_block('form','elder_list','list3');
2734
2735       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2736
2737       $action = get_var('action',array('GET','POST'));
2738       $month = get_var('month',array('GET','POST'));
2739       $year = get_var('year',array('GET','POST'));
2740       $day = get_var('day',array('GET','POST'));
2741       $date = get_var('date',array('GET','POST'));
2742
2743       if($action == 'save_month' || $action == 'save_day')
2744         {
2745            $new_data = get_var('elders_attended',array('POST'));
2746            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
2747
2748            if($action == 'save_month') {        
2749              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
2750            }
2751
2752            if($action == 'save_day') {        
2753              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
2754            }   
2755
2756            foreach ($new_data as $data)
2757            {
2758               $data_array = explode("-",$data);
2759               $elder = $data_array[0];
2760               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
2761               $this->db->query("INSERT INTO eq_attendance (elder,date) "
2762                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
2763            }
2764         
2765          $this->att_view();
2766          return false;    
2767         }
2768
2769       $sql = "SELECT * FROM eq_elder where valid=1";
2770       $this->db->query($sql,__LINE__,__FILE__);
2771       $i=0;
2772       while ($this->db->next_record())
2773         {
2774           $elder_name[$i] = $this->db->f('name');
2775           $elder_id[$i] = $this->db->f('elder');
2776           $i++;
2777         }
2778       array_multisort($elder_name, $elder_id);
2779       
2780       if($action == 'update_month')
2781       {
2782         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
2783         $i=0; 
2784         $last_time = 0; 
2785         $found_sunday = 0;
2786         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
2787         $last_date = explode("-",$sunday_list[0]['date']);
2788         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2789         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
2790         while($last_time <= $time_limit)
2791         {
2792           $day = date("w",$last_time);
2793           if(date("w",$last_time) == 0) { 
2794             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
2795             $last_date = explode("-",$sunday_list[$i]['date']);
2796             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2797             $sunday_list[$i]['day'] = $last_date[2];
2798             $sunday_list[$i]['month'] = date("M",$last_time);
2799             $sunday_list[$i]['year'] = $last_date[0];
2800             $found_sunday = 1; 
2801           }
2802           $last_time += 90000;
2803           if($found_sunday) { $i++; $found_sunday=0; }
2804         }
2805
2806         $this->t->set_var('span', $i);
2807         $this->t->set_var('month',$sunday_list[$i-1]['month']);
2808         $this->t->set_var('year',$sunday_list[$i-1]['year']);
2809         $this->t->fp('list1','month_list',True);
2810         $elder_width=200; $att_width=25; $total_width=$elder_width;
2811         for ($i=0; $i < count($sunday_list); $i++) {
2812           $link_data['menuaction'] = 'eq.eq.att_update';
2813           $link_data['month'] = $sunday_list[$i]['month'];
2814           $link_data['year'] = $sunday_list[$i]['year'];
2815           $link_data['day'] = $sunday_list[$i]['day'];
2816           $link_data['date'] = $sunday_list[$i]['date'];
2817           $link_data['action'] = 'update_day';
2818           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2819           $this->t->set_var('date',$sunday_list[$i]['date']);
2820           $this->t->set_var('day',$sunday_list[$i]['day']);
2821           $this->t->set_var('month',$sunday_list[$i]['month']);
2822           $this->t->set_var('year',$sunday_list[$i]['year']);
2823           $this->t->fp('list2','header_list',True);
2824           $total_width += $att_width;
2825         }     
2826       }
2827
2828       if($action == 'update_day')
2829       {
2830         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
2831         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
2832         $this->t->set_var('month',$month);
2833         $this->t->set_var('year',$year);
2834         $this->t->fp('list1','month_list',True);
2835         $this->t->set_var('date',$date);
2836         $this->t->set_var('day',$day);
2837         $this->t->set_var('month',$month);
2838         $this->t->set_var('year',$year);
2839         $this->t->fp('list2','header_list',True);
2840       }           
2841             
2842       for ($i=0; $i < count($elder_id); $i++) {
2843         $att_table = "";
2844         $this->nextmatchs->template_alternate_row_color(&$this->t);
2845         $this->t->set_var('elder_name',$elder_name[$i]);
2846         for ($j=0; $j < count($sunday_list); $j++) {
2847           $sql = "SELECT * FROM eq_attendance WHERE date='"
2848              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2849           $this->db->query($sql,__LINE__,__FILE__);
2850           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
2851           if($this->db->next_record()) {
2852             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
2853           } else {
2854             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
2855           }
2856         }
2857         $this->t->set_var('att_table',$att_table);
2858         $this->t->fp('list3','elder_list',True);
2859       } 
2860            
2861       $this->t->set_var('lang_done', 'Cancel');
2862       $this->t->set_var('lang_reset','Clear Form');
2863       $this->t->set_var('lang_save','Save Changes');
2864
2865       $this->t->pfp('out','form');
2866       $this->t->pfp('addhandle','edit');
2867
2868       $this->save_sessiondata();       
2869     }
2870
2871   function dir_view()
2872     {
2873       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
2874       $this->t->set_block('dir_view_t','dir_list','list');
2875       
2876       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
2877       $this->db->query($sql,__LINE__,__FILE__);
2878       $i=0;
2879       while ($this->db->next_record())
2880         {
2881           $parent[$i]['id'] = $this->db->f('parent');
2882           $parent[$i]['name'] = $this->db->f('name');
2883           $parent[$i]['phone'] = $this->db->f('phone');
2884           $parent[$i]['address'] = $this->db->f('address');
2885           $i++;
2886         }   
2887       
2888       for ($i=0; $i < count($parent); $i++) 
2889       {
2890         $name = $parent[$i]['name'];
2891         $phone = $parent[$i]['phone'];
2892         $address = $parent[$i]['address'];
2893         $this->t->set_var('name', $name);
2894         $this->t->set_var('address', $address);
2895         $this->t->set_var('phone', $phone);
2896         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2897         $this->t->set_var('tr_color',$tr_color);
2898         $this->t->fp('list','dir_list',True);
2899         //print "$phone $name $address<br>";
2900       }
2901       $this->t->pfp('out','dir_view_t');
2902       $this->save_sessiondata();   
2903     }
2904   
2905   function org_view()
2906     {
2907       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
2908       $this->t->set_block('org_view_t','calling_list','list1');
2909       $this->t->set_block('org_view_t','org_list','list2');
2910
2911       # Display a list ordered alphabetically
2912       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
2913       $this->db->query($sql,__LINE__,__FILE__);
2914       $i=0;
2915       while ($this->db->next_record())
2916         {
2917           $calling[$i]['id'] = $this->db->f('indiv_id');
2918           $calling[$i]['name'] = $this->db->f('name');
2919           $calling[$i]['position'] = $this->db->f('position');
2920           $calling[$i]['sustained'] = $this->db->f('sustained');
2921           $calling[$i]['organization'] = $this->db->f('organization');
2922           $i++;
2923         }   
2924       for ($i=0; $i < count($calling); $i++) 
2925       {
2926         $name = $calling[$i]['name'];
2927         $position = $calling[$i]['position'];
2928         $sustained = $calling[$i]['sustained'];
2929         $organization = $calling[$i]['organization'];
2930         $this->t->set_var('name', $name);
2931         $this->t->set_var('position', $position);
2932         $this->t->set_var('sustained', $sustained);
2933         $this->t->set_var('organization', $organization);
2934         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2935         $this->t->set_var('tr_color',$tr_color);
2936         $this->t->fp('list1','calling_list',True);
2937       }
2938
2939       # Display a list ordered by organization
2940       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
2941       $this->db->query($sql,__LINE__,__FILE__);
2942       $i=0;
2943       while ($this->db->next_record())
2944         {
2945           $calling[$i]['id'] = $this->db->f('indiv_id');
2946           $calling[$i]['name'] = $this->db->f('name');
2947           $calling[$i]['position'] = $this->db->f('position');
2948           $calling[$i]['sustained'] = $this->db->f('sustained');
2949           $calling[$i]['organization'] = $this->db->f('organization');
2950           $i++;
2951         }   
2952       for ($i=0; $i < count($calling); $i++) 
2953       {
2954         $name = $calling[$i]['name'];
2955         $position = $calling[$i]['position'];
2956         $sustained = $calling[$i]['sustained'];
2957         $organization = $calling[$i]['organization'];
2958         $this->t->set_var('name', $name);
2959         $this->t->set_var('position', $position);
2960         $this->t->set_var('sustained', $sustained);
2961         $this->t->set_var('organization', $organization);
2962         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2963         $this->t->set_var('tr_color',$tr_color);
2964         $this->t->fp('list2','org_list',True);
2965       }
2966       
2967       $this->t->pfp('out','org_view_t');
2968       $this->save_sessiondata();   
2969     }
2970   
2971   function schedule()
2972     {
2973       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
2974       $this->t->set_block('sched_t','presidency_list','list');
2975
2976       $action = get_var('action',array('GET','POST'));
2977       
2978       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
2979       $this->t->set_var('title','EQ Scheduling Tool');
2980
2981       $this->t->set_var('lang_save','Save Schedule');
2982       $this->t->set_var('lang_reset','Cancel');
2983       
2984       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2985       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2986
2987       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2988       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2989       
2990       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
2991       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2992       
2993       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
2994       $table_width=$date_width + $time_width + $elder_width + $family_width;
2995       $header_row = "<th width=$date_width><font size=-2>Date</th>";
2996       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2997       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
2998       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
2999       $table_data = "";
3000
3001       if($action == 'save')
3002         {
3003           $new_data = get_var('sched',array('POST'));
3004           foreach ($new_data as $presidency_array)
3005            {
3006              foreach ($presidency_array as $entry)
3007                {
3008                  $presidency = $entry['presidency'];
3009                  $appointment = $entry['appointment'];
3010                  $date = $entry['date'];
3011                  $hour = $entry['hour'];
3012                  $minute = $entry['minute'];
3013                  $pm = $entry['pm'];
3014                  $elder = $entry['elder'];
3015                  $family = $entry['family'];
3016                  if($pm) { $hour = $hour + 12; }
3017                  $time = $hour.':'.$minute.':'.'00';
3018
3019                  // Update an existing appointment
3020                  if($appointment != 0)
3021                    {
3022                      $this->db->query("UPDATE eq_appointment set" .
3023                               " family=" . $family . 
3024                               " ,elder=" . $elder . 
3025                               " ,date='" . $date . "'" .
3026                               " ,time='" . $time . "'" .
3027                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3028
3029                      //print "updating entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                  
3030                    }
3031                  
3032                  // Add a new appointment
3033                  else if(($appointment == 0) && ($date != "") && ($time != ""))
3034                    {
3035                      $this->db->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time) "
3036                            . "VALUES ('" . $appointment . "','" . $presidency . "','" . $family . "','"
3037                            . $elder . "','" . $date . "','" . $time  ."')",__LINE__,__FILE__);
3038                      
3039                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                    
3040                    }
3041                }
3042            }
3043           
3044           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3045           Header('Location: ' . $take_me_to_url);
3046         }
3047
3048       $sql = "SELECT * FROM eq_presidency where valid=1";
3049       $this->db->query($sql,__LINE__,__FILE__);
3050       $i=0;
3051       while ($this->db->next_record())
3052         {
3053           $presidency_data[$i]['id'] = $this->db->f('presidency');
3054           $presidency_data[$i]['name'] = $this->db->f('name');
3055           $presidency_data[$i]['elder'] = $this->db->f('elder');
3056           $i++;
3057         }
3058       
3059       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3060       $this->db->query($sql,__LINE__,__FILE__);
3061       $i=0;
3062       while ($this->db->next_record())
3063         {
3064           $elder_id[$i] = $this->db->f('elder');
3065           $elder_name[$i] = $this->db->f('name');
3066           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3067           $i++;
3068         }
3069       array_multisort($elder_name, $elder_id);
3070
3071       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3072       $this->db->query($sql,__LINE__,__FILE__);
3073       $i=0;
3074       while ($this->db->next_record())
3075         {
3076           $family_id[$i] = $this->db->f('family');
3077           $family_name[$i] = $this->db->f('name');
3078           $i++;
3079         }
3080       array_multisort($family_name, $family_id);
3081       
3082       for ($i=0; $i < count($presidency_data); $i++) {
3083         $presidency = $presidency_data[$i]['id'];
3084         $interviewer = $presidency_data[$i]['elder'];
3085         $name = $presidency_data[$i]['name'];
3086         $this->t->set_var('presidency_name',$name);
3087         $table_data="";
3088         
3089         // query the database for all the appointments
3090         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3091         $this->db->query($sql,__LINE__,__FILE__);
3092
3093         // Prefill any existing appointment slots
3094         while ($this->db->next_record())
3095           {
3096             $appointment = $this->db->f('appointment');
3097             $elder = $this->db->f('elder');
3098             $family = $this->db->f('family');
3099             
3100             $date = $this->db->f('date');
3101             $date_array = explode("-",$date);
3102             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3103             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3104             
3105             $time = $this->db->f('time');
3106             $time_array = explode(":",$time);
3107             $hour = $time_array[0];
3108             $minute = $time_array[1];
3109             $pm = 0;
3110             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3111             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3112             
3113             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3114
3115             // Date selection
3116             $table_data.= '<td align=left>';
3117             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3118             $table_data.= '</td>';
3119             
3120             // Hour & Minutes selection
3121             $table_data.= "<td align=center>";
3122             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3123             foreach(range(1,12) as $num) {
3124               if($hour == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3125               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3126             }
3127             $table_data.= '</select>';
3128             $table_data.= '&nbsp;:&nbsp;';
3129             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3130             foreach(range(0,3) as $num) {
3131               $num = $num * 15; if($num == 0) { $num = "00"; }
3132               if($minute == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3133               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3134             }
3135             $table_data.= '</select>';
3136             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3137             if($pm == 0) { $table_data.= '<option value=0 selected>am</option>'; $table_data.= '<option value=1>pm</option>'; }
3138             else { $table_data.= '<option value=0>am</option>'; $table_data.= '<option value=1 selected>pm</option>'; }
3139             $table_data.= '</select>';
3140             $table_data.= "</td>";
3141             
3142             // Elder drop down list (for PPIs)
3143             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3144             $table_data.= '<option value=0></option>';  
3145             for ($j=0; $j < count($elder_id); $j++) {
3146               $id = $elder_id[$j];
3147               $name = $elder_name[$j];
3148               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3149               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3150             }
3151             $table_data.='</select></td>';
3152
3153             // Family drop down list (for Visits)
3154             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3155             $table_data.= '<option value=0></option>';              
3156             for ($j=0; $j < count($elder_id); $j++) {
3157               $id = $family_id[$j];
3158               $name = $family_name[$j];
3159               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3160               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3161             }
3162             $table_data.='</select></td>';
3163             
3164             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3165             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3166         
3167             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3168             $this->t->set_var('tr_color',$tr_color);
3169             
3170           }
3171
3172         // Create blank appointment slot
3173         $appointment = 0;
3174         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3175
3176         // Date selection
3177         $table_data.= '<td align=left>';
3178         $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3179         $table_data.= '</td>';
3180         
3181         // Time selection
3182         $table_data.= "<td align=center>";
3183         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3184         $table_data.= '<option value=""></option>';
3185         foreach(range(1,12) as $num) {
3186           $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3187         }
3188         $table_data.= '</select>';
3189         $table_data.= '&nbsp;:&nbsp;';
3190         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3191         $table_data.= '<option value=""></option>';
3192         foreach(range(0,3) as $num) {
3193           $num = $num * 15; if($num == 0) { $num = "00"; }
3194           $table_data.= '<option value='.$num.'>'.$num.'</option>';
3195         }
3196         $table_data.= '</select>';
3197         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3198         $table_data.= '<option value=""></option>';
3199         $table_data.= '<option value=0>am</option>';
3200         $table_data.= '<option value=1>pm</option>';
3201         $table_data.= '</select>';
3202         $table_data.= "</td>";
3203         
3204         // Elder drop down list
3205         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3206         $table_data.= '<option value=0></option>';  
3207         for ($j=0; $j < count($elder_id); $j++) {
3208           $id = $elder_id[$j];
3209           $name = $elder_name[$j];
3210           $table_data.= '<option value='.$id.'>'.$name.'</option>';
3211         }
3212         $table_data.='</select></td>';
3213         
3214         // Family drop down list
3215         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3216         $table_data.= '<option value=0></option>';          
3217         for ($j=0; $j < count($elder_id); $j++) {
3218           $id = $family_id[$j];
3219           $name = $family_name[$j];
3220           $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3221         }
3222         $table_data.='</select></td>';
3223
3224         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3225         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3226         
3227         $this->t->set_var('table_data',$table_data);
3228         $this->t->set_var('header_row',$header_row);
3229         $this->t->set_var('table_width',$table_width);
3230         $this->t->fp('list','presidency_list',True);
3231         
3232       }
3233       
3234       $this->t->pfp('out','sched_t');
3235       $this->save_sessiondata();   
3236     }
3237   
3238   function admin()
3239     {
3240       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3241       $this->t->set_block('admin_t','upload','uploadhandle');
3242       $this->t->set_block('admin_t','admin','adminhandle');
3243       $this->t->set_block('admin_t','cmd','cmdhandle');
3244       
3245       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3246       
3247       $action = get_var('action',array('GET','POST'));
3248
3249       $this->t->pfp('out','admin_t');
3250       
3251       if($action == 'upload')
3252         {        
3253           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
3254           
3255           if((($_FILES['uploadedfile']['type'] == "application/zip") ||
3256               ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3257               ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3258               ($_FILES['uploadedfile']['type'] == "application/octet-stream")) &&
3259              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
3260             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3261             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3262             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3263             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3264             $this->t->set_var('uploadstatus',$uploadstatus);
3265             $this->t->pfp('uploadhandle','upload');
3266             $this->t->set_var('uploadhandle','');
3267             print "<table border=1 width=80%><tr><td>\n<pre>";
3268             
3269             # make a directory for this data to be stored in
3270             $date="data_" . date("Y_m_d");
3271             $data_dir = $this->upload_target_path . $date;
3272             print "-> Making the data directory: $date<br>\n";
3273             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
3274             if($return_code != 0) {
3275               print implode('\n',$result) . "<br>";
3276               print "<b><font color=red>";
3277               print "-E- Unable to create the data directory. Aborting import.";
3278               print "</font></b>";
3279               return 0;
3280             }
3281
3282             # move the file uploaded into this directory
3283             print "-> Moving the uploaded file into the data dir<br>\n";
3284             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3285             if($return_code != 0) {
3286               print implode('\n',$result) . "<br>";
3287               print "<b><font color=red>";
3288               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3289               print "</font></b>";
3290               return 0;
3291             }
3292             
3293             # unzip the data into this directory
3294             print "-> Unzipping the data<br>\n";
3295             $data_file = $data_dir . '';
3296             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3297             if($return_code != 0) {
3298               print implode('\n',$result) . "<br>";
3299               print "<b><font color=red>";
3300               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
3301               print "</font></b>";
3302               return 0;
3303             }
3304             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3305
3306             # update the data_latest link to point to this new directory
3307             print "-> Updating the latest data dir link<br>\n";
3308             $data_latest = $this->upload_target_path . 'data_latest';
3309             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3310             if($return_code != 0) {
3311               print implode('\n',$result) . "<br>";
3312               print "<b><font color=red>";
3313               print "-E- Unable to update the data latest link. Aborting import.";
3314               print "</font></b>";
3315               return 0;
3316             }
3317             
3318             # run the import perl script to encorporate it into the DB
3319             ob_start('ob_logstdout', 2);
3320             print "-> Importing the data into the EQ database<br>\n";
3321             ob_flush(); flush(); sleep(1);
3322             $import_log = $this->upload_target_path . '/import.log';
3323             $data_log = $this->upload_target_path . '/data.log';
3324             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
3325             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
3326             #print "import_cmd: $import_cmd<br>";
3327             #print "parse_cmd: $parse_cmd<br>";
3328             ob_start('ob_logstdout', 2);
3329             passthru($import_cmd);
3330             passthru($parse_cmd);
3331             ob_flush(); flush(); sleep(1);
3332
3333             # fix the permissions of the data dir
3334             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3335             
3336             $this->t->pfp('cmdhandle','cmd');
3337             print "</pre></td></tr></table>";
3338             
3339           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3340                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3341                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3342                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3343             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3344             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3345             $this->t->set_var('uploadstatus',$uploadstatus);
3346             $this->t->pfp('uploadhandle','upload',True);
3347           } else {
3348             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3349             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3350             $this->t->set_var('uploadstatus',$uploadstatus);
3351             $this->t->pfp('uploadhandle','upload',True);
3352           }
3353         }
3354       else
3355         {
3356           $this->t->set_var('adminhandle','');
3357           $this->t->pfp('adminhandle','admin'); 
3358         }
3359       
3360       $this->save_sessiondata();   
3361     }
3362   
3363 }
3364
3365 ?>