Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
            'imagecaption' => 'Image caption'
73
        ], $record);
74
 
75
        $record->id = $DB->insert_record('badge', $record);
76
        $badge = new badge($record->id);
77
 
78
        // Process badge image (if supplied).
79
        if ($badgeimage !== '') {
80
            $file = get_file_storage()->create_file_from_pathname([
81
                'contextid' => context_user::instance($USER->id)->id,
82
                'userid' => $USER->id,
83
                'component' => 'user',
84
                'filearea' => 'draft',
85
                'itemid' => file_get_unused_draft_itemid(),
86
                'filepath' => '/',
87
                'filename' => basename($badgeimage),
88
            ], "{$CFG->dirroot}/$badgeimage");
89
 
90
            // Copy image to temp file, as it'll be deleted by the following call.
91
            badges_process_badge_image($badge, $file->copy_content_to_temp());
92
        }
93
 
94
        // Process badge tags (if supplied).
95
        if ($badgetags !== '') {
96
            if (!is_array($badgetags)) {
97
                $badgetags = preg_split('/\s*,\s*/', $badgetags, -1, PREG_SPLIT_NO_EMPTY);
98
            }
99
            core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), $badgetags);
100
        }
101
 
102
        return $badge;
103
    }
104
 
105
    /**
106
     * Create badge criteria
107
     *
108
     * Note that only manual criteria issues by role is currently supported
109
     *
110
     * @param array|stdClass $record
111
     * @throws coding_exception
112
     */
113
    public function create_criteria($record): void {
114
        $record = (array) $record;
115
 
116
        if (!array_key_exists('badgeid', $record)) {
117
            throw new coding_exception('Record must contain \'badgeid\' property');
118
        }
119
        if (!array_key_exists('roleid', $record)) {
120
            throw new coding_exception('Record must contain \'roleid\' property');
121
        }
122
 
123
        $badge = new badge($record['badgeid']);
124
 
125
        // Create the overall criteria.
126
        if (count($badge->criteria) === 0) {
127
            award_criteria::build([
128
                'badgeid' => $badge->id,
129
                'criteriatype' => BADGE_CRITERIA_TYPE_OVERALL,
130
            ])->save([
131
                'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
132
            ]);
133
        }
134
 
135
        // Create the manual criteria.
136
        award_criteria::build([
137
            'badgeid' => $badge->id,
138
            'criteriatype' => BADGE_CRITERIA_TYPE_MANUAL,
139
        ])->save([
140
            'role_' . $record['roleid'] => $record['roleid'],
141
            'description' => $record['description'] ?? '',
142
        ]);
143
    }
144
 
145
    /**
146
     * Create issued badge to a user
147
     *
148
     * @param array|stdClass $record
149
     * @throws coding_exception
150
     */
151
    public function create_issued_badge($record): void {
152
        $record = (array) $record;
153
 
154
        if (!array_key_exists('badgeid', $record)) {
155
            throw new coding_exception('Record must contain \'badgeid\' property');
156
        }
157
        if (!array_key_exists('userid', $record)) {
158
            throw new coding_exception('Record must contain \'userid\' property');
159
        }
160
 
161
        $badge = new badge($record['badgeid']);
162
        $badge->issue($record['userid'], true);
163
    }
164
}