Changed monthly_hometeaching_interview_stats config variable to mean every X months.
[eq/.git] / inc / class.eq.inc.php
1 <?php
2   /**************************************************************************\
3   * phpGroupWare - eq                                                        *
4   * http://www.phpgroupware.org                                              *
5   * -----------------------------------------------                          *
6   *  This program is free software; you can redistribute it and/or modify it *
7   *  under the terms of the GNU General Public License as published by the   *
8   *  Free Software Foundation; either version 2 of the License, or (at your  *
9   *  option) any later version.                                              *
10   \**************************************************************************/
11       /* $Id: class.eq.inc.php,v 1.1.1.1 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           $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2731           if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2732           if(($month_begins) == 1) { $total = $ints[$m]; }
2733           else { $total += $ints[$m]; }
2734           $percent = ceil(($total / $num_companionships)*100);
2735           $stat_data .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2736         }
2737         $stat_data .= "</tr>";
2738         
2739         $this->t->set_var('table_width',$table_width);
2740         $this->t->set_var('header_row',$header_row);
2741         $this->t->set_var('table_data',$table_data);
2742         $this->t->set_var('stat_data',$stat_data);
2743         $this->t->fp('list','district_list',True);
2744       }
2745
2746       // Display the totals
2747       $total = 0;
2748       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Totals:</font></b></td>";
2749       for($m=$num_months; $m >=0; $m--) {
2750         $month = $current_month - $m;
2751         $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2752         if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2753         if(($month_begins) == 1) { $total = $total_ints[$m]; }
2754         else { $total += $total_ints[$m]; }
2755         $percent = ceil(($total / $total_companionships)*100);
2756         $totals .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2757       }
2758       $totals .= "</tr>";
2759           
2760       $this->t->set_var('totals',$totals);
2761       $this->t->pfp('out','int_view_t');
2762       $this->save_sessiondata(); 
2763     }
2764
2765   function int_update()
2766     {
2767       $this->t->set_file(array('form' => 'int_update.tpl'));
2768       $this->t->set_block('form','interviewer_list','int_list');
2769       $this->t->set_block('form','add','addhandle');
2770       $this->t->set_block('form','edit','edithandle');
2771       
2772       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2773       $this->t->set_var('readonly','');
2774       $this->t->set_var('disabled','');
2775       $this->t->set_var('eqpresppi_checked','');
2776       
2777       $action = get_var('action',array('GET','POST'));
2778       $companionship = get_var('companionship',array('GET','POST'));
2779       $interviewer = get_var('interviewer',array('GET','POST'));      
2780       $name = get_var('name',array('GET','POST'));
2781       $ppi = get_var('ppi',array('GET','POST'));
2782       $elder = get_var('elder',array('GET','POST'));
2783       $aaronic = get_var('aaronic',array('GET','POST'));
2784       $date = get_var('date',array('GET','POST'));
2785       $notes = get_var('notes',array('GET','POST'));
2786       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
2787       
2788       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2789       $this->db->query($sql,__LINE__,__FILE__);
2790       while ($this->db->next_record())
2791         {
2792           $supervisor = $this->db->f('supervisor');
2793           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2794           $this->db2->query($sql,__LINE__,__FILE__);
2795           $this->db2->next_record();
2796           $interviewer_name = $this->db2->f('name');
2797           
2798           if($supervisor == $interviewer) { 
2799             $this->t->set_var('interviewer',$supervisor . ' selected');
2800           } else {
2801             $this->t->set_var('interviewer',$interviewer);
2802           }
2803           $this->t->set_var('interviewer_name',$interviewer_name);
2804           $this->t->fp('int_list','interviewer_list',True);
2805         }
2806       
2807       if($action == 'save')
2808         {
2809           $notes = get_var('notes',array('POST'));
2810           $this->db->query("UPDATE eq_ppi set " .
2811                            "   ppi='" . $ppi . "'" .
2812                     ", interviewer='" . $interviewer . "'" .
2813                           ", elder='" . $elder . "'" .
2814                         ", aaronic='" . $aaronic . "'" .
2815                            ", date='" . $date . "'" .
2816                           ", notes='" . $notes . "'" .
2817                       ", eqpresppi='" . $eqpresppi . "'" .
2818                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2819           $this->int_view();
2820           return false;
2821         }
2822
2823       if($action == 'insert')
2824         {
2825           $notes = get_var('notes',array('POST'));
2826           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
2827                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2828                            . $date . "','" . $notes ."','" . $eqpresppi . "')",__LINE__,__FILE__);
2829           $this->int_view();
2830           return false;
2831         }
2832       
2833       if($action == 'add')
2834         {
2835           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2836           $this->t->set_var('ppi', '');
2837           $this->t->set_var('interviewer', $interviewer);
2838           $this->t->set_var('name',$name);
2839           $this->t->set_var('elder',$elder);
2840           $this->t->set_var('aaronic',$aaronic);
2841           $this->t->set_var('date','');
2842           $this->t->set_var('notes','');
2843           $this->t->set_var('lang_done','Cancel');
2844           $this->t->set_var('lang_action','Adding New Interview');
2845           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2846                                                                 . $ppi . '&action=' . 'insert'));
2847         }
2848
2849       if($action == 'edit' || $action == 'view')
2850         {
2851           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2852           $this->db->query($sql,__LINE__,__FILE__);
2853           $this->db->next_record();
2854           $this->t->set_var('ppi',$ppi);
2855           $this->t->set_var('name',$name);
2856           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2857           $this->t->set_var('elder',$this->db->f('elder'));
2858           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2859           $this->t->set_var('date',$this->db->f('date'));
2860           $this->t->set_var('notes',$this->db->f('notes'));
2861           if($this->db->f('eqpresppi') == 1) { $this->t->set_var('eqpresppi_checked','checked'); }
2862         }
2863       
2864       if($action == 'edit')
2865         {
2866           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2867           $this->t->set_var('lang_done','Cancel');
2868           $this->t->set_var('lang_action','Editing Interview');
2869           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2870                                                                 . $ppi . '&action=' . 'save'));
2871         }
2872
2873       if($action == 'view')
2874         {
2875           $date = $this->db->f('date');
2876           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2877           $this->t->set_var('readonly','READONLY');
2878           $this->t->set_var('disabled','DISABLED');
2879           $this->t->set_var('lang_done','Done');
2880           $this->t->set_var('lang_action','Viewing Interview');
2881           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&ppi='
2882                                                                 . $ppi . '&action=' . 'edit'));
2883         }
2884       
2885       $this->t->set_var('lang_reset','Clear Form');
2886       $this->t->set_var('lang_add','Add Interview');
2887       $this->t->set_var('lang_save','Save Changes');
2888       $this->t->set_var('edithandle','');
2889       $this->t->set_var('addhandle','');
2890
2891       $this->t->pfp('out','form');
2892       
2893       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2894       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2895       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2896      
2897       $this->save_sessiondata(); 
2898     }
2899
2900   function vis_view()
2901     {
2902       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2903       $this->t->set_block('vis_view_t','visit_list','list1');
2904       $this->t->set_block('vis_view_t','family_list','list2');
2905
2906       $this->t->set_var('lang_name','Family Name');
2907       $this->t->set_var('lang_date','Date');
2908
2909       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2910       $this->t->set_var('vis_link_title','View Yearly Visits');
2911       
2912       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2913       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2914
2915       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2916       $num_years = get_var('num_years',array('GET','POST'));
2917       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2918       $this->t->set_var('num_years',$num_years);
2919       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2920       else {  $this->t->set_var('lang_num_years','Years of History'); }
2921       $this->t->set_var('lang_filter','Filter');
2922       
2923       $year = date('Y') - $num_years + 1;
2924       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2925       
2926       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2927       $this->db->query($sql,__LINE__,__FILE__);
2928       $total_records = $this->db->num_rows();
2929
2930       $i = 0;
2931       while ($this->db->next_record())
2932         {
2933           $visit_list[$i]['visit'] = $this->db->f('visit');
2934           $visit_list[$i]['family'] = $this->db->f('family');
2935           $visit_list[$i]['date']  = $this->db->f('date');
2936           $i++;
2937         }
2938             
2939       for ($i=0; $i < count($visit_list); $i++)
2940         {         
2941           $this->nextmatchs->template_alternate_row_color(&$this->t);
2942
2943           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2944           $this->db->query($sql,__LINE__,__FILE__);
2945           $this->db->next_record();
2946                   
2947           $this->t->set_var('family',$visit_list[$i]['family']);
2948           $this->t->set_var('family_name',$this->db->f('name'));
2949           $this->t->set_var('date',$visit_list[$i]['date']);
2950           
2951           $link_data['menuaction'] = 'eq.eq.vis_update';
2952           $link_data['visit'] = $visit_list[$i]['visit'];
2953           $link_data['name'] = $this->db->f('name');
2954           $link_data['date'] = $visit_list[$i]['date'];
2955           $link_data['action'] = 'view';
2956           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2957           $this->t->set_var('lang_view','View');
2958
2959           $link_data['menuaction'] = 'eq.eq.vis_update';
2960           $link_data['visit'] = $visit_list[$i]['visit'];
2961           $link_data['name'] = $this->db->f('name');
2962           $link_data['date'] = $visit_list[$i]['date'];
2963           $link_data['action'] = 'edit';
2964           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2965           $this->t->set_var('lang_edit','Edit');
2966
2967           $this->t->fp('list1','visit_list',True);
2968         }
2969
2970       // List the families that are available to record a visit against
2971       $sql = "SELECT * FROM eq_family WHERE valid=1";
2972       $this->db->query($sql,__LINE__,__FILE__);
2973       $total_records = $this->db->num_rows();
2974
2975       $i = 0;
2976       while ($this->db->next_record())
2977         {
2978           $family_names[$i] = $this->db->f('name');
2979           $family_ids[$i] = $this->db->f('family');
2980           $i++;
2981         } array_multisort($family_names, $family_ids);
2982       
2983       for ($i=0; $i < count($family_names); $i++)
2984         {
2985           $link_data['menuaction'] = 'eq.eq.vis_update';
2986           $link_data['visit'] = '';
2987           $link_data['family'] = $family_ids[$i];
2988           $link_data['action'] = 'add';
2989           $link_data['name'] = $family_names[$i];
2990           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2991
2992           $this->t->set_var('name',$family_names[$i]);
2993           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2994           else { $this->t->set_var('table_sep',"</td>"); }
2995           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2996
2997           $this->t->fp('list2','family_list',True);
2998         }   
2999
3000       $this->t->pfp('out','vis_view_t');
3001       $this->save_sessiondata(); 
3002     }
3003
3004   function vis_update()
3005     {
3006       $this->t->set_file(array('form' => 'vis_update.tpl'));
3007       $this->t->set_block('form','add','addhandle');
3008       $this->t->set_block('form','edit','edithandle');
3009       
3010       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
3011       $this->t->set_var('readonly','');
3012       $this->t->set_var('disabled','');
3013       
3014       $action = get_var('action',array('GET','POST'));
3015       $visit = get_var('visit',array('GET','POST'));
3016       $family = get_var('family',array('GET','POST'));
3017       $name = get_var('name',array('GET','POST'));
3018       $date = get_var('date',array('GET','POST'));
3019       $notes = get_var('notes',array('GET','POST'));
3020       $companionship = 0;
3021       
3022       if($action == 'save')
3023         {
3024           $notes = get_var('notes',array('POST'));
3025           $this->db->query("UPDATE eq_visit set " .
3026                            "  date='" . $date . "'" .
3027                           ", notes='" . $notes . "'" .
3028                            " WHERE visit=" . $visit,__LINE__,__FILE__);
3029           $this->vis_view();
3030           return false;
3031         }
3032
3033       if($action == 'insert')
3034         {
3035           $notes = get_var('notes',array('POST'));
3036           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
3037                            . "VALUES ('" . $family . "','" . $companionship . "','"
3038                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
3039           $this->vis_view();
3040           return false;
3041         }
3042       
3043       if($action == 'add')
3044         {
3045           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3046           $this->t->set_var('family', $family);
3047           $this->t->set_var('visit', '');
3048           $this->t->set_var('name', $name);
3049           $this->t->set_var('date','');
3050           $this->t->set_var('notes','');
3051           $this->t->set_var('lang_done','Cancel');
3052           $this->t->set_var('lang_action','Adding New Visit');
3053           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
3054                                                                 . $family . '&action=' . 'insert'));
3055         }
3056
3057       if($action == 'edit' || $action == 'view')
3058         {
3059           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
3060           $this->db->query($sql,__LINE__,__FILE__);
3061           $this->db->next_record();
3062           $this->t->set_var('visit',$visit);
3063           $this->t->set_var('name',$name);
3064           $this->t->set_var('family', $family);
3065           $this->t->set_var('date',$this->db->f('date'));
3066           $this->t->set_var('notes',$this->db->f('notes'));
3067         }
3068       
3069       if($action == 'edit')
3070         {
3071           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3072           $this->t->set_var('lang_done','Cancel');
3073           $this->t->set_var('lang_action','Editing Visit');
3074           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3075                                                                 . $visit . '&action=' . 'save'));
3076         }
3077
3078       if($action == 'view')
3079         {
3080           $date = $this->db->f('date');
3081           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3082           $this->t->set_var('readonly','READONLY');
3083           $this->t->set_var('disabled','DISABLED');
3084           $this->t->set_var('lang_done','Done');
3085           $this->t->set_var('lang_action','Viewing Visit');
3086           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3087                                                                 . $visit . '&action=' . 'edit'));
3088         }
3089       
3090       $this->t->set_var('lang_reset','Clear Form');
3091       $this->t->set_var('lang_add','Add Visit');
3092       $this->t->set_var('lang_save','Save Changes');
3093       $this->t->set_var('edithandle','');
3094       $this->t->set_var('addhandle','');
3095
3096       $this->t->pfp('out','form');
3097       
3098       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3099       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3100       if($action == 'add') { $this->t->pfp('addhandle','add'); }
3101
3102       $this->save_sessiondata(); 
3103     }
3104
3105   function att_view()
3106     {
3107       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3108       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3109       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3110
3111       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3112       $this->t->set_block('att_view_t','act_list','list');
3113
3114       $this->t->set_block('att_view_t','month_list','list1');
3115       $this->t->set_block('att_view_t','header_list','list2');
3116       $this->t->set_block('att_view_t','elder_list','list3');
3117       
3118       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3119       $num_quarters = get_var('num_quarters',array('GET','POST'));
3120       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3121       $this->t->set_var('num_quarters',$num_quarters);
3122       $this->t->set_var('lang_filter','Filter');
3123       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
3124       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
3125
3126       $num_months = $num_quarters * 3;
3127       $current_month = $this->current_month;
3128       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3129       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3130       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3131       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3132       
3133       $sql = "SELECT * FROM eq_elder where valid=1";
3134       $this->db->query($sql,__LINE__,__FILE__);
3135       $i=0;
3136       while ($this->db->next_record())
3137         {
3138           $elder_name[$i] = $this->db->f('name');
3139           $elder_id[$i] = $this->db->f('elder');
3140           $i++;
3141         }
3142       array_multisort($elder_name, $elder_id);
3143       
3144       // Create a list of sunday dates for a window of 3 months back and current month
3145       $i=0; 
3146       $last_time = 0; 
3147       $found_sunday = 0;
3148       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
3149       $last_date = explode("-",$sunday_list[0]['date']);
3150       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3151       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
3152       while($last_time < $time_limit)
3153       {
3154         $day = date("w",$last_time);
3155         if(date("w",$last_time) == 0) {
3156           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3157           $last_date = explode("-",$sunday_list[$i]['date']);
3158           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3159           $sunday_list[$i]['day'] = $last_date[2];
3160           $sunday_list[$i]['month'] = date("M",$last_time);
3161           $sunday_list[$i]['year'] = $last_date[0];
3162           $found_sunday = 1; 
3163         }
3164         $last_time += 90000;
3165         if($found_sunday) { $i++; $found_sunday=0; }
3166       }
3167
3168       $total_elders = count($elder_id);
3169       $old_month=$sunday_list[0]['month']; $span=0;
3170       for ($i=0; $i < count($sunday_list); $i++) {
3171         $date = $sunday_list[$i]['date'];
3172         $this->t->set_var('date',$sunday_list[$i]['date']);
3173         $this->t->set_var('day',$sunday_list[$i]['day']);
3174         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3175           if($i == count($sunday_list)-1) { $span++; }
3176           $cur_month = $sunday_list[$i]['month'];
3177           $old_month = $sunday_list[$i]['month'];         
3178           $link_data['menuaction'] = 'eq.eq.att_update';
3179           $link_data['month'] = $sunday_list[$i-1]['month'];
3180           $link_data['year'] = $sunday_list[$i-1]['year'];
3181           $link_data['action'] = 'update_month';
3182           $cur_month = $sunday_list[$i-1]['month'];
3183           $cur_year = $sunday_list[$i-1]['year'];
3184           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3185           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3186           $this->t->set_var('month',$sunday_list[$i-1]['month']);
3187           $this->t->set_var('year',$sunday_list[$i-1]['year']);
3188           $this->t->set_var('span',$span); $span=0;
3189           $this->t->fp('list1','month_list',True);
3190         } $span++;
3191       }
3192       $this->t->set_var('total_elders',$total_elders);
3193       $this->t->set_var('header_row',$header_row);
3194       
3195       $elder_width=200; $att_width=25; $total_width=$elder_width; 
3196       for ($i=0; $i < count($sunday_list); $i++) {
3197         $link_data['menuaction'] = 'eq.eq.att_update';
3198         $link_data['month'] = $sunday_list[$i]['month'];
3199         $link_data['year'] = $sunday_list[$i]['year'];
3200         $link_data['day'] = $sunday_list[$i]['day'];
3201         $link_data['date'] = $sunday_list[$i]['date'];
3202         $link_data['action'] = 'update_day';
3203         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3204         $this->t->set_var('date',$sunday_list[$i]['date']);
3205         $this->t->set_var('day',$sunday_list[$i]['day']);
3206         $this->t->set_var('month',$sunday_list[$i]['month']);
3207         $this->t->set_var('year',$sunday_list[$i]['year']);
3208         $this->t->fp('list2','header_list',True);
3209         $total_width += $att_width;
3210         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3211       }
3212
3213       for ($i=0; $i < count($elder_id); $i++) {
3214         $att_table = "";
3215         $this->nextmatchs->template_alternate_row_color(&$this->t);
3216         $this->t->set_var('elder_name',$elder_name[$i]);
3217         #print "checking for elder: " . $elder_id[$i] . "<br>";
3218         for ($j=0; $j < count($sunday_list); $j++) {
3219           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3220           #print "SELECT * FROM eq_attendance WHERE date='"
3221           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
3222           $sql = "SELECT * FROM eq_attendance WHERE date='"
3223              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3224           $this->db->query($sql,__LINE__,__FILE__);
3225           if($this->db->next_record()) {
3226             $cur_month = $sunday_list[$j]['month'];
3227             if($attended[$i][$cur_month] != 1) { 
3228               $attended[$i][$cur_month]=1;
3229               $attendance[$monthnum[$cur_month]]++;
3230             } 
3231             $att_table .= '<td align=center><img src="images/checkmark.gif"></td>';
3232           } else {
3233             $att_table .= '<td>&nbsp;</td>';
3234           }
3235         }
3236         $this->t->set_var('att_table',$att_table);
3237         $this->t->fp('list3','elder_list',True);
3238       }
3239       $this->t->set_var('total_width',$total_width);
3240       $this->t->set_var('elder_width',$elder_width);
3241       $this->t->set_var('att_width',$att_width);
3242
3243       # Now calculate attendance for these months
3244       $attendance_str = "";
3245       $nonattendance_str = "";
3246       $aveattendance_str = "";
3247       $avenonattendance_str = "";
3248       $num_months=0;
3249       $ave_total_attended=0;
3250       ksort($attendance);
3251       foreach($attendance as $att => $value) {
3252         $total_attended = $attendance[$att];
3253         $ave_total_attended += $attendance[$att]; $num_months++;
3254         $percent = ceil(($total_attended / $total_elders)*100);
3255         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3256         $total_nonattended = $total_elders - $total_attended;
3257         $percent = ceil(($total_nonattended / $total_elders)*100);
3258         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3259         
3260         $total_attended = ceil(($ave_total_attended / $num_months));
3261         $percent = ceil(($total_attended / $total_elders)*100);
3262         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3263         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
3264         $percent = ceil(($total_attended / $total_elders)*100);
3265         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3266       }
3267       
3268       $this->t->set_var('attendance',$attendance_str);
3269       $this->t->set_var('aveattendance',$aveattendance_str);
3270       $this->t->set_var('nonattendance',$nonattendance_str);
3271       $this->t->set_var('avenonattendance',$avenonattendance_str);
3272       
3273       $this->t->pfp('out','att_view_t');
3274       $this->save_sessiondata(); 
3275     }
3276
3277   function att_update()
3278     {
3279       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3280       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3281       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3282
3283       $this->t->set_file(array('form' => 'att_update.tpl'));
3284       $this->t->set_block('form','edit','edithandle');
3285       
3286       $this->t->set_block('form','month_list','list1');
3287       $this->t->set_block('form','header_list','list2');
3288       $this->t->set_block('form','elder_list','list3');
3289
3290       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3291
3292       $action = get_var('action',array('GET','POST'));
3293       $month = get_var('month',array('GET','POST'));
3294       $year = get_var('year',array('GET','POST'));
3295       $day = get_var('day',array('GET','POST'));
3296       $date = get_var('date',array('GET','POST'));
3297
3298       if($action == 'save_month' || $action == 'save_day')
3299         {
3300            $new_data = get_var('elders_attended',array('POST'));
3301            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3302
3303            if($action == 'save_month') {        
3304              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3305            }
3306
3307            if($action == 'save_day') {        
3308              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3309            }   
3310
3311            foreach ($new_data as $data)
3312            {
3313               $data_array = explode("-",$data);
3314               $elder = $data_array[0];
3315               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
3316               $this->db->query("INSERT INTO eq_attendance (elder,date) "
3317                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
3318            }
3319         
3320          $this->att_view();
3321          return false;    
3322         }
3323
3324       $sql = "SELECT * FROM eq_elder where valid=1";
3325       $this->db->query($sql,__LINE__,__FILE__);
3326       $i=0;
3327       while ($this->db->next_record())
3328         {
3329           $elder_name[$i] = $this->db->f('name');
3330           $elder_id[$i] = $this->db->f('elder');
3331           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
3332           $i++;
3333         }
3334       array_multisort($elder_name, $elder_id);
3335       
3336       if($action == 'update_month')
3337       {
3338         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
3339         $i=0; 
3340         $last_time = 0; 
3341         $found_sunday = 0;
3342         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3343         $last_date = explode("-",$sunday_list[0]['date']);
3344         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3345         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3346         while($last_time <= $time_limit)
3347         {
3348           $day = date("w",$last_time);
3349           if(date("w",$last_time) == 0) { 
3350             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3351             $last_date = explode("-",$sunday_list[$i]['date']);
3352             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3353             $sunday_list[$i]['day'] = $last_date[2];
3354             $sunday_list[$i]['month'] = date("M",$last_time);
3355             $sunday_list[$i]['year'] = $last_date[0];
3356             $found_sunday = 1; 
3357           }
3358           $last_time += 90000;
3359           if($found_sunday) { $i++; $found_sunday=0; }
3360         }
3361
3362         $this->t->set_var('span', $i);
3363         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3364         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3365         $this->t->fp('list1','month_list',True);
3366         $elder_width=200; $att_width=25; $total_width=$elder_width;
3367         for ($i=0; $i < count($sunday_list); $i++) {
3368           $link_data['menuaction'] = 'eq.eq.att_update';
3369           $link_data['month'] = $sunday_list[$i]['month'];
3370           $link_data['year'] = $sunday_list[$i]['year'];
3371           $link_data['day'] = $sunday_list[$i]['day'];
3372           $link_data['date'] = $sunday_list[$i]['date'];
3373           $link_data['action'] = 'update_day';
3374           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3375           $this->t->set_var('date',$sunday_list[$i]['date']);
3376           $this->t->set_var('day',$sunday_list[$i]['day']);
3377           $this->t->set_var('month',$sunday_list[$i]['month']);
3378           $this->t->set_var('year',$sunday_list[$i]['year']);
3379           $this->t->fp('list2','header_list',True);
3380           $total_width += $att_width;
3381         }     
3382       }
3383
3384       if($action == 'update_day')
3385       {
3386         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
3387         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3388         $this->t->set_var('month',$month);
3389         $this->t->set_var('year',$year);
3390         $this->t->fp('list1','month_list',True);
3391         $this->t->set_var('date',$date);
3392         $this->t->set_var('day',$day);
3393         $this->t->set_var('month',$month);
3394         $this->t->set_var('year',$year);
3395         $this->t->fp('list2','header_list',True);
3396       }           
3397             
3398       for ($i=0; $i < count($elder_id); $i++) {
3399         $att_table = "";
3400         $this->nextmatchs->template_alternate_row_color(&$this->t);
3401         $this->t->set_var('elder_name',$elder_name[$i]);
3402         for ($j=0; $j < count($sunday_list); $j++) {
3403           $sql = "SELECT * FROM eq_attendance WHERE date='"
3404              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3405           $this->db->query($sql,__LINE__,__FILE__);
3406           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
3407           if($this->db->next_record()) {
3408             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3409           } else if($elder_attending[$elder_id[$i]] == 1) {
3410             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3411           } else {
3412             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
3413           }
3414         }
3415         $this->t->set_var('att_table',$att_table);
3416         $this->t->fp('list3','elder_list',True);
3417       } 
3418            
3419       $this->t->set_var('lang_done', 'Cancel');
3420       $this->t->set_var('lang_reset','Clear Form');
3421       $this->t->set_var('lang_save','Save Changes');
3422
3423       $this->t->pfp('out','form');
3424       $this->t->pfp('addhandle','edit');
3425
3426       $this->save_sessiondata();       
3427     }
3428
3429   function dir_view()
3430     {
3431       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3432       $this->t->set_block('dir_view_t','dir_list','list');
3433       
3434       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
3435       $this->db->query($sql,__LINE__,__FILE__);
3436       $i=0;
3437       while ($this->db->next_record())
3438         {
3439           $parent[$i]['id'] = $this->db->f('parent');
3440           $parent[$i]['name'] = $this->db->f('name');
3441           $parent[$i]['phone'] = $this->db->f('phone');
3442           $parent[$i]['address'] = $this->db->f('address');
3443           $i++;
3444         }   
3445       
3446       for ($i=0; $i < count($parent); $i++) 
3447       {
3448         $name = $parent[$i]['name'];
3449         $phone = $parent[$i]['phone'];
3450         $address = $parent[$i]['address'];
3451         $this->t->set_var('name', $name);
3452         $this->t->set_var('address', $address);
3453         $this->t->set_var('phone', $phone);
3454         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3455         $this->t->set_var('tr_color',$tr_color);
3456         $this->t->fp('list','dir_list',True);
3457         //print "$phone $name $address<br>";
3458       }
3459       $this->t->pfp('out','dir_view_t');
3460       $this->save_sessiondata();   
3461     }
3462   
3463   function org_view()
3464     {
3465       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3466       $this->t->set_block('org_view_t','calling_list','list1');
3467       $this->t->set_block('org_view_t','org_list','list2');
3468
3469       # Display a list ordered alphabetically
3470       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
3471       $this->db->query($sql,__LINE__,__FILE__);
3472       $i=0;
3473       while ($this->db->next_record())
3474         {
3475           $calling[$i]['id'] = $this->db->f('indiv_id');
3476           $calling[$i]['name'] = $this->db->f('name');
3477           $calling[$i]['position'] = $this->db->f('position');
3478           $calling[$i]['sustained'] = $this->db->f('sustained');
3479           $calling[$i]['organization'] = $this->db->f('organization');
3480           $i++;
3481         }   
3482       for ($i=0; $i < count($calling); $i++) 
3483       {
3484         $name = $calling[$i]['name'];
3485         $position = $calling[$i]['position'];
3486         $sustained = $calling[$i]['sustained'];
3487         $organization = $calling[$i]['organization'];
3488         $this->t->set_var('name', $name);
3489         $this->t->set_var('position', $position);
3490         $this->t->set_var('sustained', $sustained);
3491         $this->t->set_var('organization', $organization);
3492         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3493         $this->t->set_var('tr_color',$tr_color);
3494         $this->t->fp('list1','calling_list',True);
3495       }
3496
3497       # Display a list ordered by organization
3498       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
3499       $this->db->query($sql,__LINE__,__FILE__);
3500       $i=0;
3501       while ($this->db->next_record())
3502         {
3503           $calling[$i]['id'] = $this->db->f('indiv_id');
3504           $calling[$i]['name'] = $this->db->f('name');
3505           $calling[$i]['position'] = $this->db->f('position');
3506           $calling[$i]['sustained'] = $this->db->f('sustained');
3507           $calling[$i]['organization'] = $this->db->f('organization');
3508           $i++;
3509         }   
3510       for ($i=0; $i < count($calling); $i++) 
3511       {
3512         $name = $calling[$i]['name'];
3513         $position = $calling[$i]['position'];
3514         $sustained = $calling[$i]['sustained'];
3515         $organization = $calling[$i]['organization'];
3516         $this->t->set_var('name', $name);
3517         $this->t->set_var('position', $position);
3518         $this->t->set_var('sustained', $sustained);
3519         $this->t->set_var('organization', $organization);
3520         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3521         $this->t->set_var('tr_color',$tr_color);
3522         $this->t->fp('list2','org_list',True);
3523       }
3524       
3525       $this->t->pfp('out','org_view_t');
3526       $this->save_sessiondata();   
3527     }
3528   
3529   function schedule()
3530     {
3531       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3532       $this->t->set_block('sched_t','presidency_list','list');
3533
3534       $action = get_var('action',array('GET','POST'));
3535       
3536       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3537       $this->t->set_var('title','EQ Scheduling Tool');
3538
3539       $this->t->set_var('lang_save','Save Schedule');
3540       $this->t->set_var('lang_reset','Cancel');
3541       
3542       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3543       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3544
3545       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3546       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3547       
3548       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3549       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3550       
3551       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3552       $table_width=$date_width + $time_width + $elder_width + $family_width;
3553       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3554       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3555       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3556       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3557       $table_data = "";
3558
3559       if($action == 'save')
3560         {
3561           $new_data = get_var('sched',array('POST'));
3562           foreach ($new_data as $presidency_array)
3563            {
3564              foreach ($presidency_array as $entry)
3565                {
3566                  $presidency = $entry['presidency'];
3567                  $appointment = $entry['appointment'];
3568                  $date = $entry['date'];
3569                  $hour = $entry['hour'];
3570                  $minute = $entry['minute'];
3571                  $pm = $entry['pm'];
3572                  $elder = $entry['elder'];
3573                  $family = $entry['family'];
3574                  if($pm) { $hour = $hour + 12; }
3575                  $time = $hour.':'.$minute.':'.'00';
3576                  $uid = 0;
3577
3578                  // Zero out the family or elder if date = NULL
3579                  if($date == "") {
3580                    $elder = 0;
3581                    $family = 0;
3582                  }
3583                  
3584                  // Update an existing appointment
3585                  if($appointment < $this->max_appointments)
3586                    {
3587                      //Only perform a database update if we have made a change to this appointment
3588                      $sql = "SELECT * FROM eq_appointment where " .
3589                         "appointment='$appointment'" .
3590                         " and presidency='$presidency'" .
3591                         " and elder='$elder'" .
3592                         " and family='$family'" .
3593                         " and date='$date'" .
3594                         " and time='$time'";
3595                      $this->db->query($sql,__LINE__,__FILE__);
3596                      if(!$this->db->next_record()) {
3597                        $old_date = $this->db->f('date');
3598                        $old_time = $this->db->f('time');
3599                        $this->db2->query("UPDATE eq_appointment set" .
3600                                         " family=" . $family . 
3601                                         " ,elder=" . $elder . 
3602                                         " ,date='" . $date . "'" .
3603                                         " ,time='" . $time . "'" .
3604                                         " ,presidency='" . $presidency . "'" .
3605                                         " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3606                        
3607                        // Email the appointment
3608                        $this->email_appt($appointment);
3609                      }
3610                    }
3611                  
3612                  // Add a new appointment
3613                  else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3614                    {
3615                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";
3616                      $this->db2->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time,uid) "
3617                            . "VALUES (NULL,'" . $presidency . "','" . $family . "','"
3618                            . $elder . "','" . $date . "','" . $time  . "','" . $uid ."')",__LINE__,__FILE__);
3619
3620                      // Now reselect this entry from the database to see if we need
3621                      // to send an appointment out for it.
3622                      $sql = "SELECT * FROM eq_appointment where " .
3623                         "elder='$elder'" .
3624                         " and family='$family'" .
3625                         " and presidency='$presidency'" .
3626                         " and date='$date'" .
3627                         " and time='$time'" .
3628                         " and uid='$uid'";
3629                      $this->db3->query($sql,__LINE__,__FILE__);
3630                      if($this->db3->next_record()) {
3631                        // Email the appointment if warranted
3632                        if(($date != "") && ($time != "") && (($elder > 0) || $family > 0)) { 
3633                          $this->email_appt($this->db3->f('appointment'));
3634                        }
3635                      }
3636                    }
3637                }
3638            }
3639           
3640           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3641           //Header('Location: ' . $take_me_to_url);
3642         }
3643
3644       $sql = "SELECT * FROM eq_presidency where valid=1";
3645       $this->db->query($sql,__LINE__,__FILE__);
3646       $i=0;
3647       while ($this->db->next_record())
3648         {
3649           $presidency_data[$i]['id'] = $this->db->f('presidency');
3650           $presidency_data[$i]['name'] = $this->db->f('name');
3651           $presidency_data[$i]['elder'] = $this->db->f('elder');
3652           $i++;
3653         }
3654       
3655       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3656       $this->db->query($sql,__LINE__,__FILE__);
3657       $i=0;
3658       while ($this->db->next_record())
3659         {
3660           $elder_id[$i] = $this->db->f('elder');
3661           $elder_name[$i] = $this->db->f('name');
3662           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3663           $i++;
3664         }
3665       array_multisort($elder_name, $elder_id);
3666
3667       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3668       $this->db->query($sql,__LINE__,__FILE__);
3669       $i=0;
3670       while ($this->db->next_record())
3671         {
3672           $family_id[$i] = $this->db->f('family');
3673           $family_name[$i] = $this->db->f('name');
3674           $i++;
3675         }
3676       array_multisort($family_name, $family_id);
3677       
3678       for ($i=0; $i < count($presidency_data); $i++) {
3679         $presidency = $presidency_data[$i]['id'];
3680         $interviewer = $presidency_data[$i]['elder'];
3681         $name = $presidency_data[$i]['name'];
3682         $this->t->set_var('presidency_name',$name);
3683         $table_data="";
3684         
3685         // query the database for all the appointments
3686         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3687         $this->db->query($sql,__LINE__,__FILE__);
3688
3689         // Prefill any existing appointment slots
3690         while ($this->db->next_record())
3691           {
3692             $appointment = $this->db->f('appointment');
3693             $elder = $this->db->f('elder');
3694             $family = $this->db->f('family');
3695             
3696             $date = $this->db->f('date');
3697             $date_array = explode("-",$date);
3698             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3699             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3700             
3701             $time = $this->db->f('time');
3702             $time_array = explode(":",$time);
3703             $hour = $time_array[0];
3704             $minute = $time_array[1];
3705             $pm = 0;
3706             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3707             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3708             
3709             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3710
3711             // Date selection
3712             $table_data.= '<td align=left>';
3713             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3714             $table_data.= '</td>';
3715             
3716             // Hour & Minutes selection
3717             $table_data.= "<td align=center>";
3718             $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
3719             $table_data.= "</td>";
3720             
3721             // Elder drop down list (for PPIs)
3722             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3723             $table_data.= '<option value=0></option>';  
3724             for ($j=0; $j < count($elder_id); $j++) {
3725               $id = $elder_id[$j];
3726               $name = $elder_name[$j];
3727               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3728               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3729             }
3730             $table_data.='</select></td>';
3731
3732             // Family drop down list (for Visits)
3733             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3734             $table_data.= '<option value=0></option>';              
3735             for ($j=0; $j < count($elder_id); $j++) {
3736               $id = $family_id[$j];
3737               $name = $family_name[$j];
3738               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3739               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3740             }
3741             $table_data.='</select></td>';
3742             
3743             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3744             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3745         
3746             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3747             $this->t->set_var('tr_color',$tr_color);
3748             
3749           }
3750
3751         // Create blank appointment slot
3752         for ($b=0; $b < 4; $b++) {
3753           $appointment = $this->max_appointments + $b;
3754           $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3755
3756           // Date selection
3757           $table_data.= '<td align=left>';
3758           $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3759           $table_data.= '</td>';
3760         
3761           // Time selection
3762           $table_data.= "<td align=center>";
3763           $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
3764           $table_data.= "</td>";
3765           
3766           // Elder drop down list
3767           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3768           $table_data.= '<option value=0></option>';  
3769           for ($j=0; $j < count($elder_id); $j++) {
3770             $id = $elder_id[$j];
3771             $name = $elder_name[$j];
3772             $table_data.= '<option value='.$id.'>'.$name.'</option>';
3773           }
3774           $table_data.='</select></td>';
3775           
3776           // Family drop down list
3777           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3778           $table_data.= '<option value=0></option>';        
3779           for ($j=0; $j < count($elder_id); $j++) {
3780             $id = $family_id[$j];
3781             $name = $family_name[$j];
3782             $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3783           }
3784           $table_data.='</select></td>';
3785           
3786           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3787           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3788
3789           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3790           $this->t->set_var('tr_color',$tr_color);
3791         }
3792         
3793         $this->t->set_var('table_data',$table_data);
3794         $this->t->set_var('header_row',$header_row);
3795         $this->t->set_var('table_width',$table_width);
3796         $this->t->fp('list','presidency_list',True);
3797         
3798       }
3799       
3800       $this->t->pfp('out','sched_t');
3801       $this->save_sessiondata();   
3802     }
3803   
3804   function admin()
3805     {
3806       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3807       $this->t->set_block('admin_t','upload','uploadhandle');
3808       $this->t->set_block('admin_t','admin','adminhandle');
3809       $this->t->set_block('admin_t','cmd','cmdhandle');
3810       $this->t->set_block('admin_t','presidency','presidencyhandle');
3811       
3812       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3813       $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=presidency'));
3814       
3815       $action = get_var('action',array('GET','POST'));
3816
3817       $this->t->pfp('out','admin_t');
3818
3819       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3820       $this->db->query($sql,__LINE__,__FILE__);
3821       $i=0;
3822       while ($this->db->next_record())
3823         {
3824           $elder_id[$i] = $this->db->f('elder');
3825           $elder_name[$i] = $this->db->f('name');
3826           $elder2name[$elder_id[$i]] = $elder_name[$i];
3827           $i++;
3828         }
3829       array_multisort($elder_name, $elder_id);
3830
3831       if($action == 'upload')
3832         {        
3833           $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
3834           
3835           if(($_FILES['uploadedfile']['type'] == "application/zip") ||
3836              ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3837              ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3838              ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
3839
3840             if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
3841               $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
3842               $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
3843               $this->t->set_var('uploadstatus',$uploadstatus);
3844               $this->t->pfp('uploadhandle','upload',True);
3845               return 0;
3846             }
3847             
3848             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3849             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3850             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3851             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3852             $this->t->set_var('uploadstatus',$uploadstatus);
3853             $this->t->pfp('uploadhandle','upload');
3854             $this->t->set_var('uploadhandle','');
3855             print "<table border=1 width=80%><tr><td>\n<pre>";
3856             
3857             # make a directory for this data to be stored in
3858             $date="data_" . date("Y_m_d");
3859             $data_dir = $this->upload_target_path . '/' . $date;
3860             print "-> Making the data directory: $date<br>\n";
3861             exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
3862             if($return_code != 0) {
3863               print implode('\n',$result) . "<br>";
3864               print "<b><font color=red>";
3865               print "-E- Unable to create the data directory. Aborting import.";
3866               print "</font></b>";
3867               return 0;
3868             }
3869
3870             # move the file uploaded into this directory
3871             print "-> Moving the uploaded file into the data dir<br>\n";
3872             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3873             if($return_code != 0) {
3874               print implode('\n',$result) . "<br>";
3875               print "<b><font color=red>";
3876               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3877               print "</font></b>";
3878               return 0;
3879             }
3880             
3881             # unzip the data into this directory
3882             print "-> Unzipping the data<br>\n";
3883             exec($this->unzip_path .' -u '. $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3884             if($return_code != 0) {
3885               print implode('\n',$result) . "<br>";
3886               print "<b><font color=red>";
3887               print "-E- Unable to unzip the uploaded file into the data dir: $data_dir. Aborting import.";
3888               print "</font></b>";
3889               return 0;
3890             }
3891             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3892
3893             # update the data_latest link to point to this new directory
3894             print "-> Updating the latest data dir link<br>\n";
3895             $data_latest = $this->upload_target_path . '/data_latest';
3896             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3897             if($return_code != 0) {
3898               print implode('\n',$result) . "<br>";
3899               print "<b><font color=red>";
3900               print "-E- Unable to update the data latest link. Aborting import.";
3901               print "</font></b>";
3902               return 0;
3903             }
3904             
3905             # run the import perl script to encorporate it into the DB
3906             ob_start('ob_logstdout', 2);
3907             print "-> Importing the data into the EQ database<br>\n";
3908             ob_flush(); flush(); sleep(1);
3909             $import_log = $this->upload_target_path . '/import.log';
3910             $data_log = $this->upload_target_path . '/data.log';
3911             $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
3912             $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
3913             #print "import_cmd: $import_cmd<br>";
3914             #print "parse_cmd: $parse_cmd<br>";
3915             ob_start('ob_logstdout', 2);
3916             passthru($import_cmd);
3917             passthru($parse_cmd);
3918             ob_flush(); flush(); sleep(1);
3919
3920             # fix the permissions of the data dir
3921             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3922             
3923             $this->t->pfp('cmdhandle','cmd');
3924             print "</pre></td></tr></table>";
3925             
3926           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3927                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3928                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3929                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3930             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3931             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3932             $this->t->set_var('uploadstatus',$uploadstatus);
3933             $this->t->pfp('uploadhandle','upload',True);
3934           } else {
3935             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3936             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3937             $this->t->set_var('uploadstatus',$uploadstatus);
3938             $this->t->pfp('uploadhandle','upload',True);
3939           }
3940         }
3941       else if($action == "presidency")
3942         {
3943           $new_data = get_var('eqpres',array('POST'));
3944           foreach ($new_data as $entry)
3945            {
3946              $id = $entry['id'];
3947              $email = $entry['email'];
3948              $elder = $entry['elder'];
3949              $name = $entry['name'];
3950              $district = $entry['district'];
3951              $president = $entry['president'];
3952              $counselor = $entry['counselor'];
3953              $secretary = $entry['secretary'];
3954              $eqpresidency = $entry['eqpresidency'];
3955              // Set the elder id to 0 for EQ Presidency tagged entry
3956              if($eqpresidency == 1) { $elder="0"; }
3957              // Re-look up the elder name for the ID if we aren't an EQ Presidency tagged entry
3958              else { $name = $elder2name[$elder]; }
3959              //print "id=$id elder=$elder name=$name email=$email district=$district president=$president ";
3960              //print "counselor=$counselor secretary=$secretary eqpres=$eqpresidency<br>";
3961
3962              if(($elder > 0) || ($name != "")) {
3963                if($id < $this->max_presidency_members) {
3964                  //print "Updating Existing Entry<br>";
3965                  $this->db2->query("UPDATE eq_presidency set" .
3966                                    " elder=" . $elder . 
3967                                    " ,district=" . $district . 
3968                                    " ,name='" . $name . "'" .
3969                                    " ,email='" . $email . "'" .
3970                                    " ,president='" . $president . "'" .
3971                                    " ,counselor='" . $counselor . "'" .
3972                                    " ,secretary='" . $secretary . "'" .
3973                                    " ,eqpres='" . $eqpresidency . "'" .
3974                                    " WHERE presidency=" . $id,__LINE__,__FILE__);
3975                  
3976                } else {
3977                  //print "Adding New Entry<br>";
3978                  $this->db2->query("INSERT INTO eq_presidency (presidency,elder,district,name,"
3979                                    . "email,president,counselor,secretary,eqpres,valid) "
3980                                    . "VALUES (NULL,'" . $elder . "','" . $district . "','"
3981                                    . $name . "','" . $email . "','" . $president  . "','"
3982                                    . $counselor . "','" . $secretary . "','" . $eqpres  . "','1'"
3983                                    .")",__LINE__,__FILE__);
3984                }
3985              } else {
3986                //print "Ignoring Blank Entry<br>";
3987              }
3988            }
3989
3990           // Now update the eq_district table appropriately
3991           
3992           // Delete all the previous district entries from the table
3993           $this->db->query("DELETE from eq_district where valid=1",__LINE__,__FILE__);
3994           $this->db->query("DELETE from eq_district where valid=0",__LINE__,__FILE__);
3995
3996           // Always add a "District 0" assigned to the High Priests Group
3997           $district = 0;
3998           $name = "High Priests";
3999           $elder = 0;
4000           $valid = 0;
4001           $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4002                             . "VALUES ('" . $district . "','" . $name . "','"
4003                             . $elder . "','" . $valid . "'"
4004                             .")",__LINE__,__FILE__);
4005           
4006           
4007           // Requery the eq_presidency table
4008           $sql = "SELECT * FROM eq_presidency where valid=1";
4009           $this->db->query($sql,__LINE__,__FILE__);
4010           while ($this->db->next_record())
4011             {
4012               // Extract the data for each presidency record
4013               $id = $this->db->f('presidency');
4014               $elder = $this->db->f('elder');
4015               $name = $this->db->f('name');
4016               $district = $this->db->f('district');
4017               $name = $this->db->f('name');
4018               $valid = 1;
4019
4020               // If we have a valid district, add it to the district table
4021               if($district > 0) {
4022                 $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4023                                   . "VALUES ('" . $district . "','" . $name . "','"
4024                                   . $elder . "','" . $valid . "'"
4025                                   .")",__LINE__,__FILE__);
4026               }
4027               
4028             }
4029           
4030           $this->t->set_var('adminhandle','');
4031           $this->t->pfp('adminhandle','admin'); 
4032         }
4033       else
4034         {
4035           $this->t->set_var('adminhandle','');
4036           $this->t->pfp('adminhandle','admin'); 
4037         }
4038
4039       // Now save off the data needed for an EQ Presidency Table Update
4040       
4041       $sql = "SELECT * FROM eq_presidency where valid=1";
4042       $this->db->query($sql,__LINE__,__FILE__);
4043       $table_data = "";
4044       $header_row = "<th>Elder</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th><th>EQ Presidency</th>";
4045       while ($this->db->next_record())
4046         {
4047           // Extract the data for each presidency record
4048           $id = $this->db->f('presidency');
4049           $elder = $this->db->f('elder');
4050           $district = $this->db->f('district');
4051           $name = $this->db->f('name');
4052           $email = $this->db->f('email');
4053           $president = $this->db->f('president');
4054           $counselor = $this->db->f('counselor');
4055           $secretary = $this->db->f('secretary');
4056           $eqpresidency = $this->db->f('eqpres');
4057
4058           // Create the forms needed in the table
4059           $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4060           
4061           // Presidency ID
4062           $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4063           
4064           // Elder
4065           if($eqpresidency == 0) { 
4066             $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4067             $table_data.= '<option value=0></option>';  
4068             for ($j=0; $j < count($elder_id); $j++) {
4069               $tmp_id = $elder_id[$j];
4070               $name = $elder_name[$j];
4071               if($elder_id[$j] == $elder) { $eldername = $name; $selected = 'selected="selected"'; } else { $selected = ''; }
4072               $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4073             }
4074             $table_data.='</select></td>';
4075             $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$eldername.'">';
4076           } else {
4077             $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="EQ Presidency"></td>';
4078             $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="EQ Presidency">';
4079           }
4080             
4081           // Email Address
4082           $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4083           
4084           // District
4085           $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4086           $table_data.= '<option value=0></option>';
4087           for ($j=0; $j <= $this->max_num_districts; $j++) {
4088             if($district == $j) { $selected = 'selected="selected"'; } else { $selected = ''; }
4089             $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4090           }
4091           $table_data.='</select></td>';
4092           
4093           // President
4094           $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4095           if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4096           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4097           $table_data.='</select></td>';
4098           
4099           // Counselor
4100           $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4101           if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4102           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4103           $table_data.='</select></td>';
4104
4105           // Secretary
4106           $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4107           if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4108           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4109           $table_data.='</select></td>';
4110
4111           // EQ Presidency
4112           $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4113           if($eqpresidency == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4114           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4115           $table_data.='</select></td>';
4116
4117           // End of ROW
4118           $table_data .= "</tr>\n";
4119           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4120           $this->t->set_var('tr_color',$tr_color);
4121         }
4122
4123       // Now create 1 blank row to always have a line available to add a new elder with
4124       $id = $this->max_presidency_members;
4125       $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4126       // Presidency ID
4127       $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4128       // Elder
4129       $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4130       $table_data.= '<option value=0></option>';  
4131       for ($j=0; $j < count($elder_id); $j++) {
4132         $tmp_id = $elder_id[$j];
4133         $name = $elder_name[$j];
4134         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4135       }
4136       $table_data.='</select></td>';
4137       $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4138       // Email Address
4139       $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4140       // District
4141       $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4142       $table_data.= '<option value=0></option>';
4143       for ($j=0; $j <= $this->max_num_districts; $j++) {
4144         if($j == 0) { $selected = 'selected="selected"'; } else { $selected = ''; }
4145         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4146       }
4147       $table_data.='</select></td>';
4148       // President
4149       $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4150       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4151       $table_data.='</select></td>';
4152       // Counselor
4153       $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4154       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4155       $table_data.='</select></td>';
4156       // Secretary
4157       $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4158       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4159       $table_data.='</select></td>';
4160       // EQ Presidency
4161       $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4162       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4163       $table_data.='</select></td>';
4164       // End of ROW
4165       $table_data .= "</tr>\n";
4166       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4167       $this->t->set_var('tr_color',$tr_color);
4168       
4169       $this->t->set_var('header_row',$header_row);
4170       $this->t->set_var('table_data',$table_data);
4171       $this->t->pfp('presidencyhandle','presidency',True);
4172
4173       $this->save_sessiondata();   
4174     }
4175
4176   function email_appt($appointment)
4177     {
4178       //print "Emailing notification of appointment: $appointment <br>";
4179
4180       $sql = "SELECT * FROM eq_appointment where appointment='$appointment'";
4181       $this->db->query($sql,__LINE__,__FILE__);
4182         
4183       while ($this->db->next_record())
4184         {
4185           $appointment = $this->db->f('appointment');
4186           $presidency = $this->db->f('presidency');
4187           $interviewer = "";
4188           $email = "";
4189           $elder = $this->db->f('elder');
4190           $elder_name = "";
4191           $family = $this->db->f('family');
4192           $family_name = "";
4193           $appt_name = "";
4194           $phone = "";
4195           $location = "";
4196           $uid = $this->db->f('uid');
4197                     
4198           // Extract the year, month, day, hours, minutes, seconds from the appointment time
4199           $appt_date = $this->db->f('date');
4200           $date_array = explode("-",$appt_date);
4201           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4202           $appt_time = $this->db->f('time');
4203           $time_array = explode(":",$appt_time);
4204           $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4205
4206           // Format the appointment time into an iCal UTC equivalent
4207           $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4208           $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4209           $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4210           
4211           // Set the email address of the person making the appointment
4212           $from = $GLOBALS['phpgw_info']['user']['fullname'] . "<" .
4213                   $GLOBALS['phpgw_info']['user']['preferences']['email']['address'] . ">";
4214           
4215           $sql = "SELECT * FROM eq_presidency where presidency='$presidency'";
4216           $this->db2->query($sql,__LINE__,__FILE__);
4217           if($this->db2->next_record()) {
4218             $email = $this->db2->f('email');
4219             $interviewer = $this->db2->f('name');
4220           }
4221
4222           if($elder > 0) { 
4223             $sql = "SELECT * FROM eq_elder where elder='$elder'";
4224             $this->db2->query($sql,__LINE__,__FILE__);
4225             if($this->db2->next_record()) {
4226               $elder_name = $this->db2->f('name');
4227               $phone = $this->db2->f('phone');
4228               $appt_name = $elder_name . " Interview";
4229               $location = "$interviewer"."'s home";
4230               $duration = $this->default_ppi_appt_duration * 60;
4231             }
4232           }
4233
4234           if($family > 0) { 
4235             $sql = "SELECT * FROM eq_family where family='$family'";
4236             $this->db2->query($sql,__LINE__,__FILE__);
4237             if($this->db2->next_record()) {
4238               $family_name = $this->db2->f('name');
4239               $phone = $this->db2->f('phone');
4240               $elder_id = $this->db2->f('elder_id');
4241               $appt_name = $family_name . " Family Visit";
4242               $sql = "SELECT * FROM eq_elder where elder='$elder_id'";
4243               $this->db3->query($sql,__LINE__,__FILE__);
4244               if($this->db3->next_record()) {
4245                 $phone = $this->db3->f('phone');
4246               }
4247               $sql = "SELECT * FROM eq_parent where family='$family'";
4248               $this->db3->query($sql,__LINE__,__FILE__);
4249               if($this->db3->next_record()) {
4250                 $location=$this->db3->f('address');
4251               }
4252               $duration = $this->default_visit_appt_duration * 60;
4253             }
4254           }
4255
4256           $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4257           $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4258           $date = $dtstartstr . "-" . $dtendstr;
4259           $description = "$appt_name : $phone";
4260           
4261           if(($uid == 0) && ($appt_name != "")) { 
4262             // Create a new calendar item for this appointment, since this must be the first time we
4263             // are sending it out.
4264             print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4265             $uid = rand() . rand(); // Generate a random identifier for this appointment
4266             $subject = "Created: $appt_name";
4267             
4268             $this->db->query("UPDATE eq_appointment set" .
4269                              " uid=" . $uid . 
4270                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4271
4272             $action = "PUBLISH";
4273             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4274                                   $dtend, $date, $location, $appt_name, $description, $uid);
4275             
4276           } else if(($uid != 0) && ($appt_name == "")) {
4277             // Remove the calendar item for this appointment since it has already been sent
4278             // and there is no name we have changed it to.
4279             print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4280             $subject = "Canceled: $appt_date $appt_time";
4281             
4282             $this->db->query("UPDATE eq_appointment set" .
4283                              " uid=0" . 
4284                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4285             
4286             $action = "CANCEL";
4287             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4288                                   $dtend, $date, $location, $subject, $subject, $uid);
4289             
4290           } else if($uid != 0) {
4291             // Update the existing appointment since we have changed it
4292             print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4293
4294             $subject = "Canceled: $appt_date $appt_time";
4295             $action = "CANCEL";
4296             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4297                                   $dtend, $date, $location, $subject, $subject, $uid);
4298             
4299             $uid = rand() . rand(); // Generate a random identifier for this appointment
4300             $this->db->query("UPDATE eq_appointment set" .
4301                              " uid=" . $uid .
4302                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4303             
4304             $subject = "Updated: $appt_name";       
4305             $action = "PUBLISH";
4306             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4307                                   $dtend, $date, $location, $appt_name, $description, $uid);
4308           }
4309           
4310         }
4311           
4312       return true;
4313     }
4314
4315   function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4316     {
4317       // Initialize our local variables
4318       $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4319       $message = "";
4320       $headers = "";
4321
4322       // Form the headers for the email message
4323       $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4324       $headers.="Mime-Version: 1.0\n";
4325       $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4326       $headers.="Content-Disposition: inline\n";
4327       $headers.="Reply-To: $from\n";
4328       $headers.="From: $from\n";
4329
4330       // Print the plaintext version of the appointment
4331       $message.="--$boundary\n";
4332       $message.="Content-Type: text/plain; charset=us-ascii\n";
4333       $message.="Content-Disposition: inline\n";
4334       $message.="\n";
4335       $message.="What: $description\n";
4336       $message.="When: $date\n";
4337       $message.="Where: $location\n";
4338       $message.="\n";
4339       
4340       // Print the .ics attachment version of the appointment
4341       $message.="--$boundary\n";
4342       $message.="Content-Type: text/calendar; charset=us-ascii\n";
4343       $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4344       $message.="\n";
4345       $message.="BEGIN:VCALENDAR" . "\n";
4346       $message.="VERSION:2.0" . "\n";
4347       $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4348       $message.="METHOD:$action" . "\n";
4349       $message.="BEGIN:VEVENT" . "\n";
4350       $message.="ORGANIZER:MAILTO:$from". "\n";
4351       $message.="DTSTAMP:$dtstamp" . "\n";
4352       $message.="DTSTART:$dtstart" . "\n";
4353       $message.="DTEND:$dtend" . "\n";
4354       $message.="SUMMARY:$summary" . "\n";
4355       $message.="DESCRIPTION:$description" . "\n";
4356       $message.="LOCATION:$location" . "\n";
4357       $message.="UID:$uid" ."\n";
4358       $message.="TRANSP:OPAQUE" . "\n";
4359       $message.="SEQUENCE:0" . "\n";
4360       $message.="CLASS:PUBLIC" . "\n";
4361       $message.="END:VEVENT" . "\n";
4362       $message.="END:VCALENDAR" . "\n";
4363
4364       // Complete the message
4365       $message.="--$boundary\n";
4366
4367       // Send the message
4368       mail($to, $subject, $message, $headers);
4369       
4370     }
4371
4372   function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4373     {
4374       $form_data = "";
4375       $blank = 0;
4376       
4377       if($hour == 0) { $blank = 1; }
4378
4379       if($this->time_drop_down_lists == 1) {
4380         // Create drop down lists to get the time
4381         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4382         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4383         foreach(range(1,12) as $num) {
4384           if($hour == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4385           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4386         }
4387         $form_data.= '</select>';
4388         $form_data.= '&nbsp;:&nbsp;';
4389         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4390         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4391         $num = 0;
4392         while($num < 60) {
4393           if($num < 10) { $num = "0" . "$num"; }
4394           if($minute == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4395           if($blank == 1) { $selected = ""; }
4396           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4397           $num = $num + $this->time_drop_down_list_inc;
4398         }
4399         $form_data.= '</select>';
4400       } else {
4401         // Use free form text fields to get the time
4402         if($blank == 1) { $hour = ""; $minute = ""; $ampm = ""; }
4403         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4404         $form_data.= ':';
4405         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4406         $form_data.= '&nbsp;';
4407       }
4408       // Always use a drop-down select form for am/pm
4409       $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4410       if($blank == 0) { 
4411         if($pm == 0) { $form_data.= '<option value=0 selected>am</option>'; $form_data.= '<option value=1>pm</option>'; }
4412         if($pm == 1) { $form_data.= '<option value=0>am</option>'; $form_data.= '<option value=1 selected>pm</option>'; }
4413       } else {
4414         $form_data.= '<option value=""></option>';
4415         $form_data.= '<option value=0>am</option>';
4416         $form_data.= '<option value=1>pm</option>';
4417       }
4418       $form_data.= '</select>';
4419       
4420       return $form_data;
4421     }
4422 }
4423
4424 ?>