Introduced basic reporting functionality

This commit is contained in:
Mark Nelson 2013-05-22 19:37:39 +08:00
parent 9a506b668b
commit 8350c4afbf
3 changed files with 209 additions and 2 deletions

View file

@ -25,6 +25,8 @@
$string['addcertpage'] = 'Add another certificate page';
$string['addelement'] = 'Add element';
$string['awardedto'] = 'Awarded to';
$string['code'] = 'Code';
$string['copy'] = 'Copy';
$string['coursetimereq'] = 'Required minutes in course';
$string['coursetimereq_help'] = 'Enter here the minimum amount of time, in minutes, that a student must be logged into the course before they will be able to receive the certificate.';
@ -74,6 +76,8 @@ $string['posx'] = 'Position X';
$string['posx_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the x direction.';
$string['posy'] = 'Postion Y';
$string['posy_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the y direction.';
$string['receiveddate'] = 'Received date';
$string['report'] = 'Report';
$string['save'] = 'Save';
$string['savechangespreview'] = 'Save changes and preview';
$string['savetemplate'] = 'Save template';

109
lib.php
View file

@ -40,6 +40,17 @@ define('PROTECTION_MODIFY', 'modify');
*/
define('PROTECTION_COPY', 'copy');
/**
* @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.
*/
define('CUSTOMCERT_PER_PAGE', 20);
/**
* @var int the max number of issues to display
*/
define('CUSTOMCERT_MAX_PER_PAGE', 300);
/**
* Add customcert instance.
*
@ -55,7 +66,7 @@ function customcert_add_instance($data, $mform) {
$data->timemodified = $data->timecreated;
$data->id = $DB->insert_record('customcert', $data);
// Add a page to this certificate.
// Add a page to this customcert.
customcert_add_page($data);
return $data->id;
@ -686,6 +697,41 @@ function customcert_get_course_time($courseid) {
return 0;
}
/**
* Returns a list of issued customcerts.
*
* @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
* @return stdClass the users
*/
function customcert_get_issues($customcertid, $groupmode, $cm, $page, $perpage) {
global $CFG, $DB;
list($conditionssql, $conditionsparams) = customcert_get_conditional_issues_sql($cm, $groupmode);
// Get all the users that have customcerts issued, should only be one issue per user for a customcert
$allparams = $conditionsparams + array('customcertid' => $customcertid);
$users = $DB->get_records_sql("SELECT u.*, 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(),
$allparams,
$page * $perpage,
$perpage);
return $users;
}
/**
* Returns the total number of issues for a given customcert.
*
@ -835,4 +881,63 @@ function customcert_generate_pdf($customcert, $userid) {
}
$pdf->Output($filename, 'D');
}
}
}
/**
* Generate the report.
*
* @param stdClass $customcert
* @param stdClass $users the list of users who have had a customcert issued
* @param string $type
*/
function customcert_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
$i = 0;
$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();
}

98
report.php Normal file
View file

@ -0,0 +1,98 @@
<?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 a customcert.
*
* @package mod_customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once('lib.php');
$id = required_param('id', PARAM_INT);
$download = optional_param('download', '', PARAM_ALPHA);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 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 (CUSTOMCERT_PER_PAGE !== 0) {
if (($perpage > CUSTOMCERT_MAX_PER_PAGE) || ($perpage === 0)) {
$perpage = 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);
// Requires a course login.
require_course_login($course->id, false, $cm);
// Check capabilities.
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/customcert:manage', $context);
// Get the users who have been issued.
$users = customcert_get_issues($customcert->id, groups_get_activity_groupmode($cm), $cm, $page, $perpage);
if ($download) {
customcert_generate_report_file($customcert, $users, $download);
exit;
}
// Create the table for the users.
$table = new html_table();
$table->width = '95%';
$table->tablealign = 'center';
$table->head = array(get_string('awardedto', 'customcert'), get_string('receiveddate', 'customcert'), get_string('code', 'customcert'));
$table->align = array('left', 'left', 'center', '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'] = 'downloadreport';
$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);
$PAGE->set_url($pageurl);
$PAGE->navbar->add(get_string('report', 'customcert'));
$PAGE->set_title(format_string($customcert->name) . ": " . get_string('report', 'customcert'));
$PAGE->set_heading($course->fullname);
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($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%'));
echo $OUTPUT->footer($course);