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