-
Notifications
You must be signed in to change notification settings - Fork 173
/
NDB_BVL_Instrument_UPLOADER_TEMPLATE.class.inc
231 lines (195 loc) · 7.09 KB
/
NDB_BVL_Instrument_UPLOADER_TEMPLATE.class.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
125
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* This file contains the NDB_BVL_Instrument_TEMPLATE
* class
*
* PHP Version 5
*
* @category Instrument
* @package TEMPLATE
* @author Author Name
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/loris/
*/
/**
* Creates the form elements for the Boston_Diagnostic_Aphasia_Exam instrument
*
* @category Instrument
* @package TEMPLATE
* @author Author Name
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/loris/
*/
class NDB_BVL_Instrument_TEST_NAME extends NDB_BVL_Instrument
{
use \LorisFormDictionaryImpl;
/**
* sets up basic data.
*
* @param string $commentID the CommentID identifying the data to load
* @param string $page if a multipage form, the page to show
* @return void
* @access public
*/
function setup(?string $commentID = null, ?string $page = null): void
{
$this->formType="XIN";
$this->form = new LorisForm('test_form');
$this->page = $page; // page label (number or
// string - used by
// user-defined child classes)
// set the object properties
$this->testName = "<TEST_NAME>"; // test_names.Test_name
$this->table = '<TEST_NAME>'; // name of database table corresponding to instrument
// data keyed by commentID
$this->commentID = $commentID;
//The array of selects with multiple answers allowed
$this->selectMultipleElements = array();
// required fields for data entry completion status
$this->_requiredElements = array('Examiner', '<FIRST QUESTION OF EACH PAGE>');
// setup the form
$this->_setupForm();
}
//If the instrument is not paged, remove the switch from the _setupForm method and add all the form Elements in this function.
/**
* method to build the object into a paginated form
*
* @return void
* @access private
*/
function _setupForm(){
switch($this->page) {
case "<TEST_NAME>_page1":
$this->_page1();
break;
default:
$this->_main();
break;
}
//Defines the call back function to use when validating the form.
$this->form->addFormRule(array(&$this,'XINValidate'));
}
/**
* generates the main page of the form.
*
* @return void
* @access private
*
*/
function _main(){
// display test name
$this->addHeader("<INSTRUMENT TITLE>");
// automatically adds examiner & date of administration
$this->_addMetadataFields();
}
function _page1(){
//add form Elements here as needed.
//continue onto further pages, if needed.
//upload file
$this->addFile('file', 'upload_file', 'Upload a file');
}
function _saveValues($values) :void
{
$timepoint = TimePoint::singleton($this->getSessionID());
$candidate = Candidate::singleton($timepoint->getCandID());
if(isset($values['upload_file']) && !empty($values['upload_file']['name'])) {
require_once "File_Upload.class.inc";
$file=new File_Upload;
//pass the existing form ($form) by reference to File_Upload, and register the file_upload field names being used.
$file->registerForm($this->form);
//Tell File_Upload what file handlers to use.
$file->setFileHandler("upload_file", $this);
//Set the target directory that you want files moved into once they are validated and processed.
$config = NDB_Config::singleton();
$dir = $config->getSetting("UploadDir");
if(empty($dir)) {
$dir = ".";
};
$file->setBaseUploadDirectory($dir . "/<TEST_NAME>/");
//Set the prefix to prepend to the filenames
//$file->setFilenamePrefix($timepoint->getVisitLabel()."-".$this->testName."-");
//set the the IDs to the handler functions.
$user =& User::singleton();
$file->setHandlerArgs(array("CommentID"=>$this->getCommentID(),
"candID"=>$candidate->getCandID(),
"PSCID"=>$candidate->getPSCID(),
"visitLabel"=>$timepoint->getVisitLabel(),
"username"=>$user->getUsername(),
"values"=>$values)
);
//If the form is validated, call File_Upload::processFiles() which loops through the files and
//proccesses them (including verify, move, and import steps)
$file->processFiles();
}
//echo error messages
foreach ($file->errorLog AS $fileType => $fileErrors) {
foreach ($fileErrors AS $error) {
echo "<span style='color:red'><b>Upload Error</b> $fileType: $error</span><br>";
}
}
unset(
$values['candID'], $values['sessionID'], $values['commentID'], $values['test_name'],
$values['page'], $values['fire_away'], $values['subtest'], $values['MAX_FILE_SIZE'],
$values['upload_file']
);
$this->_save($values);
}
/**
* getTargetDirectory retrieves info about where the file should be stored.
*
* The returned directory is ADDED to the baseUploadDirectory property. It should have a trailing slash.
*
* @param string $file The full filename including directory.
*
* @return bool $success if operation succeeded
* @access public
*/
function getTargetDirectory(&$file, $args){
$output=$args['candID']."/";
return $output;
}
/*
* Import data from the uploaded file
*
* @param $file Reference to file object
* @param $args An associative array set by setHandlerArgs function
*
* @return true If operation succeeded
*/
function importFile(&$file, $args) {
$fp=fopen($file->fileInfo['tmp_name'], "r");
$db= $this->loris->getDatabaseConnection();
///Setting trackchanges to false because getting error messages
$db->_trackChanges = false;
////////////////////////////////////////////////////////////////
$upload_values = array(); //uploading values to <TEST_NAME> table
/*parse the file and store the values to upload in the array above
* and save to the database
*/
// update database
$result = $db->update($this->testName, $upload_values, array('CommentID'=>$args['CommentID']));
echo "<span style='color:red'><b>Upload Successful : </b>".$upload_values[$field]."</span><br>";
return true;
}
/**
* {@inheritDoc}
*
* @return string|null
*/
public function getFullName(): ?string
{
return "UPLOADER TEMPLATE TEST NAME";
}
/**
* {@inheritDoc}
*
* @return array
*/
function getSubtestList(): array
{
$subtest = [
['Name'=>'<TEST_NAME>_page1','Description'=>'Subtest One'],
];
return $subtest;
}
}