weekly release 5.0dev
[moodle.git] / enrol / editinstance.php
blob047ab93e933048df7f0588fe7fcb693e83f42b02
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 * Adds new instance of an enrolment plugin to specified course or edits current instance.
20 * @package core_enrol
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
26 require_once('editinstance_form.php');
28 $courseid = required_param('courseid', PARAM_INT);
29 $type = required_param('type', PARAM_COMPONENT);
30 $instanceid = optional_param('id', 0, PARAM_INT);
31 $return = optional_param('returnurl', 0, PARAM_LOCALURL);
32 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
33 $context = context_course::instance($course->id, MUST_EXIST);
35 $plugin = enrol_get_plugin($type);
36 if (!$plugin) {
37 throw new moodle_exception('invaliddata', 'error');
40 require_login($course);
42 if (!has_any_capability(['enrol/' . $type . ':config', 'moodle/course:editcoursewelcomemessage'], $context)) {
43 throw new \moodle_exception('nopermissiontoaccesspage', 'error');
46 $url = new moodle_url('/enrol/editinstance.php', ['courseid' => $course->id, 'id' => $instanceid, 'type' => $type]);
47 $PAGE->set_url($url);
48 $PAGE->set_pagelayout('admin');
49 $PAGE->set_docs_path('enrol/' . $type . '/edit');
51 if (empty($return)) {
52 $return = new moodle_url('/enrol/instances.php', array('id' => $course->id));
55 if (!enrol_is_enabled($type)) {
56 redirect($return);
59 if ($instanceid) {
60 $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => $type, 'id' => $instanceid), '*', MUST_EXIST);
61 if ($instance->status == ENROL_INSTANCE_DISABLED) { // The instance is currently disabled.
62 navigation_node::override_active_url(new moodle_url('/enrol/instances.php', ['id' => $course->id]));
63 $name = $instance->name ?: get_string('pluginname', 'enrol_' . $type);
64 $PAGE->navbar->add($name, $url);
67 } else {
68 require_capability('moodle/course:enrolconfig', $context);
69 // No instance yet, we have to add new instance.
70 navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id' => $course->id)));
72 $instance = (object)$plugin->get_instance_defaults();
73 $instance->id = null;
74 $instance->courseid = $course->id;
75 $instance->status = ENROL_INSTANCE_ENABLED; // Do not use default for automatically created instances here.
76 $PAGE->navbar->add(get_string('pluginname', 'enrol_' . $type), $url);
79 $mform = new enrol_instance_edit_form(null, array($instance, $plugin, $context, $type, $return));
81 if ($mform->is_cancelled()) {
82 redirect($return);
84 } else if ($data = $mform->get_data()) {
86 if ($instance->id) {
87 $reset = false;
88 if (isset($data->status)) {
89 $reset = ($instance->status != $data->status);
92 foreach ($data as $key => $value) {
93 $instance->$key = $value;
96 $instance->timemodified = time();
98 $plugin->update_instance($instance, $data);
100 if ($reset) {
101 $context->mark_dirty();
104 } else {
105 $fields = (array) $data;
106 $plugin->add_instance($course, $fields);
109 redirect($return);
112 $PAGE->set_heading($course->fullname);
113 $PAGE->set_title(get_string('pluginname', 'enrol_' . $type));
115 echo $OUTPUT->header();
116 echo $OUTPUT->heading(get_string('pluginname', 'enrol_' . $type));
117 $mform->display();
118 echo $OUTPUT->footer();