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