1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of the customcert module for 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 |
* Handles viewing a customcert.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once('../../config.php');
|
|
|
26 |
|
|
|
27 |
$id = required_param('id', PARAM_INT);
|
|
|
28 |
$downloadown = optional_param('downloadown', false, PARAM_BOOL);
|
|
|
29 |
$downloadall = optional_param('downloadall', 0, PARAM_INT);
|
|
|
30 |
$downloadtable = optional_param('download', null, PARAM_ALPHA);
|
|
|
31 |
$downloadissue = optional_param('downloadissue', 0, PARAM_INT);
|
|
|
32 |
$deleteissue = optional_param('deleteissue', 0, PARAM_INT);
|
|
|
33 |
$confirm = optional_param('confirm', false, PARAM_BOOL);
|
|
|
34 |
$page = optional_param('page', 0, PARAM_INT);
|
|
|
35 |
$perpage = optional_param('perpage', \mod_customcert\certificate::CUSTOMCERT_PER_PAGE, PARAM_INT);
|
|
|
36 |
|
|
|
37 |
$cm = get_coursemodule_from_id('customcert', $id, 0, false, MUST_EXIST);
|
|
|
38 |
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
|
|
|
39 |
$customcert = $DB->get_record('customcert', ['id' => $cm->instance], '*', MUST_EXIST);
|
|
|
40 |
$template = $DB->get_record('customcert_templates', ['id' => $customcert->templateid], '*', MUST_EXIST);
|
|
|
41 |
|
|
|
42 |
// Ensure the user is allowed to view this page.
|
|
|
43 |
require_login($course, false, $cm);
|
|
|
44 |
$context = context_module::instance($cm->id);
|
|
|
45 |
require_capability('mod/customcert:view', $context);
|
|
|
46 |
|
|
|
47 |
$canreceive = has_capability('mod/customcert:receiveissue', $context);
|
|
|
48 |
$canmanage = has_capability('mod/customcert:manage', $context);
|
|
|
49 |
$canviewreport = has_capability('mod/customcert:viewreport', $context);
|
|
|
50 |
|
|
|
51 |
// Initialise $PAGE.
|
|
|
52 |
$pageurl = new moodle_url('/mod/customcert/view.php', ['id' => $cm->id]);
|
|
|
53 |
\mod_customcert\page_helper::page_setup($pageurl, $context, format_string($customcert->name));
|
|
|
54 |
|
|
|
55 |
// Check if the user can view the certificate based on time spent in course.
|
|
|
56 |
if ($customcert->requiredtime && !$canmanage) {
|
|
|
57 |
if (\mod_customcert\certificate::get_course_time($course->id) < ($customcert->requiredtime * 60)) {
|
|
|
58 |
$a = new stdClass;
|
|
|
59 |
$a->requiredtime = $customcert->requiredtime;
|
|
|
60 |
$url = new moodle_url('/course/view.php', ['id' => $course->id]);
|
|
|
61 |
notice(get_string('requiredtimenotmet', 'customcert', $a), $url);
|
|
|
62 |
die;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// Check if we are deleting an issue.
|
|
|
67 |
if ($deleteissue && $canmanage && confirm_sesskey()) {
|
|
|
68 |
if (!$confirm) {
|
|
|
69 |
$nourl = new moodle_url('/mod/customcert/view.php', ['id' => $id]);
|
|
|
70 |
$yesurl = new moodle_url('/mod/customcert/view.php',
|
|
|
71 |
[
|
|
|
72 |
'id' => $id,
|
|
|
73 |
'deleteissue' => $deleteissue,
|
|
|
74 |
'confirm' => 1,
|
|
|
75 |
'sesskey' => sesskey(),
|
|
|
76 |
]
|
|
|
77 |
);
|
|
|
78 |
|
|
|
79 |
// Show a confirmation page.
|
|
|
80 |
$PAGE->navbar->add(get_string('deleteconfirm', 'customcert'));
|
|
|
81 |
$message = get_string('deleteissueconfirm', 'customcert');
|
|
|
82 |
echo $OUTPUT->header();
|
|
|
83 |
echo $OUTPUT->heading(format_string($customcert->name));
|
|
|
84 |
echo $OUTPUT->confirm($message, $yesurl, $nourl);
|
|
|
85 |
echo $OUTPUT->footer();
|
|
|
86 |
exit();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// Delete the issue.
|
|
|
90 |
$DB->delete_records('customcert_issues', ['id' => $deleteissue, 'customcertid' => $customcert->id]);
|
|
|
91 |
|
|
|
92 |
// Redirect back to the manage templates page.
|
|
|
93 |
redirect(new moodle_url('/mod/customcert/view.php', ['id' => $id]));
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// Get the current groups mode.
|
|
|
97 |
if ($groupmode = groups_get_activity_groupmode($cm)) {
|
|
|
98 |
groups_get_activity_group($cm, true);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
// Check if we are downloading all certificates.
|
|
|
102 |
if ($downloadall && $canmanage && confirm_sesskey()) {
|
|
|
103 |
$template = new \mod_customcert\template($template);
|
|
|
104 |
$issues = \mod_customcert\certificate::get_issues($customcert->id, $groupmode, $cm, 0, 0);
|
|
|
105 |
|
|
|
106 |
// The button is not visible if there are no issues, so in this case just redirect back to this page.
|
|
|
107 |
if (empty($issues)) {
|
|
|
108 |
redirect(new moodle_url('/mod/customcert/view.php', ['id' => $id]));
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
\mod_customcert\certificate::download_all_issues_for_instance($template, $issues);
|
|
|
112 |
exit();
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$event = \mod_customcert\event\course_module_viewed::create([
|
|
|
116 |
'objectid' => $customcert->id,
|
|
|
117 |
'context' => $context,
|
|
|
118 |
]);
|
|
|
119 |
$event->add_record_snapshot('course', $course);
|
|
|
120 |
$event->add_record_snapshot('customcert', $customcert);
|
|
|
121 |
$event->trigger();
|
|
|
122 |
|
|
|
123 |
// Check that we are not downloading a certificate PDF.
|
|
|
124 |
if (!$downloadown && !$downloadissue) {
|
|
|
125 |
// Generate the table to the report if there are issues to display.
|
|
|
126 |
if ($canviewreport) {
|
|
|
127 |
// Get the total number of issues.
|
|
|
128 |
$reporttable = new \mod_customcert\report_table($customcert->id, $cm, $groupmode, $downloadtable);
|
|
|
129 |
$reporttable->define_baseurl($pageurl);
|
|
|
130 |
|
|
|
131 |
if ($reporttable->is_downloading()) {
|
|
|
132 |
$reporttable->download();
|
|
|
133 |
exit();
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// If the current user has been issued a customcert generate HTML to display the details.
|
|
|
138 |
$issuehtml = '';
|
|
|
139 |
$issues = $DB->get_records('customcert_issues', ['userid' => $USER->id, 'customcertid' => $customcert->id]);
|
|
|
140 |
if ($issues && !$canmanage) {
|
|
|
141 |
// Get the most recent issue (there should only be one).
|
|
|
142 |
$issue = reset($issues);
|
|
|
143 |
$issuestring = get_string('receiveddate', 'customcert') . ': ' . userdate($issue->timecreated);
|
|
|
144 |
$issuehtml = $OUTPUT->box($issuestring);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// Create the button to download the customcert.
|
|
|
148 |
$downloadbutton = '';
|
|
|
149 |
if ($canreceive) {
|
|
|
150 |
$linkname = get_string('getcustomcert', 'customcert');
|
|
|
151 |
$link = new moodle_url('/mod/customcert/view.php', ['id' => $cm->id, 'downloadown' => true]);
|
|
|
152 |
$downloadbutton = new single_button($link, $linkname, 'get', single_button::BUTTON_PRIMARY);
|
|
|
153 |
$downloadbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem.
|
|
|
154 |
$downloadbutton = $OUTPUT->render($downloadbutton);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$numissues = \mod_customcert\certificate::get_number_of_issues($customcert->id, $cm, $groupmode);
|
|
|
158 |
|
|
|
159 |
$downloadallbutton = '';
|
|
|
160 |
if ($canmanage && $numissues > 0) {
|
|
|
161 |
$linkname = get_string('downloadallissuedcertificates', 'customcert');
|
|
|
162 |
$link = new moodle_url('/mod/customcert/view.php',
|
|
|
163 |
[
|
|
|
164 |
'id' => $cm->id,
|
|
|
165 |
'downloadall' => true,
|
|
|
166 |
'sesskey' => sesskey(),
|
|
|
167 |
]
|
|
|
168 |
);
|
|
|
169 |
$downloadallbutton = new single_button($link, $linkname, 'get', single_button::BUTTON_SECONDARY);
|
|
|
170 |
$downloadallbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem.
|
|
|
171 |
$downloadallbutton = $OUTPUT->render($downloadallbutton);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// Output all the page data.
|
|
|
175 |
echo $OUTPUT->header();
|
|
|
176 |
echo $issuehtml;
|
|
|
177 |
echo $downloadbutton;
|
|
|
178 |
echo $downloadallbutton;
|
|
|
179 |
if (isset($reporttable)) {
|
|
|
180 |
echo $OUTPUT->heading(get_string('listofissues', 'customcert', $numissues), 3);
|
|
|
181 |
groups_print_activity_menu($cm, $pageurl);
|
|
|
182 |
echo $reporttable->out($perpage, false);
|
|
|
183 |
}
|
|
|
184 |
echo $OUTPUT->footer($course);
|
|
|
185 |
exit();
|
|
|
186 |
} else if ($canreceive || $canmanage) { // Output to pdf.
|
|
|
187 |
// Set the userid value of who we are downloading the certificate for.
|
|
|
188 |
$userid = $USER->id;
|
|
|
189 |
if ($downloadown) {
|
|
|
190 |
// Create new customcert issue record if one does not already exist.
|
|
|
191 |
if (!$DB->record_exists('customcert_issues', ['userid' => $USER->id, 'customcertid' => $customcert->id])) {
|
|
|
192 |
\mod_customcert\certificate::issue_certificate($customcert->id, $USER->id);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
// Set the custom certificate as viewed.
|
|
|
196 |
$completion = new completion_info($course);
|
|
|
197 |
$completion->set_module_viewed($cm);
|
|
|
198 |
} else if ($downloadissue && $canviewreport) {
|
|
|
199 |
$userid = $downloadissue;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
// Hack alert - don't initiate the download when running Behat.
|
|
|
203 |
if (defined('BEHAT_SITE_RUNNING')) {
|
|
|
204 |
redirect(new moodle_url('/mod/customcert/view.php', ['id' => $cm->id]));
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
\core\session\manager::write_close();
|
|
|
208 |
|
|
|
209 |
// Now we want to generate the PDF.
|
|
|
210 |
$template = new \mod_customcert\template($template);
|
|
|
211 |
$template->generate_pdf(false, $userid);
|
|
|
212 |
exit();
|
|
|
213 |
}
|