PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/essay/questiontype.php

https://gitlab.com/JrLucena/moodle
PHP | 170 lines | 105 code | 21 blank | 44 comment | 3 complexity | 23b27bac57774d83ec8b04fa82868595 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Question type class for the essay question type.
  18. *
  19. * @package qtype
  20. * @subpackage essay
  21. * @copyright 2005 Mark Nielsen
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir . '/questionlib.php');
  26. /**
  27. * The essay question type.
  28. *
  29. * @copyright 2005 Mark Nielsen
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class qtype_essay extends question_type {
  33. public function is_manual_graded() {
  34. return true;
  35. }
  36. public function response_file_areas() {
  37. return array('attachments', 'answer');
  38. }
  39. public function get_question_options($question) {
  40. global $DB;
  41. $question->options = $DB->get_record('qtype_essay_options',
  42. array('questionid' => $question->id), '*', MUST_EXIST);
  43. parent::get_question_options($question);
  44. }
  45. public function save_question_options($formdata) {
  46. global $DB;
  47. $context = $formdata->context;
  48. $options = $DB->get_record('qtype_essay_options', array('questionid' => $formdata->id));
  49. if (!$options) {
  50. $options = new stdClass();
  51. $options->questionid = $formdata->id;
  52. $options->id = $DB->insert_record('qtype_essay_options', $options);
  53. }
  54. $options->responseformat = $formdata->responseformat;
  55. $options->responserequired = $formdata->responserequired;
  56. $options->responsefieldlines = $formdata->responsefieldlines;
  57. $options->attachments = $formdata->attachments;
  58. $options->attachmentsrequired = $formdata->attachmentsrequired;
  59. $options->graderinfo = $this->import_or_save_files($formdata->graderinfo,
  60. $context, 'qtype_essay', 'graderinfo', $formdata->id);
  61. $options->graderinfoformat = $formdata->graderinfo['format'];
  62. $options->responsetemplate = $formdata->responsetemplate['text'];
  63. $options->responsetemplateformat = $formdata->responsetemplate['format'];
  64. $DB->update_record('qtype_essay_options', $options);
  65. }
  66. protected function initialise_question_instance(question_definition $question, $questiondata) {
  67. parent::initialise_question_instance($question, $questiondata);
  68. $question->responseformat = $questiondata->options->responseformat;
  69. $question->responserequired = $questiondata->options->responserequired;
  70. $question->responsefieldlines = $questiondata->options->responsefieldlines;
  71. $question->attachments = $questiondata->options->attachments;
  72. $question->attachmentsrequired = $questiondata->options->attachmentsrequired;
  73. $question->graderinfo = $questiondata->options->graderinfo;
  74. $question->graderinfoformat = $questiondata->options->graderinfoformat;
  75. $question->responsetemplate = $questiondata->options->responsetemplate;
  76. $question->responsetemplateformat = $questiondata->options->responsetemplateformat;
  77. }
  78. public function delete_question($questionid, $contextid) {
  79. global $DB;
  80. $DB->delete_records('qtype_essay_options', array('questionid' => $questionid));
  81. parent::delete_question($questionid, $contextid);
  82. }
  83. /**
  84. * @return array the different response formats that the question type supports.
  85. * internal name => human-readable name.
  86. */
  87. public function response_formats() {
  88. return array(
  89. 'editor' => get_string('formateditor', 'qtype_essay'),
  90. 'editorfilepicker' => get_string('formateditorfilepicker', 'qtype_essay'),
  91. 'plain' => get_string('formatplain', 'qtype_essay'),
  92. 'monospaced' => get_string('formatmonospaced', 'qtype_essay'),
  93. 'noinline' => get_string('formatnoinline', 'qtype_essay'),
  94. );
  95. }
  96. /**
  97. * @return array the choices that should be offerd when asking if a response is required
  98. */
  99. public function response_required_options() {
  100. return array(
  101. 1 => get_string('responseisrequired', 'qtype_essay'),
  102. 0 => get_string('responsenotrequired', 'qtype_essay'),
  103. );
  104. }
  105. /**
  106. * @return array the choices that should be offered for the input box size.
  107. */
  108. public function response_sizes() {
  109. $choices = array();
  110. for ($lines = 5; $lines <= 40; $lines += 5) {
  111. $choices[$lines] = get_string('nlines', 'qtype_essay', $lines);
  112. }
  113. return $choices;
  114. }
  115. /**
  116. * @return array the choices that should be offered for the number of attachments.
  117. */
  118. public function attachment_options() {
  119. return array(
  120. 0 => get_string('no'),
  121. 1 => '1',
  122. 2 => '2',
  123. 3 => '3',
  124. -1 => get_string('unlimited'),
  125. );
  126. }
  127. /**
  128. * @return array the choices that should be offered for the number of required attachments.
  129. */
  130. public function attachments_required_options() {
  131. return array(
  132. 0 => get_string('attachmentsoptional', 'qtype_essay'),
  133. 1 => '1',
  134. 2 => '2',
  135. 3 => '3'
  136. );
  137. }
  138. public function move_files($questionid, $oldcontextid, $newcontextid) {
  139. parent::move_files($questionid, $oldcontextid, $newcontextid);
  140. $fs = get_file_storage();
  141. $fs->move_area_files_to_new_context($oldcontextid,
  142. $newcontextid, 'qtype_essay', 'graderinfo', $questionid);
  143. }
  144. protected function delete_files($questionid, $contextid) {
  145. parent::delete_files($questionid, $contextid);
  146. $fs = get_file_storage();
  147. $fs->delete_area_files($contextid, 'qtype_essay', 'graderinfo', $questionid);
  148. }
  149. }