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