first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,75 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Issued badge renderable.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
/**
* Link to external resources this badge is aligned with.
*
* @copyright 2018 Tung Thai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
*/
class badge_alignments implements renderable {
/** @var string how are the data sorted. */
public $sort = 'name';
/** @var string how are the data sorted. */
public $dir = 'ASC';
/** @var int page number to display. */
public $page = 0;
/** @var int number of badges to display per page. */
public $perpage = BADGE_PERPAGE;
/** @var int the total number of badges to display. */
public $totalcount = null;
/** @var array list of badges. */
public $alignments = array();
/** @var array list of badges. */
public $currentbadgeid = 0;
/**
* Initializes the list of alignments to display.
*
* @param array $alignments List alignments to render.
* @param int $currentbadgeid ID current badge.
*/
public function __construct($alignments, $currentbadgeid) {
$this->alignments = $alignments;
$this->currentbadgeid = $currentbadgeid;
}
}
@@ -0,0 +1,70 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Issued badge renderable.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
/**
* Collection of all badges for view.php page
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badge_collection implements renderable {
/** @var string how are the data sorted */
public $sort = 'name';
/** @var string how are the data sorted */
public $dir = 'ASC';
/** @var int page number to display */
public $page = 0;
/** @var int number of badges to display per page */
public $perpage = BADGE_PERPAGE;
/** @var int the total number of badges to display */
public $totalcount = null;
/** @var array list of badges */
public $badges = array();
/**
* Initializes the list of badges to display
*
* @param array $badges Badges to render
*/
public function __construct($badges) {
$this->badges = $badges;
}
}
@@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Issued badge renderable.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
/**
* Collection of badges used at the index.php page
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badge_management extends badge_collection implements renderable {
}
@@ -0,0 +1,70 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Issued badge renderable.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
/**
* Badge recipients rendering class
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badge_recipients implements renderable {
/** @var string how are the data sorted */
public $sort = 'lastname';
/** @var string how are the data sorted */
public $dir = 'ASC';
/** @var int page number to display */
public $page = 0;
/** @var int number of badge recipients to display per page */
public $perpage = 30;
/** @var int the total number or badge recipients to display */
public $totalcount = null;
/** @var array internal list of badge recipients ids */
public $userids = array();
/**
* Initializes the list of users to display
*
* @param array $holders List of badge holders
*/
public function __construct($holders) {
$this->userids = $holders;
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Collection of all related badges.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
/**
* Collection of all related badges.
*
* @copyright 2018 Tung Thai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
*/
class badge_related implements renderable {
/** @var string how are the data sorted. */
public $sort = 'name';
/** @var string how are the data sorted. */
public $dir = 'ASC';
/** @var int page number to display. */
public $page = 0;
/** @var int number of badges to display per page. */
public $perpage = BADGE_PERPAGE;
/** @var int the total number of badges to display. */
public $totalcount = null;
/** @var int the current badge. */
public $currentbadgeid = 0;
/** @var array list of badges. */
public $badges = array();
/**
* Initializes the list of badges to display.
*
* @param array $badges related badges to render.
* @param int $currentbadgeid ID current badge.
*/
public function __construct($badges, $currentbadgeid) {
$this->badges = $badges;
$this->currentbadgeid = $currentbadgeid;
}
}
@@ -0,0 +1,63 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Collection of use badges.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
/**
* Collection of user badges used at the mybadges.php page
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badge_user_collection extends badge_collection implements renderable {
/** @var array backpack settings */
public $backpack = null;
/** @var string search */
public $search = '';
/**
* Initializes user badge collection.
*
* @param array $badges Badges to render
* @param int $userid Badges owner
*/
public function __construct($badges, $userid) {
global $CFG;
parent::__construct($badges);
if (!empty($CFG->badges_allowexternalbackpack)) {
$this->backpack = get_backpack_settings($userid, true);
}
}
}
+180
View File
@@ -0,0 +1,180 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use coding_exception;
use context_course;
use stdClass;
use renderable;
use core_badges\badge;
use moodle_url;
use renderer_base;
/**
* Page to display badge information, such as name, description or criteria. This information is unrelated to assertions.
*
* @package core_badges
* @copyright 2022 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badgeclass implements renderable {
/** @var badge class */
public $badge;
/** @var badge class */
public $badgeid = 0;
/** @var \context The badge context*/
public $context;
/**
* Initializes the badge to display.
*
* @param int $id Id of the badge to display.
*/
public function __construct(int $id) {
$this->badgeid = $id;
$this->badge = new badge($this->badgeid);
if ($this->badge->status == BADGE_STATUS_INACTIVE) {
// Inactive badges that haven't been published previously can't be displayed.
$this->badge = null;
} else {
$this->context = $this->badge->get_context();
}
}
/**
* 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 {
global $DB, $SITE;
$data = new stdClass();
if ($this->context instanceof context_course) {
$data->coursefullname = format_string($DB->get_field('course', 'fullname', ['id' => $this->badge->courseid]),
true, ['context' => $this->context]);
} else {
$data->sitefullname = format_string($SITE->fullname, true, ['context' => $this->context]);
}
// Field: Image.
$storage = get_file_storage();
$imagefile = $storage->get_file($this->context->id, 'badges', 'badgeimage', $this->badgeid, '/', 'f3.png');
if ($imagefile) {
$imagedata = base64_encode($imagefile->get_content());
} else {
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
// Unit tests the file might not exist yet.
$imagedata = '';
} else {
throw new coding_exception('Image file does not exist.');
}
}
$data->badgeimage = 'data:image/png;base64,' . $imagedata;
// Fields: Name, description.
$data->badgename = $this->badge->name;
$data->badgedescription = $this->badge->description;
// Field: Criteria.
// This method will return the HTML with the badge criteria.
$data->criteria = $output->print_badge_criteria($this->badge);
// Field: Issuer.
$data->issuedby = format_string($this->badge->issuername, true, ['context' => $this->context]);
if (isset($this->badge->issuercontact) && !empty($this->badge->issuercontact)) {
$data->issuedbyemailobfuscated = obfuscate_mailto($this->badge->issuercontact, $data->issuedby);
}
// Fields: Other details, such as language or version.
$data->hasotherfields = false;
if (!empty($this->badge->language)) {
$data->hasotherfields = true;
$languages = get_string_manager()->get_list_of_languages();
$data->language = $languages[$this->badge->language];
}
if (!empty($this->badge->version)) {
$data->hasotherfields = true;
$data->version = $this->badge->version;
}
if (!empty($this->badge->imageauthorname)) {
$data->hasotherfields = true;
$data->imageauthorname = $this->badge->imageauthorname;
}
if (!empty($this->badge->imageauthoremail)) {
$data->hasotherfields = true;
$data->imageauthoremail = obfuscate_mailto($this->badge->imageauthoremail, $this->badge->imageauthoremail);
}
if (!empty($this->badge->imageauthorurl)) {
$data->hasotherfields = true;
$data->imageauthorurl = $this->badge->imageauthorurl;
}
if (!empty($this->badge->imagecaption)) {
$data->hasotherfields = true;
$data->imagecaption = $this->badge->imagecaption;
}
// Field: Endorsement.
$endorsement = $this->badge->get_endorsement();
if (!empty($endorsement)) {
$data->hasotherfields = true;
$endorsement = $this->badge->get_endorsement();
$endorsement->issueremail = obfuscate_mailto($endorsement->issueremail, $endorsement->issueremail);
$data->endorsement = (array) $endorsement;
}
// Field: Related badges.
$relatedbadges = $this->badge->get_related_badges(true);
if (!empty($relatedbadges)) {
$data->hasotherfields = true;
$data->hasrelatedbadges = true;
$data->relatedbadges = [];
foreach ($relatedbadges as $related) {
if (isloggedin() && !is_guest($this->context)) {
$related->url = (new moodle_url('/badges/overview.php', ['id' => $related->id]))->out(false);
}
$data->relatedbadges[] = (array)$related;
}
}
// Field: Alignments.
$alignments = $this->badge->get_alignments();
if (!empty($alignments)) {
$data->hasotherfields = true;
$data->hasalignments = true;
$data->alignments = [];
foreach ($alignments as $alignment) {
$data->alignments[] = (array)$alignment;
}
}
// Field: Tags.
$tags = \core_tag_tag::get_item_tags('core_badges', 'badge', $this->badgeid);
$taglist = new \core_tag\output\taglist($tags);
$data->badgetag = $taglist->export_for_template($output);
return $data;
}
}
+103
View File
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\output;
use renderable;
use renderer_base;
use moodle_page;
use navigation_node;
use templatable;
/**
* Abstract class for the badges tertiary navigation. The class initialises the page and type class variables.
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_action_bar implements renderable, templatable {
/** @var moodle_page $page The context we are operating within. */
protected $page;
/** @var int $type The badge type. */
protected $type;
/**
* standard_action_bar constructor.
*
* @param moodle_page $page
* @param int $type
*/
public function __construct(moodle_page $page, int $type) {
$this->type = $type;
$this->page = $page;
}
/**
* The template that this tertiary nav should use.
*
* @return string
*/
abstract public function get_template(): string;
/**
* Gets additional third party navigation nodes for display.
*
* @param renderer_base $output The output
* @return array All that sweet third party navigation action.
*/
public function get_third_party_nav_action(renderer_base $output): array {
$badgenode = $this->page->settingsnav->find('coursebadges', navigation_node::TYPE_CONTAINER);
if (!$badgenode) {
return [];
}
$leftovernodes = [];
foreach ($badgenode->children as $key => $value) {
if (array_search($value->key, $this->expected_items()) === false) {
$leftovernodes[] = $value;
}
}
$result = \core\navigation\views\secondary::create_menu_element($leftovernodes);
if ($result == false) {
return [];
} else {
$data ['thirdpartybutton'] = true;
if (count($result) == 1) {
// Return a button.
$link = key($result);
$text = current($result);
$data['thirdpartynodes'] = ['link' => $link, 'text' => $text];
} else {
// Return a url_select.
$selectobject = new \url_select($result, $this->page->url, get_string('othernavigation', 'badges'));
$data['thirdpartynodes'] = $selectobject->export_for_template($output);
$data['thirdpartybutton'] = false;
}
}
return $data;
}
/**
* Expected navigation node keys for badges.
*
* @return array default badge navigation node keys.
*/
protected function expected_items(): array {
return ['coursebadges', 'newbadge'];
}
}
@@ -0,0 +1,87 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Manage enabled backpacks for the site.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use core_badges\external\backpack_exporter;
/**
* Manage enabled backpacks renderable.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external_backpacks_page implements \renderable {
/** @var \moodle_url Badges backpacks URL. */
protected $url;
/** @var array List the backpacks at site level. */
protected $backpacks = [];
/**
* Constructor.
* @param \moodle_url $url
*/
public function __construct(\moodle_url $url) {
$this->url = $url;
$this->backpacks = badges_get_site_backpacks();
}
/**
* 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) {
global $PAGE;
$rownumber = 0;
$rowcount = count($this->backpacks);
$data = new \stdClass();
$data->baseurl = $this->url;
$data->backpacks = array();
$data->sesskey = sesskey();
foreach ($this->backpacks as $backpack) {
$exporter = new backpack_exporter($backpack);
$backpack = $exporter->export($output);
$backpack->cantest = ($backpack->apiversion == OPEN_BADGES_V2);
$backpack->canmoveup = $rownumber > 0;
$backpack->canmovedown = $rownumber < $rowcount - 1;
$data->backpacks[] = $backpack;
$rownumber++;
}
return $data;
}
}
@@ -0,0 +1,84 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* List of enabled backpacks for the site.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/tablelib.php');
require_once($CFG->libdir . '/badgeslib.php');
use html_writer;
use moodle_url;
use table_sql;
/**
* Backpacks table class.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external_backpacks_table extends table_sql {
/**
* Sets up the table.
*/
public function __construct() {
parent::__construct('backpacks');
$context = \context_system::instance();
// This object should not be used without the right permissions.
require_capability('moodle/badges:manageglobalsettings', $context);
// Define columns in the table.
$this->define_table_columns();
// Define configs.
$this->define_table_configs();
}
/**
* Setup the headers for the table.
*/
protected function define_table_columns() {
$cols = [
'backpackweburl' => get_string('backpackurl', 'core_badges'),
'sortorder' => '',
];
$this->define_columns(array_keys($cols));
$this->define_headers(array_values($cols));
}
/**
* Define table configs.
*/
protected function define_table_configs() {
$this->collapsible(false);
$this->sortable(false);
$this->pageable(false);
}
}
+187
View File
@@ -0,0 +1,187 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External badge renderable.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use renderable;
use renderer_base;
use stdClass;
/**
* An external badges for external.php page
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external_badge implements renderable {
/** @var stdClass Issued badge */
public $issued;
/** @var int User ID */
public $recipient;
/** @var bool Validation of external badge */
public $valid = true;
/**
* Initializes the badge to display
*
* @param stdClass $badge External badge information.
* @param int $recipient User id.
*/
public function __construct($badge, $recipient) {
global $DB;
// At this point a user has connected a backpack. So, we are going to get
// their backpack email rather than their account email.
$userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$user = $DB->get_record_sql("SELECT {$namefields}, b.email
FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
WHERE b.userid = :userid", array('userid' => $recipient), IGNORE_MISSING);
$this->issued = $badge;
$this->recipient = $user;
// Check if recipient is valid.
// There is no way to be 100% sure that a badge belongs to a user.
// Backpack does not return any recipient information.
// All we can do is compare that backpack email hashed using salt
// provided in the assertion matches a badge recipient from the assertion.
if ($user) {
if (isset($badge->assertion->recipient->identity)) {
$badge->assertion->salt = $badge->assertion->recipient->salt;
$badge->assertion->recipient = $badge->assertion->recipient->identity;
}
// Open Badges V2 does not even include a recipient.
if (!isset($badge->assertion->recipient)) {
$this->valid = false;
} else if (validate_email($badge->assertion->recipient) && $badge->assertion->recipient == $user->email) {
// If we have email, compare emails.
$this->valid = true;
} else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email)) {
// If recipient is hashed, but no salt, compare hashes without salt.
$this->valid = true;
} else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email . $badge->assertion->salt)) {
// If recipient is hashed, compare hashes.
$this->valid = true;
} else {
// Otherwise, we cannot be sure that this user is a recipient.
$this->valid = false;
}
} else {
$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;
if (is_object($data->badgeimage)) {
if (!empty($data->badgeimage->author)) {
$data->hasotherfields = true;
$data->imageauthorname = $data->badgeimage->author;
}
if (!empty($data->badgeimage->caption)) {
$data->hasotherfields = true;
$data->imagecaption = $data->badgeimage->caption;
}
$data->badgeimage = $data->badgeimage->id;
}
// 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;
}
}
+252
View File
@@ -0,0 +1,252 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Issued badge renderable.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/badgeslib.php');
use context_course;
use context_system;
use stdClass;
use renderable;
use core_badges\badge;
use moodle_url;
use renderer_base;
/**
* An issued badges for badge.php page
*
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class issued_badge implements renderable {
/** @var issued badge */
public $issued;
/** @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 unique hash identifying the issued badge */
public $hash;
/**
* Initializes the badge to display
*
* @param string $hash Issued badge hash
*/
public function __construct($hash) {
global $DB;
$this->hash = $hash;
$assertion = new \core_badges_assertion($hash, badges_open_badges_backpack_api());
$this->issued = $assertion->get_badge_assertion();
if (!is_numeric($this->issued['issuedOn'])) {
$this->issued['issuedOn'] = strtotime($this->issued['issuedOn']);
}
$this->badgeclass = $assertion->get_badge_class();
$rec = $DB->get_record_sql('SELECT userid, visible, badgeid
FROM {badge_issued}
WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
array('hash' => $hash), IGNORE_MISSING);
if ($rec) {
// Get a recipient from database.
$userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid));
$this->recipient = $user;
$this->visible = $rec->visible;
$this->badgeid = $rec->badgeid;
}
}
/**
* 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 {
global $CFG, $DB, $SITE, $USER;
$now = time();
if (isset($this->issued['expires'])) {
if (!is_numeric($this->issued['expires'])) {
$this->issued['expires'] = strtotime($this->issued['expires']);
}
$expiration = $this->issued['expires'];
} else {
$expiration = $now + 86400;
}
$context = null;
$data = new stdClass();
$badge = new badge($this->badgeid);
if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
$context = context_course::instance($badge->courseid);
$data->coursefullname = format_string($DB->get_field('course', 'fullname', ['id' => $badge->courseid]),
true, ['context' => $context]);
} else {
$context = context_system::instance();
$data->sitefullname = format_string($SITE->fullname, true, ['context' => $context]);
}
// Field: Image.
$data->badgeimage = is_array($this->badgeclass['image']) ? $this->badgeclass['image']['id'] : $this->badgeclass['image'];
// Field: Expiration date.
if (isset($this->issued['expires'])) {
if ($expiration < $now) {
$data->expireddate = $this->issued['expires'];
$data->expireddateformatted = userdate($this->issued['expires'], get_string('strftimedatetime', 'langconfig'));
} else {
$data->expiredate = $this->issued['expires'];
}
}
// Fields: Name, description, issuedOn.
$data->badgename = $badge->name;
$data->badgedescription = $badge->description;
$data->badgeissuedon = $this->issued['issuedOn'];
// Field: Recipient (the badge was awarded to this person).
if ($this->recipient->deleted) {
$strdata = new stdClass();
$strdata->user = fullname($this->recipient);
$strdata->site = format_string($SITE->fullname, true, ['context' => context_system::instance()]);
$data->recipientname = get_string('error:userdeleted', 'badges', $strdata);
} else {
$data->recipientname = fullname($this->recipient);
}
// Field: Criteria.
// This method will return the HTML with the badge criteria.
$data->criteria = $output->print_badge_criteria($badge);
// Field: Issuer.
$data->issuedby = format_string($badge->issuername, true, ['context' => $context]);
if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
$data->issuedbyemailobfuscated = obfuscate_mailto($badge->issuercontact, $data->issuedby);
}
// Fields: Other details, such as language or version.
$data->hasotherfields = false;
if (!empty($badge->language)) {
$data->hasotherfields = true;
$languages = get_string_manager()->get_list_of_languages();
$data->language = $languages[$badge->language];
}
if (!empty($badge->version)) {
$data->hasotherfields = true;
$data->version = $badge->version;
}
if (!empty($badge->imageauthorname)) {
$data->hasotherfields = true;
$data->imageauthorname = $badge->imageauthorname;
}
if (!empty($badge->imageauthoremail)) {
$data->hasotherfields = true;
$data->imageauthoremail = obfuscate_mailto($badge->imageauthoremail, $badge->imageauthoremail);
}
if (!empty($badge->imageauthorurl)) {
$data->hasotherfields = true;
$data->imageauthorurl = $badge->imageauthorurl;
}
if (!empty($badge->imagecaption)) {
$data->hasotherfields = true;
$data->imagecaption = $badge->imagecaption;
}
// Field: Endorsement.
$endorsement = $badge->get_endorsement();
if (!empty($endorsement)) {
$data->hasotherfields = true;
$endorsement = $badge->get_endorsement();
$endorsement->issueremail = obfuscate_mailto($endorsement->issueremail, $endorsement->issueremail);
$data->endorsement = (array) $endorsement;
}
// Field: Related badges.
$relatedbadges = $badge->get_related_badges(true);
if (!empty($relatedbadges)) {
$data->hasotherfields = true;
$data->hasrelatedbadges = true;
$data->relatedbadges = [];
foreach ($relatedbadges as $related) {
if (isloggedin() && ($context instanceof context_course && !is_guest($context))) {
$related->url = (new moodle_url('/badges/overview.php', ['id' => $related->id]))->out(false);
}
$data->relatedbadges[] = (array)$related;
}
}
// Field: Alignments.
$alignments = $badge->get_alignments();
if (!empty($alignments)) {
$data->hasotherfields = true;
$data->hasalignments = true;
$data->alignments = [];
foreach ($alignments as $alignment) {
$data->alignments[] = (array)$alignment;
}
}
// Buttons to display.
if ($USER->id == $this->recipient->id && !empty($CFG->enablebadges)) {
$data->downloadurl = (new moodle_url('/badges/badge.php', ['hash' => $this->hash, 'bake' => true]))->out(false);
if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now)
&& $userbackpack = badges_get_user_backpack($USER->id)) {
if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2P1) {
$addtobackpackurl = new moodle_url('/badges/backpack-export.php', ['hash' => $this->hash]);
} else {
$addtobackpackurl = new moodle_url('/badges/backpack-add.php', ['hash' => $this->hash]);
}
$data->addtobackpackurl = $addtobackpackurl->out(false);
}
}
// Field: Tags.
$tags = \core_tag_tag::get_item_tags('core_badges', 'badge', $this->badgeid);
$taglist = new \core_tag\output\taglist($tags);
$data->badgetag = $taglist->export_for_template($output);
return $data;
}
}
@@ -0,0 +1,173 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\output;
use core_badges\badge;
use moodle_url;
use renderer_base;
use single_button;
use moodle_page;
use url_select;
/**
* Class manage_badge_action_bar - Display the action bar
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manage_badge_action_bar extends base_action_bar {
/** @var badge $badge The badge we are managing. */
protected $badge;
/**
* manage_badge_action_bar constructor
*
* @param badge $badge The badge we are viewing
* @param moodle_page $page The page object
*/
public function __construct(badge $badge, moodle_page $page) {
parent::__construct($page, $badge->type);
$this->badge = $badge;
}
/**
* The template that this tertiary nav should use.
*
* @return string
*/
public function get_template(): string {
return 'core_badges/manage_badge';
}
/**
* Export the action bar
*
* @param renderer_base $output
* @return array
*/
public function export_for_template(renderer_base $output): array {
$elements = [];
$params = ['type' => $this->type];
if ($this->page->context->contextlevel == CONTEXT_COURSE) {
$params['id'] = $this->page->context->instanceid;
}
$elements['button'] = new single_button(new moodle_url('/badges/index.php', $params), get_string('back'), 'get');
$elements['urlselect'] = new url_select($this->generate_badge_navigation(), $this->page->url->out(false), null);
foreach ($elements as $key => $element) {
$elements[$key] = $element->export_for_template($output);
}
$additional = $this->get_third_party_nav_action($output);
$elements += $additional ?: [];
return $elements;
}
/**
* Returns a multi dimensional array of the links that should be displayed when creating a badge.
* The keys of the array feed into the text shown to the user and content of each element contain the following:
* - url URL for the option
* - additionalparams Additional params to feed into the url
* - capability The capabilities to check that governs visibility
* @return array
*/
protected function get_badge_administration_mapping_construct(): array {
return [
'boverview' => [
'url' => '/badges/overview.php',
'capability' => ''
],
'bdetails' => [
'url' => '/badges/edit.php',
'additionalparams' => ['action' => 'badge'],
'capability' => 'moodle/badges:configuredetails'
],
'bcriteria' => [
'url' => '/badges/criteria.php',
'capability' => 'moodle/badges:configurecriteria'
],
'bmessage' => [
'url' => '/badges/edit.php',
'additionalparams' => ['action' => 'message'],
'capability' => 'moodle/badges:configuremessages'
],
'bawards' => [
'url' => '/badges/recipients.php',
'capability' => 'moodle/badges:viewawarded'
],
'bendorsement' => [
'url' => '/badges/endorsement.php',
'capability' => 'moodle/badges:configuredetails'
],
'brelated' => [
'url' => '/badges/related.php',
'capability' => 'moodle/badges:configuredetails'
],
'balignment' => [
'url' => '/badges/alignment.php',
'capability' => 'moodle/badges:configuredetails'
],
];
}
/**
* Generate the options to be displayed when editing a badge. This feeds into a URL select which will be displayed
* in the tertiary navigation.
*
* @return array
*/
protected function generate_badge_navigation(): array {
global $DB;
$params = ['id' => $this->badge->id];
$options = [];
$construct = $this->get_badge_administration_mapping_construct();
foreach ($construct as $stringidentifier => $checks) {
if ($checks['capability'] && !has_capability($checks['capability'], $this->page->context)) {
continue;
}
$sql = '';
switch ($stringidentifier) {
case 'bawards':
$sql = "SELECT COUNT(b.userid)
FROM {badge_issued} b
INNER JOIN {user} u ON b.userid = u.id
WHERE b.badgeid = :badgeid AND u.deleted = 0";
break;
case 'brelated':
$sql = "SELECT COUNT(br.badgeid)
FROM {badge_related} br
WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
break;
case 'balignment':
$sql = "SELECT COUNT(bc.id)
FROM {badge_alignment} bc
WHERE bc.badgeid = :badgeid";
break;
}
$content = null;
if ($sql) {
$content = $DB->count_records_sql($sql, ['badgeid' => $this->badge->id, 'badgeid2' => $this->badge->id]);
}
$url = new moodle_url($checks['url'], $params + ($checks['additionalparams'] ?? []));
$options[get_string($stringidentifier, 'core_badges', $content)] = $url->out(false);
}
return array_flip($options);
}
}
@@ -0,0 +1,61 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\output;
use moodle_url;
use renderer_base;
use single_button;
/**
* Class recipients_action_bar - Display the action bar
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class recipients_action_bar extends manage_badge_action_bar {
/**
* The template that this tertiary nav should use.
*
* @return string
*/
public function get_template(): string {
return 'core_badges/award_badge';
}
/**
* Export the action bar
*
* @param renderer_base $output
* @return array
*/
public function export_for_template(renderer_base $output): array {
$elements = parent::export_for_template($output);
// Add button for badge manual award.
if ($this->badge->has_manual_award_criteria()
&& has_capability('moodle/badges:awardbadge', $this->page->context) && $this->badge->is_active()) {
$url = new moodle_url('/badges/award.php', ['id' => $this->badge->id]);
$button = new single_button($url, get_string('award', 'badges'), 'post', single_button::BUTTON_PRIMARY);
$elements['awardbutton'] = $button->export_for_template($output);
}
$thirdpartynav = $this->get_third_party_nav_action($output);
$elements += $thirdpartynav ?: [];
return $elements;
}
}
@@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\output;
use moodle_page;
use moodle_url;
use renderer_base;
use single_button;
/**
* Class standard_action_bar - Display the action bar
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class standard_action_bar extends base_action_bar {
/** @var bool $showmanage Whether or not to show the manage badges button. */
protected $showmanage;
/** @var bool $showaddbadge Whether or not to show the add badges button. */
protected $showaddbadge;
/** @var moodle_url $backurl BackURL to be used when the back button is required. */
protected $backurl;
/**
* standard_action_bar constructor
*
* @param moodle_page $page The page object
* @param int $type The type of badge we are operating with
* @param bool $showmanage Whether or not to show the manage badges button
* @param bool $showaddbadge Whether or not to show the add badges button
* @param moodle_url|null $backurl The backurl to be used
*/
public function __construct(moodle_page $page, int $type, bool $showmanage = true,
$showaddbadge = true, ?moodle_url $backurl = null) {
parent::__construct($page, $type);
$this->showmanage = $showmanage;
$this->showaddbadge = $showaddbadge;
$this->backurl = $backurl;
}
/**
* The template that this tertiary nav should use.
*
* @return string
*/
public function get_template(): string {
return 'core_badges/manage_badges';
}
/**
* Export the action bar
*
* @param renderer_base $output
* @return array The buttons to be rendered
*/
public function export_for_template(renderer_base $output): array {
$buttons = [];
if ($this->backurl) {
$buttons[] = new single_button($this->backurl, get_string('back'), 'get');
}
$params = ['type' => $this->type];
if ($this->page->context->contextlevel == CONTEXT_COURSE) {
$params['id'] = $this->page->context->instanceid;
}
if ($this->showmanage) {
$buttons[] = new single_button(new moodle_url('/badges/index.php', $params),
get_string('managebadges', 'core_badges'), 'get');
}
if ($this->showaddbadge && has_capability('moodle/badges:createbadge', $this->page->context)) {
$buttons[] = new single_button(new moodle_url('/badges/newbadge.php', $params),
get_string('newbadge', 'core_badges'), 'post', single_button::BUTTON_PRIMARY);
}
foreach ($buttons as $key => $button) {
$buttons[$key] = $button->export_for_template($output);
}
$data = ['buttons' => $buttons];
$additional = $this->get_third_party_nav_action($output);
$data += $additional ?: [];
return $data;
}
}