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