-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.module
4260 lines (3907 loc) · 136 KB
/
quiz.module
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
266
267
268
269
270
271
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
334
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
483
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
528
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
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
836
837
838
839
840
841
842
843
844
845
846
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
950
951
952
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
<?php
/**
* @file quiz.module
* Main file for the Quiz module.
*
* This module allows the creation of interactive quizzes for site visitors.
*/
/**
* Define question statuses...
*/
define('QUIZ_QUESTION_RANDOM', 0);
define('QUIZ_QUESTION_ALWAYS', 1);
define('QUIZ_QUESTION_NEVER', 2);
/**
* Quiz name.
*/
define('QUIZ_NAME', _quiz_get_quiz_name());
/**
* Define options for keeping results.
*/
define('QUIZ_KEEP_BEST', 0);
define('QUIZ_KEEP_LATEST', 1);
define('QUIZ_KEEP_ALL', 2);
/**
* Implements hook_autoload_info().
*
* @return string[]
*/
function quiz_autoload_info() {
return array(
// Classes
'Quiz' => 'includes/Quiz.class.inc',
'QuizController' => 'includes/QuizController.class.inc',
'QuizMetadataController' => 'includes/QuizMetadataController.class.inc',
'QuizQuestionController' => 'includes/QuizQuestionController.class.inc',
'QuizQuestionMetadataController' => 'includes/QuizQuestionMetadataController.class.inc',
'QuizQuestionRelationship' => 'includes/QuizQuestionRelationship.class.inc',
'QuizQuestionRelationshipController' => 'includes/QuizQuestionRelationshipController.class.inc',
'QuizQuestionRelationshipMetadataController' => 'includes/QuizQuestionRelationshipMetadataController.class.inc',
'QuizResult' => 'includes/QuizResult.class.inc',
'QuizResultAnswer' => 'includes/QuizResultAnswer.class.inc',
'QuizResultAnswerController' => 'includes/QuizResultAnswerController.class.inc',
'QuizResultAnswerMetadataController' => 'includes/QuizResultAnswerMetadataController.class.inc',
'QuizResultController' => 'includes/QuizResultController.class.inc',
'QuizResultMetadataController' => 'includes/QuizResultMetadataController.class.inc',
'quiz_views_handler_filter_quiz_question' => 'includes/views/handlers/quiz_views_handler_filter_quiz_question.inc',
'quiz_views_handler_filter_quiz_question_type' => 'includes/views/handlers/quiz_views_handler_filter_quiz_question_type.inc',
'quiz_views_handler_field_quiz_question_result_answer' => 'includes/views/handlers/quiz_views_handler_field_quiz_question_result_answer.inc',
// Tests
'QuizAccessTestCase' => 'tests/QuizAccessTestCase.test',
'QuizCreationTestCase' => 'tests/QuizCreationTestCase.test',
'QuizFeedbackTestCase' => 'tests/QuizFeedbackTestCase.test',
'QuizGradingTestCase' => 'tests/QuizGradingTestCase.test',
'QuizNavigationTestCase' => 'tests/QuizNavigationTestCase.test',
'QuizQuestionNumberingTestCase' => 'tests/QuizQuestionNumberingTestCase.test',
'QuizRandomTestCase' => 'tests/QuizRandomTestCase.test',
'QuizResultTestCase' => 'tests/QuizResultTestCase.test',
'QuizTakingTestCase' => 'tests/QuizTakingTestCase.test',
'QuizTestCase' => 'tests/QuizTestCase.test',
'QuizTimerTestCase' => 'tests/QuizTimerTestCase.test',
'QuizUpgradeTestCase' => 'tests/QuizUpgradeTestCase.test',
);
}
/**
* Implements hook_config_info().
*/
function quiz_config_info() {
$prefixes['quiz.settings'] = array(
'label' => t('Quizsettings'),
'group' => t('Quiz'),
);
return $prefixes;
}
/**
* Implements hook_views_api().
*/
function quiz_views_api() {
return array(
'api' => '3.0',
'path' => backdrop_get_path('module', 'quiz') . '/includes/views',
);
}
/** TODO: REPLACE CTOOLS DEPENDENCIES
* Implements hook_ctools_plugin_directory().
*
* Let the system know where our task and task_handler plugins are.
*/
function quiz_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'page_manager') {
return 'plugins/page_manager/' . $plugin_type;
}
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
/** TODO: REPLACE CTOOLS DEPENDENCIES
* Implements hook_ctools_plugin_api().
*/
function quiz_ctools_plugin_api($module, $api) {
if ($module == 'page_manager' && $api == 'pages_default') {
return array('version' => 1);
}
}
/**
* Implements hook_entity_info_alter().
*/
function quiz_entity_info_alter(&$info) {
// Add a new view mode for displaying the question when taking a quiz.
$info['node']['view modes'] += array(
'question' => array(
'label' => t('Question'),
'custom settings' => FALSE,
),
);
$info['quiz']['type']['result_type'] = 'quiz_result_type';
}
/**
* Implements hook_permission().
*/
function quiz_permission() {
return array(
// Configure quiz:
'administer quiz configuration' => array(
'title' => t('Administer quiz configuration'),
'description' => t('Control the various settings and behaviours of quiz'),
'restrict access' => TRUE,
),
// Managing quizzes:
'access quiz' => array(
'title' => t('Take quiz'),
'description' => t('Can access (take) all quizzes.'),
),
// Viewing results:
'view any quiz results' => array(
'title' => t('View any quiz results'),
'description' => t('Can view results for all quizzes and users.'),
),
'view own quiz results' => array(
'title' => t('View own quiz results'),
'description' => t('Quiz takers can view their own results, also when quiz is not passed.'),
),
'view results for own quiz' => array(
'title' => t('View results for own quiz'),
'description' => t('Quiz makers can view results for their own quizzes.'),
),
// Deleting results:
'delete any quiz results' => array(
'title' => t('Delete any quiz results'),
),
'delete results for own quiz' => array(
'title' => t('Delete own quiz results'),
),
// scoring:
'score any quiz' => array(
'title' => t('Score any quiz'),
),
'score own quiz' => array(
'title' => t('Score own quiz'),
),
'score taken quiz answer' => array(
'title' => t('score taken quiz answer'),
'description' => t('Allows attendee to score questions needing manual evaluation.'),
),
// Allow a quiz question to be viewed outside of a test.
'view quiz question outside of a quiz' => array(
'title' => t('View quiz question outside of a quiz'),
'description' => t('Questions can only be accessed through taking a quiz (not as individual nodes) unless this permission is given.'),
),
// Allow the user to see the correct answer, when viewed outside a quiz.
'view any quiz question correct response' => array(
'title' => t('View any quiz question correct response'),
'description' => t('Allow the user to see the correct answer, when viewed outside a quiz.'),
),
// Allows users to pick a name for their questions. Otherwise this is auto
// generated.
'edit question titles' => array(
'title' => t('Edit question titles'),
'description' => t('Questions automatically get a title based on the question text. This allows titles to be set manually.'),
),
// Control revisioning, only assign this permission to users who understand
// who permissions work. Note: If a quiz or question is changed and not
// revisioned you will also change existing result reports.
'manual quiz revisioning' => array(
'title' => t('Manual quiz revisioning'),
'description' => t('Quizzes are revisioned automatically each time they are changed. This allows you to do revisions manually.'),
),
'administer quiz result types' => array(
'title' => t('Administer quiz result types'),
'description' => t('Allow users to manage quiz result types and fields.'),
),
);
}
/**
* Implements hook_admin_paths().
*/
function quiz_admin_paths() {
return array(
'node/*/quiz' => TRUE,
'node/*/quiz/*' => TRUE,
);
}
/** TODO: ADAPT TO NO LONGER USE Role ID (rid)
* Helper function to determine if a user has access to the different results
* pages.
*
* @param $quiz
* The quiz node.
* @param null $quiz_result
* The result id of a result we are trying to access.
*
* @return bool
* TRUE if user has permission.
*/
function quiz_access_results($quiz, $quiz_result = NULL) {
global $user;
if ($quiz->type !== 'quiz') {
return FALSE;
}
// If rid is set we must make sure the result belongs to the quiz we are
// viewing results for.
if (!empty($quiz_result)) {
$res = db_query('SELECT qnr.nid, qnr.uid FROM {quiz_node_results} qnr WHERE result_id = :result_id', array(':result_id' => $quiz_result->result_id))->fetch();
if ($res && $res->nid != $quiz->nid) {
return FALSE;
}
}
if (user_access('score any quiz')) {
return TRUE;
}
if (user_access('view any quiz results')) {
return TRUE;
}
if (user_access('view results for own quiz') && $user->uid == $quiz->uid) {
return TRUE;
}
if (user_access('score taken quiz answer')) {
// Check if the taken user is seeing his result.
if (!empty($quiz_result) && $res && $res->uid == $user->uid) {
return TRUE;
}
}
}
/**
* Helper function to determine if a user has access to view their quiz results.
*
* @param object $quiz
* The Quiz node.
*
* @return bool|NULL
*/
function quiz_access_my_results($quiz) {
global $user;
if ($quiz->type !== 'quiz') {
return FALSE;
}
$answered = db_query('SELECT 1 FROM {quiz_node_results} WHERE nid = :nid AND uid = :uid AND is_evaluated = :is_evaluated', array(':nid' => $quiz->nid, ':uid' => $user->uid, ':is_evaluated' => 1))->fetchField();
if ($answered) {
return TRUE;
}
}
/**
* Helper function to determine if a user has access to view a specific quiz
* result.
*
* @param int $result_id
* Result id.
*
* @return bool
* True if access, false otherwise.
*/
function quiz_access_my_result($result_id) {
global $user;
// Check if the quiz taking has been completed.
if ($quiz_result = quiz_result_load($result_id)) {
$node = node_load($quiz_result->nid, $quiz_result->vid);
if (quiz_access_to_score($quiz_result) || ($quiz_result->time_end > 0 && $quiz_result->uid == $user->uid)) {
return TRUE;
}
}
return FALSE;
}
/**
* Helper function to determine if a user has access to score a quiz.
*
* @param QuizResult $quiz_result
*
* @return true|null
* Returns TRUE if the user has access, NULL otherwise.
*/
function quiz_access_to_score(QuizResult $quiz_result) {
global $user;
$quiz = node_load($quiz_result->nid);
if (user_access('score own quiz') && $quiz->uid == $user->uid) {
return TRUE;
}
if (user_access('score any quiz')) {
return TRUE;
}
if (user_access('score taken quiz answer') && $quiz_result->uid == $user->uid) {
return TRUE;
}
}
/**
* Helper function to check if the user has any of a given list of permissions.
*
* @param args
* Any number of permissions.
*
* @return true|null
* Returns TRUE if the user has access to any of the arguments given, Null
* otherwise.
*/
function quiz_access_multi_or() {
$perms = func_get_args();
foreach ($perms as $perm) {
if (user_access($perm)) {
return TRUE;
}
}
}
/**
* Implements hook_node_info().
*/
function quiz_node_info() {
return array(
'quiz' => array(
'name' => t('@quiz', array("@quiz" => QUIZ_NAME)),
'base' => 'quiz',
'description' => 'Create interactive quizzes for site visitors',
),
);
}
/**
* Implements hook_cron().
*/
function quiz_cron() {
$config = config('quiz.settings');
$result_ids = array();
// Remove old quiz results that haven't been finished.
$rm_time = $config->get('quiz_remove_partial_quiz_record');
// $time = 0 for never.
if ($rm_time) {
$res = db_select('quiz_node_results', 'qnr')
->fields('qnr', array('result_id'))
->condition('time_end', 0)
->where('(:request_time - time_start) > :remove_time', array(
':request_time' => REQUEST_TIME,
':remove_time' => $rm_time
))
->execute();
while ($result_id = $res->fetchField()) {
$result_ids[$result_id] = $result_id;
}
}
// Remove invalid quiz results.
$rm_time = $config->get('quiz_remove_invalid_quiz_record');
// $time = 0 for never.
if ($rm_time) {
$query = db_select('quiz_node_results', 'qnr');
$query->fields('qnr', array('result_id'));
$query->join('quiz_node_properties', 'qnp', 'qnr.vid = qnp.vid');
// If the user has a limited amount of takes we don't delete invalid
// results.
$query->condition('qnp.takes', 0, '=');
$query->condition('qnr.is_invalid', 1);
$query->condition('qnr.time_end', REQUEST_TIME - $rm_time, '<=');
$res = $query->execute();
while ($result_id = $res->fetchField()) {
$result_ids[$result_id] = $result_id;
}
}
entity_delete_multiple('quiz_result', $result_ids);
}
/**
* Implements hook_menu().
*/
function quiz_menu() {
// Take quiz.
$items['node/%quiz_menu/take'] = array(
'title' => 'Take',
'page callback' => 'quiz_take_page',
'page arguments' => array(1),
'access callback' => 'quiz_take_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
// Take question.
// @todo Thought - the 4th argument could be a "page" instead of a question
// number.
$items['node/%quiz_menu/take/%question_number'] = array(
'title' => 'Take',
'title callback' => 'quiz_title',
'title arguments' => array(1),
'page callback' => 'quiz_take_question',
'page arguments' => array(1, 3),
'access callback' => 'quiz_access_question',
'access arguments' => array(1, 3),
);
// Feedback.
$items['node/%quiz_menu/take/%question_number/feedback'] = array(
'title' => 'Feedback',
'title callback' => 'quiz_title',
'title arguments' => array(1),
'page callback' => 'backdrop_get_form',
'page arguments' => array('quiz_take_question_feedback_form', 1, 3),
'access callback' => 'quiz_question_feedback_access',
'access arguments' => array(1, 3),
);
// Admin pages.
$items['admin/quiz'] = array(
'title' => '@quiz',
'title arguments' => array('@quiz' => QUIZ_NAME),
'description' => 'View results, score answers, run reports and edit configurations.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer quiz configuration', 'score any quiz', 'score own quiz', 'view any quiz results', 'view results for own quiz'),
'access callback' => 'quiz_access_multi_or',
'type' => MENU_NORMAL_ITEM,
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
$items['admin/quiz/settings'] = array(
'title' => '@quiz settings',
'title arguments' => array('@quiz' => QUIZ_NAME),
'description' => 'Change settings for the all Quiz project modules.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer quiz configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
$items['admin/quiz/settings/config'] = array(
'title' => '@quiz configuration',
'title arguments' => array('@quiz' => QUIZ_NAME),
'description' => 'Configure the Quiz module.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('quiz_admin_settings'),
'access arguments' => array('administer quiz configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'quiz.admin.inc',
);
$items['admin/quiz/settings/quiz-form'] = array(
'title' => '@quiz form configuration',
'title arguments' => array('@quiz' => QUIZ_NAME),
'description' => 'Configure default values for the quiz creation form.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('quiz_admin_node_form'),
'access arguments' => array('administer quiz configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'quiz.admin.inc',
);
$items['admin/quiz/reports'] = array(
'title' => '@quiz reports and scoring',
'title arguments' => array('@quiz' => QUIZ_NAME),
'description' => 'View reports and score answers.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('view any quiz results', 'view results for own quiz'),
'access callback' => 'quiz_access_multi_or',
'type' => MENU_NORMAL_ITEM,
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
$items['admin/quiz/result_answer'] = array(
'title' => 'Quiz result answer',
'description' => 'Configure Quiz result answer behaviors',
'page callback' => 'quiz_result_answer_page',
'access arguments' => array('administer quiz configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items['node/%quiz_menu/quiz-results/%quiz_rid/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['node/%quiz_menu/quiz-results/%quiz_rid'] = array(
'title' => 'User results',
'page callback' => 'quiz_user_results',
'page arguments' => array(3),
'access callback' => 'quiz_access_my_result',
'access arguments' => array(3),
'file' => 'quiz.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['node/%quiz_menu/quiz/results/%quiz_result/view'] = array(
'title' => 'Results',
'page callback' => 'quiz_admin_results',
'page arguments' => array(1, 4),
'access callback' => 'quiz_access_results',
'access arguments' => array(1, 4),
'file' => 'quiz.admin.inc',
);
// Add questions to quiz.
$items['node/%quiz_menu/quiz/questions'] = array(
'title' => 'Manage questions',
'page callback' => 'quiz_questions_page',
'page arguments' => array(1),
'access callback' => 'quiz_type_confirm',
'access arguments' => array(1, 'update'),
'type' => MENU_LOCAL_TASK,
'file' => 'quiz.admin.inc',
'weight' => 2,
);
// User pages.
$items['user/%/quiz-results/%/view'] = array(
'title' => 'User results',
'page callback' => 'quiz_user_results',
'page arguments' => array(3),
'access arguments' => array(3),
'access callback' => 'quiz_access_my_result',
'type' => MENU_CALLBACK,
'file' => 'quiz.pages.inc',
);
// @todo: Add node access instead of user access...
$items['node/%quiz_menu/questions/term_ahah'] = array(
'page callback' => 'quiz_categorized_term_ahah',
'type' => MENU_CALLBACK,
'access callback' => 'node_access',
'access arguments' => array('create', 'quiz'),
'file' => 'quiz.admin.inc',
);
if (module_exists('devel_generate')) {
$items['admin/config/development/generate/quiz'] = array(
'title' => 'Generate quiz',
'description' => 'Generate a given number of quizzes and questions.',
'access arguments' => array('administer quiz configuration'),
'page callback' => 'backdrop_get_form',
'page arguments' => array('quiz_generate_form'),
'file' => 'quiz.devel.inc',
);
}
$items['admin/quiz/feedback'] = array(
'title' => 'Quiz feedback',
'description' => 'Configure Quiz feedback behaviors',
'page callback' => 'quiz_feedback_page',
'access arguments' => array('administer quiz configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'quiz.admin.inc',
);
if (module_exists('rules')) {
$items += rules_ui()->config_menu('admin/quiz/feedback');
}
return $items;
}
/**
* Title callback for quiz pages.
*/
function quiz_title($node) {
return $node->title;
}
/**
* Page for Quiz result answer configuration and fields.
*/
function quiz_result_answer_page() {
return t('Here, you can configure how Quiz result answers behave.');
}
/**
* Implements hook_theme().
*/
function quiz_theme($existing, $type, $theme, $path) {
return array(
'quiz_view_stats' => array(
'variables' => array('node' => NULL),
'file' => 'quiz.theme.inc',
),
'quiz_categorized_form' => array(
'render element' => 'form',
'file' => 'quiz.theme.inc',
),
'quiz_result' => array(
'variables' => array('quiz' => NULL, 'questions' => NULL, 'score' => NULL, 'summary' => NULL, 'result_id' => NULL),
'file' => 'quiz.theme.inc',
),
'quiz_progress' => array(
'variables' => array('question_number' => NULL, 'num_questions' => NULL, 'allow_jumping' => NULL, 'time_limit' => NULL),
'file' => 'quiz.theme.inc',
),
'question_selection_table' => array(
'file' => 'quiz.theme.inc',
'render element' => 'form',
),
'quiz_answer_result' => array(
'file' => 'quiz.theme.inc',
'variables' => array(),
),
'quiz_report_form' => array(
'render element' => 'form',
'path' => $path . '/theme',
'template' => 'quiz-report-form',
),
'quiz_question_score' => array(
'variables' => array('score' => NULL, 'max_score' => NULL),
'path' => $path . '/theme',
'template' => 'quiz-question-score',
),
'quiz_pager' => array(
'variables' => array('total' => 0, 'current' => 0, 'siblings' => 0),
),
'quiz_questions_page' => array(
'file' => 'quiz.theme.inc',
'render element' => 'form',
),
);
}
/**
* Implements hook_form_alter().
*
* Override settings in some existing forms. For example, we remove the
* preview button on a quiz.
*/
function quiz_form_alter(&$form, $form_state, $form_id) {
$config = config('quiz.settings');
// Set taking options the first vertical tab item.
if ('quiz_node_form' == $form_id && isset($form['menu']['#weight'])) {
$form['menu']['#weight'] = 1;
}
// Remove revision fieldset if user don't have access to revise quiz manually.
if (isset($form['#quiz_check_revision_access'])) {
if (!user_access('manual quiz revisioning') || $config->get('quiz_auto_revisioning')) {
$form['revision_information']['revision']['#type'] = 'value';
$form['revision_information']['revision']['#value'] = $form['revision_information']['revision']['#default_value'];
$form['revision_information']['log']['#type'] = 'value';
$form['revision_information']['log']['#value'] = $form['revision_information']['log']['#default_value'];
$form['revision_information']['#access'] = FALSE;
}
unset($form['actions']['preview'], $form['actions']['preview_changes']);
$form['actions']['submit']['#access'] = TRUE;
// Quiz questions might want to add a cancel button.
if (isset($form['#cancel_button'])) {
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), $form_state['redirect']),
'#weight' => 6,
);
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the quiz node form.
*/
function quiz_form_quiz_node_form_alter(&$form, $form_state) {
$form['actions']['submit']['#submit'][] = 'quiz_form_quiz_node_form_submit';
}
/**
* Quiz node form submit callback.
*
* Redirect the user to the questions form if there are no questions in the
* Quiz.
*
* @see quiz_form_quiz_node_form_alter()
*/
function quiz_form_quiz_node_form_submit($form, &$form_state) {
$node = $form_state['node'];
if (!entity_load('quiz_question_relationship', FALSE, array('parent_vid' => $node->vid))) {
backdrop_set_message(t('You just created a new @quiz. Now you have to add questions to it. This page is for adding and managing questions. Here you can create new questions or add some of your already created questions. If you want to change the quiz settings, you can use the "edit" tab.', array('@quiz' => QUIZ_NAME)));
$form_state['redirect'] = 'node/' . $node->nid . '/quiz/questions';
}
}
/**
* Implements hook_insert().
*/
function quiz_insert($node) {
// Need to set max_score if this is a cloned node.
$max_score = 0;
// Copy all the questions belonging to the quiz if this is a new translation.
if ($node->is_new && isset($node->translation_source)) {
quiz_copy_questions($node);
}
// Add references to all the questions belonging to the quiz if this is a
// cloned quiz (node_clone compatibility).
if ($node->is_new && isset($node->clone_from_original_nid)) {
$old_quiz = node_load($node->clone_from_original_nid);
$max_score = $old_quiz->max_score;
$questions = quiz_get_questions($old_quiz->nid, $old_quiz->vid);
quiz_set_questions($node, $questions);
}
_quiz_common_presave_actions($node);
// If the quiz is saved as not randomized we have to make sure that questions
// belonging to the quiz are saved as not random.
_quiz_check_num_random($node);
_quiz_check_num_always($node);
quiz_update_defaults($node);
_quiz_insert_resultoptions($node);
}
function quiz_update_defaults($node) {
global $user;
$config = config('quiz.settings');
$entity = clone $node;
if ($config->get('quiz_use_passfail')) {
$entity->summary_pass = is_array($node->summary_pass) ? $node->summary_pass['value'] : $node->summary_pass;
$entity->summary_pass_format = is_array($node->summary_pass) ? $node->summary_pass['format'] : $node->summary_pass_format;
}
$entity->summary_default = is_array($node->summary_default) ? $node->summary_default['value'] : $node->summary_default;
$entity->summary_default_format = is_array($node->summary_default) ? $node->summary_default['format'] : $node->summary_default_format;
// Save the node values.
$quiz_props = clone $entity;
$quiz_props->uid = 0;
quiz_save_properties($quiz_props);
if (!empty($node->remember_settings)) {
// Save user defaults.
$user_defaults = clone $quiz_props;
$user_defaults->nid = 0;
$user_defaults->vid = 0;
$user_defaults->uid = $user->uid;
quiz_save_properties($user_defaults);
}
if (!empty($node->remember_global)) {
// Save global defaults.
$global_defaults = clone $quiz_props;
$global_defaults->uid = 0;
$global_defaults->nid = 0;
$global_defaults->vid = 0;
quiz_save_properties($global_defaults);
}
}
/**
* Insert or update the quiz node properties accordingly.
*
* @param $entity
*/
function quiz_save_properties($entity) {
$sql = "SELECT qnp_id FROM {quiz_node_properties}
WHERE (nid = :nid AND nid > 0 AND vid = :vid AND vid > 0)
OR (uid = :uid and uid > 0)
OR (nid = :nid and uid = :uid and vid = :vid)";
$result = db_query($sql, array(':nid' => $entity->nid, ':uid' => $entity->uid, ':vid' => $entity->vid));
$entity->qnp_id = $result->fetchField();
entity_plus_save('quiz', $entity);
}
/**
* Implements hook_update().
*/
function quiz_update($node) {
// Quiz node vid (revision) was updated.
if (isset($node->revision) && $node->revision) {
// Create new quiz-question relation entries in the quiz_node_relationship
// table.
quiz_update_quiz_question_relationship($node->old_vid, $node->vid, $node->nid);
backdrop_set_message(t('Some of the updated settings may not apply to quiz being taken already. To see all changes in action you need to start again.'), 'warning');
}
// Update an existing row in the quiz_node_properties table.
_quiz_common_presave_actions($node);
quiz_update_defaults($node);
_quiz_update_resultoptions($node);
_quiz_check_num_random($node);
_quiz_check_num_always($node);
quiz_update_max_score_properties(array($node->vid));
}
/**
* Implements hook_field_extra_fields().
*/
function quiz_field_extra_fields() {
$extra = array();
$extra['node']['quiz'] = array(
'display' => array(
'take' => array(
'label' => t('Take @quiz button', array('@quiz' => QUIZ_NAME)),
'description' => t('The take button.'),
'weight' => 10,
),
'stats' => array(
'label' => t('@quiz summary', array('@quiz' => QUIZ_NAME)),
'description' => t('@quiz summary', array('@quiz' => QUIZ_NAME)),
'weight' => 9,
),
),
'form' => array(
'taking' => array(
'label' => t('Taking options'),
'description' => t('Fieldset for customizing how a @quiz is taken', array('@quiz' => QUIZ_NAME)),
'weight' => 0,
),
'quiz_availability' => array(
'label' => t('Availability options'),
'description' => t('Fieldset for customizing when a @quiz is available', array('@quiz' => QUIZ_NAME)),
'weight' => 0,
),
'summaryoptions' => array(
'label' => t('Summary options'),
'description' => t('Fieldset for customizing summaries in the @quiz reports', array('@quiz' => QUIZ_NAME)),
'weight' => 0,
),
'resultoptions' => array(
'label' => t('Result options'),
'description' => t('Fieldset for customizing result comments in @quiz reports', array('@quiz' => QUIZ_NAME)),
'weight' => 0,
),
'remember_settings' => array(
'label' => t('Remember settings'),
'description' => t('Checkbox for remembering @quiz settings', array('@quiz' => QUIZ_NAME)),
'weight' => 0,
),
'remember_global' => array(
'label' => t('Remember as global'),
'description' => t('Checkbox for remembering @quiz settings', array('@quiz' => QUIZ_NAME)),
'weight' => 0,
),
),
);
$extra['quiz_result_answer']['quiz_result_answer']['display']['table'] = array(
'label' => t('Feedback table'),
'description' => t('A table of feedback.'),
'weight' => 0,
);
$options = quiz_get_feedback_options();
foreach ($options as $option => $label) {
$extra['quiz_result_answer']['quiz_result_answer']['display'][$option] = array(
'label' => $label,
'description' => t('Feedback for @label.', array('@label' => $label)),
'weight' => 0,
);
}
$extra['quiz_result']['quiz_result']['display'] = array(
'score' => array(
'label' => t('Score'),
'description' => t('The score of the result.'),
'weight' => 1,
),
'questions' => array(
'label' => t('Questions'),
'description' => t('The questions in this result.'),
'weight' => 2,
),
'summary' => array(
'label' => t('Summary'),
'description' => t('The summary and pass/fail text.'),
'weight' => 3,
),
);
return $extra;
}
/**
* Common actions that need to be done before a quiz is inserted or updated.
*
* @param $node
* Quiz node.
*/
function _quiz_common_presave_actions(&$node) {
quiz_translate_form_date($node, 'quiz_open');
quiz_translate_form_date($node, 'quiz_close');
if (empty($node->pass_rate)) {
$node->pass_rate = 0;
}
if ($node->randomization < 2) {
$node->number_of_random_questions = 0;
}
}
/**
* Implements hook_delete().
*
* Delete Quiz data after its node is deleted.
*/
function quiz_delete($node) {
// Delete quiz results.
$res = db_query('SELECT result_id FROM {quiz_node_results}
WHERE nid = :nid', array(':nid' => $node->nid));
$result_ids = array();
while ($result_id = $res->fetchField()) {
$result_ids[] = $result_id;
}
entity_delete_multiple('quiz_result', $result_ids);
// Delete quiz entities.
$res = db_query('SELECT qnp_id FROM {quiz_node_properties}
WHERE nid = :nid', array(':nid' => $node->nid));
$qnp_ids = array();
while ($qnp_id = $res->fetchField()) {
$qnp_ids[] = $qnp_id;
}
entity_delete_multiple('quiz', $qnp_ids);
// Remove quiz node records from table quiz_node_relationship.
db_delete('quiz_node_relationship')
->condition('parent_nid', $node->nid)
->execute();
// Remove quiz node records from table quiz_node_result_options.
db_delete('quiz_node_result_options')
->condition('nid', $node->nid)
->execute();
}
/**
* Returns default values for all quiz settings.
*
* @todo also store this in the quiz_node_properties table
*
* @return
* Array of default values.
*/
function _quiz_get_node_defaults() {
return (object) array(
'allow_change' => 1,
'allow_change_blank' => 0,
'allow_jumping' => 0,
'allow_resume' => 1,
'allow_skipping' => 1,
'always_available' => TRUE,
'backwards_navigation' => 1,
'build_on_last' => '',
'keep_results' => 2,
'mark_doubtful' => 0,
'max_score' => 0,
'max_score_for_random' => 1,
'number_of_random_questions' => 0,
'pass_rate' => 75,
'quiz_always' => 1,
'quiz_close' => 0,
'quiz_open' => 0,
'randomization' => 0,
'repeat_until_correct' => 0,
'review_options' => array(
'question' => array(),
'end' => array(
'attempt' => 'attempt',
'choice' => 'choice',
'quiz_question_view_full' => 'quiz_question_view_full',
),