tce_import_omr_answers.php 7.33 KB
Newer Older
Nick's avatar
11.2  
Nick committed
1
2
3
4
<?php
//============================================================+
// File name   : tce_import_omr_answers.php
// Begin       : 2011-05-20
tecnickcom's avatar
tecnickcom committed
5
// Last Update : 2014-05-14
Nick's avatar
11.2  
Nick committed
6
7
8
9
10
11
12
13
//
// Description : Import test answers using OMR (Optical Mark Recognition)
//               technique applied to images of scanned answer sheets.
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
Nick's avatar
Nick committed
14
//               Tecnick.com LTD
Nick's avatar
11.2  
Nick committed
15
16
17
18
//               www.tecnick.com
//               info@tecnick.com
//
// License:
tecnickcom's avatar
tecnickcom committed
19
//    Copyright (C) 2004-2013 Nicola Asuni - Tecnick.com LTD
Nick's avatar
11.2  
Nick committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//    See LICENSE.TXT file for more information.
//============================================================+

/**
 * @file
 * Import test answers using OMR (Optical Mark Recognition) technique applied to images of scanned answer sheets.
 * @package com.tecnick.tcexam.admin
 * @author Nicola Asuni
 * @since 2011-05-20
 */

/**
 */

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

$pagelevel = K_AUTH_ADMIN_OMR_IMPORT;
$enable_calendar = true;
$max_omr_sheets = 10;
require_once('../../shared/code/tce_authorization.php');

$thispage_title = $l['t_omr_answers_importer'];
require_once('tce_page_header.php');
require_once('../../shared/code/tce_functions_form.php');
require_once('../../shared/code/tce_functions_tcecode.php');
Nicola Asuni's avatar
Nicola Asuni committed
45
require_once('../../shared/code/tce_functions_auth_sql.php');
Nick's avatar
11.2  
Nick committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require_once('tce_functions_omr.php');
require_once('tce_functions_user_select.php');

if (isset($_REQUEST['user_id'])) {
	$user_id = intval($_REQUEST['user_id']);
	if (!F_isAuthorizedEditorForUser($user_id)) {
		F_print_error('ERROR', $l['m_authorization_denied']);
		exit;
	}
} else {
	$user_id = 0;
}
if (isset($_REQUEST['date'])) {
	$date = $_REQUEST['date'];
Nick's avatar
Nick committed
60
61
	$date_time = strtotime($date);
	$date = date(K_TIMESTAMP_FORMAT, $date_time);
Nick's avatar
11.2  
Nick committed
62
63
64
65
} else {
	$date = date(K_TIMESTAMP_FORMAT);
}

tecnickcom's avatar
tecnickcom committed
66
67
68
69
70
71
if (!isset($_REQUEST['overwrite']) OR (empty($_REQUEST['overwrite']))) {
	$overwrite = false;
} else {
	$overwrite = F_getBoolean($_REQUEST['overwrite']);
}

nick's avatar
nick committed
72
// process uploaded files
Nick's avatar
11.2  
Nick committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
if (isset($menu_mode) AND ($menu_mode == 'upload') AND ($user_id > 0) AND !empty($_FILES)) {
	// read OMR DATA page
	$omr_testdata = F_decodeOMRTestDataQRCode($_FILES['omrfile']['tmp_name'][0]);
	if ($omr_testdata === false) {
		F_print_error('ERROR', $l['m_omr_wrong_test_data']);
	} else {
		// read OMR ANSWER SHEET pages
		$num_questions = (count($omr_testdata) - 1);
		$num_pages = ceil($num_questions / 30);
		$omr_answers = array();
		for ($i = 1; $i <= $num_pages; ++$i) {
			if ($_FILES['omrfile']['error'][$i] == 0) {
				$answers_page = F_decodeOMRPage($_FILES['omrfile']['tmp_name'][$i]);
				if (($answers_page !== false) AND !empty($answers_page)) {
					$omr_answers += $answers_page;
				} else {
tecnickcom's avatar
tecnickcom committed
89
					F_print_error('ERROR', '[OMR ANSWER SHEET '.$i.'] '.$l['m_omr_wrong_answer_sheet']);
Nick's avatar
11.2  
Nick committed
90
91
				}
			} else {
tecnickcom's avatar
tecnickcom committed
92
				F_print_error('ERROR', '[OMR ANSWER SHEET '.$i.'] '.$l['m_omr_wrong_answer_sheet']);
Nick's avatar
11.2  
Nick committed
93
94
95
96
97
			}
		}
		// sort answers
		ksort($omr_answers);
		// import answers
tecnickcom's avatar
tecnickcom committed
98
		if (F_importOMRTestData($user_id, $date, $omr_testdata, $omr_answers, $overwrite)) {
Nick's avatar
11.2  
Nick committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
			F_print_error('MESSAGE', $l['m_import_ok'].': <a href="tce_show_result_user.php?testuser_id=32&test_id='.$omr_testdata[0].'&user_id='.$user_id.'" title="'.$l['t_result_user'].'" style="text-decoration:underline;color:#0000ff;">'.$l['w_results'].'</a>');
		} else {
			F_print_error('ERROR', $l['m_import_error']);
		}
	}
	// remove uploaded files
	for ($i = 0; $i <= $max_omr_sheets; ++$i) {
		if ($_FILES['omrfile']['error'][$i] == 0) {
			@unlink($_FILES['omrfile']['tmp_name'][$i]);
		}
	}
}

// -----------------------------------------------------------------------------

echo '<div class="container">'.K_NEWLINE;

echo '<div class="tceformbox">'.K_NEWLINE;
echo '<form action="'.$_SERVER['SCRIPT_NAME'].'" method="post" enctype="multipart/form-data" id="form_omrimport">'.K_NEWLINE;

// select user
echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="user_id">'.$l['w_user'].'</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
nicolaasuni's avatar
nicolaasuni committed
125
echo '<select name="user_id" id="user_id" size="0" onchange="">'.K_NEWLINE;
Nick's avatar
11.2  
Nick committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
$sql = 'SELECT user_id, user_lastname, user_firstname, user_name FROM '.K_TABLE_USERS.' WHERE (user_id>1)';
if ($_SESSION['session_user_level'] < K_AUTH_ADMINISTRATOR) {
	// filter for level
	$sql .= ' AND ((user_level<'.$_SESSION['session_user_level'].') OR (user_id='.$_SESSION['session_user_id'].'))';
	// filter for groups
	$sql .= ' AND user_id IN (SELECT tb.usrgrp_user_id
		FROM '.K_TABLE_USERGROUP.' AS ta, '.K_TABLE_USERGROUP.' AS tb
		WHERE ta.usrgrp_group_id=tb.usrgrp_group_id
			AND ta.usrgrp_user_id='.intval($_SESSION['session_user_id']).'
			AND tb.usrgrp_user_id=user_id)';
}
$sql .= ' ORDER BY user_lastname, user_firstname, user_name';
if ($r = F_db_query($sql, $db)) {
	$countitem = 1;
	echo '<option value="0">&nbsp;</option>'.K_NEWLINE;
	while($m = F_db_fetch_array($r)) {
		echo '<option value="'.$m['user_id'].'"';
		//if ($m['user_id'] == $user_id) {
		//	echo ' selected="selected"';
		//}
		echo '>'.$countitem.'. '.htmlspecialchars($m['user_lastname'].' '.$m['user_firstname'].' - '.$m['user_name'].'', ENT_NOQUOTES, $l['a_meta_charset']).'</option>'.K_NEWLINE;
		$countitem++;
	}
} else {
	echo '</select></span></div>'.K_NEWLINE;
	F_display_db_error();
}
echo '</select>'.K_NEWLINE;
nicolaasuni's avatar
nicolaasuni committed
154
155
156
157
158

// link for user selection popup
$jsaction = 'selectWindow=window.open(\'tce_select_users_popup.php?cid=user_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
11.2  
Nick committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
echo '</span>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;

// -----------------------------------------------------------------------------
// date
echo getFormRowTextInput('date', $l['w_date'], $l['w_date'].' '.$l['w_datetime_format'], '', $date, '', 19, false, true, false);

// OMR DATA page
echo getFormUploadFile('omrfile[]', 'omrdata', $l['w_omr_data_page'], $l['h_omr_data_page'], '');

// OMR ANSWER SHEET pages
for ($i = 1; $i < $max_omr_sheets; ++$i) {
	echo getFormUploadFile('omrfile[]', 'omrsheet'.$i, $l['w_omr_answer_sheet'].' '.$i, '', 'document.getElementById(\'divomrsheet'.($i+1).'\').style.display=\'block\';');
}
echo getFormUploadFile('omrfile[]', 'omrsheet'.$max_omr_sheets, $l['w_omr_answer_sheet'].' '.$max_omr_sheets, '', '');

tecnickcom's avatar
tecnickcom committed
175
176
echo getFormRowCheckBox('overwrite', $l['w_overwrite'], $l['h_omr_overwrite'], '', 1, $overwrite, false, '');

Nick's avatar
11.2  
Nick committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// -----------------------------------------------------------------------------

echo '<div class="row">'.K_NEWLINE;
echo '<br />'.K_NEWLINE;
// show upload button
F_submit_button('upload', $l['w_upload'], $l['h_submit_file']);
echo '</div>'.K_NEWLINE;

echo '</form>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;

// hide unused file upload fields
echo '<script type="text/javascript">'.K_NEWLINE;
echo '//<![CDATA['.K_NEWLINE;
echo 'for (i=2; i<='.$max_omr_sheets.'; i++) {document.getElementById(\'divomrsheet\'+i).style.display=\'none\';}'.K_NEWLINE;
echo '//]]>'.K_NEWLINE;
echo '</script>'.K_NEWLINE;

echo '<div class="pagehelp">'.$l['hp_omr_answers_importer'].'</div>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;

require_once('../code/tce_page_footer.php');

//============================================================+
// END OF FILE
//============================================================+