|
| 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 zip and download of certificates. |
| 19 | + * |
| 20 | + * Derived from the local_bulkcustomcert by Gonzalo Romero. |
| 21 | + * |
| 22 | + * @package mod_customcert |
| 23 | + * @author Gonzalo Romero |
| 24 | + * @author Giorgio Consorti <[email protected]> |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + */ |
| 27 | + |
| 28 | +// defined('MOODLE_INTERNAL') || die(); |
| 29 | + |
| 30 | +require_once(__DIR__ . '/../../config.php'); |
| 31 | + |
| 32 | +$courseid = optional_param('courseid', null, PARAM_INT); |
| 33 | +$customcertid = optional_param('customcertid', null, PARAM_INT); |
| 34 | + |
| 35 | +if (!has_capability('mod/customcert:viewallcertificates', context_system::instance()) && !$courseid && !$customcert) { |
| 36 | + die(); |
| 37 | +} |
| 38 | + |
| 39 | +global $DB; |
| 40 | + |
| 41 | +// Increase the server timeout to handle the creation and sending of large zip files. |
| 42 | +core_php_time_limit::raise(); |
| 43 | + |
| 44 | +if ($courseid) { |
| 45 | + $course = $DB->get_record('course', ['id' => $courseid]); |
| 46 | + $certs = $DB->get_records('customcert', ['course' => $courseid]); |
| 47 | +} else if ($customcertid) { |
| 48 | + $cert = $DB->get_record('customcert', ['id' => $customcertid], '*', MUST_EXIST); |
| 49 | + $certs[$cert->id] = $cert; |
| 50 | + $course = $DB->get_record('course', ['id' => $certs[$cert->id]->course], '*', MUST_EXIST); |
| 51 | + $courseid = $course->id; |
| 52 | + unset($cert); |
| 53 | +} |
| 54 | + |
| 55 | +$context = $DB->get_record('context', ['contextlevel' => '50', 'instanceid' => $courseid]); |
| 56 | +$users = $DB->get_records('role_assignments', ['contextid' => $context->id]); |
| 57 | + |
| 58 | +// Build a list of files to zip. |
| 59 | +$filesforzipping = []; |
| 60 | + |
| 61 | +foreach ($certs as $certid => $cert_fields) { |
| 62 | + $template = null; |
| 63 | + foreach ($users as $userid => $user_fields) { |
| 64 | + if (!$DB->get_record('customcert_issues', ['userid' => $user_fields->userid, 'customcertid' => $certid])) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + if (is_null($template)) { |
| 68 | + $template = $DB->get_record('customcert_templates', ['id' => $cert_fields->templateid], '*', MUST_EXIST); |
| 69 | + $template = new \mod_customcert\template($template); |
| 70 | + } |
| 71 | + $lf = new \mod_customcert\localfile($template); |
| 72 | + if (false === $file = $lf->getPDF($user_fields->userid)) { |
| 73 | + // must generate the pdf |
| 74 | + $pdf = $template->generate_pdf(false, $user_fields->userid, true); |
| 75 | + if (!empty($pdf)) { |
| 76 | + $file = $lf->getPDF($user_fields->userid); |
| 77 | + } |
| 78 | + } |
| 79 | + if ($file) { |
| 80 | + $filesforzipping['/' . $course->shortname . '/' . $cert_fields->name . '/' .$file->get_filename()] = $file; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +if (count($filesforzipping) == 0) { |
| 86 | + // This should never happen. The option only show up if there is available certs. |
| 87 | + $url = new moodle_url('/course/view.php?id=' . $courseid); |
| 88 | + redirect($url); |
| 89 | +} else if ($zipfile = pack_files($filesforzipping)) { |
| 90 | + send_temp_file($zipfile, get_string('modulenameplural', 'customcert') . '-' . $course->shortname . '.zip'); |
| 91 | +} |
| 92 | +die(); |
| 93 | + |
| 94 | + |
| 95 | +function pack_files($filesforzipping) |
| 96 | +{ |
| 97 | + global $CFG; |
| 98 | + // Create path for new zip file. |
| 99 | + $tempzip = tempnam($CFG->tempdir . '/', 'customcert_'); |
| 100 | + // Zip files. |
| 101 | + $zipper = new zip_packer(); |
| 102 | + if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) { |
| 103 | + return $tempzip; |
| 104 | + } |
| 105 | + return false; |
| 106 | +} |
0 commit comments