fixed a few more variable rename issues that were breaking some sql queries
[eq/.git] / inc / class.tc.inc.php
index 976709f698adde9b94b3a6a7bb1e7e9fb3523abf..409b87e6bf0cd05a0709f1f70a132d26aa42b206 100644 (file)
@@ -15,6 +15,7 @@ class tc
 {
        var $db;
        var $db2;
+       var $db3;
        var $t;
        var $nextmatchs;
        var $grants;
@@ -108,6 +109,18 @@ class tc
                echo parse_navbar();
                $this->display_app_header();    
        }
+       
+       function logToFile($func, $msg)
+       {
+               // open file
+               $fd = fopen($this->upload_target_path . "/tc_trace.log", "a");
+               // append date/time to message
+               $str = "[" . date("Y/m/d h:i:s", mktime()) . "] [" . $func . "] " . $msg;
+               // write string
+               fwrite($fd, $str . "\n");
+               // close file
+               fclose($fd);
+       }
   
        function save_sessiondata()
        {
@@ -190,7 +203,7 @@ class tc
                $this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ht_view'));
                $this->t->set_var('title','Hometeaching'); 
 
-               $sql = "SELECT * FROM tc_district where valid=1 ORDER BY district ASC";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
@@ -258,7 +271,7 @@ class tc
                                $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
 
                                // Get the names of the families assigned this home teaching companionship
-                               $sql = "SELECT * from tc_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
+                               $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'];
                                $sql = $sql . " ORDER BY name ASC";
                                $this->db->query($sql,__LINE__,__FILE__);
                                $k=0;
@@ -474,8 +487,8 @@ class tc
                        $table_data.= "<tr bgcolor=#d3dce3><td colspan=20><table><tr>$companion_table_entry</tr></table><hr></td></tr>";
 
                        // Get the names of the families assigned this home teaching companionship
-                       $sql = "SELECT * from tc_family where valid=1 AND companionship=".$unique_companionships[$j]['companionship'];
-                       $sql = $sql . " ORDER BY name ASC";
+                       $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'];
+                       $sql = $sql . " ORDER BY ti.name ASC";
                        $this->db->query($sql,__LINE__,__FILE__);
                        while ($this->db->next_record()) {
                                $family_name = $this->db->f('name');
@@ -679,7 +692,7 @@ class tc
 
                for ($i=0; $i < count($names); $i++) {
                        //$this->nextmatchs->template_alternate_row_color(&$this->t);
-                       $this->t->set_var('indiv_name',$names[$i]);
+                       $this->t->set_var('individual_name',$names[$i]);
                        if(($i+1) % 3 == 0) {
                                $this->t->set_var('table_sep',"</td></tr><tr>"); 
                        } else { 
@@ -721,7 +734,7 @@ class tc
                        // Re-add the individuals who are checked as having participated in this activity
                        $indivs = get_var('individual_name',array('POST'));
                        if(is_array($indivs)) { // Only do the foreach loop if we have a valid array of indivs to work with
-                               foreach ($indivs as $indiv) {
+                               foreach ($indivs as $individual) {
                                        $this->db->query("INSERT INTO tc_participation (individual,activity) " .
                                                         "VALUES (" . $individual . ",". $activity['activity'] . ")",__LINE__,__FILE__);
                                }
@@ -748,7 +761,7 @@ class tc
                        }
 
                        $indivs = get_var('individual_name',array('POST'));
-                       foreach ($indivs as $indiv)
+                       foreach ($indivs as $individual)
                        {
                                $this->db->query("INSERT INTO tc_participation (individual,activity) " .
                                                 "VALUES (" . $individual . ",". $activity['activity'] . ")",__LINE__,__FILE__);
@@ -1353,7 +1366,7 @@ class tc
                $year = date('Y');
 
                // Get the President
-               $sql = "SELECT * FROM tc_presidency where president=1 and valid=1";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                if($this->db->next_record()) {
                        $president_name = $this->db->f('name');
@@ -1364,16 +1377,7 @@ class tc
                        $interviewer = $this->db->f('individual');
                        $district_number = '*';
                        $district_name = $president_name;
-                       $sql = "SELECT * FROM tc_individual where individual='$president_id'";
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       if($this->db2->next_record()) {
-                               $mls_id = $this->db2->f('mls_id');
-                       }
-                       $sql = "SELECT * FROM tc_individual where mls_id='$mls_id'";
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       if($this->db2->next_record()) {
-                               $president_address = $this->db2->f('address');
-                       }
+                       $president_address = $this->db->f('address');
                } else {
                        print "<hr><font color=red><h3>-E- Unable to locate President in tc_presidency table</h3></font></hr>";
                        return;
@@ -1406,18 +1410,20 @@ class tc
                        }
 
                        // Save any changes made to the ppi notes table
-                       $new_data = get_var('ppi_notes',array('POST'));
+                       $new_data = get_var('notes',array('POST'));
                        foreach ($new_data as $entry) {
-                               $ppi_notes = $entry['notes'];
+                               $notes = $entry['notes'];
                                $individual = $entry['individual'];
-                               $ppi_pri = $entry['pri'];
+                               $priority = $entry['pri'];
 
                                // Perform database save actions here
-                               $this->db->query("UPDATE tc_individual set " .
-                                                " ppi_notes='" . $ppi_notes . "'" .
-                                                ",ppi_pri='" . $ppi_pri . "'" .
-                                                " WHERE individual=" . $individual,__LINE__,__FILE__);
-
+                               $sql = "SELECT * FROM tc_individual WHERE individual='$individual'";
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               if ($this->db->next_record()) {
+                                       $scheduling_priority = $this->db->f('scheduling_priority');
+                                       //$this->logToFile("ppi_sched", "UPDATE tc_scheduling_priority SET priority='$priority', notes=\"$notes\" WHERE scheduling_priority='$scheduling_priority'");
+                                       $this->db2->query("UPDATE tc_scheduling_priority SET priority='$priority', notes=\"$notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
+                               }
                        }
 
                        $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.ppi_sched');
@@ -1508,7 +1514,7 @@ class tc
 
                // PPI SCHEDULING TABLE
                // TODO:  changed this so it picks the quorum dynamically
-               $sql = "SELECT * FROM tc_individual where valid=1 and steward='Elder' ORDER BY ppi_pri ASC, name ASC";
+               $sql = "SELECT * FROM tc_individual AS ti JOIN tc_scheduling_priority AS tsp WHERE ti.scheduling_priority=tsp.scheduling_priority AND steward='Elder' AND valid=1 ORDER BY tsp.priority ASC, ti.name ASC";
                $this->db->query($sql,__LINE__,__FILE__);
 
                $i=0; 
@@ -1517,8 +1523,8 @@ class tc
                        $individual[$i] = $this->db->f('individual');
                        $indiv_name[$i] = $this->db->f('name');
                        $indiv_phone[$individual[$i]] = $this->db->f('phone');
-                       $indiv_ppi_pri[$individual[$i]] = $this->db->f('ppi_pri');
-                       $indiv_ppi_notes[$individual[$i]] = $this->db->f('ppi_notes');
+                       $indiv_priority[$individual[$i]] = $this->db->f('priority');
+                       $indiv_notes[$individual[$i]] = $this->db->f('notes');
                        $i++;
                        $total_indivs++;
                }
@@ -1529,8 +1535,8 @@ class tc
                        $id = $individual[$i];
                        $name = $indiv_name[$i];
                        $phone = $indiv_phone[$id];
-                       $ppi_pri = $indiv_ppi_pri[$id];
-                       $ppi_notes = $indiv_ppi_notes[$id];
+                       $priority = $indiv_priority[$id];
+                       $notes = $indiv_notes[$id];
 
                        // If this individual has had a yearly PPI this year, don't show him on the schedule list
                        $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
@@ -1558,12 +1564,12 @@ class tc
                                $this->t->set_var('tr_color',$tr_color);
                                $table_data.= "<tr bgcolor=". $this->t->get_var('tr_color') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
                                $table_data.= "<td align=center>$phone</td>";
-                               //$table_data.= "<td align=center>$ppi_pri</td>";
+                               //$table_data.= "<td align=center>$priority</td>";
                                $table_data.= "<td align=center>";
-                               $table_data.= '<select name=ppi_notes['.$i.'][pri]>';
+                               $table_data.= '<select name=notes['.$i.'][pri]>';
                                foreach(range(0,6) as $num) {
                                        if($num == 0) { $num = 1; } else {$num = $num*5; }
-                                       if($ppi_pri == $num) { 
+                                       if($priority == $num) { 
                                                $selected[$num] = 'selected="selected"'; 
                                        } else { 
                                                $selected[$num] = ''; 
@@ -1572,9 +1578,9 @@ class tc
                                }
                                $table_data.= '</select></td>';
                                $table_data.= "<td align=center>$date</td>";
-                               $table_data.= '<td><input type=text size="50" maxlength="128" name="ppi_notes['.$i.'][notes]" value="'.$ppi_notes.'">';
-                               $table_data.= '<input type=hidden name="ppi_notes['.$i.'][individual]" value="'.$id.'">';
-                               $table_data.= '<input type=hidden name="ppi_notes['.$i.'][indiv_name]" value="'.$name.'">';
+                               $table_data.= '<td><input type=text size="50" maxlength="128" name="notes['.$i.'][notes]" value="'.$notes.'">';
+                               $table_data.= '<input type=hidden name="notes['.$i.'][individual]" value="'.$id.'">';
+                               $table_data.= '<input type=hidden name="notes['.$i.'][indiv_name]" value="'.$name.'">';
                                $table_data.= '</td>';
                                $table_data.= '</tr>';
                        } else {
@@ -1588,14 +1594,14 @@ class tc
                                $link = $GLOBALS['phpgw']->link('/tc/index.php',$link_data);    
                                $indivs_with_yearly_ppi++;
                                $date = $this->db2->f('date');
-                               $ppi_notes = $this->db2->f('notes');
-                               if(strlen($ppi_notes) > 40) { $ppi_notes = substr($ppi_notes,0,40) . "..."; }
+                               $notes = $this->db2->f('notes');
+                               if(strlen($notes) > 40) { $notes = substr($notes,0,40) . "..."; }
                                $tr_color2 = $this->nextmatchs->alternate_row_color($tr_color2);
                                $this->t->set_var('tr_color2',$tr_color2);
                                $completed_data.= "<tr bgcolor=". $this->t->get_var('tr_color2') ."><td title=\"$phone\"><a href=$link>$name</a></td>";
                                $completed_data.= "<td align=center>$phone</td>";
                                $completed_data.= "<td align=center><a href=".$link.">$date</a></td>";
-                               $completed_data.= "<td align=left>$ppi_notes</td>";
+                               $completed_data.= "<td align=left>$notes</td>";
                                $completed_data.= '</tr>';
                        }
                } // End for individuals Loop
@@ -1756,10 +1762,12 @@ class tc
                                $hti_pri = $entry['pri'];
                                //print "hti_notes: $hti_notes indiv_name: $indiv_name <Br>";
                                // Perform database save actions here
-                               $this->db->query("UPDATE tc_individual set " .
-                                                " hti_notes='" . $hti_notes . "'" .
-                                                ",hti_pri='" . $hti_pri . "'" .
-                                                " WHERE individual=" . $individual,__LINE__,__FILE__);
+                               $this->db->query("SELECT * FROM tc_companionship WHERE individual=$individual and valid=1",__LINE__,__FILE__);
+                               if ($this->db->next_record()) {
+                                       $scheduling_priority = $this->db->f('scheduling_priority');
+                                       //$this->logToFile("int_sched", "UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'");
+                                       $this->db2->query("UPDATE tc_scheduling_priority SET priority='$hti_pri', notes=\"$hti_notes\" WHERE scheduling_priority='$scheduling_priority'",__LINE__,__FILE__);
+                               }
                        }
 
                        $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.int_sched');
@@ -1767,7 +1775,7 @@ class tc
                }
 
                // Get the Districts
-               $sql = "SELECT * FROM tc_district where valid=1 ORDER BY district ASC";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
@@ -1775,11 +1783,7 @@ class tc
                        $districts[$i]['district'] = $this->db->f('district');
                        $districts[$i]['name'] = $this->db->f('name');
                        $districts[$i]['supervisor'] = $this->db->f('supervisor');
-                       $sql = "SELECT * FROM tc_presidency where district=$district and valid=1";
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       if($this->db2->next_record()) {
-                               $districts[$i]['presidency'] = $this->db2->f('presidency');
-                       }
+                       $districts[$i]['presidency'] = $this->db->f('presidency');
                        $i++;
                }
 
@@ -1885,7 +1889,7 @@ class tc
                        $i=0;
                        for ($j=0; $j < count($unique_companionships); $j++) {
                                // Select all the companions from each companionship
-                               $sql = "SELECT * FROM tc_companionship where valid=1 and companionship=". $unique_companionships[$j]['companionship'];
+                               $sql = "SELECT * FROM tc_companionship 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'];
                                $this->db->query($sql,__LINE__,__FILE__);
                                $k=0; $int_completed=0;
                                $comp = $unique_companionships[$j]['companionship'];
@@ -1896,20 +1900,11 @@ class tc
                                        // Get this companions information
                                        $individual = $this->db->f('individual');
 
-                                       $sql = "SELECT * FROM tc_individual where individual='$individual'";
-                                       $this->db2->query($sql,__LINE__,__FILE__);      
-                                       if($this->db2->next_record()) {
-                                               $individual = $this->db2->f('individual');
-                                               $indiv_name = $this->db2->f('name');
-                                               $indiv_phone[$individual] = $this->db2->f('phone');
-                                               $indiv_hti_pri[$individual] = $this->db2->f('hti_pri');
-                                               $indiv_hti_notes[$individual] = $this->db2->f('hti_notes');
-                                       }
-                                       $id = $individual;
-                                       $name = $indiv_name;
-                                       $phone = $indiv_phone[$id];
-                                       $hti_pri = $indiv_hti_pri[$id];
-                                       $hti_notes = $indiv_hti_notes[$id];
+                                       $id = $this->db->f('individual');
+                                       $name = $this->db->f('name');
+                                       $phone = $this->db->f('phone');
+                                       $hti_pri = $this->db->f('priority');
+                                       $hti_notes = $this->db->f('notes');
 
                                        // If the companionship has already had its quarterly interview,
                                        // Skip the other companion in the companionship.
@@ -2061,7 +2056,7 @@ class tc
                $year = date('Y');
 
                // create the family id -> family name mapping
-               $sql = "SELECT * FROM tc_family where valid=1 and individual != 0 and companionship != 0 ORDER BY name ASC";
+               $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 ORDER BY ti.name ASC";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                $family_id = NULL;
@@ -2069,11 +2064,7 @@ class tc
                        $family_id[$i] = $this->db->f('family');
                        $family_name[$i] = $this->db->f('name');
                        $familyid2name[$family_id[$i]] = $family_name[$i];
-                       $sql = "SELECT * FROM tc_individual where family='$family_id[$i]'";
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       if($this->db2->next_record()) {
-                               $familyid2address[$family_id[$i]] = $this->db2->f('address');
-                       }
+                       $familyid2address[$family_id[$i]] = $this->db->f('address');
                        $i++;
                }
                array_multisort($family_name, $family_id);
@@ -2118,10 +2109,12 @@ class tc
                                $visit_pri = $entry['pri'];
 
                                // Perform database save actions here
-                               $this->db->query("UPDATE tc_family set " .
-                                                " visit_notes='" . $visit_notes . "'" .
-                                                ",visit_pri='" . $visit_pri . "'" .
-                                                " WHERE family=" . $family,__LINE__,__FILE__);
+                               $this->db->query("SELECT * FROM tc_family WHERE family='$family'",__LINE__,__FILE__);
+                               if ($this->db->next_record()) {
+                                       $scheduling_priority = $this->db->f('scheduling_priority');
+                                       //$this->logToFile("vis_sched", "UPDATE tc_scheduling_priority SET priority='$visit_pri', notes=\"$visit_notes\" WHERE scheduling_priority='$scheduling_priority'");
+                                       $this->db2->query("UPDATE tc_scheduling_priority SET priority='$visit_pri', notes=\"$visit_notes\" WHERE scheduling_priority='$scheduling_priority'", __LINE__, __FILE__);
+                               }
                        }
 
                        $take_me_to_url = $GLOBALS['phpgw']->link('/tc/index.php','menuaction=tc.tc.vis_sched');
@@ -2138,7 +2131,7 @@ class tc
                $appt_table_data = ""; 
 
                // Find out what the President ID is
-               $sql = "SELECT * FROM tc_presidency where president=1 and valid=1";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                if($this->db->next_record()) {
                        $presidency_name = $this->db->f('name');
@@ -2203,43 +2196,18 @@ class tc
 
 
                // VISIT SCHEDULING TABLE
-               $sql = "SELECT * FROM tc_family where valid=1 and individual != 0  and companionship != 0 ORDER BY visit_pri ASC, name ASC";
+               $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 ORDER BY tsp.priority ASC, ti.name ASC";
                $this->db->query($sql,__LINE__,__FILE__);
 
                $total_families=0; $families_with_yearly_visit=0;
 
-               $i=0; 
-               $family_id = NULL;
-               $family_name = NULL;
-               $family_phone = NULL;
-               $family_visit_pri = NULL;
-               $family_visit_notes = NULL;
-               while ($this->db->next_record()) {
-                       $family_id[$i] = $this->db->f('family');
-                       $family_name[$i] = $this->db->f('name');
-                       $family_phone[$family_id[$i]] = $family_id[$i] . " ERROR";
-                       $family_visit_pri[$family_id[$i]] = $this->db->f('visit_pri');
-                       $family_visit_notes[$family_id[$i]] = $this->db->f('visit_notes');
-                       $i++;
+               while ( $this->db->next_record()) {
                        $total_families++;
-               }
-
-               $sql = "SELECT * FROM tc_individual where valid=1";
-               $this->db->query($sql,__LINE__,__FILE__);
-               while ($this->db->next_record()) {
-                       $family = $this->db->f('family');
+                       $id = $this->db->f('family');
+                       $name = $this->db->f('name');
                        $phone = $this->db->f('phone');
-                       $family_phone[$family] = $phone;
-               }
-
-               $max = count($family_id);
-
-               for($i=0; $i < $max; $i++) {
-                       $id = $family_id[$i];
-                       $name = $family_name[$i];
-                       $phone = $family_phone[$id];
-                       $vis_pri = $family_visit_pri[$id];
-                       $vis_notes = $family_visit_notes[$id];
+                       $vis_pri = $this->db->f('priority');
+                       $vis_notes = $this->db->f('notes');
 
                        // If this family has had a yearly visit this year, don't show them on the schedule list
                        $year_start = $year - 1 . "-12-31"; $year_end = $year + 1 . "-01-01";
@@ -2249,9 +2217,9 @@ class tc
 
                        if(!$this->db2->next_record()) {
                                $sql = "SELECT * FROM tc_visit WHERE family=" . $id . " AND companionship=0 ORDER BY date DESC";
-                               $this->db->query($sql,__LINE__,__FILE__);
-                               if($this->db->next_record()) { 
-                                       $date = $this->db->f('date'); 
+                               $this->db3->query($sql,__LINE__,__FILE__);
+                               if($this->db3->next_record()) { 
+                                       $date = $this->db3->f('date'); 
                                } else { 
                                        $date = ""; 
                                }
@@ -2376,7 +2344,7 @@ class tc
                        $this->t->set_var('lang_num_months','Years of History');
                }
 
-               $sql = "SELECT * FROM tc_presidency where president=1 and valid=1";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                if($this->db->next_record()) {
                        $president_name = $this->db->f('name');
@@ -2390,15 +2358,15 @@ class tc
                $this->t->set_var('district_name',$president_name);
 
                // TODO:  changed this so it picks the quorum dynamically
-               $sql = "SELECT * FROM tc_individual where steward='Elder' and valid=1 ORDER BY individual ASC";
+               $sql = "SELECT * FROM tc_individual AS ti JOIN tc_scheduling_priority as tsp where ti.scheduling_priority=tsp.scheduling_priority and ti.steward='Elder' and ti.valid=1 ORDER BY ti.individual ASC";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
                        $individual[$i] = $this->db->f('individual');
                        $indiv_name[$i] = $this->db->f('name');
                        $indiv_phone[$individual[$i]] = $this->db->f('phone');
-                       $indiv_ppi_pri[$individual[$i]] = $this->db->f('ppi_pri');
-                       $indiv_ppi_notes[$individual[$i]] = $this->db->f('ppi_notes');
+                       $indiv_priority[$individual[$i]] = $this->db->f('priority');
+                       $indiv_notes[$individual[$i]] = $this->db->f('notes');
                        $i++;
                }
                $total_indivs=$i;
@@ -2500,7 +2468,7 @@ class tc
                $notes = get_var('notes',array('GET','POST'));
                $interview_type = get_var('interview_type',array('GET','POST'));
 
-               $sql = "SELECT * FROM tc_presidency where valid=1 and (president=1 or counselor=1 or secretary=1)";
+               $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)";
                $this->db2->query($sql,__LINE__,__FILE__);
                while ($this->db2->next_record()) {
                        $indiv = $this->db2->f('individual');
@@ -2633,7 +2601,7 @@ class tc
                else if($current_month >= 7 && $current_month <= 9) { $current_month=9; }
                else if($current_month >= 10 && $current_month <= 12) { $current_month=12; }
 
-               $sql = "SELECT * FROM tc_district where valid=1 ORDER BY district ASC";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
@@ -2823,7 +2791,7 @@ class tc
                $notes = get_var('notes',array('GET','POST'));
                $interview_type = get_var('interview_type',array('GET','POST'));
 
-               $sql = "SELECT * FROM tc_presidency where valid=1 and (president=1 or counselor=1 or secretary=1 or district!=0)";
+               $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)";
                $this->db2->query($sql,__LINE__,__FILE__);
                while ($this->db2->next_record()) {
                        $indiv = $this->db2->f('individual');
@@ -2965,7 +2933,7 @@ class tc
                for ($i=0; $i < count($visit_list); $i++) {
                        $this->nextmatchs->template_alternate_row_color(&$this->t);
 
-                       $sql = "SELECT * FROM tc_family WHERE family=".$visit_list[$i]['family'];
+                       $sql = "SELECT * FROM tc_family AS tf JOIN tc_individual AS ti WHERE tf.individual=ti.individual AND tf.family=".$visit_list[$i]['family'];
                        $this->db->query($sql,__LINE__,__FILE__);
                        $this->db->next_record();
 
@@ -2993,7 +2961,7 @@ class tc
                }
 
                // List the families that are available to record a visit against
-               $sql = "SELECT * FROM tc_family WHERE companionship != 0 and valid=1";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                $total_records = $this->db->num_rows();
 
@@ -3488,7 +3456,6 @@ class tc
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
-                       $calling[$i]['id'] = $this->db->f('individual');
                        $calling[$i]['name'] = $this->db->f('name');
                        $calling[$i]['position'] = $this->db->f('position');
                        $calling[$i]['sustained'] = $this->db->f('sustained');
@@ -3510,11 +3477,10 @@ class tc
                }
 
                # Display a list ordered by organization
-               $sql = "SELECT * FROM tc_calling ORDER BY sequence ASC";
+               $sql = "SELECT * FROM tc_calling ORDER BY organization ASC";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
-                       $calling[$i]['id'] = $this->db->f('individual');
                        $calling[$i]['name'] = $this->db->f('name');
                        $calling[$i]['position'] = $this->db->f('position');
                        $calling[$i]['sustained'] = $this->db->f('sustained');
@@ -3570,7 +3536,7 @@ class tc
                $header_row.= "<th width=$location_width><font size=-2>Location</th>";
                $table_data = "";
 
-               $sql = "SELECT * FROM tc_presidency where valid=1 GROUP BY individual ORDER BY name ASC";
+               $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";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
@@ -3582,18 +3548,14 @@ class tc
                        $i++;
                }
 
-               $sql = "SELECT * FROM tc_family where valid=1 and individual != 0 ORDER BY name ASC";
+               $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 ORDER BY ti.name ASC";
                $this->db->query($sql,__LINE__,__FILE__);
                $i=0;
                while ($this->db->next_record()) {
                        $family_id[$i] = $this->db->f('family');
                        $family_name[$i] = $this->db->f('name');
                        $familyid2name[$family_id[$i]] = $family_name[$i];
-                       $sql = "SELECT * FROM tc_individual where family='$family_id[$i]' and hh_position='Head of Household'";
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       if($this->db2->next_record()) {
-                               $familyid2address[$family_id[$i]] = $this->db2->f('address');
-                       }
+                       $familyid2address[$family_id[$i]] = $this->db->f('address');
                        $i++;
                }
                array_multisort($family_name, $family_id);
@@ -4081,7 +4043,6 @@ class tc
                                                $this->db2->query("UPDATE tc_presidency set" .
                                                                  " individual=" . $indiv . 
                                                                  " ,district=" . $district . 
-                                                                 " ,name='" . $name . "'" .
                                                                  " ,email='" . $email . "'" .
                                                                  " ,president='" . $president . "'" .
                                                                  " ,counselor='" . $counselor . "'" .
@@ -4089,10 +4050,10 @@ class tc
                                                                  " WHERE presidency=" . $id,__LINE__,__FILE__);
                                        } else {
                                                //print "Adding New Entry<br>";
-                                               $this->db2->query("INSERT INTO tc_presidency (presidency,individual,district,name," .
+                                               $this->db2->query("INSERT INTO tc_presidency (presidency,individual,district," .
                                                                  "email,president,counselor,secretary,valid) " .
                                                                  "VALUES (NULL,'" . $indiv . "','" . $district . "','" .
-                                                                 $name . "','" . $email . "','" . $president  . "','" .
+                                                                 $email . "','" . $president  . "','" .
                                                                  $counselor . "','" . $secretary . "','1'" .
                                                                  ")",__LINE__,__FILE__);
                                        }
@@ -4112,13 +4073,13 @@ class tc
                        $name = "High Priests";
                        $indiv = 0;
                        $valid = 0;
-                       $this->db2->query("INSERT INTO tc_district (district,name,supervisor,valid) " .
-                                         "VALUES ('" . $district . "','" . $name . "','" .
+                       $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
+                                         "VALUES ('" . $district . "','" . 
                                          $indiv . "','" . $valid . "'" .
                                          ")",__LINE__,__FILE__);
 
                        // Requery the tc_presidency table
-                       $sql = "SELECT * FROM tc_presidency where valid=1";
+                       $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
                        $this->db->query($sql,__LINE__,__FILE__);
                        while ($this->db->next_record()) {
                                // Extract the data for each presidency record
@@ -4126,13 +4087,12 @@ class tc
                                $indiv = $this->db->f('individual');
                                $name = $this->db->f('name');
                                $district = $this->db->f('district');
-                               $name = $this->db->f('name');
                                $valid = 1;
 
                                // If we have a valid district, add it to the district table
                                if($district > 0) {
-                                       $this->db2->query("INSERT INTO tc_district (district,name,supervisor,valid) " .
-                                                         "VALUES ('" . $district . "','" . $name . "','" .
+                                       $this->db2->query("INSERT INTO tc_district (district,supervisor,valid) " .
+                                                         "VALUES ('" . $district . "','" . 
                                                          $indiv . "','" . $valid . "'" .
                                                          ")",__LINE__,__FILE__);
                                }
@@ -4149,7 +4109,7 @@ class tc
 
                // Now save off the data needed for a Presidency Table Update
 
-               $sql = "SELECT * FROM tc_presidency where valid=1";
+               $sql = "SELECT tp.*, ti.name FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.valid=1";
                $this->db->query($sql,__LINE__,__FILE__);
                $table_data = "";
                $header_row = "<th>Individual</th><th>Email</th><th>District</th><th>President</th><th>Counselor</th><th>Secretary</th>";
@@ -4319,7 +4279,7 @@ class tc
                        $dtstart = gmdate("Ymd"."\T"."His"."\Z", mktime($hour,$minute,$seconds,$month,$day,$year));
                        $dtstartstr = date("l, F d, o g:i A", mktime($hour,$minute,$seconds,$month,$day,$year));
 
-                       $sql = "SELECT * FROM tc_presidency where presidency='$presidency'";
+                       $sql = "SELECT * FROM tc_presidency AS tp JOIN tc_individual AS ti WHERE tp.individual=ti.individual AND tp.presidency='$presidency'";
                        $this->db2->query($sql,__LINE__,__FILE__);
                        if($this->db2->next_record()) {
                                $email = $this->db2->f('email');
@@ -4341,18 +4301,18 @@ class tc
                        }
 
                        if($family > 0) {
-                               $sql = "SELECT * FROM tc_family where family='$family'";
+                               $sql = "SELECT * FROM tc_family WHERE family='$family'";
                                $this->db2->query($sql,__LINE__,__FILE__);
                                if($this->db2->next_record()) {
-                                       $family_name = $this->db2->f('name');
-                                       $phone = $this->db2->f('phone');
                                        $individual = $this->db2->f('individual');
-                                       $appt_name = $family_name . " Family Visit";
                                        $sql = "SELECT * FROM tc_individual where individual='$individual'";
                                        $this->db3->query($sql,__LINE__,__FILE__);
                                        if($this->db3->next_record()) {
                                                $phone = $this->db3->f('phone');
+                                               $family_name = $this->db3->f('name');
+                                               $phone = $this->db3->f('phone');
                                        }
+                                       $appt_name = $family_name . " Family Visit";
                                        $duration = $this->default_visit_appt_duration * 60;
                                }
                        }