Skip to content
Snippets Groups Projects
Commit 83713d04 authored by Frode Petterson's avatar Frode Petterson
Browse files

Changed context for content files

Moved from course to course module.
Enriches access control and backup.
h5p/h5p-moodle-plugin#112
parent ad9f8065
No related branches found
No related tags found
No related merge requests found
...@@ -243,6 +243,7 @@ switch($action) { ...@@ -243,6 +243,7 @@ switch($action) {
// Save the valid file // Save the valid file
$file_id = $core->fs->saveFile($file, $contentid, $contextid); $file_id = $core->fs->saveFile($file, $contentid, $contextid);
// Track temporary files for later cleanup
$DB->insert_record_raw('hvp_tmpfiles', array( $DB->insert_record_raw('hvp_tmpfiles', array(
'id' => $file_id 'id' => $file_id
), false, false, true); ), false, false, true);
......
...@@ -69,13 +69,12 @@ class file_storage implements \H5PFileStorage { ...@@ -69,13 +69,12 @@ class file_storage implements \H5PFileStorage {
* What makes this content unique. * What makes this content unique.
*/ */
public function saveContent($source, $id) { public function saveContent($source, $id) {
global $COURSE;
// Remove any old content. // Remove any old content.
$this->deleteContent($id); $this->deleteContent($id);
// Contents are stored in a course context. // Contents are stored in a course context.
$context = \context_course::instance($COURSE->id); $cm = \get_coursemodule_from_instance('hvp', $id);
$context = \context_module::instance($cm->id);
$options = array( $options = array(
'contextid' => $context->id, 'contextid' => $context->id,
'component' => 'mod_hvp', 'component' => 'mod_hvp',
...@@ -95,9 +94,8 @@ class file_storage implements \H5PFileStorage { ...@@ -95,9 +94,8 @@ class file_storage implements \H5PFileStorage {
* Content identifier * Content identifier
*/ */
public function deleteContent($id) { public function deleteContent($id) {
global $COURSE; $cm = \get_coursemodule_from_instance('hvp', $id);
$context = \context_module::instance($cm->id);
$context = \context_course::instance($COURSE->id);
self::deleteFileTree($context->id, 'content', '/', $id); self::deleteFileTree($context->id, 'content', '/', $id);
} }
...@@ -133,9 +131,8 @@ class file_storage implements \H5PFileStorage { ...@@ -133,9 +131,8 @@ class file_storage implements \H5PFileStorage {
* Where the content folder will be saved * Where the content folder will be saved
*/ */
public function exportContent($id, $target) { public function exportContent($id, $target) {
global $COURSE; $cm = \get_coursemodule_from_instance('hvp', $id);
$context = \context_module::instance($cm->id);
$context = \context_course::instance($COURSE->id);
self::exportFileTree($target, $context->id, 'content', '/', $id); self::exportFileTree($target, $context->id, 'content', '/', $id);
} }
...@@ -343,8 +340,22 @@ class file_storage implements \H5PFileStorage { ...@@ -343,8 +340,22 @@ class file_storage implements \H5PFileStorage {
* Save files uploaded through the editor. * Save files uploaded through the editor.
* *
* @param \H5peditorFile $file * @param \H5peditorFile $file
* @param int $contentid
* @param \stdClass $context Course Context ID
*/ */
public function saveFile($file, $contentid, $contextid = null) { public function saveFile($file, $contentid, $contextid = null) {
global $COURSE;
if ($contentid !== 0) {
// Grab cm context
$cm = \get_coursemodule_from_instance('hvp', $contentid);
$context = \context_module::instance($cm->id);
$contextid = $context->id;
}
// Files not yet related to any activities are stored in a course context
// (These are temporary files and should not be part of backups.)
$record = array( $record = array(
'contextid' => $contextid, 'contextid' => $contextid,
'component' => 'mod_hvp', 'component' => 'mod_hvp',
...@@ -374,8 +385,6 @@ class file_storage implements \H5PFileStorage { ...@@ -374,8 +385,6 @@ class file_storage implements \H5PFileStorage {
* @param int $toid Target Content ID * @param int $toid Target Content ID
*/ */
public function cloneContentFile($file, $fromid, $toid) { public function cloneContentFile($file, $fromid, $toid) {
global $COURSE;
// Determine source file area and item id // Determine source file area and item id
$sourcefilearea = ($fromid === 'editor' ? $fromid : 'content'); $sourcefilearea = ($fromid === 'editor' ? $fromid : 'content');
$sourceitemid = ($fromid === 'editor' ? 0 : $fromid); $sourceitemid = ($fromid === 'editor' ? 0 : $fromid);
...@@ -391,8 +400,9 @@ class file_storage implements \H5PFileStorage { ...@@ -391,8 +400,9 @@ class file_storage implements \H5PFileStorage {
return; // File exists, no need to copy return; // File exists, no need to copy
} }
// Grab current context // Grab context for cm
$context = \context_course::instance($COURSE->id); $cm = \get_coursemodule_from_instance('hvp', $toid);
$context = \context_module::instance($cm->id);
// Create new file record // Create new file record
$record = array( $record = array(
...@@ -545,10 +555,9 @@ class file_storage implements \H5PFileStorage { ...@@ -545,10 +555,9 @@ class file_storage implements \H5PFileStorage {
* @param string $file path + name * @param string $file path + name
*/ */
private function getFile($filearea, $itemid, $file) { private function getFile($filearea, $itemid, $file) {
global $COURSE; // Grab cm context
$cm = \get_coursemodule_from_instance('hvp', $itemid);
// Grab current context $context = \context_module::instance($cm->id);
$context = \context_course::instance($COURSE->id);
// Load file // Load file
$fs = get_file_storage(); $fs = get_file_storage();
......
...@@ -149,5 +149,36 @@ function xmldb_hvp_upgrade($oldversion) { ...@@ -149,5 +149,36 @@ function xmldb_hvp_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2016051000, 'hvp'); upgrade_mod_savepoint(true, 2016051000, 'hvp');
} }
if ($oldversion < 2016080903) {
// Change context of activity files from COURSE to MODULE.
$filearea = 'content';
$component = 'mod_hvp';
// Find activity ID and correct context ID
$hvpsresult = $DB->get_records_sql(
"SELECT f.id AS fileid, f.itemid, c.id, f.filepath, f.filename, f.pathnamehash
FROM {files} f
JOIN {course_modules} cm ON f.itemid = cm.instance
JOIN {modules} md ON md.id = cm.module
JOIN {context} c ON c.instanceid = cm.id
WHERE md.name = 'hvp'
AND f.filearea = 'content'
AND c.contextlevel = " . CONTEXT_MODULE
);
foreach ($hvpsresult as $hvp) {
// Need to re-hash pathname after changing context
$pathnamehash = file_storage::get_pathname_hash($hvp->id, $component, $filearea, $hvp->itemid, $hvp->filepath, $hvp->filename);
// Update context ID and pathname hash for files
$DB->execute("UPDATE {files} SET contextid = {$hvp->id}, pathnamehash = '{$pathnamehash}' WHERE pathnamehash = '{$hvp->pathnamehash}'");
}
// Hvp savepoint reached.
upgrade_mod_savepoint(true, 2016080903, 'hvp');
}
return true; return true;
} }
...@@ -259,7 +259,7 @@ function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload ...@@ -259,7 +259,7 @@ function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload
break; break;
case 'content': case 'content':
if ($context->contextlevel != CONTEXT_COURSE) { if ($context->contextlevel != CONTEXT_MODULE) {
return false; // Invalid context. return false; // Invalid context.
} }
...@@ -272,13 +272,25 @@ function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload ...@@ -272,13 +272,25 @@ function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload
break; break;
case 'exports': case 'exports':
if ($context->contextlevel != CONTEXT_MODULE) {
return false; // Invalid context.
}
// Check permissions
if (!has_capability('mod/hvp:getexport', $context)) {
return false;
}
$itemid = 0;
break;
case 'editor': case 'editor':
if ($context->contextlevel != CONTEXT_COURSE) { if ($context->contextlevel != CONTEXT_COURSE) {
return false; // Invalid context. return false; // Invalid context.
} }
// Check permissions // Check permissions
if (!has_capability('mod/hvp:getexport', $context)) { if (!has_capability('mod/hvp:addinstance', $context)) {
return false; return false;
} }
......
Subproject commit d946f32b8128ca53b8bf18e5d83fc1bb8459ac12 Subproject commit c88c049438257b30e61801a940167d8805732ff0
...@@ -197,6 +197,13 @@ function hvp_add_editor_assets($id = null) { ...@@ -197,6 +197,13 @@ function hvp_add_editor_assets($id = null) {
if ($id !== null) { if ($id !== null) {
$settings['editor']['nodeVersionId'] = $id; $settings['editor']['nodeVersionId'] = $id;
// Find cm context
$cm = \get_coursemodule_from_instance('hvp', $id);
$context = \context_module::instance($cm->id);
// Override content URL
$settings['contents']['cid-'.$id]['contentUrl'] = "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/content/{$id}";
} }
$PAGE->requires->data_for_js('H5PIntegration', $settings, true); $PAGE->requires->data_for_js('H5PIntegration', $settings, true);
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016051306; $plugin->version = 2016080903;
$plugin->requires = 2013051403; $plugin->requires = 2013051403;
$plugin->cron = 0; $plugin->cron = 0;
$plugin->component = 'mod_hvp'; $plugin->component = 'mod_hvp';
......
...@@ -93,6 +93,9 @@ if (empty($export)) { ...@@ -93,6 +93,9 @@ if (empty($export)) {
$content['disable'] |= \H5PCore::DISABLE_DOWNLOAD; $content['disable'] |= \H5PCore::DISABLE_DOWNLOAD;
} }
// Find cm context
$context = \context_module::instance($cm->id);
// Add JavaScript settings for this content. // Add JavaScript settings for this content.
$cid = 'cid-' . $content['id']; $cid = 'cid-' . $content['id'];
$settings['contents'][$cid] = array( $settings['contents'][$cid] = array(
...@@ -103,6 +106,7 @@ $settings['contents'][$cid] = array( ...@@ -103,6 +106,7 @@ $settings['contents'][$cid] = array(
'title' => $content['title'], 'title' => $content['title'],
'disable' => $content['disable'], 'disable' => $content['disable'],
'url' => "{$CFG->httpswwwroot}/mod/hvp/view.php?id={$id}", 'url' => "{$CFG->httpswwwroot}/mod/hvp/view.php?id={$id}",
'contentUrl' => "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/content/" . $content['id'],
'contentUserData' => array( 'contentUserData' => array(
0 => \mod_hvp\content_user_data::load_pre_loaded_user_data($content['id']) 0 => \mod_hvp\content_user_data::load_pre_loaded_user_data($content['id'])
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment