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