PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/ddmatch/edit_ddmatch_form.php

https://bitbucket.org/systime/screening2
PHP | 98 lines | 71 code | 10 blank | 17 comment | 9 complexity | ff6dcc4bfc68f144bd12cb6e480bc956 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. /**
  3. * Defines the editing form for the match question type.
  4. *
  5. * @copyright &copy; 2007 Jamie Pratt
  6. * @author Jamie Pratt me@jamiep.org
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  8. * @package questionbank
  9. * @subpackage questiontypes
  10. */
  11. /**
  12. * match editing form definition.
  13. */
  14. class question_edit_ddmatch_form extends question_edit_form {
  15. /**
  16. * Add question-type specific form fields.
  17. *
  18. * @param object $mform the form being built.
  19. */
  20. function definition_inner(&$mform) {
  21. $mform->addElement('advcheckbox', 'shuffleanswers', get_string('shuffle', 'quiz'), null, null, array(0,1));
  22. $mform->setHelpButton('shuffleanswers', array('matchshuffle', get_string('shuffle','quiz'), 'quiz'));
  23. $mform->setDefault('shuffleanswers', 1);
  24. $mform->addElement('static', 'answersinstruct', get_string('choices', 'quiz'), get_string('filloutthreequestions', 'quiz'));
  25. $mform->closeHeaderBefore('answersinstruct');
  26. $repeated = array();
  27. $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('questionno', 'quiz', '{no}'));
  28. $repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
  29. $repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
  30. if (isset($this->question->options)){
  31. $countsubquestions = count($this->question->options->subquestions);
  32. } else {
  33. $countsubquestions = 0;
  34. }
  35. $repeatsatstart = (QUESTION_NUMANS_START > ($countsubquestions + QUESTION_NUMANS_ADD))?
  36. QUESTION_NUMANS_START : ($countsubquestions + QUESTION_NUMANS_ADD);
  37. $mform->setType('subanswer', PARAM_TEXT);
  38. $mform->setType('subquestion', PARAM_TEXT);
  39. $this->repeat_elements($repeated, $repeatsatstart, array(), 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreqblanks', 'qtype_match'));
  40. }
  41. function set_data($question) {
  42. if (isset($question->options)){
  43. $subquestions = $question->options->subquestions;
  44. if (count($subquestions)) {
  45. $key = 0;
  46. foreach ($subquestions as $subquestion){
  47. $default_values['subanswers['.$key.']'] = $subquestion->answertext;
  48. $default_values['subquestions['.$key.']'] = $subquestion->questiontext;
  49. $key++;
  50. }
  51. }
  52. $default_values['shuffleanswers'] = $question->options->shuffleanswers;
  53. $question = (object)((array)$question + $default_values);
  54. }
  55. parent::set_data($question);
  56. }
  57. function qtype() {
  58. return 'match';
  59. }
  60. function validation($data){
  61. $errors = array();
  62. $answers = $data['subanswers'];
  63. $questions = $data['subquestions'];
  64. $questioncount = 0;
  65. foreach ($questions as $key => $question){
  66. $trimmedquestion = trim($question);
  67. $trimmedanswer = trim($answers[$key]);
  68. if (!empty($trimmedanswer) && !empty($trimmedquestion)){
  69. $questioncount++;
  70. }
  71. if (!empty($trimmedquestion) && empty($trimmedanswer)){
  72. $errors['subanswers['.$key.']'] = get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
  73. }
  74. }
  75. if ($questioncount==0){
  76. $errors['subquestions[0]'] = get_string('notenoughquestions', 'qtype_match', 3);
  77. $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
  78. $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
  79. } elseif ($questioncount==1){
  80. $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
  81. $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
  82. } elseif ($questioncount==2){
  83. $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
  84. }
  85. return $errors;
  86. }
  87. }
  88. ?>