MDL-6535 new features for the essay qtype. Part I DB tables and editing form.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Thu, 10 Mar 2011 19:07:58 +0000 (19:07 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Thu, 31 Mar 2011 11:44:38 +0000 (12:44 +0100)
Also MDL-8229, MDL-10861, MDL-11080

The features are attachements with repsonses, control over whether the respons can contain images, letting the question author set the size for the input box, and allowing the author to input some text that is shown to the grader.

question/engine/simpletest/helpers.php
question/type/essay/db/install.xml [new file with mode: 0644]
question/type/essay/db/upgrade.php [new file with mode: 0644]
question/type/essay/edit_essay_form.php
question/type/essay/lang/en/qtype_essay.php
question/type/essay/question.php
question/type/essay/questiontype.php
question/type/essay/version.php

index dd1bc95..4c29cca 100644 (file)
@@ -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 (file)
index 0000000..a7c70f0
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<XMLDB PATH="question/type/essay/db" VERSION="20110310" COMMENT="XMLDB file for Moodle question/type/essay"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
+>
+  <TABLES>
+    <TABLE NAME="qtype_essay_options" COMMENT="Extra options for essay questions.">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="questionid"/>
+        <FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" COMMENT="Foreign key linking to the question table." PREVIOUS="id" NEXT="responseformat"/>
+        <FIELD NAME="responseformat" TYPE="char" LENGTH="16" NOTNULL="true" DEFAULT="editor" SEQUENCE="false" COMMENT="The type of input area students should be given for their response." PREVIOUS="questionid" NEXT="responsefieldlines"/>
+        <FIELD NAME="responsefieldlines" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="15" SEQUENCE="false" COMMENT="Approximate height, in lines, of the input box the students should be given for their response." PREVIOUS="responseformat" NEXT="attachments"/>
+        <FIELD NAME="attachments" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" COMMENT="Whether, and how many, attachments a student is allowed to include with their response. -1 means unlimited." PREVIOUS="responsefieldlines" NEXT="graderinfo"/>
+        <FIELD NAME="graderinfo" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" COMMENT="Information shown to people with permission to manually grade the question, when they are grading." PREVIOUS="attachments" NEXT="graderinfoformat"/>
+        <FIELD NAME="graderinfoformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="The text format for graderinfo." PREVIOUS="graderinfo"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
+        <KEY NAME="questionid" TYPE="foreign-unique" FIELDS="questionid" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
+      </KEYS>
+    </TABLE>
+  </TABLES>
+</XMLDB>
\ 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 (file)
index 0000000..96830b4
--- /dev/null
@@ -0,0 +1,77 @@
+<?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 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;
+}
index 454b4a3..0d2733a 100644 (file)
@@ -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';
     }
index 75e90bc..5b3ec84 100644 (file)
 
 $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';
index a04c1b5..375a9b7 100644 (file)
@@ -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);
index d91e6ed..4ec4fba 100644 (file)
@@ -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);
index e67559f..732cc98 100644 (file)
@@ -26,5 +26,5 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version  = 2010090501;
+$plugin->version  = 2011031001;
 $plugin->requires = 2010090501;