Skip to content
18 changes: 10 additions & 8 deletions classes/local/hook_callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ public static function handle_additional_user_restriction(additional_user_restri
return;
}
$aiconfig = new aiconfig($coursecontext->id);
if (
!$aiconfig->record_exists()
|| !$aiconfig->is_enabled()
|| !in_array($hook->get_purpose()->get_plugin_name(), $aiconfig->get_enabledpurposes())
) {
if (!has_capability('block/ai_control:control', $coursecontext)) {
$hook->set_access_allowed(false, 403, get_string('notallowedincourse', 'block_ai_control'));
}

$message = null;
if (!$aiconfig->record_exists() || !$aiconfig->is_enabled()) {
$message = get_string('noaiincourse', 'block_ai_control');
} else if (!in_array($hook->get_purpose()->get_plugin_name(), $aiconfig->get_enabledpurposes())) {
$message = get_string('notallowedincourse', 'block_ai_control', $hook->get_purpose()->get_plugin_name());
}

if ($message !== null && !has_capability('block/ai_control:control', $coursecontext)) {
$hook->set_access_allowed(false, 403, $message);
}
}
}
3 changes: 2 additions & 1 deletion lang/en/block_ai_control.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
$string['infotext'] = 'Warning hint when enabling AI functionalities';
$string['infotextdesc'] = 'Here you can provide a warning message that will be displayed when AI functions are about to be enabled. For example, you could inform trainers that they have to make sure that participants are properly informed about data protection and usage of the AI tools.';
$string['infotextmodalheading'] = 'Warning';
$string['notallowedincourse'] = 'At the moment you are not allowed to use this AI tool in this course.';
$string['noaiincourse'] = 'AI tools are not available in this course. Please enable them in the AI control center.';
$string['notallowedincourse'] = 'The AI functionality [{$a}] is not enabled in this course. Please enable it in the AI control center.';
$string['pluginname'] = 'AI control center';
$string['privacy:metadata'] = 'This plugin does not store any personal data.';
$string['savestate'] = 'Save';
Expand Down
12 changes: 11 additions & 1 deletion tests/local/hook_callbacks_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ final class hook_callbacks_test extends \advanced_testcase {
* @param bool $controlcapability if the user used for testing should have the 'block/ai_control:control' capability, which
* means the user usually has the teacher role in the course
* @param bool $expected the expected output: true, if the hook is allowing access, false otherwise
* @param string|null $expectedmessage the expected message if access is blocked
*/
public function test_handle_additional_user_restriction(
bool $aiconfigexists,
bool $enabled,
bool $purposeenabled,
bool $coursecontext,
bool $controlcapability,
bool $expected
bool $expected,
?string $expectedmessage
): void {
$this->resetAfterTest();

Expand Down Expand Up @@ -84,8 +86,10 @@ public function test_handle_additional_user_restriction(
hook_callbacks::handle_additional_user_restriction($hook);
if ($expected) {
$this->assertTrue($hook->is_allowed());
$this->assertSame('', $hook->get_message());
} else {
$this->assertFalse($hook->is_allowed());
$this->assertSame($expectedmessage, $hook->get_message());
}
}

Expand All @@ -107,31 +111,37 @@ public static function handle_additional_user_restriction_provider(): array {
...$allgoodconfig,
'aiconfigexists' => false,
'expected' => false,
'expectedmessage' => get_string('noaiincourse', 'block_ai_control'),
],
'studentallowed' => [
...$allgoodconfig,
'expected' => true,
'expectedmessage' => null,
],
'notenabled' => [
...$allgoodconfig,
'enabled' => false,
'expected' => false,
'expectedmessage' => get_string('noaiincourse', 'block_ai_control'),
],
'purposedisabled' => [
...$allgoodconfig,
'purposeenabled' => false,
'expected' => false,
'expectedmessage' => get_string('notallowedincourse', 'block_ai_control', 'chat'),
],
'disabledbutteacher' => [
...$allgoodconfig,
'controlcapability' => true,
'expected' => true,
'expectedmessage' => null,
],
'disabledbutothercontext' => [
...$allgoodconfig,
'enabled' => false,
'coursecontext' => false,
'expected' => true,
'expectedmessage' => null,
],
];
}
Expand Down