MDL-56677 cohort: Fix cohort search box misalignment
[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);
274dc068 32$showall = optional_param('showall', false, PARAM_BOOL);
b980c56e
PS
33
34require_login();
35
36if ($contextid) {
d197ea43 37 $context = context::instance_by_id($contextid, MUST_EXIST);
b980c56e 38} else {
6ca657a7 39 $context = context_system::instance();
b980c56e
PS
40}
41
42if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
43 print_error('invalidcontext');
44}
45
46$category = null;
47if ($context->contextlevel == CONTEXT_COURSECAT) {
48 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
49}
50
51$manager = has_capability('moodle/cohort:manage', $context);
50584dd3 52$canassign = has_capability('moodle/cohort:assign', $context);
b980c56e
PS
53if (!$manager) {
54 require_capability('moodle/cohort:view', $context);
55}
56
b980c56e
PS
57$strcohorts = get_string('cohorts', 'cohort');
58
3f873af7 59if ($category) {
d5814f4e 60 $PAGE->set_pagelayout('admin');
f6eb428e 61 $PAGE->set_context($context);
864e2805
PS
62 $PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
63 $PAGE->set_title($strcohorts);
bd08a24a 64 $PAGE->set_heading($COURSE->fullname);
274dc068 65 $showall = false;
864e2805 66} else {
367a75fa 67 admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
b980c56e 68}
b980c56e
PS
69
70echo $OUTPUT->header();
71
274dc068
MG
72if ($showall) {
73 $cohorts = cohort_get_all_cohorts($page, 25, $searchquery);
74} else {
75 $cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
76}
9a2f6770
PS
77
78$count = '';
79if ($cohorts['allcohorts'] > 0) {
80 if ($searchquery === '') {
81 $count = ' ('.$cohorts['allcohorts'].')';
82 } else {
83 $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
84 }
85}
86
87echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
b980c56e 88
274dc068
MG
89$params = array('page' => $page);
90if ($contextid) {
91 $params['contextid'] = $contextid;
92}
93if ($searchquery) {
94 $params['search'] = $searchquery;
95}
96if ($showall) {
97 $params['showall'] = true;
98}
99$baseurl = new moodle_url('/cohort/index.php', $params);
100
101if ($editcontrols = cohort_edit_controls($context, $baseurl)) {
102 echo $OUTPUT->render($editcontrols);
103}
104
6eb8bf7f 105// Add search form.
18bb2883 106$search = html_writer::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get', 'class' => 'form-inline search-cohort'));
4bd9a67b
SL
107$search .= html_writer::start_div('m-b-1');
108$search .= html_writer::label(get_string('searchcohort', 'cohort'), 'cohort_search_q', true,
109 array('class' => 'm-r-1')); // No : in form labels!
110$search .= html_writer::empty_tag('input', array('id' => 'cohort_search_q', 'type' => 'text', 'name' => 'search',
111 'value' => $searchquery, 'class' => 'form-control m-r-1'));
112$search .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('search', 'cohort'),
113 'class' => 'btn btn-secondary'));
0fa01f4e 114$search .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'contextid', 'value'=>$contextid));
274dc068 115$search .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'showall', 'value'=>$showall));
4bd9a67b 116$search .= html_writer::end_div();
543009cb
RK
117$search .= html_writer::end_tag('form');
118echo $search;
119
6eb8bf7f 120// Output pagination bar.
543009cb 121echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
b980c56e
PS
122
123$data = array();
e65d05c1 124$editcolumnisempty = true;
543009cb 125foreach($cohorts['cohorts'] as $cohort) {
b980c56e 126 $line = array();
274dc068 127 $cohortcontext = context::instance_by_id($cohort->contextid);
62184733
MG
128 $cohort->description = file_rewrite_pluginfile_urls($cohort->description, 'pluginfile.php', $cohortcontext->id,
129 'cohort', 'description', $cohort->id);
274dc068
MG
130 if ($showall) {
131 if ($cohortcontext->contextlevel == CONTEXT_COURSECAT) {
5076b961
MG
132 $line[] = html_writer::link(new moodle_url('/cohort/index.php' ,
133 array('contextid' => $cohort->contextid)), $cohortcontext->get_context_name(false));
274dc068 134 } else {
5076b961 135 $line[] = $cohortcontext->get_context_name(false);
274dc068
MG
136 }
137 }
48983fb9
MG
138 $tmpl = new \core_cohort\output\cohortname($cohort);
139 $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
140 $tmpl = new \core_cohort\output\cohortidnumber($cohort);
141 $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
b980c56e
PS
142 $line[] = format_text($cohort->description, $cohort->descriptionformat);
143
144 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
145
146 if (empty($cohort->component)) {
147 $line[] = get_string('nocomponent', 'cohort');
148 } else {
149 $line[] = get_string('pluginname', $cohort->component);
150 }
151
50584dd3
PS
152 $buttons = array();
153 if (empty($cohort->component)) {
274dc068
MG
154 $cohortmanager = has_capability('moodle/cohort:manage', $cohortcontext);
155 $cohortcanassign = has_capability('moodle/cohort:assign', $cohortcontext);
156
157 $urlparams = array('id' => $cohort->id, 'returnurl' => $baseurl->out_as_local_url());
80f98467 158 $showhideurl = new moodle_url('/cohort/edit.php', $urlparams + array('sesskey' => sesskey()));
274dc068 159 if ($cohortmanager) {
e65d05c1
MG
160 if ($cohort->visible) {
161 $showhideurl->param('hide', 1);
162 $visibleimg = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/hide'), 'alt' => get_string('hide'), 'class' => 'iconsmall'));
163 $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('hide')));
164 } else {
165 $showhideurl->param('show', 1);
166 $visibleimg = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/show'), 'alt' => get_string('show'), 'class' => 'iconsmall'));
167 $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('show')));
168 }
274dc068 169 $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams + array('delete' => 1)),
80f98467
MG
170 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall')),
171 array('title' => get_string('delete')));
274dc068 172 $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams),
80f98467
MG
173 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall')),
174 array('title' => get_string('edit')));
e65d05c1 175 $editcolumnisempty = false;
50584dd3 176 }
274dc068
MG
177 if ($cohortcanassign) {
178 $buttons[] = html_writer::link(new moodle_url('/cohort/assign.php', $urlparams),
80f98467
MG
179 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('i/users'), 'alt' => get_string('assign', 'core_cohort'), 'class' => 'iconsmall')),
180 array('title' => get_string('assign', 'core_cohort')));
e65d05c1 181 $editcolumnisempty = false;
b980c56e 182 }
b980c56e 183 }
50584dd3 184 $line[] = implode(' ', $buttons);
b980c56e 185
80f98467
MG
186 $data[] = $row = new html_table_row($line);
187 if (!$cohort->visible) {
188 $row->attributes['class'] = 'dimmed_text';
189 }
b980c56e
PS
190}
191$table = new html_table();
192$table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
e65d05c1
MG
193 get_string('memberscount', 'cohort'), get_string('component', 'cohort'));
194$table->colclasses = array('leftalign name', 'leftalign id', 'leftalign description', 'leftalign size','centeralign source');
274dc068
MG
195if ($showall) {
196 array_unshift($table->head, get_string('category'));
197 array_unshift($table->colclasses, 'leftalign category');
198}
e65d05c1
MG
199if (!$editcolumnisempty) {
200 $table->head[] = get_string('edit');
201 $table->colclasses[] = 'centeralign action';
202} else {
203 // Remove last column from $data.
204 foreach ($data as $row) {
205 array_pop($row->cells);
206 }
207}
6fc61f2d
RW
208$table->id = 'cohorts';
209$table->attributes['class'] = 'admintable generaltable';
b980c56e
PS
210$table->data = $data;
211echo html_writer::table($table);
543009cb 212echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
6eb8bf7f 213echo $OUTPUT->footer();