Skip to content

Commit

Permalink
MDL-50837 mod_scorm: Fix availability checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva authored and stronk7 committed Nov 4, 2015
1 parent 7ca8c34 commit f1178eb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 37 deletions.
14 changes: 12 additions & 2 deletions mod/scorm/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,24 +930,34 @@ function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea
* @return bool false if file not found, does not return if found - just send the file
*/
function scorm_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
global $CFG;
global $CFG, $DB;

if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}

require_login($course, true, $cm);

$canmanageactivity = has_capability('moodle/course:manageactivities', $context);
$lifetime = null;

// Check SCORM availability.
if (!$canmanageactivity) {
$scorm = $DB->get_record('scorm', array('id' => $cm->instance), 'id, timeopen, timeclose', MUST_EXIST);
list($available, $warnings) = scorm_get_availability_status($scorm);
if (!$available) {
return false;
}
}

if ($filearea === 'content') {
$revision = (int)array_shift($args); // Prevents caching problems - ignored here.
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
// TODO: add any other access restrictions here if needed!

} else if ($filearea === 'package') {
if (!has_capability('moodle/course:manageactivities', $context)) {
if (!$canmanageactivity) {
return false;
}
$revision = (int)array_shift($args); // Prevents caching problems - ignored here.
Expand Down
11 changes: 2 additions & 9 deletions mod/scorm/loadSCO.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,8 @@

require_login($course, false, $cm, false); // Call require_login anyway to set up globals correctly.

// Check if scorm closed.
$timenow = time();
if ($scorm->timeclose != 0) {
if ($scorm->timeopen > $timenow) {
print_error('notopenyet', 'scorm', null, userdate($scorm->timeopen));
} else if ($timenow > $scorm->timeclose) {
print_error('expired', 'scorm', null, userdate($scorm->timeclose));
}
}
// Check if SCORM is available.
scorm_require_available($scorm);

$context = context_module::instance($cm->id);

Expand Down
60 changes: 59 additions & 1 deletion mod/scorm/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2015,4 +2015,62 @@ 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)
*/
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
*/
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]);
}

}
24 changes: 9 additions & 15 deletions mod/scorm/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,16 @@
die;
}

// Check if scorm closed.
$timenow = time();
if ($scorm->timeclose != 0) {
if ($scorm->timeopen > $timenow) {
echo $OUTPUT->header();
echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
echo $OUTPUT->footer();
die;
} else if ($timenow > $scorm->timeclose) {
echo $OUTPUT->header();
echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
echo $OUTPUT->footer();

die;
}
// Check if SCORM available.
list($available, $warnings) = scorm_get_availability_status($scorm);
if (!$available) {
$reason = current(array_keys($warnings));
echo $OUTPUT->header();
echo $OUTPUT->box(get_string($reason, "scorm", $warnings[$reason]), "generalbox boxaligncenter");
echo $OUTPUT->footer();
die;
}

// TOC processing
$scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
Expand Down
18 changes: 8 additions & 10 deletions mod/scorm/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,17 @@
}
echo $OUTPUT->box(format_module_intro('scorm', $scorm, $cm->id).$attemptstatus, 'generalbox boxaligncenter boxwidthwide', 'intro');

$scormopen = true;
$timenow = time();
if (!empty($scorm->timeopen) && $scorm->timeopen > $timenow) {
echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
$scormopen = false;
// Check if SCORM available.
list($available, $warnings) = scorm_get_availability_status($scorm);
if (!$available) {
$reason = current(array_keys($warnings));
echo $OUTPUT->box(get_string($reason, "scorm", $warnings[$reason]), "generalbox boxaligncenter");
}
if (!empty($scorm->timeclose) && $timenow > $scorm->timeclose) {
echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
$scormopen = false;
}
if ($scormopen && empty($launch)) {

if ($available && empty($launch)) {
scorm_view_display($USER, $scorm, 'view.php?id='.$cm->id, $cm);
}

if (!empty($forcejs)) {
echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
}
Expand Down

0 comments on commit f1178eb

Please sign in to comment.