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