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