<!--
    Brian Platz
    CS85 Section 1643
    Professor Geddes
    March 14, 2012
    Assignment: Final Project
        
        THIS FILE:     CONTAINS FUNCTIONS USED BY PATHMATE_QA.PHP
                    IT IS 'INCLUDED' IN PATHMATE_QA.PHP
-->

<?php
function connect() {                // connects to database
    
include 'include.inc';
    
$conn mysql_connect($localhost$localUser$localPassword);
    
mysql_select_db($db$conn
       or die(
'Unable to select database.  ' mysql_error()); 
    
$result mysql_query("USE ".$db$conn) or die( mysql_error() );
    }
    
function 
printLoginForm() {            // prints login form to validate users
    
ECHO <<<HERE
    <h6>PathMate&reg; QA -- Login</h6>
         <form id='login' name='login' method='post' > 
           <fieldset class='center' style='width:12em'>
             <legend>Login:</legend>
             <table>
             <tr><td><label>Username: </label></td><td><input type='text' name='usr' id='usr'/></td></tr><br/>
             <tr><td><label>Password: </label></td><td><input type='password' name='pwd' id='pwd'/></td></tr>
             </table>          
           <tr colspan="2"\><td><h1><button name='btnLogin' id='btnLogin' type='submit' value='Submit' class='center'>Submit</button></h1></td></tr>
        </fieldset> 
      </form>
HERE;
}

function 
printWorkForm() {            // once user is validated, prints form for routine use
    
include 'include.inc';
    
connect();        
    
// create doctor selectbox
    
$sql "SELECT doctorID, name FROM platz_doctors ORDER BY name";
        
// assign query results to a variable
    
$docs mysql_query($sql$conn) or die("Error in getting info for Docs select: ".mysql_error() );
        
// generate a select box of mds       
    
$selMD getSelect('selMD'$docs);
    
    
// create 'nurse' selectbox
    
$sql "SELECT nurseID, name FROM platz_nurses ORDER BY name";
        
// assign query results to a variable
    
$nurses mysql_query($sql$conn) or die("Error in getting info for Nurses select: ".mysql_error() );
        
// generate a select box of rns        
    
$selRN getSelect('selRN'$nurses);
    
    
// create 'location' selectbox
    
$sql "SELECT locationID, location FROM platz_locations ORDER BY location";
        
// assign query results to a variable
    
$locs mysql_query($sql$conn) or die("Error in getting info for Locations select: ".mysql_error() );
        
// generate a select box of locations        
    
$selLoc getSelect('selLoc'$locs);
    
    
// create 'doctor' selectbox
    
$sql "SELECT errorID, error FROM platz_errors ORDER BY error";
        
// assign query results to a variable
    
$errors mysql_query($sql$conn) or die("Error in getting info for Errors select: ".mysql_error() );
        
// generate a select box of errors        
    
$selErrs getSelect('selErrs'$errors);
    
    
mysql_close(); 
    
        
// print out form
    
ECHO <<<HERE
      <h6>PathMate&reg; QA</h6>
         <form id='PMQA' name='PMQA' method='post' > 
           <fieldset class='center' style='width:18em'>
             <legend>Enter Incident:</legend>
             <table>
             <tr><td><label>Path Number: </label></td><td><input type='text' name='caseNum' id='caseNum'/></td></tr><br/>
             <tr><td><label>Doctor: </label></td><td>
$selMD</td></tr>
             <tr><td><label>Nurse: </label></td><td>
$selRN</td></tr>
             <tr><td><label>Location: </label></td><td>
$selLoc</td></tr>
             <tr><td><label>Error: </label></td><td>
$selErrs</td></tr>
             </table>     
               <h1><button type='submit' name='btnSub' id='btnSub' value='Submit' class='center'>Submit</button></h1>
        </fieldset> 
      </form>
      <form id='PMQA' name='PMQA' method='post' > 
        <h1><button type='submit' name='btnPrint' id='btnPrint' value='Print Report' class='center'>Print Report</button></h1>
      </form>
      <form id='PMQA' name='PMQA' method='post' >
        <h1><button type='submit' name='btnLogout' id='btnLogout' value='Logout' class='center'>Logout</button></h1>  
      </form>
HERE;
}

function 
verifyUser$usr$pwd ) {                                // validate login
    
include 'variables.inc';
    include 
'db_connection_info.inc';
    
$conn mysql_connect($localhost$localUser$localPassword);
    
mysql_select_db($db$conn);
    
$sql "SELECT password FROM platz_users WHERE userName = '".$usr."'" ;     // check for user, get password
    
$result mysql_query($sql$conn) or die( mysql_error() );
    
mysql_close(); 

    
$valid false;                                                // initialize valid to false
    
if ($result) {                                                // if user found...
        
while ( $row mysql_fetch_assoc($result) ) {
            foreach(
$row as $key=>$value) {
                if (
$value == $pwd) {                                     // ...and password matches...
                    
$valid true;                                         // ...validate user
                
}
            }    
        } 
    } 
    return 
$valid;


function 
getSelect($name$listVals) {            // function to create selectboxes, inputting name of selectbox and list of values
    
include "include.inc" ;       
    
     
$sel "";
     
$sel "<select name=\"$name\" >\n";
     while (
$row mysql_fetch_assoc($listVals) ) {
         
$first 1;
         foreach (
$row as $index => $value) {
            if (
$first) { 
                
$sel .= "    <option value= \"$value\">"
            } else { 
                
$sel .= "$value</option> \n"
            }
            
$first 0;
         } 
// end foreach
     
// end while
     
$sel .= "</select> \n"
     return 
$sel;
}
    
function 
storeEvent$case$location$MD$RN$error ) {        // function to store entered event into db
    
include 'include.inc';
    
connect();
    
$sql "INSERT INTO platz_events VALUES( null, CURDATE(), '$case', '$location', '$MD', '$RN', '$error' )";
    
    
$result mysql_query($sql$conn) or die( mysql_error() );
    
mysql_close(); 
}

function 
printEvents() {                        // function to print out all recorded events
    
include 'include.inc';
    
connect();
    
$sql "SELECT ev.date_entered AS 'DATE', ev.case_id AS 'CASE', l.location AS 'LOCATION',
            d.name AS 'DOCTOR', n.name AS 'NURSE', er.error AS 'ERROR'
        FROM platz_events AS ev
        INNER JOIN platz_locations AS l ON l.locationID = ev.location_id
        INNER JOIN platz_doctors AS d ON d.doctorID = ev.doctor_id
        INNER JOIN platz_nurses AS n ON n.nurseID = ev.nurse_id
        INNER JOIN platz_errors AS er ON er.errorID = ev.error_id
        ORDER BY ev.date_entered"
;
    
$result mysql_query($sql$conn) or die( mysql_error() );
    
mysql_close(); 
    
    
$output '<h1>EVENTS</h1><table class="outputTable"><tr><th>DATE</th><th>CASE</th><th>LOCATION</th><th>DOCTOR</th><th>NURSE</th><th>ERROR</th></tr><tr>';
    while (
$row mysql_fetch_assoc($result) ) {
        foreach (
$row as $name => $value) {
            
$output .= '<td>'.$value.'</td>';
        }
// end foreach
        
$output .= '</tr>';
    } 
// end while
    
$output .= '</table>';
    print 
$output;
}

?>