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