| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
class WatuQuestion { |
| 5 |
// calculate points, correctness and return the class |
| 6 |
static function calculate($question, $ans, $ansArr, $correct, $class) { |
| 7 |
$points = 0; |
| 8 |
|
| 9 |
if($question->answer_type != 'textarea') { |
| 10 |
if($ans->correct == 1 and !$question->is_survey) $class .= ' correct-answer'; |
| 11 |
if( in_array($ans->ID , $ansArr) ) $class .= ' user-answer'; |
| 12 |
if( in_array($ans->ID , $ansArr ) and $ans->correct == 1 and !$question->is_survey) $correct = true; |
| 13 |
if( in_array($ans->ID , $ansArr ) ) $points = $ans->point; |
| 14 |
} |
| 15 |
else { |
| 16 |
// textareas |
| 17 |
$user_answer = $ansArr[0]; |
| 18 |
|
| 19 |
if( strtolower(trim($ans->answer)) == strtolower(trim($user_answer)) ) { |
| 20 |
$class .= ' user-answer'; |
| 21 |
$points = $ans->point; |
| 22 |
} |
| 23 |
if( strtolower(trim($ans->answer)) == strtolower(trim($user_answer)) and $ans->correct == 1 and !$question->is_survey) { |
| 24 |
$correct = true; |
| 25 |
$class .= ' correct-answer'; |
| 26 |
} |
| 27 |
} // end working with textareas |
| 28 |
|
| 29 |
if($question->is_survey) $class = str_replace('user-answer', 'user-answer-survey', $class); |
| 30 |
|
| 31 |
return array($points, $correct, $class); |
| 32 |
} |
| 33 |
|
| 34 |
// figure out the maximum number of points the user can get on the question |
| 35 |
static function max_points($question, $all_answers) { |
| 36 |
$max_points = 0; |
| 37 |
|
| 38 |
// get only the answers of this question |
| 39 |
$q_answers = array(); |
| 40 |
foreach($all_answers as $answer) { |
| 41 |
if($answer->question_id == $question->ID) $q_answers[] = $answer; |
| 42 |
} |
| 43 |
|
| 44 |
if(!count($q_answers)) return 0; |
| 45 |
|
| 46 |
switch($question->answer_type) { |
| 47 |
case 'radio': |
| 48 |
case 'textarea': |
| 49 |
// get the answer with most points |
| 50 |
$max = 0; |
| 51 |
foreach($q_answers as $answer) { |
| 52 |
if($answer->point > $max) $max = $answer->point; |
| 53 |
} |
| 54 |
|
| 55 |
$max_points += $max; |
| 56 |
break; |
| 57 |
|
| 58 |
case 'checkbox': |
| 59 |
foreach($q_answers as $answer) { |
| 60 |
if($answer->point > 0) $max_points += $answer->point; |
| 61 |
} |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
return $max_points; |
| 66 |
} // end max_points |
| 67 |
|
| 68 |
// backward compatibility. In old versions sort order was not given |
| 69 |
// so we'll make sure all questions have correct one when loading the page |
| 70 |
static function fix_sort_order($questions) { |
| 71 |
global $wpdb; |
| 72 |
$ct = empty($questions[0]->sort_order) ? 0 : intval($questions[0]->sort_order); // get the sort order of the first question on the page. If empty for some reason, it will become 1 in the loop |
| 73 |
foreach($questions as $cnt => $question) { |
| 74 |
$ct++; |
| 75 |
if(@$question->sort_order != $ct) { |
| 76 |
$wpdb->query("UPDATE ".WATU_QUESTIONS." SET sort_order=$ct WHERE ID={$question->ID}"); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static function reorder($id, $exam_id, $dir) { |
| 82 |
global $wpdb; |
| 83 |
$id = intval($id); |
| 84 |
$exam_id = intval($exam_id); |
| 85 |
|
| 86 |
// select question |
| 87 |
$question=$wpdb->get_row($wpdb->prepare("SELECT * FROM ".WATU_QUESTIONS." WHERE ID=%d", $id)); |
| 88 |
|
| 89 |
if($dir=="up") { |
| 90 |
$new_order = $question->sort_order-1; |
| 91 |
if($new_order<0) $new_order=0; |
| 92 |
|
| 93 |
// shift others |
| 94 |
$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET sort_order=sort_order+1 |
| 95 |
WHERE ID!=%d AND sort_order=%d AND exam_id=%d", $id, $new_order, $exam_id)); |
| 96 |
} |
| 97 |
|
| 98 |
if($dir=="down") { |
| 99 |
$new_order = $question->sort_order+1; |
| 100 |
|
| 101 |
// shift others |
| 102 |
$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET sort_order=sort_order-1 |
| 103 |
WHERE ID!=%d AND sort_order=%d AND exam_id=%d", $id, $new_order, $exam_id)); |
| 104 |
} |
| 105 |
|
| 106 |
// change this one |
| 107 |
$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET sort_order=%d WHERE ID=%d", |
| 108 |
$new_order, $id)); |
| 109 |
} |
| 110 |
} |