From 92b6187d8705e9ecb1948886b40119f061b36ace Mon Sep 17 00:00:00 2001 From: Guillermo Gomez Date: Mon, 22 Nov 2021 17:59:19 +1000 Subject: [PATCH] Issue #447 - Add additional validation when getting links for missing files --- classes/local/table/files_table.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/classes/local/table/files_table.php b/classes/local/table/files_table.php index 85ba3b31..82852849 100644 --- a/classes/local/table/files_table.php +++ b/classes/local/table/files_table.php @@ -40,7 +40,7 @@ class files_table extends \table_sql { public function __construct($uniqueid, $objectlocation) { parent::__construct($uniqueid); - $fields = 'f.*, ctx.instanceid'; + $fields = 'f.*, ctx.instanceid, ctx.contextlevel'; $from = '{files} f'; $from .= ' LEFT JOIN {tool_objectfs_objects} o on f.contenthash = o.contenthash'; $from .= ' LEFT JOIN {context} ctx ON f.contextid = ctx.id'; @@ -113,11 +113,19 @@ public function col_link(\stdClass $row) { global $DB; if (substr($row->component, 0, strlen('mod_')) === "mod_") { - list ($course, $cm) = get_course_and_cm_from_cmid($row->instanceid); - if (!empty($cm)) { - $url = new \moodle_url($cm->url); + switch ((int)$row->contextlevel) { + case CONTEXT_MODULE: + list ($course, $cm) = get_course_and_cm_from_cmid($row->instanceid); + if (!empty($cm)) { + $url = new \moodle_url($cm->url); + } + break; + case CONTEXT_COURSE: + $url = new \moodle_url("/course/view.php", ['id' => $row->instanceid]); + break; + default: + // Do nothing for now, in the future this can handle other contexts. } - } else if ($row->component === 'course') { if ($row->filearea === "legacy") { $params = ['contextid' => $row->contextid];