-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontroller.php
594 lines (546 loc) · 24.6 KB
/
controller.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
/**
* Copyright (c) 2012 i>clicker (R) <http://www.iclicker.com/dnn/>
*
* This file is part of i>clicker Moodle integrate.
*
* i>clicker Moodle integrate 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.
*
* i>clicker Moodle integrate 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 i>clicker Moodle integrate. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Id$ */
/**
* Handles controller functions related to the views
*/
require_once ('../../config.php');
global $CFG,$USER,$COURSE;
require_once ('iclicker_service.php');
class iclicker_controller {
// constants
const TYPE_HTML = 'html';
const TYPE_XML = 'xml';
const TYPE_TEXT = 'txt';
const PASSWORD = '_password';
const LOGIN = '_login';
const SSO_KEY = '_key';
const SEPARATOR = '/';
const PERIOD = '.';
const XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
const SESSION_ID = '_sessionId';
const COMPENSATE_METHOD = '_method';
const XML_DATA = '_xml';
// class vars
// REQUEST
public $method = 'GET';
public $path = '';
public $query = '';
public $body = NULL;
// RESPONSE
public $status = 200;
public $message = '';
public $headers = array();
public $results = array(
);
public function __construct($getBody = false) {
// set some headers
$this->headers['Content-Encoding'] = 'UTF8';
//header('Content-type: text/plain');
//header('Cache-Control: no-cache, must-revalidate');
// get the rest path
$full_path = me();
if ($full_path) {
$path = $full_path;
$pos = strripos($full_path, '.php');
if ($pos > 1) {
$path = substr($full_path, $pos+4);
$path = trim($path, '/ '); // trim whitespace and slashes
}
if (stripos($path, '?')) {
$qloc = stripos($path, '?');
$this->query = trim(substr($path, $qloc), '?');
$path = substr($path, 0, $qloc);
}
$this->path = $path;
}
// get the body
if ($getBody) {
if (function_exists('http_get_request_body')) {
$this->body = http_get_request_body();
} else if (defined('STDIN')) {
$this->body = @stream_get_contents(STDIN);
} else {
// Moodlerooms does not allow use of php://input
// $this->body = @file_get_contents('php://input');
// cannot get the body
$this->setHeader('NO_BODY','Cannot retrieve request body content');
$this->body = null;
}
}
// allow for method overrides
$current_method = $_SERVER['REQUEST_METHOD'];
if (isset($_REQUEST[self::COMPENSATE_METHOD])) {
$comp_method = $_REQUEST[self::COMPENSATE_METHOD];
if (! empty($comp_method)) {
// Allows override to GET or DELETE
$comp_method = strtoupper(trim($comp_method));
if ('GET' == $comp_method) {
$current_method = 'GET';
} else {
if ('DELETE' == $comp_method) {
$current_method = 'DELETE';
} else {
if ('POST' == $comp_method) {
$current_method = 'POST';
}
}
}
}
}
$this->method = $current_method;
}
public function setStatus($status) {
if ($status) {
$this->status = $status;
}
}
public function setMessage($msg) {
$this->message = $msg;
}
public function setContentType($mime_type) {
$this->headers['Content-Type'] = $mime_type;
}
public function setHeader($name, $value) {
$this->headers[$name] = $value;
}
/**
* Send the response
*
* @param string $content [optional] the content to send
* @param string $message [optional] the message to send, defaults to "Invalid request"
*/
public function sendResponse($content = NULL, $message = null) {
$code = $this->status;
if (!isset($message)) {
switch ($code) {
case 200: $message='OK'; break;
case 204: $message='No Content'; break;
case 302: $message='Found'; break;
case 400: $message='Bad Request'; break;
case 401: $message='Unauthorized'; break;
case 403: $message='Forbidden'; break;
case 404: $message='Not Found'; break;
case 426: $message='Upgrade Required'; break;
default: $message='Internal Server Error';
}
}
header("HTTP/1.0 $code ".str_replace("\n", "", $message));
if ($code >= 400) {
// force plain text encoding when errors occur
$this->setContentType('text/plain');
}
$headers = $this->headers;
if (isset($headers) && ! empty($headers)) {
foreach ($headers as $key=> & $value) {
header($key.': '.$value, false);
}
unset($value);
}
// dump the body content
if (isset($content)) {
echo $content;
}
}
// XHTML view processors
public function processRegistration() {
// process calls to the registration view
if (!iclicker_service::get_current_user_id()) {
throw new ClickerSecurityException('Current user is not logged in and cannot access the registration view');
}
$this->results['new_reg'] = false;
$this->results['clicker_id_val'] = "";
if ('POST' == $this->method) {
if (optional_param('register', null, PARAM_ALPHANUM) != null) {
// we are registering a clicker
$clicker_id = optional_param('clickerId', null, PARAM_ALPHANUMEXT);
if ($clicker_id == null) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.empty');
} else {
$this->results['clicker_id_val'] = $clicker_id;
// save a new clicker registration
try {
iclicker_service::create_clicker_registration($clicker_id);
$this->addMessage(self::KEY_INFO, 'reg.registered.success', $clicker_id);
$this->addMessage(self::KEY_BELOW, 'reg.registered.below.success');
$this->results['new_reg'] = true;
}
catch (ClickerRegisteredException $e) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.duplicate', $clicker_id);
if (iclicker_service::$allow_remote_sharing) {
$this->addMessage(self::KEY_BELOW, 'reg.registered.below.duplicate', $clicker_id);
} else {
$this->addMessage(self::KEY_BELOW, 'reg.registered.below.duplicate.noshare', $clicker_id);
}
} catch (ClickerWebservicesException $e) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.failure', $clicker_id, $e);
} catch (ClickerIdInvalidException $e) {
if (ClickerIdInvalidException::F_EMPTY == $e->type) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.empty');
} else if (ClickerIdInvalidException::F_LENGTH == $e->type) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.wrong.length');
} else if (ClickerIdInvalidException::GO_NO_USER == $e->type) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.failure', $clicker_id, $e);
} else if (ClickerIdInvalidException::GO_LASTNAME == $e->type) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.go.wrong.lastname', $e);
} else if (ClickerIdInvalidException::GO_NO_MATCH == $e->type) {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.go.invalid', $clicker_id);
} else {
$this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.invalid', $clicker_id);
}
}
}
} else {
if (optional_param('activate', null, PARAM_ALPHANUM) != null) {
// First arrived at this page
$activate = optional_param('activate', 'false', PARAM_ALPHANUM);
$activate = ($activate == 'true' ? true : false);
$reg_id = optional_param('registrationId', null, PARAM_INT);
if ($reg_id == null) {
$this->addMessage(self::KEY_ERROR, 'reg.activate.registrationId.empty', null);
} else {
// save a new clicker registration
$cr = iclicker_service::set_registration_active($reg_id, $activate);
if ($cr) {
$this->addMessage(self::KEY_INFO, 'reg.activate.success.' . ($cr->activated ? 'true' : 'false'), $cr->clicker_id);
}
}
} else {
if (optional_param('remove', null, PARAM_ALPHANUM) != null) {
$reg_id = optional_param('registrationId', null, PARAM_ALPHANUMEXT);
if (($reg_id == null)) {
$this->addMessage(self::KEY_ERROR, 'reg.activate.registrationId.empty', null);
} else {
// remove a new clicker registration by deactivating it
$cr = iclicker_service::set_registration_active($reg_id, false);
if ($cr) {
$this->addMessage(self::KEY_INFO, 'reg.remove.success', $cr->clicker_id);
}
}
} else {
// invalid POST
echo('WARN: Invalid POST: does not contain register or activate, nothing to do');
}
}
}
}
$this->results['regs'] = iclicker_service::get_registrations_by_user(null, true);
$this->results['is_instructor'] = iclicker_service::is_instructor();
$this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled;
// added to allow special messages below the forms
$this->results['below_messages'] = $this->getMessages(self::KEY_BELOW);
}
public function processInstructor() {
$this->results['instPath'] = iclicker_service::block_url('instructor.php');
// admin/instructor check
if (!iclicker_service::is_admin() && !iclicker_service::is_instructor()) {
throw new ClickerSecurityException("Current user is not an instructor and cannot access the instructor view");
}
$course_id = optional_param('courseId', false, PARAM_INT);
$this->results['course_id'] = $course_id;
$courses = array();
if ($course_id) {
$course = iclicker_service::get_course($course_id);
$courses[] = $course;
} else {
$courses = iclicker_service::get_courses_for_instructor();
}
$this->results['courses'] = $courses;
$this->results['courses_count'] = count($courses);
$this->results['show_students'] = false;
if ($course_id && count($courses) == 1) {
$course = $courses[0];
$this->results['show_students'] = true;
$this->results['course'] = $course;
$students = iclicker_service::get_students_for_course_with_regs($course_id);
$this->results['students'] = $students;
$this->results['students_count'] = count($students);
}
$this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled;
}
public function processInstructorSSO() {
$this->results['instPath'] = iclicker_service::block_url('instructor.php');
// admin/instructor check
if (!iclicker_service::is_admin() && !iclicker_service::is_instructor()) {
throw new ClickerSecurityException("Current user is not an instructor and cannot access the instructor view");
}
$this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled;
$current_user_id = iclicker_service::get_current_user_id();
if (iclicker_service::$block_iclicker_sso_enabled) {
$current_user_key = null;
if ('POST' == $this->method) {
if ( optional_param('generateKey', false, PARAM_ALPHANUM) != null ) {
// handle generating a new key
$current_user_key = iclicker_service::makeUserKey($current_user_id, true);
$this->addMessage(self::KEY_INFO, 'inst.sso.generated.new.key', null);
}
}
if ($current_user_key == null) {
$current_user_key = iclicker_service::makeUserKey($current_user_id, false);
}
$this->results['sso_user_key'] = $current_user_key;
}
//$current_user_key = iclicker_service::makeUserKey($current_user_id);
//$this->results['sso_user_key'] = $current_user_key;
}
public function processAdmin() {
global $CFG;
$adminPath = iclicker_service::block_url('admin.php');
$this->results['adminPath'] = $adminPath;
$this->results['status_url'] = iclicker_service::block_url('runner_status.php');
// admin check
if (!iclicker_service::is_admin()) {
throw new ClickerSecurityException("Current user is not an admin and cannot access the admin view");
}
// get sorting params
$pageNum = 1;
$perPageNum = 20; // does not change
if (optional_param('page', null, PARAM_ALPHANUM) != null) {
$pageNum = required_param('page', PARAM_INT);
if ($pageNum < 1) {
$pageNum = 1;
}
}
$sort = 'clicker_id';
if (optional_param('sort', null, PARAM_ALPHANUM) != null) {
$sort = required_param('sort', PARAM_ALPHANUMEXT);
}
$this->results['sort'] = $sort;
// get filtering params
$search = optional_param('search', null, PARAM_ALPHANUMEXT);
if (empty($search)) {
$search = null;
}
$startDate = null;
$startDateText = '';
if (optional_param('start_date', null, PARAM_ALPHANUMEXT) != null) {
$startDate = required_param('start_date', PARAM_ALPHANUMEXT);
$startDate = strtotime($startDate);
if ($startDate) {
$startDateText = date('Y-m-d', $startDate);
} else {
$startDate = null;
}
}
$endDate = null;
$endDateText = '';
if (optional_param('end_date', null, PARAM_ALPHANUMEXT) != null) {
$endDate = required_param('end_date', PARAM_ALPHANUMEXT);
$endDate = strtotime($endDate);
if ($endDate) {
// need to make this the end of the day
$endDT = new DateTime('@' . $endDate);
$endDT->setTimezone(new DateTimeZone(date_default_timezone_get()));
$endDT->setTime(23, 59, 59);
$endDate = $endDT->getTimestamp();
$endDateText = date('Y-m-d', $endDate);
} else {
$endDate = null;
}
}
if ("POST" == $this->method) {
if (optional_param('activate', null, PARAM_ALPHANUM) != null) {
// First arrived at this page
$activate = required_param('activate', PARAM_BOOL);
if (optional_param('registrationId', null, PARAM_ALPHANUMEXT) == null) {
$this->addMessage(self::KEY_ERROR, "reg.activate.registrationId.empty", null);
} else {
$reg_id = required_param('registrationId', PARAM_INT);
// save a new clicker registration
$cr = iclicker_service::set_registration_active($reg_id, $activate);
if ($cr) {
$args = new stdClass ;
$args->cid = $cr->clicker_id;
$args->user = iclicker_service::get_user_displayname($cr->owner_id);
$this->addMessage(self::KEY_INFO, "admin.activate.success.".($cr->activated ? 'true' : 'false'), $args);
}
}
} else if (optional_param('remove', null, PARAM_ALPHANUM) != null) {
if (optional_param('registrationId', null, PARAM_ALPHANUMEXT) == null) {
$this->addMessage(self::KEY_ERROR, "reg.activate.registrationId.empty", null);
} else {
$reg_id = required_param('registrationId', PARAM_INT);
$cr = iclicker_service::get_registration_by_id($reg_id);
if ($cr) {
iclicker_service::remove_registration($reg_id);
$args = new stdClass();
$args->cid = $cr->clicker_id;
$args->rid = $reg_id;
$args->user = iclicker_service::get_user_displayname($cr->owner_id);
$this->addMessage(self::KEY_INFO, "admin.delete.success", $args);
}
}
} else if (optional_param('purge', null, PARAM_ALPHANUM) != null) {
// actually do the purging
$count = iclicker_service::purge_registrations($search, $startDate, $endDate);
$this->addMessage(self::KEY_INFO, "admin.purge.success", $count);
// reset all search params to defaults
$search = null;
$startDate = null;
$endDate = null;
$pageNum = 1;
$perPageNum = 20;
$sort = 'clicker_id';
} else {
// invalid POST
error_log('WARN: Invalid POST: does not contain remove, purge, or activate, nothing to do');
}
}
// put search and sort data into the page
$this->results['search'] = $search;
$this->results['startDate'] = $startDate;
$this->results['startDateText'] = $startDateText;
$this->results['endDate'] = $endDate;
$this->results['endDateText'] = $endDateText;
$this->results['page'] = $pageNum;
$this->results['perPage'] = $perPageNum;
$this->results['sort'] = $sort;
// put config data into page
$this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled;
$this->results['sso_shared_key'] = iclicker_service::$block_iclicker_sso_shared_key;
$this->results['domainURL'] = iclicker_service::$domain_URL;
$this->results['adminEmailAddress'] = empty($CFG->block_iclicker_notify_emails) ? '' : $CFG->block_iclicker_notify_emails;
// put error data into page
$this->results['recent_failures'] = iclicker_service::get_failures();
// handling the calcs for paging
$first = ($pageNum - 1) * $perPageNum;
$totalCount = iclicker_service::count_all_registrations($search, $startDate, $endDate);
$pageCount = floor(($totalCount + $perPageNum - 1) / $perPageNum);
$this->results['total_count'] = $totalCount;
$this->results['page_count'] = $pageCount;
$this->results['registrations'] = iclicker_service::get_all_registrations($first, $perPageNum, $sort, $search, $startDate, $endDate);
$pagerHTML = "";
if ($totalCount > 0) {
$timestamp = microtime();
for ($i = 0; $i < $pageCount; $i++) {
$currentPage = $i + 1;
$currentStart = ($i * $perPageNum) + 1;
$currentEnd = $currentStart + $perPageNum - 1;
if ($currentEnd > $totalCount) {
$currentEnd = $totalCount;
}
$marker = '['.$currentStart.'..'.$currentEnd.']';
if ($currentPage == $pageNum) {
// make it bold and not a link
$pagerHTML .= '<span class="paging_current paging_item">'.$marker.'</span>'."\n";
} else {
// make it a link
$pagingURL = $adminPath.'?page='.$currentPage.'&sort='.$sort;
if (isset($search)) {
$pagingURL .= '&search='.urlencode($search);
}
if (!empty($startDateText)) {
$pagingURL .= '&start_date='.urlencode($startDateText);
}
if (!empty($endDateText)) {
$pagingURL .= '&end_date='.urlencode($endDateText);
}
$pagingURL .= '&nc='.($timestamp.$currentPage);
$pagerHTML .= '<a class="paging_link paging_item" href="'.$pagingURL.'">'.$marker.'</a>'."\n";
}
}
$this->results['pagerHTML'] = $pagerHTML;
}
}
public function processAdminCSV() {
//global $CFG;
// admin check
if (!iclicker_service::is_admin()) {
throw new ClickerSecurityException("Current user is not an admin and cannot make CSV of all regs");
}
$this->results['registrations'] = iclicker_service::get_all_registrations();
}
// MESSAGING
var $messages = array(
);
const KEY_INFO = "INFO";
const KEY_ERROR = "ERROR";
const KEY_BELOW = "BELOW";
/**
* Adds a message
*
* @param string $key the KEY const
* @param string $message the message to add
* @param string $extra [OPTIONAL] extra string to include hidden on the page with the message
* @throws Exception if the key is invalid
*/
public function addMessageStr($key, $message, $extra = null) {
if ($key == null) {
throw new Exception("key (".$key.") must not be null");
}
if ($message) {
if (!isset($this->messages[$key])) {
$this->messages[$key] = array(
);
}
if (isset($extra)) {
if (!is_string($extra)) {
$extra = var_export($extra, true);
}
$extra = PHP_EOL . '<div style="display:none;">' . $extra . '</div>' . PHP_EOL;
} else {
$extra = '';
}
$this->messages[$key][] = $message . $extra;
}
}
/**
* Add an i18n message based on a key
*
* @param string $key the KEY const
* @param string $messageKey the i18n message key
* @param object $args [OPTIONAL] args to include
* @param string $extra [OPTIONAL] extra string to include hidden on the page with the message
* @throws Exception if the key is invalid
*/
public function addMessage($key, $messageKey, $args = null, $extra = null) {
if ($key == null) {
throw new Exception("key (".$key.") must not be null");
}
if ($messageKey) {
$message = iclicker_service::msg($messageKey, $args);
$this->addMessageStr($key, $message, $extra);
}
}
/**
* Get the messages that are currently waiting in this request
*
* @param string $key the KEY const
* @return array the list of messages to display
* @throws Exception if the key is invalid
*/
public function getMessages($key) {
if ($key == null) {
throw new Exception("key (".$key.") must not be null");
}
$messages = null;
if (isset($this->messages[$key])) {
$messages = $this->messages[$key];
if (!isset($messages)) {
$messages = array(
);
}
} else {
$messages = array(
);
}
return $messages;
}
}