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