<?php
/* 

    This script is designed to work with a browser supporting XHTML+voice+xml, such as Opera 8.

    It will fall back to plain output on a non-enabled browser.

    form variables: 
     code -  module code or part thereof - required
     year - default 2004
     session - default Jan
  
    To do
      - year and session are fixed in the script - they could be deduced from the current date ?
      - could list the matched modules
      - could allow the user to speak a code - must check if the recogniser can do the alphabet


*/

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


function 
spell($s) {
    
$say="";
    for(
$i=0;$i<strlen($s);$i++) {
      
$say.=" ".$s[$i];
    }
    return 
$say;
}

function 
sayCode($code) {
    
$p explode('-',$code); 
    
$main=$p[0]; $size=$p[1]; $level=$p[2];
    
$say spell($main);
    
$say.=", $size credits at level $level";
    return 
$say;
}

function 
getExam($examTable) {
      global 
$dblink;
      if (! (
$exam mysql_fetch_object($examTable))) return ;

      
$exam->starttime substr($exam->starttime,0,5);
      
$exam->endtime substr($exam->endtime,0,5);
      
$exam->hours substr($exam->duration,0,2) + 0;
      
$exam->minutes substr($exam->duration,2,2) + 0;
      
$exam->length = ($exam->hours 1)  ? "$exam->hours hours""$exam->hours hour";
      if (
$exam->minutes )  $exam->length .= " and $exam->minutes minutes";

      
$exam->tsstrtotime("$exam->startdate $exam->starttime");
      
$exam->pstartdate=date("l, d F ",$exam->ts);
      
$query "select * from exam_room
             where mcode = '$exam->mcode' and 
                   academicyear = $exam->academicyear and 
                   session = '$exam->session' and
                   component = '$exam->component' "
;

       if(!(
$roomTable mysql_query($query,$dblink))) print "Database Failure" ;

       
$exam->rooms = array();
       while (
$room mysql_fetch_object($roomTable)) {
            
$exam->rooms[] = $room->roomno;
       }
       
$exam->prooms smartImplode($exam->rooms,", "," and ");
       return 
$exam;
}

function 
getModuleExams($modcode) {
   global 
$dblink;
   global 
$year$session;

   
$sayCode spell($modcode);


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

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


   if (
mysql_num_rows($moduleTable) == 0)  {
       
$module->say "No module with a code matching  $sayCode exists.";
       return 
$module;
   }

   if (
mysql_num_rows($moduleTable) > ) {
       
$module->say "Multiple modules match $sayCode - you will have to be more specific";
       return 
$module;
   }

   
$module mysql_fetch_object($moduleTable);
   
$module->sayCode sayCode($module->mcode);
   
$query "select * from exam 
             where mcode = '$module->mcode' and academicyear = $year and session = '$session' order by startdate"
;

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

   if ((
$examcount mysql_num_rows($examTable)) == 
      if (
$component =='null' ) {
             
$module->say "No exams for $module->sayCode, $module->title " ;
              return 
$module;
      }
      else {
           
$module->say "No examination for $module->saycode, $module->title in the $session session.";
           return 
$module;
      }
   if (
$examcount==1) {
      
$exam getExam($examTable);

      
$module->say "The examination for $module->sayCode, $module->title, lasting $exam->length, is on " .
                   
"$exam->pstartdate starting at $exam->starttime " 
                    
"and finishing at $exam->endtime in "$exam->prooms;
      return 
$module;
   }
   else 

// multiple components
   
$module->say "There are $examcount examinations for $module->sayCode, $module->title. <br/>"
   while (
$exam getExam($examTable)) {
       if (
$exam->component == 'null'$exam->component "main";
       
$module->say.= "The $exam->component examination, lasting $exam->length, is on " .
                   
"$exam->pstartdate starting at $exam->starttime " 
                    
"and finishing at $exam->endtime in "$exam->prooms". <br/>";
   } 
   return 
$module;
}  
 
//-------- main --------------------------------------------------------------------------
// connect to the database
   
require('../../../examsql.inc');
 
   if(!(
$dblink=mysql_connect($server,$user,$password)))  {
        print 
"Database Failure" ;
        exit;
   }

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

   if(!isset(
$year)) $year 2004
   if(!isset(
$session)) $session='Jan';
   
$toyear $year ;

   if (isset(
$code)) {
      
$mcode trim(strtoupper($code));
      
$module getModuleExams($mcode);
   }

   if (
strpos(" ".$_SERVER["HTTP_ACCEPT"],"application/xhtml+voice+xml") ){
// - a speech enabled browser
       
header("Content-type :application/xhtml+voice+xml");
?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:vxml="http://www.w3.org/2001/vxml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xv="http://www.voicexml.org/2002/xhtml+voice"
>
  <head>
    <title>Examinations in CEMS: with text-2-speech</title>
    <!-- voice handler -->
    <vxml:form id="sayExam">
      <vxml:block>
          <vxml:prompt xv:src="#exams"/>
      </vxml:block>
    </vxml:form>
    <vxml:form id="sayTitle">
      <vxml:block>
         <vxml:prompt xv:src="#title"/>
      </vxml:block>
    </vxml:form>
  </head>
  <body ev:event="load" ev:handler="#sayExam">
   <h1 id=title ev:event="click" ev:handler="#sayTitle">Examinations for the academic year<?php echo " $year to $toyear, $session session" ?> </h1>
    <form method="get" action="<?php echo $PHP_SELF ?>">
    Module Code <input type="text" name="code"  value="<?php if(isset($code)) echo $code ?>"/>
    </form>
  <?php if(isset($module->mcode)) echo "<h2>$module->mcode : $module->title</h2>" ?>

  <p id="exams">
       <?php if(isset($module->say)) echo $module->say ?>
  </p>
  <p ev:event="click" ev:handler="#sayExam"><b>Say again </b></a>
  </body>
</html>

<?php
  
}
  else {
// speech not supported
?>
<html>
  <head>
    <title>Examinations in CEMS: no text-2-speech</title>
   </head>
  <body>
   <h1 id=title>Examinations for the academic year<?php echo " $year to $toyear, $session session" ?> </h1>
    <form method="get" action="<?php echo $PHP_SELF ?>">
    Module Code <input type="text" name="code"  value="<?php if(isset($code)) echo $code ?>"/>
    </form>
  <?php if(isset($module->mcode)) echo "<h2>$module->mcode : $module->title</h2>" ?>

  <p id="exams">
       <?php if(isset($module->say)) echo $module->say ?>
  </p>
  </body>
</html>

<?php
  
}

?>