diff --git a/classes/form/accessibilitysupport_form.php b/classes/form/accessibilitysupport_form.php index 6f0c882dffc..36d46b10e07 100644 --- a/classes/form/accessibilitysupport_form.php +++ b/classes/form/accessibilitysupport_form.php @@ -127,10 +127,17 @@ public function definition(): void { $mform->hardFreeze('email'); } - if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) { - $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth')); - $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth'); - $mform->closeHeaderBefore('recaptcha_element'); + // If the admin enabled re-captcha on this page. + $accessibilitysupportrecaptcha = get_config('theme_boost_union', 'accessibilitysupportrecaptcha'); + if (isset($accessibilitysupportrecaptcha) && + ($accessibilitysupportrecaptcha == THEME_BOOST_UNION_SETTING_SELECT_ALWAYS) || + ($accessibilitysupportrecaptcha == THEME_BOOST_UNION_SETTING_SELECT_ONLYGUESTSANDNONLOGGEDIN && + (!isloggedin() || isguestuser()))) { + if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) { + $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth')); + $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth'); + $mform->closeHeaderBefore('recaptcha_element'); + } } $this->add_action_buttons(true, get_string('submit')); diff --git a/lang/en/theme_boost_union.php b/lang/en/theme_boost_union.php index 0768fe42a51..590a17184ee 100644 --- a/lang/en/theme_boost_union.php +++ b/lang/en/theme_boost_union.php @@ -34,6 +34,11 @@ $string['configtitle'] = 'Boost Union'; $string['githubissueslink'] = 'Github issues'; +// General select options. +$string['never'] = 'Never'; +$string['always'] = 'Always'; +$string['forguestsonly'] = 'Only for guests and non-logged-in users'; + // Settings: General strings. $string['dontchange'] = 'Do not change anything'; @@ -1149,6 +1154,9 @@ $string['accessibilitysupportpagesrlinktitledefault'] = 'Get accessibility support'; $string['accessibilitysupportpagesrlinktitlesetting'] = 'Accessibility support page screenreader link title'; $string['accessibilitysupportpagesrlinktitlesetting_desc'] = 'In this setting, you can define the screenreader link title for the accessibility support page. This text will be used as link text which is only shown to screenreaders.'; +// ... ... Setting: Add re-captcha to accessibility support page +$string['accessibilitysupportrecaptcha'] = 'Add re-captcha to accessibility support page'; +$string['accessibilitysupportrecaptcha_desc'] = 'With this setting, you control if a re-captcha is added to the accessibility support page. This is to prevent spam and abuse of the accessibility support form, just like it is done within Moodle core\'s support form. However, adding re-captchas add an additional accessibility barrier for users who use screenreaders or other assistive technologies which might be counter-productive in this case. Thus, please choose wisely if you want to enable this setting. Please also note that, even if enabled, the re-captcha is not shown until you set the necessary API keys in the authentication settings.'; // Settings: Flavours page. $string['configtitleflavours'] = 'Flavours'; diff --git a/lib.php b/lib.php index 330d485a9b9..0f8712d318b 100644 --- a/lib.php +++ b/lib.php @@ -133,6 +133,10 @@ define('THEME_BOOST_UNION_SETTING_EXTSCSSSOURCE_DOWNLOAD', 1); define('THEME_BOOST_UNION_SETTING_EXTSCSSSOURCE_GITHUB', 2); +define('THEME_BOOST_UNION_SETTING_SELECT_NEVER', 'never'); +define('THEME_BOOST_UNION_SETTING_SELECT_ALWAYS', 'always'); +define('THEME_BOOST_UNION_SETTING_SELECT_ONLYGUESTSANDNONLOGGEDIN', 'guestandnonloggedin'); + /** * Returns the main SCSS content. * diff --git a/settings.php b/settings.php index ed7af3b1ac1..245f9074705 100644 --- a/settings.php +++ b/settings.php @@ -3013,6 +3013,27 @@ $page->hide_if('theme_boost_union/accessibilitysupportpagesrlinktitle', 'theme_boost_union/enableaccessibilitysupport', 'neq', THEME_BOOST_UNION_SETTING_SELECT_YES); + // Setting: Add re-captcha to accessibility support page. + $name = 'theme_boost_union/accessibilitysupportrecaptcha'; + $title = get_string('accessibilitysupportrecaptcha', 'theme_boost_union', null, true); + $authsettingsurl = new moodle_url('/admin/settings.php', ['section' => 'manageauths']); + $supportformsurl = new moodle_url('/user/contactsitesupport.php'); + $description = get_string('accessibilitysupportrecaptcha_desc', 'theme_boost_union', + ['settings' => $authsettingsurl, 'support' => $supportformsurl], true); + $accessibilitysupportrecaptchaoptions = [ + THEME_BOOST_UNION_SETTING_SELECT_NEVER => + get_string('never', 'theme_boost_union'), + THEME_BOOST_UNION_SETTING_SELECT_ALWAYS => + get_string('always', 'theme_boost_union'), + THEME_BOOST_UNION_SETTING_SELECT_ONLYGUESTSANDNONLOGGEDIN => + get_string('forguestsonly', 'theme_boost_union'), + ]; + $setting = new admin_setting_configselect($name, $title, $description, THEME_BOOST_UNION_SETTING_SELECT_NEVER, + $accessibilitysupportrecaptchaoptions); + $tab->add($setting); + $page->hide_if('theme_boost_union/accessibilitysupportrecaptcha', 'theme_boost_union/enableaccessibilitysupport', + 'neq', THEME_BOOST_UNION_SETTING_SELECT_YES); + // Add tab to settings page. $page->add($tab); diff --git a/tests/behat/theme_boost_union_accessibilitysettings_support.feature b/tests/behat/theme_boost_union_accessibilitysettings_support.feature index d2c6f3bc79d..19af5c63827 100644 --- a/tests/behat/theme_boost_union_accessibilitysettings_support.feature +++ b/tests/behat/theme_boost_union_accessibilitysettings_support.feature @@ -226,3 +226,30 @@ Feature: Configuring the theme_boost_union plugin for the "Support page" tab on # Unfortunately, this can't be tested with Behat yet as we do not have a way to test sent emails yet (or rather block the sending of the email). # Scenario: Sending the accessibility support form fails and the fallback message is shown + + @javascript + Scenario Outline: Setting: Add re-captcha to accessibility support page + Given the following config values are set as admin: + | config | value | + | recaptchapublickey | foo | + | recaptchaprivatekey | bar | + And the following config values are set as admin: + | config | value | plugin | + | enableaccessibilitysupport | yes | theme_boost_union | + | allowaccessibilitysupportwithoutlogin | yes | theme_boost_union | + | accessibilitysupportrecaptcha | | theme_boost_union | + When I am on accessibilitysupport page + Then "#fitem_id_recaptcha_element" "css_element" exist + And I log in as "guest" + And I am on accessibilitysupport page + And "#fitem_id_recaptcha_element" "css_element" exist + And I log out + And I log in as "student1" + And I am on accessibilitysupport page + And "#fitem_id_recaptcha_element" "css_element" exist + + Examples: + | setting | nonshouldornot | guestshouldornot | usershouldornot | + | never | should not | should not | should not | + | always | should | should | should | + | guestandnonloggedin | should | should | should not |