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