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