This commit is contained in:
Sara Arjona 2024-09-09 17:03:02 +02:00
commit ce5cda1c07
No known key found for this signature in database
4 changed files with 8 additions and 38 deletions

View File

@ -16,12 +16,12 @@
namespace core_badges\external;
use core\exception\moodle_exception;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use moodle_exception;
defined('MOODLE_INTERNAL') || die();
@ -31,16 +31,14 @@ require_once($CFG->libdir . '/badgeslib.php');
/**
* External service to get badge by id.
*
* This is mainly used by the mobile application.
*
* @package core_badges
* @category external
*
* @copyright 2024 Daniel Ureña <durenadev@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.5
*/
class get_badge extends external_api {
/**
* Returns description of method parameters
*
@ -57,10 +55,10 @@ class get_badge extends external_api {
*
* @param int $id
* @return array
* @throws \restricted_context_exception
* @throws moodle_exception
*/
public static function execute(int $id): array {
global $CFG;
global $CFG, $PAGE;
// Initialize return variables.
$warnings = [];
@ -71,24 +69,20 @@ class get_badge extends external_api {
] = self::validate_parameters(self::execute_parameters(), [
'id' => $id,
]);
// Validate badges is not disabled.
if (empty($CFG->enablebadges)) {
throw new moodle_exception('badgesdisabled', 'badges');
}
$context = \context_system::instance();
if (!has_capability('moodle/badges:viewbadges', $context)) {
throw new moodle_exception('error:nopermissiontoview', 'badges');
}
// Get the badge by id.
$badgeclass = new \core_badges\output\badgeclass($id);
if (empty($badgeclass->badge)) {
throw new moodle_exception('error:relatedbadgedoesntexist', 'badges');
}
// Manage the badge.
$PAGE->set_context($badgeclass->context);
$result = badges_prepare_badgeclass_for_external($badgeclass);
return [

View File

@ -105,29 +105,6 @@ final class get_badge_test extends externallib_advanced_testcase {
get_badge::execute($data['badge']['id']);
}
/**
* Check the capability to view badges.
* @covers ::execute
*/
public function test_get_badge_viewbadges(): void {
global $DB, $SITE;
$data = $this->prepare_test_data();
$capability = 'moodle/badges:viewbadges';
$role = $DB->get_record('role', ['shortname' => 'student']);
$context = \context_system::instance();
assign_capability($capability, CAP_PROHIBIT, $role->id, $context->id, true);
$student1 = $this->getDataGenerator()->create_and_enrol($SITE, 'student');
role_assign($role->id, $student1->id, $context->id);
accesslib_clear_all_caches_for_unit_testing();
$this->setUser($student1);
$this->expectException(\moodle_exception::class);
get_badge::execute($data['badge']['id']);
}
/**
* Test get badge by id.
* @covers ::execute

View File

@ -135,7 +135,6 @@ $functions = array(
'classname' => 'core_badges\external\get_badge',
'description' => 'Retrieves a badge by id.',
'type' => 'read',
'capabilities' => 'moodle/badges:viewbadges',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
'core_badges_get_user_badges' => array(

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2024090700.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024090700.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.5dev+ (Build: 20240907)'; // Human-friendly version name