added new tc_companion table, updated tc_companionship table, linked tc_calling table...
[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                 // Get the President
1367                 $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";
1368                 $this->db->query($sql,__LINE__,__FILE__);
1369                 if($this->db->next_record()) {
1370                         $president_name = $this->db->f('name');
1371                         $president_name_array = explode(",",$president_name);
1372                         $president_last_name = $president_name_array[0];
1373                         $president_id = $this->db->f('individual');
1374                         $presidency_id = $this->db->f('presidency');
1375                         $interviewer = $this->db->f('individual');
1376                         $district_number = '*';
1377                         $district_name = $president_name;
1378                         $president_address = $this->db->f('address');
1379                 } else {
1380                         print "<hr><font color=red><h3>-E- Unable to locate President in tc_presidency table</h3></font></hr>";
1381                         return;
1382                 }
1383
1384                 if($action == 'save') {
1385                         // Save any changes made to the appointment table
1386                         $new_data = get_var('appt_notes',array('POST'));
1387                         if($new_data != "") {
1388                                 foreach ($new_data as $entry) {
1389                                         $indiv = $entry['individual'];
1390                                         $appointment = $entry['appointment'];
1391                                         $location = $entry['location'];
1392                                         if($location == "") { $location = "$president_last_name"." home ($president_address)"; }
1393                                         if($indiv == 0) { $location = ""; }
1394
1395                                         //Only perform a database update if we have made a change to this appointment
1396                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and individual='$indiv' and location='$location'";
1397                                         $this->db->query($sql,__LINE__,__FILE__);
1398                                         if(!$this->db->next_record()) {
1399                                                 // Perform database save actions here
1400                                                 $this->db->query("UPDATE tc_appointment set " .
1401                                                                  " individual='" . $indiv . "'" .
1402                                                                  ",location='" . $location . "'" .
1403                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1404                                                 // Email the appointment
1405                                                 $this->email_appt($appointment);
1406                                         }
1407                                 }
1408                         }
1409
1410                         // Save any changes made to the ppi notes table
1411                         $new_data = get_var('notes',array('POST'));
1412                         foreach ($new_data as $entry) {
1413                                 $notes = $entry['notes'];
1414                                 $individual = $entry['individual'];
1415                                 $priority = $entry['pri'];
1416
1417                                 // Perform database save actions here
1418                                 $sql = "SELECT * FROM tc_individual WHERE individual='$individual'";
1419                                 $this->db->query($sql,__LINE__,__FILE__);
1420                                 if ($this->db->next_record()) {
1421                                         $scheduling_priority = $this->db->f('scheduling_priority');
1422                                         //$this->logToFile("ppi_sched", "UPDATE tc_scheduling_priority SET priority='$priority', notes=\"$notes\" WHERE scheduling_priority='$scheduling_priority'");
1423                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$priority', notes=\"$notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
1424                                 }
1425                         }
1426
1427                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched');
1428                         //Header('Location: ' . $take_me_to_url);
1429                 }
1430
1431                 // create the individual id -> individual name mapping
1432                 $sql = "SELECT * FROM tc_individual where valid=1 and steward='$this->default_stewardship' ORDER BY name ASC";
1433                 $this->db->query($sql,__LINE__,__FILE__);
1434                 $i=0;
1435                 $individual = NULL;
1436                 $indiv_name = NULL;
1437                 while ($this->db->next_record()) {
1438                         $indiv_name[$i] = $this->db->f('name');
1439                         $individual[$i] = $this->db->f('individual');
1440                         $i++;
1441                 }
1442                 array_multisort($indiv_name, $individual);
1443
1444                 // APPOINTMENT TABLE
1445                 $date_width=250; $time_width=100; $indiv_width=200; $location_width=100;
1446                 $appt_table_width=$date_width + $time_width + $indiv_width + $location_width;
1447                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1448                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1449                 $appt_header_row.= "<th width=$indiv_width><font size=-2>indiv</th>";
1450                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
1451                 $appt_table_data = ""; 
1452
1453                 $total_indivs=0; $indivs_with_yearly_ppi=0;
1454
1455                 // Display a scheduling table for the President
1456                 $table_data=""; $appt_table_data="";
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->db->query($sql,__LINE__,__FILE__);
1465
1466                 while ($this->db->next_record()) {
1467                         $appointment = $this->db->f('appointment');
1468                         $indiv = $this->db->f('individual');
1469                         $location = $this->db->f('location');
1470                         if(($location == "") && ($indiv > 0)) { $location = "$president_last_name"." home ($president_address)"; }
1471
1472                         $date = $this->db->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->db->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
1508                 $this->t->set_var('appt_table_data',$appt_table_data);
1509                 $this->t->set_var('appt_header_row',$appt_header_row);
1510                 $this->t->set_var('appt_table_width',$appt_table_width);
1511
1512                 // PPI SCHEDULING TABLE
1513                 $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";
1514                 $this->db->query($sql,__LINE__,__FILE__);
1515
1516                 $i=0; 
1517                 $individual = NULL;
1518                 while ($this->db->next_record()) {
1519                         $individual[$i] = $this->db->f('individual');
1520                         $indiv_name[$i] = $this->db->f('name');
1521                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
1522                         $indiv_priority[$individual[$i]] = $this->db->f('priority');
1523                         $indiv_notes[$individual[$i]] = $this->db->f('notes');
1524                         $i++;
1525                         $total_indivs++;
1526                 }
1527
1528                 $max = count($individual);
1529
1530                 for($i=0; $i < $max; $i++) {
1531                         $id = $individual[$i];
1532                         $name = $indiv_name[$i];
1533                         $phone = $indiv_phone[$id];
1534                         $priority = $indiv_priority[$id];
1535                         $notes = $indiv_notes[$id];
1536
1537                         // If this individual has had a yearly PPI this year, don't show him on the schedule list
1538                         $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
1539                         $sql = "SELECT * FROM tc_interview WHERE date > '$year_start' AND date < '$year_end' ".
1540                                "AND individual=" . $id . " AND interview_type='ppi'";
1541                         $this->db2->query($sql,__LINE__,__FILE__);
1542
1543                         if(!$this->db2->next_record()) {
1544                                 $sql = "SELECT * FROM tc_interview WHERE individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
1545                                 $this->db->query($sql,__LINE__,__FILE__);
1546                                 if($this->db->next_record()) { 
1547                                         $date = $this->db->f('date'); 
1548                                 } else { 
1549                                         $date = ""; 
1550                                 }
1551                                 $link_data['menuaction'] = 'tc.tc.ppi_update';
1552                                 $link_data['individual'] = $id;
1553                                 $link_data['name'] = $name;
1554                                 $link_data['interview'] = '';
1555                                 $link_data['interview_type'] = 1;
1556                                 $link_data['action'] = 'add';
1557                                 $link_data['interviewer'] = $interviewer;
1558                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
1559                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1560                                 $this->t->set_var('tr_color',$tr_color);
1561                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1562                                 $table_data.= "<td align=center>$phone</td>";
1563                                 //$table_data.= "<td align=center>$priority</td>";
1564                                 $table_data.= "<td align=center>";
1565                                 $table_data.= '<select name=notes['.$i.'][pri]>';
1566                                 foreach(range(0,6) as $num) {
1567                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
1568                                         if($priority == $num) { 
1569                                                 $selected[$num] = 'selected="selected"'; 
1570                                         } else { 
1571                                                 $selected[$num] = ''; 
1572                                         }
1573                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1574                                 }
1575                                 $table_data.= '</select></td>';
1576                                 $table_data.= "<td align=center>$date</td>";
1577                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="notes['.$i.'][notes]" value="'.$notes.'">';
1578                                 $table_data.= '<input type=hidden name="notes['.$i.'][individual]" value="'.$id.'">';
1579                                 $table_data.= '<input type=hidden name="notes['.$i.'][indiv_name]" value="'.$name.'">';
1580                                 $table_data.= '</td>';
1581                                 $table_data.= '</tr>';
1582                         } else {
1583                                 $link_data['menuaction'] = 'tc.tc.ppi_update';
1584                                 $link_data['interviewer'] = $this->db2->f('interviewer');
1585                                 $link_data['individual'] = $this->db2->f('individual');
1586                                 $link_data['name'] = $name;
1587                                 $link_data['interview'] = $this->db2->f('interview');
1588                                 $link_data['interview_type'] = $this->db2->f('interview_type');
1589                                 $link_data['action'] = 'view';
1590                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
1591                                 $indivs_with_yearly_ppi++;
1592                                 $date = $this->db2->f('date');
1593                                 $notes = $this->db2->f('notes');
1594                                 if(strlen($notes) > 40) { $notes = substr($notes,0,40) . "..."; }
1595                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1596                                 $this->t->set_var('tr_color2',$tr_color2);
1597                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1598                                 $completed_data.= "<td align=center>$phone</td>";
1599                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1600                                 $completed_data.= "<td align=left>$notes</td>";
1601                                 $completed_data.= '</tr>';
1602                         }
1603                 } // End for individuals Loop
1604
1605                 $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1606                 $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1607                 $completed_header_row = "<th width=$name_width><font size=-2>Individual Name</th>";
1608                 $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1609                 $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1610                 $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1611
1612                 $this->t->set_var('table_width',$table_width);
1613                 $this->t->set_var('header_row',$header_row);
1614                 $this->t->set_var('table_data',$table_data);
1615                 $this->t->set_var('completed_header_row',$completed_header_row);
1616                 $this->t->set_var('completed_table_width',$completed_table_width);
1617                 $this->t->set_var('completed',$completed_data);
1618                 $this->t->fp('indivlist','individual_list',True); 
1619
1620                 $indivs_width=300; $totals_width=100;
1621                 $totals_table_width=$indivs_width + $totals_width;
1622                 $totals_header_row = "<th width=$indivs_width><font size=-2>Individuals</th>";
1623                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1624                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1625                 $totals_data.= "<td align=left><font size=-2><b>Total Individuals with yearly PPIs completed:</b></font></td>";
1626                 $totals_data.= "<td align=center><font size=-2><b>$indivs_with_yearly_ppi / $total_indivs</b></font></td>";
1627                 $percent = ceil(($indivs_with_yearly_ppi / $total_indivs)*100);
1628                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1629                 $this->t->set_var('tr_color',$tr_color);
1630                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1631                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1632                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1633                 $totals_data.= "</tr>";
1634
1635                 $this->t->set_var('totals',$totals_data);
1636                 $this->t->set_var('totals_header_row',$totals_header_row);
1637                 $this->t->set_var('totals_table_width',$totals_table_width);
1638
1639                 $this->t->pfp('out','ppi_sched_t');
1640                 $this->save_sessiondata(); 
1641
1642         }
1643   
1644         function int_sched()
1645         {
1646                 $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1647                 $this->t->set_block('int_sched_t','individual_list','indivlist');
1648                 $this->t->set_block('int_sched_t','appt_list','apptlist');
1649                 $action = get_var('action',array('GET','POST'));
1650
1651                 $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1652                 $this->t->set_var('lang_reset','Clear Changes');
1653
1654                 $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
1655                 $this->t->set_var('int_link_title','Hometeaching Interviews');
1656
1657                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
1658                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
1659
1660                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched&action=save'));
1661                 $this->t->set_var('title','Hometeaching Interviews Scheduler');
1662
1663                 $indiv_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
1664                 $table_width=$indiv_width + $phone_width + $pri_width + $notes_width + $int_date_width;
1665                 $header_row = "<th width=$indiv_width><font size=-2>individual Name</th>";
1666                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1667                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1668                 $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
1669                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1670                 $table_data=""; $completed_data=""; $totals_data="";
1671
1672                 $year = date('Y');
1673                 $month = date('m');
1674                 $nextyear = $year + 1;
1675                 if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
1676                 if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
1677                 if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
1678                 if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
1679                 //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
1680
1681                 // create the individual id -> individual name mapping
1682                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY name ASC";
1683                 $this->db->query($sql,__LINE__,__FILE__);
1684                 $i=0;
1685                 $individual_data = NULL;
1686                 $indiv_name_data = NULL;
1687                 while ($this->db->next_record()) {
1688                         $indiv_name_data[$i] = $this->db->f('name');
1689                         $individual_data[$i] = $this->db->f('individual');
1690                         $individ2name[$individual_data[$i]] = $indiv_name_data[$i];
1691                         $i++;
1692                 }
1693                 // add any YM that are home teachers
1694                 $sql = "SELECT * FROM tc_companionship where valid=1";
1695                 $this->db->query($sql,__LINE__,__FILE__);
1696                 while ($this->db->next_record()) {
1697                         $tmp_individual = $this->db->f('individual');
1698                         $sql = "Select * FROM tc_individual where individual='$tmp_individual' and steward='' and valid=1";
1699                         $this->db2->query($sql,__LINE__,__FILE__);
1700                         while ($this->db2->next_record()) {
1701                                 $indiv_name_data[$i] = $this->db2->f('name');
1702                                 $individual_data[$i] = $this->db2->f('individual');
1703                                 $individ2name[$individual_data[$i]] = $indiv_name_data[$i];
1704                                 $i++;
1705                         }
1706                 }
1707                 array_multisort($indiv_name_data, $individual_data);
1708
1709                 if($action == 'save') {
1710                         // Save any changes made to the appointment table
1711                         $new_data = get_var('appt_notes',array('POST'));
1712                         if($new_data != "") {
1713                                 foreach ($new_data as $entry) {
1714                                         $indiv = $entry['individual'];
1715                                         $appointment = $entry['appointment'];
1716                                         $location = $entry['location'];
1717                                         if($location == "") {
1718                                                 $supervisor = $entry['supervisor'];
1719                                                 $supervisor_array = explode(",", $individ2name[$supervisor]);
1720                                                 $supervisor_last_name = $supervisor_array[0];
1721                                                 $sql = "SELECT * FROM tc_individual where individual='$supervisor'";
1722                                                 $this->db2->query($sql,__LINE__,__FILE__);
1723                                                 if($this->db2->next_record()) {
1724                                                         $mls_id = $this->db2->f('mls_id');
1725                                                 }
1726                                                 $sql = "SELECT * FROM tc_individual where mls_id='$mls_id'";
1727                                                 $this->db2->query($sql,__LINE__,__FILE__);
1728                                                 if($this->db2->next_record()) {
1729                                                         $supervisor_address = $this->db2->f('address');
1730                                                 }
1731                                                 $location = "$supervisor_last_name"." home ($supervisor_address)";
1732                                         }
1733                                         if($indiv == 0) { $location = ""; }
1734
1735                                         //print "indiv: $indiv appointment: $appointment <br>";
1736                                         //Only perform a database update if we have made a change to this appointment
1737                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and individual='$indiv' and location='$location'";
1738                                         $this->db->query($sql,__LINE__,__FILE__);
1739                                         if(!$this->db->next_record()) {
1740                                                 // Perform database save actions here
1741                                                 $this->db->query("UPDATE tc_appointment set " .
1742                                                                  " individual='" . $indiv . "'" .
1743                                                                  ",location='" . $location . "'" .
1744                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1745                                                 // Email the appointment
1746                                                 $this->email_appt($appointment);
1747                                         }
1748                                 }
1749                         }
1750
1751                         // Save any changes made to the int notes table
1752                         $new_data = get_var('hti_notes',array('POST'));
1753                         foreach ($new_data as $entry) {
1754                                 $hti_notes = $entry['notes'];
1755                                 $individual = $entry['individual'];
1756                                 $indiv_name = $entry['indiv_name'];
1757                                 $hti_pri = $entry['pri'];
1758                                 //print "hti_notes: $hti_notes indiv_name: $indiv_name <Br>";
1759                                 // Perform database save actions here
1760                                 $this->db->query("SELECT * FROM tc_companionship WHERE individual=$individual and valid=1",__LINE__,__FILE__);
1761                                 if ($this->db->next_record()) {
1762                                         $scheduling_priority = $this->db->f('scheduling_priority');
1763                                         //$this->logToFile("int_sched", "UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'");
1764                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'",__LINE__,__FILE__);
1765                                 }
1766                         }
1767
1768                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched');
1769                         //Header('Location: ' . $take_me_to_url);
1770                 }
1771
1772                 // Get the Districts
1773                 $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";
1774                 $this->db->query($sql,__LINE__,__FILE__);
1775                 $i=0;
1776                 while ($this->db->next_record()) {
1777                         $district = $this->db->f('district');
1778                         $districts[$i]['district'] = $this->db->f('district');
1779                         $districts[$i]['name'] = $this->db->f('name');
1780                         $districts[$i]['supervisor'] = $this->db->f('supervisor');
1781                         $districts[$i]['presidency'] = $this->db->f('presidency');
1782                         $i++;
1783                 }
1784
1785                 // APPOINTMENT TABLE
1786                 $district = 1;
1787                 $date_width=250; $time_width=100; $indiv_width=200; $location_width=100;
1788                 $appt_table_width=$date_width + $time_width + $indiv_width + $location_width;
1789                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1790                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1791                 $appt_header_row.= "<th width=$indiv_width><font size=-2>Individual</th>";
1792                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
1793                 $appt_table_data = ""; 
1794
1795                 $total_comps=0; $comps_with_quarterly_int=0;
1796
1797                 // Display a scheduling table for each district
1798                 for ($d=0; $d < count($districts); $d++) {
1799                         $table_data=""; $appt_table_data="";
1800                         $this->t->set_var('district_number',$districts[$d]['district']);
1801                         $this->t->set_var('district_name',$districts[$d]['name']);      
1802                         $supervisor = $districts[$d]['supervisor'];
1803                         $supervisor_array = explode(",", $supervisor);
1804                         $supervisor_last_name = $supervisor_array[0];
1805                         $sql = "SELECT * FROM tc_individual where individual='$supervisor'";
1806                         $this->db2->query($sql,__LINE__,__FILE__);
1807                         if($this->db2->next_record()) {
1808                                 $mls_id = $this->db2->f('mls_id');
1809                         }
1810                         $sql = "SELECT * FROM tc_individual where mls_id='$mls_id'";
1811                         $this->db2->query($sql,__LINE__,__FILE__);
1812                         if($this->db2->next_record()) {
1813                                 $supervisor_address = $this->db2->f('address');
1814                         }
1815                         $location = "$supervisor_last_name"." home ($supervisor_address)";
1816                         $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Individuals with Interviews Not Completed";
1817                         $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
1818                         $this->t->set_var('table_title',$table_title);
1819                         $this->t->set_var('appt_table_title',$appt_table_title);
1820
1821                         // query the database for all the appointments
1822                         $sql = "SELECT * FROM tc_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
1823                         $this->db->query($sql,__LINE__,__FILE__);
1824
1825                         while ($this->db->next_record()) {
1826                                 $appointment = $this->db->f('appointment');
1827                                 $indiv = $this->db->f('individual');
1828                                 $location = $this->db->f('location');
1829                                 if(($location == "") && ($indiv > 0)) { $location = "$supervisor_last_name"." home ($supervisor_address)"; }
1830
1831                                 $date = $this->db->f('date');
1832                                 $date_array = explode("-",$date);
1833                                 $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1834                                 $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1835
1836                                 $time = $this->db->f('time');
1837                                 $time_array = explode(":",$time);
1838                                 $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1839
1840                                 $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1841                                 $appt_table_data.= "<td align=center>$day_string</td>";
1842                                 $appt_table_data.= "<td align=center>$time_string</td>";
1843
1844                                 $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][individual]>';
1845                                 $appt_table_data.= '<option value=0></option>';
1846                                 for ($i=0; $i < count($individual_data); $i++) {
1847                                         $id = $individual_data[$i];
1848                                         $name = $indiv_name_data[$i];
1849                                         if($individual_data[$i] == $indiv) { 
1850                                                 $selected[$id] = 'selected="selected"'; 
1851                                         } else { 
1852                                                 $selected[$id] = ''; 
1853                                         }
1854                                         $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1855                                 }
1856                                 $appt_table_data.='</select></td>';
1857
1858                                 $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
1859                                 $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
1860
1861                                 $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1862                                 $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][supervisor]" value="'.$supervisor.'">';
1863
1864                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1865                                 $this->t->set_var('tr_color',$tr_color);
1866                         }
1867
1868                         $this->t->set_var('appt_table_data',$appt_table_data);
1869                         $this->t->set_var('appt_header_row',$appt_header_row);
1870                         $this->t->set_var('appt_table_width',$appt_table_width);
1871
1872                         // INTERVIEW SCHEDULING TABLE
1873
1874                         // Select all the unique companionship numbers for this district
1875                         $sql = "SELECT distinct companionship FROM tc_companionship where valid=1 and district=". $districts[$d]['district'];
1876                         $this->db->query($sql,__LINE__,__FILE__);
1877                         $j=0; $unique_companionships = '';
1878                         while ($this->db->next_record())
1879                         {
1880                                 $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
1881                                 $j++;
1882                         }
1883
1884                         $i=0;
1885                         for ($j=0; $j < count($unique_companionships); $j++) {
1886                                 // Select all the companions from each companionship
1887                                 $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'];
1888                                 $this->db->query($sql,__LINE__,__FILE__);
1889                                 $k=0; $int_completed=0;
1890                                 $comp = $unique_companionships[$j]['companionship'];
1891                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1892                                 $this->t->set_var('tr_color',$tr_color);
1893                                 $total_comps++;
1894                                 while ($this->db->next_record()) {
1895                                         // Get this companions information
1896                                         $individual = $this->db->f('individual');
1897
1898                                         $id = $this->db->f('individual');
1899                                         $name = $this->db->f('name');
1900                                         $phone = $this->db->f('phone');
1901                                         $hti_pri = $this->db->f('priority');
1902                                         $hti_notes = $this->db->f('notes');
1903
1904                                         // If the companionship has already had its quarterly interview,
1905                                         // Skip the other companion in the companionship.
1906                                         if($int_completed == 1) {
1907                                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1908                                                 $completed_data.= "<td align=center>$phone</td>";
1909                                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1910                                                 $completed_data.= "<td align=left>$hti_notes</td>";
1911                                                 $completed_data.= '</tr>';
1912                                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1913                                                 $this->t->set_var('tr_color2',$tr_color2);
1914                                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1915                                                 $this->t->set_var('tr_color',$tr_color);
1916                                                 continue;
1917                                         }
1918
1919                                         // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
1920                                         $sql = "SELECT * FROM tc_interview WHERE date >= '$quarter_start' AND date < '$quarter_end' AND individual='$id'";
1921                                         $this->db2->query($sql,__LINE__,__FILE__);
1922
1923                                         if(!$this->db2->next_record()) {
1924                                                 $sql = "SELECT * FROM tc_interview WHERE individual='$id' ORDER BY date DESC";
1925                                                 $this->db3->query($sql,__LINE__,__FILE__);
1926                                                 if($this->db3->next_record()) { 
1927                                                         $date = $this->db3->f('date'); 
1928                                                 } else { 
1929                                                         $date = ""; 
1930                                                 }
1931                                                 $link_data['menuaction'] = 'tc.tc.int_update';
1932                                                 $link_data['individual'] = $id;
1933                                                 $link_data['name'] = $name;
1934                                                 $link_data['interview'] = '';
1935                                                 $link_data['action'] = 'add';
1936                                                 $link_data['interviewer'] = $districts[$d]['supervisor'];
1937                                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
1938                                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1939                                                 $table_data.= "<td align=center>$phone</td>";
1940                                                 $table_data.= "<td align=center>";
1941                                                 $table_data.= '<select name=hti_notes['.$i.'][pri]>';
1942                                                 foreach(range(0,6) as $num) {
1943                                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
1944                                                         if($hti_pri == $num) { 
1945                                                                 $selected[$num] = 'selected="selected"'; 
1946                                                         } else { 
1947                                                                 $selected[$num] = ''; 
1948                                                         }
1949                                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1950                                                 }
1951                                                 $table_data.= '</select></td>';
1952                                                 $table_data.= "<td align=center>$date</td>";
1953                                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="hti_notes['.$i.'][notes]" value="'.$hti_notes.'">';
1954                                                 $table_data.= '<input type=hidden name="hti_notes['.$i.'][individual]" value="'.$id.'">';
1955                                                 $table_data.= '<input type=hidden name="hti_notes['.$i.'][indiv_name]" value="'.$name.'">';
1956                                                 $table_data.= '</td>';
1957                                                 $table_data.= '</tr>'."\n";
1958                                                 $i++;
1959                                         } else {
1960                                                 $link_data['menuaction'] = 'tc.tc.int_update';
1961                                                 $link_data['interviewer'] = $this->db2->f('interviewer');
1962                                                 $link_data['individual'] = $this->db2->f('individual');
1963                                                 $link_data['name'] = $name;
1964                                                 $link_data['interview'] = $this->db2->f('interview');
1965                                                 $link_data['action'] = 'view';
1966                                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
1967                                                 $comps_with_quarterly_int++;
1968                                                 $int_completed=1;
1969                                                 $date = $this->db2->f('date');
1970                                                 $hti_notes = $this->db2->f('notes');
1971                                                 if(strlen($hti_notes) > 40) { $hti_notes = substr($hti_notes,0,40) . "..."; }
1972                                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1973                                                 $completed_data.= "<td align=center>$phone</td>";
1974                                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1975                                                 $completed_data.= "<td align=left>$hti_notes</td>";
1976                                                 $completed_data.= '</tr>';
1977                                         }
1978                                 }
1979                         }
1980
1981                         $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1982                         $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1983                         $completed_header_row = "<th width=$name_width><font size=-2>Individual Name</th>";
1984                         $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1985                         $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1986                         $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
1987
1988                         $this->t->set_var('table_width',$table_width);
1989                         $this->t->set_var('header_row',$header_row);
1990                         $this->t->set_var('table_data',$table_data);
1991                         $this->t->set_var('completed_header_row',$completed_header_row);
1992                         $this->t->set_var('completed_table_width',$completed_table_width);
1993                         $this->t->set_var('completed',$completed_data);
1994                         $this->t->fp('indivlist','individual_list',True);
1995
1996                 } // End for each district loop
1997
1998
1999                 $indivs_width=300; $totals_width=100;
2000                 $totals_table_width=$indivs_width + $totals_width;
2001                 $totals_header_row = "<th width=$indivs_width><font size=-2>Individuals</th>";
2002                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2003                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2004                 $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
2005                 $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
2006                 $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
2007                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2008                 $this->t->set_var('tr_color',$tr_color);
2009                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2010                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2011                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2012                 $totals_data.= "</tr>";
2013
2014                 $this->t->set_var('totals',$totals_data);
2015                 $this->t->set_var('totals_header_row',$totals_header_row);
2016                 $this->t->set_var('totals_table_width',$totals_table_width);
2017
2018                 $this->t->pfp('out','int_sched_t');
2019                 $this->save_sessiondata(); 
2020
2021         }
2022   
2023         function vis_sched()
2024         {
2025                 $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
2026                 $this->t->set_block('vis_sched_t','family_list','familylist');
2027                 $this->t->set_block('vis_sched_t','appt_list','apptlist');
2028                 $action = get_var('action',array('GET','POST'));
2029
2030                 $this->t->set_var('lang_save','Save Appt / Pri / Notes');
2031                 $this->t->set_var('lang_reset','Clear Changes');
2032
2033                 $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
2034                 $this->t->set_var('vis_link_title','View Yearly Visits');
2035
2036                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
2037                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2038
2039                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched&action=save'));
2040                 $this->t->set_var('title','Presidency Yearly Visit Scheduler');
2041
2042                 $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
2043                 $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
2044                 $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
2045                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
2046                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
2047                 $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
2048                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2049                 $table_data=""; $completed_data=""; $totals_data="";
2050
2051                 $year = date('Y');
2052
2053                 // create the family id -> family name mapping
2054                 $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";
2055                 $this->db->query($sql,__LINE__,__FILE__);
2056                 $i=0;
2057                 $family_id = NULL;
2058                 while ($this->db->next_record()) {
2059                         $family_id[$i] = $this->db->f('family');
2060                         $family_name[$i] = $this->db->f('name');
2061                         $familyid2name[$family_id[$i]] = $family_name[$i];
2062                         $familyid2address[$family_id[$i]] = $this->db->f('address');
2063                         $i++;
2064                 }
2065                 array_multisort($family_name, $family_id);
2066
2067                 if($action == 'save') {
2068                         // Save any changes made to the appointment table
2069                         $new_data = get_var('appt_notes',array('POST'));
2070                         if($new_data != "") {
2071                                 foreach ($new_data as $entry) {
2072                                         $family = $entry['family'];
2073                                         $appointment = $entry['appointment'];
2074                                         $location = $entry['location'];
2075                                         if($location == "") {
2076                                                 $family_name_array = explode(",", $familyid2name[$family]);
2077                                                 $family_last_name = $family_name_array[0];
2078                                                 $family_address = $familyid2address[$family];
2079                                                 $location = "$family_last_name"." home ($family_address)";
2080                                         }
2081                                         if($family == 0) { $location = ""; }
2082
2083                                         //Only perform a database update if we have made a change to this appointment
2084                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and family='$family' and location='$location'";
2085                                         $this->db->query($sql,__LINE__,__FILE__);
2086                                         if(!$this->db->next_record()) {
2087                                                 // Perform database save actions here
2088                                                 $this->db->query("UPDATE tc_appointment set " .
2089                                                                  " family='" . $family . "'" .
2090                                                                  ",location='" . $location . "'" .
2091                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2092
2093                                                 // Email the appointment
2094                                                 $this->email_appt($appointment);
2095                                         }
2096                                 }
2097                         }
2098
2099                         // Save any changes made to the visit notes table
2100                         $new_data = get_var('vis_notes',array('POST'));
2101                         foreach ($new_data as $entry) {
2102                                 $visit_notes = $entry['notes'];
2103                                 $family = $entry['family_id'];
2104                                 $visit_pri = $entry['pri'];
2105                                 // Perform database save actions here
2106                                 $this->db->query("SELECT * FROM tc_family WHERE family='$family'",__LINE__,__FILE__);
2107                                 if ($this->db->next_record()) {
2108                                         $scheduling_priority = $this->db->f('scheduling_priority');
2109                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$visit_pri', notes=\"$visit_notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
2110                                 }
2111                         }
2112
2113                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched');
2114                         //Header('Location: ' . $take_me_to_url);
2115                 }
2116
2117                 // APPOINTMENT TABLE
2118                 $date_width=250; $time_width=100; $family_width=250; $location_width=100;
2119                 $appt_table_width=$date_width + $time_width + $family_width + $location_width;
2120                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2121                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2122                 $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
2123                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
2124                 $appt_table_data = ""; 
2125
2126                 // Find out what the President ID is
2127                 $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";
2128                 $this->db->query($sql,__LINE__,__FILE__);
2129                 if($this->db->next_record()) {
2130                         $presidency_name = $this->db->f('name');
2131                         $presidency_id = $this->db->f('presidency');
2132                 } else {
2133                         print "<hr><font color=red><h3>-E- Unable to locate Presidency in tc_presidency table</h3></font></hr>";
2134                         return;
2135                 }
2136
2137                 // query the database for all the appointments
2138                 $sql = "SELECT * FROM tc_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
2139                 $this->db->query($sql,__LINE__,__FILE__);
2140
2141                 while ($this->db->next_record()) {
2142                         $appointment = $this->db->f('appointment');
2143                         $family = $this->db->f('family');
2144                         $location = $this->db->f('location');
2145                         $family_name_array = explode(",", $familyid2name[$family]);
2146                         $family_last_name = $family_name_array[0];
2147                         $family_address = $familyid2address[$family];
2148                         if(($location == "") && ($family > 0)) { $location = "$family_last_name"." home ($family_address)"; }
2149
2150                         $date = $this->db->f('date');
2151                         $date_array = explode("-",$date);
2152                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2153                         $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2154
2155                         $time = $this->db->f('time');
2156                         $time_array = explode(":",$time);
2157                         $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2158
2159                         $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2160                         $appt_table_data.= "<td align=center>$day_string</td>";
2161                         $appt_table_data.= "<td align=center>$time_string</td>";
2162
2163                         $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
2164                         $appt_table_data.= '<option value=0></option>';
2165                         for ($i=0; $i < count($family_id); $i++) {
2166                                 $id = $family_id[$i];
2167                                 $name = $family_name[$i];
2168                                 if($family_id[$i] == $family) { 
2169                                         $selected[$id] = 'selected="selected"'; 
2170                                 } else { 
2171                                         $selected[$id] = ''; 
2172                                 }
2173                                 $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
2174                         }
2175                         $appt_table_data.='</select></td>';
2176
2177                         $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
2178                         $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
2179
2180                         $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2181
2182                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2183                         $this->t->set_var('tr_color',$tr_color);
2184                 }
2185
2186                 $this->t->set_var('appt_table_data',$appt_table_data);
2187                 $this->t->set_var('appt_header_row',$appt_header_row);
2188                 $this->t->set_var('appt_table_width',$appt_table_width);
2189
2190
2191                 // VISIT SCHEDULING TABLE
2192                 $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";
2193                 $this->db->query($sql,__LINE__,__FILE__);
2194
2195                 $total_families=0; $families_with_yearly_visit=0;
2196
2197                 while ( $this->db->next_record()) {
2198                         $total_families++;
2199                         $id = $this->db->f('family');
2200                         $name = $this->db->f('name');
2201                         $phone = $this->db->f('phone');
2202                         $vis_pri = $this->db->f('priority');
2203                         $vis_notes = $this->db->f('notes');
2204
2205                         // If this family has had a yearly visit this year, don't show them on the schedule list
2206                         $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2207                         $sql = "SELECT * FROM tc_visit WHERE date > '$year_start' AND date < '$year_end' ".
2208                                "AND family=" . $id . " AND companionship=0";
2209                         $this->db2->query($sql,__LINE__,__FILE__);
2210
2211                         if(!$this->db2->next_record()) {
2212                                 $sql = "SELECT * FROM tc_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
2213                                 $this->db3->query($sql,__LINE__,__FILE__);
2214                                 if($this->db3->next_record()) { 
2215                                         $date = $this->db3->f('date'); 
2216                                 } else { 
2217                                         $date = ""; 
2218                                 }
2219                                 $link_data['menuaction'] = 'tc.tc.vis_update';
2220                                 $link_data['visit'] = '';
2221                                 $link_data['family'] = $id;
2222                                 $link_data['name'] = $name;
2223                                 $link_data['action'] = 'add';
2224                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2225                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2226                                 $table_data.= "<td align=center>$phone</td>";
2227                                 $table_data.= "<td align=center>";
2228                                 $table_data.= '<select name=vis_notes['.$id.'][pri]>';
2229                                 foreach(range(0,6) as $num) {
2230                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
2231                                         if($vis_pri == $num) { 
2232                                                 $selected[$num] = 'selected="selected"'; 
2233                                         } else { 
2234                                                 $selected[$num] = ''; 
2235                                         }
2236                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2237                                 }
2238                                 $table_data.= '</select></td>';
2239                                 $table_data.= "<td align=center>$date</td>";
2240                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$id.'][notes]" value="'.$vis_notes.'">';
2241                                 $table_data.= '<input type=hidden name="vis_notes['.$id.'][family_id]" value="'.$id.'">';
2242                                 $table_data.= '<input type=hidden name="vis_notes['.$id.'][family_name]" value="'.$name.'">';
2243                                 $table_data.= '</td>';
2244                                 $table_data.= '</tr>';
2245                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2246                                 $this->t->set_var('tr_color',$tr_color);
2247                         } else {
2248                                 $link_data['menuaction'] = 'tc.tc.vis_update';
2249                                 $link_data['visit'] = $this->db2->f('visit');
2250                                 $link_data['family'] = $this->db2->f('family');
2251                                 $link_data['name'] = $name;
2252                                 $link_data['date'] = $this->db2->f('date');
2253                                 $link_data['action'] = 'view';
2254                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
2255                                 $families_with_yearly_visit++;
2256                                 $date = $this->db2->f('date');
2257                                 $vis_notes = $this->db2->f('notes');
2258                                 if(strlen($vis_notes) > 40) { $vis_notes = stripslashes(substr($vis_notes,0,40) . "..."); }
2259                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2260                                 $completed_data.= "<td align=center>$phone</td>";
2261                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2262                                 $completed_data.= "<td align=left>$vis_notes</td>";
2263                                 $completed_data.= '</tr>';
2264                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2265                                 $this->t->set_var('tr_color2',$tr_color2);
2266                         }
2267                 }
2268
2269                 $name_width=190; $phone_width=100; $date_width=100; $notes_width=300;
2270                 $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2271                 $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
2272                 $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2273                 $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2274                 $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
2275
2276                 $family_width=300; $totals_width=100;
2277                 $totals_table_width=$family_width + $totals_width;
2278                 $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
2279                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2280                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2281                 $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
2282                 $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
2283                 $percent = ceil(($families_with_yearly_visit / $total_families)*100);
2284                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2285                 $this->t->set_var('tr_color',$tr_color);
2286                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2287                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2288                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2289                 $totals_data.= "</tr>";
2290
2291                 $this->t->set_var('table_width',$table_width);
2292                 $this->t->set_var('header_row',$header_row);
2293                 $this->t->set_var('table_data',$table_data);
2294                 $this->t->set_var('totals_header_row',$totals_header_row);
2295                 $this->t->set_var('totals_table_width',$totals_table_width);
2296                 $this->t->set_var('completed_header_row',$completed_header_row);
2297                 $this->t->set_var('completed_table_width',$completed_table_width);
2298                 $this->t->set_var('completed',$completed_data);
2299                 $this->t->set_var('totals',$totals_data);
2300                 $this->t->fp('familylist','family_list',True);
2301                 $this->t->fp('apptlist','appt_list',True);
2302
2303                 $this->t->pfp('out','vis_sched_t');
2304                 $this->save_sessiondata(); 
2305         }
2306   
2307         function ppi_view()
2308         {
2309                 $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
2310                 $this->t->set_block('ppi_view_t','district_list','list');
2311
2312                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2313                 $num_months = get_var('num_months',array('GET','POST'));
2314                 if($num_months == '') { $num_months = $this->default_ppi_num_months; }
2315                 $this->t->set_var('num_months',$num_months);
2316                 if($num_months == 1) { 
2317                         $this->t->set_var('lang_num_months','Month of History'); 
2318                 } else {  
2319                         $this->t->set_var('lang_num_months','Months of History'); 
2320                 }
2321                 $this->t->set_var('lang_filter','Filter');
2322                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2323
2324                 $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2325                 $this->t->set_var('ppi_link_title','Yearly PPIs'); 
2326
2327                 $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched'));
2328                 $this->t->set_var('schedule_ppi_link_title','Schedule Yearly PPIs');
2329
2330                 $this->t->set_var('title','Yearly PPIs');
2331                 $num_months = get_var('num_months',array('GET','POST'));
2332                 if($num_months == '') { $num_months = $this->default_ppi_num_years; }
2333                 $this->t->set_var('num_months',$num_months);
2334                 if($num_months == 1) { 
2335                         $this->t->set_var('lang_num_months','Year of History'); 
2336                 } else { 
2337                         $this->t->set_var('lang_num_months','Years of History');
2338                 }
2339
2340                 $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";
2341                 $this->db->query($sql,__LINE__,__FILE__);
2342                 if($this->db->next_record()) {
2343                         $president_name = $this->db->f('name');
2344                         $interviewer = $this->db->f('individual');
2345                         $interview_type = 'ppi';
2346                 } else {
2347                         print "<hr><font color=red><h3>-E- Unable to locate President in tc_presidency table</h3></font></hr>";
2348                         return;
2349                 }
2350                 $this->t->set_var('district_number','*');
2351                 $this->t->set_var('district_name',$president_name);
2352
2353                 $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";
2354                 $this->db->query($sql,__LINE__,__FILE__);
2355                 $i=0;
2356                 while ($this->db->next_record()) {
2357                         $individual[$i] = $this->db->f('individual');
2358                         $indiv_name[$i] = $this->db->f('name');
2359                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
2360                         $indiv_priority[$individual[$i]] = $this->db->f('priority');
2361                         $indiv_notes[$individual[$i]] = $this->db->f('notes');
2362                         $i++;
2363                 }
2364                 $total_indivs=$i;
2365                 array_multisort($indiv_name, $individual);
2366                 //var_dump($indiv_name); print "<br><br>"; var_dump($individual);
2367
2368                 $header_row="<th width=$comp_width><font size=-2>Individual Name</th>";
2369
2370                 $indiv_width=400; $ppi_width=75; $table_width=$indiv_width + $num_months*$ppi_width;
2371                 $table_data="";
2372                 for($m=$num_months; $m >= 0; $m--) {
2373                         $year = date('Y') - $m;
2374                         $header_row .= "<th width=150><font size=-2>$year</th>"; 
2375                         $ppis[$m] = 0;
2376                 }
2377
2378                 for ($j=0; $j < count($individual); $j++) {
2379                         $id = $individual[$j];
2380                         $name = $indiv_name[$j];
2381                         $phone = $indiv_phone[$id];
2382
2383                         $link_data['menuaction'] = 'tc.tc.ppi_update';
2384                         $link_data['interviewer'] = $interviewer;
2385                         $link_data['indiv'] = $id;
2386                         $link_data['name'] = $name;
2387                         $link_data['interview'] = '';
2388                         $link_data['interview_type'] = $interview_type;
2389                         $link_data['action'] = 'add';
2390                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2391                         $this->nextmatchs->template_alternate_row_color(&$this->t);
2392                         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2393
2394                         // Find out how many times PPIs were performed in the past $num_months for this individual
2395                         for($m=$num_months; $m >= 0; $m--) {
2396                                 $year = date('Y') - $m;
2397                                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2398                                 $sql = "SELECT * FROM tc_interview WHERE date > '$year_start' AND date < '$year_end' ".
2399                                        "AND individual=" . $id . " AND interview_type='ppi'";
2400                                 $this->db2->query($sql,__LINE__,__FILE__);
2401
2402                                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
2403                                 if($this->db2->next_record()) {
2404                                         $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
2405                                         $link_data['menuaction'] = 'tc.tc.ppi_update';
2406                                         $link_data['companionship'] = $companionship;
2407                                         $link_data['interviewer'] = $this->db2->f('interviewer');
2408                                         $link_data['indiv'] = $id;
2409                                         $link_data['name'] = $name;
2410                                         $link_data['interview'] = $this->db2->f('interview');
2411                                         $link_data['interview_type'] = $interview_type;
2412                                         $link_data['action'] = 'view';
2413                                         $date = $this->db2->f('date');
2414                                         $date_array = explode("-",$date);
2415                                         $month = $date_array[1];
2416                                         $day   = $date_array[2];
2417                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2418                                         $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2419                                 } else { 
2420                                         $table_data .= "<td>&nbsp;</td>"; 
2421                                 }
2422                         }
2423                         $table_data .= "</tr>\n"; 
2424                 }
2425                 $table_data .= "<tr><td colspan=20><hr></td></tr>";
2426
2427                 $stat_data = "<tr><td><b><font size=-2>$total_indivs Individuals<br>PPI Totals:</font></b></td>";
2428                 for($m=$num_months; $m >=0; $m--) {
2429                         $percent = ceil(($ppis[$m] / $total_indivs)*100);
2430                         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
2431                 }
2432                 $stat_data .= "</tr>";
2433
2434                 $this->t->set_var('table_width',$table_width);
2435                 $this->t->set_var('header_row',$header_row);
2436                 $this->t->set_var('table_data',$table_data);
2437                 $this->t->set_var('stat_data',$stat_data);
2438                 $this->t->pfp('out','ppi_view_t');
2439                 $this->save_sessiondata(); 
2440         }
2441
2442         function ppi_update()
2443         {
2444                 $this->t->set_file(array('form' => 'ppi_update.tpl'));
2445                 $this->t->set_block('form','interviewer_list','int_list');
2446                 $this->t->set_block('form','add','addhandle');
2447                 $this->t->set_block('form','edit','edithandle');
2448
2449                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2450                 $this->t->set_var('readonly','');
2451                 $this->t->set_var('disabled','');
2452
2453                 $action = get_var('action',array('GET','POST'));
2454                 $companionship = get_var('companionship',array('GET','POST'));
2455                 $interviewer = get_var('interviewer',array('GET','POST'));      
2456                 $name = get_var('name',array('GET','POST'));
2457                 $interview = get_var('interview',array('GET','POST'));
2458                 $indiv = get_var('indiv',array('GET','POST'));
2459                 $date = get_var('date',array('GET','POST'));
2460                 $notes = get_var('notes',array('GET','POST'));
2461                 $interview_type = get_var('interview_type',array('GET','POST'));
2462
2463                 $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)";
2464                 $this->db2->query($sql,__LINE__,__FILE__);
2465                 while ($this->db2->next_record()) {
2466                         $indiv = $this->db2->f('individual');
2467                         $interviewer_name = $this->db2->f('name');
2468                         if($indiv == $interviewer) {
2469                                 $this->t->set_var('interviewer',$interviewer . ' selected');
2470                         } else {
2471                                 $this->t->set_var('interviewer',$interviewer);
2472                         }
2473                         $this->t->set_var('interviewer_name',$interviewer_name);
2474                         $this->t->set_var('interview_type_checked','');
2475                         $this->t->fp('int_list','interviewer_list',True);
2476                 }
2477
2478                 if($action == 'save') {
2479                         $notes = get_var('notes',array('POST'));
2480                         $this->db->query("UPDATE tc_interview set " .
2481                                          "   interview='" . $interview . "'" .
2482                                          ", interviewer='" . $interviewer . "'" .
2483                                          ", individual='" . $indiv . "'" .
2484                                          ", date='" . $date . "'" .
2485                                          ", notes='" . $notes . "'" .
2486                                          ", interview_type='" . $interview_type . "'" .
2487                                          " WHERE interview=" . $interview,__LINE__,__FILE__);
2488                         $this->ppi_view();
2489                         return false;
2490                 }
2491
2492                 if($action == 'insert') {
2493                         $notes = get_var('notes',array('POST'));
2494                         $this->db->query("INSERT INTO tc_interview (interviewer,individual,date,notes,interview_type) " .
2495                                          "VALUES ('" . $interviewer . "','" . $indiv . "','" .
2496                                          $date . "','" . $notes . "','" . $interview_type  ."')",__LINE__,__FILE__);
2497                         $this->ppi_view();
2498                         return false;
2499                 }
2500
2501                 if($action == 'add') {
2502                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2503                         $this->t->set_var('interview', '');
2504                         $this->t->set_var('interviewer', $interviewer);
2505                         $this->t->set_var('name',$name);
2506                         $this->t->set_var('indiv',$indiv);
2507                         $this->t->set_var('date','');
2508                         $this->t->set_var('notes','');
2509                         $this->t->set_var('interview_type',$interview_type);
2510                         $this->t->set_var('interview_type_checked','checked');
2511                         $this->t->set_var('lang_done','Cancel');
2512                         $this->t->set_var('lang_action','Adding New PPI');
2513                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview=' .
2514                                           $interview . '&action=' . 'insert'));
2515                 }
2516
2517                 if($action == 'edit' || $action == 'view') {
2518                         $sql = "SELECT * FROM tc_interview WHERE interview=".$interview;
2519                         $this->db->query($sql,__LINE__,__FILE__);
2520                         $this->db->next_record();
2521                         $this->t->set_var('interview',$interview);
2522                         $this->t->set_var('name',$name);
2523                         $this->t->set_var('interviewer', $this->db->f('interviewer'));
2524                         $this->t->set_var('indiv',$this->db->f('indiv'));
2525                         $this->t->set_var('date',$this->db->f('date'));
2526                         $this->t->set_var('notes',$this->db->f('notes'));
2527                         $this->t->set_var('interview_type',$this->db->f('interview_type'));
2528                         if($this->db->f('interview_type') == 1) { $this->t->set_var('interview_type_checked','checked'); }
2529                 }
2530
2531                 if($action == 'edit') {
2532                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2533                         $this->t->set_var('lang_done','Cancel');
2534                         $this->t->set_var('lang_action','Editing PPI');
2535                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview='. 
2536                                           $interview . '&action=' . 'save'));
2537                 }
2538
2539                 if($action == 'view') {
2540                         $date = $this->db->f('date');
2541                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2542                         $this->t->set_var('readonly','READONLY');
2543                         $this->t->set_var('disabled','DISABLED');
2544                         $this->t->set_var('lang_done','Done');
2545                         $this->t->set_var('lang_action','Viewing PPI');
2546                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview=' .
2547                                           $interview . '&action=' . 'edit'));
2548                 }
2549
2550                 $this->t->set_var('lang_reset','Clear Form');
2551                 $this->t->set_var('lang_add','Add PPI');
2552                 $this->t->set_var('lang_save','Save Changes');
2553                 $this->t->set_var('edithandle','');
2554                 $this->t->set_var('addhandle','');
2555
2556                 $this->t->pfp('out','form');
2557
2558                 if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2559                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2560                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
2561
2562                 $this->save_sessiondata(); 
2563         }
2564
2565         function int_view()
2566         {
2567                 $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2568                 $this->t->set_block('int_view_t','district_list','list');
2569
2570                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2571                 $num_quarters = get_var('num_quarters',array('GET','POST'));
2572                 if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2573                 $this->t->set_var('num_quarters',$num_quarters);
2574                 if($num_quarters == 1) { 
2575                         $this->t->set_var('lang_num_quarters','Quarter of History'); 
2576                 } else {
2577                         $this->t->set_var('lang_num_quarters','Quarters of History'); 
2578                 }
2579                 $this->t->set_var('lang_filter','Filter');
2580
2581                 $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2582                 $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2583
2584                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
2585                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2586
2587                 $this->t->set_var('title','Hometeaching Interviews'); 
2588
2589                 $num_months = $num_quarters * 3 - 1;
2590                 $current_month = $this->current_month;
2591                 if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2592                 else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2593                 else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2594                 else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2595
2596                 $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";
2597                 $this->db->query($sql,__LINE__,__FILE__);
2598                 $i=0;
2599                 while ($this->db->next_record()) {
2600                         $districts[$i]['district'] = $this->db->f('district');
2601                         $districts[$i]['name'] = $this->db->f('name');
2602                         $districts[$i]['supervisor'] = $this->db->f('supervisor');
2603                         $i++;
2604                 }
2605
2606                 $sql = "SELECT * FROM tc_individual where valid=1 ORDER BY individual ASC";
2607                 $this->db->query($sql,__LINE__,__FILE__);
2608                 $i=0;
2609                 while ($this->db->next_record()) {
2610                         $individual[$i] = $this->db->f('individual');
2611                         $indiv_name[$i] = $this->db->f('name');
2612                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
2613                         $i++;
2614                 }
2615                 array_multisort($indiv_name, $individual);
2616                 for($i=0; $i < count($individual); $i++) {
2617                         $id = $individual[$i];
2618                         $indivs[$id] = $indiv_name[$i];
2619                 }      
2620
2621                 $total_companionships = 0;
2622                 $this->nextmatchs->template_alternate_row_color(&$this->t);
2623                 for ($i=0; $i < count($districts); $i++) {
2624                         $this->t->set_var('district_number',$districts[$i]['district']);
2625                         $this->t->set_var('district_name',$districts[$i]['name']);      
2626                         $supervisor = $districts[$i]['supervisor'];
2627
2628                         // Select all the unique companionship numbers for this district
2629                         $sql = "SELECT distinct companionship FROM tc_companionship where valid=1 and district=". $districts[$i]['district'];
2630                         $this->db->query($sql,__LINE__,__FILE__);
2631                         $j=0; $unique_companionships = '';
2632                         while ($this->db->next_record()) {
2633                                 $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2634                                 $j++;
2635                         }
2636
2637                         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2638                         $table_data=""; $num_companionships = $j; $num_indivs = 0;
2639                         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2640                         for ($j=0; $j < count($unique_companionships); $j++) {
2641                                 // Select all the companions in each companionship
2642                                 $sql = "SELECT * FROM tc_companion where valid=1 and ".
2643                                        "companionship=". $unique_companionships[$j]['companionship'];
2644                                 $this->db->query($sql,__LINE__,__FILE__);
2645                                 $k=0;
2646                                 $comp = $unique_companionships[$j]['companionship'];
2647                                 for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2648                                 while ($this->db->next_record()) {
2649                                         // Get this companions information
2650                                         $num_indivs++;
2651                                         $companionship = $this->db->f('companionship');
2652                                         $individual = $this->db->f('individual');
2653                                         $name = $indivs[$individual];
2654                                         $phone = $indiv_phone[$individual];
2655                                         $link_data['menuaction'] = 'tc.tc.int_update';
2656                                         $link_data['companionship'] = $companionship;
2657                                         $link_data['interviewer'] = $supervisor;
2658                                         $link_data['indiv'] = $individual;
2659                                         $link_data['name'] = $name;
2660                                         $link_data['interview'] = '';
2661                                         $link_data['action'] = 'add';
2662                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2663                                         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2664
2665                                         // Find out how many times Interviews were performed in the past $num_months for this individual
2666                                         $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
2667                                         for($m=$num_months; $m >= 0; $m--) {
2668                                                 $month = $current_month - $m;
2669                                                 $year = $this->current_year;
2670                                                 if($month <= 0) { 
2671                                                         $remainder = $month; 
2672                                                         $month = 12 + $remainder; 
2673                                                         $year=$year-1; 
2674                                                 }
2675                                                 if($month < 10) { $month = "0"."$month"; }
2676                                                 $month_start = "$year"."-"."$month"."-"."01";
2677                                                 $month_end = "$year"."-"."$month"."-"."31";
2678                                                 $month = "$month"."/"."$year";
2679                                                 $sql = "SELECT * FROM tc_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
2680                                                        "AND individual=" . $individual;
2681                                                 $this->db2->query($sql,__LINE__,__FILE__);
2682                                                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
2683
2684                                                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
2685                                                 if($this->db2->next_record()) {
2686                                                         if(!$int_recorded[$companionship][$m]) {
2687                                                                 $ints[$m]++; 
2688                                                                 $total_ints[$m]++; 
2689                                                                 $int_recorded[$companionship][$m]=1;
2690                                                         }
2691                                                         $link_data['menuaction'] = 'tc.tc.int_update';
2692                                                         $link_data['companionship'] = $companionship;
2693                                                         $link_data['interviewer'] = $this->db2->f('interviewer');
2694                                                         $link_data['indiv'] = $individual;
2695                                                         $link_data['name'] = $name;
2696                                                         $link_data['interview'] = $this->db2->f('interview');
2697                                                         $link_data['action'] = 'view';
2698                                                         $date = $this->db2->f('date');
2699                                                         $date_array = explode("-",$date);
2700                                                         $month = $date_array[1];
2701                                                         $day   = $date_array[2];
2702                                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2703                                                         $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2704                                                 }
2705                                                 else { 
2706                                                         $table_data .= "<td>&nbsp;</td>"; 
2707                                                 }
2708                                         }
2709                                         $table_data .= "</tr>"; 
2710                                         $k++;
2711                                 }
2712                                 $table_data .= "<tr><td colspan=20><hr></td></tr>";
2713                         }
2714                         $total_companionships += $num_companionships;
2715                         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Totals:</font></b></td>";
2716
2717                         // Print the hometeaching interview stats
2718                         for($m=$num_months; $m >=0; $m--) {
2719                                 $month = $current_month - $m;
2720                                 if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2721                                 $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2722                                 //print "$month % $this->monthly_hometeaching_interview_stats = $month_begins <br>";
2723                                 if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2724                                 if(($month_begins) == 1) { 
2725                                         $total = $ints[$m]; 
2726                                 } else { 
2727                                         $total += $ints[$m]; 
2728                                 }
2729                                 $percent = ceil(($total / $num_companionships)*100);
2730                                 $stat_data .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2731                         }
2732                         $stat_data .= "</tr>";
2733
2734                         $this->t->set_var('table_width',$table_width);
2735                         $this->t->set_var('header_row',$header_row);
2736                         $this->t->set_var('table_data',$table_data);
2737                         $this->t->set_var('stat_data',$stat_data);
2738                         $this->t->fp('list','district_list',True);
2739                 }
2740
2741                 // Display the totals
2742                 $total = 0;
2743                 $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Totals:</font></b></td>";
2744                 for($m=$num_months; $m >=0; $m--) {
2745                         $month = $current_month - $m;
2746                         if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
2747                         $month_begins = $month % $this->monthly_hometeaching_interview_stats;
2748                         if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
2749                         if(($month_begins) == 1) { 
2750                                 $total = $total_ints[$m]; 
2751                         } else { 
2752                                 $total += $total_ints[$m]; 
2753                         }
2754                         $percent = ceil(($total / $total_companionships)*100);
2755                         $totals .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
2756                 }
2757                 $totals .= "</tr>";
2758
2759                 $this->t->set_var('totals',$totals);
2760                 $this->t->pfp('out','int_view_t');
2761                 $this->save_sessiondata(); 
2762         }
2763
2764         function int_update()
2765         {
2766                 $this->t->set_file(array('form' => 'int_update.tpl'));
2767                 $this->t->set_block('form','interviewer_list','int_list');
2768                 $this->t->set_block('form','add','addhandle');
2769                 $this->t->set_block('form','edit','edithandle');
2770
2771                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2772                 $this->t->set_var('readonly','');
2773                 $this->t->set_var('disabled','');
2774                 $this->t->set_var('interview_type_checked','');
2775
2776                 $action = get_var('action',array('GET','POST'));
2777                 $companionship = get_var('companionship',array('GET','POST'));
2778                 $interviewer = get_var('interviewer',array('GET','POST'));      
2779                 $name = get_var('name',array('GET','POST'));
2780                 $interview = get_var('interview',array('GET','POST'));
2781                 $indiv = get_var('indiv',array('GET','POST'));
2782                 $date = get_var('date',array('GET','POST'));
2783                 $notes = get_var('notes',array('GET','POST'));
2784                 $interview_type = get_var('interview_type',array('GET','POST'));
2785
2786                 $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)";
2787                 $this->db2->query($sql,__LINE__,__FILE__);
2788                 while ($this->db2->next_record()) {
2789                         $indiv = $this->db2->f('individual');
2790                         $interviewer_name = $this->db2->f('name');
2791                         if($indiv == $interviewer) {
2792                                 $this->t->set_var('interviewer',$interviewer . ' selected');
2793                         } else {
2794                                 $this->t->set_var('interviewer',$interviewer);
2795                         }
2796                         $this->t->set_var('interviewer_name',$interviewer_name);
2797                         $this->t->fp('int_list','interviewer_list',True);
2798                 }
2799
2800                 if($action == 'save') {
2801                         $notes = get_var('notes',array('POST'));
2802                         $this->db->query("UPDATE tc_interview set " .
2803                                          "   interview='" . $interview . "'" .
2804                                          ", interviewer='" . $interviewer . "'" .
2805                                          ", individual='" . $indiv . "'" .
2806                                          ", date='" . $date . "'" .
2807                                          ", notes='" . $notes . "'" .
2808                                          ", interview_type='" . $interview_type . "'" .
2809                                          " WHERE interview=" . $interview,__LINE__,__FILE__);
2810                         $this->int_view();
2811                         return false;
2812                 }
2813
2814                 if($action == 'insert') {
2815                         $notes = get_var('notes',array('POST'));
2816                         $this->db->query("INSERT INTO tc_interview (interviewer,individual,date,notes,interview_type) " .
2817                                          "VALUES ('" . $interviewer . "','" . $indiv . "','" .
2818                                          $date . "','" . $notes ."','" . $interview_type . "')",__LINE__,__FILE__);
2819                         $this->int_view();
2820                         return false;
2821                 }
2822
2823                 if($action == 'add') {
2824                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2825                         $this->t->set_var('interview', '');
2826                         $this->t->set_var('interviewer', $interviewer);
2827                         $this->t->set_var('name',$name);
2828                         $this->t->set_var('indiv',$indiv);
2829                         $this->t->set_var('date','');
2830                         $this->t->set_var('notes','');
2831                         $this->t->set_var('lang_done','Cancel');
2832                         $this->t->set_var('lang_action','Adding New Interview');
2833                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
2834                                           $interview . '&action=' . 'insert'));
2835                 }
2836
2837                 if($action == 'edit' || $action == 'view') {
2838                         $sql = "SELECT * FROM tc_interview WHERE interview=".$interview;
2839                         $this->db->query($sql,__LINE__,__FILE__);
2840                         $this->db->next_record();
2841                         $this->t->set_var('interview',$interview);
2842                         $this->t->set_var('name',$name);
2843                         $this->t->set_var('interviewer', $this->db->f('interviewer'));
2844                         $this->t->set_var('indiv',$this->db->f('individual'));
2845                         $this->t->set_var('date',$this->db->f('date'));
2846                         $this->t->set_var('notes',$this->db->f('notes'));
2847                         if($this->db->f('interview_type') == 1) { $this->t->set_var('interview_type_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=150; $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]['individual'];
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                                         // Update our location
3572                                         if($location == "") {
3573                                                 if($family > 0) {
3574                                                         $family_name_array = explode(",", $familyid2name[$family]);
3575                                                         $family_last_name = $family_name_array[0];
3576                                                         $family_address = $familyid2address[$family];
3577                                                         $location = "$family_last_name"." home ($family_address)";
3578                                                 } else if($indiv > 0) {
3579                                                         $supervisor_name_array = explode(",",$presidency2name[$presidency]);
3580                                                         $supervisor_last_name = $supervisor_name_array[0];
3581                                                         $sql = "SELECT * FROM tc_individual where individual='$presidency2indiv[$presidency]'";
3582                                                         $this->db2->query($sql,__LINE__,__FILE__);
3583                                                         if($this->db2->next_record()) {
3584                                                                 $mls_id = $this->db2->f('mls_id');
3585                                                         }
3586                                                         $sql = "SELECT * FROM tc_individual where mls_id='$mls_id'";
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                                                         $mls_id = $this->db2->f('mls_id');
3709                                                 }
3710                                                 $sql = "SELECT * FROM tc_individual where mls_id='$mls_id'";
3711                                                 $this->db2->query($sql,__LINE__,__FILE__);
3712                                                 if($this->db2->next_record()) {
3713                                                         $supervisor_address = $this->db2->f('address');
3714                                                 }
3715                                                 $location = "$supervisor_last_name"." home ($supervisor_address)";
3716                                         }
3717                                 }
3718
3719                                 $date = $this->db->f('date');
3720                                 $date_array = explode("-",$date);
3721                                 $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
3722                                 $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
3723
3724                                 $time = $this->db->f('time');
3725                                 $time_array = explode(":",$time);
3726                                 $hour = $time_array[0];
3727                                 $minute = $time_array[1];
3728                                 $pm = 0;
3729                                 if($hour > 12) { $pm=1; $hour = $hour - 12; }
3730                                 $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
3731
3732                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3733
3734                                 // Date selection
3735                                 $table_data.= '<td align=left>';
3736                                 $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
3737                                 $table_data.= '</td>';
3738
3739                                 // Hour & Minutes selection
3740                                 $table_data.= "<td align=center>";
3741                                 $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
3742                                 $table_data.= "</td>";
3743
3744                                 // individual drop down list (for PPIs)
3745                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][indiv] STYLE="font-size : 8pt">';
3746                                 $table_data.= '<option value=0></option>';  
3747                                 for ($j=0; $j < count($individual); $j++) {
3748                                         $id = $individual[$j];
3749                                         $name = $indiv_name[$j];
3750                                         if($individual[$j] == $indiv) {
3751                                                 $selected[$id] = 'selected="selected"'; 
3752                                         } else {
3753                                                 $selected[$id] = ''; 
3754                                         }
3755                                         $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
3756                                 }
3757                                 $table_data.='</select></td>';
3758
3759                                 // Family drop down list (for Visits)
3760                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family] STYLE="font-size : 8pt">';
3761                                 $table_data.= '<option value=0></option>';          
3762                                 for ($j=0; $j < count($individual); $j++) {
3763                                         $id = $family_id[$j];
3764                                         $name = $family_name[$j];
3765                                         if($family_id[$j] == $family) { 
3766                                                 $selected[$id] = 'selected="selected"'; 
3767                                         } else { 
3768                                                 $selected[$id] = ''; 
3769                                         }
3770                                         $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
3771                                 }
3772                                 $table_data.='</select></td>';
3773
3774                                 // Location text box
3775                                 $table_data.= '<td align=center><input type=text size="25" maxlength="120" ';
3776                                 $table_data.= 'name="sched['.$presidency.']['.$appointment.'][location]" value="'.$location.'" STYLE="font-size : 8pt">';
3777
3778                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3779                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3780
3781                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3782                                 $this->t->set_var('tr_color',$tr_color);
3783                         }
3784
3785                         // Create blank appointment slot
3786                         for ($b=0; $b < 4; $b++) {
3787                                 $appointment = $this->max_appointments + $b;
3788                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
3789
3790                                 // Date selection
3791                                 $table_data.= '<td align=left>';
3792                                 $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
3793                                 $table_data.= '</td>';
3794
3795                                 // Time selection
3796                                 $table_data.= "<td align=center>";
3797                                 $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
3798                                 $table_data.= "</td>";
3799
3800                                 // individual drop down list
3801                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][indiv] STYLE="font-size : 8pt">';
3802                                 $table_data.= '<option value=0></option>';  
3803                                 for ($j=0; $j < count($individual); $j++) {
3804                                         $id = $individual[$j];
3805                                         $name = $indiv_name[$j];
3806                                         $table_data.= '<option value='.$id.'>'.$name.'</option>';
3807                                 }
3808                                 $table_data.='</select></td>';
3809
3810                                 // Family drop down list
3811                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family] STYLE="font-size : 8pt">';
3812                                 $table_data.= '<option value=0></option>';          
3813                                 for ($j=0; $j < count($individual); $j++) {
3814                                         $id = $family_id[$j];
3815                                         $name = $family_name[$j];
3816                                         $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
3817                                 }
3818                                 $table_data.='</select></td>';
3819
3820                                 // Location text box
3821                                 $table_data.= '<td align=center><input type=text size="25" maxlength="120" ';
3822                                 $table_data.= 'name="sched['.$presidency.']['.$appointment.'][location]" value="" STYLE="font-size : 8pt">';
3823
3824                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
3825                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
3826
3827                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3828                                 $this->t->set_var('tr_color',$tr_color);
3829                         }
3830
3831                         $this->t->set_var('table_data',$table_data);
3832                         $this->t->set_var('header_row',$header_row);
3833                         $this->t->set_var('table_width',$table_width);
3834                         $this->t->fp('list','presidency_list',True);
3835                 }
3836
3837                 $this->t->pfp('out','sched_t');
3838                 $this->save_sessiondata();   
3839         }
3840
3841         function email()
3842         {
3843                 $this->t->set_file(array('email_t' => 'email.tpl'));
3844                 $this->t->set_block('email_t','individual_list','list');
3845
3846                 $action = get_var('action',array('GET','POST'));
3847
3848                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email'));
3849                 $this->t->set_var('title','Email Tool');
3850
3851                 $this->t->set_var('lang_email','Send Email');
3852                 $this->t->set_var('lang_reset','Cancel');
3853
3854                 $this->t->set_var('email_member_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=member'));
3855                 $this->t->set_var('email_member_link_title','Email Quorum Member');
3856
3857                 $this->t->set_var('email_quorum_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=quorum'));
3858                 $this->t->set_var('email_quorum_link_title','Email Quorum');
3859
3860                 $this->t->set_var('email_reminder_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=reminder'));
3861                 $this->t->set_var('email_reminder_link_title','Email Reminders');
3862
3863                 $this->t->set_var('email_edit_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=edit'));
3864                 $this->t->set_var('email_edit_link_title','Edit Email Addresses');
3865
3866                 $table_width=600;
3867                 $this->t->set_var('table_width',$table_width);
3868
3869                 $this->t->pfp('out','email_t');
3870                 $this->save_sessiondata();   
3871         }
3872
3873         function admin()
3874         {
3875                 $this->t->set_file(array('admin_t' => 'admin.tpl'));
3876                 $this->t->set_block('admin_t','upload','uploadhandle');
3877                 $this->t->set_block('admin_t','admin','adminhandle');
3878                 $this->t->set_block('admin_t','cmd','cmdhandle');
3879                 $this->t->set_block('admin_t','presidency','presidencyhandle');
3880
3881                 $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.admin&action=upload'));
3882                 $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.admin&action=presidency'));
3883
3884                 $action = get_var('action',array('GET','POST'));
3885
3886                 $this->t->pfp('out','admin_t');
3887
3888                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY individual ASC";
3889                 $this->db->query($sql,__LINE__,__FILE__);
3890                 $i=0;
3891                 while ($this->db->next_record()) {
3892                         $individual[$i] = $this->db->f('individual');
3893                         $indiv_name[$i] = $this->db->f('name');
3894                         $indiv2name[$individual[$i]] = $indiv_name[$i];
3895                         $i++;
3896                 }
3897                 array_multisort($indiv_name, $individual);
3898
3899                 if($action == 'upload') {
3900                         $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
3901
3902                         if(($_FILES['uploadedfile']['type'] == "application/zip") ||
3903                            ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
3904                            ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
3905                            ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
3906
3907                                 if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
3908                                         $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
3909                                         $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
3910                                         $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3911                                         $uploadstatus.= "Tmp Filename : " . $_FILES['uploadedfile']['tmp_name'] . "<br>";
3912                                         $uploadstatus.= "Filename     : " . $_FILES['uploadedfile']['name'] . "<br>";
3913                                         $uploadstatus.= "Type         : " . $_FILES['uploadedfile']['type'] . "<br>";
3914                                         $uploadstatus.= "Size         : " . $_FILES['uploadedfile']['size'] . "<br>";
3915                                         $uploadstatus.= "Error        : " . $_FILES['uploadedfile']['error'] . "<br>";   
3916                                         $this->t->set_var('uploadstatus',$uploadstatus);
3917                                         $this->t->pfp('uploadhandle','upload',True);
3918                                         return 0;
3919                                 }
3920
3921                                 $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
3922                                 $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
3923                                 $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
3924                                 $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";        
3925                                 $this->t->set_var('uploadstatus',$uploadstatus);
3926                                 $this->t->pfp('uploadhandle','upload');
3927                                 $this->t->set_var('uploadhandle','');
3928                                 print "<table border=1 width=80%><tr><td>\n<pre>";
3929
3930                                 # make a directory for this data to be stored in
3931                                 $date="data_" . date("Y_m_d");
3932                                 $data_dir = $this->upload_target_path . '/' . $date;
3933                                 print "-> Making the data directory: $date<br>\n";
3934                                 exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
3935                                 if($return_code != 0) {
3936                                         print implode('\n',$result) . "<br>";
3937                                         print "<b><font color=red>";
3938                                         print "-E- Unable to create the data directory. Aborting import.";
3939                                         print "</font></b>";
3940                                         return 0;
3941                                 }
3942
3943                                 # move the file uploaded into this directory
3944                                 print "-> Moving the uploaded file into the data dir<br>\n";
3945                                 exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
3946                                 if($return_code != 0) {
3947                                         print implode('\n',$result) . "<br>";
3948                                         print "<b><font color=red>";
3949                                         print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
3950                                         print "</font></b>";
3951                                         return 0;
3952                                 }
3953
3954                                 # unzip the data into this directory
3955                                 print "-> Unzipping the data<br>\n";
3956                                 exec($this->unzip_path .' -u '. $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
3957                                 if($return_code != 0) {
3958                                         print implode('\n',$result) . "<br>";
3959                                         print "<b><font color=red>";
3960                                         print "-E- Unable to unzip the uploaded file into the data dir: $data_dir. Aborting import.";
3961                                         print "</font></b>";
3962                                         return 0;
3963                                 }
3964                                 exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
3965
3966                                 # update the data_latest link to point to this new directory
3967                                 print "-> Updating the latest data dir link<br>\n";
3968                                 $data_latest = $this->upload_target_path . '/data_latest';
3969                                 exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
3970                                 if($return_code != 0) {
3971                                         print implode('\n',$result) . "<br>";
3972                                         print "<b><font color=red>";
3973                                         print "-E- Unable to update the data latest link. Aborting import.";
3974                                         print "</font></b>";
3975                                         return 0;
3976                                 }
3977
3978                                 # run the import perl script to encorporate it into the DB
3979                                 ob_start('ob_logstdout', 2);
3980                                 print "-> Importing the data into the database<br>\n";
3981                                 ob_flush(); flush(); sleep(1);
3982                                 $import_log = $this->upload_target_path . '/import.log';
3983                                 $data_log = $this->upload_target_path . '/data.log';
3984                                 $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
3985                                 $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
3986                                 #print "import_cmd: $import_cmd<br>";
3987                                 #print "parse_cmd: $parse_cmd<br>";
3988                                 ob_start('ob_logstdout', 2);
3989                                 passthru($import_cmd);
3990                                 passthru($parse_cmd);
3991                                 ob_flush(); flush(); sleep(1);
3992
3993                                 # fix the permissions of the data dir
3994                                 exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
3995
3996                                 $this->t->pfp('cmdhandle','cmd');
3997                                 print "</pre></td></tr></table>";
3998                         } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
3999                                   ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
4000                                   ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
4001                                   ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
4002                                 $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
4003                                 $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
4004                                 $this->t->set_var('uploadstatus',$uploadstatus);
4005                                 $this->t->pfp('uploadhandle','upload',True);
4006                         } else {
4007                                 $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
4008                                 $uploadstatus.= ") uploading the file, please try again! </font></b>";
4009                                 $this->t->set_var('uploadstatus',$uploadstatus);
4010                                 $this->t->pfp('uploadhandle','upload',True);
4011                         }
4012                 } else if($action == "presidency") {
4013                         $new_data = get_var('eqpres',array('POST'));
4014                         foreach ($new_data as $entry) {
4015                                 $id = $entry['id'];
4016                                 $email = $entry['email'];
4017                                 $indiv = $entry['indiv'];
4018                                 $name = $entry['name'];
4019                                 $district = $entry['district'];
4020                                 $president = $entry['president'];
4021                                 $counselor = $entry['counselor'];
4022                                 $secretary = $entry['secretary'];
4023                                 // look up the individual name for the ID
4024                                 $name = $indiv2name[$indiv]; 
4025                                 //print "id=$id indiv=$indiv name=$name email=$email district=$district president=$president ";
4026                                 //print "counselor=$counselor secretary=$secretary<br>";
4027
4028                                 if(($indiv > 0) || ($name != "")) {
4029                                         if($id < $this->max_presidency_members) {
4030                                                 //print "Updating Existing Entry<br>";
4031                                                 $this->db2->query("UPDATE tc_presidency set" .
4032                                                                   " individual=" . $indiv . 
4033                                                                   " ,district=" . $district . 
4034                                                                   " ,email='" . $email . "'" .
4035                                                                   " ,president='" . $president . "'" .
4036                                                                   " ,counselor='" . $counselor . "'" .
4037                                                                   " ,secretary='" . $secretary . "'" .
4038                                                                   " WHERE presidency=" . $id,__LINE__,__FILE__);
4039                                         } else {
4040                                                 //print "Adding New Entry<br>";
4041                                                 $this->db2->query("INSERT INTO tc_presidency (presidency,individual,district," .
4042                                                                   "email,president,counselor,secretary,valid) " .
4043                                                                   "VALUES (NULL,'" . $indiv . "','" . $district . "','" .
4044                                                                   $email . "','" . $president  . "','" .
4045                                                                   $counselor . "','" . $secretary . "','1'" .
4046                                                                   ")",__LINE__,__FILE__);
4047                                         }
4048                                 } else {
4049                                         //print "Ignoring Blank Entry<br>";
4050                                 }
4051                         }
4052
4053                         // Now update the tc_district table appropriately
4054
4055                         // Delete all the previous district entries from the table
4056                         $this->db->query("DELETE from tc_district where valid=1",__LINE__,__FILE__);
4057                         $this->db->query("DELETE from tc_district where valid=0",__LINE__,__FILE__);
4058
4059                         // Always add a "District 0" assigned to the High Priests Group
4060                         $district = 0;
4061                         $name = "High Priests";
4062                         $indiv = 0;
4063                         $valid = 0;
4064                         $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
4065                                           "VALUES ('" . $district . "','" . 
4066                                           $indiv . "','" . $valid . "'" .
4067                                           ")",__LINE__,__FILE__);
4068
4069                         // Requery the tc_presidency table
4070                         $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
4071                         $this->db->query($sql,__LINE__,__FILE__);
4072                         while ($this->db->next_record()) {
4073                                 // Extract the data for each presidency record
4074                                 $id = $this->db->f('presidency');
4075                                 $indiv = $this->db->f('individual');
4076                                 $name = $this->db->f('name');
4077                                 $district = $this->db->f('district');
4078                                 $valid = 1;
4079
4080                                 // If we have a valid district, add it to the district table
4081                                 if($district > 0) {
4082                                         $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
4083                                                           "VALUES ('" . $district . "','" . 
4084                                                           $indiv . "','" . $valid . "'" .
4085                                                           ")",__LINE__,__FILE__);
4086                                 }
4087                         }
4088
4089                         $this->t->set_var('adminhandle','');
4090                         $this->t->pfp('adminhandle','admin'); 
4091                 }
4092                 else
4093                 {
4094                         $this->t->set_var('adminhandle','');
4095                         $this->t->pfp('adminhandle','admin'); 
4096                 }
4097
4098                 // Now save off the data needed for a Presidency Table Update
4099
4100                 $sql = "SELECT tp.*, ti.name FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
4101                 $this->db->query($sql,__LINE__,__FILE__);
4102                 $table_data = "";
4103                 $header_row = "<th>Individual</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th>";
4104                 while ($this->db->next_record()) {
4105                         // Extract the data for each presidency record
4106                         $id = $this->db->f('presidency');
4107                         $indiv = $this->db->f('individual');
4108                         $district = $this->db->f('district');
4109                         $name = $this->db->f('name');
4110                         $email = $this->db->f('email');
4111                         $president = $this->db->f('president');
4112                         $counselor = $this->db->f('counselor');
4113                         $secretary = $this->db->f('secretary');
4114
4115                         // Create the forms needed in the table
4116                         $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4117
4118                         // Presidency ID
4119                         $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4120
4121                         // individual
4122                         if($eqpresidency == 0) {
4123                                 $table_data.= '<td align=center><select name="eqpres['.$id.'][indiv]">';
4124                                 $table_data.= '<option value=0></option>';  
4125                                 for ($j=0; $j < count($individual); $j++) {
4126                                         $tmp_id = $individual[$j];
4127                                         $name = $indiv_name[$j];
4128                                         if($individual[$j] == $indiv) { 
4129                                                 $indivname = $name; 
4130                                                 $selected = 'selected="selected"'; 
4131                                         } else { 
4132                                                 $selected = ''; 
4133                                         }
4134                                         $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4135                                 }
4136                                 $table_data.='</select></td>';
4137                                 $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$indivname.'">';
4138                         } else {
4139                                 $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="Presidency"></td>';
4140                                 $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="Presidency">';
4141                         }
4142
4143                         // Email Address
4144                         $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4145
4146                         // District
4147                         $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4148                         $table_data.= '<option value=0></option>';
4149                         for ($j=0; $j <= $this->max_num_districts; $j++) {
4150                                 if($district == $j) { 
4151                                         $selected = 'selected="selected"'; 
4152                                 } else { 
4153                                         $selected = ''; 
4154                                 }
4155                                 $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4156                         }
4157                         $table_data.='</select></td>';
4158
4159                         // President
4160                         $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4161                         if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4162                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4163                         $table_data.='</select></td>';
4164
4165                         // Counselor
4166                         $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4167                         if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4168                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4169                         $table_data.='</select></td>';
4170
4171                         // Secretary
4172                         $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4173                         if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4174                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4175                         $table_data.='</select></td>';
4176
4177                         // End of ROW
4178                         $table_data .= "</tr>\n";
4179                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4180                         $this->t->set_var('tr_color',$tr_color);
4181                 }
4182
4183                 // Now create 1 blank row to always have a line available to add a new individual with
4184                 $id = $this->max_presidency_members;
4185                 $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4186                 // Presidency ID
4187                 $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4188                 // individual
4189                 $table_data.= '<td align=center><select name="eqpres['.$id.'][indiv]">';
4190                 $table_data.= '<option value=0></option>';  
4191                 for ($j=0; $j < count($individual); $j++) {
4192                         $tmp_id = $individual[$j];
4193                         $name = $indiv_name[$j];
4194                         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4195                 }
4196                 $table_data.='</select></td>';
4197                 $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4198                 // Email Address
4199                 $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4200                 // District
4201                 $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4202                 $table_data.= '<option value=0></option>';
4203                 for ($j=0; $j <= $this->max_num_districts; $j++) {
4204                         if($j == 0) { 
4205                                 $selected = 'selected="selected"'; 
4206                         } else { 
4207                                 $selected = ''; 
4208                         }
4209                         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4210                 }
4211                 $table_data.='</select></td>';
4212                 // President
4213                 $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4214                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4215                 $table_data.='</select></td>';
4216                 // Counselor
4217                 $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4218                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4219                 $table_data.='</select></td>';
4220                 // Secretary
4221                 $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4222                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4223                 $table_data.='</select></td>';
4224                 // End of ROW
4225                 $table_data .= "</tr>\n";
4226                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4227                 $this->t->set_var('tr_color',$tr_color);
4228
4229                 $this->t->set_var('header_row',$header_row);
4230                 $this->t->set_var('table_data',$table_data);
4231                 $this->t->pfp('presidencyhandle','presidency',True);
4232
4233                 $this->save_sessiondata();   
4234         }
4235
4236         function email_appt($appointment)
4237         {
4238                 //print "Emailing notification of appointment: $appointment <br>";
4239
4240                 $sql = "SELECT * FROM tc_appointment where appointment='$appointment'";
4241                 $this->db->query($sql,__LINE__,__FILE__);
4242
4243                 while ($this->db->next_record()) {
4244                         $appointment = $this->db->f('appointment');
4245                         $presidency = $this->db->f('presidency');
4246                         $location = $this->db->f('location');
4247                         $interviewer = "";
4248                         $email = "";
4249                         $indiv = $this->db->f('individual');
4250                         $indiv_name = "";
4251                         $family = $this->db->f('family');
4252                         $family_name = "";
4253                         $appt_name = "";
4254                         $phone = "";
4255                         $uid = $this->db->f('uid');
4256
4257                         // Extract the year, month, day, hours, minutes, seconds from the appointment time
4258                         $appt_date = $this->db->f('date');
4259                         $date_array = explode("-",$appt_date);
4260                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4261                         $appt_time = $this->db->f('time');
4262                         $time_array = explode(":",$appt_time);
4263                         $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4264
4265                         // Format the appointment time into an iCal UTC equivalent
4266                         $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4267                         $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4268                         $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4269
4270                         $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.presidency='$presidency'";
4271                         $this->db2->query($sql,__LINE__,__FILE__);
4272                         if($this->db2->next_record()) {
4273                                 $email = $this->db2->f('email');
4274                                 $interviewer = $this->db2->f('name');
4275                         }
4276
4277                         // Set the email address of the interviewer
4278                         $from = $email;
4279
4280                         if($indiv > 0) {
4281                                 $sql = "SELECT * FROM tc_individual where individual='$indiv'";
4282                                 $this->db2->query($sql,__LINE__,__FILE__);
4283                                 if($this->db2->next_record()) {
4284                                         $indiv_name = $this->db2->f('name');
4285                                         $phone = $this->db2->f('phone');
4286                                         $appt_name = $indiv_name . " Interview";
4287                                         $duration = $this->default_ppi_appt_duration * 60;
4288                                 }
4289                         }
4290
4291                         if($family > 0) {
4292                                 $sql = "SELECT * FROM tc_family WHERE family='$family'";
4293                                 $this->db2->query($sql,__LINE__,__FILE__);
4294                                 if($this->db2->next_record()) {
4295                                         $individual = $this->db2->f('individual');
4296                                         $sql = "SELECT * FROM tc_individual where individual='$individual'";
4297                                         $this->db3->query($sql,__LINE__,__FILE__);
4298                                         if($this->db3->next_record()) {
4299                                                 $phone = $this->db3->f('phone');
4300                                                 $family_name = $this->db3->f('name');
4301                                                 $phone = $this->db3->f('phone');
4302                                         }
4303                                         $appt_name = $family_name . " Family Visit";
4304                                         $duration = $this->default_visit_appt_duration * 60;
4305                                 }
4306                         }
4307
4308                         $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4309                         $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4310                         $date = $dtstartstr . "-" . $dtendstr;
4311                         $description = "$appt_name : $phone";
4312
4313                         if(($uid == 0) && ($appt_name != "")) {
4314                                 // Create a new calendar item for this appointment, since this must be the first time we
4315                                 // are sending it out.
4316                                 print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4317                                 $uid = rand() . rand(); // Generate a random identifier for this appointment
4318                                 $subject = "Created: $appt_name";
4319
4320                                 $this->db->query("UPDATE tc_appointment set" .
4321                                                  " uid=" . $uid . 
4322                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4323
4324                                 $action = "PUBLISH";
4325                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4326                                                       $dtend, $date, $location, $appt_name, $description, $uid);
4327                         } else if(($uid != 0) && ($appt_name == "")) {
4328                                 // Remove the calendar item for this appointment since it has already been sent
4329                                 // and there is no name we have changed it to.
4330                                 print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4331                                 $subject = "Canceled: $appt_date $appt_time";
4332
4333                                 $this->db->query("UPDATE tc_appointment set" .
4334                                                  " uid=0" . 
4335                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4336
4337                                 $action = "CANCEL";
4338                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4339                                                       $dtend, $date, $location, $subject, $subject, $uid);
4340                         } else if($uid != 0) {
4341                                 // Update the existing appointment since we have changed it
4342                                 print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4343
4344                                 $subject = "Canceled: $appt_date $appt_time";
4345                                 $action = "CANCEL";
4346                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4347                                 $dtend, $date, $location, $subject, $subject, $uid);
4348
4349                                 $uid = rand() . rand(); // Generate a random identifier for this appointment
4350                                 $this->db->query("UPDATE tc_appointment set" .
4351                                                  " uid=" . $uid .
4352                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4353
4354                                 $subject = "Updated: $appt_name";
4355                                 $action = "PUBLISH";
4356                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4357                                                       $dtend, $date, $location, $appt_name, $description, $uid);
4358                         }
4359                 }
4360                 return true;
4361         }
4362
4363         function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4364         {
4365                 // Initialize our local variables
4366                 $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4367                 $message = "";
4368                 $headers = "";
4369
4370                 // Form the headers for the email message
4371                 $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4372                 $headers.="Mime-Version: 1.0\n";
4373                 $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4374                 $headers.="Content-Disposition: inline\n";
4375                 $headers.="Reply-To: $from\n";
4376                 $headers.="From: $from\n";
4377
4378                 // Print the plaintext version of the appointment
4379                 $message.="--$boundary\n";
4380                 $message.="Content-Type: text/plain; charset=us-ascii\n";
4381                 $message.="Content-Disposition: inline\n";
4382                 $message.="\n";
4383                 $message.="What: $description\n";
4384                 $message.="When: $date\n";
4385                 $message.="Where: $location\n";
4386                 $message.="\n";
4387
4388                 // Print the .ics attachment version of the appointment
4389                 $message.="--$boundary\n";
4390                 $message.="Content-Type: text/calendar; charset=us-ascii\n";
4391                 $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4392                 $message.="\n";
4393                 $message.="BEGIN:VCALENDAR" . "\n";
4394                 $message.="VERSION:2.0" . "\n";
4395                 $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4396                 $message.="METHOD:$action" . "\n";
4397                 $message.="BEGIN:VEVENT" . "\n";
4398                 $message.="ORGANIZER:MAILTO:$from". "\n";
4399                 $message.="DTSTAMP:$dtstamp" . "\n";
4400                 $message.="DTSTART:$dtstart" . "\n";
4401                 $message.="DTEND:$dtend" . "\n";
4402                 $message.="SUMMARY:$summary" . "\n";
4403                 $message.="DESCRIPTION:$description" . "\n";
4404                 $message.="LOCATION:$location" . "\n";
4405                 $message.="UID:$uid" ."\n";
4406                 $message.="TRANSP:OPAQUE" . "\n";
4407                 $message.="SEQUENCE:0" . "\n";
4408                 $message.="CLASS:PUBLIC" . "\n";
4409                 $message.="END:VEVENT" . "\n";
4410                 $message.="END:VCALENDAR" . "\n";
4411
4412                 // Complete the message
4413                 $message.="--$boundary\n";
4414
4415                 // Send the message
4416                 mail($to, $subject, $message, $headers);
4417         }
4418
4419         function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4420         {
4421                 $form_data = "";
4422                 $blank = 0;
4423
4424                 if($hour == 0) { $blank = 1; }
4425
4426                 if($this->time_drop_down_lists == 1) {
4427                         // Create drop down lists to get the time
4428                         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4429                         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4430                         foreach(range(1,12) as $num) {
4431                                 if($hour == $num) { 
4432                                         $selected = 'selected="selected"'; 
4433                                 } else { 
4434                                         $selected = ''; 
4435                                 }
4436                                 $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4437                         }
4438                         $form_data.= '</select>';
4439                         $form_data.= '&nbsp;:&nbsp;';
4440                         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4441                         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4442                         $num = 0;
4443                         while($num < 60) {
4444                                 if($num < 10) { $num = "0" . "$num"; }
4445                                 if($minute == $num) { 
4446                                         $selected = 'selected="selected"'; 
4447                                 } else { 
4448                                         $selected = ''; 
4449                                 }
4450                                 if($blank == 1) { $selected = ""; }
4451                                 $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4452                                 $num = $num + $this->time_drop_down_list_inc;
4453                         }
4454                         $form_data.= '</select>';
4455                 } else {
4456                         // Use free form text fields to get the time
4457                         if($blank == 1) { 
4458                                 $hour = ""; 
4459                                 $minute = ""; 
4460                                 $ampm = ""; 
4461                         }
4462                         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4463                         $form_data.= ':';
4464                         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4465                         $form_data.= '&nbsp;';
4466                 }
4467                 // Always use a drop-down select form for am/pm
4468                 $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4469                 if($blank == 0) {
4470                         if($pm == 0) { 
4471                                 $form_data.= '<option value=0 selected>am</option>'; 
4472                                 $form_data.= '<option value=1>pm</option>'; 
4473                         }
4474                         if($pm == 1) { 
4475                                 $form_data.= '<option value=0>am</option>'; 
4476                                 $form_data.= '<option value=1 selected>pm</option>'; 
4477                         }
4478                 } else {
4479                         $form_data.= '<option value=""></option>';
4480                         $form_data.= '<option value=0>am</option>';
4481                         $form_data.= '<option value=1>pm</option>';
4482                 }
4483                 $form_data.= '</select>';
4484
4485                 return $form_data;
4486         }
4487 }
4488
4489 ?>