From ff6faa59eba34e08223d6bee5508f4687a6e6755 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Wed, 11 Oct 2017 14:28:58 -0700 Subject: [PATCH] MDL-60187 mod_lti: Do not create a grade item when grades are disabled. If the privacy option "Accept grades from the tool" is disabled, the module should not appear in the gradebook when edited inline or when recover grades is running during enrolment. --- mod/lti/db/upgrade.php | 51 ++++++++++++++++++++++++++++++++++++++++++ mod/lti/lib.php | 5 +++++ mod/lti/version.php | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/mod/lti/db/upgrade.php b/mod/lti/db/upgrade.php index 8d2db1f5e3d..70f0b06b1f8 100644 --- a/mod/lti/db/upgrade.php +++ b/mod/lti/db/upgrade.php @@ -216,5 +216,56 @@ function xmldb_lti_upgrade($oldversion) { // Automatically generated Moodle v3.3.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2017051501) { + + // A bug in the LTI plugin incorrectly inserted a grade item for + // LTI instances which were set to not allow grading. + // The change finds any LTI which does not have grading enabled, + // and updates any grades to delete them. + + $ltis = $DB->get_recordset_sql(" + SELECT + l.id, + l.course, + l.instructorchoiceacceptgrades, + t.enabledcapability, + t.toolproxyid, + tc.value AS acceptgrades + FROM {lti} l + INNER JOIN {grade_items} gt + ON l.id = gt.iteminstance + LEFT JOIN {lti_types} t + ON t.id = l.typeid + LEFT JOIN {lti_types_config} tc + ON tc.typeid = t.id AND tc.name = 'acceptgrades' + WHERE gt.itemmodule = 'lti' + AND gt.itemtype = 'mod' + "); + + foreach ($ltis as $lti) { + $acceptgrades = true; + if (empty($lti->toolproxyid)) { + $typeacceptgrades = isset($lti->acceptgrades) ? $lti->acceptgrades : 2; + if (!($typeacceptgrades == 1 || + ($typeacceptgrades == 2 && $lti->instructorchoiceacceptgrades == 1))) { + $acceptgrades = false; + } + } else { + $enabledcapabilities = explode("\n", $lti->enabledcapability); + $acceptgrades = in_array('Result.autocreate', $enabledcapabilities); + } + + if (!$acceptgrades) { + grade_update('mod/lti', $lti->course, 'mod', 'lti', $lti->id, 0, null, array('deleted' => 1)); + } + + } + + $ltis->close(); + + upgrade_mod_savepoint(true, 2017051501, 'lti'); + } + return true; + } diff --git a/mod/lti/lib.php b/mod/lti/lib.php index c1e207a1d95..ab9df0ed60c 100644 --- a/mod/lti/lib.php +++ b/mod/lti/lib.php @@ -489,6 +489,11 @@ function lti_get_lti_types_from_proxy_id($toolproxyid) { function lti_grade_item_update($basiclti, $grades = null) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); + require_once($CFG->dirroot.'/mod/lti/servicelib.php'); + + if (!lti_accepts_grades($basiclti)) { + return 0; + } $params = array('itemname' => $basiclti->name, 'idnumber' => $basiclti->cmidnumber); diff --git a/mod/lti/version.php b/mod/lti/version.php index 00100c129ae..b8fc74ed6db 100644 --- a/mod/lti/version.php +++ b/mod/lti/version.php @@ -48,7 +48,7 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2017051500; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2017051501; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2017050500; // Requires this Moodle version. $plugin->component = 'mod_lti'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0;