Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 23... Línea 23...
23
 * @package    enrol_guest
23
 * @package    enrol_guest
24
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
24
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
26
 */
Línea -... Línea 27...
-
 
27
 
27
 
28
use core\output\single_button;
Línea 28... Línea 29...
28
defined('MOODLE_INTERNAL') || die();
29
use core_enrol\output\enrol_page;
29
 
30
 
30
/**
31
/**
31
 * Class enrol_guest_plugin
32
 * Class enrol_guest_plugin
Línea 146... Línea 147...
146
 
147
 
147
        return true;
148
        return true;
Línea 148... Línea 149...
148
    }
149
    }
149
 
-
 
150
    /**
150
 
151
     * Creates course enrol form, checks if form submitted
151
    /**
152
     * and enrols user if necessary. It can also redirect.
152
     * Enrol a user using the guest enrolment method
153
     *
153
     *
-
 
154
     * @param stdClass $instance
154
     * @param stdClass $instance
155
     * @param string $guestpassword
-
 
156
     * @return void
-
 
157
     */
-
 
158
    public function mark_user_as_enrolled(stdClass $instance, string $guestpassword): void {
-
 
159
        global $USER, $CFG;
-
 
160
 
-
 
161
        // Add guest role.
-
 
162
        $context = \core\context\course::instance($instance->courseid);
-
 
163
        $USER->enrol_guest_passwords[$instance->id] = $guestpassword;
-
 
164
        if (isset($USER->enrol['tempguest'][$instance->courseid])) {
-
 
165
            remove_temp_course_roles($context);
-
 
166
        }
-
 
167
        load_temp_course_role($context, $CFG->guestroleid);
-
 
168
        $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP;
-
 
169
    }
155
     * @return string html text, usually a form in a text box
170
 
156
     */
171
    #[\Override]
Línea 157... Línea 172...
157
    public function enrol_page_hook(stdClass $instance) {
172
    public function enrol_page_hook(stdClass $instance) {
158
        global $CFG, $OUTPUT, $SESSION, $USER;
173
        global $CFG, $OUTPUT, $SESSION, $USER, $PAGE;
159
 
174
 
Línea 160... Línea 175...
160
        if ($instance->password === '') {
175
        if ($instance->password === '') {
161
            return null;
176
            return null;
162
        }
177
        }
163
 
178
 
Línea 164... Línea -...
164
        if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) {
-
 
165
            // no need to show the guest access when user can already enter course as guest
179
        if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) {
166
            return null;
180
            // no need to show the guest access when user can already enter course as guest
167
        }
-
 
168
 
181
            return null;
169
        require_once("$CFG->dirroot/enrol/guest/locallib.php");
182
        }
170
        $form = new enrol_guest_enrol_form(NULL, $instance);
183
 
171
        $instanceid = optional_param('instance', 0, PARAM_INT);
184
        $title = $this->get_instance_name($instance);
172
 
-
 
173
        if ($instance->id == $instanceid) {
185
        $notification = new \core\output\notification(get_string('passwordrequired', 'enrol_guest'), 'info', false);
174
            if ($data = $form->get_data()) {
186
        $notification->set_extra_classes(['mb-0']);
175
                // add guest role
187
        $button = new single_button(
176
                $context = context_course::instance($instance->courseid);
-
 
177
                $USER->enrol_guest_passwords[$instance->id] = $data->guestpassword; // this is a hack, ideally we should not add stuff to $USER...
-
 
178
                if (isset($USER->enrol['tempguest'][$instance->courseid])) {
-
 
179
                    remove_temp_course_roles($context);
188
            $PAGE->url,
180
                }
189
            get_string('loginguest', 'moodle'),
181
                load_temp_course_role($context, $CFG->guestroleid);
190
            'get',
182
                $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP;
191
            single_button::BUTTON_PRIMARY,
183
 
192
            [
184
                // go to the originally requested page
193
                'data-id' => $instance->courseid,
185
                if (!empty($SESSION->wantsurl)) {
194
                'data-instance' => $instance->id,
186
                    $destination = $SESSION->wantsurl;
195
                'data-form' => enrol_guest\form\enrol_form::class,
187
                    unset($SESSION->wantsurl);
-
 
188
                } else {
-
 
189
                    $destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid";
-
 
190
                }
196
                'data-title' => $title,
191
                redirect($destination);
197
            ]);
192
            }
198
        $PAGE->requires->js_call_amd('enrol_guest/enrol_page', 'initEnrol', [$instance->id]);
193
        }
-
 
194
 
199
        $enrolpage = new enrol_page(
195
        ob_start();
200
            instance: $instance,
Línea 196... Línea 201...
196
        $form->display();
201
            header: $title,
197
        $output = ob_get_clean();
202
            body: $OUTPUT->render($notification),
198
 
203
            buttons: [$button]);
Línea 259... Línea 264...
259
     * Add new instance of enrol plugin.
264
     * Add new instance of enrol plugin.
260
     * @param object $course
265
     * @param object $course
261
     * @param array instance fields
266
     * @param array instance fields
262
     * @return int id of new instance, null if can not be created
267
     * @return int id of new instance, null if can not be created
263
     */
268
     */
264
    public function add_instance($course, array $fields = NULL) {
269
    public function add_instance($course, ?array $fields = NULL) {
265
        $fields = (array)$fields;
270
        $fields = (array)$fields;
Línea 266... Línea 271...
266
 
271
 
267
        if (!isset($fields['password'])) {
272
        if (!isset($fields['password'])) {
268
            $fields['password'] = '';
273
            $fields['password'] = '';
Línea 571... Línea 576...
571
/**
576
/**
572
 * Get icon mapping for font-awesome.
577
 * Get icon mapping for font-awesome.
573
 */
578
 */
574
function enrol_guest_get_fontawesome_icon_map() {
579
function enrol_guest_get_fontawesome_icon_map() {
575
    return [
580
    return [
576
        'enrol_guest:withpassword' => 'fa-key',
581
        'enrol_guest:withoutpassword' => 'fa-lock-open',
577
        'enrol_guest:withoutpassword' => 'fa-unlock-alt',
582
        'enrol_guest:withpassword' => 'fa-lock',
578
    ];
583
    ];
579
}
584
}