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