#44 Added page to view all certificates for a user

This commit is contained in:
Mark Nelson 2016-10-22 07:16:13 +10:00
parent 527d453950
commit 6230759d56
7 changed files with 329 additions and 3 deletions

View file

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

View file

@ -0,0 +1,178 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The report that displays the certificates the user has throughout the site.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @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 <markn@moodle.com>
* @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;
}
}

View file

@ -59,4 +59,12 @@ $capabilities = array(
'manager' => CAP_ALLOW
)
),
'mod/customcert:viewallcertificates' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
)
),
);

View file

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

16
lib.php
View file

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

79
my_certificates.php Normal file
View file

@ -0,0 +1,79 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Handles viewing the certificates for a certain user.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @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();

View file

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