Moodle release 2.5.4
[moodle.git] / cohort / index.php
blob3c31556f13263b0ac34428c968bb3574f3825279
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($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);
33 require_login();
35 if ($contextid) {
36 $context = context::instance_by_id($contextid, MUST_EXIST);
37 } else {
38 $context = context_system::instance();
41 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
42 print_error('invalidcontext');
45 $category = null;
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);
52 if (!$manager) {
53 require_capability('moodle/cohort:view', $context);
56 $strcohorts = get_string('cohorts', 'cohort');
58 if ($category) {
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);
64 } else {
65 admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
68 echo $OUTPUT->header();
70 $cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
72 $count = '';
73 if ($cohorts['allcohorts'] > 0) {
74 if ($searchquery === '') {
75 $count = ' ('.$cohorts['allcohorts'].')';
76 } else {
77 $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
81 echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
83 // Add search form.
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::empty_tag('input', array('type'=>'hidden', 'name'=>'contextid', 'value'=>$contextid));
90 $search .= html_writer::end_tag('div');
91 $search .= html_writer::end_tag('form');
92 echo $search;
95 // Output pagination bar.
96 $params = array('page' => $page);
97 if ($contextid) {
98 $params['contextid'] = $contextid;
100 if ($search) {
101 $params['search'] = $searchquery;
103 $baseurl = new moodle_url('/cohort/index.php', $params);
104 echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
106 $data = array();
107 foreach($cohorts['cohorts'] as $cohort) {
108 $line = array();
109 $line[] = format_string($cohort->name);
110 $line[] = s($cohort->idnumber); // All idnumbers are plain text.
111 $line[] = format_text($cohort->description, $cohort->descriptionformat);
113 $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
115 if (empty($cohort->component)) {
116 $line[] = get_string('nocomponent', 'cohort');
117 } else {
118 $line[] = get_string('pluginname', $cohort->component);
121 $buttons = array();
122 if (empty($cohort->component)) {
123 if ($manager) {
124 $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')));
125 $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')));
127 if ($manager or $canassign) {
128 $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')));
131 $line[] = implode(' ', $buttons);
133 $data[] = $line;
135 $table = new html_table();
136 $table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
137 get_string('memberscount', 'cohort'), get_string('component', 'cohort'), get_string('edit'));
138 $table->colclasses = array('leftalign name', 'leftalign id', 'leftalign description', 'leftalign size','centeralign source', 'centeralign action');
139 $table->id = 'cohorts';
140 $table->attributes['class'] = 'admintable generaltable';
141 $table->data = $data;
142 echo html_writer::table($table);
143 echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
145 if ($manager) {
146 echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
149 echo $OUTPUT->footer();