From 31168708bbb07ad8ea2b9b63d3848c5f209ab778 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 9 Apr 2013 17:28:32 +0800 Subject: [PATCH] Added a view.php file --- lang/en/customcert.php | 4 ++ lib.php | 105 +++++++++++++++++++++++++++++++++++ view.php | 123 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 view.php diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 56ddeda..de13c3b 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -43,12 +43,14 @@ $string['fontcolour'] = 'Colour'; $string['fontcolour_help'] = 'The colour of the font.'; $string['fontsize'] = 'Size'; $string['fontsize_help'] = 'The size of the font in points.'; +$string['getcustomcert'] = 'Get your custom certificate'; $string['height'] = 'Height'; $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['intro'] = 'Introduction'; $string['invalidcolour'] = 'Invalid colour chosen.'; $string['invalidposition'] = 'Please select a positive number for position {$a}.'; +$string['issued'] = 'Issued'; $string['landscape'] = 'Landscape'; $string['modulename'] = 'Custom Certificate'; $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['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['summaryofissue'] = 'Summary of issue'; $string['uploadimage'] = 'Upload image'; +$string['viewcustomcertissues'] = 'View {$a} issued custom certificates'; $string['width'] = 'Width'; $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.'; diff --git a/lib.php b/lib.php index 2c99887..80f40f2 100644 --- a/lib.php +++ b/lib.php @@ -628,3 +628,108 @@ function customcert_get_course_time($courseid) { 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; +} diff --git a/view.php b/view.php new file mode 100644 index 0000000..d750dcc --- /dev/null +++ b/view.php @@ -0,0 +1,123 @@ +. + +/** + * Handles viewing a customcert. + * + * @package mod + * @subpackage customcert + * @copyright Mark Nelson + * @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) . "
"; + } + + // 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(); +}