Checked all phpdoc @return types and fixed some other type errors (added some checks for edge cases)

This commit is contained in:
PMKuipers 2024-12-27 23:04:46 +01:00
parent a0b120df1b
commit 3bc3a1dbec
30 changed files with 184 additions and 154 deletions

View file

@ -93,7 +93,7 @@ class completionscanner {
* @param completion_criteria $crit Criteria to check for * @param completion_criteria $crit Criteria to check for
* @param stdClass $course Course DB record * @param stdClass $course Course DB record
*/ */
public function __construct(completion_criteria $crit, $course) { public function __construct($crit, $course) {
$this->courseid = $course->id; $this->courseid = $course->id;
$this->course = $course; $this->course = $course;
$this->modinfo = get_fast_modinfo($course); $this->modinfo = get_fast_modinfo($course);

View file

@ -324,6 +324,7 @@ class coursecompetencyinfo {
$competencies = $this->course_competencies(); $competencies = $this->course_competencies();
$progress = 0; $progress = 0;
$cis = []; $cis = [];
foreach ($competencies as $c) { foreach ($competencies as $c) {
$ci = $this->competencyinfo_model($c, $userid); $ci = $this->competencyinfo_model($c, $userid);
@ -376,6 +377,7 @@ class coursecompetencyinfo {
$dcount = 0; $dcount = 0;
$dprogress = 0; $dprogress = 0;
$children = []; $children = [];
$points = 0;
foreach ($dids as $did) { foreach ($dids as $did) {
$cc = new competency($did); $cc = new competency($did);
$cci = $this->competencyinfo_model($cc, $userid); $cci = $this->competencyinfo_model($cc, $userid);
@ -528,7 +530,7 @@ class coursecompetencyinfo {
* @param \core_competency\competency $competency * @param \core_competency\competency $competency
* @param int $userid * @param int $userid
* *
* @return stdClass * @return object
* *
*/ */
public function proficiency($competency, $userid) { public function proficiency($competency, $userid) {
@ -559,7 +561,7 @@ class coursecompetencyinfo {
* @param \core_competency\competency $competency * @param \core_competency\competency $competency
* @param int $userid * @param int $userid
* *
* @return stdClass * @return string
* *
*/ */
public function retrievefeedback($competency, $userid) { public function retrievefeedback($competency, $userid) {
@ -576,7 +578,7 @@ class coursecompetencyinfo {
break; break;
} }
} }
return null; return "";
} }
/** /**
@ -639,7 +641,7 @@ class coursecompetencyinfo {
return boolval($conditions["competencies"][$competency->get("id")]["required"]); return boolval($conditions["competencies"][$competency->get("id")]["required"]);
} }
} }
return(false); return false;
} }
} }

View file

@ -113,7 +113,7 @@ class courseinfo {
/** /**
* Check if current user is teacher in this course * Check if current user is teacher in this course
* @return book * @return bool
*/ */
protected function am_teacher(): bool { protected function am_teacher(): bool {
global $USER; global $USER;
@ -216,12 +216,12 @@ class courseinfo {
public function displayname() { public function displayname() {
$displayfield = get_config("local_treestudyplan", "display_field"); $displayfield = get_config("local_treestudyplan", "display_field");
if ($displayfield == "idnumber") { if ($displayfield == "idnumber") {
$idnumber = trim(preg_replace("/\s+/u", " ", $this->course->idnumber)); $idnumber = trim((string)(preg_replace("/\s+/u", " ", $this->course->idnumber)));
if (strlen($idnumber) > 0) { if (strlen($idnumber) > 0) {
return $this->course->idnumber; return $this->course->idnumber;
} }
} else if ($displayfield == "fullname") { } else if ($displayfield == "fullname") {
$fullname = trim(preg_replace("/\s+/u", " ", $this->course->fullname)); $fullname = trim((string)(preg_replace("/\s+/u", " ", $this->course->fullname)));
if (strlen($fullname) > 0) { if (strlen($fullname) > 0) {
return $fullname; return $fullname;
} }
@ -232,7 +232,7 @@ class courseinfo {
$datas = $handler->get_instance_data($this->course->id); $datas = $handler->get_instance_data($this->course->id);
foreach ($datas as $data) { foreach ($datas as $data) {
if ($data->get_field()->get('shortname') == $fieldname) { if ($data->get_field()->get('shortname') == $fieldname) {
$value = trim(preg_replace("/\s+/u", " ", $data->get_value())); $value = trim((string)(preg_replace("/\s+/u", " ", $data->get_value())));
if (strlen($value) > 0) { if (strlen($value) > 0) {
return $value; return $value;
} }
@ -577,7 +577,7 @@ class courseinfo {
); );
return [$value, "textarea", $this->course()->summary]; return [$value, "textarea", $this->course()->summary];
} else if ($fieldname == "idnumber") { } else if ($fieldname == "idnumber") {
$idnumber = trim(preg_replace("/\s+/u", " ", $this->course->idnumber)); $idnumber = trim((string)(preg_replace("/\s+/u", " ", $this->course->idnumber)));
return [$idnumber, "text", $this->course->idnumber]; return [$idnumber, "text", $this->course->idnumber];
} else if ($fieldname == "contacts") { } else if ($fieldname == "contacts") {
$cle = new \core_course_list_element($this->course()); $cle = new \core_course_list_element($this->course());

View file

@ -117,7 +117,7 @@ class courseservice extends \external_api {
if (count($intersect) == 0) { if (count($intersect) == 0) {
// Double check permissions according to the moodle capability system. // Double check permissions according to the moodle capability system.
$ctx = \context::instance_by_id($r->id); $ctx = \context::instance_by_id($r->id);
if (has_capability($capability, $ctx, $userid)) { if (is_object($ctx) && has_capability($capability, $ctx, $userid)) {
// Get the category, and double check if the category is visible to the current user. // Get the category, and double check if the category is visible to the current user.
// Just in case it is a hidden category and the user does not have the viewhidden permission. // Just in case it is a hidden category and the user does not have the viewhidden permission.
$cat = \core_course_category::get($r->instanceid, \IGNORE_MISSING, false, $userid); $cat = \core_course_category::get($r->instanceid, \IGNORE_MISSING, false, $userid);
@ -184,7 +184,7 @@ class courseservice extends \external_api {
if (count($intersect) == 0) { if (count($intersect) == 0) {
// Double check permissions according to the moodle capability system. // Double check permissions according to the moodle capability system.
$ctx = \context::instance_by_id($r->id); $ctx = \context::instance_by_id($r->id);
if (has_capability($capability, $ctx, $userid)) { if (is_object($ctx) && has_capability($capability, $ctx, $userid)) {
// Get the category, and double check if the category is visible to the current user. // Get the category, and double check if the category is visible to the current user.
// Just in case it is a hidden category and the user does not have the viewhidden permission. // Just in case it is a hidden category and the user does not have the viewhidden permission.
$cat = \core_course_category::get($r->instanceid, \IGNORE_MISSING, false, $userid); $cat = \core_course_category::get($r->instanceid, \IGNORE_MISSING, false, $userid);
@ -265,7 +265,7 @@ class courseservice extends \external_api {
$children = self::user_tops(); $children = self::user_tops();
} else if ($context->contextlevel == \CONTEXT_COURSECAT) { } else if ($context->contextlevel == \CONTEXT_COURSECAT) {
$cat = \core_course_category::get($context->instanceid, \MUST_EXIST, true); $cat = \core_course_category::get($context->instanceid, \MUST_EXIST, true);
if ($cat->is_uservisible()) { if (is_object($cat) && $cat->is_uservisible()) {
$children = [$cat]; $children = [$cat];
} else { } else {
$ci = new contextinfo($context); $ci = new contextinfo($context);
@ -309,10 +309,13 @@ class courseservice extends \external_api {
/** /**
* Get category info by id * Get category info by id
* @param mixed $id * @param mixed $id
* @return array * @return \core_course_category
*/ */
public static function get_category($id) { public static function get_category($id) {
$cat = \core_course_category::get($id); $cat = \core_course_category::get($id);
if (!is_object($cat)) {
throw new moodle_exception("unknownerror");
}
return static::map_category($cat); return static::map_category($cat);
} }
@ -439,7 +442,7 @@ class courseservice extends \external_api {
$contextcounts[$r->context_id] = $r->num; $contextcounts[$r->context_id] = $r->num;
// Add any of the categories containing studyplans to the list. // Add any of the categories containing studyplans to the list.
$ctx = \context::instance_by_id($r->context_id); $ctx = \context::instance_by_id($r->context_id);
if (has_capability($capability, $ctx) && !in_array($r->context_id, $contextids)) { if (is_object($ctx) && has_capability($capability, $ctx) && !in_array($r->context_id, $contextids)) {
$insertctxs[] = $ctx; $insertctxs[] = $ctx;
} }
} }
@ -454,7 +457,7 @@ class courseservice extends \external_api {
// Get the context. // Get the context.
$refctx = \context::instance_by_id($refctxid); $refctx = \context::instance_by_id($refctxid);
// Double check permissions. // Double check permissions.
if (has_capability($capability, $refctx)) { if (is_object($refctx) && has_capability($capability, $refctx)) {
$insertctxs[] = $refctx; $insertctxs[] = $refctx;
} }
} catch (\dml_missing_record_exception $x) { } catch (\dml_missing_record_exception $x) {
@ -489,19 +492,24 @@ class courseservice extends \external_api {
foreach ($contextids as $ctxid) { foreach ($contextids as $ctxid) {
try { try {
$ctx = \context::instance_by_id($ctxid); $ctx = \context::instance_by_id($ctxid);
if ($ctx->contextlevel == CONTEXT_SYSTEM) { if (is_object($ctx) && $ctx->contextlevel == CONTEXT_SYSTEM) {
$cat = \core_course_category::top(); $cat = \core_course_category::top();
} else if ($ctx->contextlevel == CONTEXT_COURSECAT) { } else if (is_object($ctx) && $ctx->contextlevel == CONTEXT_COURSECAT) {
$cat = \core_course_category::get($ctx->instanceid, \MUST_EXIST, false); $cat = \core_course_category::get($ctx->instanceid, \MUST_EXIST, false);
} else {
$cat = null;
} }
$cats[] = $cat;
// In edit mode, also include direct children of the currently selected context. if ($cat != null) {
if ($operation == "edit" && $ctxid == $refctxid) { $cats[] = $cat;
// Include direct children for navigation purposes. // In edit mode, also include direct children of the currently selected context.
foreach ($cat->get_children() as $ccat) { if ($operation == "edit" && $ctxid == $refctxid) {
$ccatctx = \context_coursecat::instance($ccat->id); // Include direct children for navigation purposes.
if (!in_array($ccatctx->id, $contextids)) { foreach ($cat->get_children() as $ccat) {
$cats[] = $ccat; $ccatctx = \context_coursecat::instance($ccat->id);
if (!in_array($ccatctx->id, $contextids)) {
$cats[] = $ccat;
}
} }
} }
} }
@ -566,6 +574,10 @@ class courseservice extends \external_api {
// Retrieve grade item. // Retrieve grade item.
$gi = \grade_item::fetch(["id" => $gradeitemid]); $gi = \grade_item::fetch(["id" => $gradeitemid]);
if (!is_object($gi)) {
throw new moodle_exception("unknownerror");
}
// Validate course is linked to studyplan. // Validate course is linked to studyplan.
$courseid = $gi->courseid; $courseid = $gi->courseid;
if (!$o->course_linked($courseid)) { if (!$o->course_linked($courseid)) {

View file

@ -60,7 +60,7 @@ abstract class formbase extends \moodleform {
* Also validate parameters and access permissions here * Also validate parameters and access permissions here
* *
* @param object $customdata The form customdata built on form initialization in self::init_customdata(...) * @param object $customdata The form customdata built on form initialization in self::init_customdata(...)
* @return array Form data based on parameters * @return object Form data based on parameters
*/ */
abstract public function init_formdata(object $customdata); abstract public function init_formdata(object $customdata);
@ -79,14 +79,14 @@ abstract class formbase extends \moodleform {
/** /**
* Process the submission and perform necessary actions * Process the submission and perform necessary actions
* @param object $entry The processed form data * @param object $entry The processed form data
* @return object|array Data to pass to receiver if submission successful * @return mixed Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason. * @throws \moodle_exception if an error must be given for a specific reason.
*/ */
abstract protected function process_submitted_data(object $entry); abstract protected function process_submitted_data(object $entry);
/** /**
* Process the submission and perform necessary actions * Process the submission and perform necessary actions
* @return object|array Data to pass to receiver if submission successful * @return mixed Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason. * @throws \moodle_exception if an error must be given for a specific reason.
*/ */
public function process_submission() { public function process_submission() {

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 stdClass Form data based on parameters * @return object 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;
@ -58,7 +58,6 @@ class studyplan_editform extends formbase {
if ($customdata->create) { if ($customdata->create) {
$customdata->context = \context::instance_by_id($params->contextid); $customdata->context = \context::instance_by_id($params->contextid);
} else { } else {
$customdata->plan = studyplan::find_by_id($params->studyplan_id); $customdata->plan = studyplan::find_by_id($params->studyplan_id);
$customdata->context = $customdata->plan->context(); $customdata->context = $customdata->plan->context();
$customdata->simplemodel = $customdata->plan->simple_model(); $customdata->simplemodel = $customdata->plan->simple_model();
@ -314,7 +313,7 @@ class studyplan_editform extends formbase {
/** /**
* Process the submitted data and perform necessary actions * Process the submitted data and perform necessary actions
* @param object $entry The processed form data; * @param object $entry The processed form data;
* @return bool false if submission not successful * @return mixed Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason. * @throws \moodle_exception if an error must be given for a specific reason.
*/ */
protected function process_submitted_data($entry) { protected function process_submitted_data($entry) {

View file

@ -48,7 +48,7 @@ class studyplan_fromtemplateform 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 object 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;
@ -62,7 +62,7 @@ class studyplan_fromtemplateform 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;
@ -166,7 +166,7 @@ class studyplan_fromtemplateform extends formbase {
/** /**
* Process the submitted data and perform necessary actions * Process the submitted data and perform necessary actions
* @param object $entry The processed form data; * @param object $entry The processed form data;
* @return bool false if submission not successful * @return mixed Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason. * @throws \moodle_exception if an error must be given for a specific reason.
*/ */
protected function process_submitted_data($entry) { protected function process_submitted_data($entry) {

View file

@ -47,7 +47,7 @@ class studyplanpage_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 object 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;
@ -84,7 +84,7 @@ class studyplanpage_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;
@ -199,7 +199,7 @@ class studyplanpage_editform extends formbase {
/** /**
* Process the submitted data and perform necessary actions * Process the submitted data and perform necessary actions
* @param object $entry The processed form data; * @param object $entry The processed form data;
* @return bool false if submission not successful * @return mixed Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason. * @throws \moodle_exception if an error must be given for a specific reason.
*/ */
protected function process_submitted_data($entry) { protected function process_submitted_data($entry) {

View file

@ -39,7 +39,6 @@ class text_integer extends MoodleQuickForm_text {
* @param object $renderer An HTML_QuickForm_Renderer object * @param object $renderer An HTML_QuickForm_Renderer object
* @param bool $required Whether an element is required * @param bool $required Whether an element is required
* @param string $error An error message associated with an element * @param string $error An error message associated with an element
* @return void
*/ */
public function accept(&$renderer, $required = false, $error = null) { public function accept(&$renderer, $required = false, $error = null) {
global $OUTPUT; global $OUTPUT;

View file

@ -153,7 +153,7 @@ class gradeinfo {
*/ */
public static function get_coursecontext_by_id($id): \context_course { public static function get_coursecontext_by_id($id): \context_course {
$gi = grade_item::fetch(["id" => $id]); $gi = grade_item::fetch(["id" => $id]);
if (!$gi || course_module_instance_pending_deletion($gi->courseid, $gi->itemmodule, $gi->iteminstance)) { if (!is_object($gi) || course_module_instance_pending_deletion($gi->courseid, $gi->itemmodule, $gi->iteminstance)) {
throw new \InvalidArgumentException ("Grade {$id} not found in database"); throw new \InvalidArgumentException ("Grade {$id} not found in database");
} }
return \context_course::instance($gi->courseid);; return \context_course::instance($gi->courseid);;
@ -169,7 +169,7 @@ class gradeinfo {
$this->studyitem = $studyitem; $this->studyitem = $studyitem;
$gi = grade_item::fetch(["id" => $id]); $gi = grade_item::fetch(["id" => $id]);
if (!$gi || course_module_instance_pending_deletion($gi->courseid, $gi->itemmodule, $gi->iteminstance)) { if (!is_object($gi) || course_module_instance_pending_deletion($gi->courseid, $gi->itemmodule, $gi->iteminstance)) {
throw new \InvalidArgumentException ("Grade {$id} not found in database"); throw new \InvalidArgumentException ("Grade {$id} not found in database");
} }
$this->id = $id; $this->id = $id;
@ -248,10 +248,10 @@ class gradeinfo {
$r = $DB->get_record('local_treestudyplan_gradeinc', $r = $DB->get_record('local_treestudyplan_gradeinc',
['studyitem_id' => $this->studyitem->id(), 'grade_item_id' => $this->gradeitem->id]); ['studyitem_id' => $this->studyitem->id(), 'grade_item_id' => $this->gradeitem->id]);
if ($r && $r->include && $r->required) { if ($r && $r->include && $r->required) {
return(true); return true;
} }
} }
return(false); return false;
} }
/** /**
@ -339,7 +339,7 @@ class gradeinfo {
*/ */
public function user_model($userid) { public function user_model($userid) {
global $DB; global $DB;
$grade = $this->gradeitem->get_final($userid); $grade = (object)$this->gradeitem->get_final($userid);
// Format grade for proper display. // Format grade for proper display.
if (is_object($grade)) { if (is_object($grade)) {
$finalgrade = \grade_format_gradevalue($grade->finalgrade, $this->gradeitem, true, null, 1); $finalgrade = \grade_format_gradevalue($grade->finalgrade, $this->gradeitem, true, null, 1);

View file

@ -148,7 +148,7 @@ class gradingscanner {
// First check if the completion needs grading. // First check if the completion needs grading.
$ungraded++; $ungraded++;
} else { } else {
$grade = $this->gi->get_final($userid); $grade = (object)($this->gi->get_final($userid));
if ((!empty($grade->finalgrade)) && is_numeric($grade->finalgrade)) { if ((!empty($grade->finalgrade)) && is_numeric($grade->finalgrade)) {
// Compare grade to minimum grade. // Compare grade to minimum grade.
if ($this->grade_passed($grade)) { if ($this->grade_passed($grade)) {

View file

@ -326,7 +326,7 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
$course = \get_course($gradeitem->courseid); // Fetch course from cache. $course = \get_course($gradeitem->courseid); // Fetch course from cache.
$coursefinished = ($course->enddate) ? ($course->enddate < time()) : false; $coursefinished = ($course->enddate) ? ($course->enddate < time()) : false;
if (empty($grade)) { if (!is_object($grade) || empty($grade)) {
return completion::INCOMPLETE; return completion::INCOMPLETE;
} else if ($grade->finalgrade === null) { } else if ($grade->finalgrade === null) {
// On assignments, grade NULL means a submission has not yet been graded, . // On assignments, grade NULL means a submission has not yet been graded, .
@ -341,7 +341,6 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
return completion::INCOMPLETE; return completion::INCOMPLETE;
} }
} else { } else {
$grade = $gradeitem->get_final($userid);
// First determine if we have a grade_config for this scale or this maximum grade. // First determine if we have a grade_config for this scale or this maximum grade.
$finalgrade = $grade->finalgrade; $finalgrade = $grade->finalgrade;
$scale = $gradeinfo->get_scale(); $scale = $gradeinfo->get_scale();

View file

@ -289,6 +289,7 @@ class competency_aggregator extends \local_treestudyplan\aggregator {
*/ */
public function grade_completion(gradeinfo $gradeinfo, $userid) { public function grade_completion(gradeinfo $gradeinfo, $userid) {
// COURSE COMPETENCIES DOESN'T REALLY USE THIS FUNCTION. // COURSE COMPETENCIES DOESN'T REALLY USE THIS FUNCTION.
return 0;
} }
} }

View file

@ -216,6 +216,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
*/ */
public function grade_completion(gradeinfo $gradeinfo, $userid) { public function grade_completion(gradeinfo $gradeinfo, $userid) {
// CORE COMPLETION DOESN'T REALLY USE THIS FUNCTION. // CORE COMPLETION DOESN'T REALLY USE THIS FUNCTION.
return 0;
} }
} }

View file

@ -114,7 +114,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
} }
} else { } else {
// Indeterminable. // Indeterminable.
return null; return completion::INCOMPLETE;
} }
} }
@ -167,7 +167,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
$gradeitem = $gradeinfo->get_gradeitem(); $gradeitem = $gradeinfo->get_gradeitem();
$grade = $gradeitem->get_final($userid); $grade = $gradeitem->get_final($userid);
if (empty($grade)) { if (!is_object($grade) || empty($grade)) {
return completion::INCOMPLETE; return completion::INCOMPLETE;
} else if ($grade->finalgrade === null) { } else if ($grade->finalgrade === null) {
// On assignments, grade NULL means a submission has not yet been graded, . // On assignments, grade NULL means a submission has not yet been graded, .

View file

@ -214,7 +214,7 @@ class gradegenerator {
* @param string $student Student identifier * @param string $student Student identifier
* @param string $skill Skill identifier * @param string $skill Skill identifier
* @param gradeinfo[] $gradeinfos * @param gradeinfo[] $gradeinfos
* @return stdClass[] List of gradeinfo related results ready to add * @return array List of gradeinfo related results ready to add
*/ */
public function generate(string $student, string $skill, array $gradeinfos ) { public function generate(string $student, string $skill, array $gradeinfos ) {
global $DB; global $DB;
@ -330,9 +330,12 @@ class gradegenerator {
* Store skills table into a string * Store skills table into a string
* @return string|null Json encoded string of the skills table * @return string|null Json encoded string of the skills table
*/ */
public function serialize(): ?string { public function serialize(): string {
return json_encode([ $s = json_encode(["table" => $this->table], JSON_PRETTY_PRINT);
"table" => $this->table], JSON_PRETTY_PRINT); if (!is_string($s)) {
return "";
}
return $s;
} }
/** /**

View file

@ -44,7 +44,7 @@ class webservicehelper {
* @param array|string $capability One or more capabilities to be tested OR wise * @param array|string $capability One or more capabilities to be tested OR wise
* @param \context $context The context in which to check for the capability. * @param \context $context The context in which to check for the capability.
* @throws \webservice_access_exception If none of the capabilities provided are given to the current user * @throws \webservice_access_exception If none of the capabilities provided are given to the current user
* @return boolean * @return bool
*/ */
public static function has_capabilities($capability, $context) { public static function has_capabilities($capability, $context) {
@ -55,7 +55,7 @@ class webservicehelper {
if (is_array($capability)) { if (is_array($capability)) {
return \has_any_capability($capability, $context); return \has_any_capability($capability, $context);
} else { } else {
return has_capability($capability, $context); return \has_capability($capability, $context);
} }
} }
@ -63,7 +63,7 @@ class webservicehelper {
* Test if the current user has a certain capability in any of the categories they have access to * Test if the current user has a certain capability in any of the categories they have access to
* @param string $capability The capability to scan for in the categories * @param string $capability The capability to scan for in the categories
* @param int|null $userid Userid to use in check (Use current user if null or empty) * @param int|null $userid Userid to use in check (Use current user if null or empty)
* @return boolean * @return bool
*/ */
public static function has_capability_in_any_category($capability, $userid=null ) { public static function has_capability_in_any_category($capability, $userid=null ) {
global $USER; global $USER;

View file

@ -74,7 +74,6 @@ class randomimage {
* Create a random polygon with number of points between 0 & $maxpts * Create a random polygon with number of points between 0 & $maxpts
* @param \GdImage $im The image reource * @param \GdImage $im The image reource
* @param int $maxpts Max number of point to use * @param int $maxpts Max number of point to use
* @return void
*/ */
private function random_polygon($im, int $maxpts = 20) { private function random_polygon($im, int $maxpts = 20) {
$color = imagecolorallocatealpha($im, ...$this->random_color_alpha()); $color = imagecolorallocatealpha($im, ...$this->random_color_alpha());
@ -86,7 +85,6 @@ class randomimage {
/** /**
* Creates a random arc of a random color * Creates a random arc of a random color
* @param \GdImage $im The image resource * @param \GdImage $im The image resource
* @return void
*/ */
private function random_arc($im) { private function random_arc($im) {
$cx = \random_int(0, $this->width); $cx = \random_int(0, $this->width);
@ -102,7 +100,7 @@ class randomimage {
/** /**
* Generates an array of random alpha color values. * Generates an array of random alpha color values.
* @return Array [r, g, b, a] * @return array [r, g, b, a]
*/ */
private function random_color_alpha(): Array { private function random_color_alpha(): Array {
return [ return [
@ -116,7 +114,7 @@ class randomimage {
/** /**
* Generates a set of random points for a polygon [x1, y1, x2, y2, ...] * Generates a set of random points for a polygon [x1, y1, x2, y2, ...]
* @param integer $length Number of sets of points to generate * @param integer $length Number of sets of points to generate
* @return Array The resulting array of points * @return array The resulting array of points
*/ */
private function random_pts($length): Array { private function random_pts($length): Array {
$pts = []; $pts = [];
@ -159,8 +157,12 @@ class randomimage {
} }
} }
$this->tempfile = \tempnam("/tmp", "tsp"); $this->tempfile = \tempnam("/tmp", "tsp");
imagepng($im, $this->tempfile); if (is_object($im)) {
imagedestroy($im); imagepng($im, $this->tempfile);
}
if (is_object($im)) {
imagedestroy($im);
}
} }
/** /**
@ -168,7 +170,12 @@ class randomimage {
* @return string * @return string
*/ */
public function content() { public function content() {
return $this->content = \file_get_contents($this->tempfile); $this->content = \file_get_contents($this->tempfile);
if (is_string($this->content)) {
return $this->content;
} else {
return "";
}
} }
/** /**

View file

@ -32,6 +32,7 @@ require_once($CFG->dirroot.'/question/engine/states.php'); // For reading questi
class quiz_scanner extends scanner_base { class quiz_scanner extends scanner_base {
/** /**
* Retrieve the ungraded submissions in this activity * Retrieve the ungraded submissions in this activity
* @return array
*/ */
protected function get_ungraded_submissions() { protected function get_ungraded_submissions() {
// Count all users who have one or more questions that still need grading. // Count all users who have one or more questions that still need grading.

View file

@ -93,7 +93,7 @@ class period {
// Or specified duration of the page and the sequence of the periods . // Or specified duration of the page and the sequence of the periods .
$pcount = $page->periods(); $pcount = $page->periods();
$ystart = $page->startdate()->getTimestamp(); $ystart = $page->startdate()->getTimestamp();
$yend = $page->enddate()->getTimestamp(); $yend = ((object)($page->enddate(true)))->getTimestamp();
// Estimate the period's timing to make a reasonable first guess. // Estimate the period's timing to make a reasonable first guess.
$ydelta = $yend - $ystart; $ydelta = $yend - $ystart;

View file

@ -148,8 +148,10 @@ class provider implements \core_privacy\local\metadata\provider,
$context = \context_system::instance(); $context = \context_system::instance();
$subcontext = ["invitations"]; $subcontext = ["invitations"];
writer::with_context($context) $data = new \stdClass;
->export_data($subcontext, (object)["recipient" => $invit->name, "email" => $invit->email]); $data->recipient = $invit->name;
$data->email = $invit->email;
writer::with_context($context)->export_data($subcontext, $data);
} }
@ -161,8 +163,10 @@ class provider implements \core_privacy\local\metadata\provider,
$context = \context_system::instance(); $context = \context_system::instance();
$subcontext = ["studyplans"]; $subcontext = ["studyplans"];
writer::with_context($context) $data = new \stdClass;
->export_data($subcontext, (object)["fullname" => $studyplan->fullname, "shortname" => $studyplan->shortname]); $data->fullname = $studyplan->fullname;
$data->shortname = $studyplan->shortname;
writer::with_context($context)->export_data($subcontext, $data);
} }

View file

@ -74,7 +74,7 @@ class reportinvite_form extends moodleform {
/** /**
* Get supplied user data * Get supplied user data
* @return stdClass The supplied data * @return object The supplied data
*/ */
public function get_data() { public function get_data() {
global $DB, $USER; global $DB, $USER;

View file

@ -76,7 +76,7 @@ class reportservice extends \external_api {
* @param int $pageid ID of user to check specific info for * @param int $pageid ID of user to check specific info for
* @param int|null $firstperiod First period to include in report * @param int|null $firstperiod First period to include in report
* @param int|null $lastperiod Last period to include in report * @param int|null $lastperiod Last period to include in report
* @return object * @return array
*/ */
public static function get_report_structure($pageid, $firstperiod=null, $lastperiod=null) { public static function get_report_structure($pageid, $firstperiod=null, $lastperiod=null) {
$page = studyplanpage::find_by_id($pageid); $page = studyplanpage::find_by_id($pageid);

View file

@ -114,7 +114,7 @@ class studentstudyplanservice extends \external_api {
* Get a specific studyplan for a given user * Get a specific studyplan for a given user
* @param int $userid ID of user to check specific info for * @param int $userid ID of user to check specific info for
* @param int $studyplanid ID of studyplan to view * @param int $studyplanid ID of studyplan to view
* @return array * @return array|null
*/ */
public static function get_user_studyplan($userid, $studyplanid) { public static function get_user_studyplan($userid, $studyplanid) {
global $CFG, $DB; global $CFG, $DB;
@ -162,7 +162,7 @@ class studentstudyplanservice extends \external_api {
* Get a specific studyplan page for a given user * Get a specific studyplan page for a given user
* @param int $userid ID of user to check specific info for * @param int $userid ID of user to check specific info for
* @param int $pageid ID of studyplan to view * @param int $pageid ID of studyplan to view
* @return array * @return array|null
*/ */
public static function get_user_page($userid, $pageid) { public static function get_user_page($userid, $pageid) {
global $CFG, $DB; global $CFG, $DB;

View file

@ -657,7 +657,7 @@ class studyline {
/** /**
* List the course id is linked in this studyplan * List the course id is linked in this studyplan
* Used for cohort enrolment cascading * Used for cohort enrolment cascading
* @return int[] * @return array
*/ */
public function get_linked_course_ids(): array { public function get_linked_course_ids(): array {
global $DB; global $DB;
@ -845,7 +845,6 @@ class studyline {
* @param array $model Decoded array * @param array $model Decoded array
* @param array $itemtranslation Link to array to map old item ids to new item ids for connection matching * @param array $itemtranslation Link to array to map old item ids to new item ids for connection matching
* @param array $connections Link to array of connections between item * @param array $connections Link to array of connections between item
* @return null
*/ */
public function import_studyitems(array $model, array &$itemtranslation, array &$connections) { public function import_studyitems(array $model, array &$itemtranslation, array &$connections) {
global $DB; global $DB;

View file

@ -167,37 +167,40 @@ class studyplan {
if (count($files) > 0) { if (count($files) > 0) {
$file = array_shift($files); $file = array_shift($files);
if ($file->get_filename() == ".") {
if (is_object($file) && $file->get_filename() == ".") {
// Get next file if the first is the directory itself. // Get next file if the first is the directory itself.
$file = array_shift($files); $file = array_shift($files);
} }
$url = \moodle_url::make_pluginfile_url( if (is_object($file)) {
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename(),
false // Do not force download of the file.
);
} else {
// Try the configured default in settings.
$defaulticon = get_config('local_treestudyplan', 'defaulticon');
if (empty($defaulticon)) {
// Fall back to the standard (ugly) default image.
$url = new \moodle_url($CFG->wwwroot . "/local/treestudyplan/pix/default_icon.png");
} else {
$url = \moodle_url::make_pluginfile_url( $url = \moodle_url::make_pluginfile_url(
\context_system::instance()->id, $file->get_contextid(),
'local_treestudyplan', $file->get_component(),
'defaulticon', $file->get_filearea(),
0, $file->get_itemid(),
"/", $file->get_filepath(),
$defaulticon $file->get_filename(),
); false // Do not force download of the file.
);
return $url->out;
} }
}
// Try the configured default in settings.
$defaulticon = get_config('local_treestudyplan', 'defaulticon');
if (empty($defaulticon)) {
// Fall back to the standard (ugly) default image.
$url = new \moodle_url($CFG->wwwroot . "/local/treestudyplan/pix/default_icon.png");
} else {
$url = \moodle_url::make_pluginfile_url(
\context_system::instance()->id,
'local_treestudyplan',
'defaulticon',
0,
"/",
$defaulticon
);
} }
return $url->out(); return $url->out();
} }
@ -735,7 +738,7 @@ class studyplan {
/** /**
* Retrieve the users linked to this studyplan. * Retrieve the users linked to this studyplan.
* @return stdClass[] User objects * @return \stdClass[] User objects
*/ */
public function find_linked_users(): array { public function find_linked_users(): array {
global $DB; global $DB;
@ -787,7 +790,7 @@ class studyplan {
/** /**
* Check if this studyplan is linked to a particular user * Check if this studyplan is linked to a particular user
* @param bool|stdClass $user The userid or user record of the user * @param int|stdClass $user The userid or user record of the user
*/ */
public function has_linked_user($user) { public function has_linked_user($user) {
if (is_int($user)) { if (is_int($user)) {
@ -805,7 +808,7 @@ class studyplan {
/** /**
* Check if this studyplan is linked to a particular user * Check if this studyplan is linked to a particular user
* @param bool|stdClass|null $user The userid or user record of the user Leave empty to check current user. * @param int|stdClass|null $user The userid or user record of the user Leave empty to check current user.
*/ */
public function is_coach($user=null) { public function is_coach($user=null) {
global $DB, $USER; global $DB, $USER;
@ -968,9 +971,9 @@ class studyplan {
// Next, copy the studylines. // Next, copy the studylines.
$timeless = \get_config("local_treestudyplan", "timelessperiods"); $timeless = \get_config("local_treestudyplan", "timelessperiods");
if (!$timeless && $newstartdate) { $oldstart = $this->startdate();
if (!$timeless && $newstartdate && is_object($oldstart)) {
$newstart = new \DateTime(date("Y-m-d", $newstartdate)); $newstart = new \DateTime(date("Y-m-d", $newstartdate));
$oldstart = $this->startdate();
$timeoffset = $oldstart->diff($newstart); $timeoffset = $oldstart->diff($newstart);
} else { } else {
$timeoffset = new \DateInterval("P0D"); $timeoffset = new \DateInterval("P0D");

View file

@ -509,7 +509,7 @@ class studyplanpage {
'description' => $this->r->description, 'description' => $this->r->description,
'pages' => $this->r->pages, 'pages' => $this->r->pages,
'startdate' => $this->startdate()->add($timeoffset)->format("Y-m-d"), 'startdate' => $this->startdate()->add($timeoffset)->format("Y-m-d"),
'enddate' => empty($this->r->enddate) ? null : ($this->enddate()->add($timeoffset)->format("Y-m-d")), 'enddate' => empty($this->r->enddate) ? null : (((object)$this->enddate(true))->add($timeoffset)->format("Y-m-d")),
]); ]);
// Copy any files related to this page. // Copy any files related to this page.

View file

@ -122,7 +122,7 @@ class studyplanservice extends \external_api {
/** /**
* Get editor model for specific studyplan * Get editor model for specific studyplan
* @param int $id Id of studyplan * @param int $id Id of studyplan
* @return array * @return array|null
*/ */
public static function get_studyplan_map($id) { public static function get_studyplan_map($id) {
if (isset($id) && $id > 0) { if (isset($id) && $id > 0) {
@ -1224,64 +1224,66 @@ class studyplanservice extends \external_api {
$studyitem = new studyitem($itemr->id); $studyitem = new studyitem($itemr->id);
if ($studyitem->valid() && $studyitem->type() == studyitem::COURSE) { if ($studyitem->valid() && $studyitem->type() == studyitem::COURSE) {
$courseinfo = $studyitem->getcourseinfo(); $courseinfo = $studyitem->getcourseinfo();
$gradables = gradeinfo::list_studyitem_gradables($studyitem); if(is_object($courseinfo)){
$gradables = gradeinfo::list_studyitem_gradables($studyitem);
$gradelist = []; $gradelist = [];
foreach ($gradables as $g) { foreach ($gradables as $g) {
$gi = $g->get_gradeitem(); $gi = $g->get_gradeitem();
// Only change items that do not yet have grades. // Only change items that do not yet have grades.
// Otherwise we will need to implement grade recalculations and it is not worth the trouble. . // Otherwise we will need to implement grade recalculations and it is not worth the trouble. .
// If grades are given, you likely don't want to change it like this anyway. // If grades are given, you likely don't want to change it like this anyway.
if (!$gi->has_grades()) { if (!$gi->has_grades()) {
$gi->gradetype = GRADE_TYPE_SCALE; $gi->gradetype = GRADE_TYPE_SCALE;
$gi->scaleid = $scale->id; $gi->scaleid = $scale->id;
$gi->grademin = 1; $gi->grademin = 1;
$gi->grademax = $scalemax; $gi->grademax = $scalemax;
$gi->gradepass = $scalepass; $gi->gradepass = $scalepass;
// Update, signalling with our signature and bulkupdate. // Update, signalling with our signature and bulkupdate.
$result = $gi->update("local/treestudyplan"); $result = $gi->update("local/treestudyplan");
$debug = ""; $debug = "";
if ($result) { if ($result) {
$updated = "converted"; $updated = "converted";
} else { } else {
$updated = "error"; $updated = "error";
} }
// Next update the activity's table if it has a grade field. // Next update the activity's table if it has a grade field.
// Grade is generally set to the negative scale id if it is a scale. // Grade is generally set to the negative scale id if it is a scale.
$tablename = $gi->itemmodule; $tablename = $gi->itemmodule;
$fieldname = "grade"; $fieldname = "grade";
if ($result && $gi->itemtype == "mod" && $dbman->table_exists($tablename)) { if ($result && $gi->itemtype == "mod" && $dbman->table_exists($tablename)) {
if ($dbman->field_exists($tablename, $fieldname)) { if ($dbman->field_exists($tablename, $fieldname)) {
$gradevalue = intval(0 - ($scale->id)); $gradevalue = intval(0 - ($scale->id));
try { try {
$DB->set_field($tablename, $fieldname, $gradevalue, ["id" => $gi->iteminstance]); $DB->set_field($tablename, $fieldname, $gradevalue, ["id" => $gi->iteminstance]);
} catch (\dml_exception $x) { } catch (\dml_exception $x) {
$updated = "fail"; $updated = "fail";
$debug = strval($x); $debug = strval($x);
}
} }
} }
} else {
$updated = "skipped";
} }
} else { $gradelist[] = [
$updated = "skipped"; 'name' => $gi->itemname,
'changed' => $updated,
'debug' => $debug,
];
} }
$gradelist[] = [ $list[] = [
'name' => $gi->itemname, 'course' => $courseinfo->simple_model(),
'changed' => $updated, 'grades' => $gradelist,
'debug' => $debug,
]; ];
} }
$list[] = [
'course' => $courseinfo->simple_model(),
'grades' => $gradelist,
];
} }
} }
} }
@ -2216,7 +2218,7 @@ class studyplanservice extends \external_api {
/** /**
* Count the number of templates available * Count the number of templates available
* @return array * @return int
*/ */
public static function count_templates() { public static function count_templates() {
\external_api::validate_context(\context_system::instance()); \external_api::validate_context(\context_system::instance());

View file

@ -177,7 +177,7 @@ class teachingfinder {
/** /**
* List of recognized teacher id's * List of recognized teacher id's
* @return intp[] * @return int[]
*/ */
public static function list_teacher_ids() { public static function list_teacher_ids() {
global $DB; global $DB;

View file

@ -78,7 +78,6 @@ class utilityservice extends \external_api {
* Returns description of get_mform parameters * Returns description of get_mform parameters
* *
* @return \external_function_parameters * @return \external_function_parameters
* @since Moodle 3.1
*/ */
public static function get_mform_parameters() { public static function get_mform_parameters() {
return new \external_function_parameters([ return new \external_function_parameters([
@ -122,7 +121,6 @@ class utilityservice extends \external_api {
* Returns description of get_mform() result value * Returns description of get_mform() result value
* *
* @return \core_external\external_description * @return \core_external\external_description
* @since Moodle 3.1
*/ */
public static function get_mform_returns() { public static function get_mform_returns() {
return new \external_single_structure( return new \external_single_structure(
@ -196,7 +194,7 @@ class utilityservice extends \external_api {
/** /**
* Submit specified form data into form * Submit specified form data into form
* @return array Success/fail structure * @return array of settings
*/ */
public static function getsettings() { public static function getsettings() {
global $CFG; global $CFG;