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