2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************/
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
17 // if a valid user, then can come from the DB, otherwise
18 // this might come from _POST or even _SESSION
19 function get_test_result_id($test_id, &$max_pos) {
22 if ($_SESSION['member_id']) {
23 $sql = "SELECT result_id, max_pos FROM ".TABLE_PREFIX."tests_results WHERE test_id=$test_id AND member_id='{$_SESSION['member_id']}' AND status=0";
24 } else if ($_SESSION['test_result_id']) {
25 // guest with on-going test
26 $sql = "SELECT result_id, max_pos FROM ".TABLE_PREFIX."tests_results WHERE test_id=$test_id AND result_id={$_SESSION['test_result_id']} AND status=0";
28 return 0; // new guest
31 $result = mysql_query($sql, $db);
32 if ($row = mysql_fetch_assoc($result)) {
33 $max_pos = $row['max_pos'];
34 return $row['result_id'];
40 function init_test_result_questions($test_id, $is_random, $num_questions, $mid) {
43 $sql = "INSERT INTO ".TABLE_PREFIX."tests_results VALUES (NULL, $test_id, '".$mid."', NOW(), '', 0, NOW(), 0)";
44 $result = mysql_query($sql, $db);
45 $result_id = mysql_insert_id($db);
48 // Retrieve 'num_questions' question_id randomly from those who are related to this test_id
50 $non_required_questions = array();
51 $required_questions = array();
53 $sql = "SELECT question_id, required FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$test_id";
54 $result = mysql_query($sql, $db);
56 while ($row = mysql_fetch_assoc($result)) {
57 if ($row['required'] == 1) {
58 $required_questions[] = $row['question_id'];
60 $non_required_questions[] = $row['question_id'];
64 $num_required = count($required_questions);
65 if ($num_required < max(1, $num_questions)) {
66 shuffle($non_required_questions);
67 $required_questions = array_merge($required_questions, array_slice($non_required_questions, 0, $num_questions - $num_required));
70 $random_id_string = implode(',', $required_questions);
72 $sql = "SELECT TQ.*, TQA.* FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQ.course_id={$_SESSION['course_id']} AND TQA.test_id=$test_id AND TQA.question_id IN ($random_id_string) ORDER BY TQ.question_id";
74 $sql = "SELECT TQ.*, TQA.* FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQ.course_id={$_SESSION['course_id']} AND TQA.test_id=$test_id ORDER BY TQA.ordering, TQA.question_id";
77 // $sql either gets a random set of questions (if $test_row['random']) ordered by 'question_id'
78 // or the set of all questions for this test (sorted by 'ordering').
79 $result = mysql_query($sql, $db);
80 while ($row = mysql_fetch_assoc($result)) {
81 $sql = "INSERT INTO ".TABLE_PREFIX."tests_answers VALUES ($result_id, {$row['question_id']}, {$_SESSION['member_id']}, '', '', '')";
82 mysql_query($sql, $db);
88 // $num_questions must be greater than or equal to $row_required['cnt'] + $row_optional['cnt']
89 function get_total_weight($tid, $num_questions = null) {
91 $sql = "SELECT SUM(weight) AS weight, COUNT(*) AS cnt FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$tid AND required = '1' GROUP BY required";
92 $result = mysql_query($sql, $db);
93 $row_required = mysql_fetch_assoc($result);
95 $sql = "SELECT SUM(weight) AS weight, COUNT(*) AS cnt FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$tid AND required = '0' GROUP BY required";
96 $result = mysql_query($sql, $db);
97 $row_optional = mysql_fetch_assoc($result);
101 if ($num_questions == null) {
102 $total_weight = $row_required['weight'] + $row_optional['weight'];
103 } else if ($row_optional['cnt'] > 0) {
104 $total_weight = $row_required['weight'] + ($row_optional['weight'] / $row_optional['cnt']) * min($num_questions - $row_required['cnt'], $row_optional['cnt']);
107 return $total_weight;
110 // returns T/F whether or not this member can view this test:
111 function authenticate_test($tid) {
112 if (authenticate(AT_PRIV_ADMIN, AT_PRIV_RETURN)) {
115 if (!$_SESSION['enroll']) {
120 $sql = "SELECT approved FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$_SESSION[member_id] AND course_id=$_SESSION[course_id] AND approved='y'";
121 $result = mysql_query($sql, $db);
122 if (!($row = mysql_fetch_assoc($result))) {
126 $sql = "SELECT group_id FROM ".TABLE_PREFIX."tests_groups WHERE test_id=$tid";
127 $result = mysql_query($sql, $db);
128 if (mysql_num_rows($result) == 0) {
129 // not limited to any group; everyone has access:
132 while ($row = mysql_fetch_assoc($result)) {
133 $sql = "SELECT * FROM ".TABLE_PREFIX."groups_members WHERE group_id=$row[group_id] AND member_id=$_SESSION[member_id]";
134 $result2 = mysql_query($sql, $db);
136 if ($row2 = mysql_fetch_assoc($result2)) {
141 //Check assistants privileges
142 $sql = "SELECT privileges FROM at_course_enrollment a WHERE member_id=$_SESSION[member_id] AND course_id=$_SESSION[course_id]";
143 $result = mysql_query($sql, $db);
145 list($privileges) = mysql_fetch_array($result);
146 if (query_bit($privileges, AT_PRIV_GROUPS) && query_bit($privileges, AT_PRIV_TESTS)){
155 function print_question_cats($cat_id = 0) {
159 echo '<option value="0"';
161 echo ' selected="selected"';
163 echo '>'._AT('cats_uncategorized').'</option>' . "\n";
165 $sql = 'SELECT * FROM '.TABLE_PREFIX.'tests_questions_categories WHERE course_id='.$_SESSION['course_id'].' ORDER BY title';
166 $result = mysql_query($sql, $db);
168 while ($row = mysql_fetch_array($result)) {
169 echo '<option value="'.$row['category_id'].'"';
170 if ($row['category_id'] == $cat_id) {
171 echo ' selected="selected"';
173 echo '>'.$row['title'].'</option>'."\n";
177 function print_VE ($area) {
179 <script type="text/javascript" language="javascript">
180 document.writeln('<a href="#" onclick="javascript:window.open(\'<?php echo AT_BASE_HREF; ?>mods/_standard/tests/form_editor.php?area=<?php echo $area; ?>\',\'newWin1\',\'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=640,height=480\'); return false;" style="cursor: pointer; text-decoration: none" ><?php echo _AT('use_visual_editor'); ?></a>');
184 //possibley add a <noscript> link to filemanager with target="_blank"
187 function get_random_outof($test_id, $result_id) {
191 $sql = 'SELECT SUM(Q.weight) AS weight FROM '.TABLE_PREFIX.'tests_questions_assoc Q, '.TABLE_PREFIX.'tests_answers A WHERE Q.test_id='.$test_id.' AND Q.question_id=A.question_id AND A.result_id='.$result_id;
193 $result = mysql_query($sql, $db);
195 if ($row = mysql_fetch_assoc($result)) {
196 return $row['weight'];
202 // return the next guest id
203 function get_next_guest_id()
207 $sql = "SELECT max(cast(substring(guest_id,3) as unsigned))+1 next_guest_id FROM ".TABLE_PREFIX."guests";
208 $result = mysql_query($sql, $db);
209 $row = mysql_fetch_assoc($result);
211 if ($row["next_guest_id"] == "") // first guest id
214 return "G_". $row["next_guest_id"];