Skip to content

Commit 125832b

Browse files
committed
q-edit - Ovveride default editor
1 parent cead326 commit 125832b

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

edit_stack_form.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class qtype_stack_edit_form extends question_edit_form {
7777
* Patch up data from the database before a user edits it in the form.
7878
*/
7979
public function set_data($question) {
80+
global $USER;
8081
if (!empty($question->questiontext)) {
8182
$question->questiontext = $this->convert_legacy_fact_sheets($question->questiontext);
8283
}
@@ -103,6 +104,17 @@ public function set_data($question) {
103104
$question->generalfeedback = $feedbackstring;
104105
}
105106

107+
// See prepare_text_field() for explanation of swapping formats.
108+
$editorpreference = get_user_preferences('htmleditor', '', $USER);
109+
if ($editorpreference === '') {
110+
if ($question->questiontextformat == FORMAT_HTML && $this->search_for_scripts($question->questiontext)) {
111+
$question->questiontextformat = FORMAT_PLAIN;
112+
}
113+
if ($question->generalfeedbackformat == FORMAT_HTML && $this->search_for_scripts($question->generalfeedback)) {
114+
$question->generalfeedbackformat = FORMAT_PLAIN;
115+
}
116+
}
117+
106118
parent::set_data($question);
107119
}
108120

@@ -216,7 +228,6 @@ protected function definition_inner(/* MoodleQuickForm */ $mform) {
216228
$prtnames = $qtype->get_prt_names_from_question($this->get_current_question_text(),
217229
$this->get_current_specific_feedback());
218230

219-
// TO-DO: add in warnings here. See b764b39675 for deleted materials.
220231
$warnings = '';
221232
if (isset($this->question->id)) {
222233
$question = question_bank::load_question($this->question->id);
@@ -935,6 +946,16 @@ protected function data_preprocessing_node($question, $prtname, $node) {
935946
* @return array in the format needed by the form.
936947
*/
937948
protected function prepare_text_field($field, $text, $format, $itemid, $filearea = '') {
949+
// If user editor preference is set to default and the format of the field is the
950+
// default (HTML) and the text (probably) contains scripts, this is likely to be bad.
951+
// The question will break if saved as the HTML text editor will make substitutions.
952+
// Switch format to plain text. User can switch format back to HTML if they really
953+
// want to.
954+
global $USER;
955+
$preference = get_user_preferences('htmleditor', '', $USER);
956+
if ($preference === '' && $format == FORMAT_HTML && $this->search_for_scripts($text)) {
957+
$format = FORMAT_PLAIN;
958+
}
938959
if ($filearea === '') {
939960
$filearea = $field;
940961
}
@@ -959,4 +980,17 @@ public function validation($fromform, $files) {
959980
public function qtype() {
960981
return 'stack';
961982
}
983+
984+
/**
985+
* Check whether some text contains JSXgraph, GeoGebra or scripts.
986+
* @param string|null $text
987+
* @return bool
988+
*/
989+
public function search_for_scripts($text) {
990+
if (!$text) {
991+
return false;
992+
}
993+
return preg_match("/<\/jsxgraph>|\[\[jsxgraph|\[\[geogebra|<\/geogebra>|<script/i", $text);
994+
}
995+
962996
}

0 commit comments

Comments
 (0)