aboutsummaryrefslogtreecommitdiff
path: root/gradescope2gradebook.tcl
blob: beeacbb7b4a820811f294ca10b828fd1a51b5932 (plain)
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
#!/bin/sh
# (C) 2011 by Eugeniy Mikhailov, <evgmik@gmail.com>
# vim:set ft=tcl: \
exec tclsh "$0" "$@"

package require Tcl 8.5
package require json::write
package require sqlite3
package require md5
source ./GradeBook_lib.tcl

if { $argc < 2 } {	
	puts {Usage:}
	puts "  $argv0 gradebook.sqlitedb gradescope.sqlitedb"
	puts {}
	puts {  expect both files to be sqilte tables:}
	puts {  gradescope should have a single table 'export_table':}
	puts {Example:}
	puts [concat "  $argv0" { 2020_Fall_Phys251 gradescope.db}]
	exit
}

set classDB [lindex $argv 0] 
set gradescopeDB [lindex $argv 1]

sqlite3 db $classDB
sqlite3 gdb $gradescopeDB

proc iferror { err errStat {eval_str {""} }} {
        #return; # comment out when debugging
        if { $err } {
                puts "Error: $errStat"
                if { $eval_str ne "" } {
                        puts "For query: $eval_str"
                }
        }
}

proc getColListFromAnyTable {db table} {
	set all_column_names ""
	set eval_str [concat SELECT * FROM \'$table\' LIMIT 1]
	set err [catch {
		$db eval $eval_str v {
			set all_column_names $v(*)
		}
	} errStat ]
	iferror $err $errStat
	if { $err } { return false }
	return $all_column_names
}

proc getGradescopeUsernames { db } {
	set username_list {}
	set eval_str [concat SELECT Email FROM export_table]
	set err [catch {
		$db eval $eval_str v {
			lappend username_list $v(Email)
		}
	} errStat ]
	iferror $err $errStat
	if { $err } { return false }
	return $username_list
}

proc getGradescopeUserGrade { db uname col } {
	set username_list {}
	set eval_str [concat SELECT \"$col\" FROM export_table where Email=='$uname']
	set val [$db onecolumn $eval_str]
	return $val
}

proc isInList { col listCol} {
	foreach sCol $listCol {
		set result [regexp $sCol $col match]
		if { $result } {
			return true
		}
	}
	return false

}

proc trimColName { col } {
	set result [regexp -nocase {(.*) due week.*} $col match submatch]
	if { $result} {
		#puts $col
		#puts $match
		#puts $submatch
		return $submatch
	}
	return $col
}


set commonInfoCol {{First Name} {Last Name} {SID} {Email}}
set infoColumsMarkers {{ - Lateness \(H:M:S\)} { - Max Points} { - Submission Time} {Total Lateness \(H:M:S\)}}

set skipCreationCol [concat $commonInfoCol $infoColumsMarkers]

## adding students if they do not exist
set fname [list First Name]
set lname [list Last Name]
set idnum [list SID]
set eval_str [concat SELECT * FROM 'export_table']
set err [catch {
	gdb eval $eval_str v {
		#AddUserNonWeb $fname $lname $email student $id $sect
		AddUserNonWeb $v($fname) $v($lname) $v(Email) student $v($idnum) 1
		#addStudent db $v($idnum)  $v(Contact)
		}
	} errStat ]
iferror $err $errStat


# add columns with reports to gradebook db
foreach col [getColListFromAnyTable gdb export_table] {
	if { [isInList $col $skipCreationCol] } {
		continue
	}
	#puts "$col"
	set shortCol [trimColName $col]
	#puts "$col => $shortCol"

	if { ![doesColumnExists $shortCol GradesTable] } {
		puts "Adding: ${shortCol}"
		set result [regexp -nocase "pre-lab" $shortCol match]
		if { $result} {
			AddColumnNonWeb $shortCol Quiz 10
			continue
		}
		# everything else is labreport
		AddColumnNonWeb $shortCol LabReport 100
	} else {
		#puts "$col: exists"
	}
}

# now for every user add their grades
set username_list [getGradescopeUsernames gdb]
foreach uname $username_list {
	foreach col [getColListFromAnyTable gdb export_table] {
		if { [isInList $col $skipCreationCol] } {
			continue
		}
		set shortCol [trimColName $col]
		set grade [getGradescopeUserGrade gdb $uname $col ]
		if { "" ne $grade } {
			puts "setting $grade for $uname in $col"
			UpdateColValue4UserNameNonWeb $shortCol $uname $grade
		}
	}
}