Merge branch 'MDL-76023' of https://github.com/paulholden/moodle
[moodle.git] / cohort / index.php
blob6b7746a69dce031304e660986e8119fdd7a92ede
1 <?php
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.
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/>.
17 /**
18 * Cohort related management functions, this file needs to be included manually.
20 * @package core_cohort
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
26 require_once($CFG->dirroot.'/cohort/lib.php');
27 require_once($CFG->libdir.'/adminlib.php');
29 $contextid = optional_param('contextid', 0, PARAM_INT);
30 $page = optional_param('page', 0, PARAM_INT);
31 $searchquery = optional_param('search', '', PARAM_RAW);
32 $showall = optional_param('showall', false, PARAM_BOOL);
34 require_login();
36 if ($contextid) {
37 $context = context::instance_by_id($contextid, MUST_EXIST);
38 } else {
39 $context = context_system::instance();
42 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
43 throw new \moodle_exception('invalidcontext');
46 $category = null;
47 if ($context->contextlevel == CONTEXT_COURSECAT) {
48 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
51 $manager = has_capability('moodle/cohort:manage', $context);
52 $canassign = has_capability('moodle/cohort:assign', $context);
53 if (!$manager) {
54 require_capability('moodle/cohort:view', $context);
57 $strcohorts = get_string('cohorts', 'cohort');
59 if ($category) {
60 $PAGE->set_pagelayout('admin');
61 $PAGE->set_context($context);
62 $PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
64 core_course_category::page_setup();
65 // Set the cohorts node active in the settings navigation block.
66 if ($cohortsnode = $PAGE->settingsnav->find('cohort', navigation_node::TYPE_SETTING)) {
67 $cohortsnode->make_active();
70 $PAGE->set_title($strcohorts);
71 $showall = false;
72 } else {
73 admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
74 navigation_node::override_active_url(new moodle_url('/cohort/index.php'));
75 if ($showall) {
76 $strallcohorts = get_string('allcohorts', 'cohort');
77 $PAGE->set_title($strallcohorts);
78 $PAGE->navbar->add($strallcohorts, $PAGE->url);
79 } else {
80 $PAGE->set_title(get_string('systemcohorts', 'cohort'));
84 echo $OUTPUT->header();
86 if ($showall) {
87 $cohorts = cohort_get_all_cohorts($page, 25, $searchquery);
88 } else {
89 $cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
92 $count = '';
93 if ($cohorts['allcohorts'] > 0) {
94 if ($searchquery === '') {
95 $count = ' ('.$cohorts['allcohorts'].')';
96 } else {
97 $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
101 echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
103 $params = array('page' => $page);
104 if ($contextid) {
105 $params['contextid'] = $contextid;
107 if ($searchquery) {
108 $params['search'] = $searchquery;
110 if ($showall) {
111 $params['showall'] = true;
113 $baseurl = new moodle_url('/cohort/index.php', $params);
115 if ($editcontrols = cohort_edit_controls($context, $baseurl)) {
116 echo $OUTPUT->render($editcontrols);
119 // Add search form.
120 $hiddenfields = [
121 (object) ['name' => 'contextid', 'value' => $contextid],
122 (object) ['name' => 'showall', 'value' => $showall]
125 $data = [
126 'action' => new moodle_url('/cohort/index.php'),
127 'inputname' => 'search',
128 'searchstring' => get_string('search', 'cohort'),
129 'query' => $searchquery,
130 'hiddenfields' => $hiddenfields,
131 'extraclasses' => 'mb-3'
134 echo $OUTPUT->render_from_template('core/search_input', $data);
136 // Output pagination bar.
137 echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
139 $data = array();
140 $editcolumnisempty = true;
141 foreach($cohorts['cohorts'] as $cohort) {
142 $line = array();
143 $cohortcontext = context::instance_by_id($cohort->contextid);
144 $cohort->description = file_rewrite_pluginfile_urls($cohort->description, 'pluginfile.php', $cohortcontext->id,
145 'cohort', 'description', $cohort->id);
146 if ($showall) {
147 if ($cohortcontext->contextlevel == CONTEXT_COURSECAT) {
148 $line[] = html_writer::link(new moodle_url('/cohort/index.php' ,
149 array('contextid' => $cohort->contextid)), $cohortcontext->get_context_name(false));
150 } else {
151 $line[] = $cohortcontext->get_context_name(false);
154 $tmpl = new \core_cohort\output\cohortname($cohort);
155 $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
156 $tmpl = new \core_cohort\output\cohortidnumber($cohort);
157 $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
158 $line[] = format_text($cohort->description, $cohort->descriptionformat);
160 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
162 if (empty($cohort->component)) {
163 $line[] = get_string('nocomponent', 'cohort');
164 } else {
165 $line[] = get_string('pluginname', $cohort->component);
168 $buttons = array();
169 if (empty($cohort->component)) {
170 $cohortmanager = has_capability('moodle/cohort:manage', $cohortcontext);
171 $cohortcanassign = has_capability('moodle/cohort:assign', $cohortcontext);
173 $urlparams = array('id' => $cohort->id, 'returnurl' => $baseurl->out_as_local_url(false));
174 $showhideurl = new moodle_url('/cohort/edit.php', $urlparams + array('sesskey' => sesskey()));
175 if ($cohortmanager) {
176 if ($cohort->visible) {
177 $showhideurl->param('hide', 1);
178 $visibleimg = $OUTPUT->pix_icon('t/hide', get_string('hide'));
179 $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('hide')));
180 } else {
181 $showhideurl->param('show', 1);
182 $visibleimg = $OUTPUT->pix_icon('t/show', get_string('show'));
183 $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('show')));
185 $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams + array('delete' => 1)),
186 $OUTPUT->pix_icon('t/delete', get_string('delete')),
187 array('title' => get_string('delete')));
188 $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams),
189 $OUTPUT->pix_icon('t/edit', get_string('edit')),
190 array('title' => get_string('edit')));
191 $editcolumnisempty = false;
193 if ($cohortcanassign) {
194 $buttons[] = html_writer::link(new moodle_url('/cohort/assign.php', $urlparams),
195 $OUTPUT->pix_icon('i/users', get_string('assign', 'core_cohort')),
196 array('title' => get_string('assign', 'core_cohort')));
197 $editcolumnisempty = false;
200 $line[] = implode(' ', $buttons);
202 $data[] = $row = new html_table_row($line);
203 if (!$cohort->visible) {
204 $row->attributes['class'] = 'dimmed_text';
207 $table = new html_table();
208 $table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
209 get_string('memberscount', 'cohort'), get_string('component', 'cohort'));
210 $table->colclasses = array('leftalign name', 'leftalign id', 'leftalign description', 'leftalign size','centeralign source');
211 if ($showall) {
212 array_unshift($table->head, get_string('category'));
213 array_unshift($table->colclasses, 'leftalign category');
215 if (!$editcolumnisempty) {
216 $table->head[] = get_string('edit');
217 $table->colclasses[] = 'centeralign action';
218 } else {
219 // Remove last column from $data.
220 foreach ($data as $row) {
221 array_pop($row->cells);
224 $table->id = 'cohorts';
225 $table->attributes['class'] = 'admintable generaltable';
226 $table->data = $data;
227 echo html_writer::table($table);
228 echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
229 echo $OUTPUT->footer();