Skip to content

Commit

Permalink
MDL-20636 Start supporting different response formats in essay. #216
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Mar 31, 2011
1 parent 894e8b4 commit b36d2d0
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 12 deletions.
4 changes: 2 additions & 2 deletions question/type/essay/lang/en/qtype_essay.php
Expand Up @@ -33,8 +33,8 @@
$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['formatmonospaced'] = 'Plain text, monospaced font';
$string['formatplain'] = 'Plain text';
$string['graderinfo'] = 'Information for graders';
$string['nlines'] = '{$a} lines';
$string['responsefieldlines'] = 'Input box size';
Expand Down
8 changes: 8 additions & 0 deletions question/type/essay/question.php
Expand Up @@ -46,6 +46,14 @@ public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
return new qbehaviour_manualgraded($qa, $preferredbehaviour);
}

/**
* @param moodle_page the page we are outputting to.
* @return qtype_essay_format_renderer_base the response-format-specific renderer.
*/
public function get_format_renderer(moodle_page $page) {
return $page->get_renderer('qtype_essay', 'format_' . $this->responseformat);
}

public function get_expected_data() {
return array('answer' => PARAM_CLEANHTML);
}
Expand Down
1 change: 1 addition & 0 deletions question/type/essay/questiontype.php
Expand Up @@ -52,6 +52,7 @@ public function save_question_options($formdata) {
$options = $DB->get_record('qtype_essay_options', array('questionid' => $formdata->id));
if (!$options) {
$options = new stdClass();
$options->questionid = $formdata->id;
$options->id = $DB->insert_record('qtype_essay_options', $options);
}

Expand Down
160 changes: 150 additions & 10 deletions question/type/essay/renderer.php
Expand Up @@ -39,32 +39,172 @@ public function formulation_and_controls(question_attempt $qa,
question_display_options $options) {

$question = $qa->get_question();
$responseoutput = $question->get_format_renderer($this->page);

// 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);
$answer =$responseoutput->response_area_input($inputname,
$response, $question->responsefieldlines);

} 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'));
$answer =$responseoutput->response_area_read_only($inputname,
$response, $question->responsefieldlines);
}

$files = '';
if (empty($options->readonly)) {
if ($question->attachments == 1) {
$files = $this->filepicker_input();
} else if ($question->attachments != 0) {
$files = $this->filemanager_input();
}

} else if ($question->attachments != 0) {
$files = $this->files_read_only();
}

$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::start_tag('div', array('class' => 'ablock'));
$result .= html_writer::tag('div', $answer, array('class' => 'answer'));
$result .= html_writer::end_tag('div');

return $result;
}

/**
* Displays any attached files when the question is in read-only mode.
*/
public function files_read_only() {
return '';
}

/**
* Displays the input control for when the student should upload a single file.
*/
public function filepicker_input() {
return '';
}

/**
* Displays the input control for when the student should upload a number of files.
*/
public function filemanager_input() {
return '';
}
}


/**
* A base class to abstract out the differences between different type of
* response format.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
/**
* Render the students respone when the question is in read-only mode.
* @param string $inputname the field name to use for this input.
* @param string $response the student's current response.
* @param int $lines approximate size of input box to display.
*/
public abstract function response_area_read_only($inputname, $response, $lines);

/**
* Render the students respone when the question is in read-only mode.
* @param string $inputname the field name to use for this input.
* @param string $response the student's current response.
* @param int $lines approximate size of input box to display.
*/
public abstract function response_area_input($inputname, $response, $lines);
}


/**
* An essay format renderer for essays where the student should use the HTML
* editor without the file picker.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_format_editor_renderer extends plugin_renderer_base {
public function response_area_read_only($inputname, $response, $lines) {
$formatoptions = new stdClass();
$formatoptions->para = false;
return html_writer::tag('div', format_text($response, FORMAT_HTML, $formatoptions),
array('class' => 'qtype_essay_editor qtype_essay_response'));
}

public function response_area_input($inputname, $response, $lines) {
return print_textarea(can_use_html_editor(), 18, 80, 630, 400, $inputname, $response, 0, true);
}
}


/**
* An essay format renderer for essays where the student should use the HTML
* editor with the file picker.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_format_editorfilepicker_renderer extends plugin_renderer_base {
public function response_area_read_only($inputname, $response, $lines) {
return 'Not yet implemented.';
}

public function response_area_input($inputname, $response, $lines) {
return 'Not yet implemented.';
}
}


/**
* An essay format renderer for essays where the student should use a plain
* input box, but with a normal, proportional font.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_format_plain_renderer extends plugin_renderer_base {
/**
* @return string the HTML for the textarea.
*/
protected function textarea($response, $lines, $attributes) {
$attributes['class'] = $this->class_name() . ' qtype_essay_response';
$attributes['rows'] = $lines;
return html_writer::tag('textarea', s($response), $attributes);
}

protected function class_name() {
return 'qtype_essay_plain';
}

public function response_area_read_only($inputname, $response, $lines) {
return $this->textarea($response, $lines, array('readonly' => 'readonly'));
}

public function response_area_input($inputname, $response, $lines) {
return $this->textarea($response, $lines, array('name' => $inputname));
}
}


/**
* An essay format renderer for essays where the student should use a plain
* input box with a monospaced font. You might use this, for example, for a
* question where the students should type computer code.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
protected function class_name() {
return 'qtype_essay_monospaced';
}
}
14 changes: 14 additions & 0 deletions question/type/essay/styles.css
@@ -0,0 +1,14 @@
.que.essay textarea.qtype_essay_response {
width: 100%;
}
.que.essay textarea.qtype_essay_response.qtype_essay_plain {
white-space: pre-wrap;
font: inherit;
}
.que.essay textarea.qtype_essay_response.qtype_essay_monospaced {
white-space: pre;
font-family: Andale Mono, Monaco, Courier New, DejaVu Sans Mono, monospace;
}
.que.essay .qtype_essay_response.qtype_essay_editor {
background-color: white;
}
1 change: 1 addition & 0 deletions question/type/questionbase.php
Expand Up @@ -223,6 +223,7 @@ public function get_num_parts_right(array $response) {
}

/**
* @param moodle_page the page we are outputting to.
* @return qtype_renderer the renderer to use for outputting this question.
*/
public function get_renderer(moodle_page $page) {
Expand Down

0 comments on commit b36d2d0

Please sign in to comment.