--- loncom/interface/statistics/lonproblemstatistics.pm 2002/10/30 18:37:00 1.34
+++ loncom/interface/statistics/lonproblemstatistics.pm 2003/05/12 22:07:17 1.50
@@ -1,7 +1,6 @@
# The LearningOnline Network with CAPA
-# (Publication Handler
#
-# $Id: lonproblemstatistics.pm,v 1.34 2002/10/30 18:37:00 minaeibi Exp $
+# $Id: lonproblemstatistics.pm,v 1.50 2003/05/12 22:07:17 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -26,489 +25,887 @@
# http://www.lon-capa.org/
#
# (Navigate problems for statistical reports
-# YEAR=2001
-# 5/5,7/9,7/25/1,8/11,9/13,9/26,10/5,10/9,10/22,10/26 Behrouz Minaei
-# 11/1,11/4,11/16,12/14,12/16,12/18,12/20,12/31 Behrouz Minaei
-# YEAR=2002
-# 1/22,2/1,2/6,2/25,3/2,3/6,3/17,3/21,3/22,3/26,4/7,5/6 Behrouz Minaei
-# 5/12,5/14,5/15,5/19,5/26,7/16,7/25,7/29,8/5 Behrouz Minaei
#
-###
+###############################################
+###############################################
-package Apache::lonproblemstatistics;
+=pod
+
+=head1 NAME
+
+lonproblemstatistics
+
+=head1 SYNOPSIS
+
+Routines to present problem statistics to instructors via tables,
+Excel files, and plots.
+
+=over 4
+
+=cut
+
+###############################################
+###############################################
+
+package Apache::lonproblemstatistics;
use strict;
use Apache::lonnet();
use Apache::lonhtmlcommon;
use Apache::loncoursedata;
-use GDBM_File;
+use Apache::lonstatistics;
+use Spreadsheet::WriteExcel;
-my $jr;
+my @Fields = (
+ { name => 'problem_num',
+ title => 'P#',
+ align => 'right',
+ color => '#FFFFE6' },
+ { name => 'container',
+ title => 'Container',
+ align => 'left',
+ color => '#FFFFE6',
+ sortable => 'yes' },
+ { name => 'title',
+ title => 'Title',
+ align => 'left',
+ color => '#FFFFE6',
+ special => 'link',
+ sortable => 'yes', },
+ { name => 'part',
+ title => 'Part',
+ align => 'left',
+ color => '#FFFFE6' },
+ { name => 'num_students',
+ title => '#Stdnts',
+ align => 'right',
+ color => '#EEFFCC',
+ format => '%d',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Number of Students Attempting Problem' },
+ { name => 'tries',
+ title => 'Tries',
+ align => 'right',
+ color => '#EEFFCC',
+ format => '%d',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Total Number of Tries' },
+ { name => 'max_tries',
+ title => 'Max Tries',
+ align => 'right',
+ color => '#DDFFFF',
+ format => '%d',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Maximum Number of Tries' },
+ { name => 'mean_tries',
+ title => 'Mean Tries',
+ align => 'right',
+ color => '#DDFFFF',
+ format => '%5.2f',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Average Number of Tries' },
+ { name => 'std_tries',
+ title => 'S.D. tries',
+ align => 'right',
+ color => '#DDFFFF',
+ format => '%5.2f',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Standard Deviation of Number of Tries' },
+ { name => 'skew_tries',
+ title => 'Skew Tries',
+ align => 'right',
+ color => '#DDFFFF',
+ format => '%5.2f',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Skew of Number of Tries' },
+ { name => 'deg_of_diff',
+ title => 'DoDiff',
+ align => 'right',
+ color => '#DDFFFF',
+ format => '%5.2f',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Degree of Difficulty' },
+ { name => 'num_solved',
+ title => '#YES',
+ align => 'right',
+ color => '#FFDDDD',
+ format => '%d',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Number of Students able to Solve' },
+ { name => 'num_override',
+ title => '#yes',
+ align => 'right',
+ color => '#FFDDDD',
+ format => '%d',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Number of Students given Override' },
+ { name => 'per_wrong',
+ title => '%Wrng',
+ align => 'right',
+ color => '#FFFFE6',
+ format => '%4.1f',
+ sortable => 'yes',
+ graphable => 'yes',
+ long_title => 'Percent Wrong' },
+);
+
+###############################################
+###############################################
+
+=pod
+
+=item &CreateInterface()
+
+Create the main intereface for the statistics page. Allows the user to
+select sections, maps, and output.
+
+=cut
+
+###############################################
+###############################################
+sub CreateInterface {
+ my $Str = '';
+ $Str .= '
'."\n";
+ $Str .= '';
+ $Str .= 'Sections ';
+ $Str .= 'Enrollment Status ';
+ $Str .= 'Sequences and Folders ';
+ $Str .= 'Output ';
+ $Str .= ' '."\n";
+ #
+ $Str .= ''."\n";
+ $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
+ $Str .= ' ';
+ $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
+ $Str .= ' ';
+ #
+ my $only_seq_with_assessments = sub {
+ my $s=shift;
+ if ($s->{'num_assess'} < 1) {
+ return 0;
+ } else {
+ return 1;
+ }
+ };
+ $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
+ $only_seq_with_assessments);
+ $Str .= ' '."\n";
+ $Str .= &CreateAndParseOutputSelector();
+ $Str .= ' '."\n";
+ $Str .= '
'."\n";
+ return $Str;
+}
-sub InitializeProblemStatistics {
- my ($cacheDB, $students, $courseID, $c, $r)=@_;
- my %cache;
-
- $jr = $r;
-
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
- $r->print('Unable to tie database1.');
- return ('ERROR', undef);
- }
-
- # Remove students who don't have the proper section.
- my @sectionsSelected = split(':',$cache{'sectionsSelected'});
- for(my $studentIndex=((scalar @$students)-1); $studentIndex>=0;
- $studentIndex--) {
- my $value = $cache{$students->[$studentIndex].':section'};
- my $found = 0;
- foreach (@sectionsSelected) {
- if($_ eq 'none') {
- if($value eq '' || !defined($value) || $value eq ' ') {
- $found = 1;
- last;
- }
- } else {
- if($value eq $_) {
- $found = 1;
- last;
- }
- }
- }
- if($found == 0) {
- splice(@$students, $studentIndex, 1);
- }
- }
+#######################################################
+#######################################################
+
+=pod
+
+=item &CreateAndParseOutputSelector()
- my $isNotCached = 0;
- my $lastStatus = (defined($cache{'StatisticsLastStatus'})) ?
- $cache{'StatisticsLastStatus'} : 'Nothing';
- my $whichStudents = join(':::',sort(@$students));
- if(!defined($cache{'StatisticsCached'}) ||
- $lastStatus ne $cache{'Status'} ||
- $whichStudents ne $cache{'StatisticsWhichStudents'}) {
- $isNotCached = 1;
+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;
+}
- untie(%cache);
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
- $r->print('Unable to tie database.2');
- return ('ERROR', undef);
+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};
+ }
}
- if($isNotCached && defined($cache{'StatisticsCached'})) {
- my @statkeys = split(':::', $cache{'StatisticsKeys'});
- delete $cache{'StatisticsKeys'};
- delete $cache{'StatisticsCached'};
- foreach(@statkeys) {
- delete $cache{$_};
+ #
+ # 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//;
+ foreach my $option (@OutputOptions) {
+ if (exists($option->{'special'}) &&
+ $option->{'special'} =~ /do not show/) {
+ next;
}
+ $Str .= "\n".' {'value'} eq $selected);
+ $Str .= ">".$option->{'name'}."<\/option>";
}
+ $Str .= "\n ";
+ return $Str;
+}
- untie(%cache);
- if($isNotCached) {
- &Apache::loncoursedata::DownloadStudentCourseDataSeparate($students,
- 'true',
- $cacheDB,
- 'true',
- 'true',
- $courseID,
- $r, $c);
- }
- if($c->aborted()) { return ('ERROR', undef); }
+###############################################
+###############################################
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
- $r->print('Unable to tie database.3');
- return ('ERROR', undef);
- }
- my $problemData;
- if($isNotCached) {
- ($problemData) = &ExtractStudentData(\%cache, $students);
- &CalculateStatistics($problemData, \%cache, $courseID);
- }
- untie(%cache);
+=pod
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
- $r->print('Unable to tie database.4');
- return ('ERROR', undef);
- }
- if($isNotCached) {
- foreach(keys(%$problemData)) {
- $cache{$_} = $problemData->{$_};
- }
- $cache{'StatisticsKeys'} = join(':::', keys(%$problemData));
- $cache{'StatisticsCached'} = 'true';
- $cache{'StatisticsLastStatus'} = $cache{'Status'};
- $cache{'StatisticsWhichStudents'} = $whichStudents;
- }
- untie(%cache);
+=item &Gather_Student_Data()
+
+Ensures all student data is up to date.
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
- $r->print('Unable to tie database.5');
- return ('ERROR', undef);
+=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();
+}
- my $orderedProblems = &SortProblems(\%cache,
- $cache{'ProblemStatisticsSort'},
- $cache{'SortProblems'},
- $cache{'ProblemStatisticsAscend'});
- untie(%cache);
+###############################################
+###############################################
- return ('OK', $orderedProblems);
-}
+=pod
-sub BuildProblemStatisticsPage {
- my ($cacheDB, $students, $courseID, $c, $r)=@_;
+=item &BuildProblemStatisticsPage()
- my @Header = ("Homework Sets Order","#Stdnts","Tries","Mod",
- "Mean","#YES","#yes","%Wrng","DoDiff",
- "S.D.","Skew.","D.F.1st","D.F.2nd");
- my $color=&setbgcolor(0);
- my %cache;
+Main interface to problem statistics.
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
- $r->print('Unable to tie database.6');
- return;
- }
- my $Ptr = '';
- $Ptr .= '';
- $Ptr .= 'Select Map '."\n";
- $Ptr .= '';
- $Ptr .= &Apache::lonhtmlcommon::MapOptions(\%cache, 'Statistics',
- 'Statistics');
- $Ptr .= ' '."\n";
- $Ptr .= 'Sorting Type: '."\n";
- $Ptr .= ''."\n";
- $Ptr .= &Apache::lonhtmlcommon::AscendOrderOptions(
- $cache{'ProblemStatisticsAscend'},
- 'ProblemStatistics',
- 'Statistics');
- $Ptr .= ' '."\n";
- $Ptr .= 'Select Sections ';
- $Ptr .= ' '."\n";
- $Ptr .= ''."\n";
- my @sections = split(':',$cache{'sectionList'});
- my @sectionsSelected = split(':',$cache{'sectionsSelected'});
- $Ptr .= &Apache::lonhtmlcommon::MultipleSectionSelect(\@sections,
- \@sectionsSelected,
- 'Statistics');
- $Ptr .= ' '."\n";
- $Ptr .= &ProblemStatisticsButtons($cache{'DisplayFormat'},
- $cache{'DisplayLegend'},
- $cache{'SortProblems'});
- $Ptr .= '
';
- if($cache{'DisplayLegend'} eq 'Show Legend') {
- $Ptr .= &ProblemStatisticsLegend();
- }
- $r->print($Ptr);
- $r->rflush();
- untie(%cache);
+=cut
- my ($result, $orderedProblems) =
- &InitializeProblemStatistics($cacheDB, $students, $courseID, $c, $r);
- if($result ne 'OK') {
+###############################################
+###############################################
+sub BuildProblemStatisticsPage {
+ my ($r,$c)=@_;
+ #
+ $output_mode = 'html';
+ $show = 'grouped';
+ #
+ $r->print(&CreateInterface());
+ $r->print(' ');
+ $r->print(' ');
+ $r->print(' ');
+ if (! exists($ENV{'form.statsfirstcall'})) {
+ $r->print(<
+
+Please make your selections in the boxes above and hit
+the button marked "Update Display".
+
+
+ENDMSG
return;
}
-
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
- $r->print('Unable to tie database.6');
- return;
+ #
+ &Gather_Student_Data($r);
+ #
+ #
+ if ($output_mode eq 'html') {
+ $r->print("".
+ $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
+ " \n");
+ $r->print("".localtime(time)." ");
+ $r->rflush();
+ if ($show eq 'grouped') {
+ &output_html_grouped_by_sequence($r);
+ } elsif ($show eq 'ungrouped') {
+ &output_html_ungrouped($r);
+ }
+ } elsif ($output_mode eq 'excel') {
+ $r->print("Preparing Excel Spreadsheet ");
+ &output_excel($r);
+ } else {
+ $r->print("Not implemented ");
}
- &BuildStatisticsTable(\%cache, $cache{'DisplayFormat'},
- $cache{'SortProblems'}, $orderedProblems,
- \@Header, $r, $color);
- untie(%cache);
-
return;
}
-sub BuildGraphicChart {
- my ($graph,$cacheDB,$courseDescription,$students,$courseID,$r,$c)=@_;
- my %cache;
- my $max = 0;
- unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
- return 'Unable to tie database.7';
- }
+###############################################
+###############################################
-# my @problems = split(':::', $cache{'problemList'});
-
- my $title = '';
- if($graph eq 'DoDiffGraph') {
- $title = 'Degree-of-Difficulty';
- } else {
- $title = 'Wrong-Percentage';
- }
+=pod
+=item &output_html_grouped_by_sequence()
- # foreach (@problems) {
- # my $data = 0;
- # if($graph eq 'DoDiffGraph') {
- # $data = sprintf("%.2f", $cache{$_.':degreeOfDifficulty'}),
- # } else {
- # $data = sprintf("%.1f", $cache{$_.':percentWrong'}),
- # }
- # if($max < $data) {
- # $max = $data;
- # }
- # push(@values, $data);
- # }
-
-
- my $count = 1;
- my $currentSequence = -1;
- my $sortProblems = 'Sort Within Sequence';
-
- my ($result, $orderedProblems) =
- &InitializeProblemStatistics($cacheDB, $students, $courseID, $c, $r);
- if($result ne 'OK') {
- return;
+Presents the statistics data as an html table organized by the order
+the assessments appear in the course.
+
+=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(''.&statistics_html_table_data($data,
+ 'no container').
+ " \n");
+ }
+ }
+ $r->print("
\n");
+ $r->print("
\n");
+ $r->rflush();
}
+ #
+ return;
+}
- my @values = ();
+###############################################
+###############################################
- foreach(@$orderedProblems) {
- my ($sequence,$problem,$part)=split(':', $_);
- if($cache{'StatisticsMaps'} ne 'All Maps' &&
- $cache{'StatisticsMaps'} ne $cache{$sequence.':title'}) {
- next;
- }
+=pod
- if($currentSequence == -1 ||
- ($sortProblems eq 'Sort Within Sequence' &&
- $currentSequence != $sequence)) {
- if($currentSequence ne -1) {
- #$r->print(' finish a graph ');
- }
- if($sortProblems eq 'Sort Within Sequence') {
- $r->print(''.$cache{$sequence.':title'}.' ');
- }
-
- my $sendValues = join(',', @values);
- my $sendCount = scalar(@values);
+=item &output_html_ungrouped()
- my @GData = ($courseDescription, 'Problems',
- $title, $max, $sendCount, $sendValues);
- $r->print(''."\n");
- $r->print(' ');
- $r->print('