#4 Added ability to sort the reports table
The table is now an instance of 'table_sql', not 'html_table'. This made sorting easy to implement, with the added benefit of taking advantage of the spout library for exporting data.
This commit is contained in:
parent
0f77856e90
commit
d6ebc95c77
3 changed files with 174 additions and 110 deletions
|
@ -52,12 +52,7 @@ class certificate {
|
|||
* @var int the number of issues that will be displayed on each page in the report
|
||||
* If you want to display all customcerts on a page set this to 0.
|
||||
*/
|
||||
const CUSTOMCERT_PER_PAGE = '20';
|
||||
|
||||
/**
|
||||
* @var int the max number of issues to display
|
||||
*/
|
||||
const CUSTOMCERT_MAX_PER_PAGE = '300';
|
||||
const CUSTOMCERT_PER_PAGE = '50';
|
||||
|
||||
/**
|
||||
* Handles setting the protection field for the customcert
|
||||
|
@ -233,11 +228,12 @@ class certificate {
|
|||
* @param int $customcertid
|
||||
* @param bool $groupmode are we in group mode
|
||||
* @param \stdClass $cm the course module
|
||||
* @param int $page offset
|
||||
* @param int $perpage total per page
|
||||
* @param int $limitfrom
|
||||
* @param int $limitnum
|
||||
* @param string $sort
|
||||
* @return \stdClass the users
|
||||
*/
|
||||
public static function get_issues($customcertid, $groupmode, $cm, $page, $perpage) {
|
||||
public static function get_issues($customcertid, $groupmode, $cm, $limitfrom, $limitnum, $sort = '') {
|
||||
global $DB;
|
||||
|
||||
// Get the conditional SQL.
|
||||
|
@ -252,15 +248,21 @@ class certificate {
|
|||
$allparams = $conditionsparams + array('customcertid' => $customcertid);
|
||||
|
||||
// Return the issues.
|
||||
$sql = "SELECT u.*, ci.code, ci.timecreated
|
||||
$ufields = \user_picture::fields('u');
|
||||
$sql = "SELECT $ufields, ci.code, ci.timecreated
|
||||
FROM {user} u
|
||||
INNER JOIN {customcert_issues} ci
|
||||
ON u.id = ci.userid
|
||||
WHERE u.deleted = 0
|
||||
AND ci.customcertid = :customcertid
|
||||
$conditionssql
|
||||
ORDER BY " . $DB->sql_fullname();
|
||||
return $DB->get_records_sql($sql, $allparams, $page * $perpage, $perpage);
|
||||
$conditionssql";
|
||||
if ($sort) {
|
||||
$sql .= "ORDER BY " . $sort;
|
||||
} else {
|
||||
$sql .= "ORDER BY " . $DB->sql_fullname();
|
||||
}
|
||||
|
||||
return $DB->get_records_sql($sql, $allparams, $limitfrom, $limitnum);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -379,62 +381,4 @@ class certificate {
|
|||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the report.
|
||||
*
|
||||
* @param \stdClass $customcert
|
||||
* @param \stdClass $users the list of users who have had a customcert issued
|
||||
* @param string $type
|
||||
*/
|
||||
public static function generate_report_file($customcert, $users, $type) {
|
||||
global $CFG, $COURSE;
|
||||
|
||||
if ($type == 'ods') {
|
||||
require_once($CFG->libdir . '/odslib.class.php');
|
||||
$workbook = new \MoodleODSWorkbook('-');
|
||||
} else if ($type == 'xls') {
|
||||
require_once($CFG->libdir . '/excellib.class.php');
|
||||
$workbook = new \MoodleExcelWorkbook('-');
|
||||
}
|
||||
|
||||
$filename = clean_filename($COURSE->shortname . ' ' . rtrim($customcert->name, '.') . '.' . $type);
|
||||
|
||||
// Send HTTP headers.
|
||||
$workbook->send($filename);
|
||||
|
||||
// Creating the first worksheet.
|
||||
$myxls = $workbook->add_worksheet(get_string('report', 'customcert'));
|
||||
|
||||
// Print names of all the fields.
|
||||
$myxls->write_string(0, 0, get_string('lastname'));
|
||||
$myxls->write_string(0, 1, get_string('firstname'));
|
||||
$myxls->write_string(0, 2, get_string('idnumber'));
|
||||
$myxls->write_string(0, 3, get_string('group'));
|
||||
$myxls->write_string(0, 4, get_string('receiveddate', 'customcert'));
|
||||
$myxls->write_string(0, 5, get_string('code', 'customcert'));
|
||||
|
||||
// Generate the data for the body of the spreadsheet.
|
||||
$row = 1;
|
||||
if ($users) {
|
||||
foreach ($users as $user) {
|
||||
$myxls->write_string($row, 0, $user->lastname);
|
||||
$myxls->write_string($row, 1, $user->firstname);
|
||||
$studentid = (!empty($user->idnumber)) ? $user->idnumber : ' ';
|
||||
$myxls->write_string($row, 2, $studentid);
|
||||
$ug2 = '';
|
||||
if ($usergrps = groups_get_all_groups($COURSE->id, $user->id)) {
|
||||
foreach ($usergrps as $ug) {
|
||||
$ug2 = $ug2 . $ug->name;
|
||||
}
|
||||
}
|
||||
$myxls->write_string($row, 3, $ug2);
|
||||
$myxls->write_string($row, 4, userdate($user->timecreated));
|
||||
$myxls->write_string($row, 5, $user->code);
|
||||
$row++;
|
||||
}
|
||||
}
|
||||
// Close the workbook.
|
||||
$workbook->close();
|
||||
}
|
||||
}
|
||||
|
|
149
classes/report_table.php
Normal file
149
classes/report_table.php
Normal file
|
@ -0,0 +1,149 @@
|
|||
<?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 issued certificates.
|
||||
*
|
||||
* @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 issued certificates.
|
||||
*
|
||||
* @package mod_customcert
|
||||
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class report_table extends \table_sql {
|
||||
|
||||
/**
|
||||
* @var int $customcertid The custom certificate id
|
||||
*/
|
||||
protected $customcertid;
|
||||
|
||||
/**
|
||||
* @var \stdClass $cm The course module.
|
||||
*/
|
||||
protected $cm;
|
||||
|
||||
/**
|
||||
* @var bool $groupmode are we in group mode?
|
||||
*/
|
||||
protected $groupmode;
|
||||
|
||||
/**
|
||||
* Sets up the table.
|
||||
*
|
||||
* @param int $customcertid
|
||||
* @param \stdClass $cm the course module
|
||||
* @param bool $groupmode are we in group mode?
|
||||
*/
|
||||
public function __construct($customcertid, $cm, $groupmode) {
|
||||
parent::__construct('mod_customcert_report_table');
|
||||
|
||||
$this->define_columns(array(
|
||||
'fullname',
|
||||
'timecreated',
|
||||
'code',
|
||||
));
|
||||
$this->define_headers(array(
|
||||
get_string('fullname'),
|
||||
get_string('receiveddate', 'customcert'),
|
||||
get_string('code', 'customcert')
|
||||
));
|
||||
$this->collapsible(false);
|
||||
$this->sortable(true);
|
||||
$this->no_sorting('code');
|
||||
$this->is_downloadable(true);
|
||||
|
||||
$this->customcertid = $customcertid;
|
||||
$this->cm = $cm;
|
||||
$this->groupmode = $groupmode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the fullname column.
|
||||
*
|
||||
* @param \stdClass $user
|
||||
* @return string
|
||||
*/
|
||||
public function col_fullname($user) {
|
||||
global $OUTPUT;
|
||||
|
||||
return $OUTPUT->user_picture($user) . ' ' . fullname($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the certificate time created column.
|
||||
*
|
||||
* @param \stdClass $user
|
||||
* @return string
|
||||
*/
|
||||
public function col_timecreated($user) {
|
||||
return userdate($user->timecreated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the code column.
|
||||
*
|
||||
* @param \stdClass $user
|
||||
* @return string
|
||||
*/
|
||||
public function col_code($user) {
|
||||
return $user->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = \mod_customcert\certificate::get_number_of_issues($this->customcertid, $this->cm, $this->groupmode);
|
||||
|
||||
$this->pagesize($pagesize, $total);
|
||||
|
||||
$this->rawdata = \mod_customcert\certificate::get_issues($this->customcertid, $this->groupmode, $this->cm,
|
||||
$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 = \mod_customcert\certificate::get_number_of_issues($this->customcertid, $this->cm, $this->groupmode);
|
||||
$this->out($total, false);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
49
report.php
49
report.php
|
@ -31,14 +31,6 @@ $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/report.php', array('id' => $id, 'page' => $page, 'perpage' => $perpage));
|
||||
|
||||
// Ensure the perpage variable does not exceed the max allowed if
|
||||
// the user has not specified they wish to view all customcerts.
|
||||
if (\mod_customcert\certificate::CUSTOMCERT_PER_PAGE !== 0) {
|
||||
if (($perpage > \mod_customcert\certificate::CUSTOMCERT_MAX_PER_PAGE) || ($perpage === 0)) {
|
||||
$perpage = \mod_customcert\certificate::CUSTOMCERT_PER_PAGE;
|
||||
}
|
||||
}
|
||||
|
||||
$cm = get_coursemodule_from_id('customcert', $id, 0, false, MUST_EXIST);
|
||||
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
||||
$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST);
|
||||
|
@ -54,33 +46,15 @@ require_capability('mod/customcert:manage', $context);
|
|||
if ($groupmode = groups_get_activity_groupmode($cm)) {
|
||||
groups_get_activity_group($cm, true);
|
||||
}
|
||||
$users = \mod_customcert\certificate::get_issues($customcert->id, $groupmode, $cm, $page, $perpage);
|
||||
|
||||
if ($download) {
|
||||
\mod_customcert\certificate::generate_report_file($customcert, $users, $download);
|
||||
exit;
|
||||
$table = new \mod_customcert\report_table($customcert->id, $cm, $groupmode);
|
||||
$table->define_baseurl($pageurl);
|
||||
|
||||
if ($table->is_downloading($download, 'customcert-report')) {
|
||||
$table->download();
|
||||
exit();
|
||||
}
|
||||
|
||||
// Create the table for the users.
|
||||
$table = new html_table();
|
||||
$table->attributes['class'] = 'generaltable centre';
|
||||
$table->attributes['style'] = 'width: 95%;';
|
||||
$table->head = array(get_string('awardedto', 'customcert'), get_string('receiveddate', 'customcert'), get_string('code', 'customcert'));
|
||||
$table->align = array('left', 'left', 'center');
|
||||
foreach ($users as $user) {
|
||||
$name = $OUTPUT->user_picture($user) . fullname($user);
|
||||
$date = userdate($user->timecreated);
|
||||
$code = $user->code;
|
||||
$table->data[] = array($name, $date, $code);
|
||||
}
|
||||
|
||||
// Create table to store buttons.
|
||||
$tablebutton = new html_table();
|
||||
$tablebutton->attributes['class'] = 'centre';
|
||||
$btndownloadods = $OUTPUT->single_button(new moodle_url('report.php', array('id' => $cm->id, 'download' => 'ods')), get_string("downloadods"));
|
||||
$btndownloadxls = $OUTPUT->single_button(new moodle_url('report.php', array('id' => $cm->id, 'download' => 'xls')), get_string("downloadexcel"));
|
||||
$tablebutton->data[] = array($btndownloadods, $btndownloadxls);
|
||||
|
||||
// Set up the page.
|
||||
\mod_customcert\page_helper::page_setup($pageurl, $context, get_string('customcertreport', 'customcert'));
|
||||
|
||||
|
@ -89,12 +63,9 @@ $PAGE->navbar->add(get_string('customcertreport', 'customcert'));
|
|||
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('modulenameplural', 'customcert'));
|
||||
|
||||
groups_print_activity_menu($cm, $url);
|
||||
// If perpage is not set to 0 (displaying all issues), we may need a paging bar.
|
||||
if ($perpage !== 0) {
|
||||
echo $OUTPUT->paging_bar(count((array) $users), $page, $perpage, $url);
|
||||
}
|
||||
echo '<br />';
|
||||
echo html_writer::table($table);
|
||||
echo html_writer::tag('div', html_writer::table($tablebutton), array('style' => 'margin:auto; width:50%'));
|
||||
|
||||
$table->out($perpage, false);
|
||||
|
||||
echo $OUTPUT->footer($course);
|
||||
|
|
Loading…
Reference in a new issue