From 8350c4afbf5e193309333fe12ad10ec93a452b85 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 22 May 2013 19:37:39 +0800 Subject: [PATCH] Introduced basic reporting functionality --- lang/en/customcert.php | 4 ++ lib.php | 109 ++++++++++++++++++++++++++++++++++++++++- report.php | 98 ++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 report.php diff --git a/lang/en/customcert.php b/lang/en/customcert.php index f3feba7..d62d83b 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -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'; diff --git a/lib.php b/lib.php index c0e4e6f..17e0e35 100644 --- a/lib.php +++ b/lib.php @@ -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'); } -} \ No newline at end of file +} + +/** + * 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(); +} diff --git a/report.php b/report.php new file mode 100644 index 0000000..d12a9c7 --- /dev/null +++ b/report.php @@ -0,0 +1,98 @@ +. + +/** + * Handles viewing a customcert. + * + * @package mod_customcert + * @copyright Mark Nelson + * @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 '
'; +echo html_writer::table($table); +echo html_writer::tag('div', html_writer::table($tablebutton), array('style' => 'margin:auto; width:50%')); +echo $OUTPUT->footer($course);