MDL-48984 mod_lesson: some lesson_answers answers should be plain text

Branch tables jump descriptions, short answer question answers
and numerical question answers should be plain text, and no editor
should be available here.
This commit is contained in:
Jean-Michel Vedrine 2015-02-08 12:00:23 +01:00
parent dba564c7db
commit a1300e98bb
5 changed files with 88 additions and 19 deletions

View File

@ -233,6 +233,25 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
}
$rs->close();
// Replay the upgrade step 2015022700
// to clean lesson answers that should be plain text.
// 1 = LESSON_PAGE_SHORTANSWER, 8 = LESSON_PAGE_NUMERICAL, 20 = LESSON_PAGE_BRANCHTABLE.
$sql = 'SELECT a.*
FROM {lesson_answers} a
JOIN {lesson_pages} p ON p.id = a.pageid
WHERE a.answerformat <> :format
AND a.lessonid = :lessonid
AND p.qtype IN (1, 8, 20)';
$badanswers = $DB->get_recordset_sql($sql, array('lessonid' => $this->task->get_activityid(), 'format' => FORMAT_MOODLE));
foreach ($badanswers as $badanswer) {
// Strip tags from answer text and convert back the format to FORMAT_MOODLE.
$badanswer->answer = strip_tags($badanswer->answer);
$badanswer->answerformat = FORMAT_MOODLE;
$DB->update_record('lesson_answers', $badanswer);
}
// Re-map the dependency and activitylink information
// If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
// lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the

View File

@ -137,5 +137,30 @@ function xmldb_lesson_upgrade($oldversion) {
// Lesson savepoint reached.
upgrade_mod_savepoint(true, 2014122900, 'lesson');
}
if ($oldversion < 2015022700) {
// Clean lesson answers that should be plain text.
// Unfortunately we can't use LESSON_PAGE_XX constants here as we can't include the files.
// 1 = LESSON_PAGE_SHORTANSWER, 8 = LESSON_PAGE_NUMERICAL, 20 = LESSON_PAGE_BRANCHTABLE.
$sql = 'SELECT a.*
FROM {lesson_answers} a
JOIN {lesson_pages} p ON p.id = a.pageid
WHERE a.answerformat <> :format
AND p.qtype IN (1, 8, 20)';
$badanswers = $DB->get_recordset_sql($sql, array('format' => FORMAT_MOODLE));
foreach ($badanswers as $badanswer) {
// Strip tags from answer text and convert back the format to FORMAT_MOODLE.
$badanswer->answer = strip_tags($badanswer->answer);
$badanswer->answerformat = FORMAT_MOODLE;
$DB->update_record('lesson_answers', $badanswer);
}
// Lesson savepoint reached.
upgrade_mod_savepoint(true, 2015022700, 'lesson');
}
return true;
}

View File

@ -93,20 +93,29 @@ if ($edit) {
$answereditor = 'answer_editor['.$answerscount.']';
if (is_array($data->$answereditor)) {
$answerdata = $data->$answereditor;
$answerdraftid = file_get_submitted_draft_itemid($answereditor);
$answertext = file_prepare_draft_area($answerdraftid, $PAGE->cm->context->id,
'mod_lesson', 'page_answers', $answer->id, $editoroptions, $answerdata['text']);
$data->$answereditor = array('text' => $answertext, 'format' => $answerdata['format'], 'itemid' => $answerdraftid);
if ($answerdata['format'] != FORMAT_MOODLE) {
$answerdata = $data->$answereditor;
$answerdraftid = file_get_submitted_draft_itemid($answereditor);
$answertext = file_prepare_draft_area($answerdraftid, $PAGE->cm->context->id,
'mod_lesson', 'page_answers', $answer->id, $editoroptions, $answerdata['text']);
$data->$answereditor = array('text' => $answertext, 'format' => $answerdata['format'], 'itemid' => $answerdraftid);
} else {
$data->$answereditor = $answerdata['text'];
}
}
$responseeditor = 'response_editor['.$answerscount.']';
if (is_array($data->$responseeditor)) {
$responsedata = $data->$responseeditor;
$responsedraftid = file_get_submitted_draft_itemid($responseeditor);
$responsetext = file_prepare_draft_area($responsedraftid, $PAGE->cm->context->id,
'mod_lesson', 'page_responses', $answer->id, $editoroptions, $responsedata['text']);
$data->$responseeditor = array('text' => $responsetext, 'format' => $responsedata['format'],
'itemid' => $responsedraftid);
if ($responsedata['format'] != FORMAT_MOODLE) {
$responsedraftid = file_get_submitted_draft_itemid($responseeditor);
$responsetext = file_prepare_draft_area($responsedraftid, $PAGE->cm->context->id,
'mod_lesson', 'page_responses', $answer->id, $editoroptions, $responsedata['text']);
$data->$responseeditor = array('text' => $responsetext, 'format' => $responsedata['format'],
'itemid' => $responsedraftid);
} else {
$data->$responseeditor = $responsedata['text'];
}
}
$answerscount++;
}

View File

@ -802,9 +802,9 @@ abstract class lesson_add_page_form_base extends moodleform {
$this->_form->setType('answer_editor['.$count.']', PARAM_RAW);
$this->_form->setDefault('answer_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
} else {
$this->_form->addElement('editor', 'answer_editor['.$count.']', $label,
array('rows' => '4', 'columns' => '80'), array('noclean' => true));
$this->_form->setDefault('answer_editor['.$count.']', array('text' => '', 'format' => FORMAT_MOODLE));
$this->_form->addElement('text', 'answer_editor['.$count.']', $label,
array('size' => '50', 'maxlength' => '200'));
$this->_form->setType('answer_editor['.$count.']', PARAM_TEXT);
}
if ($required) {
@ -2328,6 +2328,7 @@ abstract class lesson_page extends lesson_base {
\mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
if ($this->type == self::TYPE_STRUCTURE && $this->get_typeid() != LESSON_PAGE_BRANCHTABLE) {
// These page types have only one answer to save the jump and score.
if (count($answers) > 1) {
$answer = array_shift($answers);
foreach ($answers as $a) {
@ -2363,10 +2364,18 @@ abstract class lesson_page extends lesson_base {
$this->answers[$i]->timecreated = $this->timecreated;
}
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
$this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
if (!empty($properties->answer_editor[$i])) {
if (is_array($properties->answer_editor[$i])) {
// Multichoice and true/false pages have an HTML editor.
$this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
$this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
} else {
// Branch tables, shortanswer and mumerical pages have only a text field.
$this->answers[$i]->answer = $properties->answer_editor[$i];
$this->answers[$i]->answerformat = FORMAT_MOODLE;
}
}
if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
$this->answers[$i]->response = $properties->response_editor[$i]['text'];
$this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
@ -2469,9 +2478,16 @@ abstract class lesson_page extends lesson_base {
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
$answer = clone($newanswer);
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$answer->answer = $properties->answer_editor[$i]['text'];
$answer->answerformat = $properties->answer_editor[$i]['format'];
if (!empty($properties->answer_editor[$i])) {
if (is_array($properties->answer_editor[$i])) {
// Multichoice and true/false pages have an HTML editor.
$answer->answer = $properties->answer_editor[$i]['text'];
$answer->answerformat = $properties->answer_editor[$i]['format'];
} else {
// Branch tables, shortanswer and mumerical pages have only a text field.
$answer->answer = $properties->answer_editor[$i];
$answer->answerformat = FORMAT_MOODLE;
}
}
if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
$answer->response = $properties->response_editor[$i]['text'];

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015021900; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2015022700; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;