From 6230759d56f731b253629d75f941d8c023d7f6ce Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sat, 22 Oct 2016 07:16:13 +1000 Subject: [PATCH] #44 Added page to view all certificates for a user --- classes/certificate.php | 46 +++++++- classes/my_certificates_table.php | 178 ++++++++++++++++++++++++++++++ db/access.php | 8 ++ lang/en/customcert.php | 1 + lib.php | 16 +++ my_certificates.php | 79 +++++++++++++ version.php | 4 +- 7 files changed, 329 insertions(+), 3 deletions(-) create mode 100644 classes/my_certificates_table.php create mode 100644 my_certificates.php diff --git a/classes/certificate.php b/classes/certificate.php index 1eccf73..a54534e 100644 --- a/classes/certificate.php +++ b/classes/certificate.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); /** * Class certificate. * - * Represents a customcert activity instance. + * Helper functionality for certificates. */ class certificate { @@ -364,6 +364,50 @@ class certificate { return array($conditionssql, $conditionsparams); } + /** + * Get number of certificates for a user. + * + * @param int $userid + * @return int + */ + public static function get_number_of_certificates_for_user($userid) { + global $DB; + + $sql = "SELECT COUNT(*) + FROM {customcert} c + INNER JOIN {customcert_issues} ci + ON c.id = ci.customcertid + WHERE ci.userid = :userid"; + return $DB->count_records_sql($sql, array('userid' => $userid)); + } + + /** + * Gets the certificates for the user. + * + * @param int $userid + * @param int $limitfrom + * @param int $limitnum + * @param string $sort + * @return array + */ + public static function get_certificates_for_user($userid, $limitfrom, $limitnum, $sort = '') { + global $DB; + + if (empty($sort)) { + $sort = 'ci.timecreated DESC'; + } + + $sql = "SELECT c.id, c.name, co.fullname as coursename, ci.code, ci.timecreated + FROM {customcert} c + INNER JOIN {customcert_issues} ci + ON c.id = ci.customcertid + INNER JOIN {course} co + ON c.course = co.id + WHERE ci.userid = :userid + ORDER BY $sort"; + return $DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitnum); + } + /** * Generates a 10-digit code of random letters and numbers. * diff --git a/classes/my_certificates_table.php b/classes/my_certificates_table.php new file mode 100644 index 0000000..b0c0858 --- /dev/null +++ b/classes/my_certificates_table.php @@ -0,0 +1,178 @@ +. + +/** + * The report that displays the certificates the user has throughout the site. + * + * @package mod_customcert + * @copyright 2016 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_customcert; + +defined('MOODLE_INTERNAL') || die; + +global $CFG; + +require_once($CFG->libdir . '/tablelib.php'); + +/** + * Class for the report that displays the certificates the user has throughout the site. + * + * @package mod_customcert + * @copyright 2016 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class my_certificates_table extends \table_sql { + + /** + * @var int $userid The user id + */ + protected $userid; + + /** + * Sets up the table. + * + * @param int $userid + * @param string|null $download The file type, null if we are not downloading + */ + public function __construct($userid, $download = null) { + parent::__construct('mod_customcert_report_table'); + + $columns = array( + 'name', + 'coursename', + 'timecreated', + 'code' + ); + $headers = array( + get_string('name'), + get_string('course'), + get_string('receiveddate', 'customcert'), + get_string('code', 'customcert') + ); + + // Check if we were passed a filename, which means we want to download it. + if ($download) { + $this->is_downloading($download, 'customcert-report'); + } + + if (!$this->is_downloading()) { + $columns[] = 'download'; + $headers[] = get_string('file'); + } + + $this->define_columns($columns); + $this->define_headers($headers); + $this->collapsible(false); + $this->sortable(true); + $this->no_sorting('code'); + $this->no_sorting('download'); + $this->is_downloadable(true); + + $this->userid = $userid; + } + + /** + * Generate the name column. + * + * @param \stdClass $certificate + * @return string + */ + public function col_name($certificate) { + return $certificate->name; + } + + /** + * Generate the course name column. + * + * @param \stdClass $certificate + * @return string + */ + public function col_coursename($certificate) { + return $certificate->coursename; + } + + /** + * Generate the certificate time created column. + * + * @param \stdClass $certificate + * @return string + */ + public function col_timecreated($certificate) { + return userdate($certificate->timecreated); + } + + /** + * Generate the code column. + * + * @param \stdClass $certificate + * @return string + */ + public function col_code($certificate) { + return $certificate->code; + } + + /** + * Generate the download column. + * + * @param \stdClass $certificate + * @return string + */ + public function col_download($certificate) { + global $OUTPUT; + + $icon = new \pix_icon('i/import', get_string('download')); + $link = new \moodle_url('/mod/customcert/my_certificates.php', + array('userid' => $this->userid, + 'certificateid' => $certificate->id, + 'downloadcert' => '1')); + + return $OUTPUT->action_link($link, '', null, null, $icon); + } + + /** + * Query the reader. + * + * @param int $pagesize size of page for paginated displayed table. + * @param bool $useinitialsbar do you want to use the initials bar. + */ + public function query_db($pagesize, $useinitialsbar = true) { + $total = certificate::get_number_of_certificates_for_user($this->userid); + + $this->pagesize($pagesize, $total); + + $this->rawdata = certificate::get_certificates_for_user($this->userid, $this->get_page_start(), + $this->get_page_size(), $this->get_sql_sort()); + + // Set initial bars. + if ($useinitialsbar) { + $this->initialbars($total > $pagesize); + } + } + + /** + * Download the data. + */ + public function download() { + \core\session\manager::write_close(); + $total = certificate::get_number_of_certificates_for_user($this->userid); + $this->out($total, false); + exit; + } +} + diff --git a/db/access.php b/db/access.php index 407e96b..3db4317 100644 --- a/db/access.php +++ b/db/access.php @@ -59,4 +59,12 @@ $capabilities = array( 'manager' => CAP_ALLOW ) ), + + 'mod/customcert:viewallcertificates' => array( + 'captype' => 'read', + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => array( + 'manager' => CAP_ALLOW + ) + ), ); diff --git a/lang/en/customcert.php b/lang/en/customcert.php index b8811a0..9a47363 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -83,6 +83,7 @@ $string['modulename'] = 'Custom certificate'; $string['modulenameplural'] = 'Custom certificates'; $string['modulename_help'] = 'This module allows for the dynamic generation of PDF certificates.'; $string['modulename_link'] = 'Custom_certificate_module'; +$string['mycertificates'] = 'My certificates'; $string['name'] = 'Name'; $string['nocustomcerts'] = 'There are no custom certificates for this course'; $string['noimage'] = 'No image'; diff --git a/lib.php b/lib.php index 2c747f2..5fe342c 100644 --- a/lib.php +++ b/lib.php @@ -355,6 +355,22 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na return $customcertnode->trim_if_empty(); } +/** + * Add nodes to myprofile page. + * + * @param \core_user\output\myprofile\tree $tree Tree object + * @param stdClass $user user object + * @param bool $iscurrentuser + * @param stdClass $course Course object + * @return bool + */ +function mod_customcert_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) { + $url = new moodle_url('/mod/customcert/my_certificates.php', array('userid' => $user->id)); + $node = new core_user\output\myprofile\node('miscellaneous', 'mycustomcerts', + get_string('mycertificates', 'customcert'), null, $url); + $tree->add_node($node); +} + /** * Handles editing the 'name' of the element in a list. * diff --git a/my_certificates.php b/my_certificates.php new file mode 100644 index 0000000..203b5ce --- /dev/null +++ b/my_certificates.php @@ -0,0 +1,79 @@ +. + +/** + * Handles viewing the certificates for a certain user. + * + * @package mod_customcert + * @copyright 2016 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('../../config.php'); + +$userid = optional_param('userid', $USER->id, PARAM_INT); +$download = optional_param('download', null, PARAM_ALPHA); +$downloadcert = optional_param('downloadcert', '', PARAM_BOOL); +if ($downloadcert) { + $certificateid = required_param('certificateid', PARAM_INT); + $customcert = $DB->get_record('customcert', array('id' => $certificateid), '*', MUST_EXIST); +} +$page = optional_param('page', 0, PARAM_INT); +$perpage = optional_param('perpage', \mod_customcert\certificate::CUSTOMCERT_PER_PAGE, PARAM_INT); +$pageurl = $url = new moodle_url('/mod/customcert/my_certificates.php', array('userid' => $userid, + 'page' => $page, 'perpage' => $perpage)); + +// Requires a login. +require_login(); + +// Check that we have a valid user. +$user = \core_user::get_user($userid, '*', MUST_EXIST); + +// If we are viewing certificates that are not for the currently logged in user then do a capability check. +if (($userid != $USER->id) && !has_capability('mod/customcert:viewallcertificates', context_system::instance())) { + print_error('You are not allowed to view these certificates'); +} + +// Check if we requested to download a 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(); +} + +$table = new \mod_customcert\my_certificates_table($userid, $download); +$table->define_baseurl($pageurl); + +if ($table->is_downloading()) { + $table->download(); + exit(); +} + +$PAGE->set_url($pageurl); +$PAGE->set_context(context_user::instance($userid)); +$PAGE->set_title(get_string('mycertificates', 'customcert')); +$PAGE->set_pagelayout('standard'); +$PAGE->navigation->extend_for_user($user); + +// Additional page setup. +$PAGE->navbar->add(get_string('profile'), new moodle_url('/user/profile.php', array('id' => $userid))); +$PAGE->navbar->add(get_string('mycertificates', 'customcert')); + +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('mycertificates', 'customcert')); +$table->out($perpage, false); +echo $OUTPUT->footer(); diff --git a/version.php b/version.php index 81aad54..e1b9c77 100644 --- a/version.php +++ b/version.php @@ -24,10 +24,10 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2016052304; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2016052305; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2016052300; // Requires this Moodle version (3.1). $plugin->cron = 0; // Period for cron to check this module (secs). $plugin->component = 'mod_customcert'; $plugin->maturity = MATURITY_STABLE; -$plugin->release = "3.1 release (Build: 2016052304)"; // User-friendly version number. +$plugin->release = "3.1 release (Build: 2016052305)"; // User-friendly version number.