weekly release 2.7dev
[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 */
59a3fcd3
TH
36class qtype_randomsamatch_edit_form extends question_edit_form {
37 protected function definition_inner($mform) {
08224df1 38 $questionstoselect = array();
59a3fcd3 39 for ($i = 2; $i <= qtype_randomsamatch::MAX_SUBQUESTIONS; $i++) {
08224df1 40 $questionstoselect[$i] = $i;
41 }
42
59a3fcd3
TH
43 $mform->addElement('select', 'choose',
44 get_string('randomsamatchnumber', 'quiz'), $questionstoselect);
08224df1 45 $mform->setType('feedback', PARAM_RAW);
46
47 $mform->addElement('hidden', 'fraction', 0);
d18e0fe6 48 $mform->setType('fraction', PARAM_RAW);
08224df1 49 }
50
c7df5006 51 protected function data_preprocessing($question) {
08224df1 52 if (empty($question->name)) {
59a3fcd3 53 $question->name = get_string('randomsamatch', 'quiz');
08224df1 54 }
55
56 if (empty($question->questiontext)) {
59a3fcd3 57 $question->questiontext = get_string('randomsamatchintro', 'quiz');
08224df1 58 }
fe6ce234 59 return $question;
08224df1 60 }
61
c7df5006 62 public function qtype() {
08224df1 63 return 'randomsamatch';
64 }
65
59a3fcd3
TH
66 public function validation($data, $files) {
67 global $DB;
fe93ba83 68 $errors = parent::validation($data, $files);
5e581dd4 69 if (isset($data->categorymoveto)) {
70 list($category) = explode(',', $data['categorymoveto']);
71 } else {
72 list($category) = explode(',', $data['category']);
c958ac93 73 }
59a3fcd3 74 $saquestions = question_bank::get_qtype('randomsamatch')->get_sa_candidates($category);
08224df1 75 $numberavailable = count($saquestions);
59a3fcd3 76 if ($saquestions === false) {
7f389342 77 $a = new stdClass();
f34488b2 78 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 79 $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
80
59a3fcd3 81 } else if ($numberavailable < $data['choose']) {
7f389342 82 $a = new stdClass();
f34488b2 83 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 84 $a->nosaquestions = $numberavailable;
85 $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
86 }
87 return $errors;
08224df1 88 }
08224df1 89}