@@ -151,9 +151,18 @@ private function validate_csv($csv_data) {
151
151
}
152
152
153
153
//BEGIN VALIDATION
154
- $ course = strtolower ( $ row[ COLUMN_COURSE_PREFIX ]) . $ row [ COLUMN_COURSE_NUMBER ];
155
- $ section = intval ( $ row [ COLUMN_SECTION ]); //intval($str) returns zero when $str is not integer .
154
+ //Invalidate any row that doesn't have requisite number of fields. Do this, first.
155
+ //Invalidation will disqualify the data file to protect DB data integrity .
156
156
$ num_fields = count ($ row );
157
+ if ($ num_fields !== $ validate_num_fields ) {
158
+ $ this ->log_it ("Row {$ index } has {$ num_fields } columns. {$ validate_num_fields } expected. " );
159
+ $ validation_flag = false ;
160
+ continue ;
161
+ }
162
+
163
+ $ course = strtolower ($ row [COLUMN_COURSE_PREFIX ]) . $ row [COLUMN_COURSE_NUMBER ];
164
+ // Remove any leading zeroes from "integer" registration sections.
165
+ $ section = (ctype_digit ($ row [COLUMN_SECTION ])) ? ltrim ($ row [COLUMN_SECTION ], "0 " ) : $ row [COLUMN_SECTION ];
157
166
158
167
//Row validation filters. If any prove false, row is discarded.
159
168
switch (false ) {
@@ -163,15 +172,9 @@ private function validate_csv($csv_data) {
163
172
//Check that row shows student is registered.
164
173
case (in_array ($ row [COLUMN_REGISTRATION ], unserialize (STUDENT_REGISTERED_CODES ))):
165
174
break ;
166
- //Validate expected number of fields
167
- case ($ num_fields === $ validate_num_fields ):
168
- //Log that row is invalid per number of columns
169
- $ this ->log_it ("Row {$ index } has {$ num_fields } columns. {$ validate_num_fields } expected. " );
170
- $ validation_flag = false ;
171
- break ;
172
- //Check row columns
175
+ //Row is OK, next check row columns.
173
176
default :
174
- //Column validation filters. If any prove false, the entire row is discarded .
177
+ //Column validation filters. If any prove false, the entire data file will be disqualified .
175
178
switch (false ) {
176
179
//Check term code (skips when set to null).
177
180
case ((is_null (EXPECTED_TERM_CODE )) ? true : ($ row [COLUMN_TERM_CODE ] === EXPECTED_TERM_CODE )):
@@ -210,7 +213,7 @@ private function validate_csv($csv_data) {
210
213
$ tmp_course = $ course ;
211
214
$ tmp_section = $ section ;
212
215
$ course = self ::$ course_mappings [$ tmp_course ][$ tmp_section ]['mapped_course ' ];
213
- $ section = intval ( self ::$ course_mappings [$ tmp_course ][$ tmp_section ]['mapped_section ' ]) ;
216
+ $ section = self ::$ course_mappings [$ tmp_course ][$ tmp_section ]['mapped_section ' ];
214
217
} else {
215
218
$ this ->log_it ("{$ course } has been mapped. Section {$ section } is in feed, but not mapped. " );
216
219
$ validation_flag = false ;
@@ -440,7 +443,6 @@ private function deduplicate($subset = 'users', $key = 'user_id') {
440
443
* @return boolean true when upsert is complete
441
444
*/
442
445
private function upsert_psql () {
443
-
444
446
$ sql = array ('begin ' => 'BEGIN ' ,
445
447
'commit ' => 'COMMIT ' ,
446
448
'rollback ' => 'ROLLBACK ' );
@@ -466,11 +468,11 @@ private function upsert_psql() {
466
468
467
469
$ sql ['courses_users ' ]['temp_table ' ] = <<<SQL
468
470
CREATE TEMPORARY TABLE upsert_courses_users (
469
- semester VARCHAR,
470
- course VARCHAR,
471
+ semester VARCHAR(255) ,
472
+ course VARCHAR(255) ,
471
473
user_id VARCHAR,
472
474
user_group INTEGER,
473
- registration_section VARCHAR,
475
+ registration_section VARCHAR(255) ,
474
476
manual_registration BOOLEAN
475
477
) ON COMMIT DROP
476
478
SQL ;
@@ -682,7 +684,6 @@ private function upsert_psql() {
682
684
pg_query (self ::$ db , $ sql ['courses_users ' ]['lock ' ]);
683
685
switch (false ) {
684
686
case pg_query (self ::$ db , $ sql ['registration_section ' ]['insert ' ]):
685
-
686
687
pg_query (self ::$ db , $ sql ['rollback ' ]);
687
688
break ;
688
689
case pg_query (self ::$ db , $ sql ['courses_users ' ]['update ' ]):
0 commit comments