MDL-63148 mod_scorm: Accept userid param in event action

* Update lib scorm functions to accept and use a passed in userid
* Additional phpunit tests
This commit is contained in:
Peter 2018-10-24 13:41:28 +08:00
parent 79d87ad683
commit 40bf3feacd
3 changed files with 67 additions and 7 deletions

View file

@ -1679,17 +1679,22 @@ function scorm_refresh_events($courseid = 0, $instance = null, $cm = null) {
*
* @param calendar_event $event
* @param \core_calendar\action_factory $factory
* @param int $userid User id override
* @return \core_calendar\local\event\entities\action_interface|null
*/
function mod_scorm_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $CFG;
\core_calendar\action_factory $factory, $userid = null) {
global $CFG, $USER;
require_once($CFG->dirroot . '/mod/scorm/locallib.php');
$cm = get_fast_modinfo($event->courseid)->instances['scorm'][$event->instance];
if (empty($userid)) {
$userid = $USER->id;
}
if (has_capability('mod/scorm:viewreport', $cm->context)) {
$cm = get_fast_modinfo($event->courseid, $userid)->instances['scorm'][$event->instance];
if (has_capability('mod/scorm:viewreport', $cm->context, $userid)) {
// Teachers do not need to be reminded to complete a scorm.
return null;
}
@ -1705,7 +1710,7 @@ function mod_scorm_core_calendar_provide_event_action(calendar_event $event,
$scorm = (object)($customdata + ['timeclose' => 0, 'timeopen' => 0]);
// Check that the SCORM activity is open.
list($actionable, $warnings) = scorm_get_availability_status($scorm);
list($actionable, $warnings) = scorm_get_availability_status($scorm, false, null, $userid);
return $factory->create_instance(
get_string('enter', 'scorm'),

View file

@ -2122,10 +2122,11 @@ function scorm_check_launchable_sco($scorm, $scoid) {
* @param stdClass $scorm SCORM record
* @param boolean $checkviewreportcap Check the scorm:viewreport cap
* @param stdClass $context Module context, required if $checkviewreportcap is set to true
* @param int $userid User id override
* @return array status (available or not and possible warnings)
* @since Moodle 3.0
*/
function scorm_get_availability_status($scorm, $checkviewreportcap = false, $context = null) {
function scorm_get_availability_status($scorm, $checkviewreportcap = false, $context = null, $userid = null) {
$open = true;
$closed = false;
$warnings = array();
@ -2139,7 +2140,7 @@ function scorm_get_availability_status($scorm, $checkviewreportcap = false, $con
}
if (!$open or $closed) {
if ($checkviewreportcap and !empty($context) and has_capability('mod/scorm:viewreport', $context)) {
if ($checkviewreportcap and !empty($context) and has_capability('mod/scorm:viewreport', $context, $userid)) {
return array(true, $warnings);
}

View file

@ -141,6 +141,27 @@ class mod_scorm_lib_testcase extends externallib_advanced_testcase {
public function test_scorm_check_and_require_available() {
global $DB;
$this->setAdminUser();
// User override case.
$this->scorm->timeopen = time() + DAYSECS;
$this->scorm->timeclose = time() - DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
$this->assertEquals(true, $status);
$this->assertCount(0, $warnings);
// Now check with a student.
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context, $this->student->id);
$this->assertEquals(false, $status);
$this->assertCount(2, $warnings);
$this->assertArrayHasKey('notopenyet', $warnings);
$this->assertArrayHasKey('expired', $warnings);
$this->assertEquals(userdate($this->scorm->timeopen), $warnings['notopenyet']);
$this->assertEquals(userdate($this->scorm->timeclose), $warnings['expired']);
// Reset the scorm's times.
$this->scorm->timeopen = $this->scorm->timeclose = 0;
// Set to the student user.
self::setUser($this->student);
@ -323,6 +344,39 @@ class mod_scorm_lib_testcase extends externallib_advanced_testcase {
$this->assertFalse($actionevent->is_actionable());
}
public function test_scorm_core_calendar_provide_event_action_with_different_user_as_admin() {
$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 override with a passed in user.
$actionevent = mod_scorm_core_calendar_provide_event_action($event, $factory, $this->student->id);
$actionevent2 = mod_scorm_core_calendar_provide_event_action($event, $factory);
// Only students see scorm events.
$this->assertNull($actionevent2);
// 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();