# # Copyright (C) 2002 Motorola. All Rights Reserved use strict; # ========================= package TWiki::Plugins::QuizPlugin; # change the package name!!! # ========================= use vars qw( $web $topic $user $installWeb $VERSION $debug $quizNumber ); $VERSION = '1.000'; # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if( $TWiki::Plugins::VERSION < 1 ) { &TWiki::Func::writeWarning( "Version mismatch between QuizPlugin and Plugins.pm" ); return 0; } # Get plugin debug flag $debug = &TWiki::Func::getPreferencesFlag( "QUIZPLUGIN_DEBUG" ); # Plugin correctly initialized &TWiki::Func::writeDebug( "- TWiki::Plugins::QuizPlugin::initPlugin( $web.$topic ) is OK" ) if $debug; $quizNumber = 1; return 1; } # ========================= sub DISABLE_commonTagsHandler { ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead &TWiki::Func::writeDebug( "- QuizPlugin::commonTagsHandler( $_[2].$_[1] )" ) if $debug; } # ========================= sub DISABLE_startRenderingHandler { ### my ( $text, $web ) = @_; # do not uncomment, use $_[0], $_[1] instead &TWiki::Func::writeDebug( "- QuizPlugin::startRenderingHandler( $_[1].$topic )" ) if $debug; # This handler is called by getRenderedVersion just before the line loop # do custom extension rule, like for example: # $_[0] =~ s/old/new/go; } # ========================= sub DISABLE_outsidePREHandler { ### my ( $text ) = @_; # do not uncomment, use $_[0] instead # &TWiki::Func::writeDebug( "- QuizPlugin::outsidePREHandler( $web.$topic )" ) if $debug; # This handler is called by getRenderedVersion, in loop outside of
tag.
# This is the place to define customized rendering rules.
# Note: This is an expensive function to comment out.
# Consider startRenderingHandler instead
}
# =========================
sub DISABLE_insidePREHandler
{
### my ( $text ) = @_; # do not uncomment, use $_[0] instead
# &TWiki::Func::writeDebug( "- QuizPlugin::insidePREHandler( $web.$topic )" ) if $debug;
# This handler is called by getRenderedVersion, in loop inside of tag.
# This is the place to define customized rendering rules.
# Note: This is an expensive function to comment out.
# Consider startRenderingHandler instead
}
# =========================
sub endRenderingHandler
{
### my ( $text ) = @_; # do not uncomment, use $_[0] instead
&TWiki::Func::writeDebug( "- QuizPlugin::endRenderingHandler( $web.$topic )" ) if $debug;
# This handler is called by getRenderedVersion just after the line loop
$_[0] =~ s/%QUIZ{(.*?)}%/&handleQuiz($1)/geo;
}
# =========================
sub handleQuiz {
my $attrs = shift;
my $name = TWiki::Func::extractNameValuePair($attrs);
my $correct = TWiki::Func::extractNameValuePair($attrs, "correct");
my $jumptopic = TWiki::Func::extractNameValuePair($attrs, "jump");
if (!defined($correct)) {
return "Invalid quiz $attrs - need correct answer(s)
";
}
my $result = "";
}
my $scriptHeader = "\n";
sub cheatScript {
my ($quizNumber,$set) = @_;
return "var first$quizNumber=1; function Cheat${quizNumber}() {
if (first$quizNumber==1) {
alert(\"You should try at least once before clicking on 'Show Solutions'!\");
} else { $set }}";
}
sub submitScript{
my ($quizNumber, $check, $jumpTopic) = @_;
my $result = "function Submit${quizNumber}() { first$quizNumber=0;
if ( $check ) {if (confirm(\"Correct!!! Click on OK to continue\"))";
if ($jumpTopic ne "") {
$result .= "window.location.replace(\"$jumpTopic\")";
}
return $result . ";} else {alert(\"Wrong! Try again...\\n\");}}";
}
# Generate script for someOf
sub someOfScript {
my ($quizNumber, $legalAnswers, $correctAnswers, $jumpTopic) = @_;
my @possibles = split(/;/, $legalAnswers);
my %nameMap;
my @corrects = split(/;/, $correctAnswers);
my $n = 0;
foreach my $poss (@possibles) {
$nameMap{$poss} = "window.document.quiz${quizNumber}.field[$n].checked";
$n++;
}
my $set = "";
my $check = "";
foreach my $poss (@possibles) {
if ($check ne "") {
$check .= "&&";
}
$set .= $nameMap{$poss};
if ($correctAnswers !~ /\b$poss\b/) {
$set .= "=false;";
$check .= "!";
} else {
$set .= "=true;";
}
$check .= $nameMap{$poss};
}
return $scriptHeader .
cheatScript($quizNumber, $set) .
submitScript($quizNumber, $check, $jumpTopic) .
$scriptFooter;
}
# Generate script for string
sub stringScript {
my ($quizNumber, $correctAnswer, $jumpTopic) = @_;
return $scriptHeader .
cheatScript($quizNumber,
"window.document.quiz" . $quizNumber .
".field.value=\"$correctAnswer\";") .
submitScript($quizNumber,
"window.document.quiz$quizNumber" .
".field.value==\"$correctAnswer\"", $jumpTopic) .
$scriptFooter;
}
1;