diff --git a/mod/scorm/lang/en/scorm.php b/mod/scorm/lang/en/scorm.php index 06c2e1c558c99..c6ad22ff722aa 100644 --- a/mod/scorm/lang/en/scorm.php +++ b/mod/scorm/lang/en/scorm.php @@ -297,6 +297,7 @@ $string['not_corr_type'] = 'Type mismatch for tag {$a->tag}'; $string['notopenyet'] = 'Sorry, this activity is not available until {$a}'; $string['objectives'] = 'Objectives'; +$string['openafterclose'] = 'You have specified an open date after the close date'; $string['optallstudents'] = 'all users'; $string['optattemptsonly'] = 'users with attempts only'; $string['optnoattemptsonly'] = 'users with no attempts only'; diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index 2416be80fdf62..622b994517c6b 100644 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -50,6 +50,9 @@ define('SCORM_DISPLAY_ATTEMPTSTATUS_MY', 2); define('SCORM_DISPLAY_ATTEMPTSTATUS_ENTRY', 3); +define('SCORM_EVENT_TYPE_OPEN', 'open'); +define('SCORM_EVENT_TYPE_CLOSE', 'close'); + /** * Return an array of status options * @@ -1798,3 +1801,117 @@ function mod_scorm_get_completion_active_rule_descriptions($cm) { } return $descriptions; } + +/** + * This function will update the scorm module according to the + * event that has been modified. + * + * It will set the timeopen or timeclose value of the scorm instance + * according to the type of event provided. + * + * @throws \moodle_exception + * @param \calendar_event $event + * @param stdClass $scorm The module instance to get the range from + */ +function mod_scorm_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $scorm) { + global $DB; + + if (empty($event->instance) || $event->modulename != 'scorm') { + return; + } + + if ($event->instance != $scorm->id) { + return; + } + + if (!in_array($event->eventtype, [SCORM_EVENT_TYPE_OPEN, SCORM_EVENT_TYPE_CLOSE])) { + return; + } + + $courseid = $event->courseid; + $modulename = $event->modulename; + $instanceid = $event->instance; + $modified = false; + + $coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid]; + $context = context_module::instance($coursemodule->id); + + // The user does not have the capability to modify this activity. + if (!has_capability('moodle/course:manageactivities', $context)) { + return; + } + + if ($event->eventtype == SCORM_EVENT_TYPE_OPEN) { + // If the event is for the scorm activity opening then we should + // set the start time of the scorm activity to be the new start + // time of the event. + if ($scorm->timeopen != $event->timestart) { + $scorm->timeopen = $event->timestart; + $scorm->timemodified = time(); + $modified = true; + } + } else if ($event->eventtype == SCORM_EVENT_TYPE_CLOSE) { + // If the event is for the scorm activity closing then we should + // set the end time of the scorm activity to be the new start + // time of the event. + if ($scorm->timeclose != $event->timestart) { + $scorm->timeclose = $event->timestart; + $modified = true; + } + } + + if ($modified) { + $scorm->timemodified = time(); + $DB->update_record('scorm', $scorm); + $event = \core\event\course_module_updated::create_from_cm($coursemodule, $context); + $event->trigger(); + } +} + +/** + * This function calculates the minimum and maximum cutoff values for the timestart of + * the given event. + * + * It will return an array with two values, the first being the minimum cutoff value and + * the second being the maximum cutoff value. Either or both values can be null, which + * indicates there is no minimum or maximum, respectively. + * + * If a cutoff is required then the function must return an array containing the cutoff + * timestamp and error string to display to the user if the cutoff value is violated. + * + * A minimum and maximum cutoff return value will look like: + * [ + * [1505704373, 'The date must be after this date'], + * [1506741172, 'The date must be before this date'] + * ] + * + * @param \calendar_event $event The calendar event to get the time range for + * @param \stdClass $instance The module instance to get the range from + * @return array Returns an array with min and max date. + */ +function mod_scorm_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $instance) { + $mindate = null; + $maxdate = null; + + if ($event->eventtype == SCORM_EVENT_TYPE_OPEN) { + // The start time of the open event can't be equal to or after the + // close time of the scorm activity. + if (!empty($instance->timeclose)) { + $maxdate = [ + $instance->timeclose, + get_string('openafterclose', 'scorm') + ]; + } + } else if ($event->eventtype == SCORM_EVENT_TYPE_CLOSE) { + // The start time of the close event can't be equal to or earlier than the + // open time of the scorm activity. + if (!empty($instance->timeopen)) { + $mindate = [ + $instance->timeopen, + get_string('closebeforeopen', 'scorm') + ]; + } + } + + return [$mindate, $maxdate]; +} \ No newline at end of file diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index 869de923df990..58fbc8aa54829 100644 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -51,9 +51,6 @@ define('TOCJSLINK', 1); define('TOCFULLURL', 2); -define('SCORM_EVENT_TYPE_OPEN', 'open'); -define('SCORM_EVENT_TYPE_CLOSE', 'close'); - // Local Library of functions for module scorm. /**