|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 require_once(dirname(__FILE__) . "/../../config.php"); 00028 require_once($CFG->dirroot.'/mod/lti/locallib.php'); 00029 require_once($CFG->dirroot.'/mod/lti/servicelib.php'); 00030 00031 // TODO: Switch to core oauthlib once implemented - MDL-30149 00032 use moodle\mod\lti as lti; 00033 00034 $rawbody = file_get_contents("php://input"); 00035 00036 foreach (getallheaders() as $name => $value) { 00037 if ($name === 'Authorization') { 00038 // TODO: Switch to core oauthlib once implemented - MDL-30149 00039 $oauthparams = lti\OAuthUtil::split_header($value); 00040 00041 $consumerkey = $oauthparams['oauth_consumer_key']; 00042 break; 00043 } 00044 } 00045 00046 if (empty($consumerkey)) { 00047 throw new Exception('Consumer key is missing.'); 00048 } 00049 00050 $sharedsecret = lti_verify_message($consumerkey, lti_get_shared_secrets_by_key($consumerkey), $rawbody); 00051 00052 if ($sharedsecret === false) { 00053 throw new Exception('Message signature not valid'); 00054 } 00055 00056 $xml = new SimpleXMLElement($rawbody); 00057 00058 $body = $xml->imsx_POXBody; 00059 foreach ($body->children() as $child) { 00060 $messagetype = $child->getName(); 00061 } 00062 00063 switch ($messagetype) { 00064 case 'replaceResultRequest': 00065 try { 00066 $parsed = lti_parse_grade_replace_message($xml); 00067 } catch (Exception $e) { 00068 $responsexml = lti_get_response_xml( 00069 'failure', 00070 $e->getMessage(), 00071 uniqid(), 00072 'replaceResultResponse'); 00073 00074 echo $responsexml->asXML(); 00075 break; 00076 } 00077 00078 $ltiinstance = $DB->get_record('lti', array('id' => $parsed->instanceid)); 00079 00080 lti_verify_sourcedid($ltiinstance, $parsed); 00081 00082 $gradestatus = lti_update_grade($ltiinstance, $parsed->userid, $parsed->launchid, $parsed->gradeval); 00083 00084 $responsexml = lti_get_response_xml( 00085 $gradestatus ? 'success' : 'failure', 00086 'Grade replace response', 00087 $parsed->messageid, 00088 'replaceResultResponse' 00089 ); 00090 00091 echo $responsexml->asXML(); 00092 00093 break; 00094 00095 case 'readResultRequest': 00096 $parsed = lti_parse_grade_read_message($xml); 00097 00098 $ltiinstance = $DB->get_record('lti', array('id' => $parsed->instanceid)); 00099 00100 //Getting the grade requires the context is set 00101 $context = get_context_instance(CONTEXT_COURSE, $ltiinstance->course); 00102 $PAGE->set_context($context); 00103 00104 lti_verify_sourcedid($ltiinstance, $parsed); 00105 00106 $grade = lti_read_grade($ltiinstance, $parsed->userid); 00107 00108 $responsexml = lti_get_response_xml( 00109 isset($grade) ? 'success' : 'failure', 00110 'Result read', 00111 $parsed->messageid, 00112 'readResultResponse' 00113 ); 00114 00115 $node = $responsexml->imsx_POXBody->readResultResponse; 00116 $node = $node->addChild('result')->addChild('resultScore'); 00117 $node->addChild('language', 'en'); 00118 $node->addChild('textString', isset($grade) ? $grade : ''); 00119 00120 echo $responsexml->asXML(); 00121 00122 break; 00123 00124 case 'deleteResultRequest': 00125 $parsed = lti_parse_grade_delete_message($xml); 00126 00127 $ltiinstance = $DB->get_record('lti', array('id' => $parsed->instanceid)); 00128 00129 lti_verify_sourcedid($ltiinstance, $parsed); 00130 00131 $gradestatus = lti_delete_grade($ltiinstance, $parsed->userid); 00132 00133 $responsexml = lti_get_response_xml( 00134 $gradestatus ? 'success' : 'failure', 00135 'Grade delete request', 00136 $parsed->messageid, 00137 'deleteResultResponse' 00138 ); 00139 00140 echo $responsexml->asXML(); 00141 00142 break; 00143 00144 default: 00145 //Fire an event if we get a web service request which we don't support directly. 00146 //This will allow others to extend the LTI services, which I expect to be a common 00147 //use case, at least until the spec matures. 00148 $data = new stdClass(); 00149 $data->body = $rawbody; 00150 $data->xml = $xml; 00151 $data->messagetype = $messagetype; 00152 $data->consumerkey = $consumerkey; 00153 $data->sharedsecret = $sharedsecret; 00154 00155 //If an event handler handles the web service, it should set this global to true 00156 //So this code knows whether to send an "operation not supported" or not. 00157 global $lti_web_service_handled; 00158 $lti_web_service_handled = false; 00159 00160 events_trigger('lti_unknown_service_api_call', $data); 00161 00162 if (!$lti_web_service_handled) { 00163 $responsexml = lti_get_response_xml( 00164 'unsupported', 00165 'unsupported', 00166 lti_parse_message_id($xml), 00167 $messagetype 00168 ); 00169 00170 echo $responsexml->asXML(); 00171 } 00172 00173 break; 00174 } 00175 00176 00177 //echo print_r(apache_request_headers(), true); 00178 00179 //echo '<br />'; 00180 00181 //echo file_get_contents("php://input");