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