From 6d03299dec2d59eab162fa945ad839b8f984ba61 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 12 Jan 2011 14:51:17 +0000 Subject: [PATCH] MDL-20636 Conversion of the essay question type. --- question/type/essay/edit_essay_form.php | 41 +---- question/type/essay/lang/en/qtype_essay.php | 2 +- question/type/essay/question.php | 66 +++++++ question/type/essay/questiontype.php | 165 ++---------------- question/type/essay/renderer.php | 67 +++++++ .../type/essay/simpletest/testquestion.php | 51 ++++++ .../essay/simpletest/testquestiontype.php | 71 ++++++++ question/type/essay/version.php | 2 - question/type/truefalse/question.php | 2 +- 9 files changed, 272 insertions(+), 195 deletions(-) create mode 100644 question/type/essay/question.php create mode 100644 question/type/essay/renderer.php create mode 100644 question/type/essay/simpletest/testquestion.php create mode 100644 question/type/essay/simpletest/testquestiontype.php diff --git a/question/type/essay/edit_essay_form.php b/question/type/essay/edit_essay_form.php index 702d8c74829..79277e3205d 100644 --- a/question/type/essay/edit_essay_form.php +++ b/question/type/essay/edit_essay_form.php @@ -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'; } } diff --git a/question/type/essay/lang/en/qtype_essay.php b/question/type/essay/lang/en/qtype_essay.php index bfcf00057ef..908b6dda3e1 100644 --- a/question/type/essay/lang/en/qtype_essay.php +++ b/question/type/essay/lang/en/qtype_essay.php @@ -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.'; diff --git a/question/type/essay/question.php b/question/type/essay/question.php new file mode 100644 index 00000000000..ac7209722ba --- /dev/null +++ b/question/type/essay/question.php @@ -0,0 +1,66 @@ +. + + +/** + * 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'); + } +} diff --git a/question/type/essay/questiontype.php b/question/type/essay/questiontype.php index 102b26f1bc2..9ccfe90917c 100644 --- a/question/type/essay/questiontype.php +++ b/question/type/essay/questiontype.php @@ -15,138 +15,28 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -////////////////// -/// 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() { - 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 = '
' . $answer . '
'; - } - - 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; +/** + * 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; } - /** - * 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()); diff --git a/question/type/essay/renderer.php b/question/type/essay/renderer.php new file mode 100644 index 00000000000..58702d9f2dc --- /dev/null +++ b/question/type/essay/renderer.php @@ -0,0 +1,67 @@ +. + + +/** + * 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; + } +} diff --git a/question/type/essay/simpletest/testquestion.php b/question/type/essay/simpletest/testquestion.php new file mode 100644 index 00000000000..7c646be2f9b --- /dev/null +++ b/question/type/essay/simpletest/testquestion.php @@ -0,0 +1,51 @@ +. + + +/** + * 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 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)); + } +} diff --git a/question/type/essay/simpletest/testquestiontype.php b/question/type/essay/simpletest/testquestiontype.php new file mode 100644 index 00000000000..1c5116c523b --- /dev/null +++ b/question/type/essay/simpletest/testquestiontype.php @@ -0,0 +1,71 @@ +. + + +/** + * 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)); + + } +} diff --git a/question/type/essay/version.php b/question/type/essay/version.php index 9061bb2e5eb..4d9fe587b34 100644 --- a/question/type/essay/version.php +++ b/question/type/essay/version.php @@ -2,5 +2,3 @@ $plugin->version = 2010090501; $plugin->requires = 2010090501; - - diff --git a/question/type/truefalse/question.php b/question/type/truefalse/question.php index fae901267f5..8a6a982e5b1 100644 --- a/question/type/truefalse/question.php +++ b/question/type/truefalse/question.php @@ -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', ''); -- 2.43.0