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