#6 Added ability to view other users' certificates

This commit is contained in:
Mark Nelson 2016-08-23 16:22:58 +08:00
parent a135007160
commit ff049e2c1b
16 changed files with 90 additions and 32 deletions

View file

@ -160,8 +160,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.

View file

@ -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.
*

View file

@ -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);
}
}
}

View file

@ -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);

View file

@ -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());
}

View file

@ -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;
}

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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.

View file

@ -133,8 +133,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.

View file

@ -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));
}
/**

View file

@ -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));

View file

@ -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);
}

View file

@ -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);

View file

@ -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);
}