a5cc2b4df67691b13f929f8b6fb99894078d34d4
[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 2005/07/20 07:40:32 ajp 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 $max_num_districts;
30   var $current_year;
31   var $current_month;
32   var $upload_target_path;
33   var $script_path;
34   var $max_appointments;
35   var $max_presidency_members;
36   
37   var $public_functions = array
38     (
39      'ht_view'    => True,
40      'ht_update'  => True,
41      'act_list'   => True,
42      'act_view'   => True,
43      'act_update' => True,
44      'par_view'   => True,
45      'ppi_view'   => True,
46      'ppi_update' => True,
47      'ppi_sched'  => True,
48      'int_view'   => True,
49      'int_update' => True,
50      'int_sched'  => True,
51      'vis_sched'  => True,
52      'vis_view'   => True,
53      'vis_update' => True,
54      'att_view'   => True,
55      'att_update' => True,
56      'dir_view'   => True,
57      'org_view'   => True,
58      'schedule'   => True,
59      'admin'      => True,
60      'email_appt' => True,
61      'willing_view'   => True,
62      'willing_update' => True,
63      'send_ical_appt' => True,
64      'assign_view'    => True,
65      'assign_update'  => True,
66      'get_time_selection_form' => True,
67      );
68  
69   function eq()
70     {
71       if(file_exists("setup/eq_config.local")) {
72         include("setup/eq_config.local");
73       } else {
74         include("setup/eq_config");
75       }
76       
77       $this->script_path = "$this->application_path"."/bin";
78       $this->max_presidency_members = 99;
79       $this->max_appointments = 32768;
80       
81       $this->db         = $GLOBALS['phpgw']->db;
82       $this->db2        = $this->db;
83       $this->db3        = $this->db;
84       $this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
85       $this->t          = $GLOBALS['phpgw']->template;
86       $this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
87       $this->grants     = $GLOBALS['phpgw']->acl->get_grants('eq');
88       $this->grants[$this->account] = PHPGW_ACL_READ + PHPGW_ACL_ADD + PHPGW_ACL_EDIT + PHPGW_ACL_DELETE;
89        
90       $this->jscal = CreateObject('eq.jscalendar');   // before phpgw_header() !!!
91       $this->cal_options = 'daFormat    : "%Y-%m-%d",
92                                 ifFormat    : "%Y-%m-%d",
93                                 mondayFirst : false,
94                                 weekNumbers : false';
95        
96       $GLOBALS['phpgw_info']['flags']['app_header'] = 'Elders Quorum Tools - The 3rd Counselor';
97       $GLOBALS['phpgw']->common->phpgw_header();
98
99       $this->current_day = `date '+%d'`;
100       $this->current_day = $this->current_day-0; // Make it numeric
101       $this->current_month = `date '+%m'`;
102       $this->current_month = $this->current_month-0; // Make it numeric
103       $this->current_year = `date '+%Y'`;
104       $this->current_year = $this->current_year-0; // Make it numeric
105
106       echo parse_navbar();
107       $this->display_app_header();      
108     }
109   
110   function save_sessiondata()
111     {
112       
113     }
114
115   function display_app_header()
116     {
117       $this->t->set_file(array('eq_header' => 'header.tpl'));
118       
119       if (isset($phpgw_info['user']['preferences']['eq']['eq_font']))
120         {
121           $font = $phpgw_info['user']['preferences']['eq']['eq_font'];
122         }
123       else
124         {
125           $font = 'Arial';
126         }
127       
128       $this->t->set_var('bg_color',$phpgw_info['theme']['th_bg']);
129       $this->t->set_var('font',$font);
130       $link_data['menuaction'] = 'eq.eq.ht_view';
131       $this->t->set_var('link_hometeaching',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
132       $this->t->set_var('lang_hometeaching','HomeTeaching');
133       $link_data['menuaction'] = 'eq.eq.act_list';
134       $this->t->set_var('link_activity',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
135       $this->t->set_var('lang_activity','Activities');
136       $link_data['menuaction'] = 'eq.eq.willing_view';
137       $this->t->set_var('link_willing',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
138       $this->t->set_var('lang_willing','Willingness');
139       $link_data['menuaction'] = 'eq.eq.assign_view';
140       $this->t->set_var('link_assignment',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
141       $this->t->set_var('lang_assignment','Assignments');
142       $link_data['menuaction'] = 'eq.eq.par_view';
143       $this->t->set_var('link_participation',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
144       $this->t->set_var('lang_participation','Participation');
145       $link_data['menuaction'] = 'eq.eq.ppi_view';
146       $this->t->set_var('link_ppi',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
147       $this->t->set_var('lang_ppi','PPIs');
148       $link_data['menuaction'] = 'eq.eq.int_view';
149       $this->t->set_var('link_int',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
150       $this->t->set_var('lang_int','Interviews');
151       $link_data['menuaction'] = 'eq.eq.vis_view';
152       $this->t->set_var('link_visit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
153       $this->t->set_var('lang_visit','Visits');
154       $link_data['menuaction'] = 'eq.eq.att_view';      
155       $this->t->set_var('link_attendance',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
156       $this->t->set_var('lang_attendance','Attendance');
157       $link_data['menuaction'] = 'eq.eq.dir_view';      
158       $this->t->set_var('link_dir',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
159       $this->t->set_var('lang_dir','Directory');
160       $link_data['menuaction'] = 'eq.eq.org_view';      
161       $this->t->set_var('link_org',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
162       $this->t->set_var('lang_org','Callings');
163       $link_data['menuaction'] = 'eq.eq.admin'; 
164       $this->t->set_var('link_admin',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
165       $this->t->set_var('lang_admin','Admin');
166       $link_data['menuaction'] = 'eq.eq.schedule';      
167       $this->t->set_var('link_schedule',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
168       $this->t->set_var('lang_schedule','Scheduling');
169                 
170       $this->t->pparse('out','eq_header');
171     }
172
173   function ht_view()
174     {
175       $this->t->set_file(array('ht_view_t' => 'ht_view.tpl'));
176       $this->t->set_block('ht_view_t','district_list','list');
177    
178       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
179       $num_months = get_var('num_months',array('GET','POST'));
180       if($num_months == '') { $num_months = $this->default_ht_num_months; }
181       $this->t->set_var('num_months',$num_months);
182       $this->t->set_var('lang_filter','Filter');
183       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
184       else {  $this->t->set_var('lang_num_months','Months of History'); }
185       
186       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
187       $this->t->set_var('title','Hometeaching'); 
188       
189       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
190       $this->db->query($sql,__LINE__,__FILE__);
191       $i=0;
192       while ($this->db->next_record())
193         {
194           $districts[$i]['district'] = $this->db->f('district');
195           $districts[$i]['name'] = $this->db->f('name');
196           $districts[$i]['supervisor'] = $this->db->f('supervisor');
197           $i++;
198         }
199
200       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
201       $this->db->query($sql,__LINE__,__FILE__);
202       $i=0;
203       while ($this->db->next_record())
204         {
205           $elder_id[$i] = $this->db->f('elder');
206           $elder_name[$i] = $this->db->f('name');
207           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
208           $i++;
209         }
210       array_multisort($elder_name, $elder_id);
211
212       // Make an array mapping elder_ids to elder_names
213       for($i=0; $i < count($elder_id); $i++) {
214           $id = $elder_id[$i];
215           $elders[$id] = $elder_name[$i];
216       }      
217
218       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
219       $this->db->query($sql,__LINE__,__FILE__);
220       while ($this->db->next_record())
221         {
222           $aaronic_id = $this->db->f('aaronic');
223           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
224           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
225         }
226       
227       $total_families = 0;
228       $this->nextmatchs->template_alternate_row_color(&$this->t);
229       for ($i=0; $i < count($districts); $i++) {
230         $this->t->set_var('district_number',$districts[$i]['district']);
231         $this->t->set_var('district_name',$districts[$i]['name']);      
232         $supervisor = $districts[$i]['supervisor'];
233         $unique_companionships='';
234                 
235         // Select all the unique companionship numbers for this district
236         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
237         $this->db->query($sql,__LINE__,__FILE__);
238         $j=0;
239         while ($this->db->next_record())
240           {
241             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
242             $j++;
243           }
244
245         $comp_width=450; $visit_width=25; $table_width=$comp_width + $num_months*$visit_width;
246         $table_data=""; $num_companionships = 0; $num_families = 0;
247         for($m=$num_months; $m >= 0; $m--) { $visits[$m] = 0; }
248         for ($j=0; $j < count($unique_companionships); $j++) {
249           $companion_table_entry = "";
250           // Select all the companions in each companionship
251           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
252                  "companionship=". $unique_companionships[$j]['companionship'];
253           $this->db->query($sql,__LINE__,__FILE__);
254
255           while ($this->db->next_record())
256             {
257               // Get this companions information
258               if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
259               $companionship = $this->db->f('companionship');
260               $elder_id = $this->db->f('elder');
261               $aaronic_id = $this->db->f('aaronic');
262               if($elder_id) {
263                 $name = $elders[$elder_id];
264                 $phone = $elder_phone[$elder_id];
265               }
266               else if($aaronic_id) {
267                 $name = $aaronic[$aaronic_id]['name'];
268                 $phone = $aaronic[$aaronic_id]['phone'];                
269               }
270               $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
271             }
272           $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
273           
274           // Get the names of the families assigned this home teaching companionship
275           $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
276           $sql = $sql . " ORDER BY name ASC";
277           $this->db->query($sql,__LINE__,__FILE__);
278           $k=0;
279           while ($this->db->next_record())
280             {
281               $num_families++; $total_families++;
282               $family_name = $this->db->f('name');
283               $family_id = $this->db->f('family');
284               $this->nextmatchs->template_alternate_row_color(&$this->t);
285               $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
286               // Find out how many times Visits were performed by this companionship
287               // in the past $num_months for this Family
288               $header_row="<th width=$comp_width><font size=-2>Families</th>";
289               for($m=$num_months; $m >= 0; $m--) {
290                 $month = $this->current_month - $m;
291                 $year = $this->current_year;
292                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
293                 if($month < 10) { $month = "0"."$month"; }
294                 $month_start = "$year"."-"."$month"."-"."01";
295                 $month_end = "$year"."-"."$month"."-"."31";
296                 $month = "$month"."/"."$year";
297                 
298                 //print "m: $m month: $month year: $year month_start: $month_start month_end: $month_end<br>";
299                 // Add this to the query to filter on only visits made by this companionship:
300                 // " AND companionship=" . $unique_companionships[$j]['companionship'].
301
302                 // First check to see if the currently assigned companionship has visited them
303                 $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
304                    " AND companionship=".$unique_companionships[$j]['companionship'].
305                    " AND family=". $family_id;
306                 $query_id = $this->db2->query($sql,__LINE__,__FILE__);
307                 if($this->db2->num_rows($query_id) == 0) {
308                   // We did not find any visits made by the currently assigned companionship,
309                   // look for visits made by any other companionship other than 0. (0 == EQ Presidency Visit)
310                   $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
311                      " AND companionship!=0".
312                      " AND family=". $family_id;
313                   $query_id = $this->db2->query($sql,__LINE__,__FILE__);
314                 }
315                 $this->db2->query($sql,__LINE__,__FILE__);
316                 $link_data['menuaction'] = 'eq.eq.ht_update';
317                 $link_data['date'] = $month_start;
318                 $link_data['month_start'] = $month_start;
319                 $link_data['month_end'] = $month_end;
320                 $link_data['month'] = $month;
321                 $link_data['district'] = $districts[$i]['district'];
322                 $link_data['district_name'] = $districts[$i]['name'];
323                 $link_data['action'] = 'view';
324                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
325                 $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
326                 if(!$total_visits[$m]) { $total_visits[$m] = 0; }
327                 if($this->db2->next_record()) {
328                   if($this->db2->f('visited') == 'y') {
329                     $visits[$m]++; $total_visits[$m]++;           
330                     $table_data .= '<td align=center><a href="'.$link.'"><img src="images/checkmark.gif"></a></td>';
331                   }
332                   else if($this->db2->f('visited') == 'n') {
333                     $table_data .= '<td align=center><a href="'.$link.'"><img src="images/x.gif"></a></td>';
334                   }
335                   else {
336                     $visits[$m]++; $total_visits[$m]++;
337                     $table_data .= "<td>&nbsp;</td>";
338                   }
339                 }
340                 else {
341                   $visits[$m]++; $total_visits[$m]++;
342                   $table_data .= "<td>&nbsp;</td>";
343                 }
344               }
345               $table_data .= "</tr>"; 
346               $k++;
347             }
348           $table_data .= "<tr><td colspan=20></td></tr>";
349         }
350         $table_data .= "<tr><td colspan=20><hr></td></tr>";
351         $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
352
353         for($m=$num_months; $m >=0; $m--) {
354           $percent = ceil(($visits[$m] / $num_families)*100);
355           $stat_data .= "<td align=center><font size=-2><b>$visits[$m]<br>$percent%</font></b></td>";
356         }
357         $stat_data .= "</tr>";
358
359         $this->t->set_var('table_width',$table_width);
360         $this->t->set_var('header_row',$header_row);
361         $this->t->set_var('table_data',$table_data);
362         $this->t->set_var('stat_data',$stat_data);
363         $this->t->fp('list','district_list',True);
364       }
365
366       $totals = "<tr><td><b><font size=-2>$total_families Total Families<br>Visit Totals:</font></b></td>";
367       for($m=$num_months; $m >=0; $m--) {
368         $percent = ceil(($total_visits[$m] / $total_families)*100);
369         $totals .= "<td align=center><font size=-2><b>$total_visits[$m]<br>$percent%</font></b></td>";
370       }
371       $totals .= "</tr>";
372       
373       $this->t->set_var('totals',$totals);
374       
375       $this->t->pfp('out','ht_view_t');
376       $this->save_sessiondata();
377     }
378       
379
380   function ht_update()
381     {
382       $this->t->set_file(array('ht_update_t' => 'ht_update.tpl'));
383       $this->t->set_block('ht_update_t','district_list','list');
384       $this->t->set_block('ht_update_t','save','savehandle');
385       
386       $district = get_var('district',array('GET','POST'));
387       $district_name = get_var('district_name',array('GET','POST'));
388       $date = get_var('date',array('GET','POST'));
389       $month = get_var('month',array('GET','POST'));
390       $month_start = get_var('month_start',array('GET','POST'));
391       $month_end = get_var('month_end',array('GET','POST'));
392       $action = get_var('action',array('GET','POST'));
393       
394       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_view'));
395       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ht_update&action=save'));
396       $this->t->set_var('lang_done','Cancel');
397       $this->t->set_var('district_name',$district_name);
398       $this->t->set_var('district_number',$district);
399       $this->t->set_var('title','Hometeaching Update ' . $month);
400       $this->t->set_var('date',$date);
401       
402       if($action == 'save')
403         {
404           // Get a list of all the companionships in this district
405           $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
406           $this->db->query($sql,__LINE__,__FILE__);
407           $j=0;
408           while ($this->db->next_record())
409             {
410               $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
411               $j++;
412             }
413           for ($j=0; $j < count($unique_companionships); $j++)
414             {
415               //$comp=$unique_companionships[$j]['companionship'];
416               //print "deleting from eq_visit where companionship=$comp and date=$date and district=$district<br>";
417               // Delete all the visits that have taken place for all families for this companionsthip for this month
418               $this->db->query("DELETE from eq_visit where companionship=" . $unique_companionships[$j]['companionship'] .
419                                " AND " . "date='" . $date . "'",__LINE__,__FILE__);
420             }
421
422           // Now, add the visits that are checked for this month
423           $new_data = get_var('family_visited',array('POST'));
424           foreach ($new_data as $family)
425            {
426              foreach ($family as $data)
427                {
428                  //print "family_visited: $data <br>";
429                  $data_array = explode("/",$data);
430                  $family_id = $data_array[0];
431                  $companionship = $data_array[1];
432                  $date = $data_array[2];
433                  $visited = $data_array[3];
434                  if($visited == "") { $visited = $data_array[4]; }
435                  //print "family_id: $family_id companionship: $companionship date: $date visited: $visited<br>";
436                  $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes,visited) "
437                                   . "VALUES (" . $family_id .",". $companionship .",'". $date ."','','". $visited ."')",__LINE__,__FILE__);
438                }
439            }
440           $this->ht_view();
441           return false;
442         }
443       
444       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
445       $this->db->query($sql,__LINE__,__FILE__);
446       $i=0;
447       while ($this->db->next_record())
448         {
449           $elder_id[$i] = $this->db->f('elder');
450           $elder_name[$i] = $this->db->f('name');
451           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
452           $i++;
453         }
454       array_multisort($elder_name, $elder_id);
455
456       // Make an array mapping elder_ids to elder_names
457       for($i=0; $i < count($elder_id); $i++) {
458           $id = $elder_id[$i];
459           $elders[$id] = $elder_name[$i];
460       }      
461
462       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
463       $this->db->query($sql,__LINE__,__FILE__);
464       while ($this->db->next_record())
465         {
466           $aaronic_id = $this->db->f('aaronic');
467           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
468           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
469         }
470       
471       // Select all the unique companionship numbers for this district
472       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $district;
473       $this->db->query($sql,__LINE__,__FILE__);
474       $j=0; $unique_companionships='';
475       while ($this->db->next_record())
476         {
477           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
478           $j++;
479         }
480       
481       $comp_width=300; $visit_width=25; $table_width=$comp_width + $visit_width;
482       $table_data=""; $num_companionships = 0; $num_families = 0; $visits=0;
483       for ($j=0; $j < count($unique_companionships); $j++) {
484         $companion_table_entry = "";
485         // Select all the companions in each companionship
486         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
487                "companionship=". $unique_companionships[$j]['companionship'];
488         $this->db->query($sql,__LINE__,__FILE__);
489         
490         while ($this->db->next_record())
491           {
492             // Get this companions information
493             if($companion_table_entry != "") { $companion_table_entry .= "<td>&nbsp;/&nbsp;</td>"; }
494             $companionship = $this->db->f('companionship');
495             $elder_id = $this->db->f('elder');
496             $aaronic_id = $this->db->f('aaronic');
497             if($elder_id) {
498               $name = $elders[$elder_id];
499               $phone = $elder_phone[$elder_id];
500             }
501             else if($aaronic_id) {
502               $name = $aaronic[$aaronic_id]['name'];
503               $phone = $aaronic[$aaronic_id]['phone'];
504             }
505             $companion_table_entry .= "<td title=\"$phone\"><b>$name</b></td>";
506           }
507         $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
508         
509         // Get the names of the families assigned this home teaching companionship
510         $sql = "SELECT * from eq_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
511         $sql = $sql . " ORDER BY name ASC";
512         $this->db->query($sql,__LINE__,__FILE__);
513         while ($this->db->next_record())
514           {
515             $num_families++; $total_families++;
516             $family_name = $this->db->f('name');
517             $family_id = $this->db->f('family');
518             $this->nextmatchs->template_alternate_row_color(&$this->t);
519             $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
520             
521             $header_row="<th width=$comp_width><font size=-2>Families</th>";
522
523             // First check to see if the currently assigned companionship has visited them
524             $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
525                    " AND companionship=".$unique_companionships[$j]['companionship'].
526                    " AND family=". $family_id;
527             $query_id = $this->db2->query($sql,__LINE__,__FILE__);
528             if($this->db2->num_rows($query_id) == 0) {
529               // We did not find any visits made by the currently assigned companionship,
530               // look for visits made by any other companionship other than 0. (0 == EQ Presidency Visit)
531               $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
532                    " AND companionship!=0".
533                    " AND family=". $family_id;
534               $query_id = $this->db2->query($sql,__LINE__,__FILE__);
535             }
536             
537             $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
538             $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
539             if(!$total_visits) { $total_visits = 0; }
540             if($this->db2->next_record()) {
541               if($this->db2->f('visited') == 'y') {
542                 $visits++; $total_visits++;
543                 $table_data .= '<td width=100 align=center>';
544                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y" checked>Y';
545                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
546                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/"> ';
547                 $table_data .= '</td>';
548               } else if($this->db2->f('visited') == 'n') {
549                 $table_data .= '<td width=100 align=center>';
550                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
551                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n" checked>N';
552                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/">';
553                 $table_data .= '</td>';
554               } else {
555                 $table_data .= '<td width=100 align=center>';
556                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
557                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
558                 $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
559                 $table_data .= '</td>';
560               }
561             }
562             else {
563               $value .= "/";
564               $table_data .= '<td width=100 align=center>';
565               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/y">Y';
566               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/n">N';
567               $table_data .= '<input type="radio" name="family_visited['.$family_id.'][]" value="'.$value.'/" checked> ';
568               $table_data .= '</td>';         
569             }
570           }
571         $table_data .= "</tr>"; 
572         $table_data .= "<tr><td colspan=20></td></tr>";
573       }
574       $table_data .= "<tr><td colspan=20><hr></td></tr>";
575       $stat_data = "<tr><td><b><font size=-2>$num_families Families<br>Visit Totals:</font></b></td>";
576       
577       $percent = ceil(($visits / $num_families)*100);
578       $stat_data .= "<td align=center><font size=-2><b>$visits<br>$percent%</font></b></td>";
579       $stat_data .= "</tr>";
580       
581       $this->t->set_var('table_width',$table_width);
582       $this->t->set_var('header_row',$header_row);
583       $this->t->set_var('table_data',$table_data);
584       $this->t->set_var('stat_data',$stat_data);
585       $this->t->fp('list','district_list',True);
586
587       $this->t->set_var('lang_reset','Clear Form');
588       $this->t->set_var('lang_save','Save Changes');
589       $this->t->set_var('savehandle','');
590       
591       $this->t->pfp('out','ht_update_t');
592       $this->t->pfp('addhandle','save');
593       
594       $this->save_sessiondata();
595     }
596
597   function act_list()
598     {
599       $this->t->set_file(array('act_list_t' => 'act_list.tpl'));
600       $this->t->set_block('act_list_t','act_list','list');
601       
602       $this->t->set_var('lang_name','Assignment');
603       $this->t->set_var('lang_date','Date');
604       $this->t->set_var('lang_notes','Description');
605       
606       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
607       $this->db->query($sql,__LINE__,__FILE__);
608       $total_records = $this->db->num_rows();
609
610       $i = 0;
611       while ($this->db->next_record())
612         {
613           $activity_list[$i]['activity']  = $this->db->f('activity');
614           $activity_list[$i]['assignment'] = $this->db->f('assignment');
615           $activity_list[$i]['date']  = $this->db->f('date');
616           $activity_list[$i]['notes']  = $this->db->f('notes');
617
618           $sql = "SELECT * FROM eq_assignment WHERE assignment='" . $activity_list[$i]['assignment'] . "'";
619           $this->db2->query($sql,__LINE__,__FILE__);
620           if($this->db2->next_record())
621             {
622               $activity_list[$i]['name'] = $this->db2->f('name');
623               $activity_list[$i]['code'] = $this->db2->f('code');
624             }
625           $i++;
626         }
627             
628       for ($i=0; $i < count($activity_list); $i++)
629         {         
630           $this->nextmatchs->template_alternate_row_color(&$this->t);
631           $this->t->set_var('name',$activity_list[$i]['name']);
632           $this->t->set_var('date',$activity_list[$i]['date']);
633           $activity_notes = $activity_list[$i]['notes'];
634           if(strlen($activity_notes) > 40) { $activity_notes = substr($activity_notes,0,40) . "..."; }
635           $this->t->set_var('notes',$activity_notes);
636           
637           $link_data['menuaction'] = 'eq.eq.act_view';
638           $link_data['activity'] = $activity_list[$i]['activity'];
639           $link_data['action'] = 'view';
640           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
641           $this->t->set_var('lang_view','View');
642
643           $link_data['menuaction'] = 'eq.eq.act_update';
644           $link_data['activity'] = $activity_list[$i]['activity'];
645           $link_data['action'] = 'edit';
646           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
647           $this->t->set_var('lang_edit','Edit');
648
649           $link_data['menuaction'] = 'eq.eq.act_update';
650           $link_data['activity'] = '0';
651           $link_data['action'] = 'add';
652           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
653                            . '"><input type="submit" name="Add" value="' . 'Add Activity' .'"></font></form>');
654
655           $this->t->fp('list','act_list',True);
656         }
657
658       $this->t->pfp('out','act_list_t');
659       $this->save_sessiondata();
660     }
661
662   function act_view()
663     {
664       $this->t->set_file(array('act_view_t' => 'act_view.tpl'));
665       $this->t->set_block('act_view_t','part_list','list');
666       
667       $sql = "SELECT * FROM eq_activity WHERE activity=" . intval(get_var('activity',array('GET','POST')));
668       $this->db->query($sql,__LINE__,__FILE__);
669       $this->db->next_record();
670       $this->t->set_var('assignment', $this->db->f('assignment'));
671       $this->t->set_var('date', $this->db->f('date'));
672       $this->t->set_var('notes', $this->db->f('notes'));
673       
674       $sql = "SELECT * FROM eq_assignment WHERE assignment='" . $this->db->f('assignment') . "'";
675       $this->db2->query($sql,__LINE__,__FILE__);
676       if($this->db2->next_record())
677         {
678           $this->t->set_var('name', $this->db2->f('name'));
679           $this->t->set_var('code', $this->db2->f('code'));
680         }
681       $this->t->set_var('lang_name','Assignment');
682       $this->t->set_var('lang_date','Date');
683       $this->t->set_var('lang_notes','Description');
684       $this->t->set_var('lang_done','Done');
685       $this->t->set_var('lang_action','View');
686
687       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
688       $this->t->set_var('tr_color',$tr_color);
689             
690       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
691
692       $link_data['menuaction'] = 'eq.eq.act_update';
693       $link_data['activity'] = get_var('activity',array('GET','POST'));
694       $link_data['action'] = 'edit';
695       $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
696       $this->t->set_var('lang_edit','Edit');
697       $this->t->set_var('cal_date',$this->db->f('date'));
698       
699       // Now find out which elders participated in this activity
700       $sql = "SELECT * FROM eq_participation WHERE activity=" . intval(get_var('activity',array('GET','POST')));
701       $this->db->query($sql,__LINE__,__FILE__);
702       $total_records = $this->db->num_rows();
703
704       $i = 0;
705       while ($this->db->next_record())
706         {
707           $part_list[$i]['elder']  = $this->db->f('elder');
708           $i++;
709         }
710       
711       for ($i=0; $i < count($part_list); $i++)
712         {
713           $sql = "SELECT * FROM eq_elder WHERE elder=" . $part_list[$i]['elder'];
714           $this->db->query($sql,__LINE__,__FILE__);
715           $this->db->next_record();
716           $names[$i] = $this->db->f('name');
717         } sort($names);
718       
719       for ($i=0; $i < count($names); $i++)
720         {
721           //$this->nextmatchs->template_alternate_row_color(&$this->t);
722           $this->t->set_var('elder_name',$names[$i]);
723           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
724           else { $this->t->set_var('table_sep',"</td>"); }
725           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
726           $this->t->fp('list','part_list',True);
727         }
728       
729       $this->t->pfp('out','act_view_t');
730       $this->save_sessiondata();
731     }
732
733   function act_update()
734     {
735       $this->t->set_file(array('form' => 'act_update.tpl'));
736       $this->t->set_block('form','elder_list','list');
737       $this->t->set_block('form','add','addhandle');
738       $this->t->set_block('form','edit','edithandle');
739       $this->t->set_var('lang_done','Done');
740
741       $action = get_var('action',array('GET','POST'));
742       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_list'));
743       $activity['activity'] = intval(get_var('activity',array('GET','POST')));
744       
745       if($action == 'save')
746         {
747           $activity['assignment'] = get_var('assignment',array('POST'));
748           $activity['date'] = get_var('date',array('POST'));
749           $activity['notes']= get_var('notes',array('POST'));
750           $this->db->query("UPDATE eq_activity set " .
751                            "   assignment='" . $activity['assignment'] .
752                            "', date='" . $activity['date'] . "'" .
753                            ", notes='" . $activity['notes'] . "'" .
754                            " WHERE activity=" . $activity['activity'],__LINE__,__FILE__);
755
756           // Delete all the elders who have particiapted in this activity
757           $this->db->query("DELETE from eq_participation where activity=".$activity['activity'],__LINE__,__FILE__);
758           
759           // Re-add the elders who are checked as having participated in this activity
760           $elders = get_var('elder_name',array('POST'));
761           foreach ($elders as $elder)
762             {
763               $this->db->query("INSERT INTO eq_participation (elder,activity) "
764                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
765             }
766
767           $this->act_list();
768           return false;
769         }
770
771       if($action == 'insert')
772         {
773           $activity['assignment'] = get_var('assignment',array('POST'));
774           $activity['date'] = get_var('date',array('POST'));
775           $activity['notes']= get_var('notes',array('POST'));
776           $this->db->query("INSERT INTO eq_activity (assignment,date,notes) "
777                            . "VALUES ('" . $activity['assignment'] . "','"
778                            . $activity['date'] . "','" . $activity['notes'] . "')",__LINE__,__FILE__);
779
780           $sql = "SELECT * FROM eq_activity WHERE assignment='".$activity['assignment']."' "
781              . " AND date='".$activity['date']."' AND notes='".$activity['notes']."'";
782           $this->db->query($sql,__LINE__,__FILE__);
783           if($this->db->next_record()) {
784             //print "activity: " . $this->db->f('activity') . "<br>";
785             $activity['activity'] = $this->db->f('activity');
786           }
787           
788           $elders = get_var('elder_name',array('POST'));
789           foreach ($elders as $elder)
790             {
791               $this->db->query("INSERT INTO eq_participation (elder,activity) "
792                                . "VALUES (" . $elder . ",". $activity['activity'] . ")",__LINE__,__FILE__);
793             }
794           
795           $this->act_list();
796           return false;
797         }
798       
799       if($action == 'add')
800         {
801           $activity['activity'] = 0;
802           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
803           $this->t->set_var('assignment','');
804           $this->t->set_var('date','');
805           $this->t->set_var('notes','');
806           $this->t->set_var('lang_done','Cancel');
807           $this->t->set_var('lang_action','Adding New Activity');
808           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
809                                                                 . $activity['activity'] . '&action=' . 'insert'));
810         }
811
812       if($action == 'edit')
813         {
814           $sql = "SELECT * FROM eq_activity WHERE activity=" . $activity['activity'];
815           $this->db->query($sql,__LINE__,__FILE__);
816           $this->db->next_record();
817           $this->t->set_var('cal_date',$this->jscal->input('date',$this->db->f('date'),'','','','','',$this->cal_options));
818           $this->t->set_var('assignment', $this->db->f('assignment'));
819           $assignment = $this->db->f('assignment');
820           $this->t->set_var('date', $this->db->f('date'));
821           $this->t->set_var('notes', $this->db->f('notes'));
822           $this->t->set_var('lang_done','Cancel');
823           $this->t->set_var('lang_action','Editing Activity');
824           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.act_update&activity='
825                                                                 . $activity['activity'] . '&action=' . 'save'));
826
827         }
828
829       // Create the assignments drop-down list
830       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
831       $this->db->query($sql,__LINE__,__FILE__);
832       $i = 0;
833       while ($this->db->next_record())
834         {
835           $assignments[$i]['assignment']  = $this->db->f('assignment');
836           $assignments[$i]['name'] = $this->db->f('name');
837           $assignments[$i]['code'] = $this->db->f('code');
838           $i++;
839         }
840       
841       $assignment_data.= '<select name=assignment>';
842       $assignment_data.= '<option value=0></option>';  
843       for ($j=0; $j < count($assignments); $j++) {
844         $id = $assignments[$j]['assignment'];
845         $name = $assignments[$j]['name'];
846         if($assignments[$j]['assignment'] == $assignment) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
847         $assignment_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
848       }
849       $assignment_data.='</select>';
850       $this->t->set_var('assignment_data',$assignment_data);
851       
852       // Create elder selection boxes
853       $sql = "SELECT * FROM eq_elder";
854       $this->db->query($sql,__LINE__,__FILE__);
855       $i=0;
856       while ($this->db->next_record())
857         {
858           if($this->db->f('valid') == 1 || $action != 'add') {
859             $elder_name[$i] = $this->db->f('name');
860             $elder_id[$i] = $this->db->f('elder');
861             $elder_valid[$i] = $this->db->f('valid');
862             $i++;
863           }
864         }
865       array_multisort($elder_name, $elder_id, $elder_valid);
866
867       $j=0;
868       for ($i=0; $i < count($elder_id); $i++)
869         {
870           //$this->nextmatchs->template_alternate_row_color(&$this->t);
871           $sql = "SELECT * FROM eq_participation where activity=". $activity['activity'] . " AND elder=" . $elder_id[$i];
872           $this->db->query($sql,__LINE__,__FILE__);
873           if($this->db->next_record()) { $this->t->set_var('checked','checked'); $checked=1; }
874           else { $this->t->set_var('checked',''); $checked=0; }
875           if($checked || $elder_valid[$i] == 1) {
876             $this->t->set_var('elder_name',$elder_name[$i]);
877             $this->t->set_var('elder',$elder_id[$i]);
878             if(($j+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
879             else { $this->t->set_var('table_sep',"</td>"); }
880             if(($j) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
881             $this->t->fp('list','elder_list',True);
882             $j++;
883           }
884         }
885       
886       $this->t->set_var('lang_reset','Clear Form');
887       $this->t->set_var('lang_add','Add Activity');
888       $this->t->set_var('lang_save','Save Changes');
889       $this->t->set_var('edithandle','');
890       $this->t->set_var('addhandle','');
891       
892       $this->t->pfp('out','form');
893       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
894       if($action == 'add') { $this->t->pfp('addhandle','add'); }
895       
896       $this->save_sessiondata();
897     }
898
899     function assign_view()
900     {
901       $this->t->set_file(array('assign_view_t' => 'assign_view.tpl'));
902       $this->t->set_block('assign_view_t','assign_view','list');
903       
904       $this->t->set_var('lang_name','Assignment Name');
905       $this->t->set_var('lang_code','Code');
906       
907       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
908       $this->db->query($sql,__LINE__,__FILE__);
909       $total_records = $this->db->num_rows();
910
911       $i = 0;
912       while ($this->db->next_record())
913         {
914           $assignment_list[$i]['assignment']  = $this->db->f('assignment');
915           $assignment_list[$i]['name'] = $this->db->f('name');
916           $assignment_list[$i]['code'] = $this->db->f('code');
917           $i++;
918         }
919             
920       for ($i=0; $i < count($assignment_list); $i++)
921         {         
922           $this->nextmatchs->template_alternate_row_color(&$this->t);
923           $this->t->set_var('name',$assignment_list[$i]['name']);
924           $this->t->set_var('code',$assignment_list[$i]['code']);
925           
926           $link_data['menuaction'] = 'eq.eq.assign_update';
927           $link_data['assignment'] = $assignment_list[$i]['assignment'];
928           $link_data['action'] = 'edit';
929           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
930           $this->t->set_var('lang_edit','Edit');
931
932           $link_data['menuaction'] = 'eq.eq.assign_update';
933           $link_data['assignment'] = '0';
934           $link_data['action'] = 'add';
935           $this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/eq/index.php',$link_data)
936                            . '"><input type="submit" name="Add" value="' . 'Add Assignment' .'"></font></form>');
937
938           $this->t->fp('list','assign_view',True);
939         }
940
941       $this->t->pfp('out','assign_view_t');
942       $this->save_sessiondata();
943     }
944
945     function assign_update()
946     {
947       $this->t->set_file(array('form' => 'assign_update.tpl'));
948       $this->t->set_block('form','add','addhandle');
949       $this->t->set_block('form','edit','edithandle');
950       $this->t->set_var('lang_done','Done');
951
952       $action = get_var('action',array('GET','POST'));
953       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.assign_view'));
954       $assignment['assignment'] = intval(get_var('assignment',array('GET','POST')));
955
956       if($action == 'save')
957         {
958           $assignment['name'] = get_var('name',array('POST'));
959           $assignment['code'] = get_var('code',array('POST'));
960           $this->db->query("UPDATE eq_assignment set " .
961                            "  name='" . $assignment['name'] . "'" .
962                            ", code='" . $assignment['code'] . "'" .
963                            " WHERE assignment=" . $assignment['assignment'],__LINE__,__FILE__);
964           
965           $this->assign_view();
966           return false;
967         }
968
969       if($action == 'insert')
970         {
971           $assignment['name'] = get_var('name',array('POST'));
972           $assignment['code'] = get_var('code',array('POST'));
973           $this->db->query("INSERT INTO eq_assignment (name,code) "
974                            . "VALUES ('" . $assignment['name'] . "','"
975                            . $assignment['code'] . "')",__LINE__,__FILE__);
976           $this->assign_view();
977           return false;
978         }
979       
980       if($action == 'add')
981         {
982           $assignment['assignment'] = 0;
983           $this->t->set_var('name','');
984           $this->t->set_var('code','');
985           $this->t->set_var('lang_done','Cancel');
986           $this->t->set_var('lang_action','Adding New Assignment');
987           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.assign_update&assignment='
988                                                                 . $assignment['assignment'] . '&action=' . 'insert'));
989         }
990
991       if($action == 'edit')
992         {
993           $sql = "SELECT * FROM eq_assignment WHERE assignment=" . $assignment['assignment'];
994           $this->db->query($sql,__LINE__,__FILE__);
995           $this->db->next_record();
996           $this->t->set_var('name', $this->db->f('name'));
997           $this->t->set_var('code', $this->db->f('code'));
998           $this->t->set_var('lang_done','Cancel');
999           $this->t->set_var('lang_action','Editing Assignment');
1000           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.assign_update&assignment='
1001                                                                 . $assignment['assignment'] . '&action=' . 'save'));
1002
1003         }
1004       
1005       $this->t->set_var('lang_reset','Clear Form');
1006       $this->t->set_var('lang_add','Add Assignment');
1007       $this->t->set_var('lang_save','Save Changes');
1008       $this->t->set_var('edithandle','');
1009       $this->t->set_var('addhandle','');
1010       
1011       $this->t->pfp('out','form');
1012       if($action == 'edit') { $this->t->pfp('addhandle','edit'); }
1013       if($action == 'add') { $this->t->pfp('addhandle','add'); }
1014       
1015       $this->save_sessiondata();
1016     }
1017
1018   function par_view()
1019     {
1020       $this->t->set_file(array('par_view_t' => 'par_view.tpl'));
1021       $this->t->set_block('par_view_t','header_list','list1');
1022       $this->t->set_block('par_view_t','elder_list','list2');
1023
1024       $sql = "SELECT * FROM eq_elder where valid=1";
1025       $this->db->query($sql,__LINE__,__FILE__);
1026       $i=0;
1027       while ($this->db->next_record())
1028         {
1029           $elder_name[$i] = $this->db->f('name');
1030           $elder_id[$i] = $this->db->f('elder');
1031           $i++;
1032         }
1033       array_multisort($elder_name, $elder_id);
1034
1035       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
1036       $this->db->query($sql,__LINE__,__FILE__);
1037       $total_records = $this->db->num_rows();
1038
1039       $i = 0;
1040       while ($this->db->next_record())
1041         {
1042           $activity_list[$i]['assignment'] = $this->db->f('assignment');
1043           $activity_list[$i]['date'] = $this->db->f('date');
1044           $activity_list[$i]['activity']  = $this->db->f('activity');
1045           $i++;
1046         }
1047
1048       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
1049       $this->db->query($sql,__LINE__,__FILE__);
1050       $i=0;
1051       while($this->db->next_record())
1052         {
1053           $assignment_list[$i]['assignment'] = $this->db->f('assignment');
1054           $assignment_list[$i]['name'] = $this->db->f('name');
1055           $assignment_list[$i]['code'] = $this->db->f('code');
1056           $i++;
1057         }
1058       
1059       $elder_width=300; $part_width=25; $assignment_width=50;
1060       $total_width=$elder_width+$part_width;
1061       for ($i=0; $i < count($assignment_list); $i++) {
1062         $this->t->set_var('assignment_name',$assignment_list[$i]['name']);
1063         $this->t->set_var('assignment_code',$assignment_list[$i]['code']);
1064         $this->t->fp('list1','header_list',True);
1065         $total_width += $assignment_width;
1066       }
1067
1068       for ($i=0; $i < count($elder_id); $i++) {
1069         $participated=0; $part_table = ''; 
1070         $this->nextmatchs->template_alternate_row_color(&$this->t);
1071         $this->t->set_var('elder_name',$elder_name[$i]);
1072         for ($j=0; $j < count($assignment_list); $j++) {
1073           $date = "0000-00-00"; $checkmark=0; $num_matches=0;
1074           for ($k=0; $k < count($activity_list); $k++) {
1075             if($assignment_list[$j]['assignment'] == $activity_list[$k]['assignment']) { 
1076               $sql = "SELECT * FROM eq_participation where "
1077                  . " activity=" . $activity_list[$k]['activity']
1078                  . " AND elder=" . $elder_id[$i];
1079               $this->db->query($sql,__LINE__,__FILE__);
1080               while($this->db->next_record()) {
1081                 if($activity_list[$k]['date'] > $date) { 
1082                   $date = $activity_list[$k]['date'];
1083                 }
1084                 $checkmark=1;
1085                 $num_matches++;
1086                 $participated++;
1087               }
1088             }
1089           }
1090           if($checkmark) {
1091             $part_table .= '<td align=center><img src="images/checkmark.gif">';
1092             $part_table .= '<font size=-2>'.$num_matches.'</font><br>';
1093             $part_table .= '<font size=-2>'.$date.'</font></td>';
1094           } else {
1095             $part_table .= '<td>&nbsp;</td>';
1096           }
1097         }
1098         if($participated) { $part_table .= '<td align=center><img src="images/checkmark.gif">'.$participated.'</td>'; }
1099         else { $part_table .= '<td>&nbsp;</td>'; }
1100         $this->t->set_var('part_table',$part_table);
1101         $this->t->fp('list2','elder_list',True);
1102       }
1103       $this->t->set_var('total_width',$total_width);
1104       $this->t->set_var('elder_width',$elder_width);
1105       $this->t->set_var('part_width',$part_width);
1106       $this->t->set_var('act_width',$act_width);
1107       $this->t->pfp('out','par_view_t');
1108       $this->save_sessiondata(); 
1109     }
1110
1111     function willing_view()
1112     {
1113       $this->t->set_file(array('willing_view_t' => 'willing_view.tpl'));
1114       $this->t->set_block('willing_view_t','header_list','list1');
1115       $this->t->set_block('willing_view_t','elder_list','list2');
1116
1117       $this->t->set_var('lang_filter','Filter');
1118       $this->t->set_var('lang_filter_unwilling','Filter out unwilling Elders:');
1119
1120       $filter_unwilling = get_var('filter_unwilling',array('POST'));
1121       $this->t->set_var('filter_unwilling',$filter_unwilling);
1122       
1123       if($filter_unwilling == 'y' || $filter_unwilling == '') { 
1124         $filter_input = "<input type=\"radio\" name=\"filter_unwilling\" value=\"y\" checked>Y";
1125         $filter_input.= "<input type=\"radio\" name=\"filter_unwilling\" value=\"n\">N";
1126         $filter_input.= "&nbsp;&nbsp;";
1127       } else {
1128         $filter_input = "<input type=\"radio\" name=\"filter_unwilling\" value=\"y\">Y";
1129         $filter_input.= "<input type=\"radio\" name=\"filter_unwilling\" value=\"n\" checked>N";
1130         $filter_input.= "&nbsp;&nbsp;";
1131       }
1132       $this->t->set_var('filter_input',$filter_input);
1133       
1134       $sql = "SELECT * FROM eq_elder where valid=1";
1135       $this->db->query($sql,__LINE__,__FILE__);
1136       $i=0;
1137       while ($this->db->next_record())
1138         {
1139           $elder_name[$i] = $this->db->f('name');
1140           $elder_id[$i] = $this->db->f('elder');
1141           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1142           $i++;
1143         }
1144       array_multisort($elder_name, $elder_id);
1145
1146       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
1147       $this->db->query($sql,__LINE__,__FILE__);
1148       $i=0;
1149       while($this->db->next_record())
1150         {
1151           $assignment_list[$i]['assignment'] = $this->db->f('assignment');
1152           $assignment_list[$i]['name'] = $this->db->f('name');
1153           $assignment_list[$i]['code'] = $this->db->f('code');
1154           $i++;
1155         }
1156
1157       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
1158       $this->db->query($sql,__LINE__,__FILE__);
1159       $total_records = $this->db->num_rows();
1160
1161       $i = 0;
1162       while ($this->db->next_record())
1163         {
1164           $activity_list[$i]['assignment'] = $this->db->f('assignment');
1165           $activity_list[$i]['date'] = $this->db->f('date');
1166           $activity_list[$i]['activity']  = $this->db->f('activity');
1167           $i++;
1168         }
1169       
1170       $elder_width=275; $willing_width=40; $assignment_width=50;
1171       $total_width=$elder_width+$willing_width;
1172       
1173       for ($i=0; $i < count($assignment_list); $i++) {
1174         $this->t->set_var('assignment_name',$assignment_list[$i]['name']);
1175         $this->t->set_var('assignment_code',$assignment_list[$i]['code']);
1176         $this->t->fp('list1','header_list',True);
1177         $total_width += $assignment_width;
1178         $total_willing[$i] = 0;
1179       }
1180
1181       for ($i=0; $i < count($elder_id); $i++) {
1182         $willing_table = ''; $elder_willing=0;
1183         $this->t->set_var('elder_name',$elder_name[$i]);
1184         $this->t->set_var('elder_phone',$elder_phone[$elder_id[$i]]);
1185         $this->t->set_var('editurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_update&elder_id='
1186                                                             . $elder_id[$i] . '&action=' . 'edit'));
1187         for ($j=0; $j < count($assignment_list); $j++) {
1188           $found_willingness=0; 
1189           $sql = "SELECT * FROM eq_willingness where "
1190              . " assignment=" . $assignment_list[$j]['assignment']
1191              . " AND elder=" . $elder_id[$i];
1192           $this->db->query($sql,__LINE__,__FILE__);
1193           while($this->db->next_record()) {
1194             $found_willingness=1;
1195             $date_part="";
1196             $sql = "SELECT * FROM eq_activity where "
1197                . " assignment=". $assignment_list[$j]['assignment']
1198                . " ORDER by date DESC";
1199             $this->db2->query($sql,__LINE__,__FILE__);
1200             if($this->db2->next_record()) {
1201               $activity = $this->db2->f('activity');
1202               $date = $this->db2->f('date');
1203               $sql = "SELECT * FROM eq_participation where "
1204                  . " activity=" . $activity
1205                  . " AND elder=". $elder_id[$i];
1206               $this->db3->query($sql,__LINE__,__FILE__);
1207               if($this->db3->next_record()) {
1208                 $date_part = $date;
1209               } 
1210             }
1211               
1212             if($this->db->f('willing') == 'y') {
1213               $total_willing[$j]++;
1214               $elder_willing=1;
1215               $willing_table .= '<td align=center><img src="images/checkmark.gif"><br><font size=-2>'.$date_part.'</font></td></td>';
1216             }
1217             else if($this->db->f('willing') == 'n') {
1218               $willing_table .= '<td align=center><img src="images/x.gif"></td>';
1219             }
1220             else {
1221               $elder_willing=1;
1222               $willing_table .= "<td>&nbsp;</td>";
1223             }
1224           }
1225           if(!$found_willingness) {
1226             $elder_willing=1;
1227             $willing_table .= "<td>&nbsp;</td>";
1228           }
1229         }
1230         if(($elder_willing == 1) || ($filter_unwilling == 'n')) { 
1231           $this->t->set_var('willing_table',$willing_table);
1232           $this->t->fp('list2','elder_list',True);
1233           $this->nextmatchs->template_alternate_row_color(&$this->t);
1234         } 
1235       }
1236
1237       $stat_table = '<td><b>Total Willing to Serve</b></td>';
1238       for ($j=0; $j < count($assignment_list); $j++) {
1239         $stat_table .= "<td align=center><b>".$total_willing[$j]."</b></td>";
1240       }
1241       $this->t->set_var('stat_table',$stat_table);
1242       
1243       $this->t->set_var('total_width',$total_width);
1244       $this->t->set_var('elder_width',$elder_width);
1245       $this->t->set_var('willing_width',$willing_width);
1246       $this->t->pfp('out','willing_view_t');
1247       $this->save_sessiondata(); 
1248     }
1249     
1250   function willing_update()
1251     {
1252       //print "<font color=red>Willingness Update Under Constrcution</font>";
1253       //$this->willing_view();
1254       //return false;
1255       
1256       $this->t->set_file(array('willing_update_t' => 'willing_update.tpl'));
1257       $this->t->set_block('willing_update_t','assignment_list','list');
1258       $this->t->set_block('willing_update_t','save','savehandle');
1259       
1260       $elder_id = get_var('elder_id',array('GET','POST'));
1261       $this->t->set_var('elder_id',$elder_id);
1262       $action = get_var('action',array('GET','POST'));
1263       
1264       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_view'));
1265       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_update&action=save'));
1266       $this->t->set_var('lang_done','Cancel');
1267       $this->t->set_var('title','Willingness Update ');
1268       
1269       if($action == 'save')
1270         {
1271           // Delete all the previous willingness entries for this elder
1272           $this->db->query("DELETE from eq_willingness where elder=" . $elder_id ,__LINE__,__FILE__);
1273               
1274           // Now, add the assignment willingness that is checked for this elder
1275           $new_data = get_var('willingness',array('POST'));
1276           foreach ($new_data as $data)
1277             {
1278               $data_array = explode("/",$data);
1279               $assignment = $data_array[0];
1280               $willing = $data_array[1];
1281               //print "elder_id: $elder_id assignment: $assignment willing: $willing<br>";
1282               $this->db->query("INSERT INTO eq_willingness (elder,assignment,willing) "
1283                                . "VALUES (" . $elder_id .",". $assignment .",'". $willing . "')",__LINE__,__FILE__);
1284             }      
1285           $this->willing_view();
1286           return false;
1287         }
1288       
1289       $assignment_width=300; $willing_width=25; $table_width=$assignment_width + $willing_width;
1290       $table_data=""; 
1291
1292       // Find out the elder's name
1293       $sql = "SELECT * FROM eq_elder WHERE elder=".$elder_id." AND valid=1";
1294       $this->db->query($sql,__LINE__,__FILE__);
1295       if($this->db->next_record()) {
1296         $elder_name = $this->db->f('name');
1297         $this->t->set_var('elder_name',$elder_name);
1298       }
1299       
1300       // Select all the assignments
1301       $sql = "SELECT * FROM eq_assignment ORDER by name ASC";
1302       $this->db->query($sql,__LINE__,__FILE__);
1303       
1304       while ($this->db->next_record())
1305         {
1306           $assignment = $this->db->f('assignment');
1307           $assignment_name = $this->db->f('name');
1308           $assignment_code = $this->db->f('code');
1309
1310           $this->nextmatchs->template_alternate_row_color(&$this->t);
1311           $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$assignment_name</td>";
1312           
1313           $header_row="<th width=$comp_width><font size=-2>Assignments</th><th>Willingness</th>";
1314           $sql = "SELECT * FROM eq_willingness WHERE elder=".$elder_id." AND assignment=".$assignment;
1315           $this->db2->query($sql,__LINE__,__FILE__);
1316           $value = $assignment;
1317              
1318           if($this->db2->next_record()) {
1319               if($this->db2->f('willing') == 'y') {
1320                 $table_data .= '<td width=100 align=center>';
1321                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y" checked>Y';
1322                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1323                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/"> ';
1324                 $table_data .= '</td>';
1325               } else if($this->db2->f('willing') == 'n') {
1326                 $table_data .= '<td width=100 align=center>';
1327                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1328                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n" checked>N';
1329                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/">';
1330                 $table_data .= '</td>';
1331               } else {
1332                 $table_data .= '<td width=100 align=center>';
1333                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1334                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1335                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/" checked> ';
1336                 $table_data .= '</td>';
1337               }
1338             }
1339           else {
1340             $table_data .= '<td width=100 align=center>';
1341             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1342             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1343             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/" checked> ';
1344             $table_data .= '</td>';
1345           }
1346           
1347           $table_data .= "\n";
1348           $table_data .= "</tr>"; 
1349           $table_data .= "<tr><td colspan=20></td></tr>";
1350         }
1351       
1352       $table_data .= "<tr><td colspan=20><hr></td></tr>";
1353       
1354       $this->t->set_var('table_width',$table_width);
1355       $this->t->set_var('header_row',$header_row);
1356       $this->t->set_var('table_data',$table_data);
1357       $this->t->fp('list','assignment_list',True);
1358
1359       $this->t->set_var('lang_reset','Clear Form');
1360       $this->t->set_var('lang_save','Save Changes');
1361       $this->t->set_var('savehandle','');
1362       
1363       $this->t->pfp('out','willing_update_t');
1364       $this->t->pfp('addhandle','save');
1365       
1366       $this->save_sessiondata();
1367     }
1368     
1369     
1370     function ppi_sched()
1371       {
1372       $this->t->set_file(array('ppi_sched_t' => 'ppi_sched.tpl'));
1373       $this->t->set_block('ppi_sched_t','elder_list','elderlist');
1374       $this->t->set_block('ppi_sched_t','appt_list','apptlist');
1375       $action = get_var('action',array('GET','POST'));
1376
1377       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1378       $this->t->set_var('lang_reset','Clear Changes');
1379       
1380       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1381       $this->t->set_var('ppi_link_title','Yearly PPIs');
1382       
1383       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1384       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1385
1386       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched&action=save'));
1387       $this->t->set_var('title','Yearly PPI Scheduler');
1388
1389       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
1390       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
1391       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1392       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1393       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1394       $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
1395       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1396       $table_data=""; $completed_data=""; $totals_data="";
1397
1398       $year = date('Y');
1399       
1400       if($action == 'save')
1401         {
1402           // Save any changes made to the appointment table
1403           $new_data = get_var('appt_notes',array('POST'));
1404           if($new_data != "") { 
1405             foreach ($new_data as $entry)
1406               {
1407                 $elder = $entry['elder'];
1408                 $appointment = $entry['appointment'];
1409                 
1410                 //print "elder: $elder appointment: $appointment <br>";
1411                 
1412                 //Only perform a database update if we have made a change to this appointment
1413                 $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and elder='$elder'";
1414                 $this->db->query($sql,__LINE__,__FILE__);
1415                 if(!$this->db->next_record()) {
1416                   // Perform database save actions here
1417                   $this->db->query("UPDATE eq_appointment set " .
1418                                    " elder='" . $elder . "'" .
1419                                    " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1420                   // Email the appointment
1421                   $this->email_appt($appointment);
1422                 }
1423                 
1424               }
1425           }
1426           
1427           // Save any changes made to the ppi notes table
1428           $new_data = get_var('ppi_notes',array('POST'));
1429           foreach ($new_data as $entry)
1430            {
1431              $ppi_notes = $entry['notes'];
1432              $elder_id = $entry['elder_id'];
1433              $ppi_pri = $entry['pri'];
1434              
1435              // Perform database save actions here
1436              $this->db->query("UPDATE eq_elder set " .
1437                               " ppi_notes='" . $ppi_notes . "'" .
1438                               ",ppi_pri='" . $ppi_pri . "'" .
1439                               " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1440              
1441            }
1442
1443           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched');
1444           //Header('Location: ' . $take_me_to_url);
1445         }
1446
1447       // Get the EQ President
1448       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
1449       $this->db->query($sql,__LINE__,__FILE__);
1450       if($this->db->next_record()) {
1451         $president_name = $this->db->f('name');
1452         $president_id = $this->db->f('elder');
1453         $presidency_id = $this->db->f('presidency');
1454         $interviewer = $this->db->f('elder');
1455         $district_number = '*';
1456         $district_name = $president_name;
1457       } else {
1458         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
1459         return;
1460       }
1461       
1462       // create the elder id -> elder name mapping
1463       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1464       $this->db->query($sql,__LINE__,__FILE__);
1465       $i=0;
1466       $elder_id = NULL;
1467       $elder_name = NULL;
1468       while ($this->db->next_record())
1469         {
1470           $elder_name[$i] = $this->db->f('name');
1471           $elder_id[$i] = $this->db->f('elder');
1472           $i++;
1473         }
1474       array_multisort($elder_name, $elder_id);
1475
1476       // APPOINTMENT TABLE
1477       $date_width=150; $time_width=100; $elder_width=200;
1478       $appt_table_width=$date_width + $time_width + $elder_width;
1479       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1480       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1481       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1482       $appt_table_data = ""; 
1483
1484       $total_elders=0; $elders_with_yearly_ppi=0;
1485       
1486       // Display a scheduling table for the EQ President
1487       $table_data=""; $appt_table_data="";
1488       $table_title = "District ".$district_number.": ".$district_name.": All Elders with Yearly PPI Not Completed";
1489       $appt_table_title = "District ".$district_number.": ".$district_name.": Yearly PPI Appointment Slots";
1490       $this->t->set_var('table_title',$table_title);
1491       $this->t->set_var('appt_table_title',$appt_table_title);
1492       
1493       // query the database for all the appointments
1494       $sql = "SELECT * FROM eq_appointment where presidency=".$presidency_id." and date>=CURDATE() ORDER BY date ASC, time ASC";
1495       $this->db->query($sql,__LINE__,__FILE__);
1496         
1497       while ($this->db->next_record())
1498         {
1499           $appointment = $this->db->f('appointment');
1500           $elder = $this->db->f('elder');
1501           
1502           $date = $this->db->f('date');
1503           $date_array = explode("-",$date);
1504           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1505           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1506           
1507           $time = $this->db->f('time');
1508           $time_array = explode(":",$time);
1509           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1510           
1511           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1512           $appt_table_data.= "<td align=center>$day_string</td>";
1513           $appt_table_data.= "<td align=center>$time_string</td>";
1514
1515           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1516           $appt_table_data.= '<option value=0></option>';
1517           for ($i=0; $i < count($elder_id); $i++) {
1518             $id = $elder_id[$i];
1519             $name = $elder_name[$i];
1520             if($elder_id[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1521             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1522           }
1523           $appt_table_data.='</select></td>';
1524
1525           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1526           
1527           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1528           $this->t->set_var('tr_color',$tr_color);
1529         }
1530
1531       $this->t->set_var('appt_table_data',$appt_table_data);
1532       $this->t->set_var('appt_header_row',$appt_header_row);
1533       $this->t->set_var('appt_table_width',$appt_table_width);
1534
1535       // PPI SCHEDULING TABLE
1536       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY ppi_pri ASC";
1537       $this->db->query($sql,__LINE__,__FILE__);
1538       
1539       $i=0; 
1540       $elder_id = NULL;
1541       while ($this->db->next_record())
1542         {
1543           $elder_id[$i] = $this->db->f('elder');
1544           $elder_name[$i] = $this->db->f('name');
1545           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1546           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1547           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1548           $i++;
1549           $total_elders++;
1550         }
1551
1552       $max = count($elder_id);
1553       
1554       for($i=0; $i < $max; $i++) {
1555           $id = $elder_id[$i];
1556           $name = $elder_name[$i];
1557           $phone = $elder_phone[$id];
1558           $ppi_pri = $elder_ppi_pri[$id];
1559           $ppi_notes = $elder_ppi_notes[$id];
1560
1561           // If this elder has had a yearly PPI this year, don't show him on the schedule list
1562           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1563           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1564              "AND elder=" . $id;
1565           $this->db2->query($sql,__LINE__,__FILE__);
1566           
1567           if(!$this->db2->next_record()) {
1568             $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1569             $this->db->query($sql,__LINE__,__FILE__);
1570             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1571             $link_data['menuaction'] = 'eq.eq.ppi_update';
1572             $link_data['elder'] = $id;
1573             $link_data['name'] = $name;
1574             $link_data['ppi'] = '';
1575             $link_data['eqpresppi'] = 1;
1576             $link_data['action'] = 'add';
1577             $link_data['interviewer'] = $interviewer;
1578             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1579             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1580             $this->t->set_var('tr_color',$tr_color);
1581             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1582             $table_data.= "<td align=center>$phone</td>";
1583             //$table_data.= "<td align=center>$ppi_pri</td>";
1584             $table_data.= "<td align=center>";
1585             $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
1586             foreach(range(0,6) as $num) {
1587               if($num == 0) { $num = 1; } else {$num = $num*5; }
1588               if($ppi_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1589               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1590             }
1591             $table_data.= '</select></td>';
1592             $table_data.= "<td align=center>$date</td>";
1593             $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
1594             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_id]" value="'.$id.'">';
1595             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_name]" value="'.$name.'">';
1596             $table_data.= '</td>';
1597             $table_data.= '</tr>';
1598           } else {
1599             $link_data['menuaction'] = 'eq.eq.ppi_update';
1600             $link_data['interviewer'] = $this->db2->f('interviewer');
1601             $link_data['elder'] = $this->db2->f('elder');
1602             $link_data['name'] = $name;
1603             $link_data['ppi'] = $this->db2->f('ppi');
1604             $link_data['eqpresppi'] = $this->db2->f('eqpresppi');
1605             $link_data['action'] = 'view';
1606             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1607             $elders_with_yearly_ppi++;
1608             $date = $this->db2->f('date');
1609             $ppi_notes = $this->db2->f('notes');
1610             if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
1611             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1612             $this->t->set_var('tr_color2',$tr_color2);
1613             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1614             $completed_data.= "<td align=center>$phone</td>";
1615             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1616             $completed_data.= "<td align=left>$ppi_notes</td>";
1617             $completed_data.= '</tr>';
1618           }
1619       } // End for Elders Loop
1620
1621       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1622       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1623       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1624       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1625       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1626       $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1627             
1628       $this->t->set_var('table_width',$table_width);
1629       $this->t->set_var('header_row',$header_row);
1630       $this->t->set_var('table_data',$table_data);
1631       $this->t->set_var('completed_header_row',$completed_header_row);
1632       $this->t->set_var('completed_table_width',$completed_table_width);
1633       $this->t->set_var('completed',$completed_data);
1634       $this->t->fp('elderlist','elder_list',True); 
1635       
1636       $elders_width=300; $totals_width=100;
1637       $totals_table_width=$elders_width + $totals_width;
1638       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1639       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1640       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1641       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
1642       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
1643       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
1644       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1645       $this->t->set_var('tr_color',$tr_color);
1646       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1647       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1648       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1649       $totals_data.= "</tr>";
1650
1651       $this->t->set_var('totals',$totals_data);
1652       $this->t->set_var('totals_header_row',$totals_header_row);
1653       $this->t->set_var('totals_table_width',$totals_table_width);
1654       
1655       $this->t->pfp('out','ppi_sched_t');
1656       $this->save_sessiondata(); 
1657       
1658     }
1659   
1660   function int_sched()
1661     {
1662       $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1663       $this->t->set_block('int_sched_t','elder_list','elderlist');
1664       $this->t->set_block('int_sched_t','appt_list','apptlist');
1665       $action = get_var('action',array('GET','POST'));
1666
1667       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1668       $this->t->set_var('lang_reset','Clear Changes');
1669       
1670       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
1671       $this->t->set_var('int_link_title','Hometeaching Interviews');
1672       
1673       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
1674       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
1675
1676       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched&action=save'));
1677       $this->t->set_var('title','Hometeaching Interviews Scheduler');
1678
1679       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
1680       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $int_date_width;
1681       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1682       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1683       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1684       $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
1685       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1686       $table_data=""; $completed_data=""; $totals_data="";
1687
1688       $year = date('Y');
1689       $month = date('m');
1690       $nextyear = $year + 1;
1691       if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
1692       if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
1693       if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
1694       if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
1695       //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
1696       
1697       if($action == 'save')
1698         {
1699           // Save any changes made to the appointment table
1700           $new_data = get_var('appt_notes',array('POST'));
1701           if($new_data != "") { 
1702             foreach ($new_data as $entry)
1703               {
1704                 $elder = $entry['elder'];
1705                 $appointment = $entry['appointment'];
1706                 
1707                 //print "elder: $elder appointment: $appointment <br>";
1708                 //Only perform a database update if we have made a change to this appointment
1709                 $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and elder='$elder'";
1710                 $this->db->query($sql,__LINE__,__FILE__);
1711                 if(!$this->db->next_record()) {
1712                   // Perform database save actions here
1713                   $this->db->query("UPDATE eq_appointment set " .
1714                                    " elder='" . $elder . "'" .
1715                                    " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1716                   
1717                   // Email the appointment
1718                   $this->email_appt($appointment);
1719                 }
1720               }
1721           }
1722           
1723           // Save any changes made to the int notes table
1724           $new_data = get_var('int_notes',array('POST'));
1725           foreach ($new_data as $entry)
1726            {
1727              $int_notes = $entry['notes'];
1728              $elder_id = $entry['elder_id'];
1729              $elder_name = $entry['elder_name'];
1730              $int_pri = $entry['pri'];
1731              $aaronic = $entry['aaronic'];
1732              //print "int_notes: $int_notes elder_name: $elder_name aaronic: $aaronic <Br>";
1733              if($aaronic == 0) { 
1734                // Perform database save actions here
1735                $this->db->query("UPDATE eq_elder set " .
1736                                 " int_notes='" . $int_notes . "'" .
1737                                 ",int_pri='" . $int_pri . "'" .
1738                                 " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1739              } 
1740              
1741            }
1742
1743           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched');
1744           //Header('Location: ' . $take_me_to_url);
1745         }
1746
1747       // Get the Districts
1748       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1749       $this->db->query($sql,__LINE__,__FILE__);
1750       $i=0;
1751       while ($this->db->next_record())
1752         {
1753           $district = $this->db->f('district');
1754           $districts[$i]['district'] = $this->db->f('district');
1755           $districts[$i]['name'] = $this->db->f('name');
1756           $districts[$i]['supervisor'] = $this->db->f('supervisor');
1757           $sql = "SELECT * FROM eq_presidency where district=$district and valid=1";
1758           $this->db2->query($sql,__LINE__,__FILE__);
1759           if($this->db2->next_record()) {
1760             $districts[$i]['presidency'] = $this->db2->f('presidency');
1761           }
1762           $i++;
1763         }
1764       
1765       // create the elder id -> elder name mapping
1766       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1767       $this->db->query($sql,__LINE__,__FILE__);
1768       $i=0;
1769       $elder_id_data = NULL;
1770       $elder_name_data = NULL;
1771       while ($this->db->next_record())
1772         {
1773           $elder_name_data[$i] = $this->db->f('name');
1774           $elder_id_data[$i] = $this->db->f('elder');
1775           $i++;
1776         }
1777       array_multisort($elder_name_data, $elder_id_data);
1778
1779       // APPOINTMENT TABLE
1780       $district = 1;
1781       $date_width=150; $time_width=100; $elder_width=200;
1782       $appt_table_width=$date_width + $time_width + $elder_width;
1783       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1784       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1785       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1786       $appt_table_data = ""; 
1787
1788       $total_comps=0; $comps_with_quarterly_int=0;
1789       
1790       // Display a scheduling table for each district
1791       for ($d=0; $d < count($districts); $d++) {
1792       $table_data=""; $appt_table_data="";
1793       $this->t->set_var('district_number',$districts[$d]['district']);
1794       $this->t->set_var('district_name',$districts[$d]['name']);        
1795       $supervisor = $districts[$d]['supervisor'];
1796       $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Elders with Interviews Not Completed";
1797       $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
1798       $this->t->set_var('table_title',$table_title);
1799       $this->t->set_var('appt_table_title',$appt_table_title);
1800       
1801       // query the database for all the appointments
1802       $sql = "SELECT * FROM eq_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1803       $this->db->query($sql,__LINE__,__FILE__);
1804         
1805       while ($this->db->next_record())
1806         {
1807           $appointment = $this->db->f('appointment');
1808           $elder = $this->db->f('elder');
1809           
1810           $date = $this->db->f('date');
1811           $date_array = explode("-",$date);
1812           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1813           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1814           
1815           $time = $this->db->f('time');
1816           $time_array = explode(":",$time);
1817           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1818           
1819           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1820           $appt_table_data.= "<td align=center>$day_string</td>";
1821           $appt_table_data.= "<td align=center>$time_string</td>";
1822
1823           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1824           $appt_table_data.= '<option value=0></option>';
1825           for ($i=0; $i < count($elder_id_data); $i++) {
1826             $id = $elder_id_data[$i];
1827             $name = $elder_name_data[$i];
1828             if($elder_id_data[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1829             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1830           }
1831           $appt_table_data.='</select></td>';
1832
1833           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1834           
1835           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1836           $this->t->set_var('tr_color',$tr_color);
1837         }
1838
1839       $this->t->set_var('appt_table_data',$appt_table_data);
1840       $this->t->set_var('appt_header_row',$appt_header_row);
1841       $this->t->set_var('appt_table_width',$appt_table_width);
1842
1843       // INTERVIEW SCHEDULING TABLE
1844       
1845       // Select all the unique companionship numbers for this district
1846       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$d]['district'];
1847       $this->db->query($sql,__LINE__,__FILE__);
1848       $j=0;
1849       while ($this->db->next_record())
1850         {
1851           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1852           $j++;
1853         }
1854
1855       $i=0;
1856       for ($j=0; $j < count($unique_companionships); $j++) {
1857         // Select all the companions from each companionship
1858         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
1859            "companionship=". $unique_companionships[$j]['companionship'];
1860         $this->db->query($sql,__LINE__,__FILE__);
1861         $k=0; $int_completed=0;
1862         $comp = $unique_companionships[$j]['companionship'];
1863         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1864         $this->t->set_var('tr_color',$tr_color);
1865         $total_comps++;
1866         while ($this->db->next_record())
1867             {         
1868               // Get this companions information
1869               $elder_id = $this->db->f('elder');
1870               $aaronic_id = $this->db->f('aaronic');
1871              
1872               $sql = "SELECT * FROM eq_elder where elder=$elder_id";
1873               $this->db2->query($sql,__LINE__,__FILE__);        
1874               if($this->db2->next_record())
1875                 {
1876                   $elder_id = $this->db2->f('elder');
1877                   $elder_name = $this->db2->f('name');
1878                   $elder_phone[$elder_id] = $this->db2->f('phone');
1879                   $elder_int_pri[$elder_id] = $this->db2->f('int_pri');
1880                   $elder_int_notes[$elder_id] = $this->db2->f('int_notes');
1881                   $elder_aaronic = 0;
1882                 }
1883               else {
1884                 $sql = "SELECT * FROM eq_aaronic where aaronic=$aaronic_id";
1885                 $this->db2->query($sql,__LINE__,__FILE__);
1886                 if($this->db2->next_record())
1887                   {
1888                     $elder_id = $this->db2->f('aaronic');
1889                     $elder_name = $this->db2->f('name');
1890                     $elder_phone[$elder_id] = $this->db2->f('phone');
1891                     $elder_aaronic = 1;
1892                   }
1893               }
1894
1895               $id = $elder_id;
1896               $name = $elder_name;
1897               $phone = $elder_phone[$id];
1898               $int_pri = $elder_int_pri[$id];
1899               $int_notes = $elder_int_notes[$id];
1900               $aaronic = $elder_aaronic;
1901               
1902               // If the companionship has already had its quarterly interview,
1903               // Skip the other companion in the companionship.
1904               if($int_completed == 1) {
1905                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1906                 $completed_data.= "<td align=center>$phone</td>";
1907                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1908                 $completed_data.= "<td align=left>$int_notes</td>";
1909                 $completed_data.= '</tr>';
1910                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1911                 $this->t->set_var('tr_color2',$tr_color2);
1912                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1913                 $this->t->set_var('tr_color',$tr_color);
1914                 continue;
1915               }
1916               
1917               // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
1918               $sql = "SELECT * FROM eq_ppi WHERE date >= '$quarter_start' AND date < '$quarter_end' ".
1919                  "AND elder=" . $id;
1920               $this->db2->query($sql,__LINE__,__FILE__);
1921               
1922               if(!$this->db2->next_record()) {
1923                 $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1924                 $this->db3->query($sql,__LINE__,__FILE__);
1925                 if($this->db3->next_record()) { $date = $this->db3->f('date'); } else { $date = ""; }
1926                 $link_data['menuaction'] = 'eq.eq.int_update';
1927                 $link_data['elder'] = $id;
1928                 $link_data['aaronic'] = 0;
1929                 $link_data['name'] = $name;
1930                 $link_data['ppi'] = '';
1931                 $link_data['action'] = 'add';
1932                 $link_data['interviewer'] = $districts[$d]['supervisor'];
1933                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1934                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1935                 $table_data.= "<td align=center>$phone</td>";
1936                 $table_data.= "<td align=center>";
1937                 if($aaronic == 0) { 
1938                   $table_data.= '<select name=int_notes['.$i.'][pri]>';
1939                   foreach(range(0,6) as $num) {
1940                     if($num == 0) { $num = 1; } else {$num = $num*5; }
1941                     if($int_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1942                     $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1943                   }
1944                   $table_data.= '</select></td>';
1945                 }
1946                 $table_data.= "<td align=center>$date</td>";
1947                 if($aaronic == 0) { 
1948                   $table_data.= '<td><input type=text size="50" maxlength="128" name="int_notes['.$i.'][notes]" value="'.$int_notes.'">';
1949                 }
1950                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_id]" value="'.$id.'">';
1951                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_name]" value="'.$name.'">';
1952                 $table_data.= '<input type=hidden name="int_notes['.$i.'][aaronic]" value="'.$aaronic.'">';
1953                 $table_data.= '</td>';
1954                 $table_data.= '</tr>'."\n";
1955                 $i++;
1956               } else {
1957                 $link_data['menuaction'] = 'eq.eq.int_update';
1958                 $link_data['interviewer'] = $this->db2->f('interviewer');
1959                 $link_data['elder'] = $this->db2->f('elder');
1960                 $link_data['aaronic'] = $this->db2->f('aaronic');
1961                 $link_data['name'] = $name;
1962                 $link_data['ppi'] = $this->db2->f('ppi');
1963                 $link_data['action'] = 'view';
1964                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1965                 $comps_with_quarterly_int++;
1966                 $int_completed=1;
1967                 $date = $this->db2->f('date');
1968                 $int_notes = $this->db2->f('notes');
1969                 if(strlen($int_notes) > 40) { $int_notes = substr($int_notes,0,40) . "..."; }
1970                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1971                 $completed_data.= "<td align=center>$phone</td>";
1972                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1973                 $completed_data.= "<td align=left>$int_notes</td>";
1974                 $completed_data.= '</tr>';
1975               }
1976             }
1977       }
1978         
1979       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1980       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1981       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1982       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1983       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1984       $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
1985             
1986       $this->t->set_var('table_width',$table_width);
1987       $this->t->set_var('header_row',$header_row);
1988       $this->t->set_var('table_data',$table_data);
1989       $this->t->set_var('completed_header_row',$completed_header_row);
1990       $this->t->set_var('completed_table_width',$completed_table_width);
1991       $this->t->set_var('completed',$completed_data);
1992       $this->t->fp('elderlist','elder_list',True);
1993
1994       } // End for each district loop
1995
1996       
1997       $elders_width=300; $totals_width=100;
1998       $totals_table_width=$elders_width + $totals_width;
1999       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
2000       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2001       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2002       $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
2003       $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
2004       $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
2005       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2006       $this->t->set_var('tr_color',$tr_color);
2007       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2008       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2009       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2010       $totals_data.= "</tr>";
2011
2012       $this->t->set_var('totals',$totals_data);
2013       $this->t->set_var('totals_header_row',$totals_header_row);
2014       $this->t->set_var('totals_table_width',$totals_table_width);
2015       
2016       $this->t->pfp('out','int_sched_t');
2017       $this->save_sessiondata(); 
2018       
2019     }
2020   
2021   function vis_sched()
2022     {
2023       $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
2024       $this->t->set_block('vis_sched_t','family_list','familylist');
2025       $this->t->set_block('vis_sched_t','appt_list','apptlist');
2026       $action = get_var('action',array('GET','POST'));
2027
2028       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
2029       $this->t->set_var('lang_reset','Clear Changes');
2030       
2031       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2032       $this->t->set_var('vis_link_title','View Yearly Visits');
2033       
2034       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2035       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2036
2037       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched&action=save'));
2038       $this->t->set_var('title','EQ Presidency Yearly Visit Scheduler');
2039
2040       $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
2041       $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
2042       $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
2043       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
2044       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
2045       $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
2046       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2047       $table_data=""; $completed_data=""; $totals_data="";
2048
2049       $year = date('Y');
2050       
2051       if($action == 'save')
2052         {
2053           // Save any changes made to the appointment table
2054           $new_data = get_var('appt_notes',array('POST'));
2055           if($new_data != "") { 
2056             foreach ($new_data as $entry)
2057               {
2058                 $family = $entry['family'];
2059                 $appointment = $entry['appointment'];
2060                 
2061                 //Only perform a database update if we have made a change to this appointment
2062                 $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and family='$family'";
2063                 $this->db->query($sql,__LINE__,__FILE__);
2064                 if(!$this->db->next_record()) {
2065                   
2066                   // Perform database save actions here
2067                   $this->db->query("UPDATE eq_appointment set " .
2068                                    " family='" . $family . "'" .
2069                                    " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2070                   
2071                   // Email the appointment
2072                   $this->email_appt($appointment);
2073                 }
2074               }
2075           }
2076           
2077           // Save any changes made to the visit notes table
2078           $new_data = get_var('vis_notes',array('POST'));
2079           foreach ($new_data as $entry)
2080            {
2081              $visit_notes = $entry['notes'];
2082              $family_id = $entry['family_id'];
2083              $visit_pri = $entry['pri'];
2084              
2085              // Perform database save actions here
2086              $this->db->query("UPDATE eq_family set " .
2087                               " visit_notes='" . $visit_notes . "'" .
2088                               ",visit_pri='" . $visit_pri . "'" .
2089                               " WHERE family=" . $family_id,__LINE__,__FILE__);
2090              
2091            }
2092
2093           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched');
2094           //Header('Location: ' . $take_me_to_url);
2095         }
2096
2097       // APPOINTMENT TABLE
2098       $date_width=150; $time_width=100; $family_width=250;
2099       $appt_table_width=$date_width + $time_width + $family_width;
2100       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2101       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2102       $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
2103       $appt_table_data = ""; 
2104
2105       // Find out what the EQ Presidency ID is
2106       $sql = "SELECT * FROM eq_presidency where eqpres=1 and valid=1";
2107       $this->db->query($sql,__LINE__,__FILE__);
2108       if($this->db->next_record()) {
2109         $presidency_name = $this->db->f('name');
2110         $presidency_id = $this->db->f('presidency');
2111       } else {
2112         print "<hr><font color=red><h3>-E- Unable to locate EQ Presidency in eq_presidency table</h3></font></hr>";
2113         return;
2114       }
2115       
2116       // create the family id -> family name mapping
2117       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
2118       $this->db->query($sql,__LINE__,__FILE__);
2119       $i=0;
2120       $family_id = NULL;
2121       while ($this->db->next_record())
2122         {
2123           $family_id[$i] = $this->db->f('family');
2124           $family_name[$i] = $this->db->f('name');
2125           $i++;
2126         }
2127       array_multisort($family_name, $family_id);
2128       
2129       // query the database for all the appointments
2130       $sql = "SELECT * FROM eq_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
2131       $this->db->query($sql,__LINE__,__FILE__);
2132
2133       while ($this->db->next_record())
2134         {
2135           $appointment = $this->db->f('appointment');
2136           $family = $this->db->f('family');
2137
2138           $date = $this->db->f('date');
2139           $date_array = explode("-",$date);
2140           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2141           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2142           
2143           $time = $this->db->f('time');
2144           $time_array = explode(":",$time);
2145           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2146           
2147           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2148           $appt_table_data.= "<td align=center>$day_string</td>";
2149           $appt_table_data.= "<td align=center>$time_string</td>";
2150
2151           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
2152           $appt_table_data.= '<option value=0></option>';
2153           for ($i=0; $i < count($family_id); $i++) {
2154             $id = $family_id[$i];
2155             $name = $family_name[$i];
2156             if($family_id[$i] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
2157             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
2158           }
2159           $appt_table_data.='</select></td>';
2160
2161           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2162           
2163           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2164           $this->t->set_var('tr_color',$tr_color);
2165         }
2166
2167       $this->t->set_var('appt_table_data',$appt_table_data);
2168       $this->t->set_var('appt_header_row',$appt_header_row);
2169       $this->t->set_var('appt_table_width',$appt_table_width);
2170
2171       
2172       // VISIT SCHEDULING TABLE
2173       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY visit_pri ASC";
2174       $this->db->query($sql,__LINE__,__FILE__);
2175
2176       $total_families=0; $families_with_yearly_visit=0;
2177       
2178       $i=0; 
2179       $family_id = NULL;
2180       $family_name = NULL;
2181       $family_phone = NULL;
2182       $family_visit_pri = NULL;
2183       $family_visit_notes = NULL;
2184       while ($this->db->next_record())
2185         {
2186           $family_id[$i] = $this->db->f('family');
2187           $family_name[$i] = $this->db->f('name');
2188           $family_phone[$family_id[$i]] = $family_id[$i] . " ERROR";
2189           $family_visit_pri[$family_id[$i]] = $this->db->f('visit_pri');
2190           $family_visit_notes[$family_id[$i]] = $this->db->f('visit_notes');
2191           $i++;
2192           $total_families++;
2193         }
2194
2195       $sql = "SELECT * FROM eq_parent where valid=1";
2196       $this->db->query($sql,__LINE__,__FILE__);
2197       while ($this->db->next_record())
2198         {
2199           $family = $this->db->f('family');
2200           $phone = $this->db->f('phone');
2201           $family_phone[$family] = $phone;
2202         }
2203       
2204       $max = count($family_id);
2205       
2206       for($i=0; $i < $max; $i++) {
2207           $id = $family_id[$i];
2208           $name = $family_name[$i];
2209           $phone = $family_phone[$id];
2210           $vis_pri = $family_visit_pri[$id];
2211           $vis_notes = $family_visit_notes[$id];
2212
2213           // If this family has had a yearly visit this year, don't show them on the schedule list
2214           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2215           $sql = "SELECT * FROM eq_visit WHERE date > '$year_start' AND date < '$year_end' ".
2216              "AND family=" . $id . " AND companionship=0";
2217           $this->db2->query($sql,__LINE__,__FILE__);
2218           
2219           if(!$this->db2->next_record()) {
2220             $sql = "SELECT * FROM eq_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
2221             $this->db->query($sql,__LINE__,__FILE__);
2222             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
2223             $link_data['menuaction'] = 'eq.eq.vis_update';
2224             $link_data['visit'] = '';
2225             $link_data['family'] = $id;
2226             $link_data['name'] = $name;
2227             $link_data['action'] = 'add';
2228             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2229             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2230             $table_data.= "<td align=center>$phone</td>";
2231             $table_data.= "<td align=center>";
2232             $table_data.= '<select name=vis_notes['.$i.'][pri]>';
2233             foreach(range(0,6) as $num) {
2234               if($num == 0) { $num = 1; } else {$num = $num*5; }
2235               if($vis_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
2236               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2237             }
2238             $table_data.= '</select></td>';
2239             $table_data.= "<td align=center>$date</td>";
2240             $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$i.'][notes]" value="'.$vis_notes.'">';
2241             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_id]" value="'.$id.'">';
2242             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_name]" value="'.$name.'">';
2243             $table_data.= '</td>';
2244             $table_data.= '</tr>';
2245             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2246             $this->t->set_var('tr_color',$tr_color);
2247           } else {
2248             $link_data['menuaction'] = 'eq.eq.vis_update';
2249             $link_data['visit'] = $this->db2->f('visit');
2250             $link_data['family'] = $this->db2->f('family');
2251             $link_data['name'] = $name;
2252             $link_data['date'] = $this->db2->f('date');
2253             $link_data['action'] = 'view';
2254             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
2255             $families_with_yearly_visit++;
2256             $date = $this->db2->f('date');
2257             $vis_notes = $this->db2->f('notes');
2258             if(strlen($vis_notes) > 40) { $vis_notes = stripslashes(substr($vis_notes,0,40) . "..."); }
2259             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2260             $completed_data.= "<td align=center>$phone</td>";
2261             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2262             $completed_data.= "<td align=left>$vis_notes</td>";
2263             $completed_data.= '</tr>';
2264             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2265             $this->t->set_var('tr_color2',$tr_color2);
2266           }
2267       }
2268
2269       $name_width=190; $phone_width=100; $date_width=100; $notes_width=300;
2270       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2271       $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
2272       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2273       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2274       $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
2275       
2276       $family_width=300; $totals_width=100;
2277       $totals_table_width=$family_width + $totals_width;
2278       $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
2279       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2280       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2281       $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
2282       $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
2283       $percent = ceil(($families_with_yearly_visit / $total_families)*100);
2284       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2285       $this->t->set_var('tr_color',$tr_color);
2286       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2287       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2288       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2289       $totals_data.= "</tr>";
2290       
2291       $this->t->set_var('table_width',$table_width);
2292       $this->t->set_var('header_row',$header_row);
2293       $this->t->set_var('table_data',$table_data);
2294       $this->t->set_var('totals_header_row',$totals_header_row);
2295       $this->t->set_var('totals_table_width',$totals_table_width);
2296       $this->t->set_var('completed_header_row',$completed_header_row);
2297       $this->t->set_var('completed_table_width',$completed_table_width);
2298       $this->t->set_var('completed',$completed_data);
2299       $this->t->set_var('totals',$totals_data);
2300       $this->t->fp('familylist','family_list',True);
2301       $this->t->fp('apptlist','appt_list',True);
2302       
2303       $this->t->pfp('out','vis_sched_t');
2304       $this->save_sessiondata(); 
2305       
2306     }
2307   
2308   function ppi_view()
2309     {
2310       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
2311       $this->t->set_block('ppi_view_t','district_list','list');
2312
2313       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2314       $num_months = get_var('num_months',array('GET','POST'));
2315       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
2316       $this->t->set_var('num_months',$num_months);
2317       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
2318       else {  $this->t->set_var('lang_num_months','Months of History'); }
2319       $this->t->set_var('lang_filter','Filter');
2320       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2321             
2322       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2323       $this->t->set_var('ppi_link_title','Yearly PPIs'); 
2324
2325       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
2326       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2327       
2328       $this->t->set_var('title','Yearly PPIs');
2329       $num_months = get_var('num_months',array('GET','POST'));
2330       if($num_months == '') { $num_months = $this->default_ppi_num_years; }
2331       $this->t->set_var('num_months',$num_months);
2332       if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
2333       else { $this->t->set_var('lang_num_months','Years of History'); } 
2334
2335       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
2336       $this->db->query($sql,__LINE__,__FILE__);
2337       if($this->db->next_record()) {
2338         $president_name = $this->db->f('name');
2339         $interviewer = $this->db->f('elder');
2340         $eqpresppi = 1;
2341       } else {
2342         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
2343         return;
2344       }
2345       $this->t->set_var('district_number','*');
2346       $this->t->set_var('district_name',$president_name);
2347
2348       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2349       $this->db->query($sql,__LINE__,__FILE__);
2350       $i=0;
2351       while ($this->db->next_record())
2352         {
2353           $elder_id[$i] = $this->db->f('elder');
2354           $elder_name[$i] = $this->db->f('name');
2355           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2356           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
2357           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
2358           $i++;
2359         }
2360       $total_elders=$i;
2361       array_multisort($elder_name, $elder_id);
2362       //var_dump($elder_name); print "<br><br>"; var_dump($elder_id);
2363       
2364       $header_row="<th width=$comp_width><font size=-2>Elder Name</th>";
2365         
2366       $elder_width=400; $ppi_width=75; $table_width=$elder_width + $num_months*$ppi_width;
2367       $table_data="";
2368       for($m=$num_months; $m >= 0; $m--) {
2369         $year = date('Y') - $m;
2370         $header_row .= "<th width=150><font size=-2>$year</th>"; 
2371         $ppis[$m] = 0;
2372       }
2373       
2374       for ($j=0; $j < count($elder_id); $j++) {
2375         $id = $elder_id[$j];
2376         $name = $elder_name[$j];
2377         $phone = $elder_phone[$id];
2378         
2379         $link_data['menuaction'] = 'eq.eq.ppi_update';
2380         $link_data['interviewer'] = $interviewer;
2381         $link_data['elder'] = $id;
2382         $link_data['name'] = $name;
2383         $link_data['ppi'] = '';
2384         $link_data['eqpresppi'] = $eqpresppi;
2385         $link_data['action'] = 'add';
2386         $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2387         $this->nextmatchs->template_alternate_row_color(&$this->t);
2388         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2389
2390         // Find out how many times PPIs were performed in the past $num_months for this Elder
2391         for($m=$num_months; $m >= 0; $m--) {
2392           $year = date('Y') - $m;
2393           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2394           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
2395              "AND elder=" . $id . " AND eqpresppi=1";
2396           $this->db2->query($sql,__LINE__,__FILE__);
2397                   
2398           if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
2399           if($this->db2->next_record()) {
2400             $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
2401             $link_data['menuaction'] = 'eq.eq.ppi_update';
2402             $link_data['companionship'] = $companionship;
2403             $link_data['interviewer'] = $this->db2->f('interviewer');
2404             $link_data['elder'] = $id;
2405             $link_data['name'] = $name;
2406             $link_data['ppi'] = $this->db2->f('ppi');
2407             $link_data['eqpresppi'] = $eqpresppi;
2408             $link_data['action'] = 'view';
2409             $date = $this->db2->f('date');
2410             $date_array = explode("-",$date);
2411             $month = $date_array[1];
2412             $day   = $date_array[2];
2413             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2414             $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2415           }
2416           else { $table_data .= "<td>&nbsp;</td>"; }
2417         }
2418         $table_data .= "</tr>\n"; 
2419       }
2420       $table_data .= "<tr><td colspan=20><hr></td></tr>";
2421       
2422       $stat_data = "<tr><td><b><font size=-2>$total_elders Elders<br>PPI Totals:</font></b></td>";
2423       for($m=$num_months; $m >=0; $m--) {
2424         $percent = ceil(($ppis[$m] / $total_elders)*100);
2425         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
2426       }
2427       $stat_data .= "</tr>";
2428       
2429       $this->t->set_var('table_width',$table_width);
2430       $this->t->set_var('header_row',$header_row);
2431       $this->t->set_var('table_data',$table_data);
2432       $this->t->set_var('stat_data',$stat_data);
2433       $this->t->pfp('out','ppi_view_t');
2434       $this->save_sessiondata(); 
2435     }
2436
2437   function ppi_update()
2438     {
2439       $this->t->set_file(array('form' => 'ppi_update.tpl'));
2440       $this->t->set_block('form','interviewer_list','int_list');
2441       $this->t->set_block('form','add','addhandle');
2442       $this->t->set_block('form','edit','edithandle');
2443       
2444       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2445       $this->t->set_var('readonly','');
2446       $this->t->set_var('disabled','');
2447       
2448       $action = get_var('action',array('GET','POST'));
2449       $companionship = get_var('companionship',array('GET','POST'));
2450       $interviewer = get_var('interviewer',array('GET','POST'));      
2451       $name = get_var('name',array('GET','POST'));
2452       $ppi = get_var('ppi',array('GET','POST'));
2453       $elder = get_var('elder',array('GET','POST'));
2454       $aaronic = get_var('aaronic',array('GET','POST'));
2455       $date = get_var('date',array('GET','POST'));
2456       $notes = get_var('notes',array('GET','POST'));
2457       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
2458       
2459       $sql = "SELECT * FROM eq_elder WHERE elder=" . $interviewer;
2460       $this->db2->query($sql,__LINE__,__FILE__);
2461       $this->db2->next_record();
2462       $interviewer_name = $this->db2->f('name');
2463       $this->t->set_var('interviewer',$interviewer . ' selected');
2464       $this->t->set_var('interviewer_name',$interviewer_name);
2465       $this->t->set_var('eqpresppi_checked','');
2466       $this->t->fp('int_list','interviewer_list',True);
2467     
2468       if($action == 'save')
2469         {
2470           $notes = get_var('notes',array('POST'));
2471           $this->db->query("UPDATE eq_ppi set " .
2472                            "   ppi='" . $ppi . "'" .
2473                     ", interviewer='" . $interviewer . "'" .
2474                           ", elder='" . $elder . "'" .
2475                         ", aaronic='" . $aaronic . "'" .
2476                            ", date='" . $date . "'" .
2477                           ", notes='" . $notes . "'" .
2478                       ", eqpresppi='" . $eqpresppi . "'" .
2479                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2480           $this->ppi_view();
2481           return false;
2482         }
2483
2484       if($action == 'insert')
2485         {
2486           $notes = get_var('notes',array('POST'));
2487           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
2488                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2489                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
2490           $this->ppi_view();
2491           return false;
2492         }
2493       
2494       if($action == 'add')
2495         {
2496           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2497           $this->t->set_var('ppi', '');
2498           $this->t->set_var('interviewer', $interviewer);
2499           $this->t->set_var('name',$name);
2500           $this->t->set_var('elder',$elder);
2501           $this->t->set_var('date','');
2502           $this->t->set_var('notes','');
2503           $this->t->set_var('eqpresppi',$eqpresppi);
2504           $this->t->set_var('eqpresppi_checked','checked');
2505           $this->t->set_var('lang_done','Cancel');
2506           $this->t->set_var('lang_action','Adding New PPI');
2507           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2508                                                                 . $ppi . '&action=' . 'insert'));
2509         }
2510
2511       if($action == 'edit' || $action == 'view')
2512         {
2513           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2514           $this->db->query($sql,__LINE__,__FILE__);
2515           $this->db->next_record();
2516           $this->t->set_var('ppi',$ppi);
2517           $this->t->set_var('name',$name);
2518           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2519           $this->t->set_var('elder',$this->db->f('elder'));
2520           $this->t->set_var('date',$this->db->f('date'));
2521           $this->t->set_var('notes',$this->db->f('notes'));
2522           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
2523           if($this->db->f('eqpresppi') == 1) { $this->t->set_var('eqpresppi_checked','checked'); }
2524         }
2525       
2526       if($action == 'edit')
2527         {
2528           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2529           $this->t->set_var('lang_done','Cancel');
2530           $this->t->set_var('lang_action','Editing PPI');
2531           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2532                                                                 . $ppi . '&action=' . 'save'));
2533         }
2534
2535       if($action == 'view')
2536         {
2537           $date = $this->db->f('date');
2538           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2539           $this->t->set_var('readonly','READONLY');
2540           $this->t->set_var('disabled','DISABLED');
2541           $this->t->set_var('lang_done','Done');
2542           $this->t->set_var('lang_action','Viewing PPI');
2543           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2544                                                                 . $ppi . '&action=' . 'edit'));
2545         }
2546       
2547       $this->t->set_var('lang_reset','Clear Form');
2548       $this->t->set_var('lang_add','Add PPI');
2549       $this->t->set_var('lang_save','Save Changes');
2550       $this->t->set_var('edithandle','');
2551       $this->t->set_var('addhandle','');
2552
2553       $this->t->pfp('out','form');
2554       
2555       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2556       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2557       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2558      
2559       $this->save_sessiondata(); 
2560     }
2561
2562     function int_view()
2563     {
2564       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2565       $this->t->set_block('int_view_t','district_list','list');
2566
2567       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2568       $num_quarters = get_var('num_quarters',array('GET','POST'));
2569       if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2570       $this->t->set_var('num_quarters',$num_quarters);
2571       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2572       else {  $this->t->set_var('lang_num_quarters','Quarters of History'); }
2573       $this->t->set_var('lang_filter','Filter');
2574       
2575       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2576       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2577       
2578       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2579       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2580       
2581       $this->t->set_var('title','Hometeaching Interviews'); 
2582
2583       $num_months = $num_quarters * 3 - 1;
2584       $current_month = $this->current_month;
2585       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2586       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2587       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2588       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2589
2590       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2591       $this->db->query($sql,__LINE__,__FILE__);
2592       $i=0;
2593       while ($this->db->next_record())
2594         {
2595           $districts[$i]['district'] = $this->db->f('district');
2596           $districts[$i]['name'] = $this->db->f('name');
2597           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2598           $i++;
2599         }
2600
2601       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2602       $this->db->query($sql,__LINE__,__FILE__);
2603       $i=0;
2604       while ($this->db->next_record())
2605         {
2606           $elder_id[$i] = $this->db->f('elder');
2607           $elder_name[$i] = $this->db->f('name');
2608           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2609           $i++;
2610         }
2611       array_multisort($elder_name, $elder_id);
2612       for($i=0; $i < count($elder_id); $i++) {
2613           $id = $elder_id[$i];
2614           $elders[$id] = $elder_name[$i];
2615       }      
2616
2617       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2618       $this->db->query($sql,__LINE__,__FILE__);
2619       while ($this->db->next_record())
2620         {
2621           $aaronic_id = $this->db->f('aaronic');
2622           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2623           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2624         }
2625       
2626       $total_companionships = 0;
2627       $this->nextmatchs->template_alternate_row_color(&$this->t);
2628       for ($i=0; $i < count($districts); $i++) {
2629         $this->t->set_var('district_number',$districts[$i]['district']);
2630         $this->t->set_var('district_name',$districts[$i]['name']);      
2631         $supervisor = $districts[$i]['supervisor'];
2632         $unique_companionships='';
2633                 
2634         // Select all the unique companionship numbers for this district
2635         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2636
2637         $this->db->query($sql,__LINE__,__FILE__);
2638         $j=0;
2639         while ($this->db->next_record())
2640           {
2641             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2642             $j++;
2643           }
2644         
2645         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2646         $table_data=""; $num_companionships = $j; $num_elders = 0;
2647         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2648         for ($j=0; $j < count($unique_companionships); $j++) {
2649           // Select all the companions in each companionship
2650           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2651              "companionship=". $unique_companionships[$j]['companionship'];
2652           $this->db->query($sql,__LINE__,__FILE__);
2653           $k=0;
2654           $comp = $unique_companionships[$j]['companionship'];
2655           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2656           while ($this->db->next_record())
2657             {
2658               // Get this companions information
2659               $num_elders++;
2660               $companionship = $this->db->f('companionship');
2661               $elder_id = $this->db->f('elder');
2662               $aaronic_id = $this->db->f('aaronic');
2663               if($elder_id) {
2664                 $name = $elders[$elder_id];
2665                 $phone = $elder_phone[$elder_id];
2666               }
2667               else if($aaronic_id) {
2668                 $name = $aaronic[$aaronic_id]['name'];
2669                 $phone = $aaronic[$aaronic_id]['phone'];
2670               }
2671               $link_data['menuaction'] = 'eq.eq.int_update';
2672               $link_data['companionship'] = $companionship;
2673               $link_data['interviewer'] = $supervisor;
2674               $link_data['elder'] = $elder_id;
2675               $link_data['aaronic'] = $aaronic_id;
2676               $link_data['name'] = $name;
2677               $link_data['ppi'] = '';
2678               $link_data['action'] = 'add';
2679               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2680               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2681
2682               // Find out how many times Interviews were performed in the past $num_months for this Elder
2683               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2684               for($m=$num_months; $m >= 0; $m--) {
2685                 $month = $current_month - $m;
2686                 $year = $this->current_year;
2687                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2688                 if($month < 10) { $month = "0"."$month"; }
2689                 $month_start = "$year"."-"."$month"."-"."01";
2690                 $month_end = "$year"."-"."$month"."-"."31";
2691                 $month = "$month"."/"."$year";
2692                 $sql = "SELECT * FROM eq_ppi WHERE date >= '$month_start' AND date <= '$month_end' ".
2693                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2694                 $this->db2->query($sql,__LINE__,__FILE__);
2695                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2696               
2697                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2698                 if($this->db2->next_record()) {
2699                   if(!$int_recorded[$companionship][$m]) {
2700                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2701                   }
2702                   $link_data['menuaction'] = 'eq.eq.int_update';
2703                   $link_data['companionship'] = $companionship;
2704                   $link_data['interviewer'] = $this->db2->f('interviewer');
2705                   $link_data['elder'] = $elder_id;
2706                   $link_data['aaronic'] = $aaronic_id;
2707                   $link_data['name'] = $name;
2708                   $link_data['ppi'] = $this->db2->f('ppi');
2709                   $link_data['action'] = 'view';
2710                   $date = $this->db2->f('date');
2711                   $date_array = explode("-",$date);
2712                   $month = $date_array[1];
2713                   $day   = $date_array[2];
2714                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2715                   $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2716                 }
2717                 else { $table_data .= "<td>&nbsp;</td>"; }
2718               }
2719               $table_data .= "</tr>"; 
2720               $k++;
2721             }
2722           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2723         }
2724         $total_companionships += $num_companionships;
2725         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Totals:</font></b></td>";
2726
2727         // Print the hometeaching interview stats
2728         for($m=$num_months; $m >=0; $m--) {
2729           $month = $current_month - $m;
2730           if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2731           $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2732           //print "$month % $this->monthly_hometeaching_interview_stats = $month_begins <br>";
2733           if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2734           if(($month_begins) == 1) { $total = $ints[$m]; }
2735           else { $total += $ints[$m]; }
2736           $percent = ceil(($total / $num_companionships)*100);
2737           $stat_data .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2738         }
2739         $stat_data .= "</tr>";
2740         
2741         $this->t->set_var('table_width',$table_width);
2742         $this->t->set_var('header_row',$header_row);
2743         $this->t->set_var('table_data',$table_data);
2744         $this->t->set_var('stat_data',$stat_data);
2745         $this->t->fp('list','district_list',True);
2746       }
2747
2748       // Display the totals
2749       $total = 0;
2750       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Totals:</font></b></td>";
2751       for($m=$num_months; $m >=0; $m--) {
2752         $month = $current_month - $m;
2753         if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2754         $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2755         if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2756         if(($month_begins) == 1) { $total = $total_ints[$m]; }
2757         else { $total += $total_ints[$m]; }
2758         $percent = ceil(($total / $total_companionships)*100);
2759         $totals .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2760       }
2761       $totals .= "</tr>";
2762           
2763       $this->t->set_var('totals',$totals);
2764       $this->t->pfp('out','int_view_t');
2765       $this->save_sessiondata(); 
2766     }
2767
2768   function int_update()
2769     {
2770       $this->t->set_file(array('form' => 'int_update.tpl'));
2771       $this->t->set_block('form','interviewer_list','int_list');
2772       $this->t->set_block('form','add','addhandle');
2773       $this->t->set_block('form','edit','edithandle');
2774       
2775       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2776       $this->t->set_var('readonly','');
2777       $this->t->set_var('disabled','');
2778       $this->t->set_var('eqpresppi_checked','');
2779       
2780       $action = get_var('action',array('GET','POST'));
2781       $companionship = get_var('companionship',array('GET','POST'));
2782       $interviewer = get_var('interviewer',array('GET','POST'));      
2783       $name = get_var('name',array('GET','POST'));
2784       $ppi = get_var('ppi',array('GET','POST'));
2785       $elder = get_var('elder',array('GET','POST'));
2786       $aaronic = get_var('aaronic',array('GET','POST'));
2787       $date = get_var('date',array('GET','POST'));
2788       $notes = get_var('notes',array('GET','POST'));
2789       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
2790       
2791       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2792       $this->db->query($sql,__LINE__,__FILE__);
2793       while ($this->db->next_record())
2794         {
2795           $supervisor = $this->db->f('supervisor');
2796           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2797           $this->db2->query($sql,__LINE__,__FILE__);
2798           $this->db2->next_record();
2799           $interviewer_name = $this->db2->f('name');
2800           
2801           if($supervisor == $interviewer) { 
2802             $this->t->set_var('interviewer',$supervisor . ' selected');
2803           } else {
2804             $this->t->set_var('interviewer',$interviewer);
2805           }
2806           $this->t->set_var('interviewer_name',$interviewer_name);
2807           $this->t->fp('int_list','interviewer_list',True);
2808         }
2809       
2810       if($action == 'save')
2811         {
2812           $notes = get_var('notes',array('POST'));
2813           $this->db->query("UPDATE eq_ppi set " .
2814                            "   ppi='" . $ppi . "'" .
2815                     ", interviewer='" . $interviewer . "'" .
2816                           ", elder='" . $elder . "'" .
2817                         ", aaronic='" . $aaronic . "'" .
2818                            ", date='" . $date . "'" .
2819                           ", notes='" . $notes . "'" .
2820                       ", eqpresppi='" . $eqpresppi . "'" .
2821                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2822           $this->int_view();
2823           return false;
2824         }
2825
2826       if($action == 'insert')
2827         {
2828           $notes = get_var('notes',array('POST'));
2829           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
2830                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2831                            . $date . "','" . $notes ."','" . $eqpresppi . "')",__LINE__,__FILE__);
2832           $this->int_view();
2833           return false;
2834         }
2835       
2836       if($action == 'add')
2837         {
2838           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2839           $this->t->set_var('ppi', '');
2840           $this->t->set_var('interviewer', $interviewer);
2841           $this->t->set_var('name',$name);
2842           $this->t->set_var('elder',$elder);
2843           $this->t->set_var('aaronic',$aaronic);
2844           $this->t->set_var('date','');
2845           $this->t->set_var('notes','');
2846           $this->t->set_var('lang_done','Cancel');
2847           $this->t->set_var('lang_action','Adding New Interview');
2848           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2849                                                                 . $ppi . '&action=' . 'insert'));
2850         }
2851
2852       if($action == 'edit' || $action == 'view')
2853         {
2854           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2855           $this->db->query($sql,__LINE__,__FILE__);
2856           $this->db->next_record();
2857           $this->t->set_var('ppi',$ppi);
2858           $this->t->set_var('name',$name);
2859           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2860           $this->t->set_var('elder',$this->db->f('elder'));
2861           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2862           $this->t->set_var('date',$this->db->f('date'));
2863           $this->t->set_var('notes',$this->db->f('notes'));
2864           if($this->db->f('eqpresppi') == 1) { $this->t->set_var('eqpresppi_checked','checked'); }
2865         }
2866       
2867       if($action == 'edit')
2868         {
2869           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2870           $this->t->set_var('lang_done','Cancel');
2871           $this->t->set_var('lang_action','Editing Interview');
2872           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2873                                                                 . $ppi . '&action=' . 'save'));
2874         }
2875
2876       if($action == 'view')
2877         {
2878           $date = $this->db->f('date');
2879           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2880           $this->t->set_var('readonly','READONLY');
2881           $this->t->set_var('disabled','DISABLED');
2882           $this->t->set_var('lang_done','Done');
2883           $this->t->set_var('lang_action','Viewing Interview');
2884           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2885                                                                 . $ppi . '&action=' . 'edit'));
2886         }
2887       
2888       $this->t->set_var('lang_reset','Clear Form');
2889       $this->t->set_var('lang_add','Add Interview');
2890       $this->t->set_var('lang_save','Save Changes');
2891       $this->t->set_var('edithandle','');
2892       $this->t->set_var('addhandle','');
2893
2894       $this->t->pfp('out','form');
2895       
2896       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2897       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2898       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2899      
2900       $this->save_sessiondata(); 
2901     }
2902
2903   function vis_view()
2904     {
2905       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2906       $this->t->set_block('vis_view_t','visit_list','list1');
2907       $this->t->set_block('vis_view_t','family_list','list2');
2908
2909       $this->t->set_var('lang_name','Family Name');
2910       $this->t->set_var('lang_date','Date');
2911
2912       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2913       $this->t->set_var('vis_link_title','View Yearly Visits');
2914       
2915       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2916       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2917
2918       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2919       $num_years = get_var('num_years',array('GET','POST'));
2920       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2921       $this->t->set_var('num_years',$num_years);
2922       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2923       else {  $this->t->set_var('lang_num_years','Years of History'); }
2924       $this->t->set_var('lang_filter','Filter');
2925       
2926       $year = date('Y') - $num_years + 1;
2927       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2928       
2929       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2930       $this->db->query($sql,__LINE__,__FILE__);
2931       $total_records = $this->db->num_rows();
2932
2933       $i = 0;
2934       while ($this->db->next_record())
2935         {
2936           $visit_list[$i]['visit'] = $this->db->f('visit');
2937           $visit_list[$i]['family'] = $this->db->f('family');
2938           $visit_list[$i]['date']  = $this->db->f('date');
2939           $i++;
2940         }
2941             
2942       for ($i=0; $i < count($visit_list); $i++)
2943         {         
2944           $this->nextmatchs->template_alternate_row_color(&$this->t);
2945
2946           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2947           $this->db->query($sql,__LINE__,__FILE__);
2948           $this->db->next_record();
2949                   
2950           $this->t->set_var('family',$visit_list[$i]['family']);
2951           $this->t->set_var('family_name',$this->db->f('name'));
2952           $this->t->set_var('date',$visit_list[$i]['date']);
2953           
2954           $link_data['menuaction'] = 'eq.eq.vis_update';
2955           $link_data['visit'] = $visit_list[$i]['visit'];
2956           $link_data['name'] = $this->db->f('name');
2957           $link_data['date'] = $visit_list[$i]['date'];
2958           $link_data['action'] = 'view';
2959           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2960           $this->t->set_var('lang_view','View');
2961
2962           $link_data['menuaction'] = 'eq.eq.vis_update';
2963           $link_data['visit'] = $visit_list[$i]['visit'];
2964           $link_data['name'] = $this->db->f('name');
2965           $link_data['date'] = $visit_list[$i]['date'];
2966           $link_data['action'] = 'edit';
2967           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2968           $this->t->set_var('lang_edit','Edit');
2969
2970           $this->t->fp('list1','visit_list',True);
2971         }
2972
2973       // List the families that are available to record a visit against
2974       $sql = "SELECT * FROM eq_family WHERE valid=1";
2975       $this->db->query($sql,__LINE__,__FILE__);
2976       $total_records = $this->db->num_rows();
2977
2978       $i = 0;
2979       while ($this->db->next_record())
2980         {
2981           $family_names[$i] = $this->db->f('name');
2982           $family_ids[$i] = $this->db->f('family');
2983           $i++;
2984         } array_multisort($family_names, $family_ids);
2985       
2986       for ($i=0; $i < count($family_names); $i++)
2987         {
2988           $link_data['menuaction'] = 'eq.eq.vis_update';
2989           $link_data['visit'] = '';
2990           $link_data['family'] = $family_ids[$i];
2991           $link_data['action'] = 'add';
2992           $link_data['name'] = $family_names[$i];
2993           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2994
2995           $this->t->set_var('name',$family_names[$i]);
2996           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2997           else { $this->t->set_var('table_sep',"</td>"); }
2998           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2999
3000           $this->t->fp('list2','family_list',True);
3001         }   
3002
3003       $this->t->pfp('out','vis_view_t');
3004       $this->save_sessiondata(); 
3005     }
3006
3007   function vis_update()
3008     {
3009       $this->t->set_file(array('form' => 'vis_update.tpl'));
3010       $this->t->set_block('form','add','addhandle');
3011       $this->t->set_block('form','edit','edithandle');
3012       
3013       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
3014       $this->t->set_var('readonly','');
3015       $this->t->set_var('disabled','');
3016       
3017       $action = get_var('action',array('GET','POST'));
3018       $visit = get_var('visit',array('GET','POST'));
3019       $family = get_var('family',array('GET','POST'));
3020       $name = get_var('name',array('GET','POST'));
3021       $date = get_var('date',array('GET','POST'));
3022       $notes = get_var('notes',array('GET','POST'));
3023       $companionship = 0;
3024       
3025       if($action == 'save')
3026         {
3027           $notes = get_var('notes',array('POST'));
3028           $this->db->query("UPDATE eq_visit set " .
3029                            "  date='" . $date . "'" .
3030                           ", notes='" . $notes . "'" .
3031                            " WHERE visit=" . $visit,__LINE__,__FILE__);
3032           $this->vis_view();
3033           return false;
3034         }
3035
3036       if($action == 'insert')
3037         {
3038           $notes = get_var('notes',array('POST'));
3039           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
3040                            . "VALUES ('" . $family . "','" . $companionship . "','"
3041                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
3042           $this->vis_view();
3043           return false;
3044         }
3045       
3046       if($action == 'add')
3047         {
3048           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3049           $this->t->set_var('family', $family);
3050           $this->t->set_var('visit', '');
3051           $this->t->set_var('name', $name);
3052           $this->t->set_var('date','');
3053           $this->t->set_var('notes','');
3054           $this->t->set_var('lang_done','Cancel');
3055           $this->t->set_var('lang_action','Adding New Visit');
3056           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
3057                                                                 . $family . '&action=' . 'insert'));
3058         }
3059
3060       if($action == 'edit' || $action == 'view')
3061         {
3062           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
3063           $this->db->query($sql,__LINE__,__FILE__);
3064           $this->db->next_record();
3065           $this->t->set_var('visit',$visit);
3066           $this->t->set_var('name',$name);
3067           $this->t->set_var('family', $family);
3068           $this->t->set_var('date',$this->db->f('date'));
3069           $this->t->set_var('notes',$this->db->f('notes'));
3070         }
3071       
3072       if($action == 'edit')
3073         {
3074           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3075           $this->t->set_var('lang_done','Cancel');
3076           $this->t->set_var('lang_action','Editing Visit');
3077           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3078                                                                 . $visit . '&action=' . 'save'));
3079         }
3080
3081       if($action == 'view')
3082         {
3083           $date = $this->db->f('date');
3084           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3085           $this->t->set_var('readonly','READONLY');
3086           $this->t->set_var('disabled','DISABLED');
3087           $this->t->set_var('lang_done','Done');
3088           $this->t->set_var('lang_action','Viewing Visit');
3089           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3090                                                                 . $visit . '&action=' . 'edit'));
3091         }
3092       
3093       $this->t->set_var('lang_reset','Clear Form');
3094       $this->t->set_var('lang_add','Add Visit');
3095       $this->t->set_var('lang_save','Save Changes');
3096       $this->t->set_var('edithandle','');
3097       $this->t->set_var('addhandle','');
3098
3099       $this->t->pfp('out','form');
3100       
3101       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3102       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3103       if($action == 'add') { $this->t->pfp('addhandle','add'); }
3104
3105       $this->save_sessiondata(); 
3106     }
3107
3108   function att_view()
3109     {
3110       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3111       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3112       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3113
3114       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3115       $this->t->set_block('att_view_t','act_list','list');
3116
3117       $this->t->set_block('att_view_t','month_list','list1');
3118       $this->t->set_block('att_view_t','header_list','list2');
3119       $this->t->set_block('att_view_t','elder_list','list3');
3120       
3121       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3122       $num_quarters = get_var('num_quarters',array('GET','POST'));
3123       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3124       $this->t->set_var('num_quarters',$num_quarters);
3125       $this->t->set_var('lang_filter','Filter');
3126       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
3127       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
3128
3129       $num_months = $num_quarters * 3;
3130       $current_month = $this->current_month;
3131       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3132       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3133       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3134       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3135       
3136       $sql = "SELECT * FROM eq_elder where valid=1";
3137       $this->db->query($sql,__LINE__,__FILE__);
3138       $i=0;
3139       while ($this->db->next_record())
3140         {
3141           $elder_name[$i] = $this->db->f('name');
3142           $elder_id[$i] = $this->db->f('elder');
3143           $i++;
3144         }
3145       array_multisort($elder_name, $elder_id);
3146       
3147       // Create a list of sunday dates for a window of 3 months back and current month
3148       $i=0; 
3149       $last_time = 0; 
3150       $found_sunday = 0;
3151       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
3152       $last_date = explode("-",$sunday_list[0]['date']);
3153       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3154       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
3155       while($last_time < $time_limit)
3156       {
3157         $day = date("w",$last_time);
3158         if(date("w",$last_time) == 0) {
3159           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3160           $last_date = explode("-",$sunday_list[$i]['date']);
3161           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3162           $sunday_list[$i]['day'] = $last_date[2];
3163           $sunday_list[$i]['month'] = date("M",$last_time);
3164           $sunday_list[$i]['year'] = $last_date[0];
3165           $found_sunday = 1; 
3166         }
3167         $last_time += 90000;
3168         if($found_sunday) { $i++; $found_sunday=0; }
3169       }
3170
3171       $total_elders = count($elder_id);
3172       $old_month=$sunday_list[0]['month']; $span=0;
3173       for ($i=0; $i < count($sunday_list); $i++) {
3174         $date = $sunday_list[$i]['date'];
3175         $this->t->set_var('date',$sunday_list[$i]['date']);
3176         $this->t->set_var('day',$sunday_list[$i]['day']);
3177         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3178           if($i == count($sunday_list)-1) { $span++; }
3179           $cur_month = $sunday_list[$i]['month'];
3180           $old_month = $sunday_list[$i]['month'];         
3181           $link_data['menuaction'] = 'eq.eq.att_update';
3182           $link_data['month'] = $sunday_list[$i-1]['month'];
3183           $link_data['year'] = $sunday_list[$i-1]['year'];
3184           $link_data['action'] = 'update_month';
3185           $cur_month = $sunday_list[$i-1]['month'];
3186           $cur_year = $sunday_list[$i-1]['year'];
3187           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3188           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3189           $this->t->set_var('month',$sunday_list[$i-1]['month']);
3190           $this->t->set_var('year',$sunday_list[$i-1]['year']);
3191           $this->t->set_var('span',$span); $span=0;
3192           $this->t->fp('list1','month_list',True);
3193         } $span++;
3194       }
3195       $this->t->set_var('total_elders',$total_elders);
3196       $this->t->set_var('header_row',$header_row);
3197       
3198       $elder_width=200; $att_width=25; $total_width=$elder_width; 
3199       for ($i=0; $i < count($sunday_list); $i++) {
3200         $link_data['menuaction'] = 'eq.eq.att_update';
3201         $link_data['month'] = $sunday_list[$i]['month'];
3202         $link_data['year'] = $sunday_list[$i]['year'];
3203         $link_data['day'] = $sunday_list[$i]['day'];
3204         $link_data['date'] = $sunday_list[$i]['date'];
3205         $link_data['action'] = 'update_day';
3206         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3207         $this->t->set_var('date',$sunday_list[$i]['date']);
3208         $this->t->set_var('day',$sunday_list[$i]['day']);
3209         $this->t->set_var('month',$sunday_list[$i]['month']);
3210         $this->t->set_var('year',$sunday_list[$i]['year']);
3211         $this->t->fp('list2','header_list',True);
3212         $total_width += $att_width;
3213         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3214       }
3215
3216       for ($i=0; $i < count($elder_id); $i++) {
3217         $att_table = "";
3218         $this->nextmatchs->template_alternate_row_color(&$this->t);
3219         $this->t->set_var('elder_name',$elder_name[$i]);
3220         #print "checking for elder: " . $elder_id[$i] . "<br>";
3221         for ($j=0; $j < count($sunday_list); $j++) {
3222           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3223           #print "SELECT * FROM eq_attendance WHERE date='"
3224           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
3225           $sql = "SELECT * FROM eq_attendance WHERE date='"
3226              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3227           $this->db->query($sql,__LINE__,__FILE__);
3228           if($this->db->next_record()) {
3229             $cur_month = $sunday_list[$j]['month'];
3230             if($attended[$i][$cur_month] != 1) { 
3231               $attended[$i][$cur_month]=1;
3232               $attendance[$monthnum[$cur_month]]++;
3233             } 
3234             $att_table .= '<td align=center><img src="images/checkmark.gif"></td>';
3235           } else {
3236             $att_table .= '<td>&nbsp;</td>';
3237           }
3238         }
3239         $this->t->set_var('att_table',$att_table);
3240         $this->t->fp('list3','elder_list',True);
3241       }
3242       $this->t->set_var('total_width',$total_width);
3243       $this->t->set_var('elder_width',$elder_width);
3244       $this->t->set_var('att_width',$att_width);
3245
3246       # Now calculate attendance for these months
3247       $attendance_str = "";
3248       $nonattendance_str = "";
3249       $aveattendance_str = "";
3250       $avenonattendance_str = "";
3251       $num_months=0;
3252       $ave_total_attended=0;
3253       ksort($attendance);
3254       foreach($attendance as $att => $value) {
3255         $total_attended = $attendance[$att];
3256         $ave_total_attended += $attendance[$att]; $num_months++;
3257         $percent = ceil(($total_attended / $total_elders)*100);
3258         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3259         $total_nonattended = $total_elders - $total_attended;
3260         $percent = ceil(($total_nonattended / $total_elders)*100);
3261         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3262         
3263         $total_attended = ceil(($ave_total_attended / $num_months));
3264         $percent = ceil(($total_attended / $total_elders)*100);
3265         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3266         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
3267         $percent = ceil(($total_attended / $total_elders)*100);
3268         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3269       }
3270       
3271       $this->t->set_var('attendance',$attendance_str);
3272       $this->t->set_var('aveattendance',$aveattendance_str);
3273       $this->t->set_var('nonattendance',$nonattendance_str);
3274       $this->t->set_var('avenonattendance',$avenonattendance_str);
3275       
3276       $this->t->pfp('out','att_view_t');
3277       $this->save_sessiondata(); 
3278     }
3279
3280   function att_update()
3281     {
3282       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3283       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3284       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3285
3286       $this->t->set_file(array('form' => 'att_update.tpl'));
3287       $this->t->set_block('form','edit','edithandle');
3288       
3289       $this->t->set_block('form','month_list','list1');
3290       $this->t->set_block('form','header_list','list2');
3291       $this->t->set_block('form','elder_list','list3');
3292
3293       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3294
3295       $action = get_var('action',array('GET','POST'));
3296       $month = get_var('month',array('GET','POST'));
3297       $year = get_var('year',array('GET','POST'));
3298       $day = get_var('day',array('GET','POST'));
3299       $date = get_var('date',array('GET','POST'));
3300
3301       if($action == 'save_month' || $action == 'save_day')
3302         {
3303            $new_data = get_var('elders_attended',array('POST'));
3304            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3305
3306            if($action == 'save_month') {        
3307              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3308            }
3309
3310            if($action == 'save_day') {        
3311              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3312            }   
3313
3314            foreach ($new_data as $data)
3315            {
3316               $data_array = explode("-",$data);
3317               $elder = $data_array[0];
3318               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
3319               $this->db->query("INSERT INTO eq_attendance (elder,date) "
3320                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
3321            }
3322         
3323          $this->att_view();
3324          return false;    
3325         }
3326
3327       $sql = "SELECT * FROM eq_elder where valid=1";
3328       $this->db->query($sql,__LINE__,__FILE__);
3329       $i=0;
3330       while ($this->db->next_record())
3331         {
3332           $elder_name[$i] = $this->db->f('name');
3333           $elder_id[$i] = $this->db->f('elder');
3334           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
3335           $i++;
3336         }
3337       array_multisort($elder_name, $elder_id);
3338       
3339       if($action == 'update_month')
3340       {
3341         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
3342         $i=0; 
3343         $last_time = 0; 
3344         $found_sunday = 0;
3345         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3346         $last_date = explode("-",$sunday_list[0]['date']);
3347         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3348         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3349         while($last_time <= $time_limit)
3350         {
3351           $day = date("w",$last_time);
3352           if(date("w",$last_time) == 0) { 
3353             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3354             $last_date = explode("-",$sunday_list[$i]['date']);
3355             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3356             $sunday_list[$i]['day'] = $last_date[2];
3357             $sunday_list[$i]['month'] = date("M",$last_time);
3358             $sunday_list[$i]['year'] = $last_date[0];
3359             $found_sunday = 1; 
3360           }
3361           $last_time += 90000;
3362           if($found_sunday) { $i++; $found_sunday=0; }
3363         }
3364
3365         $this->t->set_var('span', $i);
3366         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3367         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3368         $this->t->fp('list1','month_list',True);
3369         $elder_width=200; $att_width=25; $total_width=$elder_width;
3370         for ($i=0; $i < count($sunday_list); $i++) {
3371           $link_data['menuaction'] = 'eq.eq.att_update';
3372           $link_data['month'] = $sunday_list[$i]['month'];
3373           $link_data['year'] = $sunday_list[$i]['year'];
3374           $link_data['day'] = $sunday_list[$i]['day'];
3375           $link_data['date'] = $sunday_list[$i]['date'];
3376           $link_data['action'] = 'update_day';
3377           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3378           $this->t->set_var('date',$sunday_list[$i]['date']);
3379           $this->t->set_var('day',$sunday_list[$i]['day']);
3380           $this->t->set_var('month',$sunday_list[$i]['month']);
3381           $this->t->set_var('year',$sunday_list[$i]['year']);
3382           $this->t->fp('list2','header_list',True);
3383           $total_width += $att_width;
3384         }     
3385       }
3386
3387       if($action == 'update_day')
3388       {
3389         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
3390         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3391         $this->t->set_var('month',$month);
3392         $this->t->set_var('year',$year);
3393         $this->t->fp('list1','month_list',True);
3394         $this->t->set_var('date',$date);
3395         $this->t->set_var('day',$day);
3396         $this->t->set_var('month',$month);
3397         $this->t->set_var('year',$year);
3398         $this->t->fp('list2','header_list',True);
3399       }           
3400             
3401       for ($i=0; $i < count($elder_id); $i++) {
3402         $att_table = "";
3403         $this->nextmatchs->template_alternate_row_color(&$this->t);
3404         $this->t->set_var('elder_name',$elder_name[$i]);
3405         for ($j=0; $j < count($sunday_list); $j++) {
3406           $sql = "SELECT * FROM eq_attendance WHERE date='"
3407              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3408           $this->db->query($sql,__LINE__,__FILE__);
3409           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
3410           if($this->db->next_record()) {
3411             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3412           } else if($elder_attending[$elder_id[$i]] == 1) {
3413             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3414           } else {
3415             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
3416           }
3417         }
3418         $this->t->set_var('att_table',$att_table);
3419         $this->t->fp('list3','elder_list',True);
3420       } 
3421            
3422       $this->t->set_var('lang_done', 'Cancel');
3423       $this->t->set_var('lang_reset','Clear Form');
3424       $this->t->set_var('lang_save','Save Changes');
3425
3426       $this->t->pfp('out','form');
3427       $this->t->pfp('addhandle','edit');
3428
3429       $this->save_sessiondata();       
3430     }
3431
3432   function dir_view()
3433     {
3434       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3435       $this->t->set_block('dir_view_t','dir_list','list');
3436       
3437       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
3438       $this->db->query($sql,__LINE__,__FILE__);
3439       $i=0;
3440       while ($this->db->next_record())
3441         {
3442           $parent[$i]['id'] = $this->db->f('parent');
3443           $parent[$i]['name'] = $this->db->f('name');
3444           $parent[$i]['phone'] = $this->db->f('phone');
3445           $parent[$i]['address'] = $this->db->f('address');
3446           $i++;
3447         }   
3448       
3449       for ($i=0; $i < count($parent); $i++) 
3450       {
3451         $name = $parent[$i]['name'];
3452         $phone = $parent[$i]['phone'];
3453         $address = $parent[$i]['address'];
3454         $this->t->set_var('name', $name);
3455         $this->t->set_var('address', $address);
3456         $this->t->set_var('phone', $phone);
3457         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3458         $this->t->set_var('tr_color',$tr_color);
3459         $this->t->fp('list','dir_list',True);
3460         //print "$phone $name $address<br>";
3461       }
3462       $this->t->pfp('out','dir_view_t');
3463       $this->save_sessiondata();   
3464     }
3465   
3466   function org_view()
3467     {
3468       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3469       $this->t->set_block('org_view_t','calling_list','list1');
3470       $this->t->set_block('org_view_t','org_list','list2');
3471
3472       # Display a list ordered alphabetically
3473       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
3474       $this->db->query($sql,__LINE__,__FILE__);
3475       $i=0;
3476       while ($this->db->next_record())
3477         {
3478           $calling[$i]['id'] = $this->db->f('indiv_id');
3479           $calling[$i]['name'] = $this->db->f('name');
3480           $calling[$i]['position'] = $this->db->f('position');
3481           $calling[$i]['sustained'] = $this->db->f('sustained');
3482           $calling[$i]['organization'] = $this->db->f('organization');
3483           $i++;
3484         }   
3485       for ($i=0; $i < count($calling); $i++) 
3486       {
3487         $name = $calling[$i]['name'];
3488         $position = $calling[$i]['position'];
3489         $sustained = $calling[$i]['sustained'];
3490         $organization = $calling[$i]['organization'];
3491         $this->t->set_var('name', $name);
3492         $this->t->set_var('position', $position);
3493         $this->t->set_var('sustained', $sustained);
3494         $this->t->set_var('organization', $organization);
3495         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3496         $this->t->set_var('tr_color',$tr_color);
3497         $this->t->fp('list1','calling_list',True);
3498       }
3499
3500       # Display a list ordered by organization
3501       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
3502       $this->db->query($sql,__LINE__,__FILE__);
3503       $i=0;
3504       while ($this->db->next_record())
3505         {
3506           $calling[$i]['id'] = $this->db->f('indiv_id');
3507           $calling[$i]['name'] = $this->db->f('name');
3508           $calling[$i]['position'] = $this->db->f('position');
3509           $calling[$i]['sustained'] = $this->db->f('sustained');
3510           $calling[$i]['organization'] = $this->db->f('organization');
3511           $i++;
3512         }   
3513       for ($i=0; $i < count($calling); $i++) 
3514       {
3515         $name = $calling[$i]['name'];
3516         $position = $calling[$i]['position'];
3517         $sustained = $calling[$i]['sustained'];
3518         $organization = $calling[$i]['organization'];
3519         $this->t->set_var('name', $name);
3520         $this->t->set_var('position', $position);
3521         $this->t->set_var('sustained', $sustained);
3522         $this->t->set_var('organization', $organization);
3523         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3524         $this->t->set_var('tr_color',$tr_color);
3525         $this->t->fp('list2','org_list',True);
3526       }
3527       
3528       $this->t->pfp('out','org_view_t');
3529       $this->save_sessiondata();   
3530     }
3531   
3532   function schedule()
3533     {
3534       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3535       $this->t->set_block('sched_t','presidency_list','list');
3536
3537       $action = get_var('action',array('GET','POST'));
3538       
3539       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3540       $this->t->set_var('title','EQ Scheduling Tool');
3541
3542       $this->t->set_var('lang_save','Save Schedule');
3543       $this->t->set_var('lang_reset','Cancel');
3544       
3545       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3546       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3547
3548       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3549       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3550       
3551       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3552       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3553       
3554       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3555       $table_width=$date_width + $time_width + $elder_width + $family_width;
3556       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3557       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3558       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3559       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3560       $table_data = "";
3561
3562       if($action == 'save')
3563         {
3564           $new_data = get_var('sched',array('POST'));
3565           foreach ($new_data as $presidency_array)
3566            {
3567              foreach ($presidency_array as $entry)
3568                {
3569                  $presidency = $entry['presidency'];
3570                  $appointment = $entry['appointment'];
3571                  $date = $entry['date'];
3572                  $hour = $entry['hour'];
3573                  $minute = $entry['minute'];
3574                  $pm = $entry['pm'];
3575                  $elder = $entry['elder'];
3576                  $family = $entry['family'];
3577                  if($pm) { $hour = $hour + 12; }
3578                  $time = $hour.':'.$minute.':'.'00';
3579                  $uid = 0;
3580
3581                  // Zero out the family or elder if date = NULL
3582                  if($date == "") {
3583                    $elder = 0;
3584                    $family = 0;
3585                  }
3586                  
3587                  // Update an existing appointment
3588                  if($appointment < $this->max_appointments)
3589                    {
3590                      //Only perform a database update if we have made a change to this appointment
3591                      $sql = "SELECT * FROM eq_appointment where " .
3592                         "appointment='$appointment'" .
3593                         " and presidency='$presidency'" .
3594                         " and elder='$elder'" .
3595                         " and family='$family'" .
3596                         " and date='$date'" .
3597                         " and time='$time'";
3598                      $this->db->query($sql,__LINE__,__FILE__);
3599                      if(!$this->db->next_record()) {
3600                        $old_date = $this->db->f('date');
3601                        $old_time = $this->db->f('time');
3602                        $this->db2->query("UPDATE eq_appointment set" .
3603                                         " family=" . $family . 
3604                                         " ,elder=" . $elder . 
3605                                         " ,date='" . $date . "'" .
3606                                         " ,time='" . $time . "'" .
3607                                         " ,presidency='" . $presidency . "'" .
3608                                         " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3609                        
3610                        // Email the appointment
3611                        $this->email_appt($appointment);
3612                      }
3613                    }
3614                  
3615                  // Add a new appointment
3616                  else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3617                    {
3618                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";
3619                      $this->db2->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time,uid) "
3620                            . "VALUES (NULL,'" . $presidency . "','" . $family . "','"
3621                            . $elder . "','" . $date . "','" . $time  . "','" . $uid ."')",__LINE__,__FILE__);
3622
3623                      // Now reselect this entry from the database to see if we need
3624                      // to send an appointment out for it.
3625                      $sql = "SELECT * FROM eq_appointment where " .
3626                         "elder='$elder'" .
3627                         " and family='$family'" .
3628                         " and presidency='$presidency'" .
3629                         " and date='$date'" .
3630                         " and time='$time'" .
3631                         " and uid='$uid'";
3632                      $this->db3->query($sql,__LINE__,__FILE__);
3633                      if($this->db3->next_record()) {
3634                        // Email the appointment if warranted
3635                        if(($date != "") && ($time != "") && (($elder > 0) || $family > 0)) { 
3636                          $this->email_appt($this->db3->f('appointment'));
3637                        }
3638                      }
3639                    }
3640                }
3641            }
3642           
3643           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3644           //Header('Location: ' . $take_me_to_url);
3645         }
3646
3647       $sql = "SELECT * FROM eq_presidency where valid=1";
3648       $this->db->query($sql,__LINE__,__FILE__);
3649       $i=0;
3650       while ($this->db->next_record())
3651         {
3652           $presidency_data[$i]['id'] = $this->db->f('presidency');
3653           $presidency_data[$i]['name'] = $this->db->f('name');
3654           $presidency_data[$i]['elder'] = $this->db->f('elder');
3655           $i++;
3656         }
3657       
3658       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3659       $this->db->query($sql,__LINE__,__FILE__);
3660       $i=0;
3661       while ($this->db->next_record())
3662         {
3663           $elder_id[$i] = $this->db->f('elder');
3664           $elder_name[$i] = $this->db->f('name');
3665           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3666           $i++;
3667         }
3668       array_multisort($elder_name, $elder_id);
3669
3670       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3671       $this->db->query($sql,__LINE__,__FILE__);
3672       $i=0;
3673       while ($this->db->next_record())
3674         {
3675           $family_id[$i] = $this->db->f('family');
3676           $family_name[$i] = $this->db->f('name');
3677           $i++;
3678         }
3679       array_multisort($family_name, $family_id);
3680       
3681       for ($i=0; $i < count($presidency_data); $i++) {
3682         $presidency = $presidency_data[$i]['id'];
3683         $interviewer = $presidency_data[$i]['elder'];
3684         $name = $presidency_data[$i]['name'];
3685         $this->t->set_var('presidency_name',$name);
3686         $table_data="";
3687         
3688         // query the database for all the appointments
3689         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3690         $this->db->query($sql,__LINE__,__FILE__);
3691
3692         // Prefill any existing appointment slots
3693         while ($this->db->next_record())
3694           {
3695             $appointment = $this->db->f('appointment');
3696             $elder = $this->db->f('elder');
3697             $family = $this->db->f('family');
3698             
3699             $date = $this->db->f('date');
3700             $date_array = explode("-",$date);
3701             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3702             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3703             
3704             $time = $this->db->f('time');
3705             $time_array = explode(":",$time);
3706             $hour = $time_array[0];
3707             $minute = $time_array[1];
3708             $pm = 0;
3709             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3710             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3711             
3712             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3713
3714             // Date selection
3715             $table_data.= '<td align=left>';
3716             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3717             $table_data.= '</td>';
3718             
3719             // Hour & Minutes selection
3720             $table_data.= "<td align=center>";
3721             $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
3722             $table_data.= "</td>";
3723             
3724             // Elder drop down list (for PPIs)
3725             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3726             $table_data.= '<option value=0></option>';  
3727             for ($j=0; $j < count($elder_id); $j++) {
3728               $id = $elder_id[$j];
3729               $name = $elder_name[$j];
3730               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3731               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3732             }
3733             $table_data.='</select></td>';
3734
3735             // Family drop down list (for Visits)
3736             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3737             $table_data.= '<option value=0></option>';              
3738             for ($j=0; $j < count($elder_id); $j++) {
3739               $id = $family_id[$j];
3740               $name = $family_name[$j];
3741               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3742               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3743             }
3744             $table_data.='</select></td>';
3745             
3746             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3747             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3748         
3749             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3750             $this->t->set_var('tr_color',$tr_color);
3751             
3752           }
3753
3754         // Create blank appointment slot
3755         for ($b=0; $b < 4; $b++) {
3756           $appointment = $this->max_appointments + $b;
3757           $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3758
3759           // Date selection
3760           $table_data.= '<td align=left>';
3761           $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3762           $table_data.= '</td>';
3763         
3764           // Time selection
3765           $table_data.= "<td align=center>";
3766           $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
3767           $table_data.= "</td>";
3768           
3769           // Elder drop down list
3770           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3771           $table_data.= '<option value=0></option>';  
3772           for ($j=0; $j < count($elder_id); $j++) {
3773             $id = $elder_id[$j];
3774             $name = $elder_name[$j];
3775             $table_data.= '<option value='.$id.'>'.$name.'</option>';
3776           }
3777           $table_data.='</select></td>';
3778           
3779           // Family drop down list
3780           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3781           $table_data.= '<option value=0></option>';        
3782           for ($j=0; $j < count($elder_id); $j++) {
3783             $id = $family_id[$j];
3784             $name = $family_name[$j];
3785             $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3786           }
3787           $table_data.='</select></td>';
3788           
3789           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3790           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3791
3792           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3793           $this->t->set_var('tr_color',$tr_color);
3794         }
3795         
3796         $this->t->set_var('table_data',$table_data);
3797         $this->t->set_var('header_row',$header_row);
3798         $this->t->set_var('table_width',$table_width);
3799         $this->t->fp('list','presidency_list',True);
3800         
3801       }
3802       
3803       $this->t->pfp('out','sched_t');
3804       $this->save_sessiondata();   
3805     }
3806   
3807   function admin()
3808     {
3809       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3810       $this->t->set_block('admin_t','upload','uploadhandle');
3811       $this->t->set_block('admin_t','admin','adminhandle');
3812       $this->t->set_block('admin_t','cmd','cmdhandle');
3813       $this->t->set_block('admin_t','presidency','presidencyhandle');
3814       
3815       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3816       $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=presidency'));
3817       
3818       $action = get_var('action',array('GET','POST'));
3819
3820       $this->t->pfp('out','admin_t');
3821
3822       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3823       $this->db->query($sql,__LINE__,__FILE__);
3824       $i=0;
3825       while ($this->db->next_record())
3826         {
3827           $elder_id[$i] = $this->db->f('elder');
3828           $elder_name[$i] = $this->db->f('name');
3829           $elder2name[$elder_id[$i]] = $elder_name[$i];
3830           $i++;
3831         }
3832       array_multisort($elder_name, $elder_id);
3833
3834       if($action == 'upload')
3835         {        
3836           $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
3837           
3838           if(($_FILES['uploadedfile']['type'] == "application/zip") ||
3839              ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3840              ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3841              ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
3842
3843             if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
3844               $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
3845               $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
3846               $this->t->set_var('uploadstatus',$uploadstatus);
3847               $this->t->pfp('uploadhandle','upload',True);
3848               return 0;
3849             }
3850             
3851             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3852             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3853             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3854             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3855             $this->t->set_var('uploadstatus',$uploadstatus);
3856             $this->t->pfp('uploadhandle','upload');
3857             $this->t->set_var('uploadhandle','');
3858             print "<table border=1 width=80%><tr><td>\n<pre>";
3859             
3860             # make a directory for this data to be stored in
3861             $date="data_" . date("Y_m_d");
3862             $data_dir = $this->upload_target_path . '/' . $date;
3863             print "-> Making the data directory: $date<br>\n";
3864             exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
3865             if($return_code != 0) {
3866               print implode('\n',$result) . "<br>";
3867               print "<b><font color=red>";
3868               print "-E- Unable to create the data directory. Aborting import.";
3869               print "</font></b>";
3870               return 0;
3871             }
3872
3873             # move the file uploaded into this directory
3874             print "-> Moving the uploaded file into the data dir<br>\n";
3875             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3876             if($return_code != 0) {
3877               print implode('\n',$result) . "<br>";
3878               print "<b><font color=red>";
3879               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3880               print "</font></b>";
3881               return 0;
3882             }
3883             
3884             # unzip the data into this directory
3885             print "-> Unzipping the data<br>\n";
3886             exec($this->unzip_path .' -u '. $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3887             if($return_code != 0) {
3888               print implode('\n',$result) . "<br>";
3889               print "<b><font color=red>";
3890               print "-E- Unable to unzip the uploaded file into the data dir: $data_dir. Aborting import.";
3891               print "</font></b>";
3892               return 0;
3893             }
3894             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3895
3896             # update the data_latest link to point to this new directory
3897             print "-> Updating the latest data dir link<br>\n";
3898             $data_latest = $this->upload_target_path . '/data_latest';
3899             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3900             if($return_code != 0) {
3901               print implode('\n',$result) . "<br>";
3902               print "<b><font color=red>";
3903               print "-E- Unable to update the data latest link. Aborting import.";
3904               print "</font></b>";
3905               return 0;
3906             }
3907             
3908             # run the import perl script to encorporate it into the DB
3909             ob_start('ob_logstdout', 2);
3910             print "-> Importing the data into the EQ database<br>\n";
3911             ob_flush(); flush(); sleep(1);
3912             $import_log = $this->upload_target_path . '/import.log';
3913             $data_log = $this->upload_target_path . '/data.log';
3914             $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
3915             $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
3916             #print "import_cmd: $import_cmd<br>";
3917             #print "parse_cmd: $parse_cmd<br>";
3918             ob_start('ob_logstdout', 2);
3919             passthru($import_cmd);
3920             passthru($parse_cmd);
3921             ob_flush(); flush(); sleep(1);
3922
3923             # fix the permissions of the data dir
3924             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3925             
3926             $this->t->pfp('cmdhandle','cmd');
3927             print "</pre></td></tr></table>";
3928             
3929           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3930                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3931                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3932                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3933             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3934             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3935             $this->t->set_var('uploadstatus',$uploadstatus);
3936             $this->t->pfp('uploadhandle','upload',True);
3937           } else {
3938             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3939             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3940             $this->t->set_var('uploadstatus',$uploadstatus);
3941             $this->t->pfp('uploadhandle','upload',True);
3942           }
3943         }
3944       else if($action == "presidency")
3945         {
3946           $new_data = get_var('eqpres',array('POST'));
3947           foreach ($new_data as $entry)
3948            {
3949              $id = $entry['id'];
3950              $email = $entry['email'];
3951              $elder = $entry['elder'];
3952              $name = $entry['name'];
3953              $district = $entry['district'];
3954              $president = $entry['president'];
3955              $counselor = $entry['counselor'];
3956              $secretary = $entry['secretary'];
3957              $eqpresidency = $entry['eqpresidency'];
3958              // Set the elder id to 0 for EQ Presidency tagged entry
3959              if($eqpresidency == 1) { $elder="0"; }
3960              // Re-look up the elder name for the ID if we aren't an EQ Presidency tagged entry
3961              else { $name = $elder2name[$elder]; }
3962              //print "id=$id elder=$elder name=$name email=$email district=$district president=$president ";
3963              //print "counselor=$counselor secretary=$secretary eqpres=$eqpresidency<br>";
3964
3965              if(($elder > 0) || ($name != "")) {
3966                if($id < $this->max_presidency_members) {
3967                  //print "Updating Existing Entry<br>";
3968                  $this->db2->query("UPDATE eq_presidency set" .
3969                                    " elder=" . $elder . 
3970                                    " ,district=" . $district . 
3971                                    " ,name='" . $name . "'" .
3972                                    " ,email='" . $email . "'" .
3973                                    " ,president='" . $president . "'" .
3974                                    " ,counselor='" . $counselor . "'" .
3975                                    " ,secretary='" . $secretary . "'" .
3976                                    " ,eqpres='" . $eqpresidency . "'" .
3977                                    " WHERE presidency=" . $id,__LINE__,__FILE__);
3978                  
3979                } else {
3980                  //print "Adding New Entry<br>";
3981                  $this->db2->query("INSERT INTO eq_presidency (presidency,elder,district,name,"
3982                                    . "email,president,counselor,secretary,eqpres,valid) "
3983                                    . "VALUES (NULL,'" . $elder . "','" . $district . "','"
3984                                    . $name . "','" . $email . "','" . $president  . "','"
3985                                    . $counselor . "','" . $secretary . "','" . $eqpres  . "','1'"
3986                                    .")",__LINE__,__FILE__);
3987                }
3988              } else {
3989                //print "Ignoring Blank Entry<br>";
3990              }
3991            }
3992
3993           // Now update the eq_district table appropriately
3994           
3995           // Delete all the previous district entries from the table
3996           $this->db->query("DELETE from eq_district where valid=1",__LINE__,__FILE__);
3997           $this->db->query("DELETE from eq_district where valid=0",__LINE__,__FILE__);
3998
3999           // Always add a "District 0" assigned to the High Priests Group
4000           $district = 0;
4001           $name = "High Priests";
4002           $elder = 0;
4003           $valid = 0;
4004           $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4005                             . "VALUES ('" . $district . "','" . $name . "','"
4006                             . $elder . "','" . $valid . "'"
4007                             .")",__LINE__,__FILE__);
4008           
4009           
4010           // Requery the eq_presidency table
4011           $sql = "SELECT * FROM eq_presidency where valid=1";
4012           $this->db->query($sql,__LINE__,__FILE__);
4013           while ($this->db->next_record())
4014             {
4015               // Extract the data for each presidency record
4016               $id = $this->db->f('presidency');
4017               $elder = $this->db->f('elder');
4018               $name = $this->db->f('name');
4019               $district = $this->db->f('district');
4020               $name = $this->db->f('name');
4021               $valid = 1;
4022
4023               // If we have a valid district, add it to the district table
4024               if($district > 0) {
4025                 $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4026                                   . "VALUES ('" . $district . "','" . $name . "','"
4027                                   . $elder . "','" . $valid . "'"
4028                                   .")",__LINE__,__FILE__);
4029               }
4030               
4031             }
4032           
4033           $this->t->set_var('adminhandle','');
4034           $this->t->pfp('adminhandle','admin'); 
4035         }
4036       else
4037         {
4038           $this->t->set_var('adminhandle','');
4039           $this->t->pfp('adminhandle','admin'); 
4040         }
4041
4042       // Now save off the data needed for an EQ Presidency Table Update
4043       
4044       $sql = "SELECT * FROM eq_presidency where valid=1";
4045       $this->db->query($sql,__LINE__,__FILE__);
4046       $table_data = "";
4047       $header_row = "<th>Elder</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th><th>EQ Presidency</th>";
4048       while ($this->db->next_record())
4049         {
4050           // Extract the data for each presidency record
4051           $id = $this->db->f('presidency');
4052           $elder = $this->db->f('elder');
4053           $district = $this->db->f('district');
4054           $name = $this->db->f('name');
4055           $email = $this->db->f('email');
4056           $president = $this->db->f('president');
4057           $counselor = $this->db->f('counselor');
4058           $secretary = $this->db->f('secretary');
4059           $eqpresidency = $this->db->f('eqpres');
4060
4061           // Create the forms needed in the table
4062           $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4063           
4064           // Presidency ID
4065           $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4066           
4067           // Elder
4068           if($eqpresidency == 0) { 
4069             $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4070             $table_data.= '<option value=0></option>';  
4071             for ($j=0; $j < count($elder_id); $j++) {
4072               $tmp_id = $elder_id[$j];
4073               $name = $elder_name[$j];
4074               if($elder_id[$j] == $elder) { $eldername = $name; $selected = 'selected="selected"'; } else { $selected = ''; }
4075               $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4076             }
4077             $table_data.='</select></td>';
4078             $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$eldername.'">';
4079           } else {
4080             $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="EQ Presidency"></td>';
4081             $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="EQ Presidency">';
4082           }
4083             
4084           // Email Address
4085           $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4086           
4087           // District
4088           $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4089           $table_data.= '<option value=0></option>';
4090           for ($j=0; $j <= $this->max_num_districts; $j++) {
4091             if($district == $j) { $selected = 'selected="selected"'; } else { $selected = ''; }
4092             $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4093           }
4094           $table_data.='</select></td>';
4095           
4096           // President
4097           $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4098           if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4099           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4100           $table_data.='</select></td>';
4101           
4102           // Counselor
4103           $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4104           if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4105           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4106           $table_data.='</select></td>';
4107
4108           // Secretary
4109           $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4110           if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4111           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4112           $table_data.='</select></td>';
4113
4114           // EQ Presidency
4115           $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4116           if($eqpresidency == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4117           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4118           $table_data.='</select></td>';
4119
4120           // End of ROW
4121           $table_data .= "</tr>\n";
4122           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4123           $this->t->set_var('tr_color',$tr_color);
4124         }
4125
4126       // Now create 1 blank row to always have a line available to add a new elder with
4127       $id = $this->max_presidency_members;
4128       $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4129       // Presidency ID
4130       $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4131       // Elder
4132       $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4133       $table_data.= '<option value=0></option>';  
4134       for ($j=0; $j < count($elder_id); $j++) {
4135         $tmp_id = $elder_id[$j];
4136         $name = $elder_name[$j];
4137         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4138       }
4139       $table_data.='</select></td>';
4140       $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4141       // Email Address
4142       $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4143       // District
4144       $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4145       $table_data.= '<option value=0></option>';
4146       for ($j=0; $j <= $this->max_num_districts; $j++) {
4147         if($j == 0) { $selected = 'selected="selected"'; } else { $selected = ''; }
4148         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4149       }
4150       $table_data.='</select></td>';
4151       // President
4152       $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4153       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4154       $table_data.='</select></td>';
4155       // Counselor
4156       $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4157       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4158       $table_data.='</select></td>';
4159       // Secretary
4160       $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4161       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4162       $table_data.='</select></td>';
4163       // EQ Presidency
4164       $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4165       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4166       $table_data.='</select></td>';
4167       // End of ROW
4168       $table_data .= "</tr>\n";
4169       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4170       $this->t->set_var('tr_color',$tr_color);
4171       
4172       $this->t->set_var('header_row',$header_row);
4173       $this->t->set_var('table_data',$table_data);
4174       $this->t->pfp('presidencyhandle','presidency',True);
4175
4176       $this->save_sessiondata();   
4177     }
4178
4179   function email_appt($appointment)
4180     {
4181       //print "Emailing notification of appointment: $appointment <br>";
4182
4183       $sql = "SELECT * FROM eq_appointment where appointment='$appointment'";
4184       $this->db->query($sql,__LINE__,__FILE__);
4185         
4186       while ($this->db->next_record())
4187         {
4188           $appointment = $this->db->f('appointment');
4189           $presidency = $this->db->f('presidency');
4190           $interviewer = "";
4191           $email = "";
4192           $elder = $this->db->f('elder');
4193           $elder_name = "";
4194           $family = $this->db->f('family');
4195           $family_name = "";
4196           $appt_name = "";
4197           $phone = "";
4198           $location = "";
4199           $uid = $this->db->f('uid');
4200                     
4201           // Extract the year, month, day, hours, minutes, seconds from the appointment time
4202           $appt_date = $this->db->f('date');
4203           $date_array = explode("-",$appt_date);
4204           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4205           $appt_time = $this->db->f('time');
4206           $time_array = explode(":",$appt_time);
4207           $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4208
4209           // Format the appointment time into an iCal UTC equivalent
4210           $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4211           $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4212           $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4213           
4214           // Set the email address of the person making the appointment
4215           $from = $GLOBALS['phpgw_info']['user']['fullname'] . "<" .
4216                   $GLOBALS['phpgw_info']['user']['preferences']['email']['address'] . ">";
4217           
4218           $sql = "SELECT * FROM eq_presidency where presidency='$presidency'";
4219           $this->db2->query($sql,__LINE__,__FILE__);
4220           if($this->db2->next_record()) {
4221             $email = $this->db2->f('email');
4222             $interviewer = $this->db2->f('name');
4223           }
4224
4225           if($elder > 0) { 
4226             $sql = "SELECT * FROM eq_elder where elder='$elder'";
4227             $this->db2->query($sql,__LINE__,__FILE__);
4228             if($this->db2->next_record()) {
4229               $elder_name = $this->db2->f('name');
4230               $phone = $this->db2->f('phone');
4231               $appt_name = $elder_name . " Interview";
4232               $location = "$interviewer"."'s home";
4233               $duration = $this->default_ppi_appt_duration * 60;
4234             }
4235           }
4236
4237           if($family > 0) { 
4238             $sql = "SELECT * FROM eq_family where family='$family'";
4239             $this->db2->query($sql,__LINE__,__FILE__);
4240             if($this->db2->next_record()) {
4241               $family_name = $this->db2->f('name');
4242               $phone = $this->db2->f('phone');
4243               $elder_id = $this->db2->f('elder_id');
4244               $appt_name = $family_name . " Family Visit";
4245               $sql = "SELECT * FROM eq_elder where elder='$elder_id'";
4246               $this->db3->query($sql,__LINE__,__FILE__);
4247               if($this->db3->next_record()) {
4248                 $phone = $this->db3->f('phone');
4249               }
4250               $sql = "SELECT * FROM eq_parent where family='$family'";
4251               $this->db3->query($sql,__LINE__,__FILE__);
4252               if($this->db3->next_record()) {
4253                 $location=$this->db3->f('address');
4254               }
4255               $duration = $this->default_visit_appt_duration * 60;
4256             }
4257           }
4258
4259           $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4260           $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4261           $date = $dtstartstr . "-" . $dtendstr;
4262           $description = "$appt_name : $phone";
4263           
4264           if(($uid == 0) && ($appt_name != "")) { 
4265             // Create a new calendar item for this appointment, since this must be the first time we
4266             // are sending it out.
4267             print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4268             $uid = rand() . rand(); // Generate a random identifier for this appointment
4269             $subject = "Created: $appt_name";
4270             
4271             $this->db->query("UPDATE eq_appointment set" .
4272                              " uid=" . $uid . 
4273                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4274
4275             $action = "PUBLISH";
4276             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4277                                   $dtend, $date, $location, $appt_name, $description, $uid);
4278             
4279           } else if(($uid != 0) && ($appt_name == "")) {
4280             // Remove the calendar item for this appointment since it has already been sent
4281             // and there is no name we have changed it to.
4282             print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4283             $subject = "Canceled: $appt_date $appt_time";
4284             
4285             $this->db->query("UPDATE eq_appointment set" .
4286                              " uid=0" . 
4287                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4288             
4289             $action = "CANCEL";
4290             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4291                                   $dtend, $date, $location, $subject, $subject, $uid);
4292             
4293           } else if($uid != 0) {
4294             // Update the existing appointment since we have changed it
4295             print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4296
4297             $subject = "Canceled: $appt_date $appt_time";
4298             $action = "CANCEL";
4299             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4300                                   $dtend, $date, $location, $subject, $subject, $uid);
4301             
4302             $uid = rand() . rand(); // Generate a random identifier for this appointment
4303             $this->db->query("UPDATE eq_appointment set" .
4304                              " uid=" . $uid .
4305                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4306             
4307             $subject = "Updated: $appt_name";       
4308             $action = "PUBLISH";
4309             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4310                                   $dtend, $date, $location, $appt_name, $description, $uid);
4311           }
4312           
4313         }
4314           
4315       return true;
4316     }
4317
4318   function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4319     {
4320       // Initialize our local variables
4321       $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4322       $message = "";
4323       $headers = "";
4324
4325       // Form the headers for the email message
4326       $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4327       $headers.="Mime-Version: 1.0\n";
4328       $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4329       $headers.="Content-Disposition: inline\n";
4330       $headers.="Reply-To: $from\n";
4331       $headers.="From: $from\n";
4332
4333       // Print the plaintext version of the appointment
4334       $message.="--$boundary\n";
4335       $message.="Content-Type: text/plain; charset=us-ascii\n";
4336       $message.="Content-Disposition: inline\n";
4337       $message.="\n";
4338       $message.="What: $description\n";
4339       $message.="When: $date\n";
4340       $message.="Where: $location\n";
4341       $message.="\n";
4342       
4343       // Print the .ics attachment version of the appointment
4344       $message.="--$boundary\n";
4345       $message.="Content-Type: text/calendar; charset=us-ascii\n";
4346       $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4347       $message.="\n";
4348       $message.="BEGIN:VCALENDAR" . "\n";
4349       $message.="VERSION:2.0" . "\n";
4350       $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4351       $message.="METHOD:$action" . "\n";
4352       $message.="BEGIN:VEVENT" . "\n";
4353       $message.="ORGANIZER:MAILTO:$from". "\n";
4354       $message.="DTSTAMP:$dtstamp" . "\n";
4355       $message.="DTSTART:$dtstart" . "\n";
4356       $message.="DTEND:$dtend" . "\n";
4357       $message.="SUMMARY:$summary" . "\n";
4358       $message.="DESCRIPTION:$description" . "\n";
4359       $message.="LOCATION:$location" . "\n";
4360       $message.="UID:$uid" ."\n";
4361       $message.="TRANSP:OPAQUE" . "\n";
4362       $message.="SEQUENCE:0" . "\n";
4363       $message.="CLASS:PUBLIC" . "\n";
4364       $message.="END:VEVENT" . "\n";
4365       $message.="END:VCALENDAR" . "\n";
4366
4367       // Complete the message
4368       $message.="--$boundary\n";
4369
4370       // Send the message
4371       mail($to, $subject, $message, $headers);
4372       
4373     }
4374
4375   function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4376     {
4377       $form_data = "";
4378       $blank = 0;
4379       
4380       if($hour == 0) { $blank = 1; }
4381
4382       if($this->time_drop_down_lists == 1) {
4383         // Create drop down lists to get the time
4384         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4385         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4386         foreach(range(1,12) as $num) {
4387           if($hour == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4388           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4389         }
4390         $form_data.= '</select>';
4391         $form_data.= '&nbsp;:&nbsp;';
4392         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4393         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4394         $num = 0;
4395         while($num < 60) {
4396           if($num < 10) { $num = "0" . "$num"; }
4397           if($minute == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4398           if($blank == 1) { $selected = ""; }
4399           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4400           $num = $num + $this->time_drop_down_list_inc;
4401         }
4402         $form_data.= '</select>';
4403       } else {
4404         // Use free form text fields to get the time
4405         if($blank == 1) { $hour = ""; $minute = ""; $ampm = ""; }
4406         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4407         $form_data.= ':';
4408         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4409         $form_data.= '&nbsp;';
4410       }
4411       // Always use a drop-down select form for am/pm
4412       $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4413       if($blank == 0) { 
4414         if($pm == 0) { $form_data.= '<option value=0 selected>am</option>'; $form_data.= '<option value=1>pm</option>'; }
4415         if($pm == 1) { $form_data.= '<option value=0>am</option>'; $form_data.= '<option value=1 selected>pm</option>'; }
4416       } else {
4417         $form_data.= '<option value=""></option>';
4418         $form_data.= '<option value=0>am</option>';
4419         $form_data.= '<option value=1>pm</option>';
4420       }
4421       $form_data.= '</select>';
4422       
4423       return $form_data;
4424     }
4425 }
4426
4427 ?>