Skip to content

Commit 83a0452

Browse files
authored
Check if contentbank repository is enabled or not (#30)
For being able to link content to the content bank, contentbank repository must be enabled. This commit adds some extra checks to copy file (instead of referencing it) when contentbank repository is disabled.
1 parent 515db9c commit 83a0452

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

classes/api.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,25 @@
2525

2626
defined('MOODLE_INTERNAL') || die;
2727

28-
require_once($CFG->dirroot. '/course/lib.php');
29-
require_once($CFG->dirroot. '/mod/hvp/locallib.php');
30-
require_once($CFG->dirroot . '/tag/lib.php');
28+
require_once($CFG->dirroot . '/course/lib.php');
3129
require_once($CFG->dirroot . '/lib/completionlib.php');
30+
require_once($CFG->dirroot . '/mod/hvp/locallib.php');
31+
require_once($CFG->dirroot . '/repository/lib.php');
32+
require_once($CFG->dirroot . '/tag/lib.php');
3233

33-
use context_course;
34-
use context_module;
35-
use context_user;
3634
use stdClass;
3735
use stored_file;
38-
use mod_h5pactivity\local\attempt;
39-
use tool_migratehvp2h5p\event\hvp_migrated;
40-
use moodle_exception;
36+
use context_user;
4137
use core_tag_tag;
38+
use context_course;
39+
use context_module;
4240
use completion_info;
43-
use core_competency\api as competencyapi;
41+
use moodle_exception;
4442
use core\output\notification;
45-
43+
use mod_h5pactivity\local\attempt;
44+
use core_competency\api as competencyapi;
45+
use repository;
46+
use tool_migratehvp2h5p\event\hvp_migrated;
4647
/**
4748
* Class containing helper methods for processing mod_hvp migrations.
4849
*
@@ -479,6 +480,7 @@ private static function prepare_draft_file_from_hvp(stdClass $hvp, int $copy2cb)
479480
'contextid' => $usercontext->id,
480481
];
481482

483+
$copyfile = true;
482484
if ($copy2cb == self::COPY2CBYESWITHLINK) {
483485
// The H5P file will be a reference to the content bank file.
484486
$cbfilerecord = [
@@ -492,10 +494,18 @@ private static function prepare_draft_file_from_hvp(stdClass $hvp, int $copy2cb)
492494
];
493495
$reference = \file_storage::pack_reference($cbfilerecord);
494496

495-
$repository = $DB->get_record('repository', ['type' => 'contentbank']);
496-
$file = $fs->create_file_from_reference($activityfilerecord, $repository->id, $reference);
497-
} else {
498-
// Apart from adding the file to the content bank, a copy for the activity has to be created too.
497+
// File will be linked only if contentbank repository is enabled (regardless if it's visible or hidden).
498+
$repo = repository::get_instances(['type' => 'contentbank', 'onlyvisible' => false]);
499+
if (!empty($repo)) {
500+
$repo = array_pop($repo);
501+
$file = $fs->create_file_from_reference($activityfilerecord, $repo->id, $reference);
502+
$copyfile = false;
503+
}
504+
}
505+
506+
if ($copyfile) {
507+
// No linked file exists because contentbank repository is not enabled at this context or $copy2cb setting is
508+
// not set to create a link, so a copy for the activity has to be created.
499509
$file = $fs->create_file_from_storedfile($activityfilerecord, $exportfile);
500510
}
501511
}

0 commit comments

Comments
 (0)