diff --git a/question/engine/simpletest/helpers.php b/question/engine/simpletest/helpers.php index dd1bc955211..4c29cca0fed 100644 --- a/question/engine/simpletest/helpers.php +++ b/question/engine/simpletest/helpers.php @@ -270,6 +270,12 @@ class test_question_maker { $essay->penalty = 0; $essay->qtype = question_bank::get_qtype('essay'); + $essay->responseformat = 'editor'; + $essay->responsefieldlines = 15; + $essay->attachments = 0; + $essay->graderinfo = ''; + $essay->graderinfoformat = FORMAT_MOODLE; + return $essay; } diff --git a/question/type/essay/db/install.xml b/question/type/essay/db/install.xml new file mode 100644 index 00000000000..a7c70f04273 --- /dev/null +++ b/question/type/essay/db/install.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/question/type/essay/db/upgrade.php b/question/type/essay/db/upgrade.php new file mode 100644 index 00000000000..96830b4e984 --- /dev/null +++ b/question/type/essay/db/upgrade.php @@ -0,0 +1,77 @@ +. + +/** + * Essay question type upgrade code. + * + * @package qtype + * @subpackage essay + * @copyright 2011 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Upgrade code for the essay question type. + * @param int $oldversion the version we are upgrading from. + */ +function xmldb_qtype_essay_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2011031000) { + // Define table qtype_essay_options to be created + $table = new xmldb_table('qtype_essay_options'); + + // Adding fields to table qtype_essay_options + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_field('responseformat', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, 'editor'); + $table->add_field('responsefieldlines', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '15'); + $table->add_field('attachments', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('graderinfo', XMLDB_TYPE_TEXT, 'small', null, null, null, null); + $table->add_field('graderinfoformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + + // Adding keys to table qtype_essay_options + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('questionid', XMLDB_KEY_FOREIGN_UNIQUE, array('questionid'), 'question', array('id')); + + // Conditionally launch create table for qtype_essay_options + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // essay savepoint reached + upgrade_plugin_savepoint(true, 2011031000, 'qtype', 'essay'); + } + + if ($oldversion < 2011031000) { + // Insert a row into the qtype_essay_options table for each existing essay question. + $DB->execute(" + INSERT INTO {qtype_essay_options} (questionid, responseformat, + responsefieldlines, attachments, graderinfo, graderinfoformat) + SELECT id, 'editor', 15, 0, '', " . FORMAT_MOODLE . " + FROM {question} + WHERE qtype = 'essay'"); + } + + return true; +} diff --git a/question/type/essay/edit_essay_form.php b/question/type/essay/edit_essay_form.php index 454b4a3ea98..0d2733a4009 100644 --- a/question/type/essay/edit_essay_form.php +++ b/question/type/essay/edit_essay_form.php @@ -35,6 +35,54 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class qtype_essay_edit_form extends question_edit_form { + + protected function definition_inner($mform) { + $qtype = question_bank::get_qtype('essay'); + + $mform->addElement('select', 'responseformat', + get_string('responseformat', 'qtype_essay'), $qtype->respones_formats()); + $mform->setDefault('responseformat', 'editor'); + + $mform->addElement('select', 'responsefieldlines', + get_string('responsefieldlines', 'qtype_essay'), $qtype->respones_sizes()); + $mform->setDefault('responsefieldlines', 15); + + $mform->addElement('select', 'attachments', + get_string('allowattachments', 'qtype_essay'), $qtype->attachment_options()); + $mform->setDefault('attachments', 0); + + $mform->addElement('editor', 'graderinfo', get_string('graderinfo', 'qtype_essay'), + array('rows' => 10), $this->editoroptions); + } + + function data_preprocessing($question) { + $question = parent::data_preprocessing($question); + + if (empty($question->options)) { + return $question; + } + + $question->responseformat = $question->options->responseformat; + $question->responsefieldlines = $question->options->responsefieldlines; + $question->attachments = $question->options->attachments; + + $draftid = file_get_submitted_draft_itemid('graderinfo'); + $question->graderinfo = array(); + $question->graderinfo['text'] = file_prepare_draft_area( + $draftid, // draftid + $this->context->id, // context + 'qtype_essay', // component + 'graderinfo', // filarea + !empty($question->id) ? (int) $question->id : null, // itemid + $this->fileoptions, // options + $question->options->graderinfo // text + ); + $question->graderinfo['format'] = $question->options->graderinfoformat; + $question->graderinfo['itemid'] = $draftid; + + return $question; + } + 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 75e90bcfac0..5b3ec84dc12 100644 --- a/question/type/essay/lang/en/qtype_essay.php +++ b/question/type/essay/lang/en/qtype_essay.php @@ -26,7 +26,16 @@ $string['addingessay'] = 'Adding an Essay question'; $string['addingessay_link'] = 'question/type/essay'; +$string['allowattachments'] = 'Allow attachments'; $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.'; +$string['formateditor'] = 'HTML editor'; +$string['formateditorfilepicker'] = 'HTML editor with file picker'; +$string['formatmonospaced'] = 'Plain text'; +$string['formatplain'] = 'Plain text, monospaced font'; +$string['graderinfo'] = 'Information for graders'; +$string['nlines'] = '{$a} lines'; +$string['responsefieldlines'] = 'Input box size'; +$string['responseformat'] = 'Respones format'; diff --git a/question/type/essay/question.php b/question/type/essay/question.php index a04c1b52aff..375a9b79d72 100644 --- a/question/type/essay/question.php +++ b/question/type/essay/question.php @@ -35,6 +35,12 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class qtype_essay_question extends question_with_responses { + public $responseformat; + public $responsefieldlines; + public $attachments; + public $graderinfo; + public $graderinfoformat; + public function make_behaviour(question_attempt $qa, $preferredbehaviour) { question_engine::load_behaviour_class('manualgraded'); return new qbehaviour_manualgraded($qa, $preferredbehaviour); diff --git a/question/type/essay/questiontype.php b/question/type/essay/questiontype.php index d91e6edef91..4ec4fba2210 100644 --- a/question/type/essay/questiontype.php +++ b/question/type/essay/questiontype.php @@ -39,6 +39,77 @@ class qtype_essay extends question_type { return true; } + public function get_question_options($question) { + global $DB; + $question->options = $DB->get_record('qtype_essay_options', array('questionid' => $question->id), '*', MUST_EXIST); + parent::get_question_options($question); + } + + public function save_question_options($formdata) { + global $DB; + $context = $formdata->context; + + $options = $DB->get_record('qtype_essay_options', array('questionid' => $formdata->id)); + if (!$options) { + $options = new stdClass(); + $options->id = $DB->insert_record('qtype_essay_options', $options); + } + + $options->responseformat = $formdata->responseformat; + $options->responsefieldlines = $formdata->responsefieldlines; + $options->attachments = $formdata->attachments; + $options->graderinfo = $this->import_or_save_files($formdata->graderinfo, + $context, 'qtype_essay', 'graderinfo', $formdata->id); + $options->graderinfoformat = $formdata->graderinfo['format']; + $DB->update_record('qtype_essay_options', $options); + } + + protected function initialise_question_instance(question_definition $question, $questiondata) { + parent::initialise_question_instance($question, $questiondata); + $question->responseformat = $questiondata->options->responseformat; + $question->responsefieldlines = $questiondata->options->responsefieldlines; + $question->attachments = $questiondata->options->attachments; + $question->graderinfo = $questiondata->options->graderinfo; + $question->graderinfoformat = $questiondata->options->graderinfoformat; + } + + /** + * @return array the different response formats that the question type supports. + * internal name => human-readable name. + */ + public function respones_formats() { + return array( + 'editor' => get_string('formateditor', 'qtype_essay'), + 'editorfilepicker' => get_string('formateditorfilepicker', 'qtype_essay'), + 'plain' => get_string('formatplain', 'qtype_essay'), + 'monospaced' => get_string('formatmonospaced', 'qtype_essay'), + ); + } + + /** + * @return array the choices that should be offered for the input box size. + */ + public function respones_sizes() { + $choices = array(); + for ($lines = 5; $lines <= 40; $lines += 5) { + $choices[$lines] = get_string('nlines', 'qtype_essay', $lines); + } + return $choices; + } + + /** + * @return array the choices that should be offered for the number of attachments. + */ + public function attachment_options() { + return array( + 0 => get_string('no'), + 1 => '1', + 2 => '2', + 3 => '3', + -1 => get_string('unlimited'), + ); + } + public function move_files($questionid, $oldcontextid, $newcontextid) { parent::move_files($questionid, $oldcontextid, $newcontextid); $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid); diff --git a/question/type/essay/version.php b/question/type/essay/version.php index e67559fc172..732cc988c4a 100644 --- a/question/type/essay/version.php +++ b/question/type/essay/version.php @@ -26,5 +26,5 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2010090501; +$plugin->version = 2011031001; $plugin->requires = 2010090501;