From 88c31c7551f8f0368e5b76b2d3fc4f094ea1b990 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Thu, 18 Nov 2021 18:26:06 +0100 Subject: [PATCH] MDL-72141 badges: simplify external badges page Apart from reorganising the information displayed in the external badges page, this information has been moved to reuse the template created in MDL-71937, to make it easier to modify and maintain. --- badges/classes/output/external_badge.php | 85 +++++++++++++++++++-- badges/renderer.php | 97 +----------------------- badges/templates/issued_badge.mustache | 21 ++++- lang/en/badges.php | 12 ++- lang/en/deprecated.txt | 3 + 5 files changed, 112 insertions(+), 106 deletions(-) diff --git a/badges/classes/output/external_badge.php b/badges/classes/output/external_badge.php index 7fad503d1ee..f35d17f736f 100644 --- a/badges/classes/output/external_badge.php +++ b/badges/classes/output/external_badge.php @@ -31,6 +31,8 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir . '/badgeslib.php'); use renderable; +use renderer_base; +use stdClass; /** * An external badges for external.php page @@ -39,19 +41,19 @@ use renderable; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class external_badge implements renderable { - /** @var issued badge */ + /** @var stdClass Issued badge */ public $issued; - /** @var User ID */ + /** @var int User ID */ public $recipient; - /** @var validation of external badge */ + /** @var bool Validation of external badge */ public $valid = true; /** * Initializes the badge to display * - * @param object $badge External badge information. + * @param stdClass $badge External badge information. * @param int $recipient User id. */ public function __construct($badge, $recipient) { @@ -97,5 +99,78 @@ class external_badge implements renderable { $this->valid = false; } } -} + /** + * Export this data so it can be used as the context for a mustache template. + * + * @param renderer_base $output Renderer base. + * @return stdClass + */ + public function export_for_template(renderer_base $output): stdClass { + $data = new stdClass(); + + $now = time(); + if (isset($this->issued->assertion->expires)) { + if (!is_numeric($this->issued->assertion->expires)) { + $this->issued->assertion->expires = strtotime($this->issued->assertion->expires); + } + $expiration = $this->issued->assertion->expires; + } else { + $expiration = $now + 86400; + } + + // Field: Image. + if (isset($this->issued->imageUrl)) { + $this->issued->image = $this->issued->imageUrl; + } + $data->badgeimage = $this->issued->image; + + // Field: Expiration date. + if (isset($this->issued->assertion->expires)) { + if ($expiration < $now) { + $data->expireddate = $this->issued->assertion->expires; + $data->expireddateformatted = userdate( + $this->issued->assertion->expires, + get_string('strftimedatetime', 'langconfig') + ); + } else { + $data->expiredate = $this->issued->assertion->expires; + } + } + + // Fields: Name, description, issuedOn. + $data->badgename = $this->issued->assertion->badge->name; + $data->badgedescription = $this->issued->assertion->badge->description; + if (isset($this->issued->assertion->issued_on)) { + if (!is_numeric($this->issued->assertion->issued_on)) { + $this->issued->assertion->issued_on = strtotime($this->issued->assertion->issued_on); + } + $data->badgeissuedon = $this->issued->assertion->issued_on; + } + + // Field: Recipient (the badge was awarded to this person). + $data->recipientname = fullname($this->recipient); + if (!$this->valid) { + $data->recipientnotification = new stdClass(); + $data->recipientnotification->message = get_string('recipientvalidationproblem', 'badges'); + } + + // Field: Criteria. + if (isset($this->issued->assertion->badgeclass->criteria->narrative)) { + $data->criteria = $this->issued->assertion->badgeclass->criteria->narrative; + } + + // Field: Issuer. + $data->issuedby = $this->issued->issuer->name; + if (isset($this->issued->issuer->contact) && !empty($this->issued->issuer->contact)) { + $data->issuedbyemailobfuscated = obfuscate_mailto($this->issued->issuer->contact, $data->issuedby); + } + + // Field: Hosted URL. + if (isset($this->issued->hostedUrl) && !empty($this->issued->hostedUrl)) { + $data->hostedurl = $this->issued->hostedUrl; + } + + return $data; + } +} diff --git a/badges/renderer.php b/badges/renderer.php index cc79994797d..82d415fba0e 100644 --- a/badges/renderer.php +++ b/badges/renderer.php @@ -325,101 +325,8 @@ class core_badges_renderer extends plugin_renderer_base { * @return string */ protected function render_external_badge(\core_badges\output\external_badge $ibadge) { - $issued = $ibadge->issued; - $assertion = $issued->assertion; - $issuer = $assertion->badge->issuer; - $userinfo = $ibadge->recipient; - $table = new html_table(); - $today = strtotime(date('Y-m-d')); - - $output = ''; - $output .= html_writer::start_tag('div', array('id' => 'badge')); - $output .= html_writer::start_tag('div', array('id' => 'badge-image')); - if (isset($issued->imageUrl)) { - $issued->image = $issued->imageUrl; - } - $output .= html_writer::empty_tag('img', array('src' => $issued->image, 'width' => '100')); - if (isset($assertion->expires)) { - $expiration = is_numeric($assertion->expires) ? $assertion->expires : strtotime($assertion->expires); - if ($expiration < $today) { - $output .= $this->output->pix_icon('i/expired', - get_string('expireddate', 'badges', userdate($expiration)), - 'moodle', - array('class' => 'expireimage')); - } - } - $output .= html_writer::end_tag('div'); - - $output .= html_writer::start_tag('div', array('id' => 'badge-details')); - - // Recipient information. - $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3); - $dl = array(); - // Technically, we should alway have a user at this point, but added an extra check just in case. - if ($userinfo) { - if (!$ibadge->valid) { - $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice'); - $dl[get_string('name')] = fullname($userinfo) . $notify; - } else { - $dl[get_string('name')] = fullname($userinfo); - } - } else { - $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice'); - $dl[get_string('name')] = $notify; - } - $output .= $this->definition_list($dl); - - $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3); - $dl = array(); - $dl[get_string('issuername', 'badges')] = s($issuer->name); - if (isset($issuer->origin)) { - $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)); - } - - if (isset($issuer->contact)) { - $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact); - } - $output .= $this->definition_list($dl); - - $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3); - $dl = array(); - $dl[get_string('name')] = s($assertion->badge->name); - $dl[get_string('description', 'badges')] = s($assertion->badge->description); - if (isset($assertion->badge->criteria)) { - $dl[get_string('bcriteria', 'badges')] = html_writer::tag( - 'a', - s($assertion->badge->criteria), - array('href' => $assertion->badge->criteria) - ); - } - $output .= $this->definition_list($dl); - - $dl = array(); - if (isset($assertion->issued_on)) { - $issuedate = is_numeric($assertion->issued_on) ? $assertion->issued_on : strtotime($assertion->issued_on); - $dl[get_string('dateawarded', 'badges')] = userdate($issuedate); - } - if (isset($assertion->expires)) { - if ($expiration < $today) { - $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges'); - } else { - $dl[get_string('expirydate', 'badges')] = userdate($expiration); - } - } - if (isset($assertion->evidence)) { - $dl[get_string('evidence', 'badges')] = html_writer::tag( - 'a', - s($assertion->evidence), - array('href' => $assertion->evidence) - ); - } - if (!empty($dl)) { - $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3); - $output .= $this->definition_list($dl); - } - $output .= html_writer::end_tag('div'); - - return $output; + $data = $ibadge->export_for_template($this); + return parent::render_from_template('core_badges/issued_badge', $data); } /** diff --git a/badges/templates/issued_badge.mustache b/badges/templates/issued_badge.mustache index 3ca72166987..6d57d37814c 100644 --- a/badges/templates/issued_badge.mustache +++ b/badges/templates/issued_badge.mustache @@ -30,6 +30,7 @@ * badgedescription - Badge description. * badgeissuedon - Date where the badge was issued on by the user. * recipientname - User awarded with the badge. + * recipientnotification.message - Message to be displayed if there is some issue with the recipient. * criteria - HTML code with the criteria to display. * issuedby - Badge issuer. * issuedbyemailobfuscated - Badge issuer email link obfuscated. @@ -45,6 +46,7 @@ * relatedbadges - Array of related badges (if hasrelatedbadges is set to true). * hasalignments - Whether the badge has alignments or not. * alignments - Array of alignments (if hasalignments is set to true). + * hostedurl - The URL where the badge is hosted [optional]. Example context (json): { @@ -55,7 +57,10 @@ "badgedescription":"This badge is awarded to people who have provided outstanding support to other participants in the MOOC", "badgeissuedon": 1625491897, "recipientname": "Judit Cortes", - "criteria": "Complete ALL of the listed requirements.;", + "recipientnotification": { + "message": "This user cannot be verified as a recipient of this badge." + }, + "criteria": "Complete ALL of the listed requirements.", "issuedby": "Moodle HQ", "issuedbyemailobfuscated": "Moodle HQ", "hasotherfields": true, @@ -106,7 +111,8 @@ "targetframework": "Framework name", "targetcode": "A1001" } - ] + ], + "hostedurl": "http://externalbackpack/badge?id=ABC" } }} @@ -134,6 +140,9 @@

{{badgename}}

+ {{#recipientnotification}} + {{> core/notification_warning}} + {{/recipientnotification}} {{#str}}awardedto, core_badges, {{recipientname}}{{/str}}
@@ -179,6 +188,14 @@
{{/issuedby}} + {{#hostedurl}} +
+ + {{#str}}hostedurl, core_badges{{/str}} + +
+ {{/hostedurl}} + {{#coursefullname}}
{{#str}} diff --git a/lang/en/badges.php b/lang/en/badges.php index 0524cbb04cc..499d1ed47b6 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -338,7 +338,6 @@ $string['eventbadgelistingviewed'] = 'Badge listing viewed'; $string['eventbadgerevoked'] = 'Badge revoked'; $string['eventbadgeupdated'] = 'Badge updated'; $string['eventbadgeviewed'] = 'Badge viewed'; -$string['evidence'] = 'Evidence'; $string['existingrecipients'] = 'Existing badge recipients'; $string['expired'] = 'Expired'; $string['expiredate'] = 'This badge expires on {$a}.'; @@ -359,6 +358,8 @@ $string['externalbadges_help'] = 'This area displays badges from your external b $string['fixed'] = 'Fixed date'; $string['hidden'] = 'Hidden'; $string['hiddenbadge'] = 'Unfortunately, the badge owner has not made this information available.'; +$string['hostedurl'] = 'External URL'; +$string['hostedurldescription'] = 'External URL where the badge is hosted'; $string['imageauthoremail'] = 'Image author\'s email'; $string['imageauthoremail_help'] = 'If specified, the email address of the badge image author is displayed on the badge page.'; $string['imageauthorname'] = 'Image author\'s name'; @@ -492,9 +493,7 @@ $string['privacy:metadata:manualaward:issuerid'] = 'The ID of the user awarding $string['privacy:metadata:manualaward:issuerrole'] = 'The role of the user awarding the badge'; $string['privacy:metadata:manualaward:recipientid'] = 'The ID of the user who is manually awarded a badge'; $string['recipients'] = 'Badge recipients'; -$string['recipientdetails'] = 'Recipient details'; -$string['recipientidentificationproblem'] = 'Cannot find a recipient of this badge among the existing users.'; -$string['recipientvalidationproblem'] = 'Current user cannot be verified as a recipient of this badge.'; +$string['recipientvalidationproblem'] = 'This user cannot be verified as a recipient of this badge.'; $string['relative'] = 'Relative date'; $string['relatedbages'] = 'Related badges'; $string['revoke'] = 'Revoke badge'; @@ -588,3 +587,8 @@ $string['error:backpacknotavailable'] = 'Your site is not accessible from the In $string['error:backpackproblem'] = 'There was a problem connecting to your backpack service provider. Please try again later.'; $string['sitebackpack'] = 'Active external backpack'; $string['sitebackpack_help'] = 'The external backpack that users can connect to from this site. Note that changing this setting after users have connected their backpacks will require each user to go to their backpack settings page and disconnect then reconnect.'; + +// Deprecated since Moodle 4.0. +$string['evidence'] = 'Evidence'; +$string['recipientdetails'] = 'Recipient details'; +$string['recipientidentificationproblem'] = 'Cannot find a recipient of this badge among the existing users.'; diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index d38721c0e5b..75e55cf10ca 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -163,3 +163,6 @@ mediapluginswf,core_admin mediapluginswfnote,core_admin createuserandpass,core supplyinfo,core +evidence,core_badges +recipientdetails,core_badges +recipientidentificationproblem,core_badges