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
namespace core_badges\output;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once($CFG->libdir . '/badgeslib.php');
22
 
23
use coding_exception;
24
use context_course;
25
use stdClass;
26
use renderable;
27
use core_badges\badge;
28
use moodle_url;
29
use renderer_base;
30
 
31
/**
32
 * Page to display badge information, such as name, description or criteria. This information is unrelated to assertions.
33
 *
34
 * @package    core_badges
35
 * @copyright  2022 Sara Arjona (sara@moodle.com)
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class badgeclass implements renderable {
39
 
40
    /** @var badge class */
41
    public $badge;
42
 
43
    /** @var badge class */
44
    public $badgeid = 0;
45
 
46
    /** @var \context The badge context*/
47
    public $context;
48
 
49
    /**
50
     * Initializes the badge to display.
51
     *
52
     * @param int $id Id of the badge to display.
53
     */
54
    public function __construct(int $id) {
55
        $this->badgeid = $id;
56
        $this->badge = new badge($this->badgeid);
57
        if ($this->badge->status == BADGE_STATUS_INACTIVE) {
58
            // Inactive badges that haven't been published previously can't be displayed.
59
            $this->badge = null;
60
        } else {
61
            $this->context = $this->badge->get_context();
62
        }
63
    }
64
 
65
    /**
66
     * Export this data so it can be used as the context for a mustache template.
67
     *
68
     * @param renderer_base $output Renderer base.
69
     * @return stdClass
70
     */
71
    public function export_for_template(renderer_base $output): stdClass {
72
        global $DB, $SITE;
73
 
74
        $data = new stdClass();
75
        if ($this->context instanceof context_course) {
76
            $data->coursefullname = format_string($DB->get_field('course', 'fullname', ['id' => $this->badge->courseid]),
77
                true, ['context' => $this->context]);
78
        } else {
79
            $data->sitefullname = format_string($SITE->fullname, true, ['context' => $this->context]);
80
        }
81
 
82
        // Field: Image.
83
        $storage = get_file_storage();
84
        $imagefile = $storage->get_file($this->context->id, 'badges', 'badgeimage', $this->badgeid, '/', 'f3.png');
85
        if ($imagefile) {
86
            $imagedata = base64_encode($imagefile->get_content());
87
        } else {
88
            if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
89
                // Unit tests the file might not exist yet.
90
                $imagedata = '';
91
            } else {
92
                throw new coding_exception('Image file does not exist.');
93
            }
94
        }
95
        $data->badgeimage = 'data:image/png;base64,' . $imagedata;
96
 
97
        // Fields: Name, description.
98
        $data->badgename = $this->badge->name;
99
        $data->badgedescription = $this->badge->description;
100
 
101
        // Field: Criteria.
102
        // This method will return the HTML with the badge criteria.
103
        $data->criteria = $output->print_badge_criteria($this->badge);
104
 
105
        // Field: Issuer.
106
        $data->issuedby = format_string($this->badge->issuername, true, ['context' => $this->context]);
107
        if (isset($this->badge->issuercontact) && !empty($this->badge->issuercontact)) {
108
            $data->issuedbyemailobfuscated = obfuscate_mailto($this->badge->issuercontact, $data->issuedby);
109
        }
110
 
111
        // Fields: Other details, such as language or version.
112
        $data->hasotherfields = false;
113
        if (!empty($this->badge->language)) {
114
            $data->hasotherfields = true;
115
            $languages = get_string_manager()->get_list_of_languages();
116
            $data->language = $languages[$this->badge->language];
117
        }
118
        if (!empty($this->badge->version)) {
119
            $data->hasotherfields = true;
120
            $data->version = $this->badge->version;
121
        }
122
        if (!empty($this->badge->imagecaption)) {
123
            $data->hasotherfields = true;
124
            $data->imagecaption = $this->badge->imagecaption;
125
        }
126
 
127
        // Field: Endorsement.
128
        $endorsement = $this->badge->get_endorsement();
129
        if (!empty($endorsement)) {
130
            $data->hasotherfields = true;
131
            $endorsement = $this->badge->get_endorsement();
132
            $endorsement->issueremail = obfuscate_mailto($endorsement->issueremail, $endorsement->issueremail);
133
            $data->endorsement = (array) $endorsement;
134
        }
135
 
136
        // Field: Related badges.
137
        $relatedbadges = $this->badge->get_related_badges(true);
138
        if (!empty($relatedbadges)) {
139
            $data->hasotherfields = true;
140
            $data->hasrelatedbadges = true;
141
            $data->relatedbadges = [];
142
            foreach ($relatedbadges as $related) {
143
                if (isloggedin() && !is_guest($this->context)) {
144
                    $related->url = (new moodle_url('/badges/overview.php', ['id' => $related->id]))->out(false);
145
                }
146
                $data->relatedbadges[] = (array)$related;
147
            }
148
        }
149
 
150
        // Field: Alignments.
151
        $alignments = $this->badge->get_alignments();
152
        if (!empty($alignments)) {
153
            $data->hasotherfields = true;
154
            $data->hasalignments = true;
155
            $data->alignments = [];
156
            foreach ($alignments as $alignment) {
157
                $data->alignments[] = (array)$alignment;
158
            }
159
        }
160
 
161
        // Field: Tags.
162
        $tags = \core_tag_tag::get_item_tags('core_badges', 'badge', $this->badgeid);
163
        $taglist = new \core_tag\output\taglist($tags);
164
        $data->badgetag = $taglist->export_for_template($output);
165
 
166
        return $data;
167
    }
168
}