Skip to content
4 changes: 2 additions & 2 deletions lbplanner/classes/enums/CAPABILITY_FLAG_ORNONE.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class CAPABILITY_FLAG_ORNONE extends CAPABILITY_FLAG {
* @link CAPABILITY
*/
public static function to_capability(int $num): string {
if ($num === 0) { // TODO: put in translation strings.
throw new \coding_exception('0 means the absence of capabilities, and thus cannot be converted to a capability');
if ($num === 0) {
throw new \coding_exception(get_string('err_enum_capability_none', 'local_lbplanner'));
}
return parent::to_capability($num);
}
Expand Down
4 changes: 2 additions & 2 deletions lbplanner/classes/helpers/config_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static function add_customfield(): void {

$fieldcontroller = field_controller::create(0, $record, $categorycontroller);
// Added the default attributes for the custom field.
$fieldcontroller->set('name', 'LB Planer Task Type');
$fieldcontroller->set('description', 'Tracks whether the task is GK/EK/TEST/M');
$fieldcontroller->set('name', get_string('cf_name', 'local_lbplanner'));
$fieldcontroller->set('description', get_string('cf_description', 'local_lbplanner'));
$fieldcontroller->set('type', 'select');
// Because moodle wants me to save the configdata as a json string, I have to do this.
// I don't know why moodle does this, but it does. I don't like it. but I have to do it. so I do it.
Expand Down
3 changes: 2 additions & 1 deletion lbplanner/classes/helpers/invite_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static function structure(): external_single_structure {
public static function assert_invite_pending(int $status) {
if ($status !== PLAN_INVITE_STATE::PENDING) {
$name = PLAN_INVITE_STATE::name_from($status);
throw new \moodle_exception("Invite already {$name}");
$translatedname = get_string('invite_state_' . strtolower($name), 'local_lbplanner');
throw new \moodle_exception(get_string('err_invite_alr', 'local_lbplanner', $translatedname));
}
}
}
7 changes: 4 additions & 3 deletions lbplanner/classes/helpers/modules_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,17 @@ public static function determine_uinified_grade(int $grade, int $maxgrade, int $
* @throws \moodle_exception
*/
public static function determine_type(int $cmid): int {
// TODO: cache category controller.
$catid = config_helper::get_category_id();
if ($catid === -1) {
throw new \moodle_exception('couldn\'t find custom fields category ID');
throw new \moodle_exception(get_string('err_cf_nocatid', 'local_lbplanner'));
}
$categorycontroller = category_controller::create($catid);
$instancedata = $categorycontroller->get_handler()->get_instance_data($cmid);
if (count($instancedata) === 0) {
throw new \moodle_exception("couldn't find any instance data for module ID {$cmid} in category ID {$catid}");
throw new \moodle_exception(get_string('err_cf_nodata', 'local_lbplanner', ['cmid' => $cmid, 'catid' => $catid]));
} else if (count($instancedata) > 1) {
throw new \moodle_exception("found multiple data for module ID {$cmid} in category ID {$catid}");
throw new \moodle_exception(get_string('err_cf_multidata', 'local_lbplanner', ['cmid' => $cmid, 'catid' => $catid]));
}
$type = intval($instancedata[array_key_last($instancedata)]->get_value()) - 1;
if ($type === -1) {
Expand Down
8 changes: 4 additions & 4 deletions lbplanner/classes/helpers/plan_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,19 @@ public static function copy_plan(int $planid, string $username): int {
public static function remove_user(int $planid, int $userid, int $removeuserid): int {
global $DB; // TODO: use global user object.
if (self::get_owner($planid) != $userid) {
throw new \moodle_exception('Access denied');
throw new \moodle_exception(get_string('err_accessdenied', 'local_lbplanner'));
}

if (self::get_plan_id($removeuserid) != $planid) {
throw new \moodle_exception('Cannot remove user from other plan');
throw new \moodle_exception(get_string('err_plan_cantremove_userfromother', 'local_lbplanner'));
}

if ($userid == $removeuserid) {
throw new \moodle_exception('Cannot remove yourself');
throw new \moodle_exception(get_string('err_plan_cantremove_yourself', 'local_lbplanner'));
}

if (self::get_access_type($removeuserid, $planid) === PLAN_ACCESS_TYPE::OWNER) {
throw new \moodle_exception('Cannot remove owner');
throw new \moodle_exception(get_string('err_plan_cantremove_owner', 'local_lbplanner'));
}

$newplanid = self::copy_plan($planid, user_helper::get_mdluser($removeuserid)->username);
Expand Down
2 changes: 1 addition & 1 deletion lbplanner/classes/helpers/sentry_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function init(): void {
public static function transaction_start(string $name, string $op, ?array $data = null): ?Transaction {
if (self::enabled()) {
if (SentrySdk::getCurrentHub()->getSpan() !== null) {
throw new \coding_exception('tried to start a new sentry transaction when there\'s already a span set');
throw new \coding_exception(get_string('err_sentry_transactcoll', 'local_lbplanner'));
}
$ctx = TransactionContext::make()
->setName($name)
Expand Down
6 changes: 3 additions & 3 deletions lbplanner/classes/helpers/slot_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static function get_reservation(int $reservationid): reservation {
$reservation = $DB->get_record(self::TABLE_RESERVATIONS, ['id' => $reservationid]);

if ($reservation === false) {
throw new \moodle_exception('requested reservation does not exist');
throw new \moodle_exception(get_string('err_slot_reservnoexist', 'local_lbplanner', $reservationid));
}

return reservation::from_obj($reservation);
Expand Down Expand Up @@ -395,7 +395,7 @@ public static function calculate_slot_datetime(slot $slot, DateTimeImmutable $no
// Move to next day this weekday occurs (doesn't move if it's the same as today).
$slotdatetime = $now->modify('this ' . WEEKDAY::name_from($slot->weekday) . " {$slotdaytime}");
if ($slotdatetime === false) {
throw new \coding_exception('error while calculating slot datetime');
throw new \coding_exception(get_string('err_slot_calcdatetime', 'local_lbplanner'));
}

// Check if slot is before now (because time of day and such) and move it a week into the future if so.
Expand Down Expand Up @@ -449,7 +449,7 @@ public static function check_slot_supervisor(int $supervisorid, int $slotid): bo
*/
public static function assert_slot_supervisor(int $supervisorid, int $slotid): void {
if (!self::check_slot_supervisor($supervisorid, $slotid)) {
throw new \moodle_exception('Insufficient Permission: you\'re not supervisor of this slot');
throw new \moodle_exception(get_string('err_slot_urnotsupervisor', 'local_lbplanner'));
}
}
}
2 changes: 1 addition & 1 deletion lbplanner/classes/helpers/user_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function get_user(int $userid): user {

// Create empty plan for newly registered user.
$plan = new \stdClass();
$plan->name = 'Plan for ' . ($eduplanneruser->get_mdluser()->username);
$plan->name = get_string('plan_defaultname', 'local_lbplanner', $eduplanneruser->get_mdluser()->username);
$planid = $DB->insert_record(plan_helper::TABLE, $plan);
$eduplanneruser->set_planid($planid);

Expand Down
11 changes: 6 additions & 5 deletions lbplanner/classes/model/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class course {
* @param bool $enabled whether the course is enabled
*/
public function __construct(int $id, int $courseid, int $userid, string $shortname, string $color, bool $enabled) {
// TODO: private constructor, remove checks, separate public function with checks.
$this->id = $id;
$this->courseid = $courseid;
$this->userid = $userid;
Expand Down Expand Up @@ -110,7 +111,7 @@ public function set_fresh(int $id) {
*/
public function set_color(string $color) {
if ($color[0] !== '#') {
throw new \coding_exception("incorrect color format - must be either #RGB or #RRGGBB, got \"{$color}\" instead");
throw new \coding_exception(get_string('err_color_wrongformat', 'local_lbplanner', $color));
}
$len = strlen($color);
if ($len === 4) {
Expand All @@ -122,11 +123,11 @@ public function set_color(string $color) {
// Format #RRGGBB.
$rrggbb = $color;
} else {
throw new \coding_exception("incorrect color format - got incorrect length of {$len}");
throw new \coding_exception(get_string('err_color_wronglength', 'local_lbplanner', $len));
}
$rrggbb = strtoupper($rrggbb);
if (preg_match('/^#[1-9A-F]{6}$/', $rrggbb) === false) {
throw new \coding_exception("incorrect color format - found non-hexadecimal character in color \"{$color}\"");
throw new \coding_exception(get_string('err_color_nonhexadecimal', 'local_lbplanner', $color));
}
$this->color = $rrggbb;
}
Expand All @@ -139,7 +140,7 @@ public function set_color(string $color) {
public function set_shortname(string $shortname) {
$length = strlen($shortname);
if ($length > 5 || $length < 0) {
throw new \moodle_exception("shortname length must be <=5 and >0, but is {$length} instead");
throw new \moodle_exception(get_string('err_course_shortnamelength', 'local_lbplanner', $length));
}
$this->shortname = $shortname;
}
Expand Down Expand Up @@ -173,7 +174,7 @@ public static function prepare_shortname(string $shortname): string {
*/
public function set_mdlcourse(\stdClass $mdlcourse): void {
if ($this->mdlcourse !== null) {
throw new \coding_exception('tried to set cached mdluser twice');
throw new \coding_exception(get_string('err_doublechacheset', 'local_lbplanner', 'mdlcourse'));
}
$this->mdlcourse = $mdlcourse;
}
Expand Down
16 changes: 9 additions & 7 deletions lbplanner/classes/model/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function get_assignid(): int {
if ($this->cmid !== null) {
$this->assignid = $this->get_cmobj()->instance;
} else {
throw new \coding_exception('requested assignid, but no cmid');
throw new \coding_exception(get_string('err_mod_assnocmid', 'local_lbplanner'));
}
}
return $this->assignid;
Expand Down Expand Up @@ -185,11 +185,11 @@ public function get_cmobj(): \stdClass {
['id' => $this->cmid, 'module' => modules_helper::get_assign_module_id()]
);
if ($res === false) {
throw new \moodle_exception("couldn't get course module with cmid {$this->cmid}");
throw new \moodle_exception(get_string('err_mod_cmidnocm', 'local_lbplanner', $this->cmid));
}
} else {
if ($this->assignid === null) {
throw new \coding_exception('tried to query cmid on a module object without assignid');
throw new \coding_exception(get_string('err_mod_cmidnoass', 'local_lbplanner'));
}
$courseid = $this->get_courseid();
$res = $DB->get_record(
Expand All @@ -201,9 +201,11 @@ public function get_cmobj(): \stdClass {
]
);
if ($res === false) {
throw new \moodle_exception(
"couldn't get course module with assignid {$this->assignid} and courseid {$courseid}"
);
throw new \moodle_exception(get_string(
'err_mod_assnocm',
'local_lbplanner',
['assignid' => $this->assignid, 'courseid' => $courseid],
));
}
}
$this->cmobj = $res;
Expand Down Expand Up @@ -243,7 +245,7 @@ public function get_courseid(): int {
if ($this->cmid !== null) {
$viacm = true;
} else if ($this->cmid === null) {
throw new \coding_exception('invalid module model: neither cmid nor assignid defined');
throw new \coding_exception(get_string('err_mod_nocmidnorass', 'local_lbplanner'));
}
}
}
Expand Down
42 changes: 10 additions & 32 deletions lbplanner/classes/model/slot.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,16 @@ class slot {
*/
public function __construct(int $id, int $startunit, int $duration, int $weekday, string $room, int $size) {
$this->id = $id;
if ($startunit <= 0 || $startunit > slot_helper::SCHOOL_UNIT_MAX) {
throw new \moodle_exception(
"slot's startunit must be >0 and <="
. slot_helper::SCHOOL_UNIT_MAX
. ", but is {$startunit}"
);
}
if ($duration <= 0 || ($startunit + $duration - 1) > slot_helper::SCHOOL_UNIT_MAX) {
throw new \moodle_exception(
"slot's duration must be >0 and can't exceed "
. slot_helper::SCHOOL_UNIT_MAX
. " with startunit, but is {$duration}"
);
}
$this->startunit = $startunit;
$this->duration = $duration;
$this->weekday = WEEKDAY::from($weekday);
if (strlen($room) <= 0 && strlen($room) > slot_helper::ROOM_MAXLENGTH) {
throw new \moodle_exception(
"room name's length must be >0 and <="
. slot_helper::ROOM_MAXLENGTH
. ", but is "
. strlen($room)
);
}
$this->room = $room;
if ($size < 0) {
throw new \moodle_exception('room size must be >0');
}
$this->size = $size;
$this->fullness = null;
$this->forcuruser = null;
$this->supervisors = null;
$this->filters = null;
$this->validate();
}

/**
Expand Down Expand Up @@ -161,27 +137,29 @@ public function validate(): void {
static $maxunit = slot_helper::SCHOOL_UNIT_MAX;
// Validating startunit.
if ($this->startunit < 1) {
throw new moodle_exception('can\'t have a start unit smaller than 1');
throw new moodle_exception(get_string('err_slot_startunittoosmall', 'local_lbplanner'));
} else if ($this->startunit > $maxunit) {
throw new moodle_exception("can't have a start unit larger than {$maxunit}");
throw new moodle_exception(get_string('err_slot_startunittoolarge', 'local_lbplanner'));
}
// Validating duration.
if ($this->duration < 1) {
throw new moodle_exception('duration must be at least 1');
throw new moodle_exception(get_string('err_slot_durationtoosmall', 'local_lbplanner'));
} else if ($this->startunit + $this->duration > $maxunit) {
throw new moodle_exception("slot goes past the max unit {$maxunit}");
throw new moodle_exception(get_string('err_slot_durationtoolarge', 'local_lbplanner', $maxunit));
}
// Validating weekday.
WEEKDAY::from($this->weekday);
// Validating room.
if (strlen($this->room) <= 1) {
throw new moodle_exception('room name has to be at least 2 characters long');
throw new moodle_exception(get_string('err_slot_roomnametooshort', 'local_lbplanner'));
} else if (strlen($this->room) > slot_helper::ROOM_MAXLENGTH) {
throw new moodle_exception('room name has a maximum of ' . slot_helper::ROOM_MAXLENGTH . ' characters');
throw new moodle_exception(
get_string('err_slot_roomnametoolong', 'local_lbplanner', slot_helper::ROOM_MAXLENGTH)
);
}
// Validating size.
if ($this->size < 0) {
throw new moodle_exception('can\'t have a negative size for a slot');
throw new moodle_exception(get_string('err_slot_roomsizetoosmall', 'local_lbplanner'));
}
}

Expand Down
5 changes: 5 additions & 0 deletions lbplanner/classes/model/slot_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class slot_filter {
public function __construct(int $id, int $slotid, int $courseid, string $vintage) {
assert(strlen($vintage) <= 7);

// Ensure that either $courseid or $vintage are non-null.
if (is_null($courseid) && is_null($vintage)) {
throw new \moodle_exception(get_string('err_slotfilter_bothnull', 'local_lbplanner'));
}

$this->id = $id;
$this->slotid = $slotid;
$this->courseid = $courseid;
Expand Down
4 changes: 2 additions & 2 deletions lbplanner/classes/model/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function set_mdluser(\stdClass $mdluser): void {
global $USER;
if ($this->mdluser !== null) {
if ($this->mdluser->id !== $USER->id) {
throw new \coding_exception('tried to set cached mdluser twice');
throw new \coding_exception(get_string('err_doublechacheset', 'local_lbplanner', 'mdluser'));
}
}
$this->mdluser = $mdluser;
Expand All @@ -264,7 +264,7 @@ public function get_mdluser(): \stdClass {
*/
public function set_planid(int $planid): void {
if ($this->planid !== null) {
throw new \coding_exception('tried to set cached planid twice');
throw new \coding_exception(get_string('err_doublechacheset', 'local_lbplanner', 'planid'));
}
$this->planid = $planid;
}
Expand Down
6 changes: 4 additions & 2 deletions lbplanner/classes/polyfill/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private static function find_from_name(string $name, bool $try): ?EnumCase {
if ($try) {
return null;
} else {
throw new \moodle_exception("name {$name} doesn't exist in " . static::class);
throw new \moodle_exception(
get_string('err_enum_namemissing', 'local_lbplanner', ['name' => $name, 'classname' => static::class])
);
}
}
/**
Expand Down Expand Up @@ -163,7 +165,7 @@ public static function format(): string {
} else if (is_int($case->value)) {
$formattedval = $case->value;
} else {
throw new moodle_exception('unimplemented case value type for Enum::format()');
throw new moodle_exception(get_string('err_enum_casevaluetype_unimp', 'local_lbplanner'));
}
$result .= "{$formattedval}=>{$case->name},";
}
Expand Down
4 changes: 2 additions & 2 deletions lbplanner/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@

$deprecatedcapabilities = [
'local/lb_planner:admin' => [
'message' => 'this capability was removed because of internal changes making it unnecessary',
'message' => get_string('capability_deprecated_unnecessary', 'local_lbplanner'),
],
'local/lb_planner:manager' => [
'message' => 'this capability was removed because of internal changes making it unnecessary',
'message' => get_string('capability_deprecated_unnecessary', 'local_lbplanner'),
],
];
Loading
Loading