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