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