Skip to content

Commit

Permalink
MDL-20636 Conversion of the essay question type.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Jan 13, 2011
1 parent 7a71974 commit 6d03299
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 195 deletions.
41 changes: 1 addition & 40 deletions question/type/essay/edit_essay_form.php
Expand Up @@ -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';
}
}
2 changes: 1 addition & 1 deletion question/type/essay/lang/en/qtype_essay.php
Expand Up @@ -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.';
66 changes: 66 additions & 0 deletions question/type/essay/question.php
@@ -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');
}
}
165 changes: 14 additions & 151 deletions question/type/essay/questiontype.php
Expand Up @@ -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() {
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;

/**
* 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);
Expand All @@ -156,31 +46,4 @@ protected function delete_files($questionid, $contextid) {
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());
67 changes: 67 additions & 0 deletions question/type/essay/renderer.php
@@ -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;
}
}

0 comments on commit 6d03299

Please sign in to comment.