We have added a global counter, from Peirre Omidyar, which
keeps track of the access counts of all files.
The Central.count file is kept in /~cdavid/public_html/
and in it are separate lines for each program which calls
&count.
#!/usr/local/bin/perl -- -*-perl-*-
use CGI;
require "CleanUp.pl";
require "explain.pl";
require "count.pl";
srand;
$query = new CGI;
print $query->header;
print $query->start_html('Prototype Question_2');
$scriptname = $query->script_name();#this gives the question name
#print $query->dump;#use this to check that it worked! Just remove comment(#).
print "<a href=/~cdavid/cgi-bin/test_send_memo.pl?var=$scriptname>Click here to send a message concerning errors in this question.<br></a>";
# this question was called via html?owner=Jones&name=SomeOne
$faculty_id = $query->param('owner');
$student_id = $query->param('name');
print "<br>";
print "<a href=send_memo.html>Click here to get a notation explanation</a>";
print "<br>";
HERE IS THE CALL
&count($scriptname);
which (based on the require line above) calls a common subroutine located
in the cgi-bin subdirectory.
The filename of this program is passed to &count, and that name
is what is recorded in Central.count.
print $query->start_form;
print <<EOF;
This is teacher created text concerning this particular question.
Without explicit print statements, it must be entered using a
special format.
<br>The faculty owner of this question is $faculty_id.
<br>The student owner of this questions is $student_id.
EOF
print "<br>Answer = ",$query->textfield('ans','',50,80);
print"<P> Query, is the above formula/number correct?",$query->submit;
print $query->end_form;
if ( $query->param('ans') ne '')
{
$stu_ans = $query->param('ans');
$saved_stu_ans = $stu_ans;
&CleanUp($stu_ans);#this removes potential hacker imbedded illegal commands
$ans = "sqrt(4/x)";
$x = rand;
$ans =~s/x/$x/gi;
$stu_ans =~s/x/$x/gi;
print "student_answer = $saved_stu_ans";
$stu_ans = eval($stu_ans);
$ans = eval($ans);
#print "student_answer = $stu_ans <p>";
#print "student_answer evaluated = $stu_ans<p>";
#print "answer evaluated = $ans <p>";
if (eval (($stu_ans - $ans)**2 - 0.0001 ) ge 0.0 )
{print ", <EM> Wrong</EM>,
<IMG SRC=../icons/checkno.gif>"}
else {print ", <EM> Right! </EM>,
<IMG SRC=../icons/check.gif>" };
};
print $query->end_html;
The author, Pierre Omidyar
can be visited.
sub count
{
#
# A simple counter program which maintains counts for
# all files in a single counts file. Some systems may
# not allow "." files to be created, so you should
# rename the count file.
#
# Comments to pierre@best.com (Pierre Omidyar)
#
#
# get name of accessed document
#
# open counts file for read & write
#
$doc = $_[0];
#print $doc;
$host = $query->remote_host();
#we will place the Central.counts file in public_html.
$location = "../Central.counts";
open(COUNTS, "$location") || print "<br>having trouble opening Central.counts file";
# die("pierre:can't open .counts.
");
#flock(COUNTS, 2); # will wait for lock
# this statement is causing local trouble.
#
# read counts database into associative array
#
while (<COUNTS>) {
chop;
($count, $file) = split(/:/, $_);
#print "<br> after split",$file;
if ($file eq ''){$file = $doc;
$count = 0;
};
$counts{$file} = $count;
#print "<br>",$counts{$doc};
}
close(COUNTS);
#
# increment count of hit document, but not if I'm the
# one accessing it.
#
#
# increment count for this file
#
#print "<br>",$counts{$doc};
$counts{$doc} = $counts{$doc}+1;
#print "<br> after incrementing ",$counts{$doc};
#
# rewrite count file
# put file marker back at beginning
#
open(COUNTS, ">$location") || print "<br>having trouble opening file";
foreach $file (keys %counts) {
print COUNTS $counts{$file}, ":", $file, "
";
}
close(COUNTS);
#
# print count string to STDOUT to be imbedded into WWW page
#
print "<br>This document has been called ",$counts{$doc}," times.";
}
return -1;
To try this prototype question (with counter),
click here