diff options
-rwxr-xr-x | GradeBook_lib.tcl | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/GradeBook_lib.tcl b/GradeBook_lib.tcl index a101eca..cc455da 100755 --- a/GradeBook_lib.tcl +++ b/GradeBook_lib.tcl @@ -107,6 +107,26 @@ proc SelectColvalueFromTable { table column_of_interest col row_value } { return $value } +proc existsColumnWithRowvalueInTable { table column row_value } { + set sql_str [concat SELECT 1 FROM \'$table\' WHERE \"$column\"=\"$row_value\"] + if {![db exists $sql_str]} { + dbg "Column \'$column\' does not have row with value \'$row_value\' in table \'$table\'" 3 + return false + } else { + return true + } +} + +proc UpdateColumnWithValueInTableWhere { table column val where_column row_value } { + set eval_str [concat UPDATE \'$table\' SET \"$column\"='$val' WHERE \"$where_column\"=\'$row_value\'] + set err [catch {db eval $eval_str } errStat] + if { $err } { + set msg_str "Unable to update column $column in the table $table where $where_column=$row_value. The following error happen: $errStat" + htmlErrorMsg $msg_str + dbg $msg_str 3 + } +} + proc doesColumnExists {col table} { if { $col in [getColListFromTable $table] } { return true; @@ -391,33 +411,23 @@ proc GetDefaultGradesTableColumn {} { } proc ModifyNeedsTotalForGradesCategory {db category flag} { - if {![db exists {SELECT 1 FROM GradesCategoryTable WHERE CategoryName=$category}]} { - dbg "Category: $category already does not exists in the GradesCategoryTable, creating it" 3 + if {![existsColumnWithRowvalueInTable GradesCategoryTable CategoryName $category ]} { + dbg "Category: $category does not exists in the GradesCategoryTable, creating it" 3 AddGradesCategory db $category } - set eval_str [concat UPDATE GradesCategoryTable SET \"NeedsTotal\"='$flag' WHERE \"CategoryName\"=\'$category\'] - set err [catch {db eval $eval_str } errStat] - if { $err } { - htmlErrorMsg $errStat - dbg "the following error happen: $errStat" 3 - } + UpdateColumnWithValueInTableWhere GradesCategoryTable NeedsTotal $flag CategoryName $category } proc ModifyWeightForGradesCategory {db category weight} { - if {![db exists {SELECT 1 FROM GradesCategoryTable WHERE CategoryName=$category}]} { + if {![existsColumnWithRowvalueInTable GradesCategoryTable CategoryName $category ]} { dbg "Category: $category already does not exists in the GradesCategoryTable, creating it" 3 AddGradesCategory db $category } - set eval_str [concat UPDATE GradesCategoryTable SET \"CategoryWeight\"='$weight' WHERE \"CategoryName\"=\'$category\'] - set err [catch {db eval $eval_str } errStat] - if { $err } { - htmlErrorMsg $errStat - dbg "the following error happen: $errStat" 3 - } + UpdateColumnWithValueInTableWhere GradesCategoryTable CategoryWeight $weight CategoryName $category } proc AddGradesCategory {db category} { - if {[db exists {SELECT 1 FROM GradesCategoryTable WHERE CategoryName=$category}]} { + if {[existsColumnWithRowvalueInTable GradesCategoryTable CategoryName $category ]} { dbg "Category: $category already exists in the GradesCategoryTable" 3 return } @@ -458,8 +468,12 @@ proc UpdateGradesCategores { db permission_list user } { set eval_str "SELECT CategoryName FROM GradesCategoryTable" set err [catch { db eval $eval_str v { - if { [info exist colval($v(CategoryName))] } { - ModifyWeightForGradesCategory db $v(CategoryName) $colval($v(CategoryName)) + if { [info exist colval($v(CategoryName)_weight)] } { + ModifyWeightForGradesCategory db $v(CategoryName) $colval($v(CategoryName)_weight) + } + if { [info exist colval($v(CategoryName)_needs_total)] } { + # check box existance means it set to true + ModifyNeedsTotalForGradesCategory db $v(CategoryName) true } } } errStat ] @@ -497,11 +511,22 @@ proc EditGradesCategories { db permission_list user } { } foreach index $v(*) { if { $index != "*" } { - if { $index eq "CategoryWeight" } { - puts "<th><input type=\"text\" name=\"$v(CategoryName)\" value=\"$v(CategoryWeight)\" size=5></th>" - } else { + switch $index { + "CategoryWeight" { + puts "<th><input type=\"text\" name=\"$v(CategoryName)_weight\" value=\"$v(CategoryWeight)\" size=5></th>" + } + "NeedsTotal" { + if { $v(NeedsTotal) eq "true" } { + set checkbox_flag "checked" + } else { + set checkbox_flag "" + } + puts "<th><input type=\"checkbox\" name=\"$v(CategoryName)_needs_total\" value=\"\" $checkbox_flag></th>" + } + default { puts -nonewline "<td>$v($index)</td>" } + } } } puts "</tr>" |