Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (3 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/enrol/self/locallib.php

    r136 r1331  
    2727require_once("$CFG->libdir/formslib.php");
    2828
     29/**
     30 * Check if the given password match a group enrolment key in the specified course.
     31 *
     32 * @param  int $courseid            course id
     33 * @param  string $enrolpassword    enrolment password
     34 * @return bool                     True if match
     35 * @since  Moodle 3.0
     36 */
     37function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
     38    global $DB;
     39
     40    $found = false;
     41    $groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
     42
     43    foreach ($groups as $group) {
     44        if (empty($group->enrolmentkey)) {
     45            continue;
     46        }
     47        if ($group->enrolmentkey === $enrolpassword) {
     48            $found = true;
     49            break;
     50        }
     51    }
     52    return $found;
     53}
     54
    2955class enrol_self_enrol_form extends moodleform {
    3056    protected $instance;
     
    4268
    4369    public function definition() {
     70        global $USER, $OUTPUT, $CFG;
    4471        $mform = $this->_form;
    4572        $instance = $this->_customdata;
     
    5481            $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'),
    5582                    array('id' => 'enrolpassword_'.$instance->id));
     83            $context = context_course::instance($this->instance->courseid);
     84            $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', user_picture::fields('u'));
     85            $keyholdercount = 0;
     86            foreach ($keyholders as $keyholder) {
     87                $keyholdercount++;
     88                if ($keyholdercount === 1) {
     89                    $mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
     90                }
     91                $keyholdercontext = context_user::instance($keyholder->id);
     92                if ($USER->id == $keyholder->id || has_capability('moodle/user:viewdetails', context_system::instance()) ||
     93                        has_coursecontact_role($keyholder->id)) {
     94                    $profilelink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $keyholder->id . '&amp;course=' .
     95                    $this->instance->courseid . '">' . fullname($keyholder) . '</a>';
     96                } else {
     97                    $profilelink = fullname($keyholder);
     98                }
     99                $profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
     100                $mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
     101            }
     102
    56103        } else {
    57104            $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
     
    83130            if ($data['enrolpassword'] !== $instance->password) {
    84131                if ($instance->customint1) {
    85                     $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id ASC', 'id, enrolmentkey');
    86                     $found = false;
    87                     foreach ($groups as $group) {
    88                         if (empty($group->enrolmentkey)) {
    89                             continue;
    90                         }
    91                         if ($group->enrolmentkey === $data['enrolpassword']) {
    92                             $found = true;
    93                             break;
    94                         }
    95                     }
    96                     if (!$found) {
     132                    // Check group enrolment key.
     133                    if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
    97134                        // We can not hint because there are probably multiple passwords.
    98135                        $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
Note: See TracChangeset for help on using the changeset viewer.