|
| 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 | + * Version details |
| 19 | + * |
| 20 | + * Download certificates block |
| 21 | + * -------------------------- |
| 22 | + * Displays all issued certificates for users with unique codes. |
| 23 | + * The certificates will also be issued for courses that have been archived since issuing of the certificates. |
| 24 | + * |
| 25 | + * @copyright 2015 onwards Manieer Chhettri | Marie Curie, UK | <[email protected]> |
| 26 | + * @author Manieer Chhettri | Marie Curie, UK | <[email protected]> | 2015 |
| 27 | + * @package block_download_certificates |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | + |
| 31 | +defined('MOODLE_INTERNAL') || die(); |
| 32 | + |
| 33 | +require_once($CFG->dirroot.'/mod/certificate/locallib.php'); |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * Download previously issued certificates. |
| 38 | + * |
| 39 | + * Displays all previously issued certificates for logged in user. |
| 40 | + * |
| 41 | + * @copyright 2015 onwards Manieer Chhettri | Marie Curie, UK | <[email protected]>. |
| 42 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 43 | + */ |
| 44 | +class block_download_certificates extends block_base { |
| 45 | + |
| 46 | + /** |
| 47 | + * |
| 48 | + * Download previously issued certificates. |
| 49 | + * |
| 50 | + * Displays all previously issued certificates for logged in user. |
| 51 | + */ |
| 52 | + public function init() { |
| 53 | + $this->title = get_string('download_certificates', 'block_download_certificates'); |
| 54 | + $this->version = 2016081200; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * |
| 59 | + * Restricting Applicable formats. |
| 60 | + * |
| 61 | + * Restricting where the blocks can appear on the site (Frontpage and My Learning page only). |
| 62 | + */ |
| 63 | + public function applicable_formats() { |
| 64 | + return array('all' => true, |
| 65 | + 'course-view' => false, |
| 66 | + 'mod' => false); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * |
| 71 | + * Multiple instances of the block. |
| 72 | + * |
| 73 | + * Allowing multiple instance of the block to appear throughtout the site pages. |
| 74 | + */ |
| 75 | + public function instance_allow_multiple() { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * |
| 81 | + * Retrieving relevant required data. |
| 82 | + * |
| 83 | + * Retrieving data and populating them for displaying on the block. |
| 84 | + */ |
| 85 | + public function get_content() { |
| 86 | + global $USER, $DB, $CFG; |
| 87 | + |
| 88 | + if ($this->content !== null) { |
| 89 | + return $this->content; |
| 90 | + } |
| 91 | + |
| 92 | + // User ID. |
| 93 | + $userid = optional_param('userid', $USER->id, PARAM_INT); |
| 94 | + |
| 95 | + $this->content = new stdClass; |
| 96 | + $this->content->items = array(); |
| 97 | + $this->content->icons = array(); |
| 98 | + $this->content->footer = ''; |
| 99 | + |
| 100 | + // Table headers. |
| 101 | + $table = new html_table(); |
| 102 | + |
| 103 | + $sql = "SELECT f.id AS fid, f.userid AS fuserid, f.contextid AS fcontextid, f.filename AS ffilename, |
| 104 | + ctx.id AS ctxid, ctx.contextlevel AS ctxcontextlevel, ctx.instanceid AS ctxinstanceid, |
| 105 | + cm.id AS cmid, cm.course AS cmcourse, cm.module AS cmmodule, cm.instance AS cminstance, |
| 106 | + crt.id AS crtid, crt.course AS crtcourse, crt.name AS crtname, ci.id AS ciid, |
| 107 | + ci.userid AS ciuserid, ci.certificateid AS cicertificateid, ci.code AS cicode, |
| 108 | + ci.timecreated AS citimecreated, c.id AS cid, c.fullname AS cfullname, |
| 109 | + c.shortname AS cshortname |
| 110 | + FROM {files} f |
| 111 | + INNER JOIN {context} ctx |
| 112 | + ON ctx.id = f.contextid |
| 113 | + INNER JOIN {course_modules} cm |
| 114 | + ON cm.id = ctx.instanceid |
| 115 | + INNER JOIN {certificate} crt |
| 116 | + ON crt.id = cm.instance |
| 117 | + LEFT JOIN {certificate_issues} ci |
| 118 | + ON ci.certificateid = crt.id |
| 119 | + INNER JOIN {course} c |
| 120 | + ON c.id = crt.course |
| 121 | +
|
| 122 | + WHERE f.userid = ci.userid AND |
| 123 | + f.userid = :userid AND |
| 124 | + f.component = 'mod_certificate' AND |
| 125 | + f.mimetype = 'application/pdf' |
| 126 | + ORDER BY ci.timecreated DESC"; |
| 127 | + // PDF FILES ONLY (f.mimetype = 'application/pdf'). |
| 128 | + |
| 129 | + $limit = " LIMIT 5"; // Limiting the output results to just five records. |
| 130 | + $certificates = $DB->get_records_sql($sql.$limit, array('userid' => $USER->id)); |
| 131 | + |
| 132 | + if (empty($certificates)) { |
| 133 | + $this->content->text = get_string('download_certificates_noreports', 'block_download_certificates'); |
| 134 | + return $this->content; |
| 135 | + } else { |
| 136 | + foreach ($certificates as $certdata) { |
| 137 | + |
| 138 | + $certdata->printdate = 1; // Modify printdate so that date is always printed. |
| 139 | + $certdata->printgrade = 1; // Modify printgrade so that grade is always printed. |
| 140 | + $certdata->gradefmt = 1; |
| 141 | + // Modify gradefmt so that correct suffix is printed. 1=percentage, 2=points and 3=letter. |
| 142 | + |
| 143 | + $certrecord = new stdClass(); |
| 144 | + $certrecord->timecreated = $certdata->citimecreated; |
| 145 | + |
| 146 | + // Date format. |
| 147 | + $dateformat = get_string('strftimedate', 'langconfig'); |
| 148 | + |
| 149 | + // Required variables for output. |
| 150 | + $userid = $certrecord->userid = $certdata->fuserid; |
| 151 | + $certificateissueid = $certrecord->certificateissueid = $certdata->ciid; |
| 152 | + $contextid = $certrecord->contextid = $certdata->ctxid; |
| 153 | + $courseid = $certrecord->id = $certdata->cid; |
| 154 | + $coursename = $certrecord->fullname = $certdata->cfullname; |
| 155 | + $filename = $certrecord->filename = $certdata->ffilename; |
| 156 | + $certificatename = $certrecord->name = $certdata->crtname; |
| 157 | + $code = $certrecord->code = $certdata->cicode; |
| 158 | + |
| 159 | + // Retrieving grade and date for each certificate. |
| 160 | + $grade = certificate_get_grade($certdata, $certrecord, $userid, $valueonly = true); |
| 161 | + $date = $certrecord->timecreated = $certdata->citimecreated; |
| 162 | + |
| 163 | + // Linkable Direct course. Use $courselink for clickable course link. |
| 164 | + $courselink = html_writer::link(new moodle_url('/course/view.php', array('id' => $courseid)), |
| 165 | + "<strong>" . $coursename . "</strong>", array('fullname' => $coursename)) . "<br><em>" . |
| 166 | + " Certificate Name: " . $certificatename . "<br>" . |
| 167 | + " [Issued on: " . userdate($date, $dateformat) . " | Code: " . $code . "]</em>"; |
| 168 | + |
| 169 | + // Non - Linkable course title only. The course link isn't linkable. |
| 170 | + $link = "<strong>" . $coursename . "</strong>" . "<br>" . |
| 171 | + "<em>[" . $certificatename . " | " . userdate($date, $dateformat) . " | " . $code . "]</em>"; |
| 172 | + |
| 173 | + // Direct certificate download link. |
| 174 | + $filelink = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/' |
| 175 | + .$contextid . '/mod_certificate/issue/' . $certificateissueid . '/' . $filename); |
| 176 | + |
| 177 | + $imglink = html_writer::empty_tag('img', array('src' => new moodle_url( |
| 178 | + '/blocks/download_certificates/pix/download.png'), 'alt' => "Please download", 'height' => 40, 'width' => 40)); |
| 179 | + |
| 180 | + $outputlink = '<a href="'.$filelink.'" >' . $imglink . '</a>'; |
| 181 | + $table->data[] = array ($link, $outputlink); |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + $this->content->footer = html_writer::link(new moodle_url('/blocks/download_certificates/report.php', |
| 186 | + array('userid' => $USER->id)), |
| 187 | + get_string('download_certificates_footermessage', 'block_download_certificates')); |
| 188 | + } |
| 189 | + |
| 190 | + $this->content->text = html_writer::table($table); |
| 191 | + return $this->content; |
| 192 | + } |
| 193 | +} |
0 commit comments