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