remove old readme
[atutor.git] / docs / mods / _standard / tests / lib / test_result_functions.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
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 /****************************************************************/
13 // $Id$
14
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
16
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) {
20         global $db;
21
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";
27         } else {
28                 return 0; // new guest
29         }
30
31         $result = mysql_query($sql, $db);
32         if ($row = mysql_fetch_assoc($result)) {
33                 $max_pos = $row['max_pos'];
34                 return $row['result_id'];
35         }
36
37         return 0;
38 }
39
40 function init_test_result_questions($test_id, $is_random, $num_questions, $mid) {
41         global $db;
42
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);
46
47         if ($is_random) {
48                 // Retrieve 'num_questions' question_id randomly from those who are related to this test_id
49
50                 $non_required_questions = array();
51                 $required_questions     = array();
52
53                 $sql    = "SELECT question_id, required FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$test_id";
54                 $result = mysql_query($sql, $db);
55         
56                 while ($row = mysql_fetch_assoc($result)) {
57                         if ($row['required'] == 1) {
58                                 $required_questions[] = $row['question_id'];
59                         } else {
60                                 $non_required_questions[] = $row['question_id'];
61                         }
62                 }
63         
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));
68                 }
69
70                 $random_id_string = implode(',', $required_questions);
71
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";
73         } else {
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";
75         }
76
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);
83         }
84
85         return $result_id;
86 }
87
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) {
90         global $db;
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);
94
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);
98         
99         $total_weight = 0;
100
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']);
105         }
106
107         return $total_weight;
108 }
109
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)) {
113                 return TRUE;
114         }
115         if (!$_SESSION['enroll']) {
116                 return FALSE;
117         }
118         global $db;
119
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))) {
123                 return FALSE;
124         }
125
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:
130                 return TRUE;
131         }
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);
135
136                 if ($row2 = mysql_fetch_assoc($result2)) {
137                         return TRUE;
138                 }
139         }
140
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);
144         if ($result){
145                 list($privileges) = mysql_fetch_array($result);
146                 if (query_bit($privileges, AT_PRIV_GROUPS) && query_bit($privileges, AT_PRIV_TESTS)){
147                         return TRUE;
148                 }
149
150         }
151
152         return FALSE;
153 }
154
155 function print_question_cats($cat_id = 0) {     
156
157         global $db;
158
159         echo '<option value="0"';
160         if ($cat_id == 0) {
161                 echo ' selected="selected"';
162         }
163         echo '>'._AT('cats_uncategorized').'</option>' . "\n";
164
165         $sql    = 'SELECT * FROM '.TABLE_PREFIX.'tests_questions_categories WHERE course_id='.$_SESSION['course_id'].' ORDER BY title';
166         $result = mysql_query($sql, $db);
167
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"';
172                 }
173                 echo '>'.$row['title'].'</option>'."\n";
174         }
175 }
176
177 function print_VE ($area) {
178 ?>
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>');
181         </script>
182
183 <?php
184         //possibley add a <noscript> link to filemanager with target="_blank"
185 }
186
187 function get_random_outof($test_id, $result_id) {       
188         global $db;
189         $total = 0;
190
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;
192
193         $result = mysql_query($sql, $db);
194
195         if ($row = mysql_fetch_assoc($result)) {
196                 return $row['weight'];
197         }
198
199         return 0;
200 }
201
202 // return the next guest id
203 function get_next_guest_id()
204 {
205         global $db;
206         
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);
210
211         if ($row["next_guest_id"] == "")  // first guest id
212                 return "G_0";
213         else
214                 return "G_". $row["next_guest_id"];
215 }
216 ?>