PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/edit_question_form.php

https://bitbucket.org/systime/screening2
PHP | 323 lines | 204 code | 46 blank | 73 comment | 37 complexity | 3d5b034c4362992e309cde3be12fe4dc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0, BSD-3-Clause, LGPL-2.0
  1. <?php // $Id: edit_question_form.php,v 1.15.2.4 2010/09/16 23:04:58 pichetp Exp $
  2. /**
  3. * A base class for question editing forms.
  4. *
  5. * @copyright &copy; 2006 The Open University
  6. * @author T.J.Hunt@open.ac.uk
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  8. * @package questionbank
  9. * @subpackage questiontypes
  10. *//** */
  11. /**
  12. * Form definition base class. This defines the common fields that
  13. * all question types need. Question types should define their own
  14. * class that inherits from this one, and implements the definition_inner()
  15. * method.
  16. *
  17. * @package questionbank
  18. * @subpackage questiontypes
  19. */
  20. class question_edit_form extends moodleform {
  21. /**
  22. * Question object with options and answers already loaded by get_question_options
  23. * Be careful how you use this it is needed sometimes to set up the structure of the
  24. * form in definition_inner but data is always loaded into the form with set_data.
  25. *
  26. * @var object
  27. */
  28. var $question;
  29. var $contexts;
  30. var $category;
  31. var $categorycontext;
  32. var $coursefilesid;
  33. function question_edit_form($submiturl, $question, $category, $contexts, $formeditable = true){
  34. $this->question = $question;
  35. $this->contexts = $contexts;
  36. $this->category = $category;
  37. $this->categorycontext = get_context_instance_by_id($category->contextid);
  38. //course id or site id depending on question cat context
  39. $this->coursefilesid = get_filesdir_from_context(get_context_instance_by_id($category->contextid));
  40. parent::moodleform($submiturl, null, 'post', '', null, $formeditable);
  41. }
  42. /**
  43. * Build the form definition.
  44. *
  45. * This adds all the form fields that the default question type supports.
  46. * If your question type does not support all these fields, then you can
  47. * override this method and remove the ones you don't want with $mform->removeElement().
  48. */
  49. function definition() {
  50. global $COURSE, $CFG;
  51. $qtype = $this->qtype();
  52. $langfile = "qtype_$qtype";
  53. $mform =& $this->_form;
  54. // Standard fields at the start of the form.
  55. $mform->addElement('header', 'generalheader', get_string("general", 'form'));
  56. if (!isset($this->question->id)){
  57. //adding question
  58. $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'),
  59. array('contexts' => $this->contexts->having_cap('moodle/question:add')));
  60. } elseif (!($this->question->formoptions->canmove || $this->question->formoptions->cansaveasnew)){
  61. //editing question with no permission to move from category.
  62. $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'),
  63. array('contexts' => array($this->categorycontext)));
  64. } elseif ($this->question->formoptions->movecontext){
  65. //moving question to another context.
  66. $mform->addElement('questioncategory', 'categorymoveto', get_string('category', 'quiz'),
  67. array('contexts' => $this->contexts->having_cap('moodle/question:add')));
  68. } else {
  69. //editing question with permission to move from category or save as new q
  70. $currentgrp = array();
  71. $currentgrp[0] =& $mform->createElement('questioncategory', 'category', get_string('categorycurrent', 'question'),
  72. array('contexts' => array($this->categorycontext)));
  73. if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew){
  74. //not move only form
  75. $currentgrp[1] =& $mform->createElement('checkbox', 'usecurrentcat', '', get_string('categorycurrentuse', 'question'));
  76. $mform->setDefault('usecurrentcat', 1);
  77. }
  78. $currentgrp[0]->freeze();
  79. $currentgrp[0]->setPersistantFreeze(false);
  80. $mform->addGroup($currentgrp, 'currentgrp', get_string('categorycurrent', 'question'), null, false);
  81. $mform->addElement('questioncategory', 'categorymoveto', get_string('categorymoveto', 'question'),
  82. array('contexts' => array($this->categorycontext)));
  83. if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew){
  84. //not move only form
  85. $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
  86. }
  87. }
  88. $mform->addElement('text', 'name', get_string('questionname', 'quiz'), array('size' => 50));
  89. $mform->setType('name', PARAM_TEXT);
  90. $mform->addRule('name', null, 'required', null, 'client');
  91. $mform->addElement('htmleditor', 'questiontext', get_string('questiontext', 'quiz'),
  92. array('rows' => 15, 'course' => $this->coursefilesid));
  93. $mform->setType('questiontext', PARAM_RAW);
  94. $mform->setHelpButton('questiontext', array(array('questiontext', get_string('questiontext', 'quiz'), 'quiz'), 'richtext'), false, 'editorhelpbutton');
  95. $mform->addElement('format', 'questiontextformat', get_string('format'));
  96. make_upload_directory($this->coursefilesid); // Just in case
  97. $coursefiles = get_directory_list("$CFG->dataroot/$this->coursefilesid", $CFG->moddata);
  98. foreach ($coursefiles as $filename) {
  99. if (mimeinfo("icon", $filename) == "image.gif") {
  100. $images["$filename"] = $filename;
  101. }
  102. }
  103. if (empty($images)) {
  104. $mform->addElement('static', 'image', get_string('imagedisplay', 'quiz'), get_string('noimagesyet'));
  105. } else {
  106. $mform->addElement('select', 'image', get_string('imagedisplay', 'quiz'), array_merge(array(''=>get_string('none')), $images));
  107. }
  108. $mform->addElement('text', 'defaultgrade', get_string('defaultgrade', 'quiz'),
  109. array('size' => 3));
  110. $mform->setType('defaultgrade', PARAM_INT);
  111. $mform->setDefault('defaultgrade', 1);
  112. $mform->addRule('defaultgrade', null, 'required', null, 'client');
  113. $mform->addElement('text', 'penalty', get_string('penaltyfactor', 'quiz'),
  114. array('size' => 3));
  115. $mform->setType('penalty', PARAM_NUMBER);
  116. $mform->addRule('penalty', null, 'required', null, 'client');
  117. $mform->setHelpButton('penalty', array('penalty', get_string('penalty', 'quiz'), 'quiz'));
  118. $mform->setDefault('penalty', 0.1);
  119. $mform->addElement('htmleditor', 'generalfeedback', get_string('generalfeedback', 'quiz'),
  120. array('rows' => 10, 'course' => $this->coursefilesid));
  121. $mform->setType('generalfeedback', PARAM_RAW);
  122. $mform->setHelpButton('generalfeedback', array('generalfeedback', get_string('generalfeedback', 'quiz'), 'quiz'));
  123. // Any questiontype specific fields.
  124. $this->definition_inner($mform);
  125. if (!empty($this->question->id)){
  126. $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
  127. $a = new object();
  128. if (!empty($this->question->createdby)){
  129. $a->time = userdate($this->question->timecreated);
  130. $a->user = fullname(get_record('user', 'id', $this->question->createdby));
  131. } else {
  132. $a->time = get_string('unknown', 'question');
  133. $a->user = get_string('unknown', 'question');
  134. }
  135. $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
  136. if (!empty($this->question->modifiedby)){
  137. $a = new object();
  138. $a->time = userdate($this->question->timemodified);
  139. $a->user = fullname(get_record('user', 'id', $this->question->modifiedby));
  140. $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
  141. }
  142. }
  143. // Standard fields at the end of the form.
  144. $mform->addElement('hidden', 'id');
  145. $mform->setType('id', PARAM_INT);
  146. $mform->addElement('hidden', 'qtype');
  147. $mform->setType('qtype', PARAM_ALPHA);
  148. $mform->addElement('hidden', 'inpopup');
  149. $mform->setType('inpopup', PARAM_INT);
  150. $mform->addElement('hidden', 'versioning');
  151. $mform->setType('versioning', PARAM_BOOL);
  152. $mform->addElement('hidden', 'movecontext');
  153. $mform->setType('movecontext', PARAM_BOOL);
  154. $mform->addElement('hidden', 'cmid');
  155. $mform->setType('cmid', PARAM_INT);
  156. $mform->setDefault('cmid', 0);
  157. $mform->addElement('hidden', 'courseid');
  158. $mform->setType('courseid', PARAM_INT);
  159. $mform->setDefault('courseid', 0);
  160. $mform->addElement('hidden', 'returnurl');
  161. $mform->setType('returnurl', PARAM_LOCALURL);
  162. $mform->setDefault('returnurl', 0);
  163. $buttonarray = array();
  164. if (!empty($this->question->id)){
  165. //editing / moving question
  166. if ($this->question->formoptions->movecontext){
  167. $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
  168. } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext){
  169. $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
  170. }
  171. if ($this->question->formoptions->cansaveasnew){
  172. $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
  173. }
  174. $buttonarray[] = &$mform->createElement('cancel');
  175. } else {
  176. // adding new question
  177. $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
  178. $buttonarray[] = &$mform->createElement('cancel');
  179. }
  180. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  181. $mform->closeHeaderBefore('buttonar');
  182. if ($this->question->formoptions->movecontext){
  183. $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
  184. } elseif ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))){
  185. $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
  186. }
  187. }
  188. function validation($fromform, $files) {
  189. $errors= parent::validation($fromform, $files);
  190. if (empty($fromform->makecopy) && isset($this->question->id)
  191. && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
  192. && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove){
  193. $errors['currentgrp'] = get_string('nopermissionmove', 'question');
  194. }
  195. return $errors;
  196. }
  197. /**
  198. * Add any question-type specific form fields.
  199. *
  200. * @param object $mform the form being built.
  201. */
  202. function definition_inner(&$mform) {
  203. // By default, do nothing.
  204. }
  205. /**
  206. * Get the list of form elements to repeat, one for each answer.
  207. * @param object $mform the form being built.
  208. * @param $label the label to use for each option.
  209. * @param $gradeoptions the possible grades for each answer.
  210. * @param $repeatedoptions reference to array of repeated options to fill
  211. * @param $answersoption reference to return the name of $question->options field holding an array of answers
  212. * @return array of form fields.
  213. */
  214. function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
  215. $repeated = array();
  216. $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
  217. $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
  218. $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
  219. $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
  220. array('course' => $this->coursefilesid));
  221. $repeatedoptions['answer']['type'] = PARAM_RAW;
  222. $repeatedoptions['fraction']['default'] = 0;
  223. $answersoption = 'answers';
  224. return $repeated;
  225. }
  226. /**
  227. * Add a set of form fields, obtained from get_per_answer_fields, to the form,
  228. * one for each existing answer, with some blanks for some new ones.
  229. * @param object $mform the form being built.
  230. * @param $label the label to use for each option.
  231. * @param $gradeoptions the possible grades for each answer.
  232. * @param $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
  233. * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
  234. */
  235. function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
  236. $answersoption = '';
  237. $repeatedoptions = array();
  238. $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
  239. if (isset($this->question->options)){
  240. $countanswers = count($this->question->options->$answersoption);
  241. } else {
  242. $countanswers = 0;
  243. }
  244. if ($this->question->formoptions->repeatelements){
  245. $repeatsatstart = max($minoptions, $countanswers + $addoptions);
  246. } else {
  247. $repeatsatstart = $countanswers;
  248. }
  249. $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
  250. }
  251. function set_data($question) {
  252. global $QTYPES;
  253. $QTYPES[$question->qtype]->set_default_options($question);
  254. if (empty($question->image)){
  255. unset($question->image);
  256. }
  257. // Set any options.
  258. $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
  259. if (is_array($extra_question_fields) && !empty($question->options)) {
  260. array_shift($extra_question_fields);
  261. foreach ($extra_question_fields as $field) {
  262. if (isset($question->options->$field)) {
  263. $question->$field = $question->options->$field;
  264. }
  265. }
  266. }
  267. parent::set_data($question);
  268. }
  269. /**
  270. * Override this in the subclass to question type name.
  271. * @return the question type name, should be the same as the name() method in the question type class.
  272. */
  273. function qtype() {
  274. return '';
  275. }
  276. }
  277. ?>