MDL-58665 mod_data: cache times in modinfo for performance

This commit is contained in:
Marina Glancy 2017-04-28 10:35:14 +08:00
parent 19228840b8
commit e1577187ed
2 changed files with 18 additions and 17 deletions

View File

@ -4298,21 +4298,19 @@ function data_check_updates_since(cm_info $cm, $from, $filter = array()) {
*/
function mod_data_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $DB;
$cm = get_fast_modinfo($event->courseid)->instances['data'][$event->instance];
$data = $DB->get_record('data', array('id' => $event->instance), 'id, timeavailablefrom, timeavailableto');
$now = time();
if ($data->timeavailablefrom && $data->timeavailableto) {
$actionable = (time() >= $data->timeavailablefrom) && (time() <= $data->timeavailableto);
} else if ($data->timeavailableto) {
$actionable = time() < $data->timeavailableto;
} else if ($data->timeavailablefrom) {
$actionable = time() >= $data->timeavailablefrom;
} else {
$actionable = true;
if (!empty($cm->customdata['timeavailableto']) && $cm->customdata['timeavailableto'] < $now) {
// The module has closed so the user can no longer submit anything.
return null;
}
// The module is actionable if we don't have a start time or the start time is
// in the past.
$actionable = (empty($cm->customdata['timeavailablefrom']) || $cm->customdata['timeavailablefrom'] <= $now);
return $factory->create_instance(
get_string('add', 'data'),
new \moodle_url('/mod/data/view.php', array('id' => $cm->id)),
@ -4336,7 +4334,7 @@ function data_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, completionentries';
$fields = 'id, name, intro, introformat, completionentries, timeavailablefrom, timeavailableto';
if (!$data = $DB->get_record('data', $dbparams, $fields)) {
return false;
}
@ -4353,6 +4351,13 @@ function data_get_coursemodule_info($coursemodule) {
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionentries'] = $data->completionentries;
}
// Other properties that may be used in calendar or on dashboard.
if ($data->timeavailablefrom) {
$result->customdata['timeavailablefrom'] = $data->timeavailablefrom;
}
if ($data->timeavailableto) {
$result->customdata['timeavailableto'] = $data->timeavailableto;
}
return $result;
}

View File

@ -1099,12 +1099,8 @@ class mod_data_lib_testcase extends advanced_testcase {
// Decorate action event.
$actionevent = mod_data_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('add', 'data'), $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_data_core_calendar_provide_event_action_open_in_future() {