diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index df870bd..03b257b 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -255,12 +255,16 @@ function lti_build_request($instance, $typeconfig, $course) { ( $typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS ) ) ) { $requestparams["lis_result_sourcedid"] = $sourcedid; - $serviceurl = $CFG->wwwroot . '/mod/lti/service.php'; + /* $serviceurl = $CFG->wwwroot . '/mod/lti/service.php'; if ($typeconfig['forcessl'] == '1') { $serviceurl = lti_ensure_url_is_https($serviceurl); } - $requestparams["ext_ims_lis_basic_outcome_url"] = $serviceurl; + $requestparams["ext_ims_lis_basic_outcome_url"] = $serviceurl; */ + + //Add outcome service URL + $url = new moodle_url('/mod/lti/service.php'); + $requestparams['lis_outcome_service_url'] = $url->out(); } /*if ( isset($placementsecret) && @@ -283,10 +287,6 @@ function lti_build_request($instance, $typeconfig, $course) { $requestparams["lis_person_contact_email_primary"] = $USER->email; } - //Add outcome service URL - $url = new moodle_url('/mod/lti/service.php'); - $requestparams['lis_outcome_service_url'] = $url->out(); - // Concatenate the custom parameters from the administrator and the instructor // Instructor parameters are only taken into consideration if the administrator // has giver permission diff --git a/mod/lti/service.php b/mod/lti/service.php index 9099e10..e78522d 100644 --- a/mod/lti/service.php +++ b/mod/lti/service.php @@ -61,7 +61,18 @@ foreach ($body->children() as $child) { switch ($messagetype) { case 'replaceResultRequest': - $parsed = lti_parse_grade_replace_message($xml); + try { + $parsed = lti_parse_grade_replace_message($xml); + } catch (Exception $e) { + $responsexml = lti_get_response_xml( + 'failure', + $e->getMessage(), + uniqid(), + 'replaceResultResponse'); + + echo $responsexml->asXML(); + break; + } $ltiinstance = $DB->get_record('lti', array('id' => $parsed->instanceid)); @@ -70,7 +81,7 @@ switch ($messagetype) { $gradestatus = lti_update_grade($ltiinstance, $parsed->userid, $parsed->launchid, $parsed->gradeval); $responsexml = lti_get_response_xml( - $gradestatus ? 'success' : 'error', + $gradestatus ? 'success' : 'failure', 'Grade replace response', $parsed->messageid, 'replaceResultResponse' @@ -94,14 +105,16 @@ switch ($messagetype) { $grade = lti_read_grade($ltiinstance, $parsed->userid); $responsexml = lti_get_response_xml( - isset($grade) ? 'success' : 'error', + isset($grade) ? 'success' : 'failure', 'Result read', $parsed->messageid, 'readResultResponse' ); $node = $responsexml->imsx_POXBody->readResultResponse; - $node->addChild('result')->addChild('resultScore')->addChild('textString', isset($grade) ? $grade : ''); + $node = $node->addChild('result')->addChild('resultScore'); + $node->addChild('language', 'en'); + $node->addChild('textString', isset($grade) ? $grade : ''); echo $responsexml->asXML(); @@ -117,7 +130,7 @@ switch ($messagetype) { $gradestatus = lti_delete_grade($ltiinstance, $parsed->userid); $responsexml = lti_get_response_xml( - $gradestatus ? 'success' : 'error', + $gradestatus ? 'success' : 'failure', 'Grade delete request', $parsed->messageid, 'deleteResultResponse' diff --git a/mod/lti/servicelib.php b/mod/lti/servicelib.php index 0dde18c..969212a 100644 --- a/mod/lti/servicelib.php +++ b/mod/lti/servicelib.php @@ -66,7 +66,15 @@ function lti_parse_grade_replace_message($xml) { $resultjson = json_decode((string)$node); $node = $xml->imsx_POXBody->replaceResultRequest->resultRecord->result->resultScore->textString; - $grade = floatval((string)$node); + + $score = (string) $node; + if ( ! is_numeric($score) ) { + throw new Exception('Score must be numeric'); + } + $grade = floatval($score); + if ( $grade < 0.0 || $grade > 1.0 ) { + throw new Exception('Score not between 0.0 and 1.0'); + } $parsed = new stdClass(); $parsed->gradeval = $grade * 100; @@ -163,6 +171,7 @@ function lti_read_grade($ltiinstance, $userid) { if (isset($grades) && isset($grades->items[0]) && is_array($grades->items[0]->grades)) { foreach ($grades->items[0]->grades as $agrade) { $grade = $agrade->grade; + $grade = $grade / 100.0; break; } } @@ -211,3 +220,4 @@ function lti_verify_sourcedid($ltiinstance, $parsed) { throw new Exception('SourcedId hash not valid'); } } +