MDL-20636 Conversion of the essay question type.

This commit is contained in:
Tim Hunt 2011-01-12 14:51:17 +00:00
parent 7a7197482d
commit 6d03299dec
9 changed files with 272 additions and 195 deletions

View file

@ -29,46 +29,7 @@
* essay editing form definition.
*/
class question_edit_essay_form extends question_edit_form {
/**
* Add question-type specific form fields.
*
* @param MoodleQuickForm $mform the form being built.
*/
function definition_inner(&$mform) {
$mform->addElement('editor', 'feedback', get_string('feedback', 'quiz'), null, $this->editoroptions);
$mform->setType('feedback', PARAM_RAW);
$mform->addElement('hidden', 'fraction', 0);
$mform->setType('fraction', PARAM_RAW);
//don't need this default element.
$mform->removeElement('penalty');
$mform->addElement('hidden', 'penalty', 0);
$mform->setType('penalty', PARAM_RAW);
}
function data_preprocessing($question) {
if (!empty($question->options) && !empty($question->options->answers)) {
$answer = reset($question->options->answers);
$question->feedback = array();
$draftid = file_get_submitted_draft_itemid('feedback');
$question->feedback['text'] = file_prepare_draft_area(
$draftid, // draftid
$this->context->id, // context
'question', // component
'answerfeedback', // filarea
!empty($answer->id)?(int)$answer->id:null, // itemid
$this->fileoptions, // options
$answer->feedback // text
);
$question->feedback['format'] = $answer->feedbackformat;
$question->feedback['itemid'] = $draftid;
}
$question->penalty = 0;
return $question;
}
function qtype() {
public function qtype() {
return 'essay';
}
}

View file

@ -25,7 +25,7 @@
$string['addingessay'] = 'Adding an Essay question';
$string['addingessay_link'] = 'question/type/essay';
$string['editingessay'] = 'Editing a Essay question';
$string['editingessay'] = 'Editing an Essay question';
$string['essay'] = 'Essay';
$string['essay_help'] = 'In response to a question (that may include an image) the respondent writes an answer of a paragraph or two. The essay question will not be assigned a grade until it has been reviewed by a teacher and manually graded.';
$string['essaysummary'] = 'Allows a response of a few sentences or paragraphs. This must then be graded manually.';

View file

@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Essay question definition class.
*
* @package qtype_essay
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Represents an essay question.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_question extends question_with_responses {
public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
question_engine::load_behaviour_class('manualgraded');
return new qbehaviour_manualgraded($qa, $preferredbehaviour);
}
public function get_expected_data() {
return array('answer' => PARAM_CLEANHTML);
}
public function summarise_response(array $response) {
if (isset($response['answer'])) {
$formatoptions = new stdClass();
$formatoptions->para = false;
return shorten_text(html_to_text(format_text($response['answer'], FORMAT_HTML, $formatoptions), 0, false), 200);
} else {
return null;
}
}
public function get_correct_response() {
return null;
}
public function is_complete_response(array $response) {
return !empty($response['answer']);
}
public function is_same_response(array $prevresponse, array $newresponse) {
return question_utils::arrays_same_at_key_missing_is_blank(
$prevresponse, $newresponse, 'answer');
}
}

View file

@ -15,138 +15,28 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//////////////////
/// ESSAY ///
/////////////////
/// QUESTION TYPE CLASS //////////////////
/**
* @package questionbank
* @subpackage questiontypes
* Question type class for the essay question type.
*
* @package qtype
* @subpackage essay
* @copyright 2005 Mark Nielsen
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_essay_qtype extends default_questiontype {
function name() {
return 'essay';
}
function is_manual_graded() {
/**
* The essay question type.
*
* @copyright 2005 Mark Nielsen
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay extends question_type {
public function is_manual_graded() {
return true;
}
function save_question_options($question) {
global $DB;
$context = $question->context;
$answer = $DB->get_record('question_answers', array('question' => $question->id));
if (!$answer) {
$answer = new stdClass;
$answer->question = $question->id;
$answer->answer = '';
$answer->feedback = '';
$answer->id = $DB->insert_record('question_answers', $answer);
}
$answer->feedback = $question->feedback['text'];
$answer->feedbackformat = $question->feedback['format'];
$answer->answer = $answer->feedback;
$answer->answerformat = $question->feedback['format'];
$answer->fraction = $question->fraction;
$answer->feedback = $this->import_or_save_files($question->feedback,
$context, 'question', 'answerfeedback', $answer->id);
$answer->answer = $answer->feedback;
$DB->update_record('question_answers', $answer);
return true;
}
function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
global $CFG;
$context = $this->get_context_by_category_id($question->category);
$answers = &$question->options->answers;
$readonly = empty($options->readonly) ? '' : 'disabled="disabled"';
// Only use the rich text editor for the first essay question on a page.
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
$inputname = $question->name_prefix;
$stranswer = get_string("answer", "quiz").': ';
/// set question text and media
$questiontext = format_text($question->questiontext,
$question->questiontextformat,
$formatoptions, $cmoptions->course);
// feedback handling
$feedback = '';
if ($options->feedback && !empty($answers)) {
foreach ($answers as $answer) {
$feedback = quiz_rewrite_question_urls($answer->feedback, 'pluginfile.php', $context->id, 'qtype_essay', 'feedback', array($state->attempt, $state->question), $answer->id);
$feedback = format_text($feedback, $answer->feedbackformat, $formatoptions, $cmoptions->course);
}
}
// get response value
if (isset($state->responses[''])) {
$value = $state->responses[''];
} else {
$value = '';
}
// answer
if (empty($options->readonly)) {
// the student needs to type in their answer so print out a text editor
$answer = print_textarea(can_use_html_editor(), 18, 80, 630, 400,
$inputname, $value, $cmoptions->course, true);
} else {
// it is read only, so just format the students answer and output it
$safeformatoptions = new stdClass;
$safeformatoptions->para = false;
$answer = format_text($value, FORMAT_MOODLE,
$safeformatoptions, $cmoptions->course);
$answer = '<div class="answerreview">' . $answer . '</div>';
}
include("$CFG->dirroot/question/type/essay/display.html");
}
function grade_responses(&$question, &$state, $cmoptions) {
// All grading takes place in Manual Grading
$state->responses[''] = clean_param($state->responses[''], PARAM_CLEAN);
$state->raw_grade = 0;
$state->penalty = 0;
return true;
}
/**
* Runs all the code required to set up and save an essay question for testing purposes.
* Alternate DB table prefix may be used to facilitate data deletion.
*/
function generate_test($name, $courseid = null) {
global $DB;
list($form, $question) = parent::generate_test($name, $courseid);
$form->questiontext = "What is the purpose of life?";
$form->feedback = "feedback";
$form->generalfeedback = "General feedback";
$form->fraction = 0;
$form->penalty = 0;
if ($courseid) {
$course = $DB->get_record('course', array('id' => $courseid));
}
return $this->save_question($question, $form);
}
function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
@ -156,31 +46,4 @@ class question_essay_qtype extends default_questiontype {
parent::delete_files($questionid, $contextid);
$this->delete_files_in_answers($questionid, $contextid);
}
function check_file_access($question, $state, $options, $contextid, $component,
$filearea, $args) {
if ($component == 'question' && $filearea == 'answerfeedback') {
$answerid = reset($args); // itemid is answer id.
$answers = &$question->options->answers;
if (isset($state->responses[''])) {
$response = $state->responses[''];
} else {
$response = '';
}
return $options->feedback && !empty($response);
} else {
return parent::check_file_access($question, $state, $options, $contextid, $component,
$filearea, $args);
}
}
// Restore method not needed.
}
//// END OF CLASS ////
//////////////////////////////////////////////////////////////////////////
//// INITIATION - Without this line the question type is not in use... ///
//////////////////////////////////////////////////////////////////////////
question_register_questiontype(new question_essay_qtype());

View file

@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Essay question renderer class.
*
* @package qtype_essay
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Generates the output for essay questions.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_renderer extends qtype_renderer {
public function formulation_and_controls(question_attempt $qa,
question_display_options $options) {
$question = $qa->get_question();
// Answer field.
$inputname = $qa->get_qt_field_name('answer');
$response = $qa->get_last_qt_var('answer', '');
if (empty($options->readonly)) {
// the student needs to type in their answer so print out a text editor
$answer = print_textarea(can_use_html_editor(), 18, 65, 630, 400, $inputname, $response, 0, true);
} else {
// it is read only, so just format the students answer and output it
$formatoptions = new stdClass();
$formatoptions->para = false;
$answer = html_writer::tag('div', format_text($response, FORMAT_HTML, $formatoptions),
array('class' => 'answerreview'));
}
$result = '';
$result .= html_writer::tag('div', $question->format_questiontext($qa),
array('class' => 'qtext'));
$result .= html_writer::start_tag('div', array('class' => 'ablock clearfix'));
$result .= html_writer::tag('div', get_string('answer', 'question'),
array('class' => 'prompt'));
$result .= html_writer::tag('div', $answer, array('class' => 'answer'));
$result .= html_writer::end_tag('div');
return $result;
}
}

View file

@ -0,0 +1,51 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the essay question definition class.
*
* @package qtype_essay
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
/**
* Unit tests for the matching question definition class.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_question_test extends UnitTestCase {
public function test_get_question_summary() {
$essay = test_question_maker::make_an_essay_question();
$essay->questiontext = 'Hello <img src="http://example.com/globe.png" alt="world" />';
$this->assertEqual('Hello [world]', $essay->get_question_summary(
test_question_maker::get_a_qa($essay)));
}
public function test_summarise_response() {
$longstring = str_repeat('0123456789', 50);
$essay = test_question_maker::make_an_essay_question();
$summary = $essay->summarise_response(array('answer' => $longstring));
$this->assertTrue(strlen($summary) < 250);
$this->assertEqual(substr($longstring, 0, 100), substr($summary, 0, 100));
}
}

View file

@ -0,0 +1,71 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the essay question type class.
*
* @package qtype_essay
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/type/essay/questiontype.php');
/**
* Unit tests for the essay question type class.
*
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_test extends UnitTestCase {
var $qtype;
public function setUp() {
$this->qtype = new qtype_essay();
}
public function tearDown() {
$this->qtype = null;
}
protected function get_test_question_data() {
$q = new stdClass;
$q->id = 1;
return $q;
}
public function test_name() {
$this->assertEqual($this->qtype->name(), 'essay');
}
public function test_can_analyse_responses() {
$this->assertFalse($this->qtype->can_analyse_responses());
}
public function test_get_random_guess_score() {
$q = $this->get_test_question_data();
$this->assertEqual(0, $this->qtype->get_random_guess_score($q));
}
public function test_get_possible_responses() {
$q = $this->get_test_question_data();
$this->assertEqual(array(), $this->qtype->get_possible_responses($q));
}
}

View file

@ -2,5 +2,3 @@
$plugin->version = 2010090501;
$plugin->requires = 2010090501;

View file

@ -97,7 +97,7 @@ class qtype_truefalse_question extends question_graded_automatically {
return array($fraction, question_state::graded_state_for_fraction($fraction));
}
function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($component == 'question' && $filearea == 'answerfeedback') {
$answerid = reset($args); // itemid is answer id.
$response = $qa->get_last_qt_var('answer', '');