Skip to content

Commit

Permalink
MDL-6535 new features for the essay qtype. Part I DB tables and editi…
Browse files Browse the repository at this point in the history
…ng form.

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.
  • Loading branch information
timhunt committed Mar 31, 2011
1 parent 8d13ea8 commit 894e8b4
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 1 deletion.
6 changes: 6 additions & 0 deletions question/engine/simpletest/helpers.php
Expand Up @@ -270,6 +270,12 @@ public static function make_an_essay_question() {
$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;
}

Expand Down
23 changes: 23 additions & 0 deletions question/type/essay/db/install.xml
@@ -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>
77 changes: 77 additions & 0 deletions question/type/essay/db/upgrade.php
@@ -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;
}
48 changes: 48 additions & 0 deletions question/type/essay/edit_essay_form.php
Expand Up @@ -35,6 +35,54 @@
* @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';
}
Expand Down
9 changes: 9 additions & 0 deletions question/type/essay/lang/en/qtype_essay.php
Expand Up @@ -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';
6 changes: 6 additions & 0 deletions question/type/essay/question.php
Expand Up @@ -35,6 +35,12 @@
* @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);
Expand Down
71 changes: 71 additions & 0 deletions question/type/essay/questiontype.php
Expand Up @@ -39,6 +39,77 @@ public function is_manual_graded() {
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);
Expand Down
2 changes: 1 addition & 1 deletion question/type/essay/version.php
Expand Up @@ -26,5 +26,5 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2010090501;
$plugin->version = 2011031001;
$plugin->requires = 2010090501;

0 comments on commit 894e8b4

Please sign in to comment.