-
Notifications
You must be signed in to change notification settings - Fork 6
/
quiz.devel.inc
252 lines (223 loc) · 7.07 KB
/
quiz.devel.inc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Form callback for devel_generate support.
*/
function quiz_generate_form($form, $form_state) {
$quiz_options['quiz'] = 'Quiz';
$form['quiz_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Quiz types'),
'#options' => $quiz_options,
'#default_value' => array('quiz' => 'quiz'),
'#required' => TRUE,
'#access' => FALSE,
);
$question_options = array();
foreach (quiz_get_question_types() as $name => $question_type) {
$question_options[$name] = $question_type['name'];
}
$form['quiz_question_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Question types'),
'#options' => $question_options,
'#default_value' => array_keys($question_options),
'#required' => TRUE,
);
$form['quiz_limit'] = array(
'#type' => 'textfield',
'#title' => t('Number of quizzes to generate'),
'#default_value' => 50,
'#size' => 10,
'#required' => TRUE,
'#description' => t('Quiz authors will be randomly assigned.'),
);
$form['quiz_question_limit'] = array(
'#type' => 'textfield',
'#title' => t('Number of questions per quiz'),
'#default_value' => 10,
'#size' => 10,
'#required' => TRUE,
'#description' => t('Question authors will be randomly assigned.'),
);
$form['quiz_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results per quiz'),
'#default_value' => 50,
'#size' => 10,
'#required' => TRUE,
'#description' => t('Results will be randomly assigned to users already in the system.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
/**
* Submit callback for devel_generate support.
*/
function quiz_generate_form_submit($form, &$form_state) {
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
module_load_include('inc', 'devel_generate', 'devel_generate');
$quiz_types = array_filter($form_state['values']['quiz_types']);
$quiz_limit = $form_state['values']['quiz_limit'];
$question_types = array_filter($form_state['values']['quiz_question_types']);
$question_limit = $form_state['values']['quiz_question_limit'];
$quiz_results = $form_state['values']['quiz_results'];
for ($i = 1; $i <= $quiz_limit; $i++) {
quiz_generate($quiz_type = array_rand($quiz_types), $question_types, $question_limit, $quiz_results);
}
}
/**
* Generate random quiz data.
*
* @todo associate results with random users
*
* @param type $quiz_type
* @param type $question_types
* @param type $question_limit
* @param type $quiz_results
*/
function quiz_generate($quiz_type, $question_types, $question_limit, $quiz_results) {
$users = array_filter(devel_get_users());
$quiz = (object) array(
'type' => $quiz_type,
'title' => devel_create_greeking(rand(5, 10), TRUE),
'uid' => $users[array_rand($users)],
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
// Populate all core fields on behalf of field.module.
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
devel_generate_fields($quiz, 'node', $quiz->type);
node_save($quiz);
for ($i = 1; $i <= $question_limit; $i++) {
quiz_generate_questions($quiz, array_rand($question_types));
}
for ($i = 1; $i <= $quiz_results; $i++) {
$quiz_result = entity_create('quiz_result', array(
'type' => 'quiz_result',
'nid' => $quiz->nid,
'vid' => $quiz->vid,
'uid' => $users[array_rand($users)],
));
$quiz_result->save();
db_update('quiz_node_results_answers')
->expression('points_awarded', 'result_answer_id % 5')
->expression('is_correct', 'result_answer_id % 2')
->condition('result_id', $quiz_result->result_id)
->execute();
db_update('quiz_node_results')
->expression('score', rand(1, 100))
->expression('is_evaluated', 1)
->expression('time_end', rand(REQUEST_TIME - 525600 * 60, REQUEST_TIME))
->condition('result_id', $quiz_result->result_id)
->execute();
}
drupal_set_message('Generated quiz: ' . l($quiz->title, 'node/' . $quiz->nid));
}
/**
* Generate Quiz questions.
*
* @param type $quiz
* @param type $question_type
*/
function quiz_generate_questions($quiz, $question_type) {
$users = array_filter(devel_get_users());
$question_array = array(
'type' => $question_type,
'changed' => REQUEST_TIME,
'moderate' => 0,
'promote' => 0,
'log' => '',
'status' => 1,
'sticky' => 0,
'language' => LANGUAGE_NONE,
'title' => devel_create_greeking(rand(5, 20), TRUE),
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => devel_create_para(rand(20, 50), 1)
),
),
),
'uid' => $users[array_rand($users)],
);
switch ($question_type) {
case 'truefalse':
$question_array += array('correct_answer' => rand(0, 1));
break;
case 'short_answer':
$question_array += array(
'correct_answer_evaluation' => rand(ShortAnswerQuestion::ANSWER_MATCH, ShortAnswerQuestion::ANSWER_MANUAL),
'correct_answer' => devel_create_greeking(rand(10, 20)),
);
break;
case 'long_answer':
$question_array += array(
'rubric' => devel_create_greeking(rand(10, 20)),
);
break;
case 'multichoice':
$question_array += quiz_generate_dummy_multichoice_question_info();
break;
case 'quiz_directions':
break;
default:
drupal_set_message('Unsupported question: ' . $question_type, 'error');
}
// Create question node.
$question = (object) $question_array;
// Populate all core fields on behalf of field.module.
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
devel_generate_fields($question, 'node', $question->type);
node_save($question);
_quiz_question_get_instance($question)->saveRelationships($quiz->nid, $quiz->vid);
quiz_update_max_score_properties(array($quiz->vid));
}
/**
* Template for multichoice questions.
*
* @return array
*/
function quiz_generate_dummy_multichoice_question_info() {
$array = array(
'choice_multi' => array_rand(array(0, 1)),
'choice_random' => array_rand(array(0, 1)),
'choice_boolean' => array_rand(array(0, 1)),
);
$rand = $array['choice_multi'] ? 1 : rand(2, 10);
for ($i = 0; $i < $rand; ++$i) {
if (!$array['choice_multi']) {
// Not multi answer - we can only do 1 correct answer.
if ($i == 0) {
$correct = 1;
}
else {
$correct = 0;
}
}
else {
// Multi answer.
$correct = rand(0, 1);
}
$array['alternatives'][] = array(
'correct' => $correct,
'answer' => array(
'value' => devel_create_greeking(rand(2, 10)),
'format' => filter_default_format(),
),
'feedback_if_chosen' => array(
'value' => devel_create_greeking(rand(5, 10)),
'format' => filter_default_format(),
),
'feedback_if_not_chosen' => array(
'value' => devel_create_greeking(rand(5, 10)),
'format' => filter_default_format(),
),
'score_if_chosen' => 1,
'score_if_not_chosen' => 0,
);
}
return $array;
}