Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception if catscale can not be found #241

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions classes/catscale.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public static function return_catscale_object(int $catscaleid) {
if ($catscale = $cache->get($catscaleid)) {
return $catscale;
}
$catscale = $DB->get_record('local_catquiz_catscales', ['id' => $catscaleid]) ?: null;
$catscale = $DB->get_record('local_catquiz_catscales', ['id' => $catscaleid]);
if (! $catscale) {
throw new \Exception(sprintf('Could not find a scale with ID %s', $catscaleid));
}
$cache->set($catscaleid, $catscale);
return $catscale;
}
Expand Down Expand Up @@ -142,18 +145,16 @@ public static function return_catscaleids_and_links_for_testitemitem(
*/
public static function return_default_contextid_of_catscale(int $catscaleid) {

$catscale = self::return_catscale_object($catscaleid);
if (!empty($catscale) && isset($catscale->contextid)) {
return intval($catscale->contextid);
} else {
return null;
}

$catscale = self::return_catscale_object($catscaleid);
if (!$catscale) {
try {
$catscale = self::return_catscale_object($catscaleid);
if (isset($catscale->contextid)) {
return intval($catscale->contextid);
} else {
return null;
}
} catch (\Exception $e) {
return null;
}
return $catscale->contextid;
}

/**
Expand Down Expand Up @@ -567,18 +568,19 @@ private static function add_parentscales(int $scaleid, array $all): ?array {
*/
public static function get_link_to_catscale(int $catscaleid, $url = '/local/catquiz/manage_catscales.php') {

$catscale = self::return_catscale_object($catscaleid);
if (!empty($catscale->name)) {
$catscalename = $catscale->name;
try {
$catscale = self::return_catscale_object($catscaleid);
if (!empty($catscale->name)) {
$catscalename = $catscale->name;

$url = new moodle_url($url, ['scaleid' => $catscaleid], 'lcq_catscales');
$linktoscale = html_writer::link($url, $catscalename);
$url = new moodle_url($url, ['scaleid' => $catscaleid], 'lcq_catscales');
$linktoscale = html_writer::link($url, $catscalename);

return $linktoscale;
} else {
return $linktoscale;
}
} catch (\Exception $e) {
return get_string("deletedcatscale", "local_catquiz");
}

}

/**
Expand Down
Loading