-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhook_callbacks.php
More file actions
157 lines (143 loc) · 6.31 KB
/
hook_callbacks.php
File metadata and controls
157 lines (143 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
namespace block_ai_control\local;
use core\clock;
use core_course\hook\after_form_definition_after_data;
use core_course\hook\after_form_submission;
use local_ai_manager\ai_manager_utils;
use local_ai_manager\hook\additional_user_restriction;
/**
* Hook listener callbacks.
*
* @package block_ai_control
* @copyright 2024 ISB Bayern
* @author Philipp Memmel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class hook_callbacks {
/**
* Add a checkbox to add an ai_control block.
*
* @param \core_course\hook\after_form_definition $hook
*/
public static function handle_after_form_definition(\core_course\hook\after_form_definition $hook): void {
$configmanager = \core\di::get(\local_ai_manager\local\config_manager::class);
if ($configmanager->is_tenant_enabled()) {
$mform = $hook->mform;
ai_manager_utils::add_ai_tools_category_to_mform($mform);
$mform->addElement(
'checkbox',
'addaicontrol',
get_string('pluginname', 'block_ai_control'),
get_string('addblockinstance', 'block_ai_control')
);
$mform->addHelpButton('addaicontrol', 'addblockinstance', 'block_ai_control');
$mform->setDefault('addaicontrol', 0);
}
}
/**
* Check for form setting to add/remove ai_control block and handle accordlingly.
*
* @param after_form_submission $hook the hook to retrieve the data from.
*/
public static function handle_after_form_submission(after_form_submission $hook): void {
global $DB;
// Get form data.
$data = $hook->get_data();
$clock = \core\di::get(clock::class);
$courseid = $data->id;
$coursecontextid = \context_course::instance($courseid)->id;
$existingblockinstance = $DB->get_record(
'block_instances',
['blockname' => 'ai_control', 'parentcontextid' => $coursecontextid]
);
if (!empty($data->addaicontrol)) {
if (!$existingblockinstance) {
// Add block instance.
$newinstance = new \stdClass();
$newinstance->blockname = 'ai_control';
$newinstance->parentcontextid = $coursecontextid;
// We want to make the block usable for single activity courses as well, so display in subcontexts.
$newinstance->showinsubcontexts = 1;
$newinstance->pagetypepattern = '*';
$newinstance->subpagepattern = null;
$newinstance->defaultregion = 'side-pre';
$newinstance->defaultweight = 1;
$newinstance->configdata = '';
$newinstance->timecreated = $clock->time();
$newinstance->timemodified = $newinstance->timecreated;
$newinstance->id = $DB->insert_record('block_instances', $newinstance);
$aiconfig = new aiconfig($coursecontextid);
$aiconfig->store();
}
} else {
// If tenant is not allowed, $data->addaicontrol will be empty,
// so an existing instance will be deleted by following lines.
if ($existingblockinstance) {
// Remove block instance.
blocks_delete_instance($existingblockinstance);
}
}
}
/**
* Check if block instance is present and set addaichat form setting.
*
* @param after_form_definition_after_data $hook
*/
public static function handle_after_form_definition_after_data(after_form_definition_after_data $hook): void {
global $DB;
// Get form data.
$mform = $hook->mform;
$formwrapper = $hook->formwrapper;
if (!empty($formwrapper->get_course()->id)) {
$courseid = $formwrapper->get_course()->id;
$coursecontextid = \context_course::instance($courseid)->id;
$blockinstance = $DB->get_record(
'block_instances',
['blockname' => 'ai_control', 'parentcontextid' => $coursecontextid]
);
if ($blockinstance) {
// Block present, so set checkbox accordingly.
$mform->setDefault('addaicontrol', 1);
}
}
}
/**
* Hook callback for the additional_user_restriction hook.
*
* This callback basically applies the config set by the block_ai_control to the ai_manager. That means that this function
* makes a request to the AI manager fail if the AI config of block_ai_control in the current course restricts this.
*
* @param additional_user_restriction $hook the hook object
*/
public static function handle_additional_user_restriction(additional_user_restriction $hook): void {
$coursecontext = ai_manager_utils::find_closest_parent_course_context($hook->get_context());
if (is_null($coursecontext)) {
// We could not find a usable course context, so we do not do anything.
return;
}
$aiconfig = new aiconfig($coursecontext->id);
$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);
}
}
}