diff --git a/lib/classes/external/exporter.php b/lib/classes/external/exporter.php
index 9a262815918..38c195a5964 100644
--- a/lib/classes/external/exporter.php
+++ b/lib/classes/external/exporter.php
@@ -157,7 +157,7 @@ abstract class exporter {
$formatparams = $this->get_format_parameters($property);
$format = $record->$propertyformat;
- list($text, $format) = external_format_text($data->$property, $format, $formatparams['context']->id,
+ list($text, $format) = external_format_text($data->$property, $format, $formatparams['context'],
$formatparams['component'], $formatparams['filearea'], $formatparams['itemid'], $formatparams['options']);
$data->$property = $text;
@@ -168,11 +168,11 @@ abstract class exporter {
if (!empty($definition['multiple'])) {
foreach ($data->$property as $key => $value) {
- $data->{$property}[$key] = external_format_string($value, $formatparams['context']->id,
+ $data->{$property}[$key] = external_format_string($value, $formatparams['context'],
$formatparams['striplinks'], $formatparams['options']);
}
} else {
- $data->$property = external_format_string($data->$property, $formatparams['context']->id,
+ $data->$property = external_format_string($data->$property, $formatparams['context'],
$formatparams['striplinks'], $formatparams['options']);
}
}
diff --git a/lib/externallib.php b/lib/externallib.php
index e49aaf27c82..ec42975cab1 100644
--- a/lib/externallib.php
+++ b/lib/externallib.php
@@ -896,21 +896,25 @@ function external_validate_format($format) {
* @param string $str The string to be filtered. Should be plain text, expect
* possibly for multilang tags.
* @param boolean $striplinks To strip any link in the result text. Moodle 1.8 default changed from false to true! MDL-8713
- * @param int $contextid The id of the context for the string (affects filters).
+ * @param context|int $contextorid The id of the context for the string or the context (affects filters).
* @param array $options options array/object or courseid
* @return string text
* @since Moodle 3.0
*/
-function external_format_string($str, $contextid, $striplinks = true, $options = array()) {
+function external_format_string($str, $contextorid, $striplinks = true, $options = array()) {
// Get settings (singleton).
$settings = external_settings::get_instance();
- if (empty($contextid)) {
+ if (empty($contextorid)) {
throw new coding_exception('contextid is required');
}
if (!$settings->get_raw()) {
- $context = context::instance_by_id($contextid);
+ if (is_object($contextorid) && is_a($contextorid, 'context')) {
+ $context = $contextorid;
+ } else {
+ $context = context::instance_by_id($contextorid);
+ }
$options['context'] = $context;
$options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter();
$str = format_string($str, $striplinks, $options);
@@ -944,7 +948,7 @@ function external_format_string($str, $contextid, $striplinks = true, $options =
*
* @param string $text The content that may contain ULRs in need of rewriting.
* @param int $textformat The text format.
- * @param int $contextid This parameter and the next two identify the file area to use.
+ * @param context|int $contextorid This parameter and the next two identify the file area to use.
* @param string $component
* @param string $filearea helps identify the file area.
* @param int $itemid helps identify the file area.
@@ -953,13 +957,21 @@ function external_format_string($str, $contextid, $striplinks = true, $options =
* @since Moodle 2.3
* @since Moodle 3.2 component, filearea and itemid are optional parameters
*/
-function external_format_text($text, $textformat, $contextid, $component = null, $filearea = null, $itemid = null,
+function external_format_text($text, $textformat, $contextorid, $component = null, $filearea = null, $itemid = null,
$options = null) {
global $CFG;
// Get settings (singleton).
$settings = external_settings::get_instance();
+ if (is_object($contextorid) && is_a($contextorid, 'context')) {
+ $context = $contextorid;
+ $contextid = $context->id;
+ } else {
+ $context = null;
+ $contextid = $contextorid;
+ }
+
if ($component and $filearea and $settings->get_fileurl()) {
require_once($CFG->libdir . "/filelib.php");
$text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $contextid, $component, $filearea, $itemid);
@@ -979,7 +991,7 @@ function external_format_text($text, $textformat, $contextid, $component = null,
$options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter();
$options['para'] = isset($options['para']) ? $options['para'] : false;
- $options['context'] = context::instance_by_id($contextid);
+ $options['context'] = !is_null($context) ? $context : context::instance_by_id($contextid);
$options['allowid'] = isset($options['allowid']) ? $options['allowid'] : true;
$text = format_text($text, $textformat, $options);
diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php
index 5d449da04d0..2f1aaf640ab 100644
--- a/lib/tests/externallib_test.php
+++ b/lib/tests/externallib_test.php
@@ -128,7 +128,9 @@ class core_externallib_testcase extends advanced_testcase {
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = array($test, $testformat);
+ // Function external_format_text should work with context id or context instance.
$this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
+ $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct);
$settings->set_raw(false);
$settings->set_filter(true);
@@ -137,48 +139,62 @@ class core_externallib_testcase extends advanced_testcase {
$testformat = FORMAT_MARKDOWN;
$correct = array(' $$ \pi $$
$$ \pi $$
', FORMAT_HTML); + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, ['filter' => false]), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, ['filter' => false]), $correct); $test = ''; $testformat = FORMAT_HTML; $correct = array($test, FORMAT_HTML); $options = array('allowid' => true); + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = ''; $testformat = FORMAT_HTML; $correct = array('', FORMAT_HTML); $options = new StdClass(); $options->allowid = false; + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = ''."\n".'Newline'; $testformat = FORMAT_MOODLE; $correct = array(' Newline', FORMAT_HTML); $options = new StdClass(); $options->newlines = false; + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = ''; $testformat = FORMAT_MOODLE; $correct = array('