Proyectos de Subversion Moodle

Rev

| 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
namespace core_badges\reportbuilder\local\systemreports;
20
 
21
use core_badges\reportbuilder\local\entities\badge;
22
use core_badges\reportbuilder\local\entities\badge_issued;
23
use core_reportbuilder\system_report;
24
use lang_string;
25
use moodle_url;
26
use pix_icon;
27
use stdClass;
28
 
29
defined('MOODLE_INTERNAL') || die;
30
 
31
global $CFG;
32
require_once("{$CFG->libdir}/badgeslib.php");
33
 
34
/**
35
 * Course badges system report class implementation
36
 *
37
 * @package    core_badges
38
 * @copyright  2023 David Carrillo <davidmc@moodle.com>
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class course_badges extends system_report {
42
 
43
    /**
44
     * Initialise report, we need to set the main table, load our entities and set columns/filters
45
     */
46
    protected function initialise(): void {
47
        global $USER;
48
        // Our main entity, it contains all of the column definitions that we need.
49
        $badgeentity = new badge();
50
        $entityalias = $badgeentity->get_table_alias('badge');
51
 
52
        $this->set_main_table('badge', $entityalias);
53
        $this->add_entity($badgeentity);
54
 
55
        $type = $this->get_parameter('type', 0, PARAM_INT);
56
        $courseid = $this->get_parameter('courseid', 0, PARAM_INT);
57
 
58
        $this->add_base_condition_simple('type', $type);
59
        $this->add_base_condition_simple('courseid', $courseid);
60
        $this->add_base_condition_sql("({$entityalias}.status = " . BADGE_STATUS_ACTIVE .
61
            " OR {$entityalias}.status = " . BADGE_STATUS_ACTIVE_LOCKED . ")");
62
 
63
        $badgeissuedentity = new badge_issued();
64
        $badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
65
        $this->add_entity($badgeissuedentity
66
            ->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
67
                ON {$entityalias}.id = {$badgeissuedalias}.badgeid AND {$badgeissuedalias}.userid = ".$USER->id)
68
        );
69
 
70
        $this->add_base_fields("{$badgeissuedalias}.uniquehash");
71
 
72
        // Now we can call our helper methods to add the content we want to include in the report.
73
        $this->add_columns($badgeissuedalias);
74
        $this->add_filters();
75
 
76
        // Set if report can be downloaded.
77
        $this->set_downloadable(false);
78
    }
79
 
80
    /**
81
     * Validates access to view this report
82
     *
83
     * @return bool
84
     */
85
    protected function can_view(): bool {
86
        return has_capability('moodle/badges:viewbadges', $this->get_context());
87
    }
88
 
89
    /**
90
     * Adds the columns we want to display in the report
91
     *
92
     * They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
93
     * unique identifier. If custom columns are needed just for this report, they can be defined here.
94
     *
95
     * @param string $badgeissuedalias
96
     */
97
    public function add_columns(string $badgeissuedalias): void {
98
        $columns = [
99
            'badge:image',
100
            'badge:name',
101
            'badge:description',
102
            'badge:criteria',
103
            'badge_issued:issued',
104
        ];
105
        $this->add_columns_from_entities($columns);
106
 
107
        $this->get_column('badge_issued:issued')
108
            ->set_title(new lang_string('awardedtoyou', 'core_badges'))
109
            ->add_fields("{$badgeissuedalias}.uniquehash")
110
            ->set_callback(static function(?int $value, stdClass $row) {
111
                global $OUTPUT;
112
 
113
                if (!$value) {
114
                    return '';
115
                }
116
                $format = get_string('strftimedatefullshort', 'core_langconfig');
117
                $date = $value ? userdate($value, $format) : '';
118
                $badgeurl = new moodle_url('/badges/badge.php', ['hash' => $row->uniquehash]);
119
                $icon = new pix_icon('i/valid', get_string('dateearned', 'badges', $date));
120
                return $OUTPUT->action_icon($badgeurl, $icon, null, null, true);
121
            });
122
 
123
        $this->set_initial_sort_column('badge:name', SORT_ASC);
124
    }
125
 
126
    /**
127
     * Adds the filters we want to display in the report
128
     *
129
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
130
     * unique identifier
131
     */
132
    protected function add_filters(): void {
133
        $filters = [
134
            'badge:name',
135
            'badge_issued:issued',
136
        ];
137
 
138
        $this->add_filters_from_entities($filters);
139
    }
140
}