MDL-35465 improve cohorts coding style
[moodle.git] / cohort / index.php
CommitLineData
b980c56e 1<?php
b980c56e
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Cohort related management functions, this file needs to be included manually.
19 *
6eb8bf7f 20 * @package core_cohort
707f4136 21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require('../config.php');
543009cb 26require($CFG->dirroot.'/cohort/lib.php');
864e2805 27require_once($CFG->libdir.'/adminlib.php');
b980c56e
PS
28
29$contextid = optional_param('contextid', 0, PARAM_INT);
543009cb
RK
30$page = optional_param('page', 0, PARAM_INT);
31$searchquery = optional_param('search', '', PARAM_RAW);
b980c56e
PS
32
33require_login();
34
35if ($contextid) {
d197ea43 36 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e 37} else {
6ca657a7 38 $context = context_system::instance();
b980c56e
PS
39}
40
41if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
42 print_error('invalidcontext');
43}
44
45$category = null;
46if ($context->contextlevel == CONTEXT_COURSECAT) {
47 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
48}
49
50$manager = has_capability('moodle/cohort:manage', $context);
50584dd3 51$canassign = has_capability('moodle/cohort:assign', $context);
b980c56e
PS
52if (!$manager) {
53 require_capability('moodle/cohort:view', $context);
54}
55
b980c56e
PS
56$strcohorts = get_string('cohorts', 'cohort');
57
3f873af7 58if ($category) {
367a75fa 59 $PAGE->set_pagelayout('report');
f6eb428e 60 $PAGE->set_context($context);
864e2805
PS
61 $PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
62 $PAGE->set_title($strcohorts);
bd08a24a 63 $PAGE->set_heading($COURSE->fullname);
864e2805 64} else {
367a75fa 65 admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
b980c56e 66}
b980c56e
PS
67
68echo $OUTPUT->header();
69
70echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));
71
6eb8bf7f 72// Add search form.
543009cb
RK
73$search = html_writer::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get'));
74$search .= html_writer::start_tag('div');
6eb8bf7f 75$search .= html_writer::label(get_string('searchcohort', 'cohort'), 'cohort_search_q'); // No : in form labels!
543009cb
RK
76$search .= html_writer::empty_tag('input', array('id'=>'cohort_search_q', 'type'=>'text', 'name'=>'search', 'value'=>$searchquery));
77$search .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('search', 'cohort')));
78$search .= html_writer::end_tag('div');
79$search .= html_writer::end_tag('form');
80echo $search;
81
82$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
83
6eb8bf7f 84// Output pagination bar.
543009cb
RK
85$params = array('page' => $page);
86if ($contextid) {
87 $params['contextid'] = $contextid;
88}
89if ($search) {
90 $params['search'] = $searchquery;
91}
92$baseurl = new moodle_url('/cohort/index.php', $params);
93echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
94
95$data = array();
543009cb 96foreach($cohorts['cohorts'] as $cohort) {
b980c56e
PS
97 $line = array();
98 $line[] = format_string($cohort->name);
6eb8bf7f 99 $line[] = s($cohort->idnumber); // All idnumbers are plain text.
b980c56e
PS
100 $line[] = format_text($cohort->description, $cohort->descriptionformat);
101
102 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
103
104 if (empty($cohort->component)) {
105 $line[] = get_string('nocomponent', 'cohort');
106 } else {
107 $line[] = get_string('pluginname', $cohort->component);
108 }
109
50584dd3
PS
110 $buttons = array();
111 if (empty($cohort->component)) {
112 if ($manager) {
113 $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>get_string('delete'), 'class'=>'iconsmall')));
114 $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', array('id'=>$cohort->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/edit'), 'alt'=>get_string('edit'), 'class'=>'iconsmall')));
115 }
116 if ($manager or $canassign) {
117 $buttons[] = html_writer::link(new moodle_url('/cohort/assign.php', array('id'=>$cohort->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('i/users'), 'alt'=>get_string('assign', 'core_cohort'), 'class'=>'iconsmall')));
b980c56e 118 }
b980c56e 119 }
50584dd3 120 $line[] = implode(' ', $buttons);
b980c56e
PS
121
122 $data[] = $line;
123}
124$table = new html_table();
125$table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
126 get_string('memberscount', 'cohort'), get_string('component', 'cohort'), get_string('edit'));
127$table->size = array('20%', '10%', '40%', '10%', '10%', '10%');
128$table->align = array('left', 'left', 'left', 'left','center', 'center');
129$table->width = '80%';
130$table->data = $data;
131echo html_writer::table($table);
543009cb 132echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
133
134if ($manager) {
135 echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
136}
137
6eb8bf7f 138echo $OUTPUT->footer();