Merge branch 'MDL-51213-master' of git://github.com/damyon/moodle

This commit is contained in:
David Monllao 2015-09-15 17:28:34 +08:00
commit 3bc0824e46
4 changed files with 99 additions and 4 deletions

View File

@ -155,6 +155,8 @@ class core_grades_external extends external_api {
continue;
}
$context = $coursecontext;
if ($activitygrade->itemtype == 'course') {
$item = grade_get_course_grades($course->id, $params['userids']);
$item->itemnumber = 0;
@ -166,6 +168,8 @@ class core_grades_external extends external_api {
} else {
$cm = $activityinstances[$activitygrade->itemmodule][$activitygrade->iteminstance];
$instance = $cm->instance;
$context = context_module::instance($cm->id, IGNORE_MISSING);
$grades = grade_get_grades($params['courseid'], $activitygrade->itemtype,
$activitygrade->itemmodule, $instance, $params['userids']);
}
@ -211,7 +215,7 @@ class core_grades_external extends external_api {
if ($gradeiteminstance->itemtype != 'course' and !empty($studentgrade->feedback)) {
list($studentgrade->feedback, $studentgrade->feedbackformat) =
external_format_text($studentgrade->feedback, $studentgrade->feedbackformat,
$cm->id, $params['component'], 'feedback', null);
$context->id, $params['component'], 'feedback', null);
}
$gradeitemarray['grades'][$studentid] = (array)$studentgrade;
@ -265,7 +269,7 @@ class core_grades_external extends external_api {
if (!empty($studentgrade->feedback)) {
list($studentgrade->feedback, $studentgrade->feedbackformat) =
external_format_text($studentgrade->feedback, $studentgrade->feedbackformat,
$cm->id, $params['component'], 'feedback', null);
$context->id, $params['component'], 'feedback', null);
}
$gradesarray['outcomes'][$cm->id]['grades'][$studentid] = (array)$studentgrade;

View File

@ -730,6 +730,38 @@ function external_validate_format($format) {
return $format;
}
/**
* Format the string to be returned properly as requested by the either the web service server,
* either by an internally call.
* The caller can change the format (raw) with the external_settings singleton
* All web service servers must set this singleton when parsing the $_GET and $_POST.
*
* @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 array $options options array/object or courseid
* @return string text
* @since Moodle 3.0
*/
function external_format_string($str, $contextid, $striplinks = true, $options = array()) {
// Get settings (singleton).
$settings = external_settings::get_instance();
if (empty($contextid)) {
throw new coding_exception('contextid is required');
}
if (!$settings->get_raw()) {
$context = context::instance_by_id($contextid);
$options['context'] = $context;
$options['filter'] = $settings->get_filter();
$str = format_string($str, $striplinks, $options);
}
return $str;
}
/**
* Format the text to be returned properly as requested by the either the web service server,
* either by an internally call.
@ -757,7 +789,8 @@ function external_format_text($text, $textformat, $contextid, $component, $filea
}
if (!$settings->get_raw()) {
$text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter()));
$context = context::instance_by_id($contextid);
$text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter(), 'context' => $context));
$textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML).
}
@ -795,6 +828,10 @@ class external_settings {
* Constructor - protected - can not be instanciated
*/
protected function __construct() {
if (!defined('AJAX_SCRIPT') && !defined('CLI_SCRIPT') && !defined('WS_SERVER')) {
// For normal pages, the default should match the default for format_text.
$this->filter = true;
}
}
/**

View File

@ -72,6 +72,57 @@ class core_externallib_testcase extends advanced_testcase {
$this->assertSame('aaa', $result['text']);
}
public function test_external_format_text() {
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$settings->set_raw(true);
$settings->set_filter(false);
$context = context_system::instance();
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = array($test, $testformat);
$this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
$settings->set_raw(false);
$settings->set_filter(true);
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = array('<span class="nolink"><span class="filter_mathjaxloader_equation"><p>$$ \pi $$</p>
</span></span>', FORMAT_HTML);
$this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
$settings->set_raw($currentraw);
$settings->set_filter($currentfilter);
}
public function test_external_format_string() {
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$settings->set_raw(true);
$context = context_system::instance();
$test = '$$ \pi $$ <script>hi</script> <h3>there</h3>';
$correct = $test;
$this->assertSame(external_format_string($test, $context->id), $correct);
$settings->set_raw(false);
$test = '$$ \pi $$<script>hi</script> <h3>there</h3>';
$correct = '$$ \pi $$hi there';
$this->assertSame(external_format_string($test, $context->id), $correct);
$settings->set_raw($currentraw);
$settings->set_filter($currentfilter);
}
/**
* Test for clean_returnvalue().
*/

View File

@ -1354,6 +1354,9 @@ function format_string($string, $striplinks = true, $options = null) {
} else if (is_numeric($options['context'])) {
$options['context'] = context::instance_by_id($options['context']);
}
if (!isset($options['filter'])) {
$options['filter'] = true;
}
if (!$options['context']) {
// We did not find any context? weird.
@ -1372,7 +1375,7 @@ function format_string($string, $striplinks = true, $options = null) {
// Regular expression moved to its own method for easier unit testing.
$string = replace_ampersands_not_followed_by_entity($string);
if (!empty($CFG->filterall)) {
if (!empty($CFG->filterall) && $options['filter']) {
$filtermanager = filter_manager::instance();
$filtermanager->setup_page_for_filters($PAGE, $options['context']); // Setup global stuff filters may have.
$string = $filtermanager->filter_string($string, $options['context']);