From b067d398dd9ad57a80694881d102cf46d6b3bf2f Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 30 May 2017 17:38:32 +0800 Subject: [PATCH] #82 Do not assume $COURSE represents the course for the certificate --- classes/element_helper.php | 25 ++++++ element/categoryname/classes/element.php | 20 +++-- element/coursename/classes/element.php | 5 +- element/date/classes/element.php | 8 +- element/grade/classes/element.php | 15 ++-- tests/element_helper_test.php | 104 +++++++++++++++++++++++ 6 files changed, 157 insertions(+), 20 deletions(-) create mode 100644 tests/element_helper_test.php diff --git a/classes/element_helper.php b/classes/element_helper.php index 8bb642e..39cfe6d 100644 --- a/classes/element_helper.php +++ b/classes/element_helper.php @@ -352,4 +352,29 @@ class element_helper { 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; + } + } } diff --git a/element/categoryname/classes/element.php b/element/categoryname/classes/element.php index 4b0aeb6..4985cf8 100644 --- a/element/categoryname/classes/element.php +++ b/element/categoryname/classes/element.php @@ -43,7 +43,7 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ 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 */ 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. * + * @param int $elementid * @return string */ - protected static function get_category_name() { - global $DB, $COURSE; + protected static function get_category_name($elementid) { + global $DB, $SITE; + + $courseid = \mod_customcert\element_helper::get_courseid($elementid); + $course = get_course($courseid); // Check that there is a course category available. - if (!empty($COURSE->category)) { - return $DB->get_field('course_categories', 'name', array('id' => $COURSE->category), MUST_EXIST); + if (!empty($course->category)) { + return $DB->get_field('course_categories', 'name', array('id' => $course->category), MUST_EXIST); } else { // Must be in a site template. - return $COURSE->fullname; + return $SITE->fullname; } } } diff --git a/element/coursename/classes/element.php b/element/coursename/classes/element.php index b07acaf..61af7f6 100644 --- a/element/coursename/classes/element.php +++ b/element/coursename/classes/element.php @@ -43,9 +43,10 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ 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); } /** diff --git a/element/date/classes/element.php b/element/date/classes/element.php index f24fbae..4ee8acd 100644 --- a/element/date/classes/element.php +++ b/element/date/classes/element.php @@ -94,13 +94,15 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ public function render($pdf, $preview, $user) { - global $COURSE, $DB; + global $DB; // If there is no element data, we have nothing to display. if (empty($this->element->data)) { return; } + $courseid = \mod_customcert\element_helper::get_courseid($this->id); + // Decode the information stored in the database. $dateinfo = json_decode($this->element->data); $dateitem = $dateinfo->dateitem; @@ -126,7 +128,7 @@ class element extends \mod_customcert\element { FROM {course_completions} c WHERE c.userid = :userid 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)) { $date = $timecompleted->timecompleted; } @@ -135,7 +137,7 @@ class element extends \mod_customcert\element { $gradeitem = new \stdClass(); $gradeitem->gradeitem = $dateitem; $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)) { $date = $modinfo->dategraded; } diff --git a/element/grade/classes/element.php b/element/grade/classes/element.php index 6553099..812407c 100644 --- a/element/grade/classes/element.php +++ b/element/grade/classes/element.php @@ -95,23 +95,23 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ public function render($pdf, $preview, $user) { - global $COURSE; - // If there is no element data, we have nothing to display. if (empty($this->element->data)) { return; } + $courseid = \mod_customcert\element_helper::get_courseid($this->id); + // Decode the information stored in the database. $gradeinfo = json_decode($this->element->data); // If we are previewing this certificate then just show a demonstration grade. 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); } else { // 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); @@ -260,18 +260,17 @@ class element extends \mod_customcert\element { * * @param \stdClass $gradeinfo * @param int $userid + * @param int $courseid * @return string the grade result */ - public static function get_grade($gradeinfo, $userid) { - global $COURSE; - + public static function get_grade($gradeinfo, $userid, $courseid) { // Get the grade information. $gradeitem = $gradeinfo->gradeitem; $gradeformat = $gradeinfo->gradeformat; // Check if we are displaying the course grade. 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. $courseitem->gradetype = GRADE_TYPE_VALUE; $grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid)); diff --git a/tests/element_helper_test.php b/tests/element_helper_test.php new file mode 100644 index 0000000..979311c --- /dev/null +++ b/tests/element_helper_test.php @@ -0,0 +1,104 @@ +. + +/** + * File contains the unit tests for the element helper class. + * + * @package mod_customcert + * @category test + * @copyright 2017 Mark Nelson + * @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 + * @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)); + } +}