-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmod_form.php
More file actions
176 lines (148 loc) · 9.08 KB
/
mod_form.php
File metadata and controls
176 lines (148 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once ($CFG->dirroot.'/mod/qcreate/locallib.php');
require_once ($CFG->dirroot.'/course/moodleform_mod.php');
require_once ($CFG->libdir.'/questionlib.php');
class mod_qcreate_mod_form extends moodleform_mod {
private $_requireds;
protected function definition() {
global $COURSE, $DB;
$mform =& $this->_form;
$this->_requireds = $DB->get_records('qcreate_required', array('qcreateid'=>$this->_instance), 'qtype', 'qtype, no, id');
//-------------------------------------------------------------------------------
/// Adding the "general" fieldset, where all the common settings are showed
$mform->addElement('header', 'general', get_string('general', 'form'));
/// Adding the standard "name" field
$mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
/// Adding the optional "intro" and "introformat" pair of fields
$this->add_intro_editor(false, get_string('intro', 'qcreate'));
// TODO: $mform->setHelpButton('intro', array('writing', 'richtext'), false, 'editorhelpbutton');
// TODO: $mform->addElement('format', 'introformat', get_string('format'));
//-------------------------------------------------------------------------------
$mform->addElement('header', 'timinghdr', get_string('timing', 'form'));
$mform->addElement('date_time_selector', 'timeopen', get_string('open', 'qcreate'), array('optional'=>true));
// TODO: $mform->setHelpButton('timeopen', array('timeopen', get_string('open', 'qcreate'), 'qcreate'));
$mform->addElement('date_time_selector', 'timeclose', get_string('close', 'qcreate'), array('optional'=>true));
// TODO: $mform->setHelpButton('timeclose', array('timeopen', get_string('close', 'qcreate'), 'qcreate'));
//-------------------------------------------------------------------------------
$mform->addElement('header', 'gradeshdr', get_string('grading', 'qcreate'));
$gradeoptions = array();
$gradeoptions[0] = get_string('nograde');
for ($i=100; $i>=1; $i--) {
$gradeoptions[$i] = $i;
}
$mform->addElement('select', 'grade', get_string('grade'), $gradeoptions);
$mform->setDefault('grade', 100);
// TODO: $mform->setHelpButton('grade', array('grade', get_string('grade'), 'qcreate'));
$graderatiooptions = array();
foreach (array(100, 90, 80, 67, 60, 50, 40, 33, 30, 20, 10, 0)
as $graderatiooption){
$a = new object();
$a->automatic = ($graderatiooption).'%';
$a->manual = (100 - ($graderatiooption)).'%';
$graderatiooptions[$graderatiooption] = get_string('graderatiooptions', 'qcreate', $a);
}
$mform->addElement('select', 'graderatio', get_string('graderatio', 'qcreate'), $graderatiooptions);
$mform->setDefault('graderatio', 50);
// TODO: $mform->setHelpButton('graderatio', array('automaticmanualgrading', get_string('graderatio', 'qcreate'), 'qcreate'));
$allowedgroup = array();
$allowedgroup[] =& $mform->createElement('checkbox', "ALL", '', get_string('allowall', 'qcreate'));
$mform->setDefault("allowed[ALL]", 1);
$qtypemenu = question_type_menu();
foreach ($qtypemenu as $qtype => $qtypestring){
$allowedgroup[] =& $mform->createElement('checkbox', "$qtype", '', $qtypestring);
}
$mform->addGroup($allowedgroup, 'allowed', get_string('allowedqtypes', 'qcreate'));
$mform->disabledIf('allowed', "allowed[ALL]", 'checked');
// TODO: $mform->setHelpButton('allowed', array('gradedquestiontypes', get_string('allowedqtypes', 'qcreate'), 'qcreate'));
for ($i= 1; $i<=20; $i++){
$noofquestionsmenu[$i] = $i;
}
$mform->addElement('select', 'totalrequired', get_string('noofquestionstotal', 'qcreate'), $noofquestionsmenu);
// TODO: $mform->setHelpButton('totalrequired', array('totalquestionsgraded', get_string('noofquestionstotal', 'qcreate'), 'qcreate'));
//-------------------------------------------------------------------------------
$repeatarray=array();
$repeatarray[] =& $mform->createElement('header', 'addminimumquestionshdr', get_string('addminimumquestionshdr', 'qcreate'));
$qtypeselect = array(''=>get_string('selectone', 'qcreate')) + $qtypemenu;
$repeatarray[] =& $mform->createElement('select', 'qtype', get_string('qtype', 'qcreate'), $qtypeselect);
$repeatarray[] =& $mform->createElement('select', 'minimumquestions', get_string('minimumquestions', 'qcreate'), $noofquestionsmenu);
$requiredscount = count($this->_requireds);
$repeats = $this->_requireds ? $requiredscount+2 : 4;
$repeats = $this->repeat_elements($repeatarray, $repeats, array(), 'minrepeats', 'addminrepeats', 2);
for ($i=0; $i<$repeats; $i++) {
// TODO: $mform->setHelpButton("qtype[$i]", array('questiontype', get_string('qtype', 'qcreate'), 'qcreate'));
// TODO: $mform->setHelpButton("minimumquestions[$i]", array('minimumquestions', get_string('minimumquestions', 'qcreate'), 'qcreate'));
$mform->disabledIf("minimumquestions[$i]", "qtype[$i]", 'eq', '');
}
//-------------------------------------------------------------------------------
/// Adding the "general" fieldset, where all the common settings are showed
$mform->addElement('header', 'studentaccessheader', get_string('studentaccessheader', 'qcreate'));
$studentqaccessmenu = array(0=>get_string('studentaccessaddonly', 'qcreate'),
1=>get_string('studentaccesspreview', 'qcreate'),
2=>get_string('studentaccesssaveasnew', 'qcreate'),
3=>get_string('studentaccessedit', 'qcreate'));
$mform->addElement('select', 'studentqaccess', get_string('studentqaccess', 'qcreate'), $studentqaccessmenu);
// TODO: $mform->setHelpButton('studentqaccess', array('studentquestionaccess', get_string('studentqaccess', 'qcreate'), 'qcreate'));
//-------------------------------------------------------------------------------
// add standard elements, common to all modules
$this->standard_coursemodule_elements();
//-------------------------------------------------------------------------------
// add standard buttons, common to all modules
$this->add_action_buttons();
}
function data_preprocessing(&$default_values){
$i = 0;
if ($this->_requireds){
foreach ($this->_requireds as $qtype => $required){
$default_values["minimumquestions[$i]"] = $required->no;
$default_values["qtype[$i]"] = $qtype;
$i++;
}
}
if (isset($default_values['allowed'])){
$enabled = explode(',', $default_values['allowed']);
$qtypemenu = question_type_menu();
foreach (array_keys($qtypemenu) as $qtype){
$default_values["allowed[$qtype]"] = (array_search($qtype, $enabled)!==FALSE)?1:0;
}
$default_values["allowed[ALL]"] = (array_search('ALL', $enabled)!==FALSE)?1:0;
}
}
public function validation($data, $files){
$errors = array();
if (!isset($data['allowed'])){
$errors['allowed']=get_string('needtoallowatleastoneqtype', 'qcreate');
}
$qtypemenu = question_type_menu();
$totalrequired = 0;
if (isset($data['qtype'])){
foreach ($data['qtype'] as $key => $qtype){
if ($qtype!=''){
$chkqtypes[$key] = $qtype;
$keysforthisqtype = array_keys($chkqtypes);
if (count(array_keys($data['qtype'], $qtype)) > 1){
$errors["qtype[$key]"]=get_string('morethanonemin', 'qcreate', $qtypemenu[$qtype]);
} elseif (!isset($data['allowed'][$qtype]) && !isset($data['allowed']['ALL'])){
$errors['allowed']=get_string('needtoallowqtype', 'qcreate', $qtypemenu[$qtype]);
$errors["qtype[$key]"]=get_string('needtoallowqtype', 'qcreate', $qtypemenu[$qtype]);
}
$totalrequired += $data['minimumquestions'][$key];
}
}
}
if ($totalrequired > $data['totalrequired']){
$errors['totalrequired']=get_string('totalrequiredislessthansumoftotalsforeachqtype', 'qcreate');
}
if (isset($data['allowed']['ALL']) && (count($data['allowed']) > 1)){
$errors['allowed']=get_string('allandother', 'qcreate');
}
if (($data['timeclose'] !=0) && ($data['timeopen'] !=0) && ($data['timeclose'] <= $data['timeopen'])){
$errors['timeopen']=get_string('openmustbemorethanclose', 'qcreate');
}
return $errors;
}
}