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