Newer
Older
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
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* Administrator interface for Quiz module.
*
* @file
*/
// QUIZ ADMIN
// Quiz Admin Settings
/**
* This builds the main settings form for the quiz module.
*/
function quiz_admin_settings($form, &$form_state) {
$form = array();
$form['quiz_global_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Global Configuration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Control aspects of the Quiz module\'s display'),
);
$form['quiz_global_settings']['quiz_auto_revisioning'] = array(
'#type' => 'checkbox',
'#title' => t('Auto revisioning'),
'#default_value' => variable_get('quiz_auto_revisioning', 1),
'#description' => t('It is strongly recommended that auto revisioning is always on. It makes sure that when a question or quiz is changed a new revision is created if the current revision has been answered. If this feature is switched off result reports might be broken because a users saved answer might be connected to a wrong version of the quiz and/or question she was answering. All sorts of errors might appear.'),
);
$form['quiz_global_settings']['quiz_durod'] = array(
'#type' => 'checkbox',
'#title' => t('Delete results when a user is deleted'),
'#default_value' => variable_get('quiz_durod', 0),
'#description' => t('When a user is deleted delete any and all results for that user.'),
);
$form['quiz_global_settings']['quiz_index_questions'] = array(
'#type' => 'checkbox',
'#title' => t('Index questions'),
'#default_value' => variable_get('quiz_index_questions', 1),
'#description' => t('If you turn this off questions will not show up in search results.'),
);
$form['quiz_global_settings']['quiz_default_close'] = array(
'#type' => 'textfield',
'#title' => t('Default number of days before a @quiz is closed', array('@quiz' => QUIZ_NAME)),
'#default_value' => variable_get('quiz_default_close', 30),
'#size' => 4,
'#maxlength' => 4,
'#description' => t('Supply a number of days to calculate the default close date for new quizzes.'),
);
$form['quiz_global_settings']['quiz_use_passfail'] = array(
'#type' => 'checkbox',
'#title' => t('Allow quiz creators to set a pass/fail option when creating a @quiz.', array('@quiz' => strtolower(QUIZ_NAME))),
'#default_value' => variable_get('quiz_use_passfail', 1),
'#description' => t('Check this to display the pass/fail options in the @quiz form. If you want to prohibit other quiz creators from changing the default pass/fail percentage, uncheck this option.', array('@quiz' => QUIZ_NAME)),
);
$form['quiz_global_settings']['quiz_max_result_options'] = array(
'#type' => 'textfield',
'#title' => t('Maximum Result Options'),
'#description' => t('Set the maximum number of result options (categorizations for scoring a quiz). Set to 0 to disable result options.'),
'#default_value' => variable_get('quiz_max_result_options', 5),
'#size' => 2,
'#maxlength' => 2,
'#required' => FALSE,
);
$form['quiz_global_settings']['quiz_remove_partial_quiz_record'] = array(
'#type' => 'select',
'#title' => t('Remove Incomplete Quiz Records (older than)'),
'#options' => quiz_remove_partial_quiz_record_value(),
'#description' => t('Number of days that you like to keep the incomplete quiz records'),
'#default_value' => variable_get('quiz_remove_partial_quiz_record', quiz_remove_partial_quiz_record_value()),
);
$form['quiz_global_settings']['quiz_autotitle_length'] = array(
'#type' => 'textfield',
'#title' => t('Length of automatically set question titles'),
'#size' => 3,
'#maxlength' => 3,
'#description' => t('Integer between 0 and 128. If the question creator doesn\'t set a question title the system will make a title automatically. Here you can decide how long the autotitle can be.'),
'#default_value' => variable_get('quiz_autotitle_length', 50),
);
$target = array(
'attributes' => array(
'target' => '_blank'
),
);
$links = array(
'!views' => l(t('Views'), 'http://drupal.org/project/views', $target),
'!cck' => l(t('CCK'), 'http://drupal.org/project/cck', $target),
'!jquery_countdown' => l(t('JQuery Countdown'), 'http://drupal.org/project/jquery_countdown', $target),
'!userpoints' => l(t('UserPoints'), 'http://drupal.org/project/userpoints', $target),
'@quiz' => QUIZ_NAME,
);
$form['quiz_addons'] = array(
'#type' => 'fieldset',
'#title' => t('Addons Configuration'),
'#description' => t('Quiz can integrate with other d.o modules like !views, !cck, !userpoints and !jquery_countdown. Here you can configure the way Quiz integrates with other modules. Disabled checkboxes indicates that modules are not enabled/installed', $links),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['quiz_addons']['quiz_has_userpoints'] = array(
'#type' => 'checkbox',
'#title' => t('Enable UserPoints Module Integration'),
'#default_value' => variable_get('quiz_has_userpoints', 0),
'#description' => t('!userpoints is an <strong>optional</strong> module for Quiz. It provides ways for users to gain or lose points for performing certain actions on your site like attending @quiz. You will need to install the !userpoints module to use this feature.', $links),
'#disabled' => !module_exists('userpoints'),
);
$form['quiz_addons']['quiz_has_timer'] = array(
'#type' => 'checkbox',
'#title' => t('Display Timer for Timed Quiz'),
'#default_value' => variable_get('quiz_has_timer', 0),
'#description' => t("!jquery_countdown is an <strong>optional</strong> module for Quiz. It is used to display a timer when taking a quiz. Without this timer, the user will not know how long he or she has left to complete the @quiz", $links),
'#disabled' => !function_exists('jquery_countdown_add'),
);
$form['quiz_look_feel'] = array(
'#type' => 'fieldset',
'#title' => t('Look and Feel Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Control aspects of the Quiz module\'s display'),
);
$form['quiz_look_feel']['quiz_name'] = array(
'#type' => 'textfield',
'#title' => t('Display name'),
'#default_value' => QUIZ_NAME,
'#description' => t('Change the name of the quiz type. Do you call it <em>test</em> or <em>assessment</em> instead? Change the display name of the module to something else. Currently, it is called @quiz. By default, it is called <em>Quiz</em>.',
array('@quiz' => QUIZ_NAME)),
'#required' => TRUE,
);
$form['quiz_email_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Email Settings'),
'#description' => t('Send results to quiz author/attendee via e-mail. Configure e-mail subject and body.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['quiz_email_settings']['taker'] = array(
'#type' => 'fieldset',
'#title' => t('E-mail for Quiz Takers'),
'#collapsible' => FALSE,
);
$form['quiz_email_settings']['taker']['quiz_email_results'] = array(
'#type' => 'checkbox',
'#title' => t('E-mail results to quiz takers'),
'#default_value' => variable_get('quiz_email_results', 0),
'#description' => t('Check this to send users their results at the end of a quiz.')
);
$form['quiz_email_settings']['taker']['quiz_email_results_subject_taker'] = array(
'#type' => 'textfield',
'#title' => t('Configure E-mail Subject'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz.'),
'#default_value' => variable_get('quiz_email_results_subject_taker', quiz_email_results_format('subject', 'taker')),
);
$form['quiz_email_settings']['taker']['quiz_email_results_body_taker'] = array(
'#type' => 'textarea',
'#title' => t('Configure E-mail Format'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz. !title(quiz title), !sitename, !taker(quiz takers username), !date(time when quiz was finished), !minutes(How many minutes the quiz taker spent taking the quiz), !desc(description of the quiz), !correct(points attained), !total(max score for the quiz), !percentage(percentage score), !url(url to the result page) and !author are placeholders.'),
'#default_value' => variable_get('quiz_email_results_body_taker', quiz_email_results_format('body', 'taker')),
);
$form['quiz_email_settings']['author'] = array(
'#type' => 'fieldset',
'#title' => t('E-mail for Quiz Authors'),
'#collapsible' => FALSE,
);
$form['quiz_email_settings']['author']['quiz_results_to_quiz_author'] = array(
'#type' => 'checkbox',
'#title' => t('E-mail all results to quiz author.'),
'#default_value' => variable_get('quiz_results_to_quiz_author', 0),
'#description' => t('Check this to send quiz results for all users to the quiz author.'),
);
$form['quiz_email_settings']['author']['quiz_email_results_subject'] = array(
'#type' => 'textfield',
'#title' => t('Configure E-mail Subject'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz. Authors and quiz takers gets the same format.'),
'#default_value' => variable_get('quiz_email_results_subject', quiz_email_results_format('subject', 'author')),
);
$form['quiz_email_settings']['author']['quiz_email_results_body'] = array(
'#type' => 'textarea',
'#title' => t('Configure E-mail Format'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz. !title(quiz title), !sitename, !taker(quiz takers username), !date(time when quiz was finished), !minutes(How many minutes the quiz taker spent taking the quiz), !desc(description of the quiz), !correct(points attained), !total(max score for the quiz), !percentage(percentage score), !url(url to the result page) and !author are placeholders.'),
'#default_value' => variable_get('quiz_email_results_body', quiz_email_results_format('body', 'author')),
);
$form['def_settings_link'] = array(
'#markup' => '<p>' . t('Default values for the quiz creation form can be edited <a href="!url">here</a>', array('!url' => url('admin/quiz/settings/quiz_form'))) . '</p>',
);
$form['#validate'][] = 'quiz_settings_form_validate';
$form['#submit'][] = 'quiz_settings_form_submit';
return system_settings_form($form);
}
/**
* Validation of the Form Settings form.
*
* Checks the values for the form administration form for quiz settings.
*/
function quiz_settings_form_validate($form, &$form_state) {
if (!_quiz_is_int($form_state['values']['quiz_default_close'])) {
form_set_error('quiz_default_close', t('The default number of days before a quiz is closed must be a number greater than 0.'));
}
if (!_quiz_is_int($form_state['values']['quiz_autotitle_length'], 0, 128)) {
form_set_error('quiz_autotitle_length', t('The autotitle length value must be an integer between 0 and 128.'));
}
if (!_quiz_is_int($form_state['values']['quiz_max_result_options'], 0, 100)) {
form_set_error('quiz_max_result_options', t('The number of resultoptions must be an integer between 0 and 100.'));
}
if (!_quiz_is_plain($form_state['values']['quiz_name'])) {
form_set_error('quiz_name', t('The quiz name must be plain text.'));
}
/*if (!_quiz_is_plain($form_state['values']['quiz_action_type']))
form_set_error('quiz_action_type', t('The action type must be plain text.'));*/
}
/**
* Submit the admin settings form
*/
function quiz_settings_form_submit($form, &$form_state) {
if (QUIZ_NAME != $form_state['values']['quiz_name']) {
variable_set('quiz_name', $form_state['values']['quiz_name']);
define(QUIZ_NAME, $form_state['values']['quiz_name']);
menu_rebuild();
}
}
/**
* Renders the quiz node form for the admin pages
*
* This form is used to configure default values for the quiz node form
*/
function quiz_admin_node_form($form, &$form_state) {
// Create a dummy node to use as input for quiz_form
$dummy_node = new stdClass();
// def_uid is the uid of the default user holding the default values for the node form(no real user with this uid exists)
$dummy_node->def_uid = variable_get('quiz_def_uid', 1);
$settings = _quiz_load_user_settings(variable_get('quiz_def_uid', 1));
$settings += _quiz_get_node_defaults();
foreach ($settings as $key => $value) {
if (!isset($dummy_node->$key)) {
$dummy_node->{$key} = $value;
}
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
}
$form = quiz_form($dummy_node, $form_state);
$form['direction'] = array(
'#markup' => t('Here you can change the default quiz settings for new users.'),
'#weight' => -10,
);
// unset values we can't or won't let the user edit default values for
unset(
$form['#quiz_check_revision_access'],
$form['title'],
$form['body_field'],
$form['taking']['aid'],
$form['taking']['addons'],
$form['quiz_availability']['quiz_open'],
$form['quiz_availability']['quiz_close'],
$form['resultoptions'],
$form['number_of_random_questions']
);
$form['remember_settings']['#type'] = 'value';
$form['remember_settings']['#default_value'] = TRUE;
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('quiz_admin_node_form_submit'),
);
return $form;
}
/**
* Validation function for the quiz_admin_node_form form
*/
function quiz_admin_node_form_validate($form, &$form_state) {
// Create dummy node for quiz_validate
$dummy_node = new stdClass();
foreach ($form_state['values'] as $key => $value) {
$dummy_node->{$key} = $value;
}
$dummy_node->resultoptions = array();
// We use quiz_validate to validate the default values
quiz_validate($dummy_node);
}
/**
* Submit function for quiz_admin_node_form
*
* The default values are saved as the user settings for the "default user"
* The default user is created when quiz is installed. He has a unique uid, but doesn't exist
* as a real user.
*
* Why?
* Default user settings can be loaded and saved using the same code and
* database tables as any other user settings, making the code a lot easier to maintain.
* Ref: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
*/
function quiz_admin_node_form_submit($form, &$form_state) {
// We add the uid for the "default user"
$form_state['values']['save_def_uid'] = variable_get('quiz_def_uid', NULL);
$form_state['values']['nid'] = 0;
$form_state['values']['vid'] = 0;
$form_state['values']['aid'] = '';
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
_quiz_save_user_settings($form_state['values']);
}
// QUIZ RESULTS ADMIN
/**
* Displays the quizzes by title with a link to the appropriate results
* for that specific quiz.
*
* @return
* Formatted data.
*/
function quiz_admin_quizzes() {
global $user;
$uid = $user->uid;
if (user_access('view any quiz results')) {
$uid = NULL;
}
$results = _quiz_get_quizzes($uid);
return theme('quiz_admin_quizzes', array('results' => $results));
}
/**
* Quiz result report page for the quiz admin section
*
* @param $quiz
* The quiz node
* @param $rid
* The result id
*/
function quiz_admin_results($quiz, $rid) {
$breadcrumb = drupal_get_breadcrumb();
$breadcrumb[] = l(t('Quiz Results'), 'admin/quiz/reports/results');
// Make sure we have the right version of the quiz
$vid = db_query('SELECT vid FROM {quiz_node_results} WHERE result_id = :result_id', array(':result_id' => $rid))->fetchField();
if ($quiz->vid != $vid) {
$quiz = node_load($quiz->nid, $vid);
}
// Get all the data we need.
$questions = _quiz_get_answers($quiz, $rid);
$score = quiz_calculate_score($quiz, $rid);
$summary = _quiz_get_summary_text($quiz, $score);
// Lets add the quiz title to the breadcrumb array.
$breadcrumb[] = l($quiz->title, 'admin/quiz/reports/results/' . $quiz->nid);
//drupal_set_breadcrumb($breadcrumb);
$data = array(
'quiz' => $quiz,
'questions' => $questions,
'score' => $score,
'summary' => $summary,
'rid' => $rid
);
return theme('quiz_admin_summary', $data);
}
/**
* Store values for each browser filters in $_SESSION
*
* @param $filters
* array holding the values for each filter
*/
function _quiz_results_mr_store_filters($form_state) {
$filters = $form_state['values']['table']['header']['filters'];
falcon
committed
$_SESSION[$pre . 'name'] = trim($filters['name']);
$_SESSION[$pre . 'started'] = $filters['started'];
$_SESSION[$pre . 'finished'] = $filters['finished'];
$_SESSION[$pre . 'score'] = $filters['score'];
$_SESSION[$pre . 'evaluated'] = $filters['evaluated'];
$_SESSION[$pre . 'best_results'] = $filters['best_results'];
$_SESSION[$pre . 'not_in_progress'] = $filters['not_in_progress'];
}
function quiz_browser_body_callback($form, $form_state) {
return $form['table']['body'];
function quiz_questions_browser_body_callback($form, $form_state) {
return $form['question_list']['browser']['table']['body'];
}
// MANAGE QUESTIONS
/**
* Creates a form for quiz questions.
*
* Handles the manage questions tab.
*
* @param $node
* The quiz node we are managing questions for.
* @return
* String containing the form.
*/
function quiz_questions($node) {
// Set page title.
drupal_set_title($node->title);
if ($node->randomization < 3) {
$form = drupal_get_form('quiz_questions_form', $node);
$form = drupal_get_form('quiz_categorized_form', $node);
return drupal_render($form);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
}
/**
* Form for managing what questions should be added to a quiz with categorized random questions.
*
* @param array $form_state
* The form state array
* @param object $quiz
* The quiz node
*/
function quiz_categorized_form($form, $form_state, $quiz) {
$form = array();
_quiz_categorized_existing_terms_form($form, $form_state, $quiz);
_quiz_categorized_new_term_form($form, $form_state, $quiz);
$form['nid'] = array(
'#type' => 'value',
'#value' => $quiz->nid,
);
$form['vid'] = array(
'#type' => 'value',
'#value' => $quiz->vid,
);
$form['tid'] = array(
'#type' => 'value',
'#value' => NULL,
);
// Give the user the option to create a new revision of the quiz
_quiz_add_revision_checkbox($form, $quiz);
// Timestamp is needed to avoid multiple users editing the same quiz at the same time.
$form['timestamp'] = array('#type' => 'hidden', '#default_value' => REQUEST_TIME);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['#tree'] = TRUE;
return $form;
}
function _quiz_categorized_existing_terms_form(&$form, $form_state, $quiz) {
$terms = _quiz_get_terms($quiz->vid);
foreach ($terms as $term) {
$form[$term->tid]['name'] = array(
'#markup' => check_plain($term->name),
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
);
$form[$term->tid]['number'] = array(
'#type' => 'textfield',
'#size' => 3,
'#default_value' => $term->number,
);
$form[$term->tid]['max_score'] = array(
'#type' => 'textfield',
'#size' => 3,
'#default_value' => $term->max_score,
);
$form[$term->tid]['remove'] = array(
'#type' => 'checkbox',
'#default_value' => 0,
);
$form[$term->tid]['weight'] = array(
'#type' => 'textfield',
'#size' => 3,
'#default_value' => $term->weight,
'#attributes' => array(
'class' => array('term-weight')
),
);
}
}
/**
* Form for adding new terms to a quiz
*
* @see quiz_categorized_form
*/
function _quiz_categorized_new_term_form(&$form, $form_state, $quiz) {
$form['new'] = array(
'#type' => 'fieldset',
'#title' => t('Add category'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => FALSE,
);
$form['new']['term'] = array(
'#type' => 'textfield',
'#title' => t('Category'),
'#description' => t('Type in the name of the term you would like to add questions from.'),
'#autocomplete_path' => "node/$quiz->nid/questions/term_ahah",
'#field_suffix' => '<a id="browse-for-term" href="javascript:void(0)">' . t('browse') . '</a>',
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
);
$form['new']['number'] = array(
'#type' => 'textfield',
'#title' => t('Number of questions'),
'#description' => t('How many questions would you like to draw from this term?'),
);
$form['new']['max_score'] = array(
'#type' => 'textfield',
'#title' => t('Max score for each question'),
'#description' => t('The number of points a user will be awarded for each question he gets correct.'),
'#default_value' => 1,
);
}
/**
* Validate the categorized form
*/
function quiz_categorized_form_validate($form, &$form_state) {
if (_quiz_is_int(arg(1))) {
if (node_last_changed(arg(1)) > $form_state['values']['timestamp']) {
form_set_error('changed', t('This content has been modified by another user, changes cannot be saved.'));
}
}
else {
form_set_error('changed', t('A critical error has occured. Please report error code 28 on the quiz project page.'));
return;
}
if (!empty($form_state['values']['term'])) {
$tid = _quiz_get_id_from_string($form_state['values']['term']);
if ($tid === FALSE) {
$terms = _quiz_search_terms($form_state['values']['term']);
$num_terms = count($terms);
if ($num_terms == 1) {
$tid = key($terms);
}
elseif ($num_terms > 1) {
form_set_error('term', t('You need to be more specific, or use the autocomplete feature. The term name you entered matches several terms: %terms', array('%terms' => implode(', ', $terms))));
}
elseif ($num_terms == 0) {
form_set_error('term', t("The term name you entered doesn't match any registered question terms."));
}
}
if (in_array($tid, array_keys($form))) {
form_set_error('term', t('The category you are trying to add has already been added to this quiz.'));
}
else {
form_set_value($form['tid'], $tid, $form_state);
}
if (!_quiz_is_int($form_state['values']['number'])) {
form_set_error('number', t('The number of questions needs to be a positive integer'));
}
if (!_quiz_is_int($form_state['values']['max_score'], 0)) {
form_set_error('max_score', t('The max score needs to be a positive integer or 0'));
}
}
}
/**
* Submit the categorized form
*/
function quiz_categorized_form_submit($form, $form_state) {
$quiz = node_load($form_state['values']['nid'], $form_state['values']['vid']);
$quiz->number_of_random_questions = 0;
// Update the refresh latest quizzes table so that we know what the users latest quizzes are
if (variable_get('quiz_auto_revisioning', 1)) {
$is_new_revision = quiz_has_been_answered($quiz);
}
else {
$is_new_revision = (bool) $form_state['values']['new_revision'];
}
if (!empty($form_state['values']['tid'])) {
$quiz->number_of_random_questions += _quiz_categorized_add_term($form, $form_state);
}
$quiz->number_of_random_questions += _quiz_categorized_update_terms($form, $form_state);
if ($is_new_revision) {
$quiz->revision = 1;
}
// We save the node to update its timestamp and let other modules react to the update.
// We also do this in case a new revision is required...
node_save($quiz);
}
/**
* Update the categoriez belonging to a quiz with categorized random questions.
*
* Helper function for quiz_categorized_form_submit
*/
function _quiz_categorized_update_terms(&$form, &$form_state) {
$ids = array('weight', 'max_score', 'number');
$changed = array();
$removed = array();
$num_questions = 0;
foreach ($form_state['values'] as $key => $existing) {
if (!is_numeric($key)) {
continue;
}
if (!$existing['remove']) {
$num_questions += $existing['number'];
}
foreach ($ids as $id) {
if ($existing[$id] != $form[$key][$id]['#default_value'] && !$existing['remove']) {
$existing['nid'] = $form_state['values']['nid'];
$existing['vid'] = $form_state['values']['vid'];
$existing['tid'] = $key;
$existing['weight'] = 1;
$changed[] = $form[$key]['name']['#markup'];
drupal_write_record('quiz_terms', $existing, array('vid', 'tid'));
break;
}
elseif ($existing['remove']) {
db_delete('quiz_terms')
->condition('tid', $key)
->condition('vid', $form_state['values']['vid'])
->execute();
$removed[] = $form[$key]['name']['#markup'];
break;
}
}
}
if (!empty($changed)) {
drupal_set_message(t('Updates were made for the following terms: %terms', array('%terms' => implode(', ', $changed))));
}
if (!empty($removed)) {
drupal_set_message(t('The following terms were removed: %terms', array('%terms' => implode(', ', $removed))));
}
return $num_questions;
}
/**
* Adds a term to a categorized quiz
*
* This is a helper function for the submit function.
*/
function _quiz_categorized_add_term($form, $form_state) {
drupal_set_message(t('The term was added'));
// Needs to be set to avoid error-message from db:
$form_state['values']['weight'] = 0;
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
drupal_write_record('quiz_terms', $form_state['values']);
return $form_state['values']['number'];
}
/**
* Searches for an id in the end of a string.
*
* Id should be written like "(id:23)"
*
* @param string $string
* The string where we will search for an id
* @return int
* The matched integer
*/
function _quiz_get_id_from_string($string) {
$matches = array();
preg_match('/\(id:(\d+)\)$/', $string, $matches);
return isset($matches[1]) ? (int) $matches[1] : FALSE;
}
/**
* Ahah function for finding terms...
*
* @param string $start
* The start of the string we are looking for
*/
function quiz_categorized_term_ahah($start) {
$terms = _quiz_search_terms($start, $start == '*');
$to_json = array();
foreach ($terms as $key => $value) {
$to_json["$value (id:$key)"] = $value;
}
drupal_json_output($to_json);
}
/**
* Helper function for finding terms...
*
* @param string $start
* The start of the string we are looking for
*/
function _quiz_search_terms($start, $all = FALSE) {
$terms = array();
$sql_args = array_keys(_quiz_get_vocabularies());
if (empty($sql_args)) {
return $terms;
}
$query = db_select('taxonomy_term_data', 't')
->fields('t', array('name', 'tid'))
->condition('t.vid', $sql_args , 'IN');
if (!$all) {
$query->condition('t.name', '%' . $start . '%', 'LIKE');
}
$res = $query->execute();
// TODO Don't user db_fetch_object
while ($res_o = $res->fetch()) {
$terms[$res_o->tid] = $res_o->name;
}
return $terms;
}
/**
* Handles "manage questions" tab.
*
* Displays form which allows questions to be assigned to the given quiz.
*
* This function is not used if the question assignment type "categorized random questions" is chosen
*
* @param $form_state
* The form state variable
* @param $quiz
* The quiz node.
* @return
* HTML output to create page.
*/
function quiz_questions_form($form, $form_state, $quiz) {
if ($form_state['rebuild']) {
// Save the active filters in $_SESSION
$filters = $form_state['values']['browser']['table']['header']['filters'];
_quiz_questions_store_filters($filters);
}
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
$types = _quiz_get_question_types();
_quiz_add_fields_for_creating_questions($form, $types, $quiz);
// Display questions in this quiz.
$form['question_list'] = array(
'#type' => 'fieldset',
'#title' => t('Questions in this quiz'),
'#theme' => 'question_selection_table',
'#collapsible' => TRUE,
'#attributes' => array('id' => 'mq-fieldset'),
'question_status' => array('#tree' => TRUE),
);
$form['#attached']['js'] = array(drupal_get_path('module', 'quiz') .'/theme/quiz_question_browser.js');
// Add randomization settings if this quiz allows randomized questions
_quiz_add_fields_for_random_quiz($form, $quiz);
// Build up a list of questions
$questions_to_add = array();
// We use $form_state[post] to avoid validation failures when questions are added using AJAX
if (isset($form_state['post']['weights'])) {
$questions = _quiz_get_questions_from_form_state($form_state, $questions_to_add);
}
else {
// We are coming in fresh and fetches the questions currently on the quiz from the database...
$include_random = $quiz->randomization == 2;
$questions = quiz_get_questions($quiz->nid, $quiz->vid, TRUE, FALSE, FALSE, $include_random);
}
if (empty($questions)) {
$form['question_list']['no_questions'] = array(
'#markup' => '<div id = "no-questions">' . t('There are currently no questions in this quiz. Assign existing questions by using the question browser below. You can also use the links above to create new questions.') . '</div>',
);
}
// We add the browser and allows the browser to give us information on what questions are displayed in the browser...
$hidden_questions = array();
$form['question_list']['browser'] = _quiz_question_browser_form($hidden_questions, $questions_to_add, $form_state, $quiz, $types);
// We add the questions from the browser as hidden question rows in the question list. Doing this we can have
// the question show up in the question list instantly when a question is chosen in the browser(using js).
_quiz_add_hidden_questions($questions, $hidden_questions, $form_state, $quiz);
// We add the questions to the form array
_quiz_add_questions_to_form($form, $questions, $quiz, $types);
// Show the number of questions in the table header.
$always_count = 0;
foreach ($form['question_list']['stayers'] as $stayer) {
if ($stayer['#default_value'] === 1) {
$always_count++;
}
}
$form['question_list']['#title'] .= ' (' . $always_count . ')';
// Give the user the option to create a new revision of the quiz
_quiz_add_revision_checkbox($form, $quiz);
// Timestamp is needed to avoid multiple users editing the same quiz at the same time.
$form['timestamp'] = array('#type' => 'hidden', '#default_value' => REQUEST_TIME);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('quiz_questions_form_submit'),
);
return $form;
}
/**
* Fields for creating new questions are added to the quiz_questions_form
*
* @param $form
* FAPI form(array)
* @param $types
* All the question types(array)
* @param $quiz
* The quiz node
*/
function _quiz_add_fields_for_creating_questions(&$form, &$types, &$quiz) {
// Display links to create other questions.
$form['additional_questions'] = array(
'#type' => 'fieldset',
'#title' => t('Create new question'),
'#collapsible' => TRUE,
Sivaji
committed
'#collapsed' => TRUE,
$url_query = drupal_get_destination();
$url_query['quiz_nid'] = $quiz->nid;
$url_query['quiz_vid'] = $quiz->vid;
foreach ($types as $type => $info) {
$url_type = str_replace('_', '-', $type);
$options = array(
'attributes' => array('title' => t('Create @name', array('@name' => $info['name']))),
'query' => $url_query,
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
);
$form['additional_questions'][$type] = array(
'#markup' => '<div class="add-questions">' . l($info['name'], "node/add/$url_type", $options) . '</div>',
);
}
}
/**
* Add fields for random quiz to the quiz_questions_form
*
* @param $form
* FAPI form array
* @param $quiz
* The quiz node(object)
*/
function _quiz_add_fields_for_random_quiz(&$form, $quiz) {
if ($quiz->randomization != 2) {
return;
}
$form['question_list']['random_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings for random questions'),
'#collapsible' => TRUE,
);
$form['question_list']['random_settings']['num_random_questions'] = array(
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#weight' => -5,
'#title' => t('Number of random questions'),
'#description' => t('The number of questions to be randomly selected each time someone takes this quiz'),
'#default_value' => isset($quiz->number_of_random_questions) ? $quiz->number_of_random_questions : 10,
);
$form['question_list']['random_settings']['max_score_for_random'] = array(
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#weight' => -5,
'#title' => t('Max score for each random question'),
'#default_value' => isset($quiz->max_score_for_random) ? $quiz->max_score_for_random : 1,
);
if ($quiz->randomization == 3) {
$terms = _quiz_taxonomy_select($quiz->tid);
if (!empty($terms) && function_exists('taxonomy_get_vocabularies')) {
$form['question_list']['random_settings']['random_term_id'] = array(
'#type' => 'select',
'#title' => t('Terms'),
'#size' => 1,
'#options' => _quiz_taxonomy_select($quiz->tid),
'#default_value' => $quiz->tid,
'#description' => t('Randomly select from questions with this term, or choose from the question pool below'),
'#weight' => -4,
);
}
}
}
/**
* Returns the questions that was in the question list when the form was submitted using ajax.
*
* @param $form_state
* FAPI form_state(array)
* @return $questions
* Array of questions as objects
*/
function _quiz_get_questions_from_form_state(&$form_state, &$questions_to_add) {
$questions = array();
// We first store all data from the post in a temporary array.
// Then we fetch more data for each question from the database.
$cur_questions = array();
$vids = array();
foreach ($form_state['post']['weights'] as $id => $value) {
$cur_question = new stdClass();
// Find nid and vid
$matches = array();
preg_match('/([0-9]+)-([0-9]+)/', $id, $matches);
$cur_question->nid = $matches[1];
if (!is_numeric($matches[2])) {
continue;
}
$vids[] = $cur_question->vid = $matches[2];
$cur_question->max_score = intval($form_state['post']['max_scores'][$id]);
$cur_question->weight = intval($value);
$cur_question->staying = $form_state['post']['stayers'][$id] === '1';
$cur_question->question_status = QUESTION_ALWAYS;
if ($cur_question->staying == TRUE) {
$questions_to_add[] = $id;
}
$cur_questions[$cur_question->nid] = $cur_question;
}
$query = db_select('node_revision', 'r');
$table_alias = $query->join('node', 'n', 'n.nid = r.nid');
$res = $query->addField('n', 'nid')
->addTag('node_access')
->addField('n', 'type')
->addField('n', 'vid', 'latest_vid')
->addField('r', 'title')
->condition('r.vid', $vids, 'IN')
->execute();
// TODO: Don't use db_fetch_object
while ($res_o = $res->fetch()) {
$cur_questions[$res_o->nid]->type = $res_o->type;
$cur_questions[$res_o->nid]->title = $res_o->title;
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$cur_questions[$res_o->nid]->latest_vid = $res_o->latest_vid;
$questions[] = $cur_questions[$res_o->nid];
}
return $questions;
}
/**
* Adds all information about the hidden questions to the questions array.
*
* Hidden questions are used to avoid unnecessary ajax calls.
*
* @see quiz_questions_form
*
* @param $questions
* The questions already added to the question list(array)
* @param $hidden_questions
* The questions added to the browser(array)
* @param $form_state
* FAPI form_state(array)
* @param $quiz
* The quiz node
*/
function _quiz_add_hidden_questions(&$questions, &$hidden_questions, &$form_state, &$quiz) {
$cur_questions = array();
$vids = array();
foreach ($hidden_questions as $key => $id) {
$cur_question = new stdClass();
$matches = array();
// Find nid and vid
preg_match('/([0-9]+)-([0-9]+)/', $id, $matches);
$nid = $matches[1];
$vid = $matches[2];
// If a question already exists in the $questions array we won't add a new one...
$continue = FALSE;
foreach ($questions as $question) {
if ($question->vid == $vid) {
$continue = TRUE;
break;
}
}
if (!is_numeric($nid) || !is_numeric($vid) || $continue) {
continue;
}
$cur_question->nid = $nid;
$vids[] = $cur_question->vid = $vid;