Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lti/servicelib.php
Go to the documentation of this file.
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 defined('MOODLE_INTERNAL') || die;
00028 
00029 require_once($CFG->dirroot.'/mod/lti/OAuthBody.php');
00030 
00031 // TODO: Switch to core oauthlib once implemented - MDL-30149
00032 use moodle\mod\lti as lti;
00033 
00034 define('LTI_ITEM_TYPE', 'mod');
00035 define('LTI_ITEM_MODULE', 'lti');
00036 define('LTI_SOURCE', 'mod/lti');
00037 
00038 function lti_get_response_xml($codemajor, $description, $messageref, $messagetype) {
00039     $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><imsx_POXEnvelopeResponse />');
00040     $xml->addAttribute('xmlns', 'http://www.imsglobal.org/lis/oms1p0/pox');
00041 
00042     $headerinfo = $xml->addChild('imsx_POXHeader')->addChild('imsx_POXResponseHeaderInfo');
00043 
00044     $headerinfo->addChild('imsx_version', 'V1.0');
00045     $headerinfo->addChild('imsx_messageIdentifier', (string)mt_rand());
00046 
00047     $statusinfo = $headerinfo->addChild('imsx_statusInfo');
00048     $statusinfo->addchild('imsx_codeMajor', $codemajor);
00049     $statusinfo->addChild('imsx_severity', 'status');
00050     $statusinfo->addChild('imsx_description', $description);
00051     $statusinfo->addChild('imsx_messageRefIdentifier', $messageref);
00052 
00053     $xml->addChild('imsx_POXBody')->addChild($messagetype);
00054 
00055     return $xml;
00056 }
00057 
00058 function lti_parse_message_id($xml) {
00059     $node = $xml->imsx_POXHeader->imsx_POXRequestHeaderInfo->imsx_messageIdentifier;
00060     $messageid = (string)$node;
00061 
00062     return $messageid;
00063 }
00064 
00065 function lti_parse_grade_replace_message($xml) {
00066     $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->sourcedGUID->sourcedId;
00067     $resultjson = json_decode((string)$node);
00068 
00069     $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->result->resultScore->textString;
00070 
00071     $score = (string) $node;
00072     if ( ! is_numeric($score) ) {
00073         throw new Exception('Score must be numeric');
00074     }
00075     $grade = floatval($score);
00076     if ( $grade < 0.0 || $grade > 1.0 ) {
00077         throw new Exception('Score not between 0.0 and 1.0');
00078     }
00079 
00080     $parsed = new stdClass();
00081     $parsed->gradeval = $grade * 100;
00082 
00083     $parsed->instanceid = $resultjson->data->instanceid;
00084     $parsed->userid = $resultjson->data->userid;
00085     $parsed->launchid = $resultjson->data->launchid;
00086     $parsed->sourcedidhash = $resultjson->hash;
00087 
00088     $parsed->messageid = lti_parse_message_id($xml);
00089 
00090     return $parsed;
00091 }
00092 
00093 function lti_parse_grade_read_message($xml) {
00094     $node = $xml->imsx_POXBody->readResultRequest->resultRecord->sourcedGUID->sourcedId;
00095     $resultjson = json_decode((string)$node);
00096 
00097     $parsed = new stdClass();
00098     $parsed->instanceid = $resultjson->data->instanceid;
00099     $parsed->userid = $resultjson->data->userid;
00100     $parsed->launchid = $resultjson->data->launchid;
00101     $parsed->sourcedidhash = $resultjson->hash;
00102 
00103     $parsed->messageid = lti_parse_message_id($xml);
00104 
00105     return $parsed;
00106 }
00107 
00108 function lti_parse_grade_delete_message($xml) {
00109     $node = $xml->imsx_POXBody->deleteResultRequest->resultRecord->sourcedGUID->sourcedId;
00110     $resultjson = json_decode((string)$node);
00111 
00112     $parsed = new stdClass();
00113     $parsed->instanceid = $resultjson->data->instanceid;
00114     $parsed->userid = $resultjson->data->userid;
00115     $parsed->launchid = $resultjson->data->launchid;
00116     $parsed->sourcedidhash = $resultjson->hash;
00117 
00118     $parsed->messageid = lti_parse_message_id($xml);
00119 
00120     return $parsed;
00121 }
00122 
00123 function lti_update_grade($ltiinstance, $userid, $launchid, $gradeval) {
00124     global $CFG, $DB;
00125     require_once($CFG->libdir . '/gradelib.php');
00126 
00127     $params = array();
00128     $params['itemname'] = $ltiinstance->name;
00129 
00130     $grade = new stdClass();
00131     $grade->userid   = $userid;
00132     $grade->rawgrade = $gradeval;
00133 
00134     $status = grade_update(LTI_SOURCE, $ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, 0, $grade, $params);
00135 
00136     $record = $DB->get_record('lti_submission', array('ltiid' => $ltiinstance->id, 'userid' => $userid, 'launchid' => $launchid), 'id');
00137     if ($record) {
00138         $id = $record->id;
00139     } else {
00140         $id = null;
00141     }
00142 
00143     if (!empty($id)) {
00144         $DB->update_record('lti_submission', array(
00145             'id' => $id,
00146             'dateupdated' => time(),
00147             'gradepercent' => $gradeval,
00148             'state' => 2
00149         ));
00150     } else {
00151         $DB->insert_record('lti_submission', array(
00152             'ltiid' => $ltiinstance->id,
00153             'userid' => $userid,
00154             'datesubmitted' => time(),
00155             'dateupdated' => time(),
00156             'gradepercent' => $gradeval,
00157             'originalgrade' => $gradeval,
00158             'launchid' => $launchid,
00159             'state' => 1
00160         ));
00161     }
00162 
00163     return $status == GRADE_UPDATE_OK;
00164 }
00165 
00166 function lti_read_grade($ltiinstance, $userid) {
00167     global $CFG;
00168     require_once($CFG->libdir . '/gradelib.php');
00169 
00170     $grades = grade_get_grades($ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, $userid);
00171 
00172     if (isset($grades) && isset($grades->items[0]) && is_array($grades->items[0]->grades)) {
00173         foreach ($grades->items[0]->grades as $agrade) {
00174             $grade = $agrade->grade;
00175             $grade = $grade / 100.0;
00176             break;
00177         }
00178     }
00179 
00180     if (isset($grade)) {
00181         return $grade;
00182     }
00183 }
00184 
00185 function lti_delete_grade($ltiinstance, $userid) {
00186     global $CFG;
00187     require_once($CFG->libdir . '/gradelib.php');
00188 
00189     $grade = new stdClass();
00190     $grade->userid   = $userid;
00191     $grade->rawgrade = null;
00192 
00193     $status = grade_update(LTI_SOURCE, $ltiinstance->course, LTI_ITEM_TYPE, LTI_ITEM_MODULE, $ltiinstance->id, 0, $grade, array('deleted'=>1));
00194 
00195     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
00196 }
00197 
00198 function lti_verify_message($key, $sharedsecrets, $body, $headers = null) {
00199     foreach ($sharedsecrets as $secret) {
00200         $signaturefailed = false;
00201 
00202         try {
00203             // TODO: Switch to core oauthlib once implemented - MDL-30149
00204             lti\handleOAuthBodyPOST($key, $secret, $body, $headers);
00205         } catch (Exception $e) {
00206             $signaturefailed = true;
00207         }
00208 
00209         if (!$signaturefailed) {
00210             return $secret;//Return the secret used to sign the message)
00211         }
00212     }
00213 
00214     return false;
00215 }
00216 
00217 function lti_verify_sourcedid($ltiinstance, $parsed) {
00218     $sourceid = lti_build_sourcedid($parsed->instanceid, $parsed->userid, $parsed->launchid, $ltiinstance->servicesalt);
00219 
00220     if ($sourceid->hash != $parsed->sourcedidhash) {
00221         throw new Exception('SourcedId hash not valid');
00222     }
00223 }
 All Data Structures Namespaces Files Functions Variables Enumerations