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