forked from manieer/moodle-block_report_certificates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_report_certificates.php
171 lines (146 loc) · 7.48 KB
/
block_report_certificates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version details
*
* Report certificates block
* --------------------------
* Displays all issued certificates for users with unique codes.
* The certificates will also be issued for courses that have been archived since issuing of the certificates
*
* @copyright 2015 onwards Manieer Chhettri | Marie Curie, UK | <manieer@gmail.com>
* @author Manieer Chhettri | Marie Curie, UK | <manieer@gmail.com> | 2015
* @package block_report_certificates
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/mod/certificate/locallib.php');
/**
*
* Report previously issued certificates.
*
* Displays all previously issued certificatesfor logged in user.
*
* @copyright 2015 onwards Manieer Chhettri | Marie Curie, UK | <manieer@gmail.com>.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_report_certificates extends block_base {
/**
*
* Report previously issued certificates.
*
* Displays all previously issued certificatesfor logged in user.
*/
public function init() {
$this->title = get_string('report_certificates', 'block_report_certificates');
$this->version = 2015051800;
}
/**
*
* Retrieving relevant required data.
*
* Retrieving data and populating them for displaying on the block.
*/
public function get_content() {
global $USER, $DB, $CFG;
if ($this->content !== null) {
return $this->content;
}
// User ID.
$userid = optional_param('userid', $USER->id, PARAM_INT);
$this->content = new stdClass;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
// Table headers.
$table = new html_table();
$table->head = array(get_string('report_certificates_tblheader_coursename', 'block_report_certificates'),
get_string('report_certificates_tblheader_grade', 'block_report_certificates'),
get_string('report_certificates_tblheader_code', 'block_report_certificates'),
get_string('report_certificates_tblheader_issuedate', 'block_report_certificates'),
get_string('report_certificates_tblheader_download', 'block_report_certificates'));
$table->align = array ("left", "center", "center", "center", "center");
$moduleid = $DB->get_field('modules', 'id', array('name'=>'certificate'), MUST_EXIST );
$sql = "SELECT DISTINCT ci.id AS certificateid, ci.userid, ci.code AS code,
ci.timecreated AS citimecreated,
crt.name AS certificatename, crt.*,
cm.id AS coursemoduleid, cm.course, cm.module,
c.id AS id, c.fullname AS fullname, c.*,
ctx.id AS contextid, ctx.instanceid AS instanceid,
f.itemid AS itemid, f.filename AS filename
FROM {certificate_issues} ci
INNER JOIN {certificate} crt
ON crt.id = ci.certificateid
INNER JOIN {course_modules} cm
ON cm.course = crt.course
INNER JOIN {course} c
ON c.id = cm.course
INNER JOIN {context} ctx
ON ctx.instanceid = cm.id
INNER JOIN {files} f
ON f.contextid = ctx.id
WHERE cm.module = :moduleid AND
ctx.contextlevel = 70 AND
f.mimetype = 'application/pdf' AND
ci.userid = f.userid AND
ci.userid = :userid
GROUP BY ci.code
ORDER BY ci.timecreated ASC";
// CERTIFICATE MODULE (cm.module = 4), CONTEXT_MODULE (ctx.contextlevel = 70).
// PDF FILES ONLY (f.mimetype = 'application/pdf').
$limit = " LIMIT 5"; // Limiting the output results to just five records.
$certificates = $DB->get_records_sql($sql.$limit, array('userid' => $USER->id,'moduleid'=>$moduleid));
if (empty($certificates)) {
$this->content->text = get_string('report_certificates_noreports', 'block_report_certificates');
return $this->content;
} else {
foreach ($certificates as $certdata) {
$certdata->printdate = 1; // Modify printdate so that date is always printed.
$certrecord = new stdClass();
// Date format.
$dateformat = get_string('strftimedate');
// Required variables for output.
$userid = $certrecord->userid = $certdata->userid;
$certificateid = $certrecord->certificateid = $certdata->certificateid;
$contextid = $certrecord->contextid = $certdata->contextid;
$courseid = $certrecord->id = $certdata->id;
$coursename = $certrecord->fullname = $certdata->fullname;
$filename = $certrecord->filename = $certdata->filename;
$code = $certrecord->code = $certdata->code;
// Retrieving grade and date for each certificate.
$grade = certificate_get_grade($certdata, $certrecord, $userid, $valueonly = true);
$date = $certrecord->timecreated = $certdata->citimecreated;
// Direct course link.
$link = html_writer::link(new moodle_url('/course/view.php', array('id' => $courseid)),
$coursename, array('fullname' => $coursename));
// Direct certificate download link.
$filelink = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'
.$contextid.'/mod_certificate/issue/'
.$certificateid.'/'.$filename);
$outputlink = '<a href="'.$filelink.'" >'
.'<img src="../blocks/report_certificates/pix/download.png" alt="Please download"'
.'width="100px" height="29px">'
.'</a>';
$table->data[] = array ($link, $grade, $code, userdate($date, $dateformat), $outputlink);
}
$this->content->footer = html_writer::link(new moodle_url('/blocks/report_certificates/report.php',
array('userid' => $USER->id)),
get_string('report_certificates_footermessage', 'block_report_certificates'));
}
$this->content->text = html_writer::table($table);
return $this->content;
}
}