Skip to content

Commit 6cee321

Browse files
pbailiebmcutler
authored andcommitted
Update submitty_student_auto_feed.php (#14)
A few maintenance fixes.
1 parent 889ea0a commit 6cee321

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

student_auto_feed/submitty_student_auto_feed.php

100644100755
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,18 @@ private function validate_csv($csv_data) {
151151
}
152152

153153
//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.
156156
$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];
157166

158167
//Row validation filters. If any prove false, row is discarded.
159168
switch(false) {
@@ -163,15 +172,9 @@ private function validate_csv($csv_data) {
163172
//Check that row shows student is registered.
164173
case (in_array($row[COLUMN_REGISTRATION], unserialize(STUDENT_REGISTERED_CODES))):
165174
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.
173176
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.
175178
switch(false) {
176179
//Check term code (skips when set to null).
177180
case ((is_null(EXPECTED_TERM_CODE)) ? true : ($row[COLUMN_TERM_CODE] === EXPECTED_TERM_CODE)):
@@ -210,7 +213,7 @@ private function validate_csv($csv_data) {
210213
$tmp_course = $course;
211214
$tmp_section = $section;
212215
$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'];
214217
} else {
215218
$this->log_it("{$course} has been mapped. Section {$section} is in feed, but not mapped.");
216219
$validation_flag = false;
@@ -440,7 +443,6 @@ private function deduplicate($subset = 'users', $key = 'user_id') {
440443
* @return boolean true when upsert is complete
441444
*/
442445
private function upsert_psql() {
443-
444446
$sql = array('begin' => 'BEGIN',
445447
'commit' => 'COMMIT',
446448
'rollback' => 'ROLLBACK');
@@ -466,11 +468,11 @@ private function upsert_psql() {
466468

467469
$sql['courses_users']['temp_table'] = <<<SQL
468470
CREATE TEMPORARY TABLE upsert_courses_users (
469-
semester VARCHAR,
470-
course VARCHAR,
471+
semester VARCHAR(255),
472+
course VARCHAR(255),
471473
user_id VARCHAR,
472474
user_group INTEGER,
473-
registration_section VARCHAR,
475+
registration_section VARCHAR(255),
474476
manual_registration BOOLEAN
475477
) ON COMMIT DROP
476478
SQL;
@@ -682,7 +684,6 @@ private function upsert_psql() {
682684
pg_query(self::$db, $sql['courses_users']['lock']);
683685
switch (false) {
684686
case pg_query(self::$db, $sql['registration_section']['insert']):
685-
686687
pg_query(self::$db, $sql['rollback']);
687688
break;
688689
case pg_query(self::$db, $sql['courses_users']['update']):

0 commit comments

Comments
 (0)