MDL-20636 fix some more coding style issues in the question code.
[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 *
b04a4319
TH
20 * @package qtype
21 * @subpackage randomsamatch
22 * @copyright 2007 Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
08224df1 24 */
25
d3603157 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
08224df1 30/**
31 * randomsamatch editing form definition.
b04a4319
TH
32 *
33 * @copyright 2007 Jamie Pratt me@jamiep.org
34 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
08224df1 35 */
36class question_edit_randomsamatch_form extends question_edit_form {
37 /**
38 * Add question-type specific form fields.
39 *
40 * @param MoodleQuickForm $mform the form being built.
41 */
c7df5006 42 protected function definition_inner(&$mform) {
08224df1 43 $questionstoselect = array();
44 for ($i=2; $i<=QUESTION_NUMANS; $i++){
45 $questionstoselect[$i] = $i;
46 }
47
48 $mform->addElement('select', 'choose', get_string("randomsamatchnumber", "quiz"), $questionstoselect);
49 $mform->setType('feedback', PARAM_RAW);
50
51 $mform->addElement('hidden', 'fraction', 0);
d18e0fe6 52 $mform->setType('fraction', PARAM_RAW);
08224df1 53 }
54
c7df5006 55 protected function data_preprocessing($question) {
08224df1 56 if (empty($question->name)) {
5e8a85aa 57 $question->name = get_string("randomsamatch", "quiz");
08224df1 58 }
59
60 if (empty($question->questiontext)) {
fe6ce234 61 $question->questiontext = get_string("randomsamatchintro", "quiz");
08224df1 62 }
fe6ce234 63 return $question;
08224df1 64 }
65
c7df5006 66 public function qtype() {
08224df1 67 return 'randomsamatch';
68 }
69
a78890d5 70 function validation($data, $files) {
f34488b2 71 global $QTYPES, $DB;
fe93ba83 72 $errors = parent::validation($data, $files);
5e581dd4 73 if (isset($data->categorymoveto)) {
74 list($category) = explode(',', $data['categorymoveto']);
75 } else {
76 list($category) = explode(',', $data['category']);
c958ac93 77 }
c958ac93 78 $saquestions = $QTYPES['randomsamatch']->get_sa_candidates($category);
08224df1 79 $numberavailable = count($saquestions);
80 if ($saquestions === false){
7f389342 81 $a = new stdClass();
f34488b2 82 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 83 $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
84
85 } elseif ($numberavailable < $data['choose']){
7f389342 86 $a = new stdClass();
f34488b2 87 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 88 $a->nosaquestions = $numberavailable;
89 $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
90 }
91 return $errors;
08224df1 92 }
08224df1 93}