Merge branch 'MOODLE_21_STABLE' into install_21_STABLE
[moodle.git] / enrol / cohort / addinstance.php
blobbafb716701696fbc1105a21720f8076717f44100
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Adds new instance of enrol_cohort to specified course.
21 * @package enrol
22 * @subpackage cohort
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../../config.php');
28 require_once("$CFG->dirroot/enrol/cohort/addinstance_form.php");
29 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
31 $id = required_param('id', PARAM_INT); // course id
33 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
34 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
36 require_login($course);
37 require_capability('moodle/course:enrolconfig', $context);
38 require_capability('enrol/cohort:config', $context);
40 $PAGE->set_url('/enrol/cohort/addinstance.php', array('id'=>$course->id));
41 $PAGE->set_pagelayout('admin');
43 navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
45 // Try and make the manage instances node on the navigation active
46 $courseadmin = $PAGE->settingsnav->get('courseadmin');
47 if ($courseadmin && $courseadmin->get('users') && $courseadmin->get('users')->get('manageinstances')) {
48 $courseadmin->get('users')->get('manageinstances')->make_active();
52 $enrol = enrol_get_plugin('cohort');
53 if (!$enrol->get_newinstance_link($course->id)) {
54 redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
57 $mform = new enrol_cohort_addinstance_form(NULL, $course);
59 if ($mform->is_cancelled()) {
60 redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
62 } else if ($data = $mform->get_data()) {
63 $enrol->add_instance($course, array('customint1'=>$data->cohortid, 'roleid'=>$data->roleid));
64 enrol_cohort_sync($course->id);
65 redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
68 $PAGE->set_heading($course->fullname);
69 $PAGE->set_title(get_string('pluginname', 'enrol_cohort'));
71 echo $OUTPUT->header();
72 $mform->display();
73 echo $OUTPUT->footer();