MDL-60108 registration: Redirect to registration form

- when first installed
- when manual update of registration information is required
This commit is contained in:
Marina Glancy
2017-10-17 15:00:17 +08:00
parent af925a541d
commit 1095afa4f7
12 changed files with 169 additions and 14 deletions
+99 -4
View File
@@ -40,10 +40,23 @@ use html_writer;
*/
class registration {
/** @var Fields used in a site registration form */
/** @var Fields used in a site registration form.
* IMPORTANT: any new fields with non-empty defaults have to be added to CONFIRM_NEW_FIELDS */
const FORM_FIELDS = ['name', 'description', 'contactname', 'contactemail', 'contactphone', 'imageurl', 'privacy', 'street',
'regioncode', 'countrycode', 'geolocation', 'contactable', 'emailalert', 'emailalertemail', 'commnews', 'commnewsemail', 'language'];
/** @var List of new FORM_FIELDS or siteinfo fields added indexed by the version when they were added.
* If site was already registered, admin will be promted to confirm new registration data manually. Until registration is manually confirmed,
* the scheduled task updating registration will be paused.
* Keys of this array are not important as long as they increment, use current date to avoid confusions.
*/
const CONFIRM_NEW_FIELDS = [
2017092200 => [
'commnews', // Receive communication news. This was added in 3.4 and is "On" by default. Admin must confirm or opt-out.
'mobileservicesenabled', 'mobilenotificationsenabled', 'registereduserdevices', 'registeredactiveuserdevices' // Mobile stats added in 3.4.
],
];
/** @var Site privacy: not displayed */
const HUB_SITENOTPUBLISHED = 'notdisplayed';
@@ -182,6 +195,8 @@ class registration {
}
}
// IMPORTANT: any new fields in siteinfo have to be added to the constant CONFIRM_NEW_FIELDS.
return $siteinfo;
}
@@ -192,6 +207,7 @@ class registration {
* @return string
*/
public static function get_stats_summary($siteinfo) {
$fieldsneedconfirm = self::get_new_registration_fields();
$summary = html_writer::tag('p', get_string('sendfollowinginfo_help', 'hub')) .
html_writer::start_tag('ul');
@@ -222,7 +238,8 @@ class registration {
];
foreach ($senddata as $key => $str) {
$summary .= html_writer::tag('li', $str, ['class' => 'site' . $key]);
$class = in_array($key, $fieldsneedconfirm) ? ' needsconfirmation mark' : '';
$summary .= html_writer::tag('li', $str, ['class' => 'site' . $key . $class]);
}
$summary .= html_writer::end_tag('ul');
return $summary;
@@ -238,6 +255,9 @@ class registration {
foreach (self::FORM_FIELDS as $field) {
set_config('site_' . $field . '_' . $cleanhuburl, $formdata->$field, 'hub');
}
// Even if the the connection with moodle.net fails, admin has manually submitted the form which means they don't need
// to be redirected to the site registration page any more.
set_config('site_regupdateversion_' . $cleanhuburl, max(array_keys(self::CONFIRM_NEW_FIELDS)), 'hub');
}
/**
@@ -278,6 +298,11 @@ class registration {
return;
}
if (self::get_new_registration_fields()) {
mtrace(get_string('pleaserefreshregistrationnewdata', 'admin'));
return;
}
$siteinfo = self::get_site_info();
api::update_registration($siteinfo);
$DB->update_record('registration_hubs', ['id' => $registration->id, 'timemodified' => time()]);
@@ -329,10 +354,11 @@ class registration {
* Moodle.net will check that the site is accessible, register it and redirect back
* to /admin/registration/confirmregistration.php
*
* @param string $returnurl
* @throws \coding_exception
*/
public static function register() {
global $DB;
public static function register($returnurl) {
global $DB, $SESSION;
if (self::is_registered()) {
// Caller of this method must make sure that site is not registered.
@@ -356,6 +382,7 @@ class registration {
$params = self::get_site_info();
$params['token'] = $hub->token;
$SESSION->registrationredirect = $returnurl;
redirect(new moodle_url(HUB_MOODLEORGHUBURL . '/local/hub/siteregistration.php', $params));
}
@@ -450,4 +477,72 @@ class registration {
return null;
}
}
/**
* Does admin need to be redirected to the registration page after install?
*
* @param bool|null $markasviewed if set to true will mark the registration form as viewed and admin will not be redirected
* to the registration form again (regardless of whether the site was registered or not).
* @return bool
*/
public static function show_after_install($markasviewed = null) {
global $CFG;
if (self::is_registered()) {
$showregistration = false;
$markasviewed = true;
} else {
$showregistration = !empty($CFG->registrationpending);
if ($showregistration) {
$host = parse_url($CFG->wwwroot, PHP_URL_HOST);
if ($host === 'localhost' || preg_match('|^127\.\d+\.\d+\.\d+$|', $host)) {
// If it's a localhost, don't redirect to registration, it won't work anyway.
$showregistration = false;
$markasviewed = true;
}
}
}
if ($markasviewed !== null) {
set_config('registrationpending', !$markasviewed);
}
return $showregistration;
}
/**
* Returns the list of the fields in the registration form that were added since registration or last manual update
*
* If this list is not empty the scheduled task will be paused and admin will be reminded to update registration manually.
*
* @return array
*/
public static function get_new_registration_fields() {
$fieldsneedconfirm = [];
if (!self::is_registered()) {
// Nothing to update if site is not registered.
return $fieldsneedconfirm;
}
$cleanhuburl = clean_param(HUB_MOODLEORGHUBURL, PARAM_ALPHANUMEXT);
$lastupdated = (int)get_config('hub', 'site_regupdateversion_' . $cleanhuburl);
foreach (self::CONFIRM_NEW_FIELDS as $version => $fields) {
if ($version > $lastupdated) {
$fieldsneedconfirm = array_merge($fieldsneedconfirm, $fields);
}
}
return $fieldsneedconfirm;
}
/**
* Redirect to the site registration form if it's a new install or registration needs updating
*
* @param string|moodle_url $url
*/
public static function registration_reminder($url) {
if (!has_capability('moodle/site:config', context_system::instance())) {
return;
}
if (self::show_after_install() || self::get_new_registration_fields()) {
$returnurl = new moodle_url($url);
redirect(new moodle_url('/admin/registration/index.php', ['returnurl' => $returnurl->out_as_local_url(false)]));
}
}
}
+25 -4
View File
@@ -68,6 +68,9 @@ class site_registration_form extends \moodleform {
]);
// Fields that need to be highlighted.
$highlightfields = registration::get_new_registration_fields();
$mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
$mform->addElement('text', 'name', get_string('sitename', 'hub'),
@@ -142,14 +145,14 @@ class site_registration_form extends \moodleform {
$this->add_select_with_email('commnews', 'sitecommnews', [
0 => get_string('sitecommnewsno', 'hub'),
1 => get_string('sitecommnewsyes', 'hub'),
]);
], in_array('commnews', $highlightfields));
// TODO site logo.
$mform->addElement('hidden', 'imageurl', ''); // TODO: temporary.
$mform->setType('imageurl', PARAM_URL);
$mform->addElement('header', 'sitestats', get_string('sendfollowinginfo', 'hub'));
$mform->setExpanded('sitestats', false);
$mform->setExpanded('sitestats', !empty($highlightfields));
$mform->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $siteinfo['url']);
$mform->addHelpButton('urlstring', 'siteurl', 'hub');
@@ -167,6 +170,9 @@ class site_registration_form extends \moodleform {
$this->add_action_buttons(false, $buttonlabel);
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
$this->set_data($siteinfo);
}
@@ -176,8 +182,9 @@ class site_registration_form extends \moodleform {
* @param string $elementname
* @param string $stridentifier
* @param array|null $options options for the select element
* @param bool $highlight highlight as a new field
*/
protected function add_select_with_email($elementname, $stridentifier, $options = null) {
protected function add_select_with_email($elementname, $stridentifier, $options = null, $highlight = false) {
$mform = $this->_form;
if ($options === null) {
@@ -192,7 +199,10 @@ class site_registration_form extends \moodleform {
$mform->createElement('text', $elementname . 'email', get_string('email'))
];
$mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false);
$element = $mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false);
if ($highlight) {
$element->setAttributes(['class' => $element->getAttribute('class') . ' needsconfirmation mark']);
}
$mform->hideIf($elementname . 'email', $elementname, 'eq', 0);
$mform->hideIf($elementname . 'newemail', $elementname, 'eq', 0);
$mform->hideIf($elementname . 'email', $elementname . 'newemail', 'notchecked');
@@ -260,6 +270,17 @@ class site_registration_form extends \moodleform {
unset($data->commnewsnewemail);
// Always return 'contactable'.
$data->contactable = empty($data->contactable) ? 0 : 1;
if (debugging('', DEBUG_DEVELOPER)) {
// Display debugging message for developers who added fields to the form and forgot to add them to registration::FORM_FIELDS.
$keys = array_diff(array_keys((array)$data), ['returnurl', 'mform_isexpanded_id_sitestats', 'submitbutton', 'update']);
if ($extrafields = array_diff($keys, registration::FORM_FIELDS)) {
debugging('Found extra fields in the form results: ' . join(', ', $extrafields), DEBUG_DEVELOPER);
}
if ($missingfields = array_diff(registration::FORM_FIELDS, $keys)) {
debugging('Some fields are missing in the form results: ' . join(', ', $missingfields), DEBUG_DEVELOPER);
}
}
}
return $data;
}