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