-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcoursepage.class.php
550 lines (459 loc) · 22.1 KB
/
coursepage.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
<?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();
require_once elis::lib('data/data_filter.class.php');
require_once elispm::lib('data/course.class.php');
require_once elispm::lib('data/coursetemplate.class.php');
require_once elispm::lib('data/pmclass.class.php');
require_once elispm::lib('managementpage.class.php');
require_once elispm::lib('contexts.php');
require_once elispm::file('curriculumcoursepage.class.php');
require_once elispm::file('form/courseform.class.php');
require_once elispm::file('pmclasspage.class.php');
require_once elispm::file('rolepage.class.php');
require_once elispm::file('resultspage.class.php');
require_once elispm::file('certificatepage.class.php');
class coursepage extends managementpage {
var $data_class = 'course';
var $form_class = 'cmCourseForm';
var $view_columns = array('name', 'code');
var $pagename = 'crs';
var $section = 'curr';
static $contexts = array();
/**
* 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() {
if (($id = $this->optional_param('id', 0, PARAM_INT))) {
$pgid = optional_param('s', 'crs', PARAM_TEXT);
if (!strncmp($pgid, 'cur', 3) || !strncmp($pgid, 'prg', 3)) {
try {
$context = \local_elisprogram\context\program::instance($id);
return $context;
} catch (Exception $e) {
; // Ignore exception here.
}
} else {
try {
$context = \local_elisprogram\context\course::instance($id);
return $context;
} catch (Exception $e) {
; // Ignore exception here.
}
}
}
return parent::_get_page_context();
}
public static function get_contexts($capability) {
if (!isset(coursepage::$contexts[$capability])) {
global $USER;
coursepage::$contexts[$capability] = get_contexts_by_capability_for_user('course', $capability, $USER->id);
}
return coursepage::$contexts[$capability];
}
/**
* Check the cached capabilities for the current user.
*/
public static function check_cached($capability, $id) {
if (isset(coursepage::$contexts[$capability])) {
// we've already cached which contexts the user has delete
// capabilities in
$contexts = coursepage::$contexts[$capability];
return $contexts->context_allowed($id, 'course');
}
return null;
}
/**
* Check if the user has the given capability for the requested curriculum
*/
public function _has_capability($capability, $id = null) {
if (empty($id)) {
$id = (isset($this) && method_exists($this, 'required_param'))
? $this->required_param('id', PARAM_INT)
: required_param('id', PARAM_INT);
}
// course contexts are different -- we rely on the cache because curricula
// require special logic
coursepage::get_contexts($capability);
$cached = coursepage::check_cached($capability, $id);
if ($cached !== null) {
return $cached;
}
return has_capability($capability, $this->context);
}
public function _get_page_params() {
return parent::_get_page_params();
}
public function __construct(array $params=null) {
// Check the course certificate global settings value
$showcoursecerttab = isset(elis::$config->local_elisprogram->disablecoursecertificates);
$showcoursecerttab = $showcoursecerttab &&
empty(elis::$config->local_elisprogram->disablecoursecertificates);
$numclasses = 1;
if (($id = $this->optional_param('id', 0, PARAM_INT)) > 0 && !$this->_has_capability('local/elisprogram:class_create', $id)) {
$parentclusterid = $this->optional_param('parent_clusterid', 0, PARAM_INT);
$numclasses = pmclass_count_records('', '', $id, false, pmclasspage::get_contexts('local/elisprogram:class_view'), $parentclusterid);
}
$this->tabs = array(
array('tab_id' => 'view', 'page' => get_class($this), 'params' => array('action' => 'view'), 'name' => get_string('detail', 'local_elisprogram'), 'showtab' => true),
array('tab_id' => 'edit', 'page' => get_class($this), 'params' => array('action' => 'edit'), 'name' => get_string('edit', 'local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'edit'),
// allow users to view the classes associated with this course
array(
'tab_id' => 'pmclasspage',
'page' => 'pmclasspage',
'params' => array('action' => 'default'),
'name' => get_string('course_classes', 'local_elisprogram'),
'showtab' => $numclasses > 0,
'showbutton' => $numclasses > 0,
'image' => 'class'
),
array('tab_id' => 'elem', 'page' => get_class($this), 'params' => array('action' => 'lelem'), 'name' => get_string('completion_elements', 'local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'grades'),
array('tab_id' => 'coursecurriculumpage', 'page' => 'coursecurriculumpage', 'name' => get_string('course_curricula', 'local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'curriculum'),
array('tab_id' => 'course_rolepage', 'page' => 'course_rolepage', 'name' => get_string('roles', 'role'), 'showtab' => true, 'showbutton' => false, 'image' => 'tag'),
array('tab_id' => 'course_enginepage', 'page' => 'course_enginepage', 'name' => get_string('results_engine', 'local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'calculator'),
array('tab_id' => 'course_enginestatuspage', 'page' => 'course_enginestatuspage', 'name' => get_string('status_report', 'local_elisprogram'), 'showtab' => false, 'showbutton' => false),
array(
'tab_id' => 'course_certificatepage',
'page' => 'course_certificatepage',
'name' => get_string('certificate_settings', 'local_elisprogram'),
'showtab' => $showcoursecerttab,
'showbutton' => $showcoursecerttab,
'image' => 'certificate'
),
array('tab_id' => 'delete', 'page' => get_class($this), 'params' => array('action' => 'delete'), 'name' => get_string('delete_label', 'local_elisprogram'), 'showbutton' => true, 'image' => 'delete'),
);
parent::__construct($params);
}
function can_do_view() {
return $this->_has_capability('local/elisprogram:course_view');
}
function can_do_edit() {
return $this->_has_capability('local/elisprogram:course_edit');
}
/**
* Check if the user has the capability to delete course LOs
* @return bool true if user has capability, false otherwise
*/
public function can_do_confirmdelem() {
return $this->can_do_edit();
}
/**
* Check if the user has the capability to create course LOs
* @return bool true if user has capability, false otherwise
*/
public function can_do_selem() {
return $this->can_do_edit();
}
function can_do_delete() {
return $this->_has_capability('local/elisprogram:course_delete');
}
function can_do_add() {
$context = context_system::instance();
return has_capability('local/elisprogram:course_create', $context);
}
function can_do_default() {
$contexts = coursepage::get_contexts('local/elisprogram:course_view');
return !$contexts->is_empty();
}
function display_default() {
global $DB, $USER;
// Get parameters
$sort = optional_param('sort', 'name', PARAM_ALPHA);
$dir = optional_param('dir', 'ASC', PARAM_ALPHA);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
$namesearch = trim(optional_param('search', '', PARAM_TEXT));
$alpha = optional_param('alpha', '', PARAM_ALPHA);
// Define columns
$columns = array(
'name' => array('header' => get_string('course_name','local_elisprogram')),
'code' => array('header' => get_string('course_code','local_elisprogram')),
'version' => array('header' => get_string('course_version','local_elisprogram')),
'curricula' => array('header' => get_string('course_curricula','local_elisprogram'),
'display_function' => 'count_curricula'),
);
// Set sorting
if ($dir !== 'DESC') {
$dir = 'ASC';
}
if (isset($columns[$sort])) {
$columns[$sort]['sortable'] = $dir;
}
// TBD: get context set ...
$contextset = coursepage::get_contexts('local/elisprogram:course_view');
//$contextset = pm_context_set::for_user_with_capability('course','local/elisprogram:course_view', $USER->id);
// Get list of courses
$items = course_get_listing($sort, $dir, $page*$perpage, $perpage, $namesearch, $alpha, $contextset);
$numitems = course_count_records($namesearch, $alpha, $contextset);
// Cache the context capabilities
coursepage::get_contexts('local/elisprogram:course_edit');
coursepage::get_contexts('local/elisprogram:course_delete');
$this->print_list_view($items, $numitems, $columns, $filter=null, $alphaflag=true, $searchflag=true);
}
function display_lelem() {
$id = required_param('id', PARAM_INT);
$crsid = required_param('id', PARAM_INT);
$this->print_tabs('elem', array('id' => $id));
echo $this->get_completion_page($crsid);
}
function display_eelem() {
$this->display_celem();
}
function display_celem() {
$id = required_param('id', PARAM_INT);
$elemid = optional_param('elemid', 0, PARAM_INT);
$this->print_tabs('elem', array('id' => $id));
echo $this->get_completion_edit_form($id, $elemid);
}
function display_selem() {
$id = required_param('id', PARAM_INT);
$crs = new course($id);
$crs->seturl(null, array('s'=>'crs', 'section'=>'curr', 'action'=>'selem'));
$form = $crs->create_completion_form($this->optional_param('elemid', 0, PARAM_INT));
if (!$form->is_cancelled() && !$form->is_validated()) {
$this->print_tabs('elem', array('id' => $id));
$form->display();
return;
}
if (!$form->is_cancelled()) {
$elemrecord = new Object();
$elemrecord->id = optional_param('elemid', 0, PARAM_INT);
$elemrecord->idnumber = optional_param('idnumber', '', PARAM_CLEAN);
$elemrecord->name = optional_param('name', '', PARAM_CLEAN);
$elemrecord->description = optional_param('description', '', PARAM_TEXT);
$elemrecord->completion_grade = optional_param('completion_grade', 0, PARAM_INT);
$elemrecord->required = optional_param('required', 0, PARAM_INT);
$crs->save_completion_element($elemrecord);
}
$this->print_tabs('elem', array('id' => $id));
echo $this->get_completion_page($id);
}
function display_delem() {
$elemid = optional_param('elemid', 0, PARAM_INT);
return $this->get_delete_element_form($elemid);
}
function display_confirmdelem() {
$id = required_param('id', PARAM_INT);
$confirm = required_param('confirm', PARAM_TEXT);
$elemid = optional_param('elemid', 0, PARAM_INT);
$crs = new course($id);
if (md5($elemid) != $confirm) {
echo cm_error('Invalid confirmation code!');
} else if (!$crs->delete_completion_element($elemid)){
echo cm_error('Completion element not deleted.');
} else {
echo cm_error('Completion element deleted.');
}
$this->print_tabs('elem', array('id' => $id));
echo $this->get_completion_page($id);
}
function get_completion_page($crsid) {
global $CFG, $OUTPUT;
$output = '';
$crs = new course($crsid);
$table = new stdClass();
$elements = $crs->get_completion_elements();
if (!empty($elements) && $elements->valid() === true) {
$columns = array(
'idnumber' => array('header'=>get_string('completion_idnumber','local_elisprogram')),
'name' => array('header'=>get_string('completion_name','local_elisprogram')),
'description' => array('header'=>get_string('completion_description','local_elisprogram')),
'completion_grade' => array('header'=>get_string('completion_grade','local_elisprogram')),
'required' => array('header'=>get_string('required','local_elisprogram')),
);
if ($this->can_do_edit()) {
$columns['actions'] = array('header' => '', 'display_function' => 'htmltab_display_function', 'sortable' => false);
}
foreach ($columns as $column => $cdesc) {
$columndir = "ASC";
$columnicon = $columndir == "ASC" ? "down":"up";
$columnicon = " <img src=\"".$OUTPUT->pix_url('t/'.$columnicon)."\" alt=\"\" />";
$$column = $cdesc;
$table->head[] = $$column;
$table->align[] = "left";
$table->wrap[] = false;
}
$table->head[] = "";
$table->align[] = "center";
$table->wrap[] = true;
$newarr = array();
$editstr = get_string('edit');
$editiconattrs = array('class' => 'elisicon-edit elisicon', 'alt' => $editstr, 'title' => $editstr);
$delstr = get_string('delete');
$deliconattrs = array('class' => 'elisicon-remove elisicon', 'alt' => $delstr, 'title' => $delstr);
foreach ($elements as $element) {
$editurl = 'index.php?s=crs§ion=curr&action=eelem&id='.$crs->id.'&elemid='.$element->id;
$editbutton = html_writer::link($editurl, '', $editiconattrs);
$delurl = 'index.php?s=crs§ion=curr&action=delem&id='.$crs->id.'&elemid='.$element->id;
$deletebutton = html_writer::link($delurl, '', $deliconattrs);
$newobj = new stdClass;
foreach ($columns as $column => $cdesc) {
if ($column == 'required') {
$newobj->required = empty($element->required) ? get_string('no') : get_string('yes');
} else if ($column == 'actions') {
$newobj->actions = $editbutton.' '.$deletebutton;
} else {
$newobj->$column = $element->$column;
}
}
$newarr[] = $newobj;
}
$table = new display_table($newarr, $columns);
$output .= $table->get_html();
} else {
$output .= '<div align="center">' . get_string('no_completion_elements', 'local_elisprogram') . '</div>';
}
unset($elements);
$output .= '<br clear="all" />' . "\n";
if ($this->can_do_edit()) {
$options = array('s' => 'crs', 'section' => 'curr', 'action' => 'celem', 'id' => $crs->id);
$addelement = get_string('add_element', 'local_elisprogram');
$button = new single_button(new moodle_url('index.php', $options), $addelement, 'get', array(
'disabled' => false,
'title' => $addelement,
'id' => ''
));
$output .= html_writer::tag('div', $OUTPUT->render($button), array('align' => 'center'));
}
return $output;
}
function build_navbar_view($who = null, $id_param = 'id', $extra_params = array()) {
if (!$who) {
$who = $this;
}
$this->build_navbar_default($who);
if ($id_param == 'id' || !($id = $who->optional_param($id_param, 0, PARAM_INT))) {
$id = $who->required_param('id', PARAM_INT);
}
$obj = $this->get_new_data_object($id);
$obj->load();
$params = array_merge(array('action' => 'view', 'id' => $id), $extra_params);
$url = $this->get_new_page($params, true)->url;
$who->navbar->add(htmlspecialchars($obj), $url);
}
public function build_navbar_lelem($who = null) {
if (!$who) {
$who = $this;
}
$id = required_param('id', PARAM_INT);
$page = $this->get_new_page(array('action' => 'lelem', 'id' => $id));
$this->build_navbar_view($who);
$who->navbar->add(get_string('completion_elements', 'local_elisprogram'),
$page->url);
}
public function build_navbar_delem() {
$page = $this->get_new_page(array('action' => 'delem'));
$this->build_navbar_lelem($this);
$this->navbar->add(get_string('deleting_completion_element', 'local_elisprogram'),
$page->url);
}
public function build_navbar_celem() {
$page = $this->get_new_page(array('action' => 'celem'));
$this->build_navbar_lelem($this);
$this->navbar->add(get_string('adding_completion_element', 'local_elisprogram'),
$page->url);
}
public function build_navbar_eelem() {
$page = $this->get_new_page(array('action' => 'eelem'));
$this->build_navbar_lelem($this);
$this->navbar->add(get_string('editing_completion_element', 'local_elisprogram'),
$page->url);
}
function get_default_object_for_add() {
$obj = (object) course::get_default();
return $obj;
}
/**
* Hook that gets called after a CM entity is added through this page
*
* @param object $cm_entity The CM entity added
* @uses $DB
* @uses $USER
*/
function after_cm_entity_add($cm_entity) {
global $DB, $USER;
//make sure a valid role is set
if(!empty(elis::$config->local_elisprogram->default_course_role_id) && $DB->record_exists('role', array('id' => elis::$config->local_elisprogram->default_course_role_id))) {
//get the context instance for capability checking
$context_instance = \local_elisprogram\context\course::instance($cm_entity->id);
//assign the appropriate role if the user does not have the edit capability
if (!has_capability('local/elisprogram:course_edit', $context_instance)) {
role_assign(elis::$config->local_elisprogram->default_course_role_id, $USER->id, $context_instance->id);
}
}
}
/**
* Returns the edit course completion form.
*
* @return string HTML for the form.
*/
function get_completion_edit_form($crsid, $elemid=0) {
$output = '';
$crs = new course($crsid);
$crs->seturl(null, array('s'=>'crs', 'section'=>'curr', 'action'=>'selem'));
$output .= $crs->edit_completion_form_html($elemid);
return $output;
}
function get_delete_element_form($elemid) {
global $DB;
$elemrecord = $DB->get_record(coursecompletion::TABLE, array('id'=>$elemid));
if (!($elemrecord)) {
print_error('Undefined completion element.');
}
$crs = new course($elemrecord->courseid);
$url = 'index.php';
$message = get_string('confirm_delete_completion', 'local_elisprogram', $elemrecord->idnumber);
$optionsyes = array('s' => 'crs', 'section' => 'curr', 'action' => 'confirmdelem',
'id' => $crs->id, 'elemid' => $elemid, 'confirm' => md5($elemid));
$optionsno = array('s' => 'crs', 'section' => 'curr', 'id' => $crs->id, 'action' => 'lelem');
echo cm_delete_form($url, $message, $optionsyes, $optionsno);
}
/**
* Specifies a unique shortname for the entity represented by
* a page of this type, transforming the supplied value if necessary
*
* @param string $parent_path Path of all parent elements, or the empty string
* if none
* @param string $name Initial name provided for the element
*
* @return string|NULL A valid name to identify the item with, or NULL if
* not applicable
*/
static function get_entity_name($parent_path, $name) {
$parts = explode('_', $name);
//try to find the entity type and id, and combine them
if (count($parts) == 2) {
if ($parts[0] == 'course') {
return $parts[0] . '-' . $parts[1];
}
}
return NULL;
}
}
function count_curricula($column, $item) {
return curriculumcourse_count_curriculum_records($item->id);
}