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