MDL-20534 lti: A4, add some todos towards unified Oauth
[moodle.git] / mod / lti / servicelib.php
CommitLineData
996b0fd9 1<?php
61eb12d4
CS
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * MRTODO: Brief description of this file
19 *
20 * @package mod
21 * @subpackage lti
22 * @copyright 2011 onwards MRTODO
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
996b0fd9
CS
25
26require_once($CFG->dirroot.'/mod/lti/OAuthBody.php');
27
fabd4fcf 28// TODO: Switch to core oauthlib once implemented - MDL-30149
795dff01
CS
29use moodle\mod\lti as lti;
30
996b0fd9
CS
31define('LTI_ITEM_TYPE', 'mod');
32define('LTI_ITEM_MODULE', 'lti');
33define('LTI_SOURCE', 'mod/lti');
34
ea04a9f9 35function lti_get_response_xml($codemajor, $description, $messageref, $messagetype) {
996b0fd9
CS
36 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><imsx_POXEnvelopeResponse />');
37 $xml->addAttribute('xmlns', 'http://www.imsglobal.org/lis/oms1p0/pox');
e27cb316 38
ea04a9f9 39 $headerinfo = $xml->addChild('imsx_POXHeader')->addChild('imsx_POXResponseHeaderInfo');
e27cb316 40
996b0fd9
CS
41 $headerinfo->addChild('imsx_version', 'V1.0');
42 $headerinfo->addChild('imsx_messageIdentifier', (string)mt_rand());
e27cb316 43
996b0fd9
CS
44 $statusinfo = $headerinfo->addChild('imsx_statusInfo');
45 $statusinfo->addchild('imsx_codeMajor', $codemajor);
46 $statusinfo->addChild('imsx_severity', 'status');
47 $statusinfo->addChild('imsx_description', $description);
48 $statusinfo->addChild('imsx_messageRefIdentifier', $messageref);
e27cb316 49
ea04a9f9 50 $xml->addChild('imsx_POXBody')->addChild($messagetype);
e27cb316 51
996b0fd9
CS
52 return $xml;
53}
54
ea04a9f9 55function lti_parse_message_id($xml) {
996b0fd9
CS
56 $node = $xml->imsx_POXHeader->imsx_POXRequestHeaderInfo->imsx_messageIdentifier;
57 $messageid = (string)$node;
e27cb316 58
996b0fd9
CS
59 return $messageid;
60}
61
ea04a9f9 62function lti_parse_grade_replace_message($xml) {
996b0fd9
CS
63 $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->sourcedGUID->sourcedId;
64 $resultjson = json_decode((string)$node);
e27cb316 65
996b0fd9
CS
66 $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->result->resultScore->textString;
67 $grade = floatval((string)$node);
e27cb316 68
996b0fd9
CS
69 $parsed = new stdClass();
70 $parsed->gradeval = $grade * 100;
e27cb316 71
996b0fd9
CS
72 $parsed->instanceid = $resultjson->data->instanceid;
73 $parsed->userid = $resultjson->data->userid;
f4f711d7 74 $parsed->launchid = $resultjson->data->launchid;
996b0fd9 75 $parsed->sourcedidhash = $resultjson->hash;
e27cb316 76
996b0fd9 77 $parsed->messageid = lti_parse_message_id($xml);
e27cb316 78
996b0fd9
CS
79 return $parsed;
80}
81
ea04a9f9 82function lti_parse_grade_read_message($xml) {
996b0fd9
CS
83 $node = $xml->imsx_POXBody->readResultRequest->resultRecord->sourcedGUID->sourcedId;
84 $resultjson = json_decode((string)$node);
e27cb316 85
996b0fd9
CS
86 $parsed = new stdClass();
87 $parsed->instanceid = $resultjson->data->instanceid;
88 $parsed->userid = $resultjson->data->userid;
f4f711d7 89 $parsed->launchid = $resultjson->data->launchid;
996b0fd9 90 $parsed->sourcedidhash = $resultjson->hash;
e27cb316 91
996b0fd9 92 $parsed->messageid = lti_parse_message_id($xml);
e27cb316 93
996b0fd9
CS
94 return $parsed;
95}
96
ea04a9f9 97function lti_parse_grade_delete_message($xml) {
996b0fd9
CS
98 $node = $xml->imsx_POXBody->deleteResultRequest->resultRecord->sourcedGUID->sourcedId;
99 $resultjson = json_decode((string)$node);
e27cb316 100
996b0fd9
CS
101 $parsed = new stdClass();
102 $parsed->instanceid = $resultjson->data->instanceid;
103 $parsed->userid = $resultjson->data->userid;
f4f711d7 104 $parsed->launchid = $resultjson->data->launchid;
996b0fd9 105 $parsed->sourcedidhash = $resultjson->hash;
e27cb316 106
996b0fd9 107 $parsed->messageid = lti_parse_message_id($xml);
e27cb316 108
996b0fd9
CS
109 return $parsed;
110}
111
ea04a9f9 112function lti_update_grade($ltiinstance, $userid, $launchid, $gradeval) {
f4f711d7 113 global $CFG, $DB;
996b0fd9 114 require_once($CFG->libdir . '/gradelib.php');
e27cb316 115
996b0fd9
CS
116 $params = array();
117 $params['itemname'] = $ltiinstance->name;
118
119 $grade = new stdClass();
120 $grade->userid = $userid;
121 $grade->rawgrade = $gradeval;
122
e27cb316 123 $status = grade_update(LTI_SOURCE, $ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, 0, $grade, $params);
996b0fd9 124
f4f711d7 125 $record = $DB->get_record('lti_submission', array('ltiid' => $ltiinstance->id, 'userid' => $userid, 'launchid' => $launchid), 'id');
ea04a9f9 126 if ($record) {
f4f711d7
CS
127 $id = $record->id;
128 } else {
129 $id = null;
130 }
e27cb316 131
ea04a9f9 132 if (!empty($id)) {
f4f711d7
CS
133 $DB->update_record('lti_submission', array(
134 'id' => $id,
135 'dateupdated' => time(),
136 'gradepercent' => $gradeval,
137 'state' => 2
138 ));
139 } else {
140 $DB->insert_record('lti_submission', array(
141 'ltiid' => $ltiinstance->id,
e27cb316
CS
142 'userid' => $userid,
143 'datesubmitted' => time(),
f4f711d7
CS
144 'dateupdated' => time(),
145 'gradepercent' => $gradeval,
146 'originalgrade' => $gradeval,
147 'launchid' => $launchid,
148 'state' => 1
149 ));
150 }
e27cb316 151
996b0fd9
CS
152 return $status == GRADE_UPDATE_OK;
153}
154
ea04a9f9 155function lti_read_grade($ltiinstance, $userid) {
996b0fd9
CS
156 global $CFG;
157 require_once($CFG->libdir . '/gradelib.php');
e27cb316 158
996b0fd9 159 $grades = grade_get_grades($ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, $userid);
e27cb316 160
60bd82f6 161 if (isset($grades) && isset($grades->items[0]) && is_array($grades->items[0]->grades)) {
996b0fd9
CS
162 foreach ($grades->items[0]->grades as $agrade) {
163 $grade = $agrade->grade;
164 break;
165 }
166 }
e27cb316 167
ea04a9f9 168 if (isset($grade)) {
996b0fd9
CS
169 return $grade;
170 }
171}
172
ea04a9f9 173function lti_delete_grade($ltiinstance, $userid) {
60bd82f6
CS
174 global $CFG;
175 require_once($CFG->libdir . '/gradelib.php');
e27cb316 176
996b0fd9
CS
177 $grade = new stdClass();
178 $grade->userid = $userid;
179 $grade->rawgrade = null;
180
181 $status = grade_update(LTI_SOURCE, $ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, 0, $grade, array('deleted'=>1));
e27cb316 182
996b0fd9
CS
183 return $status == GRADE_UPDATE_OK || $status == GRADE_UPDATE_ITEM_DELETED; //grade_update seems to return ok now, but could reasonably return deleted in the future
184}
185
ea04a9f9
EL
186function lti_verify_message($key, $sharedsecrets, $body, $headers = null) {
187 foreach ($sharedsecrets as $secret) {
020eea1b 188 $signaturefailed = false;
e27cb316 189
ea04a9f9 190 try {
fabd4fcf 191 // TODO: Switch to core oauthlib once implemented - MDL-30149
020eea1b 192 lti\handleOAuthBodyPOST($key, $secret, $body, $headers);
ea04a9f9 193 } catch (Exception $e) {
020eea1b 194 $signaturefailed = true;
996b0fd9 195 }
e27cb316 196
ea04a9f9 197 if (!$signaturefailed) {
020eea1b
CS
198 return $secret;//Return the secret used to sign the message)
199 }
996b0fd9 200 }
e27cb316 201
020eea1b 202 return false;
996b0fd9
CS
203}
204
ea04a9f9 205function lti_verify_sourcedid($ltiinstance, $parsed) {
f4f711d7 206 $sourceid = lti_build_sourcedid($parsed->instanceid, $parsed->userid, $parsed->launchid, $ltiinstance->servicesalt);
e27cb316 207
ea04a9f9 208 if ($sourceid->hash != $parsed->sourcedidhash) {
996b0fd9
CS
209 throw new Exception('SourcedId hash not valid');
210 }
211}