// Annotate the file areas in user by the lesson module.
$lesson->annotate_files('mod_lesson', 'mediafile', null);
$page->annotate_files('mod_lesson', 'page_contents', 'id');
- $answer->annotate_files('mod_lesson', 'page_answers', 'id');
- $answer->annotate_files('mod_lesson', 'page_responses', 'id');
// Prepare and return the structure we have just created for the lesson module.
return $this->prepare_activity_structure($lesson);
$contents = array();
$contents[] = new restore_decode_content('lesson_pages', array('contents'), 'lesson_page');
- $contents[] = new restore_decode_content('lesson_answers', array('answer', 'response'), 'lesson_answer');
return $contents;
}
// Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when
// processing attempts. It will be corrected in after_execute
- $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas.
+ $this->set_mapping('lesson_answer', $data->id, 0);
// Answers need to be processed in order, so we store them in an
// instance variable and insert them in the after_execute stage
ksort($this->answers);
foreach ($this->answers as $answer) {
$newitemid = $DB->insert_record('lesson_answers', $answer);
- $this->set_mapping('lesson_answer', $answer->id, $newitemid, true);
+ $this->set_mapping('lesson_answer', $answer->id, $newitemid);
// Update the lesson attempts to use the newly created answerid
$DB->set_field('lesson_attempts', 'answerid', $newitemid, array(
$this->add_related_files('mod_lesson', 'mediafile', null);
// Add lesson page files, by lesson_page itemname
$this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
- $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
- $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
// Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
$rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
$data->id = $cm->id;
$editoroptions['context'] = $context;
$data = file_prepare_standard_editor($data, 'contents', $editoroptions, $context, 'mod_lesson', 'page_contents', $editpage->id);
-
- $answerscount = 0;
- $answers = $editpage->get_answers();
- foreach ($answers as $answer) {
- $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);
- }
-
- $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);
- }
- $answerscount++;
- }
-
$mform->set_data($data);
$PAGE->navbar->add(get_string('edit'), new moodle_url('/mod/lesson/edit.php', array('id'=>$id)));
$PAGE->navbar->add(get_string('editingquestionpage', 'lesson', get_string($mform->qtypestring, 'lesson')));
$string['page-mod-lesson-x'] = 'Any lesson page';
$string['page-mod-lesson-view'] = 'View or preview lesson page';
$string['page-mod-lesson-edit'] = 'Edit lesson page';
-$string['pageanswers'] = 'Page answers';
$string['pagecontents'] = 'Page contents';
-$string['pageresponses'] = 'Page responses';
$string['pages'] = 'Pages';
$string['pagetitle'] = 'Page title';
$string['password'] = 'Password';
}
$fullpath = "/$context->id/mod_lesson/$filearea/$pageid/".implode('/', $args);
- } else if ($filearea === 'page_answers' || $filearea === 'page_responses') {
- $itemid = (int)array_shift($args);
- if (!$pageanswers = $DB->get_record('lesson_answers', array('id' => $itemid))) {
- return false;
- }
- $fullpath = "/$context->id/mod_lesson/$filearea/$itemid/".implode('/', $args);
-
} else if ($filearea === 'mediafile') {
if (count($args) > 1) {
// Remove the itemid when it appears to be part of the arguments. If there is only one argument
$areas = array();
$areas['page_contents'] = get_string('pagecontents', 'mod_lesson');
$areas['mediafile'] = get_string('mediafile', 'mod_lesson');
- $areas['page_answers'] = get_string('pageanswers', 'mod_lesson');
- $areas['page_responses'] = get_string('pageresponses', 'mod_lesson');
return $areas;
}
if ($label === null) {
$label = get_string('answer', 'lesson');
}
-
- if ($this->qtype != 'multichoice' && $this->qtype != 'matching') {
- $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));
- } else {
- $this->_form->addElement('editor', 'answer_editor['.$count.']', $label,
- array('rows' => '4', 'columns' => '80'),
- array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
- $this->_form->setType('answer_editor['.$count.']', PARAM_RAW);
- $this->_form->setDefault('answer_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
- }
-
+ $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));
if ($required) {
$this->_form->addRule('answer_editor['.$count.']', get_string('required'), 'required', null, 'client');
}
if ($label === null) {
$label = get_string('response', 'lesson');
}
- $this->_form->addElement('editor', 'response_editor['.$count.']', $label,
- array('rows' => '4', 'columns' => '80'),
- array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
- $this->_form->setType('response_editor['.$count.']', PARAM_RAW);
- $this->_form->setDefault('response_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
-
+ $this->_form->addElement('editor', 'response_editor['.$count.']', $label, array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
+ $this->_form->setDefault('response_editor['.$count.']', array('text'=>'', 'format'=>FORMAT_MOODLE));
if ($required) {
$this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
}
$context = context_module::instance($cm->id);
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_lesson', 'page_contents', $this->properties->id);
- $fs->delete_area_files($context->id, 'mod_lesson', 'page_answers', $this->properties->id);
- $fs->delete_area_files($context->id, 'mod_lesson', 'page_responses', $this->properties->id);
// repair the hole in the linkage
if (!$this->properties->prevpageid && !$this->properties->nextpageid) {
* @return stdClass Returns the result of the attempt
*/
final public function record_attempt($context) {
- global $DB, $USER, $OUTPUT, $PAGE;
+ global $DB, $USER, $OUTPUT;
/**
* This should be overridden by each page type to actually check the response
$options->noclean = true;
$options->para = true;
$options->overflowdiv = true;
- $result->response = file_rewrite_pluginfile_urls($result->response, 'pluginfile.php', $context->id,
- 'mod_lesson', 'page_responses', $result->answerid);
-
$result->feedback = $OUTPUT->box(format_text($this->get_contents(), $this->properties->contentsformat, $options), 'generalbox boxaligncenter');
$result->feedback .= '<div class="correctanswer generalbox"><em>'.get_string("youranswer", "lesson").'</em> : '.$result->studentanswer; // already in clean html
$result->feedback .= $OUTPUT->box($result->response, $class); // already conerted to HTML
return true;
}
- /**
- * save editor answers files and update answer record
- *
- * @param object $context
- * @param int $maxbytes
- * @param object $answer
- * @param object $answereditor
- * @param object $responseeditor
- */
- public function save_answers_files($context, $maxbytes, &$answer, $answereditor = '', $responseeditor = '') {
- global $DB;
- if (isset($answereditor['itemid'])) {
- $answer->answer = file_save_draft_area_files($answereditor['itemid'],
- $context->id, 'mod_lesson', 'page_answers', $answer->id,
- array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $maxbytes),
- $answer->answer, null);
- $DB->set_field('lesson_answers', 'answer', $answer->answer, array('id' => $answer->id));
- }
- if (isset($responseeditor['itemid'])) {
- $answer->response = file_save_draft_area_files($responseeditor['itemid'],
- $context->id, 'mod_lesson', 'page_responses', $answer->id,
- array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $maxbytes),
- $answer->response, null);
- $DB->set_field('lesson_answers', 'response', $answer->response, array('id' => $answer->id));
- }
- }
-
- /**
- * Rewrite urls in answer and response of a question answer
- *
- * @param object $answer
- * @return object answer with rewritten urls
- */
- public static function rewrite_answers_urls($answer) {
- global $PAGE;
-
- $context = context_module::instance($PAGE->cm->id);
- $answer->answer = file_rewrite_pluginfile_urls($answer->answer, 'pluginfile.php', $context->id,
- 'mod_lesson', 'page_answers', $answer->id);
- $answer->response = file_rewrite_pluginfile_urls($answer->response, 'pluginfile.php', $context->id,
- 'mod_lesson', 'page_responses', $answer->id);
-
- return $answer;
- }
-
/**
* Updates a lesson page and its answers within the database
*
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
- // Save files in answers and responses.
- $this->save_answers_files($context, $maxbytes, $this->answers[$i],
- $properties->answer_editor[$i], $properties->response_editor[$i]);
-
} else if (isset($this->answers[$i]->id)) {
$DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
unset($this->answers[$i]);
* @return array
*/
public function create_answers($properties) {
- global $DB, $PAGE;
+ global $DB;
// now add the answers
$newanswer = new stdClass;
$newanswer->lessonid = $this->lesson->id;
$newanswer->pageid = $this->properties->id;
$newanswer->timecreated = $this->properties->timecreated;
- $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
- $context = context_module::instance($cm->id);
-
$answers = array();
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
$answer->score = $properties->score[$i];
}
$answer->id = $DB->insert_record("lesson_answers", $answer);
- if (isset($properties->response_editor[$i])) {
- $this->save_answers_files($context, $PAGE->course->maxbytes, $answer,
- $properties->answer_editor[$i], $properties->response_editor[$i]);
- } else {
- $this->save_answers_files($context, $PAGE->course->maxbytes, $answer,
- $properties->answer_editor[$i]);
- }
$answers[$answer->id] = new lesson_page_answer($answer);
} else {
break;
}
public function create_answers($properties) {
- global $DB, $PAGE;
+ global $DB;
// now add the answers
$newanswer = new stdClass;
$newanswer->lessonid = $this->lesson->id;
$newanswer->pageid = $this->properties->id;
$newanswer->timecreated = $this->properties->timecreated;
- $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
- $context = context_module::instance($cm->id);
-
$answers = array();
// need to add two to offset correct response and wrong response
if (isset($answer->answer) && $answer->answer != '') {
$answer->id = $DB->insert_record("lesson_answers", $answer);
- $this->save_answers_files($context, $PAGE->course->maxbytes,
- $answer, $properties->answer_editor[$i]);
$answers[$answer->id] = new lesson_page_answer($answer);
} else if ($i < 2) {
$answer->id = $DB->insert_record("lesson_answers", $answer);
$response = $data->response;
$getanswers = $this->get_answers();
- foreach ($getanswers as $key => $answer) {
- $getanswers[$key] = parent::rewrite_answers_urls($answer);
- }
$correct = array_shift($getanswers);
$wrong = array_shift($getanswers);
} else {
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
- // Save files in answers and responses.
- $this->save_answers_files($context, $maxbytes, $this->answers[$i],
- $properties->answer_editor[$i], $properties->response_editor[$i]);
} else if ($i < 2) {
if (!isset($this->answers[$i]->id)) {
$this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
- // Save files in answers and responses.
- $this->save_answers_files( $context, $maxbytes, $this->answers[$i],
- $properties->answer_editor[$i], $properties->response_editor[$i]);
} else if (isset($this->answers[$i]->id)) {
$DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
unset($this->answers[$i]);
public function custom_definition() {
$this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
- $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'),
- array('rows' => '4', 'columns' => '80'),
- array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
- $this->_form->setType('answer_editor[0]', PARAM_RAW);
- $this->_form->setDefault('answer_editor[0]', array('text' => '', 'format' => FORMAT_HTML));
+ $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
$this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
$this->add_score(0, get_string("correctanswerscore", "lesson"), 1);
$this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
- $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'),
- array('rows' => '4', 'columns' => '80'),
- array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
- $this->_form->setType('answer_editor[1]', PARAM_RAW);
- $this->_form->setDefault('answer_editor[1]', array('text' => '', 'format' => FORMAT_HTML));
-
+ $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
$this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
$this->add_score(1, get_string("wronganswerscore", "lesson"), 0);
class lesson_display_answer_form_matching extends moodleform {
public function definition() {
- global $USER, $OUTPUT, $PAGE;
+ global $USER, $OUTPUT;
$mform = $this->_form;
$answers = $this->_customdata['answers'];
$useranswers = $this->_customdata['useranswers'];
// Temporary fixed until MDL-38885 gets integrated
$mform->setType('response', PARAM_TEXT);
}
- $context = context_module::instance($PAGE->cm->id);
- $answer->answer = file_rewrite_pluginfile_urls($answer->answer, 'pluginfile.php', $context->id,
- 'mod_lesson', 'page_answers', $answer->id);
$mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
$mform->setType($responseid, PARAM_TEXT);
if ($hasattempt) {
foreach ($answers as $key=>$answer) {
if ($answer->answer === '') {
unset($answers[$key]);
- } else {
- $answers[$key] = parent::rewrite_answers_urls($answer);
}
}
return $answers;
if (!$answer = $DB->get_record("lesson_answers", array("id" => $result->answerid))) {
print_error("Continue: answer record not found");
}
- $answer = parent::rewrite_answers_urls($answer);
if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
$result->correctanswer = true;
}
public function display($renderer, $attempt) {
global $USER, $CFG, $PAGE;
$answers = $this->get_answers();
- foreach ($answers as $key => $answer) {
- $answers[$key] = parent::rewrite_answers_urls($answer);
- }
shuffle($answers);
$params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt);
}
$result->answerid = $data->answerid;
$answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST);
- $answer = parent::rewrite_answers_urls($answer);
if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
$result->correctanswer = true;
}
$options->para = false;
$i = 1;
foreach ($answers as $answer) {
- $answer = parent::rewrite_answers_urls($answer);
$cells = array();
if ($this->lesson->custom && $answer->score > 0) {
// if the score is > 0, then it is correct
} else {
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
- // Save files in answers and responses.
- $this->save_answers_files($context, $maxbytes, $this->answers[$i],
- $properties->answer_editor[$i], $properties->response_editor[$i]);
} else if (isset($this->answers[$i]->id)) {
$DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
unset($this->answers[$i]);
$formattextdefoptions->para = false;
$formattextdefoptions->noclean = true;
foreach ($answers as $answer) {
- $answer = parent::rewrite_answers_urls($answer);
if ($this->properties->qoption) {
if ($useranswer == null) {
$userresponse = array();
$ansid = 'answer_id';
}
- $answer = lesson_page::rewrite_answers_urls($answer);
$mform->addElement('radio', $ansid, null, format_text($answer->answer, $answer->answerformat, $options), $answer->id, $disabled);
$mform->setType($ansid, PARAM_INT);
if ($hasattempt && $answer->id == $USER->modattempts[$lessonid]->answerid) {
+++ /dev/null
-@mod @mod_lesson
-Feature: In a lesson activity, teacher can add embedded images in questions answers and responses
- As a teacher
- I need to add questions with images in answers and responses
-
- @javascript @_file_upload
- Scenario: questions with images in answers and responses
- Given the following "users" exist:
- | username | firstname | lastname | email |
- | teacher1 | Teacher | 1 | teacher1@asd.com |
- | student1 | Student | 1 | student1@asd.com |
- And the following "courses" exist:
- | fullname | shortname | category |
- | Course 1 | C1 | 0 |
- And the following "course enrolments" exist:
- | user | course | role |
- | teacher1 | C1 | editingteacher |
- | student1 | C1 | student |
- And I log in as "teacher1"
- And I navigate to "My private files" node in "My profile"
- And I upload "mod/lesson/tests/fixtures/moodle_logo.jpg" file to "Files" filemanager
- And I click on "Save changes" "button"
- When I am on homepage
- And I follow "Course 1"
- And I turn editing mode on
- And I add a "Lesson" to section "1" and I fill the form with:
- | Name | Test lesson name |
- And I follow "Test lesson name"
- And I follow "Add a question page"
- And I set the field "Select a question type" to "Multichoice"
- And I press "Add a question page"
- And I set the following fields to these values:
- | Page title | Multichoice question |
- | Page contents | What animal is an amphibian? |
- | id_answer_editor_0 | Frog |
- | id_response_editor_0 | Correct answer |
- | id_jumpto_0 | Next page |
- | id_score_0 | 1 |
- | id_answer_editor_1 | Cat |
- | id_response_editor_1 | Incorrect answer |
- | id_jumpto_1 | This page |
- | id_score_1 | 0 |
- | id_answer_editor_2 | Dog |
- | id_response_editor_2 | Incorrect answer |
- | id_jumpto_2 | This page |
- | id_score_2 | 0 |
- And I click on "Image" "button" in the "#fitem_id_answer_editor_2" "css_element"
- And I click on "Browse repositories..." "button"
- And I click on "Private files" "link"
- And I click on "moodle_logo.jpg" "link"
- And I click on "Select this file" "button"
- And I set the field "Describe this image for someone who cannot see it" to "It's the logo"
- And I click on "Save image" "button"
- And I press "Save page"
- And I set the field "qtype" to "Question"
- And I set the field "Select a question type" to "True/false"
- And I press "Add a question page"
- And I set the following fields to these values:
- | Page title | Next question |
- | Page contents | Paper is made from trees. |
- | id_answer_editor_0 | True |
- | id_response_editor_0 | Correct |
- | id_jumpto_0 | Next page |
- | id_answer_editor_1 | False |
- | id_response_editor_1 | Wrong |
- | id_jumpto_1 | This page |
- And I click on "Image" "button" in the "#fitem_id_response_editor_0" "css_element"
- And I click on "Browse repositories..." "button"
- And I click on "Private files" "link"
- And I click on "moodle_logo.jpg" "link"
- And I click on "Select this file" "button"
- And I set the field "Describe this image for someone who cannot see it" to "It's the logo"
- And I click on "Save image" "button"
- And I press "Save page"
- And I log out
- And I log in as "student1"
- And I follow "Course 1"
- When I follow "Test lesson name"
- Then I should see "What animal is an amphibian?"
- And "//img[contains(@src, 'pluginfile.php')]" "xpath_element" should exist in the ".answeroption" "css_element"
- And "//img[contains(@src, 'moodle_logo.jpg')]" "xpath_element" should exist in the ".answeroption" "css_element"
- And I set the following fields to these values:
- | Cat | 1 |
- And I press "Submit"
- And I should see "Incorrect answer"
- And I press "Continue"
- And I should see "Paper is made from trees."
- And I set the following fields to these values:
- | True | 1 |
- And I press "Submit"
- And I should see "Correct"
- And I should not see "Wrong"
- And "//img[contains(@src, 'pluginfile.php')]" "xpath_element" should exist in the ".correctanswer" "css_element"
- And "//img[contains(@src, 'moodle_logo.jpg')]" "xpath_element" should exist in the ".correctanswer" "css_element"
- And I press "Continue"
- And I should see "Congratulations - end of lesson reached"
- And I should see "Your score is 1 (out of 2)."