Added new 'attending' flag to eq_elder table.
[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_quarters;
26   var $default_int_num_years;
27   var $default_vis_num_years;
28   var $default_att_num_quarters;
29   var $current_year;
30   var $current_month;
31   var $upload_target_path;
32   var $script_path;
33   
34   var $public_functions = array
35     (
36      'ht_view'    => True,
37      'ht_update'  => True,
38      'act_list'   => True,
39      'act_view'   => True,
40      'act_update' => True,
41      'par_view'   => True,
42      'ppi_view'   => True,
43      'ppi_update' => True,
44      'ppi_sched'  => True,
45      'int_view'   => True,
46      'int_update' => True,
47      'int_sched'  => True,
48      'vis_sched'  => True,
49      'vis_view'   => True,
50      'vis_update' => True,
51      'att_view'   => True,
52      'att_update' => True,
53      'dir_view'   => True,
54      'org_view'   => True,
55      'schedule'   => True,
56      'admin'      => True
57      );
58  
59   function eq()
60     {
61       $this->default_ht_num_months = 3;
62       $this->default_ppi_num_months = 3;
63       $this->default_ppi_num_years = 0;
64       $this->default_int_num_quarters = 1;
65       $this->default_int_num_years = 0;
66       $this->default_att_num_quarters = 1;
67       $this->default_vis_num_years = 1;
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       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2383       $num_years = get_var('num_years',array('GET','POST'));
2384       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2385       $this->t->set_var('num_years',$num_years);
2386       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2387       else {  $this->t->set_var('lang_num_years','Years of History'); }
2388       $this->t->set_var('lang_filter','Filter');
2389       
2390       $year = date('Y') - $num_years + 1;
2391       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2392       
2393       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2394       $this->db->query($sql,__LINE__,__FILE__);
2395       $total_records = $this->db->num_rows();
2396
2397       $i = 0;
2398       while ($this->db->next_record())
2399         {
2400           $visit_list[$i]['visit'] = $this->db->f('visit');
2401           $visit_list[$i]['family'] = $this->db->f('family');
2402           $visit_list[$i]['date']  = $this->db->f('date');
2403           $i++;
2404         }
2405             
2406       for ($i=0; $i < count($visit_list); $i++)
2407         {         
2408           $this->nextmatchs->template_alternate_row_color(&$this->t);
2409
2410           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2411           $this->db->query($sql,__LINE__,__FILE__);
2412           $this->db->next_record();
2413                   
2414           $this->t->set_var('family',$visit_list[$i]['family']);
2415           $this->t->set_var('family_name',$this->db->f('name'));
2416           $this->t->set_var('date',$visit_list[$i]['date']);
2417           
2418           $link_data['menuaction'] = 'eq.eq.vis_update';
2419           $link_data['visit'] = $visit_list[$i]['visit'];
2420           $link_data['name'] = $this->db->f('name');
2421           $link_data['date'] = $visit_list[$i]['date'];
2422           $link_data['action'] = 'view';
2423           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2424           $this->t->set_var('lang_view','View');
2425
2426           $link_data['menuaction'] = 'eq.eq.vis_update';
2427           $link_data['visit'] = $visit_list[$i]['visit'];
2428           $link_data['name'] = $this->db->f('name');
2429           $link_data['date'] = $visit_list[$i]['date'];
2430           $link_data['action'] = 'edit';
2431           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2432           $this->t->set_var('lang_edit','Edit');
2433
2434           $this->t->fp('list1','visit_list',True);
2435         }
2436
2437       // List the families that are available to record a visit against
2438       $sql = "SELECT * FROM eq_family WHERE valid=1";
2439       $this->db->query($sql,__LINE__,__FILE__);
2440       $total_records = $this->db->num_rows();
2441
2442       $i = 0;
2443       while ($this->db->next_record())
2444         {
2445           $family_names[$i] = $this->db->f('name');
2446           $family_ids[$i] = $this->db->f('family');
2447           $i++;
2448         } array_multisort($family_names, $family_ids);
2449       
2450       for ($i=0; $i < count($family_names); $i++)
2451         {
2452           $link_data['menuaction'] = 'eq.eq.vis_update';
2453           $link_data['visit'] = '';
2454           $link_data['family'] = $family_ids[$i];
2455           $link_data['action'] = 'add';
2456           $link_data['name'] = $family_names[$i];
2457           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2458
2459           $this->t->set_var('name',$family_names[$i]);
2460           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2461           else { $this->t->set_var('table_sep',"</td>"); }
2462           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2463
2464           $this->t->fp('list2','family_list',True);
2465         }   
2466
2467       $this->t->pfp('out','vis_view_t');
2468       $this->save_sessiondata(); 
2469     }
2470
2471   function vis_update()
2472     {
2473       $this->t->set_file(array('form' => 'vis_update.tpl'));
2474       $this->t->set_block('form','add','addhandle');
2475       $this->t->set_block('form','edit','edithandle');
2476       
2477       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2478       $this->t->set_var('readonly','');
2479       $this->t->set_var('disabled','');
2480       
2481       $action = get_var('action',array('GET','POST'));
2482       $visit = get_var('visit',array('GET','POST'));
2483       $family = get_var('family',array('GET','POST'));
2484       $name = get_var('name',array('GET','POST'));
2485       $date = get_var('date',array('GET','POST'));
2486       $notes = get_var('notes',array('GET','POST'));
2487       $companionship = 0;
2488       
2489       if($action == 'save')
2490         {
2491           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2492           $this->db->query("UPDATE eq_visit set " .
2493                            "  date='" . $date . "'" .
2494                           ", notes='" . $notes . "'" .
2495                            " WHERE visit=" . $visit,__LINE__,__FILE__);
2496           $this->vis_view();
2497           return false;
2498         }
2499
2500       if($action == 'insert')
2501         {
2502           $notes = $this->db->db_addslashes(get_var('notes',array('POST')));
2503           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
2504                            . "VALUES ('" . $family . "','" . $companionship . "','"
2505                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
2506           $this->vis_view();
2507           return false;
2508         }
2509       
2510       if($action == 'add')
2511         {
2512           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2513           $this->t->set_var('family', $family);
2514           $this->t->set_var('visit', '');
2515           $this->t->set_var('name', $name);
2516           $this->t->set_var('date','');
2517           $this->t->set_var('notes','');
2518           $this->t->set_var('lang_done','Cancel');
2519           $this->t->set_var('lang_action','Adding New Visit');
2520           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
2521                                                                 . $family . '&action=' . 'insert'));
2522         }
2523
2524       if($action == 'edit' || $action == 'view')
2525         {
2526           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
2527           $this->db->query($sql,__LINE__,__FILE__);
2528           $this->db->next_record();
2529           $this->t->set_var('visit',$visit);
2530           $this->t->set_var('name',$name);
2531           $this->t->set_var('family', $family);
2532           $this->t->set_var('date',$this->db->f('date'));
2533           $this->t->set_var('notes',$this->db->f('notes'));
2534         }
2535       
2536       if($action == 'edit')
2537         {
2538           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2539           $this->t->set_var('lang_done','Cancel');
2540           $this->t->set_var('lang_action','Editing Visit');
2541           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2542                                                                 . $visit . '&action=' . 'save'));
2543         }
2544
2545       if($action == 'view')
2546         {
2547           $date = $this->db->f('date');
2548           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2549           $this->t->set_var('readonly','READONLY');
2550           $this->t->set_var('disabled','DISABLED');
2551           $this->t->set_var('lang_done','Done');
2552           $this->t->set_var('lang_action','Viewing Visit');
2553           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
2554                                                                 . $visit . '&action=' . 'edit'));
2555         }
2556       
2557       $this->t->set_var('lang_reset','Clear Form');
2558       $this->t->set_var('lang_add','Add Visit');
2559       $this->t->set_var('lang_save','Save Changes');
2560       $this->t->set_var('edithandle','');
2561       $this->t->set_var('addhandle','');
2562
2563       $this->t->pfp('out','form');
2564       
2565       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
2566       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2567       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2568
2569       $this->save_sessiondata(); 
2570     }
2571
2572   function att_view()
2573     {
2574       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2575       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2576       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2577
2578       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
2579       $this->t->set_block('att_view_t','act_list','list');
2580
2581       $this->t->set_block('att_view_t','month_list','list1');
2582       $this->t->set_block('att_view_t','header_list','list2');
2583       $this->t->set_block('att_view_t','elder_list','list3');
2584       
2585       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2586       $num_quarters = get_var('num_quarters',array('GET','POST'));
2587       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
2588       $this->t->set_var('num_quarters',$num_quarters);
2589       $this->t->set_var('lang_filter','Filter');
2590       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2591       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
2592
2593       $num_months = $num_quarters * 3;
2594       $current_month = $this->current_month;
2595       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2596       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2597       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2598       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2599       
2600       $sql = "SELECT * FROM eq_elder where valid=1";
2601       $this->db->query($sql,__LINE__,__FILE__);
2602       $i=0;
2603       while ($this->db->next_record())
2604         {
2605           $elder_name[$i] = $this->db->f('name');
2606           $elder_id[$i] = $this->db->f('elder');
2607           $i++;
2608         }
2609       array_multisort($elder_name, $elder_id);
2610       
2611       // Create a list of sunday dates for a window of 3 months back and current month
2612       $i=0; 
2613       $last_time = 0; 
2614       $found_sunday = 0;
2615       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
2616       $last_date = explode("-",$sunday_list[0]['date']);
2617       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2618       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
2619       while($last_time < $time_limit)
2620       {
2621         $day = date("w",$last_time);
2622         if(date("w",$last_time) == 0) {
2623           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
2624           $last_date = explode("-",$sunday_list[$i]['date']);
2625           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2626           $sunday_list[$i]['day'] = $last_date[2];
2627           $sunday_list[$i]['month'] = date("M",$last_time);
2628           $sunday_list[$i]['year'] = $last_date[0];
2629           $found_sunday = 1; 
2630         }
2631         $last_time += 90000;
2632         if($found_sunday) { $i++; $found_sunday=0; }
2633       }
2634
2635       $total_elders = count($elder_id);
2636       $old_month=$sunday_list[0]['month']; $span=0;
2637       for ($i=0; $i < count($sunday_list); $i++) {
2638         $date = $sunday_list[$i]['date'];
2639         $this->t->set_var('date',$sunday_list[$i]['date']);
2640         $this->t->set_var('day',$sunday_list[$i]['day']);
2641         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
2642           if($i == count($sunday_list)-1) { $span++; }
2643           $cur_month = $sunday_list[$i]['month'];
2644           $old_month = $sunday_list[$i]['month'];         
2645           $link_data['menuaction'] = 'eq.eq.att_update';
2646           $link_data['month'] = $sunday_list[$i-1]['month'];
2647           $link_data['year'] = $sunday_list[$i-1]['year'];
2648           $link_data['action'] = 'update_month';
2649           $cur_month = $sunday_list[$i-1]['month'];
2650           $cur_year = $sunday_list[$i-1]['year'];
2651           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
2652           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2653           $this->t->set_var('month',$sunday_list[$i-1]['month']);
2654           $this->t->set_var('year',$sunday_list[$i-1]['year']);
2655           $this->t->set_var('span',$span); $span=0;
2656           $this->t->fp('list1','month_list',True);
2657         } $span++;
2658       }
2659       $this->t->set_var('total_elders',$total_elders);
2660       $this->t->set_var('header_row',$header_row);
2661       
2662       $elder_width=200; $att_width=25; $total_width=$elder_width; 
2663       for ($i=0; $i < count($sunday_list); $i++) {
2664         $link_data['menuaction'] = 'eq.eq.att_update';
2665         $link_data['month'] = $sunday_list[$i]['month'];
2666         $link_data['year'] = $sunday_list[$i]['year'];
2667         $link_data['day'] = $sunday_list[$i]['day'];
2668         $link_data['date'] = $sunday_list[$i]['date'];
2669         $link_data['action'] = 'update_day';
2670         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2671         $this->t->set_var('date',$sunday_list[$i]['date']);
2672         $this->t->set_var('day',$sunday_list[$i]['day']);
2673         $this->t->set_var('month',$sunday_list[$i]['month']);
2674         $this->t->set_var('year',$sunday_list[$i]['year']);
2675         $this->t->fp('list2','header_list',True);
2676         $total_width += $att_width;
2677         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
2678       }
2679
2680       for ($i=0; $i < count($elder_id); $i++) {
2681         $att_table = "";
2682         $this->nextmatchs->template_alternate_row_color(&$this->t);
2683         $this->t->set_var('elder_name',$elder_name[$i]);
2684         #print "checking for elder: " . $elder_id[$i] . "<br>";
2685         for ($j=0; $j < count($sunday_list); $j++) {
2686           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
2687           #print "SELECT * FROM eq_attendance WHERE date='"
2688           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
2689           $sql = "SELECT * FROM eq_attendance WHERE date='"
2690              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2691           $this->db->query($sql,__LINE__,__FILE__);
2692           if($this->db->next_record()) {
2693             $cur_month = $sunday_list[$j]['month'];
2694             if($attended[$i][$cur_month] != 1) { 
2695               $attended[$i][$cur_month]=1;
2696               $attendance[$monthnum[$cur_month]]++;
2697             } 
2698             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
2699           } else {
2700             $att_table .= '<td>&nbsp;</td>';
2701           }
2702         }
2703         $this->t->set_var('att_table',$att_table);
2704         $this->t->fp('list3','elder_list',True);
2705       }
2706       $this->t->set_var('total_width',$total_width);
2707       $this->t->set_var('elder_width',$elder_width);
2708       $this->t->set_var('att_width',$att_width);
2709
2710       # Now calculate attendance for these months
2711       $attendance_str = "";
2712       $nonattendance_str = "";
2713       $aveattendance_str = "";
2714       $avenonattendance_str = "";
2715       $num_months=0;
2716       $ave_total_attended=0;
2717       ksort($attendance);
2718       foreach($attendance as $att => $value) {
2719         $total_attended = $attendance[$att];
2720         $ave_total_attended += $attendance[$att]; $num_months++;
2721         $percent = ceil(($total_attended / $total_elders)*100);
2722         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2723         $total_nonattended = $total_elders - $total_attended;
2724         $percent = ceil(($total_nonattended / $total_elders)*100);
2725         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
2726         
2727         $total_attended = ceil(($ave_total_attended / $num_months));
2728         $percent = ceil(($total_attended / $total_elders)*100);
2729         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2730         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
2731         $percent = ceil(($total_attended / $total_elders)*100);
2732         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
2733       }
2734       
2735       $this->t->set_var('attendance',$attendance_str);
2736       $this->t->set_var('aveattendance',$aveattendance_str);
2737       $this->t->set_var('nonattendance',$nonattendance_str);
2738       $this->t->set_var('avenonattendance',$avenonattendance_str);
2739       
2740       $this->t->pfp('out','att_view_t');
2741       $this->save_sessiondata(); 
2742     }
2743
2744   function att_update()
2745     {
2746       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
2747       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
2748       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
2749
2750       $this->t->set_file(array('form' => 'att_update.tpl'));
2751       $this->t->set_block('form','edit','edithandle');
2752       
2753       $this->t->set_block('form','month_list','list1');
2754       $this->t->set_block('form','header_list','list2');
2755       $this->t->set_block('form','elder_list','list3');
2756
2757       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
2758
2759       $action = get_var('action',array('GET','POST'));
2760       $month = get_var('month',array('GET','POST'));
2761       $year = get_var('year',array('GET','POST'));
2762       $day = get_var('day',array('GET','POST'));
2763       $date = get_var('date',array('GET','POST'));
2764
2765       if($action == 'save_month' || $action == 'save_day')
2766         {
2767            $new_data = get_var('elders_attended',array('POST'));
2768            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
2769
2770            if($action == 'save_month') {        
2771              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
2772            }
2773
2774            if($action == 'save_day') {        
2775              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
2776            }   
2777
2778            foreach ($new_data as $data)
2779            {
2780               $data_array = explode("-",$data);
2781               $elder = $data_array[0];
2782               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
2783               $this->db->query("INSERT INTO eq_attendance (elder,date) "
2784                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
2785            }
2786         
2787          $this->att_view();
2788          return false;    
2789         }
2790
2791       $sql = "SELECT * FROM eq_elder where valid=1";
2792       $this->db->query($sql,__LINE__,__FILE__);
2793       $i=0;
2794       while ($this->db->next_record())
2795         {
2796           $elder_name[$i] = $this->db->f('name');
2797           $elder_id[$i] = $this->db->f('elder');
2798           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
2799           $i++;
2800         }
2801       array_multisort($elder_name, $elder_id);
2802       
2803       if($action == 'update_month')
2804       {
2805         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
2806         $i=0; 
2807         $last_time = 0; 
2808         $found_sunday = 0;
2809         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
2810         $last_date = explode("-",$sunday_list[0]['date']);
2811         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2812         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
2813         while($last_time <= $time_limit)
2814         {
2815           $day = date("w",$last_time);
2816           if(date("w",$last_time) == 0) { 
2817             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
2818             $last_date = explode("-",$sunday_list[$i]['date']);
2819             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
2820             $sunday_list[$i]['day'] = $last_date[2];
2821             $sunday_list[$i]['month'] = date("M",$last_time);
2822             $sunday_list[$i]['year'] = $last_date[0];
2823             $found_sunday = 1; 
2824           }
2825           $last_time += 90000;
2826           if($found_sunday) { $i++; $found_sunday=0; }
2827         }
2828
2829         $this->t->set_var('span', $i);
2830         $this->t->set_var('month',$sunday_list[$i-1]['month']);
2831         $this->t->set_var('year',$sunday_list[$i-1]['year']);
2832         $this->t->fp('list1','month_list',True);
2833         $elder_width=200; $att_width=25; $total_width=$elder_width;
2834         for ($i=0; $i < count($sunday_list); $i++) {
2835           $link_data['menuaction'] = 'eq.eq.att_update';
2836           $link_data['month'] = $sunday_list[$i]['month'];
2837           $link_data['year'] = $sunday_list[$i]['year'];
2838           $link_data['day'] = $sunday_list[$i]['day'];
2839           $link_data['date'] = $sunday_list[$i]['date'];
2840           $link_data['action'] = 'update_day';
2841           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2842           $this->t->set_var('date',$sunday_list[$i]['date']);
2843           $this->t->set_var('day',$sunday_list[$i]['day']);
2844           $this->t->set_var('month',$sunday_list[$i]['month']);
2845           $this->t->set_var('year',$sunday_list[$i]['year']);
2846           $this->t->fp('list2','header_list',True);
2847           $total_width += $att_width;
2848         }     
2849       }
2850
2851       if($action == 'update_day')
2852       {
2853         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
2854         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
2855         $this->t->set_var('month',$month);
2856         $this->t->set_var('year',$year);
2857         $this->t->fp('list1','month_list',True);
2858         $this->t->set_var('date',$date);
2859         $this->t->set_var('day',$day);
2860         $this->t->set_var('month',$month);
2861         $this->t->set_var('year',$year);
2862         $this->t->fp('list2','header_list',True);
2863       }           
2864             
2865       for ($i=0; $i < count($elder_id); $i++) {
2866         $att_table = "";
2867         $this->nextmatchs->template_alternate_row_color(&$this->t);
2868         $this->t->set_var('elder_name',$elder_name[$i]);
2869         for ($j=0; $j < count($sunday_list); $j++) {
2870           $sql = "SELECT * FROM eq_attendance WHERE date='"
2871              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
2872           $this->db->query($sql,__LINE__,__FILE__);
2873           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
2874           if($this->db->next_record()) {
2875             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
2876           } else if($elder_attending[$elder_id[$i]] == 1) {
2877             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
2878           } else {
2879             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
2880           }
2881         }
2882         $this->t->set_var('att_table',$att_table);
2883         $this->t->fp('list3','elder_list',True);
2884       } 
2885            
2886       $this->t->set_var('lang_done', 'Cancel');
2887       $this->t->set_var('lang_reset','Clear Form');
2888       $this->t->set_var('lang_save','Save Changes');
2889
2890       $this->t->pfp('out','form');
2891       $this->t->pfp('addhandle','edit');
2892
2893       $this->save_sessiondata();       
2894     }
2895
2896   function dir_view()
2897     {
2898       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
2899       $this->t->set_block('dir_view_t','dir_list','list');
2900       
2901       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
2902       $this->db->query($sql,__LINE__,__FILE__);
2903       $i=0;
2904       while ($this->db->next_record())
2905         {
2906           $parent[$i]['id'] = $this->db->f('parent');
2907           $parent[$i]['name'] = $this->db->f('name');
2908           $parent[$i]['phone'] = $this->db->f('phone');
2909           $parent[$i]['address'] = $this->db->f('address');
2910           $i++;
2911         }   
2912       
2913       for ($i=0; $i < count($parent); $i++) 
2914       {
2915         $name = $parent[$i]['name'];
2916         $phone = $parent[$i]['phone'];
2917         $address = $parent[$i]['address'];
2918         $this->t->set_var('name', $name);
2919         $this->t->set_var('address', $address);
2920         $this->t->set_var('phone', $phone);
2921         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2922         $this->t->set_var('tr_color',$tr_color);
2923         $this->t->fp('list','dir_list',True);
2924         //print "$phone $name $address<br>";
2925       }
2926       $this->t->pfp('out','dir_view_t');
2927       $this->save_sessiondata();   
2928     }
2929   
2930   function org_view()
2931     {
2932       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
2933       $this->t->set_block('org_view_t','calling_list','list1');
2934       $this->t->set_block('org_view_t','org_list','list2');
2935
2936       # Display a list ordered alphabetically
2937       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
2938       $this->db->query($sql,__LINE__,__FILE__);
2939       $i=0;
2940       while ($this->db->next_record())
2941         {
2942           $calling[$i]['id'] = $this->db->f('indiv_id');
2943           $calling[$i]['name'] = $this->db->f('name');
2944           $calling[$i]['position'] = $this->db->f('position');
2945           $calling[$i]['sustained'] = $this->db->f('sustained');
2946           $calling[$i]['organization'] = $this->db->f('organization');
2947           $i++;
2948         }   
2949       for ($i=0; $i < count($calling); $i++) 
2950       {
2951         $name = $calling[$i]['name'];
2952         $position = $calling[$i]['position'];
2953         $sustained = $calling[$i]['sustained'];
2954         $organization = $calling[$i]['organization'];
2955         $this->t->set_var('name', $name);
2956         $this->t->set_var('position', $position);
2957         $this->t->set_var('sustained', $sustained);
2958         $this->t->set_var('organization', $organization);
2959         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2960         $this->t->set_var('tr_color',$tr_color);
2961         $this->t->fp('list1','calling_list',True);
2962       }
2963
2964       # Display a list ordered by organization
2965       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
2966       $this->db->query($sql,__LINE__,__FILE__);
2967       $i=0;
2968       while ($this->db->next_record())
2969         {
2970           $calling[$i]['id'] = $this->db->f('indiv_id');
2971           $calling[$i]['name'] = $this->db->f('name');
2972           $calling[$i]['position'] = $this->db->f('position');
2973           $calling[$i]['sustained'] = $this->db->f('sustained');
2974           $calling[$i]['organization'] = $this->db->f('organization');
2975           $i++;
2976         }   
2977       for ($i=0; $i < count($calling); $i++) 
2978       {
2979         $name = $calling[$i]['name'];
2980         $position = $calling[$i]['position'];
2981         $sustained = $calling[$i]['sustained'];
2982         $organization = $calling[$i]['organization'];
2983         $this->t->set_var('name', $name);
2984         $this->t->set_var('position', $position);
2985         $this->t->set_var('sustained', $sustained);
2986         $this->t->set_var('organization', $organization);
2987         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2988         $this->t->set_var('tr_color',$tr_color);
2989         $this->t->fp('list2','org_list',True);
2990       }
2991       
2992       $this->t->pfp('out','org_view_t');
2993       $this->save_sessiondata();   
2994     }
2995   
2996   function schedule()
2997     {
2998       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
2999       $this->t->set_block('sched_t','presidency_list','list');
3000
3001       $action = get_var('action',array('GET','POST'));
3002       
3003       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3004       $this->t->set_var('title','EQ Scheduling Tool');
3005
3006       $this->t->set_var('lang_save','Save Schedule');
3007       $this->t->set_var('lang_reset','Cancel');
3008       
3009       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3010       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3011
3012       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3013       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3014       
3015       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3016       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3017       
3018       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3019       $table_width=$date_width + $time_width + $elder_width + $family_width;
3020       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3021       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3022       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3023       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3024       $table_data = "";
3025
3026       if($action == 'save')
3027         {
3028           $new_data = get_var('sched',array('POST'));
3029           foreach ($new_data as $presidency_array)
3030            {
3031              foreach ($presidency_array as $entry)
3032                {
3033                  $presidency = $entry['presidency'];
3034                  $appointment = $entry['appointment'];
3035                  $date = $entry['date'];
3036                  $hour = $entry['hour'];
3037                  $minute = $entry['minute'];
3038                  $pm = $entry['pm'];
3039                  $elder = $entry['elder'];
3040                  $family = $entry['family'];
3041                  if($pm) { $hour = $hour + 12; }
3042                  $time = $hour.':'.$minute.':'.'00';
3043
3044                  // Update an existing appointment
3045                  if($appointment != 0)
3046                    {
3047                      $this->db->query("UPDATE eq_appointment set" .
3048                               " family=" . $family . 
3049                               " ,elder=" . $elder . 
3050                               " ,date='" . $date . "'" .
3051                               " ,time='" . $time . "'" .
3052                               " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3053
3054                      //print "updating entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                  
3055                    }
3056                  
3057                  // Add a new appointment
3058                  else if(($appointment == 0) && ($date != "") && ($time != ""))
3059                    {
3060                      $this->db->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time) "
3061                            . "VALUES ('" . $appointment . "','" . $presidency . "','" . $family . "','"
3062                            . $elder . "','" . $date . "','" . $time  ."')",__LINE__,__FILE__);
3063                      
3064                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";                    
3065                    }
3066                }
3067            }
3068           
3069           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3070           Header('Location: ' . $take_me_to_url);
3071         }
3072
3073       $sql = "SELECT * FROM eq_presidency where valid=1";
3074       $this->db->query($sql,__LINE__,__FILE__);
3075       $i=0;
3076       while ($this->db->next_record())
3077         {
3078           $presidency_data[$i]['id'] = $this->db->f('presidency');
3079           $presidency_data[$i]['name'] = $this->db->f('name');
3080           $presidency_data[$i]['elder'] = $this->db->f('elder');
3081           $i++;
3082         }
3083       
3084       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3085       $this->db->query($sql,__LINE__,__FILE__);
3086       $i=0;
3087       while ($this->db->next_record())
3088         {
3089           $elder_id[$i] = $this->db->f('elder');
3090           $elder_name[$i] = $this->db->f('name');
3091           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3092           $i++;
3093         }
3094       array_multisort($elder_name, $elder_id);
3095
3096       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3097       $this->db->query($sql,__LINE__,__FILE__);
3098       $i=0;
3099       while ($this->db->next_record())
3100         {
3101           $family_id[$i] = $this->db->f('family');
3102           $family_name[$i] = $this->db->f('name');
3103           $i++;
3104         }
3105       array_multisort($family_name, $family_id);
3106       
3107       for ($i=0; $i < count($presidency_data); $i++) {
3108         $presidency = $presidency_data[$i]['id'];
3109         $interviewer = $presidency_data[$i]['elder'];
3110         $name = $presidency_data[$i]['name'];
3111         $this->t->set_var('presidency_name',$name);
3112         $table_data="";
3113         
3114         // query the database for all the appointments
3115         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3116         $this->db->query($sql,__LINE__,__FILE__);
3117
3118         // Prefill any existing appointment slots
3119         while ($this->db->next_record())
3120           {
3121             $appointment = $this->db->f('appointment');
3122             $elder = $this->db->f('elder');
3123             $family = $this->db->f('family');
3124             
3125             $date = $this->db->f('date');
3126             $date_array = explode("-",$date);
3127             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3128             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3129             
3130             $time = $this->db->f('time');
3131             $time_array = explode(":",$time);
3132             $hour = $time_array[0];
3133             $minute = $time_array[1];
3134             $pm = 0;
3135             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3136             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3137             
3138             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3139
3140             // Date selection
3141             $table_data.= '<td align=left>';
3142             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3143             $table_data.= '</td>';
3144             
3145             // Hour & Minutes selection
3146             $table_data.= "<td align=center>";
3147             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3148             foreach(range(1,12) as $num) {
3149               if($hour == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3150               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3151             }
3152             $table_data.= '</select>';
3153             $table_data.= '&nbsp;:&nbsp;';
3154             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3155             foreach(range(0,3) as $num) {
3156               $num = $num * 15; if($num == 0) { $num = "00"; }
3157               if($minute == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3158               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3159             }
3160             $table_data.= '</select>';
3161             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3162             if($pm == 0) { $table_data.= '<option value=0 selected>am</option>'; $table_data.= '<option value=1>pm</option>'; }
3163             else { $table_data.= '<option value=0>am</option>'; $table_data.= '<option value=1 selected>pm</option>'; }
3164             $table_data.= '</select>';
3165             $table_data.= "</td>";
3166             
3167             // Elder drop down list (for PPIs)
3168             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3169             $table_data.= '<option value=0></option>';  
3170             for ($j=0; $j < count($elder_id); $j++) {
3171               $id = $elder_id[$j];
3172               $name = $elder_name[$j];
3173               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3174               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3175             }
3176             $table_data.='</select></td>';
3177
3178             // Family drop down list (for Visits)
3179             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3180             $table_data.= '<option value=0></option>';              
3181             for ($j=0; $j < count($elder_id); $j++) {
3182               $id = $family_id[$j];
3183               $name = $family_name[$j];
3184               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3185               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3186             }
3187             $table_data.='</select></td>';
3188             
3189             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3190             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3191         
3192             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3193             $this->t->set_var('tr_color',$tr_color);
3194             
3195           }
3196
3197         // Create blank appointment slot
3198         $appointment = 0;
3199         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3200
3201         // Date selection
3202         $table_data.= '<td align=left>';
3203         $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3204         $table_data.= '</td>';
3205         
3206         // Time selection
3207         $table_data.= "<td align=center>";
3208         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3209         $table_data.= '<option value=""></option>';
3210         foreach(range(1,12) as $num) {
3211           $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3212         }
3213         $table_data.= '</select>';
3214         $table_data.= '&nbsp;:&nbsp;';
3215         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3216         $table_data.= '<option value=""></option>';
3217         foreach(range(0,3) as $num) {
3218           $num = $num * 15; if($num == 0) { $num = "00"; }
3219           $table_data.= '<option value='.$num.'>'.$num.'</option>';
3220         }
3221         $table_data.= '</select>';
3222         $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3223         $table_data.= '<option value=""></option>';
3224         $table_data.= '<option value=0>am</option>';
3225         $table_data.= '<option value=1>pm</option>';
3226         $table_data.= '</select>';
3227         $table_data.= "</td>";
3228         
3229         // Elder drop down list
3230         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3231         $table_data.= '<option value=0></option>';  
3232         for ($j=0; $j < count($elder_id); $j++) {
3233           $id = $elder_id[$j];
3234           $name = $elder_name[$j];
3235           $table_data.= '<option value='.$id.'>'.$name.'</option>';
3236         }
3237         $table_data.='</select></td>';
3238         
3239         // Family drop down list
3240         $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3241         $table_data.= '<option value=0></option>';          
3242         for ($j=0; $j < count($elder_id); $j++) {
3243           $id = $family_id[$j];
3244           $name = $family_name[$j];
3245           $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3246         }
3247         $table_data.='</select></td>';
3248
3249         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3250         $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3251         
3252         $this->t->set_var('table_data',$table_data);
3253         $this->t->set_var('header_row',$header_row);
3254         $this->t->set_var('table_width',$table_width);
3255         $this->t->fp('list','presidency_list',True);
3256         
3257       }
3258       
3259       $this->t->pfp('out','sched_t');
3260       $this->save_sessiondata();   
3261     }
3262   
3263   function admin()
3264     {
3265       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3266       $this->t->set_block('admin_t','upload','uploadhandle');
3267       $this->t->set_block('admin_t','admin','adminhandle');
3268       $this->t->set_block('admin_t','cmd','cmdhandle');
3269       
3270       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3271       
3272       $action = get_var('action',array('GET','POST'));
3273
3274       $this->t->pfp('out','admin_t');
3275       
3276       if($action == 'upload')
3277         {        
3278           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
3279           
3280           if((($_FILES['uploadedfile']['type'] == "application/zip") ||
3281               ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3282               ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3283               ($_FILES['uploadedfile']['type'] == "application/octet-stream")) &&
3284              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
3285             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3286             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3287             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3288             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3289             $this->t->set_var('uploadstatus',$uploadstatus);
3290             $this->t->pfp('uploadhandle','upload');
3291             $this->t->set_var('uploadhandle','');
3292             print "<table border=1 width=80%><tr><td>\n<pre>";
3293             
3294             # make a directory for this data to be stored in
3295             $date="data_" . date("Y_m_d");
3296             $data_dir = $this->upload_target_path . $date;
3297             print "-> Making the data directory: $date<br>\n";
3298             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
3299             if($return_code != 0) {
3300               print implode('\n',$result) . "<br>";
3301               print "<b><font color=red>";
3302               print "-E- Unable to create the data directory. Aborting import.";
3303               print "</font></b>";
3304               return 0;
3305             }
3306
3307             # move the file uploaded into this directory
3308             print "-> Moving the uploaded file into the data dir<br>\n";
3309             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3310             if($return_code != 0) {
3311               print implode('\n',$result) . "<br>";
3312               print "<b><font color=red>";
3313               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3314               print "</font></b>";
3315               return 0;
3316             }
3317             
3318             # unzip the data into this directory
3319             print "-> Unzipping the data<br>\n";
3320             $data_file = $data_dir . '';
3321             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 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 unzip the uploaded file into the data dir. Aborting import.";
3326               print "</font></b>";
3327               return 0;
3328             }
3329             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3330
3331             # update the data_latest link to point to this new directory
3332             print "-> Updating the latest data dir link<br>\n";
3333             $data_latest = $this->upload_target_path . 'data_latest';
3334             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3335             if($return_code != 0) {
3336               print implode('\n',$result) . "<br>";
3337               print "<b><font color=red>";
3338               print "-E- Unable to update the data latest link. Aborting import.";
3339               print "</font></b>";
3340               return 0;
3341             }
3342             
3343             # run the import perl script to encorporate it into the DB
3344             ob_start('ob_logstdout', 2);
3345             print "-> Importing the data into the EQ database<br>\n";
3346             ob_flush(); flush(); sleep(1);
3347             $import_log = $this->upload_target_path . '/import.log';
3348             $data_log = $this->upload_target_path . '/data.log';
3349             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
3350             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
3351             #print "import_cmd: $import_cmd<br>";
3352             #print "parse_cmd: $parse_cmd<br>";
3353             ob_start('ob_logstdout', 2);
3354             passthru($import_cmd);
3355             passthru($parse_cmd);
3356             ob_flush(); flush(); sleep(1);
3357
3358             # fix the permissions of the data dir
3359             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3360             
3361             $this->t->pfp('cmdhandle','cmd');
3362             print "</pre></td></tr></table>";
3363             
3364           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3365                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3366                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3367                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3368             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3369             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3370             $this->t->set_var('uploadstatus',$uploadstatus);
3371             $this->t->pfp('uploadhandle','upload',True);
3372           } else {
3373             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3374             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3375             $this->t->set_var('uploadstatus',$uploadstatus);
3376             $this->t->pfp('uploadhandle','upload',True);
3377           }
3378         }
3379       else
3380         {
3381           $this->t->set_var('adminhandle','');
3382           $this->t->pfp('adminhandle','admin'); 
3383         }
3384       
3385       $this->save_sessiondata();   
3386     }
3387   
3388 }
3389
3390 ?>