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