Fixed bug in the PPI tool where the interviewer wasn't being
[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_elder WHERE elder=" . $interviewer;
2411       $this->db2->query($sql,__LINE__,__FILE__);
2412       $this->db2->next_record();
2413       $interviewer_name = $this->db2->f('name');
2414       $this->t->set_var('interviewer',$interviewer . ' selected');
2415       $this->t->set_var('interviewer_name',$interviewer_name);
2416       $this->t->fp('int_list','interviewer_list',True);
2417     
2418       if($action == 'save')
2419         {
2420           $notes = get_var('notes',array('POST'));
2421           $this->db->query("UPDATE eq_ppi set " .
2422                            "   ppi='" . $ppi . "'" .
2423                     ", interviewer='" . $interviewer . "'" .
2424                           ", elder='" . $elder . "'" .
2425                            ", date='" . $date . "'" .
2426                           ", notes='" . $notes . "'" .
2427                       ", eqpresppi='" . $eqpresppi . "'" .
2428                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2429           $this->ppi_view();
2430           return false;
2431         }
2432
2433       if($action == 'insert')
2434         {
2435           $notes = get_var('notes',array('POST'));
2436           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,date,notes,eqpresppi) "
2437                            . "VALUES ('" . $interviewer . "','" . $elder . "','"
2438                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
2439           $this->ppi_view();
2440           return false;
2441         }
2442       
2443       if($action == 'add')
2444         {
2445           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2446           $this->t->set_var('ppi', '');
2447           $this->t->set_var('interviewer', $interviewer);
2448           $this->t->set_var('name',$name);
2449           $this->t->set_var('elder',$elder);
2450           $this->t->set_var('date','');
2451           $this->t->set_var('notes','');
2452           $this->t->set_var('eqpresppi',$eqpresppi);
2453           $this->t->set_var('lang_done','Cancel');
2454           $this->t->set_var('lang_action','Adding New PPI');
2455           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2456                                                                 . $ppi . '&action=' . 'insert'));
2457         }
2458
2459       if($action == 'edit' || $action == 'view')
2460         {
2461           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2462           $this->db->query($sql,__LINE__,__FILE__);
2463           $this->db->next_record();
2464           $this->t->set_var('ppi',$ppi);
2465           $this->t->set_var('name',$name);
2466           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2467           $this->t->set_var('elder',$this->db->f('elder'));
2468           $this->t->set_var('date',$this->db->f('date'));
2469           $this->t->set_var('notes',$this->db->f('notes'));
2470           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
2471         }
2472       
2473       if($action == 'edit')
2474         {
2475           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2476           $this->t->set_var('lang_done','Cancel');
2477           $this->t->set_var('lang_action','Editing PPI');
2478           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2479                                                                 . $ppi . '&action=' . 'save'));
2480         }
2481
2482       if($action == 'view')
2483         {
2484           $date = $this->db->f('date');
2485           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2486           $this->t->set_var('readonly','READONLY');
2487           $this->t->set_var('disabled','DISABLED');
2488           $this->t->set_var('lang_done','Done');
2489           $this->t->set_var('lang_action','Viewing PPI');
2490           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2491                                                                 . $ppi . '&action=' . 'edit'));
2492         }
2493       
2494       $this->t->set_var('lang_reset','Clear Form');
2495       $this->t->set_var('lang_add','Add PPI');
2496       $this->t->set_var('lang_save','Save Changes');
2497       $this->t->set_var('edithandle','');
2498       $this->t->set_var('addhandle','');
2499
2500       $this->t->pfp('out','form');
2501       
2502       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2503       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2504       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2505      
2506       $this->save_sessiondata(); 
2507     }
2508
2509     function int_view()
2510     {
2511       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2512       $this->t->set_block('int_view_t','district_list','list');
2513
2514       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2515       $num_quarters = get_var('num_quarters',array('GET','POST'));
2516       if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2517       $this->t->set_var('num_quarters',$num_quarters);
2518       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2519       else {  $this->t->set_var('lang_num_quarters','Quarters of History'); }
2520       $this->t->set_var('lang_filter','Filter');
2521       
2522       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2523       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2524       
2525       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2526       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2527       
2528       $this->t->set_var('title','Hometeaching Interviews'); 
2529
2530       $num_months = $num_quarters * 3 - 1;
2531       $current_month = $this->current_month;
2532       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2533       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2534       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2535       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2536
2537       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2538       $this->db->query($sql,__LINE__,__FILE__);
2539       $i=0;
2540       while ($this->db->next_record())
2541         {
2542           $districts[$i]['district'] = $this->db->f('district');
2543           $districts[$i]['name'] = $this->db->f('name');
2544           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2545           $i++;
2546         }
2547
2548       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2549       $this->db->query($sql,__LINE__,__FILE__);
2550       $i=0;
2551       while ($this->db->next_record())
2552         {
2553           $elder_id[$i] = $this->db->f('elder');
2554           $elder_name[$i] = $this->db->f('name');
2555           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2556           $i++;
2557         }
2558       array_multisort($elder_name, $elder_id);
2559       for($i=0; $i < count($elder_id); $i++) {
2560           $id = $elder_id[$i];
2561           $elders[$id] = $elder_name[$i];
2562       }      
2563
2564       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2565       $this->db->query($sql,__LINE__,__FILE__);
2566       while ($this->db->next_record())
2567         {
2568           $aaronic_id = $this->db->f('aaronic');
2569           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2570           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2571         }
2572       
2573       $total_companionships = 0;
2574       $this->nextmatchs->template_alternate_row_color(&$this->t);
2575       for ($i=0; $i < count($districts); $i++) {
2576         $this->t->set_var('district_number',$districts[$i]['district']);
2577         $this->t->set_var('district_name',$districts[$i]['name']);      
2578         $supervisor = $districts[$i]['supervisor'];
2579         $unique_companionships='';
2580                 
2581         // Select all the unique companionship numbers for this district
2582         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2583
2584         $this->db->query($sql,__LINE__,__FILE__);
2585         $j=0;
2586         while ($this->db->next_record())
2587           {
2588             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2589             $j++;
2590           }
2591         
2592         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2593         $table_data=""; $num_companionships = $j; $num_elders = 0;
2594         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2595         for ($j=0; $j < count($unique_companionships); $j++) {
2596           // Select all the companions in each companionship
2597           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2598              "companionship=". $unique_companionships[$j]['companionship'];
2599           $this->db->query($sql,__LINE__,__FILE__);
2600           $k=0;
2601           $comp = $unique_companionships[$j]['companionship'];
2602           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2603           while ($this->db->next_record())
2604             {
2605               // Get this companions information
2606               $num_elders++;
2607               $companionship = $this->db->f('companionship');
2608               $elder_id = $this->db->f('elder');
2609               $aaronic_id = $this->db->f('aaronic');
2610               if($elder_id) {
2611                 $name = $elders[$elder_id];
2612                 $phone = $elder_phone[$elder_id];
2613               }
2614               else if($aaronic_id) {
2615                 $name = $aaronic[$aaronic_id]['name'];
2616                 $phone = $aaronic[$aaronic_id]['phone'];
2617               }
2618               $link_data['menuaction'] = 'eq.eq.int_update';
2619               $link_data['companionship'] = $companionship;
2620               $link_data['interviewer'] = $supervisor;
2621               $link_data['elder'] = $elder_id;
2622               $link_data['aaronic'] = $aaronic_id;
2623               $link_data['name'] = $name;
2624               $link_data['interview'] = '';
2625               $link_data['action'] = 'add';
2626               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2627               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2628
2629               // Find out how many times Interviews were performed in the past $num_months for this Elder
2630               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2631               for($m=$num_months; $m >= 0; $m--) {
2632                 $month = $current_month - $m;
2633                 $year = $this->current_year;
2634                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2635                 if($month < 10) { $month = "0"."$month"; }
2636                 $month_start = "$year"."-"."$month"."-"."01";
2637                 $month_end = "$year"."-"."$month"."-"."31";
2638                 $month = "$month"."/"."$year";
2639                 $sql = "SELECT * FROM eq_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2640                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2641                 $this->db2->query($sql,__LINE__,__FILE__);
2642                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2643               
2644                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2645                 if($this->db2->next_record()) {
2646                   if(!$int_recorded[$companionship][$m]) {
2647                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2648                   }
2649                   $link_data['menuaction'] = 'eq.eq.int_update';
2650                   $link_data['companionship'] = $companionship;
2651                   $link_data['interviewer'] = $this->db2->f('interviewer');
2652                   $link_data['elder'] = $elder_id;
2653                   $link_data['aaronic'] = $aaronic_id;
2654                   $link_data['name'] = $name;
2655                   $link_data['interview'] = $this->db2->f('interview');
2656                   $link_data['action'] = 'view';
2657                   $date = $this->db2->f('date');
2658                   $date_array = explode("-",$date);
2659                   $month = $date_array[1];
2660                   $day   = $date_array[2];
2661                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2662                   $table_data .= '<td align=center><a href='.$link.'><img src="checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2663                 }
2664                 else { $table_data .= "<td>&nbsp;</td>"; }
2665               }
2666               $table_data .= "</tr>"; 
2667               $k++;
2668             }
2669           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2670         }
2671         $total_companionships += $num_companionships;
2672         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Quarterly Totals:</font></b></td>";
2673
2674         for($m=$num_months; $m >=0; $m--) {
2675           $month = $current_month - $m;
2676           if(($month % 3) == 1) { $quarter_total = $ints[$m]; }
2677           else { $quarter_total += $ints[$m]; }
2678           $percent = ceil(($quarter_total / $num_companionships)*100);
2679           $stat_data .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2680         }
2681         $stat_data .= "</tr>";
2682
2683         $this->t->set_var('table_width',$table_width);
2684         $this->t->set_var('header_row',$header_row);
2685         $this->t->set_var('table_data',$table_data);
2686         $this->t->set_var('stat_data',$stat_data);
2687         $this->t->fp('list','district_list',True);
2688       }
2689
2690       // Display the totals, cummulative per quarter
2691       $quarter_total = 0;
2692       $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Quarterly Totals:</font></b></td>";
2693       for($m=$num_months; $m >=0; $m--) {
2694         $month = $current_month - $m;
2695         if(($month % 3) == 1) { $quarter_total = $total_ints[$m]; }
2696         else { $quarter_total += $total_ints[$m]; }
2697         $percent = ceil(($quarter_total / $total_companionships)*100);
2698         $totals .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2699       }
2700       $totals .= "</tr>";
2701       
2702       $this->t->set_var('totals',$totals);
2703       $this->t->pfp('out','int_view_t');
2704       $this->save_sessiondata(); 
2705     }
2706
2707   function int_update()
2708     {
2709       $this->t->set_file(array('form' => 'int_update.tpl'));
2710       $this->t->set_block('form','interviewer_list','int_list');
2711       $this->t->set_block('form','add','addhandle');
2712       $this->t->set_block('form','edit','edithandle');
2713       
2714       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2715       $this->t->set_var('readonly','');
2716       $this->t->set_var('disabled','');
2717       
2718       $action = get_var('action',array('GET','POST'));
2719       $companionship = get_var('companionship',array('GET','POST'));
2720       $interviewer = get_var('interviewer',array('GET','POST'));      
2721       $name = get_var('name',array('GET','POST'));
2722       $interview = get_var('interview',array('GET','POST'));
2723       $elder = get_var('elder',array('GET','POST'));
2724       $aaronic = get_var('aaronic',array('GET','POST'));
2725       $date = get_var('date',array('GET','POST'));
2726       $notes = get_var('notes',array('GET','POST'));
2727       
2728       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2729       $this->db->query($sql,__LINE__,__FILE__);
2730       while ($this->db->next_record())
2731         {
2732           $supervisor = $this->db->f('supervisor');
2733           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2734           $this->db2->query($sql,__LINE__,__FILE__);
2735           $this->db2->next_record();
2736           $interviewer_name = $this->db2->f('name');
2737           
2738           if($supervisor == $interviewer) { 
2739             $this->t->set_var('interviewer',$supervisor . ' selected');
2740           } else {
2741             $this->t->set_var('interviewer',$interviewer);
2742           }
2743           $this->t->set_var('interviewer_name',$interviewer_name);
2744           $this->t->fp('int_list','interviewer_list',True);
2745         }
2746       
2747       if($action == 'save')
2748         {
2749           $notes = get_var('notes',array('POST'));
2750           $this->db->query("UPDATE eq_interview set " .
2751                      "   interview='" . $interview . "'" .
2752                     ", interviewer='" . $interviewer . "'" .
2753                           ", elder='" . $elder . "'" .
2754                         ", aaronic='" . $aaronic . "'" .
2755                            ", date='" . $date . "'" .
2756                           ", notes='" . $notes . "'" .
2757                            " WHERE interview=" . $interview,__LINE__,__FILE__);
2758           $this->int_view();
2759           return false;
2760         }
2761
2762       if($action == 'insert')
2763         {
2764           $notes = get_var('notes',array('POST'));
2765           $this->db->query("INSERT INTO eq_interview (interviewer,elder,aaronic,date,notes) "
2766                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2767                            . $date . "','" . $notes ."')",__LINE__,__FILE__);
2768           $this->int_view();
2769           return false;
2770         }
2771       
2772       if($action == 'add')
2773         {
2774           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2775           $this->t->set_var('interview', '');
2776           $this->t->set_var('interviewer', $interviewer);
2777           $this->t->set_var('name',$name);
2778           $this->t->set_var('elder',$elder);
2779           $this->t->set_var('aaronic',$aaronic);
2780           $this->t->set_var('date','');
2781           $this->t->set_var('notes','');
2782           $this->t->set_var('lang_done','Cancel');
2783           $this->t->set_var('lang_action','Adding New Interview');
2784           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2785                                                                 . $interview . '&action=' . 'insert'));
2786         }
2787
2788       if($action == 'edit' || $action == 'view')
2789         {
2790           $sql = "SELECT * FROM eq_interview WHERE interview=".$interview;
2791           $this->db->query($sql,__LINE__,__FILE__);
2792           $this->db->next_record();
2793           $this->t->set_var('interview',$interview);
2794           $this->t->set_var('name',$name);
2795           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2796           $this->t->set_var('elder',$this->db->f('elder'));
2797           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2798           $this->t->set_var('date',$this->db->f('date'));
2799           $this->t->set_var('notes',$this->db->f('notes'));
2800         }
2801       
2802       if($action == 'edit')
2803         {
2804           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2805           $this->t->set_var('lang_done','Cancel');
2806           $this->t->set_var('lang_action','Editing Interview');
2807           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2808                                                                 . $interview . '&action=' . 'save'));
2809         }
2810
2811       if($action == 'view')
2812         {
2813           $date = $this->db->f('date');
2814           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2815           $this->t->set_var('readonly','READONLY');
2816           $this->t->set_var('disabled','DISABLED');
2817           $this->t->set_var('lang_done','Done');
2818           $this->t->set_var('lang_action','Viewing Interview');
2819           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2820                                                                 . $interview . '&action=' . 'edit'));
2821         }
2822       
2823       $this->t->set_var('lang_reset','Clear Form');
2824       $this->t->set_var('lang_add','Add Interview');
2825       $this->t->set_var('lang_save','Save Changes');
2826       $this->t->set_var('edithandle','');
2827       $this->t->set_var('addhandle','');
2828
2829       $this->t->pfp('out','form');
2830       
2831       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2832       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2833       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2834      
2835       $this->save_sessiondata(); 
2836     }
2837
2838   function vis_view()
2839     {
2840       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2841       $this->t->set_block('vis_view_t','visit_list','list1');
2842       $this->t->set_block('vis_view_t','family_list','list2');
2843
2844       $this->t->set_var('lang_name','Family Name');
2845       $this->t->set_var('lang_date','Date');
2846
2847       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2848       $this->t->set_var('vis_link_title','View Yearly Visits');
2849       
2850       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2851       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2852
2853       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2854       $num_years = get_var('num_years',array('GET','POST'));
2855       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2856       $this->t->set_var('num_years',$num_years);
2857       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2858       else {  $this->t->set_var('lang_num_years','Years of History'); }
2859       $this->t->set_var('lang_filter','Filter');
2860       
2861       $year = date('Y') - $num_years + 1;
2862       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2863       
2864       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2865       $this->db->query($sql,__LINE__,__FILE__);
2866       $total_records = $this->db->num_rows();
2867
2868       $i = 0;
2869       while ($this->db->next_record())
2870         {
2871           $visit_list[$i]['visit'] = $this->db->f('visit');
2872           $visit_list[$i]['family'] = $this->db->f('family');
2873           $visit_list[$i]['date']  = $this->db->f('date');
2874           $i++;
2875         }
2876             
2877       for ($i=0; $i < count($visit_list); $i++)
2878         {         
2879           $this->nextmatchs->template_alternate_row_color(&$this->t);
2880
2881           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2882           $this->db->query($sql,__LINE__,__FILE__);
2883           $this->db->next_record();
2884                   
2885           $this->t->set_var('family',$visit_list[$i]['family']);
2886           $this->t->set_var('family_name',$this->db->f('name'));
2887           $this->t->set_var('date',$visit_list[$i]['date']);
2888           
2889           $link_data['menuaction'] = 'eq.eq.vis_update';
2890           $link_data['visit'] = $visit_list[$i]['visit'];
2891           $link_data['name'] = $this->db->f('name');
2892           $link_data['date'] = $visit_list[$i]['date'];
2893           $link_data['action'] = 'view';
2894           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2895           $this->t->set_var('lang_view','View');
2896
2897           $link_data['menuaction'] = 'eq.eq.vis_update';
2898           $link_data['visit'] = $visit_list[$i]['visit'];
2899           $link_data['name'] = $this->db->f('name');
2900           $link_data['date'] = $visit_list[$i]['date'];
2901           $link_data['action'] = 'edit';
2902           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2903           $this->t->set_var('lang_edit','Edit');
2904
2905           $this->t->fp('list1','visit_list',True);
2906         }
2907
2908       // List the families that are available to record a visit against
2909       $sql = "SELECT * FROM eq_family WHERE valid=1";
2910       $this->db->query($sql,__LINE__,__FILE__);
2911       $total_records = $this->db->num_rows();
2912
2913       $i = 0;
2914       while ($this->db->next_record())
2915         {
2916           $family_names[$i] = $this->db->f('name');
2917           $family_ids[$i] = $this->db->f('family');
2918           $i++;
2919         } array_multisort($family_names, $family_ids);
2920       
2921       for ($i=0; $i < count($family_names); $i++)
2922         {
2923           $link_data['menuaction'] = 'eq.eq.vis_update';
2924           $link_data['visit'] = '';
2925           $link_data['family'] = $family_ids[$i];
2926           $link_data['action'] = 'add';
2927           $link_data['name'] = $family_names[$i];
2928           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2929
2930           $this->t->set_var('name',$family_names[$i]);
2931           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2932           else { $this->t->set_var('table_sep',"</td>"); }
2933           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2934
2935           $this->t->fp('list2','family_list',True);
2936         }   
2937
2938       $this->t->pfp('out','vis_view_t');
2939       $this->save_sessiondata(); 
2940     }
2941
2942   function vis_update()
2943     {
2944       $this->t->set_file(array('form' => 'vis_update.tpl'));
2945       $this->t->set_block('form','add','addhandle');
2946       $this->t->set_block('form','edit','edithandle');
2947       
2948       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2949       $this->t->set_var('readonly','');
2950       $this->t->set_var('disabled','');
2951       
2952       $action = get_var('action',array('GET','POST'));
2953       $visit = get_var('visit',array('GET','POST'));
2954       $family = get_var('family',array('GET','POST'));
2955       $name = get_var('name',array('GET','POST'));
2956       $date = get_var('date',array('GET','POST'));
2957       $notes = get_var('notes',array('GET','POST'));
2958       $companionship = 0;
2959       
2960       if($action == 'save')
2961         {
2962           $notes = get_var('notes',array('POST'));
2963           $this->db->query("UPDATE eq_visit set " .
2964                            "  date='" . $date . "'" .
2965                           ", notes='" . $notes . "'" .
2966                            " WHERE visit=" . $visit,__LINE__,__FILE__);
2967           $this->vis_view();
2968           return false;
2969         }
2970
2971       if($action == 'insert')
2972         {
2973           $notes = get_var('notes',array('POST'));
2974           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
2975                            . "VALUES ('" . $family . "','" . $companionship . "','"
2976                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
2977           $this->vis_view();
2978           return false;
2979         }
2980       
2981       if($action == 'add')
2982         {
2983           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2984           $this->t->set_var('family', $family);
2985           $this->t->set_var('visit', '');
2986           $this->t->set_var('name', $name);
2987           $this->t->set_var('date','');
2988           $this->t->set_var('notes','');
2989           $this->t->set_var('lang_done','Cancel');
2990           $this->t->set_var('lang_action','Adding New Visit');
2991           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
2992                                                                 . $family . '&action=' . 'insert'));
2993         }
2994
2995       if($action == 'edit' || $action == 'view')
2996         {
2997           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
2998           $this->db->query($sql,__LINE__,__FILE__);
2999           $this->db->next_record();
3000           $this->t->set_var('visit',$visit);
3001           $this->t->set_var('name',$name);
3002           $this->t->set_var('family', $family);
3003           $this->t->set_var('date',$this->db->f('date'));
3004           $this->t->set_var('notes',$this->db->f('notes'));
3005         }
3006       
3007       if($action == 'edit')
3008         {
3009           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3010           $this->t->set_var('lang_done','Cancel');
3011           $this->t->set_var('lang_action','Editing Visit');
3012           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3013                                                                 . $visit . '&action=' . 'save'));
3014         }
3015
3016       if($action == 'view')
3017         {
3018           $date = $this->db->f('date');
3019           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3020           $this->t->set_var('readonly','READONLY');
3021           $this->t->set_var('disabled','DISABLED');
3022           $this->t->set_var('lang_done','Done');
3023           $this->t->set_var('lang_action','Viewing Visit');
3024           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3025                                                                 . $visit . '&action=' . 'edit'));
3026         }
3027       
3028       $this->t->set_var('lang_reset','Clear Form');
3029       $this->t->set_var('lang_add','Add Visit');
3030       $this->t->set_var('lang_save','Save Changes');
3031       $this->t->set_var('edithandle','');
3032       $this->t->set_var('addhandle','');
3033
3034       $this->t->pfp('out','form');
3035       
3036       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3037       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3038       if($action == 'add') { $this->t->pfp('addhandle','add'); }
3039
3040       $this->save_sessiondata(); 
3041     }
3042
3043   function att_view()
3044     {
3045       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3046       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3047       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3048
3049       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3050       $this->t->set_block('att_view_t','act_list','list');
3051
3052       $this->t->set_block('att_view_t','month_list','list1');
3053       $this->t->set_block('att_view_t','header_list','list2');
3054       $this->t->set_block('att_view_t','elder_list','list3');
3055       
3056       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3057       $num_quarters = get_var('num_quarters',array('GET','POST'));
3058       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3059       $this->t->set_var('num_quarters',$num_quarters);
3060       $this->t->set_var('lang_filter','Filter');
3061       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
3062       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
3063
3064       $num_months = $num_quarters * 3;
3065       $current_month = $this->current_month;
3066       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3067       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3068       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3069       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3070       
3071       $sql = "SELECT * FROM eq_elder where valid=1";
3072       $this->db->query($sql,__LINE__,__FILE__);
3073       $i=0;
3074       while ($this->db->next_record())
3075         {
3076           $elder_name[$i] = $this->db->f('name');
3077           $elder_id[$i] = $this->db->f('elder');
3078           $i++;
3079         }
3080       array_multisort($elder_name, $elder_id);
3081       
3082       // Create a list of sunday dates for a window of 3 months back and current month
3083       $i=0; 
3084       $last_time = 0; 
3085       $found_sunday = 0;
3086       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
3087       $last_date = explode("-",$sunday_list[0]['date']);
3088       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3089       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
3090       while($last_time < $time_limit)
3091       {
3092         $day = date("w",$last_time);
3093         if(date("w",$last_time) == 0) {
3094           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3095           $last_date = explode("-",$sunday_list[$i]['date']);
3096           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3097           $sunday_list[$i]['day'] = $last_date[2];
3098           $sunday_list[$i]['month'] = date("M",$last_time);
3099           $sunday_list[$i]['year'] = $last_date[0];
3100           $found_sunday = 1; 
3101         }
3102         $last_time += 90000;
3103         if($found_sunday) { $i++; $found_sunday=0; }
3104       }
3105
3106       $total_elders = count($elder_id);
3107       $old_month=$sunday_list[0]['month']; $span=0;
3108       for ($i=0; $i < count($sunday_list); $i++) {
3109         $date = $sunday_list[$i]['date'];
3110         $this->t->set_var('date',$sunday_list[$i]['date']);
3111         $this->t->set_var('day',$sunday_list[$i]['day']);
3112         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3113           if($i == count($sunday_list)-1) { $span++; }
3114           $cur_month = $sunday_list[$i]['month'];
3115           $old_month = $sunday_list[$i]['month'];         
3116           $link_data['menuaction'] = 'eq.eq.att_update';
3117           $link_data['month'] = $sunday_list[$i-1]['month'];
3118           $link_data['year'] = $sunday_list[$i-1]['year'];
3119           $link_data['action'] = 'update_month';
3120           $cur_month = $sunday_list[$i-1]['month'];
3121           $cur_year = $sunday_list[$i-1]['year'];
3122           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3123           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3124           $this->t->set_var('month',$sunday_list[$i-1]['month']);
3125           $this->t->set_var('year',$sunday_list[$i-1]['year']);
3126           $this->t->set_var('span',$span); $span=0;
3127           $this->t->fp('list1','month_list',True);
3128         } $span++;
3129       }
3130       $this->t->set_var('total_elders',$total_elders);
3131       $this->t->set_var('header_row',$header_row);
3132       
3133       $elder_width=200; $att_width=25; $total_width=$elder_width; 
3134       for ($i=0; $i < count($sunday_list); $i++) {
3135         $link_data['menuaction'] = 'eq.eq.att_update';
3136         $link_data['month'] = $sunday_list[$i]['month'];
3137         $link_data['year'] = $sunday_list[$i]['year'];
3138         $link_data['day'] = $sunday_list[$i]['day'];
3139         $link_data['date'] = $sunday_list[$i]['date'];
3140         $link_data['action'] = 'update_day';
3141         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3142         $this->t->set_var('date',$sunday_list[$i]['date']);
3143         $this->t->set_var('day',$sunday_list[$i]['day']);
3144         $this->t->set_var('month',$sunday_list[$i]['month']);
3145         $this->t->set_var('year',$sunday_list[$i]['year']);
3146         $this->t->fp('list2','header_list',True);
3147         $total_width += $att_width;
3148         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3149       }
3150
3151       for ($i=0; $i < count($elder_id); $i++) {
3152         $att_table = "";
3153         $this->nextmatchs->template_alternate_row_color(&$this->t);
3154         $this->t->set_var('elder_name',$elder_name[$i]);
3155         #print "checking for elder: " . $elder_id[$i] . "<br>";
3156         for ($j=0; $j < count($sunday_list); $j++) {
3157           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3158           #print "SELECT * FROM eq_attendance WHERE date='"
3159           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
3160           $sql = "SELECT * FROM eq_attendance WHERE date='"
3161              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3162           $this->db->query($sql,__LINE__,__FILE__);
3163           if($this->db->next_record()) {
3164             $cur_month = $sunday_list[$j]['month'];
3165             if($attended[$i][$cur_month] != 1) { 
3166               $attended[$i][$cur_month]=1;
3167               $attendance[$monthnum[$cur_month]]++;
3168             } 
3169             $att_table .= '<td align=center><img src="checkmark.gif"></td>';
3170           } else {
3171             $att_table .= '<td>&nbsp;</td>';
3172           }
3173         }
3174         $this->t->set_var('att_table',$att_table);
3175         $this->t->fp('list3','elder_list',True);
3176       }
3177       $this->t->set_var('total_width',$total_width);
3178       $this->t->set_var('elder_width',$elder_width);
3179       $this->t->set_var('att_width',$att_width);
3180
3181       # Now calculate attendance for these months
3182       $attendance_str = "";
3183       $nonattendance_str = "";
3184       $aveattendance_str = "";
3185       $avenonattendance_str = "";
3186       $num_months=0;
3187       $ave_total_attended=0;
3188       ksort($attendance);
3189       foreach($attendance as $att => $value) {
3190         $total_attended = $attendance[$att];
3191         $ave_total_attended += $attendance[$att]; $num_months++;
3192         $percent = ceil(($total_attended / $total_elders)*100);
3193         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3194         $total_nonattended = $total_elders - $total_attended;
3195         $percent = ceil(($total_nonattended / $total_elders)*100);
3196         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3197         
3198         $total_attended = ceil(($ave_total_attended / $num_months));
3199         $percent = ceil(($total_attended / $total_elders)*100);
3200         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3201         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
3202         $percent = ceil(($total_attended / $total_elders)*100);
3203         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3204       }
3205       
3206       $this->t->set_var('attendance',$attendance_str);
3207       $this->t->set_var('aveattendance',$aveattendance_str);
3208       $this->t->set_var('nonattendance',$nonattendance_str);
3209       $this->t->set_var('avenonattendance',$avenonattendance_str);
3210       
3211       $this->t->pfp('out','att_view_t');
3212       $this->save_sessiondata(); 
3213     }
3214
3215   function att_update()
3216     {
3217       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3218       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3219       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3220
3221       $this->t->set_file(array('form' => 'att_update.tpl'));
3222       $this->t->set_block('form','edit','edithandle');
3223       
3224       $this->t->set_block('form','month_list','list1');
3225       $this->t->set_block('form','header_list','list2');
3226       $this->t->set_block('form','elder_list','list3');
3227
3228       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3229
3230       $action = get_var('action',array('GET','POST'));
3231       $month = get_var('month',array('GET','POST'));
3232       $year = get_var('year',array('GET','POST'));
3233       $day = get_var('day',array('GET','POST'));
3234       $date = get_var('date',array('GET','POST'));
3235
3236       if($action == 'save_month' || $action == 'save_day')
3237         {
3238            $new_data = get_var('elders_attended',array('POST'));
3239            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3240
3241            if($action == 'save_month') {        
3242              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3243            }
3244
3245            if($action == 'save_day') {        
3246              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3247            }   
3248
3249            foreach ($new_data as $data)
3250            {
3251               $data_array = explode("-",$data);
3252               $elder = $data_array[0];
3253               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
3254               $this->db->query("INSERT INTO eq_attendance (elder,date) "
3255                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
3256            }
3257         
3258          $this->att_view();
3259          return false;    
3260         }
3261
3262       $sql = "SELECT * FROM eq_elder where valid=1";
3263       $this->db->query($sql,__LINE__,__FILE__);
3264       $i=0;
3265       while ($this->db->next_record())
3266         {
3267           $elder_name[$i] = $this->db->f('name');
3268           $elder_id[$i] = $this->db->f('elder');
3269           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
3270           $i++;
3271         }
3272       array_multisort($elder_name, $elder_id);
3273       
3274       if($action == 'update_month')
3275       {
3276         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
3277         $i=0; 
3278         $last_time = 0; 
3279         $found_sunday = 0;
3280         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3281         $last_date = explode("-",$sunday_list[0]['date']);
3282         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3283         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3284         while($last_time <= $time_limit)
3285         {
3286           $day = date("w",$last_time);
3287           if(date("w",$last_time) == 0) { 
3288             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3289             $last_date = explode("-",$sunday_list[$i]['date']);
3290             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3291             $sunday_list[$i]['day'] = $last_date[2];
3292             $sunday_list[$i]['month'] = date("M",$last_time);
3293             $sunday_list[$i]['year'] = $last_date[0];
3294             $found_sunday = 1; 
3295           }
3296           $last_time += 90000;
3297           if($found_sunday) { $i++; $found_sunday=0; }
3298         }
3299
3300         $this->t->set_var('span', $i);
3301         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3302         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3303         $this->t->fp('list1','month_list',True);
3304         $elder_width=200; $att_width=25; $total_width=$elder_width;
3305         for ($i=0; $i < count($sunday_list); $i++) {
3306           $link_data['menuaction'] = 'eq.eq.att_update';
3307           $link_data['month'] = $sunday_list[$i]['month'];
3308           $link_data['year'] = $sunday_list[$i]['year'];
3309           $link_data['day'] = $sunday_list[$i]['day'];
3310           $link_data['date'] = $sunday_list[$i]['date'];
3311           $link_data['action'] = 'update_day';
3312           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3313           $this->t->set_var('date',$sunday_list[$i]['date']);
3314           $this->t->set_var('day',$sunday_list[$i]['day']);
3315           $this->t->set_var('month',$sunday_list[$i]['month']);
3316           $this->t->set_var('year',$sunday_list[$i]['year']);
3317           $this->t->fp('list2','header_list',True);
3318           $total_width += $att_width;
3319         }     
3320       }
3321
3322       if($action == 'update_day')
3323       {
3324         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
3325         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3326         $this->t->set_var('month',$month);
3327         $this->t->set_var('year',$year);
3328         $this->t->fp('list1','month_list',True);
3329         $this->t->set_var('date',$date);
3330         $this->t->set_var('day',$day);
3331         $this->t->set_var('month',$month);
3332         $this->t->set_var('year',$year);
3333         $this->t->fp('list2','header_list',True);
3334       }           
3335             
3336       for ($i=0; $i < count($elder_id); $i++) {
3337         $att_table = "";
3338         $this->nextmatchs->template_alternate_row_color(&$this->t);
3339         $this->t->set_var('elder_name',$elder_name[$i]);
3340         for ($j=0; $j < count($sunday_list); $j++) {
3341           $sql = "SELECT * FROM eq_attendance WHERE date='"
3342              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3343           $this->db->query($sql,__LINE__,__FILE__);
3344           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
3345           if($this->db->next_record()) {
3346             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3347           } else if($elder_attending[$elder_id[$i]] == 1) {
3348             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3349           } else {
3350             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
3351           }
3352         }
3353         $this->t->set_var('att_table',$att_table);
3354         $this->t->fp('list3','elder_list',True);
3355       } 
3356            
3357       $this->t->set_var('lang_done', 'Cancel');
3358       $this->t->set_var('lang_reset','Clear Form');
3359       $this->t->set_var('lang_save','Save Changes');
3360
3361       $this->t->pfp('out','form');
3362       $this->t->pfp('addhandle','edit');
3363
3364       $this->save_sessiondata();       
3365     }
3366
3367   function dir_view()
3368     {
3369       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3370       $this->t->set_block('dir_view_t','dir_list','list');
3371       
3372       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
3373       $this->db->query($sql,__LINE__,__FILE__);
3374       $i=0;
3375       while ($this->db->next_record())
3376         {
3377           $parent[$i]['id'] = $this->db->f('parent');
3378           $parent[$i]['name'] = $this->db->f('name');
3379           $parent[$i]['phone'] = $this->db->f('phone');
3380           $parent[$i]['address'] = $this->db->f('address');
3381           $i++;
3382         }   
3383       
3384       for ($i=0; $i < count($parent); $i++) 
3385       {
3386         $name = $parent[$i]['name'];
3387         $phone = $parent[$i]['phone'];
3388         $address = $parent[$i]['address'];
3389         $this->t->set_var('name', $name);
3390         $this->t->set_var('address', $address);
3391         $this->t->set_var('phone', $phone);
3392         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3393         $this->t->set_var('tr_color',$tr_color);
3394         $this->t->fp('list','dir_list',True);
3395         //print "$phone $name $address<br>";
3396       }
3397       $this->t->pfp('out','dir_view_t');
3398       $this->save_sessiondata();   
3399     }
3400   
3401   function org_view()
3402     {
3403       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3404       $this->t->set_block('org_view_t','calling_list','list1');
3405       $this->t->set_block('org_view_t','org_list','list2');
3406
3407       # Display a list ordered alphabetically
3408       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
3409       $this->db->query($sql,__LINE__,__FILE__);
3410       $i=0;
3411       while ($this->db->next_record())
3412         {
3413           $calling[$i]['id'] = $this->db->f('indiv_id');
3414           $calling[$i]['name'] = $this->db->f('name');
3415           $calling[$i]['position'] = $this->db->f('position');
3416           $calling[$i]['sustained'] = $this->db->f('sustained');
3417           $calling[$i]['organization'] = $this->db->f('organization');
3418           $i++;
3419         }   
3420       for ($i=0; $i < count($calling); $i++) 
3421       {
3422         $name = $calling[$i]['name'];
3423         $position = $calling[$i]['position'];
3424         $sustained = $calling[$i]['sustained'];
3425         $organization = $calling[$i]['organization'];
3426         $this->t->set_var('name', $name);
3427         $this->t->set_var('position', $position);
3428         $this->t->set_var('sustained', $sustained);
3429         $this->t->set_var('organization', $organization);
3430         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3431         $this->t->set_var('tr_color',$tr_color);
3432         $this->t->fp('list1','calling_list',True);
3433       }
3434
3435       # Display a list ordered by organization
3436       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
3437       $this->db->query($sql,__LINE__,__FILE__);
3438       $i=0;
3439       while ($this->db->next_record())
3440         {
3441           $calling[$i]['id'] = $this->db->f('indiv_id');
3442           $calling[$i]['name'] = $this->db->f('name');
3443           $calling[$i]['position'] = $this->db->f('position');
3444           $calling[$i]['sustained'] = $this->db->f('sustained');
3445           $calling[$i]['organization'] = $this->db->f('organization');
3446           $i++;
3447         }   
3448       for ($i=0; $i < count($calling); $i++) 
3449       {
3450         $name = $calling[$i]['name'];
3451         $position = $calling[$i]['position'];
3452         $sustained = $calling[$i]['sustained'];
3453         $organization = $calling[$i]['organization'];
3454         $this->t->set_var('name', $name);
3455         $this->t->set_var('position', $position);
3456         $this->t->set_var('sustained', $sustained);
3457         $this->t->set_var('organization', $organization);
3458         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3459         $this->t->set_var('tr_color',$tr_color);
3460         $this->t->fp('list2','org_list',True);
3461       }
3462       
3463       $this->t->pfp('out','org_view_t');
3464       $this->save_sessiondata();   
3465     }
3466   
3467   function schedule()
3468     {
3469       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3470       $this->t->set_block('sched_t','presidency_list','list');
3471
3472       $action = get_var('action',array('GET','POST'));
3473       
3474       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3475       $this->t->set_var('title','EQ Scheduling Tool');
3476
3477       $this->t->set_var('lang_save','Save Schedule');
3478       $this->t->set_var('lang_reset','Cancel');
3479       
3480       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3481       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3482
3483       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3484       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3485       
3486       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3487       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3488       
3489       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3490       $table_width=$date_width + $time_width + $elder_width + $family_width;
3491       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3492       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3493       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3494       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3495       $table_data = "";
3496
3497       if($action == 'save')
3498         {
3499           $new_data = get_var('sched',array('POST'));
3500           foreach ($new_data as $presidency_array)
3501            {
3502              foreach ($presidency_array as $entry)
3503                {
3504                  $presidency = $entry['presidency'];
3505                  $appointment = $entry['appointment'];
3506                  $date = $entry['date'];
3507                  $hour = $entry['hour'];
3508                  $minute = $entry['minute'];
3509                  $pm = $entry['pm'];
3510                  $elder = $entry['elder'];
3511                  $family = $entry['family'];
3512                  if($pm) { $hour = $hour + 12; }
3513                  $time = $hour.':'.$minute.':'.'00';
3514                  $uid = 0;
3515
3516                  // Zero out the family or elder if date = NULL
3517                  if($date == "") {
3518                    $elder = 0;
3519                    $family = 0;
3520                  }
3521                  
3522                  // Update an existing appointment
3523                  if($appointment < $this->max_appointments)
3524                    {
3525                      //Only perform a database update if we have made a change to this appointment
3526                      $sql = "SELECT * FROM eq_appointment where " .
3527                         "appointment='$appointment'" .
3528                         " and presidency='$presidency'" .
3529                         " and elder='$elder'" .
3530                         " and family='$family'" .
3531                         " and date='$date'" .
3532                         " and time='$time'";
3533                      $this->db->query($sql,__LINE__,__FILE__);
3534                      if(!$this->db->next_record()) {
3535                        $old_date = $this->db->f('date');
3536                        $old_time = $this->db->f('time');
3537                        $this->db2->query("UPDATE eq_appointment set" .
3538                                         " family=" . $family . 
3539                                         " ,elder=" . $elder . 
3540                                         " ,date='" . $date . "'" .
3541                                         " ,time='" . $time . "'" .
3542                                         " ,presidency='" . $presidency . "'" .
3543                                         " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3544                        
3545                        // Email the appointment
3546                        $this->email_appt($appointment);
3547                      }
3548                    }
3549                  
3550                  // Add a new appointment
3551                  else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3552                    {
3553                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";
3554                      $this->db2->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time,uid) "
3555                            . "VALUES (NULL,'" . $presidency . "','" . $family . "','"
3556                            . $elder . "','" . $date . "','" . $time  . "','" . $uid ."')",__LINE__,__FILE__);
3557
3558                      // Now reselect this entry from the database to see if we need
3559                      // to send an appointment out for it.
3560                      $sql = "SELECT * FROM eq_appointment where " .
3561                         "elder='$elder'" .
3562                         " and family='$family'" .
3563                         " and presidency='$presidency'" .
3564                         " and date='$date'" .
3565                         " and time='$time'" .
3566                         " and uid='$uid'";
3567                      $this->db3->query($sql,__LINE__,__FILE__);
3568                      if($this->db3->next_record()) {
3569                        // Email the appointment if warranted
3570                        if(($date != "") && ($time != "") && (($elder > 0) || $family > 0)) { 
3571                          $this->email_appt($this->db3->f('appointment'));
3572                        }
3573                      }
3574                    }
3575                }
3576            }
3577           
3578           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3579           Header('Location: ' . $take_me_to_url);
3580         }
3581
3582       $sql = "SELECT * FROM eq_presidency where valid=1";
3583       $this->db->query($sql,__LINE__,__FILE__);
3584       $i=0;
3585       while ($this->db->next_record())
3586         {
3587           $presidency_data[$i]['id'] = $this->db->f('presidency');
3588           $presidency_data[$i]['name'] = $this->db->f('name');
3589           $presidency_data[$i]['elder'] = $this->db->f('elder');
3590           $i++;
3591         }
3592       
3593       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3594       $this->db->query($sql,__LINE__,__FILE__);
3595       $i=0;
3596       while ($this->db->next_record())
3597         {
3598           $elder_id[$i] = $this->db->f('elder');
3599           $elder_name[$i] = $this->db->f('name');
3600           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3601           $i++;
3602         }
3603       array_multisort($elder_name, $elder_id);
3604
3605       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3606       $this->db->query($sql,__LINE__,__FILE__);
3607       $i=0;
3608       while ($this->db->next_record())
3609         {
3610           $family_id[$i] = $this->db->f('family');
3611           $family_name[$i] = $this->db->f('name');
3612           $i++;
3613         }
3614       array_multisort($family_name, $family_id);
3615       
3616       for ($i=0; $i < count($presidency_data); $i++) {
3617         $presidency = $presidency_data[$i]['id'];
3618         $interviewer = $presidency_data[$i]['elder'];
3619         $name = $presidency_data[$i]['name'];
3620         $this->t->set_var('presidency_name',$name);
3621         $table_data="";
3622         
3623         // query the database for all the appointments
3624         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3625         $this->db->query($sql,__LINE__,__FILE__);
3626
3627         // Prefill any existing appointment slots
3628         while ($this->db->next_record())
3629           {
3630             $appointment = $this->db->f('appointment');
3631             $elder = $this->db->f('elder');
3632             $family = $this->db->f('family');
3633             
3634             $date = $this->db->f('date');
3635             $date_array = explode("-",$date);
3636             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3637             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3638             
3639             $time = $this->db->f('time');
3640             $time_array = explode(":",$time);
3641             $hour = $time_array[0];
3642             $minute = $time_array[1];
3643             $pm = 0;
3644             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3645             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3646             
3647             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3648
3649             // Date selection
3650             $table_data.= '<td align=left>';
3651             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3652             $table_data.= '</td>';
3653             
3654             // Hour & Minutes selection
3655             $table_data.= "<td align=center>";
3656             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3657             foreach(range(1,12) as $num) {
3658               if($hour == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3659               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3660             }
3661             $table_data.= '</select>';
3662             $table_data.= '&nbsp;:&nbsp;';
3663             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3664             foreach(range(0,3) as $num) {
3665               $num = $num * 15; if($num == 0) { $num = "00"; }
3666               if($minute == $num) { $selected[$num] = 'selected="selected"'; } else { $selected[$num] = ''; }
3667               $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3668             }
3669             $table_data.= '</select>';
3670             $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3671             if($pm == 0) { $table_data.= '<option value=0 selected>am</option>'; $table_data.= '<option value=1>pm</option>'; }
3672             else { $table_data.= '<option value=0>am</option>'; $table_data.= '<option value=1 selected>pm</option>'; }
3673             $table_data.= '</select>';
3674             $table_data.= "</td>";
3675             
3676             // Elder drop down list (for PPIs)
3677             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3678             $table_data.= '<option value=0></option>';  
3679             for ($j=0; $j < count($elder_id); $j++) {
3680               $id = $elder_id[$j];
3681               $name = $elder_name[$j];
3682               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3683               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3684             }
3685             $table_data.='</select></td>';
3686
3687             // Family drop down list (for Visits)
3688             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3689             $table_data.= '<option value=0></option>';              
3690             for ($j=0; $j < count($elder_id); $j++) {
3691               $id = $family_id[$j];
3692               $name = $family_name[$j];
3693               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3694               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3695             }
3696             $table_data.='</select></td>';
3697             
3698             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3699             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3700         
3701             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3702             $this->t->set_var('tr_color',$tr_color);
3703             
3704           }
3705
3706         // Create blank appointment slot
3707         for ($b=0; $b < 4; $b++) {
3708           $appointment = $this->max_appointments + $b;
3709           $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3710
3711           // Date selection
3712           $table_data.= '<td align=left>';
3713           $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3714           $table_data.= '</td>';
3715         
3716           // Time selection
3717           $table_data.= "<td align=center>";
3718           $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
3719           $table_data.= '<option value=""></option>';
3720           foreach(range(1,12) as $num) {
3721             $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
3722           }
3723           $table_data.= '</select>';
3724           $table_data.= '&nbsp;:&nbsp;';
3725           $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
3726           $table_data.= '<option value=""></option>';
3727           foreach(range(0,3) as $num) {
3728             $num = $num * 15; if($num == 0) { $num = "00"; }
3729             $table_data.= '<option value='.$num.'>'.$num.'</option>';
3730           }
3731           $table_data.= '</select>';
3732           $table_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
3733           $table_data.= '<option value=""></option>';
3734           $table_data.= '<option value=0>am</option>';
3735           $table_data.= '<option value=1>pm</option>';
3736           $table_data.= '</select>';
3737           $table_data.= "</td>";
3738           
3739           // Elder drop down list
3740           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3741           $table_data.= '<option value=0></option>';  
3742           for ($j=0; $j < count($elder_id); $j++) {
3743             $id = $elder_id[$j];
3744             $name = $elder_name[$j];
3745             $table_data.= '<option value='.$id.'>'.$name.'</option>';
3746           }
3747           $table_data.='</select></td>';
3748           
3749           // Family drop down list
3750           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3751           $table_data.= '<option value=0></option>';        
3752           for ($j=0; $j < count($elder_id); $j++) {
3753             $id = $family_id[$j];
3754             $name = $family_name[$j];
3755             $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3756           }
3757           $table_data.='</select></td>';
3758           
3759           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3760           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3761
3762           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3763           $this->t->set_var('tr_color',$tr_color);
3764         }
3765         
3766         $this->t->set_var('table_data',$table_data);
3767         $this->t->set_var('header_row',$header_row);
3768         $this->t->set_var('table_width',$table_width);
3769         $this->t->fp('list','presidency_list',True);
3770         
3771       }
3772       
3773       $this->t->pfp('out','sched_t');
3774       $this->save_sessiondata();   
3775     }
3776   
3777   function admin()
3778     {
3779       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3780       $this->t->set_block('admin_t','upload','uploadhandle');
3781       $this->t->set_block('admin_t','admin','adminhandle');
3782       $this->t->set_block('admin_t','cmd','cmdhandle');
3783       
3784       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3785       
3786       $action = get_var('action',array('GET','POST'));
3787
3788       $this->t->pfp('out','admin_t');
3789       
3790       if($action == 'upload')
3791         {        
3792           $target_path = $this->upload_target_path . basename( $_FILES['uploadedfile']['name']);
3793           
3794           if((($_FILES['uploadedfile']['type'] == "application/zip") ||
3795               ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3796               ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3797               ($_FILES['uploadedfile']['type'] == "application/octet-stream")) &&
3798              (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))) {
3799             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3800             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3801             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3802             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3803             $this->t->set_var('uploadstatus',$uploadstatus);
3804             $this->t->pfp('uploadhandle','upload');
3805             $this->t->set_var('uploadhandle','');
3806             print "<table border=1 width=80%><tr><td>\n<pre>";
3807             
3808             # make a directory for this data to be stored in
3809             $date="data_" . date("Y_m_d");
3810             $data_dir = $this->upload_target_path . $date;
3811             print "-> Making the data directory: $date<br>\n";
3812             exec('mkdir ' . $data_dir . ' 2>&1', $result, $return_code);
3813             if($return_code != 0) {
3814               print implode('\n',$result) . "<br>";
3815               print "<b><font color=red>";
3816               print "-E- Unable to create the data directory. Aborting import.";
3817               print "</font></b>";
3818               return 0;
3819             }
3820
3821             # move the file uploaded into this directory
3822             print "-> Moving the uploaded file into the data dir<br>\n";
3823             exec('mv ' . $target_path . ' ' . $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 move the uploaded file into the data dir. Aborting import.";
3828               print "</font></b>";
3829               return 0;
3830             }
3831             
3832             # unzip the data into this directory
3833             print "-> Unzipping the data<br>\n";
3834             $data_file = $data_dir . '';
3835             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3836             if($return_code != 0) {
3837               print implode('\n',$result) . "<br>";
3838               print "<b><font color=red>";
3839               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
3840               print "</font></b>";
3841               return 0;
3842             }
3843             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3844
3845             # update the data_latest link to point to this new directory
3846             print "-> Updating the latest data dir link<br>\n";
3847             $data_latest = $this->upload_target_path . 'data_latest';
3848             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3849             if($return_code != 0) {
3850               print implode('\n',$result) . "<br>";
3851               print "<b><font color=red>";
3852               print "-E- Unable to update the data latest link. Aborting import.";
3853               print "</font></b>";
3854               return 0;
3855             }
3856             
3857             # run the import perl script to encorporate it into the DB
3858             ob_start('ob_logstdout', 2);
3859             print "-> Importing the data into the EQ database<br>\n";
3860             ob_flush(); flush(); sleep(1);
3861             $import_log = $this->upload_target_path . '/import.log';
3862             $data_log = $this->upload_target_path . '/data.log';
3863             $import_cmd = $this->script_path . 'import_ward_data ' . $data_latest . ' | tee ' . $import_log;
3864             $parse_cmd = $this->script_path . 'parse_ward_data -v ' . $data_latest . ' > ' . $data_log;
3865             #print "import_cmd: $import_cmd<br>";
3866             #print "parse_cmd: $parse_cmd<br>";
3867             ob_start('ob_logstdout', 2);
3868             passthru($import_cmd);
3869             passthru($parse_cmd);
3870             ob_flush(); flush(); sleep(1);
3871
3872             # fix the permissions of the data dir
3873             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3874             
3875             $this->t->pfp('cmdhandle','cmd');
3876             print "</pre></td></tr></table>";
3877             
3878           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3879                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3880                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3881                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3882             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3883             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3884             $this->t->set_var('uploadstatus',$uploadstatus);
3885             $this->t->pfp('uploadhandle','upload',True);
3886           } else {
3887             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3888             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3889             $this->t->set_var('uploadstatus',$uploadstatus);
3890             $this->t->pfp('uploadhandle','upload',True);
3891           }
3892         }
3893       else
3894         {
3895           $this->t->set_var('adminhandle','');
3896           $this->t->pfp('adminhandle','admin'); 
3897         }
3898       
3899       $this->save_sessiondata();   
3900     }
3901
3902   function email_appt($appointment)
3903     {
3904       //print "Emailing notification of appointment: $appointment <br>";
3905
3906       $sql = "SELECT * FROM eq_appointment where appointment='$appointment'";
3907       $this->db->query($sql,__LINE__,__FILE__);
3908         
3909       while ($this->db->next_record())
3910         {
3911           $appointment = $this->db->f('appointment');
3912           $presidency = $this->db->f('presidency');
3913           $interviewer = "";
3914           $email = "";
3915           $elder = $this->db->f('elder');
3916           $elder_name = "";
3917           $family = $this->db->f('family');
3918           $family_name = "";
3919           $appt_name = "";
3920           $phone = "";
3921           $location = "";
3922           $uid = $this->db->f('uid');
3923                     
3924           // Extract the year, month, day, hours, minutes, seconds from the appointment time
3925           $appt_date = $this->db->f('date');
3926           $date_array = explode("-",$appt_date);
3927           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3928           $appt_time = $this->db->f('time');
3929           $time_array = explode(":",$appt_time);
3930           $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
3931
3932           // Format the appointment time into an iCal UTC equivalent
3933           $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
3934           $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
3935           
3936           // Set the email address of the person making the appointment
3937           $from = $GLOBALS['phpgw_info']['user']['fullname'] . "<" .
3938                   $GLOBALS['phpgw_info']['user']['preferences']['email']['address'] . ">";
3939           
3940           $sql = "SELECT * FROM eq_presidency where presidency='$presidency'";
3941           $this->db2->query($sql,__LINE__,__FILE__);
3942           if($this->db2->next_record()) {
3943             $email = $this->db2->f('email');
3944             $interviewer = $this->db2->f('name');
3945           }
3946
3947           if($elder > 0) { 
3948             $sql = "SELECT * FROM eq_elder where elder='$elder'";
3949             $this->db2->query($sql,__LINE__,__FILE__);
3950             if($this->db2->next_record()) {
3951               $elder_name = $this->db2->f('name');
3952               $phone = $this->db2->f('phone');
3953               $appt_name = $elder_name . " Interview";
3954               $location = "$interviewer"."'s home";
3955               $duration = 1800; // 30 minutes
3956             }
3957           }
3958
3959           if($family > 0) { 
3960             $sql = "SELECT * FROM eq_family where family='$family'";
3961             $this->db2->query($sql,__LINE__,__FILE__);
3962             if($this->db2->next_record()) {
3963               $family_name = $this->db2->f('name');
3964               $phone = $this->db2->f('phone');
3965               $elder_id = $this->db2->f('elder_id');
3966               $appt_name = $family_name . " Family Visit";
3967               $sql = "SELECT * FROM eq_elder where elder='$elder_id'";
3968               $this->db3->query($sql,__LINE__,__FILE__);
3969               if($this->db3->next_record()) {
3970                 $phone = $this->db3->f('phone');
3971               }
3972               $sql = "SELECT * FROM eq_parent where family='$family'";
3973               $this->db3->query($sql,__LINE__,__FILE__);
3974               if($this->db3->next_record()) {
3975                 $location=$this->db3->f('address');
3976               }
3977               $duration = 2700; // 45 minutes
3978             }
3979           }
3980
3981           $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
3982           $description = "$appt_name : $phone";
3983           
3984           if(($uid == 0) && ($appt_name != "")) { 
3985             // Create a new calendar item for this appointment, since this must be the first time we
3986             // are sending it out.
3987             print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
3988             $uid = rand() . rand(); // Generate a random identifier for this appointment
3989             $subject = "Created: $appt_name";
3990             
3991             $this->db->query("UPDATE eq_appointment set" .
3992                              " uid=" . $uid . 
3993                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3994
3995             $action = "PUBLISH";
3996             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
3997                                   $dtend, $location, $appt_name, $description, $uid);
3998             
3999           } else if(($uid != 0) && ($appt_name == "")) {
4000             // Remove the calendar item for this appointment since it has already been sent
4001             // and there is no name we have changed it to.
4002             print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4003             $subject = "Canceled: $appt_date $appt_time";
4004             
4005             $this->db->query("UPDATE eq_appointment set" .
4006                              " uid=0" . 
4007                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4008             
4009             $action = "CANCEL";
4010             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4011                                   $dtend, $location, $appt_name, $description, $uid);
4012             
4013           } else if($uid != 0) {
4014             // Update the existing appointment since we have changed it
4015             print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4016
4017             $subject = "Canceled: $appt_date $appt_time";
4018             $action = "CANCEL";
4019             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4020                                   $dtend, $location, $appt_name, $description, $uid);
4021             
4022             $uid = rand() . rand(); // Generate a random identifier for this appointment
4023             $this->db->query("UPDATE eq_appointment set" .
4024                              " uid=" . $uid .
4025                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4026             
4027             $subject = "Updated: $appt_name";       
4028             $action = "PUBLISH";
4029             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4030                                   $dtend, $location, $appt_name, $description, $uid);
4031           }
4032           
4033         }
4034           
4035       return true;
4036     }
4037
4038   function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $location, $summary, $description, $uid)
4039     {
4040       $headers = 'From: ' . "$from" . "\n" .
4041          'Reply-To: ' . "$from" . "\n" .
4042          'X-Mailer: PHP/' . phpversion() . "\n" .
4043          'Content-Type: text/calendar;' . "\n" .
4044          'Content-Transfer-Encoding: 7bit' . "\n";
4045       
4046       //$message = "phone: $phone date: $date time: $time";
4047       $message ="";
4048       $message.="BEGIN:VCALENDAR" . "\n";
4049       $message.="VERSION:2.0" . "\n";
4050       $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4051       $message.="METHOD:$action" . "\n";
4052       $message.="BEGIN:VEVENT" . "\n";
4053       $message.="ORGANIZER:MAILTO:$from". "\n";
4054       $message.="DTSTAMP:$dtstamp" . "\n";
4055       $message.="DTSTART:$dtstart" . "\n";
4056       $message.="DTEND:$dtend" . "\n";
4057       $message.="SUMMARY:$summary" . "\n";
4058       $message.="DESCRIPTION:$description" . "\n";
4059       $message.="LOCATION:$location" . "\n";
4060       $message.="UID:$uid" ."\n";
4061       $message.="TRANSP:OPAQUE" . "\n";
4062       $message.="SEQUENCE:0" . "\n";
4063       $message.="CLASS:PUBLIC" . "\n";
4064       $message.="END:VEVENT" . "\n";
4065       $message.="END:VCALENDAR" . "\n";
4066       
4067       mail($to, $subject, $message, $headers);
4068       
4069     }
4070 }
4071
4072 ?>