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