PluginProbe
Watu Quiz / 3.4.6
Watu Quiz v3.4.6
3.4.8 trunk 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.5.3 3.4.6 3.4.7
watu / models / exam.php

exam.php in Watu Quiz 3.4.6, at models/exam.php

106 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 class WatuExam {
5 // keep the questions after submit in the same order they were show to the user
6 // and only the questions that were shown
7 function reorder_questions($questions, $orders) {
8 global $wpdb;
9 $new_questions = array();
10 $qids = array(0);
11 foreach($questions as $question) $qids[] = $question->ID;
12
13 // all answers in the quiz
14 $qids = array_map('intval', $qids);
15 $all_answers = $wpdb->get_results("SELECT ID,answer,correct, point, question_id
16 FROM ".WATU_ANSWERS." WHERE question_id IN (" . implode(',', $qids) . ")");
17 $new_answers = array();
18
19 // reorder the answers accordingly to POST info
20 if(!empty($_POST['answer_ids'])) {
21 foreach($_POST['answer_ids'] as $aorder) {
22 $aorder = intval($aorder);
23 foreach($all_answers as $answer) {
24 if($answer->ID == $aorder) $new_answers[] = $answer;
25 } // end foreach answer
26 } // end foreach ID from post
27 } // end reordering answers
28
29 // reorder the questions accordingly to POST info
30 foreach($orders as $order) {
31 foreach($questions as $question) {
32 // dump answers
33 $question_answers = array();
34 foreach($new_answers as $answer) {
35 if($question->ID == $answer->question_id) $question_answers[] = $answer;
36 }
37 $question->answers = $question_answers;
38 if($question->ID == $order) $new_questions[] = $question;
39 } // end foreach question
40 } // end foreach orders (means question IDs ordered as POST var)
41
42 $new_questions = apply_filters('watu-reorder-questions', $new_questions, $questions, $orders);
43 return $new_questions;
44 }
45
46 // create one demo quiz when user first installs the plugin
47 static function create_demo() {
48 global $wpdb;
49
50 // don't do this if the user has already created some quizzes (let's not disturb old users)
51 $num_quizzes = $wpdb->get_var("SELECT COUNT(ID) FROM ".WATU_EXAMS);
52 if($num_quizzes) {
53 update_option('watu_demo_quiz_created', '1', false);
54 return true;
55 }
56
57 $final_screen = __("<p>Congratulations - you have completed %%QUIZ_NAME%%.</p>\n\n<p>You scored %%POINTS%% points out of %%MAX-POINTS%% points total.</p>\n\n<p>Your obtained grade is <b>%%GRADE-TITLE%%</b></p><p>%%GRADE-DESCRIPTION%%</p>", 'watu');
58
59 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_EXAMS." SET
60 name=%s, description=%s, final_screen=%s, added_on = CURDATE(), show_answers=1",
61 __('Demo Quiz', 'watu'), __('This quiz is automatically created to help you get started. It will be created only once.','watu'),
62 $final_screen));
63 $quiz_id = $wpdb->insert_id;
64
65 // create 3 questions
66 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_QUESTIONS." SET
67 exam_id=%d, question=%s, answer_type=%s", $quiz_id, __("Select the correct answer:", 'watu'), 'radio'));
68 $qid = $wpdb->insert_id;
69 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." SET
70 question_id=%d, answer=%s, correct='0', point=0, sort_order=1",
71 $qid, __('The Earth is a star', 'watu')));
72 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." SET
73 question_id=%d, answer=%s, correct='0', point=0, sort_order=2",
74 $qid, __('The Sun is a planet', 'watu')));
75 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." SET
76 question_id=%d, answer=%s, correct='1', point=1, sort_order=3",
77 $qid, __('The Earth is a planet', 'watu')));
78
79 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_QUESTIONS." SET
80 exam_id=%d, question=%s, answer_type=%s", $quiz_id, __("Select all correct answers:", 'watu'), 'checkbox'));
81 $qid = $wpdb->insert_id;
82 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." SET
83 question_id=%d, answer=%s, correct='1', point=1, sort_order=1",
84 $qid, __('WordPress is open source software', 'watu')));
85 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." SET
86 question_id=%d, answer=%s, correct='0', point=-1, sort_order=2",
87 $qid, __('There are no quiz plugins for WordPress', 'watu')));
88 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." SET
89 question_id=%d, answer=%s, correct='1', point=1, sort_order=3",
90 $qid, __('WordPress can be used for a lot more than blogging', 'watu')));
91
92 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_QUESTIONS." SET
93 exam_id=%d, question=%s, answer_type=%s", $quiz_id, __("Do you have any comments about this quiz?", 'watu'), 'textarea'));
94
95 // now add 2 grades
96 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_GRADES." SET
97 exam_id=%d, gtitle=%s, gdescription=%s, gfrom=-1, gto=1",
98 $quiz_id, __('Failed', 'watu'), __('Sorry, you could not collect enough points to pass this quiz', 'watu')));
99 $wpdb->query($wpdb->prepare("INSERT INTO ".WATU_GRADES." SET
100 exam_id=%d, gtitle=%s, gdescription=%s, gfrom=2, gto=3",
101 $quiz_id, __('Passed', 'watu'), __('Congratulations, you passed!', 'watu')));
102
103 update_option('watu_demo_quiz_created', '1', false);
104 }
105 }
106