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