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
/**
18
 * Contains class mod_feedback_templates_table
19
 *
20
 * @package   mod_feedback
21
 * @copyright 2016 Marina Glancy
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
1441 ariadna 27
use core\output\notification;
28
use core\output\action_menu;
29
use core\output\action_link;
30
use core\output\action_menu\link_secondary;
31
use core\output\actions\confirm_action;
32
 
1 efrain 33
global $CFG;
34
require_once($CFG->libdir . '/tablelib.php');
35
 
36
/**
37
 * Class mod_feedback_templates_table
38
 *
39
 * @package   mod_feedback
40
 * @copyright 2016 Marina Glancy
41
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
1441 ariadna 43
class mod_feedback_templates_table extends core_table\flexible_table {
44
    /** @var int|null The module id. */
45
    private $cmid;
1 efrain 46
 
47
    /**
48
     * Constructor
49
     * @param int $uniqueid all tables have to have a unique id, this is used
50
     *      as a key when storing table properties like sort order in the session.
51
     * @param moodle_url $baseurl
1441 ariadna 52
     * @param string|null $mode This parameter has been deprecated since 4.5 and should not be used anymore.
1 efrain 53
     */
54
    public function __construct($uniqueid, $baseurl, ?string $mode = null) {
1441 ariadna 55
        if ($mode !== null) {
56
            debugging(
57
                'The age argument has been deprecated. Please remove it from your method calls.',
58
                DEBUG_DEVELOPER,
59
            );
60
        }
1 efrain 61
        parent::__construct($uniqueid);
1441 ariadna 62
        $this->cmid = $baseurl->param('id');
63
        $tablecolumns = [
64
            'template' => get_string('template', 'feedback'),
65
            'actions' => html_writer::span(get_string('actions'), 'visually-hidden'),
66
        ];
1 efrain 67
 
68
        $this->set_attribute('class', 'templateslist');
69
 
1441 ariadna 70
        $this->define_columns(array_keys($tablecolumns));
71
        $this->define_headers(array_values($tablecolumns));
1 efrain 72
        $this->define_baseurl($baseurl);
73
        $this->column_class('template', 'template');
74
        $this->sortable(false);
75
    }
76
 
77
    /**
78
     * Displays the table with the given set of templates
79
     * @param array $templates
80
     */
81
    public function display($templates) {
82
        global $OUTPUT;
83
        if (empty($templates)) {
1441 ariadna 84
            echo $OUTPUT->notification(
85
                get_string('no_templates_available_yet', 'feedback'),
86
                notification::NOTIFY_INFO,
87
                false,
88
            );
1 efrain 89
            return;
90
        }
91
 
92
        $this->setup();
93
 
94
        foreach ($templates as $template) {
1441 ariadna 95
            $showactions = has_any_capability(
96
                ['mod/feedback:deletetemplate', 'mod/feedback:edititems', 'mod/feedback:createpublictemplate'],
97
                $this->get_context()
98
            );
99
            $data = [
100
                format_string($template->name),
101
                $showactions ? $OUTPUT->render($this->get_row_actions($template)) : '',
102
            ];
1 efrain 103
 
104
            $this->add_data($data);
105
        }
106
        $this->finish_output();
107
    }
1441 ariadna 108
 
109
    /**
110
     * Get the row actions for the given template
111
     *
112
     * @param stdClass $template
113
     * @return action_menu
114
     */
115
    private function get_row_actions(stdClass $template): action_menu {
116
        global $PAGE, $OUTPUT;
117
 
118
        $url = new moodle_url($this->baseurl, ['templateid' => $template->id, 'sesskey' => sesskey()]);
119
        $strdeletefeedback = get_string('delete_template', 'feedback');
120
        $actions = new action_menu();
121
        $actions->set_menu_trigger($OUTPUT->pix_icon('a/setting', get_string('actions')));
122
 
123
        // Preview.
124
        $actions->add(new link_secondary(
125
            new moodle_url($this->baseurl, ['templateid' => $template->id, 'sesskey' => sesskey()]),
126
            new pix_icon('t/preview', get_string('preview')),
127
            get_string('preview'),
128
        ));
129
 
130
        // Use template.
131
        if (has_capability('mod/feedback:edititems', context_module::instance($this->cmid))) {
132
            $PAGE->requires->js_call_amd('mod_feedback/usetemplate', 'init');
133
            $actions->add(new link_secondary(
134
                new moodle_url('#'),
135
                new pix_icon('i/files', get_string('preview')),
136
                get_string('use_this_template', 'mod_feedback'),
137
                ['data-action' => 'usetemplate', 'data-dataid' => $this->cmid, 'data-templateid' => $template->id],
138
            ));
139
        }
140
 
141
        // Delete.
142
        $showdelete = has_capability('mod/feedback:deletetemplate', context_module::instance($this->cmid));
143
        if ($template->ispublic) {
144
            $showdelete = has_all_capabilities(
145
                ['mod/feedback:createpublictemplate', 'mod/feedback:deletetemplate'],
146
                context_system::instance()
147
            );
148
        }
149
        if ($showdelete) {
150
            $exporturl = new moodle_url(
151
                '/mod/feedback/manage_templates.php',
152
                $url->params() + ['deletetemplate' => $template->id]
153
            );
154
            $deleteaction = new action_link(
155
                $exporturl,
156
                get_string('delete'),
157
                new confirm_action(get_string('confirmdeletetemplate', 'feedback')),
158
                ['class' => 'text-danger'],
159
                new pix_icon('t/delete', $strdeletefeedback),
160
            );
161
            $actions->add_secondary_action($deleteaction);
162
        }
163
 
164
        return $actions;
165
    }
1 efrain 166
}