See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 403]
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 declare(strict_types=1); 18 19 use core_badges\badge; 20 21 defined('MOODLE_INTERNAL') || die(); 22 23 global $CFG; 24 require_once("{$CFG->libdir}/badgeslib.php"); 25 26 /** 27 * Badges test generator 28 * 29 * @package core_badges 30 * @copyright 2022 Paul Holden <paulh@moodle.com> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class core_badges_generator extends component_generator_base { 34 35 /** 36 * Create badge 37 * 38 * @param array|stdClass $record 39 * @return badge 40 */ 41 public function create_badge($record): badge { 42 global $CFG, $DB, $USER; 43 44 $record = (array) $record; 45 46 // Save badge image path for later. 47 $badgeimage = $record['image'] ?? ''; 48 unset($record['image']); 49 50 $record = (object) array_merge([ 51 'name' => 'Test badge', 52 'description' => 'Testing badges', 53 'timecreated' => time(), 54 'timemodified' => time(), 55 'usercreated' => $USER->id, 56 'usermodified' => $USER->id, 57 'issuername' => 'Test issuer', 58 'issuerurl' => 'http://issuer-url.domain.co.nz', 59 'issuercontact' => 'issuer@example.com', 60 'expiredate' => null, 61 'expireperiod' => null, 62 'type' => BADGE_TYPE_SITE, 63 'courseid' => null, 64 'messagesubject' => 'Test message subject', 65 'message' => 'Test message body', 66 'attachment' => 1, 67 'notification' => 0, 68 'status' => BADGE_STATUS_ACTIVE, 69 'version' => OPEN_BADGES_V2, 70 'language' => 'en', 71 'imageauthorname' => 'Image author', 72 'imageauthoremail' => 'author@example.com', 73 'imageauthorurl' => 'http://image.example.com/', 74 'imagecaption' => 'Image caption' 75 ], $record); 76 77 $record->id = $DB->insert_record('badge', $record); 78 $badge = new badge($record->id); 79 80 // Process badge image (if supplied). 81 if ($badgeimage !== '') { 82 $file = get_file_storage()->create_file_from_pathname([ 83 'contextid' => context_user::instance($USER->id)->id, 84 'userid' => $USER->id, 85 'component' => 'user', 86 'filearea' => 'draft', 87 'itemid' => file_get_unused_draft_itemid(), 88 'filepath' => '/', 89 'filename' => basename($badgeimage), 90 ], "{$CFG->dirroot}/$badgeimage"); 91 92 // Copy image to temp file, as it'll be deleted by the following call. 93 badges_process_badge_image($badge, $file->copy_content_to_temp()); 94 } 95 96 return $badge; 97 } 98 99 /** 100 * Create badge criteria 101 * 102 * Note that only manual criteria issues by role is currently supported 103 * 104 * @param array|stdClass $record 105 * @throws coding_exception 106 */ 107 public function create_criteria($record): void { 108 $record = (array) $record; 109 110 if (!array_key_exists('badgeid', $record)) { 111 throw new coding_exception('Record must contain \'badgeid\' property'); 112 } 113 if (!array_key_exists('roleid', $record)) { 114 throw new coding_exception('Record must contain \'roleid\' property'); 115 } 116 117 $badge = new badge($record['badgeid']); 118 119 // Create the overall criteria. 120 if (count($badge->criteria) === 0) { 121 award_criteria::build([ 122 'badgeid' => $badge->id, 123 'criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 124 ])->save([ 125 'agg' => BADGE_CRITERIA_AGGREGATION_ALL, 126 ]); 127 } 128 129 // Create the manual criteria. 130 award_criteria::build([ 131 'badgeid' => $badge->id, 132 'criteriatype' => BADGE_CRITERIA_TYPE_MANUAL, 133 ])->save([ 134 'role_' . $record['roleid'] => $record['roleid'], 135 'description' => $record['description'] ?? '', 136 ]); 137 } 138 139 /** 140 * Create issued badge to a user 141 * 142 * @param array|stdClass $record 143 * @throws coding_exception 144 */ 145 public function create_issued_badge($record): void { 146 $record = (array) $record; 147 148 if (!array_key_exists('badgeid', $record)) { 149 throw new coding_exception('Record must contain \'badgeid\' property'); 150 } 151 if (!array_key_exists('userid', $record)) { 152 throw new coding_exception('Record must contain \'userid\' property'); 153 } 154 155 $badge = new badge($record['badgeid']); 156 $badge->issue($record['userid'], true); 157 } 158 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body