Skip to content

Commit

Permalink
MDL-41090 questions: Allow embedding files in response comments
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhancox committed Jan 15, 2018
1 parent fd2ce92 commit f3d9872
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
42 changes: 37 additions & 5 deletions mod/quiz/tests/behat/manually_mark_question.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ Feature: Teachers can override the grade for any question
And I press "Submit all and finish"
And I click on "Submit all and finish" "button" in the "Confirmation" "dialogue"
And I log out
And I log in as "teacher1"

@javascript @_switch_window @_bug_phantomjs
Scenario: Validating the marking of an essay question attempt.
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Quiz 1"
And I follow "Attempts: 1"
And I follow "Review attempt"

@javascript @_switch_window @_bug_phantomjs
Scenario: Validating the marking of an essay question attempt.
When I follow "Make comment or override mark"
And I follow "Make comment or override mark"
And I switch to "commentquestion" window
And I set the field "Mark" to "25"
And I press "Save"
Expand All @@ -57,3 +57,35 @@ Feature: Teachers can override the grade for any question
And I should see "Complete" in the "Manually graded 10 with comment: " "table_row"
# This time is same as time the window is open. So wait for it to close before proceeding.
And I wait "2" seconds

@javascript @_switch_window @_file_upload @_bug_phantomjs
Scenario: Comment on a response to an essay question attempt.
When I log in as "teacher1"
And I follow "Manage private files"
And I upload "mod/quiz/tests/fixtures/moodle_logo.jpg" file to "Files" filemanager
And I click on "Save changes" "button"
And I am on "Course 1" course homepage
And I follow "Quiz 1"
And I follow "Attempts: 1"
And I follow "Review attempt"
And I follow "Make comment or override mark"
And I switch to "commentquestion" window
And I set the field "Comment" to "Administrator's comment"
# Atto needs focus to add image, select empty p tag to do so.
And I select the text in the "Comment" Atto editor
And I click on "Image" "button" in the "[data-fieldtype=editor]" "css_element"
And I click on "Browse repositories..." "button"
And I click on "Private files" "link" in the ".fp-repo-area" "css_element"
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"
# Editor is not inserting the html for the image correctly
# when running under behat so line below manually inserts it.
And I set the field "Comment" to "<img src=\"@@PLUGINFILE@@/moodle_logo.jpg\" alt=\"It's the logo\" width=\"48\" height=\"48\" class=\"img-responsive atto_image_button_text-bottom\"><!-- File hash: a8e3ffba4ab315b3fb9187ebbf122fe9 -->"
And I press "Save" and switch to main window
And I switch to the main window
And I should see "It's the logo" in the "3" "table_row"
And "//*[contains(@class, 'comment')]//img[contains(@src, 'moodle_logo.jpg')]" "xpath_element" should exist
# This time is same as time the window is open. So wait for it to close before proceeding.
And I wait "2" seconds
Binary file added mod/quiz/tests/fixtures/moodle_logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 22 additions & 5 deletions question/behaviour/behaviourbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ public function render(question_display_options $options, $number,
*/
public function check_file_access($options, $component, $filearea, $args, $forcedownload) {
$this->adjust_display_options($options);

if ($component == 'question' && $filearea == 'response_bf_comment') {
foreach ($this->qa->get_step_iterator() as $attemptstep) {
if ($attemptstep->get_id() == $args[0]) {
return true;
}
}

return false;
}

return $this->question->check_file_access($this->qa, $options, $component,
$filearea, $args, $forcedownload);
}
Expand Down Expand Up @@ -202,7 +213,7 @@ public function get_expected_data() {
return array();
}

$vars = array('comment' => PARAM_RAW, 'commentformat' => PARAM_INT);
$vars = array('comment' => question_attempt::PARAM_RAW_FILES, 'commentformat' => PARAM_INT);
if ($this->qa->get_max_mark()) {
$vars['mark'] = PARAM_RAW_TRIMMED;
$vars['maxmark'] = PARAM_FLOAT;
Expand Down Expand Up @@ -507,15 +518,20 @@ public function process_comment(question_attempt_pending_step $pendingstep) {
* @param $comment the comment text to format. If omitted,
* $this->qa->get_manual_comment() is used.
* @param $commentformat the format of the comment, one of the FORMAT_... constants.
* @param $context the quiz context.
* @return string the comment, ready to be output.
*/
public function format_comment($comment = null, $commentformat = null) {
public function format_comment($comment = null, $commentformat = null, $context = null) {
$formatoptions = new stdClass();
$formatoptions->noclean = true;
$formatoptions->para = false;

if (is_null($comment)) {
list($comment, $commentformat) = $this->qa->get_manual_comment();
list($comment, $commentformat, $commentstep) = $this->qa->get_manual_comment();
}

if ($context !== null) {
$comment = $this->qa->rewrite_response_pluginfile_urls($comment, $context->id, 'bf_comment', $commentstep);
}

return format_text($comment, $commentformat, $formatoptions);
Expand All @@ -528,8 +544,9 @@ public function format_comment($comment = null, $commentformat = null) {
protected function summarise_manual_comment($step) {
$a = new stdClass();
if ($step->has_behaviour_var('comment')) {
$a->comment = shorten_text(html_to_text($this->format_comment(
$step->get_behaviour_var('comment')), 0, false), 200);
list($comment, $commentformat, $commentstep) = $this->qa->get_manual_comment();
$comment = question_utils::to_plain_text($comment, $commentformat);
$a->comment = shorten_text($comment, 200);
} else {
$a->comment = '';
}
Expand Down
28 changes: 24 additions & 4 deletions question/behaviour/rendererbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ public function feedback(question_attempt $qa, question_display_options $options
}

public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
global $CFG;

require_once($CFG->dirroot.'/lib/filelib.php');
require_once($CFG->dirroot.'/repository/lib.php');

$inputname = $qa->get_behaviour_field_name('comment');
$id = $inputname . '_id';
list($commenttext, $commentformat) = $qa->get_current_manual_comment();
list($commenttext, $commentformat, $commentstep) = $qa->get_current_manual_comment();

$editor = editors_get_preferred_editor($commentformat);
$strformats = format_text_menu();
Expand All @@ -80,12 +85,27 @@ public function manual_comment_fields(question_attempt $qa, question_display_opt
$formats[$fid] = $strformats[$fid];
}

$draftitemareainputname = $qa->get_behaviour_field_name('comment:itemid');
$draftitemid = optional_param($draftitemareainputname, false, PARAM_INT);

if (!$draftitemid && $commentstep === null) {
$commenttext = '';
$draftitemid = file_get_unused_draft_itemid();
} else if (!$draftitemid) {
list($draftitemid, $commenttext) = $commentstep->prepare_response_files_draft_itemid_with_text(
'bf_comment', $options->context->id, $commenttext);
}

$editor->set_text($commenttext);
$editor->use_editor($id, array('context' => $options->context));
$editor->use_editor($id, question_utils::get_editor_options($options->context),
question_utils::get_filepicker_options($options->context, $draftitemid));

$commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),
array('id' => $id, 'name' => $inputname, 'rows' => 10, 'cols' => 60)));

$attributes = ['type' => 'hidden', 'name' => $draftitemareainputname, 'value' => $draftitemid];
$commenteditor .= html_writer::empty_tag('input', $attributes);

$editorformat = '';
if (count($formats) == 1) {
reset($formats);
Expand All @@ -105,7 +125,7 @@ public function manual_comment_fields(question_attempt $qa, question_display_opt
$comment = html_writer::tag('div', html_writer::tag('div',
html_writer::tag('label', get_string('comment', 'question'),
array('for' => $id)), array('class' => 'fitemtitle')) .
html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor')),
html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor', 'data-fieldtype' => "editor")),
array('class' => 'fitem'));
$comment .= $editorformat;

Expand Down Expand Up @@ -168,7 +188,7 @@ public function manual_comment_fields(question_attempt $qa, question_display_opt
public function manual_comment_view(question_attempt $qa, question_display_options $options) {
$output = '';
if ($qa->has_manual_comment()) {
$output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
$output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment(null, null, $options->context));
}
if ($options->manualcommentlink) {
$url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
Expand Down
9 changes: 5 additions & 4 deletions question/engine/questionattempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -1391,16 +1391,17 @@ public function has_manual_comment() {

/**
* @return array(string, int) the most recent manual comment that was added
* to this question, and the FORMAT_... it is.
* to this question, the FORMAT_... it is and the step itself.
*/
public function get_manual_comment() {
foreach ($this->get_reverse_step_iterator() as $step) {
if ($step->has_behaviour_var('comment')) {
return array($step->get_behaviour_var('comment'),
$step->get_behaviour_var('commentformat'));
$step->get_behaviour_var('commentformat'),
$step);
}
}
return array(null, null);
return array(null, null, null);
}

/**
Expand All @@ -1420,7 +1421,7 @@ public function get_current_manual_comment() {
if ($commentformat === null) {
$commentformat = FORMAT_HTML;
}
return array($comment, $commentformat);
return array($comment, $commentformat, null);
}
}

Expand Down

0 comments on commit f3d9872

Please sign in to comment.