5818197a65be37c86ba01e844962f44f94937fac
[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->fp('apptlist','appt_list',True);
1511                 }
1512                 
1513                 // PPI SCHEDULING TABLE
1514                 $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";
1515                 $this->db->query($sql,__LINE__,__FILE__);
1516
1517                 $i=0; 
1518                 $individual = NULL;
1519                 while ($this->db->next_record()) {
1520                         $individual[$i] = $this->db->f('individual');
1521                         $indiv_name[$i] = $this->db->f('name');
1522                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
1523                         $indiv_priority[$individual[$i]] = $this->db->f('priority');
1524                         $indiv_notes[$individual[$i]] = $this->db->f('notes');
1525                         $i++;
1526                         $total_indivs++;
1527                 }
1528
1529                 $max = count($individual);
1530
1531                 for($i=0; $i < $max; $i++) {
1532                         $id = $individual[$i];
1533                         $name = $indiv_name[$i];
1534                         $phone = $indiv_phone[$id];
1535                         $priority = $indiv_priority[$id];
1536                         $notes = $indiv_notes[$id];
1537
1538                         // If this individual has had a yearly PPI this year, don't show him on the schedule list
1539                         $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1540                         $sql = "SELECT * FROM tc_interview WHERE date > '$year_start' AND date < '$year_end' ".
1541                                "AND individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
1542                         $this->db2->query($sql,__LINE__,__FILE__);
1543
1544                         if(!$this->db2->next_record()) {
1545                                 $sql = "SELECT * FROM tc_interview WHERE individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
1546                                 $this->db->query($sql,__LINE__,__FILE__);
1547                                 if($this->db->next_record()) { 
1548                                         $date = $this->db->f('date'); 
1549                                 } else { 
1550                                         $date = ""; 
1551                                 }
1552                                 $link_data['menuaction'] = 'tc.tc.ppi_update';
1553                                 $link_data['individual'] = $id;
1554                                 $link_data['name'] = $name;
1555                                 $link_data['interview'] = '';
1556                                 $link_data['interview_type'] = 1;
1557                                 $link_data['action'] = 'add';
1558                                 $link_data['interviewer'] = $interviewer;
1559                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
1560                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1561                                 $this->t->set_var('tr_color',$tr_color);
1562                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1563                                 $table_data.= "<td align=center>$phone</td>";
1564                                 //$table_data.= "<td align=center>$priority</td>";
1565                                 $table_data.= "<td align=center>";
1566                                 $table_data.= '<select name=notes['.$i.'][pri]>';
1567                                 foreach(range(0,6) as $num) {
1568                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
1569                                         if($priority == $num) { 
1570                                                 $selected[$num] = 'selected="selected"'; 
1571                                         } else { 
1572                                                 $selected[$num] = ''; 
1573                                         }
1574                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1575                                 }
1576                                 $table_data.= '</select></td>';
1577                                 $table_data.= "<td align=center>$date</td>";
1578                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="notes['.$i.'][notes]" value="'.$notes.'">';
1579                                 $table_data.= '<input type=hidden name="notes['.$i.'][individual]" value="'.$id.'">';
1580                                 $table_data.= '<input type=hidden name="notes['.$i.'][indiv_name]" value="'.$name.'">';
1581                                 $table_data.= '</td>';
1582                                 $table_data.= '</tr>';
1583                         } else {
1584                                 $link_data['menuaction'] = 'tc.tc.ppi_update';
1585                                 $link_data['interviewer'] = $this->db2->f('interviewer');
1586                                 $link_data['individual'] = $this->db2->f('individual');
1587                                 $link_data['name'] = $name;
1588                                 $link_data['interview'] = $this->db2->f('interview');
1589                                 $link_data['interview_type'] = $this->db2->f('interview_type');
1590                                 $link_data['action'] = 'view';
1591                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
1592                                 $indivs_with_yearly_ppi++;
1593                                 $date = $this->db2->f('date');
1594                                 $notes = $this->db2->f('notes');
1595                                 if(strlen($notes) > 40) { $notes = substr($notes,0,40) . "..."; }
1596                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1597                                 $this->t->set_var('tr_color2',$tr_color2);
1598                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1599                                 $completed_data.= "<td align=center>$phone</td>";
1600                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1601                                 $completed_data.= "<td align=left>$notes</td>";
1602                                 $completed_data.= '</tr>';
1603                         }
1604                 } // End for individuals Loop
1605
1606                 $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1607                 $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1608                 $completed_header_row = "<th width=$name_width><font size=-2>Individual</th>";
1609                 $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1610                 $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1611                 $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1612
1613                 $this->t->set_var('table_width',$table_width);
1614                 $this->t->set_var('header_row',$header_row);
1615                 $this->t->set_var('table_data',$table_data);
1616                 $this->t->set_var('completed_header_row',$completed_header_row);
1617                 $this->t->set_var('completed_table_width',$completed_table_width);
1618                 $this->t->set_var('completed',$completed_data);
1619                 $this->t->fp('indivlist','individual_list',True); 
1620
1621                 $indivs_width=300; $totals_width=100;
1622                 $totals_table_width=$indivs_width + $totals_width;
1623                 $totals_header_row = "<th width=$indivs_width><font size=-2>Individuals</th>";
1624                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1625                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1626                 $totals_data.= "<td align=left><font size=-2><b>Total Individuals with yearly PPIs completed:</b></font></td>";
1627                 $totals_data.= "<td align=center><font size=-2><b>$indivs_with_yearly_ppi / $total_indivs</b></font></td>";
1628                 $percent = ceil(($indivs_with_yearly_ppi / $total_indivs)*100);
1629                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1630                 $this->t->set_var('tr_color',$tr_color);
1631                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1632                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1633                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1634                 $totals_data.= "</tr>";
1635
1636                 $this->t->set_var('totals',$totals_data);
1637                 $this->t->set_var('totals_header_row',$totals_header_row);
1638                 $this->t->set_var('totals_table_width',$totals_table_width);
1639
1640                 $this->t->pfp('out','ppi_sched_t');
1641                 $this->save_sessiondata(); 
1642
1643         }
1644   
1645         function int_sched()
1646         {
1647                 $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1648                 $this->t->set_block('int_sched_t','individual_list','indivlist');
1649                 $this->t->set_block('int_sched_t','appt_list','apptlist');
1650                 $action = get_var('action',array('GET','POST'));
1651
1652                 $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1653                 $this->t->set_var('lang_reset','Clear Changes');
1654
1655                 $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
1656                 $this->t->set_var('int_link_title','Hometeaching Interviews');
1657
1658                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
1659                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
1660
1661                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched&action=save'));
1662                 $this->t->set_var('title','Hometeaching Interviews Scheduler');
1663
1664                 $indiv_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
1665                 $table_width=$indiv_width + $phone_width + $pri_width + $notes_width + $int_date_width;
1666                 $header_row = "<th width=$indiv_width><font size=-2>Individual</th>";
1667                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1668                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1669                 $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
1670                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1671                 $table_data=""; $completed_data=""; $totals_data="";
1672
1673                 $year = date('Y');
1674                 $month = date('m');
1675                 $nextyear = $year + 1;
1676                 if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
1677                 if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
1678                 if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
1679                 if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
1680                 //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
1681
1682                 // create the individual id -> individual name mapping
1683                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY name ASC";
1684                 $this->db->query($sql,__LINE__,__FILE__);
1685                 $i=0;
1686                 $individual_data = NULL;
1687                 $indiv_name_data = NULL;
1688                 while ($this->db->next_record()) {
1689                         $indiv_name_data[$i] = $this->db->f('name');
1690                         $individual_data[$i] = $this->db->f('individual');
1691                         $individ2name[$individual_data[$i]] = $indiv_name_data[$i];
1692                         $i++;
1693                 }
1694                 // add any YM that are home teachers
1695                 $sql = "SELECT * FROM tc_companionship where valid=1";
1696                 $this->db->query($sql,__LINE__,__FILE__);
1697                 while ($this->db->next_record()) {
1698                         $tmp_individual = $this->db->f('individual');
1699                         $sql = "Select * FROM tc_individual where individual='$tmp_individual' and steward='' and valid=1";
1700                         $this->db2->query($sql,__LINE__,__FILE__);
1701                         while ($this->db2->next_record()) {
1702                                 $indiv_name_data[$i] = $this->db2->f('name');
1703                                 $individual_data[$i] = $this->db2->f('individual');
1704                                 $individ2name[$individual_data[$i]] = $indiv_name_data[$i];
1705                                 $i++;
1706                         }
1707                 }
1708                 array_multisort($indiv_name_data, $individual_data);
1709
1710                 if($action == 'save') {
1711                         // Save any changes made to the appointment table
1712                         $new_data = get_var('appt_notes',array('POST'));
1713                         if($new_data != "") {
1714                                 foreach ($new_data as $entry) {
1715                                         $indiv = $entry['individual'];
1716                                         $appointment = $entry['appointment'];
1717                                         $location = $entry['location'];
1718                                         if($location == "") {
1719                                                 $supervisor = $entry['supervisor'];
1720                                                 $supervisor_array = explode(",", $individ2name[$supervisor]);
1721                                                 $supervisor_last_name = $supervisor_array[0];
1722                                                 $sql = "SELECT * FROM tc_individual where individual='$supervisor'";
1723                                                 $this->db2->query($sql,__LINE__,__FILE__);
1724                                                 if($this->db2->next_record()) {
1725                                                         $supervisor_address = $this->db2->f('address');
1726                                                 }
1727                                                 $location = "$supervisor_last_name"." home ($supervisor_address)";
1728                                         }
1729                                         if($indiv == 0) { $location = ""; }
1730
1731                                         //print "indiv: $indiv appointment: $appointment <br>";
1732                                         //Only perform a database update if we have made a change to this appointment
1733                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and individual='$indiv' and location='$location'";
1734                                         $this->db->query($sql,__LINE__,__FILE__);
1735                                         if(!$this->db->next_record()) {
1736                                                 // Perform database save actions here
1737                                                 $this->db->query("UPDATE tc_appointment set " .
1738                                                                  " individual='" . $indiv . "'" .
1739                                                                  ",location='" . $location . "'" .
1740                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1741                                                 // Email the appointment
1742                                                 $this->email_appt($appointment);
1743                                         }
1744                                 }
1745                         }
1746
1747                         // Save any changes made to the int notes table
1748                         $new_data = get_var('hti_notes',array('POST'));
1749                         foreach ($new_data as $entry) {
1750                                 $hti_notes = $entry['notes'];
1751                                 $individual = $entry['individual'];
1752                                 $indiv_name = $entry['indiv_name'];
1753                                 $hti_pri = $entry['pri'];
1754                                 //print "hti_notes: $hti_notes indiv_name: $indiv_name <Br>";
1755                                 // Perform database save actions here
1756                                 $this->db->query("SELECT * FROM tc_companion WHERE individual=$individual and valid=1",__LINE__,__FILE__);
1757                                 if ($this->db->next_record()) {
1758                                         $scheduling_priority = $this->db->f('scheduling_priority');
1759                                         //$this->logToFile("int_sched", "UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'");
1760                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'",__LINE__,__FILE__);
1761                                 }
1762                         }
1763
1764                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched');
1765                         //Header('Location: ' . $take_me_to_url);
1766                 }
1767
1768                 // Get the Districts
1769                 $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";
1770                 $this->db->query($sql,__LINE__,__FILE__);
1771                 $i=0;
1772                 while ($this->db->next_record()) {
1773                         $district = $this->db->f('district');
1774                         $districts[$i]['district'] = $this->db->f('district');
1775                         $districts[$i]['name'] = $this->db->f('name');
1776                         $districts[$i]['supervisor'] = $this->db->f('supervisor');
1777                         $districts[$i]['presidency'] = $this->db->f('presidency');
1778                         $i++;
1779                 }
1780
1781                 // APPOINTMENT TABLE
1782                 $district = 1;
1783                 $date_width=250; $time_width=100; $indiv_width=200; $location_width=100;
1784                 $appt_table_width=$date_width + $time_width + $indiv_width + $location_width;
1785                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1786                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1787                 $appt_header_row.= "<th width=$indiv_width><font size=-2>Individual</th>";
1788                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
1789                 $appt_table_data = ""; 
1790
1791                 $total_comps=0; $comps_with_quarterly_int=0;
1792
1793                 // Display a scheduling table for each district
1794                 for ($d=0; $d < count($districts); $d++) {
1795                         $table_data=""; $appt_table_data="";
1796                         $this->t->set_var('district_number',$districts[$d]['district']);
1797                         $this->t->set_var('district_name',$districts[$d]['name']);      
1798                         $supervisor = $districts[$d]['supervisor'];
1799                         $supervisor_array = explode(",", $supervisor);
1800                         $supervisor_last_name = $supervisor_array[0];
1801                         $sql = "SELECT * FROM tc_individual where individual='$supervisor'";
1802                         $this->db2->query($sql,__LINE__,__FILE__);
1803                         if($this->db2->next_record()) {
1804                                 $supervisor_address = $this->db2->f('address');
1805                         }
1806                         $location = "$supervisor_last_name"." home ($supervisor_address)";
1807                         $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Individuals with Interviews Not Completed";
1808                         $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
1809                         $this->t->set_var('table_title',$table_title);
1810                         $this->t->set_var('appt_table_title',$appt_table_title);
1811
1812                         // query the database for all the appointments
1813                         $sql = "SELECT * FROM tc_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1814                         $this->db->query($sql,__LINE__,__FILE__);
1815
1816                         while ($this->db->next_record()) {
1817                                 $appointment = $this->db->f('appointment');
1818                                 $indiv = $this->db->f('individual');
1819                                 $location = $this->db->f('location');
1820                                 if(($location == "") && ($indiv > 0)) { $location = "$supervisor_last_name"." home ($supervisor_address)"; }
1821
1822                                 $date = $this->db->f('date');
1823                                 $date_array = explode("-",$date);
1824                                 $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1825                                 $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1826
1827                                 $time = $this->db->f('time');
1828                                 $time_array = explode(":",$time);
1829                                 $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1830
1831                                 $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1832                                 $appt_table_data.= "<td align=center>$day_string</td>";
1833                                 $appt_table_data.= "<td align=center>$time_string</td>";
1834
1835                                 $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][individual]>';
1836                                 $appt_table_data.= '<option value=0></option>';
1837                                 for ($i=0; $i < count($individual_data); $i++) {
1838                                         $id = $individual_data[$i];
1839                                         $name = $indiv_name_data[$i];
1840                                         if($individual_data[$i] == $indiv) { 
1841                                                 $selected[$id] = 'selected="selected"'; 
1842                                         } else { 
1843                                                 $selected[$id] = ''; 
1844                                         }
1845                                         $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1846                                 }
1847                                 $appt_table_data.='</select></td>';
1848
1849                                 $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
1850                                 $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
1851
1852                                 $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1853                                 $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][supervisor]" value="'.$supervisor.'">';
1854
1855                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1856                                 $this->t->set_var('tr_color',$tr_color);
1857                         }
1858
1859                         $this->t->set_var('appt_table_data',$appt_table_data);
1860                         $this->t->set_var('appt_header_row',$appt_header_row);
1861                         $this->t->set_var('appt_table_width',$appt_table_width);
1862
1863                         // INTERVIEW SCHEDULING TABLE
1864
1865                         // Select all the unique companionship numbers for this district
1866                         $sql = "SELECT distinct companionship FROM tc_companionship where valid=1 and district=". $districts[$d]['district'];
1867                         $this->db->query($sql,__LINE__,__FILE__);
1868                         $j=0; $unique_companionships = '';
1869                         while ($this->db->next_record())
1870                         {
1871                                 $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1872                                 $j++;
1873                         }
1874
1875                         $i=0;
1876                         for ($j=0; $j < count($unique_companionships); $j++) {
1877                                 // Select all the companions from each companionship
1878                                 $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'];
1879                                 $this->db->query($sql,__LINE__,__FILE__);
1880                                 $k=0; $int_completed=0;
1881                                 $comp = $unique_companionships[$j]['companionship'];
1882                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1883                                 $this->t->set_var('tr_color',$tr_color);
1884                                 $total_comps++;
1885                                 while ($this->db->next_record()) {
1886                                         // Get this companions information
1887                                         $individual = $this->db->f('individual');
1888
1889                                         $id = $this->db->f('individual');
1890                                         $name = $this->db->f('name');
1891                                         $phone = $this->db->f('phone');
1892                                         $hti_pri = $this->db->f('priority');
1893                                         $hti_notes = $this->db->f('notes');
1894
1895                                         // If the companionship has already had its quarterly interview,
1896                                         // Skip the other companion in the companionship.
1897                                         if($int_completed == 1) {
1898                                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1899                                                 $completed_data.= "<td align=center>$phone</td>";
1900                                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1901                                                 $completed_data.= "<td align=left>$hti_notes</td>";
1902                                                 $completed_data.= '</tr>';
1903                                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1904                                                 $this->t->set_var('tr_color2',$tr_color2);
1905                                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1906                                                 $this->t->set_var('tr_color',$tr_color);
1907                                                 continue;
1908                                         }
1909
1910                                         // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
1911                                         $sql = "SELECT * FROM tc_interview WHERE date >= '$quarter_start' AND date < '$quarter_end' AND individual='$id' AND interview_type='hti'";
1912                                         $this->db2->query($sql,__LINE__,__FILE__);
1913
1914                                         if(!$this->db2->next_record()) {
1915                                                 $sql = "SELECT * FROM tc_interview WHERE individual='$id' AND interview_type='hti' ORDER BY date DESC";
1916                                                 $this->db3->query($sql,__LINE__,__FILE__);
1917                                                 if($this->db3->next_record()) { 
1918                                                         $date = $this->db3->f('date'); 
1919                                                 } else { 
1920                                                         $date = ""; 
1921                                                 }
1922                                                 $link_data['menuaction'] = 'tc.tc.int_update';
1923                                                 $link_data['individual'] = $id;
1924                                                 $link_data['name'] = $name;
1925                                                 $link_data['interview'] = '';
1926                                                 $link_data['action'] = 'add';
1927                                                 $link_data['interview_type'] = 'hti';
1928                                                 $link_data['interviewer'] = $districts[$d]['supervisor'];
1929                                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
1930                                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1931                                                 $table_data.= "<td align=center>$phone</td>";
1932                                                 $table_data.= "<td align=center>";
1933                                                 $table_data.= '<select name=hti_notes['.$i.'][pri]>';
1934                                                 foreach(range(0,6) as $num) {
1935                                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
1936                                                         if($hti_pri == $num) { 
1937                                                                 $selected[$num] = 'selected="selected"'; 
1938                                                         } else { 
1939                                                                 $selected[$num] = ''; 
1940                                                         }
1941                                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1942                                                 }
1943                                                 $table_data.= '</select></td>';
1944                                                 $table_data.= "<td align=center>$date</td>";
1945                                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="hti_notes['.$i.'][notes]" value="'.$hti_notes.'">';
1946                                                 $table_data.= '<input type=hidden name="hti_notes['.$i.'][individual]" value="'.$id.'">';
1947                                                 $table_data.= '<input type=hidden name="hti_notes['.$i.'][indiv_name]" value="'.$name.'">';
1948                                                 $table_data.= '</td>';
1949                                                 $table_data.= '</tr>'."\n";
1950                                                 $i++;
1951                                         } else {
1952                                                 $link_data['menuaction'] = 'tc.tc.int_update';
1953                                                 $link_data['interviewer'] = $this->db2->f('interviewer');
1954                                                 $link_data['individual'] = $this->db2->f('individual');
1955                                                 $link_data['name'] = $name;
1956                                                 $link_data['interview'] = $this->db2->f('interview');
1957                                                 $link_data['interview_type'] = 'hti';
1958                                                 $link_data['action'] = 'view';
1959                                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
1960                                                 $comps_with_quarterly_int++;
1961                                                 $int_completed=1;
1962                                                 $date = $this->db2->f('date');
1963                                                 $hti_notes = $this->db2->f('notes');
1964                                                 if(strlen($hti_notes) > 40) { $hti_notes = substr($hti_notes,0,40) . "..."; }
1965                                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1966                                                 $completed_data.= "<td align=center>$phone</td>";
1967                                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1968                                                 $completed_data.= "<td align=left>$hti_notes</td>";
1969                                                 $completed_data.= '</tr>';
1970                                         }
1971                                 }
1972                         }
1973
1974                         $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1975                         $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1976                         $completed_header_row = "<th width=$name_width><font size=-2>Individual</th>";
1977                         $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1978                         $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1979                         $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
1980
1981                         $this->t->set_var('table_width',$table_width);
1982                         $this->t->set_var('header_row',$header_row);
1983                         $this->t->set_var('table_data',$table_data);
1984                         $this->t->set_var('completed_header_row',$completed_header_row);
1985                         $this->t->set_var('completed_table_width',$completed_table_width);
1986                         $this->t->set_var('completed',$completed_data);
1987                         $this->t->fp('indivlist','individual_list',True);
1988
1989                 } // End for each district loop
1990
1991
1992                 $indivs_width=300; $totals_width=100;
1993                 $totals_table_width=$indivs_width + $totals_width;
1994                 $totals_header_row = "<th width=$indivs_width><font size=-2>Individuals</th>";
1995                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1996                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1997                 $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
1998                 $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
1999                 $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
2000                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2001                 $this->t->set_var('tr_color',$tr_color);
2002                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2003                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2004                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2005                 $totals_data.= "</tr>";
2006
2007                 $this->t->set_var('totals',$totals_data);
2008                 $this->t->set_var('totals_header_row',$totals_header_row);
2009                 $this->t->set_var('totals_table_width',$totals_table_width);
2010
2011                 $this->t->pfp('out','int_sched_t');
2012                 $this->save_sessiondata(); 
2013
2014         }
2015   
2016         function vis_sched()
2017         {
2018                 $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
2019                 $this->t->set_block('vis_sched_t','family_list','familylist');
2020                 $this->t->set_block('vis_sched_t','appt_list','apptlist');
2021                 $action = get_var('action',array('GET','POST'));
2022
2023                 $this->t->set_var('lang_save','Save Appt / Pri / Notes');
2024                 $this->t->set_var('lang_reset','Clear Changes');
2025
2026                 $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
2027                 $this->t->set_var('vis_link_title','View Yearly Visits');
2028
2029                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
2030                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2031
2032                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched&action=save'));
2033                 $this->t->set_var('title','Presidency Yearly Visit Scheduler');
2034
2035                 $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
2036                 $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
2037                 $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
2038                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
2039                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
2040                 $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
2041                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2042                 $table_data=""; $completed_data=""; $totals_data="";
2043
2044                 $year = date('Y');
2045
2046                 // create the family id -> family name mapping
2047                 $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";
2048                 $this->db->query($sql,__LINE__,__FILE__);
2049                 $i=0;
2050                 $family_id = NULL;
2051                 while ($this->db->next_record()) {
2052                         $family_id[$i] = $this->db->f('family');
2053                         $family_name[$i] = $this->db->f('name');
2054                         $familyid2name[$family_id[$i]] = $family_name[$i];
2055                         $familyid2address[$family_id[$i]] = $this->db->f('address');
2056                         $i++;
2057                 }
2058                 array_multisort($family_name, $family_id);
2059
2060                 if($action == 'save') {
2061                         // Save any changes made to the appointment table
2062                         $new_data = get_var('appt_notes',array('POST'));
2063                         if($new_data != "") {
2064                                 foreach ($new_data as $entry) {
2065                                         $family = $entry['family'];
2066                                         $appointment = $entry['appointment'];
2067                                         $location = $entry['location'];
2068                                         if($location == "") {
2069                                                 $family_name_array = explode(",", $familyid2name[$family]);
2070                                                 $family_last_name = $family_name_array[0];
2071                                                 $family_address = $familyid2address[$family];
2072                                                 $location = "$family_last_name"." home ($family_address)";
2073                                         }
2074                                         if($family == 0) { $location = ""; }
2075
2076                                         //Only perform a database update if we have made a change to this appointment
2077                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and family='$family' and location='$location'";
2078                                         $this->db->query($sql,__LINE__,__FILE__);
2079                                         if(!$this->db->next_record()) {
2080                                                 // Perform database save actions here
2081                                                 $this->db->query("UPDATE tc_appointment set " .
2082                                                                  " family='" . $family . "'" .
2083                                                                  ",location='" . $location . "'" .
2084                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2085
2086                                                 // Email the appointment
2087                                                 $this->email_appt($appointment);
2088                                         }
2089                                 }
2090                         }
2091
2092                         // Save any changes made to the visit notes table
2093                         $new_data = get_var('vis_notes',array('POST'));
2094                         foreach ($new_data as $entry) {
2095                                 $visit_notes = $entry['notes'];
2096                                 $family = $entry['family_id'];
2097                                 $visit_pri = $entry['pri'];
2098                                 // Perform database save actions here
2099                                 $this->db->query("SELECT * FROM tc_family WHERE family='$family'",__LINE__,__FILE__);
2100                                 if ($this->db->next_record()) {
2101                                         $scheduling_priority = $this->db->f('scheduling_priority');
2102                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$visit_pri', notes=\"$visit_notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
2103                                 }
2104                         }
2105
2106                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched');
2107                         //Header('Location: ' . $take_me_to_url);
2108                 }
2109
2110                 // APPOINTMENT TABLE
2111                 $date_width=250; $time_width=100; $family_width=250; $location_width=100;
2112                 $appt_table_width=$date_width + $time_width + $family_width + $location_width;
2113                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2114                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2115                 $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
2116                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
2117                 $appt_table_data = ""; 
2118
2119                 // Find out what the President ID is
2120                 $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";
2121                 $this->db->query($sql,__LINE__,__FILE__);
2122                 if($this->db->next_record()) {
2123                         $presidency_name = $this->db->f('name');
2124                         $presidency_id = $this->db->f('presidency');
2125                 } else {
2126                         print "<hr><font color=red><h3>-E- Unable to locate Presidency in tc_presidency table</h3></font></hr>";
2127                         return;
2128                 }
2129
2130                 // query the database for all the appointments
2131                 $sql = "SELECT * FROM tc_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
2132                 $this->db->query($sql,__LINE__,__FILE__);
2133
2134                 while ($this->db->next_record()) {
2135                         $appointment = $this->db->f('appointment');
2136                         $family = $this->db->f('family');
2137                         $location = $this->db->f('location');
2138                         $family_name_array = explode(",", $familyid2name[$family]);
2139                         $family_last_name = $family_name_array[0];
2140                         $family_address = $familyid2address[$family];
2141                         if(($location == "") && ($family > 0)) { $location = "$family_last_name"." home ($family_address)"; }
2142
2143                         $date = $this->db->f('date');
2144                         $date_array = explode("-",$date);
2145                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2146                         $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2147
2148                         $time = $this->db->f('time');
2149                         $time_array = explode(":",$time);
2150                         $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2151
2152                         $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2153                         $appt_table_data.= "<td align=center>$day_string</td>";
2154                         $appt_table_data.= "<td align=center>$time_string</td>";
2155
2156                         $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
2157                         $appt_table_data.= '<option value=0></option>';
2158                         for ($i=0; $i < count($family_id); $i++) {
2159                                 $id = $family_id[$i];
2160                                 $name = $family_name[$i];
2161                                 if($family_id[$i] == $family) { 
2162                                         $selected[$id] = 'selected="selected"'; 
2163                                 } else { 
2164                                         $selected[$id] = ''; 
2165                                 }
2166                                 $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
2167                         }
2168                         $appt_table_data.='</select></td>';
2169
2170                         $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
2171                         $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
2172
2173                         $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2174
2175                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2176                         $this->t->set_var('tr_color',$tr_color);
2177                 }
2178
2179                 $this->t->set_var('appt_table_data',$appt_table_data);
2180                 $this->t->set_var('appt_header_row',$appt_header_row);
2181                 $this->t->set_var('appt_table_width',$appt_table_width);
2182
2183
2184                 // VISIT SCHEDULING TABLE
2185                 $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";
2186                 $this->db->query($sql,__LINE__,__FILE__);
2187
2188                 $total_families=0; $families_with_yearly_visit=0;
2189
2190                 while ( $this->db->next_record()) {
2191                         $total_families++;
2192                         $id = $this->db->f('family');
2193                         $name = $this->db->f('name');
2194                         $phone = $this->db->f('phone');
2195                         $vis_pri = $this->db->f('priority');
2196                         $vis_notes = $this->db->f('notes');
2197
2198                         // If this family has had a yearly visit this year, don't show them on the schedule list
2199                         $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2200                         $sql = "SELECT * FROM tc_visit WHERE date > '$year_start' AND date < '$year_end' ".
2201                                "AND family=" . $id . " AND companionship=0";
2202                         $this->db2->query($sql,__LINE__,__FILE__);
2203
2204                         if(!$this->db2->next_record()) {
2205                                 $sql = "SELECT * FROM tc_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
2206                                 $this->db3->query($sql,__LINE__,__FILE__);
2207                                 if($this->db3->next_record()) { 
2208                                         $date = $this->db3->f('date'); 
2209                                 } else { 
2210                                         $date = ""; 
2211                                 }
2212                                 $link_data['menuaction'] = 'tc.tc.vis_update';
2213                                 $link_data['visit'] = '';
2214                                 $link_data['family'] = $id;
2215                                 $link_data['name'] = $name;
2216                                 $link_data['action'] = 'add';
2217                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2218                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2219                                 $table_data.= "<td align=center>$phone</td>";
2220                                 $table_data.= "<td align=center>";
2221                                 $table_data.= '<select name=vis_notes['.$id.'][pri]>';
2222                                 foreach(range(0,6) as $num) {
2223                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
2224                                         if($vis_pri == $num) { 
2225                                                 $selected[$num] = 'selected="selected"'; 
2226                                         } else { 
2227                                                 $selected[$num] = ''; 
2228                                         }
2229                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2230                                 }
2231                                 $table_data.= '</select></td>';
2232                                 $table_data.= "<td align=center>$date</td>";
2233                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$id.'][notes]" value="'.$vis_notes.'">';
2234                                 $table_data.= '<input type=hidden name="vis_notes['.$id.'][family_id]" value="'.$id.'">';
2235                                 $table_data.= '<input type=hidden name="vis_notes['.$id.'][family_name]" value="'.$name.'">';
2236                                 $table_data.= '</td>';
2237                                 $table_data.= '</tr>';
2238                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2239                                 $this->t->set_var('tr_color',$tr_color);
2240                         } else {
2241                                 $link_data['menuaction'] = 'tc.tc.vis_update';
2242                                 $link_data['visit'] = $this->db2->f('visit');
2243                                 $link_data['family'] = $this->db2->f('family');
2244                                 $link_data['name'] = $name;
2245                                 $link_data['date'] = $this->db2->f('date');
2246                                 $link_data['action'] = 'view';
2247                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
2248                                 $families_with_yearly_visit++;
2249                                 $date = $this->db2->f('date');
2250                                 $vis_notes = $this->db2->f('notes');
2251                                 if(strlen($vis_notes) > 40) { $vis_notes = stripslashes(substr($vis_notes,0,40) . "..."); }
2252                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2253                                 $completed_data.= "<td align=center>$phone</td>";
2254                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2255                                 $completed_data.= "<td align=left>$vis_notes</td>";
2256                                 $completed_data.= '</tr>';
2257                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2258                                 $this->t->set_var('tr_color2',$tr_color2);
2259                         }
2260                 }
2261
2262                 $name_width=190; $phone_width=100; $date_width=100; $notes_width=300;
2263                 $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2264                 $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
2265                 $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2266                 $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2267                 $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
2268
2269                 $family_width=300; $totals_width=100;
2270                 $totals_table_width=$family_width + $totals_width;
2271                 $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
2272                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2273                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2274                 $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
2275                 $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
2276                 $percent = ceil(($families_with_yearly_visit / $total_families)*100);
2277                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2278                 $this->t->set_var('tr_color',$tr_color);
2279                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2280                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2281                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2282                 $totals_data.= "</tr>";
2283
2284                 $this->t->set_var('table_width',$table_width);
2285                 $this->t->set_var('header_row',$header_row);
2286                 $this->t->set_var('table_data',$table_data);
2287                 $this->t->set_var('totals_header_row',$totals_header_row);
2288                 $this->t->set_var('totals_table_width',$totals_table_width);
2289                 $this->t->set_var('completed_header_row',$completed_header_row);
2290                 $this->t->set_var('completed_table_width',$completed_table_width);
2291                 $this->t->set_var('completed',$completed_data);
2292                 $this->t->set_var('totals',$totals_data);
2293                 $this->t->fp('familylist','family_list',True);
2294                 $this->t->fp('apptlist','appt_list',True);
2295
2296                 $this->t->pfp('out','vis_sched_t');
2297                 $this->save_sessiondata(); 
2298         }
2299   
2300         function ppi_view()
2301         {
2302                 $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
2303                 $this->t->set_block('ppi_view_t','district_list','list');
2304
2305                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2306                 $num_months = get_var('num_months',array('GET','POST'));
2307                 if($num_months == '') { $num_months = $this->default_ppi_num_months; }
2308                 $this->t->set_var('num_months',$num_months);
2309                 if($num_months == 1) { 
2310                         $this->t->set_var('lang_num_months','Month of History'); 
2311                 } else {  
2312                         $this->t->set_var('lang_num_months','Months of History'); 
2313                 }
2314                 $this->t->set_var('lang_filter','Filter');
2315                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2316
2317                 $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2318                 $this->t->set_var('ppi_link_title','Yearly PPIs'); 
2319
2320                 $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched'));
2321                 $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2322
2323                 $this->t->set_var('title','Yearly PPIs');
2324                 $num_months = get_var('num_months',array('GET','POST'));
2325                 if($num_months == '') { $num_months = $this->default_ppi_num_years; }
2326                 $this->t->set_var('num_months',$num_months);
2327                 if($num_months == 1) { 
2328                         $this->t->set_var('lang_num_months','Year of History'); 
2329                 } else { 
2330                         $this->t->set_var('lang_num_months','Years of History');
2331                 }
2332
2333                 $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";
2334                 $this->db->query($sql,__LINE__,__FILE__);
2335                 if($this->db->next_record()) {
2336                         $president_name = $this->db->f('name');
2337                         $interviewer = $this->db->f('individual');
2338                 } else {
2339                         print "<hr><font color=red><h3>-E- Unable to locate President in tc_presidency table</h3></font></hr>";
2340                         return;
2341                 }
2342                 $this->t->set_var('district_number','*');
2343                 $this->t->set_var('district_name',$president_name);
2344
2345                 $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";
2346                 $this->db->query($sql,__LINE__,__FILE__);
2347                 $i=0;
2348                 while ($this->db->next_record()) {
2349                         $individual[$i] = $this->db->f('individual');
2350                         $indiv_name[$i] = $this->db->f('name');
2351                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
2352                         $indiv_priority[$individual[$i]] = $this->db->f('priority');
2353                         $indiv_notes[$individual[$i]] = $this->db->f('notes');
2354                         $i++;
2355                 }
2356                 $total_indivs=$i;
2357                 array_multisort($indiv_name, $individual);
2358                 //var_dump($indiv_name); print "<br><br>"; var_dump($individual);
2359
2360                 $header_row="<th width=$comp_width><font size=-2>Individual</th>";
2361
2362                 $indiv_width=400; $ppi_width=75; $table_width=$indiv_width + $num_months*$ppi_width;
2363                 $table_data="";
2364                 for($m=$num_months; $m >= 0; $m--) {
2365                         $year = date('Y') - $m;
2366                         $header_row .= "<th width=150><font size=-2>$year</th>"; 
2367                         $ppis[$m] = 0;
2368                 }
2369
2370                 for ($j=0; $j < count($individual); $j++) {
2371                         $id = $individual[$j];
2372                         $name = $indiv_name[$j];
2373                         $phone = $indiv_phone[$id];
2374
2375                         $link_data['menuaction'] = 'tc.tc.ppi_update';
2376                         $link_data['interviewer'] = $interviewer;
2377                         $link_data['individual'] = $id;
2378                         $link_data['name'] = $name;
2379                         $link_data['interview'] = '';
2380                         $link_data['interview_type'] = 'ppi'; 
2381                         $link_data['action'] = 'add';
2382                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2383                         $this->nextmatchs->template_alternate_row_color(&$this->t);
2384                         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2385
2386                         // Find out how many times PPIs were performed in the past $num_months for this individual
2387                         for($m=$num_months; $m >= 0; $m--) {
2388                                 $year = date('Y') - $m;
2389                                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2390                                 $sql = "SELECT * FROM tc_interview WHERE date > '$year_start' AND date < '$year_end' ".
2391                                        "AND individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
2392                                 $this->db2->query($sql,__LINE__,__FILE__);
2393
2394                                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
2395                                 if($this->db2->next_record()) {
2396                                         $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
2397                                         $link_data['menuaction'] = 'tc.tc.ppi_update';
2398                                         $link_data['companionship'] = $companionship;
2399                                         $link_data['interviewer'] = $this->db2->f('interviewer');
2400                                         $link_data['indiv'] = $id;
2401                                         $link_data['name'] = $name;
2402                                         $link_data['interview'] = $this->db2->f('interview');
2403                                         $link_data['interview_type'] = 'ppi';
2404                                         $link_data['action'] = 'view';
2405                                         $date = $this->db2->f('date');
2406                                         $date_array = explode("-",$date);
2407                                         $month = $date_array[1];
2408                                         $day   = $date_array[2];
2409                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2410                                         $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2411                                 } else { 
2412                                         $table_data .= "<td>&nbsp;</td>"; 
2413                                 }
2414                         }
2415                         $table_data .= "</tr>\n"; 
2416                 }
2417                 $table_data .= "<tr><td colspan=20><hr></td></tr>";
2418
2419                 $stat_data = "<tr><td><b><font size=-2>$total_indivs Individuals<br>PPI Totals:</font></b></td>";
2420                 for($m=$num_months; $m >=0; $m--) {
2421                         $percent = ceil(($ppis[$m] / $total_indivs)*100);
2422                         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
2423                 }
2424                 $stat_data .= "</tr>";
2425
2426                 $this->t->set_var('table_width',$table_width);
2427                 $this->t->set_var('header_row',$header_row);
2428                 $this->t->set_var('table_data',$table_data);
2429                 $this->t->set_var('stat_data',$stat_data);
2430                 $this->t->pfp('out','ppi_view_t');
2431                 $this->save_sessiondata(); 
2432         }
2433
2434         function ppi_update()
2435         {
2436                 $this->t->set_file(array('form' => 'ppi_update.tpl'));
2437                 $this->t->set_block('form','interviewer_list','int_list');
2438                 $this->t->set_block('form','add','addhandle');
2439                 $this->t->set_block('form','edit','edithandle');
2440
2441                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2442                 $this->t->set_var('readonly','');
2443                 $this->t->set_var('disabled','');
2444
2445                 $action = get_var('action',array('GET','POST'));
2446                 $companionship = get_var('companionship',array('GET','POST'));
2447                 $interviewer = get_var('interviewer',array('GET','POST'));      
2448                 $name = get_var('name',array('GET','POST'));
2449                 $interview = get_var('interview',array('GET','POST'));
2450                 $individual = get_var('individual',array('GET','POST'));
2451                 $date = get_var('date',array('GET','POST'));
2452                 $notes = get_var('notes',array('GET','POST'));
2453                 $interview_type = get_var('interview_type',array('GET','POST'));
2454
2455             $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1 AND ";
2456             if($this->yearly_ppi_interviewer == 1) { $sql .= " (tp.president=1)"; }
2457                 if($this->yearly_ppi_interviewer == 2) { $sql .= " (tp.president=1 OR tp.counselor=1)"; }
2458                 if($this->yearly_ppi_interviewer == 3) { $sql .= " (tp.president=1 OR tp.counselor=1 OR tp.secretary=1)"; }
2459                 $this->db2->query($sql,__LINE__,__FILE__);
2460                 while ($this->db2->next_record()) {
2461                         $indiv = $this->db2->f('individual');
2462                         $interviewer_name = $this->db2->f('name');
2463                         if($indiv == $interviewer) {
2464                                 $this->t->set_var('interviewer',$indiv . ' selected');
2465                         } else {
2466                                 $this->t->set_var('interviewer',$indiv);
2467                         }
2468                         #print "indiv: $indiv interviewer: $interviewer<br>";
2469                         $this->t->set_var('interviewer_name',$interviewer_name);
2470                         $this->t->set_var('eqpresppi_checked','checked');
2471                         $this->t->fp('int_list','interviewer_list',True);
2472                 }
2473                 #print "selected interviewer: $interviewer<br>";
2474                 if($action == 'save') {
2475                         $notes = get_var('notes',array('POST'));
2476                         $this->db->query("UPDATE tc_interview set " .
2477                                          "   interview='" . $interview . "'" .
2478                                          ", interviewer='" . $interviewer . "'" .
2479                                          ", individual='" . $individual . "'" .
2480                                          ", date='" . $date . "'" .
2481                                          ", notes=\"" . $notes . "\"" .
2482                                          ", interview_type='" . $interview_type . "'" .
2483                                          " WHERE interview=" . $interview,__LINE__,__FILE__);
2484                         $this->ppi_view();
2485                         return false;
2486                 }
2487
2488                 if($action == 'insert') {
2489                         $notes = get_var('notes',array('POST'));
2490                         $this->db->query("INSERT INTO tc_interview (interviewer,individual,date,notes,interview_type) " .
2491                                          "VALUES ('" . $interviewer . "','" . $individual . "','" .
2492                                          $date . "',\"" . $notes . "\",'" . $interview_type  ."')",__LINE__,__FILE__);
2493                         $this->ppi_view();
2494                         return false;
2495                 }
2496
2497                 if($action == 'add') {
2498                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2499                         $this->t->set_var('interview', '');
2500                         $this->t->set_var('interviewer', $interviewer);
2501                         $this->t->set_var('name',$name);
2502                         $this->t->set_var('individual',$individual);
2503                         $this->t->set_var('date','');
2504                         $this->t->set_var('notes','');
2505                         $this->t->set_var('interview_type',$interview_type);
2506                         $this->t->set_var('eqpresppi_checked','checked');
2507                         $this->t->set_var('lang_done','Cancel');
2508                         $this->t->set_var('lang_action','Adding New PPI');
2509                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview=' .
2510                                           $interview . '&action=' . 'insert'));
2511                 }
2512
2513                 if($action == 'edit' || $action == 'view') {
2514                         $sql = "SELECT * FROM tc_interview WHERE interview=" . $interview . " AND interview_type='ppi'";
2515                         $this->db->query($sql,__LINE__,__FILE__);
2516                         $this->db->next_record();
2517                         $this->t->set_var('interview',$interview);
2518                         $this->t->set_var('name',$name);
2519                         $this->t->set_var('interviewer', $this->db->f('interviewer'));
2520                         $this->t->set_var('individual',$this->db->f('individual'));
2521                         $this->t->set_var('date',$this->db->f('date'));
2522                         $this->t->set_var('notes',$this->db->f('notes'));
2523                         $this->t->set_var('interview_type',$this->db->f('interview_type'));
2524                         if($this->db->f('interview_type') == 'ppi') { $this->t->set_var('eqpresppi_checked','checked'); }
2525                 }
2526
2527                 if($action == 'edit') {
2528                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2529                         $this->t->set_var('lang_done','Cancel');
2530                         $this->t->set_var('lang_action','Editing PPI');
2531                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview='. 
2532                                           $interview . '&action=' . 'save'));
2533                 }
2534
2535                 if($action == 'view') {
2536                         $date = $this->db->f('date');
2537                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2538                         $this->t->set_var('readonly','READONLY');
2539                         $this->t->set_var('disabled','DISABLED');
2540                         $this->t->set_var('lang_done','Done');
2541                         $this->t->set_var('lang_action','Viewing PPI');
2542                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview=' .
2543                                           $interview . '&action=' . 'edit'));
2544                 }
2545
2546                 $this->t->set_var('lang_reset','Clear Form');
2547                 $this->t->set_var('lang_add','Add PPI');
2548                 $this->t->set_var('lang_save','Save Changes');
2549                 $this->t->set_var('edithandle','');
2550                 $this->t->set_var('addhandle','');
2551
2552                 $this->t->pfp('out','form');
2553
2554                 if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2555                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2556                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
2557
2558                 $this->save_sessiondata(); 
2559         }
2560
2561         function int_view()
2562         {
2563                 $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2564                 $this->t->set_block('int_view_t','district_list','list');
2565
2566                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2567                 $num_quarters = get_var('num_quarters',array('GET','POST'));
2568                 if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2569                 $this->t->set_var('num_quarters',$num_quarters);
2570                 if($num_quarters == 1) { 
2571                         $this->t->set_var('lang_num_quarters','Quarter of History'); 
2572                 } else {
2573                         $this->t->set_var('lang_num_quarters','Quarters of History'); 
2574                 }
2575                 $this->t->set_var('lang_filter','Filter');
2576
2577                 $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2578                 $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2579
2580                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
2581                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2582
2583                 $this->t->set_var('title','Hometeaching Interviews'); 
2584
2585                 $num_months = $num_quarters * 3 - 1;
2586                 $current_month = $this->current_month;
2587                 if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2588                 else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2589                 else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2590                 else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2591
2592                 $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";
2593                 $this->db->query($sql,__LINE__,__FILE__);
2594                 $i=0;
2595                 while ($this->db->next_record()) {
2596                         $districts[$i]['district'] = $this->db->f('district');
2597                         $districts[$i]['name'] = $this->db->f('name');
2598                         $districts[$i]['supervisor'] = $this->db->f('supervisor');
2599                         $i++;
2600                 }
2601
2602                 $sql = "SELECT * FROM tc_individual where valid=1 ORDER BY individual ASC";
2603                 $this->db->query($sql,__LINE__,__FILE__);
2604                 $i=0;
2605                 while ($this->db->next_record()) {
2606                         $individual[$i] = $this->db->f('individual');
2607                         $indiv_name[$i] = $this->db->f('name');
2608                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
2609                         $i++;
2610                 }
2611                 array_multisort($indiv_name, $individual);
2612                 for($i=0; $i < count($individual); $i++) {
2613                         $id = $individual[$i];
2614                         $indivs[$id] = $indiv_name[$i];
2615                 }      
2616
2617                 $total_companionships = 0;
2618                 $this->nextmatchs->template_alternate_row_color(&$this->t);
2619                 for ($i=0; $i < count($districts); $i++) {
2620                         $this->t->set_var('district_number',$districts[$i]['district']);
2621                         $this->t->set_var('district_name',$districts[$i]['name']);      
2622                         $supervisor = $districts[$i]['supervisor'];
2623
2624                         // Select all the unique companionship numbers for this district
2625                         $sql = "SELECT distinct companionship FROM tc_companionship where valid=1 and district=". $districts[$i]['district'];
2626                         $this->db->query($sql,__LINE__,__FILE__);
2627                         $j=0; $unique_companionships = '';
2628                         while ($this->db->next_record()) {
2629                                 $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2630                                 $j++;
2631                         }
2632
2633                         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2634                         $table_data=""; $num_companionships = $j; $num_indivs = 0;
2635                         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2636                         for ($j=0; $j < count($unique_companionships); $j++) {
2637                                 // Select all the companions in each companionship
2638                                 $sql = "SELECT * FROM tc_companion where valid=1 and ".
2639                                        "companionship=". $unique_companionships[$j]['companionship'];
2640                                 $this->db->query($sql,__LINE__,__FILE__);
2641                                 $k=0;
2642                                 $comp = $unique_companionships[$j]['companionship'];
2643                                 for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2644                                 while ($this->db->next_record()) {
2645                                         // Get this companions information
2646                                         $num_indivs++;
2647                                         $companionship = $this->db->f('companionship');
2648                                         $individual = $this->db->f('individual');
2649                                         $name = $indivs[$individual];
2650                                         $phone = $indiv_phone[$individual];
2651                                         $link_data['menuaction'] = 'tc.tc.int_update';
2652                                         $link_data['companionship'] = $companionship;
2653                                         $link_data['interviewer'] = $supervisor;
2654                                         $link_data['individual'] = $individual;
2655                                         $link_data['name'] = $name;
2656                                         $link_data['interview'] = '';
2657                                         $link_data['interview_type'] = 'hti';
2658                                         $link_data['action'] = 'add';
2659                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2660                                         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2661
2662                                         // Find out how many times Interviews were performed in the past $num_months for this individual
2663                                         $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2664                                         for($m=$num_months; $m >= 0; $m--) {
2665                                                 $month = $current_month - $m;
2666                                                 $year = $this->current_year;
2667                                                 if($month <= 0) { 
2668                                                         $remainder = $month; 
2669                                                         $month = 12 + $remainder; 
2670                                                         $year=$year-1; 
2671                                                 }
2672                                                 if($month < 10) { $month = "0"."$month"; }
2673                                                 $month_start = "$year"."-"."$month"."-"."01";
2674                                                 $month_end = "$year"."-"."$month"."-"."31";
2675                                                 $month = "$month"."/"."$year";
2676                                                 $sql = "SELECT * FROM tc_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2677                                                        "AND individual=" . $individual . " AND interview_type='hti' ORDER BY date DESC";
2678                                                 $this->db2->query($sql,__LINE__,__FILE__);
2679                                                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2680
2681                                                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2682                                                 if($this->db2->next_record()) {
2683                                                         if(!$int_recorded[$companionship][$m]) {
2684                                                                 $ints[$m]++; 
2685                                                                 $total_ints[$m]++; 
2686                                                                 $int_recorded[$companionship][$m]=1;
2687                                                         }
2688                                                         $link_data['menuaction'] = 'tc.tc.int_update';
2689                                                         $link_data['companionship'] = $companionship;
2690                                                         $link_data['interviewer'] = $this->db2->f('interviewer');
2691                                                         $link_data['individual'] = $individual;
2692                                                         $link_data['name'] = $name;
2693                                                         $link_data['interview'] = $this->db2->f('interview');
2694                                                         $link_data['action'] = 'view';
2695                                                         $link_data['interview_type'] = 'hti';
2696                                                         $date = $this->db2->f('date');
2697                                                         $date_array = explode("-",$date);
2698                                                         $month = $date_array[1];
2699                                                         $day   = $date_array[2];
2700                                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2701                                                         $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2702                                                 }
2703                                                 else { 
2704                                                         $table_data .= "<td>&nbsp;</td>"; 
2705                                                 }
2706                                         }
2707                                         $table_data .= "</tr>"; 
2708                                         $k++;
2709                                 }
2710                                 $table_data .= "<tr><td colspan=20><hr></td></tr>";
2711                         }
2712                         $total_companionships += $num_companionships;
2713                         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Totals:</font></b></td>";
2714
2715                         // Print the hometeaching interview stats
2716                         for($m=$num_months; $m >=0; $m--) {
2717                                 $month = $current_month - $m;
2718                                 if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2719                                 $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2720                                 //print "$month % $this->monthly_hometeaching_interview_stats = $month_begins <br>";
2721                                 if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2722                                 if(($month_begins) == 1) { 
2723                                         $total = $ints[$m]; 
2724                                 } else { 
2725                                         $total += $ints[$m]; 
2726                                 }
2727                                 $percent = ceil(($total / $num_companionships)*100);
2728                                 $stat_data .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2729                         }
2730                         $stat_data .= "</tr>";
2731
2732                         $this->t->set_var('table_width',$table_width);
2733                         $this->t->set_var('header_row',$header_row);
2734                         $this->t->set_var('table_data',$table_data);
2735                         $this->t->set_var('stat_data',$stat_data);
2736                         $this->t->fp('list','district_list',True);
2737                 }
2738
2739                 // Display the totals
2740                 $total = 0;
2741                 $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Totals:</font></b></td>";
2742                 for($m=$num_months; $m >=0; $m--) {
2743                         $month = $current_month - $m;
2744                         if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2745                         $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2746                         if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2747                         if(($month_begins) == 1) { 
2748                                 $total = $total_ints[$m]; 
2749                         } else { 
2750                                 $total += $total_ints[$m]; 
2751                         }
2752                         $percent = ceil(($total / $total_companionships)*100);
2753                         $totals .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2754                 }
2755                 $totals .= "</tr>";
2756
2757                 $this->t->set_var('totals',$totals);
2758                 $this->t->pfp('out','int_view_t');
2759                 $this->save_sessiondata(); 
2760         }
2761
2762         function int_update()
2763         {
2764                 $this->t->set_file(array('form' => 'int_update.tpl'));
2765                 $this->t->set_block('form','interviewer_list','int_list');
2766                 $this->t->set_block('form','add','addhandle');
2767                 $this->t->set_block('form','edit','edithandle');
2768
2769                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2770                 $this->t->set_var('readonly','');
2771                 $this->t->set_var('disabled','');
2772                 $this->t->set_var('eqpresppi','');
2773
2774                 $action = get_var('action',array('GET','POST'));
2775                 $companionship = get_var('companionship',array('GET','POST'));
2776                 $interviewer = get_var('interviewer',array('GET','POST'));      
2777                 $name = get_var('name',array('GET','POST'));
2778                 $interview = get_var('interview',array('GET','POST'));
2779                 $individual = get_var('individual',array('GET','POST'));
2780                 $date = get_var('date',array('GET','POST'));
2781                 $notes = get_var('notes',array('GET','POST'));
2782                 $interview_type = get_var('interview_type',array('GET','POST'));
2783
2784                 $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)";
2785                 $this->db2->query($sql,__LINE__,__FILE__);
2786                 while ($this->db2->next_record()) {
2787                         $indiv = $this->db2->f('individual');
2788                         $interviewer_name = $this->db2->f('name');
2789                         if($indiv == $interviewer) {
2790                                 $this->t->set_var('interviewer',$indiv . ' selected');
2791                         } else {
2792                                 $this->t->set_var('interviewer',$indiv);
2793                         }
2794                         $this->t->set_var('interviewer_name',$interviewer_name);
2795                         $this->t->fp('int_list','interviewer_list',True);
2796                 }
2797
2798                 if($action == 'save') {
2799                         $notes = get_var('notes',array('POST'));
2800                         $this->db->query("UPDATE tc_interview set " .
2801                                          "   interview='" . $interview . "'" .
2802                                          ", interviewer='" . $interviewer . "'" .
2803                                          ", individual='" . $individual . "'" .
2804                                          ", date='" . $date . "'" .
2805                                          ", notes=\"" . $notes . "\"" .
2806                                          ", interview_type='" . $interview_type . "'" .
2807                                          " WHERE interview=" . $interview,__LINE__,__FILE__);
2808                         $this->int_view();
2809                         return false;
2810                 }
2811
2812                 if($action == 'insert') {
2813                         $notes = get_var('notes',array('POST'));
2814                         $this->db->query("INSERT INTO tc_interview (interviewer,individual,date,notes,interview_type) " .
2815                                          "VALUES ('" . $interviewer . "','" . $individual . "','" .
2816                                          $date . "',\"" . $notes ."\",'" . $interview_type . "')",__LINE__,__FILE__);
2817                         $this->int_view();
2818                         return false;
2819                 }
2820
2821                 if($action == 'add') {
2822                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2823                         $this->t->set_var('interview', '');
2824                         $this->t->set_var('interviewer', $interviewer);
2825                         $this->t->set_var('name',$name);
2826                         $this->t->set_var('individual',$individual);
2827                         $this->t->set_var('date','');
2828                         $this->t->set_var('notes','');
2829                         $this->t->set_var('interview_type',$interview_type);
2830                         $this->t->set_var('lang_done','Cancel');
2831                         $this->t->set_var('lang_action','Adding New Interview');
2832                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
2833                                           $interview . '&action=' . 'insert'));
2834                 }
2835
2836                 if($action == 'edit' || $action == 'view') {
2837                         $sql = "SELECT * FROM tc_interview WHERE interview=" . $interview . " AND interview_type='hti'";
2838                         $this->db->query($sql,__LINE__,__FILE__);
2839                         $this->db->next_record();
2840                         $this->t->set_var('interview',$interview);
2841                         $this->t->set_var('name',$name);
2842                         $this->t->set_var('interviewer', $this->db->f('interviewer'));
2843                         $this->t->set_var('individual',$this->db->f('individual'));
2844                         $this->t->set_var('date',$this->db->f('date'));
2845                         $this->t->set_var('notes',$this->db->f('notes'));
2846                         $this->t->set_var('interview_type',$this->db->f('interview_type'));
2847                         if($this->db->f('interview_type') == 'ppi') { $this->t->set_var('eqpresppi_checked','checked'); }
2848                 }
2849
2850                 if($action == 'edit') {
2851                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2852                         $this->t->set_var('lang_done','Cancel');
2853                         $this->t->set_var('lang_action','Editing Interview');
2854                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
2855                                           $interview . '&action=' . 'save'));
2856                 }
2857
2858                 if($action == 'view') {
2859                         $date = $this->db->f('date');
2860                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2861                         $this->t->set_var('readonly','READONLY');
2862                         $this->t->set_var('disabled','DISABLED');
2863                         $this->t->set_var('lang_done','Done');
2864                         $this->t->set_var('lang_action','Viewing Interview');
2865                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
2866                                           $interview . '&action=' . 'edit'));
2867                 }
2868
2869                 $this->t->set_var('lang_reset','Clear Form');
2870                 $this->t->set_var('lang_add','Add Interview');
2871                 $this->t->set_var('lang_save','Save Changes');
2872                 $this->t->set_var('edithandle','');
2873                 $this->t->set_var('addhandle','');
2874
2875                 $this->t->pfp('out','form');
2876
2877                 if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
2878                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2879                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
2880
2881                 $this->save_sessiondata(); 
2882         }
2883
2884         function vis_view()
2885         {
2886                 $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
2887                 $this->t->set_block('vis_view_t','visit_list','list1');
2888                 $this->t->set_block('vis_view_t','family_list','list2');
2889
2890                 $this->t->set_var('lang_name','Family Name');
2891                 $this->t->set_var('lang_date','Date');
2892
2893                 $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
2894                 $this->t->set_var('vis_link_title','View Yearly Visits');
2895
2896                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
2897                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2898
2899                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
2900                 $num_years = get_var('num_years',array('GET','POST'));
2901                 if($num_years == '') { $num_years = $this->default_vis_num_years; }
2902                 $this->t->set_var('num_years',$num_years);
2903                 if($num_years == 1) { 
2904                         $this->t->set_var('lang_num_years','Year of History'); 
2905                 } else {  
2906                         $this->t->set_var('lang_num_years','Years of History'); 
2907                 }
2908                 $this->t->set_var('lang_filter','Filter');
2909
2910                 $year = date('Y') - $num_years + 1;
2911                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2912
2913                 $sql = "SELECT * FROM tc_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
2914                 $this->db->query($sql,__LINE__,__FILE__);
2915                 $total_records = $this->db->num_rows();
2916
2917                 $i = 0;
2918                 while ($this->db->next_record()) {
2919                         $visit_list[$i]['visit'] = $this->db->f('visit');
2920                         $visit_list[$i]['family'] = $this->db->f('family');
2921                         $visit_list[$i]['date']  = $this->db->f('date');
2922                         $i++;
2923                 }
2924
2925                 for ($i=0; $i < count($visit_list); $i++) {
2926                         $this->nextmatchs->template_alternate_row_color(&$this->t);
2927
2928                         $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'";
2929                         $this->db->query($sql,__LINE__,__FILE__);
2930                         $this->db->next_record();
2931
2932                         $this->t->set_var('family',$visit_list[$i]['family']);
2933                         $this->t->set_var('family_name',$this->db->f('name'));
2934                         $this->t->set_var('date',$visit_list[$i]['date']);
2935
2936                         $link_data['menuaction'] = 'tc.tc.vis_update';
2937                         $link_data['visit'] = $visit_list[$i]['visit'];
2938                         $link_data['name'] = $this->db->f('name');
2939                         $link_data['date'] = $visit_list[$i]['date'];
2940                         $link_data['action'] = 'view';
2941                         $this->t->set_var('view',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
2942                         $this->t->set_var('lang_view','View');
2943
2944                         $link_data['menuaction'] = 'tc.tc.vis_update';
2945                         $link_data['visit'] = $visit_list[$i]['visit'];
2946                         $link_data['name'] = $this->db->f('name');
2947                         $link_data['date'] = $visit_list[$i]['date'];
2948                         $link_data['action'] = 'edit';
2949                         $this->t->set_var('edit',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
2950                         $this->t->set_var('lang_edit','Edit');
2951
2952                         $this->t->fp('list1','visit_list',True);
2953                 }
2954
2955                 // List the families that are available to record a visit against
2956                 $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'";
2957                 $this->db->query($sql,__LINE__,__FILE__);
2958                 $total_records = $this->db->num_rows();
2959
2960                 $i = 0;
2961                 while ($this->db->next_record()) {
2962                         $family_names[$i] = $this->db->f('name');
2963                         $family_ids[$i] = $this->db->f('family');
2964                         $i++;
2965                 } array_multisort($family_names, $family_ids);
2966
2967                 for ($i=0; $i < count($family_names); $i++) {
2968                         $link_data['menuaction'] = 'tc.tc.vis_update';
2969                         $link_data['visit'] = '';
2970                         $link_data['family'] = $family_ids[$i];
2971                         $link_data['action'] = 'add';
2972                         $link_data['name'] = $family_names[$i];
2973                         $this->t->set_var('add',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
2974
2975                         $this->t->set_var('name',$family_names[$i]);
2976                         if(($i+1) % 3 == 0) { 
2977                                 $this->t->set_var('table_sep',"</td></tr><tr>"); 
2978                         } else { 
2979                                 $this->t->set_var('table_sep',"</td>"); 
2980                         }
2981                         if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
2982
2983                         $this->t->fp('list2','family_list',True);
2984                 }   
2985
2986                 $this->t->pfp('out','vis_view_t');
2987                 $this->save_sessiondata(); 
2988         }
2989
2990         function vis_update()
2991         {
2992                 $this->t->set_file(array('form' => 'vis_update.tpl'));
2993                 $this->t->set_block('form','add','addhandle');
2994                 $this->t->set_block('form','edit','edithandle');
2995
2996                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
2997                 $this->t->set_var('readonly','');
2998                 $this->t->set_var('disabled','');
2999
3000                 $action = get_var('action',array('GET','POST'));
3001                 $visit = get_var('visit',array('GET','POST'));
3002                 $family = get_var('family',array('GET','POST'));
3003                 $name = get_var('name',array('GET','POST'));
3004                 $date = get_var('date',array('GET','POST'));
3005                 $notes = get_var('notes',array('GET','POST'));
3006                 $companionship = 0;
3007
3008                 if($action == 'save') {
3009                         $notes = get_var('notes',array('POST'));
3010                         $this->db->query("UPDATE tc_visit set " .
3011                                          "  date='" . $date . "'" .
3012                                          ", notes=\"" . $notes . "\"" .
3013                                          " WHERE visit=" . $visit,__LINE__,__FILE__);
3014                         $this->vis_view();
3015                         return false;
3016                 }
3017
3018                 if($action == 'insert') {
3019                         $notes = get_var('notes',array('POST'));
3020                         $this->db->query("INSERT INTO tc_visit (family,companionship,date,notes) " .
3021                                          "VALUES ('" . $family . "','" . $companionship . "','" .
3022                                          $date . "',\"" . $notes . "\")",__LINE__,__FILE__);
3023                         $this->vis_view();
3024                         return false;
3025                 }
3026
3027                 if($action == 'add') {
3028                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3029                         $this->t->set_var('family', $family);
3030                         $this->t->set_var('visit', '');
3031                         $this->t->set_var('name', $name);
3032                         $this->t->set_var('date','');
3033                         $this->t->set_var('notes','');
3034                         $this->t->set_var('lang_done','Cancel');
3035                         $this->t->set_var('lang_action','Adding New Visit');
3036                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_update&family=' .
3037                                           $family . '&action=' . 'insert'));
3038                 }
3039
3040                 if($action == 'edit' || $action == 'view') {
3041                         $sql = "SELECT * FROM tc_visit WHERE visit=".$visit;
3042                         $this->db->query($sql,__LINE__,__FILE__);
3043                         $this->db->next_record();
3044                         $this->t->set_var('visit',$visit);
3045                         $this->t->set_var('name',$name);
3046                         $this->t->set_var('family', $family);
3047                         $this->t->set_var('date',$this->db->f('date'));
3048                         $this->t->set_var('notes',$this->db->f('notes'));
3049                 }
3050
3051                 if($action == 'edit') {
3052                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3053                         $this->t->set_var('lang_done','Cancel');
3054                         $this->t->set_var('lang_action','Editing Visit');
3055                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_update&visit=' .
3056                                           $visit . '&action=' . 'save'));
3057                 }
3058
3059                 if($action == 'view') {
3060                         $date = $this->db->f('date');
3061                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3062                         $this->t->set_var('readonly','READONLY');
3063                         $this->t->set_var('disabled','DISABLED');
3064                         $this->t->set_var('lang_done','Done');
3065                         $this->t->set_var('lang_action','Viewing Visit');
3066                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_update&visit=' .
3067                                           $visit . '&action=' . 'edit'));
3068                 }
3069
3070                 $this->t->set_var('lang_reset','Clear Form');
3071                 $this->t->set_var('lang_add','Add Visit');
3072                 $this->t->set_var('lang_save','Save Changes');
3073                 $this->t->set_var('edithandle','');
3074                 $this->t->set_var('addhandle','');
3075
3076                 $this->t->pfp('out','form');
3077
3078                 if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3079                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3080                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
3081
3082                 $this->save_sessiondata(); 
3083         }
3084
3085         function att_view()
3086         {
3087                 $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3088                 $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3089                 $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3090
3091                 $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3092                 $this->t->set_block('att_view_t','act_list','list');
3093
3094                 $this->t->set_block('att_view_t','month_list','list1');
3095                 $this->t->set_block('att_view_t','header_list','list2');
3096                 $this->t->set_block('att_view_t','individual_list','list3');
3097
3098                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_view'));
3099                 $num_quarters = get_var('num_quarters',array('GET','POST'));
3100                 if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3101                 $this->t->set_var('num_quarters',$num_quarters);
3102                 $this->t->set_var('lang_filter','Filter');
3103                 if($num_quarters == 1) { 
3104                         $this->t->set_var('lang_num_quarters','Quarter of History'); 
3105                 } else { 
3106                         $this->t->set_var('lang_num_quarters','Quarters of History'); 
3107                 }
3108
3109                 $num_months = $num_quarters * 3;
3110                 $current_month = $this->current_month;
3111                 if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3112                 else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3113                 else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3114                 else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3115
3116                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1";
3117                 $this->db->query($sql,__LINE__,__FILE__);
3118                 $i=0;
3119                 while ($this->db->next_record()) {
3120                         $individual_name[$i] = $this->db->f('name');
3121                         $individual[$i] = $this->db->f('individual');
3122                         $i++;
3123                 }
3124                 array_multisort($individual_name, $individual);
3125
3126                 // Create a list of sunday dates for a window of 3 months back and current month
3127                 $i=0; 
3128                 $last_time = 0; 
3129                 $found_sunday = 0;
3130                 $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, ($current_month-$num_months)+1, 1, date("y")));
3131                 $last_date = explode("-",$sunday_list[0]['date']);
3132                 $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3133                 $time_limit = mktime(0, 0, 0, $current_month, 31, date("y"));
3134                 while($last_time < $time_limit) {
3135                         $day = date("w",$last_time);
3136                         if(date("w",$last_time) == 0) {
3137                                 $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3138                                 $last_date = explode("-",$sunday_list[$i]['date']);
3139                                 $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3140                                 $sunday_list[$i]['day'] = $last_date[2];
3141                                 $sunday_list[$i]['month'] = date("M",$last_time);
3142                                 $sunday_list[$i]['year'] = $last_date[0];
3143                                 $found_sunday = 1;
3144                                 $last_date = $sunday_list[$i]['date'];
3145                         }
3146                         $last_time += 90000;
3147                         if($found_sunday) { $i++; $found_sunday=0; }
3148                 }
3149
3150                 $total_individuals = count($individual);
3151                 $old_month=$sunday_list[0]['month']; $span=0;
3152                 for ($i=0; $i < count($sunday_list); $i++) {
3153                         $date = $sunday_list[$i]['date'];
3154                         $this->t->set_var('date',$sunday_list[$i]['date']);
3155                         $this->t->set_var('day',$sunday_list[$i]['day']);
3156                         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3157                                 if($i == count($sunday_list)-1) { $span++; }
3158                                 $cur_month = $sunday_list[$i]['month'];
3159                                 $old_month = $sunday_list[$i]['month'];   
3160                                 $link_data['menuaction'] = 'tc.tc.att_update';
3161                                 $link_data['month'] = $sunday_list[$i-1]['month'];
3162                                 $link_data['year'] = $sunday_list[$i-1]['year'];
3163                                 $link_data['action'] = 'update_month';
3164                                 $cur_month = $sunday_list[$i-1]['month'];
3165                                 $cur_year = $sunday_list[$i-1]['year'];
3166                                 $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3167                                 $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3168                                 $this->t->set_var('month',$sunday_list[$i-1]['month']);
3169                                 $this->t->set_var('year',$sunday_list[$i-1]['year']);
3170                                 $this->t->set_var('span',$span); $span=0;
3171                                 $this->t->fp('list1','month_list',True);
3172                         }
3173                         $span++;
3174                 }
3175                 $this->t->set_var('total_individuals',$total_individuals);
3176                 $this->t->set_var('header_row',$header_row);
3177
3178                 $individual_width=200; $att_width=25; $total_width=$individual_width; 
3179                 for ($i=0; $i < count($sunday_list); $i++) {
3180                         $link_data['menuaction'] = 'tc.tc.att_update';
3181                         $link_data['month'] = $sunday_list[$i]['month'];
3182                         $link_data['year'] = $sunday_list[$i]['year'];
3183                         $link_data['day'] = $sunday_list[$i]['day'];
3184                         $link_data['date'] = $sunday_list[$i]['date'];
3185                         $link_data['action'] = 'update_day';
3186                         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3187                         $this->t->set_var('date',$sunday_list[$i]['date']);
3188                         $this->t->set_var('day',$sunday_list[$i]['day']);
3189                         $this->t->set_var('month',$sunday_list[$i]['month']);
3190                         $this->t->set_var('year',$sunday_list[$i]['year']);
3191                         $this->t->fp('list2','header_list',True);
3192                         $total_width += $att_width;
3193                         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3194                 }
3195
3196                 for ($i=0; $i < count($individual); $i++) {
3197                         $att_table = "";
3198                         $this->nextmatchs->template_alternate_row_color(&$this->t);
3199                         $this->t->set_var('individual_name',$individual_name[$i]);
3200                         #print "checking for individual: " . $individual[$i] . "<br>";
3201                         for ($j=0; $j < count($sunday_list); $j++) {
3202                                 #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3203                                 #print "SELECT * FROM tc_attendance WHERE date='"
3204                                 #  . $sunday_list[$j]['date'] . "' AND individual=" . $individual[$i] . "<br>";
3205                                 $sql = "SELECT * FROM tc_attendance WHERE date='" .
3206                                        $sunday_list[$j]['date'] . "' AND individual=" . $individual[$i];
3207                                 $this->db->query($sql,__LINE__,__FILE__);
3208                                 if($this->db->next_record()) {
3209                                         $cur_month = $sunday_list[$j]['month'];
3210                                 if($attended[$i][$cur_month] != 1) {
3211                                         $attended[$i][$cur_month]=1;
3212                                         $attendance[$monthnum[$cur_month]]++;
3213                                 } 
3214                                         $att_table .= '<td align=center><img src="images/checkmark.gif"></td>';
3215                                 } else {
3216                                         $att_table .= '<td>&nbsp;</td>';
3217                                 }
3218                         }
3219                         $this->t->set_var('att_table',$att_table);
3220                         $this->t->fp('list3','individual_list',True);
3221                 }
3222                 $this->t->set_var('total_width',$total_width);
3223                 $this->t->set_var('individual_width',$individual_width);
3224                 $this->t->set_var('att_width',$att_width);
3225
3226                 # Now calculate attendance for these months
3227                 $attendance_str = "";
3228                 $nonattendance_str = "";
3229                 $aveattendance_str = "";
3230                 $avenonattendance_str = "";
3231                 $num_months=0;
3232                 $ave_total_attended=0;
3233                 ksort($attendance);
3234                 foreach($attendance as $att => $value) {
3235                         $total_attended = $attendance[$att];
3236                         $ave_total_attended += $attendance[$att]; $num_months++;
3237                         $percent = ceil(($total_attended / $total_individuals)*100);
3238                         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3239                         $total_nonattended = $total_individuals - $total_attended;
3240                         $percent = ceil(($total_nonattended / $total_individuals)*100);
3241                         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3242
3243                         $total_attended = ceil(($ave_total_attended / $num_months));
3244                         $percent = ceil(($total_attended / $total_individuals)*100);
3245                         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3246                         $total_attended = $total_individuals - ceil(($ave_total_attended / $num_months));
3247                         $percent = ceil(($total_attended / $total_individuals)*100);
3248                         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3249                 }
3250
3251                 $this->t->set_var('attendance',$attendance_str);
3252                 $this->t->set_var('aveattendance',$aveattendance_str);
3253                 $this->t->set_var('nonattendance',$nonattendance_str);
3254                 $this->t->set_var('avenonattendance',$avenonattendance_str);
3255
3256                 $this->t->pfp('out','att_view_t');
3257                 $this->save_sessiondata(); 
3258         }
3259
3260         function att_update()
3261         {
3262                 $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3263                 $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3264                 $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3265
3266                 $this->t->set_file(array('form' => 'att_update.tpl'));
3267                 $this->t->set_block('form','edit','edithandle');
3268
3269                 $this->t->set_block('form','month_list','list1');
3270                 $this->t->set_block('form','header_list','list2');
3271                 $this->t->set_block('form','individual_list','list3');
3272
3273                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_view'));
3274
3275                 $action = get_var('action',array('GET','POST'));
3276                 $month = get_var('month',array('GET','POST'));
3277                 $year = get_var('year',array('GET','POST'));
3278                 $day = get_var('day',array('GET','POST'));
3279                 $date = get_var('date',array('GET','POST'));
3280
3281                 if($action == 'save_month' || $action == 'save_day') {
3282                         $new_data = get_var('individuals_attended',array('POST'));
3283                         $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3284
3285                         if($action == 'save_month') {
3286                                 $this->db->query("DELETE from tc_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3287                         }
3288
3289                         if($action == 'save_day') {
3290                                 $this->db->query("DELETE from tc_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3291                         }   
3292
3293                         foreach ($new_data as $data) {
3294                                 $data_array = explode("-",$data);
3295                                 $indiv = $data_array[0];
3296                                 $date  = "$data_array[1]-$data_array[2]-$data_array[3]";              
3297                                 $this->db->query("INSERT INTO tc_attendance (individual,date) " .
3298                                                  "VALUES (" . $indiv . ",'". $date . "')",__LINE__,__FILE__);
3299                         }
3300
3301                         $this->att_view();
3302                         return false;
3303                 }
3304
3305                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1";
3306                 $this->db->query($sql,__LINE__,__FILE__);
3307                 $i=0;
3308                 while ($this->db->next_record()) {
3309                         $indiv_name[$i] = $this->db->f('name');
3310                         $individual[$i] = $this->db->f('individual');
3311                         $indiv_attending[$individual[$i]] = $this->db->f('attending');
3312                         $i++;
3313                 }
3314                 array_multisort($indiv_name, $individual);
3315
3316                 if($action == 'update_month') {
3317                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_update&action=save_month'));
3318                         $i=0; 
3319                         $last_time = 0; 
3320                         $found_sunday = 0;
3321                         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3322                         $last_date = explode("-",$sunday_list[0]['date']);
3323                         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3324                         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3325                         while($last_time <= $time_limit) {
3326                                 $day = date("w",$last_time);
3327                                         if(date("w",$last_time) == 0) { 
3328                                         $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3329                                         $last_date = explode("-",$sunday_list[$i]['date']);
3330                                         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3331                                         $sunday_list[$i]['day'] = $last_date[2];
3332                                         $sunday_list[$i]['month'] = date("M",$last_time);
3333                                         $sunday_list[$i]['year'] = $last_date[0];
3334                                         $found_sunday = 1; 
3335                                 }
3336                                 $last_time += 90000;
3337                                 if($found_sunday) { $i++; $found_sunday=0; }
3338                         }
3339
3340                         $this->t->set_var('span', $i);
3341                         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3342                         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3343                         $this->t->fp('list1','month_list',True);
3344                         $indiv_width=200; $att_width=25; $total_width=$indiv_width;
3345                         for ($i=0; $i < count($sunday_list); $i++) {
3346                                 $link_data['menuaction'] = 'tc.tc.att_update';
3347                                 $link_data['month'] = $sunday_list[$i]['month'];
3348                                 $link_data['year'] = $sunday_list[$i]['year'];
3349                                 $link_data['day'] = $sunday_list[$i]['day'];
3350                                 $link_data['date'] = $sunday_list[$i]['date'];
3351                                 $link_data['action'] = 'update_day';
3352                                 $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3353                                 $this->t->set_var('date',$sunday_list[$i]['date']);
3354                                 $this->t->set_var('day',$sunday_list[$i]['day']);
3355                                 $this->t->set_var('month',$sunday_list[$i]['month']);
3356                                 $this->t->set_var('year',$sunday_list[$i]['year']);
3357                                 $this->t->fp('list2','header_list',True);
3358                                 $total_width += $att_width;
3359                         }     
3360                 }
3361
3362                 if($action == 'update_day') {
3363                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_update&action=save_day'));
3364                         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3365                         $this->t->set_var('month',$month);
3366                         $this->t->set_var('year',$year);
3367                         $this->t->fp('list1','month_list',True);
3368                         $this->t->set_var('date',$date);
3369                         $this->t->set_var('day',$day);
3370                         $this->t->set_var('month',$month);
3371                         $this->t->set_var('year',$year);
3372                         $this->t->fp('list2','header_list',True);
3373                 }           
3374
3375                 for ($i=0; $i < count($individual); $i++) {
3376                         $att_table = "";
3377                         $this->nextmatchs->template_alternate_row_color(&$this->t);
3378                         $this->t->set_var('individual_name',$indiv_name[$i]);
3379                         for ($j=0; $j < count($sunday_list); $j++) {
3380                                 $sql = "SELECT * FROM tc_attendance WHERE date='" .
3381                                        $sunday_list[$j]['date'] . "' AND individual=" . $individual[$i];
3382                                 $this->db->query($sql,__LINE__,__FILE__);
3383                                 $value = $individual[$i] . "-" . $sunday_list[$j]['date'];
3384                                 if($this->db->next_record()) {
3385                                         $att_table .= '<td align=center><input type="checkbox" name="individuals_attended[]" value="'.$value.'" checked></td>';
3386                                 } else if($indiv_attending[$individual[$i]] == 1) {
3387                                         $att_table .= '<td align=center><input type="checkbox" name="individuals_attended[]" value="'.$value.'" checked></td>';
3388                                 } else {
3389                                         $att_table .= '<td align=center><input type="checkbox" name="individuals_attended[]" value="'.$value.'"></td>';
3390                                 }
3391                         }
3392                         $this->t->set_var('att_table',$att_table);
3393                         $this->t->fp('list3','individual_list',True);
3394                 } 
3395
3396                 $this->t->set_var('lang_done', 'Cancel');
3397                 $this->t->set_var('lang_reset','Clear Form');
3398                 $this->t->set_var('lang_save','Save Changes');
3399
3400                 $this->t->pfp('out','form');
3401                 $this->t->pfp('addhandle','edit');
3402
3403                 $this->save_sessiondata();       
3404         }
3405
3406         function dir_view()
3407         {
3408                 $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3409                 $this->t->set_block('dir_view_t','dir_list','list');
3410
3411                 $sql = "SELECT * FROM tc_individual where valid=1 and hh_position='Head of Household' ORDER BY name ASC";
3412                 $this->db->query($sql,__LINE__,__FILE__);
3413                 $i=0;
3414                 while ($this->db->next_record()) {
3415                         $parent[$i]['id'] = $this->db->f('individual');
3416                         $parent[$i]['name'] = $this->db->f('name');
3417                         $parent[$i]['phone'] = $this->db->f('phone');
3418                         $parent[$i]['address'] = $this->db->f('address');
3419                         $i++;
3420                 }   
3421
3422                 for ($i=0; $i < count($parent); $i++) {
3423                         $name = $parent[$i]['name'];
3424                         $phone = $parent[$i]['phone'];
3425                         $address = $parent[$i]['address'];
3426                         $this->t->set_var('name', $name);
3427                         $this->t->set_var('address', $address);
3428                         $this->t->set_var('phone', $phone);
3429                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3430                         $this->t->set_var('tr_color',$tr_color);
3431                         $this->t->fp('list','dir_list',True);
3432                         //print "$phone $name $address<br>";
3433                 }
3434                 $this->t->pfp('out','dir_view_t');
3435                 $this->save_sessiondata();   
3436         }
3437   
3438         function org_view()
3439         {
3440                 $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3441                 $this->t->set_block('org_view_t','calling_list','list1');
3442                 $this->t->set_block('org_view_t','org_list','list2');
3443
3444                 # Display a list ordered alphabetically
3445                 $sql = "SELECT * FROM tc_calling AS tc JOIN tc_individual AS ti WHERE tc.individual=ti.individual ORDER BY name ASC";
3446                 $this->db->query($sql,__LINE__,__FILE__);
3447                 $i=0;
3448                 while ($this->db->next_record()) {
3449                         $calling[$i]['name'] = $this->db->f('name');
3450                         $calling[$i]['position'] = $this->db->f('position');
3451                         $calling[$i]['sustained'] = $this->db->f('sustained');
3452                         $calling[$i]['organization'] = $this->db->f('organization');
3453                         $i++;
3454                 }   
3455                 for ($i=0; $i < count($calling); $i++) {
3456                         $name = $calling[$i]['name'];
3457                         $position = $calling[$i]['position'];
3458                         $sustained = $calling[$i]['sustained'];
3459                         $organization = $calling[$i]['organization'];
3460                         $this->t->set_var('name', $name);
3461                         $this->t->set_var('position', $position);
3462                         $this->t->set_var('sustained', $sustained);
3463                         $this->t->set_var('organization', $organization);
3464                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3465                         $this->t->set_var('tr_color',$tr_color);
3466                         $this->t->fp('list1','calling_list',True);
3467                 }
3468
3469                 # Display a list ordered by organization
3470                 $sql = "SELECT * FROM tc_calling AS tc JOIN tc_individual AS ti where tc.individual=ti.individual ORDER BY organization ASC";
3471                 $this->db->query($sql,__LINE__,__FILE__);
3472                 $i=0;
3473                 while ($this->db->next_record()) {
3474                         $calling[$i]['name'] = $this->db->f('name');
3475                         $calling[$i]['position'] = $this->db->f('position');
3476                         $calling[$i]['sustained'] = $this->db->f('sustained');
3477                         $calling[$i]['organization'] = $this->db->f('organization');
3478                         $i++;
3479                 }   
3480                 for ($i=0; $i < count($calling); $i++) {
3481                         $name = $calling[$i]['name'];
3482                         $position = $calling[$i]['position'];
3483                         $sustained = $calling[$i]['sustained'];
3484                         $organization = $calling[$i]['organization'];
3485                         $this->t->set_var('name', $name);
3486                         $this->t->set_var('position', $position);
3487                         $this->t->set_var('sustained', $sustained);
3488                         $this->t->set_var('organization', $organization);
3489                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3490                         $this->t->set_var('tr_color',$tr_color);
3491                         $this->t->fp('list2','org_list',True);
3492                 }
3493
3494                 $this->t->pfp('out','org_view_t');
3495                 $this->save_sessiondata();   
3496         }
3497   
3498         function schedule()
3499         {
3500                 $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3501                 $this->t->set_block('sched_t','presidency_list','list');
3502
3503                 $action = get_var('action',array('GET','POST'));
3504
3505                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.schedule&action=save'));
3506                 $this->t->set_var('title','Scheduling Tool');
3507
3508                 $this->t->set_var('lang_save','Save Schedule');
3509                 $this->t->set_var('lang_reset','Cancel');
3510
3511                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
3512                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3513
3514                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
3515                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3516
3517                 $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched'));
3518                 $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
3519
3520                 $date_width=160; $time_width=220; $indiv_width=170; $family_width=180; $location_width=100;
3521                 $table_width=$date_width + $time_width + $indiv_width + $family_width + $location_width;
3522                 $header_row = "<th width=$date_width><font size=-2>Date</th>";
3523                 $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3524                 $header_row.= "<th width=$indiv_width><font size=-2>Individual</th>";
3525                 $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3526                 $header_row.= "<th width=$location_width><font size=-2>Location</th>";
3527                 $table_data = "";
3528
3529                 $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";
3530                 $this->db->query($sql,__LINE__,__FILE__);
3531                 $i=0;
3532                 while ($this->db->next_record()) {
3533                         $presidency_data[$i]['id'] = $this->db->f('presidency');
3534                         $presidency_data[$i]['name'] = $this->db->f('name');
3535                         $presidency_data[$i]['indiv'] = $this->db->f('individual');
3536                         $presidency2name[$presidency_data[$i]['id']] = $presidency_data[$i]['name'];
3537                         $presidency2indiv[$presidency_data[$i]['id']] = $presidency_data[$i]['indiv'];
3538                         $i++;
3539                 }
3540
3541                 $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";
3542                 $this->db->query($sql,__LINE__,__FILE__);
3543                 $i=0;
3544                 while ($this->db->next_record()) {
3545                         $family_id[$i] = $this->db->f('family');
3546                         $family_name[$i] = $this->db->f('name');
3547                         $familyid2name[$family_id[$i]] = $family_name[$i];
3548                         $familyid2address[$family_id[$i]] = $this->db->f('address');
3549                         $i++;
3550                 }
3551                 array_multisort($family_name, $family_id);
3552
3553                 if($action == 'save') {
3554                         $new_data = get_var('sched',array('POST'));
3555                         foreach ($new_data as $presidency_array) {
3556                                 foreach ($presidency_array as $entry) {
3557                                         $presidency = $entry['presidency'];
3558                                         $appointment = $entry['appointment'];
3559                                         $location = $entry['location'];
3560                                         $date = $entry['date'];
3561                                         $hour = $entry['hour'];
3562                                         $minute = $entry['minute'];
3563                                         $pm = $entry['pm'];
3564                                         $indiv = $entry['individual'];
3565                                         $family = $entry['family'];
3566                                         $location = $entry['location'];
3567                                         if($pm) { $hour = $hour + 12; }
3568                                         $time = $hour.':'.$minute.':'.'00';
3569                                         $uid = 0;
3570
3571                                         // Zero out family or individual if they are invalid
3572                                         if($indiv == "") { $indiv=0; }
3573                                         if($family == "") { $family=0; }
3574
3575                                         // Update our location
3576                                         if($location == "") {
3577                                                 if($family > 0) {
3578                                                         $family_name_array = explode(",", $familyid2name[$family]);
3579                                                         $family_last_name = $family_name_array[0];
3580                                                         $family_address = $familyid2address[$family];
3581                                                         $location = "$family_last_name"." home ($family_address)";
3582                                                 } else if($indiv > 0) {
3583                                                         $supervisor_name_array = explode(",",$presidency2name[$presidency]);
3584                                                         $supervisor_last_name = $supervisor_name_array[0];
3585                                                         #print "presidency2indiv: $presidency $presidency2indiv[$presidency]<br>";
3586                                                         $sql = "SELECT * FROM tc_individual where individual='$presidency2indiv[$presidency]'";
3587                                                         $this->db2->query($sql,__LINE__,__FILE__);
3588                                                         if($this->db2->next_record()) {
3589                                                                 $supervisor_address = $this->db2->f('address');
3590                                                         }
3591                                                         $location = "$supervisor_last_name"." home ($supervisor_address)";
3592                                                 }
3593                                         }
3594
3595                                         // Zero out the family or individual if date = NULL
3596                                         if($date == "") {
3597                                                 $indiv = 0;
3598                                                 $family = 0;
3599                                                 $location = "";
3600                                         }
3601
3602                                         if(($indiv == 0) && ($family == 0)) { $location = ""; }
3603
3604                                         // Update an existing appointment
3605                                         if($appointment < $this->max_appointments)
3606                                         {
3607                                                 //Only perform a database update if we have made a change to this appointment
3608                                                 $sql = "SELECT * FROM tc_appointment where " .
3609                                                        "appointment='$appointment'" .
3610                                                        " and presidency='$presidency'" .
3611                                                        " and individual='$indiv'" .
3612                                                        " and family='$family'" .
3613                                                        " and date='$date'" .
3614                                                        " and time='$time'" .
3615                                                        " and location='$location'";
3616                                                 $this->db->query($sql,__LINE__,__FILE__);
3617                                                 if(!$this->db->next_record()) {
3618                                                         $old_date = $this->db->f('date');
3619                                                         $old_time = $this->db->f('time');
3620                                                         $this->db2->query("UPDATE tc_appointment set" .
3621                                                                           " family=" . $family . 
3622                                                                           " ,individual=" . $indiv . 
3623                                                                           " ,date='" . $date . "'" .
3624                                                                           " ,time='" . $time . "'" .
3625                                                                           " ,location='" . $location . "'" .
3626                                                                           " ,presidency='" . $presidency . "'" .
3627                                                                           " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3628
3629                                                         // Email the appointment
3630                                                         $this->email_appt($appointment);
3631                                                 }
3632                                         }
3633
3634                                         // Add a new appointment
3635                                         else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3636                                         {
3637                                                 //print "adding entry: appt=$appointment date: $date time: $time individual: $indiv family: $family<br>";
3638                                                 $this->db2->query("INSERT INTO tc_appointment (appointment,presidency,family,individual,date,time,location,uid) " .
3639                                                                   "VALUES (NULL,'" . $presidency . "','" . $family . "','" . $indiv . "','" .
3640                                                                   $date . "','" . $time  . "','" . $location . "','" . $uid ."')",__LINE__,__FILE__);
3641
3642                                                 // Now reselect this entry from the database to see if we need
3643                                                 // to send an appointment out for it.
3644                                                 $sql = "SELECT * FROM tc_appointment where " .
3645                                                        "individual='$indiv'" .
3646                                                        " and family='$family'" .
3647                                                        " and presidency='$presidency'" .
3648                                                        " and date='$date'" .
3649                                                        " and time='$time'" .
3650                                                        " and uid='$uid'" .
3651                                                        " and location='$location'";
3652                                                 $this->db3->query($sql,__LINE__,__FILE__);
3653                                                 if($this->db3->next_record()) {
3654                                                         // Email the appointment if warranted
3655                                                         if(($date != "") && ($time != "") && (($indiv > 0) || $family > 0)) { 
3656                                                                 $this->email_appt($this->db3->f('appointment'));
3657                                                         }
3658                                                 }
3659                                         }
3660                                 }
3661                         }
3662
3663                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.schedule');
3664                         //Header('Location: ' . $take_me_to_url);
3665                 }
3666
3667                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY individual ASC";
3668                 $this->db->query($sql,__LINE__,__FILE__);
3669                 $i=0;
3670                 while ($this->db->next_record()) {
3671                         $individual[$i] = $this->db->f('individual');
3672                         $indiv_name[$i] = $this->db->f('name');
3673                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
3674                         $i++;
3675                 }
3676                 array_multisort($indiv_name, $individual);
3677
3678                 for ($i=0; $i < count($presidency_data); $i++) {
3679                         $presidency = $presidency_data[$i]['id'];
3680                         $interviewer = $presidency_data[$i]['individual'];
3681                         $name = $presidency_data[$i]['name'];
3682                         $this->t->set_var('presidency_name',$name);
3683                         $table_data="";
3684
3685                         // query the database for all the appointments
3686                         $sql = "SELECT * FROM tc_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
3687                         $this->db->query($sql,__LINE__,__FILE__);
3688
3689                         // Prefill any existing appointment slots
3690                         while ($this->db->next_record()) {
3691                                 $appointment = $this->db->f('appointment');
3692                                 $indiv = $this->db->f('individual');
3693                                 $family = $this->db->f('family');
3694                                 $location = $this->db->f('location');
3695
3696                                 if($location == "") {
3697                                         if($family > 0) {
3698                                                 $family_name_array = explode(",", $familyid2name[$family]);
3699                                                 $family_last_name = $family_name_array[0];
3700                                                 $family_address = $familyid2address[$family];
3701                                                 $location = "$family_last_name"." home ($family_address)";
3702                                         } else if($indiv > 0) {
3703                                                 $supervisor_name_array = explode(",",$presidency2name[$presidency]);
3704                                                 $supervisor_last_name = $supervisor_name_array[0];
3705                                                 $sql = "SELECT * FROM tc_individual where individual='$presidency2indiv[$presidency]'";
3706                                                 $this->db2->query($sql,__LINE__,__FILE__);
3707                                                 if($this->db2->next_record()) {
3708                                                         $supervisor_address = $this->db2->f('address');
3709                                                 }
3710                                                 $location = "$supervisor_last_name"." home ($supervisor_address)";
3711                                         }
3712                                 }
3713
3714                                 $date = $this->db->f('date');
3715                                 $date_array = explode("-",$date);
3716                                 $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3717                                 $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3718
3719                                 $time = $this->db->f('time');
3720                                 $time_array = explode(":",$time);
3721                                 $hour = $time_array[0];
3722                                 $minute = $time_array[1];
3723                                 $pm = 0;
3724                                 if($hour > 12) { $pm=1; $hour = $hour - 12; }
3725                                 $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3726
3727                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3728
3729                                 // Date selection
3730                                 $table_data.= '<td align=left>';
3731                                 $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3732                                 $table_data.= '</td>';
3733
3734                                 // Hour & Minutes selection
3735                                 $table_data.= "<td align=center>";
3736                                 $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
3737                                 $table_data.= "</td>";
3738
3739                                 // individual drop down list (for PPIs)
3740                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][individual] STYLE="font-size : 8pt">';
3741                                 $table_data.= '<option value=0></option>';  
3742                                 for ($j=0; $j < count($individual); $j++) {
3743                                         $id = $individual[$j];
3744                                         $name = $indiv_name[$j];
3745                                         if($individual[$j] == $indiv) {
3746                                                 $selected[$id] = 'selected="selected"'; 
3747                                         } else {
3748                                                 $selected[$id] = ''; 
3749                                         }
3750                                         $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3751                                 }
3752                                 $table_data.='</select></td>';
3753
3754                                 // Family drop down list (for Visits)
3755                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family] STYLE="font-size : 8pt">';
3756                                 $table_data.= '<option value=0></option>';          
3757                                 for ($j=0; $j < count($individual); $j++) {
3758                                         $id = $family_id[$j];
3759                                         $name = $family_name[$j];
3760                                         if($family_id[$j] == $family) { 
3761                                                 $selected[$id] = 'selected="selected"'; 
3762                                         } else { 
3763                                                 $selected[$id] = ''; 
3764                                         }
3765                                         $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3766                                 }
3767                                 $table_data.='</select></td>';
3768
3769                                 // Location text box
3770                                 $table_data.= '<td align=center><input type=text size="25" maxlength="120" ';
3771                                 $table_data.= 'name="sched['.$presidency.']['.$appointment.'][location]" value="'.$location.'" STYLE="font-size : 8pt">';
3772
3773                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3774                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3775
3776                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3777                                 $this->t->set_var('tr_color',$tr_color);
3778                         }
3779
3780                         // Create blank appointment slot
3781                         for ($b=0; $b < 4; $b++) {
3782                                 $appointment = $this->max_appointments + $b;
3783                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3784
3785                                 // Date selection
3786                                 $table_data.= '<td align=left>';
3787                                 $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3788                                 $table_data.= '</td>';
3789
3790                                 // Time selection
3791                                 $table_data.= "<td align=center>";
3792                                 $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
3793                                 $table_data.= "</td>";
3794
3795                                 // individual drop down list
3796                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][individual] STYLE="font-size : 8pt">';
3797                                 $table_data.= '<option value=0></option>';  
3798                                 for ($j=0; $j < count($individual); $j++) {
3799                                         $id = $individual[$j];
3800                                         $name = $indiv_name[$j];
3801                                         $table_data.= '<option value='.$id.'>'.$name.'</option>';
3802                                 }
3803                                 $table_data.='</select></td>';
3804
3805                                 // Family drop down list
3806                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family] STYLE="font-size : 8pt">';
3807                                 $table_data.= '<option value=0></option>';          
3808                                 for ($j=0; $j < count($individual); $j++) {
3809                                         $id = $family_id[$j];
3810                                         $name = $family_name[$j];
3811                                         $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3812                                 }
3813                                 $table_data.='</select></td>';
3814
3815                                 // Location text box
3816                                 $table_data.= '<td align=center><input type=text size="25" maxlength="120" ';
3817                                 $table_data.= 'name="sched['.$presidency.']['.$appointment.'][location]" value="" STYLE="font-size : 8pt">';
3818
3819                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3820                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3821
3822                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3823                                 $this->t->set_var('tr_color',$tr_color);
3824                         }
3825
3826                         $this->t->set_var('table_data',$table_data);
3827                         $this->t->set_var('header_row',$header_row);
3828                         $this->t->set_var('table_width',$table_width);
3829                         $this->t->fp('list','presidency_list',True);
3830                 }
3831
3832                 $this->t->pfp('out','sched_t');
3833                 $this->save_sessiondata();   
3834         }
3835
3836         function email()
3837         {
3838                 $this->t->set_file(array('email_t' => 'email.tpl'));
3839                 $this->t->set_block('email_t','individual_list','list');
3840
3841                 $action = get_var('action',array('GET','POST'));
3842
3843                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email'));
3844                 $this->t->set_var('title','Email Tool');
3845
3846                 $this->t->set_var('lang_email','Send Email');
3847                 $this->t->set_var('lang_reset','Cancel');
3848
3849                 $this->t->set_var('email_member_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=member'));
3850                 $this->t->set_var('email_member_link_title','Email Quorum Member');
3851
3852                 $this->t->set_var('email_quorum_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=quorum'));
3853                 $this->t->set_var('email_quorum_link_title','Email Quorum');
3854
3855                 $this->t->set_var('email_reminder_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=reminder'));
3856                 $this->t->set_var('email_reminder_link_title','Email Reminders');
3857
3858                 $this->t->set_var('email_edit_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=edit'));
3859                 $this->t->set_var('email_edit_link_title','Edit Email Addresses');
3860
3861                 $table_width=600;
3862                 $this->t->set_var('table_width',$table_width);
3863
3864                 $this->t->pfp('out','email_t');
3865                 $this->save_sessiondata();   
3866         }
3867
3868         function admin()
3869         {
3870                 $this->t->set_file(array('admin_t' => 'admin.tpl'));
3871                 $this->t->set_block('admin_t','upload','uploadhandle');
3872                 $this->t->set_block('admin_t','admin','adminhandle');
3873                 $this->t->set_block('admin_t','cmd','cmdhandle');
3874                 $this->t->set_block('admin_t','presidency','presidencyhandle');
3875
3876                 $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.admin&action=upload'));
3877                 $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.admin&action=presidency'));
3878
3879                 $action = get_var('action',array('GET','POST'));
3880
3881                 $this->t->pfp('out','admin_t');
3882
3883                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY individual ASC";
3884                 $this->db->query($sql,__LINE__,__FILE__);
3885                 $i=0;
3886                 while ($this->db->next_record()) {
3887                         $individual[$i] = $this->db->f('individual');
3888                         $indiv_name[$i] = $this->db->f('name');
3889                         $indiv2name[$individual[$i]] = $indiv_name[$i];
3890                         $i++;
3891                 }
3892                 array_multisort($indiv_name, $individual);
3893
3894                 if($action == 'upload') {
3895                         $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
3896
3897                         if(($_FILES['uploadedfile']['type'] == "application/zip") ||
3898                            ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3899                            ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3900                            ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
3901
3902                                 if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
3903                                         $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
3904                                         $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
3905                                         $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3906                                         $uploadstatus.= "Tmp Filename : " . $_FILES['uploadedfile']['tmp_name'] . "<br>";
3907                                         $uploadstatus.= "Filename     : " . $_FILES['uploadedfile']['name'] . "<br>";
3908                                         $uploadstatus.= "Type         : " . $_FILES['uploadedfile']['type'] . "<br>";
3909                                         $uploadstatus.= "Size         : " . $_FILES['uploadedfile']['size'] . "<br>";
3910                                         $uploadstatus.= "Error        : " . $_FILES['uploadedfile']['error'] . "<br>";   
3911                                         $this->t->set_var('uploadstatus',$uploadstatus);
3912                                         $this->t->pfp('uploadhandle','upload',True);
3913                                         return 0;
3914                                 }
3915
3916                                 $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3917                                 $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3918                                 $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3919                                 $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";        
3920                                 $this->t->set_var('uploadstatus',$uploadstatus);
3921                                 $this->t->pfp('uploadhandle','upload');
3922                                 $this->t->set_var('uploadhandle','');
3923                                 print "<table border=1 width=80%><tr><td>\n<pre>";
3924
3925                                 # make a directory for this data to be stored in
3926                                 $date="data_" . date("Y_m_d");
3927                                 $data_dir = $this->upload_target_path . '/' . $date;
3928                                 print "-> Making the data directory: $date<br>\n";
3929                                 exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
3930                                 if($return_code != 0) {
3931                                         print implode('\n',$result) . "<br>";
3932                                         print "<b><font color=red>";
3933                                         print "-E- Unable to create the data directory. Aborting import.";
3934                                         print "</font></b>";
3935                                         return 0;
3936                                 }
3937
3938                                 # move the file uploaded into this directory
3939                                 print "-> Moving the uploaded file into the data dir<br>\n";
3940                                 exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3941                                 if($return_code != 0) {
3942                                         print implode('\n',$result) . "<br>";
3943                                         print "<b><font color=red>";
3944                                         print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3945                                         print "</font></b>";
3946                                         return 0;
3947                                 }
3948
3949                                 # unzip the data into this directory
3950                                 print "-> Unzipping the data<br>\n";
3951                                 exec($this->unzip_path .' -u '. $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3952                                 if($return_code != 0) {
3953                                         print implode('\n',$result) . "<br>";
3954                                         print "<b><font color=red>";
3955                                         print "-E- Unable to unzip the uploaded file into the data dir: $data_dir. Aborting import.";
3956                                         print "</font></b>";
3957                                         return 0;
3958                                 }
3959                                 exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3960
3961                                 # update the data_latest link to point to this new directory
3962                                 print "-> Updating the latest data dir link<br>\n";
3963                                 $data_latest = $this->upload_target_path . '/data_latest';
3964                                 exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3965                                 if($return_code != 0) {
3966                                         print implode('\n',$result) . "<br>";
3967                                         print "<b><font color=red>";
3968                                         print "-E- Unable to update the data latest link. Aborting import.";
3969                                         print "</font></b>";
3970                                         return 0;
3971                                 }
3972
3973                                 # run the import perl script to encorporate it into the DB
3974                                 ob_start('ob_logstdout', 2);
3975                                 print "-> Importing the data into the database<br>\n";
3976                                 ob_flush(); flush(); sleep(1);
3977                                 $import_log = $this->upload_target_path . '/import.log';
3978                                 $data_log = $this->upload_target_path . '/data.log';
3979                                 $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
3980                                 $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
3981                                 #print "import_cmd: $import_cmd<br>";
3982                                 #print "parse_cmd: $parse_cmd<br>";
3983                                 ob_start('ob_logstdout', 2);
3984                                 passthru($import_cmd);
3985                                 passthru($parse_cmd);
3986                                 ob_flush(); flush(); sleep(1);
3987
3988                                 # fix the permissions of the data dir
3989                                 exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3990
3991                                 $this->t->pfp('cmdhandle','cmd');
3992                                 print "</pre></td></tr></table>";
3993                         } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3994                                   ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
3995                                   ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
3996                                   ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
3997                                 $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
3998                                 $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
3999                                 $this->t->set_var('uploadstatus',$uploadstatus);
4000                                 $this->t->pfp('uploadhandle','upload',True);
4001                         } else {
4002                                 $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
4003                                 $uploadstatus.= ") uploading the file, please try again! </font></b>";
4004                                 $this->t->set_var('uploadstatus',$uploadstatus);
4005                                 $this->t->pfp('uploadhandle','upload',True);
4006                         }
4007                 } else if($action == "presidency") {
4008                         $new_data = get_var('eqpres',array('POST'));
4009                         foreach ($new_data as $entry) {
4010                                 $id = $entry['id'];
4011                                 $email = $entry['email'];
4012                                 $indiv = $entry['indiv'];
4013                                 $name = $entry['name'];
4014                                 $district = $entry['district'];
4015                                 $president = $entry['president'];
4016                                 $counselor = $entry['counselor'];
4017                                 $secretary = $entry['secretary'];
4018                                 // look up the individual name for the ID
4019                                 $name = $indiv2name[$indiv]; 
4020                                 //print "id=$id indiv=$indiv name=$name email=$email district=$district president=$president ";
4021                                 //print "counselor=$counselor secretary=$secretary<br>";
4022
4023                                 if(($indiv > 0) || ($name != "")) {
4024                                         if($id < $this->max_presidency_members) {
4025                                                 //print "Updating Existing Entry<br>";
4026                                                 $this->db2->query("UPDATE tc_presidency set" .
4027                                                                   " individual=" . $indiv . 
4028                                                                   " ,district=" . $district . 
4029                                                                   " ,email='" . $email . "'" .
4030                                                                   " ,president='" . $president . "'" .
4031                                                                   " ,counselor='" . $counselor . "'" .
4032                                                                   " ,secretary='" . $secretary . "'" .
4033                                                                   " WHERE presidency=" . $id,__LINE__,__FILE__);
4034                                         } else {
4035                                                 //print "Adding New Entry<br>";
4036                                                 $this->db2->query("INSERT INTO tc_presidency (presidency,individual,district," .
4037                                                                   "email,president,counselor,secretary,valid) " .
4038                                                                   "VALUES (NULL,'" . $indiv . "','" . $district . "','" .
4039                                                                   $email . "','" . $president  . "','" .
4040                                                                   $counselor . "','" . $secretary . "','1'" .
4041                                                                   ")",__LINE__,__FILE__);
4042                                         }
4043                                 } else {
4044                                         //print "Ignoring Blank Entry<br>";
4045                                 }
4046                         }
4047
4048                         // Now update the tc_district table appropriately
4049
4050                         // Delete all the previous district entries from the table
4051                         $this->db->query("DELETE from tc_district where valid=1",__LINE__,__FILE__);
4052                         $this->db->query("DELETE from tc_district where valid=0",__LINE__,__FILE__);
4053
4054                         // Always add a "District 0" assigned to the High Priests Group
4055                         $district = 0;
4056                         $name = "High Priests";
4057                         $indiv = 0;
4058                         $valid = 0;
4059                         $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
4060                                           "VALUES ('" . $district . "','" . 
4061                                           $indiv . "','" . $valid . "'" .
4062                                           ")",__LINE__,__FILE__);
4063
4064                         // Requery the tc_presidency table
4065                         $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
4066                         $this->db->query($sql,__LINE__,__FILE__);
4067                         while ($this->db->next_record()) {
4068                                 // Extract the data for each presidency record
4069                                 $id = $this->db->f('presidency');
4070                                 $indiv = $this->db->f('individual');
4071                                 $name = $this->db->f('name');
4072                                 $district = $this->db->f('district');
4073                                 $valid = 1;
4074
4075                                 // If we have a valid district, add it to the district table
4076                                 if($district > 0) {
4077                                         $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
4078                                                           "VALUES ('" . $district . "','" . 
4079                                                           $indiv . "','" . $valid . "'" .
4080                                                           ")",__LINE__,__FILE__);
4081                                 }
4082                         }
4083
4084                         $this->t->set_var('adminhandle','');
4085                         $this->t->pfp('adminhandle','admin'); 
4086                 }
4087                 else
4088                 {
4089                         $this->t->set_var('adminhandle','');
4090                         $this->t->pfp('adminhandle','admin'); 
4091                 }
4092
4093                 // Now save off the data needed for a Presidency Table Update
4094
4095                 $sql = "SELECT tp.*, ti.name FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
4096                 $this->db->query($sql,__LINE__,__FILE__);
4097                 $table_data = "";
4098                 $header_row = "<th>Individual</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th>";
4099                 while ($this->db->next_record()) {
4100                         // Extract the data for each presidency record
4101                         $id = $this->db->f('presidency');
4102                         $indiv = $this->db->f('individual');
4103                         $district = $this->db->f('district');
4104                         $name = $this->db->f('name');
4105                         $email = $this->db->f('email');
4106                         $president = $this->db->f('president');
4107                         $counselor = $this->db->f('counselor');
4108                         $secretary = $this->db->f('secretary');
4109
4110                         // Create the forms needed in the table
4111                         $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4112
4113                         // Presidency ID
4114                         $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4115
4116                         // individual
4117                         if($eqpresidency == 0) {
4118                                 $table_data.= '<td align=center><select name="eqpres['.$id.'][indiv]">';
4119                                 $table_data.= '<option value=0></option>';  
4120                                 for ($j=0; $j < count($individual); $j++) {
4121                                         $tmp_id = $individual[$j];
4122                                         $name = $indiv_name[$j];
4123                                         if($individual[$j] == $indiv) { 
4124                                                 $indivname = $name; 
4125                                                 $selected = 'selected="selected"'; 
4126                                         } else { 
4127                                                 $selected = ''; 
4128                                         }
4129                                         $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4130                                 }
4131                                 $table_data.='</select></td>';
4132                                 $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$indivname.'">';
4133                         } else {
4134                                 $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="Presidency"></td>';
4135                                 $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="Presidency">';
4136                         }
4137
4138                         // Email Address
4139                         $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4140
4141                         // District
4142                         $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4143                         $table_data.= '<option value=0></option>';
4144                         for ($j=0; $j <= $this->max_num_districts; $j++) {
4145                                 if($district == $j) { 
4146                                         $selected = 'selected="selected"'; 
4147                                 } else { 
4148                                         $selected = ''; 
4149                                 }
4150                                 $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4151                         }
4152                         $table_data.='</select></td>';
4153
4154                         // President
4155                         $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4156                         if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4157                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4158                         $table_data.='</select></td>';
4159
4160                         // Counselor
4161                         $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4162                         if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4163                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4164                         $table_data.='</select></td>';
4165
4166                         // Secretary
4167                         $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4168                         if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4169                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4170                         $table_data.='</select></td>';
4171
4172                         // End of ROW
4173                         $table_data .= "</tr>\n";
4174                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4175                         $this->t->set_var('tr_color',$tr_color);
4176                 }
4177
4178                 // Now create 1 blank row to always have a line available to add a new individual with
4179                 $id = $this->max_presidency_members;
4180                 $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4181                 // Presidency ID
4182                 $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4183                 // individual
4184                 $table_data.= '<td align=center><select name="eqpres['.$id.'][indiv]">';
4185                 $table_data.= '<option value=0></option>';  
4186                 for ($j=0; $j < count($individual); $j++) {
4187                         $tmp_id = $individual[$j];
4188                         $name = $indiv_name[$j];
4189                         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4190                 }
4191                 $table_data.='</select></td>';
4192                 $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4193                 // Email Address
4194                 $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4195                 // District
4196                 $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4197                 $table_data.= '<option value=0></option>';
4198                 for ($j=0; $j <= $this->max_num_districts; $j++) {
4199                         if($j == 0) { 
4200                                 $selected = 'selected="selected"'; 
4201                         } else { 
4202                                 $selected = ''; 
4203                         }
4204                         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4205                 }
4206                 $table_data.='</select></td>';
4207                 // President
4208                 $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4209                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4210                 $table_data.='</select></td>';
4211                 // Counselor
4212                 $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4213                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4214                 $table_data.='</select></td>';
4215                 // Secretary
4216                 $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4217                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4218                 $table_data.='</select></td>';
4219                 // End of ROW
4220                 $table_data .= "</tr>\n";
4221                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4222                 $this->t->set_var('tr_color',$tr_color);
4223
4224                 $this->t->set_var('header_row',$header_row);
4225                 $this->t->set_var('table_data',$table_data);
4226                 $this->t->pfp('presidencyhandle','presidency',True);
4227
4228                 $this->save_sessiondata();   
4229         }
4230
4231         function email_appt($appointment)
4232         {
4233                 //print "Emailing notification of appointment: $appointment <br>";
4234
4235                 $sql = "SELECT * FROM tc_appointment where appointment='$appointment'";
4236                 $this->db->query($sql,__LINE__,__FILE__);
4237
4238                 while ($this->db->next_record()) {
4239                         $appointment = $this->db->f('appointment');
4240                         $presidency = $this->db->f('presidency');
4241                         $location = $this->db->f('location');
4242                         $interviewer = "";
4243                         $email = "";
4244                         $indiv = $this->db->f('individual');
4245                         $indiv_name = "";
4246                         $family = $this->db->f('family');
4247                         $family_name = "";
4248                         $appt_name = "";
4249                         $phone = "";
4250                         $uid = $this->db->f('uid');
4251
4252                         // Extract the year, month, day, hours, minutes, seconds from the appointment time
4253                         $appt_date = $this->db->f('date');
4254                         $date_array = explode("-",$appt_date);
4255                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4256                         $appt_time = $this->db->f('time');
4257                         $time_array = explode(":",$appt_time);
4258                         $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4259
4260                         // Format the appointment time into an iCal UTC equivalent
4261                         $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4262                         $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4263                         $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4264
4265                         $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'";
4266                         $this->db2->query($sql,__LINE__,__FILE__);
4267                         if($this->db2->next_record()) {
4268                                 if ($this->db2->f('email1') != "") {
4269                                         $email = $this->db2->f('email1');
4270                                 } else { 
4271                                         $email = $this->db2->f('email2');
4272                                 }
4273                                 $interviewer = $this->db2->f('name');
4274                         }
4275
4276                         // Set the email address of the interviewer
4277                         $from = $email;
4278
4279                         if($indiv > 0) {
4280                                 $sql = "SELECT * FROM tc_individual where individual='$indiv'";
4281                                 $this->db2->query($sql,__LINE__,__FILE__);
4282                                 if($this->db2->next_record()) {
4283                                         $indiv_name = $this->db2->f('name');
4284                                         $phone = $this->db2->f('phone');
4285                                         $appt_name = $indiv_name . " Interview";
4286                                         $duration = $this->default_ppi_appt_duration * 60;
4287                                 }
4288                         }
4289
4290                         if($family > 0) {
4291                                 $sql = "SELECT * FROM tc_family WHERE family='$family'";
4292                                 $this->db2->query($sql,__LINE__,__FILE__);
4293                                 if($this->db2->next_record()) {
4294                                         $individual = $this->db2->f('individual');
4295                                         $sql = "SELECT * FROM tc_individual where individual='$individual'";
4296                                         $this->db3->query($sql,__LINE__,__FILE__);
4297                                         if($this->db3->next_record()) {
4298                                                 $phone = $this->db3->f('phone');
4299                                                 $family_name = $this->db3->f('name');
4300                                                 $phone = $this->db3->f('phone');
4301                                         }
4302                                         $appt_name = $family_name . " Family Visit";
4303                                         $duration = $this->default_visit_appt_duration * 60;
4304                                 }
4305                         }
4306
4307                         $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4308                         $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4309                         $date = $dtstartstr . "-" . $dtendstr;
4310                         $description = "$appt_name : $phone";
4311
4312                         if(($uid == 0) && ($appt_name != "")) {
4313                                 // Create a new calendar item for this appointment, since this must be the first time we
4314                                 // are sending it out.
4315                                 print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4316                                 $uid = rand() . rand(); // Generate a random identifier for this appointment
4317                                 $subject = "Created: $appt_name";
4318
4319                                 $this->db->query("UPDATE tc_appointment set" .
4320                                                  " uid=" . $uid . 
4321                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4322
4323                                 $action = "PUBLISH";
4324                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4325                                                       $dtend, $date, $location, $appt_name, $description, $uid);
4326                         } else if(($uid != 0) && ($appt_name == "")) {
4327                                 // Remove the calendar item for this appointment since it has already been sent
4328                                 // and there is no name we have changed it to.
4329                                 print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4330                                 $subject = "Canceled: $appt_date $appt_time";
4331
4332                                 $this->db->query("UPDATE tc_appointment set" .
4333                                                  " uid=0" . 
4334                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4335
4336                                 $action = "CANCEL";
4337                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4338                                                       $dtend, $date, $location, $subject, $subject, $uid);
4339                         } else if($uid != 0) {
4340                                 // Update the existing appointment since we have changed it
4341                                 print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4342
4343                                 $subject = "Canceled: $appt_date $appt_time";
4344                                 $action = "CANCEL";
4345                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4346                                 $dtend, $date, $location, $subject, $subject, $uid);
4347
4348                                 $uid = rand() . rand(); // Generate a random identifier for this appointment
4349                                 $this->db->query("UPDATE tc_appointment set" .
4350                                                  " uid=" . $uid .
4351                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4352
4353                                 $subject = "Updated: $appt_name";
4354                                 $action = "PUBLISH";
4355                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4356                                                       $dtend, $date, $location, $appt_name, $description, $uid);
4357                         }
4358                 }
4359                 return true;
4360         }
4361
4362         function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4363         {
4364                 // Initialize our local variables
4365                 $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4366                 $message = "";
4367                 $headers = "";
4368
4369                 // Form the headers for the email message
4370                 $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4371                 $headers.="Mime-Version: 1.0\n";
4372                 $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4373                 $headers.="Content-Disposition: inline\n";
4374                 $headers.="Reply-To: $from\n";
4375                 $headers.="From: $from\n";
4376
4377                 // Print the plaintext version of the appointment
4378                 $message.="--$boundary\n";
4379                 $message.="Content-Type: text/plain; charset=us-ascii\n";
4380                 $message.="Content-Disposition: inline\n";
4381                 $message.="\n";
4382                 $message.="What: $description\n";
4383                 $message.="When: $date\n";
4384                 $message.="Where: $location\n";
4385                 $message.="\n";
4386
4387                 // Print the .ics attachment version of the appointment
4388                 $message.="--$boundary\n";
4389                 $message.="Content-Type: text/calendar; charset=us-ascii\n";
4390                 $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4391                 $message.="\n";
4392                 $message.="BEGIN:VCALENDAR" . "\n";
4393                 $message.="VERSION:2.0" . "\n";
4394                 $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4395                 $message.="METHOD:$action" . "\n";
4396                 $message.="BEGIN:VEVENT" . "\n";
4397                 $message.="ORGANIZER:MAILTO:$from". "\n";
4398                 $message.="DTSTAMP:$dtstamp" . "\n";
4399                 $message.="DTSTART:$dtstart" . "\n";
4400                 $message.="DTEND:$dtend" . "\n";
4401                 $message.="SUMMARY:$summary" . "\n";
4402                 $message.="DESCRIPTION:$description" . "\n";
4403                 $message.="LOCATION:$location" . "\n";
4404                 $message.="UID:$uid" ."\n";
4405                 $message.="TRANSP:OPAQUE" . "\n";
4406                 $message.="SEQUENCE:0" . "\n";
4407                 $message.="CLASS:PUBLIC" . "\n";
4408                 $message.="END:VEVENT" . "\n";
4409                 $message.="END:VCALENDAR" . "\n";
4410
4411                 // Complete the message
4412                 $message.="--$boundary\n";
4413
4414                 // Send the message
4415                 mail($to, $subject, $message, $headers);
4416         }
4417
4418         function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4419         {
4420                 $form_data = "";
4421                 $blank = 0;
4422
4423                 if($hour == 0) { $blank = 1; }
4424
4425                 if($this->time_drop_down_lists == 1) {
4426                         // Create drop down lists to get the time
4427                         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4428                         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4429                         foreach(range(1,12) as $num) {
4430                                 if($hour == $num) { 
4431                                         $selected = 'selected="selected"'; 
4432                                 } else { 
4433                                         $selected = ''; 
4434                                 }
4435                                 $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4436                         }
4437                         $form_data.= '</select>';
4438                         $form_data.= '&nbsp;:&nbsp;';
4439                         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4440                         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4441                         $num = 0;
4442                         while($num < 60) {
4443                                 if($num < 10) { $num = "0" . "$num"; }
4444                                 if($minute == $num) { 
4445                                         $selected = 'selected="selected"'; 
4446                                 } else { 
4447                                         $selected = ''; 
4448                                 }
4449                                 if($blank == 1) { $selected = ""; }
4450                                 $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4451                                 $num = $num + $this->time_drop_down_list_inc;
4452                         }
4453                         $form_data.= '</select>';
4454                 } else {
4455                         // Use free form text fields to get the time
4456                         if($blank == 1) { 
4457                                 $hour = ""; 
4458                                 $minute = ""; 
4459                                 $ampm = ""; 
4460                         }
4461                         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4462                         $form_data.= ':';
4463                         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4464                         $form_data.= '&nbsp;';
4465                 }
4466                 // Always use a drop-down select form for am/pm
4467                 $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4468                 if($blank == 0) {
4469                         if($pm == 0) { 
4470                                 $form_data.= '<option value=0 selected>am</option>'; 
4471                                 $form_data.= '<option value=1>pm</option>'; 
4472                         }
4473                         if($pm == 1) { 
4474                                 $form_data.= '<option value=0>am</option>'; 
4475                                 $form_data.= '<option value=1 selected>pm</option>'; 
4476                         }
4477                 } else {
4478                         $form_data.= '<option value=""></option>';
4479                         $form_data.= '<option value=0>am</option>';
4480                         $form_data.= '<option value=1>pm</option>';
4481                 }
4482                 $form_data.= '</select>';
4483
4484                 return $form_data;
4485         }
4486 }
4487
4488 ?>