Use course module context when calling format_string/text (#200)
This commit is contained in:
parent
61e18a647b
commit
ff0652e3ac
7 changed files with 175 additions and 100 deletions
|
@ -383,6 +383,27 @@ class element_helper {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that returns the context for this element.
|
||||
*
|
||||
* @param int $elementid The element id
|
||||
* @return \context The context
|
||||
*/
|
||||
public static function get_context(int $elementid) : \context {
|
||||
global $DB;
|
||||
|
||||
$sql = "SELECT ct.contextid
|
||||
FROM {customcert_templates} ct
|
||||
INNER JOIN {customcert_pages} cp
|
||||
ON ct.id = cp.templateid
|
||||
INNER JOIN {customcert_elements} ce
|
||||
ON cp.id = ce.pageid
|
||||
WHERE ce.id = :elementid";
|
||||
$contextid = $DB->get_field_sql($sql, array('elementid' => $elementid), MUST_EXIST);
|
||||
|
||||
return \context::instance_by_id($contextid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of possible elements to add.
|
||||
*
|
||||
|
|
|
@ -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($this->get_id()));
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_category_name());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,30 +55,28 @@ class element extends \mod_customcert\element {
|
|||
* @return string the html
|
||||
*/
|
||||
public function render_html() {
|
||||
global $COURSE;
|
||||
|
||||
$categoryname = format_string($COURSE->fullname, true, ['context' => \context_course::instance($COURSE->id)]);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $categoryname);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $this->get_category_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that returns the category name.
|
||||
*
|
||||
* @param int $elementid
|
||||
* @return string
|
||||
*/
|
||||
protected static function get_category_name($elementid) {
|
||||
protected function get_category_name() : string {
|
||||
global $DB, $SITE;
|
||||
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($elementid);
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$course = get_course($courseid);
|
||||
$context = \mod_customcert\element_helper::get_context($this->get_id());
|
||||
|
||||
// Check that there is a course category available.
|
||||
if (!empty($course->category)) {
|
||||
$categoryname = $DB->get_field('course_categories', 'name', array('id' => $course->category), MUST_EXIST);
|
||||
return format_string($categoryname, true, ['context' => \context_course::instance($courseid)]);
|
||||
} else { // Must be in a site template.
|
||||
return format_string($SITE->fullname, true, ['context' => \context_system::instance()]);
|
||||
$categoryname = $SITE->fullname;
|
||||
}
|
||||
|
||||
return format_string($categoryname, true, ['context' => $context]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,11 +43,7 @@ class element extends \mod_customcert\element {
|
|||
* @param \stdClass $user the user we are rendering this for
|
||||
*/
|
||||
public function render($pdf, $preview, $user) {
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$course = get_course($courseid);
|
||||
|
||||
$coursename = format_string($course->fullname, true, ['context' => \context_course::instance($courseid)]);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $coursename);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_course_name());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,9 +55,19 @@ class element extends \mod_customcert\element {
|
|||
* @return string the html
|
||||
*/
|
||||
public function render_html() {
|
||||
global $COURSE;
|
||||
return \mod_customcert\element_helper::render_html_content($this, $this->get_course_name());
|
||||
}
|
||||
|
||||
$coursename = format_string($COURSE->fullname, true, ['context' => \context_course::instance($COURSE->id)]);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $coursename);
|
||||
/**
|
||||
* Helper function that returns the category name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_course_name() : string {
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$course = get_course($courseid);
|
||||
$context = \mod_customcert\element_helper::get_context($this->get_id());
|
||||
|
||||
return format_string($course->fullname, true, ['context' => $context]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,20 +73,9 @@ class element extends \mod_customcert\element {
|
|||
* @param \stdClass $user the user we are rendering this for
|
||||
*/
|
||||
public function render($pdf, $preview, $user) {
|
||||
global $DB;
|
||||
|
||||
// Check that the grade item is not empty.
|
||||
if (!empty($this->get_data())) {
|
||||
// Get the course module information.
|
||||
$cm = $DB->get_record('course_modules', array('id' => $this->get_data()), '*', MUST_EXIST);
|
||||
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
|
||||
|
||||
// Get the name of the item.
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_data());
|
||||
$itemname = $DB->get_field($module->name, 'name', array('id' => $cm->instance), MUST_EXIST);
|
||||
$itemname = format_string($itemname, true, ['context' => \context_course::instance($courseid)]);
|
||||
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $itemname);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_grade_item_name());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,20 +88,9 @@ class element extends \mod_customcert\element {
|
|||
* @return string the html
|
||||
*/
|
||||
public function render_html() {
|
||||
global $DB;
|
||||
|
||||
// Check that the grade item is not empty.
|
||||
if (!empty($this->get_data())) {
|
||||
// Get the course module information.
|
||||
$cm = $DB->get_record('course_modules', array('id' => $this->get_data()), '*', MUST_EXIST);
|
||||
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
|
||||
|
||||
// Get the name of the item.
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_data());
|
||||
$itemname = $DB->get_field($module->name, 'name', array('id' => $cm->instance), MUST_EXIST);
|
||||
$itemname = format_string($itemname, true, ['context' => \context_course::instance($courseid)]);
|
||||
|
||||
return \mod_customcert\element_helper::render_html_content($this, $itemname);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $this->get_grade_item_name());
|
||||
}
|
||||
|
||||
return '';
|
||||
|
@ -130,4 +108,22 @@ class element extends \mod_customcert\element {
|
|||
}
|
||||
parent::definition_after_data($mform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that returns the category name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_grade_item_name() : string {
|
||||
global $DB;
|
||||
|
||||
// Get the course module information.
|
||||
$cm = $DB->get_record('course_modules', array('id' => $this->get_data()), '*', MUST_EXIST);
|
||||
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
|
||||
|
||||
// Get the name of the item.
|
||||
$itemname = $DB->get_field($module->name, 'name', array('id' => $cm->instance), MUST_EXIST);
|
||||
|
||||
return format_string($itemname, true, ['context' => \mod_customcert\element_helper::get_context($this->get_id())]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,9 +67,7 @@ class element extends \mod_customcert\element {
|
|||
* @param \stdClass $user the user we are rendering this for
|
||||
*/
|
||||
public function render($pdf, $preview, $user) {
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$text = format_text($this->get_data(), FORMAT_HTML, ['context' => \context_course::instance($courseid)]);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $text);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_text());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,9 +79,7 @@ class element extends \mod_customcert\element {
|
|||
* @return string the html
|
||||
*/
|
||||
public function render_html() {
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$text = format_text($this->get_data(), FORMAT_HTML, ['context' => \context_course::instance($courseid)]);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $text);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $this->get_text());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,4 +94,14 @@ class element extends \mod_customcert\element {
|
|||
}
|
||||
parent::definition_after_data($mform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that returns the text.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_text() : string {
|
||||
$context = \mod_customcert\element_helper::get_context($this->get_id());
|
||||
return format_text($this->get_data(), FORMAT_HTML, ['context' => $context]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,30 +98,7 @@ class element extends \mod_customcert\element {
|
|||
* @param \stdClass $user the user we are rendering this for
|
||||
*/
|
||||
public function render($pdf, $preview, $user) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// The user field to display.
|
||||
$field = $this->get_data();
|
||||
// The value to display on the PDF.
|
||||
$value = '';
|
||||
if (is_number($field)) { // Must be a custom user profile field.
|
||||
if ($field = $DB->get_record('user_info_field', array('id' => $field))) {
|
||||
$file = $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php';
|
||||
if (file_exists($file)) {
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
require_once($file);
|
||||
$class = "profile_field_{$field->datatype}";
|
||||
$field = new $class($field->id, $user->id);
|
||||
$value = $field->display_data();
|
||||
}
|
||||
}
|
||||
} else if (!empty($user->$field)) { // Field in the user table.
|
||||
$value = $user->$field;
|
||||
}
|
||||
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$value = format_string($value, true, ['context' => \context_course::instance($courseid)]);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $value);
|
||||
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_user_field_value($user, $preview));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,35 +108,9 @@ class element extends \mod_customcert\element {
|
|||
* drag and drop interface to position it.
|
||||
*/
|
||||
public function render_html() {
|
||||
global $CFG, $DB, $USER;
|
||||
global $USER;
|
||||
|
||||
// The user field to display.
|
||||
$field = $this->get_data();
|
||||
// The value to display - we always want to show a value here so it can be repositioned.
|
||||
$value = $field;
|
||||
if (is_number($field)) { // Must be a custom user profile field.
|
||||
if ($field = $DB->get_record('user_info_field', array('id' => $field))) {
|
||||
// Found the field name, let's update the value to display.
|
||||
$value = $field->name;
|
||||
$file = $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php';
|
||||
if (file_exists($file)) {
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
require_once($file);
|
||||
$class = "profile_field_{$field->datatype}";
|
||||
$field = new $class($field->id, $USER->id);
|
||||
if ($fieldvalue = $field->display_data()) {
|
||||
// Ok, found a value for the user, let's show that instead.
|
||||
$value = $fieldvalue;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (!empty($USER->$field)) { // Field in the user table.
|
||||
$value = $USER->$field;
|
||||
}
|
||||
|
||||
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
|
||||
$value = format_string($value, true, ['context' => \context_course::instance($courseid)]);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $value);
|
||||
return \mod_customcert\element_helper::render_html_content($this, $this->get_user_field_value($USER, true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -174,4 +125,43 @@ class element extends \mod_customcert\element {
|
|||
}
|
||||
parent::definition_after_data($mform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that returns the text.
|
||||
*
|
||||
* @param \stdClass $user the user we are rendering this for
|
||||
* @param bool $preview Is this a preview?
|
||||
* @return string
|
||||
*/
|
||||
protected function get_user_field_value(\stdClass $user, bool $preview) : string {
|
||||
global $CFG, $DB;
|
||||
|
||||
// The user field to display.
|
||||
$field = $this->get_data();
|
||||
// The value to display - we always want to show a value here so it can be repositioned.
|
||||
if ($preview) {
|
||||
$value = $field;
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
if (is_number($field)) { // Must be a custom user profile field.
|
||||
if ($field = $DB->get_record('user_info_field', array('id' => $field))) {
|
||||
// Found the field name, let's update the value to display.
|
||||
$value = $field->name;
|
||||
$file = $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php';
|
||||
if (file_exists($file)) {
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
require_once($file);
|
||||
$class = "profile_field_{$field->datatype}";
|
||||
$field = new $class($field->id, $user->id);
|
||||
$value = $field->display_data();
|
||||
}
|
||||
}
|
||||
} else if (!empty($user->$field)) { // Field in the user table.
|
||||
$value = $user->$field;
|
||||
}
|
||||
|
||||
$context = \mod_customcert\element_helper::get_context($this->get_id());
|
||||
return format_string($value, true, ['context' => $context]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,6 +101,64 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
|
|||
$this->assertEquals($SITE->id, \mod_customcert\element_helper::get_courseid($element->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests we are returning the correct course module id for an element in a course customcert activity.
|
||||
*/
|
||||
public function test_get_context_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));
|
||||
|
||||
// Get the template to add elements 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 module id is returned.
|
||||
$this->assertEquals(context_module::instance($customcert->cmid),
|
||||
\mod_customcert\element_helper::get_context($element->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests we are returning the correct course module id for an element in a site template.
|
||||
*/
|
||||
public function test_get_context_element_in_site_template() {
|
||||
global $DB;
|
||||
|
||||
// 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 module id is returned.
|
||||
$this->assertEquals(context_system::instance(), \mod_customcert\element_helper::get_context($element->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test we return the correct grade items in a course.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue