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