-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresultspage.class.php
664 lines (553 loc) · 19.4 KB
/
resultspage.class.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php
/**
* ELIS(TM): Enterprise Learning Intelligence Suite
* Copyright (C) 2008-2013 Remote-Learner.net Inc (http://www.remote-learner.net)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package local_elisprogram
* @author Remote-Learner.net Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2008-2013 Remote Learner.net Inc http://www.remote-learner.net
*
*/
defined('MOODLE_INTERNAL') || die();
define('ACTION_TYPE_TRACK', 0);
define('ACTION_TYPE_CLASS', 1);
define('ACTION_TYPE_PROFILE', 2);
require_once elispm::lib('data/resultsengine.class.php');
require_once elispm::lib('lib.php');
require_once elispm::lib('page.class.php');
require_once elispm::file('form/resultsform.class.php');
abstract class enginepage extends pm_page {
const LANG_FILE = 'local_elisprogram';
public $data_class = 'resultsengine';
public $child_data_class = 'resultsengineaction';
public $form_class = 'cmEngineForm';
protected $parent_page;
public $section;
protected $_form;
public function __construct($params = null) {
parent::__construct($params);
$this->section = $this->get_parent_page()->section;
}
abstract protected function get_context();
abstract protected function get_parent_page();
abstract protected function get_course_id();
/**
* Check if the user can edit
*
* @return bool True if the user has permission to use the edit action
*/
public function can_do_edit() {
return has_capability('local/elisprogram:'.$this->type.'_edit', $this->get_context());
}
/**
* Check if the user can delete from cache
*
* @return bool True if the user has permission to use the cachedelete action
*/
function can_do_cachedelete() {
return $this->can_do_edit();
}
/**
* Check if the user can delete (from db)
*
* @return bool True if the user has permission to use the delete action
*/
function can_do_delete() {
return $this->can_do_edit();
}
/**
* Check if the user can do the default action
*
* @return bool True if the user has permission to use the default action
*/
function can_do_default() {
return has_capability('local/elisprogram:'.$this->type.'_view', $this->get_context());
}
/**
* Get the id of this results engine
*
* @return int The id of the results engine (0 if doesn't exist).
*/
function get_engine_id() {
$contextid = $this->get_context()->id;
$rid = $this->optional_param('rid', 0, PARAM_INT);
$obj = $this->get_new_data_object($rid);
if ($rid < 1) {
$filter = new field_filter('contextid', $contextid);
$results = $obj->find($filter);
if (! empty($results->current()->id)) {
$rid = $results->current()->id;
}
}
return $rid;
}
/**
* Get the action type.
*
* @param array $data An array of data values
* @return int The action type value.
* @uses $DB
*/
function get_action_type() {
global $DB;
$type = optional_param('result_type_id', -1, PARAM_INT);
// If a button hasn't been pressed we have to look in the db.
if ($type == -1) {
$params = array('resultsid' => $this->get_engine_id());
if (! $type = $DB->get_field('local_elisprogram_res_action', 'actiontype', $params, IGNORE_MULTIPLE)) {
$type = ACTION_TYPE_TRACK;
}
}
return $type;
}
/**
* Return the engine form
*
* @return object The engine form
*/
protected function get_engine_form($cache = '') {
$known = false;
$contextid = $this->get_context()->id;
$id = $this->optional_param('id', 0, PARAM_INT);
$active = $this->optional_param('active', -1, PARAM_INT);
$rid = $this->get_engine_id();
$obj = $this->get_new_data_object($rid);
$filter = new field_filter('id', $rid);
if ($obj->exists($filter)) {
$obj->id = $rid;
$obj->load();
$known = true;
}
$target = $this->get_new_page(array('action' => 'edit', 'id' => $id), true);
$obj->contextid = $contextid;
$params = $obj->to_array();
$params['id'] = $id;
$params['rid'] = $rid;
$params['courseid'] = $this->get_course_id();
$params['contextid'] = $contextid;
$params['enginetype'] = $this->type;
$params['actiontype'] = $this->get_action_type();
// If set on the form use the form value over db value.
if ($active > -1) {
$params['active'] = $active;
}
$params['cache'] = $cache;
$form = new cmEngineForm($target->url, $params);
$form->set_data($params);
return $form;
}
/**
* Get the page with tab definitions
*/
function get_tab_page() {
return $this->get_parent_page();
}
/**
* Get the default page title.
* @return string The human readable context name.
*/
public function get_page_title_default() {
$ctx = $this->get_context();
return $ctx->get_context_name(false);
}
/**
* Build the default navigation bar.
*/
function build_navbar_default($who = null) {
//obtain the base of the navbar from the parent page class
$parent_template = $this->get_parent_page()->get_new_page();
$parent_template->build_navbar_view();
$this->_navbar = $parent_template->navbar;
$id = $this->required_param('id', PARAM_INT);
$curclass = get_class($this);
$pageclass = str_replace('status', '', $curclass);
$enginepg = ($pageclass == $curclass) ? $this : new $pageclass();
$page = $enginepg->get_new_page(array('id' => $id), true);
$this->navbar->add(get_string('results_engine', self::LANG_FILE), $page->url);
}
/**
* Print the tabs
*/
function print_tabs() {
$id = $this->required_param('id', PARAM_INT);
$this->get_parent_page()->print_tabs(get_class($this), array('id' => $id));
}
/**
* Return the page parameters for the page. Used by the constructor for
* calling $this->set_url().
*
* @return array
*/
protected function _get_page_params() {
$params = parent::_get_page_params();
$id = $this->required_param('id', PARAM_INT);
$params['id'] = $id;
return $params;
}
/**
* Display the default page
*/
function display_default() {
$this->display_edit();
}
/**
* Display the edit page
*/
function display_edit() {
if (!isset($this->_form)) {
throw new ErrorException('Display called before Do');
}
$type = $this->get_action_type();
echo '
<script type="text/javascript">
$(function(){
// Accordion
$("#accordion").accordion({ header: "h3", active: '. $type .' });
$("#accordion").accordion({ change:
function(event, ui) {
document.getElementById("result_type_id").value = (ui.options.active);
}
});
});
</script>';
$this->print_tabs();
$this->_form->display();
}
/**
* Do the default
*
* Set up the editing form before save.
*/
function do_default() {
$form = $this->get_engine_form();
if (!$this->can_do_edit()) {
$form->freeze();
}
$this->_form = $form;
$this->display('default');
}
/**
* Process the edit
*/
function do_edit() {
$known = false;
$id = $this->required_param('id', PARAM_INT);
$actiontype = $this->get_action_type();
$cache = '';
if ($actiontype >= 0) {
// TODO: Figure out a good place to store this lookup table.
$types = array(ACTION_TYPE_CLASS => 'class', ACTION_TYPE_TRACK => 'track', ACTION_TYPE_PROFILE => 'profile');
$type = $types[$actiontype];
$cacheid = $type .'_cache';
$cache = $this->optional_param($cacheid, '', PARAM_SEQUENCE);
}
$form = $this->get_engine_form($cache);
if ($form->is_cancelled()) {
$target = $this->get_new_page(array('action' => 'default', 'id' => $id), true);
redirect($target->url);
return;
}
$data = $form->get_data();
if ($form->no_submit_button_pressed()) {
$this->_form = $form;
$this->display('edit');
} else if ($data) {
require_sesskey();
if (array_key_exists('track_assignment', $data) or
array_key_exists('class_assignment', $data) or
array_key_exists('profile_assignment', $data)) {
$this->_form = $form;
$this->display('edit');
} else {
$obj = $this->get_new_data_object(0);
$obj->set_from_data($data);
if ($data->rid > 0) {
$obj->id = $data->rid;
} else {
unset($obj->id);
}
$obj->save();
if ($data->active) {
// Updating existing score ranges
$typename = $form->types[$actiontype];
$data = (array)$data;
// We don't keep the type of action in the parent table so child must be cleared
$this->delete_data();
// Save new score ranges submitted
$this->save_data($data, $typename, $actiontype, $obj->id);
}
$target = $this->get_new_page(array('action' => 'default',
'id' => $id), false);
redirect($target->url);
}
} else {
$this->_form = $form;
$this->display('edit');
}
}
/**
* Process the delete
*/
function do_delete() {
$id = $this->required_param('id', PARAM_INT);
$aid = $this->optional_param('aid', 0, PARAM_INT);
$rid = $this->get_engine_id();
if ($aid > 0) {
$rec = $this->get_new_child_data_object($aid);
// To prevent "reload" or "double-click" problems
if ($rec->exists()) {
$rec->load();
// Confirm it's an action for this page, to prevent capability circumvention
if ($rec->resultsid == $rid) {
$rec->delete();
}
}
}
$this->do_default();
}
/**
* Process the delete for cached items
*/
function do_cachedelete() {
$cache = $this->optional_param('actioncache', '', PARAM_SEQUENCE);
$aid = $this->optional_param('aid', 0, PARAM_INT);
$cachedata = explode(',', $cache);
unset($cachedata[4 * $aid+3]);
unset($cachedata[4 * $aid+2]);
unset($cachedata[4 * $aid+1]);
unset($cachedata[4 * $aid]);
$cache = implode(',', $cachedata);
$form = $this->get_engine_form($cache);
$this->_form = $form;
$this->display('edit');
}
/**
* Save new submitted data
*
* @param array $data The form data
* @param string $type The name of the action type
* @param int $actiontype The id of the action type
* @param int $results_engine_id The id of the result engine entry
*/
protected function save_data($data, $type, $actiontype, $results_engine_id) {
$savetype = '';
$instance = array();
$dataobj = (object) $data;
// Since the selected value may be a hidden value, it might be set to a legacy value
// So only check valid min and max rows.
$pattern = "/${type}_([0-9]+)_m/";
// Check for existing data regarding track/class/profile actions
foreach($data as $key => $value) {
//error_log("resultspage::save_data(); checking K/V {$key} => {$value}");
if (preg_match($pattern, $key, $matches)) {
if (!isset($data[$key]) || !is_numeric($data[$key])) {
// If value is empty then it must be an empty score range row
// because form validation will catch any incomplete rows
continue;
}
if (! array_key_exists($matches[1], $instance)) {
$instance[$matches[1]] = '';
}
}
}
$updaterec = new stdClass();
$field = '';
$fieldvalue = false;
switch ($actiontype) {
case ACTION_TYPE_TRACK:
$field = 'trackid';
break;
case ACTION_TYPE_CLASS:
$field = 'classid';
break;
case ACTION_TYPE_PROFILE:
$field = 'fieldid';
$fieldvalue = true;
break;
}
$fieldmap = array();
$fieldmap['actiontype'] = 'result_type_id';
foreach ($instance as $recid => $dummy_val) {
$updaterec = $this->get_new_child_data_object();
$updaterec->resultsid = $results_engine_id;
$key = "{$type}_{$recid}_min";
$fieldmap['minimum'] = $key;
$key = "{$type}_{$recid}_max";
$fieldmap['maximum'] = $key;
$key = "{$type}_{$recid}_selected";
$fieldmap[$field] = $key;
if ($fieldvalue) {
$key = "{$type}_{$recid}_value";
$fieldmap['fielddata'] = $key;
}
$updaterec->set_from_data($dataobj, true, $fieldmap);
$updaterec->save();
}
}
/**
* Delete existing data
*
* @param int $actiontype The action type id
*/
protected function delete_data() {
$record = $this->get_new_child_data_object();
$filters = array(
new field_filter('resultsid', $this->get_engine_id()),
);
$record->delete_records($filters);
}
/**
* Returns a new instance of the data object class this page manages.
*
* @param mixed $data Usually either the id or parameters for object, false for blank
* @return object The data object pulled form the database if an id was passed
*/
public function get_new_data_object($data=false) {
return new $this->data_class($data);
}
/**
* Returns a new instance of the child data object class this page manages.
*
* @param mixed $data Usually either the id or parameters for object, false for blank
* @return object The data object pulled form the database if an id was passed
*/
public function get_new_child_data_object($data=false) {
return new $this->child_data_class($data);
}
}
/**
* Engine page for courses
*
* @author Tyler Bannister <[email protected]>
*/
class course_enginepage extends enginepage {
public $pagename = 'crsengine';
public $type = 'course';
/**
* Return the context that the page is related to. Used by the constructor
* for calling $this->set_context().
* @return object The page context
*/
protected function _get_page_context() {
$id = $this->required_param('id', PARAM_INT);
return \local_elisprogram\context\course::instance($id);
}
/**
* This function returns the page context object
* @return object Course description context ojbect
*/
public function get_context() {
return $this->context;
}
/**
* Get the course id.
*
* @return int The course id
*/
protected function get_course_id() {
return $this->required_param('id', PARAM_INT);
}
/**
* Get parent page object
*
* @return object An object of the same type as the parent page
* @uses $CFG
* @uses $CURMAN
*/
protected function get_parent_page() {
if (!isset($this->parent_page)) {
global $CFG, $CURMAN;
require_once elispm::file('coursepage.class.php');
$id = $this->required_param('id', PARAM_INT);
$this->parent_page = new coursepage(array('id' => $id,
'action' => 'view'));
}
return $this->parent_page;
}
/**
* Check if the user can do the default action
*
* @return bool True if the user has permission to use the default action
*/
function can_do_default() {
return has_capability('local/elisprogram:course_view', $this->get_context());
}
/**
* Check if the user can do the edit actions
*
* @return bool True if the user has permission to use the edit actions
*/
public function can_do_edit() {
return has_capability('local/elisprogram:course_edit', $this->get_context());
}
}
/**
* Engine page for classes
*
* Classes have an extra form field that courses don't have.
*
* @author Tyler Bannister <[email protected]>
*/
class class_enginepage extends enginepage {
public $pagename = 'clsengine';
public $type = 'class';
/**
* Return the context that the page is related to. Used by the constructor
* for calling $this->set_context().
* @return object The page context
*/
protected function _get_page_context() {
$id = $this->required_param('id', PARAM_INT);
return \local_elisprogram\context\pmclass::instance($id);
}
/**
* This function returns the page context object
* @return object pmclass context ojbect
*/
public function get_context() {
return $this->context;
}
/**
* Get the course id.
*
* @return int The course id
* @uses $DB
*/
protected function get_course_id() {
global $DB;
$classid = $this->required_param('id', PARAM_INT);
$courseid = $DB->get_field('local_elisprogram_cls', 'courseid', array('id' => $classid));
return $courseid;
}
/**
* Get parent page object
*
* @return object An object of the same type as the parent page
* @uses $CFG
* @uses $CURMAN
*/
protected function get_parent_page() {
if (!isset($this->parent_page)) {
global $CFG, $CURMAN;
require_once elispm::file('pmclasspage.class.php');
$id = $this->required_param('id');
$this->parent_page = new pmclasspage(array('id' => $id,
'action' => 'view'));
}
return $this->parent_page;
}
}