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