<?php
/* form variables: 
     code =  EXAM
     text : module code [component]
     from : mobile number - not used here
*/

function smartImplode($a,$delim,$last) {
   
$s =$a[0];
   if (
count($a) > 1) {
      for(
$i 1;$i count($a)-1$i++)  $s.=$delim.$a[$i];
      
$s.=$last.$a[count($a)-1];
   }
   return 
$s;
}


function 
getModuleExam($modcode,$component) {
   global 
$dblink$year$session;

   
$query "select * from module where mcode like '%$modcode%'";

   if(!(
$dbresult mysql_query($query,$dblink))) return  "Database Failure" ;

   if (
mysql_num_rows($dbresult) == 0)  return "No module with a code matching  $modcode";

   if (
mysql_num_rows($dbresult) > ) return "Multiple modules match $modcode - you will have to be more specific";

   
$module mysql_fetch_object($dbresult);
   
$selectComponent = ($component <>'') ? "and component = '$component' " '';
   
$query "select * from exam 
             where mcode = '$module->mcode' and academicyear = $year and session = '$session' $selectComponent"
;

   if(!(
$dbresult mysql_query($query,$dblink))) return "Database Failure" ;

   if ((
$examcount mysql_num_rows($dbresult)) == 
      if (
$component =='null' )return "No exams for $module->mcode : $module->title ";
      else return 
"No $component exam for $module->mcode : $module->title";
   if (
$examcount == ) {
      
$exam mysql_fetch_object($dbresult);
      
$exam->starttime substr($exam->starttime,0,5);
      
$exam->endtime substr($exam->endtime,0,5);
      
$exam->tsstrtotime("$exam->startdate $exam->starttime");
      
$exam->pstartdate=date("l, d M 'y",$exam->ts);
      
$query "select * from exam_room
             where mcode = '$exam->mcode' and 
                   academicyear = $exam->academicyear and 
                   session = '$exam->session' and
                   component = '$exam->component'"
;

       if(!(
$dbresult2 mysql_query($query,$dblink))) return "Database Failure" ;

       
$exam->rooms = array();
       while (
$room mysql_fetch_object($dbresult2)) {
            
$exam->rooms[] = $room->roomno;
       }
       if (
$exam->component =='null'$exam->component '';
       
$exam->prooms smartImplode($exam->rooms,", "," and ");
       
$response"$exam->component Exam for $module->mcode : $module->title on " .
                   
"$exam->pstartdate starting at $exam->starttime " 
                    
"and finishing at $exam->endtime in "$exam->prooms;
       return 
$response;
   }

// multiple components

   
$comps=array();
   while(
$exam mysql_fetch_object($dbresult)) {
      
$comps[]= $exam->component;
   }
   
$cs smartImplode($comps,", "," and ");
 
   
$response "There are $examcount exams for $module->mcode : $module->title : $cs.  After a space, add the required component to the module code.";
   return 
$response;
}  
 
//-------- main --------------------------------------------------------------------------
   
$text trim(strtoupper($text));
   if (
$sep strpos($text,' ')) {
      
$mcodesubstr($text,0,$sep-1);
      
$comp=trim(substr($text,$sep));
   }
   else {
      
$mcode=$text;
      
$comp='';
   }

// connect to the database
   
require('../../../examsql.inc');

   if(!(
$dblink=mysql_connect($server,$user,$password)))  return "Database Failure" ;

// select the database for subsequent queries
   
if(!mysql_select_db($db,$dblink))  return "Database Failure" ;

// these should be deduced from the date and the sesssion table
   
$year 2004$session='Jan';

   if (
$mcode <> '') {
      
$r getModuleExam($mcode,$comp);
      print 
"Reply:$r";
   }
   else print 
"Reply: Send a module code e.g. UFIE94-20-2 or just the distinct part E94"


?>