[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/badges/classes/ -> assertion.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Badge assertion library.
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Open Badges Assertions specification 1.0 {@link https://github.com/mozilla/openbadges/wiki/Assertions}
  31   *
  32   * Badge asserion is defined by three parts:
  33   * - Badge Assertion (information regarding a specific badge that was awarded to a badge earner)
  34   * - Badge Class (general information about a badge and what it is intended to represent)
  35   * - Issuer Class (general information of an issuing organisation)
  36   */
  37  
  38  /**
  39   * Class that represents badge assertion.
  40   *
  41   */
  42  class core_badges_assertion {
  43      /** @var object Issued badge information from database */
  44      private $_data;
  45  
  46      /** @var moodle_url Issued badge url */
  47      private $_url;
  48  
  49      /**
  50       * Constructs with issued badge unique hash.
  51       *
  52       * @param string $hash Badge unique hash from badge_issued table.
  53       */
  54      public function __construct($hash) {
  55          global $DB;
  56  
  57          $this->_data = $DB->get_record_sql('
  58              SELECT
  59                  bi.dateissued,
  60                  bi.dateexpire,
  61                  bi.uniquehash,
  62                  u.email,
  63                  b.*,
  64                  bb.email as backpackemail
  65              FROM
  66                  {badge} b
  67                  JOIN {badge_issued} bi
  68                      ON b.id = bi.badgeid
  69                  JOIN {user} u
  70                      ON u.id = bi.userid
  71                  LEFT JOIN {badge_backpack} bb
  72                      ON bb.userid = bi.userid
  73              WHERE ' . $DB->sql_compare_text('bi.uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
  74              array('hash' => $hash), IGNORE_MISSING);
  75  
  76          $this->_url = new moodle_url('/badges/badge.php', array('hash' => $this->_data->uniquehash));
  77      }
  78  
  79      /**
  80       * Get badge assertion.
  81       *
  82       * @return array Badge assertion.
  83       */
  84      public function get_badge_assertion() {
  85          global $CFG;
  86          $assertion = array();
  87          if ($this->_data) {
  88              $hash = $this->_data->uniquehash;
  89              $email = empty($this->_data->backpackemail) ? $this->_data->email : $this->_data->backpackemail;
  90              $assertionurl = new moodle_url('/badges/assertion.php', array('b' => $hash));
  91              $classurl = new moodle_url('/badges/assertion.php', array('b' => $hash, 'action' => 1));
  92  
  93              // Required.
  94              $assertion['uid'] = $hash;
  95              $assertion['recipient'] = array();
  96              $assertion['recipient']['identity'] = 'sha256$' . hash('sha256', $email . $CFG->badges_badgesalt);
  97              $assertion['recipient']['type'] = 'email'; // Currently the only supported type.
  98              $assertion['recipient']['hashed'] = true; // We are always hashing recipient.
  99              $assertion['recipient']['salt'] = $CFG->badges_badgesalt;
 100              $assertion['badge'] = $classurl->out(false);
 101              $assertion['verify'] = array();
 102              $assertion['verify']['type'] = 'hosted'; // 'Signed' is not implemented yet.
 103              $assertion['verify']['url'] = $assertionurl->out(false);
 104              $assertion['issuedOn'] = $this->_data->dateissued;
 105              // Optional.
 106              $assertion['evidence'] = $this->_url->out(false); // Currently issued badge URL.
 107              if (!empty($this->_data->dateexpire)) {
 108                  $assertion['expires'] = $this->_data->dateexpire;
 109              }
 110          }
 111          return $assertion;
 112      }
 113  
 114      /**
 115       * Get badge class information.
 116       *
 117       * @return array Badge Class information.
 118       */
 119      public function get_badge_class() {
 120          $class = array();
 121          if ($this->_data) {
 122              if (empty($this->_data->courseid)) {
 123                  $context = context_system::instance();
 124              } else {
 125                  $context = context_course::instance($this->_data->courseid);
 126              }
 127              $issuerurl = new moodle_url('/badges/assertion.php', array('b' => $this->_data->uniquehash, 'action' => 0));
 128  
 129              // Required.
 130              $class['name'] = $this->_data->name;
 131              $class['description'] = $this->_data->description;
 132              $class['image'] = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $this->_data->id, '/', 'f1')->out(false);
 133              $class['criteria'] = $this->_url->out(false); // Currently issued badge URL.
 134              $class['issuer'] = $issuerurl->out(false);
 135          }
 136          return $class;
 137      }
 138  
 139      /**
 140       * Get badge issuer information.
 141       *
 142       * @return array Issuer information.
 143       */
 144      public function get_issuer() {
 145          $issuer = array();
 146          if ($this->_data) {
 147              // Required.
 148              $issuer['name'] = $this->_data->issuername;
 149              $issuer['url'] = $this->_data->issuerurl;
 150              // Optional.
 151              if (!empty($this->_data->issuercontact)) {
 152                  $issuer['email'] = $this->_data->issuercontact;
 153              }
 154          }
 155          return $issuer;
 156      }
 157  
 158  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1