diff --git a/admin/registration/renderer.php b/admin/registration/renderer.php index 196d680e140c3..4832013004eac 100644 --- a/admin/registration/renderer.php +++ b/admin/registration/renderer.php @@ -36,14 +36,6 @@ class core_register_renderer extends plugin_renderer_base { * @return string */ public function moodleorg_registration_message() { - $moodleorgstatslink = html_writer::link('http://moodle.net/stats', - get_string('statsmoodleorg', 'admin'), - array('target' => '_blank')); - - $moodleorgregmsg = get_string('registermoodleorg', 'admin'); - $items = array(get_string('registermoodleorgli1', 'admin'), - get_string('registermoodleorgli2', 'admin', $moodleorgstatslink)); - $moodleorgregmsg .= html_writer::alist($items); - return $moodleorgregmsg; + return format_text(get_string('registermoodlenet', 'admin'), FORMAT_MARKDOWN, ['noclean' => true]); } } diff --git a/lang/en/admin.php b/lang/en/admin.php index 60a865f5e8b6d..3ca6c876a4a61 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -938,6 +938,15 @@ $string['recaptchaprivatekey'] = 'ReCAPTCHA secret key'; $string['recaptchapublickey'] = 'ReCAPTCHA site key'; $string['register'] = 'Register your site'; +$string['registermoodlenet'] = 'We\'d love to stay in touch for important things for your Moodle site! + +By registering, + +* You are contributing to our collective knowledge about the users of Moodle which helps us improve Moodle and all our community services +* You’ll be one of the first to find out about important notifications such as security alerts and new Moodle releases. +* You can access and activate mobile push notifications from your Moodle site through our free [Moodle Mobile app](https://download.moodle.org/mobile/) +* Optionally, your site can be included as a proud member and supporter of the Moodle community on the [list of registered sites](https://moodle.net/stats). +'; $string['registermoodleorg'] = 'When you register your site'; $string['registermoodleorgli1'] = 'You are added to a low-volume mailing list for important notifications such as security alerts and new releases of Moodle.'; $string['registermoodleorgli2'] = 'Statistics about your site will be added to the {$a} of the worldwide Moodle community.'; diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index 6ab1d4a54552b..6b46469aa3aeb 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -154,3 +154,5 @@ moodleorghubname,core_admin hubs,core_admin quickdownloadcalendar,core_calendar ical,core_calendar +privacy,core_hub +privacy_help,core_hub diff --git a/lang/en/hub.php b/lang/en/hub.php index fd5d5935eb762..8644b4e632858 100644 --- a/lang/en/hub.php +++ b/lang/en/hub.php @@ -114,8 +114,6 @@ $string['postaladdress_help'] = 'Postal address of this site, or of the entity represented by this site.'; $string['postsnumber'] = 'Number of posts ({$a})'; $string['previousregistrationdeleted'] = 'The previous registration has been deleted from {$a}. You can restart the registration process. Thank you.'; -$string['privacy'] = 'Privacy'; -$string['privacy_help'] = 'The hub may want to display a list of registered sites. If it does then you can choose whether or not you want to appear on that list.'; $string['publicationinfo'] = 'Course publication information'; $string['publishcourse'] = 'Publish {$a}'; $string['publishcourseon'] = 'Publish on {$a}'; @@ -152,30 +150,36 @@ $string['sharepublication_help'] = 'Uploading this course to a community server will enable people to download it and install it on their own Moodle sites.'; $string['siteadmin'] = 'Administrator'; $string['siteadmin_help'] = 'The full name of the site administrator.'; +$string['sitecommnews'] = 'Updates about Moodle news and features'; +$string['sitecommnews_help'] = 'You have the option of subscribing to our low volume email list including a newsletter about happenings in the Moodle community. '; +$string['sitecommnewsno'] = 'No, I do not want to receive any email from Moodle HQ'; +$string['sitecommnewsyes'] = 'Yes please, include me in Moodle’s regular e-newsletter updates'; $string['sitecountry'] = 'Country'; -$string['sitecountry_help'] = 'The country your organisation is in.'; +$string['sitecountry_help'] = 'The country your organisation or institution is located in.'; $string['sitedesc'] = 'Description'; -$string['sitedesc_help'] = 'This description of your site may be shown in the site listing. Please use plain text only.'; +$string['sitedesc_help'] = 'Provide some information about how you are using Moodle.'; $string['sitegeolocation'] = 'Geolocation'; $string['sitegeolocation_help'] = 'In future we may provide location-based searching in the hubs. If you want to specify the location for your site use a latitude/longitude value here (eg: -31.947884,115.871285). One way to find this is to use Google Maps.'; -$string['siteemail'] = 'Email address'; -$string['siteemail_help'] = 'You need to provide an email address so the hub administrator can contact you if necessary. This will not be used for any other purpose. It is recommended to enter a email address related to a position (example: sitemanager@example.com) and not directly to a person.'; +$string['siteemail'] = 'Admin email address'; +$string['siteemail_help'] = 'An email address is necessary so that Moodle HQ can contact you if required. It may also be used for other purposes as you allow below. We recommend a generic email address related to a position (eg.: sitemanager@example.com), not a personal address.'; $string['sitelang'] = 'Language'; -$string['sitelang_help'] = 'Your site language will be displayed on the site listing.'; +$string['sitelang_help'] = 'What is the main language used on your Moodle site?'; $string['sitename'] = 'Name'; $string['sitename_help'] = 'The name of the site will be shown on the site listing if the hub allows that.'; $string['sitephone'] = 'Phone'; $string['sitephone_help'] = 'Your phone number will only be seen by the hub administrator.'; -$string['siteprivacy'] = 'Privacy'; -$string['siteprivacynotpublished'] = 'Please do not publish this site'; -$string['siteprivacypublished'] = 'Publish the site name only'; -$string['siteprivacylinked'] = 'Publish the site name with a link'; -$string['siteregistrationcontact'] = 'Contact form'; -$string['siteregistrationcontact_help'] = 'If you allow it, other people may be able to contact you via a contact form on the hub. They will never be able to see your email address.'; -$string['siteregistrationemail'] = 'Email notifications'; -$string['siteregistrationemail_help'] = 'If you enable this the hub administrator may email you to inform you of important news like security issues.'; +$string['siteprivacy'] = 'Site listing'; +$string['siteprivacy_help'] = 'You can choose to have your site listed publicly in the list of registered sites, with or without a link to your site.'; +$string['siteprivacynotpublished'] = 'Do not list my site'; +$string['siteprivacypublished'] = 'Only display my site name'; +$string['siteprivacylinked'] = 'Display my site name with the link'; +$string['siteregistrationcontact'] = 'Display contact form'; +$string['siteregistrationcontact_help'] = 'If you allow it, other people in our Moodle community (who need a login account) can contact you via a form on our Moodle community site. However, they will never be able to see your email address.'; +$string['siteregistrationemail'] = 'Notifications about important security and technical issues.'; +$string['siteregistrationemail_help'] = 'You have the option of subscribing to our low volume email list for important news (on security issues or new releases).'; $string['siteregistrationupdated'] = 'Site registration updated'; $string['siterelease'] = 'Moodle release'; +$string['sitereleasenum'] = 'Moodle release ({$a})'; $string['siterelease_help'] = 'Moodle release number of this site.'; $string['siteurl'] = 'Site URL'; $string['siteurl_help'] = 'The URL is the address of this site. If privacy settings allow people to see site addresses then this is the URL that will be used.'; @@ -200,6 +204,7 @@ $string['update'] = 'Update'; $string['updatesite'] = 'Update registration on {$a}'; $string['updatestatus'] = 'Check it now.'; +$string['usedifferentemail'] = 'Use different email'; $string['urlalreadyregistered'] = 'Your site seems to be already registered on Moodle.net, which means something has gone wrong. Please contact the Moodle.net administrator to reset your registration so you can try again.'; $string['usersnumber'] = 'Number of users ({$a})'; $string['wrongtoken'] = 'The registration failed for some unknown reason (network?). Please try again.'; @@ -240,6 +245,8 @@ $string['notregisteredonmoodleorg'] = 'Your administrator needs to register this site with moodle.org.'; $string['orenterprivatehub'] = 'Alternatively, enter a private hub URL:'; $string['prioritise'] = 'Prioritise'; +$string['privacy'] = 'Privacy'; +$string['privacy_help'] = 'The hub may want to display a list of registered sites. If it does then you can choose whether or not you want to appear on that list.'; $string['private'] = 'Private'; $string['privatehuburl'] = 'Private hub URL'; $string['publichub'] = 'Public hub'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 6844d5fd3a8ab..1f4c1e2cd08a8 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1555,8 +1555,8 @@ $string['refreshingevents'] = 'Refreshing events'; $string['registration'] = 'Moodle registration'; $string['registrationcontact'] = 'Contact from the public'; -$string['registrationcontactno'] = 'No, I do not want a contact form in the site listing'; -$string['registrationcontactyes'] = 'Yes, provide a form for prospective Moodlers to contact me'; +$string['registrationcontactno'] = 'No, I do not want to be contacted by other people'; +$string['registrationcontactyes'] = 'Yes, provide a form for other Moodlers to contact me'; $string['registrationemail'] = 'Email notifications'; $string['registrationinfo'] = '

This page allows you to register your Moodle site with moodle.org. Registration is free. The main benefit of registering is that you will be added to a low-volume mailing list @@ -1567,9 +1567,9 @@

If you choose, you can allow your site name, country and URL to be added to the public list of Moodle Sites.

All new registrations are verified manually before they are added to the list, but once you are added you can update your registration (and your entry on the public list) at any time by resubmitting this form.

'; $string['registrationinfotitle'] = 'Registration information'; -$string['registrationno'] = 'No, I do not want to receive email'; +$string['registrationno'] = 'No, I do not want to receive any email from Moodle HQ'; $string['registrationsend'] = 'Send registration information to moodle.org'; -$string['registrationyes'] = 'Yes, please notify me about important issues'; +$string['registrationyes'] = 'Yes, notify me about important news (e.g. security issues or releases) '; $string['reject'] = 'Reject'; $string['rejectdots'] = 'Reject...'; $string['reload'] = 'Reload'; diff --git a/lib/classes/hub/registration.php b/lib/classes/hub/registration.php index a75929ef4ae0b..9bca6fb0564bd 100644 --- a/lib/classes/hub/registration.php +++ b/lib/classes/hub/registration.php @@ -29,6 +29,7 @@ use moodle_url; use context_system; use stdClass; +use html_writer; /** * Methods to use when publishing and searching courses on moodle.net @@ -41,7 +42,7 @@ class registration { /** @var Fields used in a site registration form */ const FORM_FIELDS = ['name', 'description', 'contactname', 'contactemail', 'contactphone', 'imageurl', 'privacy', 'street', - 'regioncode', 'countrycode', 'geolocation', 'contactable', 'emailalert', 'language']; + 'regioncode', 'countrycode', 'geolocation', 'contactable', 'emailalert', 'emailalertemail', 'commnews', 'commnewsemail', 'language']; /** @var Site privacy: not displayed */ const HUB_SITENOTPUBLISHED = 'notdisplayed'; @@ -162,7 +163,6 @@ public static function get_site_info($defaults = []) { $siteinfo['modulenumberaverage'] = average_number_of_courses_modules(); // Version and url. - $siteinfo['moodleversion'] = $CFG->version; $siteinfo['moodlerelease'] = $CFG->release; $siteinfo['url'] = $CFG->wwwroot; @@ -185,6 +185,49 @@ public static function get_site_info($defaults = []) { return $siteinfo; } + /** + * Human-readable summary of data that will be sent to moodle.net + * + * @param array $siteinfo result of get_site_info() + * @return string + */ + public static function get_stats_summary($siteinfo) { + $summary = html_writer::tag('p', get_string('sendfollowinginfo_help', 'hub')) . + html_writer::start_tag('ul'); + + $mobileservicesenabled = $siteinfo['mobileservicesenabled'] ? get_string('yes') : get_string('no'); + $mobilenotificationsenabled = $siteinfo['mobilenotificationsenabled'] ? get_string('yes') : get_string('no'); + $moodlerelease = $siteinfo['moodlerelease']; + if (preg_match('/^(\d+\.\d.*?)[\. ]/', $moodlerelease, $matches)) { + $moodlerelease = $matches[1]; + } + $senddata = [ + 'moodlerelease' => get_string('sitereleasenum', 'hub', $moodlerelease), + 'courses' => get_string('coursesnumber', 'hub', $siteinfo['courses']), + 'users' => get_string('usersnumber', 'hub', $siteinfo['users']), + 'enrolments' => get_string('roleassignmentsnumber', 'hub', $siteinfo['enrolments']), + 'posts' => get_string('postsnumber', 'hub', $siteinfo['posts']), + 'questions' => get_string('questionsnumber', 'hub', $siteinfo['questions']), + 'resources' => get_string('resourcesnumber', 'hub', $siteinfo['resources']), + 'badges' => get_string('badgesnumber', 'hub', $siteinfo['badges']), + 'issuedbadges' => get_string('issuedbadgesnumber', 'hub', $siteinfo['issuedbadges']), + 'participantnumberaverage' => get_string('participantnumberaverage', 'hub', + format_float($siteinfo['participantnumberaverage'], 2)), + 'modulenumberaverage' => get_string('modulenumberaverage', 'hub', + format_float($siteinfo['modulenumberaverage'], 2)), + 'mobileservicesenabled' => get_string('mobileservicesenabled', 'hub', $mobileservicesenabled), + 'mobilenotificationsenabled' => get_string('mobilenotificationsenabled', 'hub', $mobilenotificationsenabled), + 'registereduserdevices' => get_string('registereduserdevices', 'hub', $siteinfo['registereduserdevices']), + 'registeredactiveuserdevices' => get_string('registeredactiveuserdevices', 'hub', $siteinfo['registeredactiveuserdevices']), + ]; + + foreach ($senddata as $key => $str) { + $summary .= html_writer::tag('li', $str, ['class' => 'site' . $key]); + } + $summary .= html_writer::end_tag('ul'); + return $summary; + } + /** * Save registration info locally so it can be retrieved when registration needs to be updated * diff --git a/lib/classes/hub/site_registration_form.php b/lib/classes/hub/site_registration_form.php index f6f69e6580c29..3b8a6fd89f05f 100644 --- a/lib/classes/hub/site_registration_form.php +++ b/lib/classes/hub/site_registration_form.php @@ -26,6 +26,7 @@ defined('MOODLE_INTERNAL') || die(); use context_course; +use stdClass; global $CFG; require_once($CFG->libdir . '/formslib.php'); @@ -63,6 +64,7 @@ public function definition() { 'language' => explode('_', current_language())[0], 'geolocation' => '', 'emailalert' => 1, + 'commnews' => 1 ]); @@ -70,18 +72,16 @@ public function definition() { $mform->addElement('text', 'name', get_string('sitename', 'hub'), array('class' => 'registration_textfield')); - $mform->addRule('name', $strrequired, 'required', null, 'client'); $mform->setType('name', PARAM_TEXT); $mform->addHelpButton('name', 'sitename', 'hub'); $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), registration::site_privacy_options()); $mform->setType('privacy', PARAM_ALPHA); - $mform->addHelpButton('privacy', 'privacy', 'hub'); + $mform->addHelpButton('privacy', 'siteprivacy', 'hub'); unset($options); $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'), - array('rows' => 8, 'cols' => 41)); - $mform->addRule('description', $strrequired, 'required', null, 'client'); + array('rows' => 3, 'cols' => 41)); $mform->setType('description', PARAM_TEXT); $mform->addHelpButton('description', 'sitedesc', 'hub'); @@ -91,8 +91,8 @@ public function definition() { $mform->setType('language', PARAM_ALPHANUMEXT); $mform->addHelpButton('language', 'sitelang', 'hub'); - $mform->addElement('textarea', 'street', get_string('postaladdress', 'hub'), - array('rows' => 4, 'cols' => 41)); + // Postal address was part of this form before but not any more. + $mform->addElement('hidden', 'street'); $mform->setType('street', PARAM_TEXT); $mform->addHelpButton('street', 'postaladdress', 'hub'); @@ -105,22 +105,19 @@ public function definition() { $mform->addHelpButton('countrycode', 'sitecountry', 'hub'); $mform->addRule('countrycode', $strrequired, 'required', null, 'client'); - $mform->addElement('text', 'geolocation', get_string('sitegeolocation', 'hub'), - array('class' => 'registration_textfield')); + // Geolocation was part of this form before but not any more. + $mform->addElement('hidden', 'geolocation'); $mform->setType('geolocation', PARAM_RAW); $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub'); - $mform->addElement('text', 'contactname', get_string('siteadmin', 'hub'), - array('class' => 'registration_textfield')); - $mform->addRule('contactname', $strrequired, 'required', null, 'client'); + // Admin name was part of this form before but not any more. + $mform->addElement('hidden', 'contactname'); $mform->setType('contactname', PARAM_TEXT); $mform->addHelpButton('contactname', 'siteadmin', 'hub'); - $mform->addElement('text', 'contactphone', get_string('sitephone', 'hub'), - array('class' => 'registration_textfield')); + $mform->addElement('hidden', 'contactphone'); $mform->setType('contactphone', PARAM_TEXT); $mform->addHelpButton('contactphone', 'sitephone', 'hub'); - $mform->setForceLtr('contactphone'); $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'), array('class' => 'registration_textfield')); @@ -134,79 +131,30 @@ public function definition() { $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options); $mform->setType('contactable', PARAM_INT); $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub'); + $mform->hideIf('contactable', 'privacy', 'eq', registration::HUB_SITENOTPUBLISHED); unset($options); - $options = array(); - $options[0] = get_string("registrationno"); - $options[1] = get_string("registrationyes"); - $mform->addElement('select', 'emailalert', get_string('siteregistrationemail', 'hub'), $options); - $mform->setType('emailalert', PARAM_INT); - $mform->addHelpButton('emailalert', 'siteregistrationemail', 'hub'); - unset($options); + $this->add_select_with_email('emailalert', 'siteregistrationemail', [ + 0 => get_string('registrationno'), + 1 => get_string('registrationyes'), + ]); + + $this->add_select_with_email('commnews', 'sitecommnews', [ + 0 => get_string('sitecommnewsno', 'hub'), + 1 => get_string('sitecommnewsyes', 'hub'), + ]); // 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->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $siteinfo['url']); $mform->addHelpButton('urlstring', 'siteurl', 'hub'); - $mform->addElement('static', 'versionstring', get_string('siteversion', 'hub'), $CFG->version); - $mform->addElement('hidden', 'moodleversion', $siteinfo['moodleversion']); - $mform->setType('moodleversion', PARAM_INT); - $mform->addHelpButton('versionstring', 'siteversion', 'hub'); - - $mform->addElement('static', 'releasestring', get_string('siterelease', 'hub'), $CFG->release); - $mform->addElement('hidden', 'moodlerelease', $siteinfo['moodlerelease']); - $mform->setType('moodlerelease', PARAM_TEXT); - $mform->addHelpButton('releasestring', 'siterelease', 'hub'); - // Display statistic that are going to be retrieve by moodle.net. - - $mform->addElement('static', 'courseslabel', get_string('sendfollowinginfo', 'hub'), - " " . get_string('coursesnumber', 'hub', $siteinfo['courses'])); - $mform->addHelpButton('courseslabel', 'sendfollowinginfo', 'hub'); - - $mform->addElement('static', 'userslabel', '', - " " . get_string('usersnumber', 'hub', $siteinfo['users'])); - - $mform->addElement('static', 'roleassignmentslabel', '', - " " . get_string('roleassignmentsnumber', 'hub', $siteinfo['enrolments'])); - - $mform->addElement('static', 'postslabel', '', - " " . get_string('postsnumber', 'hub', $siteinfo['posts'])); - - $mform->addElement('static', 'questionslabel', '', - " " . get_string('questionsnumber', 'hub', $siteinfo['questions'])); - - $mform->addElement('static', 'resourceslabel', '', - " " . get_string('resourcesnumber', 'hub', $siteinfo['resources'])); - - $mform->addElement('static', 'badgeslabel', '', - " " . get_string('badgesnumber', 'hub', $siteinfo['badges'])); - - $mform->addElement('static', 'issuedbadgeslabel', '', - " " . get_string('issuedbadgesnumber', 'hub', $siteinfo['issuedbadges'])); - - $mform->addElement('static', 'participantnumberaveragelabel', '', - " " . get_string('participantnumberaverage', 'hub', $siteinfo['participantnumberaverage'])); - - $mform->addElement('static', 'modulenumberaveragelabel', '', - " " . get_string('modulenumberaverage', 'hub', $siteinfo['modulenumberaverage'])); - - $mobileservicestatus = $siteinfo['mobileservicesenabled'] ? get_string('yes') : get_string('no'); - $mform->addElement('static', 'mobileservicesenabledlabel', '', - " " . get_string('mobileservicesenabled', 'hub', $mobileservicestatus)); - - $mobilenotificationsstatus = $siteinfo['mobilenotificationsenabled'] ? get_string('yes') : get_string('no'); - $mform->addElement('static', 'mobilenotificationsenabledlabel', '', - " " . get_string('mobilenotificationsenabled', 'hub', $mobilenotificationsstatus)); - - $mform->addElement('static', 'registereduserdeviceslabel', '', - " " . get_string('registereduserdevices', 'hub', $siteinfo['registereduserdevices'])); - - $mform->addElement('static', 'registeredactiveuserdeviceslabel', '', - " " . get_string('registeredactiveuserdevices', 'hub', $siteinfo['registeredactiveuserdevices'])); + $mform->addElement('static', 'siteinfosummary', get_string('sendfollowinginfo', 'hub'), registration::get_stats_summary($siteinfo)); // Check if it's a first registration or update. if (registration::is_registered()) { @@ -222,5 +170,99 @@ public function definition() { $this->set_data($siteinfo); } + /** + * Add yes/no select with additional checkbox allowing to specify another email + * + * @param string $elementname + * @param string $stridentifier + * @param array|null $options options for the select element + */ + protected function add_select_with_email($elementname, $stridentifier, $options = null) { + $mform = $this->_form; + + if ($options === null) { + $options = [0 => get_string('no'), 1 => get_string('yes')]; + } + + $group = [ + $mform->createElement('select', $elementname, get_string($stridentifier, 'hub'), $options), + $mform->createElement('static', $elementname . 'sep', '', '
'), + $mform->createElement('advcheckbox', $elementname . 'newemail', '', get_string('usedifferentemail', 'hub'), + ['onchange' => "this.form.elements['{$elementname}email'].focus();"]), + $mform->createElement('text', $elementname . 'email', get_string('email')) + ]; + + $mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false); + $mform->hideIf($elementname . 'email', $elementname, 'eq', 0); + $mform->hideIf($elementname . 'newemail', $elementname, 'eq', 0); + $mform->hideIf($elementname . 'email', $elementname . 'newemail', 'notchecked'); + $mform->setType($elementname, PARAM_INT); + $mform->setType($elementname . 'email', PARAM_RAW_TRIMMED); // E-mail will be validated in validation(). + $mform->addHelpButton($elementname . 'group', $stridentifier, 'hub'); + + } + + /** + * Load in existing data as form defaults + * + * @param stdClass|array $defaultvalues object or array of default values + */ + public function set_data($defaultvalues) { + if (is_object($defaultvalues)) { + $defaultvalues = (array)$defaultvalues; + } + $defaultvalues['emailalertnewemail'] = !empty($defaultvalues['emailalert']) && !empty($defaultvalues['emailalertemail']); + if (empty($defaultvalues['emailalertnewemail'])) { + $defaultvalues['emailalertemail'] = ''; + } + $defaultvalues['commnewsnewemail'] = !empty($defaultvalues['commnews']) && !empty($defaultvalues['commnewsemail']); + if (empty($defaultvalues['commnewsnewemail'])) { + $defaultvalues['commnewsemail'] = ''; + } + parent::set_data($defaultvalues); + } + + /** + * Validation of the form data + * + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @return array of "element_name"=>"error_description" if there are errors, + * or an empty array if everything is OK + */ + public function validation($data, $files) { + $errors = parent::validation($data, $files); + // Validate optional emails. We do not use PARAM_EMAIL because it blindly clears the field if it is not a valid email. + if (!empty($data['emailalert']) && !empty($data['emailalertnewemail']) && !validate_email($data['emailalertemail'])) { + $errors['emailalertgroup'] = get_string('invalidemail'); + } + if (!empty($data['commnews']) && !empty($data['commnewsnewemail']) && !validate_email($data['commnewsemail'])) { + $errors['commnewsgroup'] = get_string('invalidemail'); + } + return $errors; + } + + /** + * Returns the form data + * + * @return stdClass + */ + public function get_data() { + if ($data = parent::get_data()) { + // Never return '*newemail' checkboxes, always return 'emailalertemail' and 'commnewsemail' even if not applicable. + if (empty($data->emailalert) || empty($data->emailalertnewemail)) { + $data->emailalertemail = null; + } + unset($data->emailalertnewemail); + if (empty($data->commnews) || empty($data->commnewsnewemail)) { + $data->commnewsemail = null; + } + unset($data->commnewsnewemail); + // Always return 'contactable'. + $data->contactable = empty($data->contactable) ? 0 : 1; + } + return $data; + } + }