Skip to content

Commit

Permalink
MDL-60061 mod_scorm: add support for drag/drop events
Browse files Browse the repository at this point in the history
AMOS BEGIN
	CPY [openafterclose,mod_choice],[openafterclose,mod_scorm]
AMOS END
  • Loading branch information
lameze committed Dec 12, 2017
1 parent 109aa07 commit f808e04
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
1 change: 1 addition & 0 deletions mod/scorm/lang/en/scorm.php
Expand Up @@ -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';
Expand Down
117 changes: 117 additions & 0 deletions mod/scorm/lib.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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];
}
3 changes: 0 additions & 3 deletions mod/scorm/locallib.php
Expand Up @@ -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.

/**
Expand Down

0 comments on commit f808e04

Please sign in to comment.