tce_edit_test.php 52.4 KB
Newer Older
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
1
2
3
4
<?php
//============================================================+
// File name   : tce_edit_test.php
// Begin       : 2004-04-27
tecnickcom's avatar
tecnickcom committed
5
// Last Update : 2013-08-23
Nick's avatar
Nick committed
6
//
Nick's avatar
Nick committed
7
// Description : Edit Tests
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
8
9
10
11
12
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
Nick's avatar
Nick committed
13
//               Tecnick.com LTD
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
14
15
16
//               www.tecnick.com
//               info@tecnick.com
//
nick's avatar
nick committed
17
// License:
Nicola Asuni's avatar
Nicola Asuni committed
18
//    Copyright (C) 2004-2013 Nicola Asuni - Tecnick.com LTD
nick's avatar
nick committed
19
//    See LICENSE.TXT file for more information.
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
20
21
22
//============================================================+

/**
nick's avatar
nick committed
23
 * @file
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
24
25
26
27
28
29
30
31
32
33
34
 * Edit test.
 * @package com.tecnick.tcexam.admin
 * @author Nicola Asuni
 * @since 2004-04-27
 */

require_once('../config/tce_config.php');

$pagelevel = K_AUTH_ADMIN_TESTS;
require_once('../../shared/code/tce_authorization.php');

nick's avatar
nick committed
35
$thispage_title = $l['t_tests_editor'];
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
36
$enable_calendar = true;
nick's avatar
nick committed
37
require_once('tce_page_header.php');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
38
39
require_once('../../shared/code/tce_functions_form.php');
require_once('../../shared/code/tce_functions_tcecode.php');
nick's avatar
nick committed
40
require_once('tce_functions_tcecode_editor.php');
Nicola Asuni's avatar
Nicola Asuni committed
41
require_once('../../shared/code/tce_functions_auth_sql.php');
nick's avatar
nick committed
42
require_once('tce_functions_user_select.php');
tecnickcom's avatar
tecnickcom committed
43
require_once('tce_functions_test_select.php');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
44
45

// set default values
tecnickcom's avatar
tecnickcom committed
46
if (!isset($_REQUEST['test_results_to_users']) OR (empty($_REQUEST['test_results_to_users']))) {
Nicola Asuni's avatar
Nicola Asuni committed
47
	$test_results_to_users = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
48
} else {
tecnickcom's avatar
tecnickcom committed
49
	$test_results_to_users = F_getBoolean($_REQUEST['test_results_to_users']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
50
}
tecnickcom's avatar
tecnickcom committed
51
if (!isset($_REQUEST['test_report_to_users']) OR (empty($_REQUEST['test_report_to_users']))) {
Nicola Asuni's avatar
Nicola Asuni committed
52
	$test_report_to_users = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
53
} else {
tecnickcom's avatar
tecnickcom committed
54
	$test_report_to_users = F_getBoolean($_REQUEST['test_report_to_users']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
55
}
tecnickcom's avatar
tecnickcom committed
56
if (!isset($_REQUEST['subject_id']) OR (empty($_REQUEST['subject_id']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
57
	$subject_id = Array();
tecnickcom's avatar
tecnickcom committed
58
59
} else {
	$subject_id = $_REQUEST['subject_id'];
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
60
}
tecnickcom's avatar
tecnickcom committed
61
if (!isset($_REQUEST['tsubset_type']) OR (empty($_REQUEST['tsubset_type']))) {
nick's avatar
nick committed
62
	$tsubset_type = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
63
} else {
tecnickcom's avatar
tecnickcom committed
64
	$tsubset_type = intval($_REQUEST['tsubset_type']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
65
}
tecnickcom's avatar
tecnickcom committed
66
if (!isset($_REQUEST['tsubset_difficulty'])) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
67
68
	$tsubset_difficulty = 1;
} else {
tecnickcom's avatar
tecnickcom committed
69
	$tsubset_difficulty = intval($_REQUEST['tsubset_difficulty']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
70
}
tecnickcom's avatar
tecnickcom committed
71
if (!isset($_REQUEST['tsubset_quantity']) OR (empty($_REQUEST['tsubset_quantity']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
72
73
	$tsubset_quantity = 1;
} else {
tecnickcom's avatar
tecnickcom committed
74
	$tsubset_quantity = intval($_REQUEST['tsubset_quantity']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
75
}
tecnickcom's avatar
tecnickcom committed
76
if (!isset($_REQUEST['tsubset_answers']) OR (empty($_REQUEST['tsubset_answers']))) {
77
	$tsubset_answers = 4;	//default value for form
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
78
} else {
tecnickcom's avatar
tecnickcom committed
79
	$tsubset_answers = intval($_REQUEST['tsubset_answers']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
80
}
tecnickcom's avatar
tecnickcom committed
81
82
if (isset($_REQUEST['tsubset_id'])) {
	$tsubset_id = intval($_REQUEST['tsubset_id']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
83
}
tecnickcom's avatar
tecnickcom committed
84
85
if (isset($_REQUEST['test_duration_time'])) {
	$test_duration_time = intval($_REQUEST['test_duration_time']);
nick's avatar
nick committed
86
}
tecnickcom's avatar
tecnickcom committed
87
88
if (isset($_REQUEST['group_id'])) {
	$group_id = intval($_REQUEST['group_id']);
nick's avatar
nick committed
89
}
tecnickcom's avatar
tecnickcom committed
90
if (!isset($_REQUEST['test_score_right']) OR (empty($_REQUEST['test_score_right']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
91
92
	$test_score_right = 0;
} else {
tecnickcom's avatar
tecnickcom committed
93
	$test_score_right = floatval($_REQUEST['test_score_right']);
nick's avatar
nick committed
94
}
tecnickcom's avatar
tecnickcom committed
95
if (!isset($_REQUEST['test_score_wrong']) OR (empty($_REQUEST['test_score_wrong']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
96
97
	$test_score_wrong = 0;
} else {
tecnickcom's avatar
tecnickcom committed
98
	$test_score_wrong = floatval($_REQUEST['test_score_wrong']);
nick's avatar
nick committed
99
}
tecnickcom's avatar
tecnickcom committed
100
if (!isset($_REQUEST['test_score_unanswered']) OR (empty($_REQUEST['test_score_unanswered']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
101
102
	$test_score_unanswered = 0;
} else {
tecnickcom's avatar
tecnickcom committed
103
	$test_score_unanswered = floatval($_REQUEST['test_score_unanswered']);
nick's avatar
nick committed
104
}
tecnickcom's avatar
tecnickcom committed
105
if (!isset($_REQUEST['test_score_threshold']) OR (empty($_REQUEST['test_score_threshold']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
106
107
	$test_score_threshold = 0;
} else {
tecnickcom's avatar
tecnickcom committed
108
	$test_score_threshold = floatval($_REQUEST['test_score_threshold']);
nick's avatar
nick committed
109
}
tecnickcom's avatar
tecnickcom committed
110
if (!isset($_REQUEST['test_random_questions_select']) OR (empty($_REQUEST['test_random_questions_select']))) {
Nicola Asuni's avatar
Nicola Asuni committed
111
	$test_random_questions_select = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
112
} else {
tecnickcom's avatar
tecnickcom committed
113
	$test_random_questions_select = F_getBoolean($_REQUEST['test_random_questions_select']);
nick's avatar
nick committed
114
}
tecnickcom's avatar
tecnickcom committed
115
if (!isset($_REQUEST['test_random_questions_order']) OR (empty($_REQUEST['test_random_questions_order']))) {
Nicola Asuni's avatar
Nicola Asuni committed
116
	$test_random_questions_order = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
117
} else {
tecnickcom's avatar
tecnickcom committed
118
	$test_random_questions_order = F_getBoolean($_REQUEST['test_random_questions_order']);
Nicola Asuni's avatar
Nicola Asuni committed
119
}
tecnickcom's avatar
tecnickcom committed
120
if (!isset($_REQUEST['test_questions_order_mode']) OR (empty($_REQUEST['test_questions_order_mode']))) {
Nicola Asuni's avatar
Nicola Asuni committed
121
122
	$test_questions_order_mode = 0;
} else {
123
	$test_questions_order_mode = max(0, min(4, intval($_REQUEST['test_questions_order_mode'])));	// 4 -> ORDER BY question_subject_id
nick's avatar
nick committed
124
}
tecnickcom's avatar
tecnickcom committed
125
if (!isset($_REQUEST['test_random_answers_select']) OR (empty($_REQUEST['test_random_answers_select']))) {
Nicola Asuni's avatar
Nicola Asuni committed
126
	$test_random_answers_select = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
127
} else {
tecnickcom's avatar
tecnickcom committed
128
	$test_random_answers_select = F_getBoolean($_REQUEST['test_random_answers_select']);
nick's avatar
nick committed
129
}
tecnickcom's avatar
tecnickcom committed
130
if (!isset($_REQUEST['test_random_answers_order']) OR (empty($_REQUEST['test_random_answers_order']))) {
Nicola Asuni's avatar
Nicola Asuni committed
131
	$test_random_answers_order = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
132
} else {
tecnickcom's avatar
tecnickcom committed
133
	$test_random_answers_order = F_getBoolean($_REQUEST['test_random_answers_order']);
Nicola Asuni's avatar
Nicola Asuni committed
134
}
tecnickcom's avatar
tecnickcom committed
135
if (!isset($_REQUEST['test_answers_order_mode']) OR (empty($_REQUEST['test_answers_order_mode']))) {
Nicola Asuni's avatar
Nicola Asuni committed
136
137
	$test_answers_order_mode = 0;
} else {
tecnickcom's avatar
tecnickcom committed
138
	$test_answers_order_mode = max(0, min(2, intval($_REQUEST['test_answers_order_mode'])));
nick's avatar
nick committed
139
}
tecnickcom's avatar
tecnickcom committed
140
if (!isset($_REQUEST['test_comment_enabled']) OR (empty($_REQUEST['test_comment_enabled']))) {
Nicola Asuni's avatar
Nicola Asuni committed
141
	$test_comment_enabled = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
142
} else {
tecnickcom's avatar
tecnickcom committed
143
	$test_comment_enabled = F_getBoolean($_REQUEST['test_comment_enabled']);
nick's avatar
nick committed
144
}
tecnickcom's avatar
tecnickcom committed
145
if (!isset($_REQUEST['test_menu_enabled']) OR (empty($_REQUEST['test_menu_enabled']))) {
Nicola Asuni's avatar
Nicola Asuni committed
146
	$test_menu_enabled = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
147
} else {
tecnickcom's avatar
tecnickcom committed
148
	$test_menu_enabled = F_getBoolean($_REQUEST['test_menu_enabled']);
nick's avatar
nick committed
149
}
tecnickcom's avatar
tecnickcom committed
150
if (!isset($_REQUEST['test_noanswer_enabled']) OR (empty($_REQUEST['test_noanswer_enabled']))) {
Nicola Asuni's avatar
Nicola Asuni committed
151
	$test_noanswer_enabled = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
152
} else {
tecnickcom's avatar
tecnickcom committed
153
	$test_noanswer_enabled = F_getBoolean($_REQUEST['test_noanswer_enabled']);
nick's avatar
nick committed
154
}
tecnickcom's avatar
tecnickcom committed
155
if (!isset($_REQUEST['test_mcma_radio']) OR (empty($_REQUEST['test_mcma_radio']))) {
Nicola Asuni's avatar
Nicola Asuni committed
156
	$test_mcma_radio = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
157
} else {
tecnickcom's avatar
tecnickcom committed
158
	$test_mcma_radio = F_getBoolean($_REQUEST['test_mcma_radio']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
159
}
tecnickcom's avatar
tecnickcom committed
160
if (!isset($_REQUEST['test_repeatable']) OR (empty($_REQUEST['test_repeatable']))) {
Nicola Asuni's avatar
Nicola Asuni committed
161
	$test_repeatable = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
162
} else {
tecnickcom's avatar
tecnickcom committed
163
	$test_repeatable = F_getBoolean($_REQUEST['test_repeatable']);
nick's avatar
nick committed
164
}
tecnickcom's avatar
tecnickcom committed
165
if (!isset($_REQUEST['test_mcma_partial_score']) OR (empty($_REQUEST['test_mcma_partial_score']))) {
Nicola Asuni's avatar
Nicola Asuni committed
166
	$test_mcma_partial_score = false;
nick's avatar
nick committed
167
} else {
tecnickcom's avatar
tecnickcom committed
168
	$test_mcma_partial_score = F_getBoolean($_REQUEST['test_mcma_partial_score']);
nick's avatar
nick committed
169
}
tecnickcom's avatar
tecnickcom committed
170
if (!isset($_REQUEST['test_logout_on_timeout']) OR (empty($_REQUEST['test_logout_on_timeout']))) {
Nicola Asuni's avatar
Nicola Asuni committed
171
	$test_logout_on_timeout = false;
nick's avatar
nick committed
172
} else {
tecnickcom's avatar
tecnickcom committed
173
	$test_logout_on_timeout = F_getBoolean($_REQUEST['test_logout_on_timeout']);
nick's avatar
nick committed
174
}
tecnickcom's avatar
tecnickcom committed
175
if (!isset($_REQUEST['test_max_score'])) {
nick's avatar
nick committed
176
	$test_max_score = 0;
tecnickcom's avatar
tecnickcom committed
177
178
} else {
	$test_max_score = floatval($_REQUEST['test_max_score']);
nick's avatar
nick committed
179
180
}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
181
$test_max_score_new = 0; // test max score
nick's avatar
nick committed
182
$qtype = array('S', 'M', 'T', 'O'); // question types
tecnickcom's avatar
tecnickcom committed
183
$qordmode = array($l['w_position'], $l['w_alphabetic'], $l['w_id'], $l['w_type'], $l['w_subject']);
Nicola Asuni's avatar
Nicola Asuni committed
184
$aordmode = array($l['w_position'], $l['w_alphabetic'], $l['w_id']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
185
186
187
188
189
190
191
192
193
194

$test_fieldset_name = '';

if (isset($_REQUEST['test_id']) AND ($_REQUEST['test_id'] > 0)) {
	$test_id = intval($_REQUEST['test_id']);
	// check user's authorization
	if (!F_isAuthorizedUser(K_TABLE_TESTS, 'test_id', $test_id, 'test_user_id')) {
		F_print_error('ERROR', $l['m_authorization_denied']);
		exit;
	}
Nick's avatar
Nick committed
195
196
} else {
	$test_id = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
197
198
}

Nick's avatar
Nick committed
199
if (isset($_POST['lock'])) {
nick's avatar
nick committed
200
	$menu_mode = 'lock';
Nick's avatar
Nick committed
201
} elseif (isset($_POST['unlock'])) {
nick's avatar
nick committed
202
203
204
	$menu_mode = 'unlock';
}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
205
206
switch($menu_mode) {

nick's avatar
nick committed
207
208
	case 'lock':{ // lock test by changing end date (subtract 1000 years)
		$sql = 'UPDATE '.K_TABLE_TESTS.' SET
Nick's avatar
Nick committed
209
			test_end_time='.F_empty_to_null(''.(intval(substr($test_end_time, 0, 1)) - 1).substr($test_end_time, 1)).'
nick's avatar
nick committed
210
			WHERE test_id='.$test_id.'';
Nick's avatar
Nick committed
211
		if (!$r = F_db_query($sql, $db)) {
nick's avatar
nick committed
212
213
214
215
216
217
218
219
220
			F_display_db_error(false);
		} else {
			F_print_error('MESSAGE', $l['m_updated']);
		}
		break;
	}

	case 'unlock':{ // unlock test by restoring original end date (add 1000 years)
		$sql = 'UPDATE '.K_TABLE_TESTS.' SET
Nick's avatar
Nick committed
221
			test_end_time='.F_empty_to_null(''.(intval(substr($test_end_time, 0, 1)) + 1).substr($test_end_time, 1)).'
nick's avatar
nick committed
222
			WHERE test_id='.$test_id.'';
Nick's avatar
Nick committed
223
		if (!$r = F_db_query($sql, $db)) {
nick's avatar
nick committed
224
225
226
227
228
229
230
			F_display_db_error(false);
		} else {
			F_print_error('MESSAGE', $l['m_updated']);
		}
		break;
	}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
231
232
	case 'deletesubject':{ // delete subject
		// check referential integrity (NOTE: mysql do not support "ON UPDATE" constraint)
Nick's avatar
Nick committed
233
		if (!F_check_unique(K_TABLE_TEST_USER, 'testuser_test_id='.$test_id.'')) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
234
235
236
237
238
			F_print_error('WARNING', $l['m_update_restrict']);
			F_stripslashes_formfields();
			break;
		}
		$sql = 'DELETE FROM '.K_TABLE_TEST_SUBJSET.' WHERE tsubset_id='.$tsubset_id.'';
Nick's avatar
Nick committed
239
		if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
240
241
242
243
244
245
246
247
248
			F_display_db_error(false);
		} else {
			F_print_error('MESSAGE', $l['m_deleted']);
		}
		break;
	}

	case 'addquestion':{ // Add question type
		// check referential integrity (NOTE: mysql do not support "ON UPDATE" constraint)
Nick's avatar
Nick committed
249
		if (!F_check_unique(K_TABLE_TEST_USER, 'testuser_test_id='.$test_id.'')) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
250
			F_print_error('WARNING', $l['m_update_restrict']);
nick's avatar
nick committed
251
252
			$formstatus = FALSE;
			F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
253
254
			break;
		}
Nick's avatar
Nick committed
255
		if ($formstatus = F_check_form_fields()) {
nick's avatar
nick committed
256
			if ((isset($subject_id)) AND (!empty($subject_id)) AND (isset($tsubset_quantity))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
257
258
259
				if ($tsubset_type == 3) {
					// free-text questions do not have alternative answers to display
					$tsubset_answers = 0;
260
				} elseif (($tsubset_answers < 2)) {		
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
261
					// questions must have at least 2 alternative answers
262
					$tsubset_answers = 999;	//select all answers 
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
263
264
265
				}
				// create a comma separated list of subjects IDs
				$subjids = '';
nick's avatar
nick committed
266
				foreach ($subject_id as $subid) {
tecnickcom's avatar
tecnickcom committed
267
					if ($subid[0] == '#') {
nick's avatar
nick committed
268
269
270
						// module ID
						$modid = intval(substr($subid, 1));
						$sqlsm = F_select_subjects_sql('subject_module_id='.$modid.'');
Nick's avatar
Nick committed
271
						if ($rsm = F_db_query($sqlsm, $db)) {
nicolaasuni's avatar
nicolaasuni committed
272
							while ($msm = F_db_fetch_array($rsm)) {
nick's avatar
nick committed
273
								$subjids .= $msm['subject_id'].',';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
274
275
276
							}
						} else {
							F_display_db_error();
nick's avatar
nick committed
277
						}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
278
					} else {
nick's avatar
nick committed
279
						$subjids .= intval($subid).',';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
280
281
					}
				}
nick's avatar
nick committed
282
283
				$subjids = substr($subjids, 0, -1);
				$subject_id = explode(',', $subjids);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
284
				$subjids = '('.$subjids.')';
Nicola Asuni's avatar
Nicola Asuni committed
285
286
287
288
289
290
291
292
				$sql_answer_position = '';
				$sql_questions_position = '';
				if (!$test_random_questions_order AND ($test_questions_order_mode == 0)) {
					$sql_questions_position = ' AND question_position>0';
				}
				if (!$test_random_answers_order AND ($test_answers_order_mode == 0)) {
					$sql_answer_position = ' AND answer_position>0';
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
293
294
				// check here if the selected number of questions are available for the current set
				// NOTE: if the same subject is used in multiple sets this control may fail.
Nicola Asuni's avatar
Nicola Asuni committed
295
				$sqlq = 'SELECT COUNT(*) AS numquestions FROM '.K_TABLE_QUESTIONS.'';
296
297
298
299
300
301
302
303
				$sqlq .= ' WHERE question_subject_id IN '.$subjids;
				if ($tsubset_difficulty > 0) {
					$sqlq .= '	AND question_difficulty='.$tsubset_difficulty;
					$alldifficulties=false;
				} else {
					$alldifficulties=true;
				}
				$sqlq .= '	AND question_enabled=\'1\'';
nick's avatar
nick committed
304
305
306
				if ($tsubset_type > 0) {
					$sqlq .= ' AND question_type='.$tsubset_type.'';
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
307
308
309
				if ($tsubset_type == 1) {
					// single question (MCSA)
					// check if the selected question has enough answers
nick's avatar
nick committed
310
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
311
312
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
nick's avatar
nick committed
313
							WHERE answer_enabled=\'1\' AND answer_isright=\'1\'';
Nicola Asuni's avatar
Nicola Asuni committed
314
					$sqlq .= $sql_answer_position;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
315
					$sqlq .= ' GROUP BY answer_question_id
nick's avatar
nick committed
316
317
							HAVING (COUNT(answer_id)>0))';
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
318
319
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
nick's avatar
nick committed
320
321
							WHERE answer_enabled=\'1\'
							AND answer_isright=\'0\'';
Nicola Asuni's avatar
Nicola Asuni committed
322
					$sqlq .= $sql_answer_position;
nick's avatar
nick committed
323
324
					$sqlq .= ' GROUP BY answer_question_id';
					$sqlq .= ' )';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
325
326
327
				} elseif ($tsubset_type == 2) {
					// multiple question (MCMA)
					// check if the selected question has enough answers
nick's avatar
nick committed
328
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
329
330
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
nick's avatar
nick committed
331
							WHERE answer_enabled=\'1\'';
Nicola Asuni's avatar
Nicola Asuni committed
332
					$sqlq .= $sql_answer_position;
nick's avatar
nick committed
333
334
					$sqlq .= ' GROUP BY answer_question_id';
					$sqlq .= ' )';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
335
336
337
				} elseif ($tsubset_type == 4) {
					// ordering question
					// check if the selected question has enough answers
nick's avatar
nick committed
338
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
339
340
341
342
343
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
							WHERE answer_enabled=\'1\'
							AND answer_position>0
							GROUP BY answer_question_id
nick's avatar
nick committed
344
							HAVING (COUNT(answer_id)>1))';
nick's avatar
nick committed
345
				}
Nicola Asuni's avatar
Nicola Asuni committed
346
				$sqlq .= $sql_questions_position;
347
				$sqlq1 = $sqlq;
Ewald's avatar
Ewald committed
348
349
350
351
352
353
				if ($tsubset_quantity < 999) {
					if (K_DATABASE_TYPE == 'ORACLE') {
						$sqlq = 'SELECT * FROM ('.$sqlq.') WHERE rownum <= '.$tsubset_quantity.'';
					} else {
						$sqlq .= ' LIMIT '.$tsubset_quantity.'';
					}
nicolaasuni's avatar
9.0.029    
nicolaasuni committed
354
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
355
				$numofrows = 0;
Nick's avatar
Nick committed
356
357
				if ($rq = F_db_query($sqlq, $db)) {
					if ($mq = F_db_fetch_array($rq)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
358
						$numofrows = $mq['numquestions'];
nick's avatar
nick committed
359
360
					}
				} else {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
361
362
					F_display_db_error();
				}
363
364
365
				if (($numofrows < $tsubset_quantity) and ($tsubset_quantity < 999) ) {
					$tsubset_quantity = $numofrows;	//take what you've got
					F_print_error('WARNING', $l['m_not_enough_answers']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
366
367
368
				}
				if (!empty($subject_id)) {
					// insert new subject
369
					$nmax = ($alldifficulties == true ? 9 : 1);
370
					$tquanorig = $tsubset_quantity;
371
					for ($n=1; $n <= $nmax; $n++) {
372
						$tquan = $tquanorig;
373
374
375
376
						$sqlq2 = $sqlq1;
						if ($alldifficulties == true){
							$tsubset_difficulty = $n;
							$sqlq2 .= '	AND question_difficulty='.$n;
Ewald's avatar
Ewald committed
377
378
							if ($tsubset_quantity < 999) {
								if (K_DATABASE_TYPE == 'ORACLE') {
379
									$sqlq2 = 'SELECT * FROM ('.$sqlq2.') WHERE rownum <= '.$tquan.'';
Ewald's avatar
Ewald committed
380
								} else {
381
									$sqlq2 .= ' LIMIT '.$tquan.'';
Ewald's avatar
Ewald committed
382
								}
383
384
385
386
387
388
389
390
							}
							if ($rq = F_db_query($sqlq2, $db)) {
								if ($mq = F_db_fetch_array($rq)) {
									$numofrows = $mq['numquestions'];
								}
							} else {
								F_display_db_error();
							}
391
							$tquan = $numofrows;
392
393
394
395
396
397
398
						}
						if ($numofrows > 0 )  {
							$sql = 'INSERT INTO '.K_TABLE_TEST_SUBJSET.' (tsubset_test_id,
								tsubset_type,
								tsubset_difficulty,
								tsubset_quantity,
								tsubset_answers
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
399
								) VALUES (
400
401
402
								\''.$test_id.'\',
								\''.$tsubset_type.'\',
								\''.$tsubset_difficulty.'\',
403
								\''.$tquan.'\',
404
								\''.$tsubset_answers.'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
405
								)';
Nick's avatar
Nick committed
406
							if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
407
								F_display_db_error(false);
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
							} else {
								$tsubset_id = F_db_insert_id($db, K_TABLE_TEST_SUBJSET, 'tsubset_id');
								// add selected subject_id
								foreach ($subject_id as $subid) {
									$sql = 'INSERT INTO '.K_TABLE_SUBJECT_SET.' (
										subjset_tsubset_id,
										subjset_subject_id
										) VALUES (
										\''.$tsubset_id.'\',
										\''.$subid.'\'
										)';
									if (!$r = F_db_query($sql, $db)) {
										F_display_db_error(false);
									}
								}//foreach
							}//else
						}//if numofrows > 0
					}//for
				}//if !empty
			} else {
				F_print_error('WARNING', $l['m_no_subject_selected']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
429
430
			}
		}
431
432
		$tsubset_quantity = intval($_REQUEST['tsubset_quantity']);//reset for display
		$tsubset_difficulty = intval($_REQUEST['tsubset_difficulty']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
433
434
435
436
		break;
	}

	case 'delete':{
nick's avatar
nick committed
437
		F_stripslashes_formfields();		// ask confirmation
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
438
439
440
441
442
443
444
		F_print_error('WARNING', $l['m_delete_confirm_test']);
		?>
		<div class="confirmbox">
		<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data" id="form_delete">
		<div>
		<input type="hidden" name="test_id" id="test_id" value="<?php echo $test_id; ?>" />
		<input type="hidden" name="test_name" id="test_name" value="<?php echo $test_name; ?>" />
nick's avatar
nick committed
445
446
		<?php
		F_submit_button('forcedelete', $l['w_delete'], $l['h_delete']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
447
448
449
450
451
452
453
454
455
456
457
		F_submit_button('cancel', $l['w_cancel'], $l['h_cancel']);
		?>
		</div>
		</form>
		</div>
		<?php
		break;
	}

	case 'forcedelete':{
		F_stripslashes_formfields(); // Delete
Nick's avatar
Nick committed
458
		if ($forcedelete == $l['w_delete']) { //check if delete button has been pushed (redundant check)
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
459
460
			// delete test
			$sql = 'DELETE FROM '.K_TABLE_TESTS.' WHERE test_id='.$test_id.'';
Nick's avatar
Nick committed
461
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
462
463
464
465
466
467
468
469
470
471
				F_display_db_error(false);
			} else {
				$test_id = false;
				F_print_error('MESSAGE', $test_name.': '.$l['m_deleted']);
			}
		}
		break;
	}

	case 'update':{ // Update
472
		// check if the confirmation checkbox has been selected
Nick's avatar
Nick committed
473
474
		if (!isset($_REQUEST['confirmupdate']) OR ($_REQUEST['confirmupdate'] != 1)) {
			F_print_error('WARNING', $l['m_form_missing_fields'].': '.$l['w_confirm'].' &rarr; '.$l['w_update']);
Nicola Asuni's avatar
Nicola Asuni committed
475
			$formstatus = FALSE;
Nick's avatar
Nick committed
476
477
478
			F_stripslashes_formfields();
			break;
		}
Nick's avatar
Nick committed
479
		if ($formstatus = F_check_form_fields()) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
480
			// check referential integrity (NOTE: mysql do not support "ON UPDATE" constraint)
Nick's avatar
Nick committed
481
			if (!F_check_unique(K_TABLE_TEST_USER, 'testuser_test_id='.$test_id.'')) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
482
				F_print_error('WARNING', $l['m_update_restrict']);
nick's avatar
nick committed
483
484
				$formstatus = FALSE;
				F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
485
486
487
				break;
			}
			// check if name is unique
Nick's avatar
Nick committed
488
			if (!F_check_unique(K_TABLE_TESTS, 'test_name=\''.$test_name.'\'', 'test_id', $test_id)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
489
				F_print_error('WARNING', $l['m_duplicate_name']);
nick's avatar
nick committed
490
491
				$formstatus = FALSE;
				F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
492
				break;
nick's avatar
nick committed
493
			}
Nicola Asuni's avatar
Nicola Asuni committed
494
495
496
			if (!empty($new_test_password)) {
				$test_password = getPasswordHash($new_test_password);
			}
nick's avatar
nick committed
497
498
			if ($test_score_threshold > $test_max_score) {
				$test_score_threshold = 0.6 * $test_max_score;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
499
			}
nick's avatar
nick committed
500
			$sql = 'UPDATE '.K_TABLE_TESTS.' SET
tecnickcom's avatar
tecnickcom committed
501
502
				test_name=\''.F_escape_sql($db, $test_name).'\',
				test_description=\''.F_escape_sql($db, $test_description).'\',
Nick's avatar
Nick committed
503
504
				test_begin_time='.F_empty_to_null($test_begin_time).',
				test_end_time='.F_empty_to_null($test_end_time).',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
505
				test_duration_time=\''.$test_duration_time.'\',
tecnickcom's avatar
tecnickcom committed
506
				test_ip_range=\''.F_escape_sql($db, $test_ip_range).'\',
Nicola Asuni's avatar
Nicola Asuni committed
507
508
				test_results_to_users=\''.intval($test_results_to_users).'\',
				test_report_to_users=\''.intval($test_report_to_users).'\',
nick's avatar
nick committed
509
510
511
				test_score_right=\''.$test_score_right.'\',
				test_score_wrong=\''.$test_score_wrong.'\',
				test_score_unanswered=\''.$test_score_unanswered.'\',
nick's avatar
nick committed
512
				test_max_score=\''.$test_max_score.'\',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
513
				test_score_threshold=\''.$test_score_threshold.'\',
Nicola Asuni's avatar
Nicola Asuni committed
514
515
				test_random_questions_select=\''.intval($test_random_questions_select).'\',
				test_random_questions_order=\''.intval($test_random_questions_order).'\',
Nicola Asuni's avatar
Nicola Asuni committed
516
				test_questions_order_mode=\''.$test_questions_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
517
518
				test_random_answers_select=\''.intval($test_random_answers_select).'\',
				test_random_answers_order=\''.intval($test_random_answers_order).'\',
Nicola Asuni's avatar
Nicola Asuni committed
519
				test_answers_order_mode=\''.$test_answers_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
520
521
522
523
524
525
526
				test_comment_enabled=\''.intval($test_comment_enabled).'\',
				test_menu_enabled=\''.intval($test_menu_enabled).'\',
				test_noanswer_enabled=\''.intval($test_noanswer_enabled).'\',
				test_mcma_radio=\''.intval($test_mcma_radio).'\',
				test_repeatable=\''.intval($test_repeatable).'\',
				test_mcma_partial_score=\''.intval($test_mcma_partial_score).'\',
				test_logout_on_timeout=\''.intval($test_logout_on_timeout).'\',
Nicola Asuni's avatar
Nicola Asuni committed
527
				test_password='.F_empty_to_null($test_password).'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
528
				WHERE test_id='.$test_id.'';
Nick's avatar
Nick committed
529
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
530
531
532
533
				F_display_db_error(false);
			} else {
				F_print_error('MESSAGE', $l['m_updated']);
			}
nick's avatar
nick committed
534

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
535
			// delete previous groups
nick's avatar
nick committed
536
537
			$sql = 'DELETE FROM '.K_TABLE_TEST_GROUPS.'
				WHERE tstgrp_test_id='.$test_id.'';
Nick's avatar
Nick committed
538
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
539
540
541
542
543
544
545
546
547
				F_display_db_error(false);
			}
			// update authorized groups
			if (!empty($user_groups)) {
				foreach ($user_groups as $group_id) {
					$sql = 'INSERT INTO '.K_TABLE_TEST_GROUPS.' (
						tstgrp_test_id,
						tstgrp_group_id
						) VALUES (
nick's avatar
nick committed
548
						\''.$test_id.'\',
Nicola Asuni's avatar
Nicola Asuni committed
549
						\''.intval($group_id).'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
550
						)';
Nick's avatar
Nick committed
551
					if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
552
553
						F_display_db_error(false);
					}
554
555
556
557
					$sql = "SELECT * FROM set_admin_group($test_id, $group_id)";
					if (!$r = F_db_query($sql, $db)) {
						F_display_db_error(false);
					}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
558
559
				}
			}
tecnickcom's avatar
tecnickcom committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582

			// delete previous SSL certificates
			$sql = 'DELETE FROM '.K_TABLE_TEST_SSLCERTS.'
				WHERE tstssl_test_id='.$test_id.'';
			if (!$r = F_db_query($sql, $db)) {
				F_display_db_error(false);
			}
			// update authorized SSL certificates
			if (!empty($sslcerts)) {
				foreach ($sslcerts as $ssl_id) {
					$sql = 'INSERT INTO '.K_TABLE_TEST_SSLCERTS.' (
						tstssl_test_id,
						tstssl_ssl_id
						) VALUES (
						\''.$test_id.'\',
						\''.intval($ssl_id).'\'
						)';
					if (!$r = F_db_query($sql, $db)) {
						F_display_db_error(false);
					}
				}
			}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
583
584
585
586
587
		}
		break;
	}

	case 'add':{ // Add
Nick's avatar
Nick committed
588
		if ($formstatus = F_check_form_fields()) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
589
			// check if name is unique
tecnickcom's avatar
tecnickcom committed
590
			if (!F_check_unique(K_TABLE_TESTS, 'test_name=\''.F_escape_sql($db, $test_name).'\'')) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
591
				F_print_error('WARNING', $l['m_duplicate_name']);
nick's avatar
nick committed
592
593
				$formstatus = FALSE;
				F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
594
595
596
597
598
599
				break;
			}
			if (isset($test_id) AND ($test_id > 0)) {
				// save previous test_id.
				$old_test_id = $test_id;
			}
Nicola Asuni's avatar
Nicola Asuni committed
600
601
602
			if (!empty($new_test_password)) {
				$test_password = getPasswordHash($new_test_password);
			}
nick's avatar
nick committed
603
604
			$sql = 'INSERT INTO '.K_TABLE_TESTS.' (
			test_name,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
605
606
607
608
609
				test_description,
				test_begin_time,
				test_end_time,
				test_duration_time,
				test_ip_range,
nick's avatar
nick committed
610
611
				test_results_to_users,
				test_report_to_users,
nick's avatar
nick committed
612
613
				test_score_right,
				test_score_wrong,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
614
				test_score_unanswered,
nick's avatar
nick committed
615
616
				test_max_score,
				test_user_id,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
617
618
619
				test_score_threshold,
				test_random_questions_select,
				test_random_questions_order,
Nicola Asuni's avatar
Nicola Asuni committed
620
				test_questions_order_mode,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
621
622
				test_random_answers_select,
				test_random_answers_order,
Nicola Asuni's avatar
Nicola Asuni committed
623
				test_answers_order_mode,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
624
625
626
				test_comment_enabled,
				test_menu_enabled,
				test_noanswer_enabled,
nick's avatar
nick committed
627
				test_mcma_radio,
nick's avatar
nick committed
628
629
				test_repeatable,
				test_mcma_partial_score,
Nicola Asuni's avatar
Nicola Asuni committed
630
631
				test_logout_on_timeout,
				test_password
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
632
				) VALUES (
tecnickcom's avatar
tecnickcom committed
633
634
				\''.F_escape_sql($db, $test_name).'\',
				\''.F_escape_sql($db, $test_description).'\',
Nick's avatar
Nick committed
635
636
				'.F_empty_to_null($test_begin_time).',
				'.F_empty_to_null($test_end_time).',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
637
				\''.$test_duration_time.'\',
tecnickcom's avatar
tecnickcom committed
638
				\''.F_escape_sql($db, $test_ip_range).'\',
Nicola Asuni's avatar
Nicola Asuni committed
639
640
				\''.intval($test_results_to_users).'\',
				\''.intval($test_report_to_users).'\',
nick's avatar
nick committed
641
642
				\''.$test_score_right.'\',
				\''.$test_score_wrong.'\',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
643
				\''.$test_score_unanswered.'\',
nick's avatar
nick committed
644
645
				\''.$test_max_score.'\',
				\''.intval($_SESSION['session_user_id']).'\',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
646
				\''.$test_score_threshold.'\',
Nicola Asuni's avatar
Nicola Asuni committed
647
648
				\''.intval($test_random_questions_select).'\',
				\''.intval($test_random_questions_order).'\',
Nicola Asuni's avatar
Nicola Asuni committed
649
				\''.$test_questions_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
650
651
				\''.intval($test_random_answers_select).'\',
				\''.intval($test_random_answers_order).'\',
Nicola Asuni's avatar
Nicola Asuni committed
652
				\''.$test_answers_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
653
654
655
656
657
658
659
				\''.intval($test_comment_enabled).'\',
				\''.intval($test_menu_enabled).'\',
				\''.intval($test_noanswer_enabled).'\',
				\''.intval($test_mcma_radio).'\',
				\''.intval($test_repeatable).'\',
				\''.intval($test_mcma_partial_score).'\',
				\''.intval($test_logout_on_timeout).'\',
Nicola Asuni's avatar
Nicola Asuni committed
660
				'.F_empty_to_null($test_password).'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
661
				)';
Nick's avatar
Nick committed
662
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
663
664
665
666
667
668
669
670
671
672
673
				F_display_db_error(false);
			} else {
				$test_id = F_db_insert_id($db, K_TABLE_TESTS, 'test_id');
			}
			// add authorized user's groups
			if (!empty($user_groups)) {
				foreach ($user_groups as $group_id) {
					$sql = 'INSERT INTO '.K_TABLE_TEST_GROUPS.' (
						tstgrp_test_id,
						tstgrp_group_id
						) VALUES (
nick's avatar
nick committed
674
						\''.$test_id.'\',
Nicola Asuni's avatar
Nicola Asuni committed
675
						\''.intval($group_id).'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
676
						)';
Nick's avatar
Nick committed
677
					if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
678
679
						F_display_db_error(false);
					}
680
681
682
683
					$sql = "SELECT * FROM set_admin_group($test_id, $group_id)";
					if (!$r = F_db_query($sql, $db)) {
						F_display_db_error(false);
					}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
684
685
				}
			}
tecnickcom's avatar
tecnickcom committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
			// update authorized SSL certificates
			if (!empty($sslcerts)) {
				foreach ($sslcerts as $ssl_id) {
					$sql = 'INSERT INTO '.K_TABLE_TEST_SSLCERTS.' (
						tstssl_test_id,
						tstssl_ssl_id
						) VALUES (
						\''.$test_id.'\',
						\''.intval($ssl_id).'\'
						)';
					if (!$r = F_db_query($sql, $db)) {
						F_display_db_error(false);
					}
				}
			}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
702
703
			if (isset($old_test_id) AND ($old_test_id > 0)) {
				// copy here previous selected questions to this new test
Nick's avatar
Nick committed
704
				$sql = 'SELECT *
nick's avatar
nick committed
705
					FROM '.K_TABLE_TEST_SUBJSET.'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
706
					WHERE tsubset_test_id=\''.$old_test_id.'\'';
Nick's avatar
Nick committed
707
				if ($r = F_db_query($sql, $db)) {
nicolaasuni's avatar
nicolaasuni committed
708
					while ($m = F_db_fetch_array($r)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
709
						// insert new subject
nick's avatar
nick committed
710
711
						$sqlu = 'INSERT INTO '.K_TABLE_TEST_SUBJSET.' (
							tsubset_test_id,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
712
713
714
715
716
717
718
719
720
721
722
							tsubset_type,
							tsubset_difficulty,
							tsubset_quantity,
							tsubset_answers
							) VALUES (
							\''.$test_id.'\',
							\''.$m['tsubset_type'].'\',
							\''.$m['tsubset_difficulty'].'\',
							\''.$m['tsubset_quantity'].'\',
							\''.$m['tsubset_answers'].'\'
							)';
Nick's avatar
Nick committed
723
						if (!$ru = F_db_query($sqlu, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
724
725
726
727
728
729
							F_display_db_error();
						} else {
							$tsubset_id = F_db_insert_id($db, K_TABLE_TEST_SUBJSET, 'tsubset_id');
							$sqls = 'SELECT *
								FROM '.K_TABLE_SUBJECT_SET.'
								WHERE subjset_tsubset_id=\''.$m['tsubset_id'].'\'';
Nick's avatar
Nick committed
730
							if ($rs = F_db_query($sqls, $db)) {
nicolaasuni's avatar
nicolaasuni committed
731
								while ($ms = F_db_fetch_array($rs)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
732
733
734
735
									$sqlp = 'INSERT INTO '.K_TABLE_SUBJECT_SET.' (
										subjset_tsubset_id,
										subjset_subject_id
										) VALUES (
nick's avatar
nick committed
736
737
										\''.$tsubset_id.'\',
										\''.$ms['subjset_subject_id'].'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
738
										)';
Nick's avatar
Nick committed
739
									if (!$rp = F_db_query($sqlp, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
										F_display_db_error();
									}
								}
							} else {
								F_display_db_error();
							}
						}
					}
				} else {
					F_display_db_error();
				}
			}
		}
		break;
	}

	case 'clear':{ // Clear form fields
		$test_name = '';
		$test_description = '';
		$test_begin_time = date(K_TIMESTAMP_FORMAT);
		$test_end_time = date(K_TIMESTAMP_FORMAT, time() + K_SECONDS_IN_DAY);
		$test_duration_time = 60;
Ewald's avatar
Ewald committed
762
		$test_ip_range = K_DEFAULT_IP_RANGE;
nick's avatar
nick committed
763
		$test_results_to_users = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
764
		$test_report_to_users = false;
nick's avatar
nick committed
765
766
767
		$test_score_right = 1;
		$test_score_wrong = 0;
		$test_score_unanswered = 0;
Nicola Asuni's avatar
Nicola Asuni committed
768
		$test_max_score = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
769
770
771
		$test_score_threshold = 0;
		$test_random_questions_select = true;
		$test_random_questions_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
772
		$test_questions_order_mode = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
773
774
		$test_random_answers_select = true;
		$test_random_answers_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
775
		$test_answers_order_mode = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
776
777
778
		$test_comment_enabled = true;
		$test_menu_enabled = true;
		$test_noanswer_enabled = true;
nick's avatar
nick committed
779
		$test_mcma_radio = true;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
780
		$test_repeatable = false;
nick's avatar
nick committed
781
782
		$test_mcma_partial_score = true;
		$test_logout_on_timeout = false;
Nicola Asuni's avatar
Nicola Asuni committed
783
		$test_password = '';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
784
785
786
		break;
	}

Nicola Asuni's avatar
Nicola Asuni committed
787
788
	default :{
		break;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
789
790
791
792
793
794
795
796
797
798
	}

} //end of switch

// --- Initialize variables

if (!isset($test_num) OR (!empty($test_num))) {
	$test_num = 1; // default number of PDF tests to generate
}

Nick's avatar
Nick committed
799
if ($formstatus) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
800
	if ($menu_mode != 'clear') {
Nick's avatar
Nick committed
801
		if (!isset($test_id) OR empty($test_id)) {
Nick's avatar
Nick committed
802
803
804
805
806
807
			$test_id = 0;
			$test_name = '';
			$test_description = '';
			$test_begin_time = date(K_TIMESTAMP_FORMAT);
			$test_end_time = date(K_TIMESTAMP_FORMAT, time() + K_SECONDS_IN_DAY);
			$test_duration_time = 60;
Ewald's avatar
Ewald committed
808
			$test_ip_range = K_DEFAULT_IP_RANGE;
Nick's avatar
Nick committed
809
810
811
812
813
814
815
816
817
			$test_results_to_users = false;
			$test_report_to_users = false;
			$test_score_right = 1;
			$test_score_wrong = 0;
			$test_score_unanswered = 0;
			$test_max_score = 0;
			$test_score_threshold = 0;
			$test_random_questions_select = true;
			$test_random_questions_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
818
			$test_questions_order_mode = 0;
Nick's avatar
Nick committed
819
820
			$test_random_answers_select = true;
			$test_random_answers_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
821
			$test_answers_order_mode = 0;
Nick's avatar
Nick committed
822
823
824
825
826
827
828
			$test_comment_enabled = true;
			$test_menu_enabled = true;
			$test_noanswer_enabled = true;
			$test_mcma_radio = true;
			$test_repeatable = false;
			$test_mcma_partial_score = true;
			$test_logout_on_timeout = false;
Nicola Asuni's avatar
Nicola Asuni committed
829
			$test_password = '';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
830
		} else {
nick's avatar
nick committed
831
			$sql = 'SELECT * FROM '.K_TABLE_TESTS.' WHERE test_id='.$test_id.' LIMIT 1';
Nick's avatar
Nick committed
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
			if ($r = F_db_query($sql, $db)) {
				if ($m = F_db_fetch_array($r)) {
					$test_id = $m['test_id'];
					$test_name = $m['test_name'];
					$test_description = $m['test_description'];
					$test_begin_time = $m['test_begin_time'];
					$test_end_time = $m['test_end_time'];
					$test_duration_time = $m['test_duration_time'];
					$test_ip_range = $m['test_ip_range'];
					$test_results_to_users = F_getBoolean($m['test_results_to_users']);
					$test_report_to_users = F_getBoolean($m['test_report_to_users']);
					$test_score_right = $m['test_score_right'];
					$test_score_wrong = $m['test_score_wrong'];
					$test_score_unanswered = $m['test_score_unanswered'];
					$test_max_score = $m['test_max_score'];
					$test_score_threshold = $m['test_score_threshold'];
					$test_random_questions_select = F_getBoolean($m['test_random_questions_select']);
					$test_random_questions_order = F_getBoolean($m['test_random_questions_order']);
Nicola Asuni's avatar
Nicola Asuni committed
850
					$test_questions_order_mode = intval($m['test_questions_order_mode']);
Nick's avatar
Nick committed
851
852
					$test_random_answers_select = F_getBoolean($m['test_random_answers_select']);
					$test_random_answers_order = F_getBoolean($m['test_random_answers_order']);
Nicola Asuni's avatar
Nicola Asuni committed
853
					$test_answers_order_mode = intval($m['test_answers_order_mode']);
Nick's avatar
Nick committed
854
855
856
857
858
859
860
					$test_comment_enabled = F_getBoolean($m['test_comment_enabled']);
					$test_menu_enabled = F_getBoolean($m['test_menu_enabled']);
					$test_noanswer_enabled = F_getBoolean($m['test_noanswer_enabled']);
					$test_mcma_radio = F_getBoolean($m['test_mcma_radio']);
					$test_repeatable = F_getBoolean($m['test_repeatable']);
					$test_mcma_partial_score = F_getBoolean($m['test_mcma_partial_score']);
					$test_logout_on_timeout = F_getBoolean($m['test_logout_on_timeout']);
Nicola Asuni's avatar
Nicola Asuni committed
861
					$test_password = $m['test_password'];
Nick's avatar
Nick committed
862
863
864
865
866
867
				} else {
					$test_name = '';
					$test_description = '';
					$test_begin_time = date(K_TIMESTAMP_FORMAT);
					$test_end_time = date(K_TIMESTAMP_FORMAT, time() + K_SECONDS_IN_DAY);
					$test_duration_time = 60;
Ewald's avatar
Ewald committed
868
					$test_ip_range = K_DEFAULT_IP_RANGE;
Nick's avatar
Nick committed
869
870
871
872
873
874
875
876
877
					$test_results_to_users = false;
					$test_report_to_users = false;
					$test_score_right = 1;
					$test_score_wrong = 0;
					$test_score_unanswered = 0;
					$test_max_score = 0;
					$test_score_threshold = 0;
					$test_random_questions_select = true;
					$test_random_questions_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
878
					$test_questions_order_mode = 0;
Nick's avatar
Nick committed
879
880
					$test_random_answers_select = true;
					$test_random_answers_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
881
					$test_answers_order_mode = 0;
Nick's avatar
Nick committed
882
883
884
885
886
887
888
					$test_comment_enabled = true;
					$test_menu_enabled = true;
					$test_noanswer_enabled = true;
					$test_mcma_radio = true;
					$test_repeatable = false;
					$test_mcma_partial_score = true;
					$test_logout_on_timeout = false;
Nicola Asuni's avatar
Nicola Asuni committed
889
					$test_password = '';
Nick's avatar
Nick committed
890
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
891
			} else {
Nick's avatar
Nick committed
892
				F_display_db_error();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
893
894
895
896
			}
		}
	}
}
897
898
899
900
901
if (($test_end_time <= $test_begin_time) and ($menu_mode != 'lock')) {
	F_print_error('WARNING', $l['m_error_end_time']);
	//	$formstatus = FALSE;
	F_stripslashes_formfields();
}
nick's avatar
nick committed
902
$millennium = substr(date('Y'), 0, 1);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
903

Nick's avatar
Nick committed
904
echo '<div class="container">'.K_NEWLINE;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
905

Nick's avatar
Nick committed
906
907
echo '<div class="tceformbox">'.K_NEWLINE;
echo '<form action="'.$_SERVER['SCRIPT_NAME'].'" method="post" enctype="multipart/form-data" id="form_testeditor">'.K_NEWLINE;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
908

Nick's avatar
Nick committed
909
910
911
912
913
914
echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="test_id">'.$l['w_test'].'</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
echo '<select name="test_id" id="test_id" size="0" onchange="document.getElementById(\'form_testeditor\').submit()" title="'.$l['h_test'].'">'.K_NEWLINE;
Nick's avatar
Nick committed
915
916
917
918
919
echo '<option value="0" style="background-color:#009900;color:white;"';
if ($test_id == 0) {
	echo ' selected="selected"';
}
echo '>+</option>'.K_NEWLINE;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
920
$sql = F_select_tests_sql();
Nick's avatar
Nick committed
921
if ($r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
922
	$countitem = 1;
nicolaasuni's avatar
nicolaasuni committed
923
	while ($m = F_db_fetch_array($r)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
924
		echo '<option value="'.$m['test_id'].'"';
Nick's avatar
Nick committed
925
		if ($m['test_id'] == $test_id) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
926
927
928
			echo ' selected="selected"';
			$test_fieldset_name = ''.substr($m['test_begin_time'], 0, 10).' '.htmlspecialchars($m['test_name'], ENT_NOQUOTES, $l['a_meta_charset']).'';
		}
nick's avatar
nick committed
929
930
931
932
933
		echo '>'.$countitem.'. ';
		if (substr($m['test_end_time'], 0, 1) < $millennium) {
			echo '* ';
		}
		echo substr($m['test_begin_time'], 0, 10).' '.htmlspecialchars($m['test_name'], ENT_NOQUOTES, $l['a_meta_charset']).'</option>'.K_NEWLINE;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
934
935
		$countitem++;
	}
Nick's avatar
Nick committed
936
} else {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
937
938
939
	echo '</select></span></div>'.K_NEWLINE;
	F_display_db_error();
}
Nick's avatar
Nick committed
940
echo '</select>'.K_NEWLINE;
Nicola Asuni's avatar
Nicola Asuni committed
941
942
943
944
945

// link for user selection popup
$jsaction = 'selectWindow=window.open(\'tce_select_tests_popup.php?cid=test_id\', \'selectWindow\', \'dependent, height=600, width=