# -*- Tcl -*-
########################################################################
# Online-Exam workflow, designed similar to mobile-clicker
# ========================================================
#
# This workflow lets a teacher choose from a predefined set of exam
# questions, which are typically open text questions. A teacher
# selects one or several exam questions via drag and drop. The teacher
# can test the exam by entering test answers. The results are provided
# in form of a table.
#
# 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 -label "Text Interaction" -form en:TestItemText.form}
# {entry -name New.Item.ShortTextInteraction -label "Short Text Interaction" -form en:TestItemShortText.form}
# {entry -name New.Item.MCInteraction -label "MC Interaction" -form en:TestItemMC.form}
# {entry -name New.Item.SCInteraction -label "SC Interaction" -form en:TestItemSC.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),
# - "edit" (for students),
# - "poll" (for teachers),
# - "print-answers" (for teachers),
# - "delete" (for teachers),
#
# Gustaf Neumann, Feb 2012
########################################################################
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#
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.online-exam-draft_exam#"
State published -actions {unpublish} -form_loader load_form -view_method edit \
-form "#xowf.online-exam-open#"
State done -actions {republish restart} -form_loader load_form -view_method edit \
-form "#xowf.online-exam-closed#"
########################################################################
# 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/online-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
}
########################################################################
# 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 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 "."
set menu ""
} else {
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 lLink "$wf_pretty_link?m=list"
set aLink [$obj pretty_link -query m=answer]
set pLink [$obj pretty_link -query m=print-answers]
#util_user_message -html -message "$survey is available as $pLink"
# [#xowf.refresh#,
set menu "\[#xowf.online-exam-exam_instances#,\
#xowf.online-exam-protocol#\]"
}
set extraAction ""
switch $state {
"created" {
append extraAction "
" \
"#xowf.online-exam-try_out# " \
"#xowf.testrun#"
}
"published" {
append extraAction "
" \
"#xowf.online-exam-can_answer# " \
"$aLink"
}
}
if {$state in {published done}} {
if {$state eq "done"} {
[$ctx object] setCSSDefaults
set marked [xowf::test_item::answer_manager marked_results $wf $combined_form_info]
}
set answerStats [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 ne "done"}] \
-manager_obj $obj \
-target_state done \
-wf $wf]
} else {
set answerStats ""
}
append text "$answerStats\n"
append report "$menu $extraAction"
# Remove wrapping forms
regsub -all {?form[^>]*>} $fullQuestionForm {} fullQuestionForm
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
::xo::cc unset_query_parameter return_url
########################################################################
# web-callable method "delete"
#
# Delete the workflow instance and all its associated data.
#
:proc www-delete {} {
xowf::test_item::answer_manager delete_all_answer_data [self]
next
}
########################################################################
# web-callable method "print-answers"
#
# Print the answers in a somewhat printer friendly way.
#
:proc www-print-answers {} {
set HTML ""
set ctx [::xowf::Context require [self]]
set wf [xowf::test_item::answer_manager get_answer_wf [self]]
if {$wf ne ""} {
set items [xowf::test_item::answer_manager get_wf_instances $wf]
set withSignature [expr {[dict exists ${:instance_attributes} signature]
? [dict get ${:instance_attributes} signature]
: 0 }]
set examTitle ${:title}
foreach i [$items children] {
$i set _userName [acs_user::get_element -user_id [$i creation_user] -element username]
$i set _fullName [::xo::get_user_name [$i creation_user]]
}
$items orderby _userName
foreach i [$items children] {
set userName [$i set _userName]
set fullName [$i set _fullName]
if {[$i state] ne "done"} {
ns_log notice "online-exam: submission of $userName is not finished (state [$i state])"
continue
}
set time [::xo::db::tcl_date [$i property _last_modified] tz_var]
set pretty_date [clock format [clock scan $time] -format "%Y-%m-%d %T"]
#
# The call to "render_content" calls actually the
# "summary_form" of online-exam-answer.wf when the submit
# instance is in state "done". We set the __feedback_mode to
# get the auto-correction included.
#
$i set __feedback_mode 2
set question_form [$i render_content]
if {$withSignature} {
set answerAttributes [xowf::test_item::renaming_form_loader \
answer_attributes [$i instance_attributes]]
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 "#xowf.online-exam-submission_signature#: $submissionSignature
\n"
}
} else {
set signatureString ""
}
append HTML "\n
" \
"
$userName · $fullName · $pretty_date · IP [$i property ip]
" \
$signatureString \
$question_form \
"\n"
}
}
if {$HTML eq ""} {
set HTML "#xowiki.no_data#"
} else {
set HTML "
#xowf.online-exam-protocol#
$HTML"
}
::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]]
$wf www-create-or-use -parent_id [:item_id]
}
}
########################################################################
# 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: