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
use core_badges\badge;
20
use moodle_url;
21
use renderer_base;
22
use single_button;
23
use moodle_page;
24
use url_select;
25
 
26
/**
27
 * Class manage_badge_action_bar - Display the action bar
28
 *
29
 * @package   core_badges
30
 * @copyright 2021 Peter Dias
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class manage_badge_action_bar extends base_action_bar {
34
    /** @var badge $badge The badge we are managing. */
35
    protected $badge;
36
 
37
    /**
38
     * manage_badge_action_bar constructor
39
     *
40
     * @param badge $badge The badge we are viewing
41
     * @param moodle_page $page The page object
42
     */
43
    public function __construct(badge $badge, moodle_page $page) {
44
        parent::__construct($page, $badge->type);
45
        $this->badge = $badge;
46
    }
47
 
48
    /**
49
     * The template that this tertiary nav should use.
50
     *
51
     * @return string
52
     */
53
    public function get_template(): string {
54
        return 'core_badges/manage_badge';
55
    }
56
 
57
    /**
58
     * Export the action bar
59
     *
60
     * @param renderer_base $output
61
     * @return array
62
     */
63
    public function export_for_template(renderer_base $output): array {
64
        $elements = [];
65
        $params = ['type' => $this->type];
66
        if ($this->page->context->contextlevel == CONTEXT_COURSE) {
67
            $params['id'] = $this->page->context->instanceid;
68
        }
69
        $elements['button'] = new single_button(new moodle_url('/badges/index.php', $params), get_string('back'), 'get');
1441 ariadna 70
        $badgenav = $this->generate_badge_navigation();
71
        if ($badgenav) {
72
            $badgenavselect = new url_select($badgenav, $this->page->url->out(false), null);
73
            $badgenavselect->set_label(get_string('badgesnavigation', 'badges'), ['class' => 'visually-hidden']);
74
            $elements['urlselect'] = $badgenavselect;
75
        }
1 efrain 76
        foreach ($elements as $key => $element) {
77
            $elements[$key] = $element->export_for_template($output);
78
        }
79
        $additional = $this->get_third_party_nav_action($output);
80
        $elements += $additional ?: [];
81
 
82
        return $elements;
83
    }
84
 
85
    /**
86
     * Returns a multi dimensional array of the links that should be displayed when creating a badge.
87
     * The keys of the array feed into the text shown to the user and content of each element contain the following:
88
     *  - url               URL for the option
89
     *  - additionalparams  Additional params to feed into the url
90
     *  - capability        The capabilities to check that governs visibility
91
     * @return array
92
     */
93
    protected function get_badge_administration_mapping_construct(): array {
94
        return [
95
            'boverview' => [
96
                'url' => '/badges/overview.php',
97
                'capability' => ''
98
            ],
99
            'bdetails' => [
100
                'url' => '/badges/edit.php',
101
                'additionalparams' => ['action' => 'badge'],
102
                'capability' => 'moodle/badges:configuredetails'
103
            ],
104
            'bcriteria' => [
105
                'url' => '/badges/criteria.php',
106
                'capability' => 'moodle/badges:configurecriteria'
107
            ],
108
            'bmessage' => [
109
                'url' => '/badges/edit.php',
110
                'additionalparams' => ['action' => 'message'],
111
                'capability' => 'moodle/badges:configuremessages'
112
            ],
113
            'bawards' => [
114
                'url' => '/badges/recipients.php',
115
                'capability' => 'moodle/badges:viewawarded'
116
            ],
117
            'bendorsement' => [
118
                'url' => '/badges/endorsement.php',
119
                'capability' => 'moodle/badges:configuredetails'
120
            ],
121
            'brelated' => [
122
                'url' => '/badges/related.php',
123
                'capability' => 'moodle/badges:configuredetails'
124
            ],
125
            'balignment' => [
126
                'url' => '/badges/alignment.php',
127
                'capability' => 'moodle/badges:configuredetails'
128
            ],
129
        ];
130
    }
131
 
132
    /**
133
     * Generate the options to be displayed when editing a badge. This feeds into a URL select which will be displayed
134
     * in the tertiary navigation.
135
     *
136
     * @return array
137
     */
138
    protected function generate_badge_navigation(): array {
139
        global $DB;
1441 ariadna 140
 
1 efrain 141
        $params = ['id' => $this->badge->id];
142
        $options = [];
143
        $construct = $this->get_badge_administration_mapping_construct();
144
        foreach ($construct as $stringidentifier => $checks) {
145
            if ($checks['capability'] && !has_capability($checks['capability'], $this->page->context)) {
146
                continue;
147
            }
148
 
149
            $sql = '';
150
            switch ($stringidentifier) {
151
                case 'bawards':
152
                    $sql = "SELECT COUNT(b.userid)
153
                              FROM {badge_issued} b
154
                        INNER JOIN {user} u ON b.userid = u.id
155
                             WHERE b.badgeid = :badgeid AND u.deleted = 0";
156
                    break;
157
                case 'brelated':
158
                    $sql = "SELECT COUNT(br.badgeid)
159
                              FROM {badge_related} br
160
                             WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
161
                    break;
162
                case 'balignment':
163
                    $sql = "SELECT COUNT(bc.id)
164
                              FROM {badge_alignment} bc
165
                             WHERE bc.badgeid = :badgeid";
166
                    break;
167
            }
168
 
169
            $content = null;
170
            if ($sql) {
171
                $content = $DB->count_records_sql($sql, ['badgeid' => $this->badge->id, 'badgeid2' => $this->badge->id]);
172
            }
173
 
174
            $url = new moodle_url($checks['url'], $params + ($checks['additionalparams'] ?? []));
175
            $options[get_string($stringidentifier, 'core_badges', $content)] = $url->out(false);
176
        }
1441 ariadna 177
        if (count($options) <= 1) {
178
            return [];
179
        }
180
 
1 efrain 181
        return array_flip($options);
182
    }
183
}