747a9a5ab2e2293b5f6bd9e494bb03f75d8921d9
[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['interview'] = '';
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['interview'] = $this->db2->f('int');
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;
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->fp('int_list','interviewer_list',True);
2460     
2461       if($action == 'save')
2462         {
2463           $notes = get_var('notes',array('POST'));
2464           $this->db->query("UPDATE eq_ppi set " .
2465                            "   ppi='" . $ppi . "'" .
2466                     ", interviewer='" . $interviewer . "'" .
2467                           ", elder='" . $elder . "'" .
2468                         ", aaronic='" . $aaronic . "'" .
2469                            ", date='" . $date . "'" .
2470                           ", notes='" . $notes . "'" .
2471                       ", eqpresppi='" . $eqpresppi . "'" .
2472                            " WHERE ppi=" . $ppi,__LINE__,__FILE__);
2473           $this->ppi_view();
2474           return false;
2475         }
2476
2477       if($action == 'insert')
2478         {
2479           $notes = get_var('notes',array('POST'));
2480           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes,eqpresppi) "
2481                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2482                            . $date . "','" . $notes . "','" . $eqpresppi  ."')",__LINE__,__FILE__);
2483           $this->ppi_view();
2484           return false;
2485         }
2486       
2487       if($action == 'add')
2488         {
2489           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2490           $this->t->set_var('ppi', '');
2491           $this->t->set_var('interviewer', $interviewer);
2492           $this->t->set_var('name',$name);
2493           $this->t->set_var('elder',$elder);
2494           $this->t->set_var('date','');
2495           $this->t->set_var('notes','');
2496           $this->t->set_var('eqpresppi',$eqpresppi);
2497           $this->t->set_var('lang_done','Cancel');
2498           $this->t->set_var('lang_action','Adding New PPI');
2499           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2500                                                                 . $ppi . '&action=' . 'insert'));
2501         }
2502
2503       if($action == 'edit' || $action == 'view')
2504         {
2505           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$ppi;
2506           $this->db->query($sql,__LINE__,__FILE__);
2507           $this->db->next_record();
2508           $this->t->set_var('ppi',$ppi);
2509           $this->t->set_var('name',$name);
2510           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2511           $this->t->set_var('elder',$this->db->f('elder'));
2512           $this->t->set_var('date',$this->db->f('date'));
2513           $this->t->set_var('notes',$this->db->f('notes'));
2514           $this->t->set_var('eqpresppi',$this->db->f('eqpresppi'));
2515         }
2516       
2517       if($action == 'edit')
2518         {
2519           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2520           $this->t->set_var('lang_done','Cancel');
2521           $this->t->set_var('lang_action','Editing PPI');
2522           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2523                                                                 . $ppi . '&action=' . 'save'));
2524         }
2525
2526       if($action == 'view')
2527         {
2528           $date = $this->db->f('date');
2529           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2530           $this->t->set_var('readonly','READONLY');
2531           $this->t->set_var('disabled','DISABLED');
2532           $this->t->set_var('lang_done','Done');
2533           $this->t->set_var('lang_action','Viewing PPI');
2534           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_update&ppi='
2535                                                                 . $ppi . '&action=' . 'edit'));
2536         }
2537       
2538       $this->t->set_var('lang_reset','Clear Form');
2539       $this->t->set_var('lang_add','Add PPI');
2540       $this->t->set_var('lang_save','Save Changes');
2541       $this->t->set_var('edithandle','');
2542       $this->t->set_var('addhandle','');
2543
2544       $this->t->pfp('out','form');
2545       
2546       if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2547       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2548       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2549      
2550       $this->save_sessiondata(); 
2551     }
2552
2553     function int_view()
2554     {
2555       $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2556       $this->t->set_block('int_view_t','district_list','list');
2557
2558       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2559       $num_quarters = get_var('num_quarters',array('GET','POST'));
2560       if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2561       $this->t->set_var('num_quarters',$num_quarters);
2562       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
2563       else {  $this->t->set_var('lang_num_quarters','Quarters of History'); }
2564       $this->t->set_var('lang_filter','Filter');
2565       
2566       $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2567       $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2568       
2569       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
2570       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2571       
2572       $this->t->set_var('title','Hometeaching Interviews'); 
2573
2574       $num_months = $num_quarters * 3 - 1;
2575       $current_month = $this->current_month;
2576       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2577       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2578       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2579       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2580
2581       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2582       $this->db->query($sql,__LINE__,__FILE__);
2583       $i=0;
2584       while ($this->db->next_record())
2585         {
2586           $districts[$i]['district'] = $this->db->f('district');
2587           $districts[$i]['name'] = $this->db->f('name');
2588           $districts[$i]['supervisor'] = $this->db->f('supervisor');
2589           $i++;
2590         }
2591
2592       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
2593       $this->db->query($sql,__LINE__,__FILE__);
2594       $i=0;
2595       while ($this->db->next_record())
2596         {
2597           $elder_id[$i] = $this->db->f('elder');
2598           $elder_name[$i] = $this->db->f('name');
2599           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
2600           $i++;
2601         }
2602       array_multisort($elder_name, $elder_id);
2603       for($i=0; $i < count($elder_id); $i++) {
2604           $id = $elder_id[$i];
2605           $elders[$id] = $elder_name[$i];
2606       }      
2607
2608       $sql = "SELECT * FROM eq_aaronic where valid=1 ORDER BY aaronic ASC";
2609       $this->db->query($sql,__LINE__,__FILE__);
2610       while ($this->db->next_record())
2611         {
2612           $aaronic_id = $this->db->f('aaronic');
2613           $aaronic[$aaronic_id]['name'] = $this->db->f('name');
2614           $aaronic[$aaronic_id]['phone'] = $this->db->f('phone');
2615         }
2616       
2617       $total_companionships = 0;
2618       $this->nextmatchs->template_alternate_row_color(&$this->t);
2619       for ($i=0; $i < count($districts); $i++) {
2620         $this->t->set_var('district_number',$districts[$i]['district']);
2621         $this->t->set_var('district_name',$districts[$i]['name']);      
2622         $supervisor = $districts[$i]['supervisor'];
2623         $unique_companionships='';
2624                 
2625         // Select all the unique companionship numbers for this district
2626         $sql = "SELECT distinct companionship FROM eq_companionship where valid=1 and district=". $districts[$i]['district'];
2627
2628         $this->db->query($sql,__LINE__,__FILE__);
2629         $j=0;
2630         while ($this->db->next_record())
2631           {
2632             $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2633             $j++;
2634           }
2635         
2636         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2637         $table_data=""; $num_companionships = $j; $num_elders = 0;
2638         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2639         for ($j=0; $j < count($unique_companionships); $j++) {
2640           // Select all the companions in each companionship
2641           $sql = "SELECT * FROM eq_companionship where valid=1 and ".
2642              "companionship=". $unique_companionships[$j]['companionship'];
2643           $this->db->query($sql,__LINE__,__FILE__);
2644           $k=0;
2645           $comp = $unique_companionships[$j]['companionship'];
2646           for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2647           while ($this->db->next_record())
2648             {
2649               // Get this companions information
2650               $num_elders++;
2651               $companionship = $this->db->f('companionship');
2652               $elder_id = $this->db->f('elder');
2653               $aaronic_id = $this->db->f('aaronic');
2654               if($elder_id) {
2655                 $name = $elders[$elder_id];
2656                 $phone = $elder_phone[$elder_id];
2657               }
2658               else if($aaronic_id) {
2659                 $name = $aaronic[$aaronic_id]['name'];
2660                 $phone = $aaronic[$aaronic_id]['phone'];
2661               }
2662               $link_data['menuaction'] = 'eq.eq.int_update';
2663               $link_data['companionship'] = $companionship;
2664               $link_data['interviewer'] = $supervisor;
2665               $link_data['elder'] = $elder_id;
2666               $link_data['aaronic'] = $aaronic_id;
2667               $link_data['name'] = $name;
2668               $link_data['interview'] = '';
2669               $link_data['action'] = 'add';
2670               $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2671               $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2672
2673               // Find out how many times Interviews were performed in the past $num_months for this Elder
2674               $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2675               for($m=$num_months; $m >= 0; $m--) {
2676                 $month = $current_month - $m;
2677                 $year = $this->current_year;
2678                 if($month <= 0) { $remainder = $month; $month = 12 + $remainder; $year=$year-1; }
2679                 if($month < 10) { $month = "0"."$month"; }
2680                 $month_start = "$year"."-"."$month"."-"."01";
2681                 $month_end = "$year"."-"."$month"."-"."31";
2682                 $month = "$month"."/"."$year";
2683                 $sql = "SELECT * FROM eq_ppi WHERE date >= '$month_start' AND date <= '$month_end' ".
2684                    "AND elder=" . $elder_id . " AND aaronic=" . $aaronic_id;
2685                 $this->db2->query($sql,__LINE__,__FILE__);
2686                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2687               
2688                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2689                 if($this->db2->next_record()) {
2690                   if(!$int_recorded[$companionship][$m]) {
2691                     $ints[$m]++; $total_ints[$m]++; $int_recorded[$companionship][$m]=1;
2692                   }
2693                   $link_data['menuaction'] = 'eq.eq.int_update';
2694                   $link_data['companionship'] = $companionship;
2695                   $link_data['interviewer'] = $this->db2->f('interviewer');
2696                   $link_data['elder'] = $elder_id;
2697                   $link_data['aaronic'] = $aaronic_id;
2698                   $link_data['name'] = $name;
2699                   $link_data['interview'] = $this->db2->f('interview');
2700                   $link_data['action'] = 'view';
2701                   $date = $this->db2->f('date');
2702                   $date_array = explode("-",$date);
2703                   $month = $date_array[1];
2704                   $day   = $date_array[2];
2705                   $link = $GLOBALS['phpgw']->link('/eq/index.php',$link_data);
2706                   $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2707                 }
2708                 else { $table_data .= "<td>&nbsp;</td>"; }
2709               }
2710               $table_data .= "</tr>"; 
2711               $k++;
2712             }
2713           $table_data .= "<tr><td colspan=20><hr></td></tr>";
2714         }
2715         $total_companionships += $num_companionships;
2716         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Quarterly Totals:</font></b></td>";
2717
2718         // Print the hometeaching interview stats
2719         if($this->monthly_hometeaching_interviews == 0) { //Quarterly
2720           for($m=$num_months; $m >=0; $m--) {
2721             $month = $current_month - $m;
2722             if(($month % 3) == 1) { $quarter_total = $ints[$m]; }
2723             else { $quarter_total += $ints[$m]; }
2724             $percent = ceil(($quarter_total / $num_companionships)*100);
2725             $stat_data .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2726           }
2727           $stat_data .= "</tr>";
2728         }
2729         else { // Monthly
2730           for($m=$num_months; $m >=0; $m--) {
2731             $percent = ceil(($ints[$m] / $num_companionships)*100);
2732             $stat_data .= "<td align=center><font size=-2><b>$ints[$m]<br>$percent%</font></b></td>";
2733           }
2734           $stat_data .= "</tr>";
2735         }
2736         
2737         $this->t->set_var('table_width',$table_width);
2738         $this->t->set_var('header_row',$header_row);
2739         $this->t->set_var('table_data',$table_data);
2740         $this->t->set_var('stat_data',$stat_data);
2741         $this->t->fp('list','district_list',True);
2742       }
2743
2744       // Display the totals
2745       if($this->monthly_hometeaching_interviews == 0) { //Quarterly
2746         $quarter_total = 0;
2747         $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Quarterly Totals:</font></b></td>";
2748         for($m=$num_months; $m >=0; $m--) {
2749           $month = $current_month - $m;
2750           if(($month % 3) == 1) { $quarter_total = $total_ints[$m]; }
2751           else { $quarter_total += $total_ints[$m]; }
2752           $percent = ceil(($quarter_total / $total_companionships)*100);
2753           $totals .= "<td align=center><font size=-2><b>$quarter_total<br>$percent%</font></b></td>";
2754         }
2755         $totals .= "</tr>";
2756       }
2757       else { //Monthly
2758         $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Monthly Totals:</font></b></td>";
2759         for($m=$num_months; $m >=0; $m--) {
2760           $percent = ceil(($total_ints[$m] / $total_companionships)*100);
2761           $totals .= "<td align=center><font size=-2><b>$total_ints[$m]<br>$percent%</font></b></td>";
2762         }
2763         $totals .= "</tr>";
2764       }
2765       
2766       $this->t->set_var('totals',$totals);
2767       $this->t->pfp('out','int_view_t');
2768       $this->save_sessiondata(); 
2769     }
2770
2771   function int_update()
2772     {
2773       $this->t->set_file(array('form' => 'int_update.tpl'));
2774       $this->t->set_block('form','interviewer_list','int_list');
2775       $this->t->set_block('form','add','addhandle');
2776       $this->t->set_block('form','edit','edithandle');
2777       
2778       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_view'));
2779       $this->t->set_var('readonly','');
2780       $this->t->set_var('disabled','');
2781       
2782       $action = get_var('action',array('GET','POST'));
2783       $companionship = get_var('companionship',array('GET','POST'));
2784       $interviewer = get_var('interviewer',array('GET','POST'));      
2785       $name = get_var('name',array('GET','POST'));
2786       $interview = get_var('interview',array('GET','POST'));
2787       $elder = get_var('elder',array('GET','POST'));
2788       $aaronic = get_var('aaronic',array('GET','POST'));
2789       $date = get_var('date',array('GET','POST'));
2790       $notes = get_var('notes',array('GET','POST'));
2791       
2792       $sql = "SELECT * FROM eq_district where valid=1 ORDER BY district ASC";
2793       $this->db->query($sql,__LINE__,__FILE__);
2794       while ($this->db->next_record())
2795         {
2796           $supervisor = $this->db->f('supervisor');
2797           $sql = "SELECT * FROM eq_elder WHERE elder=" . $supervisor;
2798           $this->db2->query($sql,__LINE__,__FILE__);
2799           $this->db2->next_record();
2800           $interviewer_name = $this->db2->f('name');
2801           
2802           if($supervisor == $interviewer) { 
2803             $this->t->set_var('interviewer',$supervisor . ' selected');
2804           } else {
2805             $this->t->set_var('interviewer',$interviewer);
2806           }
2807           $this->t->set_var('interviewer_name',$interviewer_name);
2808           $this->t->fp('int_list','interviewer_list',True);
2809         }
2810       
2811       if($action == 'save')
2812         {
2813           $notes = get_var('notes',array('POST'));
2814           $this->db->query("UPDATE eq_ppi set " .
2815                            "   ppi='" . $interview . "'" .
2816                     ", interviewer='" . $interviewer . "'" .
2817                           ", elder='" . $elder . "'" .
2818                         ", aaronic='" . $aaronic . "'" .
2819                            ", date='" . $date . "'" .
2820                           ", notes='" . $notes . "'" .
2821                            " WHERE ppi=" . $interview,__LINE__,__FILE__);
2822           $this->int_view();
2823           return false;
2824         }
2825
2826       if($action == 'insert')
2827         {
2828           $notes = get_var('notes',array('POST'));
2829           $this->db->query("INSERT INTO eq_ppi (interviewer,elder,aaronic,date,notes) "
2830                            . "VALUES ('" . $interviewer . "','" . $elder . "','" . $aaronic . "','"
2831                            . $date . "','" . $notes ."')",__LINE__,__FILE__);
2832           $this->int_view();
2833           return false;
2834         }
2835       
2836       if($action == 'add')
2837         {
2838           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2839           $this->t->set_var('interview', '');
2840           $this->t->set_var('interviewer', $interviewer);
2841           $this->t->set_var('name',$name);
2842           $this->t->set_var('elder',$elder);
2843           $this->t->set_var('aaronic',$aaronic);
2844           $this->t->set_var('date','');
2845           $this->t->set_var('notes','');
2846           $this->t->set_var('lang_done','Cancel');
2847           $this->t->set_var('lang_action','Adding New Interview');
2848           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2849                                                                 . $interview . '&action=' . 'insert'));
2850         }
2851
2852       if($action == 'edit' || $action == 'view')
2853         {
2854           $sql = "SELECT * FROM eq_ppi WHERE ppi=".$interview;
2855           $this->db->query($sql,__LINE__,__FILE__);
2856           $this->db->next_record();
2857           $this->t->set_var('interview',$interview);
2858           $this->t->set_var('name',$name);
2859           $this->t->set_var('interviewer', $this->db->f('interviewer'));
2860           $this->t->set_var('elder',$this->db->f('elder'));
2861           $this->t->set_var('aaronic',$this->db->f('aaronic'));
2862           $this->t->set_var('date',$this->db->f('date'));
2863           $this->t->set_var('notes',$this->db->f('notes'));
2864         }
2865       
2866       if($action == 'edit')
2867         {
2868           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2869           $this->t->set_var('lang_done','Cancel');
2870           $this->t->set_var('lang_action','Editing Interview');
2871           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2872                                                                 . $interview . '&action=' . 'save'));
2873         }
2874
2875       if($action == 'view')
2876         {
2877           $date = $this->db->f('date');
2878           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2879           $this->t->set_var('readonly','READONLY');
2880           $this->t->set_var('disabled','DISABLED');
2881           $this->t->set_var('lang_done','Done');
2882           $this->t->set_var('lang_action','Viewing Interview');
2883           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_update&interview='
2884                                                                 . $interview . '&action=' . 'edit'));
2885         }
2886       
2887       $this->t->set_var('lang_reset','Clear Form');
2888       $this->t->set_var('lang_add','Add Interview');
2889       $this->t->set_var('lang_save','Save Changes');
2890       $this->t->set_var('edithandle','');
2891       $this->t->set_var('addhandle','');
2892
2893       $this->t->pfp('out','form');
2894       
2895       if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2896       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2897       if($action == 'add') { $this->t->pfp('addhandle','add'); }
2898      
2899       $this->save_sessiondata(); 
2900     }
2901
2902   function vis_view()
2903     {
2904       $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2905       $this->t->set_block('vis_view_t','visit_list','list1');
2906       $this->t->set_block('vis_view_t','family_list','list2');
2907
2908       $this->t->set_var('lang_name','Family Name');
2909       $this->t->set_var('lang_date','Date');
2910
2911       $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2912       $this->t->set_var('vis_link_title','View Yearly Visits');
2913       
2914       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
2915       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2916
2917       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
2918       $num_years = get_var('num_years',array('GET','POST'));
2919       if($num_years == '') { $num_years = $this->default_vis_num_years; }
2920       $this->t->set_var('num_years',$num_years);
2921       if($num_years == 1) { $this->t->set_var('lang_num_years','Year of History'); }
2922       else {  $this->t->set_var('lang_num_years','Years of History'); }
2923       $this->t->set_var('lang_filter','Filter');
2924       
2925       $year = date('Y') - $num_years + 1;
2926       $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2927       
2928       $sql = "SELECT * FROM eq_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2929       $this->db->query($sql,__LINE__,__FILE__);
2930       $total_records = $this->db->num_rows();
2931
2932       $i = 0;
2933       while ($this->db->next_record())
2934         {
2935           $visit_list[$i]['visit'] = $this->db->f('visit');
2936           $visit_list[$i]['family'] = $this->db->f('family');
2937           $visit_list[$i]['date']  = $this->db->f('date');
2938           $i++;
2939         }
2940             
2941       for ($i=0; $i < count($visit_list); $i++)
2942         {         
2943           $this->nextmatchs->template_alternate_row_color(&$this->t);
2944
2945           $sql = "SELECT * FROM eq_family WHERE family=".$visit_list[$i]['family'];
2946           $this->db->query($sql,__LINE__,__FILE__);
2947           $this->db->next_record();
2948                   
2949           $this->t->set_var('family',$visit_list[$i]['family']);
2950           $this->t->set_var('family_name',$this->db->f('name'));
2951           $this->t->set_var('date',$visit_list[$i]['date']);
2952           
2953           $link_data['menuaction'] = 'eq.eq.vis_update';
2954           $link_data['visit'] = $visit_list[$i]['visit'];
2955           $link_data['name'] = $this->db->f('name');
2956           $link_data['date'] = $visit_list[$i]['date'];
2957           $link_data['action'] = 'view';
2958           $this->t->set_var('view',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2959           $this->t->set_var('lang_view','View');
2960
2961           $link_data['menuaction'] = 'eq.eq.vis_update';
2962           $link_data['visit'] = $visit_list[$i]['visit'];
2963           $link_data['name'] = $this->db->f('name');
2964           $link_data['date'] = $visit_list[$i]['date'];
2965           $link_data['action'] = 'edit';
2966           $this->t->set_var('edit',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2967           $this->t->set_var('lang_edit','Edit');
2968
2969           $this->t->fp('list1','visit_list',True);
2970         }
2971
2972       // List the families that are available to record a visit against
2973       $sql = "SELECT * FROM eq_family WHERE valid=1";
2974       $this->db->query($sql,__LINE__,__FILE__);
2975       $total_records = $this->db->num_rows();
2976
2977       $i = 0;
2978       while ($this->db->next_record())
2979         {
2980           $family_names[$i] = $this->db->f('name');
2981           $family_ids[$i] = $this->db->f('family');
2982           $i++;
2983         } array_multisort($family_names, $family_ids);
2984       
2985       for ($i=0; $i < count($family_names); $i++)
2986         {
2987           $link_data['menuaction'] = 'eq.eq.vis_update';
2988           $link_data['visit'] = '';
2989           $link_data['family'] = $family_ids[$i];
2990           $link_data['action'] = 'add';
2991           $link_data['name'] = $family_names[$i];
2992           $this->t->set_var('add',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
2993
2994           $this->t->set_var('name',$family_names[$i]);
2995           if(($i+1) % 3 == 0) { $this->t->set_var('table_sep',"</td></tr><tr>"); }
2996           else { $this->t->set_var('table_sep',"</td>"); }
2997           if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2998
2999           $this->t->fp('list2','family_list',True);
3000         }   
3001
3002       $this->t->pfp('out','vis_view_t');
3003       $this->save_sessiondata(); 
3004     }
3005
3006   function vis_update()
3007     {
3008       $this->t->set_file(array('form' => 'vis_update.tpl'));
3009       $this->t->set_block('form','add','addhandle');
3010       $this->t->set_block('form','edit','edithandle');
3011       
3012       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_view'));
3013       $this->t->set_var('readonly','');
3014       $this->t->set_var('disabled','');
3015       
3016       $action = get_var('action',array('GET','POST'));
3017       $visit = get_var('visit',array('GET','POST'));
3018       $family = get_var('family',array('GET','POST'));
3019       $name = get_var('name',array('GET','POST'));
3020       $date = get_var('date',array('GET','POST'));
3021       $notes = get_var('notes',array('GET','POST'));
3022       $companionship = 0;
3023       
3024       if($action == 'save')
3025         {
3026           $notes = get_var('notes',array('POST'));
3027           $this->db->query("UPDATE eq_visit set " .
3028                            "  date='" . $date . "'" .
3029                           ", notes='" . $notes . "'" .
3030                            " WHERE visit=" . $visit,__LINE__,__FILE__);
3031           $this->vis_view();
3032           return false;
3033         }
3034
3035       if($action == 'insert')
3036         {
3037           $notes = get_var('notes',array('POST'));
3038           $this->db->query("INSERT INTO eq_visit (family,companionship,date,notes) "
3039                            . "VALUES ('" . $family . "','" . $companionship . "','"
3040                            . $date . "','" . $notes . "')",__LINE__,__FILE__);
3041           $this->vis_view();
3042           return false;
3043         }
3044       
3045       if($action == 'add')
3046         {
3047           $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3048           $this->t->set_var('family', $family);
3049           $this->t->set_var('visit', '');
3050           $this->t->set_var('name', $name);
3051           $this->t->set_var('date','');
3052           $this->t->set_var('notes','');
3053           $this->t->set_var('lang_done','Cancel');
3054           $this->t->set_var('lang_action','Adding New Visit');
3055           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&family='
3056                                                                 . $family . '&action=' . 'insert'));
3057         }
3058
3059       if($action == 'edit' || $action == 'view')
3060         {
3061           $sql = "SELECT * FROM eq_visit WHERE visit=".$visit;
3062           $this->db->query($sql,__LINE__,__FILE__);
3063           $this->db->next_record();
3064           $this->t->set_var('visit',$visit);
3065           $this->t->set_var('name',$name);
3066           $this->t->set_var('family', $family);
3067           $this->t->set_var('date',$this->db->f('date'));
3068           $this->t->set_var('notes',$this->db->f('notes'));
3069         }
3070       
3071       if($action == 'edit')
3072         {
3073           $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3074           $this->t->set_var('lang_done','Cancel');
3075           $this->t->set_var('lang_action','Editing Visit');
3076           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3077                                                                 . $visit . '&action=' . 'save'));
3078         }
3079
3080       if($action == 'view')
3081         {
3082           $date = $this->db->f('date');
3083           $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3084           $this->t->set_var('readonly','READONLY');
3085           $this->t->set_var('disabled','DISABLED');
3086           $this->t->set_var('lang_done','Done');
3087           $this->t->set_var('lang_action','Viewing Visit');
3088           $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_update&visit='
3089                                                                 . $visit . '&action=' . 'edit'));
3090         }
3091       
3092       $this->t->set_var('lang_reset','Clear Form');
3093       $this->t->set_var('lang_add','Add Visit');
3094       $this->t->set_var('lang_save','Save Changes');
3095       $this->t->set_var('edithandle','');
3096       $this->t->set_var('addhandle','');
3097
3098       $this->t->pfp('out','form');
3099       
3100       if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3101       if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3102       if($action == 'add') { $this->t->pfp('addhandle','add'); }
3103
3104       $this->save_sessiondata(); 
3105     }
3106
3107   function att_view()
3108     {
3109       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3110       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3111       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3112
3113       $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3114       $this->t->set_block('att_view_t','act_list','list');
3115
3116       $this->t->set_block('att_view_t','month_list','list1');
3117       $this->t->set_block('att_view_t','header_list','list2');
3118       $this->t->set_block('att_view_t','elder_list','list3');
3119       
3120       $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3121       $num_quarters = get_var('num_quarters',array('GET','POST'));
3122       if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3123       $this->t->set_var('num_quarters',$num_quarters);
3124       $this->t->set_var('lang_filter','Filter');
3125       if($num_quarters == 1) { $this->t->set_var('lang_num_quarters','Quarter of History'); }
3126       else { $this->t->set_var('lang_num_quarters','Quarters of History'); }
3127
3128       $num_months = $num_quarters * 3;
3129       $current_month = $this->current_month;
3130       if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3131       else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3132       else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3133       else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3134       
3135       $sql = "SELECT * FROM eq_elder where valid=1";
3136       $this->db->query($sql,__LINE__,__FILE__);
3137       $i=0;
3138       while ($this->db->next_record())
3139         {
3140           $elder_name[$i] = $this->db->f('name');
3141           $elder_id[$i] = $this->db->f('elder');
3142           $i++;
3143         }
3144       array_multisort($elder_name, $elder_id);
3145       
3146       // Create a list of sunday dates for a window of 3 months back and current month
3147       $i=0; 
3148       $last_time = 0; 
3149       $found_sunday = 0;
3150       $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $current_month-$num_months, 1, date("y")));
3151       $last_date = explode("-",$sunday_list[0]['date']);
3152       $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3153       $time_limit = mktime(0, 0, 0, date("m"), date("t"), date("y"));
3154       while($last_time < $time_limit)
3155       {
3156         $day = date("w",$last_time);
3157         if(date("w",$last_time) == 0) {
3158           $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3159           $last_date = explode("-",$sunday_list[$i]['date']);
3160           $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3161           $sunday_list[$i]['day'] = $last_date[2];
3162           $sunday_list[$i]['month'] = date("M",$last_time);
3163           $sunday_list[$i]['year'] = $last_date[0];
3164           $found_sunday = 1; 
3165         }
3166         $last_time += 90000;
3167         if($found_sunday) { $i++; $found_sunday=0; }
3168       }
3169
3170       $total_elders = count($elder_id);
3171       $old_month=$sunday_list[0]['month']; $span=0;
3172       for ($i=0; $i < count($sunday_list); $i++) {
3173         $date = $sunday_list[$i]['date'];
3174         $this->t->set_var('date',$sunday_list[$i]['date']);
3175         $this->t->set_var('day',$sunday_list[$i]['day']);
3176         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3177           if($i == count($sunday_list)-1) { $span++; }
3178           $cur_month = $sunday_list[$i]['month'];
3179           $old_month = $sunday_list[$i]['month'];         
3180           $link_data['menuaction'] = 'eq.eq.att_update';
3181           $link_data['month'] = $sunday_list[$i-1]['month'];
3182           $link_data['year'] = $sunday_list[$i-1]['year'];
3183           $link_data['action'] = 'update_month';
3184           $cur_month = $sunday_list[$i-1]['month'];
3185           $cur_year = $sunday_list[$i-1]['year'];
3186           $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3187           $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3188           $this->t->set_var('month',$sunday_list[$i-1]['month']);
3189           $this->t->set_var('year',$sunday_list[$i-1]['year']);
3190           $this->t->set_var('span',$span); $span=0;
3191           $this->t->fp('list1','month_list',True);
3192         } $span++;
3193       }
3194       $this->t->set_var('total_elders',$total_elders);
3195       $this->t->set_var('header_row',$header_row);
3196       
3197       $elder_width=200; $att_width=25; $total_width=$elder_width; 
3198       for ($i=0; $i < count($sunday_list); $i++) {
3199         $link_data['menuaction'] = 'eq.eq.att_update';
3200         $link_data['month'] = $sunday_list[$i]['month'];
3201         $link_data['year'] = $sunday_list[$i]['year'];
3202         $link_data['day'] = $sunday_list[$i]['day'];
3203         $link_data['date'] = $sunday_list[$i]['date'];
3204         $link_data['action'] = 'update_day';
3205         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3206         $this->t->set_var('date',$sunday_list[$i]['date']);
3207         $this->t->set_var('day',$sunday_list[$i]['day']);
3208         $this->t->set_var('month',$sunday_list[$i]['month']);
3209         $this->t->set_var('year',$sunday_list[$i]['year']);
3210         $this->t->fp('list2','header_list',True);
3211         $total_width += $att_width;
3212         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3213       }
3214
3215       for ($i=0; $i < count($elder_id); $i++) {
3216         $att_table = "";
3217         $this->nextmatchs->template_alternate_row_color(&$this->t);
3218         $this->t->set_var('elder_name',$elder_name[$i]);
3219         #print "checking for elder: " . $elder_id[$i] . "<br>";
3220         for ($j=0; $j < count($sunday_list); $j++) {
3221           #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3222           #print "SELECT * FROM eq_attendance WHERE date='"
3223           #  . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i] . "<br>";
3224           $sql = "SELECT * FROM eq_attendance WHERE date='"
3225              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3226           $this->db->query($sql,__LINE__,__FILE__);
3227           if($this->db->next_record()) {
3228             $cur_month = $sunday_list[$j]['month'];
3229             if($attended[$i][$cur_month] != 1) { 
3230               $attended[$i][$cur_month]=1;
3231               $attendance[$monthnum[$cur_month]]++;
3232             } 
3233             $att_table .= '<td align=center><img src="images/checkmark.gif"></td>';
3234           } else {
3235             $att_table .= '<td>&nbsp;</td>';
3236           }
3237         }
3238         $this->t->set_var('att_table',$att_table);
3239         $this->t->fp('list3','elder_list',True);
3240       }
3241       $this->t->set_var('total_width',$total_width);
3242       $this->t->set_var('elder_width',$elder_width);
3243       $this->t->set_var('att_width',$att_width);
3244
3245       # Now calculate attendance for these months
3246       $attendance_str = "";
3247       $nonattendance_str = "";
3248       $aveattendance_str = "";
3249       $avenonattendance_str = "";
3250       $num_months=0;
3251       $ave_total_attended=0;
3252       ksort($attendance);
3253       foreach($attendance as $att => $value) {
3254         $total_attended = $attendance[$att];
3255         $ave_total_attended += $attendance[$att]; $num_months++;
3256         $percent = ceil(($total_attended / $total_elders)*100);
3257         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3258         $total_nonattended = $total_elders - $total_attended;
3259         $percent = ceil(($total_nonattended / $total_elders)*100);
3260         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3261         
3262         $total_attended = ceil(($ave_total_attended / $num_months));
3263         $percent = ceil(($total_attended / $total_elders)*100);
3264         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3265         $total_attended = $total_elders - ceil(($ave_total_attended / $num_months));
3266         $percent = ceil(($total_attended / $total_elders)*100);
3267         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3268       }
3269       
3270       $this->t->set_var('attendance',$attendance_str);
3271       $this->t->set_var('aveattendance',$aveattendance_str);
3272       $this->t->set_var('nonattendance',$nonattendance_str);
3273       $this->t->set_var('avenonattendance',$avenonattendance_str);
3274       
3275       $this->t->pfp('out','att_view_t');
3276       $this->save_sessiondata(); 
3277     }
3278
3279   function att_update()
3280     {
3281       $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3282       $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3283       $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3284
3285       $this->t->set_file(array('form' => 'att_update.tpl'));
3286       $this->t->set_block('form','edit','edithandle');
3287       
3288       $this->t->set_block('form','month_list','list1');
3289       $this->t->set_block('form','header_list','list2');
3290       $this->t->set_block('form','elder_list','list3');
3291
3292       $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_view'));
3293
3294       $action = get_var('action',array('GET','POST'));
3295       $month = get_var('month',array('GET','POST'));
3296       $year = get_var('year',array('GET','POST'));
3297       $day = get_var('day',array('GET','POST'));
3298       $date = get_var('date',array('GET','POST'));
3299
3300       if($action == 'save_month' || $action == 'save_day')
3301         {
3302            $new_data = get_var('elders_attended',array('POST'));
3303            $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3304
3305            if($action == 'save_month') {        
3306              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3307            }
3308
3309            if($action == 'save_day') {        
3310              $this->db->query("DELETE from eq_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3311            }   
3312
3313            foreach ($new_data as $data)
3314            {
3315               $data_array = explode("-",$data);
3316               $elder = $data_array[0];
3317               $date  = "$data_array[1]-$data_array[2]-$data_array[3]";        
3318               $this->db->query("INSERT INTO eq_attendance (elder,date) "
3319                                . "VALUES (" . $elder . ",'". $date . "')",__LINE__,__FILE__);
3320            }
3321         
3322          $this->att_view();
3323          return false;    
3324         }
3325
3326       $sql = "SELECT * FROM eq_elder where valid=1";
3327       $this->db->query($sql,__LINE__,__FILE__);
3328       $i=0;
3329       while ($this->db->next_record())
3330         {
3331           $elder_name[$i] = $this->db->f('name');
3332           $elder_id[$i] = $this->db->f('elder');
3333           $elder_attending[$elder_id[$i]] = $this->db->f('attending');
3334           $i++;
3335         }
3336       array_multisort($elder_name, $elder_id);
3337       
3338       if($action == 'update_month')
3339       {
3340         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_month'));
3341         $i=0; 
3342         $last_time = 0; 
3343         $found_sunday = 0;
3344         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3345         $last_date = explode("-",$sunday_list[0]['date']);
3346         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3347         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3348         while($last_time <= $time_limit)
3349         {
3350           $day = date("w",$last_time);
3351           if(date("w",$last_time) == 0) { 
3352             $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3353             $last_date = explode("-",$sunday_list[$i]['date']);
3354             $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3355             $sunday_list[$i]['day'] = $last_date[2];
3356             $sunday_list[$i]['month'] = date("M",$last_time);
3357             $sunday_list[$i]['year'] = $last_date[0];
3358             $found_sunday = 1; 
3359           }
3360           $last_time += 90000;
3361           if($found_sunday) { $i++; $found_sunday=0; }
3362         }
3363
3364         $this->t->set_var('span', $i);
3365         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3366         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3367         $this->t->fp('list1','month_list',True);
3368         $elder_width=200; $att_width=25; $total_width=$elder_width;
3369         for ($i=0; $i < count($sunday_list); $i++) {
3370           $link_data['menuaction'] = 'eq.eq.att_update';
3371           $link_data['month'] = $sunday_list[$i]['month'];
3372           $link_data['year'] = $sunday_list[$i]['year'];
3373           $link_data['day'] = $sunday_list[$i]['day'];
3374           $link_data['date'] = $sunday_list[$i]['date'];
3375           $link_data['action'] = 'update_day';
3376           $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/eq/index.php',$link_data));
3377           $this->t->set_var('date',$sunday_list[$i]['date']);
3378           $this->t->set_var('day',$sunday_list[$i]['day']);
3379           $this->t->set_var('month',$sunday_list[$i]['month']);
3380           $this->t->set_var('year',$sunday_list[$i]['year']);
3381           $this->t->fp('list2','header_list',True);
3382           $total_width += $att_width;
3383         }     
3384       }
3385
3386       if($action == 'update_day')
3387       {
3388         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.att_update&action=save_day'));
3389         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3390         $this->t->set_var('month',$month);
3391         $this->t->set_var('year',$year);
3392         $this->t->fp('list1','month_list',True);
3393         $this->t->set_var('date',$date);
3394         $this->t->set_var('day',$day);
3395         $this->t->set_var('month',$month);
3396         $this->t->set_var('year',$year);
3397         $this->t->fp('list2','header_list',True);
3398       }           
3399             
3400       for ($i=0; $i < count($elder_id); $i++) {
3401         $att_table = "";
3402         $this->nextmatchs->template_alternate_row_color(&$this->t);
3403         $this->t->set_var('elder_name',$elder_name[$i]);
3404         for ($j=0; $j < count($sunday_list); $j++) {
3405           $sql = "SELECT * FROM eq_attendance WHERE date='"
3406              . $sunday_list[$j]['date'] . "' AND elder=" . $elder_id[$i];
3407           $this->db->query($sql,__LINE__,__FILE__);
3408           $value = $elder_id[$i] . "-" . $sunday_list[$j]['date'];
3409           if($this->db->next_record()) {
3410             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3411           } else if($elder_attending[$elder_id[$i]] == 1) {
3412             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'" checked></td>';
3413           } else {
3414             $att_table .= '<td align=center><input type="checkbox" name="elders_attended[]" value="'.$value.'"></td>';
3415           }
3416         }
3417         $this->t->set_var('att_table',$att_table);
3418         $this->t->fp('list3','elder_list',True);
3419       } 
3420            
3421       $this->t->set_var('lang_done', 'Cancel');
3422       $this->t->set_var('lang_reset','Clear Form');
3423       $this->t->set_var('lang_save','Save Changes');
3424
3425       $this->t->pfp('out','form');
3426       $this->t->pfp('addhandle','edit');
3427
3428       $this->save_sessiondata();       
3429     }
3430
3431   function dir_view()
3432     {
3433       $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3434       $this->t->set_block('dir_view_t','dir_list','list');
3435       
3436       $sql = "SELECT * FROM eq_parent where valid=1 ORDER BY name ASC";
3437       $this->db->query($sql,__LINE__,__FILE__);
3438       $i=0;
3439       while ($this->db->next_record())
3440         {
3441           $parent[$i]['id'] = $this->db->f('parent');
3442           $parent[$i]['name'] = $this->db->f('name');
3443           $parent[$i]['phone'] = $this->db->f('phone');
3444           $parent[$i]['address'] = $this->db->f('address');
3445           $i++;
3446         }   
3447       
3448       for ($i=0; $i < count($parent); $i++) 
3449       {
3450         $name = $parent[$i]['name'];
3451         $phone = $parent[$i]['phone'];
3452         $address = $parent[$i]['address'];
3453         $this->t->set_var('name', $name);
3454         $this->t->set_var('address', $address);
3455         $this->t->set_var('phone', $phone);
3456         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3457         $this->t->set_var('tr_color',$tr_color);
3458         $this->t->fp('list','dir_list',True);
3459         //print "$phone $name $address<br>";
3460       }
3461       $this->t->pfp('out','dir_view_t');
3462       $this->save_sessiondata();   
3463     }
3464   
3465   function org_view()
3466     {
3467       $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3468       $this->t->set_block('org_view_t','calling_list','list1');
3469       $this->t->set_block('org_view_t','org_list','list2');
3470
3471       # Display a list ordered alphabetically
3472       $sql = "SELECT * FROM eq_calling ORDER BY name ASC";
3473       $this->db->query($sql,__LINE__,__FILE__);
3474       $i=0;
3475       while ($this->db->next_record())
3476         {
3477           $calling[$i]['id'] = $this->db->f('indiv_id');
3478           $calling[$i]['name'] = $this->db->f('name');
3479           $calling[$i]['position'] = $this->db->f('position');
3480           $calling[$i]['sustained'] = $this->db->f('sustained');
3481           $calling[$i]['organization'] = $this->db->f('organization');
3482           $i++;
3483         }   
3484       for ($i=0; $i < count($calling); $i++) 
3485       {
3486         $name = $calling[$i]['name'];
3487         $position = $calling[$i]['position'];
3488         $sustained = $calling[$i]['sustained'];
3489         $organization = $calling[$i]['organization'];
3490         $this->t->set_var('name', $name);
3491         $this->t->set_var('position', $position);
3492         $this->t->set_var('sustained', $sustained);
3493         $this->t->set_var('organization', $organization);
3494         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3495         $this->t->set_var('tr_color',$tr_color);
3496         $this->t->fp('list1','calling_list',True);
3497       }
3498
3499       # Display a list ordered by organization
3500       $sql = "SELECT * FROM eq_calling ORDER BY sequence ASC";
3501       $this->db->query($sql,__LINE__,__FILE__);
3502       $i=0;
3503       while ($this->db->next_record())
3504         {
3505           $calling[$i]['id'] = $this->db->f('indiv_id');
3506           $calling[$i]['name'] = $this->db->f('name');
3507           $calling[$i]['position'] = $this->db->f('position');
3508           $calling[$i]['sustained'] = $this->db->f('sustained');
3509           $calling[$i]['organization'] = $this->db->f('organization');
3510           $i++;
3511         }   
3512       for ($i=0; $i < count($calling); $i++) 
3513       {
3514         $name = $calling[$i]['name'];
3515         $position = $calling[$i]['position'];
3516         $sustained = $calling[$i]['sustained'];
3517         $organization = $calling[$i]['organization'];
3518         $this->t->set_var('name', $name);
3519         $this->t->set_var('position', $position);
3520         $this->t->set_var('sustained', $sustained);
3521         $this->t->set_var('organization', $organization);
3522         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3523         $this->t->set_var('tr_color',$tr_color);
3524         $this->t->fp('list2','org_list',True);
3525       }
3526       
3527       $this->t->pfp('out','org_view_t');
3528       $this->save_sessiondata();   
3529     }
3530   
3531   function schedule()
3532     {
3533       $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3534       $this->t->set_block('sched_t','presidency_list','list');
3535
3536       $action = get_var('action',array('GET','POST'));
3537       
3538       $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule&action=save'));
3539       $this->t->set_var('title','EQ Scheduling Tool');
3540
3541       $this->t->set_var('lang_save','Save Schedule');
3542       $this->t->set_var('lang_reset','Cancel');
3543       
3544       $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.vis_sched'));
3545       $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3546
3547       $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.int_sched'));
3548       $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3549       
3550       $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.ppi_sched'));
3551       $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3552       
3553       $date_width=150; $time_width=200; $elder_width=200; $family_width=200;
3554       $table_width=$date_width + $time_width + $elder_width + $family_width;
3555       $header_row = "<th width=$date_width><font size=-2>Date</th>";
3556       $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3557       $header_row.= "<th width=$elder_width><font size=-2>Elder</th>";
3558       $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3559       $table_data = "";
3560
3561       if($action == 'save')
3562         {
3563           $new_data = get_var('sched',array('POST'));
3564           foreach ($new_data as $presidency_array)
3565            {
3566              foreach ($presidency_array as $entry)
3567                {
3568                  $presidency = $entry['presidency'];
3569                  $appointment = $entry['appointment'];
3570                  $date = $entry['date'];
3571                  $hour = $entry['hour'];
3572                  $minute = $entry['minute'];
3573                  $pm = $entry['pm'];
3574                  $elder = $entry['elder'];
3575                  $family = $entry['family'];
3576                  if($pm) { $hour = $hour + 12; }
3577                  $time = $hour.':'.$minute.':'.'00';
3578                  $uid = 0;
3579
3580                  // Zero out the family or elder if date = NULL
3581                  if($date == "") {
3582                    $elder = 0;
3583                    $family = 0;
3584                  }
3585                  
3586                  // Update an existing appointment
3587                  if($appointment < $this->max_appointments)
3588                    {
3589                      //Only perform a database update if we have made a change to this appointment
3590                      $sql = "SELECT * FROM eq_appointment where " .
3591                         "appointment='$appointment'" .
3592                         " and presidency='$presidency'" .
3593                         " and elder='$elder'" .
3594                         " and family='$family'" .
3595                         " and date='$date'" .
3596                         " and time='$time'";
3597                      $this->db->query($sql,__LINE__,__FILE__);
3598                      if(!$this->db->next_record()) {
3599                        $old_date = $this->db->f('date');
3600                        $old_time = $this->db->f('time');
3601                        $this->db2->query("UPDATE eq_appointment set" .
3602                                         " family=" . $family . 
3603                                         " ,elder=" . $elder . 
3604                                         " ,date='" . $date . "'" .
3605                                         " ,time='" . $time . "'" .
3606                                         " ,presidency='" . $presidency . "'" .
3607                                         " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3608                        
3609                        // Email the appointment
3610                        $this->email_appt($appointment);
3611                      }
3612                    }
3613                  
3614                  // Add a new appointment
3615                  else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3616                    {
3617                      //print "adding entry: appt=$appointment date: $date time: $time elder: $elder family: $family<br>";
3618                      $this->db2->query("INSERT INTO eq_appointment (appointment,presidency,family,elder,date,time,uid) "
3619                            . "VALUES (NULL,'" . $presidency . "','" . $family . "','"
3620                            . $elder . "','" . $date . "','" . $time  . "','" . $uid ."')",__LINE__,__FILE__);
3621
3622                      // Now reselect this entry from the database to see if we need
3623                      // to send an appointment out for it.
3624                      $sql = "SELECT * FROM eq_appointment where " .
3625                         "elder='$elder'" .
3626                         " and family='$family'" .
3627                         " and presidency='$presidency'" .
3628                         " and date='$date'" .
3629                         " and time='$time'" .
3630                         " and uid='$uid'";
3631                      $this->db3->query($sql,__LINE__,__FILE__);
3632                      if($this->db3->next_record()) {
3633                        // Email the appointment if warranted
3634                        if(($date != "") && ($time != "") && (($elder > 0) || $family > 0)) { 
3635                          $this->email_appt($this->db3->f('appointment'));
3636                        }
3637                      }
3638                    }
3639                }
3640            }
3641           
3642           $take_me_to_url = $GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.schedule');
3643           //Header('Location: ' . $take_me_to_url);
3644         }
3645
3646       $sql = "SELECT * FROM eq_presidency where valid=1";
3647       $this->db->query($sql,__LINE__,__FILE__);
3648       $i=0;
3649       while ($this->db->next_record())
3650         {
3651           $presidency_data[$i]['id'] = $this->db->f('presidency');
3652           $presidency_data[$i]['name'] = $this->db->f('name');
3653           $presidency_data[$i]['elder'] = $this->db->f('elder');
3654           $i++;
3655         }
3656       
3657       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3658       $this->db->query($sql,__LINE__,__FILE__);
3659       $i=0;
3660       while ($this->db->next_record())
3661         {
3662           $elder_id[$i] = $this->db->f('elder');
3663           $elder_name[$i] = $this->db->f('name');
3664           $elder_phone[$elder_id[$i]] = $this->db->f('phone');
3665           $i++;
3666         }
3667       array_multisort($elder_name, $elder_id);
3668
3669       $sql = "SELECT * FROM eq_family where valid=1 and elder_id != 0 ORDER BY name ASC";
3670       $this->db->query($sql,__LINE__,__FILE__);
3671       $i=0;
3672       while ($this->db->next_record())
3673         {
3674           $family_id[$i] = $this->db->f('family');
3675           $family_name[$i] = $this->db->f('name');
3676           $i++;
3677         }
3678       array_multisort($family_name, $family_id);
3679       
3680       for ($i=0; $i < count($presidency_data); $i++) {
3681         $presidency = $presidency_data[$i]['id'];
3682         $interviewer = $presidency_data[$i]['elder'];
3683         $name = $presidency_data[$i]['name'];
3684         $this->t->set_var('presidency_name',$name);
3685         $table_data="";
3686         
3687         // query the database for all the appointments
3688         $sql = "SELECT * FROM eq_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3689         $this->db->query($sql,__LINE__,__FILE__);
3690
3691         // Prefill any existing appointment slots
3692         while ($this->db->next_record())
3693           {
3694             $appointment = $this->db->f('appointment');
3695             $elder = $this->db->f('elder');
3696             $family = $this->db->f('family');
3697             
3698             $date = $this->db->f('date');
3699             $date_array = explode("-",$date);
3700             $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3701             $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3702             
3703             $time = $this->db->f('time');
3704             $time_array = explode(":",$time);
3705             $hour = $time_array[0];
3706             $minute = $time_array[1];
3707             $pm = 0;
3708             if($hour > 12) { $pm=1; $hour = $hour - 12; }
3709             $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3710             
3711             $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3712
3713             // Date selection
3714             $table_data.= '<td align=left>';
3715             $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3716             $table_data.= '</td>';
3717             
3718             // Hour & Minutes selection
3719             $table_data.= "<td align=center>";
3720             $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
3721             $table_data.= "</td>";
3722             
3723             // Elder drop down list (for PPIs)
3724             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3725             $table_data.= '<option value=0></option>';  
3726             for ($j=0; $j < count($elder_id); $j++) {
3727               $id = $elder_id[$j];
3728               $name = $elder_name[$j];
3729               if($elder_id[$j] == $elder) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3730               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3731             }
3732             $table_data.='</select></td>';
3733
3734             // Family drop down list (for Visits)
3735             $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3736             $table_data.= '<option value=0></option>';              
3737             for ($j=0; $j < count($elder_id); $j++) {
3738               $id = $family_id[$j];
3739               $name = $family_name[$j];
3740               if($family_id[$j] == $family) { $selected[$id] = 'selected="selected"'; } else { $selected[$id] = ''; }
3741               $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3742             }
3743             $table_data.='</select></td>';
3744             
3745             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3746             $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3747         
3748             $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3749             $this->t->set_var('tr_color',$tr_color);
3750             
3751           }
3752
3753         // Create blank appointment slot
3754         for ($b=0; $b < 4; $b++) {
3755           $appointment = $this->max_appointments + $b;
3756           $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3757
3758           // Date selection
3759           $table_data.= '<td align=left>';
3760           $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3761           $table_data.= '</td>';
3762         
3763           // Time selection
3764           $table_data.= "<td align=center>";
3765           $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
3766           $table_data.= "</td>";
3767           
3768           // Elder drop down list
3769           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][elder]>';
3770           $table_data.= '<option value=0></option>';  
3771           for ($j=0; $j < count($elder_id); $j++) {
3772             $id = $elder_id[$j];
3773             $name = $elder_name[$j];
3774             $table_data.= '<option value='.$id.'>'.$name.'</option>';
3775           }
3776           $table_data.='</select></td>';
3777           
3778           // Family drop down list
3779           $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family]>';
3780           $table_data.= '<option value=0></option>';        
3781           for ($j=0; $j < count($elder_id); $j++) {
3782             $id = $family_id[$j];
3783             $name = $family_name[$j];
3784             $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3785           }
3786           $table_data.='</select></td>';
3787           
3788           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3789           $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3790
3791           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3792           $this->t->set_var('tr_color',$tr_color);
3793         }
3794         
3795         $this->t->set_var('table_data',$table_data);
3796         $this->t->set_var('header_row',$header_row);
3797         $this->t->set_var('table_width',$table_width);
3798         $this->t->fp('list','presidency_list',True);
3799         
3800       }
3801       
3802       $this->t->pfp('out','sched_t');
3803       $this->save_sessiondata();   
3804     }
3805   
3806   function admin()
3807     {
3808       $this->t->set_file(array('admin_t' => 'admin.tpl'));
3809       $this->t->set_block('admin_t','upload','uploadhandle');
3810       $this->t->set_block('admin_t','admin','adminhandle');
3811       $this->t->set_block('admin_t','cmd','cmdhandle');
3812       $this->t->set_block('admin_t','presidency','presidencyhandle');
3813       
3814       $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=upload'));
3815       $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/eq/index.php','menuaction=eq.eq.admin&action=presidency'));
3816       
3817       $action = get_var('action',array('GET','POST'));
3818
3819       $this->t->pfp('out','admin_t');
3820
3821       $sql = "SELECT * FROM eq_elder where valid=1 ORDER BY elder ASC";
3822       $this->db->query($sql,__LINE__,__FILE__);
3823       $i=0;
3824       while ($this->db->next_record())
3825         {
3826           $elder_id[$i] = $this->db->f('elder');
3827           $elder_name[$i] = $this->db->f('name');
3828           $elder2name[$elder_id[$i]] = $elder_name[$i];
3829           $i++;
3830         }
3831       array_multisort($elder_name, $elder_id);
3832
3833       if($action == 'upload')
3834         {        
3835           $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
3836           
3837           if(($_FILES['uploadedfile']['type'] == "application/zip") ||
3838              ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3839              ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3840              ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
3841
3842             if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
3843               $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
3844               $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
3845               $this->t->set_var('uploadstatus',$uploadstatus);
3846               $this->t->pfp('uploadhandle','upload',True);
3847               return 0;
3848             }
3849             
3850             $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3851             $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3852             $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3853             $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";    
3854             $this->t->set_var('uploadstatus',$uploadstatus);
3855             $this->t->pfp('uploadhandle','upload');
3856             $this->t->set_var('uploadhandle','');
3857             print "<table border=1 width=80%><tr><td>\n<pre>";
3858             
3859             # make a directory for this data to be stored in
3860             $date="data_" . date("Y_m_d");
3861             $data_dir = $this->upload_target_path . '/' . $date;
3862             print "-> Making the data directory: $date<br>\n";
3863             exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
3864             if($return_code != 0) {
3865               print implode('\n',$result) . "<br>";
3866               print "<b><font color=red>";
3867               print "-E- Unable to create the data directory. Aborting import.";
3868               print "</font></b>";
3869               return 0;
3870             }
3871
3872             # move the file uploaded into this directory
3873             print "-> Moving the uploaded file into the data dir<br>\n";
3874             exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3875             if($return_code != 0) {
3876               print implode('\n',$result) . "<br>";
3877               print "<b><font color=red>";
3878               print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3879               print "</font></b>";
3880               return 0;
3881             }
3882             
3883             # unzip the data into this directory
3884             print "-> Unzipping the data<br>\n";
3885             $data_file = $data_dir . '';
3886             exec('unzip ' . $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3887             if($return_code != 0) {
3888               print implode('\n',$result) . "<br>";
3889               print "<b><font color=red>";
3890               print "-E- Unable to unzip the uploaded file into the data dir. Aborting import.";
3891               print "</font></b>";
3892               return 0;
3893             }
3894             exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3895
3896             # update the data_latest link to point to this new directory
3897             print "-> Updating the latest data dir link<br>\n";
3898             $data_latest = $this->upload_target_path . '/data_latest';
3899             exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3900             if($return_code != 0) {
3901               print implode('\n',$result) . "<br>";
3902               print "<b><font color=red>";
3903               print "-E- Unable to update the data latest link. Aborting import.";
3904               print "</font></b>";
3905               return 0;
3906             }
3907             
3908             # run the import perl script to encorporate it into the DB
3909             ob_start('ob_logstdout', 2);
3910             print "-> Importing the data into the EQ database<br>\n";
3911             ob_flush(); flush(); sleep(1);
3912             $import_log = $this->upload_target_path . '/import.log';
3913             $data_log = $this->upload_target_path . '/data.log';
3914             $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
3915             $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
3916             #print "import_cmd: $import_cmd<br>";
3917             #print "parse_cmd: $parse_cmd<br>";
3918             ob_start('ob_logstdout', 2);
3919             passthru($import_cmd);
3920             passthru($parse_cmd);
3921             ob_flush(); flush(); sleep(1);
3922
3923             # fix the permissions of the data dir
3924             exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3925             
3926             $this->t->pfp('cmdhandle','cmd');
3927             print "</pre></td></tr></table>";
3928             
3929           } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3930                     ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3931                     ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3932                     ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3933             $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3934             $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3935             $this->t->set_var('uploadstatus',$uploadstatus);
3936             $this->t->pfp('uploadhandle','upload',True);
3937           } else {
3938             $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
3939             $uploadstatus.= ") uploading the file, please try again! </font></b>";
3940             $this->t->set_var('uploadstatus',$uploadstatus);
3941             $this->t->pfp('uploadhandle','upload',True);
3942           }
3943         }
3944       else if($action == "presidency")
3945         {
3946           $new_data = get_var('eqpres',array('POST'));
3947           foreach ($new_data as $entry)
3948            {
3949              $id = $entry['id'];
3950              $email = $entry['email'];
3951              $elder = $entry['elder'];
3952              $name = $entry['name'];
3953              $district = $entry['district'];
3954              $president = $entry['president'];
3955              $counselor = $entry['counselor'];
3956              $secretary = $entry['secretary'];
3957              $eqpresidency = $entry['eqpresidency'];
3958              // Set the elder id to 0 for EQ Presidency tagged entry
3959              if($eqpresidency == 1) { $elder="0"; }
3960              // Re-look up the elder name for the ID if we aren't an EQ Presidency tagged entry
3961              else { $name = $elder2name[$elder]; }
3962              //print "id=$id elder=$elder name=$name email=$email district=$district president=$president ";
3963              //print "counselor=$counselor secretary=$secretary eqpres=$eqpresidency<br>";
3964
3965              if(($elder > 0) || ($name != "")) {
3966                if($id < $this->max_presidency_members) {
3967                  //print "Updating Existing Entry<br>";
3968                  $this->db2->query("UPDATE eq_presidency set" .
3969                                    " elder=" . $elder . 
3970                                    " ,district=" . $district . 
3971                                    " ,name='" . $name . "'" .
3972                                    " ,email='" . $email . "'" .
3973                                    " ,president='" . $president . "'" .
3974                                    " ,counselor='" . $counselor . "'" .
3975                                    " ,secretary='" . $secretary . "'" .
3976                                    " ,eqpres='" . $eqpresidency . "'" .
3977                                    " WHERE presidency=" . $id,__LINE__,__FILE__);
3978                  
3979                } else {
3980                  //print "Adding New Entry<br>";
3981                  $this->db2->query("INSERT INTO eq_presidency (presidency,elder,district,name,"
3982                                    . "email,president,counselor,secretary,eqpres,valid) "
3983                                    . "VALUES (NULL,'" . $elder . "','" . $district . "','"
3984                                    . $name . "','" . $email . "','" . $president  . "','"
3985                                    . $counselor . "','" . $secretary . "','" . $eqpres  . "','1'"
3986                                    .")",__LINE__,__FILE__);
3987                }
3988              } else {
3989                //print "Ignoring Blank Entry<br>";
3990              }
3991            }
3992
3993           // Now update the eq_district table appropriately
3994           
3995           // Delete all the previous district entries from the table
3996           $this->db->query("DELETE from eq_district where valid=1",__LINE__,__FILE__);
3997           $this->db->query("DELETE from eq_district where valid=0",__LINE__,__FILE__);
3998
3999           // Always add a "District 0" assigned to the High Priests Group
4000           $district = 0;
4001           $name = "High Priests";
4002           $elder = 0;
4003           $valid = 0;
4004           $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4005                             . "VALUES ('" . $district . "','" . $name . "','"
4006                             . $elder . "','" . $valid . "'"
4007                             .")",__LINE__,__FILE__);
4008           
4009           
4010           // Requery the eq_presidency table
4011           $sql = "SELECT * FROM eq_presidency where valid=1";
4012           $this->db->query($sql,__LINE__,__FILE__);
4013           while ($this->db->next_record())
4014             {
4015               // Extract the data for each presidency record
4016               $id = $this->db->f('presidency');
4017               $elder = $this->db->f('elder');
4018               $name = $this->db->f('name');
4019               $district = $this->db->f('district');
4020               $name = $this->db->f('name');
4021               $valid = 1;
4022
4023               // If we have a valid district, add it to the district table
4024               if($district > 0) {
4025                 $this->db2->query("INSERT INTO eq_district (district,name,supervisor,valid) "
4026                                   . "VALUES ('" . $district . "','" . $name . "','"
4027                                   . $elder . "','" . $valid . "'"
4028                                   .")",__LINE__,__FILE__);
4029               }
4030               
4031             }
4032           
4033           $this->t->set_var('adminhandle','');
4034           $this->t->pfp('adminhandle','admin'); 
4035         }
4036       else
4037         {
4038           $this->t->set_var('adminhandle','');
4039           $this->t->pfp('adminhandle','admin'); 
4040         }
4041
4042       // Now save off the data needed for an EQ Presidency Table Update
4043       
4044       $sql = "SELECT * FROM eq_presidency where valid=1";
4045       $this->db->query($sql,__LINE__,__FILE__);
4046       $table_data = "";
4047       $header_row = "<th>Elder</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th><th>EQ Presidency</th>";
4048       while ($this->db->next_record())
4049         {
4050           // Extract the data for each presidency record
4051           $id = $this->db->f('presidency');
4052           $elder = $this->db->f('elder');
4053           $district = $this->db->f('district');
4054           $name = $this->db->f('name');
4055           $email = $this->db->f('email');
4056           $president = $this->db->f('president');
4057           $counselor = $this->db->f('counselor');
4058           $secretary = $this->db->f('secretary');
4059           $eqpresidency = $this->db->f('eqpres');
4060
4061           // Create the forms needed in the table
4062           $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4063           
4064           // Presidency ID
4065           $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4066           
4067           // Elder
4068           if($eqpresidency == 0) { 
4069             $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4070             $table_data.= '<option value=0></option>';  
4071             for ($j=0; $j < count($elder_id); $j++) {
4072               $tmp_id = $elder_id[$j];
4073               $name = $elder_name[$j];
4074               if($elder_id[$j] == $elder) { $eldername = $name; $selected = 'selected="selected"'; } else { $selected = ''; }
4075               $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4076             }
4077             $table_data.='</select></td>';
4078             $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$eldername.'">';
4079           } else {
4080             $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="EQ Presidency"></td>';
4081             $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="EQ Presidency">';
4082           }
4083             
4084           // Email Address
4085           $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4086           
4087           // District
4088           $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4089           $table_data.= '<option value=0></option>';
4090           for ($j=0; $j <= $this->max_num_districts; $j++) {
4091             if($district == $j) { $selected = 'selected="selected"'; } else { $selected = ''; }
4092             $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4093           }
4094           $table_data.='</select></td>';
4095           
4096           // President
4097           $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4098           if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4099           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4100           $table_data.='</select></td>';
4101           
4102           // Counselor
4103           $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4104           if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4105           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4106           $table_data.='</select></td>';
4107
4108           // Secretary
4109           $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4110           if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4111           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4112           $table_data.='</select></td>';
4113
4114           // EQ Presidency
4115           $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4116           if($eqpresidency == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4117           else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4118           $table_data.='</select></td>';
4119
4120           // End of ROW
4121           $table_data .= "</tr>\n";
4122           $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4123           $this->t->set_var('tr_color',$tr_color);
4124         }
4125
4126       // Now create 1 blank row to always have a line available to add a new elder with
4127       $id = $this->max_presidency_members;
4128       $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4129       // Presidency ID
4130       $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4131       // Elder
4132       $table_data.= '<td align=center><select name="eqpres['.$id.'][elder]">';
4133       $table_data.= '<option value=0></option>';  
4134       for ($j=0; $j < count($elder_id); $j++) {
4135         $tmp_id = $elder_id[$j];
4136         $name = $elder_name[$j];
4137         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4138       }
4139       $table_data.='</select></td>';
4140       $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4141       // Email Address
4142       $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4143       // District
4144       $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4145       $table_data.= '<option value=0></option>';
4146       for ($j=0; $j <= $this->max_num_districts; $j++) {
4147         if($j == 0) { $selected = 'selected="selected"'; } else { $selected = ''; }
4148         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4149       }
4150       $table_data.='</select></td>';
4151       // President
4152       $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4153       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4154       $table_data.='</select></td>';
4155       // Counselor
4156       $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4157       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4158       $table_data.='</select></td>';
4159       // Secretary
4160       $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4161       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4162       $table_data.='</select></td>';
4163       // EQ Presidency
4164       $table_data.= '<td align=center><select name="eqpres['.$id.'][eqpresidency]">';
4165       $table_data.= '<option value=0>0</option><option value=1>1</option>';
4166       $table_data.='</select></td>';
4167       // End of ROW
4168       $table_data .= "</tr>\n";
4169       $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4170       $this->t->set_var('tr_color',$tr_color);
4171       
4172       $this->t->set_var('header_row',$header_row);
4173       $this->t->set_var('table_data',$table_data);
4174       $this->t->pfp('presidencyhandle','presidency',True);
4175
4176       $this->save_sessiondata();   
4177     }
4178
4179   function email_appt($appointment)
4180     {
4181       //print "Emailing notification of appointment: $appointment <br>";
4182
4183       $sql = "SELECT * FROM eq_appointment where appointment='$appointment'";
4184       $this->db->query($sql,__LINE__,__FILE__);
4185         
4186       while ($this->db->next_record())
4187         {
4188           $appointment = $this->db->f('appointment');
4189           $presidency = $this->db->f('presidency');
4190           $interviewer = "";
4191           $email = "";
4192           $elder = $this->db->f('elder');
4193           $elder_name = "";
4194           $family = $this->db->f('family');
4195           $family_name = "";
4196           $appt_name = "";
4197           $phone = "";
4198           $location = "";
4199           $uid = $this->db->f('uid');
4200                     
4201           // Extract the year, month, day, hours, minutes, seconds from the appointment time
4202           $appt_date = $this->db->f('date');
4203           $date_array = explode("-",$appt_date);
4204           $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4205           $appt_time = $this->db->f('time');
4206           $time_array = explode(":",$appt_time);
4207           $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4208
4209           // Format the appointment time into an iCal UTC equivalent
4210           $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4211           $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4212           $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4213           
4214           // Set the email address of the person making the appointment
4215           $from = $GLOBALS['phpgw_info']['user']['fullname'] . "<" .
4216                   $GLOBALS['phpgw_info']['user']['preferences']['email']['address'] . ">";
4217           
4218           $sql = "SELECT * FROM eq_presidency where presidency='$presidency'";
4219           $this->db2->query($sql,__LINE__,__FILE__);
4220           if($this->db2->next_record()) {
4221             $email = $this->db2->f('email');
4222             $interviewer = $this->db2->f('name');
4223           }
4224
4225           if($elder > 0) { 
4226             $sql = "SELECT * FROM eq_elder where elder='$elder'";
4227             $this->db2->query($sql,__LINE__,__FILE__);
4228             if($this->db2->next_record()) {
4229               $elder_name = $this->db2->f('name');
4230               $phone = $this->db2->f('phone');
4231               $appt_name = $elder_name . " Interview";
4232               $location = "$interviewer"."'s home";
4233               $duration = $this->default_ppi_appt_duration * 60;
4234             }
4235           }
4236
4237           if($family > 0) { 
4238             $sql = "SELECT * FROM eq_family where family='$family'";
4239             $this->db2->query($sql,__LINE__,__FILE__);
4240             if($this->db2->next_record()) {
4241               $family_name = $this->db2->f('name');
4242               $phone = $this->db2->f('phone');
4243               $elder_id = $this->db2->f('elder_id');
4244               $appt_name = $family_name . " Family Visit";
4245               $sql = "SELECT * FROM eq_elder where elder='$elder_id'";
4246               $this->db3->query($sql,__LINE__,__FILE__);
4247               if($this->db3->next_record()) {
4248                 $phone = $this->db3->f('phone');
4249               }
4250               $sql = "SELECT * FROM eq_parent where family='$family'";
4251               $this->db3->query($sql,__LINE__,__FILE__);
4252               if($this->db3->next_record()) {
4253                 $location=$this->db3->f('address');
4254               }
4255               $duration = $this->default_visit_appt_duration * 60;
4256             }
4257           }
4258
4259           $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4260           $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4261           $date = $dtstartstr . "-" . $dtendstr;
4262           $description = "$appt_name : $phone";
4263           
4264           if(($uid == 0) && ($appt_name != "")) { 
4265             // Create a new calendar item for this appointment, since this must be the first time we
4266             // are sending it out.
4267             print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4268             $uid = rand() . rand(); // Generate a random identifier for this appointment
4269             $subject = "Created: $appt_name";
4270             
4271             $this->db->query("UPDATE eq_appointment set" .
4272                              " uid=" . $uid . 
4273                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4274
4275             $action = "PUBLISH";
4276             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4277                                   $dtend, $date, $location, $appt_name, $description, $uid);
4278             
4279           } else if(($uid != 0) && ($appt_name == "")) {
4280             // Remove the calendar item for this appointment since it has already been sent
4281             // and there is no name we have changed it to.
4282             print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4283             $subject = "Canceled: $appt_date $appt_time";
4284             
4285             $this->db->query("UPDATE eq_appointment set" .
4286                              " uid=0" . 
4287                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4288             
4289             $action = "CANCEL";
4290             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4291                                   $dtend, $date, $location, $subject, $subject, $uid);
4292             
4293           } else if($uid != 0) {
4294             // Update the existing appointment since we have changed it
4295             print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4296
4297             $subject = "Canceled: $appt_date $appt_time";
4298             $action = "CANCEL";
4299             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4300                                   $dtend, $date, $location, $subject, $subject, $uid);
4301             
4302             $uid = rand() . rand(); // Generate a random identifier for this appointment
4303             $this->db->query("UPDATE eq_appointment set" .
4304                              " uid=" . $uid .
4305                              " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4306             
4307             $subject = "Updated: $appt_name";       
4308             $action = "PUBLISH";
4309             $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4310                                   $dtend, $date, $location, $appt_name, $description, $uid);
4311           }
4312           
4313         }
4314           
4315       return true;
4316     }
4317
4318   function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4319     {
4320       // Initialize our local variables
4321       $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4322       $message = "";
4323       $headers = "";
4324
4325       // Form the headers for the email message
4326       $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4327       $headers.="Mime-Version: 1.0\n";
4328       $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4329       $headers.="Content-Disposition: inline\n";
4330       $headers.="Reply-To: $from\n";
4331       $headers.="From: $from\n";
4332
4333       // Print the plaintext version of the appointment
4334       $message.="--$boundary\n";
4335       $message.="Content-Type: text/plain; charset=us-ascii\n";
4336       $message.="Content-Disposition: inline\n";
4337       $message.="\n";
4338       $message.="What: $description\n";
4339       $message.="When: $date\n";
4340       $message.="Where: $location\n";
4341       $message.="\n";
4342       
4343       // Print the .ics attachment version of the appointment
4344       $message.="--$boundary\n";
4345       $message.="Content-Type: text/calendar; charset=us-ascii\n";
4346       $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4347       $message.="\n";
4348       $message.="BEGIN:VCALENDAR" . "\n";
4349       $message.="VERSION:2.0" . "\n";
4350       $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4351       $message.="METHOD:$action" . "\n";
4352       $message.="BEGIN:VEVENT" . "\n";
4353       $message.="ORGANIZER:MAILTO:$from". "\n";
4354       $message.="DTSTAMP:$dtstamp" . "\n";
4355       $message.="DTSTART:$dtstart" . "\n";
4356       $message.="DTEND:$dtend" . "\n";
4357       $message.="SUMMARY:$summary" . "\n";
4358       $message.="DESCRIPTION:$description" . "\n";
4359       $message.="LOCATION:$location" . "\n";
4360       $message.="UID:$uid" ."\n";
4361       $message.="TRANSP:OPAQUE" . "\n";
4362       $message.="SEQUENCE:0" . "\n";
4363       $message.="CLASS:PUBLIC" . "\n";
4364       $message.="END:VEVENT" . "\n";
4365       $message.="END:VCALENDAR" . "\n";
4366
4367       // Complete the message
4368       $message.="--$boundary\n";
4369
4370       // Send the message
4371       mail($to, $subject, $message, $headers);
4372       
4373     }
4374
4375   function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4376     {
4377       $form_data = "";
4378       $blank = 0;
4379       
4380       if($hour == 0) { $blank = 1; }
4381
4382       if($this->time_drop_down_lists == 1) {
4383         // Create drop down lists to get the time
4384         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4385         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4386         foreach(range(1,12) as $num) {
4387           if($hour == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4388           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4389         }
4390         $form_data.= '</select>';
4391         $form_data.= '&nbsp;:&nbsp;';
4392         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4393         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4394         $num = 0;
4395         while($num < 60) {
4396           if($num < 10) { $num = "0" . "$num"; }
4397           if($minute == $num) { $selected = 'selected="selected"'; } else { $selected = ''; }
4398           if($blank == 1) { $selected = ""; }
4399           $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4400           $num = $num + $this->time_drop_down_list_inc;
4401         }
4402         $form_data.= '</select>';
4403       } else {
4404         // Use free form text fields to get the time
4405         if($blank == 1) { $hour = ""; $minute = ""; $ampm = ""; }
4406         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4407         $form_data.= ':';
4408         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4409         $form_data.= '&nbsp;';
4410       }
4411       // Always use a drop-down select form for am/pm
4412       $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4413       if($blank == 0) { 
4414         if($pm == 0) { $form_data.= '<option value=0 selected>am</option>'; $form_data.= '<option value=1>pm</option>'; }
4415         if($pm == 1) { $form_data.= '<option value=0>am</option>'; $form_data.= '<option value=1 selected>pm</option>'; }
4416       } else {
4417         $form_data.= '<option value=""></option>';
4418         $form_data.= '<option value=0>am</option>';
4419         $form_data.= '<option value=1>pm</option>';
4420       }
4421       $form_data.= '</select>';
4422       
4423       return $form_data;
4424     }
4425 }
4426
4427 ?>