source: moodle/trunk/fuentes/enrol/guest/edit.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 3.2 KB
Line 
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.
13//
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/>.
16
17/**
18 * Edit instance of enrol_guest.
19 *
20 * Adds new instance of enrol_guest to specified course
21 * or edits current instance.
22 *
23 * @package    enrol_guest
24 * @copyright  2015 Andrew Hancox <andrewdchancox@googlemail.com>
25 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require('../../config.php');
29
30$courseid   = required_param('courseid', PARAM_INT);
31$instanceid = optional_param('id', 0, PARAM_INT);
32
33$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
34$context = context_course::instance($course->id, MUST_EXIST);
35
36require_login($course);
37require_capability('enrol/guest:config', $context);
38
39$PAGE->set_url('/enrol/guest/edit.php', array('courseid' => $course->id, 'id' => $instanceid));
40$PAGE->set_pagelayout('admin');
41
42$return = new moodle_url('/enrol/instances.php', array('id' => $course->id));
43if (!enrol_is_enabled('guest')) {
44    redirect($return);
45}
46
47$plugin = enrol_get_plugin('guest');
48
49if ($instanceid) {
50    $conditions = array('courseid' => $course->id, 'enrol' => 'guest', 'id' => $instanceid);
51    $instance = $DB->get_record('enrol', $conditions, '*', MUST_EXIST);
52} else {
53    require_capability('moodle/course:enrolconfig', $context);
54    // No instance yet, we have to add new instance.
55    navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id' => $course->id)));
56
57    $instance = (object)$plugin->get_instance_defaults();
58    $instance->id       = null;
59    $instance->courseid = $course->id;
60}
61
62$mform = new \enrol_guest\enrol_guest_edit_form(null, array($instance, $plugin));
63$mform->set_data($instance);
64
65if ($mform->is_cancelled()) {
66    redirect($return);
67
68} else if ($data = $mform->get_data()) {
69
70    if ($instance->id) {
71        $reset = ($instance->status != $data->status);
72
73        $instance->status         = $data->status;
74        $instance->password       = $data->password;
75        $instance->timemodified   = time();
76        $DB->update_record('enrol', $instance);
77
78        if ($reset) {
79            $context->mark_dirty();
80        }
81
82        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
83    } else {
84        $fields = array(
85            'status'          => $data->status,
86            'password'        => $data->password);
87        $plugin->add_instance($course, $fields);
88    }
89
90    redirect($return);
91}
92
93$PAGE->set_heading($course->fullname);
94$PAGE->set_title(get_string('pluginname', 'enrol_guest'));
95
96echo $OUTPUT->header();
97echo $OUTPUT->heading(get_string('pluginname', 'enrol_guest'));
98$mform->display();
99echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.