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