MDL-21781 fixed cap checks
[moodle.git] / cohort / index.php
CommitLineData
b980c56e
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Cohort related management functions, this file needs to be included manually.
20 *
21 * @package moodlecore
22 * @subpackage cohort
23 * @copyright 2010 Petr Skoda (info@skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require('../config.php');
28
29$contextid = optional_param('contextid', 0, PARAM_INT);
30
31require_login();
32
33if ($contextid) {
34 $context = get_context_instance_by_id($contextid, MUST_EXIST);
35} else {
36 $context = get_context_instance(CONTEXT_SYSTEM);
37}
38
39if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
40 print_error('invalidcontext');
41}
42
43$category = null;
44if ($context->contextlevel == CONTEXT_COURSECAT) {
45 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
46}
47
48$manager = has_capability('moodle/cohort:manage', $context);
49if (!$manager) {
50 require_capability('moodle/cohort:view', $context);
51}
52
53// TODO: use admin_external_page in system context
54
55$strcohorts = get_string('cohorts', 'cohort');
56
57$PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
58$PAGE->set_title($strcohorts);
59$PAGE->set_context($context);
60
61// TODO: ohlala, the navbar is not doing what I would expect
62$PAGE->navbar->add(get_string('home'), new moodle_url('/'), navbar::TYPE_SYSTEM);
63if ($category) {
64 $PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
65}
66$PAGE->navbar->add($strcohorts);
67
68echo $OUTPUT->header();
69
70echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));
71
72$cohorts = $DB->get_records('cohort', array('contextid'=>$context->id));
73
74$data = array();
75foreach($cohorts as $cohort) {
76 $line = array();
77 $line[] = format_string($cohort->name);
78 $line[] = $cohort->idnumber;
79 $line[] = format_text($cohort->description, $cohort->descriptionformat);
80
81 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
82
83 if (empty($cohort->component)) {
84 $line[] = get_string('nocomponent', 'cohort');
85 } else {
86 $line[] = get_string('pluginname', $cohort->component);
87 }
88
89 if ($manager) {
90 if (empty($cohort->component)) {
91 $buttons = html_writer::link(new moodle_url('/cohort/edit.php', array('id'=>$cohort->id)), get_string('edit'));
92 $buttons .= ' '.html_writer::link(new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1)), get_string('delete'));
93 $buttons .= ' '.html_writer::link(new moodle_url('/cohort/assign.php', array('id'=>$cohort->id)), get_string('assign', 'cohort'));
94 } else {
95 $buttons = '';
96 }
97 } else {
98 $buttons = '';
99 }
100 $line[] = $buttons;
101
102 $data[] = $line;
103}
104$table = new html_table();
105$table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
106 get_string('memberscount', 'cohort'), get_string('component', 'cohort'), get_string('edit'));
107$table->size = array('20%', '10%', '40%', '10%', '10%', '10%');
108$table->align = array('left', 'left', 'left', 'left','center', 'center');
109$table->width = '80%';
110$table->data = $data;
111echo html_writer::table($table);
112
113if ($manager) {
114 echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
115}
116
117echo $OUTPUT->footer();