Added a view.php file

This commit is contained in:
Mark Nelson 2013-04-09 17:28:32 +08:00
parent 94978c9f03
commit 31168708bb
3 changed files with 232 additions and 0 deletions

View file

@ -43,12 +43,14 @@ $string['fontcolour'] = 'Colour';
$string['fontcolour_help'] = 'The colour of the font.'; $string['fontcolour_help'] = 'The colour of the font.';
$string['fontsize'] = 'Size'; $string['fontsize'] = 'Size';
$string['fontsize_help'] = 'The size of the font in points.'; $string['fontsize_help'] = 'The size of the font in points.';
$string['getcustomcert'] = 'Get your custom certificate';
$string['height'] = 'Height'; $string['height'] = 'Height';
$string['heightnotvalid'] = 'The height has to be a valid number.'; $string['heightnotvalid'] = 'The height has to be a valid number.';
$string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.'; $string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.';
$string['intro'] = 'Introduction'; $string['intro'] = 'Introduction';
$string['invalidcolour'] = 'Invalid colour chosen.'; $string['invalidcolour'] = 'Invalid colour chosen.';
$string['invalidposition'] = 'Please select a positive number for position {$a}.'; $string['invalidposition'] = 'Please select a positive number for position {$a}.';
$string['issued'] = 'Issued';
$string['landscape'] = 'Landscape'; $string['landscape'] = 'Landscape';
$string['modulename'] = 'Custom Certificate'; $string['modulename'] = 'Custom Certificate';
$string['modulenameplural'] = 'Custom Certificates'; $string['modulenameplural'] = 'Custom Certificates';
@ -65,7 +67,9 @@ $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['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'] = '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['posy_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the y direction.';
$string['summaryofissue'] = 'Summary of issue';
$string['uploadimage'] = 'Upload image'; $string['uploadimage'] = 'Upload image';
$string['viewcustomcertissues'] = 'View {$a} issued custom certificates';
$string['width'] = 'Width'; $string['width'] = 'Width';
$string['widthnotvalid'] = 'The width has to be a valid number.'; $string['widthnotvalid'] = 'The width has to be a valid number.';
$string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.'; $string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.';

105
lib.php
View file

@ -628,3 +628,108 @@ function customcert_get_course_time($courseid) {
return 0; return 0;
} }
/**
* Returns the total number of issues for a given customcert.
*
* @param int $customcertid
* @param stdClass $cm the course module
* @param bool $groupmode the group mode
* @return int the number of issues
*/
function customcert_get_number_of_issues($customcertid, $cm, $groupmode) {
global $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);
return $DB->count_records_sql("SELECT COUNT(u.*) as count
FROM {user} u
INNER JOIN {customcert_issues} ci
ON u.id = ci.userid
WHERE u.deleted = 0
AND ci.customcertid = :customcertid
$conditionssql",
$allparams);
}
/**
* Returns an array of the conditional variables to use in the get_issues SQL query.
*
* @param stdClass $cm the course module
* @param bool $groupmode are we in group mode ?
* @return array the conditional variables
*/
function customcert_get_conditional_issues_sql($cm, $groupmode) {
// Get all users that can manage this customcert to exclude them from the report.
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$conditionssql = '';
$conditionsparams = array();
if ($certmanagers = array_keys(get_users_by_capability($context, 'mod/customcert:manage', 'u.id'))) {
list($sql, $params) = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert');
$conditionssql .= "AND NOT u.id $sql \n";
$conditionsparams += $params;
}
$restricttogroup = false;
if ($groupmode) {
$currentgroup = groups_get_activity_group($cm);
if ($currentgroup) {
$restricttogroup = true;
$groupusers = array_keys(groups_get_members($currentgroup, 'u.*'));
if (empty($groupusers)) {
return array();
}
}
}
$restricttogrouping = false;
// If groupmembersonly used, remove users who are not in any group.
if (!empty($CFG->enablegroupings) and $cm->groupmembersonly) {
if ($groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id')) {
$restricttogrouping = true;
} else {
return array();
}
}
if ($restricttogroup || $restricttogrouping) {
if ($restricttogroup) {
$allowedusers = $groupusers;
} else if ($restricttogroup && $restricttogrouping) {
$allowedusers = array_intersect($groupusers, $groupingusers);
} else {
$allowedusers = $groupingusers;
}
list($sql, $params) = $DB->get_in_or_equal($allowedusers, SQL_PARAMS_NAMED, 'grp');
$conditionssql .= "AND u.id $sql \n";
$conditionsparams += $params;
}
return array($conditionssql, $conditionsparams);
}
/**
* Generates a 10-digit code of random letters and numbers.
*
* @return string
*/
function customcert_generate_code() {
global $DB;
$uniquecodefound = false;
$code = random_string(10);
while (!$uniquecodefound) {
if (!$DB->record_exists('customcert_issues', array('code' => $code))) {
$uniquecodefound = true;
} else {
$code = random_string(10);
}
}
return $code;
}

123
view.php Normal file
View file

@ -0,0 +1,123 @@
<?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
* @subpackage 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("$CFG->dirroot/mod/customcert/lib.php");
$id = required_param('id', PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$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);
// Ensure the user is allowed to view this page.
require_login($course, true, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/customcert:view', $context);
// Initialise $PAGE.
$PAGE->set_url('/mod/customcert/view.php', array('id' => $cm->id));
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title(format_string($customcert->name));
$PAGE->set_heading(format_string($course->fullname));
// Check that no action was passed, if so that means we are not outputting to PDF.
if (empty($action)) {
// Get the current groups mode.
groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/customcert/view.php?id=' . $cm->id);
$currentgroup = groups_get_activity_group($cm);
$groupmode = groups_get_activity_groupmode($cm);
// Generate the link to the report if there are issues to display.
$reportlink = '';
if (has_capability('mod/customcert:manage', $context)) {
// Get the total number of issues.
$numissues = customcert_get_number_of_issues($customcert->id, $cm, $groupmode);
// If the number of issues is greater than 0 display a link to the report.
if ($numissues > 0) {
$url = html_writer::tag('a', get_string('viewcustomcertissues', 'customcert', $numissues),
array('href' => $CFG->wwwroot . '/mod/customcert/report.php?id=' . $cm->id));
$reportlink = html_writer::tag('div', $url, array('class' => 'reportlink'));
}
}
// Generate the intro content if it exists.
$intro = '';
if (!empty($customcert->intro)) {
$intro = $OUTPUT->box(format_module_intro('customcert', $customcert, $cm->id), 'generalbox', 'intro');
}
// If the current user has been issued a customcert generate HTML to display the details.
$issuelist = '';
if ($issues = $DB->get_records('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id))) {
$header = $OUTPUT->heading(get_string('summaryofissue', 'customcert'));
$table = new html_table();
$table->class = 'generaltable';
$table->head = array(get_string('issued', 'customcert'));
$table->align = array('left');
$table->attributes = array('style' => 'width:20%; margin:auto');
foreach ($issues as $issue) {
$row = array();
$row[] = userdate($issue->timecreated);
$table->data[$issue->id] = $row;
}
$issuelist = $header . html_writer::table($table) . "<br />";
}
// Create the button to download the customcert.
$linkname = get_string('getcustomcert', 'customcert');
$link = new moodle_url('/mod/customcert/view.php', array('id' => $cm->id, 'action' => 'download'));
$downloadbutton = new single_button($link, $linkname);
$downloadbutton->add_action(new popup_action('click', $link, 'customcertpopup', array('height' => 600, 'width' => 800)));
$downloadbutton = html_writer::tag('div', $OUTPUT->render($downloadbutton), array('style' => 'text-align:center'));
// Output all the page data.
echo $OUTPUT->header();
echo $reportlink;
echo $intro;
echo $issuelist;
echo $downloadbutton;
echo $OUTPUT->footer($course);
exit;
} else { // Output to pdf
// Create new customcert issue record if one does not already exist.
if (!$DB->record_exists('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id))) {
$customcertissue = new stdClass();
$customcertissue->customcertid = $customcert->id;
$customcertissue->userid = $USER->id;
$customcertissue->code = customcert_generate_code();
$customcertissue->timecreated = time();
// Insert the record into the database.
$DB->insert_record('customcert_issues', $customcertissue);
}
// Now we want to generate the PDF.
customcert_generate_pdf();
}