--- loncom/interface/statistics/lonstudentassessment.pm 2002/07/26 19:49:26 1.4 +++ loncom/interface/statistics/lonstudentassessment.pm 2007/10/03 17:39:13 1.145 @@ -1,12 +1,10 @@ # The LearningOnline Network with CAPA -# (Publication Handler # -# $Id: lonstudentassessment.pm,v 1.4 2002/07/26 19:49:26 stredwic Exp $ +# $Id: lonstudentassessment.pm,v 1.145 2007/10/03 17:39:13 albertel Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). -# # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or @@ -26,605 +24,2151 @@ # 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 Behrouz Minaei # -### +####################################################### +####################################################### + +=pod + +=head1 NAME + +lonstudentassessment + +=head1 SYNOPSIS + +Presents assessment data about a student or a group of students. -package Apache::lonstudentassessment; +=head1 Subroutines + +=over 4 + +=cut + +####################################################### +####################################################### + +package Apache::lonstudentassessment; use strict; -use Apache::lonhtmlcommon; +use Apache::lonstatistics(); +use Apache::lonhtmlcommon(); +use Apache::loncommon(); use Apache::loncoursedata; -use GDBM_File; +use Apache::lonnet; # for logging porpoises +use Apache::lonlocal; +use Apache::grades(); +use Apache::lonmsgdisplay(); +use Time::HiRes; +use Spreadsheet::WriteExcel; +use Spreadsheet::WriteExcel::Utility(); +use lib '/home/httpd/lib/perl/'; +use LONCAPA; + + +####################################################### +####################################################### +=pod + +=item Package Variables + +=over 4 + +=item $Statistics Hash ref to store student data. Indexed by symb, + contains hashes with keys 'score' and 'max'. + +=cut + +####################################################### +####################################################### + +my $Statistics; + +####################################################### +####################################################### + +=pod + +=item $show_links 'yes' or 'no' for linking to student performance data + +=item $output_mode 'html', 'excel', or 'csv' for output mode + +=item $show 'all', 'totals', or 'scores' determines how much data is output + +=item $single_student_mode evaluates to true if we are showing only one +student. + +=cut + +####################################################### +####################################################### +my $show_links; +my $output_mode; +my $chosen_output; +my $single_student_mode; + +####################################################### +####################################################### +# End of package variable declarations + +=pod + +=back + +=cut + +####################################################### +####################################################### + +=pod + +=item &BuildStudentAssessmentPage() + +Inputs: + +=over 4 + +=item $r Apache Request + +=item $c Apache Connection + +=back -#my $jr; +=cut +####################################################### +####################################################### sub BuildStudentAssessmentPage { - my ($cacheDB,$students,$courseID,$formName,$headings,$spacing, - $studentInformation,$r,$c)=@_; -# $jr = $r; - my %cache; - unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER,0640)) { - $r->print('
Unable to tie database.'); + my ($r,$c)=@_; + # + undef($Statistics); + undef($show_links); + undef($output_mode); + undef($chosen_output); + undef($single_student_mode); + # + my %Saveable_Parameters = ('Status' => 'scalar', + 'chartoutputmode' => 'scalar', + 'chartoutputdata' => 'scalar', + 'Section' => 'array', + 'Groups' => 'array', + 'StudentData' => 'array', + 'Maps' => 'array'); + &Apache::loncommon::store_course_settings('chart',\%Saveable_Parameters); + &Apache::loncommon::restore_course_settings('chart',\%Saveable_Parameters); + # + &Apache::lonstatistics::PrepareClasslist(); + # + $single_student_mode = 0; + $single_student_mode = 1 if ($env{'form.SelectedStudent'}); + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, + ['selectstudent']); + if ($env{'form.selectstudent'}) { + &Apache::lonstatistics::DisplayClasslist($r); return; } - - # 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); + # + # Print out the HTML headers for the interface + # This also parses the output mode selector + # This step must *always* be done. + $r->print(&CreateInterface()); + $r->print(''); + $r->print(''); + $r->rflush(); + # + if (! exists($env{'form.notfirstrun'}) && ! $single_student_mode) { + return; + } + $r->print(''."\n"); - foreach (@$students) { - next if ($_ ne $selectedName && - $selectedName ne 'All Students'); - $selected = 1; - my $courseData; - if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER,0640)) { - if($cache{$_.':lastDownloadTime'} eq 'Not downloaded') { - untie(%cache); - $courseData = - &Apache::loncoursedata::DownloadCourseInformation($_, - $courseID); - if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT,0640)) { - &Apache::loncoursedata::ProcessStudentData(\%cache, - $courseData, $_); - untie(%cache); - } else { - last if($c->aborted()); - next; - } - } else { - untie(%cache); - } - } else { - last if($c->aborted()); - next; +####################################################### +####################################################### + +sub get_student_fields_to_show { + my @to_show = @Apache::lonstatistics::SelectedStudentData; + foreach (@to_show) { + if ($_ eq 'all') { + @to_show = @Apache::lonstatistics::StudentDataOrder; + last; } + } + return @to_show; +} - last if ($c->aborted()); +####################################################### +####################################################### - if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER,0640)) { - my $displayString = 'DISPLAYDATA'.$spacing; - $r->print(&Apache::lonhtmlcommon::FormatStudentInformation( - \%cache, $_, - $infoKeys, - $displayString, - 'preformatted')); - $r->print(&StudentReport(\%cache, $_, $spacing, $sequenceKeys)); - $r->print("\n"); - untie(%cache); - } - } - $r->print(''."\n"); - if($selected == 0) { - $Str .= '
'."\n"; - $Str .= ''. + &Apache::loncommon::help_open_topic("Chart_Sections"). + ' | '; + $Str .= ''.&mt('Groups').''. + ' | '; + $Str .= ''.&mt('Student Data'). + &Apache::loncommon::help_open_topic("Chart_Student_Data"). + ' | '; + $Str .= ''.&mt('Access Status').''. + &Apache::loncommon::help_open_topic("Chart_Enrollment_Status"). + ' | '; + $Str .= ''.&mt('Sequences and Folders').''. + &Apache::loncommon::help_open_topic("Chart_Sequences"). + ' | '; + $Str .= ''.&mt('Output Format').''. + &Apache::loncommon::help_open_topic("Chart_Output_Formats"). + ' | '; + $Str .= ''.&mt('Output Data').''. + &Apache::loncommon::help_open_topic("Chart_Output_Data"). + ' | '; + $Str .= '|||||||||||||||||||
'."\n"; - my @sections = split(':',$cache->{'sectionList'}); - my @selectedSections = split(':',$cache->{'sectionsSelected'}); - $Str .= &Apache::lonhtmlcommon::MultipleSectionSelect(\@sections, - \@selectedSections, - 'Statistics'); + $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5); + $Str .= ' | '; + $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5); $Str .= ' | '; - $Str .= &CreateColumnSelectionBox($doNotShow); + $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple', + 5,undef); $Str .= ' | '."\n";
- $Str .= '';
+ $Str .= ' 'x5;
+ $Str .= '';
+ $Str .= ' 'x5;
+ $Str .= '';
+ $Str .= ' 'x5;
+ $Str .=
+ &mt('Status [_1]',
+ '');
+ $Str .= ' '; return $Str; } -sub CreateTableHeadings { - my($cache,$spacing,$infoKeys,$infoHeadings,$sequenceKeys, - $sequenceHeadings)=@_; +####################################################### +####################################################### - my $Str = ''; - $Str .= '
Output Formats\n"; + $Str .= "
Output Data\n"; + $Str .= "
'; - return $Str; - } - - my $Version; - my $problemsCorrect = 0; - my $totalProblems = 0; - my $problemsSolved = 0; - my $numberOfParts = 0; -# foreach my $sequence (split(':', $cache->{'orderedSequences'})) { - foreach my $sequence (@$showSequences) { - my $characterCount=0; - foreach my $problemID (split(':', $cache->{$sequence.':problems'})) { - my $problem = $cache->{$problemID.':problem'}; - my $LatestVersion = $cache->{$name.':version:'.$problem}; - - # Output dashes for all the parts of this problem if there - # is no version information about the current problem. - if(!$LatestVersion) { - foreach my $part (split(/\:/,$cache->{$sequence.':'. - $problemID. - ':parts'})) { - $Str .= ' '; - $totalProblems++; - $characterCount++; - } - next; - } +=item &html_outputstudent($r,$student) - my %partData=undef; - # Initialize part data, display skips correctly - # Skip refers to when a student made no submissions on that - # part/problem. - foreach my $part (split(/\:/,$cache->{$sequence.':'. - $problemID. - ':parts'})) { - $partData{$part.':tries'}=0; - $partData{$part.':code'}=' '; - } - - # Looping through all the versions of each part, starting with the - # oldest version. Basically, it gets the most recent - # set of grade data for each part. - for(my $Version=1; $Version<=$LatestVersion; $Version++) { - foreach my $part (split(/\:/,$cache->{$sequence.':'. - $problemID. - ':parts'})) { - - if(!defined($cache->{$name.":$Version:$problem". - ":resource.$part.solved"})) { - # No grade for this submission, so skip - next; - } +Return a line of the chart for a student. - my $tries=0; - my $code=' '; +=item &html_finish($r) - $tries = $cache->{$name.':'.$Version.':'.$problem. - ':resource.'.$part.'.tries'}; - $partData{$part.':tries'}=($tries) ? $tries : 0; - - my $val = $cache->{$name.':'.$Version.':'.$problem. - ':resource.'.$part.'.solved'}; - if ($val eq 'correct_by_student') {$code = '*';} - elsif ($val eq 'correct_by_override') {$code = '+';} - elsif ($val eq 'incorrect_attempted') {$code = '.';} - elsif ($val eq 'incorrect_by_override'){$code = '-';} - elsif ($val eq 'excused') {$code = 'x';} - elsif ($val eq 'ungraded_attempted') {$code = '#';} - else {$code = ' ';} - $partData{$part.':code'}=$code; - } - } +=cut - # All grades (except for versionless parts) are displayed as links - # to their submission record. Loop through all the parts for the - # current problem in the correct order and prepare the output links - $Str .= ''; - foreach(split(/\:/,$cache->{$sequence.':'.$problemID. - ':parts'})) { - if($partData{$_.':code'} eq '*') { - $problemsCorrect++; - if (($partData{$_.':tries'}<10) && - ($partData{$_.':tries'} ne '')) { - $partData{$_.':code'}=$partData{$_.':tries'}; - } - } elsif($partData{$_.':code'} eq '+') { - $problemsCorrect++; - } +####################################################### +####################################################### +{ + my $padding; + my $count; + + my $nodata_count; # The number of students for which there is no data + my %prog_state; # progress state used by loncommon PrgWin routines + my $total_sum_width; + + my %width; # Holds sequence width information + my @sequences; + my $navmap; # Have to keep this around since weakref is a bit zealous + +sub html_cleanup { + undef(%prog_state); + undef(%width); + # + undef($navmap); + undef(@sequences); +} - $Str .= $partData{$_.':code'}; - $characterCount++; +sub html_initialize { + my ($r) = @_; + # + $padding = ' 'x3; + $count = 0; + $nodata_count = 0; + &html_cleanup(); + ($navmap,@sequences) = + &Apache::lonstatistics::selected_sequences_with_assessments(); + if (! ref($navmap)) { + # Unable to get data, so bail out + $r->print(" ". + &mt('Unable to retrieve course information.'). + ''); + } - if($partData{$_.':code'} ne 'x') { - $totalProblems++; - } + # If we're showing links, show a checkbox to open in new + # windows. + if ($show_links ne 'no') { + $r->print(<".$env{'course.'.$env{'request.course.id'}.'.description'}. + " ".localtime(time).""); + # + if ($chosen_output->{'base'} !~ /^final table/) { + $r->print("".$chosen_output->{'shortdesc'}.""); + } + my $Str = "\n"; + # First, the @StudentData fields need to be listed + my @to_show = &get_student_fields_to_show(); + foreach my $field (@to_show) { + my $title=$Apache::lonstatistics::StudentData{$field}->{'title'}; + my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'}; + my $width=$Apache::lonstatistics::StudentData{$field}->{'width'}; + $Str .= $title.' 'x($width-$base).$padding; + } + # + # Compute the column widths and output the sequence titles + my $total_count; + # + # Compute sequence widths + my $starttime = Time::HiRes::time; + foreach my $seq (@sequences) { + my $symb = $seq->symb; + my $title = $seq->compTitle; + $width{$symb}->{'width_sum'} = 0; + # Compute width of sum + if ($chosen_output->{'sequence_sum'}) { + if ($chosen_output->{'every_problem'}) { + # Use 1 digit for a space + $width{$symb}->{'width_sum'} += 1; + } + $total_count += &count_parts($navmap,$seq); + # Use 3 digits for the sum + $width{$symb}->{'width_sum'} += 3; + } + # Compute width of maximum + if ($chosen_output->{'sequence_max'}) { + if ($width{$symb}->{'width_sum'}>0) { + # One digit for the '/' + $width{$symb}->{'width_sum'} +=1; } - $Str.=' "; + $r->print($Str); + $r->rflush(); + + $r->print(< "); + } + # First, the @StudentData fields need to be listed + my @to_show = &get_student_fields_to_show(); + foreach my $field (@to_show) { + my $title=$student->{$field}; + # Deal with 'comments' - how I love special cases + if ($field eq 'comments') { + $title = ''.&mt('Comments').''; + } + my $base = length($title); + my $width=$Apache::lonstatistics::StudentData{$field}->{'width'}; + $Str .= $title.' 'x($width-$base).$padding; + } + # Get ALL the students data + my %StudentsData; + my @tmp = &Apache::loncoursedata::get_current_state + ($student->{'username'},$student->{'domain'},undef, + $env{'request.course.id'}); + if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:(.*)/)) { + %StudentsData = @tmp; + } else { + my $error = $1; + if (scalar(@tmp) < 1) { + $Str .= 'No Course Data'."\n"; + } else { + $Str .= 'Error getting student data ('. + $error.') '."\n"; + } + $nodata_count++; + $r->print($Str); + $r->rflush(); + return; + } + # + # By sequence build up the data + my $studentstats; + my $PerformanceStr = ''; + foreach my $seq (@sequences) { + my $symb = $seq->symb; + my ($performance,$performance_length,$score,$seq_max,$rawdata); + if ($chosen_output->{'tries'}) { + ($performance,$performance_length,$score,$seq_max,$rawdata) = + &student_tries_on_sequence($student,\%StudentsData, + $navmap,$seq,$show_links); + } else { + ($performance,$performance_length,$score,$seq_max,$rawdata) = + &student_performance_on_sequence($student,\%StudentsData, + $navmap,$seq,$show_links, + $chosen_output->{ignore_weight}); + } + my $ratio=''; + if ($chosen_output->{'every_problem'} && + $chosen_output->{'sequence_sum'}) { + $ratio .= ' '; + } + if ($chosen_output->{'sequence_sum'} && $score ne ' ') { + my $score .= sprintf("%3.0f",$score); + $ratio .= (' 'x(3-length($score))).$score; + } elsif($chosen_output->{'sequence_sum'}) { + $ratio .= ' 'x3; + } + if ($chosen_output->{'sequence_max'}) { + if ($chosen_output->{'sequence_sum'}) { + $ratio .= '/'; + } + $ratio .= sprintf("%3.0f",$seq_max); + } + # + if (! $chosen_output->{'every_problem'}) { + $performance = ''; + $performance_length=0; + } + $performance .= ' 'x($width{$symb}->{'width_total'} - + $performance_length - + $width{$symb}->{'width_sum'}). + $ratio; + # + $Str .= $performance.$padding; + # + $studentstats->{$symb}->{'score'}= $score; + $studentstats->{$symb}->{'max'} = $seq_max; + } + # + # Total it up and store the statistics info. + my ($score,$max); + while (my ($symb,$seq_stats) = each (%{$studentstats})) { + $Statistics->{$symb}->{'score'} += $seq_stats->{'score'}; + if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) { + $Statistics->{$symb}->{'max'} = $seq_stats->{'max'}; + } + if ($seq_stats->{'score'} ne ' ') { + $score += $seq_stats->{'score'}; + $Statistics->{$symb}->{'num_students'}++; + } + $max += $seq_stats->{'max'}; + } + if (! defined($score)) { + $score = ' ' x $total_sum_width; + } else { + $score = sprintf("%.0f",$score); + $score = (' 'x(3-length($score))).$score; + } + $Str .= ' '.' 'x($total_sum_width-length($score)).$score.' / '.$max; + $Str .= " \n"; + # + $r->print($Str); + # + $r->rflush(); + &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student'); + return; +} -=cut +sub html_finish { + my ($r) = @_; + return if (! defined($navmap)); + # + # Check for suppressed output and close the progress window if so + $r->print("\n"); + if ($chosen_output->{'summary_table'}) { + if ($single_student_mode) { + $r->print(&SingleStudentTotal()); + } else { + $r->print(&StudentAverageTotal()); + } + } + $r->rflush(); + &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state); + &html_cleanup(); + return; +} -sub CreateLegend { - my $Str = " ". - "1..9: correct by student in 1..9 tries\n". - " *: correct by student in more than 9 tries\n". - " +: correct by override\n". - " -: incorrect by override\n". - " .: incorrect attempted\n". - " #: ungraded attempted\n". - " : not attempted\n". - " x: excused". - " "; +sub StudentAverageTotal { + my $Str = ' '.&mt('Summary Tables').''.$/; + $Str .= '
'.&mt('Summary table for [_1] ([_2]@[_3])', + $student->{'fullname'}, + $student->{'username'},$student->{'domain'}).''; + $Str .= $/; + $Str .= '
'.&mt('Compiling Excel spreadsheet for [_1] students', + $studentcount)."\n"); + } else { + $r->print(''. + &mt('Compiling Excel spreadsheet for 1 student'). + "\n"); + } + $r->rflush(); + # + # Initialize progress window + %prog_state=&Apache::lonhtmlcommon::Create_PrgWin + ($r,'Excel File Compilation Status', + 'Excel File Compilation Progress', $studentcount, + 'inline',undef,'Statistics','stats_status'); + # + &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state, + 'Processing first student'); + return; } -=pod +sub excel_outputstudent { + my ($r,$student) = @_; + if ($request_aborted || ! defined($navmap) || ! defined($excel_sheet)) { + return; + } + $cols_output=0; + # + # Write out student data + my @to_show = &get_student_fields_to_show(); + foreach my $field (@to_show) { + my $value = $student->{$field}; + if ($field eq 'comments') { + $value = &Apache::lonmsgdisplay::retrieve_instructor_comments + ($student->{'username'},$student->{'domain'}); + } + $excel_sheet->write($rows_output,$cols_output++,$value); + } + # + # Get student assessment data + my %StudentsData; + my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'}, + $student->{'domain'}, + undef, + $env{'request.course.id'}); + if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) { + %StudentsData = @tmp; + } + # + # Write out sequence scores and totals data + my %total_cell_translation; + my %maximum_cell_translation; + foreach my $seq (@sequences) { + my $symb = $seq->symb; + $cols_output = $formula_data{$symb}->{'Excel:startcol'}; + # Keep track of cells to translate in total cell + $total_cell_translation{$formula_data{$symb}->{'Excel:scorecell'}} = + &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell + ($rows_output,$formula_data{$symb}->{'Excel:scorecol'}); + # and maximum cell + $maximum_cell_translation{$formula_data{$symb}->{'Excel:maxcell'}} = + &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell + ($rows_output,$formula_data{$symb}->{'Excel:maxcol'}); + # + my ($performance,$performance_length,$score,$seq_max,$rawdata); + if ($chosen_output->{'tries'} || $chosen_output->{'correct'}){ + ($performance,$performance_length,$score,$seq_max,$rawdata) = + &student_tries_on_sequence($student,\%StudentsData, + $navmap,$seq,'no'); + } else { + ($performance,$performance_length,$score,$seq_max,$rawdata) = + &student_performance_on_sequence($student,\%StudentsData, + $navmap,$seq,'no', + $chosen_output->{ignore_weight}); + } + if ($chosen_output->{'every_problem'}) { + if ($chosen_output->{'correct'}) { + # only indiciate if each item is correct or not + foreach my $value (@$rawdata) { + # positive means correct, 0 or negative means + # incorrect + $value = $value > 0 ? 1 : 0; + $excel_sheet->write($rows_output,$cols_output++,$value); + } + } else { + foreach my $value (@$rawdata) { + if ($score eq ' ' || !defined($value)) { + $cols_output++; + } else { + $excel_sheet->write($rows_output,$cols_output++, + $value); + } + } + } + } + if ($chosen_output->{'sequence_sum'} && + $chosen_output->{'every_problem'}) { + # Write a formula for the sum of this sequence + my %replaceCells= + ('^'.$formula_data{$symb}->{'Excel:startcell'}.':'.$formula_data{$symb}->{'Excel:endcell'}.'$' + => + &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell($rows_output,$formula_data{$symb}->{'Excel:startcol'}).':'. + &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell($rows_output,$formula_data{$symb}->{'Excel:endcol'}) + ); + # The undef is for the format + $excel_sheet->repeat_formula($rows_output,$cols_output++, + $formula_data{$symb}->{'Excel:sum'},undef, + %replaceCells, %replaceCells); + } elsif ($chosen_output->{'sequence_sum'}) { + if ($score eq ' ') { + $cols_output++; + } else { + $excel_sheet->write($rows_output,$cols_output++,$score); + } + } + if ($chosen_output->{'sequence_max'}) { + $excel_sheet->write($rows_output,$cols_output++,$seq_max); + } + } + # + if ($chosen_output->{'grand_total'}) { + $excel_sheet->repeat_formula($rows_output,$cols_output++, + $total_formula,undef, + %total_cell_translation); + } + if ($chosen_output->{'grand_maximum'}) { + $excel_sheet->repeat_formula($rows_output,$cols_output++, + $maximum_formula,undef, + %maximum_cell_translation); + } + # + # Bookkeeping + $rows_output++; + $cols_output=0; + # + # Update the progress window + &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student'); + return; +} -=item &CreateColumnSelectors() +sub excel_finish { + my ($r) = @_; + if ($request_aborted || ! defined($navmap) || ! defined($excel_sheet)) { + &excel_cleanup(); + return; + } + # + # Write the excel file + $excel_workbook->close(); + # + # Close the progress window + &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state); + # + # Tell the user where to get their excel file + $r->print(''. + 'Your Excel spreadsheet.'."\n"); + $r->rflush(); + &excel_cleanup(); + return; +} -This function generates the checkboxes above the column headings. The -column will be removed if the checkbox is unchecked. +} +####################################################### +####################################################### -=over 4 +=pod -Input: $CacheData, $headings +=head2 CSV output routines -$CacheData: A pointer to a hash tied to the cached data +=item &csv_initialize($r) -$headings: An array of the names of the columns for the student -information. They are used to know what are the student information columns +=item &csv_outputstudent($r,$student) -Output: $present +=item &csv_finish($r) -$present: The string contains the first row of a table. Each column contains -a checkbox which is left justified. Currently left justification is used -for consistency of location over the column in which it presides. +=cut -=back +####################################################### +####################################################### +{ + +my $outputfile; +my $filename; +my $request_aborted; +my %prog_state; # progress window state +my $navmap; +my @sequences; + +sub csv_cleanup { + undef($outputfile); + undef($filename); + undef($request_aborted); + undef(%prog_state); + # + undef($navmap); + undef(@sequences); +} -=cut +sub csv_initialize{ + my ($r) = @_; -sub CreateColumnSelectors { - my ($infoHeadings, $sequenceHeadings, $sequenceKeys)=@_; + &csv_cleanup(); + ($navmap,@sequences) = + &Apache::lonstatistics::selected_sequences_with_assessments(); + if (! ref($navmap)) { + # Unable to get data, so bail out + $r->print(" ". + &mt('Unable to retrieve course information.'). + ''); + } + # + # Deal with unimplemented requests + $request_aborted = undef; + if ($chosen_output->{'base'} =~ /final table/) { + $r->print(<+The Summary Table (Scores) option is not available for non-HTML output. + +END + $request_aborted = 1; + } + return if ($request_aborted); + # + # Initialize progress window + my $studentcount = scalar(@Apache::lonstatistics::Students); + %prog_state=&Apache::lonhtmlcommon::Create_PrgWin + ($r,'CSV File Compilation Status', + 'CSV File Compilation Progress', $studentcount, + 'inline',undef,'Statistics','stats_status'); + # + # Open a file + ($outputfile,$filename) = &Apache::loncommon::create_text_file($r,'csv'); + if (! defined($outputfile)) { return ''; } + # + # Datestamp + my $description = $env{'course.'.$env{'request.course.id'}.'.description'}; + print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'. + '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'. + "\n"; + print $outputfile '"'. + &Apache::loncommon::csv_translate + (&Apache::lonstatistics::section_and_enrollment_description()). + '"'."\n"; + foreach my $item ('shortdesc','non_html_notes') { + next if (! exists($chosen_output->{$item})); + print $outputfile + '"'.&Apache::loncommon::csv_translate($chosen_output->{$item}).'"'. + "\n"; + } + # + # Print out the headings + my $sequence_row = ''; + my $resource_row = undef; + foreach my $field (&get_student_fields_to_show()) { + $sequence_row .='"",'; + $resource_row .= '"'.&Apache::loncommon::csv_translate($field).'",'; + } + foreach my $seq (@sequences) { + $sequence_row .= '"'. + &Apache::loncommon::csv_translate($seq->compTitle).'",'; + my $count = 0; + if ($chosen_output->{'every_problem'}) { + foreach my $res (&get_resources($navmap,$seq)) { + if (scalar(@{$res->parts}) < 1) { + next; + } + foreach my $part (@{$res->parts}) { + $resource_row .= '"'. + &Apache::loncommon::csv_translate + ($res->compTitle.', Part '.$res->part_display($part)).'",'; + $count++; + } + } + } + $sequence_row.='"",'x$count; + if ($chosen_output->{'sequence_sum'}) { + if($chosen_output->{'correct'}) { + $resource_row .= '"sum",'; + } else { + $resource_row .= '"score",'; + } + } + if ($chosen_output->{'sequence_max'}) { + $sequence_row.= '"",'; + $resource_row .= '"maximum possible",'; + } + } + if ($chosen_output->{'grand_total'}) { + $sequence_row.= '"",'; + $resource_row.= '"Total",'; + } + if ($chosen_output->{'grand_maximum'}) { + $sequence_row.= '"",'; + $resource_row.= '"Maximum",'; + } + chomp($sequence_row); + chomp($resource_row); + print $outputfile $sequence_row."\n"; + print $outputfile $resource_row."\n"; + return; +} - my $present = ''; - for(my $index=0; $index<(scalar @$infoHeadings); $index++) { - $present .= '';
- $present .= '[$index].'" />';
- $present .= ' | '."\n";
+sub csv_outputstudent {
+ my ($r,$student) = @_;
+ if ($request_aborted || ! defined($navmap) || ! defined($outputfile)) {
+ return;
+ }
+ my $Str = '';
+ #
+ # Output student fields
+ my @to_show = &get_student_fields_to_show();
+ foreach my $field (@to_show) {
+ my $value = $student->{$field};
+ if ($field eq 'comments') {
+ $value = &Apache::lonmsgdisplay::retrieve_instructor_comments
+ ($student->{'username'},$student->{'domain'});
+ }
+ $Str .= '"'.&Apache::loncommon::csv_translate($value).'",';
+ }
+ #
+ # Get student assessment data
+ my %StudentsData;
+ my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
+ $student->{'domain'},
+ undef,
+ $env{'request.course.id'});
+ if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
+ %StudentsData = @tmp;
+ }
+ #
+ # Output performance data
+ my $total = 0;
+ my $maximum = 0;
+ foreach my $seq (@sequences) {
+ my ($performance,$performance_length,$score,$seq_max,$rawdata);
+ if ($chosen_output->{'tries'}){
+ ($performance,$performance_length,$score,$seq_max,$rawdata) =
+ &student_tries_on_sequence($student,\%StudentsData,
+ $navmap,$seq,'no');
+ } else {
+ ($performance,$performance_length,$score,$seq_max,$rawdata) =
+ &student_performance_on_sequence($student,\%StudentsData,
+ $navmap,$seq,'no',
+ $chosen_output->{ignore_weight});
+ }
+ if ($chosen_output->{'every_problem'}) {
+ if ($chosen_output->{'correct'}) {
+ $score = 0;
+ # Deal with number of parts correct data
+ $Str .= '"'.join('","',( map { if ($_>0) {
+ $score += 1;
+ 1;
+ } else {
+ 0;
+ }
+ } @$rawdata)).'",';
+ } else {
+ $Str .= '"'.join('","',(@$rawdata)).'",';
+ }
+ }
+ if ($chosen_output->{'sequence_sum'}) {
+ $Str .= '"'.$score.'",';
+ }
+ if ($chosen_output->{'sequence_max'}) {
+ $Str .= '"'.$seq_max.'",';
+ }
+ $total+=$score;
+ $maximum += $seq_max;
+ }
+ if ($chosen_output->{'grand_total'}) {
+ $Str .= '"'.$total.'",';
+ }
+ if ($chosen_output->{'grand_maximum'}) {
+ $Str .= '"'.$maximum.'",';
}
+ chop($Str);
+ $Str .= "\n";
+ print $outputfile $Str;
+ #
+ # Update the progress window
+ &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
+ return;
+}
- for(my $index=0; $index<(scalar @$sequenceHeadings); $index++) {
- $present .= '';
- $present .= '[$index].'" />';
- $present .= ' | '."\n";
+sub csv_finish {
+ my ($r) = @_;
+ if ($request_aborted || ! defined($navmap) || ! defined($outputfile)) {
+ &csv_cleanup();
+ return;
}
+ close($outputfile);
+ #
+ # Close the progress window
+ &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
+ #
+ # Tell the user where to get their csv file
+ $r->print(''. + ''.&mt('Your csv file.').''."\n"); + $r->rflush(); + &csv_cleanup(); + return; + +} + +} - return $present; +# This function will return an HTML string including a star, with +# a mouseover popup showing the "real" value. An optional second +# argument lets you show something other than a star. +sub show_star { + my $popup = shift; + my $symbol = shift || '*'; + # Escape the popup for JS. + $popup =~ s/([^-a-zA-Z0-9:;,._ ()|!\/?=&*])/'\\' . sprintf("%lo", ord($1))/ge; + + return "$symbol"; } -#---- END Student Assessment Web Page ---------------------------------------- +####################################################### +####################################################### -#---- Student Assessment Worker Functions ------------------------------------ +=pod -sub FindSelectedStudent { - my($cache, $selectedName, $students)=@_; +=item &StudentTriesOnSequence() - if($selectedName eq 'All Students' || - $selectedName eq 'No Student Selected') { - return $selectedName; - } +Inputs: + +=over 4 + +=item $student - for(my $index=0; $index<(scalar @$students); $index++) { - my $fullname = $cache->{$students->[$index].':fullname'}; - if($fullname eq $selectedName) { - if($cache->{'StudentAssessmentMove'} eq 'next') { - if($index == ((scalar @$students) - 1)) { - $selectedName = $students->[0]; - return $selectedName; +=item $studentdata Hash ref to all student data + +=item $seq Hash ref, the sequence we are working on + +=item $links if defined we will output links to each resource. + +=back + +=cut + +####################################################### +####################################################### +sub student_tries_on_sequence { + my ($student,$studentdata,$navmap,$seq,$links) = @_; + $links = 'no' if (! defined($links)); + my $Str = ''; + my ($sum,$max) = (0,0); + my $performance_length = 0; + my @TriesData = (); + my $tries; + my $hasdata = 0; # flag - true if the student has any data on the sequence + foreach my $resource (&get_resources($navmap,$seq)) { + my $resource_data = $studentdata->{$resource->symb}; + my $value = ''; + foreach my $partnum (@{$resource->parts()}) { + $tries = undef; + $max++; + $performance_length++; + my $symbol = ' '; # default to space + # + my $awarded = 0; + if (exists($resource_data->{'resource.'.$partnum.'.awarded'})) { + $awarded = $resource_data->{'resource.'.$partnum.'.awarded'}; + $awarded = 0 if (! $awarded); + } + # + my $status = ''; + if (exists($resource_data->{'resource.'.$partnum.'.solved'})) { + $status = $resource_data->{'resource.'.$partnum.'.solved'}; + } + # + my $tries = 0; + if(exists($resource_data->{'resource.'.$partnum.'.tries'})) { + $tries = $resource_data->{'resource.'.$partnum.'.tries'}; + $hasdata =1; + } + # + if ($awarded > 0) { + # The student has gotten the problem correct to some degree + if ($status eq 'excused') { + $symbol = 'x'; + $max--; + } elsif ($status eq 'correct_by_override' && !$resource->is_task()) { + $symbol = '+'; + $sum++; + } elsif ($tries > 0) { + if ($tries > 9) { + $symbol = show_star($tries); + } else { + $symbol = $tries; + } + $sum++; } else { - $selectedName = $students->[$index+1]; - return $selectedName; + $symbol = '+'; + $sum++; } - } elsif($cache->{'StudentAssessmentMove'} eq 'previous') { - if($index == 0) { - $selectedName = $students->[-1]; - return $selectedName; + } else { + # The student has the problem incorrect or it is ungraded + if ($status eq 'excused') { + $symbol = 'x'; + $max--; + } elsif ($status eq 'incorrect_by_override') { + $symbol = '-'; + } elsif ($status eq 'ungraded_attempted') { + $symbol = 'u'; + } elsif ($status eq 'incorrect_attempted' || + $tries > 0) { + $symbol = '.'; } else { - $selectedName = $students->[$index-1]; - return $selectedName; + # Problem is wrong and has not been attempted. + $symbol=' '; } + } + # + if (! defined($tries)) { + $tries = 0; + } + if ($status =~ /^(incorrect|ungraded)/) { + # Bug 3390: show '-' for tries on incorrect problems + # (csv & excel only) + push(@TriesData,-$tries); } else { - $selectedName = $students->[$index]; - return $selectedName; + push (@TriesData,$tries); } - last; + # + if ( ($links eq 'yes' && $symbol ne ' ') || + ($links eq 'all')) { + my $link = '/adm/grades'. + '?symb='.&escape($resource->symb). + '&student='.$student->{'username'}. + '&userdom='.$student->{'domain'}. + '&command=submission'; + $symbol = &link($symbol, $link); + } + $value .= $symbol; } + $Str .= $value; } - - return 'No Student Selected'; + if ($seq->randompick()) { + $max = $seq->randompick(); + } + if (! $hasdata && $sum == 0) { + $sum = ' '; + } + return ($Str,$performance_length,$sum,$max,\@TriesData); } =pod -=item &ShouldShowColumn() +=item &link -Determine if a specified column should be shown on the chart. +Inputs: =over 4 -Input: $cache, $test +=item $text -$cache: A pointer to the hash tied to the cached data +=item $target -$test: The form name of the column (heading.$headingIndex) or -(sequence.$sequenceIndex) +=back -Output: 0 (false), 1 (true) +Takes the text and creates a link to the $text that honors +the value of 'new window' if clicked on, but uses a real +'href' so middle and right clicks still work. -=back +$target and $text are assumed to be already correctly escaped; i.e., it +can be dumped out directly into the output stream as-is. =cut -sub ShouldShowColumns { - my ($cache,$headings,$cacheKey)=@_; +sub link { + my ($text,$target) = @_; + return + "$text"; +} + +####################################################### +####################################################### - my @infoKeys=(); - my @infoHeadings=(); +=pod - my @sequenceKeys=(); - my @sequenceHeadings=(); +=item &student_performance_on_sequence - my %doNotShow; +Inputs: - my $index; - my $count = 0; - my $check = ''; - for($index=0; $index < scalar @$headings; $index++) { - $check = 'HeadingColumn'.$headings->[$index]; - if($cache->{'HeadingsFound'} =~ /$check/) { - push(@infoHeadings, $headings->[$index]); - push(@infoKeys, $cacheKey->[$index]); - } else { - $doNotShow{$count.':name'} = $headings->[$index]; - $doNotShow{$count.':id'} = 'HeadingColumn'.$headings->[$index]; - $count++; - } - } +=over 4 - foreach my $sequence (split(/\:/,$cache->{'orderedSequences'})) { - $check = 'SequenceColumn'.$sequence; - if($cache->{'SequencesFound'} eq 'All Sequences' || - $cache->{'SequencesFound'} =~ /$check/) { - push(@sequenceHeadings, $cache->{$sequence.':title'}); - push(@sequenceKeys, $sequence); - } else { - $doNotShow{$count.':name'} = $cache->{$sequence.':title'}; - $doNotShow{$count.':id'} = 'SequenceColumn'.$sequence; - $count++; +=item $student + +=item $studentdata Hash ref to all student data + +=item $seq Hash ref, the sequence we are working on + +=item $links if defined we will output links to each resource. + +=back + +=cut + +####################################################### +####################################################### +sub student_performance_on_sequence { + my ($student,$studentdata,$navmap,$seq,$links,$awarded_only) = @_; + $links = 'no' if (! defined($links)); + my $Str = ''; # final result string + my ($score,$max) = (0,0); + my $performance_length = 0; + my $symbol; + my @ScoreData = (); + my $partscore; + my $hasdata = 0; # flag, 0 if there were no submissions on the sequence + foreach my $resource (&get_resources($navmap,$seq)) { + my $symb = $resource->symb; + my $resource_data = $studentdata->{$symb}; + foreach my $part (@{$resource->parts()}) { + $partscore = undef; + my $weight; + if (!$awarded_only){ + $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight', + $symb, + $student->{'domain'}, + $student->{'username'}, + $student->{'section'}); + } + if (!defined($weight) || ($weight eq '')) { + $weight=1; + } + # + $max += $weight; # see the 'excused' branch below... + $performance_length++; # one character per part + $symbol = ' '; # default to space + # + my $awarded; + if (exists($resource_data->{'resource.'.$part.'.awarded'})) { + $awarded = $resource_data->{'resource.'.$part.'.awarded'}; + $awarded = 0 if (! $awarded); + $hasdata = 1; + } + # + $partscore = &Apache::grades::compute_points($weight,$awarded); + if (! defined($awarded)) { + $partscore = undef; + } + $score += $partscore; + $symbol = $partscore; + if (abs($symbol - sprintf("%.0f",$symbol)) < 0.001) { + $symbol = sprintf("%.0f",$symbol); + } + if (length($symbol) > 1) { + $symbol = show_star($symbol); + } + if (exists($resource_data->{'resource.'.$part.'.solved'}) && + $resource_data->{'resource.'.$part.'.solved'} ne '') { + my $status = $resource_data->{'resource.'.$part.'.solved'}; + if ($status eq 'excused') { + $symbol = 'x'; + $max -= $weight; # Do not count 'excused' problems. + } elsif ($status eq 'ungraded_attempted') { + $symbol = 'u'; + } + $hasdata = 1; + } elsif ($resource_data->{'resource.'.$part.'.award'} eq 'DRAFT') { + $symbol = 'd'; + $hasdata = 1; + } elsif (!exists($resource_data->{'resource.'.$part.'.awarded'})){ + # Unsolved. Did they try? + if (exists($resource_data->{'resource.'.$part.'.tries'})){ + $symbol = '.'; + $hasdata = 1; + } else { + $symbol = ' '; + } + } + # + if (! defined($partscore)) { + $partscore = $symbol; + } + push (@ScoreData,$partscore); + # + if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) { + my $link = '/adm/grades' . + '?symb='.&escape($symb). + '&student='.$student->{'username'}. + '&userdom='.$student->{'domain'}. + '&command=submission'; + $symbol = &link($symbol, $link); + } + $Str .= $symbol; } } + if (! $hasdata && $score == 0) { + $score = ' '; + } + return ($Str,$performance_length,$score,$max,\@ScoreData); +} + +####################################################### +####################################################### + +=pod + +=item &CreateLegend() + +This function returns a formatted string containing the legend for the +chart. The legend describes the symbols used to represent grades for +problems. - $doNotShow{'count'} = $count; +=cut - return (\@infoHeadings, \@infoKeys, \@sequenceHeadings, - \@sequenceKeys, \%doNotShow); +####################################################### +####################################################### +sub CreateLegend { + my $Str = " ". + " digit score or number of tries to get correct ". + " * correct by student in more than 9 tries\n". + " + correct by hand grading or override\n". + " - incorrect by override\n". + " . incorrect attempted\n". + " u ungraded attempted\n". + " d draft answer saved but not submitted\n". + " not attempted (blank field)\n". + " x excused". + " "; + return $Str; } -#---- END Student Assessment Worker Functions -------------------------------- +####################################################### +####################################################### + +=pod + +=back + +=cut + +####################################################### +####################################################### 1; + __END__ |