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

Use coursemodule property for content files

This fixes content file handling issues when creating new content.
parent 2b9f59be
No related branches found
No related tags found
No related merge requests found
...@@ -378,9 +378,9 @@ class file_storage implements \H5PFileStorage { ...@@ -378,9 +378,9 @@ class file_storage implements \H5PFileStorage {
* *
* @param string $file path + name * @param string $file path + name
* @param string|int $fromid Content ID or 'editor' string * @param string|int $fromid Content ID or 'editor' string
* @param int $toid Target Content ID * @param stdClass $tocontent Target Content
*/ */
public function cloneContentFile($file, $fromid, $toid) { public function cloneContentFile($file, $fromid, $tocontent) {
// 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);
...@@ -392,20 +392,19 @@ class file_storage implements \H5PFileStorage { ...@@ -392,20 +392,19 @@ class file_storage implements \H5PFileStorage {
} }
// Check to make sure source doesn't exist already // Check to make sure source doesn't exist already
if ($this->getFile('content', $toid, $file) !== false) { if ($this->getFile('content', $tocontent, $file) !== false) {
return; // File exists, no need to copy return; // File exists, no need to copy
} }
// Grab context for cm // Grab context for CM
$cm = \get_coursemodule_from_instance('hvp', $toid); $context = \context_module::instance($tocontent->coursemodule);
$context = \context_module::instance($cm->id);
// Create new file record // Create new file record
$record = array( $record = array(
'contextid' => $context->id, 'contextid' => $context->id,
'component' => 'mod_hvp', 'component' => 'mod_hvp',
'filearea' => 'content', 'filearea' => 'content',
'itemid' => $toid, 'itemid' => $tocontent->id,
'filepath' => $this->getFilepath($file), 'filepath' => $this->getFilepath($file),
'filename' => $this->getFilename($file) 'filename' => $this->getFilename($file)
); );
...@@ -418,11 +417,11 @@ class file_storage implements \H5PFileStorage { ...@@ -418,11 +417,11 @@ class file_storage implements \H5PFileStorage {
* Used when saving content. * Used when saving content.
* *
* @param string $file path + name * @param string $file path + name
* @param int $contentid * @param stdClass $content
* @return string|int File ID or NULL if not found * @return string|int File ID or NULL if not found
*/ */
public function getContentFile($file, $contentid) { public function getContentFile($file, $content) {
$file = $this->getFile('content', $contentid, $file); $file = $this->getFile('content', $content, $file);
return ($file === false ? null : $file->get_id()); return ($file === false ? null : $file->get_id());
} }
...@@ -431,10 +430,10 @@ class file_storage implements \H5PFileStorage { ...@@ -431,10 +430,10 @@ class file_storage implements \H5PFileStorage {
* Used when saving content. * Used when saving content.
* *
* @param string $file path + name * @param string $file path + name
* @param int $contentid * @param stdClass $content
*/ */
public function removeContentFile($file, $contentid) { public function removeContentFile($file, $content) {
$file = $this->getFile('content', $contentid, $file); $file = $this->getFile('content', $content, $file);
if ($file !== false) { if ($file !== false) {
$file->delete(); $file->delete();
} }
...@@ -547,7 +546,7 @@ class file_storage implements \H5PFileStorage { ...@@ -547,7 +546,7 @@ class file_storage implements \H5PFileStorage {
* Help make it easy to load content files. * Help make it easy to load content files.
* *
* @param string $filearea * @param string $filearea
* @param int $itemid * @param int|stdClass $itemid
* @param string $file path + name * @param string $file path + name
*/ */
private function getFile($filearea, $itemid, $file) { private function getFile($filearea, $itemid, $file) {
...@@ -557,8 +556,13 @@ class file_storage implements \H5PFileStorage { ...@@ -557,8 +556,13 @@ class file_storage implements \H5PFileStorage {
// Use Course context // Use Course context
$context = \context_course::instance($COURSE->id); $context = \context_course::instance($COURSE->id);
} }
elseif (is_object($itemid)) {
// Grab CM context from item
$context = \context_module::instance($itemid->coursemodule);
$itemid = $itemid->id;
}
else { else {
// Use CM context // Use item ID to find CM context
$cm = \get_coursemodule_from_instance('hvp', $itemid); $cm = \get_coursemodule_from_instance('hvp', $itemid);
$context = \context_module::instance($cm->id); $context = \context_module::instance($cm->id);
} }
......
Subproject commit 23f20f7bafd8e8c9ddafe5a6768c42f01975b0e7 Subproject commit d1ad5947b82ccbe2c3badad8d804f4d44fdc8efa
...@@ -159,7 +159,7 @@ function hvp_save_content($hvp) { ...@@ -159,7 +159,7 @@ function hvp_save_content($hvp) {
$params = json_decode($hvp->params); $params = json_decode($hvp->params);
// Move any uploaded images or files. Determine content dependencies. // Move any uploaded images or files. Determine content dependencies.
$editor->processParameters($hvp->id, $hvp->library, $params, isset($oldlib) ? $oldlib : NULL, isset($oldparams) ? $oldparams : NULL); $editor->processParameters($hvp, $hvp->library, $params, isset($oldlib) ? $oldlib : NULL, isset($oldparams) ? $oldparams : NULL);
} }
return $hvp->id; return $hvp->id;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment