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