From cd477f8c3ef2c5631e4a3331fd6436ed0ea3fe90 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Fri, 28 Apr 2017 10:35:29 +0800 Subject: [PATCH] MDL-58665 mod_scorm: cache times in modinfo for performance --- mod/scorm/lib.php | 23 ++++++++++++++++++++--- mod/scorm/tests/lib_test.php | 8 ++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index 8daa28df239..c4a7c0f041a 100644 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -1627,12 +1627,21 @@ function scorm_refresh_events($courseid = 0) { */ function mod_scorm_core_calendar_provide_event_action(calendar_event $event, \core_calendar\action_factory $factory) { - global $CFG, $DB; + global $CFG; require_once($CFG->dirroot . '/mod/scorm/locallib.php'); $cm = get_fast_modinfo($event->courseid)->instances['scorm'][$event->instance]; - $scorm = $DB->get_record('scorm', array('id' => $event->instance)); + + if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < time()) { + // The scorm has closed so the user can no longer submit anything. + return null; + } + + // Restore scorm object from cached values in $cm, we only need id, timeclose and timeopen. + $customdata = $cm->customdata ?: []; + $customdata['id'] = $cm->instance; + $scorm = (object)($customdata + ['timeclose' => 0, 'timeopen' => 0]); // Check that the SCORM activity is open. list($actionable, $warnings) = scorm_get_availability_status($scorm); @@ -1660,7 +1669,8 @@ function scorm_get_coursemodule_info($coursemodule) { global $DB; $dbparams = ['id' => $coursemodule->instance]; - $fields = 'id, name, intro, introformat, completionstatusrequired, completionscorerequired, completionstatusallscos'; + $fields = 'id, name, intro, introformat, completionstatusrequired, completionscorerequired, completionstatusallscos, '. + 'timeopen, timeclose'; if (!$scorm = $DB->get_record('scorm', $dbparams, $fields)) { return false; } @@ -1679,6 +1689,13 @@ function scorm_get_coursemodule_info($coursemodule) { $result->customdata['customcompletionrules']['completionscorerequired'] = $scorm->completionscorerequired; $result->customdata['customcompletionrules']['completionstatusallscos'] = $scorm->completionstatusallscos; } + // Populate some other values that can be used in calendar or on dashboard. + if ($scorm->timeopen) { + $result->customdata['timeopen'] = $scorm->timeopen; + } + if ($scorm->timeclose) { + $result->customdata['timeclose'] = $scorm->timeclose; + } return $result; } diff --git a/mod/scorm/tests/lib_test.php b/mod/scorm/tests/lib_test.php index 6aac2524aec..cd3382236e7 100644 --- a/mod/scorm/tests/lib_test.php +++ b/mod/scorm/tests/lib_test.php @@ -242,12 +242,8 @@ class mod_scorm_lib_testcase extends externallib_advanced_testcase { // Decorate action event. $actionevent = mod_scorm_core_calendar_provide_event_action($event, $factory); - // Confirm the event was decorated. - $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); - $this->assertEquals(get_string('enter', 'scorm'), $actionevent->get_name()); - $this->assertInstanceOf('moodle_url', $actionevent->get_url()); - $this->assertEquals(1, $actionevent->get_item_count()); - $this->assertFalse($actionevent->is_actionable()); + // No event on the dashboard if module is closed. + $this->assertNull($actionevent); } public function test_scorm_core_calendar_provide_event_action_open_in_future() {