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