tce_edit_test.php 50.6 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']))) {
nick's avatar
nick committed
77
	$tsubset_answers = 2;
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 {
tecnickcom's avatar
tecnickcom committed
123
	$test_questions_order_mode = max(0, min(3, intval($_REQUEST['test_questions_order_mode'])));
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
260
261
262
263
264
265
				if ($tsubset_type == 3) {
					// free-text questions do not have alternative answers to display
					$tsubset_answers = 0;
				} elseif (($tsubset_answers < 2) AND ($tsubset_difficulty > 0)) {
					// questions must have at least 2 alternative answers
					$tsubset_answers = 2;
				}
				// 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.'';
nick's avatar
nick committed
296
297
298
				$sqlq .= ' WHERE question_subject_id IN '.$subjids.'
					AND question_difficulty='.$tsubset_difficulty.'
					AND question_enabled=\'1\'';
nick's avatar
nick committed
299
300
301
				if ($tsubset_type > 0) {
					$sqlq .= ' AND question_type='.$tsubset_type.'';
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
302
303
304
				if ($tsubset_type == 1) {
					// single question (MCSA)
					// check if the selected question has enough answers
nick's avatar
nick committed
305
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
306
307
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
nick's avatar
nick committed
308
							WHERE answer_enabled=\'1\' AND answer_isright=\'1\'';
Nicola Asuni's avatar
Nicola Asuni committed
309
					$sqlq .= $sql_answer_position;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
310
					$sqlq .= ' GROUP BY answer_question_id
nick's avatar
nick committed
311
312
							HAVING (COUNT(answer_id)>0))';
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
313
314
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
nick's avatar
nick committed
315
316
							WHERE answer_enabled=\'1\'
							AND answer_isright=\'0\'';
Nicola Asuni's avatar
Nicola Asuni committed
317
					$sqlq .= $sql_answer_position;
nick's avatar
nick committed
318
319
320
321
322
					$sqlq .= ' GROUP BY answer_question_id';
					if ($tsubset_answers > 0) {
						$sqlq .= ' HAVING (COUNT(answer_id)>='.($tsubset_answers-1).')';
					}
					$sqlq .= ' )';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
323
324
325
				} elseif ($tsubset_type == 2) {
					// multiple question (MCMA)
					// check if the selected question has enough answers
nick's avatar
nick committed
326
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
327
328
							SELECT answer_question_id
							FROM '.K_TABLE_ANSWERS.'
nick's avatar
nick committed
329
							WHERE answer_enabled=\'1\'';
Nicola Asuni's avatar
Nicola Asuni committed
330
					$sqlq .= $sql_answer_position;
nick's avatar
nick committed
331
332
333
334
335
					$sqlq .= ' GROUP BY answer_question_id';
					if ($tsubset_answers > 0) {
						$sqlq .= ' HAVING (COUNT(answer_id)>='.$tsubset_answers.')';
					}
					$sqlq .= ' )';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
336
337
338
				} elseif ($tsubset_type == 4) {
					// ordering question
					// check if the selected question has enough answers
nick's avatar
nick committed
339
					$sqlq .= ' AND question_id IN (
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
340
341
342
343
344
							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
345
							HAVING (COUNT(answer_id)>1))';
nick's avatar
nick committed
346
				}
Nicola Asuni's avatar
Nicola Asuni committed
347
				$sqlq .= $sql_questions_position;
nicolaasuni's avatar
9.0.029    
nicolaasuni committed
348
349
350
351
352
				if (K_DATABASE_TYPE == 'ORACLE') {
					$sqlq = 'SELECT * FROM ('.$sqlq.') WHERE rownum <= '.$tsubset_quantity.'';
				} else {
					$sqlq .= ' LIMIT '.$tsubset_quantity.'';
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
353
				$numofrows = 0;
Nick's avatar
Nick committed
354
355
				if ($rq = F_db_query($sqlq, $db)) {
					if ($mq = F_db_fetch_array($rq)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
356
						$numofrows = $mq['numquestions'];
nick's avatar
nick committed
357
358
					}
				} else {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
359
360
361
362
363
364
365
366
					F_display_db_error();
				}
				if ($numofrows < $tsubset_quantity) {
					F_print_error('WARNING', $l['m_unavailable_questions']);
					break;
				}
				if (!empty($subject_id)) {
					// insert new subject
nick's avatar
nick committed
367
					$sql = 'INSERT INTO '.K_TABLE_TEST_SUBJSET.' (tsubset_test_id,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
368
369
370
371
372
373
374
375
376
377
378
						tsubset_type,
						tsubset_difficulty,
						tsubset_quantity,
						tsubset_answers
						) VALUES (
						\''.$test_id.'\',
						\''.$tsubset_type.'\',
						\''.$tsubset_difficulty.'\',
						\''.$tsubset_quantity.'\',
						\''.$tsubset_answers.'\'
						)';
Nick's avatar
Nick committed
379
					if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
380
381
382
383
384
385
386
387
388
						F_display_db_error(false);
					} 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 (
nick's avatar
nick committed
389
390
								\''.$tsubset_id.'\',
								\''.$subid.'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
391
								)';
Nick's avatar
Nick committed
392
							if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
393
394
395
396
397
398
399
400
401
402
403
								F_display_db_error(false);
							}
						}
					}
				}
			}
		}
		break;
	}

	case 'delete':{
nick's avatar
nick committed
404
		F_stripslashes_formfields();		// ask confirmation
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
405
406
407
408
409
410
411
		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
412
413
		<?php
		F_submit_button('forcedelete', $l['w_delete'], $l['h_delete']);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
414
415
416
417
418
419
420
421
422
423
424
		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
425
		if ($forcedelete == $l['w_delete']) { //check if delete button has been pushed (redundant check)
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
426
427
			// delete test
			$sql = 'DELETE FROM '.K_TABLE_TESTS.' WHERE test_id='.$test_id.'';
Nick's avatar
Nick committed
428
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
429
430
431
432
433
434
435
436
437
438
				F_display_db_error(false);
			} else {
				$test_id = false;
				F_print_error('MESSAGE', $test_name.': '.$l['m_deleted']);
			}
		}
		break;
	}

	case 'update':{ // Update
Nick's avatar
Nick committed
439
440
441
		// check if the confirmation chekbox has been selected
		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
442
			$formstatus = FALSE;
Nick's avatar
Nick committed
443
444
445
			F_stripslashes_formfields();
			break;
		}
Nick's avatar
Nick committed
446
		if ($formstatus = F_check_form_fields()) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
447
			// check referential integrity (NOTE: mysql do not support "ON UPDATE" constraint)
Nick's avatar
Nick committed
448
			if (!F_check_unique(K_TABLE_TEST_USER, 'testuser_test_id='.$test_id.'')) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
449
				F_print_error('WARNING', $l['m_update_restrict']);
nick's avatar
nick committed
450
451
				$formstatus = FALSE;
				F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
452
453
454
				break;
			}
			// check if name is unique
Nick's avatar
Nick committed
455
			if (!F_check_unique(K_TABLE_TESTS, 'test_name=\''.$test_name.'\'', 'test_id', $test_id)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
456
				F_print_error('WARNING', $l['m_duplicate_name']);
nick's avatar
nick committed
457
458
				$formstatus = FALSE;
				F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
459
				break;
nick's avatar
nick committed
460
			}
Nicola Asuni's avatar
Nicola Asuni committed
461
462
463
			if (!empty($new_test_password)) {
				$test_password = getPasswordHash($new_test_password);
			}
nick's avatar
nick committed
464
465
			if ($test_score_threshold > $test_max_score) {
				$test_score_threshold = 0.6 * $test_max_score;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
466
			}
nick's avatar
nick committed
467
			$sql = 'UPDATE '.K_TABLE_TESTS.' SET
tecnickcom's avatar
tecnickcom committed
468
469
				test_name=\''.F_escape_sql($db, $test_name).'\',
				test_description=\''.F_escape_sql($db, $test_description).'\',
Nick's avatar
Nick committed
470
471
				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
472
				test_duration_time=\''.$test_duration_time.'\',
tecnickcom's avatar
tecnickcom committed
473
				test_ip_range=\''.F_escape_sql($db, $test_ip_range).'\',
Nicola Asuni's avatar
Nicola Asuni committed
474
475
				test_results_to_users=\''.intval($test_results_to_users).'\',
				test_report_to_users=\''.intval($test_report_to_users).'\',
nick's avatar
nick committed
476
477
478
				test_score_right=\''.$test_score_right.'\',
				test_score_wrong=\''.$test_score_wrong.'\',
				test_score_unanswered=\''.$test_score_unanswered.'\',
nick's avatar
nick committed
479
				test_max_score=\''.$test_max_score.'\',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
480
				test_score_threshold=\''.$test_score_threshold.'\',
Nicola Asuni's avatar
Nicola Asuni committed
481
482
				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
483
				test_questions_order_mode=\''.$test_questions_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
484
485
				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
486
				test_answers_order_mode=\''.$test_answers_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
487
488
489
490
491
492
493
				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
494
				test_password='.F_empty_to_null($test_password).'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
495
				WHERE test_id='.$test_id.'';
Nick's avatar
Nick committed
496
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
497
498
499
500
				F_display_db_error(false);
			} else {
				F_print_error('MESSAGE', $l['m_updated']);
			}
nick's avatar
nick committed
501

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
502
			// delete previous groups
nick's avatar
nick committed
503
504
			$sql = 'DELETE FROM '.K_TABLE_TEST_GROUPS.'
				WHERE tstgrp_test_id='.$test_id.'';
Nick's avatar
Nick committed
505
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
506
507
508
509
510
511
512
513
514
				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
515
						\''.$test_id.'\',
Nicola Asuni's avatar
Nicola Asuni committed
516
						\''.intval($group_id).'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
517
						)';
Nick's avatar
Nick committed
518
					if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
519
520
521
522
						F_display_db_error(false);
					}
				}
			}
tecnickcom's avatar
tecnickcom committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545

			// 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
546
547
548
549
550
		}
		break;
	}

	case 'add':{ // Add
Nick's avatar
Nick committed
551
		if ($formstatus = F_check_form_fields()) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
552
			// check if name is unique
tecnickcom's avatar
tecnickcom committed
553
			if (!F_check_unique(K_TABLE_TESTS, 'test_name=\''.F_escape_sql($db, $test_name).'\'')) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
554
				F_print_error('WARNING', $l['m_duplicate_name']);
nick's avatar
nick committed
555
556
				$formstatus = FALSE;
				F_stripslashes_formfields();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
557
558
559
560
561
562
				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
563
564
565
			if (!empty($new_test_password)) {
				$test_password = getPasswordHash($new_test_password);
			}
nick's avatar
nick committed
566
567
			$sql = 'INSERT INTO '.K_TABLE_TESTS.' (
			test_name,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
568
569
570
571
572
				test_description,
				test_begin_time,
				test_end_time,
				test_duration_time,
				test_ip_range,
nick's avatar
nick committed
573
574
				test_results_to_users,
				test_report_to_users,
nick's avatar
nick committed
575
576
				test_score_right,
				test_score_wrong,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
577
				test_score_unanswered,
nick's avatar
nick committed
578
579
				test_max_score,
				test_user_id,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
580
581
582
				test_score_threshold,
				test_random_questions_select,
				test_random_questions_order,
Nicola Asuni's avatar
Nicola Asuni committed
583
				test_questions_order_mode,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
584
585
				test_random_answers_select,
				test_random_answers_order,
Nicola Asuni's avatar
Nicola Asuni committed
586
				test_answers_order_mode,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
587
588
589
				test_comment_enabled,
				test_menu_enabled,
				test_noanswer_enabled,
nick's avatar
nick committed
590
				test_mcma_radio,
nick's avatar
nick committed
591
592
				test_repeatable,
				test_mcma_partial_score,
Nicola Asuni's avatar
Nicola Asuni committed
593
594
				test_logout_on_timeout,
				test_password
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
595
				) VALUES (
tecnickcom's avatar
tecnickcom committed
596
597
				\''.F_escape_sql($db, $test_name).'\',
				\''.F_escape_sql($db, $test_description).'\',
Nick's avatar
Nick committed
598
599
				'.F_empty_to_null($test_begin_time).',
				'.F_empty_to_null($test_end_time).',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
600
				\''.$test_duration_time.'\',
tecnickcom's avatar
tecnickcom committed
601
				\''.F_escape_sql($db, $test_ip_range).'\',
Nicola Asuni's avatar
Nicola Asuni committed
602
603
				\''.intval($test_results_to_users).'\',
				\''.intval($test_report_to_users).'\',
nick's avatar
nick committed
604
605
				\''.$test_score_right.'\',
				\''.$test_score_wrong.'\',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
606
				\''.$test_score_unanswered.'\',
nick's avatar
nick committed
607
608
				\''.$test_max_score.'\',
				\''.intval($_SESSION['session_user_id']).'\',
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
609
				\''.$test_score_threshold.'\',
Nicola Asuni's avatar
Nicola Asuni committed
610
611
				\''.intval($test_random_questions_select).'\',
				\''.intval($test_random_questions_order).'\',
Nicola Asuni's avatar
Nicola Asuni committed
612
				\''.$test_questions_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
613
614
				\''.intval($test_random_answers_select).'\',
				\''.intval($test_random_answers_order).'\',
Nicola Asuni's avatar
Nicola Asuni committed
615
				\''.$test_answers_order_mode.'\',
Nicola Asuni's avatar
Nicola Asuni committed
616
617
618
619
620
621
622
				\''.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
623
				'.F_empty_to_null($test_password).'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
624
				)';
Nick's avatar
Nick committed
625
			if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
626
627
628
629
630
631
632
633
634
635
636
				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
637
						\''.$test_id.'\',
Nicola Asuni's avatar
Nicola Asuni committed
638
						\''.intval($group_id).'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
639
						)';
Nick's avatar
Nick committed
640
					if (!$r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
641
642
643
644
						F_display_db_error(false);
					}
				}
			}
nick's avatar
nick committed
645

tecnickcom's avatar
tecnickcom committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
			// 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
662
663
			if (isset($old_test_id) AND ($old_test_id > 0)) {
				// copy here previous selected questions to this new test
Nick's avatar
Nick committed
664
				$sql = 'SELECT *
nick's avatar
nick committed
665
					FROM '.K_TABLE_TEST_SUBJSET.'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
666
					WHERE tsubset_test_id=\''.$old_test_id.'\'';
Nick's avatar
Nick committed
667
				if ($r = F_db_query($sql, $db)) {
nicolaasuni's avatar
nicolaasuni committed
668
					while ($m = F_db_fetch_array($r)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
669
						// insert new subject
nick's avatar
nick committed
670
671
						$sqlu = 'INSERT INTO '.K_TABLE_TEST_SUBJSET.' (
							tsubset_test_id,
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
672
673
674
675
676
677
678
679
680
681
682
							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
683
						if (!$ru = F_db_query($sqlu, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
684
685
686
687
688
689
							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
690
							if ($rs = F_db_query($sqls, $db)) {
nicolaasuni's avatar
nicolaasuni committed
691
								while ($ms = F_db_fetch_array($rs)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
692
693
694
695
									$sqlp = 'INSERT INTO '.K_TABLE_SUBJECT_SET.' (
										subjset_tsubset_id,
										subjset_subject_id
										) VALUES (
nick's avatar
nick committed
696
697
										\''.$tsubset_id.'\',
										\''.$ms['subjset_subject_id'].'\'
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
698
										)';
Nick's avatar
Nick committed
699
									if (!$rp = F_db_query($sqlp, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
										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;
		$test_ip_range = '*.*.*.*';
nick's avatar
nick committed
723
		$test_results_to_users = false;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
724
		$test_report_to_users = false;
nick's avatar
nick committed
725
726
727
		$test_score_right = 1;
		$test_score_wrong = 0;
		$test_score_unanswered = 0;
Nicola Asuni's avatar
Nicola Asuni committed
728
		$test_max_score = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
729
730
731
		$test_score_threshold = 0;
		$test_random_questions_select = true;
		$test_random_questions_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
732
		$test_questions_order_mode = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
733
734
		$test_random_answers_select = true;
		$test_random_answers_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
735
		$test_answers_order_mode = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
736
737
738
		$test_comment_enabled = true;
		$test_menu_enabled = true;
		$test_noanswer_enabled = true;
nick's avatar
nick committed
739
		$test_mcma_radio = true;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
740
		$test_repeatable = false;
nick's avatar
nick committed
741
742
		$test_mcma_partial_score = true;
		$test_logout_on_timeout = false;
Nicola Asuni's avatar
Nicola Asuni committed
743
		$test_password = '';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
744
745
746
		break;
	}

Nicola Asuni's avatar
Nicola Asuni committed
747
748
	default :{
		break;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
749
750
751
752
753
754
755
756
757
758
	}

} //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
759
if ($formstatus) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
760
	if ($menu_mode != 'clear') {
Nick's avatar
Nick committed
761
		if (!isset($test_id) OR empty($test_id)) {
Nick's avatar
Nick committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
			$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;
			$test_ip_range = '*.*.*.*';
			$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
778
			$test_questions_order_mode = 0;
Nick's avatar
Nick committed
779
780
			$test_random_answers_select = true;
			$test_random_answers_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
781
			$test_answers_order_mode = 0;
Nick's avatar
Nick committed
782
783
784
785
786
787
788
			$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
789
			$test_password = '';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
790
		} else {
nick's avatar
nick committed
791
			$sql = 'SELECT * FROM '.K_TABLE_TESTS.' WHERE test_id='.$test_id.' LIMIT 1';
Nick's avatar
Nick committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
			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
810
					$test_questions_order_mode = intval($m['test_questions_order_mode']);
Nick's avatar
Nick committed
811
812
					$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
813
					$test_answers_order_mode = intval($m['test_answers_order_mode']);
Nick's avatar
Nick committed
814
815
816
817
818
819
820
					$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
821
					$test_password = $m['test_password'];
Nick's avatar
Nick committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
				} 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;
					$test_ip_range = '*.*.*.*';
					$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
838
					$test_questions_order_mode = 0;
Nick's avatar
Nick committed
839
840
					$test_random_answers_select = true;
					$test_random_answers_order = true;
Nicola Asuni's avatar
Nicola Asuni committed
841
					$test_answers_order_mode = 0;
Nick's avatar
Nick committed
842
843
844
845
846
847
848
					$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
849
					$test_password = '';
Nick's avatar
Nick committed
850
				}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
851
			} else {
Nick's avatar
Nick committed
852
				F_display_db_error();
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
853
854
855
856
			}
		}
	}
}
nick's avatar
nick committed
857
$millennium = substr(date('Y'), 0, 1);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
858

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

Nick's avatar
Nick committed
861
862
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
863

Nick's avatar
Nick committed
864
865
866
867
868
869
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
870
871
872
873
874
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
875
$sql = F_select_tests_sql();
Nick's avatar
Nick committed
876
if ($r = F_db_query($sql, $db)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
877
	$countitem = 1;
nicolaasuni's avatar
nicolaasuni committed
878
	while ($m = F_db_fetch_array($r)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
879
		echo '<option value="'.$m['test_id'].'"';
Nick's avatar
Nick committed
880
		if ($m['test_id'] == $test_id) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
881
882
883
			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
884
885
886
887
888
		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
889
890
		$countitem++;
	}
Nick's avatar
Nick committed
891
} else {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
892
893
894
	echo '</select></span></div>'.K_NEWLINE;
	F_display_db_error();
}
Nick's avatar
Nick committed
895
echo '</select>'.K_NEWLINE;
Nicola Asuni's avatar
Nicola Asuni committed
896
897
898
899
900

// link for user selection popup
$jsaction = 'selectWindow=window.open(\'tce_select_tests_popup.php?cid=test_id\', \'selectWindow\', \'dependent, height=600, width=800, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no\');return false;';
echo '<a href="#" onclick="'.$jsaction.'" class="xmlbutton" title="'.$l['w_select'].'">...</a>';

Nick's avatar
Nick committed
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
echo '</span>'.K_NEWLINE;
echo '<br /><br />'.K_NEWLINE;
echo '</div>'.K_NEWLINE;

echo getFormNoscriptSelect('selectrecord');

echo '<fieldset>'.K_NEWLINE;
echo '<legend>'.$l['w_test'].'</legend>'.K_NEWLINE;

echo getFormRowTextInput('test_name', $l['w_name'], $l['h_test_name'], '', $test_name, '', 255, false, false, false);
echo getFormRowTextBox('test_description', $l['w_description'], $l['h_test_description'], $test_description, false);
echo getFormRowTextInput('test_begin_time', $l['w_time_begin'], $l['w_time_begin'].' '.$l['w_datetime_format'], '', $test_begin_time, '', 19, false, true, false);
echo getFormRowTextInput('test_end_time', $l['w_time_end'], $l['w_time_end'].' '.$l['w_datetime_format'], '', $test_end_time, '', 19, false, true, false);
echo getFormRowTextInput('test_duration_time', $l['w_test_time'], $l['h_test_time'], '['.$l['w_minutes'].']', $test_duration_time, '^([0-9]*)$', 20, false, false, false);
echo getFormRowTextInput('test_ip_range', $l['w_ip_range'], $l['h_ip_range'], '', $test_ip_range, '^([0-9a-fA-F,\:\.\*-]*)$', 255, false, false, false);

echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="user_groups">'.$l['w_groups'].'</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
echo '<select name="user_groups[]" id="user_groups" size="5" multiple="multiple">'.K_NEWLINE;
nicolaasuni's avatar
nicolaasuni committed
923
924
//$sql = F_user_group_select_sql();
$sql = 'SELECT * FROM '.K_TABLE_GROUPS.' ORDER BY group_name';
Nick's avatar
Nick committed
925
if ($r = F_db_query($sql, $db)) {
nicolaasuni's avatar
nicolaasuni committed
926
	while ($m = F_db_fetch_array($r)) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
927
		echo '<option value="'.$m['group_id'].'"';
nick's avatar
nick committed
928
		if (isset($test_id) AND ($test_id > 0) AND (F_isTestOnGroup($test_id, $m['group_id']))) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
929
930
931
932
			echo ' selected="selected"';
		}
		echo '>'.htmlspecialchars($m['group_name'], ENT_NOQUOTES, $l['a_meta_charset']).'</option>'.K_NEWLINE;
	}
nick's avatar
nick committed
933
} else {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
934
935
936
	echo '</select></span></div>'.K_NEWLINE;
	F_display_db_error();
}
Nick's avatar
Nick committed
937
938
939
940
echo '</select>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;

tecnickcom's avatar
tecnickcom committed
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="sslcerts">'.$l['w_sslcerts'].'</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
echo '<select name="sslcerts[]" id="sslcerts" size="5" multiple="multiple">'.K_NEWLINE;
$sql = 'SELECT * FROM '.K_TABLE_SSLCERTS.' ORDER BY ssl_name';
if ($r = F_db_query($sql, $db)) {
	while ($m = F_db_fetch_array($r)) {
		echo '<option value="'.$m['ssl_id'].'"';
		if (isset($test_id) AND ($test_id > 0) AND (F_isTestOnSSLCerts($test_id, $m['ssl_id']))) {
			echo ' selected="selected"';
		}
		echo '>'.htmlspecialchars($m['ssl_name'].' ('.substr($m['ssl_end_date'],0,10).')', ENT_NOQUOTES, $l['a_meta_charset']).'</option>'.K_NEWLINE;
	}
} else {
	echo '</select></span></div>'.K_NEWLINE;
	F_display_db_error();
}
echo '</select>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;

Nick's avatar
Nick committed
964
965
966
967
968
969
970
971
972
973
echo getFormRowTextInput('test_score_right', $l['w_score_right'], $l['h_score_right'], '', $test_score_right, '^([0-9\+\-]*)([\.]?)([0-9]*)$', 20, false, false, false);
echo getFormRowTextInput('test_score_wrong', $l['w_score_wrong'], $l['h_score_wrong'], '', $test_score_wrong, '^([0-9\+\-]*)([\.]?)([0-9]*)$', 20, false, false, false);
echo getFormRowTextInput('test_score_unanswered', $l['w_score_unanswered'], $l['h_score_unanswered'], '', $test_score_unanswered, '^([0-9\+\-]*)([\.]?)([0-9]*)$', 20, false, false, false);
echo getFormRowTextInput('test_score_threshold', $l['w_test_score_threshold'], $l['h_test_score_threshold'], '', $test_score_threshold, '^([0-9\+\-]*)([\.]?)([0-9]*)$', 20, false, false, false);

echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="test_random_questions_select">'.$l['w_random_questions'].':</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
974
echo '<input type="checkbox" name="test_random_questions_select" id="test_random_questions_select" value="1"';
Nick's avatar
Nick committed
975
if ($test_random_questions_select) {echo ' checked="checked"';}
nick's avatar
nick committed
976
977
978
979
echo ' onclick="JF_check_random_boxes()"';
echo ' title="'.$l['h_random_questions'].'" />';
echo ' <label for="test_random_questions_select">'.$l['w_select'].'</label>'.K_NEWLINE;

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
980
echo ' <input type="checkbox" name="test_random_questions_order" id="test_random_questions_order" value="1"';
Nick's avatar
Nick committed
981
if ($test_random_questions_order) {echo ' checked="checked"';}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
982
echo ' onclick="JF_check_random_boxes()"';
nick's avatar
nick committed
983
echo ' title="'.$l['w_order'].'" />';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
984