Differences Between: [Versions 401 and 403] [Versions 402 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/tags for later. 47 $badgeimage = $record['image'] ?? ''; 48 $badgetags = $record['tags'] ?? ''; 49 unset($record['image'], $record['tags']); 50 51 $record = (object) array_merge([ 52 'name' => 'Test badge', 53 'description' => 'Testing badges', 54 'timecreated' => time(), 55 'timemodified' => time(), 56 'usercreated' => $USER->id, 57 'usermodified' => $USER->id, 58 'issuername' => 'Test issuer', 59 'issuerurl' => 'http://issuer-url.domain.co.nz', 60 'issuercontact' => 'issuer@example.com', 61 'expiredate' => null, 62 'expireperiod' => null, 63 'type' => BADGE_TYPE_SITE, 64 'courseid' => null, 65 'messagesubject' => 'Test message subject', 66 'message' => 'Test message body', 67 'attachment' => 1, 68 'notification' => 0, 69 'status' => BADGE_STATUS_ACTIVE, 70 'version' => OPEN_BADGES_V2, 71 'language' => 'en', 72 'imageauthorname' => 'Image author', 73 'imageauthoremail' => 'author@example.com', 74 'imageauthorurl' => 'http://image.example.com/', 75 'imagecaption' => 'Image caption' 76 ], $record); 77 78 $record->id = $DB->insert_record('badge', $record); 79 $badge = new badge($record->id); 80 81 // Process badge image (if supplied). 82 if ($badgeimage !== '') { 83 $file = get_file_storage()->create_file_from_pathname([ 84 'contextid' => context_user::instance($USER->id)->id, 85 'userid' => $USER->id, 86 'component' => 'user', 87 'filearea' => 'draft', 88 'itemid' => file_get_unused_draft_itemid(), 89 'filepath' => '/', 90 'filename' => basename($badgeimage), 91 ], "{$CFG->dirroot}/$badgeimage"); 92 93 // Copy image to temp file, as it'll be deleted by the following call. 94 badges_process_badge_image($badge, $file->copy_content_to_temp()); 95 } 96 97 // Process badge tags (if supplied). 98 if ($badgetags !== '') { 99 if (!is_array($badgetags)) { 100 $badgetags = preg_split('/\s*,\s*/', $badgetags, -1, PREG_SPLIT_NO_EMPTY); 101 } 102 core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), $badgetags); 103 } 104 105 return $badge; 106 } 107 108 /** 109 * Create badge criteria 110 * 111 * Note that only manual criteria issues by role is currently supported 112 * 113 * @param array|stdClass $record 114 * @throws coding_exception 115 */ 116 public function create_criteria($record): void { 117 $record = (array) $record; 118 119 if (!array_key_exists('badgeid', $record)) { 120 throw new coding_exception('Record must contain \'badgeid\' property'); 121 } 122 if (!array_key_exists('roleid', $record)) { 123 throw new coding_exception('Record must contain \'roleid\' property'); 124 } 125 126 $badge = new badge($record['badgeid']); 127 128 // Create the overall criteria. 129 if (count($badge->criteria) === 0) { 130 award_criteria::build([ 131 'badgeid' => $badge->id, 132 'criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 133 ])->save([ 134 'agg' => BADGE_CRITERIA_AGGREGATION_ALL, 135 ]); 136 } 137 138 // Create the manual criteria. 139 award_criteria::build([ 140 'badgeid' => $badge->id, 141 'criteriatype' => BADGE_CRITERIA_TYPE_MANUAL, 142 ])->save([ 143 'role_' . $record['roleid'] => $record['roleid'], 144 'description' => $record['description'] ?? '', 145 ]); 146 } 147 148 /** 149 * Create issued badge to a user 150 * 151 * @param array|stdClass $record 152 * @throws coding_exception 153 */ 154 public function create_issued_badge($record): void { 155 $record = (array) $record; 156 157 if (!array_key_exists('badgeid', $record)) { 158 throw new coding_exception('Record must contain \'badgeid\' property'); 159 } 160 if (!array_key_exists('userid', $record)) { 161 throw new coding_exception('Record must contain \'userid\' property'); 162 } 163 164 $badge = new badge($record['badgeid']); 165 $badge->issue($record['userid'], true); 166 } 167 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body