#82 Do not assume $COURSE represents the course for the certificate

This commit is contained in:
Mark Nelson 2017-05-30 17:38:32 +08:00
parent 1ac7c1ffb6
commit d0a0fc5abc
6 changed files with 157 additions and 20 deletions

View file

@ -352,4 +352,29 @@ class element_helper {
return $sequence; return $sequence;
} }
/**
* Helper function that returns the course id for this element.
*
* @param int $elementid The element id
* @return int The course id
*/
public static function get_courseid($elementid) {
global $DB, $SITE;
$sql = "SELECT course
FROM {customcert} c
INNER JOIN {customcert_pages} cp
ON c.templateid = cp.templateid
INNER JOIN {customcert_elements} ce
ON cp.id = ce.pageid
WHERE ce.id = :elementid";
// Check if there is a course associated with this element.
if ($course = $DB->get_record_sql($sql, array('elementid' => $elementid))) {
return $course->course;
} else { // Must be in a site template.
return $SITE->id;
}
}
} }

View file

@ -43,7 +43,7 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for * @param \stdClass $user the user we are rendering this for
*/ */
public function render($pdf, $preview, $user) { public function render($pdf, $preview, $user) {
\mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name()); \mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name($this->id));
} }
/** /**
@ -55,22 +55,28 @@ class element extends \mod_customcert\element {
* @return string the html * @return string the html
*/ */
public function render_html() { public function render_html() {
return \mod_customcert\element_helper::render_html_content($this, self::get_category_name()); global $COURSE;
return \mod_customcert\element_helper::render_html_content($this, $COURSE->fullname);
} }
/** /**
* Helper function that returns the category name. * Helper function that returns the category name.
* *
* @param int $elementid
* @return string * @return string
*/ */
protected static function get_category_name() { protected static function get_category_name($elementid) {
global $DB, $COURSE; global $DB, $SITE;
$courseid = \mod_customcert\element_helper::get_courseid($elementid);
$course = get_course($courseid);
// Check that there is a course category available. // Check that there is a course category available.
if (!empty($COURSE->category)) { if (!empty($course->category)) {
return $DB->get_field('course_categories', 'name', array('id' => $COURSE->category), MUST_EXIST); return $DB->get_field('course_categories', 'name', array('id' => $course->category), MUST_EXIST);
} else { // Must be in a site template. } else { // Must be in a site template.
return $COURSE->fullname; return $SITE->fullname;
} }
} }
} }

View file

@ -43,9 +43,10 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for * @param \stdClass $user the user we are rendering this for
*/ */
public function render($pdf, $preview, $user) { public function render($pdf, $preview, $user) {
global $COURSE; $courseid = \mod_customcert\element_helper::get_courseid($this->id);
$course = get_course($courseid);
\mod_customcert\element_helper::render_content($pdf, $this, $COURSE->fullname); \mod_customcert\element_helper::render_content($pdf, $this, $course->fullname);
} }
/** /**

View file

@ -94,13 +94,15 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for * @param \stdClass $user the user we are rendering this for
*/ */
public function render($pdf, $preview, $user) { public function render($pdf, $preview, $user) {
global $COURSE, $DB; global $DB;
// If there is no element data, we have nothing to display. // If there is no element data, we have nothing to display.
if (empty($this->element->data)) { if (empty($this->element->data)) {
return; return;
} }
$courseid = \mod_customcert\element_helper::get_courseid($this->id);
// Decode the information stored in the database. // Decode the information stored in the database.
$dateinfo = json_decode($this->element->data); $dateinfo = json_decode($this->element->data);
$dateitem = $dateinfo->dateitem; $dateitem = $dateinfo->dateitem;
@ -126,7 +128,7 @@ class element extends \mod_customcert\element {
FROM {course_completions} c FROM {course_completions} c
WHERE c.userid = :userid WHERE c.userid = :userid
AND c.course = :courseid"; AND c.course = :courseid";
if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $COURSE->id))) { if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $courseid))) {
if (!empty($timecompleted->timecompleted)) { if (!empty($timecompleted->timecompleted)) {
$date = $timecompleted->timecompleted; $date = $timecompleted->timecompleted;
} }
@ -135,7 +137,7 @@ class element extends \mod_customcert\element {
$gradeitem = new \stdClass(); $gradeitem = new \stdClass();
$gradeitem->gradeitem = $dateitem; $gradeitem->gradeitem = $dateitem;
$gradeitem->gradeformat = GRADE_DISPLAY_TYPE_PERCENTAGE; $gradeitem->gradeformat = GRADE_DISPLAY_TYPE_PERCENTAGE;
if ($modinfo = \customcertelement_grade\element::get_grade($gradeitem, $issue->userid)) { if ($modinfo = \customcertelement_grade\element::get_grade($gradeitem, $issue->userid, $courseid)) {
if (!empty($modinfo->dategraded)) { if (!empty($modinfo->dategraded)) {
$date = $modinfo->dategraded; $date = $modinfo->dategraded;
} }

View file

@ -95,23 +95,23 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for * @param \stdClass $user the user we are rendering this for
*/ */
public function render($pdf, $preview, $user) { public function render($pdf, $preview, $user) {
global $COURSE;
// If there is no element data, we have nothing to display. // If there is no element data, we have nothing to display.
if (empty($this->element->data)) { if (empty($this->element->data)) {
return; return;
} }
$courseid = \mod_customcert\element_helper::get_courseid($this->id);
// Decode the information stored in the database. // Decode the information stored in the database.
$gradeinfo = json_decode($this->element->data); $gradeinfo = json_decode($this->element->data);
// If we are previewing this certificate then just show a demonstration grade. // If we are previewing this certificate then just show a demonstration grade.
if ($preview) { if ($preview) {
$courseitem = \grade_item::fetch_course_item($COURSE->id); $courseitem = \grade_item::fetch_course_item($courseid);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, 2); $grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, 2);
} else { } else {
// Get the grade for the grade item. // Get the grade for the grade item.
$grade = self::get_grade($gradeinfo, $user->id); $grade = self::get_grade($gradeinfo, $user->id, $courseid);
} }
\mod_customcert\element_helper::render_content($pdf, $this, $grade); \mod_customcert\element_helper::render_content($pdf, $this, $grade);
@ -260,18 +260,17 @@ class element extends \mod_customcert\element {
* *
* @param \stdClass $gradeinfo * @param \stdClass $gradeinfo
* @param int $userid * @param int $userid
* @param int $courseid
* @return string the grade result * @return string the grade result
*/ */
public static function get_grade($gradeinfo, $userid) { public static function get_grade($gradeinfo, $userid, $courseid) {
global $COURSE;
// Get the grade information. // Get the grade information.
$gradeitem = $gradeinfo->gradeitem; $gradeitem = $gradeinfo->gradeitem;
$gradeformat = $gradeinfo->gradeformat; $gradeformat = $gradeinfo->gradeformat;
// Check if we are displaying the course grade. // Check if we are displaying the course grade.
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) { if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
if ($courseitem = \grade_item::fetch_course_item($COURSE->id)) { if ($courseitem = \grade_item::fetch_course_item($courseid)) {
// Set the grade type we want. // Set the grade type we want.
$courseitem->gradetype = GRADE_TYPE_VALUE; $courseitem->gradetype = GRADE_TYPE_VALUE;
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid)); $grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));

View file

@ -0,0 +1,104 @@
<?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/>.
/**
* File contains the unit tests for the element helper class.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Unit tests for the element helper class.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_customcert_element_helper_testcase extends advanced_testcase {
/**
* Test set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Tests we are returning the correct course id for an element in a course customcert activity.
*/
public function test_get_courseid_element_in_course_certificate() {
global $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a custom certificate in the course.
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
'emailstudents' => 1));
// Get the template to add elemenets to.
$template = $DB->get_record('customcert_templates', array('contextid' => context_module::instance($customcert->cmid)->id));
$template = new \mod_customcert\template($template);
// Add a page to the template.
$pageid = $template->add_page();
// Add an element to this page.
$element = new \stdClass();
$element->name = 'Test element';
$element->element = 'testelement';
$element->pageid = $pageid;
$element->sequence = \mod_customcert\element_helper::get_element_sequence($element->pageid);
$element->timecreated = time();
$element->id = $DB->insert_record('customcert_elements', $element);
// Confirm the correct course id is returned.
$this->assertEquals($course->id, \mod_customcert\element_helper::get_courseid($element->id));
}
/**
* Tests we are returning the correct course id for an element in a site template.
*/
public function test_get_courseid_element_in_site_template() {
global $DB, $SITE;
// Add a template to the site.
$template = \mod_customcert\template::create('Site template', context_system::instance()->id);
// Add a page to the template.
$pageid = $template->add_page();
// Add an element to this page.
$element = new \stdClass();
$element->name = 'Test element';
$element->element = 'testelement';
$element->pageid = $pageid;
$element->sequence = \mod_customcert\element_helper::get_element_sequence($element->pageid);
$element->timecreated = time();
$element->id = $DB->insert_record('customcert_elements', $element);
// Confirm the correct course id is returned.
$this->assertEquals($SITE->id, \mod_customcert\element_helper::get_courseid($element->id));
}
}