MDL-35465 move duplicate check to cohort_add_member()
[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 *
707f4136 21 * @package core
b980c56e 22 * @subpackage cohort
707f4136 23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require('../config.php');
543009cb 28require($CFG->dirroot.'/cohort/lib.php');
864e2805 29require_once($CFG->libdir.'/adminlib.php');
b980c56e
PS
30
31$contextid = optional_param('contextid', 0, PARAM_INT);
543009cb
RK
32$page = optional_param('page', 0, PARAM_INT);
33$searchquery = optional_param('search', '', PARAM_RAW);
b980c56e
PS
34
35require_login();
36
37if ($contextid) {
d197ea43 38 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e 39} else {
6ca657a7 40 $context = context_system::instance();
b980c56e
PS
41}
42
43if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
44 print_error('invalidcontext');
45}
46
47$category = null;
48if ($context->contextlevel == CONTEXT_COURSECAT) {
49 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
50}
51
52$manager = has_capability('moodle/cohort:manage', $context);
50584dd3 53$canassign = has_capability('moodle/cohort:assign', $context);
b980c56e
PS
54if (!$manager) {
55 require_capability('moodle/cohort:view', $context);
56}
57
b980c56e
PS
58$strcohorts = get_string('cohorts', 'cohort');
59
3f873af7 60if ($category) {
367a75fa 61 $PAGE->set_pagelayout('report');
f6eb428e 62 $PAGE->set_context($context);
864e2805
PS
63 $PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
64 $PAGE->set_title($strcohorts);
bd08a24a 65 $PAGE->set_heading($COURSE->fullname);
864e2805 66} else {
367a75fa 67 admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
b980c56e 68}
b980c56e
PS
69
70echo $OUTPUT->header();
71
72echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));
73
543009cb
RK
74// add search form
75$search = html_writer::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get'));
76$search .= html_writer::start_tag('div');
77$search .= html_writer::label(get_string('searchcohort', 'cohort').':', 'cohort_search_q');
78$search .= html_writer::empty_tag('input', array('id'=>'cohort_search_q', 'type'=>'text', 'name'=>'search', 'value'=>$searchquery));
79$search .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('search', 'cohort')));
80$search .= html_writer::end_tag('div');
81$search .= html_writer::end_tag('form');
82echo $search;
83
84$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
85
86// output pagination bar
87$params = array('page' => $page);
88if ($contextid) {
89 $params['contextid'] = $contextid;
90}
91if ($search) {
92 $params['search'] = $searchquery;
93}
94$baseurl = new moodle_url('/cohort/index.php', $params);
95echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
96
97$data = array();
543009cb 98foreach($cohorts['cohorts'] as $cohort) {
b980c56e
PS
99 $line = array();
100 $line[] = format_string($cohort->name);
3c8d3d4d 101 $line[] = s($cohort->idnumber); // plain text
b980c56e
PS
102 $line[] = format_text($cohort->description, $cohort->descriptionformat);
103
104 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
105
106 if (empty($cohort->component)) {
107 $line[] = get_string('nocomponent', 'cohort');
108 } else {
109 $line[] = get_string('pluginname', $cohort->component);
110 }
111
50584dd3
PS
112 $buttons = array();
113 if (empty($cohort->component)) {
114 if ($manager) {
115 $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')));
116 $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')));
117 }
118 if ($manager or $canassign) {
119 $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 120 }
b980c56e 121 }
50584dd3 122 $line[] = implode(' ', $buttons);
b980c56e
PS
123
124 $data[] = $line;
125}
126$table = new html_table();
127$table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
128 get_string('memberscount', 'cohort'), get_string('component', 'cohort'), get_string('edit'));
129$table->size = array('20%', '10%', '40%', '10%', '10%', '10%');
130$table->align = array('left', 'left', 'left', 'left','center', 'center');
131$table->width = '80%';
132$table->data = $data;
133echo html_writer::table($table);
543009cb 134echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
135
136if ($manager) {
137 echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
138}
139
140echo $OUTPUT->footer();