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
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');
70
        $elements['urlselect'] = new url_select($this->generate_badge_navigation(), $this->page->url->out(false), null);
71
        foreach ($elements as $key => $element) {
72
            $elements[$key] = $element->export_for_template($output);
73
        }
74
        $additional = $this->get_third_party_nav_action($output);
75
        $elements += $additional ?: [];
76
 
77
        return $elements;
78
    }
79
 
80
    /**
81
     * Returns a multi dimensional array of the links that should be displayed when creating a badge.
82
     * The keys of the array feed into the text shown to the user and content of each element contain the following:
83
     *  - url               URL for the option
84
     *  - additionalparams  Additional params to feed into the url
85
     *  - capability        The capabilities to check that governs visibility
86
     * @return array
87
     */
88
    protected function get_badge_administration_mapping_construct(): array {
89
        return [
90
            'boverview' => [
91
                'url' => '/badges/overview.php',
92
                'capability' => ''
93
            ],
94
            'bdetails' => [
95
                'url' => '/badges/edit.php',
96
                'additionalparams' => ['action' => 'badge'],
97
                'capability' => 'moodle/badges:configuredetails'
98
            ],
99
            'bcriteria' => [
100
                'url' => '/badges/criteria.php',
101
                'capability' => 'moodle/badges:configurecriteria'
102
            ],
103
            'bmessage' => [
104
                'url' => '/badges/edit.php',
105
                'additionalparams' => ['action' => 'message'],
106
                'capability' => 'moodle/badges:configuremessages'
107
            ],
108
            'bawards' => [
109
                'url' => '/badges/recipients.php',
110
                'capability' => 'moodle/badges:viewawarded'
111
            ],
112
            'bendorsement' => [
113
                'url' => '/badges/endorsement.php',
114
                'capability' => 'moodle/badges:configuredetails'
115
            ],
116
            'brelated' => [
117
                'url' => '/badges/related.php',
118
                'capability' => 'moodle/badges:configuredetails'
119
            ],
120
            'balignment' => [
121
                'url' => '/badges/alignment.php',
122
                'capability' => 'moodle/badges:configuredetails'
123
            ],
124
        ];
125
    }
126
 
127
    /**
128
     * Generate the options to be displayed when editing a badge. This feeds into a URL select which will be displayed
129
     * in the tertiary navigation.
130
     *
131
     * @return array
132
     */
133
    protected function generate_badge_navigation(): array {
134
        global $DB;
135
        $params = ['id' => $this->badge->id];
136
        $options = [];
137
        $construct = $this->get_badge_administration_mapping_construct();
138
        foreach ($construct as $stringidentifier => $checks) {
139
            if ($checks['capability'] && !has_capability($checks['capability'], $this->page->context)) {
140
                continue;
141
            }
142
 
143
            $sql = '';
144
            switch ($stringidentifier) {
145
                case 'bawards':
146
                    $sql = "SELECT COUNT(b.userid)
147
                              FROM {badge_issued} b
148
                        INNER JOIN {user} u ON b.userid = u.id
149
                             WHERE b.badgeid = :badgeid AND u.deleted = 0";
150
                    break;
151
                case 'brelated':
152
                    $sql = "SELECT COUNT(br.badgeid)
153
                              FROM {badge_related} br
154
                             WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
155
                    break;
156
                case 'balignment':
157
                    $sql = "SELECT COUNT(bc.id)
158
                              FROM {badge_alignment} bc
159
                             WHERE bc.badgeid = :badgeid";
160
                    break;
161
            }
162
 
163
            $content = null;
164
            if ($sql) {
165
                $content = $DB->count_records_sql($sql, ['badgeid' => $this->badge->id, 'badgeid2' => $this->badge->id]);
166
            }
167
 
168
            $url = new moodle_url($checks['url'], $params + ($checks['additionalparams'] ?? []));
169
            $options[get_string($stringidentifier, 'core_badges', $content)] = $url->out(false);
170
        }
171
        return array_flip($options);
172
    }
173
}