| 1 |
<?php
|
| 2 |
|
| 3 |
// $Header: /cvsroot/tikiwiki/tiki/tiki-take_quiz.php,v 1.14.2.7 2005/11/24 21:23:04 sylvieg Exp $
|
| 4 |
|
| 5 |
// Copyright (c) 2002-2005, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
|
| 6 |
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
|
| 7 |
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
|
| 8 |
|
| 9 |
// Initialization
|
| 10 |
require_once ('tiki-setup.php');
|
| 11 |
|
| 12 |
include_once ('lib/quizzes/quizlib.php');
|
| 13 |
|
| 14 |
if ($feature_categories == 'y') {
|
| 15 |
global $categlib;
|
| 16 |
if (!is_object($categlib)) {
|
| 17 |
include_once('lib/categories/categlib.php');
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
if ($feature_quizzes != 'y') {
|
| 22 |
$smarty->assign('msg', tra("This feature is disabled").": feature_quizzes");
|
| 23 |
|
| 24 |
$smarty->display("error.tpl");
|
| 25 |
die;
|
| 26 |
}
|
| 27 |
|
| 28 |
if (!isset($_REQUEST["quizId"])) {
|
| 29 |
$smarty->assign('msg', tra("No quiz indicated"));
|
| 30 |
|
| 31 |
$smarty->display("error.tpl");
|
| 32 |
die;
|
| 33 |
}
|
| 34 |
|
| 35 |
$smarty->assign('individual', 'n');
|
| 36 |
|
| 37 |
if ($userlib->object_has_one_permission($_REQUEST["quizId"], 'quiz')) {
|
| 38 |
$smarty->assign('individual', 'y');
|
| 39 |
|
| 40 |
if ($tiki_p_admin != 'y') {
|
| 41 |
$perms = $userlib->get_permissions(0, -1, 'permName_desc', '', 'quizzes');
|
| 42 |
|
| 43 |
foreach ($perms["data"] as $perm) {
|
| 44 |
$permName = $perm["permName"];
|
| 45 |
|
| 46 |
if ($userlib->object_has_permission($user, $_REQUEST["quizId"], 'quiz', $permName)) {
|
| 47 |
$$permName = 'y';
|
| 48 |
|
| 49 |
$smarty->assign("$permName", 'y');
|
| 50 |
} else {
|
| 51 |
$$permName = 'n';
|
| 52 |
|
| 53 |
$smarty->assign("$permName", 'n');
|
| 54 |
}
|
| 55 |
}
|
| 56 |
}
|
| 57 |
} elseif ($tiki_p_admin != 'y' && $feature_categories == 'y') {
|
| 58 |
$perms_array = $categlib->get_object_categories_perms($user, 'quiz', $_REQUEST['quizId']);
|
| 59 |
if ($perms_array) {
|
| 60 |
$is_categorized = TRUE;
|
| 61 |
foreach ($perms_array as $perm => $value) {
|
| 62 |
$$perm = $value;
|
| 63 |
}
|
| 64 |
} else {
|
| 65 |
$is_categorized = FALSE;
|
| 66 |
}
|
| 67 |
if ($is_categorized && isset($tiki_p_view_categories) && $tiki_p_view_categories != 'y') {
|
| 68 |
if (!isset($user)){
|
| 69 |
$smarty->assign('msg',$smarty->fetch('modules/mod-login_box.tpl'));
|
| 70 |
$smarty->assign('errortitle',tra("Please login"));
|
| 71 |
} else {
|
| 72 |
$smarty->assign('msg',tra("Permission denied you cannot view this page"));
|
| 73 |
}
|
| 74 |
$smarty->display("error.tpl");
|
| 75 |
die;
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
$smarty->assign('quizId', $_REQUEST["quizId"]);
|
| 80 |
$quiz_info = $quizlib->get_quiz($_REQUEST["quizId"]);
|
| 81 |
|
| 82 |
if ($tiki_p_take_quiz != 'y') {
|
| 83 |
$smarty->assign('msg', tra("You do not have permission to use this feature"));
|
| 84 |
|
| 85 |
$smarty->display("error.tpl");
|
| 86 |
die;
|
| 87 |
}
|
| 88 |
|
| 89 |
if ($user) {
|
| 90 |
// If the quiz cannot be repeated
|
| 91 |
if ($quiz_info["canRepeat"] == 'n') {
|
| 92 |
// Check if user has taken this quiz
|
| 93 |
if ($quizlib->user_has_taken_quiz($user, $_REQUEST["quizId"])) {
|
| 94 |
$smarty->assign('msg', tra("You cannot take this quiz twice"));
|
| 95 |
|
| 96 |
$smarty->display("error.tpl");
|
| 97 |
die;
|
| 98 |
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
$smarty->assign('ans', 'n');
|
| 103 |
|
| 104 |
if (isset($_REQUEST["timeleft"])) {
|
| 105 |
$smarty->assign('ans', 'y');
|
| 106 |
|
| 107 |
$_SESSION["finishQuiz"] = date("U");
|
| 108 |
$elapsed = $_SESSION["finishQuiz"] - $_SESSION["startQuiz"];
|
| 109 |
|
| 110 |
if ($user) {
|
| 111 |
// If the quiz cannot be repeated
|
| 112 |
if ($quiz_info["canRepeat"] == 'n') {
|
| 113 |
// Check if user has taken this quiz
|
| 114 |
if ($quizlib->user_has_taken_quiz($user, $_REQUEST["quizId"])) {
|
| 115 |
$smarty->assign('msg', tra("You cannot take this quiz twice"));
|
| 116 |
|
| 117 |
$smarty->display("error.tpl");
|
| 118 |
die;
|
| 119 |
} else {
|
| 120 |
$quizlib->user_takes_quiz($user, $_REQUEST["quizId"]);
|
| 121 |
}
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
// Now get the quiz information
|
| 126 |
// Verify time limit if appropiate
|
| 127 |
if ($quiz_info["timeLimited"] == 'y') {
|
| 128 |
if ($elapsed > $quiz_info["timeLimit"] * 60) {
|
| 129 |
$smarty->assign('msg', tra("Quiz time limit exceeded quiz cannot be computed"));
|
| 130 |
|
| 131 |
$smarty->display("error.tpl");
|
| 132 |
die;
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
// Now for each quiz question verify the points the user did get
|
| 137 |
$questions = $quizlib->list_quiz_questions($_REQUEST["quizId"], 0, -1, 'position_asc', '');
|
| 138 |
$points = 0;
|
| 139 |
$max = 0;
|
| 140 |
|
| 141 |
$temp_max = count($questions["data"]);
|
| 142 |
for ($i = 0; $i < $temp_max; $i++) {
|
| 143 |
$options = $quizlib->list_quiz_question_options($questions["data"][$i]["questionId"], 0, -1, 'optionText_asc', '');
|
| 144 |
|
| 145 |
$qid = $questions["data"][$i]["questionId"];
|
| 146 |
$max += $questions["data"][$i]["maxPoints"];
|
| 147 |
|
| 148 |
if (isset($_REQUEST["question_$qid"])) {
|
| 149 |
check_ticket('take-quiz');
|
| 150 |
$opt = $quizlib->get_quiz_question_option($_REQUEST["question_$qid"]);
|
| 151 |
|
| 152 |
$points += $opt["points"];
|
| 153 |
// Register the answer for quiz stats
|
| 154 |
$quizlib->register_quiz_answer($_REQUEST["quizId"], $qid, $_REQUEST["question_$qid"]);
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
$result = $quizlib->calculate_quiz_result($_REQUEST["quizId"], $points);
|
| 159 |
// register the result for quiz stats
|
| 160 |
$userResultId = $quizlib->register_quiz_stats($_REQUEST["quizId"], $user, $elapsed, $points, $max, $result["resultId"]);
|
| 161 |
$smarty->assign_by_ref('result', $result);
|
| 162 |
|
| 163 |
if ($quiz_info["storeResults"] == 'y') {
|
| 164 |
$temp_max = count($questions["data"]);
|
| 165 |
for ($i = 0; $i < $temp_max; $i++) {
|
| 166 |
$options = $quizlib->list_quiz_question_options($questions["data"][$i]["questionId"], 0, -1, 'optionText_asc', '');
|
| 167 |
|
| 168 |
$qid = $questions["data"][$i]["questionId"];
|
| 169 |
|
| 170 |
if (isset($_REQUEST["question_$qid"])) {
|
| 171 |
check_ticket('take-quiz');
|
| 172 |
$quizlib->register_user_quiz_answer($userResultId, $_REQUEST["quizId"], $qid, $_REQUEST["question_$qid"]);
|
| 173 |
}
|
| 174 |
|
| 175 |
// TAKE CARE OF FILE UPLOADS FOR QUESTIONS
|
| 176 |
if(isset($_FILES["question_upload_$qid"]) && ($tmp_file = $_FILES["question_upload_$qid"]['tmp_name'])) {
|
| 177 |
$filename = $_FILES["question_upload_$qid"]['name'];
|
| 178 |
$filetype = $_FILES["question_upload_$qid"]['type'];
|
| 179 |
$filesize = $_FILES["question_upload_$qid"]['size'];
|
| 180 |
|
| 181 |
$binFile = $_FILES["question_upload_$qid"]['tmp_name'];
|
| 182 |
$data = addslashes(fread(fopen($binFile, "r"), filesize($binFile)));
|
| 183 |
$quizlib->register_user_quiz_answer_upload($userResultId, $qid, $filename, $filetype, $filesize, $tmp_file);
|
| 184 |
|
| 185 |
}
|
| 186 |
|
| 187 |
}
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
}
|
| 192 |
|
| 193 |
//print("points: $points over $max<br />");
|
| 194 |
} else {
|
| 195 |
$_SESSION["startQuiz"] = date("U");
|
| 196 |
}
|
| 197 |
|
| 198 |
$quiz_info["timeLimitsec"] = $quiz_info["timeLimit"] * 60;
|
| 199 |
$smarty->assign('quiz_info', $quiz_info);
|
| 200 |
|
| 201 |
$questions = $quizlib->list_quiz_questions($_REQUEST["quizId"], 0, -1, 'position_asc', '');
|
| 202 |
|
| 203 |
$temp_max = count($questions["data"]);
|
| 204 |
for ($i = 0; $i < $temp_max; $i++) {
|
| 205 |
$options = $quizlib->list_quiz_question_options($questions["data"][$i]["questionId"], 0, -1, 'optionText_asc', '');
|
| 206 |
|
| 207 |
$questions["data"][$i]["options"] = $options["data"];
|
| 208 |
}
|
| 209 |
|
| 210 |
$smarty->assign_by_ref('questions', $questions["data"]);
|
| 211 |
|
| 212 |
$section = 'quizzes';
|
| 213 |
include_once ('tiki-section_options.php');
|
| 214 |
|
| 215 |
if ($feature_theme_control == 'y') {
|
| 216 |
$cat_type = 'quiz';
|
| 217 |
|
| 218 |
$cat_objid = $_REQUEST["quizId"];
|
| 219 |
include ('tiki-tc.php');
|
| 220 |
}
|
| 221 |
|
| 222 |
ask_ticket('take-quiz');
|
| 223 |
|
| 224 |
// Display the template
|
| 225 |
$smarty->assign('mid', 'tiki-take_quiz.tpl');
|
| 226 |
$smarty->display("tiki.tpl");
|
| 227 |
|
| 228 |
?>
|