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