From 4d5a33e424796c25f3941d66bfa792823402e56b 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 f46b934e7b0..9f7bd5ff9ac 100644 --- a/mod/lti/db/upgrade.php +++ b/mod/lti/db/upgrade.php @@ -103,5 +103,56 @@ function xmldb_lti_upgrade($oldversion) { // Automatically generated Moodle v3.4.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2017111301) { + + // 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, 2017111301, '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 b5e0d842f71..a871ec1612a 100644 --- a/mod/lti/version.php +++ b/mod/lti/version.php @@ -48,7 +48,7 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2017111300; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2017111301; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2017110800; // Requires this Moodle version. $plugin->component = 'mod_lti'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0;