From: pkiddie <pk...@us...> - 2005-09-12 09:47:17
|
Update of /cvsroot/stack/stack-1-0/scripts/rqp/moodle_16_rqp/quiz/questiontypes/rqp In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16221/scripts/rqp/moodle_16_rqp/quiz/questiontypes/rqp Added Files: RQPv1p0Client.php editquestion.php icon.gif lib.php nb_easyxml_lite.php nb_soapfuncs.php options.php questiontype.php rqp.html types.php uni_soap.php.tortoise.removed uni_soap.php5.tortoise.removed Log Message: Moodle V1.6 updated files - until new RQP code on Moodle CVS repository, overwrite latest distribution with these files --- NEW FILE: lib.php --- <?php // $Id: lib.php,v 1.1 2005/09/12 09:46:59 pkiddie Exp $ /** * Library of functions used by the RQP question type * * @version $Id: lib.php,v 1.1 2005/09/12 09:46:59 pkiddie Exp $ * @author Alex Smith and other members of the Serving Mathematics project * {@link http://maths.york.ac.uk/serving_maths} * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package quiz */ define('RQP_SERVER_RENDERING', 1); define('RQP_SERVER_IMPLICITCLONING', 2); define('RQP_SERVER_EXPLICITCLONING', 4); define('RQP_SERVER_AUTHORING', 8); define('RQP_SERVER_ADMINOPTIONS', 16); define('RQP_SERVER_TEACHEROPTIONS', 32); define('RQP_SERVER_USEROPTIONS', 64); //Define RQP version <TODO>: Should this be in the client define ('RQP_VER','1.0'); /** * Checks the current version of PHP and uses the correct instance checking function, based * upon an instanced object and its parent class * * is_a is deprecated in PHP 5, in favour of instanceof * <TODO>PHP4 parser does not like 'instanceof' * Source: www.php.net * @param object $IIO_INSTANCE the instantiated object * @param string $IIO_CLASS the class to check inheritance for */ function is_instance_of($IIO_INSTANCE, $IIO_CLASS) { /*if(floor(phpversion()) > 4) { if($IIO_INSTANCE instanceof $IIO_CLASS){ return true; } else{ return false; } } else*/if(floor(phpversion()) > 3){ return is_a($IIO_INSTANCE, $IIO_CLASS); } else{ return false; } } /** * Create an option dialog and pass $optionType and $type to options.php script by GET method */ function quiz_rqp_optionsDialog($optionType, $type) { $jsOpenOptions = '<script language="javascript" type="text/javascript">'; $jsOpenOptions .= 'javascript:'; $jsOpenOptions .= "window.open('options.php?typeid=$type&optionstype=$optionType','optionspopup', 'left=20,top=20,width=500,height=500,toolbar=0,resizable=1')"; $jsOpenOptions .= '</script>'; echo $jsOpenOptions; } /** * Outputs a key-val array based upon the POSTed $form variables. Only ouputs those that * belong to a particualar $inputPrefix * * Used to initialise the $inputData RenderType parameter * HACK: Also responsible for passing a 'save' request to the RQP server, as STACK authoring form * does not know by itself when the authoring form has gone to completion. * * @param object $form The $form variables * @param string $inputPrefix The prefix by which to retreive the $form variables for * @return array the key-val array $inputData */ function quiz_rqp_parse_input(&$form, $inputPrefix) { foreach($form->$inputPrefix as $arrayKey=>$formField) { //Check that the form field is not an array of PHP values - otherwise we must serialise if (!is_array($formField)) { $inputData[] = array( 'key' => $arrayKey, 'val' => $formField ); } else { $inputData[] = array( 'key' => $arrayKey, 'val' => serialize($formField)); } } //HACK. Authoring form doesnt know when to end an authoring session on its own, so tell it to if (isset($form->save)) { $inputData[] = array( 'key' => 'save', 'val' => 'save'); } return $inputData; } /** * Creates a colon separated list of space separated values from an associative * array of arrays * * An associative array of values or an associative array of arrays is imploded * to a string by creating a colon separated list of space separated values. The * key is treated as the first value. The {@link quiz_rqp_explode} function can * restore the array from this string representation. * @return string The string representation of the array. This is a colon * separated list of space separated values. * @param array $array An associative array of single values or an associative * array of arrays to be imploded. */ function quiz_rqp_implode($array) { if (count($array) < 1) { return ''; } $str = ''; foreach ($array as $key => $val) { $str .= $key . ' '; if (is_array($val)) { if (count($val) > 0) { foreach ($val as $subval) { $str .= $subval . ' '; } // Remove the trailing space $str = substr($str, 0, -1); } } else { $str .= $val; } $str .= ':'; } // Remove the trailing semi-colon return substr($str, 0, -1); } /** * Recreates an associative array or an associative array of arrays from the * string representation * * Takes a colon separated list of space separated values as produced by * {@link quiz_rqp_implode} and recreates the array. If an array of single values * is expected then an error results if an element has more than one value. * Otherwise every value is an array. * @return array The associative array restored from the string. Every * element is a single value if $multi is false or an array * if $multi is true. * @param string $str The string to explode. This is a colon separated list of * space separated values. * @param boolean $multi Flag indicating if the values in the array are expected * to be of multiple cardinality (i.e. an array of arrays * is expected) or single values (i.e. an array of values). * The default is false indicating an array of single * values is expected. */ function quiz_rqp_explode($str, $multi=false) { // Explode by colon if ($str === '') { return array(); } $array = explode(':', $str); $n = count($array); $return = array(); for ($i = 0; $i < $n; $i++) { // Explode by space $array[$i] = explode(' ', $array[$i]); // Get the key $key = array_shift($array[$i]); if (array_key_exists($key, $return)) { // Element appears twice! return false; } // Save the element if ($multi) { $return[$key] = $array[$i]; } else if (count($array[$i]) > 1) { return false; } else { $return[$key] = $array[$i][0]; } } return $return; } function quiz_rqp_print_serverinfo($serverinfo) { $info->align = array('right', 'left'); $info->data = array(); // will hold the data for the info table $info->data[] = array('<b>'.get_string('name').':</b>',$serverinfo['type']); $info->data[] = array('<b>'.get_string('serveridentifier', 'quiz').':</b>',$serverinfo['version']); $info->data[] = array('<b><a href="'.$serverinfo['details'].'">'.get_string('description').'</a></b>'); print_table($info); } function quiz_rqp_debug_soap($item) { global $CFG; if ($CFG->debug) { echo 'Here is the dump of the soap fault:<pre>'; var_dump($item); echo '<pre>'; } } //Returns a server url given a specified type, by randomly selecting a server function quiz_rqp_get_server($typeid) { if (!$servers = get_records('quiz_rqp_servers', 'typeid', $typeid)) { return false; } shuffle($servers); return $servers[0]; } function quiz_rqp_get_server_old($typeid) { if (!array_key_exists($typeid, $remote_connections)) { // get the available servers if (!$servers = get_records('quiz_rqp_servers', 'typeid', $typeid)) { // we don't have a server for this question type return false; } // put them in a random order shuffle($servers); // go through them and try to connect to each until we are successful foreach ($servers as $server) { if ($remote_connections[$typeid] = rqp_connect($server->url)) { break; // we have a connection } else { // We have a dead server here, should somehow flag that } } } // check that we did get a connection if (!$remote_connections[$typeid]) { unset($remote_connections[$typeid]); return false; } return true; } ?> --- NEW FILE: nb_soapfuncs.php --- <?php /***************************************************************************** The purpose of this library is to provide a very simple, interoperable SOAP implentation for PHP that is not dependent on any optional libraries and can function with both PHP version 4.3 and PHP version 5. This library is expected to be used with generated code from ws_gen and requires nb_easyxml.php. *****************************************************************************/ class nbSOAPFault { var $faultcode; var $faultstring; var $faultdetail; function nbSOAPFault($faultcode, $faultstring, $faultdetail) { $this->faultcode = $faultcode; $this->faultstring = $faultstring; $this->faultdetail = $faultdetail; $this->faultactor = $_SERVER['PHP_SELF']; } function soapify() { $out = "<soap:Fault>\n"; $out .= "<faultcode>".$this->faultcode."</faultcode>\n"; $out .= "<faultstring>".$this->faultstring."</faultstring>\n"; $out .= "<faultactor>".$this->faultactor."</faultactor>\n"; $out .= "<faultdetail>".$this->faultdetail."</faultdetail>\n"; $out .= "</soap:Fault>"; return $out; } } function desoap_nbSOAPFault($xmp, $cp) { return new nbSOAPFault("","",""); } class nbSOAP { //# this is just a placeholder for now, eventually it will // hold serialization methods for inbuilt types and // the SOAP request functions. } // HTTP 1.1 version; under development, still to get proxy support and error reporting. function nbSOAP_request11($URI, $SOAPAction, $data, $timeLimit = 20) { $contents = ""; $urldata = parse_url($URI); if(!array_key_exists('port', $urldata)) $urldata['port']=80; // $request = "GET " . $urldata['path'] . " HTTP/1.1\r\n"; $request = "POST " . $urldata['path'] . " HTTP/1.1\r\n"; $request .= "Host: ". $urldata['host'] ."\r\n"; $request .= "Accept: */*\r\n"; $request .= "User-Agent: PHP-script\r\n"; $request .= "Content-Type: text/xml; charset=utf-8\r\n"; $request .= "Content-length: " . strlen($data) . "\r\n"; $request .= "SOAPAction: \"" . $SOAPAction . "\"\r\n\r\n"; $request .= $data; $startTime = time(); $sock = fsockopen($urldata['host'],$urldata['port'], $errno, $errmsg, $timeLimit); if($sock==FALSE) { $fault = new nbSOAPFault("Server", "Timed out trying to contact server.", "No response from server."); $fault = "<soap:Body>\n" . $fault->soapify() . "</soap:Body>\n"; return nbSOAP_Envelope($fault); } stream_set_timeout($sock, 2); stream_set_blocking($sock, false); //echo "<pre>".htmlentities($request)."</pre>"; // Uncomment this for debugging if($sock == false) { return false; } else { fputs($sock, $request); //# this part needs to properly check the length of response, and //# time out neatly if there's too long a delay. $headersComplete = false; $pageComplete = false; $cl = -1; while ((!feof($sock))&&(!$pageComplete)) { $contents .= fread($sock, 8192); $cl = strlen($contents); if(!$headersComplete) { //# remove any leading continues $headerEnd = strpos($contents,"\r\n\r\n"); while(($headerEnd != false)&&(!$headersComplete)) { //echo "<p><i><b>Hdrend</b><br/><pre>[" . htmlentities($contents) . "]</pre></i></p>"; $fle = strpos($contents,"\r\n"); // first line end $line = substr($contents, 0, $fle); list($protocol, $rcode) = split(" ", $line, 3); $rcode = intval($rcode); // ensure its an integer; if(($rcode >= 100)&&($rcode <= 199)) // informational, I'm not interested { $contents = substr($contents, $headerEnd+4); $headerEnd = strpos($contents,"\r\n\r\n"); } elseif($rcode == 200) { $headersComplete = true; // check for chunked encoding $clp = strpos($contents,"Transfer-Encoding:"); // check for content length $clp = strpos($contents,"Content-Length:"); if($clp != false) { $cle = strpos($contents,"\r\n",$clp); $contentLength = intval(trim(substr($contents, $clp+15, $cle-$clp-15))); $headerLength = $cle+4; } else // no content length { // should really check that it is either chunked or CloseConnection // and through an exception if not... $contentLength = -1; } } else { $fault = new nbSOAPFault("Server", "An http error was returned, http code $rcode", "An http error was returned, http code $rcode"); $fault = "<soap:Body>\n" . $fault->soapify() . "</soap:Body>\n"; return nbSOAP_Envelope($fault); } } } if($headersComplete) // not an else because I want it done if headersComplete just set { if(($contentLength!=-1)&&(strlen($contents)>=$contentLength+$headerLength)) $pageComplete = true; } $callTime = time()-$startTime; if($callTime >= $timeLimit) { $pageComplete = true; $fault = new nbSOAPFault("Server", "Timed out calling server.", "No response, or an incomplete response before the request timed out."); $fault = "<soap:Body>\n" . $fault->soapify() . "</soap:Body>\n"; return nbSOAP_Envelope($fault); } //echo "<p><i><pre>[" . htmlentities($contents) . "]</pre></i></p>"; } fclose($sock); } //echo "<pre>" . htmlentities($contents) . "</pre>"; // Uncomment this for debugging return $contents; } function nbSOAP_request10($URI, $SOAPAction, $data) { $contents = ""; $urldata = parse_url($URI); if(!array_key_exists('port', $urldata)) $urldata['port']=80; $request = "POST " . $urldata['path'] . " HTTP/1.0\r\n"; $request .= "Host: ". $urldata['host'] ."\r\n"; $request .= "Accept: */*\r\n"; $request .= "User-Agent: PHP-script\r\n"; $request .= "Content-Type: text/xml; charset=utf-8\r\n"; $request .= "Content-length: " . strlen($data) . "\r\n"; $request .= "SOAPAction: \"" . $SOAPAction . "\"\r\n\r\n"; $request .= $data; $sock = fsockopen($urldata['host'],$urldata['port'], $errno, $errmsg, 30); stream_set_blocking($sock, false); echo "<pre>".htmlentities($request)."</pre>"; // Uncomment this for debugging if($sock == false) { return false; } else { fputs($sock, $request); while (!feof($sock)) { $contents .= fread($sock, 8192); } fclose($sock); } echo "<pre>" . htmlentities($contents) . "</pre>"; // Uncomment this for debugging return $contents; } function nbSOAP_Envelope($body) { $body = trim($body); if(substr($body,0,10) != "<soap:Body") return false; $soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; $soapXML .= "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; $soapXML .= "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"; $soapXML .= $body; $soapXML .= "\n</soap:Envelope>\n"; return $soapXML; } function encodeXMLSafeString($in) { // this is virtually the same as htmlspecialchars, however I've made my own version to // ensure it always matches the decodeXMLSafeString function. $plain = array("&", "<", ">", "\""); $code = array("&", "<", ">", """); return str_replace($plain, $code, strval($in)); } function decodeXMLSafeString($in) { $code = array("<", ">", """, "&"); $plain = array("<", ">", "\"", "&"); return str_replace($code, $plain, $in); } ?> --- NEW FILE: questiontype.php --- <?php // $Id: questiontype.php,v 1.1 2005/09/12 09:46:59 pkiddie Exp $ /** * This file defines the RQP question type class * * @version $Id: questiontype.php,v 1.1 2005/09/12 09:46:59 pkiddie Exp $ * @author Alex Smith and other members of the Serving Mathematics project * {@link http://maths.york.ac.uk/serving_maths} * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package quiz */ require_once($CFG->dirroot . '/mod/quiz/questiontypes/rqp/lib.php'); //require_once($CFG->dirroot . '/mod/quiz/questiontypes/rqp/remote.php'); require_once($CFG->dirroot . '/mod/quiz/questiontypes/rqp/RQPv1p0Client.php'); /** * RQP question type class */ class quiz_rqp_qtype extends quiz_default_questiontype { /** * Name of the rqp question type * * @ return string 'rqp' */ function name() { return 'rqp'; } /** * * Save the RQP question * * We must only save the RQP question when the server has indicated to us that it is complete */ function save_question($question, &$form, $course) { if ($form->completion=='complete') { $question = parent::save_question($question, $form, $course); } return $question; } /** * Indicates to question.php that there are no more authoring rendering forms to process. * Authoring has reached completion */ function finished_edit_wizard(&$form) { return isset($form->backtoquiz); } /** * Echos any hidden fields at the bottom of the RQP script. * Custom definition from base questiontype.php */ function print_question_form_end($question, $submitscript='') { global $USER; echo '<tr valign="top"> <td colspan="2" align="center"> <input type="submit" '.$submitscript.' value="'.'edit'.'" /> '; if ($question->id) { // Switched off until bug 3445 is fixed // echo '<input type="submit" name="makecopy" '.$submitscript.' value="'.get_string("makecopy", "quiz").'" /> '; } echo '<input type="submit" name="save" value="'.'save'.'" /> <input type="submit" name="cancel" value="'.get_string("cancel").'" /> <input type="hidden" name="sesskey" value="'.$USER->sesskey.'" /> <input type="hidden" name="id" value="'.$question->id.'" /> <input type="hidden" name="qtype" value="'.$question->qtype.'" />'; // The following hidden field indicates that the versioning code should be turned on, i.e., // that old versions should be kept if necessary echo '<input type="hidden" name="versioning" value="on" /> </td></tr>'; } /** * Save the type-specific options * * This also saves additional information that it receives from * an RQP_itemInformation call to the RQP server */ function save_question_options($form) { global $CFG; require_once("RQPv1p0Client.php"); $rqpserver = new RQPv1p0(); //Create new instance of RQP web service consumer // Check source type if (!$type = get_record('quiz_rqp_types', 'id', $form->type)) { $result->notice = get_string('invalidsourcetype', 'quiz'); return $result; } if (!$server = quiz_rqp_get_server($type->id)) { $result->notice = get_string('noserverforrqptype', 'quiz'); return $result; } else { //Choose RQP server of correct type $rqpserver->Url = $server->url; //Take URL of server as one that is retrieved from random fn // Create the object to be stored in quiz_rqp table $options = new object; $options->question = $form->id; $options->type = $form->type; $options->source = $form->source; //Check source file and retrieve more fields from Item information call $itemResponse = $rqpserver->RQP_ItemInformation($form->source); //Check SOAP fault has not been returned if (is_instance_of($itemResponse,'nbSOAPFault')) { $result->notice = 'SOAP fault returned. Please try again!'; return $result; } if ($itemResponse['error']) { $result->notice = $item->error; return $result; } //Shouldnt return for a warning if ($itemResponse['warning']) { $result->notice = $item->warning; } if (!$itemResponse['itemProperties']['valid']) { $result->notice = 'item not valid'; //todo: translate return $result; } if ($itemResponse['itemProperties']['timeDependent']) { $result->noticeyesno = get_string('notimedependentitems', 'quiz'); } if ($itemResponse['itemProperties']['canComputerScore']) { $result->noticeyesno = get_string('nomanualmarkprovision', 'quiz'); } //itemProperties['adaptive'] ignored at the time being //Now perform the addition into the relevent fields $question = new object; //1. The quiz_questions table if ($question = get_record('quiz_questions', $form->id)) { $question->defaultgrade = $itemResponse['itemPropeties']['maxScore']; if (!update_record('quiz_questions', $question)) { $result->error = "Could not update quiz question to add default grade!"; return $result; } } //2. The quiz_rqp table // Save the options if ($old = get_record('quiz_rqp', 'question', $form->id)) { $old->type = $options->type; $old->source = $options->source; $old->template = $itemResponse['itemProperties']['template']; if (!update_record('quiz_rqp', $old)) { $result->error = "Could not update quiz rqp options! (id=$old->id)"; return $result; } } else { if (!insert_record('quiz_rqp', $options)) { $result->error = 'Could not insert quiz rqp options!'; return $result; } } return true; } } /** * Loads the question type specific options for the question. * * This function loads all question type specific options for the * question from the database into the $question->options field. * @return bool Indicates success or failure. * @param object $question The question object for the question. */ function get_question_options(&$question) { $options =& $question->options; if (!$options = get_record('quiz_rqp', 'question', $question->id)) { return false; } if (!$type = get_record('quiz_rqp_types', 'id', $options->type)) { return false; } //$options->type_name = $type->name; return true; } /** * Return a value or array of values which will give full marks if graded as * the $state->responses field * * The correct answers are obtained from the RQP server via the * RQP_SessionInformation operation * @return mixed An array of values giving the responses corresponding * to the (or a) correct answer to the question. * @param object $question The question for which the correct answer is to * be retrieved. * @param object $state The state object that corresponds to the question, * for which a correct answer is needed. */ function get_correct_responses(&$question, &$state) { $info = remote_session_info($question, $state); if (false === $info || is_soap_fault($info)) { return null; } return $info->correctResponses; } /** * Creates empty session and response information for the question * * This function is called to start a question session. Empty question type * specific session data and empty response data is added to the state object. * @return bool Indicates success or failure. * @param object $question The question for which the session is to be created. * @param object $state The state to create the session for. This is passed by * reference and will be updated. * @param object $cmoptions (not used) * @param object $attempt The attempt for which the session is to be * started. (not used) */ function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) { $state->responses = array('' => ''); $state->options->persistent_data = ''; return true; } /** * Restores the session data and most recent responses for the given state * * This function loads any session data associated with the question session * in the given state from the quiz_rqp_states table into the state object. * @return bool Indicates success or failure. * @param object $question The question object for the question including any * question type specific information. * @param object $state The saved state to load the session for. This * object is updated to include the question * type specific session information and responses * (it is passed by reference). */ function restore_session_and_responses(&$question, &$state) { if (!$options = get_record('quiz_rqp_states', 'stateid', $state->id)) { return false; } //from lib.php $state->responses = quiz_rqp_explode($options->responses); $state->options->persistent_data = $options->persistent_data; return true; } /** * Saves the session data and responses for the question in a new state * * This function saves all session data from the state object into the * quiz_rqp_states table * @return bool Indicates success or failure. * @param object $question The question object for the question including * the question type specific information. * @param object $state The state for which the question type specific * data and responses should be saved. */ function save_session_and_responses(&$question, &$state) { //from lib.php $options->stateid = $state->id; $options->responses = quiz_rqp_implode($state->responses); $options->persistent_data = $state->options->persistent_data; if ($state->update) { if (!$options->id = get_field('quiz_rqp_states', 'id', 'stateid', $state->id)) { return false; } if (!update_record('quiz_rqp_states', $options)) { return false; } } else { if (!insert_record('quiz_rqp_states', $options)) { return false; } } return true; } /** * Prints the main content of the question including any interactions * * This function prints the main content of the question which it obtains * from the RQP server via the Render operation. It also updates * $state->options->persistent_data and $state->options->template_vars * with the values returned by the RQP server. * @param object $question The question to be rendered. * @param object $state The state to render the question in. The grading * information is in ->grade, ->raw_grade and * ->penalty. The current responses are in * ->responses. The last graded state is in * ->last_graded (hence the most recently graded * responses are in ->last_graded->responses). * @param object $cmoptions * @param object $options An object describing the rendering options. */ function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) { global $CFG; require_once("RQPv1p0Client.php"); $rqpserver = new RQPv1p0(); //Create new instance of RQP web service consumer if (!$server = quiz_rqp_get_server($question->options->type)) { notify('No servers of specified type'); exit; } $rqpserver->Url = $server->url; // Use the render output created during grading if it exists if (isset($state->options->renderoutput)) { $output =& $state->options->renderoutput; // Initial render operation at beginning of session } else { //Initialise render parameters $render['source'] = $question->options->source; $render['options'] = array ( 'admin' => '' , 'teacher' => 'current quiz name', 'user' => '', 'user-agent' => '', 'accept' => '', 'language' => ''); $render['persistentData']= ''; $render['directives'] = array(''); $render['mimetypes'] = array('text/html'); $render['namePrefix'] = $question->name_prefix; //Must be name_prefix in order to process form fields $render['itemBase'] = ''; $render['resourceBase'] = $CFG->dataroot . '/moddata/quiz/rqp/resource/'; $render['tempfileBase'] = $CFG->dataroot . '/moddata/quiz/rqp/temp/'; $render['inputData']=array(); $output = $rqpserver->RQP_Render($render['source'],$render['options'], $render['persistentData'],$render['inputData'], $render['directives'],$render['mimetypes'], $render['namePrefix'],$render['itemBase'], $render['resourceBase'],$render['tempfileBase']); //Check SOAP fault has not been returned if (is_instance_of($output,'nbSOAPFault')) { notify('SOAP fault returned. Please try again!'); unset($output); exit; } } $state->options->persistent_data = $output->persistentData; //Echo all output params //1. Each head element if (!empty($output['head'])) { foreach($output['head'] as $htmlString) { echo($htmlString); } } //2. The title - most likely the question title if (!empty($output['output']['title'])) { echo '<h2>'. html_entity_decode($output['output']['title']) . '</h2><br />'; } //3. The question stem if (!empty($output['output']['stem'])) { echo '<div class="RQPstem">'; echo html_entity_decode($output['output']['stem']); echo '</div>'; } //3. The body - most likely interactions go here! if (!empty($output['output']['body'])) { echo html_entity_decode($output['output']['body']); } //4. Any feedback when required if ($options->feedback) { if (!empty($output['output']['feedback'])) { echo '<div class="RQPfeedback">'; echo html_entity_decode($output['output']['feedback']); echo '</div>'; } } //5. Any STACK responses if ($options->validation) { if (!empty($output['output']['response'])) { echo '<div class="RQPresponse">'; echo html_entity_decode($output['output']['response']); echo '</div>'; } } //6. Answer where required if (!empty($output['output']['answer'])) { echo '<div class="RQPanswer">'; echo html_entity_decode($output['output']['answer']); echo '</div>'; } //7. Solution to answering the question if ($options->correct_responses) { if (!empty($output['output']['solution'])) { echo '<div class="RQPsolution">'; echo html_entity_decode($output['output']['solution']); echo '</div>'; } } // Remove the render output created during grading (if any) unset($state->options->renderoutput); } /** * Prints the submit and validate buttons * @param object $question The question for which the buttons are to be printed * @param object $state The state the question is in (not used) * @param object $cmoptions * @param object $options An object describing the rendering options. * (not used. This function should only have been called * if the options were such that the buttons are required) */ function print_question_submit_buttons(&$question, &$state, $cmoptions, $options) { echo '<input type="submit" name="'; echo $question->name_prefix; echo 'validate" value="'; print_string('validate', 'quiz'); echo '" /> '; if ($cmoptions->optionflags & QUIZ_ADAPTIVE) { echo '<input type="submit" name="'; echo $question->name_prefix; echo 'mark" value="'; print_string('mark', 'quiz'); echo '" />'; } //QUIZ_ADAPTIVE flag ignored! } /** * Performs response processing and grading * * This function calls RQP_Render to perform response processing and grading * and updates the state accordingly. It also caches the rendering output in case * it is needed later. * @return boolean Indicates success or failure. * @param object $question The question to be graded. * @param object $state The state of the question to grade. The current * responses are in ->responses. The last graded state * is in ->last_graded (hence the most recently graded * responses are in ->last_graded->responses). The * question type specific information is also * included. The ->raw_grade and ->penalty fields * must be updated. The method is able to * close the question session (preventing any further * attempts at this question) by setting * $state->event to QUIZ_EVENTCLOSE. * @param object $cmoptions */ function grade_responses(&$question, &$state, $cmoptions) { global $CFG; require_once("RQPv1p0Client.php"); // Perform the grading and rendering // Make the code more readable $options =& $question->options; //Initialise render parameters $render['source'] = $options->source; $render['options'] = array ( 'admin' => '' , 'teacher' => 'current quiz name', 'user' => '', 'user-agent' => '', 'accept' => '', 'language' => ''); $render['persistentData']= ''; $render['directives'] = array(''); $render['mimetypes'] = array('text/html');; $render['namePrefix'] = $question->name_prefix; $render['itemBase'] = ''; $render['resourceBase'] = $CFG->dataroot . '/moddata/quiz/rqp/resource/'; $render['tempfileBase'] = $CFG->dataroot . '/moddata/quiz/rqp/temp/'; $render['inputData']=array(); // Add prefix to response variable names foreach ($state->responses as $key => $resp) { $render['inputData'][] = array( 'key' => $question->name_prefix . $key, 'val' => $resp); } if (QUIZ_EVENTGRADE == $state->event || QUIZ_EVENTCLOSE == $state->event) { $render['directives'][] = 'advanceState'; } $rqpserver = new RQPv1p0(); //Create new instance of RQP web service consumer if (!$server = quiz_rqp_get_server($options->type)) { notify('No servers of specified type'); exit; } $rqpserver->Url = $server->url; $output = $rqpserver->RQP_Render($render['source'],$render['options'], $render['persistentData'],$render['inputData'], $render['directives'],$render['mimetypes'], $render['namePrefix'],$render['itemBase'], $render['resourceBase'],$render['tempfileBase']); //Check SOAP fault has not been returned if (is_instance_of($output,'nbSOAPFault')) { notify('SOAP fault returned. Please try again!'); unset($output); return false; } $state->options->persistent_data = $output['persistentData']; // Save the rendering results for later - important $state->options->renderoutput = $output; if (isset($output['outcomeVars']['rawScore'])) { $state->raw_grade = (float) $output['outcomeVars']['rawScore']; if (isset($output['outcomeVars']['penalty'])) { $state->penalty = (float) $output['outcomeVars']['penalty'] * $question->maxgrade; } else { $state->penalty = 0; } } /*else if (isset($output['outcomeVars']['grade'])) { // This won't work quite as we would like but it is the best we can // do given that the server won't tell us the information we need $state->raw_grade = (float) $output['outcomeVars']['grade'];*/ else if (isset($output['outcomeVars']['score'])) { $state->raw_grade = (float) $output['outcomeVars']['score']; $state->penalty = 0; } else { $state->raw_grade = 0; $state->penalty = 0; } /*$state->raw_grade = ($state->raw_grade * ((float) $question->maxgrade)) / ((float) $question->options->maxscore); // <TODO>*/ $state->raw_grade = ($state->raw_grade * ((float) $question->maxgrade)) / ((float) $question->defaultgrade); return true; } /** * Includes configuration settings for the question type on the quiz admin * page * * Returns an array of objects describing the options for the question type * to be included on the quiz module admin page. * This is currently only a link to the server setup page types.php * @return array Array of objects describing the configuration options to * be included on the quiz module admin page. */ function get_config_options() { global $CFG; $link->name = 'managetypes'; $link->link = 'types.php'; return array($link); } } ////////////////////////////////////////////////////////////////////////// //// INITIATION - Without this line the question type is not in use... /// ////////////////////////////////////////////////////////////////////////// $QUIZ_QTYPES[RQP]= new quiz_rqp_qtype(); ?> --- NEW FILE: RQPv1p0Client.php --- <?php include("nb_easyxml_lite.php"); include("nb_soapfuncs.php"); /********* Data structures used by this web service ********* * * type ServerInformationDType{ * ['type']=>string * ['version']=>string * ['rqpVersion']=>string * ['details']=>anyURI * ['studentDocs']=>anyURI * ['teacherDocs']=>anyURI * ['adminDocs']=>anyURI * ['serverProperties']=>array(key=>val, ...) * ['mimetypes']=>array(string, ...) * ['resources']=>array(anyURI, ...) * } * * type ItemInformationDType{ * ['name']=>string * ['itemProperties']=>array(key=>val, ...) * ['error']=>string * ['warning']=>string * } * * type RenderDType{ * ['persistentData']=>string * ['outcomeVars']=>array(key=>val, ...) * ['onSubmit']=>string * ['head']=>array(string, ...) * ['output']=>array(key=>val, ...) * ['mimetype']=>string * ['files']=>array(anyURI, ...) * } * * * type input{ * ['key']=>string * ['val']=>string * } * * type CloneDType{ * ['clones']=>array(string, ...) * } * */ class RQPv1p0 { var $Url; /** * web service consumer method RQP_ServerInformation * * @return ServerInformationDType */ function RQP_ServerInformation() // returns a ServerInformationDType { $soapXML = "<soap:Body>\n"; $soapXML .= "<RQP_ServerInformation xmlns=\"http://mantis.york.ac.uk/rqp_v1p0\">\n"; $soapXML .= "</RQP_ServerInformation>\n"; $soapXML .= "</soap:Body>\n"; $soapXML = nbSOAP_Envelope($soapXML); // Send the request (syncronous http 1.0 only at the moment) $soapOut = nbSOAP_request11($this->Url, "http://mantis.york.ac.uk/rqp_v1p0/RQP_ServerInformation", $soapXML); // is it an error? $soapStart = strpos($soapOut, "<?xml"); if($soapStart==-1) return false; $xml = nb_easyxmldoc(substr($soapOut,$soapStart)); $body = $xml->findElement("Body", 0, "http://schemas.xmlsoap.org/soap/envelope/"); $cp = $xml->FindChildElement($body); if($cp==false) // empty soap:Body return null; $tn = $xml->getName($cp); if($tn=="Fault") // should really report it... return desoap_nbSOAPFault($xml, $cp); if($tn != "RQP_ServerInformationResponse") return null; $cp = $xml->FindChildElement($cp); if($cp==false) // empty value return null; $result = $this->desoap_ServerInformationDType($xml, $cp, "RQP_ServerInformation"); return $result; } /** * web service consumer method RQP_ItemInformation * * @param source : string * @return ItemInformationDType */ function RQP_ItemInformation($source) // returns a ItemInformationDType { $soapXML = "<soap:Body>\n"; $soapXML .= "<RQP_ItemInformation xmlns=\"http://mantis.york.ac.uk/rqp_v1p0\">\n"; // Parameter string source $soapXML .= $this->soapify_string($source, "source"); $soapXML .= "</RQP_ItemInformation>\n"; $soapXML .= "</soap:Body>\n"; $soapXML = nbSOAP_Envelope($soapXML); // Send the request (syncronous http 1.0 only at the moment) $soapOut = nbSOAP_request11($this->Url, "http://mantis.york.ac.uk/rqp_v1p0/RQP_ItemInformation", $soapXML); // is it an error? $soapStart = strpos($soapOut, "<?xml"); if($soapStart==-1) return false; $xml = nb_easyxmldoc(substr($soapOut,$soapStart)); $body = $xml->findElement("Body", 0, "http://schemas.xmlsoap.org/soap/envelope/"); $cp = $xml->FindChildElement($body); if($cp==false) // empty soap:Body return null; $tn = $xml->getName($cp); if($tn=="Fault") // should really report it... return desoap_nbSOAPFault($xml, $cp); if($tn != "RQP_ItemInformationResponse") return null; $cp = $xml->FindChildElement($cp); if($cp==false) // empty value return null; $result = $this->desoap_ItemInformationDType($xml, $cp, "RQP_ItemInformation"); return $result; } /** * web service consumer method RQP_Render * * @param source : string * @param options : kvPair[] * @param persistentData : string * @param inputData : input[] * @param directives : string[] * @param mimetypes : string[] * @param namePrefix : QName * @param itemBase : anyURI * @param resourceBase : anyURI * @param tempfileBase : anyURI * @return RenderDType */ function RQP_Render($source, $options, $persistentData, $inputData, $directives, $mimetypes, $namePrefix, $itemBase, $resourceBase, $tempfileBase) // returns a RenderDType { $soapXML = "<soap:Body>\n"; $soapXML .= "<RQP_Render xmlns=\"http://mantis.york.ac.uk/rqp_v1p0\">\n"; // Parameter string source $soapXML .= $this->soapify_string($source, "source"); // Parameter kvPair[] options $soapXML .= $this->soapify_kvPairArray($options, "options"); // Parameter string persistentData $soapXML .= $this->soapify_string($persistentData, "persistentData"); // Parameter input[] inputData $soapXML .= $this->soapify_inputArray($inputData, "inputData"); // Parameter string[] directives $soapXML .= $this->soapify_stringArray($directives, "directives"); // Parameter string[] mimetypes $soapXML .= $this->soapify_stringArray($mimetypes, "mimetypes"); // Parameter QName namePrefix $soapXML .= $this->soapify_QName($namePrefix, "namePrefix"); // Parameter anyURI itemBase $soapXML .= $this->soapify_anyURI($itemBase, "itemBase"); // Parameter anyURI resourceBase $soapXML .= $this->soapify_anyURI($resourceBase, "resourceBase"); // Parameter anyURI tempfileBase $soapXML .= $this->soapify_anyURI($tempfileBase, "tempfileBase"); $soapXML .= "</RQP_Render>\n"; $soapXML .= "</soap:Body>\n"; $soapXML = nbSOAP_Envelope($soapXML); // Send the request (syncronous http 1.0 only at the moment) $soapOut = nbSOAP_request11($this->Url, "http://mantis.york.ac.uk/rqp_v1p0/RQP_Render", $soapXML); // is it an error? $soapStart = strpos($soapOut, "<?xml"); if($soapStart==-1) return false; $xml = nb_easyxmldoc(substr($soapOut,$soapStart)); $body = $xml->findElement("Body", 0, "http://schemas.xmlsoap.org/soap/envelope/"); $cp = $xml->FindChildElement($body); if($cp==false) // empty soap:Body return null; $tn = $xml->getName($cp); if($tn=="Fault") // should really report it... return desoap_nbSOAPFault($xml, $cp); if($tn != "RQP_RenderResponse") return null; $cp = $xml->FindChildElement($cp); if($cp==false) // empty value return null; $result = $this->desoap_RenderDType($xml, $cp, "RQP_Render"); return $result; } /** * web service consumer method RQP_Clone * * @param source : string * @param number : int * @return CloneDType */ function RQP_Clone($source, $number) // returns a CloneDType { $soapXML = "<soap:Body>\n"; $soapXML .= "<RQP_Clone xmlns=\"http://mantis.york.ac.uk/rqp_v1p0\">\n"; // Parameter string source $soapXML .= $this->soapify_string($source, "source"); // Parameter int number $soapXML .= $this->soapify_int($number, "number"); $soapXML .= "</RQP_Clone>\n"; $soapXML .= "</soap:Body>\n"; $soapXML = nbSOAP_Envelope($soapXML); // Send the request (syncronous http 1.0 only at the moment) $soapOut = nbSOAP_request11($this->Url, "http://mantis.york.ac.uk/rqp_v1p0/RQP_Clone", $soapXML); // is it an error? $soapStart = strpos($soapOut, "<?xml"); if($soapStart==-1) return false; $xml = nb_easyxmldoc(substr($soapOut,$soapStart)); $body = $xml->findElement("Body", 0, "http://schemas.xmlsoap.org/soap/envelope/"); $cp = $xml->FindChildElement($body); if($cp==false) // empty soap:Body return null; $tn = $xml->getName($cp); if($tn=="Fault") // should really report it... return desoap_nbSOAPFault($xml, $cp); if($tn != "RQP_CloneResponse") return null; $cp = $xml->FindChildElement($cp); if($cp==false) // empty value return null; $result = $this->desoap_CloneDType($xml, $cp, "RQP_Clone"); return $result; } function soapify_ServerInformationDType($input, $name) { $ret = "<$name>"; $ret .= "\n"; if(array_key_exists('type', $input)) $ret .= $this->soapify_string($input['type'], "type"); if(array_key_exists('version', $input)) $ret .= $this->soapify_string($input['version'], "version"); if(array_key_exists('rqpVersion', $input)) $ret .= $this->soapify_string($input['rqpVersion'], "rqpVersion"); if(array_key_exists('details', $input)) $ret .= $this->soapify_anyURI($input['details'], "details"); if(array_key_exists('studentDocs', $input)) $ret .= $this->soapify_anyURI($input['studentDocs'], "studentDocs"); if(array_key_exists('teacherDocs', $input)) $ret .= $this->soapify_anyURI($input['teacherDocs'], "teacherDocs"); if(array_key_exists('adminDocs', $input)) $ret .= $this->soapify_anyURI($input['adminDocs'], "adminDocs"); if(array_key_exists('serverProperties', $input)) $ret .= $this->soapify_kvPairArray($input['serverProperties'], "serverProperties"); if(array_key_exists('mimetypes', $input)) $ret .= $this->soapify_stringArray($input['mimetypes'], "mimetypes"); if(array_key_exists('resources', $input)) $ret .= $this->soapify_anyURIArray($input['resources'], "resources"); $ret .= "\n"; $ret .= "</$name>\n"; return $ret; } function desoap_ServerInformationDType($xml, $idx, $name) { $ret = array(); $cidx = $xml->FindChildElement($idx); while($cidx != false) { $nm = $xml->getName($cidx); switch($nm) { // string type case "type": $ret['type'] = $xml->getContent($cidx); break; // string version case "version": $ret['version'] = $xml->getContent($cidx); break; // string rqpVersion case "rqpVersion": $ret['rqpVersion'] = $xml->getContent($cidx); break; // anyURI details case "details": $ret['details'] = $xml->getContent($cidx); break; // anyURI studentDocs case "studentDocs": $ret['studentDocs'] = $xml->getContent($cidx); break; // anyURI teacherDocs case "teacherDocs": $ret['teacherDocs'] = $xml->getContent($cidx); break; // anyURI adminDocs case "adminDocs": $ret['adminDocs'] = $xml->getContent($cidx); break; // kvPair[] serverProperties case "serverProperties": $ret['serverProperties'] = $this->desoap_kvPairArray($xml, $cidx, "serverProperties"); break; // string[] mimetypes case "mimetypes": $ret['mimetypes'] = $this->desoap_stringArray($xml, $cidx, "mimetypes"); break; // anyURI[] resources case "resources": $ret['resources'] = $this->desoap_anyURIArray($xml, $cidx, "resources"); break; } $cidx = $xml->FindNextPeer($cidx); } return $ret; } function soapify_ItemInformationDType($input, $name) { $ret = "<$name>"; $ret .= "\n"; if(array_key_exists('name', $input)) $ret .= $this->soapify_string($input['name'], "name"); if(array_key_exists('itemProperties', $input)) $ret .= $this->soapify_kvPairArray($input['itemProperties'], "itemProperties"); if(array_key_exists('error', $input)) $ret .= $this->soapify_string($input['error'], "error"); if(array_key_exists('warning', $input)) $ret .= $this->soapify_string($input['warning'], "warning"); $ret .= "\n"; $ret .= "</$name>\n"; return $ret; } function desoap_ItemInformationDType($xml, $idx, $name) { $ret = array(); $cidx = $xml->FindChildElement($idx); while($cidx != false) { $nm = $xml->getName($cidx); switch($nm) { // string name case "name": $ret['name'] = $xml->getContent($cidx); break; // kvPair[] itemProperties case "itemProperties": $ret['itemProperties'] = $this->desoap_kvPairArray($xml, $cidx, "itemProperties"); break; // string error case "error": $ret['error'] = $xml->getContent($cidx); break; // string warning case "warning": $ret['warning'] = $xml->getContent($cidx); break; } $cidx = $xml->FindNextPeer($cidx); } return $ret; } function soapify_string($input, $name) { $ret = "<$name>"; $ret .= encodeXMLSafeString($input); $ret .= "</$name>\n"; return $ret; } function desoap_string($xml, $idx, $name) { // ideally I should check element name is $name first $ret = decodeXMLSafeString($xml->getContent($idx)); return $ret; } function soapify_RenderDType($input, $name) { $ret = "<$name>"; $ret .= "\n"; if(array_key_exists('persistentData', $input)) $ret .= $this->soapify_string($input['persistentData'], "persistentData"); if(array_key_exists('outcomeVars', $input)) $ret .= $this->soapify_kvPairArray($input['outcomeVars'], "outcomeVars"); if(array_key_exists('onSubmit', $input)) $ret .= $this->soapify_string($input['onSubmit'], "onSubmit"); if(array_key_exists('head', $input)) $ret .= $this->soapify_stringArray($input['head'], "head"); if(array_key_exists('output', $input)) $ret .= $this->soapify_kvPairArray($input['output'], "output"); if(array_key_exists('mimetype', $input)) $ret .= $this->soapify_string($input['mimetype'], "mimetype"); if(array_key_exists('files', $input)) $ret .= $this->soapify_anyURIArray($input['files'], "files"); $ret .= "\n"; $ret .= "</$name>\n"; return $ret; } function desoap_RenderDType($xml, $idx, $name) { $ret = array(); $cidx = $xml->FindChildElement($idx); while($cidx != false) { $nm = $xml->getName($cidx); switch($nm) { // string persistentData case "persistentData": $ret['persistentData'] = $xml->getContent($cidx); break; // kvPair[] outcomeVars case "outcomeVars": $ret['outcomeVars'] = $this->desoap_kvPairArray($xml, $cidx, "outcomeVars"); break; // string onSubmit case "onSubmit": $ret['onSubmit'] = $xml->getContent($cidx); break; // string[] head case "head": $ret['head'] = $this->desoap_stringArray($xml, $cidx, "head"); break; // kvPair[] output case "output": $ret['output'] = $this->desoap_kvPairArray($xml, $cidx, "output"); break; // string mimetype case "mimetype": $ret['mimetype'] = $xml->getContent($cidx); break; // anyURI[] files case "files": $ret['files'] = $this->desoap_anyURIArray($xml, $cidx, "files"); break; } $cidx = $xml->FindNextPeer($cidx); } return $ret; } function soapify_kvPair($input, $name) { $ret = "<$name>"; $ret .= "\n"; if(array_key_exists('key', $input)) $ret .= $this->soapify_string($input['key'], "key"); if(array_key_exists('val', $input)) $ret .= $this->soapify_string($input['val'], "val"); $ret .= "\n"; $ret .= "</$name>\n"; return $ret; } function desoap_kvPair($xml, $idx, $name) { $ret = array(); $cidx = $xml->FindChildElement($idx); while($cidx != false) { $nm = $xml->getName($cidx); switch($nm) { // string key case "key": $ret['key'] = $xml->getContent($cidx); break; // string val case "val": $ret['val'] = $xml->getContent($cidx); break; } $cidx = $xml->FindNextPeer($cidx); } return $ret; } function soapify_input($input, $name) { $ret = "<$name>"; $ret .= "\n"; if(array_key_exists('key', $input)) $ret .= $this->soapify_string($input['key'], "key"); if(array_key_exists('val', $input)) $ret .= $this->soapify_string($input['val'], "val"); $ret .= "\n"; $ret .= "</$name>\n"; return $ret; } function desoap_input($xml, $idx, $name) { $ret = array(); $cidx = $xml->FindChildElement($idx); while($cidx != false) { $nm = $xml->getName($cidx); switch($nm) { // string key case "key": $ret['key'] = $xml->getContent($cidx); break; // string val case "val": $ret['val'] = $xml->getContent($cidx); break; } $cidx = $xml->FindNextPeer($cidx); } return $ret; } function soapify_QName($input, $name) { $ret = "<$name>"; $ret .= encodeXMLSafeString($input); $ret .= "</$name>\n"; return $ret; } function desoap_QName($xml, $idx, $name) { // ideally I should check element name is $name first $ret = decodeXMLSafeString($xml->getContent($idx)); return $ret; } function soapify_anyURI($input, $name) { $ret = "<$name>"; $ret .= encodeXMLSafeString($input); $ret .= "</$name>\n"; return $ret; } function desoap_anyURI($xml, $idx, $name) { // ideally I should check element name is $name first $ret = decodeXMLSafeString($xml->getContent($idx)); return $ret; } function soapify_CloneDType($input, $name) { $ret = "<$name>"; $ret .= "\n"; if(array_key_exists('clones', $input)) $ret .= $this->soapify_stringArray($input['clones'], "clones"); $ret .= "\n"; $ret .= "</$name>\n"; return $ret; } function desoap_CloneDType($xml, $idx, $name) { $ret = array(); $cidx = $xml->FindChildElement($idx); while($cidx != false) { $nm = $xml->getName($cidx); switch($nm) { // string[] clones case "clones": $ret['clones'] = $this->desoap_stringArray($xml, $cidx, "clones"); break; } $cidx = $xml->FindNextPeer($cidx); } return $ret; } function soapify_int($input, $name) { $ret = "<$name>"; $ret .= strval(intval($input)); $ret .= "</$name>\n"; return $ret; } function desoap_int($xml, $idx, $name) { // ideally I should check element name is $name first $ret = decodeXMLSafeString($xml->getContent($idx)); return $ret; } function soapify_kvPairArray($input, $name) { $ret = "<$name>\n"; if(is_array($input)) { reset($input); while(list($k, $v)=each($input)) { // manual changes to simplify PHP kvPair arrays start here $ret .= $this->soapify_kvPair(array("key"=>$k, "val"=>$v) , "kvPair") . "\n"; // manual changes to simplify PHP kvPair arrays end here } } else $ret .= $this->soapify_kvPair($input, "kvPair") . "\n"; $ret .= "</$name>\n"; return $ret; /*$ret = "<$name>\n"; if(is_array($input)) { reset($input); while(list($k, $v)=each($input)) { $ret .= $this->soapify_kvPair($v, "kvPair") . "\n"; } } else $ret .= $this->soapify_kvPair($input, "kvPair") . "\n"; $ret .= "</$name>\n"; return $ret;*/ ... [truncated message content] |