9684e8e114691f1970584e306b2a62e9cd3db97d
[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                 $indiv_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $ppi_date_width=20;
1695                 $table_width=$indiv_width + $phone_width + $pri_width + $notes_width + $ppi_date_width;
1696                 $header_row = "<th width=$indiv_width><font size=-2>individual Name</th>";
1697                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
1698                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
1699                 $header_row.= "<th width=$ppi_date_width><font size=-2>Last PPI</th>";
1700                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
1701                 $table_data=""; $completed_data=""; $totals_data="";
1702
1703                 $year = date('Y');
1704                 $month = date('m');
1705                 $period = intval(($month-1)/$this->ppi_frequency) + 1;
1706                 $start_of_period = ($period-1)*$this->ppi_frequency + 1;
1707                 $end_of_period = $period * $this->ppi_frequency;
1708
1709                 if($action == 'save') {
1710                         // Save any changes made to the appointment table
1711                         $new_data = get_var('appt_notes',array('POST'));
1712                         if($new_data != "") {
1713                                 foreach ($new_data as $entry) {
1714                                         $indiv = $entry['individual'];
1715                                         $appointment = $entry['appointment'];
1716                                         $location = $entry['location'];
1717                                     $presidency_location = $entry['presidency_location'];
1718                                     if($location == "") { $location = $presidency_location; }
1719                                         if($indiv == 0) { $location = ""; }
1720
1721                                         //Only perform a database update if we have made a change to this appointment
1722                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and individual='$indiv' and location='$location'";
1723                                         $this->db->query($sql,__LINE__,__FILE__);
1724                                         if(!$this->db->next_record()) {
1725                                                 // Perform database save actions here
1726                                                 $this->db->query("UPDATE tc_appointment set " .
1727                                                                  " individual='" . $indiv . "'" .
1728                                                                  ",location='" . $location . "'" .
1729                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
1730                                                 // Email the appointment
1731                                                 $this->email_appt($appointment);
1732                                         }
1733                                 }
1734                         }
1735
1736                         // Save any changes made to the ppi notes table
1737                         $new_data = get_var('notes',array('POST'));
1738                         foreach ($new_data as $entry) {
1739                                 $notes = $entry['notes'];
1740                                 $individual = $entry['individual'];
1741                                 $priority = $entry['pri'];
1742
1743                                 // Perform database save actions here
1744                                 $sql = "SELECT * FROM tc_individual WHERE individual='$individual'";
1745                                 $this->db->query($sql,__LINE__,__FILE__);
1746                                 if ($this->db->next_record()) {
1747                                         $scheduling_priority = $this->db->f('scheduling_priority');
1748                                         //$this->logToFile("ppi_sched", "UPDATE tc_scheduling_priority SET priority='$priority', notes=\"$notes\" WHERE scheduling_priority='$scheduling_priority'");
1749                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$priority', notes=\"$notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
1750                                 }
1751                         }
1752
1753                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched');
1754                         //Header('Location: ' . $take_me_to_url);
1755                 }
1756
1757                 // create the individual id -> individual name mapping
1758                 $sql = "SELECT * FROM tc_individual where valid=1 and steward='$this->default_stewardship' ORDER BY name ASC";
1759                 $this->db->query($sql,__LINE__,__FILE__);
1760                 $i=0;
1761                 $individual = NULL;
1762                 $indiv_name = NULL;
1763                 while ($this->db->next_record()) {
1764                         $indiv_name[$i] = $this->db->f('name');
1765                         $individual[$i] = $this->db->f('individual');
1766                         $i++;
1767                 }
1768                 array_multisort($indiv_name, $individual);
1769
1770                 // APPOINTMENT TABLE
1771                 $date_width=250; $time_width=100; $indiv_width=200; $location_width=100;
1772                 $appt_table_width=$date_width + $time_width + $indiv_width + $location_width;
1773                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
1774                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
1775                 $appt_header_row.= "<th width=$indiv_width><font size=-2>Individual</th>";
1776                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
1777                 $appt_table_data = "";
1778                 $table_data="";
1779
1780                 $total_indivs=0; $indivs_with_yearly_ppi=0;
1781
1782                 // Get the President
1783                 $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti where tp.individual=ti.individual AND tp.valid=1 AND ";
1784                 if($this->yearly_ppi_interviewer == 1) { $sql .= " (tp.president=1)"; }
1785                 if($this->yearly_ppi_interviewer == 2) { $sql .= " (tp.president=1 OR tp.counselor=1)"; }
1786                 if($this->yearly_ppi_interviewer == 3) { $sql .= " (tp.president=1 OR tp.counselor=1 OR tp.secretary=1)"; }
1787                 $this->db->query($sql,__LINE__,__FILE__);
1788                 while ($this->db->next_record()) {
1789                   $presidency_name = $this->db->f('name');
1790                   $presidency_name_array = explode(",",$presidency_name);
1791                   $presidency_last_name = $presidency_name_array[0];
1792                   $presidency_id = $this->db->f('presidency');
1793                   $presidency_address = $this->db->f('address');
1794                   $presidency_location = "$presidency_last_name"." home ($presidency_address)";
1795                   $appt_table_data = "";
1796
1797                   // Display a scheduling table for this presidency member
1798                   $district_number = '*';
1799                   $district_name = $presidency_name;
1800                   $not_completed_table_title = "District ".$district_number.": ".$district_name.": All indivs with " . $this->ppi_frequency_label . " PPI Not Completed";
1801                   $appt_table_title = "District ".$district_number.": ".$district_name.": ".$this->ppi_frequency_label." PPI Appointment Slots";
1802                   $this->t->set_var('not_completed_table_title',$not_completed_table_title);
1803                   $this->t->set_var('appt_table_title',$appt_table_title);
1804
1805                   // query the database for all the appointments
1806                   $sql = "SELECT * FROM tc_appointment where presidency=".$presidency_id." and date>=CURDATE() ORDER BY date ASC, time ASC";
1807                   $this->db2->query($sql,__LINE__,__FILE__);
1808
1809                   while ($this->db2->next_record()) {
1810                         $appointment = $this->db2->f('appointment');
1811                         $indiv = $this->db2->f('individual');
1812                         $location = $this->db2->f('location');
1813                         if(($location == "") && ($indiv > 0)) { $location = $presidency_location; }
1814
1815                         $date = $this->db2->f('date');
1816                         $date_array = explode("-",$date);
1817                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
1818                         $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
1819
1820                         $time = $this->db2->f('time');
1821                         $time_array = explode(":",$time);
1822                         $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
1823
1824                         $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1825                         $appt_table_data.= "<td align=center>$day_string</td>";
1826                         $appt_table_data.= "<td align=center>$time_string</td>";
1827
1828                         $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][individual]>';
1829                         $appt_table_data.= '<option value=0></option>';
1830                         for ($i=0; $i < count($individual); $i++) {
1831                           $id = $individual[$i];
1832                           $name = $indiv_name[$i];
1833                           if($individual[$i] == $indiv) { 
1834                                 $selected[$id] = 'selected="selected"'; 
1835                           } else { 
1836                                 $selected[$id] = ''; 
1837                           }
1838                           $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
1839                         }
1840                         $appt_table_data.='</select></td>';
1841
1842                         $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
1843                         $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
1844
1845                         $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
1846
1847                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1848                         $this->t->set_var('tr_color',$tr_color);
1849                   }
1850                   $this->t->set_var('appt_table_data',$appt_table_data);
1851                   $this->t->set_var('appt_header_row',$appt_header_row);
1852                   $this->t->set_var('appt_table_width',$appt_table_width);
1853                   $this->t->fp('apptlist','appt_list',True);
1854                 }
1855                 
1856                 // PPI SCHEDULING TABLE
1857                 $sql = "SELECT * FROM tc_individual AS ti JOIN tc_scheduling_priority AS tsp WHERE ti.scheduling_priority=tsp.scheduling_priority AND steward='$this->default_stewardship' AND valid=1 ORDER BY tsp.priority ASC, ti.name ASC";
1858                 $this->db->query($sql,__LINE__,__FILE__);
1859
1860                 $i=0; 
1861                 $individual = NULL;
1862                 while ($this->db->next_record()) {
1863                         $individual[$i] = $this->db->f('individual');
1864                         $indiv_name[$i] = $this->db->f('name');
1865                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
1866                         $indiv_priority[$individual[$i]] = $this->db->f('priority');
1867                         $indiv_notes[$individual[$i]] = $this->db->f('notes');
1868                         $i++;
1869                         $total_indivs++;
1870                 }
1871
1872                 $max = count($individual);
1873                 
1874                 for($i=0; $i < $max; $i++) {
1875                         $id = $individual[$i];
1876                         $name = $indiv_name[$i];
1877                         $phone = $indiv_phone[$id];
1878                         $priority = $indiv_priority[$id];
1879                         $notes = $indiv_notes[$id];
1880
1881                         // If this individual has had a PPI this period, don't show him on the schedule list
1882                         $year_start = $year . "-" . $start_of_period . "-01";
1883                         $year_end = $year . "-" . $end_of_period . "-31";
1884                         $sql = "SELECT * FROM tc_interview WHERE date >= '$year_start' AND date <= '$year_end' ".
1885                                "AND individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
1886                         $this->db2->query($sql,__LINE__,__FILE__);
1887
1888                         if(!$this->db2->next_record()) {
1889                                 $sql = "SELECT * FROM tc_interview WHERE individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
1890                                 $this->db->query($sql,__LINE__,__FILE__);
1891                                 if($this->db->next_record()) { 
1892                                         $date = $this->db->f('date'); 
1893                                 } else { 
1894                                         $date = ""; 
1895                                 }
1896                                 $link_data['menuaction'] = 'tc.tc.ppi_update';
1897                                 $link_data['individual'] = $id;
1898                                 $link_data['name'] = $name;
1899                                 $link_data['interview'] = '';
1900                                 $link_data['interview_type'] = 1;
1901                                 $link_data['action'] = 'add';
1902                                 $link_data['interviewer'] = $interviewer;
1903                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
1904                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1905                                 $this->t->set_var('tr_color',$tr_color);
1906                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1907                                 $table_data.= "<td align=center>$phone</td>";
1908                                 //$table_data.= "<td align=center>$priority</td>";
1909                                 $table_data.= "<td align=center>";
1910                                 $table_data.= '<select name=notes['.$i.'][pri]>';
1911                                 foreach(range(0,6) as $num) {
1912                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
1913                                         if($priority == $num) { 
1914                                                 $selected[$num] = 'selected="selected"'; 
1915                                         } else { 
1916                                                 $selected[$num] = ''; 
1917                                         }
1918                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
1919                                 }
1920                                 $table_data.= '</select></td>';
1921                                 $table_data.= "<td align=center>$date</td>";
1922                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="notes['.$i.'][notes]" value="'.$notes.'">';
1923                                 $table_data.= '<input type=hidden name="notes['.$i.'][individual]" value="'.$id.'">';
1924                                 $table_data.= '<input type=hidden name="notes['.$i.'][indiv_name]" value="'.$name.'">';
1925                                 $table_data.= '</td>';
1926                                 $table_data.= '</tr>';
1927                         } else {
1928                                 $link_data['menuaction'] = 'tc.tc.ppi_update';
1929                                 $link_data['interviewer'] = $this->db2->f('interviewer');
1930                                 $link_data['individual'] = $this->db2->f('individual');
1931                                 $link_data['name'] = $name;
1932                                 $link_data['interview'] = $this->db2->f('interview');
1933                                 $link_data['interview_type'] = $this->db2->f('interview_type');
1934                                 $link_data['action'] = 'view';
1935                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
1936                                 $indivs_with_yearly_ppi++;
1937                                 $date = $this->db2->f('date');
1938                                 $notes = $this->db2->f('notes');
1939                                 if(strlen($notes) > 40) { $notes = substr($notes,0,40) . "..."; }
1940                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
1941                                 $this->t->set_var('tr_color2',$tr_color2);
1942                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
1943                                 $completed_data.= "<td align=center>$phone</td>";
1944                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
1945                                 $completed_data.= "<td align=left>$notes</td>";
1946                                 $completed_data.= '</tr>';
1947                         }
1948                 } // End for individuals Loop
1949
1950                 $completed_table_title = "District ".$district_number.": ".$district_name.": All indivs with " . $this->ppi_frequency_label . " PPI Completed";
1951                 $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
1952                 $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
1953                 $completed_header_row = "<th width=$name_width><font size=-2>Individual</th>";
1954                 $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
1955                 $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
1956                 $completed_header_row.= "<th width=$notes_width><font size=-2>PPI Notes</th>";
1957
1958                 $this->t->set_var('completed_table_title',$completed_table_title);
1959                 $this->t->set_var('table_width',$table_width);
1960                 $this->t->set_var('header_row',$header_row);
1961                 $this->t->set_var('table_data',$table_data);
1962                 $this->t->set_var('completed_header_row',$completed_header_row);
1963                 $this->t->set_var('completed_table_width',$completed_table_width);
1964                 $this->t->set_var('completed',$completed_data);
1965                 $this->t->fp('indivlist','individual_list',True); 
1966
1967                 $indivs_width=300; $totals_width=100;
1968                 $totals_table_width=$indivs_width + $totals_width;
1969                 $totals_header_row = "<th width=$indivs_width><font size=-2>Individuals</th>";
1970                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
1971                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1972                 $totals_data.= "<td align=left><font size=-2><b>Total Individuals with " . $this->ppi_frequency_label . " PPIs completed:</b></font></td>";
1973                 $totals_data.= "<td align=center><font size=-2><b>$indivs_with_yearly_ppi / $total_indivs</b></font></td>";
1974                 $percent = ceil(($indivs_with_yearly_ppi / $total_indivs)*100);
1975                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
1976                 $this->t->set_var('tr_color',$tr_color);
1977                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
1978                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
1979                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
1980                 $totals_data.= "</tr>";
1981
1982                 $this->t->set_var('totals',$totals_data);
1983                 $this->t->set_var('totals_header_row',$totals_header_row);
1984                 $this->t->set_var('totals_table_width',$totals_table_width);
1985
1986                 $this->t->pfp('out','ppi_sched_t');
1987                 $this->save_sessiondata(); 
1988
1989         }
1990   
1991         function int_sched()
1992         {
1993                 $this->t->set_file(array('int_sched_t' => 'int_sched.tpl'));
1994                 $this->t->set_block('int_sched_t','individual_list','indivlist');
1995                 $this->t->set_block('int_sched_t','appt_list','apptlist');
1996                 $action = get_var('action',array('GET','POST'));
1997
1998                 $this->t->set_var('lang_save','Save Appt / Pri / Notes');
1999                 $this->t->set_var('lang_reset','Clear Changes');
2000
2001                 $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2002                 $this->t->set_var('int_link_title','Hometeaching Interviews');
2003
2004                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
2005                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2006
2007                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched&action=save'));
2008                 $this->t->set_var('title','Hometeaching Interviews Scheduler');
2009
2010                 $indiv_width=500; $phone_width=25; $pri_width=10; $notes_width=128; $int_date_width=20;
2011                 $table_width=$indiv_width + $phone_width + $pri_width + $notes_width + $int_date_width;
2012                 $header_row = "<th width=$indiv_width><font size=-2>Individual</th>";
2013                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
2014                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
2015                 $header_row.= "<th width=$int_date_width><font size=-2>Last Interview</th>";
2016                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2017                 $table_data=""; $completed_data=""; $totals_data="";
2018
2019                 $year = date('Y');
2020                 $month = date('m');
2021                 $nextyear = $year + 1;
2022                 if($month >= 1 && $month <= 3) { $quarter_start=$year."-01-01"; $quarter_end=$year."-04-01"; }
2023                 if($month >= 4 && $month <= 6) { $quarter_start=$year."-04-01"; $quarter_end=$year."-07-01"; }
2024                 if($month >= 7 && $month <= 9) { $quarter_start=$year."-07-01"; $quarter_end=$year."-10-01"; }
2025                 if($month >= 10 && $month <= 12) { $quarter_start=$year."-10-01"; $quarter_end=$nextyear."-01-01"; }
2026                 //print "year: $year month: $month quarter_start: $quarter_start quarter_end: $quarter_end<br>";
2027
2028                 // create the individual id -> individual name mapping
2029                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY name ASC";
2030                 $this->db->query($sql,__LINE__,__FILE__);
2031                 $i=0;
2032                 $individual_data = NULL;
2033                 $indiv_name_data = NULL;
2034                 while ($this->db->next_record()) {
2035                         $indiv_name_data[$i] = $this->db->f('name');
2036                         $individual_data[$i] = $this->db->f('individual');
2037                         $individ2name[$individual_data[$i]] = $indiv_name_data[$i];
2038                         $i++;
2039                 }
2040                 // add any YM that are home teachers
2041                 $sql = "SELECT * FROM tc_companionship where valid=1";
2042                 $this->db->query($sql,__LINE__,__FILE__);
2043                 while ($this->db->next_record()) {
2044                         $tmp_individual = $this->db->f('individual');
2045                         $sql = "Select * FROM tc_individual where individual='$tmp_individual' and steward='' and valid=1";
2046                         $this->db2->query($sql,__LINE__,__FILE__);
2047                         while ($this->db2->next_record()) {
2048                                 $indiv_name_data[$i] = $this->db2->f('name');
2049                                 $individual_data[$i] = $this->db2->f('individual');
2050                                 $individ2name[$individual_data[$i]] = $indiv_name_data[$i];
2051                                 $i++;
2052                         }
2053                 }
2054                 array_multisort($indiv_name_data, $individual_data);
2055
2056                 if($action == 'save') {
2057                         // Save any changes made to the appointment table
2058                         $new_data = get_var('appt_notes',array('POST'));
2059                         if($new_data != "") {
2060                                 foreach ($new_data as $entry) {
2061                                         $indiv = $entry['individual'];
2062                                         $appointment = $entry['appointment'];
2063                                         $location = $entry['location'];
2064                                         if($location == "") {
2065                                                 $supervisor = $entry['supervisor'];
2066                                                 $supervisor_array = explode(",", $individ2name[$supervisor]);
2067                                                 $supervisor_last_name = $supervisor_array[0];
2068                                                 $sql = "SELECT * FROM tc_individual where individual='$supervisor'";
2069                                                 $this->db2->query($sql,__LINE__,__FILE__);
2070                                                 if($this->db2->next_record()) {
2071                                                         $supervisor_address = $this->db2->f('address');
2072                                                 }
2073                                                 $location = "$supervisor_last_name"." home ($supervisor_address)";
2074                                         }
2075                                         if($indiv == 0) { $location = ""; }
2076
2077                                         //print "indiv: $indiv appointment: $appointment <br>";
2078                                         //Only perform a database update if we have made a change to this appointment
2079                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and individual='$indiv' and location='$location'";
2080                                         $this->db->query($sql,__LINE__,__FILE__);
2081                                         if(!$this->db->next_record()) {
2082                                                 // Perform database save actions here
2083                                                 $this->db->query("UPDATE tc_appointment set " .
2084                                                                  " individual='" . $indiv . "'" .
2085                                                                  ",location='" . $location . "'" .
2086                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2087                                                 // Email the appointment
2088                                                 $this->email_appt($appointment);
2089                                         }
2090                                 }
2091                         }
2092
2093                         // Save any changes made to the int notes table
2094                         $new_data = get_var('hti_notes',array('POST'));
2095                         foreach ($new_data as $entry) {
2096                                 $hti_notes = $entry['notes'];
2097                                 $individual = $entry['individual'];
2098                                 $indiv_name = $entry['indiv_name'];
2099                                 $hti_pri = $entry['pri'];
2100                                 //print "hti_notes: $hti_notes indiv_name: $indiv_name <Br>";
2101                                 // Perform database save actions here
2102                                 $this->db->query("SELECT * FROM tc_companion WHERE individual=$individual and valid=1",__LINE__,__FILE__);
2103                                 if ($this->db->next_record()) {
2104                                         $scheduling_priority = $this->db->f('scheduling_priority');
2105                                         //$this->logToFile("int_sched", "UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'");
2106                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'",__LINE__,__FILE__);
2107                                 }
2108                         }
2109
2110                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched');
2111                         //Header('Location: ' . $take_me_to_url);
2112                 }
2113
2114                 // Get the Districts
2115                 $sql = "SELECT * FROM tc_district AS td JOIN (tc_presidency AS tp, tc_individual AS ti) WHERE td.district=tp.district AND td.supervisor=ti.individual AND td.valid=1 ORDER BY td.district ASC";
2116                 $this->db->query($sql,__LINE__,__FILE__);
2117                 $i=0;
2118                 while ($this->db->next_record()) {
2119                         $district = $this->db->f('district');
2120                         $districts[$i]['district'] = $this->db->f('district');
2121                         $districts[$i]['name'] = $this->db->f('name');
2122                         $districts[$i]['supervisor'] = $this->db->f('supervisor');
2123                         $districts[$i]['presidency'] = $this->db->f('presidency');
2124                         $i++;
2125                 }
2126
2127                 // APPOINTMENT TABLE
2128                 $district = 1;
2129                 $date_width=250; $time_width=100; $indiv_width=200; $location_width=100;
2130                 $appt_table_width=$date_width + $time_width + $indiv_width + $location_width;
2131                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2132                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2133                 $appt_header_row.= "<th width=$indiv_width><font size=-2>Individual</th>";
2134                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
2135                 $appt_table_data = ""; 
2136
2137                 $total_comps=0; $comps_with_quarterly_int=0;
2138
2139                 // Display a scheduling table for each district
2140                 for ($d=0; $d < count($districts); $d++) {
2141                         $table_data=""; $appt_table_data="";
2142                         $this->t->set_var('district_number',$districts[$d]['district']);
2143                         $this->t->set_var('district_name',$districts[$d]['name']);      
2144                         $supervisor = $districts[$d]['supervisor'];
2145                         $supervisor_array = explode(",", $supervisor);
2146                         $supervisor_last_name = $supervisor_array[0];
2147                         $sql = "SELECT * FROM tc_individual where individual='$supervisor'";
2148                         $this->db2->query($sql,__LINE__,__FILE__);
2149                         if($this->db2->next_record()) {
2150                                 $supervisor_address = $this->db2->f('address');
2151                         }
2152                         $location = "$supervisor_last_name"." home ($supervisor_address)";
2153                         $table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": All Individuals with Interviews Not Completed";
2154                         $appt_table_title = "District ".$districts[$d]['district'].": ".$districts[$d]['name'].": Interview Appointment Slots";
2155                         $this->t->set_var('table_title',$table_title);
2156                         $this->t->set_var('appt_table_title',$appt_table_title);
2157
2158                         // query the database for all the appointments
2159                         $sql = "SELECT * FROM tc_appointment where presidency=".$districts[$d]['presidency']." and date>=CURDATE() ORDER BY date ASC, time ASC";
2160                         $this->db->query($sql,__LINE__,__FILE__);
2161
2162                         while ($this->db->next_record()) {
2163                                 $appointment = $this->db->f('appointment');
2164                                 $indiv = $this->db->f('individual');
2165                                 $location = $this->db->f('location');
2166                                 if(($location == "") && ($indiv > 0)) { $location = "$supervisor_last_name"." home ($supervisor_address)"; }
2167
2168                                 $date = $this->db->f('date');
2169                                 $date_array = explode("-",$date);
2170                                 $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2171                                 $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2172
2173                                 $time = $this->db->f('time');
2174                                 $time_array = explode(":",$time);
2175                                 $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2176
2177                                 $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2178                                 $appt_table_data.= "<td align=center>$day_string</td>";
2179                                 $appt_table_data.= "<td align=center>$time_string</td>";
2180
2181                                 $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][individual]>';
2182                                 $appt_table_data.= '<option value=0></option>';
2183                                 for ($i=0; $i < count($individual_data); $i++) {
2184                                         $id = $individual_data[$i];
2185                                         $name = $indiv_name_data[$i];
2186                                         if($individual_data[$i] == $indiv) { 
2187                                                 $selected[$id] = 'selected="selected"'; 
2188                                         } else { 
2189                                                 $selected[$id] = ''; 
2190                                         }
2191                                         $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
2192                                 }
2193                                 $appt_table_data.='</select></td>';
2194
2195                                 $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
2196                                 $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
2197
2198                                 $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2199                                 $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][supervisor]" value="'.$supervisor.'">';
2200
2201                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2202                                 $this->t->set_var('tr_color',$tr_color);
2203                         }
2204
2205                         $this->t->set_var('appt_table_data',$appt_table_data);
2206                         $this->t->set_var('appt_header_row',$appt_header_row);
2207                         $this->t->set_var('appt_table_width',$appt_table_width);
2208
2209                         // INTERVIEW SCHEDULING TABLE
2210
2211                         // Select all the unique companionship numbers for this district
2212                         $sql = "SELECT distinct companionship FROM tc_companionship where valid=1 and district=". $districts[$d]['district'];
2213                         $this->db->query($sql,__LINE__,__FILE__);
2214                         $j=0; $unique_companionships = '';
2215                         while ($this->db->next_record())
2216                         {
2217                                 $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2218                                 $j++;
2219                         }
2220
2221                         $i=0;
2222                         for ($j=0; $j < count($unique_companionships); $j++) {
2223                                 // Select all the companions from each companionship
2224                                 $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'];
2225                                 $this->db->query($sql,__LINE__,__FILE__);
2226                                 $k=0; $int_completed=0;
2227                                 $comp = $unique_companionships[$j]['companionship'];
2228                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2229                                 $this->t->set_var('tr_color',$tr_color);
2230                                 $total_comps++;
2231                                 while ($this->db->next_record()) {
2232                                         // Get this companions information
2233                                         $individual = $this->db->f('individual');
2234
2235                                         $id = $this->db->f('individual');
2236                                         $name = $this->db->f('name');
2237                                         $phone = $this->db->f('phone');
2238                                         $hti_pri = $this->db->f('priority');
2239                                         $hti_notes = $this->db->f('notes');
2240
2241                                         // If the companionship has already had its quarterly interview,
2242                                         // Skip the other companion in the companionship.
2243                                         if($int_completed == 1) {
2244                                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2245                                                 $completed_data.= "<td align=center>$phone</td>";
2246                                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2247                                                 $completed_data.= "<td align=left>$hti_notes</td>";
2248                                                 $completed_data.= '</tr>';
2249                                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2250                                                 $this->t->set_var('tr_color2',$tr_color2);
2251                                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2252                                                 $this->t->set_var('tr_color',$tr_color);
2253                                                 continue;
2254                                         }
2255
2256                                         // If this companionship has had a hometeaching interview this quarter, don't show them on the schedule list
2257                                         $sql = "SELECT * FROM tc_interview WHERE date >= '$quarter_start' AND date < '$quarter_end' AND individual='$id' AND interview_type='hti'";
2258                                         $this->db2->query($sql,__LINE__,__FILE__);
2259
2260                                         if(!$this->db2->next_record()) {
2261                                                 $sql = "SELECT * FROM tc_interview WHERE individual='$id' AND interview_type='hti' ORDER BY date DESC";
2262                                                 $this->db3->query($sql,__LINE__,__FILE__);
2263                                                 if($this->db3->next_record()) { 
2264                                                         $date = $this->db3->f('date'); 
2265                                                 } else { 
2266                                                         $date = ""; 
2267                                                 }
2268                                                 $link_data['menuaction'] = 'tc.tc.int_update';
2269                                                 $link_data['individual'] = $id;
2270                                                 $link_data['name'] = $name;
2271                                                 $link_data['interview'] = '';
2272                                                 $link_data['action'] = 'add';
2273                                                 $link_data['interview_type'] = 'hti';
2274                                                 $link_data['interviewer'] = $districts[$d]['supervisor'];
2275                                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2276                                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2277                                                 $table_data.= "<td align=center>$phone</td>";
2278                                                 $table_data.= "<td align=center>";
2279                                                 $table_data.= '<select name=hti_notes['.$i.'][pri]>';
2280                                                 foreach(range(0,6) as $num) {
2281                                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
2282                                                         if($hti_pri == $num) { 
2283                                                                 $selected[$num] = 'selected="selected"'; 
2284                                                         } else { 
2285                                                                 $selected[$num] = ''; 
2286                                                         }
2287                                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2288                                                 }
2289                                                 $table_data.= '</select></td>';
2290                                                 $table_data.= "<td align=center>$date</td>";
2291                                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="hti_notes['.$i.'][notes]" value="'.$hti_notes.'">';
2292                                                 $table_data.= '<input type=hidden name="hti_notes['.$i.'][individual]" value="'.$id.'">';
2293                                                 $table_data.= '<input type=hidden name="hti_notes['.$i.'][indiv_name]" value="'.$name.'">';
2294                                                 $table_data.= '</td>';
2295                                                 $table_data.= '</tr>'."\n";
2296                                                 $i++;
2297                                         } else {
2298                                                 $link_data['menuaction'] = 'tc.tc.int_update';
2299                                                 $link_data['interviewer'] = $this->db2->f('interviewer');
2300                                                 $link_data['individual'] = $this->db2->f('individual');
2301                                                 $link_data['name'] = $name;
2302                                                 $link_data['interview'] = $this->db2->f('interview');
2303                                                 $link_data['interview_type'] = 'hti';
2304                                                 $link_data['action'] = 'view';
2305                                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
2306                                                 $comps_with_quarterly_int++;
2307                                                 $int_completed=1;
2308                                                 $date = $this->db2->f('date');
2309                                                 $hti_notes = $this->db2->f('notes');
2310                                                 if(strlen($hti_notes) > 40) { $hti_notes = substr($hti_notes,0,40) . "..."; }
2311                                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2312                                                 $completed_data.= "<td align=center>$phone</td>";
2313                                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2314                                                 $completed_data.= "<td align=left>$hti_notes</td>";
2315                                                 $completed_data.= '</tr>';
2316                                         }
2317                                 }
2318                         }
2319
2320                         $name_width=175; $phone_width=100; $date_width=100; $notes_width=300;
2321                         $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2322                         $completed_header_row = "<th width=$name_width><font size=-2>Individual</th>";
2323                         $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2324                         $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2325                         $completed_header_row.= "<th width=$notes_width><font size=-2>Interview Notes</th>";
2326
2327                         $this->t->set_var('table_width',$table_width);
2328                         $this->t->set_var('header_row',$header_row);
2329                         $this->t->set_var('table_data',$table_data);
2330                         $this->t->set_var('completed_header_row',$completed_header_row);
2331                         $this->t->set_var('completed_table_width',$completed_table_width);
2332                         $this->t->set_var('completed',$completed_data);
2333                         $this->t->fp('indivlist','individual_list',True);
2334
2335                 } // End for each district loop
2336
2337
2338                 $indivs_width=300; $totals_width=100;
2339                 $totals_table_width=$indivs_width + $totals_width;
2340                 $totals_header_row = "<th width=$indivs_width><font size=-2>Individuals</th>";
2341                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2342                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2343                 $totals_data.= "<td align=left><font size=-2><b>Total Companionships with interviews completed:</b></font></td>";
2344                 $totals_data.= "<td align=center><font size=-2><b>$comps_with_quarterly_int / $total_comps</b></font></td>";
2345                 $percent = ceil(($comps_with_quarterly_int / $total_comps)*100);
2346                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2347                 $this->t->set_var('tr_color',$tr_color);
2348                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2349                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2350                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2351                 $totals_data.= "</tr>";
2352
2353                 $this->t->set_var('totals',$totals_data);
2354                 $this->t->set_var('totals_header_row',$totals_header_row);
2355                 $this->t->set_var('totals_table_width',$totals_table_width);
2356
2357                 $this->t->pfp('out','int_sched_t');
2358                 $this->save_sessiondata(); 
2359
2360         }
2361   
2362         function vis_sched()
2363         {
2364                 $this->t->set_file(array('vis_sched_t' => 'vis_sched.tpl'));
2365                 $this->t->set_block('vis_sched_t','family_list','familylist');
2366                 $this->t->set_block('vis_sched_t','appt_list','apptlist');
2367                 $action = get_var('action',array('GET','POST'));
2368
2369                 $this->t->set_var('lang_save','Save Appt / Pri / Notes');
2370                 $this->t->set_var('lang_reset','Clear Changes');
2371
2372                 $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
2373                 $this->t->set_var('vis_link_title','View Yearly Visits');
2374
2375                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
2376                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
2377
2378                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched&action=save'));
2379                 $this->t->set_var('title','Presidency Yearly Visit Scheduler');
2380
2381                 $family_width=500; $phone_width=40; $pri_width=10; $notes_width=128; $visit_date_width=20;
2382                 $table_width=$family_width + $phone_width + $pri_width + $notes_width + $visit_date_width;
2383                 $header_row = "<th width=$family_width><font size=-2>Family Name</th>";
2384                 $header_row.= "<th width=$phone_width><font size=-2>Phone</th>";
2385                 $header_row.= "<th width=$pri_width><font size=-2>Priority</th>";
2386                 $header_row.= "<th width=$visit_date_width><font size=-2>Last Visit</th>";
2387                 $header_row.= "<th width=$notes_width><font size=-2>Scheduling Notes</th>";
2388                 $table_data=""; $completed_data=""; $totals_data="";
2389
2390                 $year = date('Y');
2391
2392                 // create the family id -> family name mapping
2393                 $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";
2394                 $this->db->query($sql,__LINE__,__FILE__);
2395                 $i=0;
2396                 $family_id = NULL;
2397                 while ($this->db->next_record()) {
2398                         $family_id[$i] = $this->db->f('family');
2399                         $family_name[$i] = $this->db->f('name');
2400                         $familyid2name[$family_id[$i]] = $family_name[$i];
2401                         $familyid2address[$family_id[$i]] = $this->db->f('address');
2402                         $i++;
2403                 }
2404                 array_multisort($family_name, $family_id);
2405
2406                 if($action == 'save') {
2407                         // Save any changes made to the appointment table
2408                         $new_data = get_var('appt_notes',array('POST'));
2409                         if($new_data != "") {
2410                                 foreach ($new_data as $entry) {
2411                                         $family = $entry['family'];
2412                                         $appointment = $entry['appointment'];
2413                                         $location = $entry['location'];
2414                                         if($location == "") {
2415                                                 $family_name_array = explode(",", $familyid2name[$family]);
2416                                                 $family_last_name = $family_name_array[0];
2417                                                 $family_address = $familyid2address[$family];
2418                                                 $location = "$family_last_name"." home ($family_address)";
2419                                         }
2420                                         if($family == 0) { $location = ""; }
2421
2422                                         //Only perform a database update if we have made a change to this appointment
2423                                         $sql = "SELECT * FROM tc_appointment where appointment='$appointment' and family='$family' and location='$location'";
2424                                         $this->db->query($sql,__LINE__,__FILE__);
2425                                         if(!$this->db->next_record()) {
2426                                                 // Perform database save actions here
2427                                                 $this->db->query("UPDATE tc_appointment set " .
2428                                                                  " family='" . $family . "'" .
2429                                                                  ",location='" . $location . "'" .
2430                                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
2431
2432                                                 // Email the appointment
2433                                                 $this->email_appt($appointment);
2434                                         }
2435                                 }
2436                         }
2437
2438                         // Save any changes made to the visit notes table
2439                         $new_data = get_var('vis_notes',array('POST'));
2440                         foreach ($new_data as $entry) {
2441                                 $visit_notes = $entry['notes'];
2442                                 $family = $entry['family_id'];
2443                                 $visit_pri = $entry['pri'];
2444                                 // Perform database save actions here
2445                                 $this->db->query("SELECT * FROM tc_family WHERE family='$family'",__LINE__,__FILE__);
2446                                 if ($this->db->next_record()) {
2447                                         $scheduling_priority = $this->db->f('scheduling_priority');
2448                                         $this->db2->query("UPDATE tc_scheduling_priority SET priority='$visit_pri', notes=\"$visit_notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
2449                                 }
2450                         }
2451
2452                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched');
2453                         //Header('Location: ' . $take_me_to_url);
2454                 }
2455
2456                 // APPOINTMENT TABLE
2457                 $date_width=250; $time_width=100; $family_width=250; $location_width=100;
2458                 $appt_table_width=$date_width + $time_width + $family_width + $location_width;
2459                 $appt_header_row = "<th width=$date_width><font size=-2>Date</th>";
2460                 $appt_header_row.= "<th width=$time_width><font size=-2>Time</th>";      
2461                 $appt_header_row.= "<th width=$family_width><font size=-2>Family</th>";
2462                 $appt_header_row.= "<th width=$location_width><font size=-2>Location</th>";
2463                 $appt_table_data = ""; 
2464
2465                 // Find out what the President ID is
2466                 $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";
2467                 $this->db->query($sql,__LINE__,__FILE__);
2468                 if($this->db->next_record()) {
2469                         $presidency_name = $this->db->f('name');
2470                         $presidency_id = $this->db->f('presidency');
2471                 } else {
2472                         print "<hr><font color=red><h3>-E- Unable to locate Presidency in tc_presidency table</h3></font></hr>";
2473                         return;
2474                 }
2475
2476                 // query the database for all the appointments
2477                 $sql = "SELECT * FROM tc_appointment where presidency=$presidency_id and date>=CURDATE() ORDER BY date ASC, time ASC";
2478                 $this->db->query($sql,__LINE__,__FILE__);
2479
2480                 while ($this->db->next_record()) {
2481                         $appointment = $this->db->f('appointment');
2482                         $family = $this->db->f('family');
2483                         $location = $this->db->f('location');
2484                         $family_name_array = explode(",", $familyid2name[$family]);
2485                         $family_last_name = $family_name_array[0];
2486                         $family_address = $familyid2address[$family];
2487                         if(($location == "") && ($family > 0)) { $location = "$family_last_name"." home ($family_address)"; }
2488
2489                         $date = $this->db->f('date');
2490                         $date_array = explode("-",$date);
2491                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
2492                         $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
2493
2494                         $time = $this->db->f('time');
2495                         $time_array = explode(":",$time);
2496                         $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
2497
2498                         $appt_table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2499                         $appt_table_data.= "<td align=center>$day_string</td>";
2500                         $appt_table_data.= "<td align=center>$time_string</td>";
2501
2502                         $appt_table_data.= '<td align=center><select name=appt_notes['.$appointment.'][family]>';
2503                         $appt_table_data.= '<option value=0></option>';
2504                         for ($i=0; $i < count($family_id); $i++) {
2505                                 $id = $family_id[$i];
2506                                 $name = $family_name[$i];
2507                                 if($family_id[$i] == $family) { 
2508                                         $selected[$id] = 'selected="selected"'; 
2509                                 } else { 
2510                                         $selected[$id] = ''; 
2511                                 }
2512                                 $appt_table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
2513                         }
2514                         $appt_table_data.='</select></td>';
2515
2516                         $appt_table_data.= '<td align=center><input type=text size="35" maxlength="120" ';
2517                         $appt_table_data.= 'name="appt_notes['.$appointment.'][location]" value="'.$location.'">';
2518
2519                         $appt_table_data.= '<input type=hidden name="appt_notes['.$appointment.'][appointment]" value="'.$appointment.'">';
2520
2521                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2522                         $this->t->set_var('tr_color',$tr_color);
2523                 }
2524
2525                 $this->t->set_var('appt_table_data',$appt_table_data);
2526                 $this->t->set_var('appt_header_row',$appt_header_row);
2527                 $this->t->set_var('appt_table_width',$appt_table_width);
2528
2529
2530                 // VISIT SCHEDULING TABLE
2531                 $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";
2532                 $this->db->query($sql,__LINE__,__FILE__);
2533
2534                 $total_families=0; $families_with_yearly_visit=0;
2535
2536                 while ( $this->db->next_record()) {
2537                         $total_families++;
2538                         $id = $this->db->f('family');
2539                         $name = $this->db->f('name');
2540                         $phone = $this->db->f('phone');
2541                         $vis_pri = $this->db->f('priority');
2542                         $vis_notes = $this->db->f('notes');
2543
2544                         // If this family has had a yearly visit this year, don't show them on the schedule list
2545                         $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2546                         $sql = "SELECT * FROM tc_visit WHERE date > '$year_start' AND date < '$year_end' ".
2547                                "AND family=" . $id . " AND companionship=0";
2548                         $this->db2->query($sql,__LINE__,__FILE__);
2549
2550                         if(!$this->db2->next_record()) {
2551                                 $sql = "SELECT * FROM tc_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
2552                                 $this->db3->query($sql,__LINE__,__FILE__);
2553                                 if($this->db3->next_record()) { 
2554                                         $date = $this->db3->f('date'); 
2555                                 } else { 
2556                                         $date = ""; 
2557                                 }
2558                                 $link_data['menuaction'] = 'tc.tc.vis_update';
2559                                 $link_data['visit'] = '';
2560                                 $link_data['family'] = $id;
2561                                 $link_data['name'] = $name;
2562                                 $link_data['action'] = 'add';
2563                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2564                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2565                                 $table_data.= "<td align=center>$phone</td>";
2566                                 $table_data.= "<td align=center>";
2567                                 $table_data.= '<select name=vis_notes['.$id.'][pri]>';
2568                                 foreach(range(0,6) as $num) {
2569                                         if($num == 0) { $num = 1; } else {$num = $num*5; }
2570                                         if($vis_pri == $num) { 
2571                                                 $selected[$num] = 'selected="selected"'; 
2572                                         } else { 
2573                                                 $selected[$num] = ''; 
2574                                         }
2575                                         $table_data.= '<option value='.$num.' '.$selected[$num].'>'.$num.'</option>';
2576                                 }
2577                                 $table_data.= '</select></td>';
2578                                 $table_data.= "<td align=center>$date</td>";
2579                                 $table_data.= '<td><input type=text size="50" maxlength="128" name="vis_notes['.$id.'][notes]" value="'.$vis_notes.'">';
2580                                 $table_data.= '<input type=hidden name="vis_notes['.$id.'][family_id]" value="'.$id.'">';
2581                                 $table_data.= '<input type=hidden name="vis_notes['.$id.'][family_name]" value="'.$name.'">';
2582                                 $table_data.= '</td>';
2583                                 $table_data.= '</tr>';
2584                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2585                                 $this->t->set_var('tr_color',$tr_color);
2586                         } else {
2587                                 $link_data['menuaction'] = 'tc.tc.vis_update';
2588                                 $link_data['visit'] = $this->db2->f('visit');
2589                                 $link_data['family'] = $this->db2->f('family');
2590                                 $link_data['name'] = $name;
2591                                 $link_data['date'] = $this->db2->f('date');
2592                                 $link_data['action'] = 'view';
2593                                 $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
2594                                 $families_with_yearly_visit++;
2595                                 $date = $this->db2->f('date');
2596                                 $vis_notes = $this->db2->f('notes');
2597                                 if(strlen($vis_notes) > 40) { $vis_notes = stripslashes(substr($vis_notes,0,40) . "..."); }
2598                                 $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name Family</a></td>";
2599                                 $completed_data.= "<td align=center>$phone</td>";
2600                                 $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
2601                                 $completed_data.= "<td align=left>$vis_notes</td>";
2602                                 $completed_data.= '</tr>';
2603                                 $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
2604                                 $this->t->set_var('tr_color2',$tr_color2);
2605                         }
2606                 }
2607
2608                 $name_width=190; $phone_width=100; $date_width=100; $notes_width=300;
2609                 $completed_table_width=$name_width + $phone_width + $date_width + $notes_width;
2610                 $completed_header_row = "<th width=$name_width><font size=-2>Family Name</th>";
2611                 $completed_header_row.= "<th width=$phone_width><font size=-2>Phone</th>";      
2612                 $completed_header_row.= "<th width=$date_width><font size=-2>Date</th>";
2613                 $completed_header_row.= "<th width=$notes_width><font size=-2>Visit Notes</th>";
2614
2615                 $family_width=300; $totals_width=100;
2616                 $totals_table_width=$family_width + $totals_width;
2617                 $totals_header_row = "<th width=$family_width><font size=-2>Families</th>";
2618                 $totals_header_row.= "<th width=$totals_width><font size=-2>$year</th>";
2619                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2620                 $totals_data.= "<td align=left><font size=-2><b>Total Families with yearly Visits completed:</b></font></td>";
2621                 $totals_data.= "<td align=center><font size=-2><b>$families_with_yearly_visit / $total_families</b></font></td>";
2622                 $percent = ceil(($families_with_yearly_visit / $total_families)*100);
2623                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
2624                 $this->t->set_var('tr_color',$tr_color);
2625                 $totals_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
2626                 $totals_data.= "<td align=left><font size=-2><b>Percentage:</b></font></td>";
2627                 $totals_data.= "<td align=center><font size=-2><b>$percent%</b></font></td>";
2628                 $totals_data.= "</tr>";
2629
2630                 $this->t->set_var('table_width',$table_width);
2631                 $this->t->set_var('header_row',$header_row);
2632                 $this->t->set_var('table_data',$table_data);
2633                 $this->t->set_var('totals_header_row',$totals_header_row);
2634                 $this->t->set_var('totals_table_width',$totals_table_width);
2635                 $this->t->set_var('completed_header_row',$completed_header_row);
2636                 $this->t->set_var('completed_table_width',$completed_table_width);
2637                 $this->t->set_var('completed',$completed_data);
2638                 $this->t->set_var('totals',$totals_data);
2639                 $this->t->fp('familylist','family_list',True);
2640                 $this->t->fp('apptlist','appt_list',True);
2641
2642                 $this->t->pfp('out','vis_sched_t');
2643                 $this->save_sessiondata(); 
2644         }
2645   
2646         function ppi_view()
2647         {
2648                 $this->t->set_file(array('ppi_view_t' => 'ppi_view.tpl'));
2649                 $this->t->set_block('ppi_view_t','district_list','list');
2650
2651                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2652                 $num_months = get_var('num_months',array('GET','POST'));
2653                 if($num_months == '') { $num_months = $this->default_ppi_num_months; }
2654                 $this->t->set_var('num_months',$num_months);
2655                 if($num_months == 1) { 
2656                         $this->t->set_var('lang_num_months','Month of History'); 
2657                 } else {  
2658                         $this->t->set_var('lang_num_months','Months of History'); 
2659                 }
2660                 $this->t->set_var('lang_filter','Filter');
2661                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2662
2663                 $this->t->set_var('ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2664                 $this->t->set_var('ppi_link_title',$this->ppi_frequency_label . ' PPIs'); 
2665
2666                 $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched'));
2667                 $this->t->set_var('schedule_ppi_link_title','Schedule ' . $this->ppi_frequency_label . ' PPIs');
2668
2669                 $this->t->set_var('title',$this->ppi_frequency_label . ' PPIs');
2670                 $num_months = get_var('num_months',array('GET','POST'));
2671                 if($num_months == '') { $num_months = $this->default_ppi_num_years; }
2672                 $this->t->set_var('num_months',$num_months);
2673                 if($num_months == 1) { 
2674                         $this->t->set_var('lang_num_months','Year of History'); 
2675                 } else { 
2676                         $this->t->set_var('lang_num_months','Years of History');
2677                 }
2678
2679                 $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";
2680                 $this->db->query($sql,__LINE__,__FILE__);
2681                 if($this->db->next_record()) {
2682                         $president_name = $this->db->f('name');
2683                         $interviewer = $this->db->f('individual');
2684                 } else {
2685                         print "<hr><font color=red><h3>-E- Unable to locate President in tc_presidency table</h3></font></hr>";
2686                         return;
2687                 }
2688                 $this->t->set_var('district_number','*');
2689                 $this->t->set_var('district_name',$president_name);
2690
2691                 $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";
2692                 $this->db->query($sql,__LINE__,__FILE__);
2693                 $i=0;
2694                 while ($this->db->next_record()) {
2695                         $individual[$i] = $this->db->f('individual');
2696                         $indiv_name[$i] = $this->db->f('name');
2697                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
2698                         $indiv_priority[$individual[$i]] = $this->db->f('priority');
2699                         $indiv_notes[$individual[$i]] = $this->db->f('notes');
2700                         $i++;
2701                 }
2702                 $total_indivs=$i;
2703                 array_multisort($indiv_name, $individual);
2704                 //var_dump($indiv_name); print "<br><br>"; var_dump($individual);
2705
2706                 $header_row="<th width=$comp_width><font size=-2>Individual</th>";
2707
2708                 $indiv_width=400; $ppi_width=75; $table_width=$indiv_width + $num_months*$ppi_width;
2709                 $table_data="";
2710                 for($m=$num_months; $m >= 0; $m--) {
2711                         $year = date('Y') - $m;
2712                         $header_row .= "<th width=150><font size=-2>$year</th>"; 
2713                         $ppis[$m] = 0;
2714                 }
2715
2716                 for ($j=0; $j < count($individual); $j++) {
2717                         $id = $individual[$j];
2718                         $name = $indiv_name[$j];
2719                         $phone = $indiv_phone[$id];
2720
2721                         $link_data['menuaction'] = 'tc.tc.ppi_update';
2722                         $link_data['interviewer'] = $interviewer;
2723                         $link_data['individual'] = $id;
2724                         $link_data['name'] = $name;
2725                         $link_data['interview'] = '';
2726                         $link_data['interview_type'] = 'ppi'; 
2727                         $link_data['action'] = 'add';
2728                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2729                         $this->nextmatchs->template_alternate_row_color(&$this->t);
2730                         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
2731
2732                         // Find out how many times PPIs were performed in the past $num_months for this individual
2733                         for($m=$num_months; $m >= 0; $m--) {
2734                                 $year = date('Y') - $m;
2735                                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
2736                                 $sql = "SELECT * FROM tc_interview WHERE date > '$year_start' AND date < '$year_end' ".
2737                                        "AND individual=" . $id . " AND interview_type='ppi' ORDER BY date DESC";
2738                                 $this->db2->query($sql,__LINE__,__FILE__);
2739
2740                                 if(!$total_ppis[$m]) { $total_ppis[$m] = 0; }
2741                                 if($this->db2->next_record()) {
2742                                         $ppis[$m]++; $total_ppis[$m]++; $ppi_recorded[$companionship][$m]=1;
2743                                         $link_data['menuaction'] = 'tc.tc.ppi_update';
2744                                         $link_data['companionship'] = $companionship;
2745                                         $link_data['interviewer'] = $this->db2->f('interviewer');
2746                                         $link_data['indiv'] = $id;
2747                                         $link_data['name'] = $name;
2748                                         $link_data['interview'] = $this->db2->f('interview');
2749                                         $link_data['interview_type'] = 'ppi';
2750                                         $link_data['action'] = 'view';
2751                                         $date = $this->db2->f('date');
2752                                         $date_array = explode("-",$date);
2753                                         $month = $date_array[1];
2754                                         $day   = $date_array[2];
2755                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
2756                                         $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
2757                                 } else { 
2758                                         $table_data .= "<td>&nbsp;</td>"; 
2759                                 }
2760                         }
2761                         $table_data .= "</tr>\n"; 
2762                 }
2763                 $table_data .= "<tr><td colspan=20><hr></td></tr>";
2764
2765                 $stat_data = "<tr><td><b><font size=-2>$total_indivs Individuals<br>PPI Totals:</font></b></td>";
2766                 for($m=$num_months; $m >=0; $m--) {
2767                         $percent = ceil(($ppis[$m] / $total_indivs)*100);
2768                         $stat_data .= "<td align=center><font size=-2><b>$ppis[$m]<br>$percent%</font></b></td>";
2769                 }
2770                 $stat_data .= "</tr>";
2771
2772                 $this->t->set_var('table_width',$table_width);
2773                 $this->t->set_var('header_row',$header_row);
2774                 $this->t->set_var('table_data',$table_data);
2775                 $this->t->set_var('stat_data',$stat_data);
2776                 $this->t->pfp('out','ppi_view_t');
2777                 $this->save_sessiondata(); 
2778         }
2779
2780         function ppi_update()
2781         {
2782                 $this->t->set_file(array('form' => 'ppi_update.tpl'));
2783                 $this->t->set_block('form','interviewer_list','int_list');
2784                 $this->t->set_block('form','add','addhandle');
2785                 $this->t->set_block('form','edit','edithandle');
2786
2787                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_view'));
2788                 $this->t->set_var('readonly','');
2789                 $this->t->set_var('disabled','');
2790
2791                 $action = get_var('action',array('GET','POST'));
2792                 $companionship = get_var('companionship',array('GET','POST'));
2793                 $interviewer = get_var('interviewer',array('GET','POST'));      
2794                 $name = get_var('name',array('GET','POST'));
2795                 $interview = get_var('interview',array('GET','POST'));
2796                 $individual = get_var('individual',array('GET','POST'));
2797                 $date = get_var('date',array('GET','POST'));
2798                 $notes = get_var('notes',array('GET','POST'));
2799                 $interview_type = get_var('interview_type',array('GET','POST'));
2800
2801             $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1 AND ";
2802             if($this->yearly_ppi_interviewer == 1) { $sql .= " (tp.president=1)"; }
2803                 if($this->yearly_ppi_interviewer == 2) { $sql .= " (tp.president=1 OR tp.counselor=1)"; }
2804                 if($this->yearly_ppi_interviewer == 3) { $sql .= " (tp.president=1 OR tp.counselor=1 OR tp.secretary=1)"; }
2805                 $this->db2->query($sql,__LINE__,__FILE__);
2806                 while ($this->db2->next_record()) {
2807                         $indiv = $this->db2->f('individual');
2808                         $interviewer_name = $this->db2->f('name');
2809                         if($indiv == $interviewer) {
2810                                 $this->t->set_var('interviewer',$indiv . ' selected');
2811                         } else {
2812                                 $this->t->set_var('interviewer',$indiv);
2813                         }
2814                         #print "indiv: $indiv interviewer: $interviewer<br>";
2815                         $this->t->set_var('interviewer_name',$interviewer_name);
2816                         $this->t->set_var('eqpresppi_checked','checked');
2817                         $this->t->fp('int_list','interviewer_list',True);
2818                 }
2819                 #print "selected interviewer: $interviewer<br>";
2820                 if($action == 'save') {
2821                         $notes = get_var('notes',array('POST'));
2822                         $this->db->query("UPDATE tc_interview set " .
2823                                          "   interview='" . $interview . "'" .
2824                                          ", interviewer='" . $interviewer . "'" .
2825                                          ", individual='" . $individual . "'" .
2826                                          ", date='" . $date . "'" .
2827                                          ", notes=\"" . $notes . "\"" .
2828                                          ", interview_type='" . $interview_type . "'" .
2829                                          " WHERE interview=" . $interview,__LINE__,__FILE__);
2830                         $this->ppi_view();
2831                         return false;
2832                 }
2833
2834                 if($action == 'insert') {
2835                         $notes = get_var('notes',array('POST'));
2836                         $this->db->query("INSERT INTO tc_interview (interviewer,individual,date,notes,interview_type) " .
2837                                          "VALUES ('" . $interviewer . "','" . $individual . "','" .
2838                                          $date . "',\"" . $notes . "\",'" . $interview_type  ."')",__LINE__,__FILE__);
2839                         $this->ppi_view();
2840                         return false;
2841                 }
2842
2843                 if($action == 'add') {
2844                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
2845                         $this->t->set_var('interview', '');
2846                         $this->t->set_var('interviewer', $interviewer);
2847                         $this->t->set_var('name',$name);
2848                         $this->t->set_var('individual',$individual);
2849                         $this->t->set_var('date','');
2850                         $this->t->set_var('notes','');
2851                         $this->t->set_var('interview_type',$interview_type);
2852                         $this->t->set_var('eqpresppi_checked','checked');
2853                         $this->t->set_var('lang_done','Cancel');
2854                         $this->t->set_var('lang_action','Adding New PPI');
2855                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview=' .
2856                                           $interview . '&action=' . 'insert'));
2857                 }
2858
2859                 if($action == 'edit' || $action == 'view') {
2860                         $sql = "SELECT * FROM tc_interview WHERE interview=" . $interview . " AND interview_type='ppi'";
2861                         $this->db->query($sql,__LINE__,__FILE__);
2862                         $this->db->next_record();
2863                         $this->t->set_var('interview',$interview);
2864                         $this->t->set_var('name',$name);
2865                         $this->t->set_var('interviewer', $this->db->f('interviewer'));
2866                         $this->t->set_var('individual',$this->db->f('individual'));
2867                         $this->t->set_var('date',$this->db->f('date'));
2868                         $this->t->set_var('notes',$this->db->f('notes'));
2869                         $this->t->set_var('interview_type',$this->db->f('interview_type'));
2870                         if($this->db->f('interview_type') == 'ppi') { $this->t->set_var('eqpresppi_checked','checked'); }
2871                 }
2872
2873                 if($action == 'edit') {
2874                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
2875                         $this->t->set_var('lang_done','Cancel');
2876                         $this->t->set_var('lang_action','Editing PPI');
2877                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview='. 
2878                                           $interview . '&action=' . 'save'));
2879                 }
2880
2881                 if($action == 'view') {
2882                         $date = $this->db->f('date');
2883                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
2884                         $this->t->set_var('readonly','READONLY');
2885                         $this->t->set_var('disabled','DISABLED');
2886                         $this->t->set_var('lang_done','Done');
2887                         $this->t->set_var('lang_action','Viewing PPI');
2888                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_update&interview=' .
2889                                           $interview . '&action=' . 'edit'));
2890                 }
2891
2892                 $this->t->set_var('lang_reset','Clear Form');
2893                 $this->t->set_var('lang_add','Add PPI');
2894                 $this->t->set_var('lang_save','Save Changes');
2895                 $this->t->set_var('edithandle','');
2896                 $this->t->set_var('addhandle','');
2897
2898                 $this->t->pfp('out','form');
2899
2900                 if($action == 'view') { $this->t->set_var('lang_save','Edit PPI'); }
2901                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
2902                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
2903
2904                 $this->save_sessiondata(); 
2905         }
2906
2907         function int_view()
2908         {
2909                 $this->t->set_file(array('int_view_t' => 'int_view.tpl'));
2910                 $this->t->set_block('int_view_t','district_list','list');
2911
2912                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2913                 $num_quarters = get_var('num_quarters',array('GET','POST'));
2914                 if($num_quarters == '') { $num_quarters = $this->default_int_num_quarters; }
2915                 $this->t->set_var('num_quarters',$num_quarters);
2916                 if($num_quarters == 1) { 
2917                         $this->t->set_var('lang_num_quarters','Quarter of History'); 
2918                 } else {
2919                         $this->t->set_var('lang_num_quarters','Quarters of History'); 
2920                 }
2921                 $this->t->set_var('lang_filter','Filter');
2922
2923                 $this->t->set_var('int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
2924                 $this->t->set_var('int_link_title','Hometeaching Interviews'); 
2925
2926                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
2927                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
2928
2929                 $this->t->set_var('title','Hometeaching Interviews'); 
2930
2931                 $num_months = $num_quarters * 3 - 1;
2932                 $current_month = $this->current_month;
2933                 if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
2934                 else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
2935                 else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
2936                 else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
2937
2938                 $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";
2939                 $this->db->query($sql,__LINE__,__FILE__);
2940                 $i=0;
2941                 while ($this->db->next_record()) {
2942                         $districts[$i]['district'] = $this->db->f('district');
2943                         $districts[$i]['name'] = $this->db->f('name');
2944                         $districts[$i]['supervisor'] = $this->db->f('supervisor');
2945                         $i++;
2946                 }
2947
2948                 $sql = "SELECT * FROM tc_individual where valid=1 ORDER BY individual ASC";
2949                 $this->db->query($sql,__LINE__,__FILE__);
2950                 $i=0;
2951                 while ($this->db->next_record()) {
2952                         $individual[$i] = $this->db->f('individual');
2953                         $indiv_name[$i] = $this->db->f('name');
2954                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
2955                         $i++;
2956                 }
2957                 array_multisort($indiv_name, $individual);
2958                 for($i=0; $i < count($individual); $i++) {
2959                         $id = $individual[$i];
2960                         $indivs[$id] = $indiv_name[$i];
2961                 }      
2962
2963                 $total_companionships = 0;
2964                 $this->nextmatchs->template_alternate_row_color(&$this->t);
2965                 for ($i=0; $i < count($districts); $i++) {
2966                         $this->t->set_var('district_number',$districts[$i]['district']);
2967                         $this->t->set_var('district_name',$districts[$i]['name']);      
2968                         $supervisor = $districts[$i]['supervisor'];
2969
2970                         // Select all the unique companionship numbers for this district
2971                         $sql = "SELECT distinct companionship FROM tc_companionship where valid=1 and district=". $districts[$i]['district'];
2972                         $this->db->query($sql,__LINE__,__FILE__);
2973                         $j=0; $unique_companionships = '';
2974                         while ($this->db->next_record()) {
2975                                 $unique_companionships[$j]['companionship'] = $this->db->f('companionship');
2976                                 $j++;
2977                         }
2978
2979                         $comp_width=250; $int_width=75; $table_width=$comp_width + $num_months*$int_width;
2980                         $table_data=""; $num_companionships = $j; $num_indivs = 0;
2981                         for($m=$num_months; $m >= 0; $m--) { $ints[$m] = 0; }
2982                         for ($j=0; $j < count($unique_companionships); $j++) {
2983                                 // Select all the companions in each companionship
2984                                 $sql = "SELECT * FROM tc_companion where valid=1 and ".
2985                                        "companionship=". $unique_companionships[$j]['companionship'];
2986                                 $this->db->query($sql,__LINE__,__FILE__);
2987                                 $k=0;
2988                                 $comp = $unique_companionships[$j]['companionship'];
2989                                 for($m=$num_months; $m >= 0; $m--) { $int_recorded[$comp][$m] = 0; }
2990                                 while ($this->db->next_record()) {
2991                                         // Get this companions information
2992                                         $num_indivs++;
2993                                         $companionship = $this->db->f('companionship');
2994                                         $individual = $this->db->f('individual');
2995                                         $name = $indivs[$individual];
2996                                         $phone = $indiv_phone[$individual];
2997                                         $link_data['menuaction'] = 'tc.tc.int_update';
2998                                         $link_data['companionship'] = $companionship;
2999                                         $link_data['interviewer'] = $supervisor;
3000                                         $link_data['individual'] = $individual;
3001                                         $link_data['name'] = $name;
3002                                         $link_data['interview'] = '';
3003                                         $link_data['interview_type'] = 'hti';
3004                                         $link_data['action'] = 'add';
3005                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
3006                                         $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
3007
3008                                         // Find out how many times Interviews were performed in the past $num_months for this individual
3009                                         $header_row="<th width=$comp_width><font size=-2>Companionship</th>";
3010                                         for($m=$num_months; $m >= 0; $m--) {
3011                                                 $month = $current_month - $m;
3012                                                 $year = $this->current_year;
3013                                                 if($month <= 0) { 
3014                                                         $remainder = $month; 
3015                                                         $month = 12 + $remainder; 
3016                                                         $year=$year-1; 
3017                                                 }
3018                                                 if($month < 10) { $month = "0"."$month"; }
3019                                                 $month_start = "$year"."-"."$month"."-"."01";
3020                                                 $month_end = "$year"."-"."$month"."-"."31";
3021                                                 $month = "$month"."/"."$year";
3022                                                 $sql = "SELECT * FROM tc_interview WHERE date >= '$month_start' AND date <= '$month_end' ".
3023                                                        "AND individual=" . $individual . " AND interview_type='hti' ORDER BY date DESC";
3024                                                 $this->db2->query($sql,__LINE__,__FILE__);
3025                                                 $header_row .= "<th width=$int_width><font size=-2>$month</th>";
3026
3027                                                 if(!$total_ints[$m]) { $total_ints[$m] = 0; }
3028                                                 if($this->db2->next_record()) {
3029                                                         if(!$int_recorded[$companionship][$m]) {
3030                                                                 $ints[$m]++; 
3031                                                                 $total_ints[$m]++; 
3032                                                                 $int_recorded[$companionship][$m]=1;
3033                                                         }
3034                                                         $link_data['menuaction'] = 'tc.tc.int_update';
3035                                                         $link_data['companionship'] = $companionship;
3036                                                         $link_data['interviewer'] = $this->db2->f('interviewer');
3037                                                         $link_data['individual'] = $individual;
3038                                                         $link_data['name'] = $name;
3039                                                         $link_data['interview'] = $this->db2->f('interview');
3040                                                         $link_data['action'] = 'view';
3041                                                         $link_data['interview_type'] = 'hti';
3042                                                         $date = $this->db2->f('date');
3043                                                         $date_array = explode("-",$date);
3044                                                         $month = $date_array[1];
3045                                                         $day   = $date_array[2];
3046                                                         $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);
3047                                                         $table_data .= '<td align=center><a href='.$link.'><img src="images/checkmark.gif">&nbsp;'.$month.'-'.$day.'</a></td>';
3048                                                 }
3049                                                 else { 
3050                                                         $table_data .= "<td>&nbsp;</td>"; 
3051                                                 }
3052                                         }
3053                                         $table_data .= "</tr>"; 
3054                                         $k++;
3055                                 }
3056                                 $table_data .= "<tr><td colspan=20><hr></td></tr>";
3057                         }
3058                         $total_companionships += $num_companionships;
3059                         $stat_data = "<tr><td><b><font size=-2>$num_companionships Companionships<br>Interview Totals:</font></b></td>";
3060
3061                         // Print the hometeaching interview stats
3062                         for($m=$num_months; $m >=0; $m--) {
3063                                 $month = $current_month - $m;
3064                                 if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
3065                                 $month_begins = $month % $this->monthly_hometeaching_interview_stats;
3066                                 //print "$month % $this->monthly_hometeaching_interview_stats = $month_begins <br>";
3067                                 if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
3068                                 if(($month_begins) == 1) { 
3069                                         $total = $ints[$m]; 
3070                                 } else { 
3071                                         $total += $ints[$m]; 
3072                                 }
3073                                 $percent = ceil(($total / $num_companionships)*100);
3074                                 $stat_data .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
3075                         }
3076                         $stat_data .= "</tr>";
3077
3078                         $this->t->set_var('table_width',$table_width);
3079                         $this->t->set_var('header_row',$header_row);
3080                         $this->t->set_var('table_data',$table_data);
3081                         $this->t->set_var('stat_data',$stat_data);
3082                         $this->t->fp('list','district_list',True);
3083                 }
3084
3085                 // Display the totals
3086                 $total = 0;
3087                 $totals = "<tr><td><b><font size=-2>$total_companionships Total Comps<br>Interview Totals:</font></b></td>";
3088                 for($m=$num_months; $m >=0; $m--) {
3089                         $month = $current_month - $m;
3090                         if($month < 0) { $month = 12 + $month; } // Handle going backwards over a year boundary
3091                         $month_begins = $month % $this->monthly_hometeaching_interview_stats;
3092                         if($this->monthly_hometeaching_interview_stats == 1) { $month_begins = 1; }
3093                         if(($month_begins) == 1) { 
3094                                 $total = $total_ints[$m]; 
3095                         } else { 
3096                                 $total += $total_ints[$m]; 
3097                         }
3098                         $percent = ceil(($total / $total_companionships)*100);
3099                         $totals .= "<td align=center><font size=-2><b>$total<br>$percent%</font></b></td>";
3100                 }
3101                 $totals .= "</tr>";
3102
3103                 $this->t->set_var('totals',$totals);
3104                 $this->t->pfp('out','int_view_t');
3105                 $this->save_sessiondata(); 
3106         }
3107
3108         function int_update()
3109         {
3110                 $this->t->set_file(array('form' => 'int_update.tpl'));
3111                 $this->t->set_block('form','interviewer_list','int_list');
3112                 $this->t->set_block('form','add','addhandle');
3113                 $this->t->set_block('form','edit','edithandle');
3114
3115                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_view'));
3116                 $this->t->set_var('readonly','');
3117                 $this->t->set_var('disabled','');
3118                 $this->t->set_var('eqpresppi','');
3119
3120                 $action = get_var('action',array('GET','POST'));
3121                 $companionship = get_var('companionship',array('GET','POST'));
3122                 $interviewer = get_var('interviewer',array('GET','POST'));      
3123                 $name = get_var('name',array('GET','POST'));
3124                 $interview = get_var('interview',array('GET','POST'));
3125                 $individual = get_var('individual',array('GET','POST'));
3126                 $date = get_var('date',array('GET','POST'));
3127                 $notes = get_var('notes',array('GET','POST'));
3128                 $interview_type = get_var('interview_type',array('GET','POST'));
3129
3130                 $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)";
3131                 $this->db2->query($sql,__LINE__,__FILE__);
3132                 while ($this->db2->next_record()) {
3133                         $indiv = $this->db2->f('individual');
3134                         $interviewer_name = $this->db2->f('name');
3135                         if($indiv == $interviewer) {
3136                                 $this->t->set_var('interviewer',$indiv . ' selected');
3137                         } else {
3138                                 $this->t->set_var('interviewer',$indiv);
3139                         }
3140                         $this->t->set_var('interviewer_name',$interviewer_name);
3141                         $this->t->fp('int_list','interviewer_list',True);
3142                 }
3143
3144                 if($action == 'save') {
3145                         $notes = get_var('notes',array('POST'));
3146                         $this->db->query("UPDATE tc_interview set " .
3147                                          "   interview='" . $interview . "'" .
3148                                          ", interviewer='" . $interviewer . "'" .
3149                                          ", individual='" . $individual . "'" .
3150                                          ", date='" . $date . "'" .
3151                                          ", notes=\"" . $notes . "\"" .
3152                                          ", interview_type='" . $interview_type . "'" .
3153                                          " WHERE interview=" . $interview,__LINE__,__FILE__);
3154                         $this->int_view();
3155                         return false;
3156                 }
3157
3158                 if($action == 'insert') {
3159                         $notes = get_var('notes',array('POST'));
3160                         $this->db->query("INSERT INTO tc_interview (interviewer,individual,date,notes,interview_type) " .
3161                                          "VALUES ('" . $interviewer . "','" . $individual . "','" .
3162                                          $date . "',\"" . $notes ."\",'" . $interview_type . "')",__LINE__,__FILE__);
3163                         $this->int_view();
3164                         return false;
3165                 }
3166
3167                 if($action == 'add') {
3168                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3169                         $this->t->set_var('interview', '');
3170                         $this->t->set_var('interviewer', $interviewer);
3171                         $this->t->set_var('name',$name);
3172                         $this->t->set_var('individual',$individual);
3173                         $this->t->set_var('date','');
3174                         $this->t->set_var('notes','');
3175                         $this->t->set_var('interview_type',$interview_type);
3176                         $this->t->set_var('lang_done','Cancel');
3177                         $this->t->set_var('lang_action','Adding New Interview');
3178                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
3179                                           $interview . '&action=' . 'insert'));
3180                 }
3181
3182                 if($action == 'edit' || $action == 'view') {
3183                         $sql = "SELECT * FROM tc_interview WHERE interview=" . $interview . " AND interview_type='hti'";
3184                         $this->db->query($sql,__LINE__,__FILE__);
3185                         $this->db->next_record();
3186                         $this->t->set_var('interview',$interview);
3187                         $this->t->set_var('name',$name);
3188                         $this->t->set_var('interviewer', $this->db->f('interviewer'));
3189                         $this->t->set_var('individual',$this->db->f('individual'));
3190                         $this->t->set_var('date',$this->db->f('date'));
3191                         $this->t->set_var('notes',$this->db->f('notes'));
3192                         $this->t->set_var('interview_type',$this->db->f('interview_type'));
3193                         if($this->db->f('interview_type') == 'ppi') { $this->t->set_var('eqpresppi_checked','checked'); }
3194                 }
3195
3196                 if($action == 'edit') {
3197                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3198                         $this->t->set_var('lang_done','Cancel');
3199                         $this->t->set_var('lang_action','Editing Interview');
3200                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
3201                                           $interview . '&action=' . 'save'));
3202                 }
3203
3204                 if($action == 'view') {
3205                         $date = $this->db->f('date');
3206                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3207                         $this->t->set_var('readonly','READONLY');
3208                         $this->t->set_var('disabled','DISABLED');
3209                         $this->t->set_var('lang_done','Done');
3210                         $this->t->set_var('lang_action','Viewing Interview');
3211                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_update&interview=' .
3212                                           $interview . '&action=' . 'edit'));
3213                 }
3214
3215                 $this->t->set_var('lang_reset','Clear Form');
3216                 $this->t->set_var('lang_add','Add Interview');
3217                 $this->t->set_var('lang_save','Save Changes');
3218                 $this->t->set_var('edithandle','');
3219                 $this->t->set_var('addhandle','');
3220
3221                 $this->t->pfp('out','form');
3222
3223                 if($action == 'view') { $this->t->set_var('lang_save','Edit Interview'); }
3224                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3225                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
3226
3227                 $this->save_sessiondata(); 
3228         }
3229
3230         function vis_view()
3231         {
3232                 $this->t->set_file(array('vis_view_t' => 'vis_view.tpl'));
3233                 $this->t->set_block('vis_view_t','visit_list','list1');
3234                 $this->t->set_block('vis_view_t','family_list','list2');
3235
3236                 $this->t->set_var('lang_name','Family Name');
3237                 $this->t->set_var('lang_date','Date');
3238
3239                 $this->t->set_var('vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
3240                 $this->t->set_var('vis_link_title','View Yearly Visits');
3241
3242                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
3243                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3244
3245                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
3246                 $num_years = get_var('num_years',array('GET','POST'));
3247                 if($num_years == '') { $num_years = $this->default_vis_num_years; }
3248                 $this->t->set_var('num_years',$num_years);
3249                 if($num_years == 1) { 
3250                         $this->t->set_var('lang_num_years','Year of History'); 
3251                 } else {  
3252                         $this->t->set_var('lang_num_years','Years of History'); 
3253                 }
3254                 $this->t->set_var('lang_filter','Filter');
3255
3256                 $year = date('Y') - $num_years + 1;
3257                 $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
3258
3259                 $sql = "SELECT * FROM tc_visit WHERE companionship=0 and date > '$year_start' ORDER BY date DESC";
3260                 $this->db->query($sql,__LINE__,__FILE__);
3261                 $total_records = $this->db->num_rows();
3262
3263                 $i = 0;
3264                 while ($this->db->next_record()) {
3265                         $visit_list[$i]['visit'] = $this->db->f('visit');
3266                         $visit_list[$i]['family'] = $this->db->f('family');
3267                         $visit_list[$i]['date']  = $this->db->f('date');
3268                         $i++;
3269                 }
3270
3271                 for ($i=0; $i < count($visit_list); $i++) {
3272                         $this->nextmatchs->template_alternate_row_color(&$this->t);
3273
3274                         $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'";
3275                         $this->db->query($sql,__LINE__,__FILE__);
3276                         $this->db->next_record();
3277
3278                         $this->t->set_var('family',$visit_list[$i]['family']);
3279                         $this->t->set_var('family_name',$this->db->f('name'));
3280                         $this->t->set_var('date',$visit_list[$i]['date']);
3281
3282                         $link_data['menuaction'] = 'tc.tc.vis_update';
3283                         $link_data['visit'] = $visit_list[$i]['visit'];
3284                         $link_data['name'] = $this->db->f('name');
3285                         $link_data['date'] = $visit_list[$i]['date'];
3286                         $link_data['action'] = 'view';
3287                         $this->t->set_var('view',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3288                         $this->t->set_var('lang_view','View');
3289
3290                         $link_data['menuaction'] = 'tc.tc.vis_update';
3291                         $link_data['visit'] = $visit_list[$i]['visit'];
3292                         $link_data['name'] = $this->db->f('name');
3293                         $link_data['date'] = $visit_list[$i]['date'];
3294                         $link_data['action'] = 'edit';
3295                         $this->t->set_var('edit',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3296                         $this->t->set_var('lang_edit','Edit');
3297
3298                         $this->t->fp('list1','visit_list',True);
3299                 }
3300
3301                 // List the families that are available to record a visit against
3302                 $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'";
3303                 $this->db->query($sql,__LINE__,__FILE__);
3304                 $total_records = $this->db->num_rows();
3305
3306                 $i = 0;
3307                 while ($this->db->next_record()) {
3308                         $family_names[$i] = $this->db->f('name');
3309                         $family_ids[$i] = $this->db->f('family');
3310                         $i++;
3311                 } array_multisort($family_names, $family_ids);
3312
3313                 for ($i=0; $i < count($family_names); $i++) {
3314                         $link_data['menuaction'] = 'tc.tc.vis_update';
3315                         $link_data['visit'] = '';
3316                         $link_data['family'] = $family_ids[$i];
3317                         $link_data['action'] = 'add';
3318                         $link_data['name'] = $family_names[$i];
3319                         $this->t->set_var('add',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3320
3321                         $this->t->set_var('name',$family_names[$i]);
3322                         if(($i+1) % 3 == 0) { 
3323                                 $this->t->set_var('table_sep',"</td></tr><tr>"); 
3324                         } else { 
3325                                 $this->t->set_var('table_sep',"</td>"); 
3326                         }
3327                         if(($i) % 3 == 0) { $this->nextmatchs->template_alternate_row_color(&$this->t); }
3328
3329                         $this->t->fp('list2','family_list',True);
3330                 }   
3331
3332                 $this->t->pfp('out','vis_view_t');
3333                 $this->save_sessiondata(); 
3334         }
3335
3336         function vis_update()
3337         {
3338                 $this->t->set_file(array('form' => 'vis_update.tpl'));
3339                 $this->t->set_block('form','add','addhandle');
3340                 $this->t->set_block('form','edit','edithandle');
3341
3342                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_view'));
3343                 $this->t->set_var('readonly','');
3344                 $this->t->set_var('disabled','');
3345
3346                 $action = get_var('action',array('GET','POST'));
3347                 $visit = get_var('visit',array('GET','POST'));
3348                 $family = get_var('family',array('GET','POST'));
3349                 $name = get_var('name',array('GET','POST'));
3350                 $date = get_var('date',array('GET','POST'));
3351                 $notes = get_var('notes',array('GET','POST'));
3352                 $companionship = 0;
3353
3354                 if($action == 'save') {
3355                         $notes = get_var('notes',array('POST'));
3356                         $this->db->query("UPDATE tc_visit set " .
3357                                          "  date='" . $date . "'" .
3358                                          ", notes=\"" . $notes . "\"" .
3359                                          " WHERE visit=" . $visit,__LINE__,__FILE__);
3360                         $this->vis_view();
3361                         return false;
3362                 }
3363
3364                 if($action == 'insert') {
3365                         $notes = get_var('notes',array('POST'));
3366                         $this->db->query("INSERT INTO tc_visit (family,companionship,date,notes) " .
3367                                          "VALUES ('" . $family . "','" . $companionship . "','" .
3368                                          $date . "',\"" . $notes . "\")",__LINE__,__FILE__);
3369                         $this->vis_view();
3370                         return false;
3371                 }
3372
3373                 if($action == 'add') {
3374                         $this->t->set_var('cal_date',$this->jscal->input('date','','','','','','',$this->cal_options));
3375                         $this->t->set_var('family', $family);
3376                         $this->t->set_var('visit', '');
3377                         $this->t->set_var('name', $name);
3378                         $this->t->set_var('date','');
3379                         $this->t->set_var('notes','');
3380                         $this->t->set_var('lang_done','Cancel');
3381                         $this->t->set_var('lang_action','Adding New Visit');
3382                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_update&family=' .
3383                                           $family . '&action=' . 'insert'));
3384                 }
3385
3386                 if($action == 'edit' || $action == 'view') {
3387                         $sql = "SELECT * FROM tc_visit WHERE visit=".$visit;
3388                         $this->db->query($sql,__LINE__,__FILE__);
3389                         $this->db->next_record();
3390                         $this->t->set_var('visit',$visit);
3391                         $this->t->set_var('name',$name);
3392                         $this->t->set_var('family', $family);
3393                         $this->t->set_var('date',$this->db->f('date'));
3394                         $this->t->set_var('notes',$this->db->f('notes'));
3395                 }
3396
3397                 if($action == 'edit') {
3398                         $this->t->set_var('cal_date',$this->jscal->input('date',$date,'','','','','',$this->cal_options));
3399                         $this->t->set_var('lang_done','Cancel');
3400                         $this->t->set_var('lang_action','Editing Visit');
3401                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_update&visit=' .
3402                                           $visit . '&action=' . 'save'));
3403                 }
3404
3405                 if($action == 'view') {
3406                         $date = $this->db->f('date');
3407                         $this->t->set_var('cal_date','<input type=text size="10" maxlength="10" name="date" value="'.$date.'" readonly>');
3408                         $this->t->set_var('readonly','READONLY');
3409                         $this->t->set_var('disabled','DISABLED');
3410                         $this->t->set_var('lang_done','Done');
3411                         $this->t->set_var('lang_action','Viewing Visit');
3412                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_update&visit=' .
3413                                           $visit . '&action=' . 'edit'));
3414                 }
3415
3416                 $this->t->set_var('lang_reset','Clear Form');
3417                 $this->t->set_var('lang_add','Add Visit');
3418                 $this->t->set_var('lang_save','Save Changes');
3419                 $this->t->set_var('edithandle','');
3420                 $this->t->set_var('addhandle','');
3421
3422                 $this->t->pfp('out','form');
3423
3424                 if($action == 'view') { $this->t->set_var('lang_save','Edit Visit'); }
3425                 if($action == 'edit' || $action == 'view') { $this->t->pfp('addhandle','edit'); }
3426                 if($action == 'add') { $this->t->pfp('addhandle','add'); }
3427
3428                 $this->save_sessiondata(); 
3429         }
3430
3431         function att_view()
3432         {
3433                 $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3434                 $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3435                 $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3436
3437                 $this->t->set_file(array('att_view_t' => 'att_view.tpl'));
3438                 $this->t->set_block('att_view_t','act_list','list');
3439
3440                 $this->t->set_block('att_view_t','month_list','list1');
3441                 $this->t->set_block('att_view_t','header_list','list2');
3442                 $this->t->set_block('att_view_t','individual_list','list3');
3443
3444                 $this->t->set_var('linkurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_view'));
3445                 $num_quarters = get_var('num_quarters',array('GET','POST'));
3446                 if($num_quarters == '') { $num_quarters = $this->default_att_num_quarters; }
3447                 $this->t->set_var('num_quarters',$num_quarters);
3448                 $this->t->set_var('lang_filter','Filter');
3449                 if($num_quarters == 1) { 
3450                         $this->t->set_var('lang_num_quarters','Quarter of History'); 
3451                 } else { 
3452                         $this->t->set_var('lang_num_quarters','Quarters of History'); 
3453                 }
3454
3455                 $num_months = $num_quarters * 3;
3456                 $current_month = $this->current_month;
3457                 if($current_month >= 1 && $current_month <= 3) { $current_month=3; }
3458                 else if($current_month >= 4 && $current_month <= 6) { $current_month=6; }
3459                 else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
3460                 else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
3461
3462                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1";
3463                 $this->db->query($sql,__LINE__,__FILE__);
3464                 $i=0;
3465                 while ($this->db->next_record()) {
3466                         $individual_name[$i] = $this->db->f('name');
3467                         $individual[$i] = $this->db->f('individual');
3468                         $i++;
3469                 }
3470                 array_multisort($individual_name, $individual);
3471
3472                 // Create a list of sunday dates for a window of 3 months back and current month
3473                 $i=0; 
3474                 $last_time = 0; 
3475                 $found_sunday = 0;
3476                 $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, ($current_month-$num_months)+1, 1, date("y")));
3477                 $last_date = explode("-",$sunday_list[0]['date']);
3478                 $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3479                 $time_limit = mktime(0, 0, 0, $current_month, 31, date("y"));
3480                 while($last_time < $time_limit) {
3481                         $day = date("w",$last_time);
3482                         if(date("w",$last_time) == 0) {
3483                                 $sunday_list[$i]['date'] = date("Y-m-d", $last_time);
3484                                 $last_date = explode("-",$sunday_list[$i]['date']);
3485                                 $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3486                                 $sunday_list[$i]['day'] = $last_date[2];
3487                                 $sunday_list[$i]['month'] = date("M",$last_time);
3488                                 $sunday_list[$i]['year'] = $last_date[0];
3489                                 $found_sunday = 1;
3490                                 $last_date = $sunday_list[$i]['date'];
3491                         }
3492                         $last_time += 90000;
3493                         if($found_sunday) { $i++; $found_sunday=0; }
3494                 }
3495
3496                 $total_individuals = count($individual);
3497                 $old_month=$sunday_list[0]['month']; $span=0;
3498                 for ($i=0; $i < count($sunday_list); $i++) {
3499                         $date = $sunday_list[$i]['date'];
3500                         $this->t->set_var('date',$sunday_list[$i]['date']);
3501                         $this->t->set_var('day',$sunday_list[$i]['day']);
3502                         if(($old_month != $sunday_list[$i]['month']) || $i == count($sunday_list)-1) {
3503                                 if($i == count($sunday_list)-1) { $span++; }
3504                                 $cur_month = $sunday_list[$i]['month'];
3505                                 $old_month = $sunday_list[$i]['month'];   
3506                                 $link_data['menuaction'] = 'tc.tc.att_update';
3507                                 $link_data['month'] = $sunday_list[$i-1]['month'];
3508                                 $link_data['year'] = $sunday_list[$i-1]['year'];
3509                                 $link_data['action'] = 'update_month';
3510                                 $cur_month = $sunday_list[$i-1]['month'];
3511                                 $cur_year = $sunday_list[$i-1]['year'];
3512                                 $header_row .= "<th><font size=-3>$cur_month&nbsp;$cur_year</font></th>";
3513                                 $this->t->set_var('update_month',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3514                                 $this->t->set_var('month',$sunday_list[$i-1]['month']);
3515                                 $this->t->set_var('year',$sunday_list[$i-1]['year']);
3516                                 $this->t->set_var('span',$span); $span=0;
3517                                 $this->t->fp('list1','month_list',True);
3518                         }
3519                         $span++;
3520                 }
3521                 $this->t->set_var('total_individuals',$total_individuals);
3522                 $this->t->set_var('header_row',$header_row);
3523
3524                 $individual_width=200; $att_width=25; $total_width=$individual_width; 
3525                 for ($i=0; $i < count($sunday_list); $i++) {
3526                         $link_data['menuaction'] = 'tc.tc.att_update';
3527                         $link_data['month'] = $sunday_list[$i]['month'];
3528                         $link_data['year'] = $sunday_list[$i]['year'];
3529                         $link_data['day'] = $sunday_list[$i]['day'];
3530                         $link_data['date'] = $sunday_list[$i]['date'];
3531                         $link_data['action'] = 'update_day';
3532                         $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3533                         $this->t->set_var('date',$sunday_list[$i]['date']);
3534                         $this->t->set_var('day',$sunday_list[$i]['day']);
3535                         $this->t->set_var('month',$sunday_list[$i]['month']);
3536                         $this->t->set_var('year',$sunday_list[$i]['year']);
3537                         $this->t->fp('list2','header_list',True);
3538                         $total_width += $att_width;
3539                         $attendance[$monthnum[$sunday_list[$i]['month']]]=0;
3540                 }
3541
3542                 for ($i=0; $i < count($individual); $i++) {
3543                         $att_table = "";
3544                         $this->nextmatchs->template_alternate_row_color(&$this->t);
3545                         $this->t->set_var('individual_name',$individual_name[$i]);
3546                         #print "checking for individual: " . $individual[$i] . "<br>";
3547                         for ($j=0; $j < count($sunday_list); $j++) {
3548                                 #print "checking for date: " .  $sunday_list[$j]['date'] . "<br>";
3549                                 #print "SELECT * FROM tc_attendance WHERE date='"
3550                                 #  . $sunday_list[$j]['date'] . "' AND individual=" . $individual[$i] . "<br>";
3551                                 $sql = "SELECT * FROM tc_attendance WHERE date='" .
3552                                        $sunday_list[$j]['date'] . "' AND individual=" . $individual[$i];
3553                                 $this->db->query($sql,__LINE__,__FILE__);
3554                                 if($this->db->next_record()) {
3555                                         $cur_month = $sunday_list[$j]['month'];
3556                                 if($attended[$i][$cur_month] != 1) {
3557                                         $attended[$i][$cur_month]=1;
3558                                         $attendance[$monthnum[$cur_month]]++;
3559                                 } 
3560                                         $att_table .= '<td align=center><img src="images/checkmark.gif"></td>';
3561                                 } else {
3562                                         $att_table .= '<td>&nbsp;</td>';
3563                                 }
3564                         }
3565                         $this->t->set_var('att_table',$att_table);
3566                         $this->t->fp('list3','individual_list',True);
3567                 }
3568                 $this->t->set_var('total_width',$total_width);
3569                 $this->t->set_var('individual_width',$individual_width);
3570                 $this->t->set_var('att_width',$att_width);
3571
3572                 # Now calculate attendance for these months
3573                 $attendance_str = "";
3574                 $nonattendance_str = "";
3575                 $aveattendance_str = "";
3576                 $avenonattendance_str = "";
3577                 $num_months=0;
3578                 $ave_total_attended=0;
3579                 ksort($attendance);
3580                 foreach($attendance as $att => $value) {
3581                         $total_attended = $attendance[$att];
3582                         $ave_total_attended += $attendance[$att]; $num_months++;
3583                         $percent = ceil(($total_attended / $total_individuals)*100);
3584                         $attendance_str.="<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3585                         $total_nonattended = $total_individuals - $total_attended;
3586                         $percent = ceil(($total_nonattended / $total_individuals)*100);
3587                         $nonattendance_str.="<td align=center><font size=-2><b>$total_nonattended ($percent%)</b></font></td>";
3588
3589                         $total_attended = ceil(($ave_total_attended / $num_months));
3590                         $percent = ceil(($total_attended / $total_individuals)*100);
3591                         $aveattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3592                         $total_attended = $total_individuals - ceil(($ave_total_attended / $num_months));
3593                         $percent = ceil(($total_attended / $total_individuals)*100);
3594                         $avenonattendance_str .= "<td align=center><font size=-2><b>$total_attended ($percent%)</b></font></td>";
3595                 }
3596
3597                 $this->t->set_var('attendance',$attendance_str);
3598                 $this->t->set_var('aveattendance',$aveattendance_str);
3599                 $this->t->set_var('nonattendance',$nonattendance_str);
3600                 $this->t->set_var('avenonattendance',$avenonattendance_str);
3601
3602                 $this->t->pfp('out','att_view_t');
3603                 $this->save_sessiondata(); 
3604         }
3605
3606         function att_update()
3607         {
3608                 $monthnum['Jan']=1; $monthnum['Feb']=2; $monthnum['Mar']=3; $monthnum['Apr']=4;
3609                 $monthnum['May']=5; $monthnum['Jun']=6; $monthnum['Jul']=7; $monthnum['Aug']=8;
3610                 $monthnum['Sep']=9; $monthnum['Oct']=10; $monthnum['Nov']=11; $monthnum['Dec']=12;
3611
3612                 $this->t->set_file(array('form' => 'att_update.tpl'));
3613                 $this->t->set_block('form','edit','edithandle');
3614
3615                 $this->t->set_block('form','month_list','list1');
3616                 $this->t->set_block('form','header_list','list2');
3617                 $this->t->set_block('form','individual_list','list3');
3618
3619                 $this->t->set_var('done_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_view'));
3620
3621                 $action = get_var('action',array('GET','POST'));
3622                 $month = get_var('month',array('GET','POST'));
3623                 $year = get_var('year',array('GET','POST'));
3624                 $day = get_var('day',array('GET','POST'));
3625                 $date = get_var('date',array('GET','POST'));
3626
3627                 if($action == 'save_month' || $action == 'save_day') {
3628                         $new_data = get_var('individuals_attended',array('POST'));
3629                         $month = $monthnum[$month]; if($month < 10) { $month = "0" . $month; }
3630
3631                         if($action == 'save_month') {
3632                                 $this->db->query("DELETE from tc_attendance where date LIKE '".$year."-".$month."-%'",__LINE__,__FILE__);
3633                         }
3634
3635                         if($action == 'save_day') {
3636                                 $this->db->query("DELETE from tc_attendance where date LIKE '".$year."-".$month."-".$day."'",__LINE__,__FILE__);
3637                         }   
3638
3639                         foreach ($new_data as $data) {
3640                                 $data_array = explode("-",$data);
3641                                 $indiv = $data_array[0];
3642                                 $date  = "$data_array[1]-$data_array[2]-$data_array[3]";              
3643                                 $this->db->query("INSERT INTO tc_attendance (individual,date) " .
3644                                                  "VALUES (" . $indiv . ",'". $date . "')",__LINE__,__FILE__);
3645                         }
3646
3647                         $this->att_view();
3648                         return false;
3649                 }
3650
3651                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1";
3652                 $this->db->query($sql,__LINE__,__FILE__);
3653                 $i=0;
3654                 while ($this->db->next_record()) {
3655                         $indiv_name[$i] = $this->db->f('name');
3656                         $individual[$i] = $this->db->f('individual');
3657                         $indiv_attending[$individual[$i]] = $this->db->f('attending');
3658                         $i++;
3659                 }
3660                 array_multisort($indiv_name, $individual);
3661
3662                 if($action == 'update_month') {
3663                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_update&action=save_month'));
3664                         $i=0; 
3665                         $last_time = 0; 
3666                         $found_sunday = 0;
3667                         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], 1, $year));
3668                         $last_date = explode("-",$sunday_list[0]['date']);
3669                         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3670                         $time_limit = mktime(0, 0, 0, $monthnum[$month], 31, $year);
3671                         while($last_time <= $time_limit) {
3672                                 $day = date("w",$last_time);
3673                                         if(date("w",$last_time) == 0) { 
3674                                         $sunday_list[$i]['date'] = date("Y-m-d", $last_time); 
3675                                         $last_date = explode("-",$sunday_list[$i]['date']);
3676                                         $last_time = mktime(0, 0, 0, $last_date[1], $last_date[2], $last_date[0]);
3677                                         $sunday_list[$i]['day'] = $last_date[2];
3678                                         $sunday_list[$i]['month'] = date("M",$last_time);
3679                                         $sunday_list[$i]['year'] = $last_date[0];
3680                                         $found_sunday = 1; 
3681                                 }
3682                                 $last_time += 90000;
3683                                 if($found_sunday) { $i++; $found_sunday=0; }
3684                         }
3685
3686                         $this->t->set_var('span', $i);
3687                         $this->t->set_var('month',$sunday_list[$i-1]['month']);
3688                         $this->t->set_var('year',$sunday_list[$i-1]['year']);
3689                         $this->t->fp('list1','month_list',True);
3690                         $indiv_width=200; $att_width=25; $total_width=$indiv_width;
3691                         for ($i=0; $i < count($sunday_list); $i++) {
3692                                 $link_data['menuaction'] = 'tc.tc.att_update';
3693                                 $link_data['month'] = $sunday_list[$i]['month'];
3694                                 $link_data['year'] = $sunday_list[$i]['year'];
3695                                 $link_data['day'] = $sunday_list[$i]['day'];
3696                                 $link_data['date'] = $sunday_list[$i]['date'];
3697                                 $link_data['action'] = 'update_day';
3698                                 $this->t->set_var('update_day',$GLOBALS['phpgw']->link('/tc/index.php',$link_data));
3699                                 $this->t->set_var('date',$sunday_list[$i]['date']);
3700                                 $this->t->set_var('day',$sunday_list[$i]['day']);
3701                                 $this->t->set_var('month',$sunday_list[$i]['month']);
3702                                 $this->t->set_var('year',$sunday_list[$i]['year']);
3703                                 $this->t->fp('list2','header_list',True);
3704                                 $total_width += $att_width;
3705                         }     
3706                 }
3707
3708                 if($action == 'update_day') {
3709                         $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.att_update&action=save_day'));
3710                         $sunday_list[0]['date'] = date("Y-m-d", mktime(0, 0, 0, $monthnum[$month], $day, $year));
3711                         $this->t->set_var('month',$month);
3712                         $this->t->set_var('year',$year);
3713                         $this->t->fp('list1','month_list',True);
3714                         $this->t->set_var('date',$date);
3715                         $this->t->set_var('day',$day);
3716                         $this->t->set_var('month',$month);
3717                         $this->t->set_var('year',$year);
3718                         $this->t->fp('list2','header_list',True);
3719                 }           
3720
3721                 for ($i=0; $i < count($individual); $i++) {
3722                         $att_table = "";
3723                         $this->nextmatchs->template_alternate_row_color(&$this->t);
3724                         $this->t->set_var('individual_name',$indiv_name[$i]);
3725                         for ($j=0; $j < count($sunday_list); $j++) {
3726                                 $sql = "SELECT * FROM tc_attendance WHERE date='" .
3727                                        $sunday_list[$j]['date'] . "' AND individual=" . $individual[$i];
3728                                 $this->db->query($sql,__LINE__,__FILE__);
3729                                 $value = $individual[$i] . "-" . $sunday_list[$j]['date'];
3730                                 if($this->db->next_record()) {
3731                                         $att_table .= '<td align=center><input type="checkbox" name="individuals_attended[]" value="'.$value.'" checked></td>';
3732                                 } else if($indiv_attending[$individual[$i]] == 1) {
3733                                         $att_table .= '<td align=center><input type="checkbox" name="individuals_attended[]" value="'.$value.'" checked></td>';
3734                                 } else {
3735                                         $att_table .= '<td align=center><input type="checkbox" name="individuals_attended[]" value="'.$value.'"></td>';
3736                                 }
3737                         }
3738                         $this->t->set_var('att_table',$att_table);
3739                         $this->t->fp('list3','individual_list',True);
3740                 } 
3741
3742                 $this->t->set_var('lang_done', 'Cancel');
3743                 $this->t->set_var('lang_reset','Clear Form');
3744                 $this->t->set_var('lang_save','Save Changes');
3745
3746                 $this->t->pfp('out','form');
3747                 $this->t->pfp('addhandle','edit');
3748
3749                 $this->save_sessiondata();       
3750         }
3751
3752         function dir_view()
3753         {
3754                 $this->t->set_file(array('dir_view_t' => 'dir_view.tpl'));
3755                 $this->t->set_block('dir_view_t','dir_list','list');
3756
3757                 $sql = "SELECT * FROM tc_individual where valid=1 and hh_position='Head of Household' ORDER BY name ASC";
3758                 $this->db->query($sql,__LINE__,__FILE__);
3759                 $i=0;
3760                 while ($this->db->next_record()) {
3761                         $parent[$i]['id'] = $this->db->f('individual');
3762                         $parent[$i]['name'] = $this->db->f('name');
3763                         $parent[$i]['phone'] = $this->db->f('phone');
3764                         $parent[$i]['address'] = $this->db->f('address');
3765                         $i++;
3766                 }   
3767
3768                 for ($i=0; $i < count($parent); $i++) {
3769                         $name = $parent[$i]['name'];
3770                         $phone = $parent[$i]['phone'];
3771                         $address = $parent[$i]['address'];
3772                         $this->t->set_var('name', $name);
3773                         $this->t->set_var('address', $address);
3774                         $this->t->set_var('phone', $phone);
3775                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3776                         $this->t->set_var('tr_color',$tr_color);
3777                         $this->t->fp('list','dir_list',True);
3778                         //print "$phone $name $address<br>";
3779                 }
3780                 $this->t->pfp('out','dir_view_t');
3781                 $this->save_sessiondata();   
3782         }
3783   
3784         function org_view()
3785         {
3786                 $this->t->set_file(array('org_view_t' => 'org_view.tpl'));
3787                 $this->t->set_block('org_view_t','calling_list','list1');
3788                 $this->t->set_block('org_view_t','org_list','list2');
3789
3790                 # Display a list ordered alphabetically
3791                 $sql = "SELECT * FROM tc_calling AS tc JOIN tc_individual AS ti WHERE tc.individual=ti.individual ORDER BY name ASC";
3792                 $this->db->query($sql,__LINE__,__FILE__);
3793                 $i=0;
3794                 while ($this->db->next_record()) {
3795                         $calling[$i]['name'] = $this->db->f('name');
3796                         $calling[$i]['position'] = $this->db->f('position');
3797                         $calling[$i]['sustained'] = $this->db->f('sustained');
3798                         $calling[$i]['organization'] = $this->db->f('organization');
3799                         $i++;
3800                 }   
3801                 for ($i=0; $i < count($calling); $i++) {
3802                         $name = $calling[$i]['name'];
3803                         $position = $calling[$i]['position'];
3804                         $sustained = $calling[$i]['sustained'];
3805                         $organization = $calling[$i]['organization'];
3806                         $this->t->set_var('name', $name);
3807                         $this->t->set_var('position', $position);
3808                         $this->t->set_var('sustained', $sustained);
3809                         $this->t->set_var('organization', $organization);
3810                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3811                         $this->t->set_var('tr_color',$tr_color);
3812                         $this->t->fp('list1','calling_list',True);
3813                 }
3814
3815                 # Display a list ordered by organization
3816                 $sql = "SELECT * FROM tc_calling AS tc JOIN tc_individual AS ti where tc.individual=ti.individual ORDER BY organization ASC";
3817                 $this->db->query($sql,__LINE__,__FILE__);
3818                 $i=0;
3819                 while ($this->db->next_record()) {
3820                         $calling[$i]['name'] = $this->db->f('name');
3821                         $calling[$i]['position'] = $this->db->f('position');
3822                         $calling[$i]['sustained'] = $this->db->f('sustained');
3823                         $calling[$i]['organization'] = $this->db->f('organization');
3824                         $i++;
3825                 }   
3826                 for ($i=0; $i < count($calling); $i++) {
3827                         $name = $calling[$i]['name'];
3828                         $position = $calling[$i]['position'];
3829                         $sustained = $calling[$i]['sustained'];
3830                         $organization = $calling[$i]['organization'];
3831                         $this->t->set_var('name', $name);
3832                         $this->t->set_var('position', $position);
3833                         $this->t->set_var('sustained', $sustained);
3834                         $this->t->set_var('organization', $organization);
3835                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
3836                         $this->t->set_var('tr_color',$tr_color);
3837                         $this->t->fp('list2','org_list',True);
3838                 }
3839
3840                 $this->t->pfp('out','org_view_t');
3841                 $this->save_sessiondata();   
3842         }
3843   
3844         function schedule()
3845         {
3846                 $this->t->set_file(array('sched_t' => 'schedule.tpl'));
3847                 $this->t->set_block('sched_t','presidency_list','list');
3848
3849                 $action = get_var('action',array('GET','POST'));
3850
3851                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.schedule&action=save'));
3852                 $this->t->set_var('title','Scheduling Tool');
3853
3854                 $this->t->set_var('lang_save','Save Schedule');
3855                 $this->t->set_var('lang_reset','Cancel');
3856
3857                 $this->t->set_var('schedule_vis_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched'));
3858                 $this->t->set_var('schedule_vis_link_title','Schedule Yearly Visits');
3859
3860                 $this->t->set_var('schedule_int_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched'));
3861                 $this->t->set_var('schedule_int_link_title','Schedule Hometeaching Interviews');
3862
3863                 $this->t->set_var('schedule_ppi_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched'));
3864                 $this->t->set_var('schedule_ppi_link_title','Schedule ' . $this->ppi_frequency_label . ' PPIs');
3865
3866                 $date_width=160; $time_width=220; $indiv_width=170; $family_width=180; $location_width=100;
3867                 $table_width=$date_width + $time_width + $indiv_width + $family_width + $location_width;
3868                 $header_row = "<th width=$date_width><font size=-2>Date</th>";
3869                 $header_row.= "<th width=$time_width><font size=-2>Time</th>";      
3870                 $header_row.= "<th width=$indiv_width><font size=-2>Individual</th>";
3871                 $header_row.= "<th width=$family_width><font size=-2>Family</th>";
3872                 $header_row.= "<th width=$location_width><font size=-2>Location</th>";
3873                 $table_data = "";
3874
3875                 $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";
3876                 $this->db->query($sql,__LINE__,__FILE__);
3877                 $i=0;
3878                 while ($this->db->next_record()) {
3879                         $presidency_data[$i]['id'] = $this->db->f('presidency');
3880                         $presidency_data[$i]['name'] = $this->db->f('name');
3881                         $presidency_data[$i]['indiv'] = $this->db->f('individual');
3882                         $presidency2name[$presidency_data[$i]['id']] = $presidency_data[$i]['name'];
3883                         $presidency2indiv[$presidency_data[$i]['id']] = $presidency_data[$i]['indiv'];
3884                         $i++;
3885                 }
3886
3887                 $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";
3888                 $this->db->query($sql,__LINE__,__FILE__);
3889                 $i=0;
3890                 while ($this->db->next_record()) {
3891                         $family_id[$i] = $this->db->f('family');
3892                         $family_name[$i] = $this->db->f('name');
3893                         $familyid2name[$family_id[$i]] = $family_name[$i];
3894                         $familyid2address[$family_id[$i]] = $this->db->f('address');
3895                         $i++;
3896                 }
3897                 array_multisort($family_name, $family_id);
3898
3899                 if($action == 'save') {
3900                         $new_data = get_var('sched',array('POST'));
3901                         foreach ($new_data as $presidency_array) {
3902                                 foreach ($presidency_array as $entry) {
3903                                         $presidency = $entry['presidency'];
3904                                         $appointment = $entry['appointment'];
3905                                         $location = $entry['location'];
3906                                         $date = $entry['date'];
3907                                         $hour = $entry['hour'];
3908                                         $minute = $entry['minute'];
3909                                         $pm = $entry['pm'];
3910                                         $indiv = $entry['individual'];
3911                                         $family = $entry['family'];
3912                                         $location = $entry['location'];
3913                                         if($pm) { $hour = $hour + 12; }
3914                                         $time = $hour.':'.$minute.':'.'00';
3915                                         $uid = 0;
3916
3917                                         // Zero out family or individual if they are invalid
3918                                         if($indiv == "") { $indiv=0; }
3919                                         if($family == "") { $family=0; }
3920
3921                                         // Update our location
3922                                         if($location == "") {
3923                                                 if($family > 0) {
3924                                                         $family_name_array = explode(",", $familyid2name[$family]);
3925                                                         $family_last_name = $family_name_array[0];
3926                                                         $family_address = $familyid2address[$family];
3927                                                         $location = "$family_last_name"." home ($family_address)";
3928                                                 } else if($indiv > 0) {
3929                                                         $supervisor_name_array = explode(",",$presidency2name[$presidency]);
3930                                                         $supervisor_last_name = $supervisor_name_array[0];
3931                                                         #print "presidency2indiv: $presidency $presidency2indiv[$presidency]<br>";
3932                                                         $sql = "SELECT * FROM tc_individual where individual='$presidency2indiv[$presidency]'";
3933                                                         $this->db2->query($sql,__LINE__,__FILE__);
3934                                                         if($this->db2->next_record()) {
3935                                                                 $supervisor_address = $this->db2->f('address');
3936                                                         }
3937                                                         $location = "$supervisor_last_name"." home ($supervisor_address)";
3938                                                 }
3939                                         }
3940
3941                                         // Zero out the family or individual if date = NULL
3942                                         if($date == "") {
3943                                                 $indiv = 0;
3944                                                 $family = 0;
3945                                                 $location = "";
3946                                         }
3947
3948                                         if(($indiv == 0) && ($family == 0)) { $location = ""; }
3949
3950                                         // Update an existing appointment
3951                                         if($appointment < $this->max_appointments)
3952                                         {
3953                                                 //Only perform a database update if we have made a change to this appointment
3954                                                 $sql = "SELECT * FROM tc_appointment where " .
3955                                                        "appointment='$appointment'" .
3956                                                        " and presidency='$presidency'" .
3957                                                        " and individual='$indiv'" .
3958                                                        " and family='$family'" .
3959                                                        " and date='$date'" .
3960                                                        " and time='$time'" .
3961                                                        " and location='$location'";
3962                                                 $this->db->query($sql,__LINE__,__FILE__);
3963                                                 if(!$this->db->next_record()) {
3964                                                         $old_date = $this->db->f('date');
3965                                                         $old_time = $this->db->f('time');
3966                                                         $this->db2->query("UPDATE tc_appointment set" .
3967                                                                           " family=" . $family . 
3968                                                                           " ,individual=" . $indiv . 
3969                                                                           " ,date='" . $date . "'" .
3970                                                                           " ,time='" . $time . "'" .
3971                                                                           " ,location='" . $location . "'" .
3972                                                                           " ,presidency='" . $presidency . "'" .
3973                                                                           " WHERE appointment=" . $appointment,__LINE__,__FILE__);
3974
3975                                                         // Email the appointment
3976                                                         $this->email_appt($appointment);
3977                                                 }
3978                                         }
3979
3980                                         // Add a new appointment
3981                                         else if(($appointment >= $this->max_appointments) && ($date != "") && ($time != ""))
3982                                         {
3983                                                 //print "adding entry: appt=$appointment date: $date time: $time individual: $indiv family: $family<br>";
3984                                                 $this->db2->query("INSERT INTO tc_appointment (appointment,presidency,family,individual,date,time,location,uid) " .
3985                                                                   "VALUES (NULL,'" . $presidency . "','" . $family . "','" . $indiv . "','" .
3986                                                                   $date . "','" . $time  . "','" . $location . "','" . $uid ."')",__LINE__,__FILE__);
3987
3988                                                 // Now reselect this entry from the database to see if we need
3989                                                 // to send an appointment out for it.
3990                                                 $sql = "SELECT * FROM tc_appointment where " .
3991                                                        "individual='$indiv'" .
3992                                                        " and family='$family'" .
3993                                                        " and presidency='$presidency'" .
3994                                                        " and date='$date'" .
3995                                                        " and time='$time'" .
3996                                                        " and uid='$uid'" .
3997                                                        " and location='$location'";
3998                                                 $this->db3->query($sql,__LINE__,__FILE__);
3999                                                 if($this->db3->next_record()) {
4000                                                         // Email the appointment if warranted
4001                                                         if(($date != "") && ($time != "") && (($indiv > 0) || $family > 0)) { 
4002                                                                 $this->email_appt($this->db3->f('appointment'));
4003                                                         }
4004                                                 }
4005                                         }
4006                                 }
4007                         }
4008
4009                         $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.schedule');
4010                         //Header('Location: ' . $take_me_to_url);
4011                 }
4012
4013                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY individual ASC";
4014                 $this->db->query($sql,__LINE__,__FILE__);
4015                 $i=0;
4016                 while ($this->db->next_record()) {
4017                         $individual[$i] = $this->db->f('individual');
4018                         $indiv_name[$i] = $this->db->f('name');
4019                         $indiv_phone[$individual[$i]] = $this->db->f('phone');
4020                         $i++;
4021                 }
4022                 array_multisort($indiv_name, $individual);
4023
4024                 for ($i=0; $i < count($presidency_data); $i++) {
4025                         $presidency = $presidency_data[$i]['id'];
4026                         $interviewer = $presidency_data[$i]['individual'];
4027                         $name = $presidency_data[$i]['name'];
4028                         $this->t->set_var('presidency_name',$name);
4029                         $table_data="";
4030
4031                         // query the database for all the appointments
4032                         $sql = "SELECT * FROM tc_appointment where presidency=$presidency and date>=CURDATE() ORDER BY date ASC, time ASC";
4033                         $this->db->query($sql,__LINE__,__FILE__);
4034
4035                         // Prefill any existing appointment slots
4036                         while ($this->db->next_record()) {
4037                                 $appointment = $this->db->f('appointment');
4038                                 $indiv = $this->db->f('individual');
4039                                 $family = $this->db->f('family');
4040                                 $location = $this->db->f('location');
4041
4042                                 if($location == "") {
4043                                         if($family > 0) {
4044                                                 $family_name_array = explode(",", $familyid2name[$family]);
4045                                                 $family_last_name = $family_name_array[0];
4046                                                 $family_address = $familyid2address[$family];
4047                                                 $location = "$family_last_name"." home ($family_address)";
4048                                         } else if($indiv > 0) {
4049                                                 $supervisor_name_array = explode(",",$presidency2name[$presidency]);
4050                                                 $supervisor_last_name = $supervisor_name_array[0];
4051                                                 $sql = "SELECT * FROM tc_individual where individual='$presidency2indiv[$presidency]'";
4052                                                 $this->db2->query($sql,__LINE__,__FILE__);
4053                                                 if($this->db2->next_record()) {
4054                                                         $supervisor_address = $this->db2->f('address');
4055                                                 }
4056                                                 $location = "$supervisor_last_name"." home ($supervisor_address)";
4057                                         }
4058                                 }
4059
4060                                 $date = $this->db->f('date');
4061                                 $date_array = explode("-",$date);
4062                                 $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4063                                 $day_string = date("l d-M-Y", mktime(0,0,0,$month,$day,$year));
4064
4065                                 $time = $this->db->f('time');
4066                                 $time_array = explode(":",$time);
4067                                 $hour = $time_array[0];
4068                                 $minute = $time_array[1];
4069                                 $pm = 0;
4070                                 if($hour > 12) { $pm=1; $hour = $hour - 12; }
4071                                 $time_string = date("g:i a", mktime($time_array[0], $time_array[1], $time_array[2]));
4072
4073                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4074
4075                                 // Date selection
4076                                 $table_data.= '<td align=left>';
4077                                 $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]',$date,'','','','','',$this->cal_options);
4078                                 $table_data.= '</td>';
4079
4080                                 // Hour & Minutes selection
4081                                 $table_data.= "<td align=center>";
4082                                 $table_data .= $this->get_time_selection_form($hour, $minute, $pm, $presidency, $appointment);
4083                                 $table_data.= "</td>";
4084
4085                                 // individual drop down list (for PPIs)
4086                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][individual] STYLE="font-size : 8pt">';
4087                                 $table_data.= '<option value=0></option>';  
4088                                 for ($j=0; $j < count($individual); $j++) {
4089                                         $id = $individual[$j];
4090                                         $name = $indiv_name[$j];
4091                                         if($individual[$j] == $indiv) {
4092                                                 $selected[$id] = 'selected="selected"'; 
4093                                         } else {
4094                                                 $selected[$id] = ''; 
4095                                         }
4096                                         $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.'</option>';
4097                                 }
4098                                 $table_data.='</select></td>';
4099
4100                                 // Family drop down list (for Visits)
4101                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family] STYLE="font-size : 8pt">';
4102                                 $table_data.= '<option value=0></option>';          
4103                                 for ($j=0; $j < count($individual); $j++) {
4104                                         $id = $family_id[$j];
4105                                         $name = $family_name[$j];
4106                                         if($family_id[$j] == $family) { 
4107                                                 $selected[$id] = 'selected="selected"'; 
4108                                         } else { 
4109                                                 $selected[$id] = ''; 
4110                                         }
4111                                         $table_data.= '<option value='.$id.' '.$selected[$id].'>'.$name.' Family</option>';
4112                                 }
4113                                 $table_data.='</select></td>';
4114
4115                                 // Location text box
4116                                 $table_data.= '<td align=center><input type=text size="25" maxlength="120" ';
4117                                 $table_data.= 'name="sched['.$presidency.']['.$appointment.'][location]" value="'.$location.'" STYLE="font-size : 8pt">';
4118
4119                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
4120                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
4121
4122                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4123                                 $this->t->set_var('tr_color',$tr_color);
4124                         }
4125
4126                         // Create blank appointment slot
4127                         for ($b=0; $b < 4; $b++) {
4128                                 $appointment = $this->max_appointments + $b;
4129                                 $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4130
4131                                 // Date selection
4132                                 $table_data.= '<td align=left>';
4133                                 $table_data.= $this->jscal->input('sched['.$presidency.']['.$appointment.'][date]','','','','','','',$this->cal_options);
4134                                 $table_data.= '</td>';
4135
4136                                 // Time selection
4137                                 $table_data.= "<td align=center>";
4138                                 $table_data .= $this->get_time_selection_form(0, 0, 0, $presidency, $appointment);
4139                                 $table_data.= "</td>";
4140
4141                                 // individual drop down list
4142                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][individual] STYLE="font-size : 8pt">';
4143                                 $table_data.= '<option value=0></option>';  
4144                                 for ($j=0; $j < count($individual); $j++) {
4145                                         $id = $individual[$j];
4146                                         $name = $indiv_name[$j];
4147                                         $table_data.= '<option value='.$id.'>'.$name.'</option>';
4148                                 }
4149                                 $table_data.='</select></td>';
4150
4151                                 // Family drop down list
4152                                 $table_data.= '<td align=center><select name=sched['.$presidency.']['.$appointment.'][family] STYLE="font-size : 8pt">';
4153                                 $table_data.= '<option value=0></option>';          
4154                                 for ($j=0; $j < count($individual); $j++) {
4155                                         $id = $family_id[$j];
4156                                         $name = $family_name[$j];
4157                                         $table_data.= '<option value='.$id.'>'.$name.' Family</option>';
4158                                 }
4159                                 $table_data.='</select></td>';
4160
4161                                 // Location text box
4162                                 $table_data.= '<td align=center><input type=text size="25" maxlength="120" ';
4163                                 $table_data.= 'name="sched['.$presidency.']['.$appointment.'][location]" value="" STYLE="font-size : 8pt">';
4164
4165                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][appointment]" value="'.$appointment.'">';
4166                                 $table_data.= '<input type=hidden name="sched['.$presidency.']['.$appointment.'][presidency]" value="'.$presidency.'">';
4167
4168                                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4169                                 $this->t->set_var('tr_color',$tr_color);
4170                         }
4171
4172                         $this->t->set_var('table_data',$table_data);
4173                         $this->t->set_var('header_row',$header_row);
4174                         $this->t->set_var('table_width',$table_width);
4175                         $this->t->fp('list','presidency_list',True);
4176                 }
4177
4178                 $this->t->pfp('out','sched_t');
4179                 $this->save_sessiondata();   
4180         }
4181
4182         function email()
4183         {
4184                 $this->t->set_file(array('email_t' => 'email.tpl'));
4185                 $this->t->set_block('email_t','individual_list','list');
4186
4187                 $action = get_var('action',array('GET','POST'));
4188
4189                 $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email'));
4190                 $this->t->set_var('title','Email Tool');
4191
4192                 $this->t->set_var('lang_email','Send Email');
4193                 $this->t->set_var('lang_reset','Cancel');
4194
4195                 $this->t->set_var('email_member_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=member'));
4196                 $this->t->set_var('email_member_link_title','Email Quorum Member');
4197
4198                 $this->t->set_var('email_quorum_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=quorum'));
4199                 $this->t->set_var('email_quorum_link_title','Email Quorum');
4200
4201                 $this->t->set_var('email_reminder_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=reminder'));
4202                 $this->t->set_var('email_reminder_link_title','Email Reminders');
4203
4204                 $this->t->set_var('email_edit_link',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.email&action=edit'));
4205                 $this->t->set_var('email_edit_link_title','Edit Email Addresses');
4206
4207                 $table_width=600;
4208                 $this->t->set_var('table_width',$table_width);
4209
4210                 $this->t->pfp('out','email_t');
4211                 $this->save_sessiondata();   
4212         }
4213
4214         function admin()
4215         {
4216                 $this->t->set_file(array('admin_t' => 'admin.tpl'));
4217                 $this->t->set_block('admin_t','upload','uploadhandle');
4218                 $this->t->set_block('admin_t','admin','adminhandle');
4219                 $this->t->set_block('admin_t','cmd','cmdhandle');
4220                 $this->t->set_block('admin_t','presidency','presidencyhandle');
4221
4222                 $this->t->set_var('upload_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.admin&action=upload'));
4223                 $this->t->set_var('presidency_action',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.admin&action=presidency'));
4224
4225                 $action = get_var('action',array('GET','POST'));
4226
4227                 $this->t->pfp('out','admin_t');
4228
4229                 $sql = "SELECT * FROM tc_individual where steward='$this->default_stewardship' and valid=1 ORDER BY individual ASC";
4230                 $this->db->query($sql,__LINE__,__FILE__);
4231                 $i=0;
4232                 while ($this->db->next_record()) {
4233                         $individual[$i] = $this->db->f('individual');
4234                         $indiv_name[$i] = $this->db->f('name');
4235                         $indiv2name[$individual[$i]] = $indiv_name[$i];
4236                         $i++;
4237                 }
4238                 array_multisort($indiv_name, $individual);
4239
4240                 if($action == 'upload') {
4241                         $target_path = $this->upload_target_path . '/' . basename( $_FILES['uploadedfile']['name']);
4242
4243                         if(($_FILES['uploadedfile']['type'] == "application/zip") ||
4244                            ($_FILES['uploadedfile']['type'] == "application/x-zip-compressed") ||
4245                            ($_FILES['uploadedfile']['type'] == "application/x-zip") ||
4246                            ($_FILES['uploadedfile']['type'] == "application/octet-stream")) {
4247
4248                                 if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
4249                                         $uploadstatus = "<b><font color=red> -E- Unable to move the uploaded file to ";
4250                                         $uploadstatus.= "the target path (check the path and permissions) of: $target_path</font></b>";
4251                                         $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
4252                                         $uploadstatus.= "Tmp Filename : " . $_FILES['uploadedfile']['tmp_name'] . "<br>";
4253                                         $uploadstatus.= "Filename     : " . $_FILES['uploadedfile']['name'] . "<br>";
4254                                         $uploadstatus.= "Type         : " . $_FILES['uploadedfile']['type'] . "<br>";
4255                                         $uploadstatus.= "Size         : " . $_FILES['uploadedfile']['size'] . "<br>";
4256                                         $uploadstatus.= "Error        : " . $_FILES['uploadedfile']['error'] . "<br>";   
4257                                         $this->t->set_var('uploadstatus',$uploadstatus);
4258                                         $this->t->pfp('uploadhandle','upload',True);
4259                                         return 0;
4260                                 }
4261
4262                                 $uploadstatus = "<b>The following file was uploaded successfully: </b><br><br>";
4263                                 $uploadstatus.= "Filename : " . $_FILES['uploadedfile']['name'] . "<br>";
4264                                 $uploadstatus.= "Type     : " . $_FILES['uploadedfile']['type'] . "<br>";
4265                                 $uploadstatus.= "Size     : " . $_FILES['uploadedfile']['size'] . "<br>";        
4266                                 $this->t->set_var('uploadstatus',$uploadstatus);
4267                                 $this->t->pfp('uploadhandle','upload');
4268                                 $this->t->set_var('uploadhandle','');
4269                                 print "<table border=1 width=80%><tr><td>\n<pre>";
4270
4271                                 # make a directory for this data to be stored in
4272                                 $date="data_" . date("Y_m_d");
4273                                 $data_dir = $this->upload_target_path . '/' . $date;
4274                                 print "-> Making the data directory: $date<br>\n";
4275                                 exec('mkdir -p ' . $data_dir . ' 2>&1', $result, $return_code);
4276                                 if($return_code != 0) {
4277                                         print implode('\n',$result) . "<br>";
4278                                         print "<b><font color=red>";
4279                                         print "-E- Unable to create the data directory. Aborting import.";
4280                                         print "</font></b>";
4281                                         return 0;
4282                                 }
4283
4284                                 # move the file uploaded into this directory
4285                                 print "-> Moving the uploaded file into the data dir<br>\n";
4286                                 exec('mv ' . $target_path . ' ' . $data_dir . '/' . ' 2>&1', $result, $return_code);
4287                                 if($return_code != 0) {
4288                                         print implode('\n',$result) . "<br>";
4289                                         print "<b><font color=red>";
4290                                         print "-E- Unable to move the uploaded file into the data dir. Aborting import.";
4291                                         print "</font></b>";
4292                                         return 0;
4293                                 }
4294
4295                                 # unzip the data into this directory
4296                                 print "-> Unzipping the data<br>\n";
4297                                 exec($this->unzip_path .' -u '. $data_dir . '/*.zip -d ' . $data_dir . ' 2>&1', $result, $return_code);
4298                                 if($return_code != 0) {
4299                                         print implode('\n',$result) . "<br>";
4300                                         print "<b><font color=red>";
4301                                         print "-E- Unable to unzip the uploaded file into the data dir: $data_dir. Aborting import.";
4302                                         print "</font></b>";
4303                                         return 0;
4304                                 }
4305                                 exec('mv ' . $data_dir . '/*/* '. $data_dir . ' 2>&1', $result, $return_code);
4306
4307                                 # update the data_latest link to point to this new directory
4308                                 print "-> Updating the latest data dir link<br>\n";
4309                                 $data_latest = $this->upload_target_path . '/data_latest';
4310                                 exec('rm ' . $data_latest. '; ln -s ' . $data_dir .' '. $data_latest .' 2>&1', $result, $return_code);
4311                                 if($return_code != 0) {
4312                                         print implode('\n',$result) . "<br>";
4313                                         print "<b><font color=red>";
4314                                         print "-E- Unable to update the data latest link. Aborting import.";
4315                                         print "</font></b>";
4316                                         return 0;
4317                                 }
4318
4319                                 # run the import perl script to encorporate it into the DB
4320                                 ob_start('ob_logstdout', 2);
4321                                 print "-> Importing the data into the database<br>\n";
4322                                 ob_flush(); flush(); sleep(1);
4323                                 $import_log = $this->upload_target_path . '/import.log';
4324                                 $data_log = $this->upload_target_path . '/data.log';
4325                                 $import_cmd = $this->script_path . '/import_ward_data ' . $data_latest . ' 2>&1 | tee ' . $import_log;
4326                                 $parse_cmd = $this->script_path . '/parse_ward_data -v ' . $data_latest . ' > ' . $data_log . '2>&1';
4327                                 #print "import_cmd: $import_cmd<br>";
4328                                 #print "parse_cmd: $parse_cmd<br>";
4329                                 ob_start('ob_logstdout', 2);
4330                                 passthru($import_cmd);
4331                                 passthru($parse_cmd);
4332                                 ob_flush(); flush(); sleep(1);
4333
4334                                 # fix the permissions of the data dir
4335                                 exec('chmod -R o-rwx ' . $data_dir, $result, $return_code);
4336
4337                                 $this->t->pfp('cmdhandle','cmd');
4338                                 print "</pre></td></tr></table>";
4339                         } else if(($_FILES['uploadedfile']['type'] != "application/zip") &&
4340                                   ($_FILES['uploadedfile']['type'] != "application/x-zip-compressed") &&
4341                                   ($_FILES['uploadedfile']['type'] != "application/x-zip") &&
4342                                   ($_FILES['uploadedfile']['type'] != "application/octet-stream")) {
4343                                 $uploadstatus = "<b><font color=red>The file format must be a .zip file, please try again! </font></b>";
4344                                 $uploadstatus.= "<br><br><b>Detected file format: " . $_FILES['uploadedfile']['type'] . "</b>";
4345                                 $this->t->set_var('uploadstatus',$uploadstatus);
4346                                 $this->t->pfp('uploadhandle','upload',True);
4347                         } else {
4348                                 $uploadstatus = "<b><font color=red> There was an error (" . $_FILES['uploadedfile']['error'];
4349                                 $uploadstatus.= ") uploading the file, please try again! </font></b>";
4350                                 $this->t->set_var('uploadstatus',$uploadstatus);
4351                                 $this->t->pfp('uploadhandle','upload',True);
4352                         }
4353                 } else if($action == "presidency") {
4354                         $new_data = get_var('eqpres',array('POST'));
4355                         foreach ($new_data as $entry) {
4356                                 $id = $entry['id'];
4357                                 $email = $entry['email'];
4358                                 $indiv = $entry['indiv'];
4359                                 $name = $entry['name'];
4360                                 $district = $entry['district'];
4361                                 $president = $entry['president'];
4362                                 $counselor = $entry['counselor'];
4363                                 $secretary = $entry['secretary'];
4364                                 // look up the individual name for the ID
4365                                 $name = $indiv2name[$indiv]; 
4366                                 //print "id=$id indiv=$indiv name=$name email=$email district=$district president=$president ";
4367                                 //print "counselor=$counselor secretary=$secretary<br>";
4368
4369                                 if(($indiv > 0) || ($name != "")) {
4370                                         if($id < $this->max_presidency_members) {
4371                                                 //print "Updating Existing Entry<br>";
4372                                                 $this->db2->query("UPDATE tc_presidency set" .
4373                                                                   " individual=" . $indiv . 
4374                                                                   " ,district=" . $district . 
4375                                                                   " ,email='" . $email . "'" .
4376                                                                   " ,president='" . $president . "'" .
4377                                                                   " ,counselor='" . $counselor . "'" .
4378                                                                   " ,secretary='" . $secretary . "'" .
4379                                                                   " WHERE presidency=" . $id,__LINE__,__FILE__);
4380                                         } else {
4381                                                 //print "Adding New Entry<br>";
4382                                                 $this->db2->query("INSERT INTO tc_presidency (presidency,individual,district," .
4383                                                                   "email,president,counselor,secretary,valid) " .
4384                                                                   "VALUES (NULL,'" . $indiv . "','" . $district . "','" .
4385                                                                   $email . "','" . $president  . "','" .
4386                                                                   $counselor . "','" . $secretary . "','1'" .
4387                                                                   ")",__LINE__,__FILE__);
4388                                         }
4389                                 } else {
4390                                         //print "Ignoring Blank Entry<br>";
4391                                 }
4392                         }
4393
4394                         // Now update the tc_district table appropriately
4395
4396                         // Delete all the previous district entries from the table
4397                         $this->db->query("DELETE from tc_district where valid=1",__LINE__,__FILE__);
4398                         $this->db->query("DELETE from tc_district where valid=0",__LINE__,__FILE__);
4399
4400                         // Always add a "District 0" assigned to the High Priests Group
4401                         $district = 0;
4402                         $name = "High Priests";
4403                         $indiv = 0;
4404                         $valid = 0;
4405                         $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
4406                                           "VALUES ('" . $district . "','" . 
4407                                           $indiv . "','" . $valid . "'" .
4408                                           ")",__LINE__,__FILE__);
4409
4410                         // Requery the tc_presidency table
4411                         $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
4412                         $this->db->query($sql,__LINE__,__FILE__);
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                                 $name = $this->db->f('name');
4418                                 $district = $this->db->f('district');
4419                                 $valid = 1;
4420
4421                                 // If we have a valid district, add it to the district table
4422                                 if($district > 0) {
4423                                         $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
4424                                                           "VALUES ('" . $district . "','" . 
4425                                                           $indiv . "','" . $valid . "'" .
4426                                                           ")",__LINE__,__FILE__);
4427                                 }
4428                         }
4429
4430                         $this->t->set_var('adminhandle','');
4431                         $this->t->pfp('adminhandle','admin'); 
4432                 }
4433                 else
4434                 {
4435                         $this->t->set_var('adminhandle','');
4436                         $this->t->pfp('adminhandle','admin'); 
4437                 }
4438
4439                 // Now save off the data needed for a Presidency Table Update
4440
4441                 $sql = "SELECT tp.*, ti.name FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
4442                 $this->db->query($sql,__LINE__,__FILE__);
4443                 $table_data = "";
4444                 $header_row = "<th>Individual</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th>";
4445                 while ($this->db->next_record()) {
4446                         // Extract the data for each presidency record
4447                         $id = $this->db->f('presidency');
4448                         $indiv = $this->db->f('individual');
4449                         $district = $this->db->f('district');
4450                         $name = $this->db->f('name');
4451                         $email = $this->db->f('email');
4452                         $president = $this->db->f('president');
4453                         $counselor = $this->db->f('counselor');
4454                         $secretary = $this->db->f('secretary');
4455
4456                         // Create the forms needed in the table
4457                         $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4458
4459                         // Presidency ID
4460                         $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4461
4462                         // individual
4463                         if($eqpresidency == 0) {
4464                                 $table_data.= '<td align=center><select name="eqpres['.$id.'][indiv]">';
4465                                 $table_data.= '<option value=0></option>';  
4466                                 for ($j=0; $j < count($individual); $j++) {
4467                                         $tmp_id = $individual[$j];
4468                                         $name = $indiv_name[$j];
4469                                         if($individual[$j] == $indiv) { 
4470                                                 $indivname = $name; 
4471                                                 $selected = 'selected="selected"'; 
4472                                         } else { 
4473                                                 $selected = ''; 
4474                                         }
4475                                         $table_data.= '<option value='.$tmp_id.' '.$selected.'>'.$name.'</option>';
4476                                 }
4477                                 $table_data.='</select></td>';
4478                                 $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="'.$indivname.'">';
4479                         } else {
4480                                 $table_data.= '<td align=left><input type=text size="20" name="eqpresname" value="Presidency"></td>';
4481                                 $table_data.= '<input type=hidden name="eqpres['.$id.'][name]" value="Presidency">';
4482                         }
4483
4484                         // Email Address
4485                         $table_data .= '<td><input type="text" size="50" name="eqpres['.$id.'][email]" value="'.$email.'"></td>';
4486
4487                         // District
4488                         $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4489                         $table_data.= '<option value=0></option>';
4490                         for ($j=0; $j <= $this->max_num_districts; $j++) {
4491                                 if($district == $j) { 
4492                                         $selected = 'selected="selected"'; 
4493                                 } else { 
4494                                         $selected = ''; 
4495                                 }
4496                                 $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4497                         }
4498                         $table_data.='</select></td>';
4499
4500                         // President
4501                         $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4502                         if($president == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4503                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4504                         $table_data.='</select></td>';
4505
4506                         // Counselor
4507                         $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4508                         if($counselor == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4509                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4510                         $table_data.='</select></td>';
4511
4512                         // Secretary
4513                         $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4514                         if($secretary == 1) { $table_data .= '<option value=0>0</option><option value=1 selected="selected">1</option>'; }
4515                         else { $table_data .= '<option value=0 selected="selected">0</option><option value=1>1</option>'; }
4516                         $table_data.='</select></td>';
4517
4518                         // End of ROW
4519                         $table_data .= "</tr>\n";
4520                         $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4521                         $this->t->set_var('tr_color',$tr_color);
4522                 }
4523
4524                 // Now create 1 blank row to always have a line available to add a new individual with
4525                 $id = $this->max_presidency_members;
4526                 $table_data .= "<tr bgcolor=". $this->t->get_var('tr_color') .">";
4527                 // Presidency ID
4528                 $table_data .= '<input type=hidden name="eqpres['.$id.'][id]" value="'.$id.'">';
4529                 // individual
4530                 $table_data.= '<td align=center><select name="eqpres['.$id.'][indiv]">';
4531                 $table_data.= '<option value=0></option>';  
4532                 for ($j=0; $j < count($individual); $j++) {
4533                         $tmp_id = $individual[$j];
4534                         $name = $indiv_name[$j];
4535                         $table_data.= '<option value='.$tmp_id.'>'.$name.'</option>';
4536                 }
4537                 $table_data.='</select></td>';
4538                 $table_data.='<input type=hidden name="eqpres['.$id.'][name]" value="">';
4539                 // Email Address
4540                 $table_data.='<td><input type="text" size="50" name="eqpres['.$id.'][email]" value=""></td>';
4541                 // District
4542                 $table_data.= '<td align=center><select name="eqpres['.$id.'][district]">';
4543                 $table_data.= '<option value=0></option>';
4544                 for ($j=0; $j <= $this->max_num_districts; $j++) {
4545                         if($j == 0) { 
4546                                 $selected = 'selected="selected"'; 
4547                         } else { 
4548                                 $selected = ''; 
4549                         }
4550                         $table_data.= '<option value='.$j.' '.$selected.'>'.$j.'</option>';
4551                 }
4552                 $table_data.='</select></td>';
4553                 // President
4554                 $table_data.= '<td align=center><select name="eqpres['.$id.'][president]">';
4555                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4556                 $table_data.='</select></td>';
4557                 // Counselor
4558                 $table_data.= '<td align=center><select name="eqpres['.$id.'][counselor]">';
4559                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4560                 $table_data.='</select></td>';
4561                 // Secretary
4562                 $table_data.= '<td align=center><select name="eqpres['.$id.'][secretary]">';
4563                 $table_data.= '<option value=0>0</option><option value=1>1</option>';
4564                 $table_data.='</select></td>';
4565                 // End of ROW
4566                 $table_data .= "</tr>\n";
4567                 $tr_color = $this->nextmatchs->alternate_row_color($tr_color);
4568                 $this->t->set_var('tr_color',$tr_color);
4569
4570                 $this->t->set_var('header_row',$header_row);
4571                 $this->t->set_var('table_data',$table_data);
4572                 $this->t->pfp('presidencyhandle','presidency',True);
4573
4574                 $this->save_sessiondata();   
4575         }
4576
4577         function email_appt($appointment)
4578         {
4579                 //print "Emailing notification of appointment: $appointment <br>";
4580
4581                 $sql = "SELECT * FROM tc_appointment where appointment='$appointment'";
4582                 $this->db->query($sql,__LINE__,__FILE__);
4583
4584                 while ($this->db->next_record()) {
4585                         $appointment = $this->db->f('appointment');
4586                         $presidency = $this->db->f('presidency');
4587                         $location = $this->db->f('location');
4588                         $interviewer = "";
4589                         $email = "";
4590                         $indiv = $this->db->f('individual');
4591                         $indiv_name = "";
4592                         $family = $this->db->f('family');
4593                         $family_name = "";
4594                         $appt_name = "";
4595                         $phone = "";
4596                         $uid = $this->db->f('uid');
4597
4598                         // Extract the year, month, day, hours, minutes, seconds from the appointment time
4599                         $appt_date = $this->db->f('date');
4600                         $date_array = explode("-",$appt_date);
4601                         $year = $date_array[0]; $month = $date_array[1]; $day = $date_array[2];
4602                         $appt_time = $this->db->f('time');
4603                         $time_array = explode(":",$appt_time);
4604                         $hour = $time_array[0]; $minute = $time_array[1]; $seconds = $time_array[2];
4605
4606                         // Format the appointment time into an iCal UTC equivalent
4607                         $dtstamp = gmdate("Ymd"."\T"."His"."\Z");
4608                         $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
4609                         $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
4610
4611                         $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'";
4612                         $this->db2->query($sql,__LINE__,__FILE__);
4613                         if($this->db2->next_record()) {
4614                                 if ($this->db2->f('email1') != "") {
4615                                         $email = $this->db2->f('email1');
4616                                 } else { 
4617                                         $email = $this->db2->f('email2');
4618                                 }
4619                                 $interviewer = $this->db2->f('name');
4620                         }
4621
4622                         // Set the email address of the interviewer
4623                         $from = $email;
4624
4625                         if($indiv > 0) {
4626                                 $sql = "SELECT * FROM tc_individual where individual='$indiv'";
4627                                 $this->db2->query($sql,__LINE__,__FILE__);
4628                                 if($this->db2->next_record()) {
4629                                         $indiv_name = $this->db2->f('name');
4630                                         $phone = $this->db2->f('phone');
4631                                         $appt_name = $indiv_name . " Interview";
4632                                         $duration = $this->default_ppi_appt_duration * 60;
4633                                 }
4634                         }
4635
4636                         if($family > 0) {
4637                                 $sql = "SELECT * FROM tc_family WHERE family='$family'";
4638                                 $this->db2->query($sql,__LINE__,__FILE__);
4639                                 if($this->db2->next_record()) {
4640                                         $individual = $this->db2->f('individual');
4641                                         $sql = "SELECT * FROM tc_individual where individual='$individual'";
4642                                         $this->db3->query($sql,__LINE__,__FILE__);
4643                                         if($this->db3->next_record()) {
4644                                                 $phone = $this->db3->f('phone');
4645                                                 $family_name = $this->db3->f('name');
4646                                                 $phone = $this->db3->f('phone');
4647                                         }
4648                                         $appt_name = $family_name . " Family Visit";
4649                                         $duration = $this->default_visit_appt_duration * 60;
4650                                 }
4651                         }
4652
4653                         $dtend = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4654                         $dtendstr = date("g:i A", mktime($hour,$minute,$seconds+$duration,$month,$day,$year));
4655                         $date = $dtstartstr . "-" . $dtendstr;
4656                         $description = "$appt_name : $phone";
4657
4658                         if(($uid == 0) && ($appt_name != "")) {
4659                                 // Create a new calendar item for this appointment, since this must be the first time we
4660                                 // are sending it out.
4661                                 print "Sent new appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4662                                 $uid = rand() . rand(); // Generate a random identifier for this appointment
4663                                 $subject = "Created: $appt_name";
4664
4665                                 $this->db->query("UPDATE tc_appointment set" .
4666                                                  " uid=" . $uid . 
4667                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4668
4669                                 $action = "PUBLISH";
4670                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4671                                                       $dtend, $date, $location, $appt_name, $description, $uid);
4672                         } else if(($uid != 0) && ($appt_name == "")) {
4673                                 // Remove the calendar item for this appointment since it has already been sent
4674                                 // and there is no name we have changed it to.
4675                                 print "Sent deleted appointment to " . $interviewer . " at " . $email . " for " . $appt_date . " " . $appt_time . "<br>";
4676                                 $subject = "Canceled: $appt_date $appt_time";
4677
4678                                 $this->db->query("UPDATE tc_appointment set" .
4679                                                  " uid=0" . 
4680                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4681
4682                                 $action = "CANCEL";
4683                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4684                                                       $dtend, $date, $location, $subject, $subject, $uid);
4685                         } else if($uid != 0) {
4686                                 // Update the existing appointment since we have changed it
4687                                 print "Sent updated appointment to " . $interviewer . " at " . $email . " for " . $appt_name . "<br>";
4688
4689                                 $subject = "Canceled: $appt_date $appt_time";
4690                                 $action = "CANCEL";
4691                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4692                                 $dtend, $date, $location, $subject, $subject, $uid);
4693
4694                                 $uid = rand() . rand(); // Generate a random identifier for this appointment
4695                                 $this->db->query("UPDATE tc_appointment set" .
4696                                                  " uid=" . $uid .
4697                                                  " WHERE appointment=" . $appointment,__LINE__,__FILE__);
4698
4699                                 $subject = "Updated: $appt_name";
4700                                 $action = "PUBLISH";
4701                                 $this->send_ical_appt($action, $email, $from, $subject, $dtstamp, $dtstart,
4702                                                       $dtend, $date, $location, $appt_name, $description, $uid);
4703                         }
4704                 }
4705                 return true;
4706         }
4707
4708         function send_ical_appt($action, $to, $from, $subject, $dtstamp, $dtstart, $dtend, $date, $location, $summary, $description, $uid)
4709         {
4710                 // Initialize our local variables
4711                 $boundary = "=MIME_APPOINTMENT_BOUNDARY";
4712                 $message = "";
4713                 $headers = "";
4714
4715                 // Form the headers for the email message
4716                 $headers.="X-Mailer: PHP/" . phpversion() . "\n";
4717                 $headers.="Mime-Version: 1.0\n";
4718                 $headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
4719                 $headers.="Content-Disposition: inline\n";
4720                 $headers.="Reply-To: $from\n";
4721                 $headers.="From: $from\n";
4722
4723                 // Print the plaintext version of the appointment
4724                 $message.="--$boundary\n";
4725                 $message.="Content-Type: text/plain; charset=us-ascii\n";
4726                 $message.="Content-Disposition: inline\n";
4727                 $message.="\n";
4728                 $message.="What: $description\n";
4729                 $message.="When: $date\n";
4730                 $message.="Where: $location\n";
4731                 $message.="\n";
4732
4733                 // Print the .ics attachment version of the appointment
4734                 $message.="--$boundary\n";
4735                 $message.="Content-Type: text/calendar; charset=us-ascii\n";
4736                 $message.="Content-Disposition: attachment; filename=\"appointment.ics\"\n";
4737                 $message.="\n";
4738                 $message.="BEGIN:VCALENDAR" . "\n";
4739                 $message.="VERSION:2.0" . "\n";
4740                 $message.="PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" . "\n";
4741                 $message.="METHOD:$action" . "\n";
4742                 $message.="BEGIN:VEVENT" . "\n";
4743                 $message.="ORGANIZER:MAILTO:$from". "\n";
4744                 $message.="DTSTAMP:$dtstamp" . "\n";
4745                 $message.="DTSTART:$dtstart" . "\n";
4746                 $message.="DTEND:$dtend" . "\n";
4747                 $message.="SUMMARY:$summary" . "\n";
4748                 $message.="DESCRIPTION:$description" . "\n";
4749                 $message.="LOCATION:$location" . "\n";
4750                 $message.="UID:$uid" ."\n";
4751                 $message.="TRANSP:OPAQUE" . "\n";
4752                 $message.="SEQUENCE:0" . "\n";
4753                 $message.="CLASS:PUBLIC" . "\n";
4754                 $message.="END:VEVENT" . "\n";
4755                 $message.="END:VCALENDAR" . "\n";
4756
4757                 // Complete the message
4758                 $message.="--$boundary\n";
4759
4760                 // Send the message
4761                 mail($to, $subject, $message, $headers);
4762         }
4763
4764         function get_time_selection_form($hour, $minute, $pm, $presidency, $appointment)
4765         {
4766                 $form_data = "";
4767                 $blank = 0;
4768
4769                 if($hour == 0) { $blank = 1; }
4770
4771                 if($this->time_drop_down_lists == 1) {
4772                         // Create drop down lists to get the time
4773                         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][hour]>';
4774                         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4775                         foreach(range(1,12) as $num) {
4776                                 if($hour == $num) { 
4777                                         $selected = 'selected="selected"'; 
4778                                 } else { 
4779                                         $selected = ''; 
4780                                 }
4781                                 $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4782                         }
4783                         $form_data.= '</select>';
4784                         $form_data.= '&nbsp;:&nbsp;';
4785                         $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][minute]>';
4786                         if($blank == 1) { $form_data.= '<option value=""></option>'; }
4787                         $num = 0;
4788                         while($num < 60) {
4789                                 if($num < 10) { $num = "0" . "$num"; }
4790                                 if($minute == $num) { 
4791                                         $selected = 'selected="selected"'; 
4792                                 } else { 
4793                                         $selected = ''; 
4794                                 }
4795                                 if($blank == 1) { $selected = ""; }
4796                                 $form_data.= '<option value='.$num.' '.$selected.'>'.$num.'</option>';
4797                                 $num = $num + $this->time_drop_down_list_inc;
4798                         }
4799                         $form_data.= '</select>';
4800                 } else {
4801                         // Use free form text fields to get the time
4802                         if($blank == 1) { 
4803                                 $hour = ""; 
4804                                 $minute = ""; 
4805                                 $ampm = ""; 
4806                         }
4807                         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][hour] value='.$hour.'>';
4808                         $form_data.= ':';
4809                         $form_data.= '<input type=text size=2 name=sched['.$presidency.']['.$appointment.'][minute] value='.$minute.'>';
4810                         $form_data.= '&nbsp;';
4811                 }
4812                 // Always use a drop-down select form for am/pm
4813                 $form_data.= '<select name=sched['.$presidency.']['.$appointment.'][pm]>';
4814                 if($blank == 0) {
4815                         if($pm == 0) { 
4816                                 $form_data.= '<option value=0 selected>am</option>'; 
4817                                 $form_data.= '<option value=1>pm</option>'; 
4818                         }
4819                         if($pm == 1) { 
4820                                 $form_data.= '<option value=0>am</option>'; 
4821                                 $form_data.= '<option value=1 selected>pm</option>'; 
4822                         }
4823                 } else {
4824                         $form_data.= '<option value=""></option>';
4825                         $form_data.= '<option value=0>am</option>';
4826                         $form_data.= '<option value=1>pm</option>';
4827                 }
4828                 $form_data.= '</select>';
4829
4830                 return $form_data;
4831         }
4832 }
4833
4834 ?>