source: moodle/trunk/fuentes/enrol/manual/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: 4.3 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 * Adds new instance of enrol_manual to specified course
19 * or edits current instance.
20 *
21 * @package    enrol_manual
22 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require('../../config.php');
27require_once('edit_form.php');
28
29$courseid = required_param('courseid', PARAM_INT);
30
31$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
32$context = context_course::instance($course->id, MUST_EXIST);
33
34require_login($course);
35require_capability('enrol/manual:config', $context);
36
37$PAGE->set_url('/enrol/manual/edit.php', array('courseid'=>$course->id));
38$PAGE->set_pagelayout('admin');
39
40$return = new moodle_url('/enrol/instances.php', array('id'=>$course->id));
41if (!enrol_is_enabled('manual')) {
42    redirect($return);
43}
44
45$plugin = enrol_get_plugin('manual');
46
47if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'), 'id ASC')) {
48    $instance = array_shift($instances);
49    if ($instances) {
50        // Oh - we allow only one instance per course!!
51        foreach ($instances as $del) {
52            $plugin->delete_instance($del);
53        }
54    }
55    // Merge these two settings to one value for the single selection element.
56    if ($instance->notifyall and $instance->expirynotify) {
57        $instance->expirynotify = 2;
58    }
59    unset($instance->notifyall);
60
61} else {
62    require_capability('moodle/course:enrolconfig', $context);
63    // No instance yet, we have to add new instance.
64    navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
65    $instance = new stdClass();
66    $instance->id              = null;
67    $instance->courseid        = $course->id;
68    $instance->expirynotify    = $plugin->get_config('expirynotify');
69    $instance->expirythreshold = $plugin->get_config('expirythreshold');
70}
71
72$mform = new enrol_manual_edit_form(null, array($instance, $plugin, $context));
73
74if ($mform->is_cancelled()) {
75    redirect($return);
76
77} else if ($data = $mform->get_data()) {
78    if ($data->expirynotify == 2) {
79        $data->expirynotify = 1;
80        $data->notifyall = 1;
81    } else {
82        $data->notifyall = 0;
83    }
84    if (!$data->expirynotify) {
85        // Keep previous/default value of disabled expirythreshold option.
86        $data->expirythreshold = $instance->expirythreshold;
87    }
88    if ($instance->id) {
89        $instance->roleid          = $data->roleid;
90        $instance->enrolperiod     = $data->enrolperiod;
91        $instance->expirynotify    = $data->expirynotify;
92        $instance->notifyall       = $data->notifyall;
93        $instance->expirythreshold = $data->expirythreshold;
94        $instance->timemodified    = time();
95        $markdirty = ($instance->status != $data->status);
96        $instance->status = $data->status;
97
98        $DB->update_record('enrol', $instance);
99        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
100
101        if ($markdirty) {
102            $context->mark_dirty();
103        }
104
105    } else {
106        $fields = array(
107            'status'          => $data->status,
108            'roleid'          => $data->roleid,
109            'enrolperiod'     => $data->enrolperiod,
110            'expirynotify'    => $data->expirynotify,
111            'notifyall'       => $data->notifyall,
112            'expirythreshold' => $data->expirythreshold);
113        $plugin->add_instance($course, $fields);
114    }
115
116    redirect($return);
117}
118
119$PAGE->set_title(get_string('pluginname', 'enrol_manual'));
120$PAGE->set_heading($course->fullname);
121
122echo $OUTPUT->header();
123echo $OUTPUT->heading(get_string('pluginname', 'enrol_manual'));
124$mform->display();
125echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.