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

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

/**
 */

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_bulk_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
46
require_once('../../shared/code/tce_functions_auth_sql.php');
nick's avatar
nick committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require_once('tce_functions_omr.php');
require_once('tce_functions_user_select.php');

if (isset($_REQUEST['date'])) {
	$date = $_REQUEST['date'];
	$date_time = strtotime($date);
	$date = date(K_TIMESTAMP_FORMAT, $date_time);
} else {
	$date = date(K_TIMESTAMP_FORMAT);
}

if (isset($_REQUEST['omrdir']) AND (strpos($_REQUEST['omrdir'], K_PATH_CACHE.'OMR') == 0)) {
	$omrdir = $_REQUEST['omrdir'];
} else {
	$omrdir = K_PATH_CACHE.'OMR/';
}

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

nick's avatar
nick committed
70
71
// process OMR files on the specified directory
if (isset($menu_mode) AND ($menu_mode == 'upload') AND file_exists($omrdir)) {
Nicola Asuni's avatar
Nicola Asuni committed
72
73
	$logfilename = 'log_import_omr_'.time().'.txt';
	$logfile = K_PATH_CACHE.'OMR/'.$logfilename;
nick's avatar
nick committed
74
75
76
77
78
79
80
81
82
83
84
	$dirhdl = @opendir($omrdir);
	if ($dirhdl !== false) {
		while ($file = readdir($dirhdl)) {
			if (($file != '.') AND ($file != '..')) {
				$filename = $omrdir.$file;
				$matches = array();
				if (!is_dir($filename) AND preg_match('/OMR_([^_]+)_QR.([a-zA-Z]+)/', $file, $matches)) {
					// read OMR DATA page
					$omr_testdata = F_decodeOMRTestDataQRCode($filename);
					if ($omr_testdata === false) {
						F_print_error('ERROR', $l['m_omr_wrong_test_data']);
Nicola Asuni's avatar
Nicola Asuni committed
85
						file_put_contents($logfile, 'ERROR'."\t".$file."\t".'UNABLE TO DECODE'."\n", FILE_APPEND);
nick's avatar
nick committed
86
					} else {
Nicola Asuni's avatar
Nicola Asuni committed
87
						file_put_contents($logfile, 'OK'."\t".$file."\t".'SUCCESSFULLY DECODED'."\n", FILE_APPEND);
nick's avatar
nick committed
88
89
90
91
92
93
94
95
96
97
						// 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) {
							$answerfile = 'OMR_'.$matches[1].'_A'.$i.'.'.$matches[2];
							if (file_exists($omrdir.$answerfile)) {
								$answers_page = F_decodeOMRPage($omrdir.$answerfile);
								if (($answers_page !== false) AND !empty($answers_page)) {
									$omr_answers += $answers_page;
Nicola Asuni's avatar
Nicola Asuni committed
98
									file_put_contents($logfile, 'OK'."\t".$answerfile."\t".'SUCCESSFULLY DECODED'."\n", FILE_APPEND);
nick's avatar
nick committed
99
								} else {
tecnickcom's avatar
tecnickcom committed
100
									F_print_error('ERROR', '[OMR ANSWER SHEET '.$answerfile.'] '.$l['m_omr_wrong_answer_sheet']);
Nicola Asuni's avatar
Nicola Asuni committed
101
									file_put_contents($logfile, 'ERROR'."\t".$answerfile."\t".'UNABLE TO DECODE'."\n", FILE_APPEND);
nick's avatar
nick committed
102
103
								}
							} else {
tecnickcom's avatar
tecnickcom committed
104
								F_print_error('ERROR', '[OMR ANSWER SHEET '.$answerfile.'] '.$l['m_omr_wrong_answer_sheet']);
Nicola Asuni's avatar
Nicola Asuni committed
105
								file_put_contents($logfile, 'ERROR'."\t".$answerfile."\t".'MISSING IMAGE FILE'."\n", FILE_APPEND);
nick's avatar
nick committed
106
107
108
109
110
111
112
							}
						}
						// sort answers
						ksort($omr_answers);
						// get user ID from user registration code
						$user_id = F_getUIDfromRegnum($matches[1]);
						// import answers
tecnickcom's avatar
tecnickcom committed
113
						if (($user_id > 0) AND F_isAuthorizedEditorForUser($user_id) AND F_importOMRTestData($user_id, $date, $omr_testdata, $omr_answers, $overwrite)) {
Nicola Asuni's avatar
Nicola Asuni committed
114
115
							F_print_error('MESSAGE', '['.$matches[1].'] '.$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>');
							file_put_contents($logfile, 'OK'."\t".$matches[1]."\t".'SUCCESSFULLY IMPORTED - UID: '.$user_id."\n", FILE_APPEND);
nick's avatar
nick committed
116
						} else {
Nicola Asuni's avatar
Nicola Asuni committed
117
118
							F_print_error('ERROR', '['.$matches[1].'] '.$l['m_import_error']);
							file_put_contents($logfile, 'ERROR'."\t".$matches[1]."\t".'UNABLE TO IMPORT - UID: '.$user_id."\n", FILE_APPEND);
nick's avatar
nick committed
119
120
121
122
123
						}
					}
				} // if QR file
			}
		}
Nicola Asuni's avatar
Nicola Asuni committed
124
125
		// print a link to log file
		F_print_error('MESSAGE', 'LOGFILE: <a href="tce_filemanager.php?d='.urlencode(K_PATH_CACHE.'OMR/').'&f='.urlencode($logfile).'&v=1" title="'.$l['w_select'].'">'.$logfilename.'</a>');
nick's avatar
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
154
155
156
157
158
	}
}

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

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;

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

if (file_exists(K_PATH_CACHE.'OMR')) {
	// directory containing files to import
	$dirs = array('OMR/');
	$dirhdl = @opendir(K_PATH_CACHE.'OMR/');
	if ($dirhdl !== false) {
		while ($file = readdir($dirhdl)) {
			if (($file != '.') AND ($file != '..')) {
				$filename = K_PATH_CACHE.'OMR/'.$file.'/';
				if (is_dir($filename)) {
					$dirs[$filename] = 'OMR/'.$file.'/';
				}
			}
		}
		// sort files alphabetically
		natcasesort($dirs);
		echo getFormRowSelectBox('omrdir', $l['w_omr_dir'], $l['h_omr_dir'], '', $omrdir, $dirs, '');
	}
}

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

nick's avatar
nick committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// -----------------------------------------------------------------------------

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;

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

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

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