850129bf5ea7f2df04d6b382ba1a6d9794f188ff
[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       $sql = "SELECT * FROM eq_elder where valid=1";
1100       $this->db->query($sql,__LINE__,__FILE__);
1101       $i=0;
1102       while ($this->db->next_record())
1103         {
1104           $elder_name[$i] = $this->db->f('name');
1105           $elder_id[$i] = $this->db->f('elder');
1106           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1107           $i++;
1108         }
1109       array_multisort($elder_name, $elder_id);
1110
1111       $sql = "SELECT * FROM eq_assignment ORDER BY name ASC";
1112       $this->db->query($sql,__LINE__,__FILE__);
1113       $i=0;
1114       while($this->db->next_record())
1115         {
1116           $assignment_list[$i]['assignment'] = $this->db->f('assignment');
1117           $assignment_list[$i]['name'] = $this->db->f('name');
1118           $assignment_list[$i]['code'] = $this->db->f('code');
1119           $i++;
1120         }
1121
1122       $sql = "SELECT * FROM eq_activity ORDER BY date DESC";
1123       $this->db->query($sql,__LINE__,__FILE__);
1124       $total_records = $this->db->num_rows();
1125
1126       $i = 0;
1127       while ($this->db->next_record())
1128         {
1129           $activity_list[$i]['assignment'] = $this->db->f('assignment');
1130           $activity_list[$i]['date'] = $this->db->f('date');
1131           $activity_list[$i]['activity']  = $this->db->f('activity');
1132           $i++;
1133         }
1134       
1135       $elder_width=275; $willing_width=40; $assignment_width=50;
1136       $total_width=$elder_width+$willing_width;
1137       
1138       for ($i=0; $i < count($assignment_list); $i++) {
1139         $this->t->set_var('assignment_name',$assignment_list[$i]['name']);
1140         $this->t->set_var('assignment_code',$assignment_list[$i]['code']);
1141         $this->t->fp('list1','header_list',True);
1142         $total_width += $assignment_width;
1143         $total_willing[$i] = 0;
1144       }
1145
1146       for ($i=0; $i < count($elder_id); $i++) {
1147         $willing_table = ''; 
1148         $this->nextmatchs->template_alternate_row_color(&$this->t);
1149         $this->t->set_var('elder_name',$elder_name[$i]);
1150         $this->t->set_var('elder_phone',$elder_phone[$elder_id[$i]]);
1151         $this->t->set_var('editurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_update&elder_id='
1152                                                             . $elder_id[$i] . '&action=' . 'edit'));
1153         for ($j=0; $j < count($assignment_list); $j++) {
1154           $found_willingness=0;
1155           $sql = "SELECT * FROM eq_willingness where "
1156              . " assignment=" . $assignment_list[$j]['assignment']
1157              . " AND elder=" . $elder_id[$i];
1158           $this->db->query($sql,__LINE__,__FILE__);
1159           while($this->db->next_record()) {
1160             $found_willingness=1;
1161             $date_part="";
1162             $sql = "SELECT * FROM eq_activity where "
1163                . " assignment=". $assignment_list[$j]['assignment']
1164                . " ORDER by date DESC";
1165             $this->db2->query($sql,__LINE__,__FILE__);
1166             if($this->db2->next_record()) {
1167               $activity = $this->db2->f('activity');
1168               $date = $this->db2->f('date');
1169               $sql = "SELECT * FROM eq_participation where "
1170                  . " activity=" . $activity
1171                  . " AND elder=". $elder_id[$i];
1172               $this->db3->query($sql,__LINE__,__FILE__);
1173               if($this->db3->next_record()) {
1174                 $date_part = $date;
1175               } 
1176             }
1177               
1178             if($this->db->f('willing') == 'y') {
1179               $total_willing[$j]++;
1180               $willing_table .= '<td align=center><img src="checkmark.gif"><br><font size=-2>'.$date_part.'</font></td></td>';
1181             }
1182             else if($this->db->f('willing') == 'n') {
1183               $willing_table .= '<td align=center><img src="x.gif"></td>';
1184             }
1185             else {
1186               $willing_table .= "<td>&nbsp;</td>";
1187             }
1188           }
1189           if(!$found_willingness) {
1190             $willing_table .= "<td>&nbsp;</td>";
1191           }
1192         }
1193         $this->t->set_var('willing_table',$willing_table);
1194         $this->t->fp('list2','elder_list',True);
1195       }
1196
1197       $stat_table = '<td><b>Total Willing to Serve</b></td>';
1198       for ($j=0; $j < count($assignment_list); $j++) {
1199         $stat_table .= "<td align=center><b>".$total_willing[$j]."</b></td>";
1200       }
1201       $this->t->set_var('stat_table',$stat_table);
1202       
1203       $this->t->set_var('total_width',$total_width);
1204       $this->t->set_var('elder_width',$elder_width);
1205       $this->t->set_var('willing_width',$willing_width);
1206       $this->t->pfp('out','willing_view_t');
1207       $this->save_sessiondata(); 
1208     }
1209     
1210   function willing_update()
1211     {
1212       //print "<font color=red>Willingness Update Under Constrcution</font>";
1213       //$this->willing_view();
1214       //return false;
1215       
1216       $this->t->set_file(array('willing_update_t' => 'willing_update.tpl'));
1217       $this->t->set_block('willing_update_t','assignment_list','list');
1218       $this->t->set_block('willing_update_t','save','savehandle');
1219       
1220       $elder_id = get_var('elder_id',array('GET','POST'));
1221       $this->t->set_var('elder_id',$elder_id);
1222       $action = get_var('action',array('GET','POST'));
1223       
1224       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_view'));
1225       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.willing_update&action=save'));
1226       $this->t->set_var('lang_done','Cancel');
1227       $this->t->set_var('title','Willingness Update ');
1228       
1229       if($action == 'save')
1230         {
1231           // Delete all the previous willingness entries for this elder
1232           $this->db->query("DELETE from eq_willingness where elder=" . $elder_id ,__LINE__,__FILE__);
1233               
1234           // Now, add the assignment willingness that is checked for this elder
1235           $new_data = get_var('willingness',array('POST'));
1236           foreach ($new_data as $data)
1237             {
1238               $data_array = explode("/",$data);
1239               $assignment = $data_array[0];
1240               $willing = $data_array[1];
1241               //print "elder_id: $elder_id assignment: $assignment willing: $willing<br>";
1242               $this->db->query("INSERT INTO eq_willingness (elder,assignment,willing) "
1243                                . "VALUES (" . $elder_id .",". $assignment .",'". $willing . "')",__LINE__,__FILE__);
1244             }      
1245           $this->willing_view();
1246           return false;
1247         }
1248       
1249       $assignment_width=300; $willing_width=25; $table_width=$assignment_width + $willing_width;
1250       $table_data=""; 
1251
1252       // Find out the elder's name
1253       $sql = "SELECT * FROM eq_elder WHERE elder=".$elder_id." AND valid=1";
1254       $this->db->query($sql,__LINE__,__FILE__);
1255       if($this->db->next_record()) {
1256         $elder_name = $this->db->f('name');
1257         $this->t->set_var('elder_name',$elder_name);
1258       }
1259       
1260       // Select all the assignments
1261       $sql = "SELECT * FROM eq_assignment ORDER by name ASC";
1262       $this->db->query($sql,__LINE__,__FILE__);
1263       
1264       while ($this->db->next_record())
1265         {
1266           $assignment = $this->db->f('assignment');
1267           $assignment_name = $this->db->f('name');
1268           $assignment_code = $this->db->f('code');
1269
1270           $this->nextmatchs->template_alternate_row_color(&$this->t);
1271           $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$assignment_name</td>";
1272           
1273           $header_row="<th width=$comp_width><font size=-2>Assignments</th><th>Willingness</th>";
1274           $sql = "SELECT * FROM eq_willingness WHERE elder=".$elder_id." AND assignment=".$assignment;
1275           $this->db2->query($sql,__LINE__,__FILE__);
1276           $value = $assignment;
1277              
1278           if($this->db2->next_record()) {
1279               if($this->db2->f('willing') == 'y') {
1280                 $table_data .= '<td width=100 align=center>';
1281                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y" checked>Y';
1282                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1283                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/"> ';
1284                 $table_data .= '</td>';
1285               } else if($this->db2->f('willing') == 'n') {
1286                 $table_data .= '<td width=100 align=center>';
1287                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1288                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n" checked>N';
1289                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/">';
1290                 $table_data .= '</td>';
1291               } else {
1292                 $table_data .= '<td width=100 align=center>';
1293                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1294                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1295                 $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/" checked> ';
1296                 $table_data .= '</td>';
1297               }
1298             }
1299           else {
1300             $table_data .= '<td width=100 align=center>';
1301             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/y">Y';
1302             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/n">N';
1303             $table_data .= '<input type="radio" name="willingness['.$assignment.']" value="'.$value.'/" checked> ';
1304             $table_data .= '</td>';
1305           }
1306           
1307           $table_data .= "\n";
1308           $table_data .= "</tr>"; 
1309           $table_data .= "<tr><td colspan=20></td></tr>";
1310         }
1311       
1312       $table_data .= "<tr><td colspan=20><hr></td></tr>";
1313       
1314       $this->t->set_var('table_width',$table_width);
1315       $this->t->set_var('header_row',$header_row);
1316       $this->t->set_var('table_data',$table_data);
1317       $this->t->fp('list','assignment_list',True);
1318
1319       $this->t->set_var('lang_reset','Clear Form');
1320       $this->t->set_var('lang_save','Save Changes');
1321       $this->t->set_var('savehandle','');
1322       
1323       $this->t->pfp('out','willing_update_t');
1324       $this->t->pfp('addhandle','save');
1325       
1326       $this->save_sessiondata();
1327     }
1328     
1329     
1330     function ppi_sched()
1331       {
1332       $this->t->set_file(array('ppi_sched_t' => 'ppi_sched.tpl'));
1333       $this->t->set_block('ppi_sched_t','elder_list','elderlist');
1334       $this->t->set_block('ppi_sched_t','appt_list','apptlist');
1335       $action = get_var('action',array('GET','POST'));
1336
1337       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1338       $this->t->set_var('lang_reset','Clear Changes');
1339       
1340       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
1341       $this->t->set_var('ppi_link_title','Yearly PPIs');
1342       
1343       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
1344       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
1345
1346       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched&action=save'));
1347       $this->t->set_var('title','Yearly PPI Scheduler');
1348
1349       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
1350       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
1351       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1352       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1353       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1354       $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
1355       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1356       $table_data=""; $completed_data=""; $totals_data="";
1357
1358       $year = date('Y');
1359       
1360       if($action == 'save')
1361         {
1362           // Save any changes made to the appointment table
1363           $new_data = get_var('appt_notes',array('POST'));
1364           foreach ($new_data as $entry)
1365            {
1366              $elder = $entry['elder'];
1367              $appointment = $entry['appointment'];
1368
1369              //print "elder: $elder appointment: $appointment <br>";
1370              
1371              //Only perform a database update if we have made a change to this appointment
1372              $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and elder='$elder'";
1373              $this->db->query($sql,__LINE__,__FILE__);
1374              if(!$this->db->next_record()) {
1375                // Perform database save actions here
1376                $this->db->query("UPDATE eq_appointment set " .
1377                                 " elder='" . $elder . "'" .
1378                                 " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1379                // Email the appointment
1380                $this->email_appt($appointment);
1381              }
1382
1383            }
1384           
1385           // Save any changes made to the ppi notes table
1386           $new_data = get_var('ppi_notes',array('POST'));
1387           foreach ($new_data as $entry)
1388            {
1389              $ppi_notes = $entry['notes'];
1390              $elder_id = $entry['elder_id'];
1391              $ppi_pri = $entry['pri'];
1392              
1393              // Perform database save actions here
1394              $this->db->query("UPDATE eq_elder set " .
1395                               " ppi_notes='" . $ppi_notes . "'" .
1396                               ",ppi_pri='" . $ppi_pri . "'" .
1397                               " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1398              
1399            }
1400
1401           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched');
1402           Header('Location: ' . $take_me_to_url);
1403         }
1404
1405       // Get the EQ President
1406       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
1407       $this->db->query($sql,__LINE__,__FILE__);
1408       if($this->db->next_record()) {
1409         $president_name = $this->db->f('name');
1410         $president_id = $this->db->f('elder');
1411         $presidency_id = $this->db->f('presidency');
1412         $district_number = '*';
1413         $district_name = $president_name;
1414       } else {
1415         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
1416         return;
1417       }
1418       
1419       // create the elder id -> elder name mapping
1420       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1421       $this->db->query($sql,__LINE__,__FILE__);
1422       $i=0;
1423       $elder_id = NULL;
1424       $elder_name = NULL;
1425       while ($this->db->next_record())
1426         {
1427           $elder_name[$i] = $this->db->f('name');
1428           $elder_id[$i] = $this->db->f('elder');
1429           $i++;
1430         }
1431       array_multisort($elder_name, $elder_id);
1432
1433       // APPOINTMENT TABLE
1434       $date_width=150; $time_width=100; $elder_width=200;
1435       $appt_table_width=$date_width + $time_width + $elder_width;
1436       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1437       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1438       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1439       $appt_table_data = ""; 
1440
1441       $total_elders=0; $elders_with_yearly_ppi=0;
1442       
1443       // Display a scheduling table for the EQ President
1444       $table_data=""; $appt_table_data="";
1445       $table_title = "District ".$district_number.": ".$district_name.": All Elders with Yearly PPI Not Completed";
1446       $appt_table_title = "District ".$district_number.": ".$district_name.": Yearly PPI Appointment Slots";
1447       $this->t->set_var('table_title',$table_title);
1448       $this->t->set_var('appt_table_title',$appt_table_title);
1449       
1450       // query the database for all the appointments
1451       $sql = "SELECT * FROM eq_appointment where presidency=".$presidency_id." and date>=CURDATE() ORDER BY date ASC, time ASC";
1452       $this->db->query($sql,__LINE__,__FILE__);
1453         
1454       while ($this->db->next_record())
1455         {
1456           $appointment = $this->db->f('appointment');
1457           $elder = $this->db->f('elder');
1458           
1459           $date = $this->db->f('date');
1460           $date_array = explode("-",$date);
1461           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1462           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1463           
1464           $time = $this->db->f('time');
1465           $time_array = explode(":",$time);
1466           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1467           
1468           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1469           $appt_table_data.= "<td align=center>$day_string</td>";
1470           $appt_table_data.= "<td align=center>$time_string</td>";
1471
1472           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1473           $appt_table_data.= '<option value=0></option>';
1474           for ($i=0; $i < count($elder_id); $i++) {
1475             $id = $elder_id[$i];
1476             $name = $elder_name[$i];
1477             if($elder_id[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1478             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1479           }
1480           $appt_table_data.='</select></td>';
1481
1482           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1483           
1484           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1485           $this->t->set_var('tr_color',$tr_color);
1486         }
1487
1488       $this->t->set_var('appt_table_data',$appt_table_data);
1489       $this->t->set_var('appt_header_row',$appt_header_row);
1490       $this->t->set_var('appt_table_width',$appt_table_width);
1491
1492       // PPI SCHEDULING TABLE
1493       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY ppi_pri ASC";
1494       $this->db->query($sql,__LINE__,__FILE__);
1495       
1496       $i=0; 
1497       $elder_id = NULL;
1498       while ($this->db->next_record())
1499         {
1500           $elder_id[$i] = $this->db->f('elder');
1501           $elder_name[$i] = $this->db->f('name');
1502           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
1503           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
1504           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
1505           $i++;
1506           $total_elders++;
1507         }
1508
1509       $max = count($elder_id);
1510       
1511       for($i=0; $i < $max; $i++) {
1512           $id = $elder_id[$i];
1513           $name = $elder_name[$i];
1514           $phone = $elder_phone[$id];
1515           $ppi_pri = $elder_ppi_pri[$id];
1516           $ppi_notes = $elder_ppi_notes[$id];
1517
1518           // If this elder has had a yearly PPI this year, don't show him on the schedule list
1519           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1520           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
1521              "AND elder=" . $id;
1522           $this->db2->query($sql,__LINE__,__FILE__);
1523           
1524           if(!$this->db2->next_record()) {
1525             $sql = "SELECT * FROM eq_ppi WHERE elder=" . $id . " ORDER BY date DESC";
1526             $this->db->query($sql,__LINE__,__FILE__);
1527             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
1528             $link_data['menuaction'] = 'eq.eq.ppi_update';
1529             $link_data['elder'] = $id;
1530             $link_data['name'] = $name;
1531             $link_data['ppi'] = '';
1532             $link_data['eqpresppi'] = 1;
1533             $link_data['action'] = 'add';
1534             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1535             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1536             $this->t->set_var('tr_color',$tr_color);
1537             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1538             $table_data.= "<td align=center>$phone</td>";
1539             //$table_data.= "<td align=center>$ppi_pri</td>";
1540             $table_data.= "<td align=center>";
1541             $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
1542             foreach(range(0,6) as $num) {
1543               if($num == 0) { $num = 1; } else {$num = $num*5; }
1544               if($ppi_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1545               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1546             }
1547             $table_data.= '</select></td>';
1548             $table_data.= "<td align=center>$date</td>";
1549             $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
1550             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_id]" value="'.$id.'">';
1551             $table_data.= '<input type=hidden name="ppi_notes['.$i.'][elder_name]" value="'.$name.'">';
1552             $table_data.= '</td>';
1553             $table_data.= '</tr>';
1554           } else {
1555             $link_data['menuaction'] = 'eq.eq.ppi_update';
1556             $link_data['interviewer'] = $this->db2->f('interviewer');
1557             $link_data['elder'] = $this->db2->f('elder');
1558             $link_data['name'] = $name;
1559             $link_data['ppi'] = $this->db2->f('ppi');
1560             $link_data['eqpresppi'] = $this->db2->f('eqpresppi');
1561             $link_data['action'] = 'view';
1562             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1563             $elders_with_yearly_ppi++;
1564             $date = $this->db2->f('date');
1565             $ppi_notes = $this->db2->f('notes');
1566             if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
1567             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1568             $this->t->set_var('tr_color2',$tr_color2);
1569             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1570             $completed_data.= "<td align=center>$phone</td>";
1571             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1572             $completed_data.= "<td align=left>$ppi_notes</td>";
1573             $completed_data.= '</tr>';
1574           }
1575       } // End for Elders Loop
1576
1577       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1578       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1579       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1580       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1581       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1582       $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1583             
1584       $this->t->set_var('table_width',$table_width);
1585       $this->t->set_var('header_row',$header_row);
1586       $this->t->set_var('table_data',$table_data);
1587       $this->t->set_var('completed_header_row',$completed_header_row);
1588       $this->t->set_var('completed_table_width',$completed_table_width);
1589       $this->t->set_var('completed',$completed_data);
1590       $this->t->fp('elderlist','elder_list',True); 
1591       
1592       $elders_width=300; $totals_width=100;
1593       $totals_table_width=$elders_width + $totals_width;
1594       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1595       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1596       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1597       $totals_data.= "<td align=left><font size=-2><b>Total Elders with yearly PPIs completed:</b></font></td>";
1598       $totals_data.= "<td align=center><font size=-2><b>$elders_with_yearly_ppi / $total_elders</b></font></td>";
1599       $percent = ceil(($elders_with_yearly_ppi / $total_elders)*100);
1600       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1601       $this->t->set_var('tr_color',$tr_color);
1602       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1603       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1604       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1605       $totals_data.= "</tr>";
1606
1607       $this->t->set_var('totals',$totals_data);
1608       $this->t->set_var('totals_header_row',$totals_header_row);
1609       $this->t->set_var('totals_table_width',$totals_table_width);
1610       
1611       $this->t->pfp('out','ppi_sched_t');
1612       $this->save_sessiondata(); 
1613       
1614     }
1615   
1616   function int_sched()
1617     {
1618       $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1619       $this->t->set_block('int_sched_t','elder_list','elderlist');
1620       $this->t->set_block('int_sched_t','appt_list','apptlist');
1621       $action = get_var('action',array('GET','POST'));
1622
1623       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1624       $this->t->set_var('lang_reset','Clear Changes');
1625       
1626       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
1627       $this->t->set_var('int_link_title','Hometeaching Interviews');
1628       
1629       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
1630       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
1631
1632       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched&action=save'));
1633       $this->t->set_var('title','Hometeaching Interviews Scheduler');
1634
1635       $elder_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
1636       $table_width=$elder_width + $phone_width + $pri_width + $notes_width + $int_date_width;
1637       $header_row = "<th width=$elder_width><font size=-2>Elder Name</th>";
1638       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1639       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1640       $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
1641       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1642       $table_data=""; $completed_data=""; $totals_data="";
1643
1644       $year = date('Y');
1645       $month = date('m');
1646       $nextyear = $year + 1;
1647       if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
1648       if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
1649       if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
1650       if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
1651       //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
1652       
1653       if($action == 'save')
1654         {
1655           // Save any changes made to the appointment table
1656           $new_data = get_var('appt_notes',array('POST'));
1657           foreach ($new_data as $entry)
1658            {
1659              $elder = $entry['elder'];
1660              $appointment = $entry['appointment'];
1661
1662              //print "elder: $elder appointment: $appointment <br>";
1663              //Only perform a database update if we have made a change to this appointment
1664              $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and elder='$elder'";
1665              $this->db->query($sql,__LINE__,__FILE__);
1666              if(!$this->db->next_record()) {
1667                // Perform database save actions here
1668                $this->db->query("UPDATE eq_appointment set " .
1669                                 " elder='" . $elder . "'" .
1670                                 " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1671                
1672                // Email the appointment
1673                $this->email_appt($appointment);
1674              }
1675            }
1676           
1677           // Save any changes made to the int notes table
1678           $new_data = get_var('int_notes',array('POST'));
1679           foreach ($new_data as $entry)
1680            {
1681              $int_notes = $entry['notes'];
1682              $elder_id = $entry['elder_id'];
1683              $elder_name = $entry['elder_name'];
1684              $int_pri = $entry['pri'];
1685              $aaronic = $entry['aaronic'];
1686              //print "int_notes: $int_notes elder_name: $elder_name aaronic: $aaronic <Br>";
1687              if($aaronic == 0) { 
1688                // Perform database save actions here
1689                $this->db->query("UPDATE eq_elder set " .
1690                                 " int_notes='" . $int_notes . "'" .
1691                                 ",int_pri='" . $int_pri . "'" .
1692                                 " WHERE elder=" . $elder_id,__LINE__,__FILE__);
1693              } 
1694              
1695            }
1696
1697           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched');
1698           Header('Location: ' . $take_me_to_url);
1699         }
1700
1701       // Get the Districts
1702       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
1703       $this->db->query($sql,__LINE__,__FILE__);
1704       $i=0;
1705       while ($this->db->next_record())
1706         {
1707           $district = $this->db->f('district');
1708           $districts[$i]['district'] = $this->db->f('district');
1709           $districts[$i]['name'] = $this->db->f('name');
1710           $districts[$i]['supervisor'] = $this->db->f('supervisor');
1711           $sql = "SELECT * FROM eq_presidency where district=$district and valid=1";
1712           $this->db2->query($sql,__LINE__,__FILE__);
1713           if($this->db2->next_record()) {
1714             $districts[$i]['presidency'] = $this->db2->f('presidency');
1715           }
1716           $i++;
1717         }
1718       
1719       // create the elder id -> elder name mapping
1720       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY name ASC";
1721       $this->db->query($sql,__LINE__,__FILE__);
1722       $i=0;
1723       $elder_id_data = NULL;
1724       $elder_name_data = NULL;
1725       while ($this->db->next_record())
1726         {
1727           $elder_name_data[$i] = $this->db->f('name');
1728           $elder_id_data[$i] = $this->db->f('elder');
1729           $i++;
1730         }
1731       array_multisort($elder_name_data, $elder_id_data);
1732
1733       // APPOINTMENT TABLE
1734       $district = 1;
1735       $date_width=150; $time_width=100; $elder_width=200;
1736       $appt_table_width=$date_width + $time_width + $elder_width;
1737       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1738       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1739       $appt_header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
1740       $appt_table_data = ""; 
1741
1742       $total_comps=0; $comps_with_quarterly_int=0;
1743       
1744       // Display a scheduling table for each district
1745       for ($d=0; $d < count($districts); $d++) {
1746       $table_data=""; $appt_table_data="";
1747       $this->t->set_var('district_number',$districts[$d]['district']);
1748       $this->t->set_var('district_name',$districts[$d]['name']);        
1749       $supervisor = $districts[$d]['supervisor'];
1750       $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Elders with Interviews Not Completed";
1751       $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
1752       $this->t->set_var('table_title',$table_title);
1753       $this->t->set_var('appt_table_title',$appt_table_title);
1754       
1755       // query the database for all the appointments
1756       $sql = "SELECT * FROM eq_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1757       $this->db->query($sql,__LINE__,__FILE__);
1758         
1759       while ($this->db->next_record())
1760         {
1761           $appointment = $this->db->f('appointment');
1762           $elder = $this->db->f('elder');
1763           
1764           $date = $this->db->f('date');
1765           $date_array = explode("-",$date);
1766           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1767           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1768           
1769           $time = $this->db->f('time');
1770           $time_array = explode(":",$time);
1771           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1772           
1773           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1774           $appt_table_data.= "<td align=center>$day_string</td>";
1775           $appt_table_data.= "<td align=center>$time_string</td>";
1776
1777           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][elder]>';
1778           $appt_table_data.= '<option value=0></option>';
1779           for ($i=0; $i < count($elder_id_data); $i++) {
1780             $id = $elder_id_data[$i];
1781             $name = $elder_name_data[$i];
1782             if($elder_id_data[$i] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
1783             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1784           }
1785           $appt_table_data.='</select></td>';
1786
1787           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1788           
1789           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1790           $this->t->set_var('tr_color',$tr_color);
1791         }
1792
1793       $this->t->set_var('appt_table_data',$appt_table_data);
1794       $this->t->set_var('appt_header_row',$appt_header_row);
1795       $this->t->set_var('appt_table_width',$appt_table_width);
1796
1797       // INTERVIEW SCHEDULING TABLE
1798       
1799       // Select all the unique companionship numbers for this district
1800       $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$d]['district'];
1801       $this->db->query($sql,__LINE__,__FILE__);
1802       $j=0;
1803       while ($this->db->next_record())
1804         {
1805           $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1806           $j++;
1807         }
1808
1809       $i=0;
1810       for ($j=0; $j < count($unique_companionships); $j++) {
1811         // Select all the companions from each companionship
1812         $sql = "SELECT * FROM eq_companionship where valid=1 and ".
1813            "companionship=". $unique_companionships[$j]['companionship'];
1814         $this->db->query($sql,__LINE__,__FILE__);
1815         $k=0; $int_completed=0;
1816         $comp = $unique_companionships[$j]['companionship'];
1817         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1818         $this->t->set_var('tr_color',$tr_color);
1819         $total_comps++;
1820         while ($this->db->next_record())
1821             {         
1822               // Get this companions information
1823               $elder_id = $this->db->f('elder');
1824               $aaronic_id = $this->db->f('aaronic');
1825              
1826               $sql = "SELECT * FROM eq_elder where elder=$elder_id";
1827               $this->db2->query($sql,__LINE__,__FILE__);        
1828               if($this->db2->next_record())
1829                 {
1830                   $elder_id = $this->db2->f('elder');
1831                   $elder_name = $this->db2->f('name');
1832                   $elder_phone[$elder_id] = $this->db2->f('phone');
1833                   $elder_int_pri[$elder_id] = $this->db2->f('int_pri');
1834                   $elder_int_notes[$elder_id] = $this->db2->f('int_notes');
1835                   $elder_aaronic = 0;
1836                 }
1837               else {
1838                 $sql = "SELECT * FROM eq_aaronic where aaronic=$aaronic_id";
1839                 $this->db2->query($sql,__LINE__,__FILE__);
1840                 if($this->db2->next_record())
1841                   {
1842                     $elder_id = $this->db2->f('aaronic');
1843                     $elder_name = $this->db2->f('name');
1844                     $elder_phone[$elder_id] = $this->db2->f('phone');
1845                     $elder_aaronic = 1;
1846                   }
1847               }
1848
1849               $id = $elder_id;
1850               $name = $elder_name;
1851               $phone = $elder_phone[$id];
1852               $int_pri = $elder_int_pri[$id];
1853               $int_notes = $elder_int_notes[$id];
1854               $aaronic = $elder_aaronic;
1855               
1856               // If the companionship has already had its quarterly interview,
1857               // Skip the other companion in the companionship.
1858               if($int_completed == 1) {
1859                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1860                 $completed_data.= "<td align=center>$phone</td>";
1861                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1862                 $completed_data.= "<td align=left>$int_notes</td>";
1863                 $completed_data.= '</tr>';
1864                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1865                 $this->t->set_var('tr_color2',$tr_color2);
1866                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1867                 $this->t->set_var('tr_color',$tr_color);
1868                 continue;
1869               }
1870               
1871               // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
1872               $sql = "SELECT * FROM eq_interview WHERE date >= '$quarter_start' AND date < '$quarter_end' ".
1873                  "AND elder=" . $id;
1874               $this->db2->query($sql,__LINE__,__FILE__);
1875               
1876               if(!$this->db2->next_record()) {
1877                 $sql = "SELECT * FROM eq_interview WHERE elder=" . $id . " ORDER BY date DESC";
1878                 $this->db3->query($sql,__LINE__,__FILE__);
1879                 if($this->db3->next_record()) { $date = $this->db3->f('date'); } else { $date = ""; }
1880                 $link_data['menuaction'] = 'eq.eq.int_update';
1881                 $link_data['elder'] = $id;
1882                 $link_data['aaronic'] = 0;
1883                 $link_data['name'] = $name;
1884                 $link_data['interview'] = '';
1885                 $link_data['action'] = 'add';
1886                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
1887                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1888                 $table_data.= "<td align=center>$phone</td>";
1889                 $table_data.= "<td align=center>";
1890                 if($aaronic == 0) { 
1891                   $table_data.= '<select name=int_notes['.$i.'][pri]>';
1892                   foreach(range(0,6) as $num) {
1893                     if($num == 0) { $num = 1; } else {$num = $num*5; }
1894                     if($int_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
1895                     $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1896                   }
1897                   $table_data.= '</select></td>';
1898                 }
1899                 $table_data.= "<td align=center>$date</td>";
1900                 if($aaronic == 0) { 
1901                   $table_data.= '<td><input type=text size="50" maxlength="128" name="int_notes['.$i.'][notes]" value="'.$int_notes.'">';
1902                 }
1903                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_id]" value="'.$id.'">';
1904                 $table_data.= '<input type=hidden name="int_notes['.$i.'][elder_name]" value="'.$name.'">';
1905                 $table_data.= '<input type=hidden name="int_notes['.$i.'][aaronic]" value="'.$aaronic.'">';
1906                 $table_data.= '</td>';
1907                 $table_data.= '</tr>'."\n";
1908                 $i++;
1909               } else {
1910                 $link_data['menuaction'] = 'eq.eq.int_update';
1911                 $link_data['interviewer'] = $this->db2->f('interviewer');
1912                 $link_data['elder'] = $this->db2->f('elder');
1913                 $link_data['aaronic'] = $this->db2->f('aaronic');
1914                 $link_data['name'] = $name;
1915                 $link_data['interview'] = $this->db2->f('int');
1916                 $link_data['action'] = 'view';
1917                 $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
1918                 $comps_with_quarterly_int++;
1919                 $int_completed=1;
1920                 $date = $this->db2->f('date');
1921                 $int_notes = $this->db2->f('notes');
1922                 if(strlen($int_notes) > 40) { $int_notes = substr($int_notes,0,40) . "..."; }
1923                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1924                 $completed_data.= "<td align=center>$phone</td>";
1925                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1926                 $completed_data.= "<td align=left>$int_notes</td>";
1927                 $completed_data.= '</tr>';
1928               }
1929             }
1930       }
1931         
1932       $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1933       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1934       $completed_header_row = "<th width=$name_width><font size=-2>Elder Name</th>";
1935       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1936       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1937       $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
1938             
1939       $this->t->set_var('table_width',$table_width);
1940       $this->t->set_var('header_row',$header_row);
1941       $this->t->set_var('table_data',$table_data);
1942       $this->t->set_var('completed_header_row',$completed_header_row);
1943       $this->t->set_var('completed_table_width',$completed_table_width);
1944       $this->t->set_var('completed',$completed_data);
1945       $this->t->fp('elderlist','elder_list',True);
1946
1947       } // End for each district loop
1948
1949       
1950       $elders_width=300; $totals_width=100;
1951       $totals_table_width=$elders_width + $totals_width;
1952       $totals_header_row = "<th width=$elders_width><font size=-2>Elders</th>";
1953       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1954       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1955       $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
1956       $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
1957       $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
1958       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1959       $this->t->set_var('tr_color',$tr_color);
1960       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1961       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1962       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1963       $totals_data.= "</tr>";
1964
1965       $this->t->set_var('totals',$totals_data);
1966       $this->t->set_var('totals_header_row',$totals_header_row);
1967       $this->t->set_var('totals_table_width',$totals_table_width);
1968       
1969       $this->t->pfp('out','int_sched_t');
1970       $this->save_sessiondata(); 
1971       
1972     }
1973   
1974   function vis_sched()
1975     {
1976       $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
1977       $this->t->set_block('vis_sched_t','family_list','familylist');
1978       $this->t->set_block('vis_sched_t','appt_list','apptlist');
1979       $action = get_var('action',array('GET','POST'));
1980
1981       $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1982       $this->t->set_var('lang_reset','Clear Changes');
1983       
1984       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
1985       $this->t->set_var('vis_link_title','View Yearly Visits');
1986       
1987       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
1988       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
1989
1990       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched&action=save'));
1991       $this->t->set_var('title','EQ Presidency Yearly Visit Scheduler');
1992
1993       $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
1994       $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
1995       $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
1996       $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1997       $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1998       $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
1999       $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2000       $table_data=""; $completed_data=""; $totals_data="";
2001
2002       $year = date('Y');
2003       
2004       if($action == 'save')
2005         {
2006           // Save any changes made to the appointment table
2007           $new_data = get_var('appt_notes',array('POST'));
2008           foreach ($new_data as $entry)
2009            {
2010              $family = $entry['family'];
2011              $appointment = $entry['appointment'];
2012
2013              //Only perform a database update if we have made a change to this appointment
2014              $sql = "SELECT * FROM eq_appointment where appointment='$appointment' and family='$family'";
2015              $this->db->query($sql,__LINE__,__FILE__);
2016              if(!$this->db->next_record()) {
2017
2018                // Perform database save actions here
2019                $this->db->query("UPDATE eq_appointment set " .
2020                                 " family='" . $family . "'" .
2021                                 " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2022                
2023                // Email the appointment
2024                $this->email_appt($appointment);
2025              }
2026            }
2027           
2028           // Save any changes made to the visit notes table
2029           $new_data = get_var('vis_notes',array('POST'));
2030           foreach ($new_data as $entry)
2031            {
2032              $visit_notes = $entry['notes'];
2033              $family_id = $entry['family_id'];
2034              $visit_pri = $entry['pri'];
2035              
2036              // Perform database save actions here
2037              $this->db->query("UPDATE eq_family set " .
2038                               " visit_notes='" . $visit_notes . "'" .
2039                               ",visit_pri='" . $visit_pri . "'" .
2040                               " WHERE family=" . $family_id,__LINE__,__FILE__);
2041              
2042            }
2043
2044           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched');
2045           Header('Location: ' . $take_me_to_url);
2046         }
2047
2048       // APPOINTMENT TABLE
2049       $date_width=150; $time_width=100; $family_width=250;
2050       $appt_table_width=$date_width + $time_width + $family_width;
2051       $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2052       $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2053       $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
2054       $appt_table_data = ""; 
2055
2056       // Find out what the EQ Presidency ID is
2057       $sql = "SELECT * FROM eq_presidency where president=0 and counselor=0 and secretary=0 and valid=1";
2058       $this->db->query($sql,__LINE__,__FILE__);
2059       if($this->db->next_record()) {
2060         $presidency_name = $this->db->f('name');
2061         $presidency_id = $this->db->f('presidency');
2062       } else {
2063         print "<hr><font color=red><h3>-E- Unable to locate EQ Presidency in eq_presidency table</h3></font></hr>";
2064         return;
2065       }
2066       
2067       // create the family id -> family name mapping
2068       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
2069       $this->db->query($sql,__LINE__,__FILE__);
2070       $i=0;
2071       $family_id = NULL;
2072       while ($this->db->next_record())
2073         {
2074           $family_id[$i] = $this->db->f('family');
2075           $family_name[$i] = $this->db->f('name');
2076           $i++;
2077         }
2078       array_multisort($family_name, $family_id);
2079       
2080       // query the database for all the appointments
2081       $sql = "SELECT * FROM eq_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
2082       $this->db->query($sql,__LINE__,__FILE__);
2083
2084       while ($this->db->next_record())
2085         {
2086           $appointment = $this->db->f('appointment');
2087           $family = $this->db->f('family');
2088
2089           $date = $this->db->f('date');
2090           $date_array = explode("-",$date);
2091           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2092           $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2093           
2094           $time = $this->db->f('time');
2095           $time_array = explode(":",$time);
2096           $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2097           
2098           $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2099           $appt_table_data.= "<td align=center>$day_string</td>";
2100           $appt_table_data.= "<td align=center>$time_string</td>";
2101
2102           $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
2103           $appt_table_data.= '<option value=0></option>';
2104           for ($i=0; $i < count($family_id); $i++) {
2105             $id = $family_id[$i];
2106             $name = $family_name[$i];
2107             if($family_id[$i] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
2108             $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
2109           }
2110           $appt_table_data.='</select></td>';
2111
2112           $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2113           
2114           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2115           $this->t->set_var('tr_color',$tr_color);
2116         }
2117
2118       $this->t->set_var('appt_table_data',$appt_table_data);
2119       $this->t->set_var('appt_header_row',$appt_header_row);
2120       $this->t->set_var('appt_table_width',$appt_table_width);
2121
2122       
2123       // VISIT SCHEDULING TABLE
2124       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY visit_pri ASC";
2125       $this->db->query($sql,__LINE__,__FILE__);
2126
2127       $total_families=0; $families_with_yearly_visit=0;
2128       
2129       $i=0; 
2130       $family_id = NULL;
2131       $family_name = NULL;
2132       $family_phone = NULL;
2133       $family_visit_pri = NULL;
2134       $family_visit_notes = NULL;
2135       while ($this->db->next_record())
2136         {
2137           $family_id[$i] = $this->db->f('family');
2138           $family_name[$i] = $this->db->f('name');
2139           $family_phone[$family_id[$i]] = $family_id[$i] . " ERROR";
2140           $family_visit_pri[$family_id[$i]] = $this->db->f('visit_pri');
2141           $family_visit_notes[$family_id[$i]] = $this->db->f('visit_notes');
2142           $i++;
2143           $total_families++;
2144         }
2145
2146       $sql = "SELECT * FROM eq_parent where valid=1";
2147       $this->db->query($sql,__LINE__,__FILE__);
2148       while ($this->db->next_record())
2149         {
2150           $family = $this->db->f('family');
2151           $phone = $this->db->f('phone');
2152           $family_phone[$family] = $phone;
2153         }
2154       
2155       $max = count($family_id);
2156       
2157       for($i=0; $i < $max; $i++) {
2158           $id = $family_id[$i];
2159           $name = $family_name[$i];
2160           $phone = $family_phone[$id];
2161           $vis_pri = $family_visit_pri[$id];
2162           $vis_notes = $family_visit_notes[$id];
2163
2164           // If this family has had a yearly visit this year, don't show them on the schedule list
2165           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2166           $sql = "SELECT * FROM eq_visit WHERE date > '$year_start' AND date < '$year_end' ".
2167              "AND family=" . $id . " AND companionship=0";
2168           $this->db2->query($sql,__LINE__,__FILE__);
2169           
2170           if(!$this->db2->next_record()) {
2171             $sql = "SELECT * FROM eq_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
2172             $this->db->query($sql,__LINE__,__FILE__);
2173             if($this->db->next_record()) { $date = $this->db->f('date'); } else { $date = ""; }
2174             $link_data['menuaction'] = 'eq.eq.vis_update';
2175             $link_data['visit'] = '';
2176             $link_data['family'] = $id;
2177             $link_data['name'] = $name;
2178             $link_data['action'] = 'add';
2179             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2180             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2181             $table_data.= "<td align=center>$phone</td>";
2182             $table_data.= "<td align=center>";
2183             $table_data.= '<select name=vis_notes['.$i.'][pri]>';
2184             foreach(range(0,6) as $num) {
2185               if($num == 0) { $num = 1; } else {$num = $num*5; }
2186               if($vis_pri == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
2187               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2188             }
2189             $table_data.= '</select></td>';
2190             $table_data.= "<td align=center>$date</td>";
2191             $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$i.'][notes]" value="'.$vis_notes.'">';
2192             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_id]" value="'.$id.'">';
2193             $table_data.= '<input type=hidden name="vis_notes['.$i.'][family_name]" value="'.$name.'">';
2194             $table_data.= '</td>';
2195             $table_data.= '</tr>';
2196             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2197             $this->t->set_var('tr_color',$tr_color);
2198           } else {
2199             $link_data['menuaction'] = 'eq.eq.vis_update';
2200             $link_data['visit'] = $this->db2->f('visit');
2201             $link_data['family'] = $this->db2->f('family');
2202             $link_data['name'] = $name;
2203             $link_data['date'] = $this->db2->f('date');
2204             $link_data['action'] = 'view';
2205             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);    
2206             $families_with_yearly_visit++;
2207             $date = $this->db2->f('date');
2208             $vis_notes = $this->db2->f('notes');
2209             if(strlen($vis_notes) > 40) { $vis_notes = stripslashes(substr($vis_notes,0,40) . "..."); }
2210             $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2211             $completed_data.= "<td align=center>$phone</td>";
2212             $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2213             $completed_data.= "<td align=left>$vis_notes</td>";
2214             $completed_data.= '</tr>';
2215             $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2216             $this->t->set_var('tr_color2',$tr_color2);
2217           }
2218       }
2219
2220       $name_width=190; $phone_width=100; $date_width=100; $notes_width=300;
2221       $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2222       $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
2223       $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2224       $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2225       $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
2226       
2227       $family_width=300; $totals_width=100;
2228       $totals_table_width=$family_width + $totals_width;
2229       $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
2230       $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2231       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2232       $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
2233       $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
2234       $percent = ceil(($families_with_yearly_visit / $total_families)*100);
2235       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2236       $this->t->set_var('tr_color',$tr_color);
2237       $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2238       $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2239       $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2240       $totals_data.= "</tr>";
2241       
2242       $this->t->set_var('table_width',$table_width);
2243       $this->t->set_var('header_row',$header_row);
2244       $this->t->set_var('table_data',$table_data);
2245       $this->t->set_var('totals_header_row',$totals_header_row);
2246       $this->t->set_var('totals_table_width',$totals_table_width);
2247       $this->t->set_var('completed_header_row',$completed_header_row);
2248       $this->t->set_var('completed_table_width',$completed_table_width);
2249       $this->t->set_var('completed',$completed_data);
2250       $this->t->set_var('totals',$totals_data);
2251       $this->t->fp('familylist','family_list',True);
2252       $this->t->fp('apptlist','appt_list',True);
2253       
2254       $this->t->pfp('out','vis_sched_t');
2255       $this->save_sessiondata(); 
2256       
2257     }
2258   
2259   function ppi_view()
2260     {
2261       $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
2262       $this->t->set_block('ppi_view_t','district_list','list');
2263
2264       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2265       $num_months = get_var('num_months',array('GET','POST'));
2266       if($num_months == '') { $num_months = $this->default_ppi_num_months; }
2267       $this->t->set_var('num_months',$num_months);
2268       if($num_months == 1) { $this->t->set_var('lang_num_months','Month of History'); }
2269       else {  $this->t->set_var('lang_num_months','Months of History'); }
2270       $this->t->set_var('lang_filter','Filter');
2271       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2272             
2273       $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2274       $this->t->set_var('ppi_link_title','Yearly PPIs'); 
2275
2276       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
2277       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2278       
2279       $this->t->set_var('title','Yearly PPIs');
2280       $num_months = get_var('num_months',array('GET','POST'));
2281       if($num_months == '') { $num_months = $this->default_ppi_num_years; }
2282       $this->t->set_var('num_months',$num_months);
2283       if($num_months == 1) { $this->t->set_var('lang_num_months','Year of History'); }
2284       else { $this->t->set_var('lang_num_months','Years of History'); } 
2285
2286       $sql = "SELECT * FROM eq_presidency where president=1 and valid=1";
2287       $this->db->query($sql,__LINE__,__FILE__);
2288       if($this->db->next_record()) {
2289         $president_name = $this->db->f('name');
2290         $interviewer = $this->db->f('elder');
2291         $eqpresppi = 1;
2292       } else {
2293         print "<hr><font color=red><h3>-E- Unable to locate EQ President in eq_presidency table</h3></font></hr>";
2294         return;
2295       }
2296       $this->t->set_var('district_number','*');
2297       $this->t->set_var('district_name',$president_name);
2298
2299       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2300       $this->db->query($sql,__LINE__,__FILE__);
2301       $i=0;
2302       while ($this->db->next_record())
2303         {
2304           $elder_id[$i] = $this->db->f('elder');
2305           $elder_name[$i] = $this->db->f('name');
2306           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2307           $elder_ppi_pri[$elder_id[$i]] = $this->db->f('ppi_pri');
2308           $elder_ppi_notes[$elder_id[$i]] = $this->db->f('ppi_notes');
2309           $i++;
2310         }
2311       $total_elders=$i;
2312       array_multisort($elder_name, $elder_id);
2313       //var_dump($elder_name); print "<br><br>"; var_dump($elder_id);
2314       
2315       $header_row="<th width=$comp_width><font size=-2>Elder Name</th>";
2316         
2317       $elder_width=400; $ppi_width=75; $table_width=$elder_width + $num_months*$ppi_width;
2318       $table_data="";
2319       for($m=$num_months; $m >= 0; $m--) {
2320         $year = date('Y') - $m;
2321         $header_row .= "<th width=150><font size=-2>$year</th>"; 
2322         $ppis[$m] = 0;
2323       }
2324       
2325       for ($j=0; $j < count($elder_id); $j++) {
2326         $id = $elder_id[$j];
2327         $name = $elder_name[$j];
2328         $phone = $elder_phone[$id];
2329         
2330         $link_data['menuaction'] = 'eq.eq.ppi_update';
2331         $link_data['interviewer'] = $interviewer;
2332         $link_data['elder'] = $id;
2333         $link_data['name'] = $name;
2334         $link_data['ppi'] = '';
2335         $link_data['eqpresppi'] = $eqpresppi;
2336         $link_data['action'] = 'add';
2337         $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2338         $this->nextmatchs->template_alternate_row_color(&$this->t);
2339         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2340
2341         // Find out how many times PPIs were performed in the past $num_months for this Elder
2342         for($m=$num_months; $m >= 0; $m--) {
2343           $year = date('Y') - $m;
2344           $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2345           $sql = "SELECT * FROM eq_ppi WHERE date > '$year_start' AND date < '$year_end' ".
2346              "AND elder=" . $id;
2347           $this->db2->query($sql,__LINE__,__FILE__);
2348                   
2349           if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
2350           if($this->db2->next_record()) {
2351             $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
2352             $link_data['menuaction'] = 'eq.eq.ppi_update';
2353             $link_data['companionship'] = $companionship;
2354             $link_data['interviewer'] = $this->db2->f('interviewer');
2355             $link_data['elder'] = $id;
2356             $link_data['name'] = $name;
2357             $link_data['ppi'] = $this->db2->f('ppi');
2358             $link_data['eqpresppi'] = $eqpresppi;
2359             $link_data['action'] = 'view';
2360             $date = $this->db2->f('date');
2361             $date_array = explode("-",$date);
2362             $month = $date_array[1];
2363             $day   = $date_array[2];
2364             $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2365             $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2366           }
2367           else { $table_data .= "<td>&nbsp;</td>"; }
2368         }
2369         $table_data .= "</tr>\n"; 
2370       }
2371       $table_data .= "<tr><td colspan=20><hr></td></tr>";
2372       
2373       $stat_data = "<tr><td><b><font size=-2>$total_elders Elders<br>PPI Totals:</font></b></td>";
2374       for($m=$num_months; $m >=0; $m--) {
2375         $percent = ceil(($ppis[$m] / $total_elders)*100);
2376         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
2377       }
2378       $stat_data .= "</tr>";
2379       
2380       $this->t->set_var('table_width',$table_width);
2381       $this->t->set_var('header_row',$header_row);
2382       $this->t->set_var('table_data',$table_data);
2383       $this->t->set_var('stat_data',$stat_data);
2384       $this->t->pfp('out','ppi_view_t');
2385       $this->save_sessiondata(); 
2386     }
2387
2388   function ppi_update()
2389     {
2390       $this->t->set_file(array('form' => 'ppi_update.tpl'));
2391       $this->t->set_block('form','interviewer_list','int_list');
2392       $this->t->set_block('form','add','addhandle');
2393       $this->t->set_block('form','edit','edithandle');
2394       
2395       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_view'));
2396       $this->t->set_var('readonly','');
2397       $this->t->set_var('disabled','');
2398       
2399       $action = get_var('action',array('GET','POST'));
2400       $companionship = get_var('companionship',array('GET','POST'));
2401       $interviewer = get_var('interviewer',array('GET','POST'));      
2402       $name = get_var('name',array('GET','POST'));
2403       $ppi = get_var('ppi',array('GET','POST'));
2404       $elder = get_var('elder',array('GET','POST'));
2405       $aaronic = get_var('aaronic',array('GET','POST'));
2406       $date = get_var('date',array('GET','POST'));
2407       $notes = get_var('notes',array('GET','POST'));
2408       $eqpresppi = get_var('eqpresppi',array('GET','POST'));
2409       
2410       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2411       $this->db->query($sql,__LINE__,__FILE__);
2412       while ($this->db->next_record())
2413         {
2414           $supervisor = $this->db->f('supervisor');
2415           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2416           $this->db2->query($sql,__LINE__,__FILE__);
2417           $this->db2->next_record();
2418           $interviewer_name = $this->db2->f('name');
2419           
2420           if($supervisor == $interviewer) { 
2421             $this->t->set_var('interviewer',$supervisor . ' selected');
2422           } else {
2423             $this->t->set_var('interviewer',$interviewer);
2424           }
2425           $this->t->set_var('interviewer_name',$interviewer_name);
2426           $this->t->fp('int_list','interviewer_list',True);
2427         }
2428       
2429       if($action == 'save')
2430         {
2431           $notes = get_var('notes',array('POST'));
2432           $this->db->query("UPDATE eq_ppi set " .
2433                            "   ppi='" . $ppi . "'" .
2434                     ", interviewer='" . $interviewer . "'" .
2435                           ", elder='" . $elder . "'" .
2436                            ", date='" . $date . "'" .
2437                           ", notes='" . $notes . "'" .
2438                       ", eqpresppi='" . $eqpresppi . "'" .
2439                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2440           $this->ppi_view();
2441           return false;
2442         }
2443
2444       if($action == 'insert')
2445         {
2446           $notes = get_var('notes',array('POST'));
2447           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,date,notes,eqpresppi) "
2448                            . "VALUES ('" . $interviewer . "','" . $elder . "','"
2449                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
2450           $this->ppi_view();
2451           return false;
2452         }
2453       
2454       if($action == 'add')
2455         {
2456           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2457           $this->t->set_var('ppi', '');
2458           $this->t->set_var('interviewer', $interviewer);
2459           $this->t->set_var('name',$name);
2460           $this->t->set_var('elder',$elder);
2461           $this->t->set_var('date','');
2462           $this->t->set_var('notes','');
2463           $this->t->set_var('eqpresppi',$eqpresppi);
2464           $this->t->set_var('lang_done','Cancel');
2465           $this->t->set_var('lang_action','Adding New PPI');
2466           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2467                                                                 . $ppi . '&action=' . 'insert'));
2468         }
2469
2470       if($action == 'edit' || $action == 'view')
2471         {
2472           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2473           $this->db->query($sql,__LINE__,__FILE__);
2474           $this->db->next_record();
2475           $this->t->set_var('ppi',$ppi);
2476           $this->t->set_var('name',$name);
2477           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2478           $this->t->set_var('elder',$this->db->f('elder'));
2479           $this->t->set_var('date',$this->db->f('date'));
2480           $this->t->set_var('notes',$this->db->f('notes'));
2481           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
2482         }
2483       
2484       if($action == 'edit')
2485         {
2486           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2487           $this->t->set_var('lang_done','Cancel');
2488           $this->t->set_var('lang_action','Editing PPI');
2489           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2490                                                                 . $ppi . '&action=' . 'save'));
2491         }
2492
2493       if($action == 'view')
2494         {
2495           $date = $this->db->f('date');
2496           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2497           $this->t->set_var('readonly','READONLY');
2498           $this->t->set_var('disabled','DISABLED');
2499           $this->t->set_var('lang_done','Done');
2500           $this->t->set_var('lang_action','Viewing PPI');
2501           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2502                                                                 . $ppi . '&action=' . 'edit'));
2503         }
2504       
2505       $this->t->set_var('lang_reset','Clear Form');
2506       $this->t->set_var('lang_add','Add PPI');
2507       $this->t->set_var('lang_save','Save Changes');
2508       $this->t->set_var('edithandle','');
2509       $this->t->set_var('addhandle','');
2510
2511       $this->t->pfp('out','form');
2512       
2513       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2514       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2515       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2516      
2517       $this->save_sessiondata(); 
2518     }
2519
2520     function int_view()
2521     {
2522       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2523       $this->t->set_block('int_view_t','district_list','list');
2524
2525       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2526       $num_quarters = get_var('num_quarters',array('GET','POST'));
2527       if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2528       $this->t->set_var('num_quarters',$num_quarters);
2529       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2530       else {  $this->t->set_var('lang_num_quarters','Quarters of History'); }
2531       $this->t->set_var('lang_filter','Filter');
2532       
2533       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2534       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2535       
2536       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2537       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2538       
2539       $this->t->set_var('title','Hometeaching Interviews'); 
2540
2541       $num_months = $num_quarters * 3 - 1;
2542       $current_month = $this->current_month;
2543       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2544       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2545       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2546       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2547
2548       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2549       $this->db->query($sql,__LINE__,__FILE__);
2550       $i=0;
2551       while ($this->db->next_record())
2552         {
2553           $districts[$i]['district'] = $this->db->f('district');
2554           $districts[$i]['name'] = $this->db->f('name');
2555           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2556           $i++;
2557         }
2558
2559       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2560       $this->db->query($sql,__LINE__,__FILE__);
2561       $i=0;
2562       while ($this->db->next_record())
2563         {
2564           $elder_id[$i] = $this->db->f('elder');
2565           $elder_name[$i] = $this->db->f('name');
2566           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2567           $i++;
2568         }
2569       array_multisort($elder_name, $elder_id);
2570       for($i=0; $i < count($elder_id); $i++) {
2571           $id = $elder_id[$i];
2572           $elders[$id] = $elder_name[$i];
2573       }      
2574
2575       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2576       $this->db->query($sql,__LINE__,__FILE__);
2577       while ($this->db->next_record())
2578         {
2579           $aaronic_id = $this->db->f('aaronic');
2580           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2581           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2582         }
2583       
2584       $total_companionships = 0;
2585       $this->nextmatchs->template_alternate_row_color(&$this->t);
2586       for ($i=0; $i < count($districts); $i++) {
2587         $this->t->set_var('district_number',$districts[$i]['district']);
2588         $this->t->set_var('district_name',$districts[$i]['name']);      
2589         $supervisor = $districts[$i]['supervisor'];
2590         $unique_companionships='';
2591                 
2592         // Select all the unique companionship numbers for this district
2593         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2594
2595         $this->db->query($sql,__LINE__,__FILE__);
2596         $j=0;
2597         while ($this->db->next_record())
2598           {
2599             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2600             $j++;
2601           }
2602         
2603         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2604         $table_data=""; $num_companionships = $j; $num_elders = 0;
2605         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2606         for ($j=0; $j < count($unique_companionships); $j++) {
2607           // Select all the companions in each companionship
2608           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2609              "companionship=". $unique_companionships[$j]['companionship'];
2610           $this->db->query($sql,__LINE__,__FILE__);
2611           $k=0;
2612           $comp = $unique_companionships[$j]['companionship'];
2613           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2614           while ($this->db->next_record())
2615             {
2616               // Get this companions information
2617               $num_elders++;
2618               $companionship = $this->db->f('companionship');
2619               $elder_id = $this->db->f('elder');
2620               $aaronic_id = $this->db->f('aaronic');
2621               if($elder_id) {
2622                 $name = $elders[$elder_id];
2623                 $phone = $elder_phone[$elder_id];
2624               }
2625               else if($aaronic_id) {
2626                 $name = $aaronic[$aaronic_id]['name'];
2627                 $phone = $aaronic[$aaronic_id]['phone'];
2628               }
2629               $link_data['menuaction'] = 'eq.eq.int_update';
2630               $link_data['companionship'] = $companionship;
2631               $link_data['interviewer'] = $supervisor;
2632               $link_data['elder'] = $elder_id;
2633               $link_data['aaronic'] = $aaronic_id;
2634               $link_data['name'] = $name;
2635               $link_data['interview'] = '';
2636               $link_data['action'] = 'add';
2637               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2638               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2639
2640               // Find out how many times Interviews were performed in the past $num_months for this Elder
2641               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2642               for($m=$num_months; $m >= 0; $m--) {
2643                 $month = $current_month - $m;
2644                 $year = $this->current_year;
2645                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2646                 if($month < 10) { $month = "0"."$month"; }
2647                 $month_start = "$year"."-"."$month"."-"."01";
2648                 $month_end = "$year"."-"."$month"."-"."31";
2649                 $month = "$month"."/"."$year";
2650                 $sql = "SELECT * FROM eq_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2651                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2652                 $this->db2->query($sql,__LINE__,__FILE__);
2653                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2654               
2655                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2656                 if($this->db2->next_record()) {
2657                   if(!$int_recorded[$companionship][$m]) {
2658                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2659                   }
2660                   $link_data['menuaction'] = 'eq.eq.int_update';
2661                   $link_data['companionship'] = $companionship;
2662                   $link_data['interviewer'] = $this->db2->f('interviewer');
2663                   $link_data['elder'] = $elder_id;
2664                   $link_data['aaronic'] = $aaronic_id;
2665                   $link_data['name'] = $name;
2666                   $link_data['interview'] = $this->db2->f('interview');
2667                   $link_data['action'] = 'view';
2668                   $date = $this->db2->f('date');
2669                   $date_array = explode("-",$date);
2670                   $month = $date_array[1];
2671                   $day   = $date_array[2];
2672                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2673                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2674                 }
2675                 else { $table_data .= "<td>&nbsp;</td>"; }
2676               }
2677               $table_data .= "</tr>"; 
2678               $k++;
2679             }
2680           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2681         }
2682         $total_companionships += $num_companionships;
2683         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Quarterly Totals:</font></b></td>";
2684
2685         for($m=$num_months; $m >=0; $m--) {
2686           $month = $current_month - $m;
2687           if(($month % 3) == 1) { $quarter_total = $ints[$m]; }
2688           else { $quarter_total += $ints[$m]; }
2689           $percent = ceil(($quarter_total / $num_companionships)*100);
2690           $stat_data .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2691         }
2692         $stat_data .= "</tr>";
2693
2694         $this->t->set_var('table_width',$table_width);
2695         $this->t->set_var('header_row',$header_row);
2696         $this->t->set_var('table_data',$table_data);
2697         $this->t->set_var('stat_data',$stat_data);
2698         $this->t->fp('list','district_list',True);
2699       }
2700
2701       // Display the totals, cummulative per quarter
2702       $quarter_total = 0;
2703       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Quarterly Totals:</font></b></td>";
2704       for($m=$num_months; $m >=0; $m--) {
2705         $month = $current_month - $m;
2706         if(($month % 3) == 1) { $quarter_total = $total_ints[$m]; }
2707         else { $quarter_total += $total_ints[$m]; }
2708         $percent = ceil(($quarter_total / $total_companionships)*100);
2709         $totals .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2710       }
2711       $totals .= "</tr>";
2712       
2713       $this->t->set_var('totals',$totals);
2714       $this->t->pfp('out','int_view_t');
2715       $this->save_sessiondata(); 
2716     }
2717
2718   function int_update()
2719     {
2720       $this->t->set_file(array('form' => 'int_update.tpl'));
2721       $this->t->set_block('form','interviewer_list','int_list');
2722       $this->t->set_block('form','add','addhandle');
2723       $this->t->set_block('form','edit','edithandle');
2724       
2725       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2726       $this->t->set_var('readonly','');
2727       $this->t->set_var('disabled','');
2728       
2729       $action = get_var('action',array('GET','POST'));
2730       $companionship = get_var('companionship',array('GET','POST'));
2731       $interviewer = get_var('interviewer',array('GET','POST'));      
2732       $name = get_var('name',array('GET','POST'));
2733       $interview = get_var('interview',array('GET','POST'));
2734       $elder = get_var('elder',array('GET','POST'));
2735       $aaronic = get_var('aaronic',array('GET','POST'));
2736       $date = get_var('date',array('GET','POST'));
2737       $notes = get_var('notes',array('GET','POST'));
2738       
2739       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2740       $this->db->query($sql,__LINE__,__FILE__);
2741       while ($this->db->next_record())
2742         {
2743           $supervisor = $this->db->f('supervisor');
2744           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2745           $this->db2->query($sql,__LINE__,__FILE__);
2746           $this->db2->next_record();
2747           $interviewer_name = $this->db2->f('name');
2748           
2749           if($supervisor == $interviewer) { 
2750             $this->t->set_var('interviewer',$supervisor . ' selected');
2751           } else {
2752             $this->t->set_var('interviewer',$interviewer);
2753           }
2754           $this->t->set_var('interviewer_name',$interviewer_name);
2755           $this->t->fp('int_list','interviewer_list',True);
2756         }
2757       
2758       if($action == 'save')
2759         {
2760           $notes = get_var('notes',array('POST'));
2761           $this->db->query("UPDATE eq_interview set " .
2762                      "   interview='" . $interview . "'" .
2763                     ", interviewer='" . $interviewer . "'" .
2764                           ", elder='" . $elder . "'" .
2765                         ", aaronic='" . $aaronic . "'" .
2766                            ", date='" . $date . "'" .
2767                           ", notes='" . $notes . "'" .
2768                            " WHERE interview=" . $interview,__LINE__,__FILE__);
2769           $this->int_view();
2770           return false;
2771         }
2772
2773       if($action == 'insert')
2774         {
2775           $notes = get_var('notes',array('POST'));
2776           $this->db->query("INSERT INTO eq_interview (interviewer,elder,aaronic,date,notes) "
2777                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2778                            . $date . "','" . $notes ."')",__LINE__,__FILE__);
2779           $this->int_view();
2780           return false;
2781         }
2782       
2783       if($action == 'add')
2784         {
2785           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2786           $this->t->set_var('interview', '');
2787           $this->t->set_var('interviewer', $interviewer);
2788           $this->t->set_var('name',$name);
2789           $this->t->set_var('elder',$elder);
2790           $this->t->set_var('aaronic',$aaronic);
2791           $this->t->set_var('date','');
2792           $this->t->set_var('notes','');
2793           $this->t->set_var('lang_done','Cancel');
2794           $this->t->set_var('lang_action','Adding New Interview');
2795           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2796                                                                 . $interview . '&action=' . 'insert'));
2797         }
2798
2799       if($action == 'edit' || $action == 'view')
2800         {
2801           $sql = "SELECT * FROM eq_interview WHERE interview=".$interview;
2802           $this->db->query($sql,__LINE__,__FILE__);
2803           $this->db->next_record();
2804           $this->t->set_var('interview',$interview);
2805           $this->t->set_var('name',$name);
2806           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2807           $this->t->set_var('elder',$this->db->f('elder'));
2808           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2809           $this->t->set_var('date',$this->db->f('date'));
2810           $this->t->set_var('notes',$this->db->f('notes'));
2811         }
2812       
2813       if($action == 'edit')
2814         {
2815           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2816           $this->t->set_var('lang_done','Cancel');
2817           $this->t->set_var('lang_action','Editing Interview');
2818           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2819                                                                 . $interview . '&action=' . 'save'));
2820         }
2821
2822       if($action == 'view')
2823         {
2824           $date = $this->db->f('date');
2825           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2826           $this->t->set_var('readonly','READONLY');
2827           $this->t->set_var('disabled','DISABLED');
2828           $this->t->set_var('lang_done','Done');
2829           $this->t->set_var('lang_action','Viewing Interview');
2830           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2831                                                                 . $interview . '&action=' . 'edit'));
2832         }
2833       
2834       $this->t->set_var('lang_reset','Clear Form');
2835       $this->t->set_var('lang_add','Add Interview');
2836       $this->t->set_var('lang_save','Save Changes');
2837       $this->t->set_var('edithandle','');
2838       $this->t->set_var('addhandle','');
2839
2840       $this->t->pfp('out','form');
2841       
2842       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2843       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2844       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2845      
2846       $this->save_sessiondata(); 
2847     }
2848
2849   function vis_view()
2850     {
2851       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2852       $this->t->set_block('vis_view_t','visit_list','list1');
2853       $this->t->set_block('vis_view_t','family_list','list2');
2854
2855       $this->t->set_var('lang_name','Family Name');
2856       $this->t->set_var('lang_date','Date');
2857
2858       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2859       $this->t->set_var('vis_link_title','View Yearly Visits');
2860       
2861       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2862       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2863
2864       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2865       $num_years = get_var('num_years',array('GET','POST'));
2866       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2867       $this->t->set_var('num_years',$num_years);
2868       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2869       else {  $this->t->set_var('lang_num_years','Years of History'); }
2870       $this->t->set_var('lang_filter','Filter');
2871       
2872       $year = date('Y') - $num_years + 1;
2873       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2874       
2875       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2876       $this->db->query($sql,__LINE__,__FILE__);
2877       $total_records = $this->db->num_rows();
2878
2879       $i = 0;
2880       while ($this->db->next_record())
2881         {
2882           $visit_list[$i]['visit'] = $this->db->f('visit');
2883           $visit_list[$i]['family'] = $this->db->f('family');
2884           $visit_list[$i]['date']  = $this->db->f('date');
2885           $i++;
2886         }
2887             
2888       for ($i=0; $i < count($visit_list); $i++)
2889         {         
2890           $this->nextmatchs->template_alternate_row_color(&$this->t);
2891
2892           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2893           $this->db->query($sql,__LINE__,__FILE__);
2894           $this->db->next_record();
2895                   
2896           $this->t->set_var('family',$visit_list[$i]['family']);
2897           $this->t->set_var('family_name',$this->db->f('name'));
2898           $this->t->set_var('date',$visit_list[$i]['date']);
2899           
2900           $link_data['menuaction'] = 'eq.eq.vis_update';
2901           $link_data['visit'] = $visit_list[$i]['visit'];
2902           $link_data['name'] = $this->db->f('name');
2903           $link_data['date'] = $visit_list[$i]['date'];
2904           $link_data['action'] = 'view';
2905           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2906           $this->t->set_var('lang_view','View');
2907
2908           $link_data['menuaction'] = 'eq.eq.vis_update';
2909           $link_data['visit'] = $visit_list[$i]['visit'];
2910           $link_data['name'] = $this->db->f('name');
2911           $link_data['date'] = $visit_list[$i]['date'];
2912           $link_data['action'] = 'edit';
2913           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2914           $this->t->set_var('lang_edit','Edit');
2915
2916           $this->t->fp('list1','visit_list',True);
2917         }
2918
2919       // List the families that are available to record a visit against
2920       $sql = "SELECT * FROM eq_family WHERE valid=1";
2921       $this->db->query($sql,__LINE__,__FILE__);
2922       $total_records = $this->db->num_rows();
2923
2924       $i = 0;
2925       while ($this->db->next_record())
2926         {
2927           $family_names[$i] = $this->db->f('name');
2928           $family_ids[$i] = $this->db->f('family');
2929           $i++;
2930         } array_multisort($family_names, $family_ids);
2931       
2932       for ($i=0; $i < count($family_names); $i++)
2933         {
2934           $link_data['menuaction'] = 'eq.eq.vis_update';
2935           $link_data['visit'] = '';
2936           $link_data['family'] = $family_ids[$i];
2937           $link_data['action'] = 'add';
2938           $link_data['name'] = $family_names[$i];
2939           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2940
2941           $this->t->set_var('name',$family_names[$i]);
2942           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2943           else { $this->t->set_var('table_sep',"</td>"); }
2944           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2945
2946           $this->t->fp('list2','family_list',True);
2947         }   
2948
2949       $this->t->pfp('out','vis_view_t');
2950       $this->save_sessiondata(); 
2951     }
2952
2953   function vis_update()
2954     {
2955       $this->t->set_file(array('form' => 'vis_update.tpl'));
2956       $this->t->set_block('form','add','addhandle');
2957       $this->t->set_block('form','edit','edithandle');
2958       
2959       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2960       $this->t->set_var('readonly','');
2961       $this->t->set_var('disabled','');
2962       
2963       $action = get_var('action',array('GET','POST'));
2964       $visit = get_var('visit',array('GET','POST'));
2965       $family = get_var('family',array('GET','POST'));
2966       $name = get_var('name',array('GET','POST'));
2967       $date = get_var('date',array('GET','POST'));
2968       $notes = get_var('notes',array('GET','POST'));
2969       $companionship = 0;
2970       
2971       if($action == 'save')
2972         {
2973           $notes = get_var('notes',array('POST'));
2974           $this->db->query("UPDATE eq_visit set " .
2975                            "  date='" . $date . "'" .
2976                           ", notes='" . $notes . "'" .
2977                            " WHERE visit=" . $visit,__LINE__,__FILE__);
2978           $this->vis_view();
2979           return false;
2980         }
2981
2982       if($action == 'insert')
2983         {
2984           $notes = get_var('notes',array('POST'));
2985           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
2986                            . "VALUES ('" . $family . "','" . $companionship . "','"
2987                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
2988           $this->vis_view();
2989           return false;
2990         }
2991       
2992       if($action == 'add')
2993         {
2994           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2995           $this->t->set_var('family', $family);
2996           $this->t->set_var('visit', '');
2997           $this->t->set_var('name', $name);
2998           $this->t->set_var('date','');
2999           $this->t->set_var('notes','');
3000           $this->t->set_var('lang_done','Cancel');
3001           $this->t->set_var('lang_action','Adding New Visit');
3002           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
3003                                                                 . $family . '&action=' . 'insert'));
3004         }
3005
3006       if($action == 'edit' || $action == 'view')
3007         {
3008           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
3009           $this->db->query($sql,__LINE__,__FILE__);
3010           $this->db->next_record();
3011           $this->t->set_var('visit',$visit);
3012           $this->t->set_var('name',$name);
3013           $this->t->set_var('family', $family);
3014           $this->t->set_var('date',$this->db->f('date'));
3015           $this->t->set_var('notes',$this->db->f('notes'));
3016         }
3017       
3018       if($action == 'edit')
3019         {
3020           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3021           $this->t->set_var('lang_done','Cancel');
3022           $this->t->set_var('lang_action','Editing Visit');
3023           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3024                                                                 . $visit . '&action=' . 'save'));
3025         }
3026
3027       if($action == 'view')
3028         {
3029           $date = $this->db->f('date');
3030           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3031           $this->t->set_var('readonly','READONLY');
3032           $this->t->set_var('disabled','DISABLED');
3033           $this->t->set_var('lang_done','Done');
3034           $this->t->set_var('lang_action','Viewing Visit');
3035           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3036                                                                 . $visit . '&action=' . 'edit'));
3037         }
3038       
3039       $this->t->set_var('lang_reset','Clear Form');
3040       $this->t->set_var('lang_add','Add Visit');
3041       $this->t->set_var('lang_save','Save Changes');
3042       $this->t->set_var('edithandle','');
3043       $this->t->set_var('addhandle','');
3044
3045       $this->t->pfp('out','form');
3046       
3047       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3048       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3049       if($action == 'add') { $this->t->pfp('addhandle','add'); }
3050
3051       $this->save_sessiondata(); 
3052     }
3053
3054   function att_view()
3055     {
3056       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3057       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3058       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3059
3060       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3061       $this->t->set_block('att_view_t','act_list','list');
3062
3063       $this->t->set_block('att_view_t','month_list','list1');
3064       $this->t->set_block('att_view_t','header_list','list2');
3065       $this->t->set_block('att_view_t','elder_list','list3');
3066       
3067       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3068       $num_quarters = get_var('num_quarters',array('GET','POST'));
3069       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3070       $this->t->set_var('num_quarters',$num_quarters);
3071       $this->t->set_var('lang_filter','Filter');
3072       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
3073       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
3074
3075       $num_months = $num_quarters * 3;
3076       $current_month = $this->current_month;
3077       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3078       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3079       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3080       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3081       
3082       $sql = "SELECT * FROM eq_elder where valid=1";
3083       $this->db->query($sql,__LINE__,__FILE__);
3084       $i=0;
3085       while ($this->db->next_record())
3086         {
3087           $elder_name[$i] = $this->db->f('name');
3088           $elder_id[$i] = $this->db->f('elder');
3089           $i++;
3090         }
3091       array_multisort($elder_name, $elder_id);
3092       
3093       // Create a list of sunday dates for a window of 3 months back and current month
3094       $i=0; 
3095       $last_time = 0; 
3096       $found_sunday = 0;
3097       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
3098       $last_date = explode("-",$sunday_list[0]['date']);
3099       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3100       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
3101       while($last_time < $time_limit)
3102       {
3103         $day = date("w",$last_time);
3104         if(date("w",$last_time) == 0) {
3105           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3106           $last_date = explode("-",$sunday_list[$i]['date']);
3107           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3108           $sunday_list[$i]['day'] = $last_date[2];
3109           $sunday_list[$i]['month'] = date("M",$last_time);
3110           $sunday_list[$i]['year'] = $last_date[0];
3111           $found_sunday = 1; 
3112         }
3113         $last_time += 90000;
3114         if($found_sunday) { $i++; $found_sunday=0; }
3115       }
3116
3117       $total_elders = count($elder_id);
3118       $old_month=$sunday_list[0]['month']; $span=0;
3119       for ($i=0; $i < count($sunday_list); $i++) {
3120         $date = $sunday_list[$i]['date'];
3121         $this->t->set_var('date',$sunday_list[$i]['date']);
3122         $this->t->set_var('day',$sunday_list[$i]['day']);
3123         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3124           if($i == count($sunday_list)-1) { $span++; }
3125           $cur_month = $sunday_list[$i]['month'];
3126           $old_month = $sunday_list[$i]['month'];         
3127           $link_data['menuaction'] = 'eq.eq.att_update';
3128           $link_data['month'] = $sunday_list[$i-1]['month'];
3129           $link_data['year'] = $sunday_list[$i-1]['year'];
3130           $link_data['action'] = 'update_month';
3131           $cur_month = $sunday_list[$i-1]['month'];
3132           $cur_year = $sunday_list[$i-1]['year'];
3133           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3134           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3135           $this->t->set_var('month',$sunday_list[$i-1]['month']);
3136           $this->t->set_var('year',$sunday_list[$i-1]['year']);
3137           $this->t->set_var('span',$span); $span=0;
3138           $this->t->fp('list1','month_list',True);
3139         } $span++;
3140       }
3141       $this->t->set_var('total_elders',$total_elders);
3142       $this->t->set_var('header_row',$header_row);
3143       
3144       $elder_width=200; $att_width=25; $total_width=$elder_width; 
3145       for ($i=0; $i < count($sunday_list); $i++) {
3146         $link_data['menuaction'] = 'eq.eq.att_update';
3147         $link_data['month'] = $sunday_list[$i]['month'];
3148         $link_data['year'] = $sunday_list[$i]['year'];
3149         $link_data['day'] = $sunday_list[$i]['day'];
3150         $link_data['date'] = $sunday_list[$i]['date'];
3151         $link_data['action'] = 'update_day';
3152         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3153         $this->t->set_var('date',$sunday_list[$i]['date']);
3154         $this->t->set_var('day',$sunday_list[$i]['day']);
3155         $this->t->set_var('month',$sunday_list[$i]['month']);
3156         $this->t->set_var('year',$sunday_list[$i]['year']);
3157         $this->t->fp('list2','header_list',True);
3158         $total_width += $att_width;
3159         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3160       }
3161
3162       for ($i=0; $i < count($elder_id); $i++) {
3163         $att_table = "";
3164         $this->nextmatchs->template_alternate_row_color(&$this->t);
3165         $this->t->set_var('elder_name',$elder_name[$i]);
3166         #print "checking for elder: " . $elder_id[$i] . "<br>";
3167         for ($j=0; $j < count($sunday_list); $j++) {
3168           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3169           #print "SELECT * FROM eq_attendance WHERE date='"
3170           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
3171           $sql = "SELECT * FROM eq_attendance WHERE date='"
3172              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3173           $this->db->query($sql,__LINE__,__FILE__);
3174           if($this->db->next_record()) {
3175             $cur_month = $sunday_list[$j]['month'];
3176             if($attended[$i][$cur_month] != 1) { 
3177               $attended[$i][$cur_month]=1;
3178               $attendance[$monthnum[$cur_month]]++;
3179             } 
3180             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
3181           } else {
3182             $att_table .= '<td>&nbsp;</td>';
3183           }
3184         }
3185         $this->t->set_var('att_table',$att_table);
3186         $this->t->fp('list3','elder_list',True);
3187       }
3188       $this->t->set_var('total_width',$total_width);
3189       $this->t->set_var('elder_width',$elder_width);
3190       $this->t->set_var('att_width',$att_width);
3191
3192       # Now calculate attendance for these months
3193       $attendance_str = "";
3194       $nonattendance_str = "";
3195       $aveattendance_str = "";
3196       $avenonattendance_str = "";
3197       $num_months=0;
3198       $ave_total_attended=0;
3199       ksort($attendance);
3200       foreach($attendance as $att => $value) {
3201         $total_attended = $attendance[$att];
3202         $ave_total_attended += $attendance[$att]; $num_months++;
3203         $percent = ceil(($total_attended / $total_elders)*100);
3204         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3205         $total_nonattended = $total_elders - $total_attended;
3206         $percent = ceil(($total_nonattended / $total_elders)*100);
3207         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3208         
3209         $total_attended = ceil(($ave_total_attended / $num_months));
3210         $percent = ceil(($total_attended / $total_elders)*100);
3211         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3212         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
3213         $percent = ceil(($total_attended / $total_elders)*100);
3214         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3215       }
3216       
3217       $this->t->set_var('attendance',$attendance_str);
3218       $this->t->set_var('aveattendance',$aveattendance_str);
3219       $this->t->set_var('nonattendance',$nonattendance_str);
3220       $this->t->set_var('avenonattendance',$avenonattendance_str);
3221       
3222       $this->t->pfp('out','att_view_t');
3223       $this->save_sessiondata(); 
3224     }
3225
3226   function att_update()
3227     {
3228       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3229       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3230       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3231
3232       $this->t->set_file(array('form' => 'att_update.tpl'));
3233       $this->t->set_block('form','edit','edithandle');
3234       
3235       $this->t->set_block('form','month_list','list1');
3236       $this->t->set_block('form','header_list','list2');
3237       $this->t->set_block('form','elder_list','list3');
3238
3239       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3240
3241       $action = get_var('action',array('GET','POST'));
3242       $month = get_var('month',array('GET','POST'));
3243       $year = get_var('year',array('GET','POST'));
3244       $day = get_var('day',array('GET','POST'));
3245       $date = get_var('date',array('GET','POST'));
3246
3247       if($action == 'save_month' || $action == 'save_day')
3248         {
3249            $new_data = get_var('elders_attended',array('POST'));
3250            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3251
3252            if($action == 'save_month') {        
3253              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3254            }
3255
3256            if($action == 'save_day') {        
3257              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3258            }   
3259
3260            foreach ($new_data as $data)
3261            {
3262               $data_array = explode("-",$data);
3263               $elder = $data_array[0];
3264               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
3265               $this->db->query("INSERT INTO eq_attendance (elder,date) "
3266                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
3267            }
3268         
3269          $this->att_view();
3270          return false;    
3271         }
3272
3273       $sql = "SELECT * FROM eq_elder where valid=1";
3274       $this->db->query($sql,__LINE__,__FILE__);
3275       $i=0;
3276       while ($this->db->next_record())
3277         {
3278           $elder_name[$i] = $this->db->f('name');
3279           $elder_id[$i] = $this->db->f('elder');
3280           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
3281           $i++;
3282         }
3283       array_multisort($elder_name, $elder_id);
3284       
3285       if($action == 'update_month')
3286       {
3287         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
3288         $i=0; 
3289         $last_time = 0; 
3290         $found_sunday = 0;
3291         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3292         $last_date = explode("-",$sunday_list[0]['date']);
3293         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3294         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3295         while($last_time <= $time_limit)
3296         {
3297           $day = date("w",$last_time);
3298           if(date("w",$last_time) == 0) { 
3299             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3300             $last_date = explode("-",$sunday_list[$i]['date']);
3301             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3302             $sunday_list[$i]['day'] = $last_date[2];
3303             $sunday_list[$i]['month'] = date("M",$last_time);
3304             $sunday_list[$i]['year'] = $last_date[0];
3305             $found_sunday = 1; 
3306           }
3307           $last_time += 90000;
3308           if($found_sunday) { $i++; $found_sunday=0; }
3309         }
3310
3311         $this->t->set_var('span', $i);
3312         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3313         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3314         $this->t->fp('list1','month_list',True);
3315         $elder_width=200; $att_width=25; $total_width=$elder_width;
3316         for ($i=0; $i < count($sunday_list); $i++) {
3317           $link_data['menuaction'] = 'eq.eq.att_update';
3318           $link_data['month'] = $sunday_list[$i]['month'];
3319           $link_data['year'] = $sunday_list[$i]['year'];
3320           $link_data['day'] = $sunday_list[$i]['day'];
3321           $link_data['date'] = $sunday_list[$i]['date'];
3322           $link_data['action'] = 'update_day';
3323           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3324           $this->t->set_var('date',$sunday_list[$i]['date']);
3325           $this->t->set_var('day',$sunday_list[$i]['day']);
3326           $this->t->set_var('month',$sunday_list[$i]['month']);
3327           $this->t->set_var('year',$sunday_list[$i]['year']);
3328           $this->t->fp('list2','header_list',True);
3329           $total_width += $att_width;
3330         }     
3331       }
3332
3333       if($action == 'update_day')
3334       {
3335         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
3336         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3337         $this->t->set_var('month',$month);
3338         $this->t->set_var('year',$year);
3339         $this->t->fp('list1','month_list',True);
3340         $this->t->set_var('date',$date);
3341         $this->t->set_var('day',$day);
3342         $this->t->set_var('month',$month);
3343         $this->t->set_var('year',$year);
3344         $this->t->fp('list2','header_list',True);
3345       }           
3346             
3347       for ($i=0; $i < count($elder_id); $i++) {
3348         $att_table = "";
3349         $this->nextmatchs->template_alternate_row_color(&$this->t);
3350         $this->t->set_var('elder_name',$elder_name[$i]);
3351         for ($j=0; $j < count($sunday_list); $j++) {
3352           $sql = "SELECT * FROM eq_attendance WHERE date='"
3353              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3354           $this->db->query($sql,__LINE__,__FILE__);
3355           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
3356           if($this->db->next_record()) {
3357             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3358           } else if($elder_attending[$elder_id[$i]] == 1) {
3359             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3360           } else {
3361             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
3362           }
3363         }
3364         $this->t->set_var('att_table',$att_table);
3365         $this->t->fp('list3','elder_list',True);
3366       } 
3367            
3368       $this->t->set_var('lang_done', 'Cancel');
3369       $this->t->set_var('lang_reset','Clear Form');
3370       $this->t->set_var('lang_save','Save Changes');
3371
3372       $this->t->pfp('out','form');
3373       $this->t->pfp('addhandle','edit');
3374
3375       $this->save_sessiondata();       
3376     }
3377
3378   function dir_view()
3379     {
3380       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3381       $this->t->set_block('dir_view_t','dir_list','list');
3382       
3383       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
3384       $this->db->query($sql,__LINE__,__FILE__);
3385       $i=0;
3386       while ($this->db->next_record())
3387         {
3388           $parent[$i]['id'] = $this->db->f('parent');
3389           $parent[$i]['name'] = $this->db->f('name');
3390           $parent[$i]['phone'] = $this->db->f('phone');
3391           $parent[$i]['address'] = $this->db->f('address');
3392           $i++;
3393         }   
3394       
3395       for ($i=0; $i < count($parent); $i++) 
3396       {
3397         $name = $parent[$i]['name'];
3398         $phone = $parent[$i]['phone'];
3399         $address = $parent[$i]['address'];
3400         $this->t->set_var('name', $name);
3401         $this->t->set_var('address', $address);
3402         $this->t->set_var('phone', $phone);
3403         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3404         $this->t->set_var('tr_color',$tr_color);
3405         $this->t->fp('list','dir_list',True);
3406         //print "$phone $name $address<br>";
3407       }
3408       $this->t->pfp('out','dir_view_t');
3409       $this->save_sessiondata();   
3410     }
3411   
3412   function org_view()
3413     {
3414       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3415       $this->t->set_block('org_view_t','calling_list','list1');
3416       $this->t->set_block('org_view_t','org_list','list2');
3417
3418       # Display a list ordered alphabetically
3419       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
3420       $this->db->query($sql,__LINE__,__FILE__);
3421       $i=0;
3422       while ($this->db->next_record())
3423         {
3424           $calling[$i]['id'] = $this->db->f('indiv_id');
3425           $calling[$i]['name'] = $this->db->f('name');
3426           $calling[$i]['position'] = $this->db->f('position');
3427           $calling[$i]['sustained'] = $this->db->f('sustained');
3428           $calling[$i]['organization'] = $this->db->f('organization');
3429           $i++;
3430         }   
3431       for ($i=0; $i < count($calling); $i++) 
3432       {
3433         $name = $calling[$i]['name'];
3434         $position = $calling[$i]['position'];
3435         $sustained = $calling[$i]['sustained'];
3436         $organization = $calling[$i]['organization'];
3437         $this->t->set_var('name', $name);
3438         $this->t->set_var('position', $position);
3439         $this->t->set_var('sustained', $sustained);
3440         $this->t->set_var('organization', $organization);
3441         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3442         $this->t->set_var('tr_color',$tr_color);
3443         $this->t->fp('list1','calling_list',True);
3444       }
3445
3446       # Display a list ordered by organization
3447       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
3448       $this->db->query($sql,__LINE__,__FILE__);
3449       $i=0;
3450       while ($this->db->next_record())
3451         {
3452           $calling[$i]['id'] = $this->db->f('indiv_id');
3453           $calling[$i]['name'] = $this->db->f('name');
3454           $calling[$i]['position'] = $this->db->f('position');
3455           $calling[$i]['sustained'] = $this->db->f('sustained');
3456           $calling[$i]['organization'] = $this->db->f('organization');
3457           $i++;
3458         }   
3459       for ($i=0; $i < count($calling); $i++) 
3460       {
3461         $name = $calling[$i]['name'];
3462         $position = $calling[$i]['position'];
3463         $sustained = $calling[$i]['sustained'];
3464         $organization = $calling[$i]['organization'];
3465         $this->t->set_var('name', $name);
3466         $this->t->set_var('position', $position);
3467         $this->t->set_var('sustained', $sustained);
3468         $this->t->set_var('organization', $organization);
3469         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3470         $this->t->set_var('tr_color',$tr_color);
3471         $this->t->fp('list2','org_list',True);
3472       }
3473       
3474       $this->t->pfp('out','org_view_t');
3475       $this->save_sessiondata();   
3476     }
3477   
3478   function schedule()
3479     {
3480       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3481       $this->t->set_block('sched_t','presidency_list','list');
3482
3483       $action = get_var('action',array('GET','POST'));
3484       
3485       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3486       $this->t->set_var('title','EQ Scheduling Tool');
3487
3488       $this->t->set_var('lang_save','Save Schedule');
3489       $this->t->set_var('lang_reset','Cancel');
3490       
3491       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3492       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3493
3494       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3495       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3496       
3497       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3498       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3499       
3500       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3501       $table_width=$date_width + $time_width + $elder_width + $family_width;
3502       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3503       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3504       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3505       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3506       $table_data = "";
3507
3508       if($action == 'save')
3509         {
3510           $new_data = get_var('sched',array('POST'));
3511           foreach ($new_data as $presidency_array)
3512            {
3513              foreach ($presidency_array as $entry)
3514                {
3515                  $presidency = $entry['presidency'];
3516                  $appointment = $entry['appointment'];
3517                  $date = $entry['date'];
3518                  $hour = $entry['hour'];
3519                  $minute = $entry['minute'];
3520                  $pm = $entry['pm'];
3521                  $elder = $entry['elder'];
3522                  $family = $entry['family'];
3523                  if($pm) { $hour = $hour + 12; }
3524                  $time = $hour.':'.$minute.':'.'00';
3525                  $uid = 0;
3526
3527                  // Zero out the family or elder if date = NULL
3528                  if($date == "") {
3529                    $elder = 0;
3530                    $family = 0;
3531                  }
3532                  
3533                  // Update an existing appointment
3534                  if($appointment < $this->max_appointments)
3535                    {
3536                      //Only perform a database update if we have made a change to this appointment
3537                      $sql = "SELECT * FROM eq_appointment where " .
3538                         "appointment='$appointment'" .
3539                         " and presidency='$presidency'" .
3540                         " and elder='$elder'" .
3541                         " and family='$family'" .
3542                         " and date='$date'" .
3543                         " and time='$time'";
3544                      $this->db->query($sql,__LINE__,__FILE__);
3545                      if(!$this->db->next_record()) {
3546                        $old_date = $this->db->f('date');
3547                        $old_time = $this->db->f('time');
3548                        $this->db2->query("UPDATE eq_appointment set" .
3549                                         " family=" . $family . 
3550                                         " ,elder=" . $elder . 
3551                                         " ,date='" . $date . "'" .
3552                                         " ,time='" . $time . "'" .
3553                                         " ,presidency='" . $presidency . "'" .
3554                                         " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3555                        
3556                        // Email the appointment
3557                        $this->email_appt($appointment);
3558                      }
3559                    }
3560                  
3561                  // Add a new appointment
3562                  else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3563                    {
3564                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";
3565                      $this->db2->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time,uid) "
3566                            . "VALUES (NULL,'" . $presidency . "','" . $family . "','"
3567                            . $elder . "','" . $date . "','" . $time  . "','" . $uid ."')",__LINE__,__FILE__);
3568
3569                      // Now reselect this entry from the database to see if we need
3570                      // to send an appointment out for it.
3571                      $sql = "SELECT * FROM eq_appointment where " .
3572                         "elder='$elder'" .
3573                         " and family='$family'" .
3574                         " and presidency='$presidency'" .
3575                         " and date='$date'" .
3576                         " and time='$time'" .
3577                         " and uid='$uid'";
3578                      $this->db3->query($sql,__LINE__,__FILE__);
3579                      if($this->db3->next_record()) {
3580                        // Email the appointment if warranted
3581                        if(($date != "") && ($time != "") && (($elder > 0) || $family > 0)) { 
3582                          $this->email_appt($this->db3->f('appointment'));
3583                        }
3584                      }
3585                    }
3586                }
3587            }
3588           
3589           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3590           Header('Location: ' . $take_me_to_url);
3591         }
3592
3593       $sql = "SELECT * FROM eq_presidency where valid=1";
3594       $this->db->query($sql,__LINE__,__FILE__);
3595       $i=0;
3596       while ($this->db->next_record())
3597         {
3598           $presidency_data[$i]['id'] = $this->db->f('presidency');
3599           $presidency_data[$i]['name'] = $this->db->f('name');
3600           $presidency_data[$i]['elder'] = $this->db->f('elder');
3601           $i++;
3602         }
3603       
3604       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3605       $this->db->query($sql,__LINE__,__FILE__);
3606       $i=0;
3607       while ($this->db->next_record())
3608         {
3609           $elder_id[$i] = $this->db->f('elder');
3610           $elder_name[$i] = $this->db->f('name');
3611           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3612           $i++;
3613         }
3614       array_multisort($elder_name, $elder_id);
3615
3616       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3617       $this->db->query($sql,__LINE__,__FILE__);
3618       $i=0;
3619       while ($this->db->next_record())
3620         {
3621           $family_id[$i] = $this->db->f('family');
3622           $family_name[$i] = $this->db->f('name');
3623           $i++;
3624         }
3625       array_multisort($family_name, $family_id);
3626       
3627       for ($i=0; $i < count($presidency_data); $i++) {
3628         $presidency = $presidency_data[$i]['id'];
3629         $interviewer = $presidency_data[$i]['elder'];
3630         $name = $presidency_data[$i]['name'];
3631         $this->t->set_var('presidency_name',$name);
3632         $table_data="";
3633         
3634         // query the database for all the appointments
3635         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3636         $this->db->query($sql,__LINE__,__FILE__);
3637
3638         // Prefill any existing appointment slots
3639         while ($this->db->next_record())
3640           {
3641             $appointment = $this->db->f('appointment');
3642             $elder = $this->db->f('elder');
3643             $family = $this->db->f('family');
3644             
3645             $date = $this->db->f('date');
3646             $date_array = explode("-",$date);
3647             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3648             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3649             
3650             $time = $this->db->f('time');
3651             $time_array = explode(":",$time);
3652             $hour = $time_array[0];
3653             $minute = $time_array[1];
3654             $pm = 0;
3655             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3656             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3657             
3658             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3659
3660             // Date selection
3661             $table_data.= '<td align=left>';
3662             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3663             $table_data.= '</td>';
3664             
3665             // Hour & Minutes selection
3666             $table_data.= "<td align=center>";
3667             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3668             foreach(range(1,12) as $num) {
3669               if($hour == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3670               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3671             }
3672             $table_data.= '</select>';
3673             $table_data.= '&nbsp;:&nbsp;';
3674             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3675             foreach(range(0,3) as $num) {
3676               $num = $num * 15; if($num == 0) { $num = "00"; }
3677               if($minute == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3678               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3679             }
3680             $table_data.= '</select>';
3681             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3682             if($pm == 0) { $table_data.= '<option value=0 selected>am</option>'; $table_data.= '<option value=1>pm</option>'; }
3683             else { $table_data.= '<option value=0>am</option>'; $table_data.= '<option value=1 selected>pm</option>'; }
3684             $table_data.= '</select>';
3685             $table_data.= "</td>";
3686             
3687             // Elder drop down list (for PPIs)
3688             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3689             $table_data.= '<option value=0></option>';  
3690             for ($j=0; $j < count($elder_id); $j++) {
3691               $id = $elder_id[$j];
3692               $name = $elder_name[$j];
3693               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3694               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3695             }
3696             $table_data.='</select></td>';
3697
3698             // Family drop down list (for Visits)
3699             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3700             $table_data.= '<option value=0></option>';              
3701             for ($j=0; $j < count($elder_id); $j++) {
3702               $id = $family_id[$j];
3703               $name = $family_name[$j];
3704               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3705               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3706             }
3707             $table_data.='</select></td>';
3708             
3709             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3710             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3711         
3712             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3713             $this->t->set_var('tr_color',$tr_color);
3714             
3715           }
3716
3717         // Create blank appointment slot
3718         for ($b=0; $b < 4; $b++) {
3719           $appointment = $this->max_appointments + $b;
3720           $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3721
3722           // Date selection
3723           $table_data.= '<td align=left>';
3724           $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3725           $table_data.= '</td>';
3726         
3727           // Time selection
3728           $table_data.= "<td align=center>";
3729           $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3730           $table_data.= '<option value=""></option>';
3731           foreach(range(1,12) as $num) {
3732             $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3733           }
3734           $table_data.= '</select>';
3735           $table_data.= '&nbsp;:&nbsp;';
3736           $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3737           $table_data.= '<option value=""></option>';
3738           foreach(range(0,3) as $num) {
3739             $num = $num * 15; if($num == 0) { $num = "00"; }
3740             $table_data.= '<option value='.$num.'>'.$num.'</option>';
3741           }
3742           $table_data.= '</select>';
3743           $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3744           $table_data.= '<option value=""></option>';
3745           $table_data.= '<option value=0>am</option>';
3746           $table_data.= '<option value=1>pm</option>';
3747           $table_data.= '</select>';
3748           $table_data.= "</td>";
3749           
3750           // Elder drop down list
3751           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3752           $table_data.= '<option value=0></option>';  
3753           for ($j=0; $j < count($elder_id); $j++) {
3754             $id = $elder_id[$j];
3755             $name = $elder_name[$j];
3756             $table_data.= '<option value='.$id.'>'.$name.'</option>';
3757           }
3758           $table_data.='</select></td>';
3759           
3760           // Family drop down list
3761           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3762           $table_data.= '<option value=0></option>';        
3763           for ($j=0; $j < count($elder_id); $j++) {
3764             $id = $family_id[$j];
3765             $name = $family_name[$j];
3766             $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3767           }
3768           $table_data.='</select></td>';
3769           
3770           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3771           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3772
3773           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3774           $this->t->set_var('tr_color',$tr_color);
3775         }
3776         
3777         $this->t->set_var('table_data',$table_data);
3778         $this->t->set_var('header_row',$header_row);
3779         $this->t->set_var('table_width',$table_width);
3780         $this->t->fp('list','presidency_list',True);
3781         
3782       }
3783       
3784       $this->t->pfp('out','sched_t');
3785       $this->save_sessiondata();   
3786     }
3787   
3788   function admin()
3789     {
3790       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3791       $this->t->set_block('admin_t','upload','uploadhandle');
3792       $this->t->set_block('admin_t','admin','adminhandle');
3793       $this->t->set_block('admin_t','cmd','cmdhandle');
3794       
3795       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3796       
3797       $action = get_var('action',array('GET','POST'));
3798
3799       $this->t->pfp('out','admin_t');
3800       
3801       if($action == 'upload')
3802         {        
3803           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
3804           
3805           if((($_FILES['uploadedfile']['type'] == "application/zip") ||
3806               ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3807               ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3808               ($_FILES['uploadedfile']['type'] == "application/octet-stream")) &&
3809              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
3810             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3811             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3812             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3813             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3814             $this->t->set_var('uploadstatus',$uploadstatus);
3815             $this->t->pfp('uploadhandle','upload');
3816             $this->t->set_var('uploadhandle','');
3817             print "<table border=1 width=80%><tr><td>\n<pre>";
3818             
3819             # make a directory for this data to be stored in
3820             $date="data_" . date("Y_m_d");
3821             $data_dir = $this->upload_target_path . $date;
3822             print "-> Making the data directory: $date<br>\n";
3823             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
3824             if($return_code != 0) {
3825               print implode('\n',$result) . "<br>";
3826               print "<b><font color=red>";
3827               print "-E- Unable to create the data directory. Aborting import.";
3828               print "</font></b>";
3829               return 0;
3830             }
3831
3832             # move the file uploaded into this directory
3833             print "-> Moving the uploaded file into the data dir<br>\n";
3834             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3835             if($return_code != 0) {
3836               print implode('\n',$result) . "<br>";
3837               print "<b><font color=red>";
3838               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3839               print "</font></b>";
3840               return 0;
3841             }
3842             
3843             # unzip the data into this directory
3844             print "-> Unzipping the data<br>\n";
3845             $data_file = $data_dir . '';
3846             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3847             if($return_code != 0) {
3848               print implode('\n',$result) . "<br>";
3849               print "<b><font color=red>";
3850               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
3851               print "</font></b>";
3852               return 0;
3853             }
3854             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3855
3856             # update the data_latest link to point to this new directory
3857             print "-> Updating the latest data dir link<br>\n";
3858             $data_latest = $this->upload_target_path . 'data_latest';
3859             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3860             if($return_code != 0) {
3861               print implode('\n',$result) . "<br>";
3862               print "<b><font color=red>";
3863               print "-E- Unable to update the data latest link. Aborting import.";
3864               print "</font></b>";
3865               return 0;
3866             }
3867             
3868             # run the import perl script to encorporate it into the DB
3869             ob_start('ob_logstdout', 2);
3870             print "-> Importing the data into the EQ database<br>\n";
3871             ob_flush(); flush(); sleep(1);
3872             $import_log = $this->upload_target_path . '/import.log';
3873             $data_log = $this->upload_target_path . '/data.log';
3874             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
3875             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
3876             #print "import_cmd: $import_cmd<br>";
3877             #print "parse_cmd: $parse_cmd<br>";
3878             ob_start('ob_logstdout', 2);
3879             passthru($import_cmd);
3880             passthru($parse_cmd);
3881             ob_flush(); flush(); sleep(1);
3882
3883             # fix the permissions of the data dir
3884             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3885             
3886             $this->t->pfp('cmdhandle','cmd');
3887             print "</pre></td></tr></table>";
3888             
3889           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3890                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3891                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3892                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3893             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3894             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3895             $this->t->set_var('uploadstatus',$uploadstatus);
3896             $this->t->pfp('uploadhandle','upload',True);
3897           } else {
3898             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3899             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3900             $this->t->set_var('uploadstatus',$uploadstatus);
3901             $this->t->pfp('uploadhandle','upload',True);
3902           }
3903         }
3904       else
3905         {
3906           $this->t->set_var('adminhandle','');
3907           $this->t->pfp('adminhandle','admin'); 
3908         }
3909       
3910       $this->save_sessiondata();   
3911     }
3912
3913   function email_appt($appointment)
3914     {
3915       //print "Emailing notification of appointment: $appointment <br>";
3916
3917       $sql = "SELECT * FROM eq_appointment where appointment='$appointment'";
3918       $this->db->query($sql,__LINE__,__FILE__);
3919         
3920       while ($this->db->next_record())
3921         {
3922           $appointment = $this->db->f('appointment');
3923           $presidency = $this->db->f('presidency');
3924           $interviewer = "";
3925           $email = "";
3926           $elder = $this->db->f('elder');
3927           $elder_name = "";
3928           $family = $this->db->f('family');
3929           $family_name = "";
3930           $appt_name = "";
3931           $phone = "";
3932           $location = "";
3933           $uid = $this->db->f('uid');
3934                     
3935           // Extract the year, month, day, hours, minutes, seconds from the appointment time
3936           $appt_date = $this->db->f('date');
3937           $date_array = explode("-",$appt_date);
3938           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3939           $appt_time = $this->db->f('time');
3940           $time_array = explode(":",$appt_time);
3941           $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
3942
3943           // Format the appointment time into an iCal UTC equivalent
3944           $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
3945           $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
3946           
3947           // Set the email address of the person making the appointment
3948           $from = $GLOBALS['phpgw_info']['user']['fullname'] . "<" .
3949                   $GLOBALS['phpgw_info']['user']['preferences']['email']['address'] . ">";
3950           
3951           $sql = "SELECT * FROM eq_presidency where presidency='$presidency'";
3952           $this->db2->query($sql,__LINE__,__FILE__);
3953           if($this->db2->next_record()) {
3954             $email = $this->db2->f('email');
3955             $interviewer = $this->db2->f('name');
3956           }
3957
3958           if($elder > 0) { 
3959             $sql = "SELECT * FROM eq_elder where elder='$elder'";
3960             $this->db2->query($sql,__LINE__,__FILE__);
3961             if($this->db2->next_record()) {
3962               $elder_name = $this->db2->f('name');
3963               $phone = $this->db2->f('phone');
3964               $appt_name = $elder_name . " Interview";
3965               $location = "$interviewer"."'s home";
3966               $duration = 1800; // 30 minutes
3967             }
3968           }
3969
3970           if($family > 0) { 
3971             $sql = "SELECT * FROM eq_family where family='$family'";
3972             $this->db2->query($sql,__LINE__,__FILE__);
3973             if($this->db2->next_record()) {
3974               $family_name = $this->db2->f('name');
3975               $phone = $this->db2->f('phone');
3976               $elder_id = $this->db2->f('elder_id');
3977               $appt_name = $family_name . " Family Visit";
3978               $sql = "SELECT * FROM eq_elder where elder='$elder_id'";
3979               $this->db3->query($sql,__LINE__,__FILE__);
3980               if($this->db3->next_record()) {
3981                 $phone = $this->db3->f('phone');
3982               }
3983               $sql = "SELECT * FROM eq_parent where family='$family'";
3984               $this->db3->query($sql,__LINE__,__FILE__);
3985               if($this->db3->next_record()) {
3986                 $location=$this->db3->f('address');
3987               }
3988               $duration = 2700; // 45 minutes
3989             }
3990           }
3991
3992           $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
3993           $description = "$appt_name : $phone";
3994           
3995           if(($uid == 0) && ($appt_name != "")) { 
3996             // Create a new calendar item for this appointment, since this must be the first time we
3997             // are sending it out.
3998             print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
3999             $uid = rand() . rand(); // Generate a random identifier for this appointment
4000             $subject = "Created: $appt_name";
4001             
4002             $this->db->query("UPDATE eq_appointment set" .
4003                              " uid=" . $uid . 
4004                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4005
4006             $action = "PUBLISH";
4007             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4008                                   $dtend, $location, $appt_name, $description, $uid);
4009             
4010           } else if(($uid != 0) && ($appt_name == "")) {
4011             // Remove the calendar item for this appointment since it has already been sent
4012             // and there is no name we have changed it to.
4013             print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4014             $subject = "Canceled: $appt_date $appt_time";
4015             
4016             $this->db->query("UPDATE eq_appointment set" .
4017                              " uid=0" . 
4018                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4019             
4020             $action = "CANCEL";
4021             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4022                                   $dtend, $location, $appt_name, $description, $uid);
4023             
4024           } else if($uid != 0) {
4025             // Update the existing appointment since we have changed it
4026             print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4027
4028             $subject = "Canceled: $appt_date $appt_time";
4029             $action = "CANCEL";
4030             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4031                                   $dtend, $location, $appt_name, $description, $uid);
4032             
4033             $uid = rand() . rand(); // Generate a random identifier for this appointment
4034             $this->db->query("UPDATE eq_appointment set" .
4035                              " uid=" . $uid .
4036                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4037             
4038             $subject = "Updated: $appt_name";       
4039             $action = "PUBLISH";
4040             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4041                                   $dtend, $location, $appt_name, $description, $uid);
4042           }
4043           
4044         }
4045           
4046       return true;
4047     }
4048
4049   function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $location, $summary, $description, $uid)
4050     {
4051       $headers = 'From: ' . "$from" . "\n" .
4052          'Reply-To: ' . "$from" . "\n" .
4053          'X-Mailer: PHP/' . phpversion() . "\n" .
4054          'Content-Type: text/calendar;' . "\n" .
4055          'Content-Transfer-Encoding: 7bit' . "\n";
4056       
4057       //$message = "phone: $phone date: $date time: $time";
4058       $message ="";
4059       $message.="BEGIN:VCALENDAR" . "\n";
4060       $message.="VERSION:2.0" . "\n";
4061       $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4062       $message.="METHOD:$action" . "\n";
4063       $message.="BEGIN:VEVENT" . "\n";
4064       $message.="ORGANIZER:MAILTO:$from". "\n";
4065       $message.="DTSTAMP:$dtstamp" . "\n";
4066       $message.="DTSTART:$dtstart" . "\n";
4067       $message.="DTEND:$dtend" . "\n";
4068       $message.="SUMMARY:$summary" . "\n";
4069       $message.="DESCRIPTION:$description" . "\n";
4070       $message.="LOCATION:$location" . "\n";
4071       $message.="UID:$uid" ."\n";
4072       $message.="TRANSP:OPAQUE" . "\n";
4073       $message.="SEQUENCE:0" . "\n";
4074       $message.="CLASS:PUBLIC" . "\n";
4075       $message.="END:VEVENT" . "\n";
4076       $message.="END:VCALENDAR" . "\n";
4077       
4078       mail($to, $subject, $message, $headers);
4079       
4080     }
4081 }
4082
4083 ?>