moodle/cohort/edit.php

137 lines
4.6 KiB
PHP
Raw Normal View History

2010-04-23 09:07:51 +00:00
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core
2010-04-23 09:07:51 +00:00
* @subpackage cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
2010-04-23 09:07:51 +00:00
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
require($CFG->dirroot.'/course/lib.php');
2010-04-23 09:07:51 +00:00
require($CFG->dirroot.'/cohort/lib.php');
require($CFG->dirroot.'/cohort/edit_form.php');
$id = optional_param('id', 0, PARAM_INT);
$contextid = optional_param('contextid', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_BOOL);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
require_login();
$category = null;
if ($id) {
$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
$context = context::instance_by_id($cohort->contextid, MUST_EXIST);
2010-04-23 09:07:51 +00:00
} else {
$context = context::instance_by_id($contextid, MUST_EXIST);
2010-04-23 09:07:51 +00:00
if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
print_error('invalidcontext');
}
$cohort = new stdClass();
2010-04-23 09:07:51 +00:00
$cohort->id = 0;
$cohort->contextid = $context->id;
$cohort->name = '';
$cohort->description = '';
}
2010-04-23 09:08:37 +00:00
require_capability('moodle/cohort:manage', $context);
2010-04-23 09:07:51 +00:00
$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
if (!empty($cohort->component)) {
// we can not manually edit cohorts that were created by external systems, sorry
redirect($returnurl);
}
2010-04-23 09:07:51 +00:00
$PAGE->set_context($context);
$PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
$PAGE->set_context($context);
if ($context->contextlevel == CONTEXT_COURSECAT) {
$category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
2011-07-09 17:18:06 +02:00
navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
$PAGE->set_pagelayout('report');
} else {
navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
$PAGE->set_pagelayout('admin');
2010-04-23 09:07:51 +00:00
}
if ($delete and $cohort->id) {
$PAGE->url->param('delete', 1);
if ($confirm and confirm_sesskey()) {
cohort_delete_cohort($cohort);
redirect($returnurl);
}
$strheading = get_string('delcohort', 'cohort');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
2010-05-27 09:33:46 +00:00
$PAGE->set_heading($COURSE->fullname);
2010-04-23 09:07:51 +00:00
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
$yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
$message = get_string('delconfirm', 'cohort', format_string($cohort->name));
echo $OUTPUT->confirm($message, $yesurl, $returnurl);
echo $OUTPUT->footer();
die;
}
$editoroptions = array('maxfiles'=>0, 'context'=>$context);
if ($cohort->id) {
// edit existing
$cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
2010-04-23 09:07:51 +00:00
$strheading = get_string('editcohort', 'cohort');
} else {
// add new
$cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
2010-04-23 09:07:51 +00:00
$strheading = get_string('addcohort', 'cohort');
}
$PAGE->set_title($strheading);
2010-05-27 09:33:46 +00:00
$PAGE->set_heading($COURSE->fullname);
2010-04-23 09:07:51 +00:00
$PAGE->navbar->add($strheading);
$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
2010-04-23 09:07:51 +00:00
if ($editform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $editform->get_data()) {
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
if ($data->id) {
cohort_update_cohort($data);
} else {
cohort_add_cohort($data);
}
// use new context id, it could have been changed
redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
2010-04-23 09:07:51 +00:00
}
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $editform->display();
echo $OUTPUT->footer();