Use course module context when calling format_string/text (#200)

This commit is contained in:
Mark Nelson 2018-12-27 12:15:57 +08:00
parent f8a55869b5
commit 61d0bd433f
7 changed files with 175 additions and 100 deletions

View file

@ -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]);
}
}

View file

@ -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]);
}
}

View file

@ -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())]);
}
}

View file

@ -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]);
}
}

View file

@ -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]);
}
}