MDL-51017 mod_scorm: New WS mod_scorm_get_scorm_attempt_count

This commit is contained in:
Juan Leyva 2015-08-06 14:33:14 +02:00
parent f495510548
commit 22de67f4af
8 changed files with 388 additions and 30 deletions

View File

@ -1158,6 +1158,7 @@ $services = array(
'core_user_add_user_private_files',
'mod_assign_view_grading_table',
'mod_scorm_view_scorm',
'mod_scorm_get_scorm_attempt_count',
'mod_page_view_page',
'mod_resource_view_resource',
'mod_folder_view_folder',

View File

@ -28,6 +28,7 @@ defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/scorm/lib.php');
require_once($CFG->dirroot . '/mod/scorm/locallib.php');
/**
* SCORM module external functions
@ -102,4 +103,83 @@ class mod_scorm_external extends external_api {
)
);
}
/**
* Describes the parameters for get_scorm_attempt_count.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_scorm_attempt_count_parameters() {
return new external_function_parameters(
array(
'scormid' => new external_value(PARAM_INT, 'SCORM instance id'),
'userid' => new external_value(PARAM_INT, 'User id'),
'ignoremissingcompletion' => new external_value(PARAM_BOOL,
'Ignores attempts that haven\'t reported a grade/completion',
VALUE_DEFAULT, false),
)
);
}
/**
* Return the number of attempts done by a user in the given SCORM.
*
* @param int $scormid the scorm id
* @param int $userid the user id
* @param bool $ignoremissingcompletion ignores attempts that haven't reported a grade/completion
* @return array of warnings and the attempts count
* @since Moodle 3.0
*/
public static function get_scorm_attempt_count($scormid, $userid, $ignoremissingcompletion = false) {
global $USER, $DB;
$params = self::validate_parameters(self::get_scorm_attempt_count_parameters(),
array('scormid' => $scormid, 'userid' => $userid,
'ignoremissingcompletion' => $ignoremissingcompletion));
$attempts = array();
$warnings = array();
$scorm = $DB->get_record('scorm', array('id' => $params['scormid']), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
$context = context_module::instance($cm->id);
self::validate_context($context);
// Validate the user obtaining the context, it will fail if the user doesn't exists or have been deleted.
context_user::instance($params['userid']);
// Extra checks so only users with permissions can view other users attempts.
if ($USER->id != $params['userid']) {
require_capability('mod/scorm:viewreport', $context);
}
// If the SCORM is not open this function will throw exceptions.
scorm_require_available($scorm);
$attemptscount = scorm_get_attempt_count($params['userid'], $scorm, false, $params['ignoremissingcompletion']);
$result = array();
$result['attemptscount'] = $attemptscount;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_scorm_attempt_count return value.
*
* @return external_single_structure
* @since Moodle 3.0
*/
public static function get_scorm_attempt_count_returns() {
return new external_single_structure(
array(
'attemptscount' => new external_value(PARAM_INT, 'Attempts count'),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -33,4 +33,12 @@ $functions = array(
'type' => 'write',
'capabilities' => ''
),
'mod_scorm_get_scorm_attempt_count' => array(
'classname' => 'mod_scorm_external',
'methodname' => 'get_scorm_attempt_count',
'description' => 'Return the number of attempts done by a user in the given SCORM.',
'type' => 'read',
'capabilities' => ''
),
);

View File

@ -2023,4 +2023,64 @@ function scorm_check_launchable_sco($scorm, $scoid) {
}
// Returning 0 will cause default behaviour which will find the first launchable sco in the package.
return 0;
}
}
/**
* Check if a SCORM is available for the current user.
*
* @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
* @return array status (available or not and possible warnings)
* @since Moodle 3.0
*/
function scorm_get_availability_status($scorm, $checkviewreportcap = false, $context = null) {
$open = true;
$closed = false;
$warnings = array();
$timenow = time();
if (!empty($scorm->timeopen) and $scorm->timeopen > $timenow) {
$open = false;
}
if (!empty($scorm->timeclose) and $timenow > $scorm->timeclose) {
$closed = true;
}
if (!$open or $closed) {
if ($checkviewreportcap and !empty($context) and has_capability('mod/scorm:viewreport', $context)) {
return array(true, $warnings);
}
if (!$open) {
$warnings['notopenyet'] = userdate($scorm->timeopen);
}
if ($closed) {
$warnings['expired'] = userdate($scorm->timeclose);
}
return array(false, $warnings);
}
// Scorm is available.
return array(true, $warnings);
}
/**
* Requires a SCORM package to be available for the current user.
*
* @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
* @throws moodle_exception
* @since Moodle 3.0
*/
function scorm_require_available($scorm, $checkviewreportcap = false, $context = null) {
list($available, $warnings) = scorm_get_availability_status($scorm, $checkviewreportcap, $context);
if (!$available) {
$reason = current(array_keys($warnings));
throw new moodle_exception($reason, 'scorm', '', $warnings[$reason]);
}
}

View File

@ -42,21 +42,37 @@ require_once($CFG->dirroot . '/mod/scorm/lib.php');
*/
class mod_scorm_external_testcase extends externallib_advanced_testcase {
/**
* Set up for every test
*/
public function setUp() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$this->course = $this->getDataGenerator()->create_course();
$this->scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $this->course->id));
$this->context = context_module::instance($this->scorm->cmid);
$this->cm = get_coursemodule_from_instance('scorm', $this->scorm->id);
// Create users.
$this->student = self::getDataGenerator()->create_user();
$this->teacher = self::getDataGenerator()->create_user();
// Users enrolments.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
}
/**
* Test view_scorm
*/
public function test_view_scorm() {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id));
$context = context_module::instance($scorm->cmid);
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
// Test invalid instance id.
try {
mod_scorm_external::view_scorm(0);
@ -69,20 +85,20 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_scorm_external::view_scorm($scorm->id);
mod_scorm_external::view_scorm($this->scorm->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
// Test user with full capabilities.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentrole->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_scorm_external::view_scorm($scorm->id);
$result = mod_scorm_external::view_scorm($this->scorm->id);
$result = external_api::clean_returnvalue(mod_scorm_external::view_scorm_returns(), $result);
$events = $sink->get_events();
@ -91,10 +107,98 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_scorm\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/scorm/view.php', array('id' => $cm->id));
$this->assertEquals($this->context, $event->get_context());
$moodleurl = new \moodle_url('/mod/scorm/view.php', array('id' => $this->cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
/**
* Test get scorm attempt count
*/
public function test_mod_scorm_get_scorm_attempt_count_own_empty() {
// Set to the student user.
self::setUser($this->student);
// Retrieve my attempts (should be 0).
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(0, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_own_with_complete() {
// Set to the student user.
self::setUser($this->student);
// Create attempts.
$scoes = scorm_get_scoes($this->scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed');
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(2, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_own_incomplete() {
// Set to the student user.
self::setUser($this->student);
// Create a complete attempt, and an incomplete attempt.
$scoes = scorm_get_scoes($this->scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 2, 'cmi.core.credit', '0');
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id, true);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(1, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_others_as_teacher() {
// As a teacher.
self::setUser($this->teacher);
// Create a completed attempt for student.
$scoes = scorm_get_scoes($this->scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
// I should be able to view the attempts for my students.
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(1, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_others_as_student() {
// Create a second student.
$student2 = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student2->id, $this->course->id, $this->studentrole->id, 'manual');
// As a student.
self::setUser($student2);
// I should not be able to view the attempts of another student.
$this->setExpectedException('required_capability_exception');
mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
}
public function test_mod_scorm_get_scorm_attempt_count_invalid_instanceid() {
// As student.
self::setUser($this->student);
// Test invalid instance id.
$this->setExpectedException('moodle_exception');
mod_scorm_external::get_scorm_attempt_count(0, $this->student->id);
}
public function test_mod_scorm_get_scorm_attempt_count_invalid_userid() {
// As student.
self::setUser($this->student);
$this->setExpectedException('moodle_exception');
mod_scorm_external::get_scorm_attempt_count($this->scorm->id, -1);
}
}

View File

@ -42,6 +42,31 @@ require_once($CFG->dirroot . '/mod/scorm/lib.php');
*/
class mod_scorm_lib_testcase extends externallib_advanced_testcase {
/**
* Set up for every test
*/
public function setUp() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$this->course = $this->getDataGenerator()->create_course();
$this->scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $this->course->id));
$this->context = context_module::instance($this->scorm->cmid);
$this->cm = get_coursemodule_from_instance('scorm', $this->scorm->id);
// Create users.
$this->student = self::getDataGenerator()->create_user();
$this->teacher = self::getDataGenerator()->create_user();
// Users enrolments.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
}
/**
* Test scorm_view
* @return void
@ -49,19 +74,10 @@ class mod_scorm_lib_testcase extends externallib_advanced_testcase {
public function test_scorm_view() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id));
$context = context_module::instance($scorm->cmid);
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
scorm_view($scorm, $course, $cm, $context);
scorm_view($this->scorm, $this->course, $this->cm, $this->context);
$events = $sink->get_events();
$this->assertCount(1, $events);
@ -69,11 +85,100 @@ class mod_scorm_lib_testcase extends externallib_advanced_testcase {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_scorm\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$url = new \moodle_url('/mod/scorm/view.php', array('id' => $cm->id));
$this->assertEquals($this->context, $event->get_context());
$url = new \moodle_url('/mod/scorm/view.php', array('id' => $this->cm->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
/**
* Test scorm_get_availability_status and scorm_require_available
* @return void
*/
public function test_scorm_check_and_require_available() {
global $DB;
// Set to the student user.
self::setUser($this->student);
// Usual case.
list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
$this->assertEquals(true, $status);
$this->assertCount(0, $warnings);
// SCORM not open.
$this->scorm->timeopen = time() + DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
$this->assertEquals(false, $status);
$this->assertCount(1, $warnings);
// SCORM closed.
$this->scorm->timeopen = 0;
$this->scorm->timeclose = time() - DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
$this->assertEquals(false, $status);
$this->assertCount(1, $warnings);
// SCORM not open and closed.
$this->scorm->timeopen = time() + DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
$this->assertEquals(false, $status);
$this->assertCount(2, $warnings);
// Now additional checkings with different parameters values.
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
$this->assertEquals(false, $status);
$this->assertCount(2, $warnings);
// SCORM not open.
$this->scorm->timeopen = time() + DAYSECS;
$this->scorm->timeclose = 0;
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
$this->assertEquals(false, $status);
$this->assertCount(1, $warnings);
// SCORM closed.
$this->scorm->timeopen = 0;
$this->scorm->timeclose = time() - DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
$this->assertEquals(false, $status);
$this->assertCount(1, $warnings);
// SCORM not open and closed.
$this->scorm->timeopen = time() + DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
$this->assertEquals(false, $status);
$this->assertCount(2, $warnings);
// As teacher now.
self::setUser($this->teacher);
// SCORM not open and closed.
$this->scorm->timeopen = time() + DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, false);
$this->assertEquals(false, $status);
$this->assertCount(2, $warnings);
// Now, we use the special capability.
// SCORM not open and closed.
$this->scorm->timeopen = time() + DAYSECS;
list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context);
$this->assertEquals(true, $status);
$this->assertCount(0, $warnings);
// Check exceptions does not broke anything.
scorm_require_available($this->scorm, true, $this->context);
// Now, expect exceptions.
$this->setExpectedException('moodle_exception', get_string("notopenyet", "scorm", userdate($this->scorm->timeopen)));
// Now as student other condition.
self::setUser($this->student);
$this->scorm->timeopen = 0;
$this->scorm->timeclose = time() - DAYSECS;
$this->setExpectedException('moodle_exception', get_string("expired", "scorm", userdate($this->scorm->timeclose)));
scorm_require_available($this->scorm, false);
}
}

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015051102; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2015051103; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2015050500; // Requires this Moodle version.
$plugin->component = 'mod_scorm'; // Full name of the plugin (used for diagnostics).
$plugin->cron = 300;

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015091000.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015091000.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.