Skip to content

Commit

Permalink
Create manually CB file when it is null (#25)
Browse files Browse the repository at this point in the history
In Moodle 3.9.1 downwards, file is not created inside method
$cb->create_content_from_file. It has to be created manually in
order to make sure it will work as expected.
  • Loading branch information
sarjona authored Jan 8, 2021
1 parent a91adf6 commit 801c531
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ private static function prepare_draft_file_from_hvp(stdClass $hvp, int $copy2cb)
$exportfilename = $hvp->slug . '-' . $hvp->id . '.h5p';
$fs = get_file_storage();
$exportfile = $fs->get_file($coursecontext->id, 'mod_hvp', 'exports', 0, '/', $exportfilename);
mtrace($exportfilename);
if (empty($exportfile)) {
// We need to generate the H5P file.
// There are two scenarios where hvp don't create the package file:
Expand All @@ -421,7 +420,7 @@ private static function prepare_draft_file_from_hvp(stdClass $hvp, int $copy2cb)
$course = $DB->get_record('course', ['id' => $hvp->course], '*', MUST_EXIST);
$COURSE = $course;
// Trigger a fake visualization will create the export file.
$view = new \mod_hvp\view_assets($hvp->cm, $course);
$view = new \mod_hvp\view_assets($hvp->cm, $course);
// Slug value can change on export.
$hvp->slug = $DB->get_field('hvp', 'slug', ['id' => $hvp->id]);
$exportfilename = $hvp->slug . '-' . $hvp->id . '.h5p';
Expand Down Expand Up @@ -455,6 +454,20 @@ private static function prepare_draft_file_from_hvp(stdClass $hvp, int $copy2cb)
$content->set_name($hvp->name);
}
$cbfile = $content->get_file();
if (is_null($cbfile)) {
// Moodle 3.9.1 doesn't create the file in $cb->create_content_from_file so it has to be created manually.
$filerecord = [
'component' => 'contentbank',
'filearea' => 'public',
'itemid' => $content->get_id(),
'author' => fullname($USER),
'filepath' => '/',
'filename' => $exportfile->get_filename(),
'contextid' => $coursecontext->id,
];
$file = $fs->create_file_from_storedfile($filerecord, $exportfile);
$cbfile = $content->get_file();
}

$activityfilerecord = [
'component' => 'user',
Expand Down

0 comments on commit 801c531

Please sign in to comment.