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
.'/course/lib.php');
27 require($CFG->dirroot
.'/cohort/lib.php');
28 require($CFG->dirroot
.'/cohort/edit_form.php');
30 $id = optional_param('id', 0, PARAM_INT
);
31 $contextid = optional_param('contextid', 0, PARAM_INT
);
32 $delete = optional_param('delete', 0, PARAM_BOOL
);
33 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
34 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL
);
40 $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST
);
41 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
43 $context = context
::instance_by_id($contextid, MUST_EXIST
);
44 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
45 print_error('invalidcontext');
47 $cohort = new stdClass();
49 $cohort->contextid
= $context->id
;
51 $cohort->description
= '';
54 require_capability('moodle/cohort:manage', $context);
57 $returnurl = new moodle_url($returnurl);
59 $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id
));
62 if (!empty($cohort->component
)) {
63 // We can not manually edit cohorts that were created by external systems, sorry.
67 $PAGE->set_context($context);
68 $baseurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id
, 'id' => $cohort->id
));
69 $PAGE->set_url($baseurl);
70 $PAGE->set_context($context);
71 $PAGE->set_pagelayout('admin');
73 if ($context->contextlevel
== CONTEXT_COURSECAT
) {
74 $category = $DB->get_record('course_categories', array('id'=>$context->instanceid
), '*', MUST_EXIST
);
75 navigation_node
::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid
)));
78 navigation_node
::override_active_url(new moodle_url('/cohort/index.php', array()));
81 if ($delete and $cohort->id
) {
82 $PAGE->url
->param('delete', 1);
83 if ($confirm and confirm_sesskey()) {
84 cohort_delete_cohort($cohort);
87 $strheading = get_string('delcohort', 'cohort');
88 $PAGE->navbar
->add($strheading);
89 $PAGE->set_title($strheading);
90 $PAGE->set_heading($COURSE->fullname
);
91 echo $OUTPUT->header();
92 echo $OUTPUT->heading($strheading);
93 $yesurl = new moodle_url('/cohort/edit.php', array('id' => $cohort->id
, 'delete' => 1,
94 'confirm' => 1, 'sesskey' => sesskey(), 'returnurl' => $returnurl->out_as_local_url()));
95 $message = get_string('delconfirm', 'cohort', format_string($cohort->name
));
96 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
97 echo $OUTPUT->footer();
101 $editoroptions = array('maxfiles'=>0, 'context'=>$context);
104 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
105 $strheading = get_string('editcohort', 'cohort');
109 $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
110 $strheading = get_string('addcohort', 'cohort');
113 $PAGE->set_title($strheading);
114 $PAGE->set_heading($COURSE->fullname
);
115 $PAGE->navbar
->add($strheading);
117 $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort, 'returnurl'=>$returnurl));
119 if ($editform->is_cancelled()) {
120 redirect($returnurl);
122 } else if ($data = $editform->get_data()) {
123 $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
126 cohort_update_cohort($data);
128 cohort_add_cohort($data);
131 if ($returnurl->get_param('showall') ||
$returnurl->get_param('contextid') == $data->contextid
) {
132 // Redirect to where we were before.
133 redirect($returnurl);
135 // Use new context id, it has been changed.
136 redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid
)));
140 echo $OUTPUT->header();
141 echo $OUTPUT->heading($strheading);
143 if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
144 echo $OUTPUT->render($editcontrols);
147 echo $editform->display();
148 echo $OUTPUT->footer();