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