Fixed attendance tool to show history by quarters, not months
[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 = 1;
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_quarters = get_var('num_quarters',array('GET','POST'));
2045       if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2046       $this->t->set_var('num_quarters',$num_quarters);
2047       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2048       else {  $this->t->set_var('lang_num_quarters','Quarters of History'); }
2049       $this->t->set_var('lang_filter','Filter');
2050       
2051       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2052       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2053       
2054       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2055       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2056       
2057       $this->t->set_var('title','Hometeaching Interviews'); 
2058
2059       $num_months = $num_quarters * 3 - 1;
2060       $current_month = $this->current_month;
2061       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2062       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2063       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2064       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2065
2066       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2067       $this->db->query($sql,__LINE__,__FILE__);
2068       $i=0;
2069       while ($this->db->next_record())
2070         {
2071           $districts[$i]['district'] = $this->db->f('district');
2072           $districts[$i]['name'] = $this->db->f('name');
2073           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2074           $i++;
2075         }
2076
2077       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2078       $this->db->query($sql,__LINE__,__FILE__);
2079       $i=0;
2080       while ($this->db->next_record())
2081         {
2082           $elder_id[$i] = $this->db->f('elder');
2083           $elder_name[$i] = $this->db->f('name');
2084           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2085           $i++;
2086         }
2087       array_multisort($elder_name, $elder_id);
2088       for($i=0; $i < count($elder_id); $i++) {
2089           $id = $elder_id[$i];
2090           $elders[$id] = $elder_name[$i];
2091       }      
2092
2093       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2094       $this->db->query($sql,__LINE__,__FILE__);
2095       while ($this->db->next_record())
2096         {
2097           $aaronic_id = $this->db->f('aaronic');
2098           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2099           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2100         }
2101       
2102       $total_companionships = 0;
2103       $this->nextmatchs->template_alternate_row_color(&$this->t);
2104       for ($i=0; $i < count($districts); $i++) {
2105         $this->t->set_var('district_number',$districts[$i]['district']);
2106         $this->t->set_var('district_name',$districts[$i]['name']);      
2107         $supervisor = $districts[$i]['supervisor'];
2108         $unique_companionships='';
2109                 
2110         // Select all the unique companionship numbers for this district
2111         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2112
2113         $this->db->query($sql,__LINE__,__FILE__);
2114         $j=0;
2115         while ($this->db->next_record())
2116           {
2117             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2118             $j++;
2119           }
2120         
2121         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2122         $table_data=""; $num_companionships = $j; $num_elders = 0;
2123         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2124         for ($j=0; $j < count($unique_companionships); $j++) {
2125           // Select all the companions in each companionship
2126           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2127              "companionship=". $unique_companionships[$j]['companionship'];
2128           $this->db->query($sql,__LINE__,__FILE__);
2129           $k=0;
2130           $comp = $unique_companionships[$j]['companionship'];
2131           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2132           while ($this->db->next_record())
2133             {
2134               // Get this companions information
2135               $num_elders++;
2136               $companionship = $this->db->f('companionship');
2137               $elder_id = $this->db->f('elder');
2138               $aaronic_id = $this->db->f('aaronic');
2139               if($elder_id) {
2140                 $name = $elders[$elder_id];
2141                 $phone = $elder_phone[$elder_id];
2142               }
2143               else if($aaronic_id) {
2144                 $name = $aaronic[$aaronic_id]['name'];
2145                 $phone = $aaronic[$aaronic_id]['phone'];
2146               }
2147               $link_data['menuaction'] = 'eq.eq.int_update';
2148               $link_data['companionship'] = $companionship;
2149               $link_data['interviewer'] = $supervisor;
2150               $link_data['elder'] = $elder_id;
2151               $link_data['aaronic'] = $aaronic_id;
2152               $link_data['name'] = $name;
2153               $link_data['interview'] = '';
2154               $link_data['action'] = 'add';
2155               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2156               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2157
2158               // Find out how many times Interviews were performed in the past $num_months for this Elder
2159               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2160               for($m=$num_months; $m >= 0; $m--) {
2161                 $month = $current_month - $m;
2162                 $year = $this->current_year;
2163                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2164                 if($month < 10) { $month = "0"."$month"; }
2165                 $month_start = "$year"."-"."$month"."-"."01";
2166                 $month_end = "$year"."-"."$month"."-"."31";
2167                 $month = "$month"."/"."$year";
2168                 $sql = "SELECT * FROM eq_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2169                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2170                 $this->db2->query($sql,__LINE__,__FILE__);
2171                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2172               
2173                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2174                 if($this->db2->next_record()) {
2175                   if(!$int_recorded[$companionship][$m]) {
2176                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2177                   }
2178                   $link_data['menuaction'] = 'eq.eq.int_update';
2179                   $link_data['companionship'] = $companionship;
2180                   $link_data['interviewer'] = $this->db2->f('interviewer');
2181                   $link_data['elder'] = $elder_id;
2182                   $link_data['aaronic'] = $aaronic_id;
2183                   $link_data['name'] = $name;
2184                   $link_data['interview'] = $this->db2->f('interview');
2185                   $link_data['action'] = 'view';
2186                   $date = $this->db2->f('date');
2187                   $date_array = explode("-",$date);
2188                   $month = $date_array[1];
2189                   $day   = $date_array[2];
2190                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2191                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2192                 }
2193                 else { $table_data .= "<td>&nbsp;</td>"; }
2194               }
2195               $table_data .= "</tr>"; 
2196               $k++;
2197             }
2198           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2199         }
2200         $total_companionships += $num_companionships;
2201         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Quarterly Totals:</font></b></td>";
2202
2203         for($m=$num_months; $m >=0; $m--) {
2204           $month = $current_month - $m;
2205           if(($month % 3) == 1) { $quarter_total = $ints[$m]; }
2206           else { $quarter_total += $ints[$m]; }
2207           $percent = ceil(($quarter_total / $num_companionships)*100);
2208           $stat_data .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2209         }
2210         $stat_data .= "</tr>";
2211
2212         $this->t->set_var('table_width',$table_width);
2213         $this->t->set_var('header_row',$header_row);
2214         $this->t->set_var('table_data',$table_data);
2215         $this->t->set_var('stat_data',$stat_data);
2216         $this->t->fp('list','district_list',True);
2217       }
2218
2219       // Display the totals, cummulative per quarter
2220       $quarter_total = 0;
2221       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Quarterly Totals:</font></b></td>";
2222       for($m=$num_months; $m >=0; $m--) {
2223         $month = $current_month - $m;
2224         if(($month % 3) == 1) { $quarter_total = $total_ints[$m]; }
2225         else { $quarter_total += $total_ints[$m]; }
2226         $percent = ceil(($quarter_total / $total_companionships)*100);
2227         $totals .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2228       }
2229       $totals .= "</tr>";
2230       
2231       $this->t->set_var('totals',$totals);
2232       $this->t->pfp('out','int_view_t');
2233       $this->save_sessiondata(); 
2234     }
2235
2236   function int_update()
2237     {
2238       $this->t->set_file(array('form' => 'int_update.tpl'));
2239       $this->t->set_block('form','interviewer_list','int_list');
2240       $this->t->set_block('form','add','addhandle');
2241       $this->t->set_block('form','edit','edithandle');
2242       
2243       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2244       $this->t->set_var('readonly','');
2245       $this->t->set_var('disabled','');
2246       
2247       $action = get_var('action',array('GET','POST'));
2248       $companionship = get_var('companionship',array('GET','POST'));
2249       $interviewer = get_var('interviewer',array('GET','POST'));      
2250       $name = get_var('name',array('GET','POST'));
2251       $interview = get_var('interview',array('GET','POST'));
2252       $elder = get_var('elder',array('GET','POST'));
2253       $aaronic = get_var('aaronic',array('GET','POST'));
2254       $date = get_var('date',array('GET','POST'));
2255       $notes = get_var('notes',array('GET','POST'));
2256       
2257       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2258       $this->db->query($sql,__LINE__,__FILE__);
2259       while ($this->db->next_record())
2260         {
2261           $supervisor = $this->db->f('supervisor');
2262           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2263           $this->db2->query($sql,__LINE__,__FILE__);
2264           $this->db2->next_record();
2265           $interviewer_name = $this->db2->f('name');
2266           
2267           if($supervisor == $interviewer) { 
2268             $this->t->set_var('interviewer',$supervisor . ' selected');
2269           } else {
2270             $this->t->set_var('interviewer',$interviewer);
2271           }
2272           $this->t->set_var('interviewer_name',$interviewer_name);
2273           $this->t->fp('int_list','interviewer_list',True);
2274         }
2275       
2276       if($action == 'save')
2277         {
2278           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2279           $this->db->query("UPDATE eq_interview set " .
2280                      "   interview='" . $interview . "'" .
2281                     ", interviewer='" . $interviewer . "'" .
2282                           ", elder='" . $elder . "'" .
2283                         ", aaronic='" . $aaronic . "'" .
2284                            ", date='" . $date . "'" .
2285                           ", notes='" . $notes . "'" .
2286                            " WHERE interview=" . $interview,__LINE__,__FILE__);
2287           $this->int_view();
2288           return false;
2289         }
2290
2291       if($action == 'insert')
2292         {
2293           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2294           $this->db->query("INSERT INTO eq_interview (interviewer,elder,aaronic,date,notes) "
2295                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2296                            . $date . "','" . $notes ."')",__LINE__,__FILE__);
2297           $this->int_view();
2298           return false;
2299         }
2300       
2301       if($action == 'add')
2302         {
2303           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2304           $this->t->set_var('interview', '');
2305           $this->t->set_var('interviewer', $interviewer);
2306           $this->t->set_var('name',$name);
2307           $this->t->set_var('elder',$elder);
2308           $this->t->set_var('aaronic',$aaronic);
2309           $this->t->set_var('date','');
2310           $this->t->set_var('notes','');
2311           $this->t->set_var('lang_done','Cancel');
2312           $this->t->set_var('lang_action','Adding New Interview');
2313           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2314                                                                 . $interview . '&action=' . 'insert'));
2315         }
2316
2317       if($action == 'edit' || $action == 'view')
2318         {
2319           $sql = "SELECT * FROM eq_interview WHERE interview=".$interview;
2320           $this->db->query($sql,__LINE__,__FILE__);
2321           $this->db->next_record();
2322           $this->t->set_var('interview',$interview);
2323           $this->t->set_var('name',$name);
2324           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2325           $this->t->set_var('elder',$this->db->f('elder'));
2326           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2327           $this->t->set_var('date',$this->db->f('date'));
2328           $this->t->set_var('notes',$this->db->f('notes'));
2329         }
2330       
2331       if($action == 'edit')
2332         {
2333           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2334           $this->t->set_var('lang_done','Cancel');
2335           $this->t->set_var('lang_action','Editing Interview');
2336           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2337                                                                 . $interview . '&action=' . 'save'));
2338         }
2339
2340       if($action == 'view')
2341         {
2342           $date = $this->db->f('date');
2343           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2344           $this->t->set_var('readonly','READONLY');
2345           $this->t->set_var('disabled','DISABLED');
2346           $this->t->set_var('lang_done','Done');
2347           $this->t->set_var('lang_action','Viewing Interview');
2348           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2349                                                                 . $interview . '&action=' . 'edit'));
2350         }
2351       
2352       $this->t->set_var('lang_reset','Clear Form');
2353       $this->t->set_var('lang_add','Add Interview');
2354       $this->t->set_var('lang_save','Save Changes');
2355       $this->t->set_var('edithandle','');
2356       $this->t->set_var('addhandle','');
2357
2358       $this->t->pfp('out','form');
2359       
2360       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2361       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2362       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2363      
2364       $this->save_sessiondata(); 
2365     }
2366
2367   function vis_view()
2368     {
2369       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2370       $this->t->set_block('vis_view_t','visit_list','list1');
2371       $this->t->set_block('vis_view_t','family_list','list2');
2372
2373       $this->t->set_var('lang_name','Family Name');
2374       $this->t->set_var('lang_date','Date');
2375
2376       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2377       $this->t->set_var('vis_link_title','View Yearly Visits');
2378       
2379       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2380       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2381       
2382       
2383       $sql = "SELECT * FROM eq_visit WHERE companionship=0 ORDER BY date DESC";
2384       $this->db->query($sql,__LINE__,__FILE__);
2385       $total_records = $this->db->num_rows();
2386
2387       $i = 0;
2388       while ($this->db->next_record())
2389         {
2390           $visit_list[$i]['visit'] = $this->db->f('visit');
2391           $visit_list[$i]['family'] = $this->db->f('family');
2392           $visit_list[$i]['date']  = $this->db->f('date');
2393           $i++;
2394         }
2395             
2396       for ($i=0; $i < count($visit_list); $i++)
2397         {         
2398           $this->nextmatchs->template_alternate_row_color(&$this->t);
2399
2400           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2401           $this->db->query($sql,__LINE__,__FILE__);
2402           $this->db->next_record();
2403                   
2404           $this->t->set_var('family',$visit_list[$i]['family']);
2405           $this->t->set_var('family_name',$this->db->f('name'));
2406           $this->t->set_var('date',$visit_list[$i]['date']);
2407           
2408           $link_data['menuaction'] = 'eq.eq.vis_update';
2409           $link_data['visit'] = $visit_list[$i]['visit'];
2410           $link_data['name'] = $this->db->f('name');
2411           $link_data['date'] = $visit_list[$i]['date'];
2412           $link_data['action'] = 'view';
2413           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2414           $this->t->set_var('lang_view','View');
2415
2416           $link_data['menuaction'] = 'eq.eq.vis_update';
2417           $link_data['visit'] = $visit_list[$i]['visit'];
2418           $link_data['name'] = $this->db->f('name');
2419           $link_data['date'] = $visit_list[$i]['date'];
2420           $link_data['action'] = 'edit';
2421           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2422           $this->t->set_var('lang_edit','Edit');
2423
2424           $this->t->fp('list1','visit_list',True);
2425         }
2426
2427       // List the families that are available to record a visit against
2428       $sql = "SELECT * FROM eq_family WHERE valid=1";
2429       $this->db->query($sql,__LINE__,__FILE__);
2430       $total_records = $this->db->num_rows();
2431
2432       $i = 0;
2433       while ($this->db->next_record())
2434         {
2435           $family_names[$i] = $this->db->f('name');
2436           $family_ids[$i] = $this->db->f('family');
2437           $i++;
2438         } array_multisort($family_names, $family_ids);
2439       
2440       for ($i=0; $i < count($family_names); $i++)
2441         {
2442           $link_data['menuaction'] = 'eq.eq.vis_update';
2443           $link_data['visit'] = '';
2444           $link_data['family'] = $family_ids[$i];
2445           $link_data['action'] = 'add';
2446           $link_data['name'] = $family_names[$i];
2447           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2448
2449           $this->t->set_var('name',$family_names[$i]);
2450           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2451           else { $this->t->set_var('table_sep',"</td>"); }
2452           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2453
2454           $this->t->fp('list2','family_list',True);
2455         }   
2456
2457       $this->t->pfp('out','vis_view_t');
2458       $this->save_sessiondata(); 
2459     }
2460
2461   function vis_update()
2462     {
2463       $this->t->set_file(array('form' => 'vis_update.tpl'));
2464       $this->t->set_block('form','add','addhandle');
2465       $this->t->set_block('form','edit','edithandle');
2466       
2467       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2468       $this->t->set_var('readonly','');
2469       $this->t->set_var('disabled','');
2470       
2471       $action = get_var('action',array('GET','POST'));
2472       $visit = get_var('visit',array('GET','POST'));
2473       $family = get_var('family',array('GET','POST'));
2474       $name = get_var('name',array('GET','POST'));
2475       $date = get_var('date',array('GET','POST'));
2476       $notes = get_var('notes',array('GET','POST'));
2477       $companionship = 0;
2478       
2479       if($action == 'save')
2480         {
2481           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2482           $this->db->query("UPDATE eq_visit set " .
2483                            "  date='" . $date . "'" .
2484                           ", notes='" . $notes . "'" .
2485                            " WHERE visit=" . $visit,__LINE__,__FILE__);
2486           $this->vis_view();
2487           return false;
2488         }
2489
2490       if($action == 'insert')
2491         {
2492           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2493           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
2494                            . "VALUES ('" . $family . "','" . $companionship . "','"
2495                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
2496           $this->vis_view();
2497           return false;
2498         }
2499       
2500       if($action == 'add')
2501         {
2502           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2503           $this->t->set_var('family', $family);
2504           $this->t->set_var('visit', '');
2505           $this->t->set_var('name', $name);
2506           $this->t->set_var('date','');
2507           $this->t->set_var('notes','');
2508           $this->t->set_var('lang_done','Cancel');
2509           $this->t->set_var('lang_action','Adding New Visit');
2510           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
2511                                                                 . $family . '&action=' . 'insert'));
2512         }
2513
2514       if($action == 'edit' || $action == 'view')
2515         {
2516           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
2517           $this->db->query($sql,__LINE__,__FILE__);
2518           $this->db->next_record();
2519           $this->t->set_var('visit',$visit);
2520           $this->t->set_var('name',$name);
2521           $this->t->set_var('family', $family);
2522           $this->t->set_var('date',$this->db->f('date'));
2523           $this->t->set_var('notes',$this->db->f('notes'));
2524         }
2525       
2526       if($action == 'edit')
2527         {
2528           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2529           $this->t->set_var('lang_done','Cancel');
2530           $this->t->set_var('lang_action','Editing Visit');
2531           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2532                                                                 . $visit . '&action=' . 'save'));
2533         }
2534
2535       if($action == 'view')
2536         {
2537           $date = $this->db->f('date');
2538           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2539           $this->t->set_var('readonly','READONLY');
2540           $this->t->set_var('disabled','DISABLED');
2541           $this->t->set_var('lang_done','Done');
2542           $this->t->set_var('lang_action','Viewing Visit');
2543           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2544                                                                 . $visit . '&action=' . 'edit'));
2545         }
2546       
2547       $this->t->set_var('lang_reset','Clear Form');
2548       $this->t->set_var('lang_add','Add Visit');
2549       $this->t->set_var('lang_save','Save Changes');
2550       $this->t->set_var('edithandle','');
2551       $this->t->set_var('addhandle','');
2552
2553       $this->t->pfp('out','form');
2554       
2555       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
2556       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2557       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2558
2559       $this->save_sessiondata(); 
2560     }
2561
2562   function att_view()
2563     {
2564       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2565       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2566       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2567
2568       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
2569       $this->t->set_block('att_view_t','act_list','list');
2570
2571       $this->t->set_block('att_view_t','month_list','list1');
2572       $this->t->set_block('att_view_t','header_list','list2');
2573       $this->t->set_block('att_view_t','elder_list','list3');
2574       
2575       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2576       $num_quarters = get_var('num_quarters',array('GET','POST'));
2577       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
2578       $this->t->set_var('num_quarters',$num_quarters);
2579       $this->t->set_var('lang_filter','Filter');
2580       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2581       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
2582
2583       $num_months = $num_quarters * 3;
2584       $current_month = $this->current_month;
2585       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2586       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2587       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2588       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2589       
2590       $sql = "SELECT * FROM eq_elder where valid=1";
2591       $this->db->query($sql,__LINE__,__FILE__);
2592       $i=0;
2593       while ($this->db->next_record())
2594         {
2595           $elder_name[$i] = $this->db->f('name');
2596           $elder_id[$i] = $this->db->f('elder');
2597           $i++;
2598         }
2599       array_multisort($elder_name, $elder_id);
2600       
2601       // Create a list of sunday dates for a window of 3 months back and current month
2602       $i=0; 
2603       $last_time = 0; 
2604       $found_sunday = 0;
2605       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
2606       $last_date = explode("-",$sunday_list[0]['date']);
2607       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2608       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
2609       while($last_time < $time_limit)
2610       {
2611         $day = date("w",$last_time);
2612         if(date("w",$last_time) == 0) {
2613           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
2614           $last_date = explode("-",$sunday_list[$i]['date']);
2615           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2616           $sunday_list[$i]['day'] = $last_date[2];
2617           $sunday_list[$i]['month'] = date("M",$last_time);
2618           $sunday_list[$i]['year'] = $last_date[0];
2619           $found_sunday = 1; 
2620         }
2621         $last_time += 90000;
2622         if($found_sunday) { $i++; $found_sunday=0; }
2623       }
2624
2625       $total_elders = count($elder_id);
2626       $old_month=$sunday_list[0]['month']; $span=0;
2627       for ($i=0; $i < count($sunday_list); $i++) {
2628         $date = $sunday_list[$i]['date'];
2629         $this->t->set_var('date',$sunday_list[$i]['date']);
2630         $this->t->set_var('day',$sunday_list[$i]['day']);
2631         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
2632           if($i == count($sunday_list)-1) { $span++; }
2633           $cur_month = $sunday_list[$i]['month'];
2634           $old_month = $sunday_list[$i]['month'];         
2635           $link_data['menuaction'] = 'eq.eq.att_update';
2636           $link_data['month'] = $sunday_list[$i-1]['month'];
2637           $link_data['year'] = $sunday_list[$i-1]['year'];
2638           $link_data['action'] = 'update_month';
2639           $cur_month = $sunday_list[$i-1]['month'];
2640           $cur_year = $sunday_list[$i-1]['year'];
2641           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
2642           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2643           $this->t->set_var('month',$sunday_list[$i-1]['month']);
2644           $this->t->set_var('year',$sunday_list[$i-1]['year']);
2645           $this->t->set_var('span',$span); $span=0;
2646           $this->t->fp('list1','month_list',True);
2647         } $span++;
2648       }
2649       $this->t->set_var('total_elders',$total_elders);
2650       $this->t->set_var('header_row',$header_row);
2651       
2652       $elder_width=200; $att_width=25; $total_width=$elder_width; 
2653       for ($i=0; $i < count($sunday_list); $i++) {
2654         $link_data['menuaction'] = 'eq.eq.att_update';
2655         $link_data['month'] = $sunday_list[$i]['month'];
2656         $link_data['year'] = $sunday_list[$i]['year'];
2657         $link_data['day'] = $sunday_list[$i]['day'];
2658         $link_data['date'] = $sunday_list[$i]['date'];
2659         $link_data['action'] = 'update_day';
2660         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2661         $this->t->set_var('date',$sunday_list[$i]['date']);
2662         $this->t->set_var('day',$sunday_list[$i]['day']);
2663         $this->t->set_var('month',$sunday_list[$i]['month']);
2664         $this->t->set_var('year',$sunday_list[$i]['year']);
2665         $this->t->fp('list2','header_list',True);
2666         $total_width += $att_width;
2667         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
2668       }
2669
2670       for ($i=0; $i < count($elder_id); $i++) {
2671         $att_table = "";
2672         $this->nextmatchs->template_alternate_row_color(&$this->t);
2673         $this->t->set_var('elder_name',$elder_name[$i]);
2674         #print "checking for elder: " . $elder_id[$i] . "<br>";
2675         for ($j=0; $j < count($sunday_list); $j++) {
2676           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
2677           #print "SELECT * FROM eq_attendance WHERE date='"
2678           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
2679           $sql = "SELECT * FROM eq_attendance WHERE date='"
2680              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2681           $this->db->query($sql,__LINE__,__FILE__);
2682           if($this->db->next_record()) {
2683             $cur_month = $sunday_list[$j]['month'];
2684             if($attended[$i][$cur_month] != 1) { 
2685               $attended[$i][$cur_month]=1;
2686               $attendance[$monthnum[$cur_month]]++;
2687             } 
2688             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
2689           } else {
2690             $att_table .= '<td>&nbsp;</td>';
2691           }
2692         }
2693         $this->t->set_var('att_table',$att_table);
2694         $this->t->fp('list3','elder_list',True);
2695       }
2696       $this->t->set_var('total_width',$total_width);
2697       $this->t->set_var('elder_width',$elder_width);
2698       $this->t->set_var('att_width',$att_width);
2699
2700       # Now calculate attendance for these months
2701       $attendance_str = "";
2702       $nonattendance_str = "";
2703       $aveattendance_str = "";
2704       $avenonattendance_str = "";
2705       $num_months=0;
2706       $ave_total_attended=0;
2707       ksort($attendance);
2708       foreach($attendance as $att => $value) {
2709         $total_attended = $attendance[$att];
2710         $ave_total_attended += $attendance[$att]; $num_months++;
2711         $percent = ceil(($total_attended / $total_elders)*100);
2712         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2713         $total_nonattended = $total_elders - $total_attended;
2714         $percent = ceil(($total_nonattended / $total_elders)*100);
2715         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
2716         
2717         $total_attended = ceil(($ave_total_attended / $num_months));
2718         $percent = ceil(($total_attended / $total_elders)*100);
2719         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2720         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
2721         $percent = ceil(($total_attended / $total_elders)*100);
2722         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2723       }
2724       
2725       $this->t->set_var('attendance',$attendance_str);
2726       $this->t->set_var('aveattendance',$aveattendance_str);
2727       $this->t->set_var('nonattendance',$nonattendance_str);
2728       $this->t->set_var('avenonattendance',$avenonattendance_str);
2729       
2730       $this->t->pfp('out','att_view_t');
2731       $this->save_sessiondata(); 
2732     }
2733
2734   function att_update()
2735     {
2736       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2737       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2738       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2739
2740       $this->t->set_file(array('form' => 'att_update.tpl'));
2741       $this->t->set_block('form','edit','edithandle');
2742       
2743       $this->t->set_block('form','month_list','list1');
2744       $this->t->set_block('form','header_list','list2');
2745       $this->t->set_block('form','elder_list','list3');
2746
2747       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2748
2749       $action = get_var('action',array('GET','POST'));
2750       $month = get_var('month',array('GET','POST'));
2751       $year = get_var('year',array('GET','POST'));
2752       $day = get_var('day',array('GET','POST'));
2753       $date = get_var('date',array('GET','POST'));
2754
2755       if($action == 'save_month' || $action == 'save_day')
2756         {
2757            $new_data = get_var('elders_attended',array('POST'));
2758            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
2759
2760            if($action == 'save_month') {        
2761              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
2762            }
2763
2764            if($action == 'save_day') {        
2765              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
2766            }   
2767
2768            foreach ($new_data as $data)
2769            {
2770               $data_array = explode("-",$data);
2771               $elder = $data_array[0];
2772               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
2773               $this->db->query("INSERT INTO eq_attendance (elder,date) "
2774                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
2775            }
2776         
2777          $this->att_view();
2778          return false;    
2779         }
2780
2781       $sql = "SELECT * FROM eq_elder where valid=1";
2782       $this->db->query($sql,__LINE__,__FILE__);
2783       $i=0;
2784       while ($this->db->next_record())
2785         {
2786           $elder_name[$i] = $this->db->f('name');
2787           $elder_id[$i] = $this->db->f('elder');
2788           $i++;
2789         }
2790       array_multisort($elder_name, $elder_id);
2791       
2792       if($action == 'update_month')
2793       {
2794         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
2795         $i=0; 
2796         $last_time = 0; 
2797         $found_sunday = 0;
2798         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
2799         $last_date = explode("-",$sunday_list[0]['date']);
2800         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2801         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
2802         while($last_time <= $time_limit)
2803         {
2804           $day = date("w",$last_time);
2805           if(date("w",$last_time) == 0) { 
2806             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
2807             $last_date = explode("-",$sunday_list[$i]['date']);
2808             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2809             $sunday_list[$i]['day'] = $last_date[2];
2810             $sunday_list[$i]['month'] = date("M",$last_time);
2811             $sunday_list[$i]['year'] = $last_date[0];
2812             $found_sunday = 1; 
2813           }
2814           $last_time += 90000;
2815           if($found_sunday) { $i++; $found_sunday=0; }
2816         }
2817
2818         $this->t->set_var('span', $i);
2819         $this->t->set_var('month',$sunday_list[$i-1]['month']);
2820         $this->t->set_var('year',$sunday_list[$i-1]['year']);
2821         $this->t->fp('list1','month_list',True);
2822         $elder_width=200; $att_width=25; $total_width=$elder_width;
2823         for ($i=0; $i < count($sunday_list); $i++) {
2824           $link_data['menuaction'] = 'eq.eq.att_update';
2825           $link_data['month'] = $sunday_list[$i]['month'];
2826           $link_data['year'] = $sunday_list[$i]['year'];
2827           $link_data['day'] = $sunday_list[$i]['day'];
2828           $link_data['date'] = $sunday_list[$i]['date'];
2829           $link_data['action'] = 'update_day';
2830           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2831           $this->t->set_var('date',$sunday_list[$i]['date']);
2832           $this->t->set_var('day',$sunday_list[$i]['day']);
2833           $this->t->set_var('month',$sunday_list[$i]['month']);
2834           $this->t->set_var('year',$sunday_list[$i]['year']);
2835           $this->t->fp('list2','header_list',True);
2836           $total_width += $att_width;
2837         }     
2838       }
2839
2840       if($action == 'update_day')
2841       {
2842         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
2843         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
2844         $this->t->set_var('month',$month);
2845         $this->t->set_var('year',$year);
2846         $this->t->fp('list1','month_list',True);
2847         $this->t->set_var('date',$date);
2848         $this->t->set_var('day',$day);
2849         $this->t->set_var('month',$month);
2850         $this->t->set_var('year',$year);
2851         $this->t->fp('list2','header_list',True);
2852       }           
2853             
2854       for ($i=0; $i < count($elder_id); $i++) {
2855         $att_table = "";
2856         $this->nextmatchs->template_alternate_row_color(&$this->t);
2857         $this->t->set_var('elder_name',$elder_name[$i]);
2858         for ($j=0; $j < count($sunday_list); $j++) {
2859           $sql = "SELECT * FROM eq_attendance WHERE date='"
2860              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2861           $this->db->query($sql,__LINE__,__FILE__);
2862           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
2863           if($this->db->next_record()) {
2864             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
2865           } else {
2866             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
2867           }
2868         }
2869         $this->t->set_var('att_table',$att_table);
2870         $this->t->fp('list3','elder_list',True);
2871       } 
2872            
2873       $this->t->set_var('lang_done', 'Cancel');
2874       $this->t->set_var('lang_reset','Clear Form');
2875       $this->t->set_var('lang_save','Save Changes');
2876
2877       $this->t->pfp('out','form');
2878       $this->t->pfp('addhandle','edit');
2879
2880       $this->save_sessiondata();       
2881     }
2882
2883   function dir_view()
2884     {
2885       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
2886       $this->t->set_block('dir_view_t','dir_list','list');
2887       
2888       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
2889       $this->db->query($sql,__LINE__,__FILE__);
2890       $i=0;
2891       while ($this->db->next_record())
2892         {
2893           $parent[$i]['id'] = $this->db->f('parent');
2894           $parent[$i]['name'] = $this->db->f('name');
2895           $parent[$i]['phone'] = $this->db->f('phone');
2896           $parent[$i]['address'] = $this->db->f('address');
2897           $i++;
2898         }   
2899       
2900       for ($i=0; $i < count($parent); $i++) 
2901       {
2902         $name = $parent[$i]['name'];
2903         $phone = $parent[$i]['phone'];
2904         $address = $parent[$i]['address'];
2905         $this->t->set_var('name', $name);
2906         $this->t->set_var('address', $address);
2907         $this->t->set_var('phone', $phone);
2908         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2909         $this->t->set_var('tr_color',$tr_color);
2910         $this->t->fp('list','dir_list',True);
2911         //print "$phone $name $address<br>";
2912       }
2913       $this->t->pfp('out','dir_view_t');
2914       $this->save_sessiondata();   
2915     }
2916   
2917   function org_view()
2918     {
2919       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
2920       $this->t->set_block('org_view_t','calling_list','list1');
2921       $this->t->set_block('org_view_t','org_list','list2');
2922
2923       # Display a list ordered alphabetically
2924       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
2925       $this->db->query($sql,__LINE__,__FILE__);
2926       $i=0;
2927       while ($this->db->next_record())
2928         {
2929           $calling[$i]['id'] = $this->db->f('indiv_id');
2930           $calling[$i]['name'] = $this->db->f('name');
2931           $calling[$i]['position'] = $this->db->f('position');
2932           $calling[$i]['sustained'] = $this->db->f('sustained');
2933           $calling[$i]['organization'] = $this->db->f('organization');
2934           $i++;
2935         }   
2936       for ($i=0; $i < count($calling); $i++) 
2937       {
2938         $name = $calling[$i]['name'];
2939         $position = $calling[$i]['position'];
2940         $sustained = $calling[$i]['sustained'];
2941         $organization = $calling[$i]['organization'];
2942         $this->t->set_var('name', $name);
2943         $this->t->set_var('position', $position);
2944         $this->t->set_var('sustained', $sustained);
2945         $this->t->set_var('organization', $organization);
2946         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2947         $this->t->set_var('tr_color',$tr_color);
2948         $this->t->fp('list1','calling_list',True);
2949       }
2950
2951       # Display a list ordered by organization
2952       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
2953       $this->db->query($sql,__LINE__,__FILE__);
2954       $i=0;
2955       while ($this->db->next_record())
2956         {
2957           $calling[$i]['id'] = $this->db->f('indiv_id');
2958           $calling[$i]['name'] = $this->db->f('name');
2959           $calling[$i]['position'] = $this->db->f('position');
2960           $calling[$i]['sustained'] = $this->db->f('sustained');
2961           $calling[$i]['organization'] = $this->db->f('organization');
2962           $i++;
2963         }   
2964       for ($i=0; $i < count($calling); $i++) 
2965       {
2966         $name = $calling[$i]['name'];
2967         $position = $calling[$i]['position'];
2968         $sustained = $calling[$i]['sustained'];
2969         $organization = $calling[$i]['organization'];
2970         $this->t->set_var('name', $name);
2971         $this->t->set_var('position', $position);
2972         $this->t->set_var('sustained', $sustained);
2973         $this->t->set_var('organization', $organization);
2974         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2975         $this->t->set_var('tr_color',$tr_color);
2976         $this->t->fp('list2','org_list',True);
2977       }
2978       
2979       $this->t->pfp('out','org_view_t');
2980       $this->save_sessiondata();   
2981     }
2982   
2983   function schedule()
2984     {
2985       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
2986       $this->t->set_block('sched_t','presidency_list','list');
2987
2988       $action = get_var('action',array('GET','POST'));
2989       
2990       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
2991       $this->t->set_var('title','EQ Scheduling Tool');
2992
2993       $this->t->set_var('lang_save','Save Schedule');
2994       $this->t->set_var('lang_reset','Cancel');
2995       
2996       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2997       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2998
2999       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3000       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3001       
3002       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3003       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3004       
3005       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3006       $table_width=$date_width + $time_width + $elder_width + $family_width;
3007       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3008       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3009       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3010       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3011       $table_data = "";
3012
3013       if($action == 'save')
3014         {
3015           $new_data = get_var('sched',array('POST'));
3016           foreach ($new_data as $presidency_array)
3017            {
3018              foreach ($presidency_array as $entry)
3019                {
3020                  $presidency = $entry['presidency'];
3021                  $appointment = $entry['appointment'];
3022                  $date = $entry['date'];
3023                  $hour = $entry['hour'];
3024                  $minute = $entry['minute'];
3025                  $pm = $entry['pm'];
3026                  $elder = $entry['elder'];
3027                  $family = $entry['family'];
3028                  if($pm) { $hour = $hour + 12; }
3029                  $time = $hour.':'.$minute.':'.'00';
3030
3031                  // Update an existing appointment
3032                  if($appointment != 0)
3033                    {
3034                      $this->db->query("UPDATE eq_appointment set" .
3035                               " family=" . $family . 
3036                               " ,elder=" . $elder . 
3037                               " ,date='" . $date . "'" .
3038                               " ,time='" . $time . "'" .
3039                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3040
3041                      //print "updating entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                  
3042                    }
3043                  
3044                  // Add a new appointment
3045                  else if(($appointment == 0) && ($date != "") && ($time != ""))
3046                    {
3047                      $this->db->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time) "
3048                            . "VALUES ('" . $appointment . "','" . $presidency . "','" . $family . "','"
3049                            . $elder . "','" . $date . "','" . $time  ."')",__LINE__,__FILE__);
3050                      
3051                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                    
3052                    }
3053                }
3054            }
3055           
3056           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3057           Header('Location: ' . $take_me_to_url);
3058         }
3059
3060       $sql = "SELECT * FROM eq_presidency where valid=1";
3061       $this->db->query($sql,__LINE__,__FILE__);
3062       $i=0;
3063       while ($this->db->next_record())
3064         {
3065           $presidency_data[$i]['id'] = $this->db->f('presidency');
3066           $presidency_data[$i]['name'] = $this->db->f('name');
3067           $presidency_data[$i]['elder'] = $this->db->f('elder');
3068           $i++;
3069         }
3070       
3071       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3072       $this->db->query($sql,__LINE__,__FILE__);
3073       $i=0;
3074       while ($this->db->next_record())
3075         {
3076           $elder_id[$i] = $this->db->f('elder');
3077           $elder_name[$i] = $this->db->f('name');
3078           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3079           $i++;
3080         }
3081       array_multisort($elder_name, $elder_id);
3082
3083       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3084       $this->db->query($sql,__LINE__,__FILE__);
3085       $i=0;
3086       while ($this->db->next_record())
3087         {
3088           $family_id[$i] = $this->db->f('family');
3089           $family_name[$i] = $this->db->f('name');
3090           $i++;
3091         }
3092       array_multisort($family_name, $family_id);
3093       
3094       for ($i=0; $i < count($presidency_data); $i++) {
3095         $presidency = $presidency_data[$i]['id'];
3096         $interviewer = $presidency_data[$i]['elder'];
3097         $name = $presidency_data[$i]['name'];
3098         $this->t->set_var('presidency_name',$name);
3099         $table_data="";
3100         
3101         // query the database for all the appointments
3102         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3103         $this->db->query($sql,__LINE__,__FILE__);
3104
3105         // Prefill any existing appointment slots
3106         while ($this->db->next_record())
3107           {
3108             $appointment = $this->db->f('appointment');
3109             $elder = $this->db->f('elder');
3110             $family = $this->db->f('family');
3111             
3112             $date = $this->db->f('date');
3113             $date_array = explode("-",$date);
3114             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3115             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3116             
3117             $time = $this->db->f('time');
3118             $time_array = explode(":",$time);
3119             $hour = $time_array[0];
3120             $minute = $time_array[1];
3121             $pm = 0;
3122             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3123             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3124             
3125             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3126
3127             // Date selection
3128             $table_data.= '<td align=left>';
3129             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3130             $table_data.= '</td>';
3131             
3132             // Hour & Minutes selection
3133             $table_data.= "<td align=center>";
3134             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3135             foreach(range(1,12) as $num) {
3136               if($hour == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3137               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3138             }
3139             $table_data.= '</select>';
3140             $table_data.= '&nbsp;:&nbsp;';
3141             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3142             foreach(range(0,3) as $num) {
3143               $num = $num * 15; if($num == 0) { $num = "00"; }
3144               if($minute == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3145               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3146             }
3147             $table_data.= '</select>';
3148             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3149             if($pm == 0) { $table_data.= '<option value=0 selected>am</option>'; $table_data.= '<option value=1>pm</option>'; }
3150             else { $table_data.= '<option value=0>am</option>'; $table_data.= '<option value=1 selected>pm</option>'; }
3151             $table_data.= '</select>';
3152             $table_data.= "</td>";
3153             
3154             // Elder drop down list (for PPIs)
3155             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3156             $table_data.= '<option value=0></option>';  
3157             for ($j=0; $j < count($elder_id); $j++) {
3158               $id = $elder_id[$j];
3159               $name = $elder_name[$j];
3160               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3161               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3162             }
3163             $table_data.='</select></td>';
3164
3165             // Family drop down list (for Visits)
3166             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3167             $table_data.= '<option value=0></option>';              
3168             for ($j=0; $j < count($elder_id); $j++) {
3169               $id = $family_id[$j];
3170               $name = $family_name[$j];
3171               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3172               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3173             }
3174             $table_data.='</select></td>';
3175             
3176             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3177             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3178         
3179             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3180             $this->t->set_var('tr_color',$tr_color);
3181             
3182           }
3183
3184         // Create blank appointment slot
3185         $appointment = 0;
3186         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3187
3188         // Date selection
3189         $table_data.= '<td align=left>';
3190         $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3191         $table_data.= '</td>';
3192         
3193         // Time selection
3194         $table_data.= "<td align=center>";
3195         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3196         $table_data.= '<option value=""></option>';
3197         foreach(range(1,12) as $num) {
3198           $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3199         }
3200         $table_data.= '</select>';
3201         $table_data.= '&nbsp;:&nbsp;';
3202         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3203         $table_data.= '<option value=""></option>';
3204         foreach(range(0,3) as $num) {
3205           $num = $num * 15; if($num == 0) { $num = "00"; }
3206           $table_data.= '<option value='.$num.'>'.$num.'</option>';
3207         }
3208         $table_data.= '</select>';
3209         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3210         $table_data.= '<option value=""></option>';
3211         $table_data.= '<option value=0>am</option>';
3212         $table_data.= '<option value=1>pm</option>';
3213         $table_data.= '</select>';
3214         $table_data.= "</td>";
3215         
3216         // Elder drop down list
3217         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3218         $table_data.= '<option value=0></option>';  
3219         for ($j=0; $j < count($elder_id); $j++) {
3220           $id = $elder_id[$j];
3221           $name = $elder_name[$j];
3222           $table_data.= '<option value='.$id.'>'.$name.'</option>';
3223         }
3224         $table_data.='</select></td>';
3225         
3226         // Family drop down list
3227         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3228         $table_data.= '<option value=0></option>';          
3229         for ($j=0; $j < count($elder_id); $j++) {
3230           $id = $family_id[$j];
3231           $name = $family_name[$j];
3232           $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3233         }
3234         $table_data.='</select></td>';
3235
3236         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3237         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3238         
3239         $this->t->set_var('table_data',$table_data);
3240         $this->t->set_var('header_row',$header_row);
3241         $this->t->set_var('table_width',$table_width);
3242         $this->t->fp('list','presidency_list',True);
3243         
3244       }
3245       
3246       $this->t->pfp('out','sched_t');
3247       $this->save_sessiondata();   
3248     }
3249   
3250   function admin()
3251     {
3252       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3253       $this->t->set_block('admin_t','upload','uploadhandle');
3254       $this->t->set_block('admin_t','admin','adminhandle');
3255       $this->t->set_block('admin_t','cmd','cmdhandle');
3256       
3257       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3258       
3259       $action = get_var('action',array('GET','POST'));
3260
3261       $this->t->pfp('out','admin_t');
3262       
3263       if($action == 'upload')
3264         {        
3265           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
3266           
3267           if((($_FILES['uploadedfile']['type'] == "application/zip") ||
3268               ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3269               ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3270               ($_FILES['uploadedfile']['type'] == "application/octet-stream")) &&
3271              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
3272             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3273             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3274             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3275             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3276             $this->t->set_var('uploadstatus',$uploadstatus);
3277             $this->t->pfp('uploadhandle','upload');
3278             $this->t->set_var('uploadhandle','');
3279             print "<table border=1 width=80%><tr><td>\n<pre>";
3280             
3281             # make a directory for this data to be stored in
3282             $date="data_" . date("Y_m_d");
3283             $data_dir = $this->upload_target_path . $date;
3284             print "-> Making the data directory: $date<br>\n";
3285             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
3286             if($return_code != 0) {
3287               print implode('\n',$result) . "<br>";
3288               print "<b><font color=red>";
3289               print "-E- Unable to create the data directory. Aborting import.";
3290               print "</font></b>";
3291               return 0;
3292             }
3293
3294             # move the file uploaded into this directory
3295             print "-> Moving the uploaded file into the data dir<br>\n";
3296             exec('mv ' . $target_path . ' ' . $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 move the uploaded file into the data dir. Aborting import.";
3301               print "</font></b>";
3302               return 0;
3303             }
3304             
3305             # unzip the data into this directory
3306             print "-> Unzipping the data<br>\n";
3307             $data_file = $data_dir . '';
3308             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3309             if($return_code != 0) {
3310               print implode('\n',$result) . "<br>";
3311               print "<b><font color=red>";
3312               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
3313               print "</font></b>";
3314               return 0;
3315             }
3316             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3317
3318             # update the data_latest link to point to this new directory
3319             print "-> Updating the latest data dir link<br>\n";
3320             $data_latest = $this->upload_target_path . 'data_latest';
3321             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3322             if($return_code != 0) {
3323               print implode('\n',$result) . "<br>";
3324               print "<b><font color=red>";
3325               print "-E- Unable to update the data latest link. Aborting import.";
3326               print "</font></b>";
3327               return 0;
3328             }
3329             
3330             # run the import perl script to encorporate it into the DB
3331             ob_start('ob_logstdout', 2);
3332             print "-> Importing the data into the EQ database<br>\n";
3333             ob_flush(); flush(); sleep(1);
3334             $import_log = $this->upload_target_path . '/import.log';
3335             $data_log = $this->upload_target_path . '/data.log';
3336             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
3337             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
3338             #print "import_cmd: $import_cmd<br>";
3339             #print "parse_cmd: $parse_cmd<br>";
3340             ob_start('ob_logstdout', 2);
3341             passthru($import_cmd);
3342             passthru($parse_cmd);
3343             ob_flush(); flush(); sleep(1);
3344
3345             # fix the permissions of the data dir
3346             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3347             
3348             $this->t->pfp('cmdhandle','cmd');
3349             print "</pre></td></tr></table>";
3350             
3351           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3352                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3353                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3354                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3355             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3356             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3357             $this->t->set_var('uploadstatus',$uploadstatus);
3358             $this->t->pfp('uploadhandle','upload',True);
3359           } else {
3360             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3361             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3362             $this->t->set_var('uploadstatus',$uploadstatus);
3363             $this->t->pfp('uploadhandle','upload',True);
3364           }
3365         }
3366       else
3367         {
3368           $this->t->set_var('adminhandle','');
3369           $this->t->pfp('adminhandle','admin'); 
3370         }
3371       
3372       $this->save_sessiondata();   
3373     }
3374   
3375 }
3376
3377 ?>