2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Utility code for LTI service handling.
21 * @copyright Copyright (c) 2011 Moodlerooms Inc. (http://www.moodlerooms.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @author Chris Scribner
26 defined('MOODLE_INTERNAL') ||
die;
28 require_once($CFG->dirroot
.'/mod/lti/OAuthBody.php');
29 require_once($CFG->dirroot
.'/mod/lti/locallib.php');
31 // TODO: Switch to core oauthlib once implemented - MDL-30149.
32 use moodle\mod\lti
as lti
;
34 define('LTI_ITEM_TYPE', 'mod');
35 define('LTI_ITEM_MODULE', 'lti');
36 define('LTI_SOURCE', 'mod/lti');
38 function lti_get_response_xml($codemajor, $description, $messageref, $messagetype) {
39 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><imsx_POXEnvelopeResponse />');
40 $xml->addAttribute('xmlns', 'http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0');
42 $headerinfo = $xml->addChild('imsx_POXHeader')->addChild('imsx_POXResponseHeaderInfo');
44 $headerinfo->addChild('imsx_version', 'V1.0');
45 $headerinfo->addChild('imsx_messageIdentifier', (string)mt_rand());
47 $statusinfo = $headerinfo->addChild('imsx_statusInfo');
48 $statusinfo->addchild('imsx_codeMajor', $codemajor);
49 $statusinfo->addChild('imsx_severity', 'status');
50 $statusinfo->addChild('imsx_description', $description);
51 $statusinfo->addChild('imsx_messageRefIdentifier', $messageref);
52 $incomingtype = str_replace('Response', 'Request', $messagetype);
53 $statusinfo->addChild('imsx_operationRefIdentifier', $incomingtype);
55 $xml->addChild('imsx_POXBody')->addChild($messagetype);
60 function lti_parse_message_id($xml) {
61 if (empty($xml->imsx_POXHeader
)) {
65 $node = $xml->imsx_POXHeader
->imsx_POXRequestHeaderInfo
->imsx_messageIdentifier
;
66 $messageid = (string)$node;
71 function lti_parse_grade_replace_message($xml) {
72 $node = $xml->imsx_POXBody
->replaceResultRequest
->resultRecord
->sourcedGUID
->sourcedId
;
73 $resultjson = json_decode((string)$node);
74 if ( is_null($resultjson) ) {
75 throw new Exception('Invalid sourcedId in result message');
77 $node = $xml->imsx_POXBody
->replaceResultRequest
->resultRecord
->result
->resultScore
->textString
;
79 $score = (string) $node;
80 if ( ! is_numeric($score) ) {
81 throw new Exception('Score must be numeric');
83 $grade = floatval($score);
84 if ( $grade < 0.0 ||
$grade > 1.0 ) {
85 throw new Exception('Score not between 0.0 and 1.0');
88 $parsed = new stdClass();
89 $parsed->gradeval
= $grade;
91 $parsed->instanceid
= $resultjson->data
->instanceid
;
92 $parsed->userid
= $resultjson->data
->userid
;
93 $parsed->launchid
= $resultjson->data
->launchid
;
94 $parsed->typeid
= $resultjson->data
->typeid
;
95 $parsed->sourcedidhash
= $resultjson->hash
;
97 $parsed->messageid
= lti_parse_message_id($xml);
102 function lti_parse_grade_read_message($xml) {
103 $node = $xml->imsx_POXBody
->readResultRequest
->resultRecord
->sourcedGUID
->sourcedId
;
104 $resultjson = json_decode((string)$node);
105 if ( is_null($resultjson) ) {
106 throw new Exception('Invalid sourcedId in result message');
109 $parsed = new stdClass();
110 $parsed->instanceid
= $resultjson->data
->instanceid
;
111 $parsed->userid
= $resultjson->data
->userid
;
112 $parsed->launchid
= $resultjson->data
->launchid
;
113 $parsed->typeid
= $resultjson->data
->typeid
;
114 $parsed->sourcedidhash
= $resultjson->hash
;
116 $parsed->messageid
= lti_parse_message_id($xml);
121 function lti_parse_grade_delete_message($xml) {
122 $node = $xml->imsx_POXBody
->deleteResultRequest
->resultRecord
->sourcedGUID
->sourcedId
;
123 $resultjson = json_decode((string)$node);
124 if ( is_null($resultjson) ) {
125 throw new Exception('Invalid sourcedId in result message');
128 $parsed = new stdClass();
129 $parsed->instanceid
= $resultjson->data
->instanceid
;
130 $parsed->userid
= $resultjson->data
->userid
;
131 $parsed->launchid
= $resultjson->data
->launchid
;
132 $parsed->typeid
= $resultjson->data
->typeid
;
133 $parsed->sourcedidhash
= $resultjson->hash
;
135 $parsed->messageid
= lti_parse_message_id($xml);
140 function lti_accepts_grades($ltiinstance) {
143 $acceptsgrades = true;
144 $ltitype = $DB->get_record('lti_types', array('id' => $ltiinstance->typeid
));
146 if (empty($ltitype->toolproxyid
)) {
147 $typeconfig = lti_get_config($ltiinstance);
149 $typeacceptgrades = isset($typeconfig['acceptgrades']) ?
$typeconfig['acceptgrades'] : LTI_SETTING_DELEGATE
;
151 if (!($typeacceptgrades == LTI_SETTING_ALWAYS ||
152 ($typeacceptgrades == LTI_SETTING_DELEGATE
&& $ltiinstance->instructorchoiceacceptgrades
== LTI_SETTING_ALWAYS
))) {
153 $acceptsgrades = false;
156 $enabledcapabilities = explode("\n", $ltitype->enabledcapability
);
157 $acceptsgrades = in_array('Result.autocreate', $enabledcapabilities) ||
in_array('BasicOutcome.url', $enabledcapabilities);
160 return $acceptsgrades;
164 * Set the passed user ID to the session user.
168 function lti_set_session_user($userid) {
171 if ($user = $DB->get_record('user', array('id' => $userid))) {
172 \core\session\manager
::set_user($user);
176 function lti_update_grade($ltiinstance, $userid, $launchid, $gradeval) {
178 require_once($CFG->libdir
. '/gradelib.php');
181 $params['itemname'] = $ltiinstance->name
;
183 $gradeval = $gradeval * floatval($ltiinstance->grade
);
185 $grade = new stdClass();
186 $grade->userid
= $userid;
187 $grade->rawgrade
= $gradeval;
189 $status = grade_update(LTI_SOURCE
, $ltiinstance->course
, LTI_ITEM_TYPE
, LTI_ITEM_MODULE
, $ltiinstance->id
, 0, $grade, $params);
191 $record = $DB->get_record('lti_submission', array('ltiid' => $ltiinstance->id
, 'userid' => $userid,
192 'launchid' => $launchid), 'id');
200 $DB->update_record('lti_submission', array(
202 'dateupdated' => time(),
203 'gradepercent' => $gradeval,
207 $DB->insert_record('lti_submission', array(
208 'ltiid' => $ltiinstance->id
,
210 'datesubmitted' => time(),
211 'dateupdated' => time(),
212 'gradepercent' => $gradeval,
213 'originalgrade' => $gradeval,
214 'launchid' => $launchid,
219 return $status == GRADE_UPDATE_OK
;
222 function lti_read_grade($ltiinstance, $userid) {
224 require_once($CFG->libdir
. '/gradelib.php');
226 $grades = grade_get_grades($ltiinstance->course
, LTI_ITEM_TYPE
, LTI_ITEM_MODULE
, $ltiinstance->id
, $userid);
228 $ltigrade = floatval($ltiinstance->grade
);
230 if (!empty($ltigrade) && isset($grades) && isset($grades->items
[0]) && is_array($grades->items
[0]->grades
)) {
231 foreach ($grades->items
[0]->grades
as $agrade) {
232 $grade = $agrade->grade
;
234 return $grade / $ltigrade;
240 function lti_delete_grade($ltiinstance, $userid) {
242 require_once($CFG->libdir
. '/gradelib.php');
244 $grade = new stdClass();
245 $grade->userid
= $userid;
246 $grade->rawgrade
= null;
248 $status = grade_update(LTI_SOURCE
, $ltiinstance->course
, LTI_ITEM_TYPE
, LTI_ITEM_MODULE
, $ltiinstance->id
, 0, $grade);
250 return $status == GRADE_UPDATE_OK
;
253 function lti_verify_message($key, $sharedsecrets, $body, $headers = null) {
254 foreach ($sharedsecrets as $secret) {
255 $signaturefailed = false;
258 // TODO: Switch to core oauthlib once implemented - MDL-30149.
259 lti\
handle_oauth_body_post($key, $secret, $body, $headers);
260 } catch (Exception
$e) {
261 debugging('LTI message verification failed: '.$e->getMessage());
262 $signaturefailed = true;
265 if (!$signaturefailed) {
266 return $secret; // Return the secret used to sign the message).
274 * Validate source ID from external request
276 * @param object $ltiinstance
277 * @param object $parsed
280 function lti_verify_sourcedid($ltiinstance, $parsed) {
281 $sourceid = lti_build_sourcedid($parsed->instanceid
, $parsed->userid
,
282 $ltiinstance->servicesalt
, $parsed->typeid
, $parsed->launchid
);
284 if ($sourceid->hash
!= $parsed->sourcedidhash
) {
285 throw new Exception('SourcedId hash not valid');
290 * Extend the LTI services through the ltisource plugins
292 * @param stdClass $data LTI request data
294 * @throws coding_exception
296 function lti_extend_lti_services($data) {
297 $plugins = get_plugin_list_with_function('ltisource', $data->messagetype
);
298 if (!empty($plugins)) {
299 // There can only be one.
300 if (count($plugins) > 1) {
301 throw new coding_exception('More than one ltisource plugin handler found');
303 $data->xml
= new SimpleXMLElement($data->body
);
304 $callback = current($plugins);
305 call_user_func($callback, $data);