This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexController.php
335 lines (238 loc) · 11 KB
/
IndexController.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?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/>.
/**
* Dashboard Reporting
*
* The Reporting Dashboard plugin is a block which runs alongside the ELBP and Grade Tracker blocks, to provide a better experience and extra features,
* such as combined reporting across both plugins. It also allows you to create your own custom SQL reports which can be run on any aspect of Moodle.
*
* @package block_bc_dashboard
* @copyright 2017-onwards Conn Warwicker
* @author Conn Warwicker <conn@cmrwarwicker.com>
* @link https://github.com/cwarwicker/moodle-block_elbp
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* Originally developed at Bedford College, now maintained by Conn Warwicker
*
*/
namespace BCDB\Controllers;
/**
* Description of DashboardController
*
* @author cwarwicker
*/
class IndexController extends \BCDB\Controller {
protected $component = 'index';
public function main(){
// Check permissions to view reporting
if (!$this->hasCapability('block/bc_dashboard:view_bc_dashboard')){
\bcdb_fatalError( get_string('invalidaccess', 'block_bc_dashboard') );
}
}
public function action_view($args = false){
global $CFG, $USER, $bcdb;
$type = (isset($args[0])) ? $args[0] : 'all';
$id = (isset($args[1])) ? $args[1] : false;
// If we're trying to look at the list of students on a specific course, they either need:
// Capability on that course
// Capability on frontpage and assigned to that course as a teacher
if ($type == 'course' && $id){
if (!$this->hasCapability('block/bc_dashboard:view_bc_dashboard', $id)){
\bcdb_fatalError( get_string('invalidaccess', 'block_bc_dashboard') );
}
} else {
// Otherwise, we just want to see if they have this permission on any of their contexts
if (!$this->hasCapability('block/bc_dashboard:view_bc_dashboard')){
\bcdb_fatalError( get_string('invalidaccess', 'block_bc_dashboard') );
}
}
// Mass Actions
if (isset($_POST['mass_action']) && isset($_POST['students']))
{
$this->runMassActions($type);
}
// Add students
elseif (isset($_POST['submit_assign']))
{
if ($type == 'mentees'){
$OBJ = new \ELBP\PersonalTutor();
} elseif ($type == 'additionalsupport'){
$OBJ = new \ELBP\ASL();
} else {
return false;
}
$OBJ->loadTutorID($USER->id);
// Loop through students
$OBJ->setAssignBy("username");
$OBJ->assignIndividualMentees($_POST['findstudent']);
$this->view->set("messages", array('general' => $OBJ->getOutputMsg()));
}
}
public function runMassActions($type = null){
global $CFG, $USER, $bcdb;
// Sort students into an array first
$usersArray = array();
foreach($_POST['students'] as $id){
$user = \bcdb_get_user($id);
if ($user){
$usersArray[$user->id] = $user;
}
}
\bcdb_sort_users($usersArray);
// Send a message
if ($_POST['mass_action'] == 'message')
{
// Confirmed
if (isset($_POST['confirmed']) && !empty($_POST['message']))
{
$Alert = new \ELBP\EmailAlert();
$subject = (!empty($_POST['subject'])) ? $_POST['subject'] : get_string('nosubject', 'block_bc_dashboard');
$content = $_POST['message'];
$htmlContent = nl2br($content);
$successMsg = array();
$errMsg = array();
foreach($usersArray as $user){
$usersArray[$user->id] = $user;
if ($Alert->send($user, $subject, $content, $htmlContent, null, $USER) ) {
$successMsg[] = '<i class="fa fa-check-square"></i> ' . get_string('messagesentto', 'block_bc_dashboard') . ": " . fullname($user) . " ({$user->username})";
} else {
$errMsg[] = '<i class="fa fa-cross-square"></i> ' . get_string('failedsendmessageto', 'block_bc_dashboard') . ": " . fullname($user) . " ({$user->username})";
}
}
$messages = array(
'success' => implode("<br>", $successMsg),
'errors' => implode("<br>", $errMsg)
);
$this->view->set("messages", $messages);
$this->view->set("students", $usersArray);
}
// Not confirmed
else
{
$output = "";
$hidden = "";
foreach($usersArray as $user){
$output .= bcdb_get_user_name($user->id) . ", ";
$hidden .= "<input type='hidden' name='students[]' value='{$user->id}' />";
}
$output = substr($output, 0, -2);
$output .= "<form action='' method='post'>{$hidden}";
$output .= "<br><input type='text' name='subject' placeholder='".get_string('subject', 'block_bc_dashboard')."' /><br>";
$output .= "<br><textarea name='message' style='width:80%;height:200px;'></textarea><br><br>";
$output .= "<button class='btn btn-primary' type='submit' name='confirmed'>".get_string('sendmessage', 'block_bc_dashboard')."</button>";
$output .= "<input type='hidden' name='mass_action' value='message' /> ";
$output .= "<a href='' class='btn btn-danger'>".get_string('cancel')."</a></form>";
bcdb_confirmation_page( get_string('messagestudents', 'block_bc_dashboard') , $output);
}
}
// Remove student
elseif ($_POST['mass_action'] == 'remove' && isset($_POST['students']))
{
// Confirmed
if (isset($_POST['confirmed']))
{
$successMsg = array();
$errorMsg = array();
// Mentees
if ($type == 'mentees')
{
$PT = new \ELBP\PersonalTutor();
$PT->loadTutorID($USER->id);
}
// Addiiotnal SUpport tutor
elseif ($type == 'additionalsupport')
{
$PT = new \ELBP\ASL();
$PT->loadTutorID($USER->id);
}
foreach($usersArray as $user){
if ($PT->removeMentee($user->id)){
$successMsg[] = '<i class="fa fa-check-square"></i> ' . get_string('removedstudent', 'block_bc_dashboard') . ": " . bcdb_get_user_name($user->id, false);
} else {
$errorMsg[] = '<i class="fa fa-check-square"></i> ' . get_string('removestudent:error', 'block_bc_dashboard') . ": " . bcdb_get_user_name($user->id, false) . " - {$PT->getOutputMsg()}";
}
}
$messages = array(
'success' => implode("<br>", $successMsg),
'errors' => implode("<br>", $errorMsg)
);
$this->view->set("messages", $messages);
}
// Not confirmed
else
{
$output = "";
$hidden = "";
$output .= get_string('removestudents:sure', 'block_bc_dashboard') . "<br><br>";
foreach($usersArray as $user){
$output .= bcdb_get_user_name($user->id) . ", ";
$hidden .= "<input type='hidden' name='students[]' value='{$user->id}' />";
}
$output = substr($output, 0, -2);
$output .= "<form action='' method='post'>{$hidden}<br>";
$output .= "<button class='btn btn-primary' type='submit' name='confirmed'>".get_string('confirm')."</button>";
$output .= "<input type='hidden' name='mass_action' value='remove' /> ";
$output .= "<a href='' class='btn btn-danger'>".get_string('cancel')."</a></form>";
bcdb_confirmation_page( get_string('removestudents', 'block_bc_dashboard') , $output);
}
}
// Else, see if its a ELBP plugin mass action
else
{
if (strpos($_POST['mass_action'], ":") && $bcdb['elbp'] == true)
{
$explode = explode(":", $_POST['mass_action']);
$pluginID = $explode[0];
$action = $explode[1];
if (is_numeric($pluginID) && !empty($action))
{
// Get plugin
$ELBP = new \ELBP\ELBP();
$plugin = $ELBP->getPluginByID($pluginID);
if ($plugin)
{
$result = $plugin->massAction($action, $usersArray);
if ($result && $result['result'])
{
$this->view->set("messages", array(
'success' => $result['success']
));
$this->view->set("students", $result['students']);
}
}
}
}
}
}
public function action_admin(){
global $bcdb;
// ELBP
if (!$bcdb['elbp']){
exit;
}
$ELBP = new \ELBP\ELBP();
// permissions
$access = $ELBP->getCoursePermissions(SITEID);
if (!$access['god'] && !$access['elbpadmin']){
\bcdb_fatalError( get_string('invalidaccess', 'block_bc_dashboard') );
}
// Mass Actions
if (isset($_POST['mass_action']) && isset($_POST['students']))
{
$this->runMassActions();
}
}
}