diff --git a/classes/element.php b/classes/element.php index 5f85ab2..13335c4 100644 --- a/classes/element.php +++ b/classes/element.php @@ -159,8 +159,9 @@ abstract class element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public abstract function render($pdf, $preview); + public abstract function render($pdf, $preview, $user); /** * Render the element in html. diff --git a/classes/report_table.php b/classes/report_table.php index 280738a..5267e55 100644 --- a/classes/report_table.php +++ b/classes/report_table.php @@ -68,15 +68,18 @@ class report_table extends \table_sql { 'fullname', 'timecreated', 'code', + 'download' )); $this->define_headers(array( get_string('fullname'), get_string('receiveddate', 'customcert'), - get_string('code', 'customcert') + get_string('code', 'customcert'), + get_string('file') )); $this->collapsible(false); $this->sortable(true); $this->no_sorting('code'); + $this->no_sorting('download'); $this->is_downloadable(true); $this->customcertid = $customcertid; @@ -116,6 +119,24 @@ class report_table extends \table_sql { return $user->code; } + /** + * Generate the download column. + * + * @param \stdClass $user + * @return string + */ + public function col_download($user) { + global $OUTPUT; + + $icon = new \pix_icon('i/import', get_string('download')); + $link = new \moodle_url('/mod/customcert/report.php', + array('id' => $this->cm->id, + 'downloadcert' => '1', + 'userid' => $user->id)); + + return $OUTPUT->action_link($link, '', null, null, $icon); + } + /** * Query the reader. * diff --git a/classes/template.php b/classes/template.php index a85107f..5fc04d4 100644 --- a/classes/template.php +++ b/classes/template.php @@ -247,9 +247,16 @@ class template { * Generate the PDF for the template. * * @param bool $preview true if it is a preview, false otherwise + * @param int $userid the id of the user whose certificate we want to view */ - public function generate_pdf($preview = false) { - global $CFG, $DB; + public function generate_pdf($preview = false, $userid = null) { + global $CFG, $DB, $USER; + + if (empty($userid)) { + $user = $USER; + } else { + $user = \core_user::get_user($userid); + } require_once($CFG->libdir . '/pdflib.php'); @@ -289,7 +296,7 @@ class template { foreach ($elements as $element) { // Get an instance of the element class. if ($e = \mod_customcert\element::instance($element)) { - $e->render($pdf, $preview); + $e->render($pdf, $preview, $user); } } } diff --git a/element/border/classes/element.php b/element/border/classes/element.php index a27d48d..5b2c877 100644 --- a/element/border/classes/element.php +++ b/element/border/classes/element.php @@ -47,8 +47,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { $colour = \TCPDF_COLORS::convertHTMLColorToDec($this->element->colour, $colour); $pdf->SetLineStyle(array('width' => $this->element->data, 'color' => $colour)); $pdf->Line(0, 0, $pdf->getPageWidth(), 0); diff --git a/element/categoryname/classes/element.php b/element/categoryname/classes/element.php index 8d47072..8b94996 100644 --- a/element/categoryname/classes/element.php +++ b/element/categoryname/classes/element.php @@ -32,8 +32,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { \mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name()); } diff --git a/element/code/classes/element.php b/element/code/classes/element.php index f090805..2cd0f22 100644 --- a/element/code/classes/element.php +++ b/element/code/classes/element.php @@ -32,9 +32,10 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { - global $DB, $USER; + public function render($pdf, $preview, $user) { + global $DB; if ($preview) { $code = \mod_customcert\certificate::generate_code(); @@ -44,7 +45,7 @@ class element extends \mod_customcert\element { // Get the customcert this page belongs to. $customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST); // Now we can get the issue for this user. - $issue = $DB->get_record('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id), '*', MUST_EXIST); + $issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id), '*', MUST_EXIST); $code = $issue->code; } diff --git a/element/coursename/classes/element.php b/element/coursename/classes/element.php index a00ca08..dcd4c9e 100644 --- a/element/coursename/classes/element.php +++ b/element/coursename/classes/element.php @@ -32,8 +32,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { global $COURSE; \mod_customcert\element_helper::render_content($pdf, $this, $COURSE->fullname); diff --git a/element/date/classes/element.php b/element/date/classes/element.php index 2c85621..8461ee4 100644 --- a/element/date/classes/element.php +++ b/element/date/classes/element.php @@ -83,9 +83,10 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { - global $COURSE, $DB, $USER; + public function render($pdf, $preview, $user) { + global $COURSE, $DB; // If there is no element data, we have nothing to display. if (empty($this->element->data)) { @@ -106,7 +107,7 @@ class element extends \mod_customcert\element { // Get the customcert this page belongs to. $customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST); // Now we can get the issue for this user. - $issue = $DB->get_record('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id), '*', MUST_EXIST); + $issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id), '*', MUST_EXIST); if ($dateitem == CUSTOMCERT_DATE_ISSUE) { $date = $issue->timecreated; diff --git a/element/grade/classes/element.php b/element/grade/classes/element.php index 7217fb0..401630b 100644 --- a/element/grade/classes/element.php +++ b/element/grade/classes/element.php @@ -83,9 +83,10 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { - global $COURSE, $USER; + public function render($pdf, $preview, $user) { + global $COURSE; // If there is no element data, we have nothing to display. if (empty($this->element->data)) { @@ -101,7 +102,7 @@ class element extends \mod_customcert\element { $grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, 2); } else { // Get the grade for the grade item. - $grade = self::get_grade($gradeinfo, $USER->id); + $grade = self::get_grade($gradeinfo, $user->id); } \mod_customcert\element_helper::render_content($pdf, $this, $grade); diff --git a/element/gradeitemname/classes/element.php b/element/gradeitemname/classes/element.php index d677f90..4721b1f 100644 --- a/element/gradeitemname/classes/element.php +++ b/element/gradeitemname/classes/element.php @@ -60,8 +60,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { global $DB; // Check that the grade item is not empty. diff --git a/element/image/classes/element.php b/element/image/classes/element.php index 39b817a..49b9ca1 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -132,8 +132,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { global $CFG; // If there is no element data, we have nothing to display. diff --git a/element/studentname/classes/element.php b/element/studentname/classes/element.php index 1f870e4..6ddde2d 100644 --- a/element/studentname/classes/element.php +++ b/element/studentname/classes/element.php @@ -32,11 +32,10 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { - global $USER; - - \mod_customcert\element_helper::render_content($pdf, $this, fullname($USER)); + public function render($pdf, $preview, $user) { + \mod_customcert\element_helper::render_content($pdf, $this, fullname($user)); } /** diff --git a/element/teachername/classes/element.php b/element/teachername/classes/element.php index 286881c..feb4881 100644 --- a/element/teachername/classes/element.php +++ b/element/teachername/classes/element.php @@ -58,8 +58,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { global $DB; $teacher = $DB->get_record('user', array('id' => $this->element->data)); diff --git a/element/text/classes/element.php b/element/text/classes/element.php index c025b05..b3055d8 100644 --- a/element/text/classes/element.php +++ b/element/text/classes/element.php @@ -56,8 +56,9 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { + public function render($pdf, $preview, $user) { \mod_customcert\element_helper::render_content($pdf, $this, $this->element->data); } diff --git a/element/userfield/classes/element.php b/element/userfield/classes/element.php index c331b47..918c7a2 100644 --- a/element/userfield/classes/element.php +++ b/element/userfield/classes/element.php @@ -87,9 +87,10 @@ class element extends \mod_customcert\element { * * @param \pdf $pdf the pdf object * @param bool $preview true if it is a preview, false otherwise + * @param \stdClass $user the user we are rendering this for */ - public function render($pdf, $preview) { - global $DB, $USER; + public function render($pdf, $preview, $user) { + global $CFG, $DB; // The user field to display. $field = $this->element->data; @@ -97,10 +98,17 @@ class element extends \mod_customcert\element { $value = ''; if (is_number($field)) { // Must be a custom user profile field. if ($field = $DB->get_record('user_info_field', array('id' => $field))) { - $value = $USER->profile[$field->shortname]; + $file = $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php'; + if (file_exists($file)) { + require_once($CFG->dirroot . '/user/profile/lib.php'); + require_once($file); + $class = "profile_field_{$field->datatype}"; + $field = new $class($field->id, $user->id); + $value = $field->display_data(); + } } - } else if (!empty($USER->$field)) { // Field in the user table. - $value = $USER->$field; + } else if (!empty($user->$field)) { // Field in the user table. + $value = $user->$field; } \mod_customcert\element_helper::render_content($pdf, $this, $value); diff --git a/report.php b/report.php index c7563c7..a4f29e8 100644 --- a/report.php +++ b/report.php @@ -24,8 +24,12 @@ require_once('../../config.php'); -$id = required_param('id', PARAM_INT); +$id = required_param('id', PARAM_INT); $download = optional_param('download', '', PARAM_ALPHA); +$downloadcert = optional_param('downloadcert', '', PARAM_BOOL); +if ($downloadcert) { + $userid = required_param('userid', PARAM_INT); +} $page = optional_param('page', 0, PARAM_INT); $perpage = optional_param('perpage', \mod_customcert\certificate::CUSTOMCERT_PER_PAGE, PARAM_INT); @@ -42,7 +46,15 @@ require_login($course, false, $cm); $context = context_module::instance($cm->id); require_capability('mod/customcert:manage', $context); -// Get the users who have been issued. +// Check if we requested to download another user's certificate. +if ($downloadcert) { + $template = $DB->get_record('customcert_templates', array('id' => $customcert->templateid), '*', MUST_EXIST); + $template = new \mod_customcert\template($template); + $template->generate_pdf(false, $userid); + exit(); +} + +// Check if we are in group mode. if ($groupmode = groups_get_activity_groupmode($cm)) { groups_get_activity_group($cm, true); }