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