Moodle code style fixes completed

This commit is contained in:
PMKuipers 2023-08-25 17:33:20 +02:00
parent f8fd27528a
commit cbc2c0e7ca
28 changed files with 316 additions and 341 deletions

View file

@ -124,8 +124,8 @@ class associationservice extends \external_api {
$params = ["pattern_nm" => $pattern, "pattern_id" => $pattern, ]; $params = ["pattern_nm" => $pattern, "pattern_id" => $pattern, ];
$sql = "SELECT c.* from {cohort} c LEFT JOIN {local_treestudyplan_cohort} j ON c.id = j.cohort_id"; $sql = "SELECT c.* from {cohort} c LEFT JOIN {local_treestudyplan_cohort} j ON c.id = j.cohort_id
$sql .= " WHERE c.visible = 1 AND(name LIKE :pattern_nm OR idnumber LIKE :pattern_id)"; WHERE c.visible = 1 AND(name LIKE :pattern_nm OR idnumber LIKE :pattern_id)";
if (isset($excludeid) && is_numeric($excludeid)) { if (isset($excludeid) && is_numeric($excludeid)) {
$sql .= " AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)"; $sql .= " AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)";
$params['exclude_id'] = $excludeid; $params['exclude_id'] = $excludeid;
@ -171,8 +171,8 @@ class associationservice extends \external_api {
"pattern_ln" => $pattern, "pattern_ln" => $pattern,
"pattern_un" => $pattern, "pattern_un" => $pattern,
]; ];
$sql = "SELECT u.* from {user} u LEFT JOIN {local_treestudyplan_user} j ON u.id = j.user_id"; $sql = "SELECT u.* from {user} u LEFT JOIN {local_treestudyplan_user} j ON u.id = j.user_id
$sql .= " WHERE u.deleted != 1 AND (firstname LIKE :pattern_fn OR lastname LIKE :pattern_ln OR username LIKE :pattern_un)"; WHERE u.deleted != 1 AND (firstname LIKE :pattern_fn OR lastname LIKE :pattern_ln OR username LIKE :pattern_un)";
if (isset($excludeid) && is_numeric($excludeid)) { if (isset($excludeid) && is_numeric($excludeid)) {
$sql .= " AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)"; $sql .= " AND (j.studyplan_id IS NULL OR j.studyplan_id != :exclude_id)";
$params['exclude_id'] = $excludeid; $params['exclude_id'] = $excludeid;
@ -207,7 +207,7 @@ class associationservice extends \external_api {
public static function connect_cohort($studyplanid, $cohortid) { public static function connect_cohort($studyplanid, $cohortid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context());
if (!$DB->record_exists('local_treestudyplan_cohort', ['studyplan_id' => $studyplanid, 'cohort_id' => $cohortid])) { if (!$DB->record_exists('local_treestudyplan_cohort', ['studyplan_id' => $studyplanid, 'cohort_id' => $cohortid])) {
@ -243,7 +243,7 @@ class associationservice extends \external_api {
public static function disconnect_cohort($studyplanid, $cohortid) { public static function disconnect_cohort($studyplanid, $cohortid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context());
if ($DB->record_exists('local_treestudyplan_cohort', ['studyplan_id' => $studyplanid, 'cohort_id' => $cohortid])) { if ($DB->record_exists('local_treestudyplan_cohort', ['studyplan_id' => $studyplanid, 'cohort_id' => $cohortid])) {
@ -279,7 +279,7 @@ class associationservice extends \external_api {
public static function connect_user($studyplanid, $userid) { public static function connect_user($studyplanid, $userid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context());
if (!$DB->record_exists('local_treestudyplan_user', ['studyplan_id' => $studyplanid, 'user_id' => $userid])) { if (!$DB->record_exists('local_treestudyplan_user', ['studyplan_id' => $studyplanid, 'user_id' => $userid])) {
@ -313,7 +313,7 @@ class associationservice extends \external_api {
// Actual functions. // Actual functions.
public static function disconnect_user($studyplanid, $userid) { public static function disconnect_user($studyplanid, $userid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context());
if ($DB->record_exists('local_treestudyplan_user', ['studyplan_id' => $studyplanid, 'user_id' => $userid])) { if ($DB->record_exists('local_treestudyplan_user', ['studyplan_id' => $studyplanid, 'user_id' => $userid])) {
@ -343,11 +343,11 @@ class associationservice extends \external_api {
// Actual functions. // Actual functions.
public static function associated_users($studyplanid) { public static function associated_users($studyplanid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context());
$sql = "SELECT DISTINCT u.* FROM {user} u INNER JOIN {local_treestudyplan_user} j ON j.user_id = u.id"; $sql = "SELECT DISTINCT u.* FROM {user} u INNER JOIN {local_treestudyplan_user} j ON j.user_id = u.id
$sql .= " WHERE j.studyplan_id = :studyplan_id"; WHERE j.studyplan_id = :studyplan_id";
$rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]); $rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]);
$users = []; $users = [];
@ -372,11 +372,11 @@ class associationservice extends \external_api {
// Actual functions. // Actual functions.
public static function associated_cohorts($studyplanid) { public static function associated_cohorts($studyplanid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context());
$sql = "SELECT DISTINCT c.* FROM {cohort} c INNER JOIN {local_treestudyplan_cohort} j ON j.cohort_id = c.id"; $sql = "SELECT DISTINCT c.* FROM {cohort} c INNER JOIN {local_treestudyplan_cohort} j ON j.cohort_id = c.id
$sql .= " WHERE j.studyplan_id = :studyplan_id"; WHERE j.studyplan_id = :studyplan_id";
$rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]); $rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]);
$cohorts = []; $cohorts = [];
foreach ($rs as $c) { foreach ($rs as $c) {
@ -400,20 +400,20 @@ class associationservice extends \external_api {
public static function all_associated($studyplanid) { public static function all_associated($studyplanid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context());
$users = []; $users = [];
// SQL JOIN script selecting all users that have a cohort linked to this studyplan . // SQL JOIN script selecting all users that have a cohort linked to this studyplan .
// Or are directly linked. // Or are directly linked.
$sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.idnumber, u.email" $sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.idnumber, u.email
."FROM {user} u" FROM {user} u
."LEFT JOIN {cohort_members} cm ON u.id = cm.userid" LEFT JOIN {cohort_members} cm ON u.id = cm.userid
."LEFT JOIN {local_treestudyplan_cohort} tc ON cm.cohortid = tc.cohort_id" LEFT JOIN {local_treestudyplan_cohort} tc ON cm.cohortid = tc.cohort_id
."LEFT JOIN {local_treestudyplan_user} tu ON u.id = tu.user_id" LEFT JOIN {local_treestudyplan_user} tu ON u.id = tu.user_id
."WHERE tc.studyplan_id = {$studyplan->id()}" WHERE tc.studyplan_id = {$studyplan->id()}
."OR tu.studyplan_id = {$studyplan->id()}" OR tu.studyplan_id = {$studyplan->id()}
."ORDER BY u.lastname, u.firstname"; ORDER BY u.lastname, u.firstname";
$rs = $DB->get_recordset_sql($sql); $rs = $DB->get_recordset_sql($sql);
foreach ($rs as $u) { foreach ($rs as $u) {
@ -455,7 +455,7 @@ class associationservice extends \external_api {
// Actual functions. // Actual functions.
public static function cascade_cohortsync($studyplanid) { public static function cascade_cohortsync($studyplanid) {
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $studyplan->context());
$enroller = new cascadecohortsync($studyplan); $enroller = new cascadecohortsync($studyplan);

View file

@ -51,13 +51,28 @@ class corecompletioninfo {
} }
public static function completiontypes() { public static function completiontypes() {
global $COMPLETION_CRITERIA_TYPES; /* While it is tempting to use the global array COMPLETION_CRITERIA_TYPES,
/* Just return the keys of the global array COMPLETION_CRITERIA_TYPES,
so we don't have to manually add any completion types if moodle decides to add a few. so we don't have to manually add any completion types if moodle decides to add a few.
Unfortunately, the global variable breaks the moodle coding standard... We can just as easily add the list here manually, since adding a completion type
requires adding code to this page anyway.
And this way we can keep the moodle code style checker happy.
(Moodle will probably refator that part of the code anyway in the future, without
taking effects of this plugin into account :)
Array declaration based completion/criteria/completion_criteria.php:85 where the global
COMPLETION_CRITERIA_TYPES iw/was defined.
*/ */
if (!isset(self::$completiontypes)) { if (!isset(self::$completiontypes)) {
self::$completiontypes = \array_keys($COMPLETION_CRITERIA_TYPES); self::$completiontypes = [
COMPLETION_CRITERIA_TYPE_SELF => 'self',
COMPLETION_CRITERIA_TYPE_DATE => 'date',
COMPLETION_CRITERIA_TYPE_UNENROL => 'unenrol',
COMPLETION_CRITERIA_TYPE_ACTIVITY => 'activity',
COMPLETION_CRITERIA_TYPE_DURATION => 'duration',
COMPLETION_CRITERIA_TYPE_GRADE => 'grade',
COMPLETION_CRITERIA_TYPE_ROLE => 'role',
COMPLETION_CRITERIA_TYPE_COURSE => 'course',
];
} }
return self::$completiontypes; return self::$completiontypes;
} }
@ -192,10 +207,11 @@ class corecompletioninfo {
]; ];
foreach ($criterias as $criteria) { foreach ($criterias as $criteria) {
// Unfortunately, we cannot easily get the criteria details with get_details() without having a . /* Unfortunately, we cannot easily get the criteria details with get_details() without having a
// User completion object involved, so'we'll have to retrieve the details per completion type. user completion object involved, so'we'll have to retrieve the details per completion type.
// See moodle/completion/criteria/completion_criteria_*.php::get_details() for the code that is. See moodle/completion/criteria/completion_criteria_*.php::get_details() for the code that
// In the code below is based on. the code below is based on.
*/
if ($type == COMPLETION_CRITERIA_TYPE_SELF) { if ($type == COMPLETION_CRITERIA_TYPE_SELF) {
$details = [ $details = [

View file

@ -71,12 +71,12 @@ class courseinfo {
return static::$contentitems; return static::$contentitems;
} }
protected function amTeacher() { protected function am_teacher() {
global $USER; global $USER;
return is_enrolled($this->coursecontext, $USER, 'mod/assign:grade'); return is_enrolled($this->coursecontext, $USER, 'mod/assign:grade');
} }
protected function iCanSelectGradables($userid = -1) { protected function i_can_select_gradables($userid = -1) {
global $USER, $DB; global $USER, $DB;
if ($userid <= 0) { if ($userid <= 0) {
$usr = $USER; $usr = $USER;
@ -217,9 +217,9 @@ class courseinfo {
'timing' => $timing, 'timing' => $timing,
'startdate' => date("Y-m-d", $this->course->startdate, ), 'startdate' => date("Y-m-d", $this->course->startdate, ),
'enddate' => date("Y-m-d", $this->course->enddate), 'enddate' => date("Y-m-d", $this->course->enddate),
'amteacher' => $this->amTeacher(), 'amteacher' => $this->am_teacher(),
'canupdatecourse' => \has_capability("moodle/course:update", $this->coursecontext), 'canupdatecourse' => \has_capability("moodle/course:update", $this->coursecontext),
'canselectgradables' => $this->iCanSelectGradables(), 'canselectgradables' => $this->i_can_select_gradables(),
'tag' => "Editormodel", 'tag' => "Editormodel",
'grades' => [], 'grades' => [],
]; ];

View file

@ -1,57 +0,0 @@
<?php
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
*
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/externallib.php');
require_once($CFG->libdir.'/modinfolib.php');
require_once($CFG->dirroot.'/course/lib.php');
use core_course\local\repository\caching_content_item_readonly_repository;
use core_course\local\repository\content_item_readonly_repository;
use \grade_item;
class coursemoduleinfo {
private $id;
private $cm;
private $cminfo;
private $dbrecord;
public function __construct($id) {
global $DB;
// Determine the icon for the associated activity.
$this->id = $id;
$this->cm = $DB->get_record("course_modules", ["id" => $id]);
$this->cm_info = \cm_info::create($this->cm);
}
public function getTitle() {
return $this->cm_info->name;
}
public function setTitle($value) {
$this->cm_info->set_name($value);
// TODO: Actually save this after setting the cminfo.
}
}

View file

@ -312,7 +312,7 @@ class courseservice extends \external_api {
public static function scan_grade_progress($gradeitemid, $studyplanid) { public static function scan_grade_progress($gradeitemid, $studyplanid) {
global $DB; global $DB;
// Verify access to the study plan. // Verify access to the study plan.
$o = studyplan::findById($studyplanid); $o = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEW, $o->context()); webservicehelper::require_capabilities(self::CAP_VIEW, $o->context());
// Retrieve grade item. // Retrieve grade item.
@ -345,7 +345,7 @@ class courseservice extends \external_api {
public static function scan_completion_progress($criteriaid, $studyplanid, $courseid) { public static function scan_completion_progress($criteriaid, $studyplanid, $courseid) {
global $DB; global $DB;
// Verify access to the study plan. // Verify access to the study plan.
$o = studyplan::findById($studyplanid); $o = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEW, $o->context()); webservicehelper::require_capabilities(self::CAP_VIEW, $o->context());
$crit = \completion_criteria::fetch(["id" => $criteriaid]); $crit = \completion_criteria::fetch(["id" => $criteriaid]);
@ -375,7 +375,7 @@ class courseservice extends \external_api {
public static function scan_badge_progress($badgeid, $studyplanid) { public static function scan_badge_progress($badgeid, $studyplanid) {
global $DB; global $DB;
// Check access to the study plan. // Check access to the study plan.
$o = studyplan::findById($studyplanid); $o = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEW, $o->context()); webservicehelper::require_capabilities(self::CAP_VIEW, $o->context());
// Validate that badge is linked to studyplan. // Validate that badge is linked to studyplan.

View file

@ -57,7 +57,7 @@ class gradeinfo {
private static $sections = []; private static $sections = [];
protected static function getSectionSequence($sectionid) { protected static function get_sectionsequence($sectionid) {
global $DB; global $DB;
if (!array_key_exists($sectionid, self::$sections)) { if (!array_key_exists($sectionid, self::$sections)) {
self::$sections[$sectionid] = explode(", ", $DB->get_field("course_sections", "sequence", ["id" => $sectionid])); self::$sections[$sectionid] = explode(", ", $DB->get_field("course_sections", "sequence", ["id" => $sectionid]));
@ -65,15 +65,15 @@ class gradeinfo {
return self::$sections[$sectionid]; return self::$sections[$sectionid];
} }
public function getGradeitem() { public function get_gradeitem() {
return $this->gradeitem; return $this->gradeitem;
} }
public function getGradingscanner() { public function get_gradingscanner() {
return $this->gradingscanner; return $this->gradingscanner;
} }
public function getScale() { public function get_scale() {
return $this->scale; return $this->scale;
} }
@ -96,7 +96,7 @@ class gradeinfo {
return null; return null;
} }
public static function getCourseContextById($id) { public static function get_coursecontext_by_id($id) {
$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 (!$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");
@ -131,7 +131,7 @@ class gradeinfo {
// Sort by position in course. // Sort by position in course.
// . // .
$this->section = $cminfo->sectionnum; $this->section = $cminfo->sectionnum;
$ssequence = self::getSectionSequence($cminfo->section); $ssequence = self::get_sectionsequence($cminfo->section);
$this->sectionorder = array_search($cminfo->id, $ssequence); $this->sectionorder = array_search($cminfo->id, $ssequence);
$this->link = "/mod/{$gi->itemmodule}/view.php?id={$cminfo->id}"; $this->link = "/mod/{$gi->itemmodule}/view.php?id={$cminfo->id}";
@ -336,6 +336,8 @@ class gradeinfo {
$gradable = new static($gi->id, $studyitem); $gradable = new static($gi->id, $studyitem);
$list[] = $gradable; $list[] = $gradable;
} catch (\InvalidArgumentException $x) { } catch (\InvalidArgumentException $x) {
// Pass and do not add to the list if an error occurs.
$gradable = null; // Clean up gradable variable.
} }
} }
} }

View file

@ -102,9 +102,7 @@ class gradingscanner {
$ungraded++; $ungraded++;
} else { } else {
$grade = $this->gi->get_final($userid); $grade = $this->gi->get_final($userid);
if (!is_numeric($grade->finalgrade) && empty($grade->finalgrade)) { if ( (!empty($grade->finalgrade)) && is_numeric($grade->finalgrade)) {
// Skip.
} else {
// Compare grade to minimum grade. // Compare grade to minimum grade.
if ($this->grade_passed($grade)) { if ($this->grade_passed($grade)) {
$completedpass++; $completedpass++;

View file

@ -30,14 +30,22 @@ use \local_treestudyplan\debug;
class bistate_aggregator extends \local_treestudyplan\aggregator { class bistate_aggregator extends \local_treestudyplan\aggregator {
public const DEPRECATED = false; public const DEPRECATED = false;
private const DEFAULT_CONDITION = "50";
private $threshexcellent = 1.0; // Minimum fraction that must be completed to aggregate as excellent (usually 1.0). private $agcfg = null;
private $threshgood = 0.8; // Minimum fraction that must be completed to aggregate as good.
private $threshcompleted = 0.66; // Minimum fraction that must be completed to aggregate as completed. private function cfg() {
private $usefailed = true; // Support failed completion yes/no. if (empty($this->agcfg)) {
private $threshprogress = 0.33; // Minimum fraction that must be failed to aggregate as failed instead of progress. $this->agcfg = (object)[
private $acceptpending_as_submitted = false; // Also count ungraded but submitted . 'thresh_excellent' => 1.0, // Minimum fraction that must be completed to aggregate as excellent (usually 1.0).
'thresh_good' => 0.8, // Minimum fraction that must be completed to aggregate as good.
'thresh_completed' => 0.66, // Minimum fraction that must be completed to aggregate as completed.
'use_failed' => true, // Support failed completion yes/no.
'thresh_progress' => 0.33, // Deprecated!
'accept_pending_as_submitted' => false, // Also count ungraded but submitted .
];
}
return $this->agcfg;
}
public function __construct($configstr) { public function __construct($configstr) {
// Allow public constructor for testing purposes. // Allow public constructor for testing purposes.
@ -50,11 +58,11 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
foreach (["thresh_excellent", "thresh_good", "thresh_completed", "thresh_progress", ] as $key) { foreach (["thresh_excellent", "thresh_good", "thresh_completed", "thresh_progress", ] as $key) {
$val = intval(get_config('local_treestudyplan', "bistate_{$key}")); $val = intval(get_config('local_treestudyplan', "bistate_{$key}"));
if ($val >= 0 && $val <= 100) { if ($val >= 0 && $val <= 100) {
$this->$key = floatval($val)/100; $this->cfg()->$key = floatval($val) / 100;
} }
} }
foreach (["use_failed", "accept_pending_as_submitted"] as $key) { foreach (["use_failed", "accept_pending_as_submitted"] as $key) {
$this->$key = boolval(get_config('local_treestudyplan', "bistate_{$key}")); $this->cfg()->$key = boolval(get_config('local_treestudyplan', "bistate_{$key}"));
} }
// Next, decode json. // Next, decode json.
@ -66,13 +74,13 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
if (array_key_exists($key, $config)) { if (array_key_exists($key, $config)) {
$val = $config[$key]; $val = $config[$key];
if ($val >= 0 && $val <= 100) { if ($val >= 0 && $val <= 100) {
$this->$key = floatval($val)/100; $this->cfg()->$key = floatval($val) / 100;
} }
} }
} }
foreach (["use_failed", "accept_pending_as_submitted"] as $key) { foreach (["use_failed", "accept_pending_as_submitted"] as $key) {
if (array_key_exists($key, $config)) { if (array_key_exists($key, $config)) {
$this->$key = boolval($config[$key]); $this->cfg()->$key = boolval($config[$key]);
} }
} }
} }
@ -81,12 +89,12 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
// Return active configuration model. // Return active configuration model.
public function config_string() { public function config_string() {
return json_encode([ return json_encode([
"thresh_excellent" => 100*$this->thresh_excellent, "thresh_excellent" => 100 * $this->cfg()->thresh_excellent,
"thresh_good" => 100*$this->thresh_good, "thresh_good" => 100 * $this->cfg()->thresh_good,
"thresh_completed" => 100*$this->thresh_completed, "thresh_completed" => 100 * $this->cfg()->thresh_completed,
"thresh_progress" => 100*$this->thresh_progress, "thresh_progress" => 100 * $this->cfg()->thresh_progress,
"use_failed" => $this->use_failed, "use_failed" => $this->cfg()->use_failed,
"accept_pending_as_submitted" => $this->accept_pending_as_submitted, "accept_pending_as_submitted" => $this->cfg()->accept_pending_as_submitted,
]); ]);
} }
@ -124,12 +132,12 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
$totalrequired = 0; $totalrequired = 0;
$requiredmet = 0; $requiredmet = 0;
$MINPROGRESS = ($this->accept_pending_as_submitted) ? completion::PENDING : completion::PROGRESS; $minprogress = ($this->accept_pending_as_submitted) ? completion::PENDING : completion::PROGRESS;
foreach ($completions as $index => $c) { foreach ($completions as $index => $c) {
$completed += ($c >= completion::COMPLETED) ? 1 : 0; $completed += ($c >= completion::COMPLETED) ? 1 : 0;
$progress += ($c >= $MINPROGRESS) ? 1 : 0; $progress += ($c >= $minprogress) ? 1 : 0;
$failed += ($c <= completion::FAILED) ? 1 : 0; $failed += ($c <= completion::FAILED) ? 1 : 0;
} }
$started = $progress + $failed; $started = $progress + $failed;
@ -217,7 +225,7 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
public function grade_completion(gradeinfo $gradeinfo, $userid) { public function grade_completion(gradeinfo $gradeinfo, $userid) {
global $DB; global $DB;
$table = "local_treestudyplan_gradecfg"; $table = "local_treestudyplan_gradecfg";
$gradeitem = $gradeinfo->getGradeitem(); $gradeitem = $gradeinfo->get_gradeitem();
$grade = $gradeitem->get_final($userid); $grade = $gradeitem->get_final($userid);
$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;
@ -231,7 +239,7 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
// Since we want old results to be visible until a pending item was graded, we only use this state here. // Since we want old results to be visible until a pending item was graded, we only use this state here.
// Pending items are otherwise expressly indicated by the "pendingsubmission" field in the user model. // Pending items are otherwise expressly indicated by the "pendingsubmission" field in the user model.
if ($gradeinfo->getGradingscanner()->pending($userid)) { if ($gradeinfo->get_gradingscanner()->pending($userid)) {
return completion::PENDING; return completion::PENDING;
} else { } else {
return completion::INCOMPLETE; return completion::INCOMPLETE;
@ -240,7 +248,7 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
$grade = $gradeitem->get_final($userid); $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->getScale(); $scale = $gradeinfo->get_scale();
if ( isset($scale)) { if ( isset($scale)) {
$gradecfg = $DB->get_record($table, ["scale_id" => $scale->id]); $gradecfg = $DB->get_record($table, ["scale_id" => $scale->id]);
} else if ($gradeitem->grademin == 0) { } else if ($gradeitem->grademin == 0) {

View file

@ -31,7 +31,16 @@ use \local_treestudyplan\debug;
class core_aggregator extends \local_treestudyplan\aggregator { class core_aggregator extends \local_treestudyplan\aggregator {
public const DEPRECATED = false; public const DEPRECATED = false;
private $acceptpending_as_submitted = false; // Also count ungraded but submitted . private $agcfg = null;
private function cfg() {
if (empty($this->agcfg)) {
$this->agcfg = (object)[
'accept_pending_as_submitted' => false, // Also count ungraded but submitted .
];
}
return $this->agcfg;
}
public function __construct($configstr) { public function __construct($configstr) {
// Allow public constructor for testing purposes. // Allow public constructor for testing purposes.
@ -41,7 +50,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
protected function initialize($configstr) { protected function initialize($configstr) {
// First initialize with the defaults. // First initialize with the defaults.
foreach (["accept_pending_as_submitted"] as $key) { foreach (["accept_pending_as_submitted"] as $key) {
$this->$key = boolval(get_config('local_treestudyplan', "bistate_{$key}")); $this->cfg()->$key = boolval(get_config('local_treestudyplan', "bistate_{$key}"));
} }
// Next, decode json. // Next, decode json.
@ -51,7 +60,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
// Copy all valid config settings to this item. // Copy all valid config settings to this item.
foreach (["accept_pending_as_submitted"] as $key) { foreach (["accept_pending_as_submitted"] as $key) {
if (array_key_exists($key, $config)) { if (array_key_exists($key, $config)) {
$this->$key = boolval($config[$key]); $this->cfg()->$key = boolval($config[$key]);
} }
} }
} }
@ -60,7 +69,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
// Return active configuration model. // Return active configuration model.
public function config_string() { public function config_string() {
return json_encode([ return json_encode([
"accept_pending_as_submitted" => $this->accept_pending_as_submitted, "accept_pending_as_submitted" => $this->cfg()->accept_pending_as_submitted,
]); ]);
} }
@ -174,7 +183,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
public function grade_completion(gradeinfo $gradeinfo, $userid) { public function grade_completion(gradeinfo $gradeinfo, $userid) {
global $DB; global $DB;
$table = "local_treestudyplan_gradecfg"; $table = "local_treestudyplan_gradecfg";
$gradeitem = $gradeinfo->getGradeitem(); $gradeitem = $gradeinfo->get_gradeitem();
$grade = $gradeitem->get_final($userid); $grade = $gradeitem->get_final($userid);
if (empty($grade)) { if (empty($grade)) {
@ -186,7 +195,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
// Since we want old results to be visible until a pending item was graded, we only use this state here. // Since we want old results to be visible until a pending item was graded, we only use this state here.
// Pending items are otherwise expressly indicated by the "pendingsubmission" field in the user model. // Pending items are otherwise expressly indicated by the "pendingsubmission" field in the user model.
if ($gradeinfo->getGradingscanner()->pending($userid)) { if ($gradeinfo->get_gradingscanner()->pending($userid)) {
return completion::PENDING; return completion::PENDING;
} else { } else {
return completion::INCOMPLETE; return completion::INCOMPLETE;
@ -195,7 +204,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
$grade = $gradeitem->get_final($userid); $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->getScale(); $scale = $gradeinfo->get_scale();
if ( isset($scale)) { if ( isset($scale)) {
$gradecfg = $DB->get_record($table, ["scale_id" => $scale->id]); $gradecfg = $DB->get_record($table, ["scale_id" => $scale->id]);
} else if ($gradeitem->grademin == 0) { } else if ($gradeitem->grademin == 0) {

View file

@ -51,7 +51,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
$cexcellent = 0; $cexcellent = 0;
$cprogress = 0; $cprogress = 0;
$cpending = 0; $cpending = 0;
$count = sizeof($a); $count = count($a);
if ($count > 0) { if ($count > 0) {
@ -116,7 +116,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
public function grade_completion(gradeinfo $gradeinfo, $userid) { public function grade_completion(gradeinfo $gradeinfo, $userid) {
global $DB; global $DB;
$gradeitem = $gradeinfo->getGradeitem(); $gradeitem = $gradeinfo->get_gradeitem();
$grade = $gradeitem->get_final($userid); $grade = $gradeitem->get_final($userid);
if (empty($grade)) { if (empty($grade)) {
@ -128,7 +128,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
// Since we want old results to be visible until a pending item was graded, we only use this state here. // Since we want old results to be visible until a pending item was graded, we only use this state here.
// Pending items are otherwise expressly indicated by the "pendingsubmission" field in the user model. // Pending items are otherwise expressly indicated by the "pendingsubmission" field in the user model.
if ($gradeinfo->getGradingscanner()->pending($userid)) { if ($gradeinfo->get_gradingscanner()->pending($userid)) {
return completion::PENDING; return completion::PENDING;
} else { } else {
return completion::INCOMPLETE; return completion::INCOMPLETE;
@ -136,7 +136,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
} else { } else {
$finalgrade = $grade->finalgrade; $finalgrade = $grade->finalgrade;
$scale = $gradeinfo->getScale(); $scale = $gradeinfo->get_scale();
if ($gradeitem->gradepass > 0) { if ($gradeitem->gradepass > 0) {
// Base completion off of gradepass (if set). // Base completion off of gradepass (if set).

View file

@ -122,7 +122,7 @@ class gradegenerator {
} }
// Below is mostly just for reference in the json file. // Below is mostly just for reference in the json file.
public function addUserNameInfo(string $student, $firstname, $lastname) { public function add_username_info(string $student, $firstname, $lastname) {
$this->addstudent($student); $this->addstudent($student);
$this->table[$student]["firstname"] = $firstname; $this->table[$student]["firstname"] = $firstname;
$this->table[$student]["lastname"] = $lastname; $this->table[$student]["lastname"] = $lastname;
@ -175,7 +175,7 @@ class gradegenerator {
for ($i = 0; $i < count($gradeinfos); $i++) { for ($i = 0; $i < count($gradeinfos); $i++) {
$g = $gradeinfos[$i]; $g = $gradeinfos[$i];
$gi = $g->getGradeitem(); $gi = $g->get_gradeitem();
$gr = $gen[$i]; $gr = $gen[$i];
// First get the configured interpretation for this scale or grade. // First get the configured interpretation for this scale or grade.
@ -280,12 +280,12 @@ class gradegenerator {
$this->table = $o["table"]; $this->table = $o["table"];
} }
public function toFile(string $filename) { public function to_file(string $filename) {
$filename = self::expand_tilde($filename); $filename = self::expand_tilde($filename);
file_put_contents($filename, $this->serialize()); file_put_contents($filename, $this->serialize());
} }
public function fromFile(string $filename) { public function from_file(string $filename) {
$filename = self::expand_tilde($filename); $filename = self::expand_tilde($filename);
if (file_exists($filename)) { if (file_exists($filename)) {
try { try {

View file

@ -29,8 +29,8 @@ class assign_scanner extends scanner_base {
$sql = "SELECT DISTINCT asgn_sub.userid $sql = "SELECT DISTINCT asgn_sub.userid
FROM {assign_submission} asgn_sub FROM {assign_submission} asgn_sub
JOIN {assign} a ON a.id = asgn_sub.assignment JOIN {assign} a ON a.id = asgn_sub.assignment
LEFT JOIN {assign_grades} ag ON ag.assignment = asgn_sub.assignment AND ag.userid = asgn_sub.userid AND LEFT JOIN {assign_grades} ag ON ag.assignment = asgn_sub.assignment AND ag.userid = asgn_sub.userid
asgn_sub.attemptnumber = ag.attemptnumber AND asgn_sub.attemptnumber = ag.attemptnumber
WHERE a.id = {$this->gi->iteminstance} WHERE a.id = {$this->gi->iteminstance}
AND asgn_sub.status = 'submitted' AND asgn_sub.status = 'submitted'
AND asgn_sub.userid > 0 AND asgn_sub.userid > 0

View file

@ -22,6 +22,7 @@
namespace local_treestudyplan\local\ungradedscanners; namespace local_treestudyplan\local\ungradedscanners;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/question/engine/states.php'); // For reading question state. require_once($CFG->dirroot.'/question/engine/states.php'); // For reading question state.
class quiz_scanner extends scanner_base { class quiz_scanner extends scanner_base {
@ -31,20 +32,20 @@ class quiz_scanner extends scanner_base {
global $DB; global $DB;
// First find all question attempts that need grading. // First find all question attempts that need grading.
$sql = "SELECT qza.id as submissionid, qza.userid as userid," $sql = "SELECT qza.id as submissionid, qza.userid as userid,
. "qas.questionattemptid as attempt_id, qas.sequencenumber as sequencenumber " qas.questionattemptid as attempt_id, qas.sequencenumber as sequencenumber
. "FROM {question_attempt_steps} qas " FROM {question_attempt_steps} qas
. "JOIN {question_attempts} qna ON qas.questionattemptid = qna.id " JOIN {question_attempts} qna ON qas.questionattemptid = qna.id
. "JOIN {quiz_attempts} qza ON qna.questionusageid = qza.uniqueid " JOIN {quiz_attempts} qza ON qna.questionusageid = qza.uniqueid
. "WHERE qas.state = 'needsgrading' AND qza.quiz = {$this->gi->iteminstance}"; WHERE qas.state = 'needsgrading' AND qza.quiz = {$this->gi->iteminstance}";
$rs = $DB->get_recordset_sql($sql); $rs = $DB->get_recordset_sql($sql);
$submissions = []; $submissions = [];
foreach ($rs as $r) { foreach ($rs as $r) {
// Now, check if . // Now, check if .
$maxstatesql = "SELECT MAX(qas.sequencenumber) " $maxstatesql = "SELECT MAX(qas.sequencenumber)
. "FROM {question_attempt_steps} qas " FROM {question_attempt_steps} qas
. "WHERE qas.questionattemptid = {$r->attempt_id}"; WHERE qas.questionattemptid = {$r->attempt_id}";
$max = $DB->get_field_sql($maxstatesql); $max = $DB->get_field_sql($maxstatesql);
if ($r->sequencenumber == $max) { if ($r->sequencenumber == $max) {
$submissions[$r->userid] = true; // Set array index based on user id, to avoid checking if value is in array. $submissions[$r->userid] = true; // Set array index based on user id, to avoid checking if value is in array.

View file

@ -27,8 +27,8 @@ require_once($CFG->libdir.'/externallib.php');
class period { class period {
const TABLE = "local_treestudyplan_period"; const TABLE = "local_treestudyplan_period";
private static $CACHE = []; private static $cache = [];
private static $PAGECACHE = []; private static $pagecache = [];
private $r; // Holds database record. private $r; // Holds database record.
private $id; private $id;
@ -39,11 +39,11 @@ class period {
} }
// Cache constructors to avoid multiple creation events in one session. // Cache constructors to avoid multiple creation events in one session.
public static function findById($id): self { public static function find_by_id($id): self {
if (!array_key_exists($id, self::$CACHE)) { if (!array_key_exists($id, self::$cache)) {
self::$CACHE[$id] = new self($id); self::$cache[$id] = new self($id);
} }
return self::$CACHE[$id]; return self::$cache[$id];
} }
/** /**
@ -57,7 +57,7 @@ class period {
} }
try { try {
$id = $DB->get_field(self::TABLE, "id", ["page_id" => $page->id(), "period" => $periodnr], MUST_EXIST); $id = $DB->get_field(self::TABLE, "id", ["page_id" => $page->id(), "period" => $periodnr], MUST_EXIST);
$period = self::findById($id); $period = self::find_by_id($id);
} catch (\dml_missing_record_exception $x) { } catch (\dml_missing_record_exception $x) {
// Period does not exist - create one ... // Period does not exist - create one ...
// Make a best guess estimate of the start and end date, based on surrounding periods,. // Make a best guess estimate of the start and end date, based on surrounding periods,.
@ -84,7 +84,7 @@ class period {
$enddate = $DB->get_field(self::TABLE, "startdate", $enddate = $DB->get_field(self::TABLE, "startdate",
["page_id" => $page->id(), "period" => $periodnr + 1], MUST_EXIST); ["page_id" => $page->id(), "period" => $periodnr + 1], MUST_EXIST);
$pstart = strtotime($enddate) - (24 * 60 * 60); // Subtract one day. $pstart = strtotime($enddate) - (24 * 60 * 60); // Subtract one day.
} catch (\dml_missing_record_exception $x2) { } catch (\dml_missing_record_exception $x) {
// If not, do a fair guess. // If not, do a fair guess.
$pend = $pstart + $ptime; $pend = $pstart + $ptime;
} }
@ -103,24 +103,24 @@ class period {
} }
// Cache constructors to avoid multiple creation events in one session. // Cache constructors to avoid multiple creation events in one session.
public static function findForPage(studyplanpage $page): array { public static function find_for_page(studyplanpage $page): array {
if (!array_key_exists($page->id(), self::$PAGECACHE)) { if (!array_key_exists($page->id(), self::$pagecache)) {
$periods = []; $periods = [];
// Find and add the periods to an array with the period sequence as a key. // Find and add the periods to an array with the period sequence as a key.
for ($i = 1; $i <= $page->periods(); $i++) { for ($i = 1; $i <= $page->periods(); $i++) {
$period = self::find($page, $i); $period = self::find($page, $i);
$periods[$i] = $period; $periods[$i] = $period;
} }
self::$PAGECACHE[$page->id()] = $periods; self::$pagecache[$page->id()] = $periods;
} }
return self::$PAGECACHE[$page->id()]; return self::$pagecache[$page->id()];
} }
private function __construct($id) { private function __construct($id) {
global $DB; global $DB;
$this->id = $id; $this->id = $id;
$this->r = $DB->get_record(self::TABLE, ['id' => $id]); $this->r = $DB->get_record(self::TABLE, ['id' => $id]);
$this->page = studyplanpage::findById($this->r->page_id); $this->page = studyplanpage::find_by_id($this->r->page_id);
} }
public function id() { public function id() {
@ -184,7 +184,7 @@ class period {
/** /**
* Do not use directly to add periods, unless performing import * Do not use directly to add periods, unless performing import
* The static find() and findForPage() functions create the period if needed * The static find() and find_for_page() functions create the period if needed
*/ */
public static function add($fields) { public static function add($fields) {
global $DB; global $DB;
@ -208,8 +208,8 @@ class period {
} }
} }
$id = $DB->insert_record(self::TABLE, $info); $id = $DB->insert_record(self::TABLE, $info);
unset(self::$PAGECACHE[$fields['page_id']]); // Invalidate the cache for this page. unset(self::$pagecache[$fields['page_id']]); // Invalidate the cache for this page.
return self::findById($id); // Make sure the new page is immediately cached. return self::find_by_id($id); // Make sure the new page is immediately cached.
} }
public function edit($fields) { public function edit($fields) {
@ -224,14 +224,14 @@ class period {
$DB->update_record(self::TABLE, $info); $DB->update_record(self::TABLE, $info);
// Reload record after edit. // Reload record after edit.
$this->r = $DB->get_record(self::TABLE, ['id' => $this->id], "*", MUST_EXIST); $this->r = $DB->get_record(self::TABLE, ['id' => $this->id], "*", MUST_EXIST);
unset(self::$PAGECACHE[$this->r->page_id]); // Invalidate the cache for this page. unset(self::$pagecache[$this->r->page_id]); // Invalidate the cache for this page.
return $this; return $this;
} }
public function delete() { public function delete() {
global $DB; global $DB;
$DB->delete_records(self::TABLE, ['id' => $this->id]); $DB->delete_records(self::TABLE, ['id' => $this->id]);
unset(self::$PAGECACHE[$this->r->page_id]); // Invalidate the cache for this page. unset(self::$pagecache[$this->r->page_id]); // Invalidate the cache for this page.
return success::success(); return success::success();
} }
@ -241,7 +241,7 @@ class period {
public static function page_model(studyplanpage $page) { public static function page_model(studyplanpage $page) {
$model = []; $model = [];
foreach (self::findForPage($page) as $p) { foreach (self::find_for_page($page) as $p) {
$model[] = $p->model(); $model[] = $p->model();
} }
return $model; return $model;

View file

@ -35,8 +35,7 @@ use tool_dataprivacy\context_instance;
class provider implements \core_privacy\local\metadata\provider, class provider implements \core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider, \core_privacy\local\request\plugin\provider,
\core_privacy\local\request\core_userlist_provider \core_privacy\local\request\core_userlist_provider {
{
/** /**
* Get the language string identifier with the component's language * Get the language string identifier with the component's language
* file to explain why this plugin stores no data. * file to explain why this plugin stores no data.
@ -124,8 +123,7 @@ class provider implements \core_privacy\local\metadata\provider,
// Export studyplan associations. // Export studyplan associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT * FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE ( a.user_id = :userid AND s.context_id = :contextid) WHERE ( a.user_id = :userid AND s.context_id = :contextid)";
";
$records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); $records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) { foreach ($records as $r) {
static::export_studyplan_data_for_user($r); static::export_studyplan_data_for_user($r);
@ -193,10 +191,10 @@ class provider implements \core_privacy\local\metadata\provider,
foreach ($contextlist->get_contexts() as $context) { foreach ($contextlist->get_contexts() as $context) {
if ($context->contextlevel == CONTEXT_SYSTEM) { if ($context->contextlevel == CONTEXT_SYSTEM) {
$sql = "SELECT s.id FROM {local_treestudyplan} " $sql = "SELECT s.id FROM {local_treestudyplan}
. "INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id " INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
. "WHERE ( a.user_id = :userid " WHERE ( a.user_id = :userid
. "AND ( s.context_id IS NULL OR s.context_id == 0 OR s.context_id = :contextid))"; AND ( s.context_id IS NULL OR s.context_id == 0 OR s.context_id = :contextid))";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, "userid" => $user->id]); $planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, "userid" => $user->id]);
foreach ($planids as $planid) { foreach ($planids as $planid) {

View file

@ -115,7 +115,7 @@ class studentstudyplanservice extends \external_api {
public static function get_user_studyplan($userid, $studyplanid) { public static function get_user_studyplan($userid, $studyplanid) {
global $CFG, $DB; global $CFG, $DB;
$studyplan = studyplan::findById($studyplanid); $studyplan = studyplan::find_by_id($studyplanid);
webservicehelper::require_capabilities(self::CAP_VIEWOTHER, $studyplan->context()); webservicehelper::require_capabilities(self::CAP_VIEWOTHER, $studyplan->context());
if ($studyplan->has_linked_user($userid)) { if ($studyplan->has_linked_user($userid)) {

View file

@ -37,7 +37,7 @@ class studyitem {
public const TABLE = "local_treestudyplan_item"; public const TABLE = "local_treestudyplan_item";
private static $STUDYITEMCACHE = []; private static $studyitemcache = [];
private $r; // Holds database record. private $r; // Holds database record.
private $id; private $id;
@ -57,11 +57,11 @@ class studyitem {
return $this->r->conditions; return $this->r->conditions;
} }
public static function findById($id): self { public static function find_by_id($id): self {
if (!array_key_exists($id, self::$STUDYITEMCACHE)) { if (!array_key_exists($id, self::$studyitemcache)) {
self::$STUDYITEMCACHE[$id] = new self($id); self::$studyitemcache[$id] = new self($id);
} }
return self::$STUDYITEMCACHE[$id]; return self::$studyitemcache[$id];
} }
public function __construct($id) { public function __construct($id) {
@ -69,7 +69,7 @@ class studyitem {
$this->id = $id; $this->id = $id;
$this->r = $DB->get_record(self::TABLE, ['id' => $id], "*", MUST_EXIST); $this->r = $DB->get_record(self::TABLE, ['id' => $id], "*", MUST_EXIST);
$this->studyline = studyline::findById($this->r->line_id); $this->studyline = studyline::find_by_id($this->r->line_id);
$this->aggregator = $this->studyline()->studyplan()->aggregator(); $this->aggregator = $this->studyline()->studyplan()->aggregator();
} }
@ -128,7 +128,7 @@ class studyitem {
$model = [ $model = [
'id' => $this->r->id, // Id is needed in export model because of link references. 'id' => $this->r->id, // Id is needed in export model because of link references.
'type' => $this->isValid() ? $this->r->type : self::INVALID, 'type' => $this->valid() ? $this->r->type : self::INVALID,
'conditions' => $this->r->conditions, 'conditions' => $this->r->conditions,
'slot' => $this->r->slot, 'slot' => $this->r->slot,
'layer' => $this->r->layer, 'layer' => $this->r->layer,
@ -217,7 +217,7 @@ class studyitem {
} }
} }
$id = $DB->insert_record(self::TABLE, $info); $id = $DB->insert_record(self::TABLE, $info);
$item = self::findById($id); $item = self::find_by_id($id);
if ($item->type() == self::COURSE) { if ($item->type() == self::COURSE) {
// Signal the studyplan that a course has been added so it can be marked for csync cascading. // Signal the studyplan that a course has been added so it can be marked for csync cascading.
$item->studyline()->studyplan()->mark_csync_changed(); $item->studyline()->studyplan()->mark_csync_changed();
@ -242,7 +242,7 @@ class studyitem {
return $this; return $this;
} }
public function isValid() { public function valid() {
// Check if referenced courses, badges and/or competencies still exist. // Check if referenced courses, badges and/or competencies still exist.
if ($this->r->type == static::COURSE) { if ($this->r->type == static::COURSE) {
return courseinfo::exists($this->r->course_id); return courseinfo::exists($this->r->course_id);
@ -306,7 +306,7 @@ class studyitem {
$list = []; $list = [];
$ids = $DB->get_fieldset_select(self::TABLE, "id", "line_id = :line_id ORDER BY layer", ['line_id' => $line->id()]); $ids = $DB->get_fieldset_select(self::TABLE, "id", "line_id = :line_id ORDER BY layer", ['line_id' => $line->id()]);
foreach ($ids as $id) { foreach ($ids as $id) {
$item = self::findById($id, $line); $item = self::find_by_id($id, $line);
$list[] = $item; $list[] = $item;
} }
return $list; return $list;
@ -380,7 +380,7 @@ class studyitem {
// Add continuation_info if available. // Add continuation_info if available.
if (self::exists($this->r->continuation_id)) { if (self::exists($this->r->continuation_id)) {
$citem = self::findById($this->r->continuation_id); $citem = self::find_by_id($this->r->continuation_id);
$model['continuation'] = $citem->link_model($userid); $model['continuation'] = $citem->link_model($userid);
} }
@ -414,7 +414,7 @@ class studyitem {
private function completion($userid) { private function completion($userid) {
global $DB; global $DB;
if ($this->isValid()) { if ($this->valid()) {
if (strtolower($this->r->type) == 'course') { if (strtolower($this->r->type) == 'course') {
// Determine competency by competency completion. // Determine competency by competency completion.
$courseinfo = $this->getcourseinfo(); $courseinfo = $this->getcourseinfo();
@ -423,7 +423,7 @@ class studyitem {
// Does not need to use aggregator. // Does not need to use aggregator.
// Either true, or the completion of the reference. // Either true, or the completion of the reference.
if (self::exists($this->r->continuation_id)) { if (self::exists($this->r->continuation_id)) {
$citem = self::findById($this->r->continuation_id); $citem = self::find_by_id($this->r->continuation_id);
return $citem->completion($userid); return $citem->completion($userid);
} else { } else {
return completion::COMPLETED; return completion::COMPLETED;
@ -434,7 +434,7 @@ class studyitem {
// Retrieve incoming connections. // Retrieve incoming connections.
$incoming = $DB->get_records(studyitemconnection::TABLE, ['to_id' => $this->r->id]); $incoming = $DB->get_records(studyitemconnection::TABLE, ['to_id' => $this->r->id]);
foreach ($incoming as $conn) { foreach ($incoming as $conn) {
$item = self::findById($conn->from_id); $item = self::find_by_id($conn->from_id);
$incompleted[] = $item->completion($userid); $incompleted[] = $item->completion($userid);
} }
return $this->aggregator->aggregate_junction($incompleted, $this, $userid); return $this->aggregator->aggregate_junction($incompleted, $this, $userid);
@ -486,12 +486,12 @@ class studyitem {
$fields->line_id = $newline->id(); $fields->line_id = $newline->id();
// Create new record with the new data. // Create new record with the new data.
$id = $DB->insert_record(self::TABLE, (array)$fields); $id = $DB->insert_record(self::TABLE, (array)$fields);
$new = self::findById($id, $newline); $new = self::find_by_id($id, $newline);
// Copy the grading info if relevant. // Copy the grading info if relevant.
$gradables = gradeinfo::list_studyitem_gradables($this); $gradables = gradeinfo::list_studyitem_gradables($this);
foreach ($gradables as $g) { foreach ($gradables as $g) {
gradeinfo::include_grade($g->getGradeitem()->id, $new->id(), true); gradeinfo::include_grade($g->get_gradeitem()->id, $new->id(), true);
} }
return $new; return $new;
} }

View file

@ -50,11 +50,11 @@ class studyitemconnection {
} }
public function from_item() { public function from_item() {
return studyitem::findById($this->r->from_id); return studyitem::find_by_id($this->r->from_id);
} }
public function to_item() { public function to_item() {
return studyitem::findById($this->r->to_id); return studyitem::find_by_id($this->r->to_id);
} }
public function from_id() { public function from_id() {

View file

@ -45,7 +45,7 @@ class studyline {
public const TABLE = "local_treestudyplan_line"; public const TABLE = "local_treestudyplan_line";
private static $STUDYLINECACHE = []; private static $studylinecache = [];
private $r; // Holds database record. private $r; // Holds database record.
private $id; private $id;
@ -64,18 +64,18 @@ class studyline {
return $this->page; return $this->page;
} }
public static function findById($id): self { public static function find_by_id($id): self {
if (!array_key_exists($id, self::$STUDYLINECACHE)) { if (!array_key_exists($id, self::$studylinecache)) {
self::$STUDYLINECACHE[$id] = new self($id); self::$studylinecache[$id] = new self($id);
} }
return self::$STUDYLINECACHE[$id]; return self::$studylinecache[$id];
} }
private function __construct($id) { private function __construct($id) {
global $DB; global $DB;
$this->id = $id; $this->id = $id;
$this->r = $DB->get_record(self::TABLE, ['id' => $id]); $this->r = $DB->get_record(self::TABLE, ['id' => $id]);
$this->page = studyplanpage::findById($this->r->page_id); $this->page = studyplanpage::find_by_id($this->r->page_id);
$this->studyplan = $this->page->studyplan(); $this->studyplan = $this->page->studyplan();
} }
@ -194,7 +194,7 @@ class studyline {
} }
} }
$id = $DB->insert_record(self::TABLE, $info); $id = $DB->insert_record(self::TABLE, $info);
return self::findById($id); return self::find_by_id($id);
} }
public function edit($fields) { public function edit($fields) {
@ -249,7 +249,7 @@ class studyline {
$ids = $DB->get_fieldset_select(self::TABLE, "id", "page_id = :page_id ORDER BY sequence", $ids = $DB->get_fieldset_select(self::TABLE, "id", "page_id = :page_id ORDER BY sequence",
['page_id' => $page->id()]); ['page_id' => $page->id()]);
foreach ($ids as $id) { foreach ($ids as $id) {
$list[] = self::findById($id); $list[] = self::find_by_id($id);
} }
return $list; return $list;
} }
@ -305,7 +305,7 @@ class studyline {
$children = studyitem::find_studyline_children($this); $children = studyitem::find_studyline_children($this);
foreach ($children as $c) { foreach ($children as $c) {
if ($c->isValid()) { if ($c->valid()) {
$slotset = null; $slotset = null;
if ($c->slot() > 0) { if ($c->slot() > 0) {
if (in_array($c->type(), self::COMPETENCY_TYPES)) { if (in_array($c->type(), self::COMPETENCY_TYPES)) {
@ -336,7 +336,7 @@ class studyline {
$fields->studyplan_id = $newstudyplan->id(); $fields->studyplan_id = $newstudyplan->id();
// Create new record with the new data. // Create new record with the new data.
$id = $DB->insert_record(self::TABLE, (array)$fields); $id = $DB->insert_record(self::TABLE, (array)$fields);
$new = self::findById($id); $new = self::find_by_id($id);
// Next copy all the study items for this studyline. // Next copy all the study items for this studyline.
// And record the original and copy id's in the $translation array. // And record the original and copy id's in the $translation array.

View file

@ -28,7 +28,7 @@ require_once($CFG->libdir.'/externallib.php');
class studyplan { class studyplan {
const TABLE = "local_treestudyplan"; const TABLE = "local_treestudyplan";
private static $STUDYPLANCACHE = []; private static $studyplancache = [];
private $r; // Holds database record. private $r; // Holds database record.
private $id; private $id;
@ -42,11 +42,11 @@ class studyplan {
} }
// Cache constructors to avoid multiple creation events in one session. // Cache constructors to avoid multiple creation events in one session.
public static function findById($id): self { public static function find_by_id($id): self {
if (!array_key_exists($id, self::$STUDYPLANCACHE)) { if (!array_key_exists($id, self::$studyplancache)) {
self::$STUDYPLANCACHE[$id] = new self($id); self::$studyplancache[$id] = new self($id);
} }
return self::$STUDYPLANCACHE[$id]; return self::$studyplancache[$id];
} }
private function __construct($id) { private function __construct($id) {
@ -183,7 +183,7 @@ class studyplan {
// Get a list of available scales. // Get a list of available scales.
$scales = array_map( function($scale) { $scales = array_map( function($scale) {
return [ "id" => $scale->id, "name" => $scale->name, ]; return [ "id" => $scale->id, "name" => $scale->name, ];
}, \grade_scale::fetch_all(array('courseid' => 0)) ); }, \grade_scale::fetch_all(['courseid' => 0]) );
$model['advanced']['force_scales'] = [ $model['advanced']['force_scales'] = [
'scales' => $scales, 'scales' => $scales,
@ -204,7 +204,7 @@ class studyplan {
} }
} }
$id = $DB->insert_record(self::TABLE, $info); $id = $DB->insert_record(self::TABLE, $info);
$plan = self::findById($id); // Make sure the new studyplan is immediately cached. $plan = self::find_by_id($id); // Make sure the new studyplan is immediately cached.
// Start temporary skräpp code. // Start temporary skräpp code.
// Add a single page and copy the names.This keeps the data sane until the upgrade to . // Add a single page and copy the names.This keeps the data sane until the upgrade to .
@ -311,7 +311,7 @@ class studyplan {
} }
foreach ($ids as $id) { foreach ($ids as $id) {
$list[] = studyplan::findById($id); $list[] = self::find_by_id($id);
} }
return $list; return $list;
} }
@ -326,7 +326,7 @@ class studyplan {
} }
$ids = $DB->get_fieldset_select(self::TABLE, "id", $where, ["shortname" => $shortname, "contextid" => $contextid]); $ids = $DB->get_fieldset_select(self::TABLE, "id", $where, ["shortname" => $shortname, "contextid" => $contextid]);
foreach ($ids as $id) { foreach ($ids as $id) {
$list[] = studyplan::findById($id); $list[] = self::find_by_id($id);
} }
return $list; return $list;
} }
@ -338,20 +338,20 @@ class studyplan {
INNER JOIN {local_treestudyplan_cohort} j ON j.studyplan_id = s.id INNER JOIN {local_treestudyplan_cohort} j ON j.studyplan_id = s.id
INNER JOIN {cohort_members} cm ON j.cohort_id = cm.cohortid INNER JOIN {cohort_members} cm ON j.cohort_id = cm.cohortid
WHERE cm.userid = :userid"; WHERE cm.userid = :userid";
$cohortplan_ids = $DB->get_fieldset_sql($sql, ['userid' => $userid]); $cohortplanids = $DB->get_fieldset_sql($sql, ['userid' => $userid]);
$sql = "SELECT s.id FROM {local_treestudyplan} s $sql = "SELECT s.id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_user} j ON j.studyplan_id = s.id INNER JOIN {local_treestudyplan_user} j ON j.studyplan_id = s.id
WHERE j.user_id = :userid"; WHERE j.user_id = :userid";
$userplan_ids = $DB->get_fieldset_sql($sql, ['userid' => $userid]); $userplanids = $DB->get_fieldset_sql($sql, ['userid' => $userid]);
$plans = []; $plans = [];
foreach ($cohortplan_ids as $id) { foreach ($cohortplanids as $id) {
$plans[$id] = self::findById($id); $plans[$id] = self::find_by_id($id);
} }
foreach ($userplan_ids as $id) { foreach ($userplanids as $id) {
if (!array_key_exists($id, $plans)) { if (!array_key_exists($id, $plans)) {
$plans[$id] = self::findById($id); $plans[$id] = self::find_by_id($id);
} }
} }
@ -475,14 +475,14 @@ class studyplan {
} }
public static function duplicate_plan($planid, $name, $shortname) { public static function duplicate_plan($planid, $name, $shortname) {
$ori = self::findById($planid); $ori = self::find_by_id($planid);
$new = $ori->duplicate($name, $shortname); $new = $ori->duplicate($name, $shortname);
return $new->simple_model(); return $new->simple_model();
} }
public function duplicate($name, $shortname) { public function duplicate($name, $shortname) {
// First duplicate the studyplan structure. // First duplicate the studyplan structure.
$newplan = studyplan::add([ $newplan = self::add([
'name' => $name, 'name' => $name,
'shortname' => $shortname, 'shortname' => $shortname,
'description' => $this->r->description, 'description' => $this->r->description,

View file

@ -28,7 +28,7 @@ require_once($CFG->libdir.'/externallib.php');
class studyplanpage { class studyplanpage {
const TABLE = "local_treestudyplan_page"; const TABLE = "local_treestudyplan_page";
private static $CACHE = []; private static $cache = [];
private $r; // Holds database record. private $r; // Holds database record.
private $id; private $id;
@ -39,18 +39,18 @@ class studyplanpage {
} }
// Cache constructors to avoid multiple creation events in one session. // Cache constructors to avoid multiple creation events in one session.
public static function findById($id): self { public static function find_by_id($id): self {
if (!array_key_exists($id, self::$CACHE)) { if (!array_key_exists($id, self::$cache)) {
self::$CACHE[$id] = new self($id); self::$cache[$id] = new self($id);
} }
return self::$CACHE[$id]; return self::$cache[$id];
} }
private function __construct($id) { private function __construct($id) {
global $DB; global $DB;
$this->id = $id; $this->id = $id;
$this->r = $DB->get_record(self::TABLE, ['id' => $id]); $this->r = $DB->get_record(self::TABLE, ['id' => $id]);
$this->studyplan = studyplan::findById($this->r->studyplan_id); $this->studyplan = studyplan::find_by_id($this->r->studyplan_id);
} }
public function id() { public function id() {
@ -170,7 +170,7 @@ class studyplanpage {
} }
$id = $DB->insert_record(self::TABLE, $info); $id = $DB->insert_record(self::TABLE, $info);
return self::findById($id); // Make sure the new page is immediately cached. return self::find_by_id($id); // Make sure the new page is immediately cached.
} }
public function edit($fields) { public function edit($fields) {
@ -252,20 +252,20 @@ class studyplanpage {
$ids = $DB->get_fieldset_select(self::TABLE, "id", "studyplan_id = :plan_id ORDER BY startdate", $ids = $DB->get_fieldset_select(self::TABLE, "id", "studyplan_id = :plan_id ORDER BY startdate",
['plan_id' => $plan->id()]); ['plan_id' => $plan->id()]);
foreach ($ids as $id) { foreach ($ids as $id) {
$list[] = self::findById($id); $list[] = self::find_by_id($id);
} }
return $list; return $list;
} }
public static function duplicate_page($pageid, $name, $shortname) { public static function duplicate_page($pageid, $name, $shortname) {
$ori = self::findById($pageid); $ori = self::find_by_id($pageid);
$new = $ori->duplicate($name, $shortname); $new = $ori->duplicate($name, $shortname);
return $new->simple_model(); return $new->simple_model();
} }
public function duplicate($newstudyplan) { public function duplicate($newstudyplan) {
// First duplicate the studyplan structure. // First duplicate the studyplan structure.
$new = studyplanpage::add([ $new = self::add([
'studyplan_id' => $newstudyplan->id(), 'studyplan_id' => $newstudyplan->id(),
'fullname' => $this->r->fullname, 'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
@ -316,7 +316,7 @@ class studyplanpage {
} }
public function export_page_csv() { public function export_page_csv() {
$plist = period::findForPage($this); $plist = period::find_for_page($this);
$model = $this->editor_model(); $model = $this->editor_model();
@ -473,7 +473,7 @@ class studyplanpage {
} }
public function import_periods_model($model) { public function import_periods_model($model) {
$periods = period::findForPage($this); $periods = period::find_for_page($this);
foreach ($model as $pmodel) { foreach ($model as $pmodel) {
$pi = $pmodel["period"]; $pi = $pmodel["period"];
if (array_key_exists($pi, $periods)) { if (array_key_exists($pi, $periods)) {

View file

@ -86,7 +86,7 @@ class studyplanservice extends \external_api {
public static function get_studyplan_map($id) { public static function get_studyplan_map($id) {
if (isset($id) && $id > 0) { if (isset($id) && $id > 0) {
$studyplan = studyplan::findById($id); $studyplan = studyplan::find_by_id($id);
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $studyplan->context()); webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $studyplan->context());
return $studyplan->editor_model(); return $studyplan->editor_model();
} else { } else {
@ -112,7 +112,7 @@ class studyplanservice extends \external_api {
public static function get_studyline_map($id) { public static function get_studyline_map($id) {
$o = studyline::findById($id); $o = studyline::find_by_id($id);
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $o->context()); webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $o->context());
return $o->editor_model(); return $o->editor_model();
} }
@ -196,7 +196,7 @@ class studyplanservice extends \external_api {
// Do not validate the context in this case, just check the permissions. // Do not validate the context in this case, just check the permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, $context, false); webservicehelper::require_capabilities(self::CAP_EDIT, $context, false);
$o = studyplan::findById($id); $o = studyplan::find_by_id($id);
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
$o->edit([ $o->edit([
@ -232,7 +232,7 @@ class studyplanservice extends \external_api {
} }
public static function delete_studyplan($id, $force = false) { public static function delete_studyplan($id, $force = false) {
$o = studyplan::findById($id); $o = studyplan::find_by_id($id);
// Validate if the requesting user has the right to edit the plan in it's current context. // Validate if the requesting user has the right to edit the plan in it's current context.
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
return $o->delete(!!$force)->model(); return $o->delete(!!$force)->model();
@ -260,7 +260,7 @@ class studyplanservice extends \external_api {
public static function add_studyline($pageid, $name, $shortname, $color, $sequence) { public static function add_studyline($pageid, $name, $shortname, $color, $sequence) {
// Validate if the requesting user has the right to edit the plan in it's current context. // Validate if the requesting user has the right to edit the plan in it's current context.
$page = studyplanpage::findById($pageid); $page = studyplanpage::find_by_id($pageid);
webservicehelper::require_capabilities(self::CAP_EDIT, $page->studyplan()->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $page->studyplan()->context());
$o = studyline::add([ $o = studyline::add([
@ -293,7 +293,7 @@ class studyplanservice extends \external_api {
} }
public static function edit_studyline($id, $name, $shortname, $color) { public static function edit_studyline($id, $name, $shortname, $color) {
$o = studyline::findById($id); $o = studyline::find_by_id($id);
// Validate if the requesting user has the right to edit the plan in it's current context. // Validate if the requesting user has the right to edit the plan in it's current context.
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
@ -322,7 +322,7 @@ class studyplanservice extends \external_api {
} }
public static function delete_studyline($id) { public static function delete_studyline($id) {
$o = studyline::findById($id); $o = studyline::find_by_id($id);
// Validate if the requesting user has the right to edit the plan in it's current context. // Validate if the requesting user has the right to edit the plan in it's current context.
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
return $o->delete()->model(); return $o->delete()->model();
@ -353,7 +353,7 @@ class studyplanservice extends \external_api {
public static function reorder_studylines($resequence) { public static function reorder_studylines($resequence) {
// Validate if the requesting user has the right to edit the lines in it's current context. // Validate if the requesting user has the right to edit the lines in it's current context.
foreach ($resequence as $sq) { foreach ($resequence as $sq) {
$o = studyline::findById(($sq['id'])); $o = studyline::find_by_id(($sq['id']));
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
} }
@ -381,7 +381,7 @@ class studyplanservice extends \external_api {
} }
public static function get_studyitem($id) { public static function get_studyitem($id) {
$o = studyitem::findById($id); $o = studyitem::find_by_id($id);
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $o->context()); webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $o->context());
return $o->editor_model(); return $o->editor_model();
@ -414,7 +414,7 @@ class studyplanservice extends \external_api {
} }
public static function add_studyitem($lineid, $type, $details, $slot = -1, $layer = 0) { public static function add_studyitem($lineid, $type, $details, $slot = -1, $layer = 0) {
webservicehelper::require_capabilities(self::CAP_EDIT, studyline::findById($lineid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyline::find_by_id($lineid)->context());
$o = studyitem::add([ $o = studyitem::add([
'line_id' => $lineid, 'line_id' => $lineid,
@ -449,7 +449,7 @@ class studyplanservice extends \external_api {
public static function edit_studyitem($id, $conditions, $continuationid = false) { public static function edit_studyitem($id, $conditions, $continuationid = false) {
$o = studyitem::findById($id); $o = studyitem::find_by_id($id);
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
$config = [ $config = [
@ -490,7 +490,7 @@ class studyplanservice extends \external_api {
public static function reorder_studyitems($resequence) { public static function reorder_studyitems($resequence) {
// Check for permissions to modify the studyplan. // Check for permissions to modify the studyplan.
foreach ($resequence as $sq) { foreach ($resequence as $sq) {
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::findById(($sq['id']))->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id(($sq['id']))->context());
} }
return studyitem::reorder($resequence)->model(); return studyitem::reorder($resequence)->model();
@ -513,7 +513,7 @@ class studyplanservice extends \external_api {
} }
public static function delete_studyitem($id) { public static function delete_studyitem($id) {
$o = studyitem::findById($id); $o = studyitem::find_by_id($id);
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
return $o->delete()->model(); return $o->delete()->model();
@ -538,8 +538,8 @@ class studyplanservice extends \external_api {
public static function connect_studyitems($fromid, $toid) { public static function connect_studyitems($fromid, $toid) {
// Validate permissions. // Validate permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::findById($fromid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($fromid)->context());
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::findById($toid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($toid)->context());
$o = studyitemconnection::connect($fromid, $toid); $o = studyitemconnection::connect($fromid, $toid);
return $o->model(); return $o->model();
@ -564,8 +564,8 @@ class studyplanservice extends \external_api {
public static function disconnect_studyitems($fromid, $toid) { public static function disconnect_studyitems($fromid, $toid) {
// Validate permissions. // Validate permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::findById($fromid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($fromid)->context());
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::findById($toid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($toid)->context());
return studyitemconnection::disconnect($fromid, $toid)->model(); return studyitemconnection::disconnect($fromid, $toid)->model();
} }
@ -622,12 +622,12 @@ class studyplanservice extends \external_api {
global $USER; global $USER;
// Find related course and course context. // Find related course and course context.
$coursecontext = gradeinfo::getCourseContextById($gradeid); $coursecontext = gradeinfo::get_coursecontext_by_id($gradeid);
// Do sanity checks. // Do sanity checks.
\external_api::validate_context($coursecontext); \external_api::validate_context($coursecontext);
// Check correct capabilities. // Check correct capabilities.
if (has_capability('local/treestudyplan:editstudyplan', studyitem::findById($itemid)->context()) || if (has_capability('local/treestudyplan:editstudyplan', studyitem::find_by_id($itemid)->context()) ||
is_enrolled($coursecontext, $USER, 'local/treestudyplan:selectowngradables')) { is_enrolled($coursecontext, $USER, 'local/treestudyplan:selectowngradables')) {
return gradeinfo::include_grade($gradeid, $itemid, $include, $required)->model(); return gradeinfo::include_grade($gradeid, $itemid, $include, $required)->model();
} else { } else {
@ -684,7 +684,7 @@ class studyplanservice extends \external_api {
$dbman = $DB->get_manager(); $dbman = $DB->get_manager();
// Validate permissions. // Validate permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::findById($studyplanid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
$list = []; $list = [];
// Check if scaleid is valid. // Check if scaleid is valid.
@ -705,13 +705,13 @@ class studyplanservice extends \external_api {
foreach ($records as $itemr) { foreach ($records as $itemr) {
$studyitem = new studyitem($itemr->id); $studyitem = new studyitem($itemr->id);
if ($studyitem->isValid() && $studyitem->type() == studyitem::COURSE) { if ($studyitem->valid() && $studyitem->type() == studyitem::COURSE) {
$courseinfo = $studyitem->getcourseinfo(); $courseinfo = $studyitem->getcourseinfo();
$gradables = gradeinfo::list_studyitem_gradables($studyitem); $gradables = gradeinfo::list_studyitem_gradables($studyitem);
$gradelist = []; $gradelist = [];
foreach ($gradables as $g) { foreach ($gradables as $g) {
$gi = $g->getGradeItem(); $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. .
@ -827,7 +827,7 @@ class studyplanservice extends \external_api {
global $DB; global $DB;
// Validate permissions. // Validate permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::findById($studyplanid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
// Find studyline id's. // Find studyline id's.
$studylineids = $DB->get_fieldset_select(studyline::TABLE, "id", "studyplan_id = :plan_id", ['plan_id' => $studyplanid]); $studylineids = $DB->get_fieldset_select(studyline::TABLE, "id", "studyplan_id = :plan_id", ['plan_id' => $studyplanid]);
@ -837,7 +837,7 @@ class studyplanservice extends \external_api {
foreach ($records as $itemr) { foreach ($records as $itemr) {
$studyitem = new studyitem($itemr->id); $studyitem = new studyitem($itemr->id);
if ($studyitem->isValid() && $studyitem->type() == studyitem::COURSE) { if ($studyitem->valid() && $studyitem->type() == studyitem::COURSE) {
$record = $DB->get_record("course_format_options", $record = $DB->get_record("course_format_options",
["courseid" => $studyitem->courseid(), "name" => "automaticenddate"]); ["courseid" => $studyitem->courseid(), "name" => "automaticenddate"]);
if ($record && $record->value) { if ($record && $record->value) {
@ -870,7 +870,7 @@ class studyplanservice extends \external_api {
public static function duplicate_plan($studyplanid, $name, $shortname) { public static function duplicate_plan($studyplanid, $name, $shortname) {
// Validate permissions. // Validate permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::findById($studyplanid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
return studyplan::duplicate_plan($studyplanid, $name, $shortname); return studyplan::duplicate_plan($studyplanid, $name, $shortname);
} }
@ -895,9 +895,9 @@ class studyplanservice extends \external_api {
public static function export_plan($studyplanid, $format = "json") { public static function export_plan($studyplanid, $format = "json") {
try { try {
// Validate permissions. // Validate permissions.
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::findById($studyplanid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
$plan = studyplan::findById($studyplanid); $plan = studyplan::find_by_id($studyplanid);
if ($format == "csv") { if ($format == "csv") {
// FIXME: Make sure this webservice function gets called for the page instead of the studyplan. // FIXME: Make sure this webservice function gets called for the page instead of the studyplan.
return $plan->pages()[0]->export_page_csv(); return $plan->pages()[0]->export_page_csv();
@ -923,8 +923,8 @@ class studyplanservice extends \external_api {
$systemcontext = webservicehelper::system_context(); $systemcontext = webservicehelper::system_context();
try { try {
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::findById($studyplanid)->context()); webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
$plan = studyplan::findById($studyplanid); $plan = studyplan::find_by_id($studyplanid);
// FIXME: Make sure this gets called for the page instead of the studyplan. // FIXME: Make sure this gets called for the page instead of the studyplan.
return $plan->pages()[0]->export_studylines(); return $plan->pages()[0]->export_studylines();
} catch (\webservice_access_exception $x) { } catch (\webservice_access_exception $x) {
@ -978,7 +978,7 @@ class studyplanservice extends \external_api {
public static function import_studylines($studyplanid, $content, $format = "application/json") { public static function import_studylines($studyplanid, $content, $format = "application/json") {
try { try {
$plan = studyplan::findById($studyplanid); $plan = studyplan::find_by_id($studyplanid);
// Validate import context. // Validate import context.
webservicehelper::require_capabilities(self::CAP_EDIT, $plan->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $plan->context());
@ -1091,7 +1091,7 @@ class studyplanservice extends \external_api {
public static function edit_period($id, $fullname, $shortname, $startdate, $enddate) { public static function edit_period($id, $fullname, $shortname, $startdate, $enddate) {
$p = period::findById($id); $p = period::find_by_id($id);
webservicehelper::require_capabilities(self::CAP_EDIT, $p->page()->studyplan()->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $p->page()->studyplan()->context());
$p->edit([ $p->edit([
@ -1124,7 +1124,7 @@ class studyplanservice extends \external_api {
public static function course_period_timing($periodid, $courseid, $span = 1) { public static function course_period_timing($periodid, $courseid, $span = 1) {
global $DB; global $DB;
$period = period::findById($periodid); $period = period::find_by_id($periodid);
$periodnr = $period->period(); $periodnr = $period->period();
$page = $period->page(); $page = $period->page();
// Check for studyplan edit permissions. // Check for studyplan edit permissions.
@ -1135,7 +1135,7 @@ class studyplanservice extends \external_api {
if (webservicehelper::has_capabilities("moodle/course:update", $coursecontext)) { if (webservicehelper::has_capabilities("moodle/course:update", $coursecontext)) {
// Get the proper list of all the periods for this page. // Get the proper list of all the periods for this page.
$periods = period::findForPage($page); $periods = period::find_for_page($page);
$pstart = $periods[$periodnr]; $pstart = $periods[$periodnr];
@ -1187,7 +1187,7 @@ class studyplanservice extends \external_api {
} }
public static function set_studyitem_span($id, $span = null) { public static function set_studyitem_span($id, $span = null) {
$o = studyitem::findById($id); $o = studyitem::find_by_id($id);
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context()); webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
$config = [ 'span' => $span]; $config = [ 'span' => $span];

View file

@ -37,7 +37,7 @@ class teachingfinder {
} }
$list = []; $list = [];
foreach ($records as $r) { foreach ($records as $r) {
$list[] = studyplan::findById($r->studyplan_id); $list[] = studyplan::find_by_id($r->studyplan_id);
} }
return $list; return $list;
} }

View file

@ -72,7 +72,7 @@ if (!empty($options["all"])) {
} }
$generator = new gradegenerator(); $generator = new gradegenerator();
$generator->fromFile($options["file"]); $generator->from_file($options["file"]);
cli_writeln(count($plans)." studyplans found:"); cli_writeln(count($plans)." studyplans found:");
foreach ($plans as $plan) { foreach ($plans as $plan) {
@ -80,7 +80,7 @@ foreach ($plans as $plan) {
$users = $plan->find_linked_users(); $users = $plan->find_linked_users();
foreach ($users as $u) { foreach ($users as $u) {
$generator->addstudent($u->username); $generator->addstudent($u->username);
$generator->addUserNameInfo($u->username, $u->firstname, $u->lastname); $generator->add_username_info($u->username, $u->firstname, $u->lastname);
cli_writeln(" - {$u->firstname} {$u->lastname} / {$u->username}"); cli_writeln(" - {$u->firstname} {$u->lastname} / {$u->username}");
} }
@ -96,4 +96,4 @@ foreach ($plans as $plan) {
} }
} }
$generator->toFile($options["file"]); $generator->to_file($options["file"]);

View file

@ -97,7 +97,7 @@ if (!empty($options["all"])) {
} }
$generator = new gradegenerator(); $generator = new gradegenerator();
$generator->fromFile($options["file"]); $generator->from_file($options["file"]);
$assignments = []; $assignments = [];
@ -125,7 +125,7 @@ foreach ($plans as $plan) {
$gen = $generator->generate($u->username, $line->shortname(), $gradables); $gen = $generator->generate($u->username, $line->shortname(), $gradables);
foreach ($gen as $gg) { foreach ($gen as $gg) {
$g = $gg->gi; $g = $gg->gi;
$gi = $g->getGradeitem(); $gi = $g->get_gradeitem();
$name = $gi->itemname; $name = $gi->itemname;
$grade = $gg->gradetext; $grade = $gg->gradetext;
@ -184,4 +184,4 @@ foreach ($plans as $plan) {
} }
} }
$generator->toFile($options["file"]); $generator->to_file($options["file"]);