MDL-9716
[moodle-linuxchix.git] / question / type / rqp / rqp.php
blobd566122502aecdcd4becbdc515e7bbc7157235f0
1 <?php // $Id$
2 /**
3 * Library of functions binding RQP to SOAP
5 * @version $Id$
6 * @author Alex Smith and others members of the Serving Mathematics project
7 * {@link http://maths.york.ac.uk/serving_maths}
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package questionbank
10 * @subpackage questiontypes
13 // Load the SOAP library that gives a unified wrapping to either the native
14 // PHP5 SOAP extension if available or to nuSOAP otherwise.
15 require_once($CFG->dirroot . '/lib/soaplib.php');
17 /**
18 * Base RQP URI for RQP-defined identifiers
20 * RQP defines standard URIs for common values of the parameters. Currently
21 * there is no RQP domain so we define a base URI here so that it can be
22 * changed later.
24 define('RQP_URI_BASE', 'http://rqp.org/');
26 /**
27 * RQP parameter URIs
29 * RQP defines standard URIs for common values of the parameters. These are
30 * defined in several categories under different directories under the base
31 * URI.
33 define('RQP_URI_ERROR', RQP_URI_BASE . 'errors/');
34 define('RQP_URI_FORMAT', RQP_URI_BASE . 'formats/');
35 define('RQP_URI_OUTCOME', RQP_URI_BASE . 'outcomes/');
36 define('RQP_URI_COMPONENT', RQP_URI_BASE . 'components/');
39 /**
40 * Start a SOAP connection
42 * @param string $server The URL of the RQP server that we want to connect to
43 * @return mixed Returns a SoapClient object if connection is successful
44 * or false in the case of a soap fault.
46 function rqp_connect($server) {
47 $connection = soap_connect($server . '?wsdl');
48 if (is_soap_fault($connection)) {
49 return false;
51 return $connection;
54 /**
55 * Get server information using the RQP_ServerInformation operation
57 * @param SoapClient $connection The URL of the RQP server that we want to connect to
58 * @return object Object holding the return parameters or a SoapFault.
60 function rqp_server_info($connection) {
61 return soap_call($connection, 'RQP_ServerInformation', array());
64 /**
65 * Get item information using the RQP_ItemInformation operation
67 * @param SoapClient $connection The URL of the RQP server that we want to connect to
68 * @param string $source Item source
69 * @param anyURI $format Item format
70 * @return object Object holding the return parameters or a SoapFault.
72 function rqp_item_info($connection, $source, $format='') {
73 $itemInfo = soap_call($connection, 'RQP_ItemInformation',
74 array('source'=>$source, 'format'=>$format));
75 if (is_soap_fault($itemInfo)) {
76 return $itemInfo;
78 return $itemInfo;
81 /**
82 * Process an item template to produce template variables using the RQP_ProcessTemplate operation
84 * @param SoapClient $connection The URL of the RQP server that we want to connect to
85 * @param string $source Item source
86 * @param anyURI $format Item format
87 * @param array $options Options array
88 * @return object Object holding the return parameters or a SoapFault.
90 function rqp_process_template($connection, $source, $format='', $options=array()) {
91 $return = soap_call($connection, 'RQP_ProcessTemplate',
92 array('source'=>$source, 'format'=>$format, 'options'=>$options));
93 if (is_soap_fault($return)) {
94 return $return;
96 return $return;
99 /**
100 * Clone an item template using the RQP_ProcessTemplate operation
102 * @param SoapClient $connection The URL of the RQP server that we want to connect to
103 * @param string $source Item source
104 * @param anyURI $format Item format
105 * @return object Object holding the return parameters or a SoapFault.
107 function rqp_clone($connection, $source, $format='') {
109 $return = soap_call($connection, 'RQP_Clone', array('source'=>$source,
110 'format'=>$format));
111 if (is_soap_fault($return)) {
112 return $return;
114 return $return;
118 * Get runtime information about the item in the given state using the
119 * RQP_SessionInformation operation
121 * @param SoapClient $connection The URL of the RQP server that we want to connect to
122 * @param string $source Item source
123 * @param anyURI $format Item format
124 * @param array $options Options array
125 * @param string $persistentData String giving the state of the item session
126 * @return object Object holding the return parameters or a SoapFault.
128 function rqp_session_info($connection, $source, $format='', $options=array(), $persistentData='') {
129 // make an array of key-value pairs from the template variables array
130 array_walk($options, create_function('&$val, $key',
131 '$val = (object) array(\'identifier\'=>$key, \'values\'=>$val);'));
133 $return = soap_call($connection, 'RQP_SessionInformation',
134 array('source'=>$source, 'format'=>$format, 'options'=>$options,
135 'persistentData'=>$persistentData));
136 if (is_soap_fault($return)) {
137 return $return;
139 $responses = array();
140 if (!empty($return->correctResponses)) {
141 foreach ($return->correctResponses as $var) {
142 $responses[$var->name] = $var->value;
145 $return->correctResponses = $responses;
146 return $return;
150 * Process and render the item in the given state using the RQP_Render operation
152 * @param SoapClient $connection The URL of the RQP server that we want to connect to
153 * @param string $source Item source
154 * @param anyURI $format Item format
155 * @param array $options Options array
156 * @param string $persistentData String giving the state of the item session
157 * @param array $inputData Array of responses
158 * @param array $directives Array of directives
159 * @param array $mimetypes Array of mime types orederd by preference
160 * @param string $namePrefix
161 * @param anyURI $itemBase
162 * @param anyURI $resourceBase
163 * @param anyURI tempfileBase
164 * @return object Object holding the return parameters or a SoapFault.
166 function rqp_render($connection, $source, $format='', $options=array(), $persistentData='',
167 $inputData=array(), $directives=array(), $mimetypes=array(), $namePrefix='',
168 $itemBase='', $resourceBase='', $tempfileBase='') {
170 // make an array of name-value pairs from the responses array
171 array_walk($responses, create_function('&$val, $key',
172 '$val = (object) array(\'name\'=>$key, \'value\'=>$val);'));
174 $return = soap_call($connection, 'RQP_Render', array('source'=>$source,
175 'format'=>$format, 'index'=>$index, 'templateVars'=>array_values($templateVars),
176 'persistentData'=>$persistentData, 'responses'=>$responses,
177 'advanceState'=>$advanceState, 'embedPrefix'=>$embedPrefix,
178 'appletBase'=>$appletBase, 'mediaBase'=>$mediaBase,
179 'renderFormat'=>$renderFormat, 'modalFormat'=>$modalFormat));
180 if (is_soap_fault($return)) {
181 return $return;
183 $outcomeVars = array();
184 if (!empty($return->outcomeVars)) {
185 foreach ($return->outcomeVars as $var) {
186 $outcomeVars[$var->identifier] = $var->values;
189 $return->outcomeVars = $outcomeVars;
191 $templateVars = array();
192 if (!empty($return->templateVars)) {
193 foreach ($return->templateVars as $var) {
194 $templateVars[$var->identifier] = $var->values;
197 $return->templateVars = $templateVars;
199 $output = array();
200 if (!empty($return->output)) {
201 foreach ($return->output as $out) {
202 $id = $out->identifier;
203 unset($out->identifier);
204 $output[$id] = $out;
207 $return->output = $output;
209 return $return;
213 * Call to the RQP_Author operation
215 * @param SoapClient $connection The URL of the RQP server
216 * @param string $source Item source
217 * @param anyURI $format Item format
218 * @param string $persistentData String giving the state of the authoring session
219 * @param string $embedPrefix
220 * @param array $responses Teacher responses from the form elements
221 * @param anyURI $renderFormat
222 * @return object Object holding the return parameters or a SoapFault.
224 function rqp_author($connection, $source, $format='', $persistentData='',
225 $embedPrefix='', $responses=array(), $renderFormat='') {
227 // make an array of name-value pairs from the responses array
228 array_walk($responses, create_function('&$val, $key',
229 '$val = (object) array(\'name\'=>$key, \'value\'=>$val);'));
231 return soap_call($connection, 'RQP_Author', array('source'=>$source,
232 'format'=>$format, 'persistentData'=>$persistentData, 'responses'=>$responses,
233 'embedPrefix'=>$embedPrefix, 'renderFormat'=>$renderFormat));