Merge branch 'MDL-41659_master' of https://github.com/totara/openbadges
[moodle.git] / badges / renderer.php
index 7946f50..2fa2614 100644 (file)
@@ -282,24 +282,24 @@ class core_badges_renderer extends plugin_renderer_base {
         global $USER, $CFG, $DB;
         $issued = $ibadge->issued;
         $userinfo = $ibadge->recipient;
+        $badgeclass = $ibadge->badgeclass;
         $badge = new badge($ibadge->badgeid);
-        $today_date = date('Y-m-d');
-        $today = strtotime($today_date);
+        $now = time();
 
         $table = new html_table();
         $table->id = 'issued-badge-table';
 
         $imagetable = new html_table();
         $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
-        $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued['badge']['image'])));
+        $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $badgeclass['image'])));
         if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
             $imagetable->data[] = array($this->output->single_button(
-                        new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
+                        new moodle_url('/badges/badge.php', array('hash' => $issued['uid'], 'bake' => true)),
                         get_string('download'),
                         'POST'));
-            $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1;
-            if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today) && badges_user_has_backpack($USER->id)) {
-                $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
+            $expiration = isset($issued['expires']) ? $issued['expires'] : $now + 86400;
+            if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now) && badges_user_has_backpack($USER->id)) {
+                $assertion = new moodle_url('/badges/assertion.php', array('b' => $issued['uid']));
                 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
                 $attributes = array(
                         'type'  => 'button',
@@ -339,24 +339,23 @@ class core_badges_renderer extends plugin_renderer_base {
 
         $datatable->data[] = array(get_string('bcriteria', 'badges'), self::print_badge_criteria($badge));
         $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
-        $datatable->data[] = array(get_string('dateawarded', 'badges'), $issued['issued_on']);
+        $datatable->data[] = array(get_string('dateawarded', 'badges'), userdate($issued['issuedOn']));
         if (isset($issued['expires'])) {
-            $expiration = strtotime($issued['expires']);
-            if ($expiration < $today) {
-                $cell = new html_table_cell($issued['expires'] . get_string('warnexpired', 'badges'));
+            if ($issued['expires'] < $now) {
+                $cell = new html_table_cell(userdate($issued['expires']) . get_string('warnexpired', 'badges'));
                 $cell->attributes = array('class' => 'notifyproblem warning');
                 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
 
                 $image = html_writer::start_tag('div', array('class' => 'badge'));
-                $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
+                $image .= html_writer::empty_tag('img', array('src' => $badgeclass['image']));
                 $image .= $this->output->pix_icon('i/expired',
-                                get_string('expireddate', 'badges', $issued['expires']),
+                                get_string('expireddate', 'badges', userdate($issued['expires'])),
                                 'moodle',
                                 array('class' => 'expireimage'));
                 $image .= html_writer::end_tag('div');
                 $imagetable->data[0] = array($image);
             } else {
-                $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
+                $datatable->data[] = array(get_string('expirydate', 'badges'), userdate($issued['expires']));
             }
         }
 
@@ -909,15 +908,15 @@ class issued_badge implements renderable {
     /** @var badge recipient */
     public $recipient;
 
+    /** @var badge class */
+    public $badgeclass;
+
     /** @var badge visibility to others */
     public $visible = 0;
 
     /** @var badge class */
     public $badgeid = 0;
 
-    /** @var issued badge unique hash */
-    public $hash = "";
-
     /**
      * Initializes the badge to display
      *
@@ -925,8 +924,10 @@ class issued_badge implements renderable {
      */
     public function __construct($hash) {
         global $DB;
-        $this->issued = badges_get_issued_badge_info($hash);
-        $this->hash = $hash;
+
+        $assertion = new core_badges_assertion($hash);
+        $this->issued = $assertion->get_badge_assertion();
+        $this->badgeclass = $assertion->get_badge_class();
 
         $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
                 FROM {badge_issued}