Skip to content

Commit

Permalink
Merge branch 'MDL-72928-rebase' of https://github.com/Chocolate-light…
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta authored and rezaies committed Feb 11, 2022
2 parents 057a21e + ae14ff3 commit 9af5456
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 197 deletions.
134 changes: 134 additions & 0 deletions admin/tool/componentlibrary/content/moodle/components/dom-modal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

---
layout: docs
title: "HTML Modals"
description: "A reusable handled modal component"
date: 2021-12-09T14:48:00+08:00
draft: false
tags:
- MDL-71963
- MDL-72928
- "4.0"
---

## How it works

The core/utility module allows different modals to be displayed automatically when interacting with the page.

Modals are configured using a set of specific data-attributes.

## Source files

* `lib/amd/src/utility.js` ({{< jsdoc module="core/utility" >}})
* `lib/templates/modal.mustache`

## Usage
The confirmation AMD module is loaded automatically, so the only thing you need to do is to add some specific data attributes
to the target element.

To display a confirmation modal.
{{< highlight html >}}
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]'>Show confirmation modal</button>
{{< /highlight >}}

To display an alert modal.
{{< highlight html >}}
<button type="button" class="btn btn-primary" data-modal="alert" data-modal-title-str='["cookiesenabled", "core"]'
data-modal-content-str='["cookiesenabled_help_html", "core"]'>Show alert modal</button>
{{< /highlight >}}

You can also use it on PHP, you just need to set the attributes parameter to any moodle output component that takes attributes:
{{< php >}}
echo $OUTPUT->single_button('#', get_string('delete'), 'get', [
'data-modal' => 'modal',
'data-modal-title-str' => json_encode(['delete', 'core']),
'data-modal-content-str' => json_encode(['areyousure']),
'data-modal-yes-button-str' => json_encode(['delete', 'core'])
]);
{{< / php >}}

## Attributes

<table class="table">
<thead>
<tr>
<th style="width: 250px;">Data attribute</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>data-modal</td>
<td>One of either "confirmation", or "alert".</td>
</tr>
<tr>
<td>data-modal-title-str</td>
<td>The modal title language string identifier, must be provided in JSON encoded format.</td>
</tr>
<tr>
<td>data-modal-content-str</td>
<td>The modal content or content language string identifier, must be provided in JSON encoded format.</td>
</tr>
<tr>
<td>data-modal-yes-button-str</td>
<td>
The language string identifier for the "Yes" button, must be provided in JSON encoded format.
Confirmation modals only.
</td>
</tr>
<tr>
<td>data-modal-toast</td>
<td>
If set to "true" it will display a modal toast in the end.
Confirmation modals only.
</td>
</tr>
<tr>
<td>data-modal-toast-confirmation-str</td>
<td>
The confirmation toast language string identifier, must be provided in JSON encoded format.
Confirmation modals only.
</td>
</tr>
<tr>
<td>data-modal-destination</td>
<td>
An url to redirect the user to.
Confirmation modals only.
</td>
</tr>
</tbody>
</table>

## Examples

### Basic Alert modal

{{< example >}}
<button type="button" class="btn btn-primary" data-modal="alert" data-modal-title-str='["cookiesenabled", "core"]'
data-modal-content-str='["cookiesenabled_help_html", "core"]'>Show alert modal</button>
{{< /example >}}

### Basic confirmation modal

{{< example >}}
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]'>Show confirmation modal</button>
{{< /example >}}

### Confirmation modal with a toast

{{< example >}}
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]' data-modal-toast="true"
data-modal-toast-confirmation-str='["deleteblockinprogress", "block", "Online users"]'>Show confirmation modal</button>
{{< /example >}}

### Confirmation modal with redirect

{{< example >}}
<button type="button" class="btn btn-primary" data-modal="confirmation" data-modal-title-str='["delete", "core"]'
data-modal-content-str='["areyousure"]' data-modal-yes-button-str='["delete", "core"]'
data-modal-destination="http://moodle.com">Show confirmation modal</button>
{{< /example >}}
5 changes: 0 additions & 5 deletions auth/classes/output/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class login implements renderable, templatable {
public $instructions;
/** @var moodle_url The form action login URL. */
public $loginurl;
/** @var bool Whether the username should be remembered. */
public $rememberusername;
/** @var moodle_url The sign-up URL. */
public $signupurl;
/** @var string The user name to pre-fill the form with. */
Expand Down Expand Up @@ -96,10 +94,8 @@ public function __construct(array $authsequence, $username = '') {
$this->cansignup = $CFG->registerauth == 'email' || !empty($CFG->registerauth);
if ($CFG->rememberusername == 0) {
$this->cookieshelpicon = new help_icon('cookiesenabledonlysession', 'core');
$this->rememberusername = false;
} else {
$this->cookieshelpicon = new help_icon('cookiesenabled', 'core');
$this->rememberusername = true;
}

$this->autofocusform = !empty($CFG->loginpageautofocus);
Expand Down Expand Up @@ -156,7 +152,6 @@ public function export_for_template(renderer_base $output) {
list($data->instructions, $data->instructionsformat) = external_format_text($this->instructions, FORMAT_MOODLE,
context_system::instance()->id);
$data->loginurl = $this->loginurl->out(false);
$data->rememberusername = $this->rememberusername;
$data->signupurl = $this->signupurl->out(false);
$data->username = $this->username;
$data->logintoken = $this->logintoken;
Expand Down
13 changes: 0 additions & 13 deletions auth/tests/behat/login.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ Feature: Authentication
When I click on "Log out" "link" in the "#page-footer" "css_element"
Then I should see "You are not logged in" in the "page-footer" "region"

Scenario Outline: Checking the display of the Remember username checkbox
Given the following config values are set as admin:
| rememberusername | <settingvalue> |
And I am on homepage
When I click on "Log in" "link" in the ".logininfo" "css_element"
Then I should <expect> "Remember username"

Examples:
| settingvalue | expect |
| 0 | not see |
| 1 | see |
| 2 | see |

@javascript @accessibility
Scenario: Login page must be accessible
When I am on site homepage
Expand Down
1 change: 0 additions & 1 deletion auth/tests/behat/loginform.feature
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Feature: Test if the login form provides the correct feedback
Then the focused element is "Username" "field"
And I set the field "Username" to "admin"
And I set the field "Password" to "admin"
And I set the field "Remember username" to "1"
And I press "Log in"
And I log out
And I follow "Log in"
Expand Down
28 changes: 15 additions & 13 deletions auth/tests/behat/rememberusername.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@core @core_auth
Feature: Test the 'remember username' feature works.
In order to see my saved username on the login form
In order for users to easily log in to the site
As a user
I need to have logged in once before and clicked 'Remember username'
I need the site to remember my username when the feature is enabled

Background:
Given the following "users" exist:
Expand All @@ -11,40 +11,42 @@ Feature: Test the 'remember username' feature works.

# Given the user has logged in and selected 'Remember username', when they log in again, then their username should be remembered.
Scenario: Check that 'remember username' works without javascript for teachers.
# Log in the first time and check the 'remember username' box.
Given I am on homepage
# Log in the first time with $CFG->rememberusername set to Yes.
Given the following config values are set as admin:
| rememberusername | 1 |
And I am on homepage
And I click on "Log in" "link" in the ".logininfo" "css_element"
And I set the field "Username" to "teacher1"
And I set the field "Password" to "teacher1"
And I set the field "Remember username" to "1"
And I press "Log in"
And I log out
# Log out and check that the username was remembered.
When I am on homepage
And I click on "Log in" "link" in the ".logininfo" "css_element"
Then the field "username" matches value "teacher1"
And the field "Remember username" matches value "1"

# Given the user has logged in before and selected 'Remember username', when they log in again and unset 'Remember username', then
# their username should be forgotten for future log in attempts.
Scenario: Check that 'remember username' unsetting works without javascript for teachers.
# Log in the first time and check the 'remember username' box.
Given I am on homepage
# Log in the first time with $CFG->rememberusername set to Optional.
Given the following config values are set as admin:
| rememberusername | 2 |
And I am on homepage
And I click on "Log in" "link" in the ".logininfo" "css_element"
And I set the field "Username" to "teacher1"
And I set the field "Password" to "teacher1"
And I set the field "Remember username" to "1"
And I press "Log in"
And I log out
# Log in again, unsetting the 'remember username' field.
# Log in again, the username should have been remembered.
When I am on homepage
And I click on "Log in" "link" in the ".logininfo" "css_element"
Then the field "username" matches value "teacher1"
And I set the field "Password" to "teacher1"
And I set the field "Remember username" to "0"
And I press "Log in"
And I log out
And the following config values are set as admin:
| rememberusername | 0 |
# Check username has been forgotten.
Then I am on homepage
And I am on homepage
And I click on "Log in" "link" in the ".logininfo" "css_element"
Then the field "username" matches value ""
And the field "Remember username" matches value "0"
10 changes: 0 additions & 10 deletions blocks/login/block_login.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,6 @@ function get_content () {
$this->content->text .= ' class="form-control" value="" autocomplete="current-password"/>';
$this->content->text .= '</div>';

if (isset($CFG->rememberusername) and $CFG->rememberusername == 2) {
$checked = $username ? 'checked="checked"' : '';
$this->content->text .= '<div class="form-check">';
$this->content->text .= '<label class="form-check-label">';
$this->content->text .= '<input type="checkbox" name="rememberusername" id="rememberusername"
class="form-check-input" value="1" '.$checked.'/> ';
$this->content->text .= get_string('rememberusername', 'admin').'</label>';
$this->content->text .= '</div>';
}

$this->content->text .= '<div class="form-group">';
$this->content->text .= '<input type="submit" class="btn btn-primary btn-block" value="'.get_string('login').'" />';
$this->content->text .= '</div>';
Expand Down
8 changes: 7 additions & 1 deletion lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,16 @@
The essential one is the session cookie, usually called MoodleSession. You must allow this cookie in your browser to provide continuity and to remain logged in when browsing the site. When you log out or close the browser, this cookie is destroyed (in your browser and on the server).
The other cookie is purely for convenience, usually called MOODLEID or similar. It just remembers your username in the browser. This means that when you return to this site, the username field on the login page is already filled in for you. It is safe to refuse this cookie - you will just have to retype your username each time you log in.';
$string['cookiesenabled_help_html'] = 'Two cookies are used on this site:<br/><br/>
The essential one is the session cookie, usually called MoodleSession. You must allow this cookie in your browser to provide continuity and to remain logged in when browsing the site. When you log out or close the browser, this cookie is destroyed (in your browser and on the server).<br/><br/>
The other cookie is purely for convenience, usually called MOODLEID or similar. It just remembers your username in the browser. This means that when you return to this site, the username field on the login page is already filled in for you. It is safe to refuse this cookie - you will just have to retype your username each time you log in.';
$string['cookiesenabledonlysession'] = 'Cookies must be enabled in your browser';
$string['cookiesenabledonlysession_help'] = 'This site uses one session cookie, usually called MoodleSession. You must allow this cookie in your browser to provide continuity and to remain logged in when browsing the site. When you log out or close the browser, this cookie is destroyed (in your browser and on the server).';
$string['cookiesnotenabled'] = 'Unfortunately, cookies are currently not enabled in your browser';
$string['cookiesnotice'] = 'Cookies notice';
$string['copy'] = 'copy';
$string['copyasnoun'] = 'copy';
$string['copycourse'] = 'Copy course';
Expand Down Expand Up @@ -2250,7 +2256,7 @@
$string['usermenu'] = 'User menu';
$string['usermenugoback'] = 'Go back to user menu';
$string['username'] = 'Username';
$string['usernameemail'] = 'Username / email';
$string['usernameemail'] = 'Username or email';
$string['usernameemailmatch'] = 'The username and email address do not relate to the same user';
$string['usernameexists'] = 'This username already exists, choose another';
$string['usernamelowercase'] = 'Only lowercase letters allowed';
Expand Down
2 changes: 0 additions & 2 deletions lib/amd/build/confirm.min.js

This file was deleted.

Loading

0 comments on commit 9af5456

Please sign in to comment.