# -*- Tcl -*-
########################################################################
# Inclass-Exam workflow, designed similar to online-exam
# ========================================================
#
# Defining exams: This workflow lets a teacher choose from a
# predefined set of exam questions, which are typically open text,
# short text, single or multiple choice questions. The teacher
# selects test questions via drag and drop. The teacher can perform a
# test run of the created exam, and can get the results via a result
# table.
#
# Publishing and closing exams: When a teacher is satisfied with the
# exam, the exam can be published. In this step, all answers of the
# testing phase are deleted. In the process of publishing, the link to
# start the exam is offered to the user. When the exam is published,
# the teacher can see the incoming answers in the report by refreshing
# the page. When the exam is done, it is unpublished. The workflow
# offers the teacher to see a summary of the results in form of a
# table (an to download the results via csv), or the teacher can
# produce a printer friendly version of the answers.
#
# An admin might with to add the following entries to the folder to ease
# creation of exercises and exams
#
# {clear_menu -menu New}
#
# {entry -name New.Item.TextInteraction -form en:TestItemText.form}
# {entry -name New.Item.ShortTextInteraction -form en:TestItemShortText.form}
# {entry -name New.Item.MCInteraction -form en:TestItemMC.form}
# {entry -name New.Item.SCInteraction -form en:TestItemSC.form}
# {entry -name New.Item.ReorderInteraction -form en:TestItemReorder.form}
# {entry -name New.Item.UploadInteraction -form en:TestItemUpload.form}
# {entry -name New.App.Exam -label "Online Exam" -form en:online-exam.wf}
#
# The policy has to allow the following methods on FormPages:
#
# - "answer" (for students),
# - "proctor" (for students),
# - "view-my-exam" (for students),
# - "edit" (for students),
# - "poll" (for teachers),
# - "print-answers" (for teachers),
# - "print-answer-table" (for teachers),
# - "print-participants" (for teachers),
# - "delete" (for teachers),
# - "qrcode" (for teachers)
#
# Gustaf Neumann, Feb 2012-2020
########################################################################
set :autoname 1 ;# to avoid editable name field
set :policy ::xowf::test_item::test-item-policy-publish
set :debug 0
set :live_updates 1
Action select -next_state created -label #xowf.online-exam-select# \
-title #xowf.online-exam-title-select#
Action publish -next_state published -label #xowf.online-exam-publish# \
-title #xowf.online-exam-title-publish#
Action unpublish -next_state done -label #xowf.online-exam-unpublish#
Action republish -next_state published -label #xowf.online-exam-republish# \
-title #xowf.online-exam-title-republish#
Action restart -next_state initial -label #xowf.restart# \
-title #xowf.online-exam-title-restart#
Action open_submission_review -next_state submission_review -label #xowf.open_submission_review# \
-title #xowf.open_submission_review_title#
Action close_submission_review -next_state done -label #xowf.close_submission_review# \
-title #xowf.close_submission_review_title#
State parameter {
{extra_css {/resources/xowf/test-item.css}}
}
State initial -actions {select} -form en:select_question.form -view_method edit
State created -actions {publish restart} -form_loader load_form -view_method edit \
-form "#xowf.inclass-exam-draft_exam#"
State published -actions {unpublish} -form_loader load_form -view_method edit \
-form "#xowf.inclass-exam-open#"
State done -actions {republish open_submission_review restart} -form_loader load_form -view_method edit \
-form "#xowf.inclass-exam-closed#"
State submission_review -actions {close_submission_review} -form_loader load_form -view_method edit \
-form "#xowf.inclass-exam-review#"
########################################################################
# Activate action select: After the teacher has selected the
# exercises, the answer workflow is created.
#
select proc activate {obj} {
xowf::test_item::answer_manager create_workflow \
-answer_workflow /packages/xowf/lib/inclass-exam-answer.wf \
$obj
}
########################################################################
# Activate action publish: delete all responses for the workflow and
# publish user participation link.
#
publish proc activate {obj} {
xowf::test_item::answer_manager delete_all_answer_data $obj
:publish_link $obj
}
########################################################################
# Activate action republish: publish user participation link.
#
republish proc activate {obj} {
:publish_link $obj
}
########################################################################
# When the user un-publishes an exam, just the user participation
# link should be removed for the users
#
unpublish proc activate {obj} {
:unpublish_link $obj
}
open_submission_review proc activate {obj} {
set aLink [$obj pretty_link -query m=view-my-exam]
util_user_message -html \
-message "[$obj name] exam review is available as [ns_quotehtml $aLink]"
}
########################################################################
# publish_link: make the user participation link available for the
# target group
#
Action instproc publish_link {obj} {
set aLink [$obj pretty_link -query m=answer]
util_user_message -html \
-message "[$obj name] is available as [ns_quotehtml $aLink]"
# TODO: make it happen in the LMS
}
########################################################################
# unpublish_link: remove the user participation link for the target
# group
#
Action instproc unpublish_link {obj} {
util_user_message -html -message "[$obj name] is closed"
# TODO: make it happen in the LMS
}
########################################################################
# form loader: create dynamically a form containing the disabled
# questions as a preview and the survey results (the results can be
# refreshed).
#
:proc load_form {ctx title} {
set obj [$ctx object]
set state [$obj property _state]
set combined_form_info [::xowf::test_item::question_manager combined_question_form -with_numbers $obj]
set fullQuestionForm [dict get $combined_form_info form]
set full_fc [dict get $combined_form_info disabled_form_constraints]
#:log fullQuestionForm=$fullQuestionForm
set text "
$title
"
set menu ""
set proctoring [$obj property proctoring 0]
set synchronized [$obj property synchronized 0]
append text [subst {
}]
set question_objs [dict get $combined_form_info question_objs]
set nrQuestions [llength $question_objs]
set randomizationOk [dict get $combined_form_info randomization_for_exam]
set autograde [dict get $combined_form_info autograde]
append text [subst {
}]
set wf [xowf::test_item::answer_manager get_answer_wf $obj]
if {$wf eq ""} {
:msg "cannot get current workflow for [$obj name]"
set lLink "."
set tLink "."
set aLink "."
set pLink "."
} else {
#
# Always compute the test-run and answer link.
#
set wf_pretty_link [$wf pretty_link]
set tLink [export_vars -base $wf_pretty_link {
{m create-new} {p.return_url "[::xo::cc url]"} {p.try_out_mode 1} {title "[$obj title]"}
}]
set aLink [$obj pretty_link -query m=answer]
if {$proctoring} {
#
# send link via "m=proctor"
#
set tLink [export_vars -base [$obj pretty_link] {
{m proctor} {link "$tLink&p.proctor=1"}
}]
#
# We could send answer link ("aLink") as well this way, but we
# want to keep the link short, therefore, we handle the proctor
# link inside the www-answer method.
#
}
#
# If there are answers, include the full menu.
#
set answers [xowf::test_item::answer_manager get_answers $wf]
if {[llength $answers] > 0} {
set lLink "$wf_pretty_link?m=list"
set pLink1 [$obj pretty_link -query m=print-answers]
set pLink2 [$obj pretty_link -query m=print-answer-table]
set pLink3 [$obj pretty_link -query m=print-participants]
set menu "\["
if {[acs_user::site_wide_admin_p -user_id [::xo::cc user_id]]} {
append menu "#xowf.online-exam-exam_instances#, "
}
append menu \
"#xowf.Participants#, " \
"#xowf.online-exam-protocol#, " \
"#xowf.online-exam-results-table#\]"
}
}
switch $state {
"created" -
"done" -
"submission_review" -
"published" {
#
# In inclass cases, never show all questions on screen, since
# the teacher might have the screen on the projector.
#
template::add_script -src urn:ad:js:bootstrap3
set fullQuestionForm [subst {
$fullQuestionForm
}]
}
}
set extraAction ""
switch $state {
"created" {
append extraAction " " \
"#xowf.online-exam-try_out# " \
"#xowf.testrun#"
}
"published" {
append extraAction " " \
"#xowf.online-exam-can_answer# " \
"$aLink"
}
}
set answerStatus ""
set marked ""
if {$state in {published done submission_review}} {
if {$state eq "done"} {
[$ctx object] setCSSDefaults
set marked [xowf::test_item::answer_manager marked_results -obj $obj -wf $wf $combined_form_info]
set marked "" ;# not needed right now
}
set answerStatus [xowf::test_item::answer_manager answers_panel \
-heading "#xowf.online-exam-submitted_exams_heading#" \
-submission_msg "#xowf.online-exam-submitted_exams_msg#" \
-polling=[expr {${:live_updates} && $state ni {initial created done}}] \
-manager_obj $obj \
-target_state done \
-wf $wf]
}
set qrCode ""
set countdownHTML ""
if {$state eq "published"} {
set src [$obj pretty_link -query m=qrcode]
set qrCode [subst {}]
set target_time [xowf::test_item::question_manager exam_target_time \
-manager $obj -base_time [$obj last_modified]]
set countdownHTML [xowf::test_item::answer_manager countdown_timer \
-target_time $target_time -id "countdown"]
}
# Remove wrapping forms
regsub -all {?form[^>]*>} $fullQuestionForm {} fullQuestionForm
append text [subst {
$answerStatus
$fullQuestionForm
$qrCode
$countdownHTML
}]
set footer "$menu $extraAction"
#-form [subst { text/html}]
set f [::xowiki::Form new \
-destroy_on_cleanup \
-set name en:question \
-form [subst { text/html}] \
-text {} \
-anon_instances t \
-form_constraints $full_fc \
]
}
########################################################################
#
# Object specific operations
#
########################################################################
:object-specific {
set ctx [:wf_context]
set container [$ctx wf_container]
if {$ctx ne $container} {
$ctx forward load_form $container %proc $ctx
}
${container}::Property return_url -default "" -allow_query_parameter true
#
# Unset the actual query return_url, since we want to use it via
# property. In some cases, we have to set it explicitly from the
# property, e.g. in www-delete.
#
::xo::cc unset_query_parameter return_url
########################################################################
# web-callable method "delete"
#
# Delete the workflow instance and all its associated data.
#
:proc www-delete {} {
::xo::cc set_query_parameter return_url [:property return_url]
xowf::test_item::answer_manager delete_all_answer_data [self]
next
}
########################################################################
# web-callable method "print-answer-table"
#
# Print the answers in a tabular form.
#
:proc www-print-answer-table {} {
set HTML ""
set withAnswerColumns [${:package_id} query_parameter with_answers:boolean 0]
set wf [xowf::test_item::answer_manager get_answer_wf [self]]
if {$wf ne ""} {
#set form_info [::xowf::test_item::question_manager combined_question_form -with_numbers [self]]
set items [xowf::test_item::answer_manager get_wf_instances $wf]
set items2 [$items deep_copy]
foreach i [$items2 children] {
$i set online-exam-userName [acs_user::get_element -user_id [$i creation_user] -element username]
$i set online-exam-fullName [::xo::get_user_name [$i creation_user]]
}
set HTML [::xowf::test_item::answer_manager results_table \
-package_id ${:package_id} \
-items $items2 \
-state * \
-with_answers $withAnswerColumns \
[self]]
$items2 destroy
}
if {$HTML eq ""} {
set HTML "#xowiki.no_data#"
} else {
set HTML "
#xowf.online-exam-results-table#
$HTML"
}
set return_url [[$wf package_id] query_parameter local_return_url:localurl [:pretty_link]]
append HTML "
\n"
:www-view $HTML
}
########################################################################
# web-callable method "view-my-exam "
#
# Provide feedback to the student about the results.
#
:proc www-view-my-exam {} {
if {${:state} eq "submission_review"} {
::xo::cc set_query_parameter creation_user [ad_conn user_id]
::xo::cc set_query_parameter as_student 1
:www-print-answers
} else {
:www-view "#xowf.Exam-review-not-open#"
}
}
########################################################################
# web-callable method "print-answers"
#
# Print the answers in a somewhat printer friendly way.
#
:proc www-print-answers {} {
set HTML ""
set heading ""
set as_student [${:package_id} query_parameter as_student:boolean 0]
set combined_form_info [::xowf::test_item::question_manager combined_question_form [self]]
set autograde [dict get $combined_form_info autograde]
set totalPoints [::xowf::test_item::question_manager total_minutes $combined_form_info]
set percentage_to_grade {50.0 60.0 70.0 80.0} ;# WI
set ctx [::xowf::Context require [self]]
set wf [xowf::test_item::answer_manager get_answer_wf [self]]
if {$wf ne ""} {
set package_id [$wf package_id]
set filter_id [$package_id query_parameter id:integer ""]
set creation_user [$package_id query_parameter creation_user:integer ""]
set revision_id [$package_id query_parameter rid:integer ""]
set items [xowf::test_item::answer_manager get_wf_instances \
{*}[expr {$creation_user ne "" ? "-creation_user $creation_user" : ""}] \
{*}[expr {$filter_id ne "" ? "-item_id $filter_id" : ""}] \
$wf]
set withSignature [expr {[dict exists ${:instance_attributes} signature]
? [dict get ${:instance_attributes} signature]
: 0 }]
set examTitle ${:title}
if {$revision_id ne ""} {
set r [::xowiki::FormPage get_instance_from_db -revision_id $revision_id]
if {[$r item_id] ni [lmap i [$items children] {$i item_id}]} {
error "invalid revision id '$revision_id' provided"
}
$items destroy
set items [::xo::OrderedComposite new -destroy_on_cleanup]
$items add $r
}
foreach i [$items children] {
$i set online-exam-userName [acs_user::get_element -user_id [$i creation_user] -element username]
$i set online-exam-fullName [::xo::get_user_name [$i creation_user]]
}
$items orderby online-exam-userName
foreach i [$items children] {
set userName [$i set online-exam-userName]
set fullName [$i set online-exam-fullName]
set state [$i state]
#if {$state ne "done"} {
# ns_log notice "online-exam: submission of $userName is not finished (state $state)"
# #continue
#}
set revisions [$i get_revision_sets]
if {[llength $revisions] <=1 } {
# just an initial revision
ns_log notice "online-exam: submission of $userName is empty. Ignoring."
continue
}
#
# The call to "render_content" calls actually the
# "summary_form" of online/inclass-exam-answer.wf when the submit
# instance is in state "done". We set the __feedback_mode to
# get the auto-correction included.
#
foreach f [::xowiki::formfield::FormField info instances -closure] {
#ns_log notice "FF could DESTROY $f [$f name]"
if {[string match *_ [$f name]]} {
#ns_log notice "FF DESTROY $f [$f name]"
$f destroy
}
}
$wf form_field_flush_cache
set achieved_points {}
xo::cc eval_as_user -user_id [$i creation_user] {
$i set __feedback_mode 2
set question_form [$i render_content]
if {$withSignature || $autograde} {
set answerAttributes [xowf::test_item::renaming_form_loader \
answer_attributes [$i instance_attributes]]
if {$autograde} {
set achieved_points [xowf::test_item::answer_manager achieved_points \
-answer_object $i -answer_attributes $answerAttributes]
dict set achieved_points totalPoints $totalPoints
}
}
}
if {$withSignature} {
set sha256 [ns_md string -digest sha256 $answerAttributes]
set signatureString "
online-exam-actual_signature: $sha256
\n"
set submissionSignature [$i property signature ""]
if {$submissionSignature ne ""} {
append signatureString "
\n"
}
} else {
set signatureString ""
}
set time [::xo::db::tcl_date [$i property _last_modified] tz_var]
set pretty_date [clock format [clock scan $time] -format "%Y-%m-%d"]
if {$filter_id ne "" && [:property proctoring] eq "t"} {
set user_id [$i creation_user]
set img_url [:pretty_link -query m=proctor-image&user_id=$user_id]
set proctoring_dir [acs_root_dir]/proctoring/${:item_id}/$user_id/
set files [glob -nocomplain -path $proctoring_dir *.*]
#ns_log notice "proctoring_dir $proctoring_dir files $files"
if {$revision_id ne ""} {
set filtered_revisions [xowf::test_item::answer_manager revisions_up_to $revisions $revision_id]
} else {
set filtered_revisions $revisions
}
set start_date [ns_set get [lindex $filtered_revisions 0] creation_date]
set end_date [ns_set get [lindex $filtered_revisions end] creation_date]
set start_clock [clock scan [::xo::db::tcl_date $start_date tz_var]]
set end_clock [clock scan [::xo::db::tcl_date $end_date tz_var]]
set image ""
ns_log notice "start date $start_date end_date $end_date / $start_clock $end_clock"
foreach f $files {
#ns_log notice "check: $f"
if {[regexp {/([^/]+)-(\d+)[.](webm|png|jpeg)$} $f . type stamp ext]} {
set inWindow [expr {$stamp >= $start_clock && $stamp <= $end_clock}]
ns_log notice "parsed $type $stamp $ext $inWindow $stamp \
[clock format $stamp -format {%m-%d %H:%M:%S}] >= \
$start_clock ([expr {$stamp >= $start_clock}]) \
&& $stamp <= $end_clock ([expr {$stamp <= $end_clock}])"
if {$inWindow} {
dict set image $stamp $type $ext
}
}
}
set markup ""
foreach ts [lsort -integer [dict keys $image]] {
#ns_log notice "ts $ts [dict get $image $ts]"
append markup [subst {
[clock format $ts -format {%Y-%m-%d %H:%M:%S}]
}]
append markup {
}
foreach type {camera-image desktop-image} {
if {[dict exists $image $ts $type]} {
set ext [dict get $image $ts $type]
append markup [subst {}]
}
}
if {[dict exists $image $ts camera-audio]} {
set ext [dict get $image $ts camera-audio]
append markup [subst {}]
}
append markup
\n
}
set question_form [subst {
$question_form
$markup
}]
}
set view [expr {$as_student
? "student"
: $filter_id ne ""
? "revision_overview"
: "default"}]
set runtime_panel [xowf::test_item::answer_manager runtime_panel \
-revision_id $revision_id \
-view $view \
-achieved_points $achieved_points \
$i]
if {$autograde} {
dict incr grade_count [xowf::test_item::answer_manager grade \
-achieved_points $achieved_points \
-percentage_boundaries $percentage_to_grade]
}
set heading "$userName · $fullName · $pretty_date"
append HTML [subst {
[expr {$as_student ? "" : "
$heading
"}]
$runtime_panel
$signatureString
$question_form
}]
}
}
if {$HTML eq ""} {
set HTML "#xowiki.no_data#"
}
if {$as_student} {
if {$heading eq ""} {
set userName [acs_user::get_element -user_id [ad_conn user_id] -element username]
set fullName [::xo::get_user_name [ad_conn user_id]]
set heading "$userName - $fullName"
}
set HTML "
\n"
}
::xo::cc set_parameter template_file view-plain-master
::xo::cc set_parameter MenuBar 0
xo::Page requireCSS /resources/xowf/test-item.css
:www-view $HTML
}
########################################################################
# web-callable method "answer"
#
# Create or use an answering workflow for the current exam. This is
# a convenience routine to shorten the published URL.
#
:proc www-answer {} {
#
# Make sure that no-one tries to start the answer workflow in a
# state different to "published".
#
if {[:property _state] ne "published"} {
util_user_message -html -message "Cannot start answer workflow in this state"
} else {
set wf [xowf::test_item::answer_manager get_answer_wf [self]]
set proctoring [:property proctoring]
if {$proctoring ne "" && $proctoring} {
set cLink [export_vars -base [:pretty_link] {
{m proctor} {link "[:pretty_link -query m=proctor-answer]"}
}]
::${:package_id} returnredirect $cLink
} else {
$wf www-create-or-use -parent_id [:item_id]
}
}
}
:proc www-proctor-answer {} {
if {[:property _state] ne "published"} {
util_user_message -html -message "Cannot start answer workflow in this state"
} else {
set wf [xowf::test_item::answer_manager get_answer_wf [self]]
$wf www-create-or-use -parent_id [:item_id]
}
}
:proc www-qrcode {} {
set aLink [:pretty_link -absolute true -query m=answer]
set fn /tmp/qr-${:item_id}.png
exec qrencode -o $fn -l h $aLink
ns_returnfile 200 image/png $fn
ad_script_abort
}
:proc www-proctor {} {
#
# Redirect the exam to an iframe for implementing proctoring. The
# basic idea is that the web application turns on the camera and
# keeps the iframe while the user is iterating through the exam.
# The line "
You are being proctored!
" is just a
# placeholder and has to be replaced with real code.
#
set link [${:package_id} query_parameter link:localurl ""]
::xo::cc set_parameter template_file view-plain-master
::xo::cc set_parameter MenuBar 0
if {[file exists [acs_package_root_dir xowf]/lib/proctored-page.adp]} {
set object_id ${:item_id}
set object_url $link
set upload_url /xowf-proctoring-upload
${:package_id} return_page -adp /packages/xowf/lib/proctored-page -variables {
object_id
object_url
upload_url
}
} else {
return [:www-view [subst {
You ([xo::cc user_id]) are being proctored in exam ${:object_id}!
}]]
}
}
:proc www-proctor-image {} {
set type [${:package_id} query_parameter type:ascii ""]
set ts [${:package_id} query_parameter ts:integer ""]
set ext [${:package_id} query_parameter e:wordchar ""]
set user_id [${:package_id} query_parameter user_id:integer ""]
set id ${:item_id}
set proctoring_dir [acs_root_dir]/proctoring/$id/$user_id
set png_path $proctoring_dir/$type-$ts.$ext
#ns_log notice "image: $png_path ... [file exists $$png_path]"
ns_returnfile 200 [ns_guesstype $ts.$ext] $png_path
ad_script_abort
}
########################################################################
# AJAX call "poll"
#
# Return statistics about working and finished exams.
#
:proc www-poll {} {
set wf [xowf::test_item::answer_manager get_answer_wf [self]]
set answers [xowf::test_item::answer_manager get_answers $wf]
set answered [xowf::test_item::answer_manager get_answers -state done $wf]
ns_return 200 text/plain [llength $answered]/[llength $answers]
#ns_log notice "MASTER POLL [self] ${:name}, returned [llength $answered]/[llength $answers]"
ad_script_abort
}
}
#
# Local variables:
# mode: tcl
# tcl-indent-level: 2
# indent-tabs-mode: nil
# End: