Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 20... Línea 20...
20
 * @package    enrol_self
20
 * @package    enrol_self
21
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
21
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
23
 */
Línea -... Línea 24...
-
 
24
 
-
 
25
use core\output\single_button;
-
 
26
use core_enrol\output\enrol_page;
24
 
27
 
25
/**
28
/**
26
 * Self enrolment plugin implementation.
29
 * Self enrolment plugin implementation.
27
 * @author Petr Skoda
30
 * @author Petr Skoda
28
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Línea 132... Línea 135...
132
 
135
 
133
        return true;
136
        return true;
Línea 134... Línea 137...
134
    }
137
    }
-
 
138
 
-
 
139
    /**
-
 
140
     * Returns edit icons for the page with list of instances.
-
 
141
     *
-
 
142
     * @param stdClass $instance
-
 
143
     * @return array
-
 
144
     */
-
 
145
    public function get_action_icons(stdClass $instance): array {
-
 
146
        global $OUTPUT;
-
 
147
 
-
 
148
        $context = context_course::instance($instance->courseid);
-
 
149
 
-
 
150
        $icons = [];
-
 
151
        if (has_any_capability(['enrol/self:config', 'moodle/course:editcoursewelcomemessage'], $context)) {
-
 
152
            $linkparams = [
-
 
153
                'courseid' => $instance->courseid,
-
 
154
                'id' => $instance->id,
-
 
155
                'type' => $instance->enrol,
-
 
156
            ];
-
 
157
            $editlink = new moodle_url('/enrol/editinstance.php', $linkparams);
-
 
158
            $icon = new pix_icon('t/edit', get_string('edit'), 'core', ['class' => 'iconsmall']);
-
 
159
            $icons[] = $OUTPUT->action_icon($editlink, $icon);
-
 
160
        }
-
 
161
 
-
 
162
        return $icons;
-
 
163
    }
135
 
164
 
136
    /**
165
    /**
137
     * Self enrol user to course
166
     * Self enrol user to course
138
     *
167
     *
139
     * @param stdClass $instance enrolment instance
-
 
140
     * @param stdClass $data data needed for enrolment.
168
     * @param stdClass $instance enrolment instance
141
     * @return bool|array true if enroled else eddor code and messege
169
     * @param stdClass $data data needed for enrolment.
142
     */
170
     */
Línea 143... Línea 171...
143
    public function enrol_self(stdClass $instance, $data = null) {
171
    public function enrol_self(stdClass $instance, $data = null) {
Línea 174... Línea 202...
174
                }
202
                }
175
            }
203
            }
176
        }
204
        }
177
    }
205
    }
Línea 178... Línea -...
178
 
-
 
179
    /**
-
 
180
     * Creates course enrol form, checks if form submitted
-
 
181
     * and enrols user if necessary. It can also redirect.
-
 
182
     *
-
 
183
     * @param stdClass $instance
-
 
184
     * @return string html text, usually a form in a text box
206
 
185
     */
207
    #[\Override]
186
    public function enrol_page_hook(stdClass $instance) {
208
    public function enrol_page_hook(stdClass $instance) {
Línea -... Línea 209...
-
 
209
        global $CFG, $OUTPUT, $USER, $PAGE;
-
 
210
 
-
 
211
        $buttonurl = null;
-
 
212
        $buttontext = '';
187
        global $CFG, $OUTPUT, $USER;
213
        $buttonattrs = [];
Línea 188... Línea 214...
188
 
214
        $body = '';
189
        require_once("$CFG->dirroot/enrol/self/locallib.php");
-
 
190
 
215
        $title = $this->get_instance_name($instance);
191
        $enrolstatus = $this->can_self_enrol($instance);
216
 
-
 
217
        $enrolstatus = $this->can_self_enrol($instance);
192
 
218
        if ($enrolstatus === true) {
-
 
219
            if ($instance->password) {
-
 
220
                // Self-enrolment with password. Display a button to open a form in a modal.
193
        if (true === $enrolstatus) {
221
                $body = get_string('enrolkeyrequired', 'enrol_self');
194
            // This user can self enrol using this instance.
222
                $buttonurl = $PAGE->url;
-
 
223
                $buttonattrs = [
195
            $form = new enrol_self_enrol_form(null, $instance);
224
                    'data-id' => $instance->courseid,
-
 
225
                    'data-instance' => $instance->id,
-
 
226
                    'data-form' => enrol_self\form\enrol_form::class,
-
 
227
                    'data-title' => $title,
-
 
228
                ];
-
 
229
                $PAGE->requires->js_call_amd('enrol_self/enrol_page', 'initEnrol', [$instance->id]);
196
            $instanceid = optional_param('instance', 0, PARAM_INT);
230
            } else {
-
 
231
                // Self-enrolment without password. Display a button to self enrol. If button is pressed - enrol the user.
197
            if ($instance->id == $instanceid) {
232
                if (optional_param('action', null, PARAM_TEXT) === 'enrol' && confirm_sesskey()) {
-
 
233
                    $this->enrol_self($instance, (object)[]);
-
 
234
                    return '';
198
                if ($data = $form->get_data()) {
235
                }
-
 
236
                $body = get_string('nopassword', 'enrol_self');
-
 
237
                $buttonurl = new moodle_url($PAGE->url, ['action' => 'enrol', 'sesskey' => sesskey()]);
-
 
238
            }
-
 
239
            $buttontext = get_string('enrolme', 'enrol_self');
-
 
240
        } else if (isguestuser()) {
-
 
241
            // User is not logged in. Display a button to login.
-
 
242
            $buttonurl = new moodle_url(get_login_url());
-
 
243
            $body = get_string('noguestaccess', 'enrol');
-
 
244
            $buttontext = get_string('continue');
199
                    $this->enrol_self($instance, $data);
245
        } else if (!$enrolstatus) {
200
                }
-
 
201
            }
-
 
202
        } else {
246
            // No reason why user can not use this method, do not display anything.
203
            // This user can not self enrol using this instance. Using an empty form to keep
-
 
204
            // the UI consistent with other enrolment plugins that returns a form.
247
            return '';
205
            $data = new stdClass();
248
        } else {
206
            $data->header = $this->get_instance_name($instance);
249
            $body = $enrolstatus;
207
            $data->info = $enrolstatus;
250
        }
208
 
251
 
209
            // The can_self_enrol call returns a button to the login page if the user is a
252
        $notification = new \core\output\notification($body, 'info', false);
210
            // guest, setting the login url to the form if that is the case.
253
        $notification->set_extra_classes(['mb-0']);
211
            $url = isguestuser() ? get_login_url() : null;
-
 
212
            $form = new enrol_self_empty_form($url, $data);
254
        $enrolpage = new enrol_page(
213
        }
255
            instance: $instance,
-
 
256
            header: $title,
214
 
257
            body: $OUTPUT->render($notification),
215
        ob_start();
258
            buttons: $buttonurl ?
216
        $form->display();
259
                [new single_button($buttonurl, $buttontext, 'get', single_button::BUTTON_PRIMARY, $buttonattrs)] :
Línea 217... Línea 260...
217
        $output = ob_get_clean();
260
                []);
218
        return $OUTPUT->box($output);
261
        return $OUTPUT->render($enrolpage);
219
    }
262
    }
Línea 257... Línea 300...
257
    /**
300
    /**
258
     * Does this plugin support some way to self enrol?
301
     * Does this plugin support some way to self enrol?
259
     * This function doesn't check user capabilities. Use can_self_enrol to check capabilities.
302
     * This function doesn't check user capabilities. Use can_self_enrol to check capabilities.
260
     *
303
     *
261
     * @param stdClass $instance enrolment instance
304
     * @param stdClass $instance enrolment instance
262
     * @return bool - true means "Enrol me in this course" link could be available
305
     * @return bool|string - true means "Enrol me in this course" link could be available
263
     */
306
     */
264
    public function is_self_enrol_available(stdClass $instance) {
307
    public function is_self_enrol_available(stdClass $instance) {
265
        global $CFG, $DB, $USER;
308
        global $CFG, $DB, $USER;
Línea 266... Línea 309...
266
 
309
 
Línea 392... Línea 435...
392
     * @see \enrol_plugin::send_course_welcome_message_to_user()
435
     * @see \enrol_plugin::send_course_welcome_message_to_user()
393
     * @todo MDL-81185 Final deprecation in Moodle 4.8.
436
     * @todo MDL-81185 Final deprecation in Moodle 4.8.
394
     */
437
     */
395
    #[\core\attribute\deprecated('enrol_plugin::send_course_welcome_message_to_user', since: '4.4', mdl: 'MDL-4188')]
438
    #[\core\attribute\deprecated('enrol_plugin::send_course_welcome_message_to_user', since: '4.4', mdl: 'MDL-4188')]
396
    protected function email_welcome_message($instance, $user) {
439
    protected function email_welcome_message($instance, $user) {
397
        \core\deprecation::emit_deprecation_if_present(__FUNCTION__);
440
        \core\deprecation::emit_deprecation(__FUNCTION__);
398
        $this->send_course_welcome_message_to_user(
441
        $this->send_course_welcome_message_to_user(
399
            instance: $instance,
442
            instance: $instance,
400
            userid: $user->id,
443
            userid: $user->id,
401
            sendoption: $instance->customint4,
444
            sendoption: $instance->customint4,
402
            message: $instance->customtext1,
445
            message: $instance->customtext1,
Línea 826... Línea 869...
826
     * @return bool
869
     * @return bool
827
     */
870
     */
828
    public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
871
    public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
829
        global $CFG, $DB;
872
        global $CFG, $DB;
Línea -... Línea 974...
-
 
974
        }
-
 
975
 
-
 
976
        // Course welcome message.
-
 
977
        if (has_any_capability(['enrol/self:config', 'moodle/course:editcoursewelcomemessage'], $context)) {
-
 
978
            $mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'),
-
 
979
                    enrol_send_welcome_email_options());
-
 
980
            $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
-
 
981
 
-
 
982
            $options = [
-
 
983
                'cols' => '60',
-
 
984
                'rows' => '8',
-
 
985
            ];
-
 
986
            $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options);
-
 
987
            $mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol'));
-
 
988
            $mform->hideIf(
-
 
989
                elementname: 'customtext1',
-
 
990
                dependenton: 'customint4',
-
 
991
                condition: 'eq',
-
 
992
                value: ENROL_DO_NOT_SEND_EMAIL,
942
        );
993
            );
943
 
994
 
944
        // Static form elements cannot be hidden by hideIf() so we need to add a dummy group.
995
            // Static form elements cannot be hidden by hideIf() so we need to add a dummy group.
945
        // See: https://tracker.moodle.org/browse/MDL-66251.
996
            // See: https://tracker.moodle.org/browse/MDL-66251.
946
        $group[] = $mform->createElement(
997
            $group[] = $mform->createElement(
947
            'static',
998
                'static',
948
            'customwelcomemessage_extra_help',
999
                'customwelcomemessage_extra_help',
949
            null,
1000
                null,
950
            get_string(
1001
                get_string(
951
                identifier: 'customwelcomemessage_help',
1002
                    identifier: 'customwelcomemessage_help',
952
                component: 'core_enrol',
1003
                    component: 'core_enrol',
953
            ),
1004
                ),
954
        );
1005
            );
955
        $mform->addGroup($group, 'group_customwelcomemessage_extra_help', '', ' ', false);
1006
            $mform->addGroup($group, 'group_customwelcomemessage_extra_help', '', ' ', false);
956
        $mform->hideIf(
1007
            $mform->hideIf(
957
            elementname: 'group_customwelcomemessage_extra_help',
1008
                elementname: 'group_customwelcomemessage_extra_help',
958
            dependenton: 'customint4',
1009
                dependenton: 'customint4',
-
 
1010
                condition: 'eq',
959
            condition: 'eq',
1011
                value: ENROL_DO_NOT_SEND_EMAIL,
Línea -... Línea 1012...
-
 
1012
            );
960
            value: ENROL_DO_NOT_SEND_EMAIL,
1013
        }
961
        );
1014
 
962
 
1015
        // Enrolment changes warning.
963
        if (enrol_accessing_via_instance($instance)) {
1016
        if (has_capability('enrol/self:config', $context) && enrol_accessing_via_instance($instance)) {
964
            $warntext = get_string('instanceeditselfwarningtext', 'core_enrol');
1017
            $warntext = get_string('instanceeditselfwarningtext', 'core_enrol');
Línea 1084... Línea 1137...
1084
     * Add new instance of enrol plugin.
1137
     * Add new instance of enrol plugin.
1085
     * @param object $course
1138
     * @param object $course
1086
     * @param array $fields instance fields
1139
     * @param array $fields instance fields
1087
     * @return int id of new instance, null if can not be created
1140
     * @return int id of new instance, null if can not be created
1088
     */
1141
     */
1089
    public function add_instance($course, array $fields = null) {
1142
    public function add_instance($course, ?array $fields = null) {
1090
        // In the form we are representing 2 db columns with one field.
1143
        // In the form we are representing 2 db columns with one field.
1091
        if (!empty($fields) && !empty($fields['expirynotify'])) {
1144
        if (!empty($fields) && !empty($fields['expirynotify'])) {
1092
            if ($fields['expirynotify'] == 2) {
1145
            if ($fields['expirynotify'] == 2) {
1093
                $fields['expirynotify'] = 1;
1146
                $fields['expirynotify'] = 1;
1094
                $fields['notifyall'] = 1;
1147
                $fields['notifyall'] = 1;
Línea 1144... Línea 1197...
1144
        }
1197
        }
1145
        return $roles;
1198
        return $roles;
1146
    }
1199
    }
Línea 1147... Línea 1200...
1147
 
1200
 
1148
    /**
-
 
1149
     * Get the "from" contact which the email will be sent from.
-
 
1150
     *
-
 
1151
     * @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_*
-
 
1152
     * @param $context context where the user will be fetched
-
 
1153
     * @return mixed|stdClass the contact user object.
1201
    /**
1154
     * @deprecated since Moodle 4.4
-
 
1155
     * @see \enrol_plugin::get_welcome_message_contact()
-
 
1156
     * @todo MDL-81185 Final deprecation in Moodle 4.8.
1202
     * @deprecated since Moodle 4.4
1157
     */
1203
     */
1158
    #[\core\attribute\deprecated('enrol_plugin::get_welcome_message_contact', since: '4.4', mdl: 'MDL-4188')]
1204
    #[\core\attribute\deprecated('enrol_plugin::get_welcome_message_contact', since: '4.4', mdl: 'MDL-4188', final: true)]
1159
    public function get_welcome_email_contact($sendoption, $context) {
1205
    public function get_welcome_email_contact() {
1160
        \core\deprecation::emit_deprecation_if_present(__FUNCTION__);
-
 
1161
        return $this->get_welcome_message_contact(
-
 
1162
            sendoption: $sendoption,
-
 
1163
            context: $context,
-
 
1164
        );
1206
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
Línea 1165... Línea 1207...
1165
    }
1207
    }
1166
 
1208
 
1167
    /**
1209
    /**
Línea 1263... Línea 1305...
1263
 * Get icon mapping for font-awesome.
1305
 * Get icon mapping for font-awesome.
1264
 */
1306
 */
1265
function enrol_self_get_fontawesome_icon_map() {
1307
function enrol_self_get_fontawesome_icon_map() {
1266
    return [
1308
    return [
1267
        'enrol_self:withkey' => 'fa-key',
1309
        'enrol_self:withkey' => 'fa-key',
1268
        'enrol_self:withoutkey' => 'fa-sign-in',
1310
        'enrol_self:withoutkey' => 'fa-solid fa-right-to-bracket',
1269
    ];
1311
    ];
1270
}
1312
}