Skip to content

Commit 6892da7

Browse files
committed
code_review
1 parent 70f5bd5 commit 6892da7

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

classes/condition.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,20 @@ public function is_available($not, \core_availability\info $info, $grabthelot, $
102102
if (isset($course->lang) && $course->lang === $this->languageid) {
103103
$allow = true;
104104
} else {
105-
if ($userid === $USER->id) {
105+
$language = match (true) {
106106
// Checking the language of the currently logged in user, so do not
107107
// default to the account language, because the session language
108108
// or the language of the current course may be different.
109-
$language = current_language();
110-
} else {
111-
if (is_null($userid)) {
112-
// Fall back to site language or English.
113-
$language = $CFG->lang;
114-
} else {
115-
// Checking access for someone else than the logged in user, so
116-
// use the preferred language of that user account.
117-
// This language is never empty as there is a not-null constraint.
118-
$language = \core_user::get_user($userid)->lang;
119-
}
120-
}
109+
$userid === $USER->id => current_language(),
110+
111+
// When the userid is null then fall back to site language.
112+
is_null($userid) => $CFG->lang,
113+
114+
// Checking access for someone else than the logged in user, so
115+
// use the preferred language of that user account.
116+
// This language is never empty as there is a not-null constraint.
117+
default => \core_user::get_user($userid)->lang,
118+
};
121119

122120
if ($language === $this->languageid) {
123121
$allow = true;

tests/condition_test.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ public function test_save(): void {
198198
public function test_get_description(): void {
199199
$mockinfo = new mock_info();
200200
$language = new condition((object)['type' => 'language', 'id' => '']);
201-
$this->assertEquals($language->get_description(false, false, $mockinfo), '');
201+
$this->assertSame($language->get_description(false, false, $mockinfo), '');
202+
$language = new condition((object)['type' => 'language', 'id' => 'xw']);
203+
$this->assertSame($language->get_description(false, false, $mockinfo), '');
202204
$language = new condition((object)['type' => 'language', 'id' => 'en']);
203205
$desc = $language->get_description(true, false, $mockinfo);
204206
$this->assertEquals('The student\'s language is English ‎(en)‎', $desc);
@@ -230,6 +232,8 @@ public function test_is_available(): void {
230232
$condition = new condition((object)['type' => 'language', 'id' => 'en']);
231233
$this->assertTrue($condition->is_available(false, $infomodule, false, $user->id));
232234
$this->assertFalse($condition->is_available(true, $infomodule, false, $user->id));
235+
$this->setAdminUser();
236+
$this->assertNotEmpty($USER);
233237
}
234238

235239
/**

0 commit comments

Comments
 (0)