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 the certificates the user has throughout the site.
|
|
|
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 the certificates the user has throughout the site.
|
|
|
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 my_certificates_table extends \table_sql {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @var int $userid The user id
|
|
|
44 |
*/
|
|
|
45 |
protected $userid;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Sets up the table.
|
|
|
49 |
*
|
|
|
50 |
* @param int $userid
|
|
|
51 |
* @param string|null $download The file type, null if we are not downloading
|
|
|
52 |
*/
|
|
|
53 |
public function __construct($userid, $download = null) {
|
|
|
54 |
parent::__construct('mod_customcert_report_table');
|
|
|
55 |
|
|
|
56 |
$columns = [
|
|
|
57 |
'name',
|
|
|
58 |
'coursename',
|
|
|
59 |
'timecreated',
|
|
|
60 |
'code',
|
|
|
61 |
];
|
|
|
62 |
$headers = [
|
|
|
63 |
get_string('name'),
|
|
|
64 |
get_string('course'),
|
|
|
65 |
get_string('receiveddate', 'customcert'),
|
|
|
66 |
get_string('code', 'customcert'),
|
|
|
67 |
];
|
|
|
68 |
|
|
|
69 |
// Check if we were passed a filename, which means we want to download it.
|
|
|
70 |
if ($download) {
|
|
|
71 |
$this->is_downloading($download, 'customcert-report');
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if (!$this->is_downloading()) {
|
|
|
75 |
$columns[] = 'download';
|
|
|
76 |
$headers[] = get_string('file');
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$this->define_columns($columns);
|
|
|
80 |
$this->define_headers($headers);
|
|
|
81 |
$this->collapsible(false);
|
|
|
82 |
$this->sortable(true);
|
|
|
83 |
$this->no_sorting('code');
|
|
|
84 |
$this->no_sorting('download');
|
|
|
85 |
$this->is_downloadable(true);
|
|
|
86 |
|
|
|
87 |
$this->userid = $userid;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Generate the name column.
|
|
|
92 |
*
|
|
|
93 |
* @param \stdClass $certificate
|
|
|
94 |
* @return string
|
|
|
95 |
*/
|
|
|
96 |
public function col_name($certificate) {
|
|
|
97 |
$cm = get_coursemodule_from_instance('customcert', $certificate->id);
|
|
|
98 |
$context = \context_module::instance($cm->id);
|
|
|
99 |
|
|
|
100 |
return format_string($certificate->name, true, ['context' => $context]);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Generate the course name column.
|
|
|
105 |
*
|
|
|
106 |
* @param \stdClass $certificate
|
|
|
107 |
* @return string
|
|
|
108 |
*/
|
|
|
109 |
public function col_coursename($certificate) {
|
|
|
110 |
$cm = get_coursemodule_from_instance('customcert', $certificate->id);
|
|
|
111 |
$context = \context_module::instance($cm->id);
|
|
|
112 |
|
|
|
113 |
return format_string($certificate->coursename, true, ['context' => $context]);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Generate the certificate time created column.
|
|
|
118 |
*
|
|
|
119 |
* @param \stdClass $certificate
|
|
|
120 |
* @return string
|
|
|
121 |
*/
|
|
|
122 |
public function col_timecreated($certificate) {
|
|
|
123 |
return userdate($certificate->timecreated);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Generate the code column.
|
|
|
128 |
*
|
|
|
129 |
* @param \stdClass $certificate
|
|
|
130 |
* @return string
|
|
|
131 |
*/
|
|
|
132 |
public function col_code($certificate) {
|
|
|
133 |
return $certificate->code;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Generate the download column.
|
|
|
138 |
*
|
|
|
139 |
* @param \stdClass $certificate
|
|
|
140 |
* @return string
|
|
|
141 |
*/
|
|
|
142 |
public function col_download($certificate) {
|
|
|
143 |
global $OUTPUT;
|
|
|
144 |
|
|
|
145 |
$icon = new \pix_icon('download', get_string('download'), 'customcert');
|
|
|
146 |
$link = new \moodle_url('/mod/customcert/my_certificates.php',
|
|
|
147 |
['userid' => $this->userid,
|
|
|
148 |
'certificateid' => $certificate->id,
|
|
|
149 |
'downloadcert' => '1']);
|
|
|
150 |
|
|
|
151 |
return $OUTPUT->action_link($link, '', null, null, $icon);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Query the reader.
|
|
|
156 |
*
|
|
|
157 |
* @param int $pagesize size of page for paginated displayed table.
|
|
|
158 |
* @param bool $useinitialsbar do you want to use the initials bar.
|
|
|
159 |
*/
|
|
|
160 |
public function query_db($pagesize, $useinitialsbar = true) {
|
|
|
161 |
$total = certificate::get_number_of_certificates_for_user($this->userid);
|
|
|
162 |
|
|
|
163 |
$this->pagesize($pagesize, $total);
|
|
|
164 |
|
|
|
165 |
$this->rawdata = certificate::get_certificates_for_user($this->userid, $this->get_page_start(),
|
|
|
166 |
$this->get_page_size(), $this->get_sql_sort());
|
|
|
167 |
|
|
|
168 |
// Set initial bars.
|
|
|
169 |
if ($useinitialsbar) {
|
|
|
170 |
$this->initialbars($total > $pagesize);
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Download the data.
|
|
|
176 |
*/
|
|
|
177 |
public function download() {
|
|
|
178 |
\core\session\manager::write_close();
|
|
|
179 |
$total = certificate::get_number_of_certificates_for_user($this->userid);
|
|
|
180 |
$this->out($total, false);
|
|
|
181 |
exit;
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|