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

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