Skip to content
Browse files

MDL-38505 Permit lesson to import images in question text

  • Loading branch information...
1 parent d2aa53b commit efbe07b03c8292e3d79f1cbfc96d84090fd33118 Jean-Michel Vedrine committed Mar 15, 2013
Showing with 22 additions and 1 deletion.
  1. +20 −1 mod/lesson/format.php
  2. +2 −0 mod/lesson/import.php
View
21 mod/lesson/format.php
@@ -285,6 +285,7 @@ class qformat_default {
var $displayerrors = true;
var $category = null;
var $questionids = array();
+ protected $importcontext = null;
var $qtypeconvert = array('numerical' => LESSON_PAGE_NUMERICAL,
'multichoice' => LESSON_PAGE_MULTICHOICE,
'truefalse' => LESSON_PAGE_TRUEFALSE,
@@ -297,6 +298,10 @@ function provide_import() {
return false;
}
+ function set_importcontext($context) {
+ $this->importcontext = $context;
+ }
+
function importpreprocess() {
// Does any pre-processing that may be desired
return true;
@@ -410,6 +415,15 @@ function importprocess($filename, $lesson, $pageid) {
$this->questionids[] = $question->id;
+ // Import images in question text.
+ if (isset($question->questiontextitemid)) {
+ $questiontext = file_save_draft_area_files($question->questiontextitemid,
+ $this->importcontext->id, 'mod_lesson', 'page_contents', $newpageid,
+ null , $question->questiontext);
+ // Update content with recoded urls.
+ $DB->set_field("lesson_pages", "contents", $questiontext, array("id" => $newpageid));
+ }
+
// Now to save all the answers and type-specific options
$question->lessonid = $lesson->id; // needed for foreign key
@@ -603,7 +617,12 @@ function importpostprocess() {
protected function format_question_text($question) {
$formatoptions = new stdClass();
$formatoptions->noclean = true;
- return html_to_text(format_text($question->questiontext,
+ // The html_to_text call strips out all URLs, but format_text complains
+ // if it finds @@PLUGINFILE@@ tokens. So, we need to replace
+ // @@PLUGINFILE@@ with a real URL, but it doesn't matter what.
+ // We use http://example.com/.
+ $text = str_replace('@@PLUGINFILE@@/', 'http://example.com/', $question->questiontext);
+ return html_to_text(format_text($text,
$question->questiontextformat, $formatoptions), 0, false);
}
View
2 mod/lesson/import.php
@@ -82,6 +82,8 @@
require_once($formatclassfile);
$format = new $formatclass();
+ $format->set_importcontext($context);
+
// Do anything before that we need to
if (! $format->importpreprocess()) {
print_error('preprocesserror', 'lesson');

0 comments on commit efbe07b

Please sign in to comment.
Something went wrong with that request. Please try again.