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