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