@@ -141,25 +141,38 @@ private function validate_csv($csv_data) {
141
141
142
142
//Validate CSV
143
143
$ validate_num_fields = VALIDATE_NUM_FIELDS ;
144
- $ validation_flag = true ;
144
+ $ validation_flag = true ; //Set to false to invalidate the entire CSV file.
145
+ $ rpi_found_non_empty_row = false ; //RPI edge case flag.
145
146
foreach ($ csv_data as $ index => $ csv_row ) {
146
- //Split each row by delim character so that individual fields are indexed .
147
- //Trim any extraneous whitespaces from all rows and fields .
148
- $ row = array ();
149
- foreach ( explode ( CSV_DELIM_CHAR , trim ( $ csv_row )) as $ i => $ field ) {
150
- $ row [ $ i ] = trim ( $ field );
151
- }
147
+ // 1) Trim CSV row. Do not trim CSV_DELIM_CHAR .
148
+ // 2) Convert CSV row to array .
149
+ // 3) Trim array fields.
150
+ $ trim_str = " \t\n\r\0\x0B" ; //$trim_str = space, tab, newline, carriage return, null byte, vertical tab
151
+ $ trim_str = str_replace ( CSV_DELIM_CHAR , "" , $ trim_str ); //remove CSV_DELIM_CHAR from $trim_str
152
+ $ row = array_map ( ' trim ' , explode ( CSV_DELIM_CHAR , trim ( $ csv_row , $ trim_str )));
152
153
153
154
//BEGIN VALIDATION
154
155
//Invalidate any row that doesn't have requisite number of fields. Do this, first.
155
156
//Invalidation will disqualify the data file to protect DB data integrity.
156
157
$ num_fields = count ($ row );
157
158
if ($ num_fields !== $ validate_num_fields ) {
158
- $ this ->log_it ("Row {$ index } has {$ num_fields } columns. {$ validate_num_fields } expected. " );
159
+ $ this ->log_it ("Row {$ index } has {$ num_fields } columns. {$ validate_num_fields } expected. CSV disqualified. " );
159
160
$ validation_flag = false ;
160
161
continue ;
162
+ } else if (empty (array_filter ($ row , function ($ field ) { return !empty ($ field ); }))) {
163
+ if (!$ rpi_found_non_empty_row ) {
164
+ // RPI edge case to skip a correctly sized row of all empty fields — at the top of a data file, before proper data is read — without invalidating the whole data file.
165
+ $ this ->log_it ("Row {$ index } is correct size ( {$ validate_num_fields }), but all columns are empty — at top of CSV. Ignoring row. " );
166
+ continue ;
167
+ } else {
168
+ // Correctly sized empty row below data row(s) — invalidate data file.
169
+ $ this ->log_it ("Row {$ index } is correct size ( {$ validate_num_fields }), but all columns are empty — below a non-empty data row. CSV disqualified. " );
170
+ $ validation_flag = false ;
171
+ continue ;
172
+ }
161
173
}
162
174
175
+ $ rpi_found_non_empty_row = true ;
163
176
$ course = strtolower ($ row [COLUMN_COURSE_PREFIX ]) . $ row [COLUMN_COURSE_NUMBER ];
164
177
// Remove any leading zeroes from "integer" registration sections.
165
178
$ section = (ctype_digit ($ row [COLUMN_SECTION ])) ? ltrim ($ row [COLUMN_SECTION ], "0 " ) : $ row [COLUMN_SECTION ];
@@ -222,6 +235,7 @@ private function validate_csv($csv_data) {
222
235
223
236
//Validation passed. Include row in data set.
224
237
self ::$ data ['users ' ][] = array ('user_id ' => $ row [COLUMN_USER_ID ],
238
+ 'user_numeric_id ' => $ row [COLUMN_NUMERIC_ID ],
225
239
'user_firstname ' => $ row [COLUMN_FIRSTNAME ],
226
240
'user_preferredname ' => $ row [COLUMN_PREFERREDNAME ],
227
241
'user_lastname ' => $ row [COLUMN_LASTNAME ],
@@ -451,6 +465,7 @@ private function upsert_psql() {
451
465
$ sql ['users ' ]['temp_table ' ] = <<<SQL
452
466
CREATE TEMPORARY TABLE upsert_users (
453
467
user_id VARCHAR,
468
+ user_numeric_id VARCHAR,
454
469
user_firstname VARCHAR,
455
470
user_preferred_firstname VARCHAR,
456
471
user_lastname VARCHAR,
@@ -484,7 +499,7 @@ private function upsert_psql() {
484
499
SQL ;
485
500
486
501
$ sql ['users ' ]['data ' ] = <<<SQL
487
- INSERT INTO upsert_users VALUES ($1,$2,$3,$4,$5);
502
+ INSERT INTO upsert_users VALUES ($1,$2,$3,$4,$5,$6 );
488
503
SQL ;
489
504
490
505
$ sql ['courses_users ' ]['data ' ] = <<<SQL
@@ -509,6 +524,7 @@ private function upsert_psql() {
509
524
$ sql ['users ' ]['update ' ] = <<<SQL
510
525
UPDATE users
511
526
SET
527
+ user_numeric_id=upsert_users.user_numeric_id,
512
528
user_firstname=upsert_users.user_firstname,
513
529
user_lastname=upsert_users.user_lastname,
514
530
user_preferred_firstname=
@@ -543,12 +559,14 @@ private function upsert_psql() {
543
559
$ sql ['users ' ]['insert ' ] = <<<SQL
544
560
INSERT INTO users (
545
561
user_id,
562
+ user_numeric_id,
546
563
user_firstname,
547
564
user_lastname,
548
565
user_preferred_firstname,
549
566
user_email
550
567
) SELECT
551
568
upsert_users.user_id,
569
+ upsert_users.user_numeric_id,
552
570
upsert_users.user_firstname,
553
571
upsert_users.user_lastname,
554
572
upsert_users.user_preferred_firstname,
0 commit comments