Merge branch 'MDL-39054' of git://github.com/jacks92/moodle
[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
9a2f6770
PS
70$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
71
72$count = '';
73if ($cohorts['allcohorts'] > 0) {
74 if ($searchquery === '') {
75 $count = ' ('.$cohorts['allcohorts'].')';
76 } else {
77 $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
78 }
79}
80
81echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
b980c56e 82
6eb8bf7f 83// Add search form.
543009cb
RK
84$search = html_writer::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get'));
85$search .= html_writer::start_tag('div');
6eb8bf7f 86$search .= html_writer::label(get_string('searchcohort', 'cohort'), 'cohort_search_q'); // No : in form labels!
543009cb
RK
87$search .= html_writer::empty_tag('input', array('id'=>'cohort_search_q', 'type'=>'text', 'name'=>'search', 'value'=>$searchquery));
88$search .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('search', 'cohort')));
89$search .= html_writer::end_tag('div');
90$search .= html_writer::end_tag('form');
91echo $search;
92
543009cb 93
6eb8bf7f 94// Output pagination bar.
543009cb
RK
95$params = array('page' => $page);
96if ($contextid) {
97 $params['contextid'] = $contextid;
98}
99if ($search) {
100 $params['search'] = $searchquery;
101}
102$baseurl = new moodle_url('/cohort/index.php', $params);
103echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
104
105$data = array();
543009cb 106foreach($cohorts['cohorts'] as $cohort) {
b980c56e
PS
107 $line = array();
108 $line[] = format_string($cohort->name);
6eb8bf7f 109 $line[] = s($cohort->idnumber); // All idnumbers are plain text.
b980c56e
PS
110 $line[] = format_text($cohort->description, $cohort->descriptionformat);
111
112 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
113
114 if (empty($cohort->component)) {
115 $line[] = get_string('nocomponent', 'cohort');
116 } else {
117 $line[] = get_string('pluginname', $cohort->component);
118 }
119
50584dd3
PS
120 $buttons = array();
121 if (empty($cohort->component)) {
122 if ($manager) {
123 $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')));
124 $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')));
125 }
126 if ($manager or $canassign) {
127 $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 128 }
b980c56e 129 }
50584dd3 130 $line[] = implode(' ', $buttons);
b980c56e
PS
131
132 $data[] = $line;
133}
134$table = new html_table();
135$table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
136 get_string('memberscount', 'cohort'), get_string('component', 'cohort'), get_string('edit'));
6fc61f2d
RW
137$table->colclasses = array('leftalign name', 'leftalign id', 'leftalign description', 'leftalign size','centeralign source', 'centeralign action');
138$table->id = 'cohorts';
139$table->attributes['class'] = 'admintable generaltable';
b980c56e
PS
140$table->data = $data;
141echo html_writer::table($table);
543009cb 142echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
143
144if ($manager) {
145 echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
146}
147
6eb8bf7f 148echo $OUTPUT->footer();