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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
#!/bin/sh
# (C) 2010 by Eugeniy Mikhailov, <evgmik@gmail.com>
# vim:set ft=tcl: \
exec tclsh "$0" "$@"
# internal version of this code
set VERSION 1.0
package require sqlite3
package require ncgi
package require md5
::ncgi::parse
# defaults
set sortCol LastName
set user guest
set password guest
set action defaultview
# defaults end
# read cookies
set user [::ncgi::cookie user]
set sortCol [::ncgi::cookie sortCol]
set password [::ncgi::cookie password]
set action [::ncgi::value action defaultview]
# end of read cookies
if { [catch {set script_name $env(SCRIPT_NAME)} errStat] } { set script_name unknown}
# ########################## procs begin #################################
proc dbg {msg {level 1}} {
if { $level <=2 } {
set fid [open log a+]
puts $fid $msg
close $fid
}
}
set dbfile "./testdb"
#set url_base
sqlite3 db $dbfile
proc CreatePasswordsTable {db} {
db eval {CREATE TABLE PasswordsTable(UserName text, PasswordHash text, GroupName text)}
set eval_str [list INSERT INTO PasswordsTable VALUES('instructor', '[::md5::md5 -hex qwerty]', 'instructor')]
db eval $eval_str
set eval_str [list INSERT INTO PasswordsTable VALUES('ta', '[::md5::md5 -hex qwerty]', 'ta')]
db eval $eval_str
set eval_str [list INSERT INTO PasswordsTable VALUES('jhn', '[::md5::md5 -hex qwerty]', 'student')]
db eval $eval_str
set eval_str [list INSERT INTO PasswordsTable VALUES('ale', '[::md5::md5 -hex qwerty]', 'student')]
db eval $eval_str
set eval_str [list INSERT INTO PasswordsTable VALUES('dan', '[::md5::md5 -hex qwerty]', 'student')]
db eval $eval_str
}
proc CreateGradesTable {db} {
db eval {CREATE TABLE GradesTable(FirstName text, LastName text, UserName text, HW01 real)}
set eval_str [list INSERT INTO GradesTable VALUES('','', '_Col_Category_', '')]
db eval $eval_str
set eval_str [list INSERT INTO GradesTable VALUES('','', '_Max_Points_', '')]
db eval $eval_str
set eval_str [list INSERT INTO GradesTable VALUES('John','Lname1', 'jhn', 7)]
db eval $eval_str
set eval_str [list INSERT INTO GradesTable VALUES('Ale','Lname2', 'ale', 5)]
db eval $eval_str
set eval_str [list INSERT INTO GradesTable VALUES('Dan','Lname3', 'dan', 3)]
db eval $eval_str
}
proc CreateAccessRightsTable {db} {
db eval {CREATE TABLE AccessRightsTable(actionname text, instructor integer, ta integer, student integer, guest integer)}
# guest should have no rights make sure that 0 is evereywhere
db eval {INSERT INTO AccessRightsTable VALUES('showgrades', 1, 1, 1, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('sort', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('addcolumnrequest', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('addcolumn', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('deletecolumn', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('renamecolumn', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('showcontrols', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('changegrades', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('updategrades', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('changecolumn', 1, 1, 0, 0)}
db eval {INSERT INTO AccessRightsTable VALUES('updatecolumn', 1, 1, 0, 0)}
}
proc htmlErrorMsg { msg } {
puts "<div class=\"errormsg\">error: $msg</div>"
}
proc htmlReplaceEmptyString { string } {
# empty string replaced with "---"
regsub {^$} $string "---" string
# white spaces only string replaced with "---"
regsub {^\s+$} $string "---" string
set string
}
proc htmlDBout {db permission_list {sort_col {}}} {
array set permission $permission_list
global script_name
set defSortCol LastName
if { $sort_col == {} } {
set sort_col $defSortCol
dbg "empty sort col changed to $sort_col" 4
}
# testing for the existense of the sorting column
set eval_str [list SELECT * FROM GradesTable ORDER BY $sort_col]
set err [catch {db eval $eval_str } errStat]
if { $err } {
dbg $errStat 3
dbg "changing to default sorting column $defSortCol" 3
set sort_col $defSortCol
}
set show_header 1
# show the table with grades
set eval_str [list SELECT * FROM GradesTable ORDER BY $sort_col]
set err [catch {
db eval $eval_str v {
if { $show_header } {
set show_header 0
puts {<table class="gradestable" border="1">}
puts "<tr>"
foreach col $v(*) {
puts -nonewline "<th><a href=$script_name?action=sort&sortCol=$col>$col</a>"
# below list has action and action_label pairs
set action_list [list changegrades "change grades" deletecolumn delete renamecolumn rename changecolumn "change column"]
set separator {<br>}
foreach {act act_label} $action_list {
if { [info exist permission($act) ] } {
if { $permission($act) } {
puts -nonewline "$separator<a href=$script_name?action=$act&columnname=[::ncgi::encode $col]>$act_label</a>"
}
}
}
puts -nonewline "</th>"
}
puts "</tr>"
puts "<tr>"
} else {
puts "<tr>"
}
foreach index $v(*) {
if { $index != "*" } {
set col_value [htmlReplaceEmptyString $v($index)]
puts -nonewline "<td>$col_value</td>"
}
}
puts "</tr>"
}
} errStat ]
if { $err } {
dbg "we should never be here if $sortCol exist in the table" 1
dbg $errStat 1
}
puts "</table>"
}
proc htmlTop {permission_list} {
array set permission $permission_list
if { $permission(GroupName) == "guest" } {
askToLogin
} else {
LogOffOption
}
}
proc htmlFooter {permission_list} {
array set permission $permission_list
global VERSION
puts "<div class=\"footer\">"
puts "GradeBook $VERSION code is written by Eugeniy Mikhailov"
puts "</div>"
}
proc LogOffOption {} {
global user password script_name
puts "<div class=\"logoff\">"
puts "You are logged in as <span class=login>$user</span> do you wish to "
puts "<a href=\"$script_name?action=logoff\">logoff</a>"
puts "</div>"
}
proc askToLogin {} {
global script_name
puts "<div class=\"login\">"
puts "Either you are here first time or you password and user name does not match. <br>"
puts "Please login <br>"
puts "<form name=\"input\" action=\"$script_name\" method=\"post\" />"
puts {Login: <input type="text" name="user"><br>}
puts {Password: <input type="password" name="password"><br>}
puts {<input type="hidden" name="action" value="logon"/>}
puts {<input type="submit" value="Submit" />}
puts {</form>}
puts "</div>"
}
#proc CheckAccessRights { user password} {}
#proc IsUserknown {} {return 1}
#proc SetLoginInfo {} {
#global user password
#set isAccessGranted [IsUserknown]
#if { $isAccessGranted } {
#dbg "access granted to user $user"
#}
#set access_rights [CheckAccessRights $user $password]
#}
proc LogMeOn {} {
global user password
set user [::ncgi::value user guest]
set password [::ncgi::value password guest]
dbg "Logging in and setting cookies"
::ncgi::setCookie -name user -value $user
::ncgi::setCookie -name password -value $password
}
proc LogMeOff {} {
dbg "Logging off"
global user password
set user guest
set password guest
::ncgi::setCookie -name user -value $user
::ncgi::setCookie -name password -value $password
}
proc SetSortColumn {} {
global sortCol
set sortCol [::ncgi::value sortCol LastName]
::ncgi::setCookie -name sortCol -value $sortCol
}
proc isActionGranted { action permission_list user } {
array set permission $permission_list
if { $action == "defaultview" } {
# this one permitted to everyone
return 1;
}
if { [info exist permission($action) ] && $permission($action) } {
dbg "requested action $action is granted" 4
return 1;
} else {
dbg "requested action $action is not granted" 4
return 0;
}
}
proc ChoseAction {action permission_list user} {
array set permission $permission_list
dbg "requested action: $action" 1
if { [isActionGranted $action $permission_list $user] } {
switch $action {
sort { SetSortColumn; ChoseAction defaultview $permission_list $user }
addcolumn {
AddColumn $permission_list $user;
htmlDefaultView $permission_list $user
}
addcolumnrequest { AddColumnRequest $permission_list $user }
deletecolumn {
DeleteColumn $permission_list $user;
htmlDefaultView $permission_list $user
}
changegrades { ChangeGrades $permission_list $user }
updategrades { UpdateGrades $permission_list $user
htmlDefaultView $permission_list $user
}
renamecolumn { }
changecolumn { ChangeColumn $permission_list $user }
updatecolumn { UpdateColumn $permission_list $user }
showcontrols { ShowControls $permission_list $user }
showgrades { htmlGradesTable db $permission_list $user }
defaultview { htmlDefaultView $permission_list $user }
default { }
}
}
}
proc ChangeColumn { permission_list user } {
set columnname [::ncgi::value columnname {}]
set category "none"
set eval_str "SELECT \"$columnname\" FROM GradesTable where UserName=\"_Col_Category_\""
set err [catch {
db eval $eval_str v {
set category $v($columnname)
}
} errStat ]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
set eval_str "SELECT \"$columnname\" FROM GradesTable where UserName=\"_Max_Points_\""
set err [catch {
db eval $eval_str v {
set maxpoints $v($columnname)
}
} errStat ]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
puts {<div class="add_new_column">}
puts "<form name=\"input\" method=\"post\" />"
set out_str {}
append out_str {Column Name: <input type="text" name="newcolumnname" value="} $columnname {"><br>}
puts $out_str
set out_str {}
append out_str {Category: <select name="category" value="} $category {">}
puts $out_str
# opt list has option name and corresponding text pairs
set opt_list [list \
none --Select--\
Quiz Quiz\
HomeWork HomeWork\
LabReport LabReport\
MidTerm MidTerm\
FinalExam FinalExam\
]
foreach {name txt} $opt_list {
set out_str {}
if { $name eq $category } {
append out_str {<option value="} $name {" selected>} $txt {</option>}
} else {
append out_str {<option value="} $name {">} $txt {</option>}
}
puts $out_str
}
puts {</select> <br>}
set out_str {}
append out_str {Max Point Possible:<input type="text" name="maxpointpossible" value="} $maxpoints {"> <br>}
puts $out_str
puts {<input type="hidden" name="action" value="updatecolumn"/>}
set out_str {}
append out_str {<input type="hidden" name="oldcolumnname" value="} $columnname {"/>}
puts $out_str
puts {<input type="submit" value="Submit" />}
puts {</form>}
puts {</div>}
}
proc UpdateGrades { permission_list user } {
global script_name
set subaction [::ncgi::value subaction {}]
set columnname [::ncgi::value columnname {}]
set nv [::ncgi::nvlist]
array set colval $nv
if { $subaction eq "Submit" } {
set eval_str "SELECT UserName FROM GradesTable"
set err [catch {
db eval $eval_str v {
if { [info exist colval($v(UserName))] } {
set sql_str [concat UPDATE GradesTable SET \"$columnname\"=\'$colval($v(UserName))\' where UserName=\"$v(UserName)\"]
set err2 [catch {
db eval $sql_str
} errStat2 ]
if { $err2 } {
htmlErrorMsg $errStat2
dbg "the following error happen: $errStat2" 3
}
}
}
} errStat ]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
}
}
proc ChangeGrades { permission_list user } {
global script_name
set columnname [::ncgi::value columnname {}]
if { $columnname != "" } {
# output only selected columns
set column_list {}
lappend column_list FirstName LastName UserName
lappend column_list \"$columnname\"
set sql_column_list [join $column_list ","]
puts "<form name=\"input\" action=\"$script_name\" method=\"post\" />"
# output only selected columns
set eval_str "SELECT $sql_column_list FROM GradesTable WHERE UserName<>'_Col_Category_' AND UserName<>'_Max_Points_' ORDER BY LastName"
set show_header 1
set err [catch {
db eval $eval_str v {
if { $show_header } {
set show_header 0
puts {<table class="gradestable" border="1">}
puts "<tr>"
foreach col $v(*) {
puts -nonewline "<th>$col</th>"
}
puts "<tr>"
} else {
puts "<tr>"
}
foreach index $v(*) {
if { $index != "*" } {
if { $index eq $columnname } {
#column with grade
puts "<th><input type=\"text\" name=\"$v(UserName)\" value=\"$v($columnname)\" size=5></th>"
} else {
puts -nonewline "<td>$v($index)</td>"
}
}
}
puts "</tr>"
}
} errStat ]
puts "</table>"
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
puts {<input type="hidden" name="action" value="updategrades"/>}
puts [concat <input type="hidden" name="columnname" value="$columnname"/>]
puts {<input type="submit" name="subaction" value="Submit" />}
puts {<input type="submit" name="subaction" value="Cancel" />}
puts {</form>}
#puts "<a href=\"$script_name\">Cancel changes</a>"
} else {
htmlErrorMsg "empty column names are not permitted"
}
}
proc DeleteColumn { permission_list user } {
global script_name
set columnname [::ncgi::value columnname {}]
if { $columnname != "" } {
set eval_str [concat SELECT * FROM GradesTable ]
set err [catch {db eval $eval_str v {} } errStat]
set old_column_list $v(*)
# removing the column name to be deleted from total list
set new_column_list {}
foreach cname $old_column_list {
if { $cname ne $columnname } {
lappend new_column_list \"$cname\"
}
}
set sql_new_column_list [join $new_column_list ","]
set eval_str "BEGIN TRANSACTION;
CREATE TEMPORARY TABLE GradesTable_backup($sql_new_column_list);
INSERT INTO GradesTable_backup SELECT $sql_new_column_list FROM GradesTable;
DROP TABLE GradesTable;
CREATE TABLE GradesTable($sql_new_column_list);
INSERT INTO GradesTable SELECT $sql_new_column_list FROM GradesTable_backup;
DROP TABLE GradesTable_backup;
COMMIT;"
set err [catch {db eval $eval_str } errStat]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
} else {
htmlErrorMsg "empty column names are not permitted"
}
}
proc AddColumnNonWeb { columnname2add column_category maxpointpossible } {
if { $columnname2add != "" } {
set eval_str [concat ALTER TABLE GradesTable ADD \"$columnname2add\" text]
set err [catch {db eval $eval_str } errStat]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
set sql_str [concat UPDATE GradesTable SET \"$columnname2add\"=\'$column_category\' where UserName=\"_Col_Category_\"]
set err [catch {db eval $sql_str } errStat]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
set sql_str [concat UPDATE GradesTable SET \"$columnname2add\"=\'$maxpointpossible\' where UserName=\"_Max_Points_\"]
set err [catch {db eval $sql_str } errStat]
if { $err } {
htmlErrorMsg $errStat
dbg "the following error happen: $errStat" 3
}
} else {
htmlErrorMsg "empty column names are not permitted"
}
}
proc AddColumn { permission_list user } {
global script_name
set columnname2add [::ncgi::value columnname2add {}]
set column_category [::ncgi::value column_category {}]
set maxpointpossible [::ncgi::value maxpointpossible {}]
AddColumnNonWeb $columnname2add $column_category $maxpointpossible
}
proc AddColumnRequest { permission_list user } {
global script_name
puts {<div class="add_new_column">}
puts "<form name=\"input\" method=\"post\" />"
puts {Column Name: <input type="text" name="columnname2add"><br>}
puts {Category: <select name="column_category">}
puts {<option value="none">--Select--</option>}
puts {<option value="Quiz">Quiz</option>}
puts {<option value="HomeWork">HomeWork</option>}
puts {<option value="LabReport">LabReport</option>}
puts {<option value="MidTerm">MidTerm</option>}
puts {<option value="FinalExam">FinalExam</option>}
puts {</select> <br>}
puts {Max Point Possible:<input type="text" name="maxpointpossible"><br>}
puts {<input type="hidden" name="action" value="addcolumn"/>}
puts {<input type="submit" value="Submit" />}
puts {</form>}
puts {</div>}
}
proc ShowControls { permission_list user } {
array set permission $permission_list
global script_name
dbg "outputing contol list"
puts "<div class=\"controls\">"
set action_list [ list addcolumnrequest "Add Column" ]
set separator {}
foreach {act act_label} $action_list {
if { [info exist permission($act) ] } {
if { $permission($act) } {
puts -nonewline "$separator<a href=$script_name?action=$act>$act_label</a>"
}
}
}
puts "</div>"
}
proc AccessGroupRights {db user password } {
dbg "access rights check for user: $user"
set PasswordHash [::md5::md5 -hex $password]
set eval_str [list SELECT GroupName FROM PasswordsTable WHERE UserName='$user' AND PasswordHash='$PasswordHash']
db eval $eval_str group_name_array {}
if { [ info exist group_name_array(GroupName) ] } {
set group $group_name_array(GroupName);
} else {
set group guest
}
dbg "Detected group is $group"
set eval_str [list SELECT actionname,$group FROM AccessRightsTable ]
set permission_list [list GroupName $group]
db eval $eval_str permissions { lappend permission_list $permissions(actionname) $permissions($group) }
dbg "permissions for user $user belonging to the group $group are: $permission_list"
return $permission_list
}
proc htmlStudentGrades { db user } {
set defSortCol LastName
global script_name
set sort_col $defSortCol
set show_header 1
# show the table with grades
set eval_str [list SELECT * FROM GradesTable WHERE UserName='$user' ORDER BY $sort_col]
set err [catch {
db eval $eval_str v {
if { $show_header } {
set show_header 0
puts {<table class="gradestable" border="1">}
puts "<tr>"
foreach col $v(*) {
puts -nonewline "<th>$col</th>"
}
puts "</tr>"
puts "<tr>"
} else {
puts "<tr>"
}
foreach index $v(*) {
if { $index != "*" } {
set col_value [htmlReplaceEmptyString $v($index)]
puts -nonewline "<td>$col_value</td>"
}
}
puts "</tr>"
}
} errStat ]
if { $err } {
dbg "we should never be here if $sortCol and $user exist in the table" 1
dbg $errStat 1
}
puts "</table>"
}
proc htmlGradesTable {db permission_list user} {
array set permission $permission_list
global sortCol
switch $permission(GroupName) {
guest { }
student { htmlStudentGrades db $user}
ta { htmlDBout db $permission_list $sortCol}
instructor { htmlDBout db $permission_list $sortCol}
default { }
}
}
proc htmlDefaultView { permission_list user } {
ChoseAction showcontrols $permission_list $user
ChoseAction showgrades $permission_list $user
}
proc htmlHeader {} {
puts {
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content= "text/html; charset=us-ascii" />
<title>Grade Book</title>
<link rel="stylesheet" type="text/css" href="/~evmik/GradeBook.css" />
</head>
}
}
##################### end of procs ####################################
set timestamp [clock format [clock seconds] -format "%Y-%m-%dT%H:%M:%S"]
#CreatePasswordsTable db
#CreateGradesTable db
#CreateAccessRightsTable db
dbg [::ncgi::names] 4
# logon and logoff actions are granted to everyone
if { $action == "logon" } { LogMeOn; set action defaultview }
if { $action == "logoff" } { LogMeOff; set action defaultview }
dbg "===== Connection at $timestamp for user $user ====="
set permission_list [AccessGroupRights db $user $password]
::ncgi::header
htmlHeader
puts "<body>"
htmlTop $permission_list
ChoseAction $action $permission_list $user
#htmlDefaultView $permission_list $user
#htmlGradesTable db $permission_list $user
#htmlDBout db $sortCol
htmlFooter $permission_list
puts "</body>"
|