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