2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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($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
);
36 $context = context
::instance_by_id($contextid, MUST_EXIST
);
38 $context = context_system
::instance();
41 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
42 print_error('invalidcontext');
46 if ($context->contextlevel
== CONTEXT_COURSECAT
) {
47 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid
), '*', MUST_EXIST
);
50 $manager = has_capability('moodle/cohort:manage', $context);
51 $canassign = has_capability('moodle/cohort:assign', $context);
53 require_capability('moodle/cohort:view', $context);
56 $strcohorts = get_string('cohorts', 'cohort');
59 $PAGE->set_pagelayout('report');
60 $PAGE->set_context($context);
61 $PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id
));
62 $PAGE->set_title($strcohorts);
63 $PAGE->set_heading($COURSE->fullname
);
65 admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
68 echo $OUTPUT->header();
70 $cohorts = cohort_get_cohorts($context->id
, $page, 25, $searchquery);
73 if ($cohorts['allcohorts'] > 0) {
74 if ($searchquery === '') {
75 $count = ' ('.$cohorts['allcohorts'].')';
77 $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
81 echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
84 $search = html_writer
::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get'));
85 $search .= html_writer
::start_tag('div');
86 $search .= html_writer
::label(get_string('searchcohort', 'cohort'), 'cohort_search_q'); // No : in form labels!
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');
94 // Output pagination bar.
95 $params = array('page' => $page);
97 $params['contextid'] = $contextid;
100 $params['search'] = $searchquery;
102 $baseurl = new moodle_url('/cohort/index.php', $params);
103 echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
106 foreach($cohorts['cohorts'] as $cohort) {
108 $line[] = format_string($cohort->name
);
109 $line[] = s($cohort->idnumber
); // All idnumbers are plain text.
110 $line[] = format_text($cohort->description
, $cohort->descriptionformat
);
112 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id
));
114 if (empty($cohort->component
)) {
115 $line[] = get_string('nocomponent', 'cohort');
117 $line[] = get_string('pluginname', $cohort->component
);
121 if (empty($cohort->component
)) {
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')));
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')));
130 $line[] = implode(' ', $buttons);
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'));
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';
140 $table->data
= $data;
141 echo html_writer
::table($table);
142 echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
145 echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id
)), get_string('add'));
148 echo $OUTPUT->footer();