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