Checked phpdoc @return types up to contextinfo.php

This commit is contained in:
PMKuipers 2024-12-27 16:25:21 +01:00
parent 04a1a8b049
commit 136284368c
8 changed files with 98 additions and 79 deletions

View file

@ -80,7 +80,7 @@ abstract class aggregator {
* @param mixed $configstr Configuration string for aggregator * @param mixed $configstr Configuration string for aggregator
* @throws moodle_exception If method is not found * @throws moodle_exception If method is not found
*/ */
public static function create($method, $configstr): self { public static function create($method, $configstr): object {
if (self::supported($method)) { if (self::supported($method)) {
$agclass = self::aggregator_name($method); $agclass = self::aggregator_name($method);
return new $agclass($configstr); return new $agclass($configstr);
@ -95,7 +95,7 @@ abstract class aggregator {
* @param mixed $method Aggregation method * @param mixed $method Aggregation method
* @param mixed $configstr Configuration string for aggregator * @param mixed $configstr Configuration string for aggregator
*/ */
public static function create_or_default($method, $configstr): self { public static function create_or_default($method, $configstr): object {
try { try {
return self::create($method, $configstr); return self::create($method, $configstr);
} catch (\moodle_exception $x) { } catch (\moodle_exception $x) {

View file

@ -106,31 +106,36 @@ class associationservice extends \external_api {
global $DB; global $DB;
$ctx = \context::instance_by_id($r->contextid); $ctx = \context::instance_by_id($r->contextid);
$ctxpath = array_reverse($ctx->get_parent_context_ids(true)); if (is_object($ctx)) {
if (count($ctxpath) > 1 && $ctxpath[0] == 1) { $ctxpath = array_reverse($ctx->get_parent_context_ids(true));
array_shift($ctxpath); if (count($ctxpath) > 1 && $ctxpath[0] == 1) {
array_shift($ctxpath);
}
$result = [
"id" => $r->id,
"name" => $r->name,
"idnumber" => $r->idnumber,
"description" => $r->description,
"visible" => $r->visible,
"context" => [
"name" => $ctx->get_context_name(false, false),
"shortname" => $ctx->get_context_name(false, true),
"path" => array_map(function($c) {
$cx = (object)\context::instance_by_id($c);
return $cx->get_context_name(false, false);
}, $ctxpath),
"shortpath" => array_map(function($c) {
$cx = (object)\context::instance_by_id($c);
return $cx->get_context_name(false, true);
}, $ctxpath),
],
];
return $result;
} else {
throw new \moodle_exception("");
} }
$result = [
"id" => $r->id,
"name" => $r->name,
"idnumber" => $r->idnumber,
"description" => $r->description,
"visible" => $r->visible,
"context" => [
"name" => $ctx->get_context_name(false, false),
"shortname" => $ctx->get_context_name(false, true),
"path" => array_map(function($c) {
return \context::instance_by_id($c)->get_context_name(false, false);
}, $ctxpath),
"shortpath" => array_map(function($c) {
return \context::instance_by_id($c)->get_context_name(false, true);
}, $ctxpath),
],
];
return $result;
} }
/** /**

View file

@ -397,7 +397,7 @@ class badgeinfo {
* Gets the module instance from the database and returns it. * Gets the module instance from the database and returns it.
* If no module instance exists this function returns false. * If no module instance exists this function returns false.
* @param int $cmid Course module id * @param int $cmid Course module id
* @return stdClass|bool * @return object|null
*/ */
private static function get_mod_instance($cmid) { private static function get_mod_instance($cmid) {
global $DB; global $DB;
@ -445,9 +445,10 @@ class badgeinfo {
if ($crit->criteriatype == BADGE_CRITERIA_TYPE_ACTIVITY) { if ($crit->criteriatype == BADGE_CRITERIA_TYPE_ACTIVITY) {
foreach ($crit->params as $p) { foreach ($crit->params as $p) {
$mod = self::get_mod_instance($p["module"]); $mod = self::get_mod_instance($p["module"]);
if (!$mod) { if (!is_object($mod)) {
$title = get_string('error:nosuchmod', 'badges'); $title = get_string('error:nosuchmod', 'badges');
$description = get_string('error:nosuchmod', 'badges'); $description = get_string('error:nosuchmod', 'badges');
continue;
} else { } else {
$title = \html_writer::tag('b', '"' . get_string('modulename', $mod->modname) . ' - ' . $mod->name . '"');; $title = \html_writer::tag('b', '"' . get_string('modulename', $mod->modname) . ' - ' . $mod->name . '"');;
$description = \html_writer::tag('b', '"' . get_string('modulename', $mod->modname) . ' - ' . $mod->name . '"'); $description = \html_writer::tag('b', '"' . get_string('modulename', $mod->modname) . ' - ' . $mod->name . '"');

View file

@ -103,7 +103,11 @@ class cascadecohortsync {
$groupdata->name = $groupname; $groupdata->name = $groupname;
$groupid = groups_create_group($groupdata); $groupid = groups_create_group($groupdata);
return $groupid; if (is_int($groupid)) {
return $groupid;
} else {
return 0;
}
} }
/** /**

View file

@ -109,7 +109,7 @@ class completionscanner {
'itemmodule' => $this->cm->modname, 'itemmodule' => $this->cm->modname,
'iteminstance' => $this->cm->instance, 'iteminstance' => $this->cm->instance,
'courseid' => $this->courseid]); 'courseid' => $this->courseid]);
if ($gi !== false) { if (is_object($gi)) {
/* Grade none items should not be relevant. /* Grade none items should not be relevant.
Note that the grade status is probably only relevant if the item Note that the grade status is probably only relevant if the item
has not yet received a completion, but has been submitted. has not yet received a completion, but has been submitted.

View file

@ -78,11 +78,11 @@ class contextinfo {
public function path($short=false) { public function path($short=false) {
if ($short) { if ($short) {
return array_map(function($c) { return array_map(function($c) {
return \context::instance_by_id($c)->get_context_name(false, true); return ((object)\context::instance_by_id($c))->get_context_name(false, true);
}, $this->ctxpath); }, $this->ctxpath);
} else { } else {
return array_map(function($c) { return array_map(function($c) {
return \context::instance_by_id($c)->get_context_name(false, false); return ((object)\context::instance_by_id($c))->get_context_name(false, false);
}, $this->ctxpath); }, $this->ctxpath);
} }
} }
@ -113,7 +113,13 @@ class contextinfo {
if ($contextid <= 1) { if ($contextid <= 1) {
$contextid = 1; $contextid = 1;
} }
return \context::instance_by_id($contextid);
$ctx = \context::instance_by_id($contextid);
if (is_object($ctx)) {
return $ctx;
} else {
return \context_system::instance();
}
} }
} }

View file

@ -50,7 +50,7 @@ class studyplan_editform extends formbase {
* Translate parameters into customdata. * Translate parameters into customdata.
* *
* @param object $params The parameters for form initialization * @param object $params The parameters for form initialization
* @return array Form data based on parameters * @return stdClass Form data based on parameters
*/ */
public static function init_customdata(object $params) { public static function init_customdata(object $params) {
$customdata = new stdClass; $customdata = new stdClass;
@ -87,7 +87,7 @@ class studyplan_editform extends formbase {
* Also validate parameters and access permissions here * Also validate parameters and access permissions here
* *
* @param object $customdata The parameters for form initialization * @param object $customdata The parameters for form initialization
* @return array Form data based on parameters * @return object Form data based on parameters
*/ */
public function init_formdata(object $customdata) { public function init_formdata(object $customdata) {
global $DB; global $DB;

91
lib.php
View file

@ -318,56 +318,57 @@ function local_treestudyplan_send_invite($inviteid) {
$noreply = 'noreply@' . get_host_from_url($CFG->wwwroot); $noreply = 'noreply@' . get_host_from_url($CFG->wwwroot);
$mailer = get_mailer(); $mailer = get_mailer();
$mailer->setFrom($noreply, "{$USER->firstname} {$USER->lastname}"); if ($mailer != null ) {
$mailer->addAddress($invite->email, $invite->name); $mailer->setFrom($noreply, "{$USER->firstname} {$USER->lastname}");
$mailer->addReplyTo($USER->email, "{$USER->firstname} {$USER->lastname}"); $mailer->addAddress($invite->email, $invite->name);
$mailer->addReplyTo($USER->email, "{$USER->firstname} {$USER->lastname}");
$invitehref = $CFG->wwwroot."/local/treestudyplan/invited.php?key={$invite->invitekey}"; $invitehref = $CFG->wwwroot."/local/treestudyplan/invited.php?key={$invite->invitekey}";
$data = [ 'permissions' => '', $data = [ 'permissions' => '',
'invitee' => $invite->name, 'invitee' => $invite->name,
'sender' => "{$USER->firstname} {$USER->lastname}", 'sender' => "{$USER->firstname} {$USER->lastname}",
'link' => $invitehref]; 'link' => $invitehref];
if ($invite->allow_details || $invite->allow_calendar || $invite->allow_badges) { if ($invite->allow_details || $invite->allow_calendar || $invite->allow_badges) {
$data['permissions'] = get_string('invite_mail_permissions', 'local_treestudyplan'); $data['permissions'] = get_string('invite_mail_permissions', 'local_treestudyplan');
$data['permissions'] .= "<ul>\n"; $data['permissions'] .= "<ul>\n";
if ($invite->allow_details) { if ($invite->allow_details) {
$data['permissions'] .= "<li>".get_string('invite_allow_details', 'local_treestudyplan')."</li>\n"; $data['permissions'] .= "<li>".get_string('invite_allow_details', 'local_treestudyplan')."</li>\n";
} }
if ($invite->allow_calendar) { if ($invite->allow_calendar) {
$data['permissions'] .= "<li>".get_string('invite_allow_calendar', 'local_treestudyplan')."</li>\n"; $data['permissions'] .= "<li>".get_string('invite_allow_calendar', 'local_treestudyplan')."</li>\n";
} }
if ($invite->allow_badges) { if ($invite->allow_badges) {
$data['permissions'] .= "<li>".get_string('invite_allow_badges', 'local_treestudyplan')."</li>\n"; $data['permissions'] .= "<li>".get_string('invite_allow_badges', 'local_treestudyplan')."</li>\n";
}
$data['permissions'] .= "</ul></p>\n";
} }
$data['permissions'] .= "</ul></p>\n"; $body = get_string('invite_mail_text', 'local_treestudyplan', $data);
$subject = get_string('invite_mail_subject', 'local_treestudyplan', $data);
$html = "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>.
<html xmlns='http://www.w3.org/1999/xhtml'>.
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<title>{$subject}</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0'/>
</head>
<body>
{$body}
</body>
</html>";
$mailer->isHTML(true);
$mailer->Subject = $subject;
$mailer->Body = $html;
$mailer->AltBody = strip_tags($body);
$mailer->send();
} }
$body = get_string('invite_mail_text', 'local_treestudyplan', $data);
$subject = get_string('invite_mail_subject', 'local_treestudyplan', $data);
$html = "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>.
<html xmlns='http://www.w3.org/1999/xhtml'>.
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<title>{$subject}</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0'/>
</head>
<body>
{$body}
</body>
</html>";
$mailer->isHTML(true);
$mailer->Subject = $subject;
$mailer->Body = $html;
$mailer->AltBody = strip_tags($body);
$mailer->send();
} }
/** /**
@ -469,6 +470,7 @@ function local_treestudyplan_pluginfile(
} }
// We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering. // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
send_stored_file($file, 24 * 60 * 60, 0, $forcedownload, $options); send_stored_file($file, 24 * 60 * 60, 0, $forcedownload, $options);
return true;
} else { } else {
return false; return false;
} }
@ -497,6 +499,7 @@ function local_treestudyplan_pluginfile(
} }
// We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering. // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
send_stored_file($file, 24 * 60 * 60, 0, $forcedownload, $options); send_stored_file($file, 24 * 60 * 60, 0, $forcedownload, $options);
return true;
} else { } else {
return false; return false;
} }