From 879901ef24f2a8fe42256ab7c3a50ca545507bc5 Mon Sep 17 00:00:00 2001 From: Huynh Date: Mon, 6 Sep 2021 16:37:59 +0700 Subject: [PATCH] MDL-72354 badges: Show always issuer details when creating badges Co-author: Huynh Nguyen --- badges/classes/badge.php | 30 ++------- badges/classes/form/badge.php | 47 ++++++------- badges/renderer.php | 5 ++ badges/tests/behat/add_badge.feature | 98 ++++++++++++++++------------ lang/en/badges.php | 2 +- 5 files changed, 88 insertions(+), 94 deletions(-) diff --git a/badges/classes/badge.php b/badges/classes/badge.php index f3d5da7e8ff..ecf48de8ff7 100644 --- a/badges/classes/badge.php +++ b/badges/classes/badge.php @@ -168,14 +168,6 @@ class badge { } } - if (badges_open_badges_backpack_api() != OPEN_BADGES_V1) { - // For Open Badges 2 we need to use a single site issuer with no exceptions. - $issuer = badges_get_default_issuer(); - $this->issuername = $issuer['name']; - $this->issuercontact = $issuer['email']; - $this->issuerurl = $issuer['url']; - } - $this->criteria = self::get_criteria(); } @@ -1020,17 +1012,9 @@ class badge { $fordb->timemodified = $now; $fordb->usercreated = $USER->id; $fordb->usermodified = $USER->id; - - if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) { - $fordb->issuername = $data->issuername; - $fordb->issuerurl = $data->issuerurl; - $fordb->issuercontact = $data->issuercontact; - } else { - $url = parse_url($CFG->wwwroot); - $fordb->issuerurl = $url['scheme'] . '://' . $url['host']; - $fordb->issuername = $CFG->badges_defaultissuername; - $fordb->issuercontact = $CFG->badges_defaultissuercontact; - } + $fordb->issuername = $data->issuername; + $fordb->issuerurl = $data->issuerurl; + $fordb->issuercontact = $data->issuercontact; if (!property_exists($data, 'expiry')) { $data->expiry = 0; @@ -1087,11 +1071,9 @@ class badge { $this->imageauthorurl = $data->imageauthorurl; $this->imagecaption = $data->imagecaption; $this->usermodified = $USER->id; - if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) { - $this->issuername = $data->issuername; - $this->issuerurl = $data->issuerurl; - $this->issuercontact = $data->issuercontact; - } + $this->issuername = $data->issuername; + $this->issuerurl = $data->issuerurl; + $this->issuercontact = $data->issuercontact; $this->expiredate = ($data->expiry == 1) ? $data->expiredate : null; $this->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null; diff --git a/badges/classes/form/badge.php b/badges/classes/form/badge.php index 162d84ab157..8774830657e 100644 --- a/badges/classes/form/badge.php +++ b/badges/classes/form/badge.php @@ -38,7 +38,7 @@ class badge extends moodleform { * Defines the form */ public function definition() { - global $CFG; + global $CFG, $SITE; $mform = $this->_form; $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false; @@ -97,29 +97,28 @@ class badge extends moodleform { $mform->addHelpButton('imagecaption', 'imagecaption', 'badges'); $mform->addElement('tags', 'tags', get_string('tags', 'badges'), ['itemtype' => 'badge', 'component' => 'core_badges']); - if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) { - $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges')); + $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges')); - $mform->addElement('text', 'issuername', get_string('name'), ['size' => '70']); - $mform->setType('issuername', PARAM_NOTAGS); - $mform->addRule('issuername', null, 'required'); - if (isset($CFG->badges_defaultissuername)) { - $mform->setDefault('issuername', $CFG->badges_defaultissuername); - } - $mform->addHelpButton('issuername', 'issuername', 'badges'); + $mform->addElement('text', 'issuername', get_string('issuername', 'badges'), ['size' => '70']); + $mform->setType('issuername', PARAM_NOTAGS); + $mform->addRule('issuername', null, 'required'); + $site = get_site(); + $issuername = $CFG->badges_defaultissuername ?: $site->fullname; + $mform->setDefault('issuername', $issuername); + $mform->addHelpButton('issuername', 'issuername', 'badges'); - $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), ['size' => '70']); - if (isset($CFG->badges_defaultissuercontact)) { - $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact); - } - $mform->setType('issuercontact', PARAM_RAW); - $mform->addHelpButton('issuercontact', 'contact', 'badges'); - // Set issuer URL. - // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot. - $url = parse_url($CFG->wwwroot); - $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']); - $mform->setType('issuerurl', PARAM_URL); + $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), ['size' => '70']); + if (isset($CFG->badges_defaultissuercontact)) { + $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact); } + $mform->setType('issuercontact', PARAM_RAW); + $mform->addRule('issuercontact', null, 'email'); + $mform->addHelpButton('issuercontact', 'contact', 'badges'); + // Set issuer URL. + // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot. + $url = parse_url($CFG->wwwroot); + $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']); + $mform->setType('issuerurl', PARAM_URL); $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges')); @@ -211,12 +210,6 @@ class badge extends moodleform { $errors = parent::validation($data, $files); - if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) { - if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) { - $errors['issuercontact'] = get_string('invalidemail'); - } - } - if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) { $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges'); } diff --git a/badges/renderer.php b/badges/renderer.php index 182229e6881..050f7c1c52d 100644 --- a/badges/renderer.php +++ b/badges/renderer.php @@ -201,6 +201,11 @@ class core_badges_renderer extends plugin_renderer_base { $dl = array(); $dl[get_string('issuername', 'badges')] = $badge->issuername; $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)); + $dl[get_string('issuerurl', 'badges')] = html_writer::tag( + 'a', + $badge->issuerurl, + ['href' => $badge->issuerurl, 'target' => '_blank'], + ); $display .= $this->definition_list($dl); // Issuance details if any. diff --git a/badges/tests/behat/add_badge.feature b/badges/tests/behat/add_badge.feature index 561d661242d..d5d8f0ec025 100644 --- a/badges/tests/behat/add_badge.feature +++ b/badges/tests/behat/add_badge.feature @@ -20,20 +20,22 @@ Feature: Add badges to the system Then I should see "There are currently no badges available for users to earn." @javascript @_file_upload - Scenario: Add a badge - Given I navigate to "Badges > Badges settings" in site administration - And I set the field "Badge issuer name" to "Test Badge Site" - And I set the field "Badge issuer email address" to "testuser@example.com" - And I press "Save changes" + Scenario: Add a site badge + Given the following config values are set as admin: + | badges_defaultissuername | Test Badge Site | + | badges_defaultissuercontact | testuser@example.com | And I navigate to "Badges > Add a new badge" in site administration + And the field "Issuer name" matches value "Test Badge Site" + And the field "Issuer contact" matches value "testuser@example.com" And I set the following fields to these values: - | Name | Test badge with 'apostrophe' and other friends (<>&@#) | - | Version | v1 | - | Language | English | - | Description | Test badge description | - | Image author | http://author.example.com | - | Image caption | Test caption image | - | Tags | Math, Physics | + | Name | Test badge with 'apostrophe' and other friends (<>&@#) | + | Version | v1 | + | Language | English | + | Description | Test badge description | + | Image author | http://author.example.com | + | Image caption | Test caption image | + | Tags | Math, Physics | + | Issuer contact | issuer@example.com | And I upload "badges/tests/behat/badge.png" file to "Image" filemanager When I press "Create badge" Then I should see "Edit details" @@ -42,11 +44,11 @@ Feature: Add badges to the system And I should see "Related badges (0)" And I should see "Alignments (0)" And I should not see "Create badge" - And I should not see "Issuer details" And I select "Overview" from the "jump" singleselect And I should see "Issuer details" And I should see "Test Badge Site" - And I should see "testuser@example.com" + And I should see "issuer@example.com" + And I should not see "testuser@example.com" And I should see "Tags" And I should see "Math" And I should see "Physics" @@ -172,37 +174,49 @@ Feature: Add badges to the system And I should see "Add a new badge" @javascript @_file_upload - Scenario: Edit a badge - Given I navigate to "Badges > Badges settings" in site administration - And I set the field "Badge issuer name" to "Test Badge Site" - And I set the field "Badge issuer email address" to "testuser@example.com" - And I press "Save changes" - And I navigate to "Badges > Add a new badge" in site administration + Scenario: Edit a site badge + Given the following "core_badges > Badge" exists: + | name | Site badge | + | status | inactive | + | version | 1 | + | language | ca | + | description | Test badge description | + | image | badges/tests/behat/badge.png | + | imageauthorurl | http://imtheauthor.example.com | + | imagecaption | My caption image | + | issuercontact | testuser@example.com | + And the following "core_badges > Criterias" exist: + | badge | role | + | Site badge | editingteacher | + And I navigate to "Badges > Manage badges" in site administration + When I press "Edit" action in the "Site badge" report row + And I should see "Site badge" + And the field "Issuer contact" matches value "testuser@example.com" And I set the following fields to these values: - | Name | Test badge with 'apostrophe' and other friends (<>&@#) | - | Version | firstversion | - | Language | English | - | Description | Test badge description | - | Image author | http://author.example.com | - | Image caption | Test caption image | - | Tags | Math, Physics | - And I upload "badges/tests/behat/badge.png" file to "Image" filemanager - And I press "Create badge" - When I select "Edit details" from the "jump" singleselect - And I should see "Test badge with 'apostrophe' and other friends (&@#)" - And I should not see "Issuer details" - And I should see "Math" - And I should see "Physics" - And I set the following fields to these values: - | Name | Test badge renamed | - | Version | secondversion | - | Tags | Math, History | + | Name | Test badge with 'apostrophe' and other friends (<>&@#) | + | Version | secondversion | + | Language | English | + | Description | Modified test badge description | + | Image author | http://author.example.com | + | Image caption | Test caption image | + | Tags | Math, History | + | Issuer contact | issuer@invalid.cat | And I press "Save changes" And I select "Overview" from the "jump" singleselect - Then I should not see "Test badge with 'apostrophe' and other friends (&@#)" - And I should not see "firstversion" - And I should not see "Math, Physics" - And I should see "Test badge renamed" + And I expand all fieldsets + Then I should see "Test badge with 'apostrophe' and other friends (&@#)" + And I should not see "Site badge" And I should see "secondversion" + And I should not see "firstversion" And I should see "Math" And I should see "History" + And I should see "issuer@invalid.cat" + And I should not see "testuser@example.com" + + Scenario: Default value for issuer name + When I navigate to "Badges > Add a new badge" in site administration + Then the field "Issuer name" matches value "Acceptance test site" + But the following config values are set as admin: + | badges_defaultissuername | Test Badge Site | + And I navigate to "Badges > Add a new badge" in site administration + And the field "Issuer name" matches value "Test Badge Site" diff --git a/lang/en/badges.php b/lang/en/badges.php index 83a5b9a0bf0..f1c90b58d2d 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -191,7 +191,7 @@ $string['configuremessage'] = 'Badge message'; $string['connect'] = 'Connect'; $string['connected'] = 'Connected'; $string['connecting'] = 'Connecting...'; -$string['contact'] = 'Contact'; +$string['contact'] = 'Issuer contact'; $string['contact_help'] = 'An email address associated with the badge issuer.'; $string['copy'] = 'Copy'; $string['copyof'] = 'Copy of {$a}';