e23188c295c9c2d5b2ea8799342d74785006a71a
[moodle.git] / mod / lti / servicelib.php
1 <?php
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/>.
17 /**
18  * Utility code for LTI service handling.
19  *
20  * @package    mod
21  * @subpackage lti
22  * @copyright  Copyright (c) 2011 Moodlerooms Inc. (http://www.moodlerooms.com)
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @author     Chris Scribner
25  */
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->dirroot.'/mod/lti/OAuthBody.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/lis/oms1p0/pox');
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);
53     $xml->addChild('imsx_POXBody')->addChild($messagetype);
55     return $xml;
56 }
58 function lti_parse_message_id($xml) {
59     $node = $xml->imsx_POXHeader->imsx_POXRequestHeaderInfo->imsx_messageIdentifier;
60     $messageid = (string)$node;
62     return $messageid;
63 }
65 function lti_parse_grade_replace_message($xml) {
66     $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->sourcedGUID->sourcedId;
67     $resultjson = json_decode((string)$node);
69     $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->result->resultScore->textString;
71     $score = (string) $node;
72     if ( ! is_numeric($score) ) {
73         throw new Exception('Score must be numeric');
74     }
75     $grade = floatval($score);
76     if ( $grade < 0.0 || $grade > 1.0 ) {
77         throw new Exception('Score not between 0.0 and 1.0');
78     }
80     $parsed = new stdClass();
81     $parsed->gradeval = $grade * 100;
83     $parsed->instanceid = $resultjson->data->instanceid;
84     $parsed->userid = $resultjson->data->userid;
85     $parsed->launchid = $resultjson->data->launchid;
86     $parsed->sourcedidhash = $resultjson->hash;
88     $parsed->messageid = lti_parse_message_id($xml);
90     return $parsed;
91 }
93 function lti_parse_grade_read_message($xml) {
94     $node = $xml->imsx_POXBody->readResultRequest->resultRecord->sourcedGUID->sourcedId;
95     $resultjson = json_decode((string)$node);
97     $parsed = new stdClass();
98     $parsed->instanceid = $resultjson->data->instanceid;
99     $parsed->userid = $resultjson->data->userid;
100     $parsed->launchid = $resultjson->data->launchid;
101     $parsed->sourcedidhash = $resultjson->hash;
103     $parsed->messageid = lti_parse_message_id($xml);
105     return $parsed;
108 function lti_parse_grade_delete_message($xml) {
109     $node = $xml->imsx_POXBody->deleteResultRequest->resultRecord->sourcedGUID->sourcedId;
110     $resultjson = json_decode((string)$node);
112     $parsed = new stdClass();
113     $parsed->instanceid = $resultjson->data->instanceid;
114     $parsed->userid = $resultjson->data->userid;
115     $parsed->launchid = $resultjson->data->launchid;
116     $parsed->sourcedidhash = $resultjson->hash;
118     $parsed->messageid = lti_parse_message_id($xml);
120     return $parsed;
123 function lti_update_grade($ltiinstance, $userid, $launchid, $gradeval) {
124     global $CFG, $DB;
125     require_once($CFG->libdir . '/gradelib.php');
127     $params = array();
128     $params['itemname'] = $ltiinstance->name;
130     $grade = new stdClass();
131     $grade->userid   = $userid;
132     $grade->rawgrade = $gradeval;
134     $status = grade_update(LTI_SOURCE, $ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, 0, $grade, $params);
136     $record = $DB->get_record('lti_submission', array('ltiid' => $ltiinstance->id, 'userid' => $userid, 'launchid' => $launchid), 'id');
137     if ($record) {
138         $id = $record->id;
139     } else {
140         $id = null;
141     }
143     if (!empty($id)) {
144         $DB->update_record('lti_submission', array(
145             'id' => $id,
146             'dateupdated' => time(),
147             'gradepercent' => $gradeval,
148             'state' => 2
149         ));
150     } else {
151         $DB->insert_record('lti_submission', array(
152             'ltiid' => $ltiinstance->id,
153             'userid' => $userid,
154             'datesubmitted' => time(),
155             'dateupdated' => time(),
156             'gradepercent' => $gradeval,
157             'originalgrade' => $gradeval,
158             'launchid' => $launchid,
159             'state' => 1
160         ));
161     }
163     return $status == GRADE_UPDATE_OK;
166 function lti_read_grade($ltiinstance, $userid) {
167     global $CFG;
168     require_once($CFG->libdir . '/gradelib.php');
170     $grades = grade_get_grades($ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, $userid);
172     if (isset($grades) && isset($grades->items[0]) && is_array($grades->items[0]->grades)) {
173         foreach ($grades->items[0]->grades as $agrade) {
174             $grade = $agrade->grade;
175             $grade = $grade / 100.0;
176             break;
177         }
178     }
180     if (isset($grade)) {
181         return $grade;
182     }
185 function lti_delete_grade($ltiinstance, $userid) {
186     global $CFG;
187     require_once($CFG->libdir . '/gradelib.php');
189     $grade = new stdClass();
190     $grade->userid   = $userid;
191     $grade->rawgrade = null;
193     $status = grade_update(LTI_SOURCE, $ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, 0, $grade, array('deleted'=>1));
195     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
198 function lti_verify_message($key, $sharedsecrets, $body, $headers = null) {
199     foreach ($sharedsecrets as $secret) {
200         $signaturefailed = false;
202         try {
203             // TODO: Switch to core oauthlib once implemented - MDL-30149
204             lti\handleOAuthBodyPOST($key, $secret, $body, $headers);
205         } catch (Exception $e) {
206             $signaturefailed = true;
207         }
209         if (!$signaturefailed) {
210             return $secret;//Return the secret used to sign the message)
211         }
212     }
214     return false;
217 function lti_verify_sourcedid($ltiinstance, $parsed) {
218     $sourceid = lti_build_sourcedid($parsed->instanceid, $parsed->userid, $parsed->launchid, $ltiinstance->servicesalt);
220     if ($sourceid->hash != $parsed->sourcedidhash) {
221         throw new Exception('SourcedId hash not valid');
222     }