-
Notifications
You must be signed in to change notification settings - Fork 7
/
trackpage.class.php
497 lines (432 loc) · 19.7 KB
/
trackpage.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
<?php
/**
* General class for displaying pages in the curriculum management system.
*
* ELIS(TM): Enterprise Learning Intelligence Suite
* Copyright (C) 2008-2012 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 elis
* @subpackage curriculummanagement
* @author Remote-Learner.net Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2008-2012 Remote Learner.net Inc http://www.remote-learner.net
*
*/
defined('MOODLE_INTERNAL') || die();
// Waiting on conversion
//require_once (CURMAN_DIRLOCATION . '/clustertrackpage.class.php');
//require_once (CURMAN_DIRLOCATION . '/rolepage.class.php');
require_once elispm::lib('data/track.class.php');
require_once elispm::lib('data/user.class.php');
require_once elispm::file('usertrackpage.class.php');
require_once elispm::lib('managementpage.class.php');
require_once elispm::file('trackassignmentpage.class.php');
require_once elispm::file('form/trackform.class.php');
class trackpage extends managementpage {
var $data_class = 'track';
var $form_class = 'trackform';
var $pagename = 'trk';
var $section = 'curr';
var $view_columns = array('name', 'description');
static $contexts = array();
public static function get_contexts($capability) {
if (!isset(trackpage::$contexts[$capability])) {
global $USER;
trackpage::$contexts[$capability] = get_contexts_by_capability_for_user('track', $capability, $USER->id);
}
return trackpage::$contexts[$capability];
}
/**
* Check the cached capabilities for the current track.
*/
public static function check_cached($capability, $id) {
if (isset(trackpage::$contexts[$capability])) {
// we've already cached which contexts the user has delete
// capabilities in
$contexts = trackpage::$contexts[$capability];
return $contexts->context_allowed($id, 'track');
}
return null;
}
/**
* Determines whether the current user is allowed to enrol users into the provided track
*
* @param int $trackid The id of the track we are checking permissions on
*
* @return boolean Whether the user is allowed to enrol users into the curriculum
*
*/
public static function can_enrol_into_track($trackid) {
global $USER;
//check the standard capability
// TODO: Ugly, this needs to be overhauled
$tpage = new trackpage();
if ($tpage->_has_capability('local/elisprogram:track_enrol', $trackid)) {
return true;
}
//get the context for the "indirect" capability
$context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:track_enrol_userset_user', $USER->id);
//get the clusters and check the context against them
$clusters = clustertrack::get_clusters($trackid);
if(!empty($clusters)) {
foreach($clusters as $cluster) {
if($context->context_allowed($cluster->clusterid, 'cluster')) {
return true;
}
}
}
return false;
}
/**
* Check if the user has the given capability for the requested track
*/
public function _has_capability($capability, $id = null) {
$id = $id ? $id : $this->required_param('id', PARAM_INT);
$cached = trackpage::check_cached($capability, $id);
if ($cached !== null) {
return $cached;
}
$context = \local_elisprogram\context\track::instance($id);
return has_capability($capability, $context);
}
public function __construct(array $params=null) {
parent::__construct($params);
$curid = $this->optional_param('parent', 0, PARAM_INT); // TBD: get_cm_id(empty($action));
$curid_param = $curid ? array('parent' => $curid) : array();
$this->tabs = array(
array('tab_id' => 'view', 'page' => 'trackpage', 'params' => array('action' => 'view') + $curid_param, 'name' => get_string('detail','local_elisprogram'), 'showtab' => true),
array('tab_id' => 'edit', 'page' => 'trackpage', 'params' => array('action' => 'edit') + $curid_param, 'name' => get_string('edit','local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'edit'),
array('tab_id' => 'trackclusterpage', 'page' => 'trackclusterpage', 'name' => get_string('clusters','local_elisprogram'), 'params' => $curid_param, 'showtab' => true, 'showbutton' => true, 'image' => 'cluster'),
array('tab_id' => 'trackuserpage', 'page' => 'trackuserpage', 'params' => $curid_param, 'name' => get_string('users','local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'user'),
array('tab_id' => 'trackassignmentpage', 'page' => 'trackassignmentpage', 'params' => $curid_param, 'name' => get_string('track_classes','local_elisprogram'), 'showtab' => true, 'showbutton' => true, 'image' => 'class'),
array('tab_id' => 'track_rolepage', 'page' => 'track_rolepage', 'params' => $curid_param, 'name' => get_string('roles', 'role'), 'showtab' => true, 'showbutton' => false, 'image' => 'tag'),
array('tab_id' => 'delete', 'page' => 'trackpage', 'params' => array('action' => 'delete') + $curid_param, 'name' => get_string('delete','local_elisprogram'), 'showbutton' => true, 'image' => 'delete'),
);
}
function can_do_view() {
return $this->_has_capability('local/elisprogram:track_view');
}
function can_do_edit() {
return $this->_has_capability('local/elisprogram:track_edit');
}
function can_do_delete() {
return $this->_has_capability('local/elisprogram:track_delete');
}
function can_do_confirm() {
return $this->can_do_delete();
}
function can_do_add() {
global $USER;
if (!empty($USER->id)) {
$contexts = get_contexts_by_capability_for_user('curriculum',
'local/elisprogram:track_create', $USER->id);
if ($contexts->is_empty()) {
return false;
}
/* ***
// Now user MUST have 'local/elisprogram:program_edit'
// on at least one curricula
$contexts = get_contexts_by_capability_for_user('curriculum',
'local/elisprogram:program_edit', $USER->id);
return !$contexts->is_empty();
*** */
return true;
}
return false;
}
function can_do_default() {
$contexts = trackpage::get_contexts('local/elisprogram:track_view');
return !$contexts->is_empty();
}
/**
* Overrides the default navigation to include curriculum breadcrumbs if appropriate
*/
function build_navbar_default($who = null, $addparent = true, $params = array()) {
$pgid = optional_param('s', 'trk', PARAM_TEXT);
$action = $this->optional_param('action', '', PARAM_CLEAN);
$cancel = $this->optional_param('cancel', '', PARAM_CLEAN);
$paramname = '';
$parent = $this->get_cm_id(empty($action), $paramname);
$params = array();
$lp_bc = true;
if (!empty($parent) && strncmp($pgid, 'trk', 3) != 0) {
//viewing from within curriculum
$params['id'] = $parent;
//$params[$paramname] = $parent;
$curriculumpage = new curriculumpage($params);
$curriculum_navigation = $curriculumpage->build_navbar_view($this, $paramname);
$lp_bc = false;
}
parent::build_navbar_default(null, $lp_bc, $params);
}
function build_navbar_view($who = null, $id_param = 'id', $extra_params = array()) {
$paramname = '';
$curid = $this->get_cm_id(false, $paramname);
parent::build_navbar_view(null, 'id', $curid ? array($paramname => $curid): array());
}
/**
* Display the track listing, filtering on curriculum if the id parameter is present
*/
function display_default() {
// 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);
$parent_clusterid = $this->optional_param('parent_clusterid', 0, PARAM_INT);
//curriculum id
$id = $this->get_cm_id();
// Define columns
$columns = array(
'name' => array('header' => get_string('track_name', 'local_elisprogram')),
'description' => array('header' => get_string('track_description', 'local_elisprogram')),
'parcur' => array('header' => get_string('track_parcur', 'local_elisprogram')),
'class' => array('header' => get_string('track_classes', 'local_elisprogram'))
);
// TBD
if ($dir !== 'DESC') {
$dir = 'ASC';
}
if (isset($columns[$sort])) {
$columns[$sort]['sortable'] = $dir;
} else {
$sort = 'name';
$columns[$sort]['sortable'] = $dir;
}
$items = track_get_listing($sort, $dir, $page*$perpage, $perpage, $namesearch, $alpha, $id, $parent_clusterid, trackpage::get_contexts('local/elisprogram:track_view'));
$numitems = track_count_records($namesearch, $alpha, $id, $parent_clusterid, trackpage::get_contexts('local/elisprogram:track_view'));
trackpage::get_contexts('local/elisprogram:track_edit');
trackpage::get_contexts('local/elisprogram:track_delete');
if (!empty($id)) {
//print curriculum tabs if viewing from the curriculum view
$curriculumpage = new curriculumpage(array('id' => $id));
$curriculumpage->print_tabs('trackpage', array('id' => $id));
}
$this->print_list_view($items, $numitems, $columns, $filter=null, $alphaflag=true, $searchflag=true);
}
function add_table_items($items, $columns, $table=null) {
//$countries = cm_get_list_of_countries(); ???
$table->width = "95%";
foreach ($items as $item) {
// TODO: (short term) push this logic to the page class, by using a get_cell_value($item, $columnname) function that is called for
// each cell in the table.
// TODO: (long term) push this logic to the model, either by using accessors or by using field types
$newarr = array();
foreach ($columns as $column => $cdesc) {
if ($column == 'class') {
$newarr[] = '<a href="index.php?s=trkcls&section=curr&trackid='.$item->id.
'">' . $item->$column . '</a>';
} else {
$newarr[] = $item->$column;
}
// Add link to specified columns
if (in_array($column, $this->view_columns)) {
$target = $this->get_new_page(array('action' => 'view', 'id' => $item->id));
$newarr[count($newarr)-1] = '<a href="' . $target->url . '">' . $newarr[count($newarr)-1] . '</a>';
}
}
$newarr[] = $this->get_buttons(array('id' => $item->id));
$table->data[] = $newarr;
}
return $table;
}
/**
* Prints the single-button form used to request the add action for a record type.
*/
function print_add_button() {
global $OUTPUT;
if (!$this->can_do('add')) {
return;
}
$obj = $this->get_new_data_object();
echo '<div align="center">';
$options = array('s' => $this->pagename, 'action' => 'add');
$parent = $this->optional_param('id', 0, PARAM_INT);
if ($parent) {
$options['parent'] = $parent;
}
// FIXME: change to language string
//echo print_single_button('index.php', $options, get_string('add','local_elisprogram').' ' . get_string($obj->get_verbose_name(),'local_elisprogram'), 'get', '_self', true, get_string('add','local_elisprogram').' ' . get_string($obj->get_verbose_name(),'local_elisprogram'));
$button = new single_button(new moodle_url('index.php', $options), get_string('add_track','local_elisprogram'), 'get');
echo $OUTPUT->render($button);
echo '</div>';
}
/**
* Converts add button data to data for our add form
*
* @return stdClass Form data, or null if none
*/
function get_default_object_for_add() {
$parent = $this->optional_param('parent', 0, PARAM_INT);
if ($parent) {
$obj = new stdClass;
$obj->curid = $parent;
$obj->parent = $parent;
return $obj;
} else {
return NULL;
}
}
/**
* Hook that gets called after a CM entity is added through this page
* (Note: this function should only use the id field from the supplied cm entity
* as the rest of the data is not guaranteed to be there)
*
* @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_track_role_id) && $DB->record_exists('role', array('id' => elis::$config->local_elisprogram->default_track_role_id))) {
//get the context instance for capability checking
$context_instance = \local_elisprogram\context\track::instance($cm_entity->id);
//assign the appropriate role if the user does not have the edit capability
if(!has_capability('local/elisprogram:track_edit', $context_instance)) {
role_assign(elis::$config->local_elisprogram->default_track_role_id, $USER->id, $context_instance->id);
}
}
}
/**
* Returns the cm id corresponding to this page's entity, taking into account
* weirdness from cancel actions
*
* @param bool $check_id true to check 'id' param
* @param string &$paramname to return param name
* @return int The appropriate id, or zero if none available
*/
function get_cm_id($check_id = true, &$paramname = null) {
$idparams = array('parent', 'curid', 'id');
foreach ($idparams as $param) {
if ($param == 'id' && !$check_id) {
break;
}
if ($paramname !== null) {
$paramname = $param;
}
$id = $this->optional_param($param, 0, PARAM_INT);
if (!empty($id)) {
break;
}
}
return $id;
}
/**
* Creates a new table object with specified $items and $columns.
* @param array $items
* @param array $columns
*/
function create_table_object($items, $columns) {
$parent_clusterid = $this->optional_param('parent_clusterid', 0, PARAM_INT);
$extra_params = array();
if (!empty($parent_clusterid)) {
$extra_params['parent_clusterid'] = $parent_clusterid;
}
if ($curid = $this->get_cm_id()) {
$extra_params['parent'] = $curid;
}
$page_object = $this->get_new_page($extra_params);
//pass along the extra parameters because they are protected within
//the page object
return new management_page_table($items, $columns, $page_object, $extra_params);
}
/**
* 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', 'trk', PARAM_TEXT);
$action = $this->optional_param('action', '', PARAM_ACTION);
if ($pgid == 'usrtrk') {
try {
$pgcontext = \local_elisprogram\context\user::instance($id);
return $pgcontext;
} catch (Execption $e) {
; // Ignore execption.
}
} else if ($pgid == 'clsttrk') {
try {
$pgcontext = \local_elisprogram\context\userset::instance($id);
return $pgcontext;
} catch (Execption $e) {
; // Ignore execption.
}
} else {
$isprogcontext = ($pgid == 'trk' && ($action == '' || $action == 'default')) || strncmp($pgid, 'cur', 3) == 0 || strncmp($pgid, 'prg', 3) == 0;
try {
$pgcontext = $isprogcontext ? \local_elisprogram\context\program::instance($id) : \local_elisprogram\context\track::instance($id);
return $pgcontext;
} catch (Execption $e) {
; // Ignore execption.
}
}
} else if (($id = $this->optional_param('parent', 0, PARAM_INT)) || ($id = $this->optional_param('curid', 0, PARAM_INT))) {
try {
$pgcontext = \local_elisprogram\context\program::instance($id);
return $pgcontext;
} catch (Execption $e) {
; // Ignore execption.
}
}
return parent::_get_page_context();
}
/**
* Determines the name of the context class that represents this page's cm entity
*
* @return string The name of the context class that represents this page's cm entity
*
* @todo Do something less complex to determine the appropriate class
* (requires page class redesign)
*/
function get_page_context() {
$action = $this->optional_param('action', '', PARAM_ACTION);
$id = $this->optional_param('id', 0, PARAM_INT);
if ($action == '' && $id > 0) {
//need a special case because there isn't a proper page for listing
//tracks that belong to a curriculum
//@todo Create a proper page that lists tracks that belong to a curriculum
return 'curriculum';
} else {
return 'track';
}
}
/**
* 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] == 'track') {
return $parts[0] . '-' . $parts[1];
}
}
return NULL;
}
}