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