'."\n";
- $Str .= '';
- return $Str;
-}
-
-#######################################################
-#######################################################
-
-=pod
-
-=item &CreateAndParseOutputSelector()
-
-Construct a selection list of options for output and parse output selections.
-The current output selected is indicated by the values of the two package
-variables $output_mode and $show. @OutputOptions holds the descriptions of
-the output options and the values for $output_mode and $show.
-
-Based on code from lonstudentassessment.pm.
-
-=cut
-
-#######################################################
-#######################################################
-my $output_mode;
-my $show;
-
-my @OutputOptions =
- (
- { name => 'problem statistics grouped by sequence',
- value => 'HTML problem statistics grouped',
- description => 'Output statistics for the problem parts.',
- mode => 'html',
- show => 'grouped',
- },
- { name => 'problem statistics ungrouped',
- value => 'HTML problem statistics ungrouped',
- description => 'Output statistics for the problem parts.',
- mode => 'html',
- show => 'ungrouped',
- },
- { name => 'problem statistics, Excel',
- value => 'Excel problem statistics',
- description => 'Output statistics for the problem parts '.
- 'in an Excel workbook',
- mode => 'excel',
- show => 'all',
- },
- );
-
-sub OutputDescriptions {
- my $Str = '';
- $Str .= "
Output Modes
\n";
- $Str .= "
\n";
- foreach my $outputmode (@OutputOptions) {
- $Str .="
".$outputmode->{'name'}."
\n";
- $Str .="
".$outputmode->{'description'}."
\n";
- }
- $Str .= "
\n";
- return $Str;
-}
-
-sub CreateAndParseOutputSelector {
- my $Str = '';
- my $elementname = 'statsoutputmode';
- #
- # Format for output options is 'mode, restrictions';
- my $selected = 'HTML problem statistics grouped';
- if (exists($ENV{'form.'.$elementname})) {
- if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
- $selected = $ENV{'form.'.$elementname}->[0];
- } else {
- $selected = $ENV{'form.'.$elementname};
- }
- }
- #
- # Set package variables describing output mode
- $output_mode = 'html';
- $show = 'all';
- foreach my $option (@OutputOptions) {
- next if ($option->{'value'} ne $selected);
- $output_mode = $option->{'mode'};
- $show = $option->{'show'};
- }
- #
- # Build the form element
- $Str = qq/";
+ $Str .= '';
+ $Str .= ' 'x5;
+ $Str .= 'Plot '.&plot_dropdown().(' 'x10);
+ $Str .= '';
+ $Str .= ' 'x5;
+ $Str .= '';
+ $Str .= ' 'x5;
+ $Str .= '';
+ $Str .= ' 'x5;
return $Str;
}
@@ -305,44 +354,6 @@ sub CreateAndParseOutputSelector {
=pod
-=item &Gather_Student_Data()
-
-Ensures all student data is up to date.
-
-=cut
-
-###############################################
-###############################################
-sub Gather_Student_Data {
- my ($r) = @_;
- my $c = $r->connection();
- #
- my @Sequences = &Apache::lonstatistics::Sequences_with_Assess();
- #
- my @Students = @Apache::lonstatistics::Students;
- #
- # Open the progress window
- my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
- ($r,'Statistics Compilation Status',
- 'Statistics Compilation Progress', scalar(@Students));
- #
- while (my $student = shift @Students) {
- return if ($c->aborted());
- my ($status,undef) = &Apache::loncoursedata::ensure_current_data
- ($student->{'username'},$student->{'domain'},
- $ENV{'request.course.id'});
- &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
- 'last student');
- }
- &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
- $r->rflush();
-}
-
-###############################################
-###############################################
-
-=pod
-
=item &BuildProblemStatisticsPage()
Main interface to problem statistics.
@@ -354,247 +365,540 @@ Main interface to problem statistics.
sub BuildProblemStatisticsPage {
my ($r,$c)=@_;
#
- $output_mode = 'html';
- $show = 'grouped';
- #
- $r->print(&CreateInterface());
- $r->print('');
+ my %Saveable_Parameters = ('Status' => 'scalar',
+ 'statsoutputmode' => 'scalar',
+ 'Section' => 'array',
+ 'StudentData' => 'array',
+ 'Maps' => 'array',
+ 'fieldselections'=> 'array');
+ &Apache::loncommon::store_course_settings('statistics',
+ \%Saveable_Parameters);
+ &Apache::loncommon::restore_course_settings('statistics',
+ \%Saveable_Parameters);
+ #
+ &Apache::lonstatistics::PrepareClasslist();
+ #
+ # Clear the package variables
+ undef(@StatsArray);
+ #
+ # Finally let the user know we are here
+ my $interface = &CreateInterface();
+ $r->print($interface);
$r->print('');
- $r->print('');
+ #
if (! exists($ENV{'form.statsfirstcall'})) {
+ $r->print('');
+ $r->print('
'.
+ &mt('Press "Generate Statistics" when you are ready.').
+ '
'.
+ &mt('It may take some time to update the student data '.
+ 'for the first analysis. Future analysis this session '.
+ ' will not have this delay.').
+ '
');
return;
+ } elsif ($ENV{'form.statsfirstcall'} eq 'yes' ||
+ exists($ENV{'form.UpdateCache'}) ||
+ exists($ENV{'form.ClearCache'}) ) {
+ $r->print('');
+ &Apache::lonstatistics::Gather_Student_Data($r);
+ } else {
+ $r->print('');
}
+ $r->rflush();
#
- &Gather_Student_Data($r);
- #
+ # This probably does not need to be done each time we are called, but
+ # it does not slow things down noticably.
+ &Apache::loncoursedata::populate_weight_table();
#
- if ($output_mode eq 'html') {
- $r->print("
\n");
+ return;
+}
-Presents the statistics data as an html table organized by the order
-the assessments appear in the course.
+sub html_preamble {
+ my $Str='';
+ $Str .= "
\n";
+ my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
+ if (defined($starttime) || defined($endtime)) {
+ # Inform the user what the time limits on the data are.
+ $Str .= '
'.&mt('Statistics on submissions from [_1] to [_2]',
+ &Apache::lonlocal::locallocaltime($starttime),
+ &Apache::lonlocal::locallocaltime($endtime)
+ ).'
';
+ }
+ $Str .= "
".&mt('Compiled on [_1]',
+ &Apache::lonlocal::locallocaltime(time))."
";
+ return $Str;
+}
-=cut
###############################################
###############################################
-sub output_html_grouped_by_sequence {
- my ($r) = @_;
- my $problem_num = 0;
- #$r->print(&ProblemStatisticsLegend());
- foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
- next if ($sequence->{'num_assess'}<1);
- $r->print("
".$sequence->{'title'}."
");
- $r->print('
'."\n");
- $r->print('
'."\n");
- $r->print('
');
- my $Str = &statistics_table_header('no container no plots');
- $r->print('
'.$Str."
\n");
- foreach my $resource (@{$sequence->{'contents'}}) {
- next if ($resource->{'type'} ne 'assessment');
- foreach my $part (@{$resource->{'parts'}}) {
- $problem_num++;
- my $data = &get_statistics($sequence,$resource,$part,
- $problem_num);
- my $option = '';
- $r->print('
\n");
- $r->rflush();
+ $ymax = int($ymax)+1+2;
#
+ my $std_data .= ''.join(',',@Labels).''.$/.
+ ''.join(',',@Mean).''.$/;
+ #
+ my $std_error_data .= ''.join(',',@Labels).''.$/.
+ ''.join(',',@Mean).''.$/.
+ ''.join(',',@STD).''.$/;
+ #
+ $plot=<<"END";
+
+ Mean and S.D. of Tries
+
+ Problem Number
+
+ $std_error_data
+
+
+ $std_data
+
+
+END
+ my $plotresult =
+ '
'.&Apache::lonxml::xmlparse($r,'web',$plot).'
'.$/;
+ $r->print($plotresult);
return;
}
+sub plot_dropdown {
+ my $current = '';
+ #
+ if (defined($ENV{'form.plot'})) {
+ $current = $ENV{'form.plot'};
+ }
+ #
+ my @Additional_Plots = (
+ { graphable=>'yes',
+ name => 'degrees',
+ title => 'DoDisc and DoDiff' },
+ { graphable=>'yes',
+ name => 'tries statistics',
+ title => 'Mean and S.D. of Tries' });
+ #
+ my $Str= "\n".''."\n";
+ return $Str;
+}
+
###############################################
###############################################
-
-=pod
-
-=item &output_excel()
-
-Presents the statistical data in an Excel 95 compatable spreadsheet file.
-
-=cut
-
+##
+## Excel output routines
+##
###############################################
###############################################
-sub output_excel {
+sub Excel_output {
my ($r) = @_;
+ $r->print('
'.&mt('Preparing Excel Spreadsheet').'
');
+ ##
+ ## Compute the statistics
+ &compute_all_statistics($r);
+ my $c = $r->connection;
+ return if ($c->aborted());
+ ##
+ ## Create the excel workbook
my $filename = '/prtspool/'.
$ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
- time.'_'.rand(1000000000).'.xls';
- #
- my $excel_workbook = undef;
- my $excel_sheet = undef;
- #
- my $rows_output = 0;
- my $cols_output = 0;
+ time.'_'.rand(1000000000).'.xls';
+ my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
#
# Create sheet
- $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
+ my $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
#
# Check for errors
if (! defined($excel_workbook)) {
$r->log_error("Error creating excel spreadsheet $filename: $!");
- $r->print("Problems creating new Excel file. ".
+ $r->print(&mt("Problems creating new Excel file. ".
"This error has been logged. ".
- "Please alert your LON-CAPA administrator");
- return ;
+ "Please alert your LON-CAPA administrator."));
+ return 0;
}
#
# The excel spreadsheet stores temporary data in files, then put them
@@ -608,7 +912,12 @@ sub output_excel {
if (length($sheetname) > 31) {
$sheetname = substr($sheetname,0,31);
}
- $excel_sheet = $excel_workbook->addworksheet($sheetname);
+ my $excel_sheet = $excel_workbook->addworksheet(
+ &Apache::loncommon::clean_excel_name($sheetname));
+ ##
+ ## Begin creating excel sheet
+ ##
+ my ($rows_output,$cols_output) = (0,0);
#
# Put the course description in the header
$excel_sheet->write($rows_output,$cols_output++,
@@ -635,6 +944,21 @@ sub output_excel {
$excel_sheet->write($rows_output,$cols_output++,$sectionstring);
$cols_output += scalar(@Sections);
#
+ # Time restrictions
+ my $time_string;
+ if (defined($starttime)) {
+ # call localtime but not lonlocal:locallocaltime because excel probably
+ # cannot handle localized text. Probably.
+ $time_string .= 'Data collected from '.localtime($time_string);
+ if (defined($endtime)) {
+ $time_string .= ' to '.localtime($endtime);
+ }
+ $time_string .= '.';
+ } elsif (defined($endtime)) {
+ # See note above about lonlocal:locallocaltime
+ $time_string .= 'Data collected before '.localtime($endtime).'.';
+ }
+ #
# Put the date in there too
$excel_sheet->write($rows_output,$cols_output++,
'Compiled on '.localtime(time));
@@ -642,241 +966,174 @@ sub output_excel {
$rows_output++;
$cols_output=0;
#
- # Add the headers
+ # Long Headers
+ foreach my $field (@Fields) {
+ next if ($field->{'name'} eq 'problem_num');
+ next if ($field->{'selected'} ne 'yes');
+ if (exists($field->{'long_title'})) {
+ $excel_sheet->write($rows_output,$cols_output++,
+ $field->{'long_title'});
+ } else {
+ $excel_sheet->write($rows_output,$cols_output++,'');
+ }
+ }
+ $rows_output++;
+ $cols_output=0;
+ # Brief headers
foreach my $field (@Fields) {
+ next if ($field->{'selected'} ne 'yes');
next if ($field->{'name'} eq 'problem_num');
+ # Use english for excel as I am not sure how well excel handles
+ # other character sets....
$excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
}
$rows_output++;
- #
- # Write the data
- my $problem_num=0;
- foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
- next if ($sequence->{'num_assess'}<1);
- foreach my $resource (@{$sequence->{'contents'}}) {
- next if ($resource->{'type'} ne 'assessment');
- foreach my $part (@{$resource->{'parts'}}) {
- $cols_output=0;
- $problem_num++;
- my $data = &get_statistics($sequence,$resource,$part,
- $problem_num);
- #
- if (!defined($part) || $part eq '') {
- $part = ' ';
- }
- foreach my $field (@Fields) {
- next if ($field->{'name'} eq 'problem_num');
- $excel_sheet->write($rows_output,$cols_output++,
- $data->{$field->{'name'}});
- }
- $rows_output++;
- }
+ foreach my $data (@StatsArray) {
+ $cols_output=0;
+ foreach my $field (@Fields) {
+ next if ($field->{'selected'} ne 'yes');
+ next if ($field->{'name'} eq 'problem_num');
+ $excel_sheet->write($rows_output,$cols_output++,
+ $data->{$field->{'name'}});
}
+ $rows_output++;
}
#
- # Write the excel file
$excel_workbook->close();
+ #
# Tell the user where to get their excel file
$r->print(' '.
- 'Your Excel spreadsheet.'."\n");
+ ''.
+ &mt('Your Excel Spreadsheet').''."\n");
$r->rflush();
return;
}
-###############################################
-###############################################
-
-=pod
-
-=item &statistics_html_table_data()
-
-Help function used to format the rows for HTML table output.
-
-=cut
-
-###############################################
-###############################################
-sub statistics_html_table_data {
- my ($data,$options) = @_;
- my $row = '';
- foreach my $field (@Fields) {
- next if ($options =~ /no $field->{'name'}/);
- $row .= '
{'align'})) {
- $row .= ' align="'.$field->{'align'}.'"';
- }
- $row .= '>';
- if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
- $row .= '';
- }
- if (exists($field->{'format'})) {
- $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
- } else {
- $row .= $data->{$field->{'name'}};
- }
- if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
- $row.= '';
+##################################################
+##################################################
+##
+## Statistics Gathering and Manipulation Routines
+##
+##################################################
+##################################################
+sub compute_statistics_on_sequence {
+ my ($seq) = @_;
+ my @Data;
+ foreach my $res (@{$seq->{'contents'}}) {
+ next if ($res->{'type'} ne 'assessment');
+ foreach my $part (@{$res->{'parts'}}) {
+ #
+ # This is where all the work happens
+ my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
+ push (@Data,$data);
+ push (@StatsArray,$data);
}
- $row .= '
';
}
- return $row;
+ return @Data;
}
-sub statistics_table_header {
- my ($options) = @_;
- my $header_row;
- foreach my $field (@Fields) {
- next if ($options =~ /no $field->{'name'}/);
- $header_row .= '
\n");
#
- # Print out the data
- $ENV{'form.sortby'} = 'Contents';
-# &output_html_ungrouped($r);
+ # Renumber the data set
+ my $count;
+ foreach my $data (@StatsArray) {
+ $data->{'problem_num'} = ++$count;
+ }
return;
}
-###############################################
-###############################################
+########################################################
+########################################################
-=pod
+=pod
-=item &DrawGraph()
+=item &get_statistics()
-=cut
+Wrapper routine from the call to loncoursedata::get_problem_statistics.
+Calls lonstathelpers::get_time_limits() to limit the data set by time
+and &compute_discrimination_factor
-###############################################
-###############################################
-sub DrawGraph {
- my ($values,$title,$xaxis,$yaxis,$Max)=@_;
- $title = '' if (! defined($title));
- $xaxis = '' if (! defined($xaxis));
- $yaxis = '' if (! defined($yaxis));
- #
- my $sendValues = join(',', @$values);
- my $sendCount = scalar(@$values);
- $Max =1 if ($Max < 1);
- if ( int($Max) < $Max ) {
- $Max++;
- $Max = int($Max);
- }
- my @GData = ($title,$xaxis,$yaxis,$Max,$sendCount,$sendValues);
- return '';
-}
+Inputs: $sequence, $resource, $part, $problem_num
+
+Returns: Hash reference with statistics data from
+loncoursedata::get_problem_statistics.
+
+=cut
+########################################################
+########################################################
sub get_statistics {
my ($sequence,$resource,$part,$problem_num) = @_;
#
+ my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
my $symb = $resource->{'symb'};
my $courseid = $ENV{'request.course.id'};
#
- my $students = \@Apache::lonstatistics::Students;
- if ($Apache::lonstatistics::SelectedSections[0] eq 'all') {
- $students = undef;
- }
my $data = &Apache::loncoursedata::get_problem_statistics
- ($students,$symb,$part,$courseid);
+ (\@Apache::lonstatistics::SelectedSections,
+ $Apache::lonstatistics::enrollment_status,
+ $symb,$part,$courseid,$starttime,$endtime);
$data->{'part'} = $part;
$data->{'problem_num'} = $problem_num;
$data->{'container'} = $sequence->{'title'};
@@ -884,76 +1141,237 @@ sub get_statistics {
$data->{'title.link'} = $resource->{'src'}.'?symb='.
&Apache::lonnet::escape($resource->{'symb'});
#
+ if ($SelectedFields{'deg_of_disc'}) {
+ $data->{'deg_of_disc'} =
+ &compute_discrimination_factor($resource,$part,$sequence);
+ }
return $data;
}
+
###############################################
###############################################
-=pod
+=pod
+
+=item &compute_discrimination_factor()
+
+Inputs: $Resource, $Sequence
-=item &ProblemStatisticsLegend()
+Returns: integer between -1 and 1
=cut
###############################################
###############################################
-sub ProblemStatisticsLegend {
- my $Ptr = '';
- $Ptr = '
';
- $Ptr .= '
';
- $Ptr .= '#Stdnts
';
- $Ptr .= '
Total number of students attempted the problem.';
- $Ptr .= '
';
- $Ptr .= 'Tries
';
- $Ptr .= '
Total number of tries for solving the problem.';
- $Ptr .= '
';
- $Ptr .= 'Max Tries
';
- $Ptr .= '
Largest number of tries for solving the problem by a student.';
- $Ptr .= '
';
- $Ptr .= 'Mean
';
- $Ptr .= '
Average number of tries. [ Tries / #Stdnts ]';
- $Ptr .= '
';
- $Ptr .= '#YES
';
- $Ptr .= '
Number of students solved the problem correctly.';
- $Ptr .= '
';
- $Ptr .= '#yes
';
- $Ptr .= '
Number of students solved the problem by override.';
- $Ptr .= '
';
- $Ptr .= '%Wrong
';
- $Ptr .= '
Percentage of students who tried to solve the problem ';
- $Ptr .= 'but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
- $Ptr .= '
';
- $Ptr .= 'DoDiff
';
- $Ptr .= '
Degree of Difficulty of the problem. ';
- $Ptr .= '[ 1 - ((#YES+#yes) / Tries) ]';
- $Ptr .= '
';
- $Ptr .= 'S.D.
';
- $Ptr .= '
Standard Deviation of the tries. ';
- $Ptr .= '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) ';
- $Ptr .= 'where Xi denotes every student\'s tries ]';
- $Ptr .= '
';
- $Ptr .= 'Skew.
';
- $Ptr .= '
Skewness of the students tries.';
- $Ptr .= '[(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]';
- $Ptr .= '
';
- $Ptr .= 'Dis.F.
';
- $Ptr .= '
Discrimination Factor: A Standard for evaluating the ';
- $Ptr .= 'problem according to a Criterion ';
- $Ptr .= '[Criterion to group students into %27 Upper Students - ';
- $Ptr .= 'and %27 Lower Students] ';
- $Ptr .= '1st Criterion for Sorting the Students: ';
- $Ptr .= 'Sum of Partial Credit Awarded / Total Number of Tries ';
- $Ptr .= '2nd Criterion for Sorting the Students: ';
- $Ptr .= 'Total number of Correct Answers / Total Number of Tries';
- $Ptr .= '
';
- $Ptr .= '
Disc.
';
- $Ptr .= '
Number of Students had at least one discussion.';
- $Ptr .= '
';
- return $Ptr;
+sub compute_discrimination_factor {
+ my ($resource,$part,$sequence) = @_;
+ my @Resources;
+ foreach my $res (@{$sequence->{'contents'}}) {
+ next if ($res->{'symb'} eq $resource->{'symb'});
+ push (@Resources,$res->{'symb'});
+ }
+ #
+ # rank
+ my $ranking =
+ &Apache::loncoursedata::rank_students_by_scores_on_resources
+ (\@Resources,
+ \@Apache::lonstatistics::SelectedSections,
+ $Apache::lonstatistics::enrollment_status,undef);
+ #
+ # compute their percent scores on the problems in the sequence,
+ my $number_to_grab = int(scalar(@{$ranking})/4);
+ my $num_students = scalar(@{$ranking});
+ my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()];
+ } @{$ranking}[0..$number_to_grab];
+ my @TopSet =
+ map {
+ $_->[&Apache::loncoursedata::RNK_student()];
+ } @{$ranking}[($num_students-$number_to_grab)..($num_students-1)];
+ my ($bottom_sum,$bottom_max) =
+ &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@BottomSet);
+ my ($top_sum,$top_max) =
+ &Apache::loncoursedata::get_sum_of_scores($resource,$part,\@TopSet);
+ my $deg_of_disc;
+ if ($top_max == 0 || $bottom_max==0) {
+ $deg_of_disc = 'nan';
+ } else {
+ $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
+ }
+ #&Apache::lonnet::logthis(' '.$top_sum.'/'.$top_max.
+ # ' - '.$bottom_sum.'/'.$bottom_max);
+ return $deg_of_disc;
}
-#---- END Problem Statistics Web Page ----------------------------------------
+###############################################
+###############################################
+
+=pod
+
+=item ProblemStatisticsLegend
+
+=over 4
+
+=item #Stdnts
+Total number of students attempted the problem.
+
+=item Tries
+Total number of tries for solving the problem.
+
+=item Max Tries
+Largest number of tries for solving the problem by a student.
+
+=item Mean
+Average number of tries. [ Tries / #Stdnts ]
+
+=item #YES
+Number of students solved the problem correctly.
+
+=item #yes
+Number of students solved the problem by override.
+
+=item %Wrong
+Percentage of students who tried to solve the problem
+but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
+
+=item DoDiff
+Degree of Difficulty of the problem.
+[ 1 - ((#YES+#yes) / Tries) ]
+
+=item S.D.
+Standard Deviation of the tries.
+[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1)
+where Xi denotes every student\'s tries ]
+
+=item Skew.
+Skewness of the students tries.
+[(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
+
+=item Dis.F.
+Discrimination Factor: A Standard for evaluating the
+problem according to a Criterion
+
+=item [Criterion to group students into %27 Upper Students -
+and %27 Lower Students]
+1st Criterion for Sorting the Students:
+Sum of Partial Credit Awarded / Total Number of Tries
+2nd Criterion for Sorting the Students:
+Total number of Correct Answers / Total Number of Tries
+
+=item Disc.
+Number of Students had at least one discussion.
+
+=back
+
+=cut
+
+
+############################################################
+############################################################
+##
+## How this all works:
+## Statistics are computed by calling &get_statistics with the sequence,
+## resource, and part id to run statistics on. At various places within
+## the loops which compute the statistics, as well as before and after
+## the entire process, subroutines can be called. The subroutines are
+## registered to the following hooks:
+##
+## hook subroutine inputs
+## ----------------------------------------------------------
+## pre $r,$count
+## pre_seq $r,$count,$seq
+## pre_res $r,$count,$seq,$res
+## calc $r,$count,$seq,$res,$data
+## post_res $r,$count,$seq,$res
+## post_seq $r,$count,$seq
+## post $r,$count
+##
+## abort $r
+##
+## subroutines will be called in the order in which they are registered.
+##
+############################################################
+############################################################
+{
+
+my %hooks;
+my $aborted = 0;
+
+sub abort_computation {
+ $aborted = 1;
+}
+
+sub clear_hooks {
+ $aborted = 0;
+ undef(%hooks);
+}
+
+sub register_hook {
+ my ($hookname,$subref)=@_;
+ if ($hookname !~ /^(pre|pre_seq|pre_res|post|post_seq|post_res|calc)$/){
+ return;
+ }
+ if (ref($subref) ne 'CODE') {
+ &Apache::lonnet::logthis('attempt to register hook to non-code: '.
+ $hookname,' = '.$subref);
+ } else {
+ if (exists($hooks{$hookname})) {
+ push(@{$hooks{$hookname}},$subref);
+ } else {
+ $hooks{$hookname} = [$subref];
+ }
+ }
+ return;
+}
+
+sub run_hooks {
+ my $context = shift();
+ foreach my $hook (@{$hooks{$context}}) {
+ if ($aborted && $context ne 'abort') {
+ last;
+ }
+ my $retvalue = $hook->(@_);
+ if (defined($retvalue) && $retvalue eq '0') {
+ $aborted = 1 if (! $aborted);
+ }
+ }
+}
+
+sub run_statistics {
+ my ($r) = @_;
+ my $count = 0;
+ &run_hooks('pre',$r,$count);
+ foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
+ last if ($aborted);
+ next if ($seq->{'num_assess'}<1);
+ &run_hooks('pre_seq',$r,$count,$seq);
+ foreach my $res (@{$seq->{'contents'}}) {
+ last if ($aborted);
+ next if ($res->{'type'} ne 'assessment');
+ &run_hooks('pre_res',$r,$count,$seq,$res);
+ foreach my $part (@{$res->{'parts'}}) {
+ last if ($aborted);
+ #
+ # This is where all the work happens
+ my $data = &get_statistics($seq,$res,$part,++$count);
+ &run_hooks('calc',$r,$count,$seq,$res,$part,$data);
+ }
+ &run_hooks('post_res',$r,$count,$seq,$res);
+ }
+ &run_hooks('post_seq',$r,$count,$seq);
+ }
+ if ($aborted) {
+ &run_hooks('abort',$r);
+ } else {
+ &run_hooks('post',$r,$count);
+ }
+ return;
+}
+
+} # End of %hooks scope
+
+############################################################
+############################################################
1;
__END__