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