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