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
/**
18
 * The report that displays issued certificates.
19
 *
20
 * @package    mod_customcert
21
 * @copyright  2016 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_customcert;
26
 
27
defined('MOODLE_INTERNAL') || die;
28
 
29
global $CFG;
30
 
31
require_once($CFG->libdir . '/tablelib.php');
32
 
33
/**
34
 * Class for the report that displays issued certificates.
35
 *
36
 * @package    mod_customcert
37
 * @copyright  2016 Mark Nelson <markn@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class report_table extends \table_sql {
41
 
42
    /**
43
     * @var int $customcertid The custom certificate id
44
     */
45
    protected $customcertid;
46
 
47
    /**
48
     * @var \stdClass $cm The course module.
49
     */
50
    protected $cm;
51
 
52
    /**
53
     * @var bool $groupmode are we in group mode?
54
     */
55
    protected $groupmode;
56
 
57
    /**
58
     * Sets up the table.
59
     *
60
     * @param int $customcertid
61
     * @param \stdClass $cm the course module
62
     * @param bool $groupmode are we in group mode?
63
     * @param string|null $download The file type, null if we are not downloading
64
     */
65
    public function __construct($customcertid, $cm, $groupmode, $download = null) {
66
        parent::__construct('mod_customcert_report_table');
67
 
68
        $context = \context_module::instance($cm->id);
69
        $extrafields = \core_user\fields::for_identity($context)->get_required_fields();
70
 
71
        $columns = [];
72
        $columns[] = 'fullname';
73
        foreach ($extrafields as $extrafield) {
74
            $columns[] = $extrafield;
75
        }
76
        $columns[] = 'timecreated';
77
        $columns[] = 'code';
78
 
79
        $headers = [];
80
        $headers[] = get_string('fullname');
81
        foreach ($extrafields as $extrafield) {
82
            $headers[] = \core_user\fields::get_display_name($extrafield);
83
        }
84
        $headers[] = get_string('receiveddate', 'customcert');
85
        $headers[] = get_string('code', 'customcert');
86
 
87
        // Check if we were passed a filename, which means we want to download it.
88
        if ($download) {
89
            $this->is_downloading($download, 'customcert-report');
90
        }
91
 
92
        if (!$this->is_downloading()) {
93
            $columns[] = 'download';
94
            $headers[] = get_string('file');
95
        }
96
 
97
        if (!$this->is_downloading() && has_capability('mod/customcert:manage', $context)) {
98
            $columns[] = 'actions';
99
            $headers[] = '';
100
        }
101
 
102
        $this->define_columns($columns);
103
        $this->define_headers($headers);
104
        $this->collapsible(false);
105
        $this->sortable(true);
106
        $this->no_sorting('code');
107
        $this->no_sorting('download');
108
        $this->is_downloadable(true);
109
 
110
        $this->customcertid = $customcertid;
111
        $this->cm = $cm;
112
        $this->groupmode = $groupmode;
113
    }
114
 
115
    /**
116
     * Generate the fullname column.
117
     *
118
     * @param \stdClass $user
119
     * @return string
120
     */
121
    public function col_fullname($user) {
122
        global $OUTPUT;
123
 
124
        if (!$this->is_downloading()) {
125
            return $OUTPUT->user_picture($user) . ' ' . fullname($user);
126
        } else {
127
            return fullname($user);
128
        }
129
    }
130
 
131
    /**
132
     * Generate the certificate time created column.
133
     *
134
     * @param \stdClass $user
135
     * @return string
136
     */
137
    public function col_timecreated($user) {
138
        if ($this->is_downloading() === '') {
139
            return userdate($user->timecreated);
140
        }
141
        $format = '%Y-%m-%d %H:%M';
142
        return userdate($user->timecreated, $format);
143
    }
144
 
145
    /**
146
     * Generate the code column.
147
     *
148
     * @param \stdClass $user
149
     * @return string
150
     */
151
    public function col_code($user) {
152
        return $user->code;
153
    }
154
 
155
    /**
156
     * Generate the download column.
157
     *
158
     * @param \stdClass $user
159
     * @return string
160
     */
161
    public function col_download($user) {
162
        global $OUTPUT;
163
 
164
        $icon = new \pix_icon('download', get_string('download'), 'customcert');
165
        $link = new \moodle_url('/mod/customcert/view.php',
166
            [
167
                'id' => $this->cm->id,
168
                'downloadissue' => $user->id,
169
            ]
170
        );
171
 
172
        return $OUTPUT->action_link($link, '', null, null, $icon);
173
    }
174
 
175
    /**
176
     * Generate the actions column.
177
     *
178
     * @param \stdClass $user
179
     * @return string
180
     */
181
    public function col_actions($user) {
182
        global $OUTPUT;
183
 
184
        $icon = new \pix_icon('i/delete', get_string('delete'));
185
        $link = new \moodle_url('/mod/customcert/view.php',
186
            [
187
                'id' => $this->cm->id,
188
                'deleteissue' => $user->issueid,
189
                'sesskey' => sesskey(),
190
            ]
191
        );
192
 
193
        return $OUTPUT->action_icon($link, $icon, null, ['class' => 'action-icon delete-icon']);
194
    }
195
 
196
    /**
197
     * Query the reader.
198
     *
199
     * @param int $pagesize size of page for paginated displayed table.
200
     * @param bool $useinitialsbar do you want to use the initials bar.
201
     */
202
    public function query_db($pagesize, $useinitialsbar = true) {
203
        $total = \mod_customcert\certificate::get_number_of_issues($this->customcertid, $this->cm, $this->groupmode);
204
 
205
        $this->pagesize($pagesize, $total);
206
 
207
        $this->rawdata = \mod_customcert\certificate::get_issues($this->customcertid, $this->groupmode, $this->cm,
208
            $this->get_page_start(), $this->get_page_size(), $this->get_sql_sort());
209
 
210
        // Set initial bars.
211
        if ($useinitialsbar) {
212
            $this->initialbars($total > $pagesize);
213
        }
214
    }
215
 
216
    /**
217
     * Download the data.
218
     */
219
    public function download() {
220
        \core\session\manager::write_close();
221
        $total = \mod_customcert\certificate::get_number_of_issues($this->customcertid, $this->cm, $this->groupmode);
222
        $this->out($total, false);
223
        exit;
224
    }
225
}
226