diff --git a/classes/form/enrolkey_signup_form.php b/classes/form/enrolkey_signup_form.php old mode 100644 new mode 100755 index 0c71c54..0f9ca7e --- a/classes/form/enrolkey_signup_form.php +++ b/classes/form/enrolkey_signup_form.php @@ -32,20 +32,11 @@ */ namespace auth_enrolkey\form; -use auth_enrolkey\utility; -use moodle_url; - defined('MOODLE_INTERNAL') || die; require_once($CFG->dirroot . '/login/signup_form.php'); -/** - * Class for the enrolkey signup form. - * - * @package auth_enrolkey - * @copyright 2021 Nicholas Hoobin - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ + class enrolkey_signup_form extends \login_signup_form { /** @@ -53,26 +44,27 @@ class enrolkey_signup_form extends \login_signup_form { */ public function definition() { global $CFG; + global $PAGE; + $PAGE->requires->js('/auth/enrolkey/js/enrolkey_signup_form.js'); // Generates the default signup form. parent::definition(); $mform = $this->_form; - $element = $mform->createElement('text', 'signup_token', get_string('signup_field_title', 'auth_enrolkey')); + //Field "course enrolment token": + if(get_config('auth_enrolkey', 'tokenrequired')) { + $element = $mform->createElement('text', 'signup_token', get_string('signup_field_title', 'auth_enrolkey')); - // View https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#setType for more types. - $mform->setType('signup_token', PARAM_TEXT); - $token = optional_param('signup_token', '', PARAM_TEXT); - if (!empty($token)) { - $mform->setDefault('signup_token', $token); - } + // View https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#setType for more types. + $mform->setType('signup_token', PARAM_TEXT); - // Make the course token field visible earlier. - $mform->insertElementBefore($element, 'email'); + // Make the course token field visible earlier. + $mform->insertElementBefore($element, 'email'); - if ($this->signup_token_required()) { - $mform->addRule('signup_token', get_string('signup_missing', 'auth_enrolkey'), 'required', null, 'client'); + if ($this->signup_token_required()) { + $mform->addRule('signup_token', get_string('signup_missing', 'auth_enrolkey'), 'required', null, 'server'); + } } if ($this->signup_captcha_enabled()) { @@ -80,6 +72,45 @@ public function definition() { $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth'); $mform->closeHeaderBefore('recaptcha_element'); } + // If needed, remove additional elements "first name", "last name", "city" and "country" + // we hide it, not physically remove it, because we still fill it automatically (with javascript) + // with dummy data: "firstname" for field 'firstname', and "lastname" + // for field 'lastname' (this is done to avoid breaking moodle since firstname + // and lastname are normally required. + if(!get_config('auth_enrolkey', 'enabledfirstname')) { + $mform->hideif('firstname', ''); + $mform->setDefault('firstname', "firstname"); + } + if(!get_config('auth_enrolkey', 'enabledlastname')) { + $mform->hideif('lastname', ''); + $mform->setDefault('lastname', "lastname"); + } + if(!get_config('auth_enrolkey', 'enabledcity')) { + $mform->removeElement('city'); + } + if(!get_config('auth_enrolkey', 'enabledcountry')) { + $mform->removeElement('country'); + } + + // Set the "required" rules. Note that "firstname" and "lastname" + // are already set to "required" in the original signup_form.php file. + if(!get_config('auth_enrolkey', 'enablefirstname') || + !get_config('auth_enrolkey', 'requiredfirstname')){ + unset($mform->_rules['firstname'], $mform->_errors['firstname'], + $mform->_required['4']); + } + if(!get_config('auth_enrolkey', 'enablelastname') || + !get_config('auth_enrolkey', 'requiredlastname')){ + unset($mform->_rules['lastname'], $mform->_errors['lastname'], + $mform->_required['5']); + } + if(get_config('auth_enrolkey', 'requiredcity')) { + $mform->addRule('city', get_string('missingcity', 'auth_enrolkey'), 'required', null, 'client'); + } + if(get_config('auth_enrolkey', 'requiredcountry')) { + $mform->addRule('country', get_string('missingcountry', 'auth_enrolkey'), 'required', null, 'client'); + } + } /** @@ -92,64 +123,28 @@ public function definition() { * or an empty array if everything is OK (true allowed for backwards compatibility too). */ public function validation($data, $files) { - global $CFG; - + global $DB; $errors = parent::validation($data, $files); - $signuptoken = $data['signup_token']; + $enrolplugin = enrol_get_plugin('self'); - if ($signuptoken !== '') { - // For any case where the token is populated, perform a lookup. - $tokenisvalid = $this->check_database_for_signuptoken($signuptoken); + $token = $data['signup_token']; - if ($tokenisvalid === false) { - $errors['signup_token'] = get_string('signup_token_invalid', 'auth_enrolkey'); - } - - } else { - // The form submission is an empty string, double check if the token is required. - if ($this->signup_token_required()) { - $errors['signup_token'] = get_string('signup_missing', 'auth_enrolkey'); - } - } - - if (get_config('auth_enrolkey', 'unsuspendaccounts') && utility::search_for_suspended_user($data)) { - - $stringdata = (object) ['href' => (new moodle_url('/auth/enrolkey/unsuspend.php'))->out()]; - if (empty($CFG->createuserwithemail)) { - $errors['username'] = $errors['username'] . get_string('suspendeduseratsignup', 'auth_enrolkey', $stringdata); - } else { - $errors['email'] = $errors['email' ] . get_string('suspendeduseratsignup', 'auth_enrolkey', $stringdata); - } - } - - return $errors; - } - - /** - * Checks the enrolment records for any matching self enrolment key. - * - * @param string $token Returns true on success. False on failure. - * @return bool - */ - private function check_database_for_signuptoken($token) { - global $DB; - - $selfenrolinstance = false; + if (!empty($token)) { + $selfenrolinstance = false; - $instances = $DB->get_records('enrol', array('password' => $token, 'enrol' => 'self')); + $instances = $DB->get_records('enrol', array('password' => $token, 'enrol' => 'self')); - // There may be more than one enrolment instance configured with various dates to check against. - foreach ($instances as $instance) { - // There may be things that prevent self enrol, such as requiring a capability, or full course. - // This should not be a blocker to account creation. The creation should pass, then report the error. - if ($instance->status == ENROL_INSTANCE_ENABLED) { - $selfenrolinstance = true; + // There may be more than one enrolment instance configured with various dates to check against. + foreach ($instances as $instance) { + // DO we ever want to pass feedback to user? + if ($enrolplugin->can_self_enrol($instance) === true) { + $selfenrolinstance = true; + } } - } - // Lookup group enrol keys. - $instances = $DB->get_records_sql(" + // Lookup group enrol keys. + $instances = $DB->get_records_sql(" SELECT e.* FROM {groups} g JOIN {enrol} e ON e.courseid = g.courseid @@ -157,13 +152,20 @@ private function check_database_for_signuptoken($token) { AND e.customint1 = 1 WHERE g.enrolmentkey = ? ", array($token)); - foreach ($instances as $instance) { - if ($instance->status == ENROL_INSTANCE_ENABLED) { - $selfenrolinstance = true; + foreach ($instances as $instance) { + // DO we ever want to pass feedback to user? + if ($enrolplugin->can_self_enrol($instance) === true) { + $selfenrolinstance = true; + } + } + + // No token matched, this will produce an error message. There are concerns about bruteforcing. + if (!$selfenrolinstance) { + $errors['signup_token'] = get_string('signup_token_invalid', 'auth_enrolkey'); } } - return $selfenrolinstance; + return $errors; } /** diff --git a/js/enrolkey_signup_form.js b/js/enrolkey_signup_form.js new file mode 100644 index 0000000..23f769b --- /dev/null +++ b/js/enrolkey_signup_form.js @@ -0,0 +1,23 @@ +/* we fill fields 'firstname' and 'lastname' automatically with dummy data + if the User hasn't completed them, just when he clicks on "submit": + "firstname" for field 'firstname', and "lastname" for field 'lastname' + (this is done to avoid breaking moodle since firstname and lastname + are normally required.) */ + +var mform = document.getElementsByClassName('mform')[0]; + +function default_names(){ + var firstname = document.getElementById('id_firstname'); + var lastname = document.getElementById('id_lastname'); + // set the default values if blank + if (firstname.value == '' || firstname.value == null){ + firstname.value = 'firstname'; + } + if (lastname.value == '' || lastname.value == null){ + lastname.value = 'lastname'; + } +}; + +if(mform.addEventListener){ + mform.addEventListener("submit", default_names, false); +} diff --git a/js/settings.js b/js/settings.js new file mode 100644 index 0000000..21e218c --- /dev/null +++ b/js/settings.js @@ -0,0 +1,90 @@ +/* Automatically set to "checked" the checkbox for the item above the + "required?" checkbox. This way, if the Admin checks "required country?", + the item "country" is automatically "checked" above, + i.e, it MUST be displayed to the user. */ + + +/* Binding of the event "checked" on "required?" item. */ + +/* item "firstname" */ +document.getElementById('id_s_auth_enrolkey_requiredfirstname') + .addEventListener('change', function() +{ + if (this.checked) + { + document.getElementById('id_s_auth_enrolkey_enabledfirstname').checked = true; + } +}, false); + +/* item "lastname" */ +document.getElementById('id_s_auth_enrolkey_requiredlastname') + .addEventListener('change', function() +{ + if (this.checked) + { + document.getElementById('id_s_auth_enrolkey_enabledlastname').checked = true; + } +}, false); + +/* item "city" */ +document.getElementById('id_s_auth_enrolkey_requiredcity') + .addEventListener('change', function() +{ + if (this.checked) + { + document.getElementById('id_s_auth_enrolkey_enabledcity').checked = true; + } +}, false); + +/* item "country" */ +document.getElementById('id_s_auth_enrolkey_requiredcountry') + .addEventListener('change', function() +{ + if (this.checked) + { + document.getElementById('id_s_auth_enrolkey_enabledcountry').checked = true; + } +}, false); + +/* and conversely, if an item is NOT displayed, it CANNOT be required to the + user. */ + +/* item "firstname" */ +document.getElementById('id_s_auth_enrolkey_enabledfirstname') + .addEventListener('change', function() +{ + if (!this.checked) + { + document.getElementById('id_s_auth_enrolkey_requiredfirstname').checked = false; + } +}, false); + +/* item "lastname" */ +document.getElementById('id_s_auth_enrolkey_enabledlastname') + .addEventListener('change', function() +{ + if (!this.checked) + { + document.getElementById('id_s_auth_enrolkey_requiredlastname').checked = false; + } +}, false); + +/* item "city" */ +document.getElementById('id_s_auth_enrolkey_enabledcity') + .addEventListener('change', function() +{ + if (!this.checked) + { + document.getElementById('id_s_auth_enrolkey_requiredcity').checked = false; + } +}, false); + +/* item "country" */ +document.getElementById('id_s_auth_enrolkey_enabledcountry') + .addEventListener('change', function() +{ + if (!this.checked) + { + document.getElementById('id_s_auth_enrolkey_requiredcountry').checked = false; + } +}, false); \ No newline at end of file diff --git a/lang/en/auth_enrolkey.php b/lang/en/auth_enrolkey.php old mode 100644 new mode 100755 index e19d29d..929da04 --- a/lang/en/auth_enrolkey.php +++ b/lang/en/auth_enrolkey.php @@ -22,85 +22,45 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['cohortsync'] = 'Sync audiences on signup'; -$string['cohortsync_description'] = 'Sync system audiences when a user signs up. This removes the delay from cron running and doing this task. Warning: this may cause the signup process to be slower.'; $string['description'] = 'This provides Enrolment key based self-registration'; -$string['edit_profile'] = 'Edit fields'; -$string['edit_redirect'] = 'Edit URL'; -$string['enrolkeyuse'] = 'Use new enrolment key'; -$string['errorenrolling'] = 'There was an error enrolling in course \'{$a->course}\'. The error message is: {$a->err}'; -$string['heading_unsuspend'] = 'Your account may be suspended, please enter an enrolment key'; -$string['label_cohortselect'] = 'Select cohorts'; -$string['label_cohortselect_help'] = 'Search cohort names and IDs in this field.'; -$string['label_cohortselect_empty'] = 'No cohorts selected'; -$string['label_redirection'] = 'Redirection URL'; -$string['label_redirection_help'] = 'The URL entered here will redirect the user at the end of their self sign-up.
-This field will accept absolute and relative urls.
-Please remember to include the initial slash / when using a relative URL. - - -'; -$string['settings_heading'] = 'General settings'; -$string['settings_content'] = '

Enrolment key based self-registration enables a user to create their own account via a \'Create new account\' button on the login page. The user then receives an email containing a secure link to a page where they can confirm their account. Future logins just check the username and password against the stored values in the Moodle database.

During self-registration if an enrolment key is entered in the enrolment key field then it will proceed to automatically enrol the new user into any course that it matches. The keys are enabled in (Course administration > Users > Enrolment methods > Add method > Self enrolment).

Note: In addition to enabling the plugin, Enrolment key based self-registration must also be selected from the self registration drop-down menu on the \'Manage authentication\' page.

'; +$string['settings_heading'] = 'Security'; +$string['settings_security_desc'] = 'In addition to enabling the plugin, to allow the self-registration by email, this plugin must also be selected in the page "Site administration > Plugins > Authentication > Manage authentication", section "Common Settings", under "self-registration".'; +$string['settings_tokenkey'] = 'Enrolment Token key'; +$string['settings_tokenkey_desc'] = 'During self-registration if an enrolment key, provided by the teacher, is entered in the enrolment key field then it will proceed to automatically enrol the new user into any course that it matches. The keys are enabled in (Course administration > Users > Enrolment methods > Add method > Self enrolment).'; $string['settings_visible_description'] = 'Adds a new form element to the sign-up page for self-registration users. This will be checked against available enrolment keys and enrol the user to the matching courses'; $string['settings_required_description'] = 'The enrolment key will be a required field for validation'; $string['settings_visible_title'] = 'Enable enrolment key element'; $string['settings_required_title'] = 'Require enrolment key for validation'; $string['settings_email_title'] = 'Require email confirmation'; -$string['settings_email_description'] = 'Force users to confirm their account with an email before accessing enrolled courses. - -'; -$string['settings_partial'] = 'Partial'; +$string['settings_email_description'] = 'Require users to confirm their account with an email before accessing enrolled courses.'; $string['signup_failure'] = 'Opps! Something went wrong, and you may not have been enrolled properly. Go to Home'; $string['signup_field_title'] = 'Enrolment key'; $string['signup_token_invalid'] = 'The enrolment key you have entered is invalid'; $string['signup_missing'] = 'Missing enrolment key'; -$string['menumanage'] = 'Manage enrolkey cohort rules'; -$string['menusettings'] = 'Enrolkey settings'; $string['noemail'] = 'Tried to send you an email but failed!'; $string['signup_view'] = 'Course enrolment'; $string['signup_view_message_basic'] = 'You have been enrolled as a {$a->role} into the course \'{$a->course}\''; $string['signup_view_message_basic_dates'] = 'You have enrolled into {$a->course} as a {$a->role}. href}>Click here to view the course.
Course starts: {$a->startdate}
Course ends: {$a->enddate}'; $string['signup_view_message_basic_dates_startonly'] = 'You have enrolled into {$a->course} as a {$a->role}. href}>Click here to view the course.
Course starts: {$a->startdate}'; $string['signup_view_message_basic_dates_endonly'] = 'You have enrolled into {$a->course} as a {$a->role}. href}>Click here to view the course.
Course ends: {$a->enddate}'; -$string['signup_auth_instructions'] = 'Hi! For full access to courses you\'ll need to take -a minute to create a new account for yourself on this web site. -Each of the individual courses may also have a one-time -"enrolment key", which you can use during this sign up: -
    -
  1. Fill out the New Account form with your details.
  2. -
  3. You will be prompted for an "enrolment key" - use the one -that your teacher has given you. This will "enrol" you in the -course.
  4. -
  5. Your account will be created and you will be logged in.
  6. -
  7. You can now access the full course for this session.
  8. -
  9. An email has also been immediately sent to your email address.
  10. -
  11. Read your email, and click on the web link it contains.
  12. -
  13. From now on you will only need to enter your personal -username and password (in the form on this page) to log in -and access any course you have enrolled in.
  14. -
'; -$string['suspendeduseratsignup'] = '
Perhaps your account exists but was suspended? Please sign up with a new enrolkey here'; -$string['th_cohorts'] = 'Assigned cohorts'; -$string['th_enrolkeyname'] = 'Enrolkey name'; -$string['th_fullname'] = 'Course fullname'; -$string['th_profilefields'] = 'Profile fields'; -$string['th_redirecturl'] = 'Redirection URL'; -$string['title_cohort'] = 'Edit cohort assignment'; -$string['title_profile'] = 'Edit profile fields'; -$string['title_redirect'] = 'Edit redirection URL'; -$string['title_unsuspend'] = 'Suspended account'; -$string['edit_cohort'] = 'Edit assignment'; -$string['unsuspendaccounts'] = 'Un-suspend accounts with a valid enrolkey'; -$string['unsuspendaccounts_description'] = 'On the login, if a user is suspended, and is using the enrolkey authentication type, redirect them to an intermediate page which asks for their username, password and enrolkey to un-suspend them.'; +$string['signup_auth_instructions'] = 'For full access to courses you\'ll need to take a minute to create a new account for yourself on this web site.'; $string['recaptcha'] = 'Adds a visual/audio confirmation form element to the sign-up page for self-registering users. This protects your site against spammers and contributes to a worthwhile cause. See http://www.google.com/recaptcha for more details.'; $string['recaptcha_key'] = 'Enable reCAPTCHA element'; $string['pluginname'] = 'Enrolment key based self-registration'; $string['privacy:metadata'] = 'The auth enrolkey plugin does not store any personal data.'; +$string['optionalfield'] = 'Additional fields for User'; +$string['optionalfield_desc'] = 'Choose the additional fields (in addition to "Username" and "email" which are mandatory) which are shown to new user on the Sign up page. If you want, you can make any of them "required", i.e the User *must* fill something.'; +$string['enablefirstname'] = '1 - "first name"'; +$string['enablefirstnameDesc'] = 'User can define own\'s first name'; +$string['requiredfirstname'] = 'required?'; +$string['enablelastname'] = '2 - "last name"'; +$string['enablelastnameDesc'] = 'User can define own\'s last name'; +$string['requiredlastname'] = 'required?'; +$string['enablecity'] = '3 - "city"'; +$string['enablecityDesc'] = 'User can define own\'s city'; +$string['requiredcity'] = 'required?'; +$string['missingcity'] = 'Missing city/town'; +$string['enablecountry'] = '4 - "country"'; +$string['enablecountryDesc'] = 'User can define own\'s country'; +$string['requiredcountry'] = 'required?'; +$string['missingcountry'] = 'Missing country'; diff --git a/settings.php b/settings.php old mode 100644 new mode 100755 index 829482d..7ce02f9 --- a/settings.php +++ b/settings.php @@ -23,63 +23,51 @@ */ defined('MOODLE_INTERNAL') || die; +$PAGE->requires->js('/auth/enrolkey/js/settings.js'); -if ($hassiteconfig) { +if ($ADMIN->fulltree) { require_once($CFG->dirroot . '/auth/enrolkey/auth.php'); $options = array(get_string('no'), get_string('yes')); - $settings->visiblename = get_string('menusettings', 'auth_enrolkey'); - - $settings->add(new admin_setting_heading('auth_enrolkey_heading', get_string('settings_heading', 'auth_enrolkey'), - get_string('settings_content', 'auth_enrolkey'))); - - $settings->add(new admin_setting_configselect('auth_enrolkey/tokenrequired', - get_string('settings_required_title', 'auth_enrolkey'), - get_string('settings_required_description', 'auth_enrolkey'), 1, $options)); - + //Choose optional fields: firstname, lastname, city, country + $settings->add(new admin_setting_heading('auth_enrolkey/optionalfield', + new lang_string('optionalfield', 'auth_enrolkey'), + new lang_string('optionalfield_desc', 'auth_enrolkey'))); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/enabledfirstname', + get_string('enablefirstname', 'auth_enrolkey'), '', 0)); + $settings->add(new admin_setting_configcheckbox( 'auth_enrolkey/requiredfirstname', + get_string('requiredfirstname', 'auth_enrolkey'),'', 0)); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/enabledlastname', + get_string('enablelastname', 'auth_enrolkey'), '', 0)); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/requiredlastname', + get_string('requiredlastname', 'auth_enrolkey'),'', 0)); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/enabledcity', + get_string('enablecity', 'auth_enrolkey'), '', 0)); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/requiredcity', + get_string('requiredcity', 'auth_enrolkey'),'', 0)); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/enabledcountry', + get_string('enablecountry', 'auth_enrolkey'), '', 0)); + $settings->add(new admin_setting_configcheckbox('auth_enrolkey/requiredcountry', + get_string('requiredcountry', 'auth_enrolkey'), '', 0)); + + // security + $settings->add(new admin_setting_heading('auth_enrolkey_heading', + get_string('settings_heading', 'auth_enrolkey'), + get_string('settings_security_desc', 'auth_enrolkey'))); $settings->add(new admin_setting_configselect('auth_enrolkey/recaptcha', get_string('recaptcha_key', 'auth_enrolkey'), get_string('recaptcha', 'auth_enrolkey'), 0, $options)); - - $optionspartial = $options; - $optionspartial[] = get_string('settings_partial', 'auth_enrolkey'); $settings->add(new admin_setting_configselect('auth_enrolkey/emailconfirmation', get_string('settings_email_title', 'auth_enrolkey'), - get_string('settings_email_description', 'auth_enrolkey'), 0, $optionspartial)); + get_string('settings_email_description', 'auth_enrolkey'), 0, $options)); - $settings->add(new admin_setting_configselect('auth_enrolkey/unsuspendaccounts', - get_string('unsuspendaccounts', 'auth_enrolkey'), - get_string('unsuspendaccounts_description', 'auth_enrolkey'), 0, $options)); - - if (function_exists('totara_cohort_check_and_update_dynamic_cohort_members')) { - $settings->add(new admin_setting_configcheckbox('auth_enrolkey/totaracohortsync', - get_string('cohortsync', 'auth_enrolkey'), - get_string('cohortsync_description', 'auth_enrolkey'), 0)); - } - - if (moodle_major_version() >= '3.3') { - $authplugin = get_auth_plugin('enrolkey'); - display_auth_lock_options($settings, $authplugin->authtype, $authplugin->userfields, - get_string('auth_fieldlocks_help', 'auth'), false, false, $authplugin->get_custom_user_profile_fields()); - } - - $authplugin = get_auth_plugin('enrolkey'); - display_auth_lock_options($settings, $authplugin->authtype, $authplugin->userfields, - '', true, true, $authplugin->get_custom_user_profile_fields()); + // Enrolment token key + $settings->add(new admin_setting_heading('auth_enrolkey/tokenkey', + get_string('settings_tokenkey', 'auth_enrolkey'), + get_string('settings_tokenkey_desc', 'auth_enrolkey'))); + $settings->add(new admin_setting_configselect('auth_enrolkey/tokenrequired', + get_string('settings_required_title', 'auth_enrolkey'), + get_string('settings_required_description', 'auth_enrolkey'), 0, $options)); - // Create category for Enrolkey. - $ADMIN->add('authsettings', new admin_category('auth_enrolkey', get_string('pluginname', 'auth_enrolkey'))); - // Add settings page toconfigure defaults. - $ADMIN->add('auth_enrolkey', $settings); - // Clear '$settings' to prevent adding again our site category. - $settings = null; - // Add options. - $ADMIN->add('auth_enrolkey', - new admin_externalpage( - 'auth_enrolkey_manage', - get_string('menumanage', 'auth_enrolkey'), - new moodle_url($CFG->wwwroot.'/auth/enrolkey/manage.php') - ) - ); }