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