Merge branch 'MDL-67513-m310' of https://github.com/NeillM/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / randomsamatch / edit_randomsamatch_form.php
CommitLineData
aeb15530 1<?php
d3603157
TH
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
08224df1 17/**
18 * Defines the editing form for the randomsamatch question type.
19 *
93a54393 20 * @package qtype_randomsamatch
b04a4319
TH
21 * @copyright 2007 Jamie Pratt me@jamiep.org
22 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
08224df1 23 */
24
d3603157 25
a17b297d
TH
26defined('MOODLE_INTERNAL') || die();
27
28
08224df1 29/**
30 * randomsamatch editing form definition.
b04a4319
TH
31 *
32 * @copyright 2007 Jamie Pratt me@jamiep.org
33 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
08224df1 34 */
59a3fcd3
TH
35class qtype_randomsamatch_edit_form extends question_edit_form {
36 protected function definition_inner($mform) {
08224df1 37 $questionstoselect = array();
59a3fcd3 38 for ($i = 2; $i <= qtype_randomsamatch::MAX_SUBQUESTIONS; $i++) {
08224df1 39 $questionstoselect[$i] = $i;
40 }
41
59a3fcd3 42 $mform->addElement('select', 'choose',
93a54393 43 get_string('randomsamatchnumber', 'qtype_randomsamatch'), $questionstoselect);
08224df1 44 $mform->setType('feedback', PARAM_RAW);
45
93a54393
JMV
46 $mform->addElement('advcheckbox', 'subcats',
47 get_string('subcats', 'qtype_randomsamatch'), null, null, array(0, 1));
48 $mform->addHelpButton('subcats', 'subcats', 'qtype_randomsamatch');
49 $mform->setDefault('subcats', 1);
50
08224df1 51 $mform->addElement('hidden', 'fraction', 0);
d18e0fe6 52 $mform->setType('fraction', PARAM_RAW);
93a54393
JMV
53
54 $this->add_combined_feedback_fields(true);
55 $this->add_interactive_settings(true, true);
08224df1 56 }
57
c7df5006 58 protected function data_preprocessing($question) {
93a54393
JMV
59 $question = parent::data_preprocessing($question);
60 $question = $this->data_preprocessing_combined_feedback($question, true);
61 $question = $this->data_preprocessing_hints($question, true, true);
62
63 if (!empty($question->options)) {
64 $question->choose = $question->options->choose;
65 $question->subcats = $question->options->subcats;
66 }
67
08224df1 68 if (empty($question->name)) {
93a54393 69 $question->name = get_string('randomsamatch', 'qtype_randomsamatch');
08224df1 70 }
71
72 if (empty($question->questiontext)) {
93a54393 73 $question->questiontext = get_string('randomsamatchintro', 'qtype_randomsamatch');
08224df1 74 }
fe6ce234 75 return $question;
08224df1 76 }
77
c7df5006 78 public function qtype() {
08224df1 79 return 'randomsamatch';
80 }
81
59a3fcd3
TH
82 public function validation($data, $files) {
83 global $DB;
fe93ba83 84 $errors = parent::validation($data, $files);
93a54393 85
5e581dd4 86 if (isset($data->categorymoveto)) {
87 list($category) = explode(',', $data['categorymoveto']);
88 } else {
89 list($category) = explode(',', $data['category']);
c958ac93 90 }
93a54393
JMV
91 $saquestions = question_bank::get_qtype('randomsamatch')->get_available_saquestions_from_category(
92 $category, $data['subcats']);
08224df1 93 $numberavailable = count($saquestions);
59a3fcd3 94 if ($saquestions === false) {
7f389342 95 $a = new stdClass();
f34488b2 96 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 97 $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
98
59a3fcd3 99 } else if ($numberavailable < $data['choose']) {
7f389342 100 $a = new stdClass();
f34488b2 101 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 102 $a->nosaquestions = $numberavailable;
103 $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
104 }
105 return $errors;
08224df1 106 }
08224df1 107}