Skip to content

Commit

Permalink
MDL-57998 mod_scorm: added action events
Browse files Browse the repository at this point in the history
Part of MDL-55611 epic.
  • Loading branch information
mdjnelson authored and Damyon Wiese committed Apr 3, 2017
1 parent 7aedfe3 commit 3a41f73
Show file tree
Hide file tree
Showing 6 changed files with 547 additions and 6 deletions.
2 changes: 2 additions & 0 deletions mod/scorm/lang/en/scorm.php
Expand Up @@ -72,6 +72,8 @@
$string['browsemode'] = 'Preview mode';
$string['browserepository'] = 'Browse repository';
$string['calculatedweight'] = 'Calculated weight';
$string['calendarend'] = 'The SCORM activity \'{$a}\' closes';
$string['calendarstart'] = 'The SCORM activity \'{$a}\' opens';
$string['cannotaccess'] = 'You cannot call this script in that way';
$string['cannotfindsco'] = 'Could not find SCO';
$string['closebeforeopen'] = 'You have specified a close date before the open date.';
Expand Down
65 changes: 65 additions & 0 deletions mod/scorm/lib.php
Expand Up @@ -165,6 +165,7 @@ function scorm_add_instance($scorm, $mform=null) {
scorm_parse($record, true);

scorm_grade_item_update($record);
scorm_update_calendar($record, $cmid);

return $record->id;
}
Expand Down Expand Up @@ -255,6 +256,7 @@ function scorm_update_instance($scorm, $mform=null) {

scorm_grade_item_update($scorm);
scorm_update_grades($scorm);
scorm_update_calendar($scorm, $cmid);

return true;
}
Expand Down Expand Up @@ -1553,3 +1555,66 @@ function mod_scorm_get_fontawesome_icon_map() {
'mod_scorm:wait' => 'fa-clock-o',
];
}

/**
* This standard function will check all instances of this module
* and make sure there are up-to-date events created for each of them.
* If courseid = 0, then every scorm event in the site is checked, else
* only scorm events belonging to the course specified are checked.
*
* @param int $courseid
* @return bool
*/
function scorm_refresh_events($courseid = 0) {
global $CFG, $DB;

require_once($CFG->dirroot . '/mod/scorm/locallib.php');

if ($courseid) {
// Make sure that the course id is numeric.
if (!is_numeric($courseid)) {
return false;
}
if (!$scorms = $DB->get_records('scorm', array('course' => $courseid))) {
return false;
}
} else {
if (!$scorms = $DB->get_records('scorm')) {
return false;
}
}

foreach ($scorms as $scorm) {
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
scorm_update_calendar($scorm, $cm->id);
}

return true;
}

/**
* Handles creating actions for events.
*
* @param \core_calendar\event $event
* @param \core_calendar\action_factory $factory
* @return \core_calendar\local\event\value_objects\action|\core_calendar\local\interfaces\action_interface|null
*/
function mod_scorm_core_calendar_provide_event_action(\core_calendar\event $event,
\core_calendar\action_factory $factory) {
global $CFG, $DB;

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));

// Check that the SCORM activity is open.
list($actionable, $warnings) = scorm_get_availability_status($scorm);

return $factory->create_instance(
get_string('enter', 'scorm'),
new \moodle_url('/mod/scorm/view.php', array('id' => $cm->id)),
1,
$actionable
);
}
101 changes: 101 additions & 0 deletions mod/scorm/locallib.php
Expand Up @@ -51,6 +51,9 @@
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 Expand Up @@ -2352,3 +2355,101 @@ function scorm_eval_prerequisites($prerequisites, $usertracks) {
}
return eval('return '.implode($stack).';');
}

/**
* Update the calendar entries for this scorm activity.
*
* @param stdClass $scorm the row from the database table scorm.
* @param int $cmid The coursemodule id
* @return bool
*/
function scorm_update_calendar(stdClass $scorm, $cmid) {
global $DB, $CFG;

require_once($CFG->dirroot.'/calendar/lib.php');

// Scorm start calendar events.
$event = new stdClass();
$event->eventtype = SCORM_EVENT_TYPE_OPEN;
// The SCORM_EVENT_TYPE_OPEN event should only be an action event if no close time is specified.
$event->type = empty($scorm->timeclose) ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
if ($event->id = $DB->get_field('event', 'id',
array('modulename' => 'scorm', 'instance' => $scorm->id, 'eventtype' => $event->eventtype))) {
if ((!empty($scorm->timeopen)) && ($scorm->timeopen > 0)) {
// Calendar event exists so update it.
$event->name = get_string('calendarstart', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->timestart = $scorm->timeopen;
$event->timesort = $scorm->timeopen;
$event->visible = instance_is_visible('scorm', $scorm);
$event->timeduration = 0;

$calendarevent = \core_calendar\event::load($event->id);
$calendarevent->update($event);
} else {
// Calendar event is on longer needed.
$calendarevent = \core_calendar\event::load($event->id);
$calendarevent->delete();
}
} else {
// Event doesn't exist so create one.
if ((!empty($scorm->timeopen)) && ($scorm->timeopen > 0)) {
$event->name = get_string('calendarstart', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->courseid = $scorm->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'scorm';
$event->instance = $scorm->id;
$event->timestart = $scorm->timeopen;
$event->timesort = $scorm->timeopen;
$event->visible = instance_is_visible('scorm', $scorm);
$event->timeduration = 0;

\core_calendar\event::create($event);
}
}

// Scorm end calendar events.
$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = SCORM_EVENT_TYPE_CLOSE;
if ($event->id = $DB->get_field('event', 'id',
array('modulename' => 'scorm', 'instance' => $scorm->id, 'eventtype' => $event->eventtype))) {
if ((!empty($scorm->timeclose)) && ($scorm->timeclose > 0)) {
// Calendar event exists so update it.
$event->name = get_string('calendarend', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->timestart = $scorm->timeclose;
$event->timesort = $scorm->timeclose;
$event->visible = instance_is_visible('scorm', $scorm);
$event->timeduration = 0;

$calendarevent = \core_calendar\event::load($event->id);
$calendarevent->update($event);
} else {
// Calendar event is on longer needed.
$calendarevent = \core_calendar\event::load($event->id);
$calendarevent->delete();
}
} else {
// Event doesn't exist so create one.
if ((!empty($scorm->timeclose)) && ($scorm->timeclose > 0)) {
$event->name = get_string('calendarend', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->courseid = $scorm->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'scorm';
$event->instance = $scorm->id;
$event->timestart = $scorm->timeclose;
$event->timesort = $scorm->timeclose;
$event->visible = instance_is_visible('scorm', $scorm);
$event->timeduration = 0;

\core_calendar\event::create($event);
}
}

return true;
}
12 changes: 6 additions & 6 deletions mod/scorm/tests/externallib_test.php
Expand Up @@ -220,9 +220,6 @@ public function test_mod_scorm_get_scorm_scoes() {
$student = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();

// Set to the student user.
self::setUser($student);

// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();

Expand All @@ -233,6 +230,9 @@ public function test_mod_scorm_get_scorm_scoes() {
$record->timeclose = $record->timeopen + DAYSECS;
$scorm = self::getDataGenerator()->create_module('scorm', $record);

// Set to the student user.
self::setUser($student);

// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
Expand Down Expand Up @@ -421,9 +421,6 @@ public function test_mod_scorm_insert_scorm_tracks() {
// Create users.
$student = self::getDataGenerator()->create_user();

// Set to the student user.
self::setUser($student);

// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();

Expand All @@ -449,6 +446,9 @@ public function test_mod_scorm_insert_scorm_tracks() {
'value' => '80'
);

// Set to the student user.
self::setUser($student);

// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
Expand Down
136 changes: 136 additions & 0 deletions mod/scorm/tests/lib_test.php
Expand Up @@ -191,4 +191,140 @@ public function test_scorm_check_and_require_available() {
public function test_scorm_get_last_completed_attempt() {
$this->assertEquals(1, scorm_get_last_completed_attempt($this->scorm->id, $this->student->id));
}

public function test_scorm_core_calendar_provide_event_action_open() {
$this->resetAfterTest();

$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();

// Create a scorm activity.
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id,
'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));

// Create a calendar event.
$event = $this->create_action_event($course->id, $scorm->id, SCORM_EVENT_TYPE_OPEN);

// Create an action factory.
$factory = new \core_calendar\action_factory();

// 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->assertTrue($actionevent->is_actionable());
}

public function test_scorm_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();

$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();

// Create a scorm activity.
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id,
'timeclose' => time() - DAYSECS));

// Create a calendar event.
$event = $this->create_action_event($course->id, $scorm->id, SCORM_EVENT_TYPE_OPEN);

// Create an action factory.
$factory = new \core_calendar\action_factory();

// 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());
}

public function test_scorm_core_calendar_provide_event_action_open_in_future() {
$this->resetAfterTest();

$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();

// Create a scorm activity.
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id,
'timeopen' => time() + DAYSECS));

// Create a calendar event.
$event = $this->create_action_event($course->id, $scorm->id, SCORM_EVENT_TYPE_OPEN);

// Create an action factory.
$factory = new \core_calendar\action_factory();

// 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());
}

public function test_scorm_core_calendar_provide_event_action_no_time_specified() {
$this->resetAfterTest();

$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();

// Create a scorm activity.
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id));

// Create a calendar event.
$event = $this->create_action_event($course->id, $scorm->id, SCORM_EVENT_TYPE_OPEN);

// Create an action factory.
$factory = new \core_calendar\action_factory();

// 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->assertTrue($actionevent->is_actionable());
}

/**
* Creates an action event.
*
* @param int $courseid
* @param int $instanceid The data id.
* @param string $eventtype The event type. eg. DATA_EVENT_TYPE_OPEN.
* @return bool|\core_calendar\event
*/
private function create_action_event($courseid, $instanceid, $eventtype) {
$event = new stdClass();
$event->name = 'Calendar event';
$event->modulename = 'scorm';
$event->courseid = $courseid;
$event->instance = $instanceid;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = $eventtype;
$event->timestart = time();

return \core_calendar\event::create($event);
}
}

0 comments on commit 3a41f73

Please sign in to comment.