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