|
| 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 | + * Class represents a local file of an issued certificate. |
| 19 | + * |
| 20 | + * @package mod_customcert |
| 21 | + * @copyright 2023 Giorgio Consorti <[email protected]> |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +namespace mod_customcert; |
| 26 | + |
| 27 | +use file_exception; |
| 28 | + |
| 29 | +defined('MOODLE_INTERNAL') || die(); |
| 30 | + |
| 31 | +/** |
| 32 | + * Class represents a local file of an issued certificate. |
| 33 | + * |
| 34 | + * @package mod_customcert |
| 35 | + * @copyright 023 Giorgio Consorti <[email protected]> |
| 36 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 37 | + */ |
| 38 | +class localfile { |
| 39 | + |
| 40 | + /** |
| 41 | + * The template representing the content of the file. |
| 42 | + * |
| 43 | + * @var \mod_customcert\template |
| 44 | + */ |
| 45 | + protected $template; |
| 46 | + |
| 47 | + /** |
| 48 | + * The component name for the file storage. |
| 49 | + */ |
| 50 | + const component = 'mod_customcert'; |
| 51 | + |
| 52 | + /** |
| 53 | + * The filearea name for the file storage. |
| 54 | + */ |
| 55 | + const filearea = 'customcert_issues'; |
| 56 | + |
| 57 | + /** |
| 58 | + * The constructor. |
| 59 | + * |
| 60 | + * @param \mod_customcert\template $template |
| 61 | + */ |
| 62 | + public function __construct(\mod_customcert\template $template) { |
| 63 | + $this->template = $template; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Save the PDF to the file storage. |
| 68 | + * |
| 69 | + * @param string $pdfcontent string content of the pdf |
| 70 | + * @param integer|null $userid the id of the user whose certificate we want to save |
| 71 | + * @return stored_file|false the stored_file object on success, false on error |
| 72 | + */ |
| 73 | + public function savePDF(string $pdfcontent, ?int $userid = null) { |
| 74 | + global $CFG, $USER; |
| 75 | + require_once($CFG->libdir . '/filelib.php'); |
| 76 | + |
| 77 | + if (empty($userid)) { |
| 78 | + $userid = $USER->id; |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + $file = $this->getPDF($userid); |
| 83 | + if (!$file) { |
| 84 | + // Create file containing the pdf |
| 85 | + $fs = get_file_storage(); |
| 86 | + $file = $fs->create_file_from_string($this->buildFileInfo($userid), $pdfcontent); |
| 87 | + } |
| 88 | + return $file; |
| 89 | + } catch (file_exception $e) { |
| 90 | + // maybe log the exception |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the PDF from the file storage. |
| 97 | + * |
| 98 | + * @param integer|null $userid the id of the user whose certificate we want to get |
| 99 | + * @return \stored_file|false the stored_file object on success, false on error |
| 100 | + */ |
| 101 | + public function getPDF(?int $userid = null) { |
| 102 | + global $CFG, $USER; |
| 103 | + require_once($CFG->libdir . '/filelib.php'); |
| 104 | + |
| 105 | + if (empty($userid)) { |
| 106 | + $userid = $USER->id; |
| 107 | + } |
| 108 | + |
| 109 | + $fileinfo = $this->buildFileInfo($userid); |
| 110 | + $fs = get_file_storage(); |
| 111 | + return $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], |
| 112 | + $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Delete the PDF from the file storage. |
| 117 | + * |
| 118 | + * @param integer|null $userid the id of the user whose certificate we want to get |
| 119 | + * @return bool true on success |
| 120 | + */ |
| 121 | + public function deletePDF(?int $userid = null) { |
| 122 | + global $USER; |
| 123 | + |
| 124 | + if (empty($userid)) { |
| 125 | + $userid = $USER->id; |
| 126 | + } |
| 127 | + |
| 128 | + try { |
| 129 | + $file = $this->getPDF($userid); |
| 130 | + if ($file) { |
| 131 | + return $file->delete(); |
| 132 | + } |
| 133 | + return false; |
| 134 | + } catch (file_exception $e) { |
| 135 | + // maybe log the exception |
| 136 | + return false; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Send the PDF to the browser or return it as a string. |
| 142 | + * |
| 143 | + * @param int $userid the id of the user whose certificate we want to view |
| 144 | + * @param string $deliveryoption the delivery option of the customcert |
| 145 | + * @param bool $return Do we want to return the contents of the PDF? |
| 146 | + * @return string|void Can return the PDF in string format if specified. |
| 147 | + */ |
| 148 | + public function sendPDF(?int $userid = NULL, string $deliveryoption = certificate::DELIVERY_OPTION_DOWNLOAD, bool $return = false) { |
| 149 | + global $USER; |
| 150 | + |
| 151 | + if (empty($userid)) { |
| 152 | + $userid = $USER->id; |
| 153 | + } |
| 154 | + |
| 155 | + $file = $this->getPDF($userid); |
| 156 | + if ($file) { |
| 157 | + if ($return) { |
| 158 | + return $file->get_content(); |
| 159 | + } else { |
| 160 | + // send the file to the browser |
| 161 | + send_stored_file( |
| 162 | + $file, |
| 163 | + 0, |
| 164 | + 0, |
| 165 | + $deliveryoption == certificate::DELIVERY_OPTION_DOWNLOAD, |
| 166 | + ['filename' => $file->get_filename()] |
| 167 | + ); |
| 168 | + die(); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Check if a pdf exists in the file storage area. |
| 175 | + * |
| 176 | + * @param \stdClass $cm the course module |
| 177 | + * @param integer|null $userid the id of the user whose PDF we want to check |
| 178 | + * @param integer|null $templateid the template id of the customcert we want to check |
| 179 | + * @return \stored_file|false the stored_file object on success, false on error |
| 180 | + */ |
| 181 | + public static function existsPDF($cm, ?int $userid = null, ?int $templateid = null) { |
| 182 | + |
| 183 | + $fileinfo = self::buildFileInfoArr($cm, $userid, $templateid); |
| 184 | + $fs = get_file_storage(); |
| 185 | + return $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], |
| 186 | + $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Build the fileinfo array needed by the file storage. |
| 191 | + * |
| 192 | + * @param integer|null $userid the id of the user whose fileinfo array we want to generate |
| 193 | + * @return array the fileinfo array |
| 194 | + */ |
| 195 | + protected function buildFileInfo(?int $userid = null) { |
| 196 | + |
| 197 | + return self::buildFileInfoArr($this->template->get_cm(), $userid, $this->template->get_id()); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Build the fileinfo array needed by the file storage, static version. |
| 202 | + * |
| 203 | + * @param \stdClass $cm the course module |
| 204 | + * @param integer|null $userid the id of the user whose fileinfo array we want to generate |
| 205 | + * @param integer|null $templateid the template id of the customcert of the array we want to generate |
| 206 | + * @return array the fileinfo array |
| 207 | + */ |
| 208 | + private static function buildFileInfoArr ($cm, ?int $userid = null, ?int $templateid = null) { |
| 209 | + |
| 210 | + /** @var \moodle_database $DB */ |
| 211 | + global $DB, $USER; |
| 212 | + |
| 213 | + if (empty($userid)) { |
| 214 | + $userid = $USER->id; |
| 215 | + } |
| 216 | + |
| 217 | + if (empty($templateid)) { |
| 218 | + $customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST); |
| 219 | + $templateid = $customcert->templateid; |
| 220 | + } |
| 221 | + |
| 222 | + $course = $DB->get_record('course', ['id' => $cm->course]); |
| 223 | + $context = $DB->get_record('context', ['contextlevel' => '50', 'instanceid' => $course->id]); |
| 224 | + $user_info = $DB->get_record('user', ['id' => $userid]); |
| 225 | + |
| 226 | + return [ |
| 227 | + 'contextid' => $context->id, |
| 228 | + 'component' => self::component, |
| 229 | + 'filearea' => self::filearea, |
| 230 | + 'itemid' => $templateid, |
| 231 | + 'userid' => $USER->id, |
| 232 | + 'author' => fullname($USER), |
| 233 | + 'filepath' => '/' . $course->id . '/', |
| 234 | + 'filename' => self::buildFileName($user_info->username, $templateid, $course->shortname), |
| 235 | + ]; |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Build the PDF filename. |
| 240 | + * |
| 241 | + * @param string $username |
| 242 | + * @param string $templateid |
| 243 | + * @param string $courseShortname |
| 244 | + * @return string the PDF file name |
| 245 | + */ |
| 246 | + public static function buildFileName($username, $templateid, $courseShortname) { |
| 247 | + return $username . '_cert-' . $templateid . '_course-' . $courseShortname . '.pdf'; |
| 248 | + } |
| 249 | +} |
0 commit comments