Skip to content

Commit

Permalink
MDL-72354 badges: Show always issuer details when creating badges
Browse files Browse the repository at this point in the history
Co-author: Huynh Nguyen
  • Loading branch information
Huynh authored and sarjona committed Jul 19, 2024
1 parent 554a790 commit 879901e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 94 deletions.
30 changes: 6 additions & 24 deletions badges/classes/badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,6 @@ public function __construct($badgeid) {
}
}

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();
}

Expand Down Expand Up @@ -1020,17 +1012,9 @@ public static function create_badge(stdClass $data, ?int $courseid = null): badg
$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;
Expand Down Expand Up @@ -1087,11 +1071,9 @@ public function update(stdClass $data): bool {
$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;

Expand Down
47 changes: 20 additions & 27 deletions badges/classes/form/badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,29 +97,28 @@ public function definition() {
$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'));

Expand Down Expand Up @@ -211,12 +210,6 @@ public function validation($data, $files) {

$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');
}
Expand Down
5 changes: 5 additions & 0 deletions badges/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public function print_badge_overview($badge, $context) {
$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.
Expand Down
98 changes: 56 additions & 42 deletions badges/tests/behat/add_badge.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
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 "[email protected]"
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"
Expand All @@ -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 "[email protected]"
And I should see "[email protected]"
And I should not see "[email protected]"
And I should see "Tags"
And I should see "Math"
And I should see "Physics"
Expand Down Expand Up @@ -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 "[email protected]"
And I press "Save changes"
And I navigate to "Badges > Add a new badge" in site administration
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"
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 "[email protected]"
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 "[email protected]"
And I should not see "[email protected]"

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"
2 changes: 1 addition & 1 deletion lang/en/badges.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
$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}';
Expand Down

0 comments on commit 879901e

Please sign in to comment.