');
$r->rflush();
#
- if (exists($ENV{'form.problemchoice'}) &&
- ! exists($ENV{'form.SelectAnother'})) {
+ my %anoncounter =
+ &Apache::lonnet::dump('nohist_anonsurveys',
+ $env{'course.'.$env{'request.course.id'}.'.domain'},
+ $env{'course.'.$env{'request.course.id'}.'.num'});
+ if (exists($env{'form.problemchoice'}) &&
+ ! exists($env{'form.SelectAnother'})) {
foreach my $button (@SubmitButtons) {
if ($button->{'name'} eq 'break') {
$r->print(" \n");
@@ -91,117 +105,734 @@ sub BuildStudentSubmissionsPage {
# Determine which problems we are to analyze
my @Symbs =
&Apache::lonstathelpers::get_selected_symbs('problemchoice');
- foreach my $selected (@Symbs) {
- $r->print(''.$/);
+
+ # If there are multi-part problems with anonymous survey and named
+ # parts check if named was picked for display.
+ #
+ my %mixed_named;
+ foreach my $envkey (%env) {
+ if ($envkey =~ /^form\.mixed_(\d+:\d+)$/) {
+ my $item = $1;
+ if ($env{$envkey} =~ /^symb_(.+)$/) {
+ my $symb = &unescape($1);
+ if (ref($mixed_named{$symb}) eq 'ARRAY') {
+ push(@{$mixed_named{$symb}},$item);
+ } else {
+ @{$mixed_named{$symb}} = ($item);
+ }
+ }
+ }
}
#
# Get resource objects
my $navmap = Apache::lonnavmaps::navmap->new();
if (!defined($navmap)) {
- $r->print('
');
if (! scalar(@Problems) || ! defined($Problems[0])) {
- $r->print('resource is undefined');
+ $r->print(&mt('resource is undefined'));
+ } elsif (!$show_named && @Students < $threshold) {
+ $r->print(&mt('The number of students matching the selection criteria is too few for display of submission data for anonymous surveys.').' '.&mt('There must be at least [quant,_1,student].',$threshold).' '.&mt('Contact a Domain Coordinator if you need the threshold to be changed for this course.'));
} else {
if (scalar(@Problems) == 1) {
my $resource = $Problems[0];
$r->print('
';
+ }
+ #
+ # Figure out what it is we need to output for this student
+ my @essays;
+ my %prob_data;
+ my $maxrow;
+ foreach my $prob (@$problems) {
+ my $symb = $prob->symb;
+ $prob_data{$symb}={};
+ foreach my $partid (@{$prob->parts}) {
+ if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
+ next if ($show_named);
+ } else {
+ next unless ($show_named);
+ }
+ my @responses = $prob->responseIds($partid);
+ my @response_type = $prob->responseType($partid);
+ for (my $i=0;$i<=$#responses;$i++) {
+ my $respid = $responses[$i];
+ my $results =
+ &Apache::loncoursedata::get_response_data_by_student
+ ($student,$prob->symb(),$respid);
+ my $resptype = $response_type[$i];
+ my @headers = &get_headers($prob,$partid,$respid,
+ $resptype,
+ $problem_analysis{$prob->src},
+ 'html','normal',
+ @extra_resp_headers);
+ my $width = scalar(@headers);
+ next if ($width < 1);
+ my $resp_data;
+ $resp_data->{'fake'} = qq{
};
+ if (! defined($results)) {
+ $results = [];
+ }
+ #
+ if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
+ $maxrow = scalar(@$results);
+ }
+ for (my $j=scalar(@$results)-1;$j>=0;$j--) {
+ if ($env{'form.all_sub'} ne 'true') {
+ next if ($j ne scalar(@$results)-1);
+ }
+ my $response = &hashify_response($results->[$j],
+ $prob,
+ $student,
+ $partid,
+ $respid);
+ if ($resptype eq 'essay') {
+ push(@essays,
+ &html_essay_results(\@headers,
+ $prob,$partid,$respid,
+ $response,
+ $single_response).
+ '');
+ } elsif (lc($resptype) eq 'task') {
+ my $results =
+ &html_task_results(\@headers,
+ $prob,$partid,$respid,
+ $response,$resptype);
+ if ($results) {
+ push(@{$resp_data->{'real'}},$results);
+ }
+ } else {
+ push(@{$resp_data->{'real'}},
+ &html_non_essay_results(\@headers,
+ $prob,$partid,$respid,
+ $response,$resptype));
+ }
+ }
+ $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
+ } # end of $i loop
+ } # end of partid loop
+ } # end of prob loop
+ #
+ # if there is no data, skip this student.
+ next if (! $maxrow && ! scalar(@essays));
+ #
+ # Go through the problem data and output a row.
+ my $printed_something;
+ for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
+ my $html;
+ my $no_data = 1;
+ foreach my $prob (@$problems) {
+ foreach my $partid (@{$prob->parts}) {
+ my @responses = $prob->responseIds($partid);
+ my @response_type = $prob->responseType($partid);
+ for (my $i=0;$i<=$#responses;$i++) {
+ my $respid = $responses[$i];
+ my $resp_data =
+ $prob_data{$prob->symb}->{$partid}->{$respid};
+ next if ($response_type[$i] eq 'essay');
+ if (defined($resp_data->{'real'}->[$rows_output])) {
+ $html .= $resp_data->{'real'}->[$rows_output];
+ $no_data = 0;
+ } else {
+ $html .= $resp_data->{'fake'};
+ }
+ }
+ }
+ }
+ if (! $no_data) {
+ $r->print(&Apache::loncommon::start_data_table_row().$student_row_data.$html.
+ &Apache::loncommon::end_data_table_row().$/);
+ $printed_something=1;
+ }
+ }
+ if (@essays) {
+ my $tr = &Apache::loncommon::start_data_table_row();
+ my $td = qq{
';
+ return $submission;
+}
+
+sub html_task_results {
+ my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
+ if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
+ return '';
+ }
+
+ my @values;
+ @values = map { $response->{$_}; } @$headers;
+
+ my $td = '
';
+ my $str = $td.join('
'.$td,@values).'';
+ return $str;
+}
+
+sub html_non_essay_results {
+ my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
+ if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
+ return '';
+ }
+ #
+ my $submission = &HTML::Entities::decode(&unescape($response->{'Submission'}));
+ return '' if (! defined($submission) || $submission eq '');
+ $submission =~ s/\\\"/\"/g;
+ $submission =~ s/\\\'/\'/g;
+ if ($resptype eq 'radiobutton') {
+ $submission = &HTML::Entities::encode($submission,'<>&"');
+ $submission =~ s/=([^=])$//;
+ $submission = ''.$submission.'';
+ }
+ $response->{'Submission'} = $submission;
+ #
+ my @values;
+ if ($resptype =~ /^(option|match|rank)$/) {
+ my %submission =
+ map {
+ my ($foil,$value) = split('=',&unescape($_));
+ ($foil,$value);
+ } split('&',$response->{'Submission'});
+ my %correct;
+ if (exists($response->{'Correct'})) {
+ %correct =
+ map {
+ my ($foil,$value)=split('=',&unescape($_));
+ ($foil,$value);
+ } split('&',$response->{'Correct'});
+ }
+ #
+ foreach my $original_header (@$headers) {
+ if ($original_header =~ /^_/) {
+ # '_' denotes a foil column
+ my ($header) = ($original_header =~ m/^_(.*)$/);
+ my $option = '';
+ if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
+ if (exists($correct{$foil})) {
+ $option = $correct{$foil};
+ }
+ } elsif (exists($submission{$header})) {
+ $option = $submission{$header};
+ }
+ push(@values,&HTML::Entities::encode($option));
+ } elsif ($original_header eq 'Time') {
+ push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
+ } else {
+ # A normal column
+ push(@values,$response->{$original_header});
+ }
+ }
+ } else {
+ foreach my $original_header (@$headers) {
+ if ($original_header eq 'Time') {
+ push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
+ } else {
+ # A normal column
+ push(@values,$response->{$original_header});
+ }
+ }
+ }
+ my $td = '
';
+ my $str = $td.join('
'.$td,@values).'';
+ return $str;
+}
+
+
+#########################################################
+#########################################################
+##
+## Excel Output Routines
+##
+#########################################################
+#########################################################
+sub prepare_excel_output {
+ my ($r,$Problems,$Students,$anoncounter,$show_named) = @_;
my $c = $r->connection();
#
+ #
+ # Determine the number of columns in the spreadsheet
+ my $columncount = 3; # username, domain, id
+ my @extra_resp_headers = &get_extra_response_headers($show_named);
+ my $lastprob;
+ my %problem_analysis;
+ foreach my $prob (@$Problems) {
+ my $symb = $prob->symb();
+ my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
+ $problem_analysis{$prob->src}=\%analysis;
+ foreach my $partid (@{$prob->parts}) {
+ if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
+ next if ($show_named);
+ } else {
+ next unless ($show_named);
+ }
+
+ my $responses = [$prob->responseIds($partid)];
+ my $resptypes = [$prob->responseType($partid)];
+ for (my $i=0;$i[$i],
+ $resptypes->[$i],
+ $problem_analysis{$prob->src},
+ 'excel','display',
+ @extra_resp_headers);
+ $columncount += scalar(@headers);
+ }
+ }
+ last if ($columncount > 255);
+ $lastprob = $prob;
+ }
+ if ($columncount > 255) {
+ $r->print('
'.&mt('Unable to complete request').'
'.$/.
+ '
'.&mt('LON-CAPA is unable to produce your Excel spreadsheet because your selections will result in more than 255 columns. Excel allows only 255 columns in a spreadsheet.').'
'.$/.
+ '
'.&mt('Consider selecting fewer problems to generate reports on, or reducing the number of items per problem. Or use HTML or CSV output.').'
'.$/);
+ if (ref($lastprob)) {
+ $r->print('
'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'
');
+ }
+ $r->rflush();
+ return;
+ }
+ #
+ # Print out a message telling them what we are doing
if (scalar(@$Problems) > 1) {
- $r->print('
'.
+ $r->print('
'.
&mt('Preparing Excel spreadsheet of student responses to [_1] problems',
scalar(@$Problems)).
- '');
+ '
'.
- &mt('Generating CSV report of student responses').'
');
+ $r->print('
'.
+ &mt('Generating CSV report of student responses').'
');
#
# Progress window
- my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
- ($r,'CSV File Compilation Status',
- 'CSV File Compilation Progress',
- scalar(@$Students),'inline',undef,'Statistics','stats_status');
-
+ my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@$students));
+
$r->rflush();
#
# Open a file
my $outputfile;
my $filename = '/prtspool/'.
- $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+ $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
time.'_'.rand(1000000000).'.csv';
unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
$r->log_error("Couldn't open $filename for output $!");
- $r->print("Problems occured in writing the csv file. ".
- "This error has been logged. ".
- "Please alert your LON-CAPA administrator.");
+ $r->print(
+ '
'
+ .&mt('Problems occurred in writing the CSV file.')
+ .' '.&mt('This error has been logged.')
+ .' '.&mt('Please alert your LON-CAPA administrator.')
+ .'
'
+ );
$outputfile = undef;
}
#
+ # Compute the number of columns per response
+ my @extra_resp_headers = &get_extra_response_headers($show_named);
#
- my @Columns;
- if (exists($ENV{'form.concise'}) && $ENV{'form.concise'} eq 'true') {
- foreach (@DefaultColumns) {
- if ($_->{'name'} =~ /^(username|domain|id)$/){
- push(@Columns,$_);
+ # Create the table header
+ my @student_columns = &get_student_columns($show_named);
+ my $student_column_count = scalar(@student_columns);
+ #
+ my %headers;
+ push(@{$headers{'student'}},@student_columns);
+ # Pad for the student data
+ foreach my $row ('problem','part','response') {
+ $headers{$row}=[map {''} @student_columns];
+ }
+ #
+ # we put the headers into the %headers hash
+ my %problem_analysis;
+ my %start_col;
+ my $max_column = $student_column_count;
+ foreach my $prob (@$problems) {
+ my $symb = $prob->symb();
+ my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
+ $problem_analysis{$prob->src}=\%analysis;
+ $headers{'problem'}->[$max_column] = $prob->compTitle;
+ foreach my $partid (@{$prob->parts}) {
+ if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
+ next if ($show_named);
+ } else {
+ next unless ($show_named);
+ }
+ $headers{'part'}->[$max_column] = $prob->part_display($partid);
+ my $responses = [$prob->responseIds($partid)];
+ my $resptypes = [$prob->responseType($partid)];
+ for (my $i=0;$i[$i],
+ $resptypes->[$i],
+ $problem_analysis{$prob->src},
+ 'csv','display',
+ @extra_resp_headers);
+ $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
+ $max_column;
+ $headers{'response'}->[$max_column]=
+ &mt('Response [_1]',$responses->[$i]);
+ for (my $j=0;$j<=$#headers;$j++) {
+ $headers{'student'}->[$max_column+$j]=$headers[$j];
+ }
+ $max_column += scalar(@headers);
}
}
- } else {
- @Columns = @DefaultColumns;
}
- #
- my $response_type = &get_response_type($resource,$partid,$respid);
- if (! defined($response_type) || $response_type eq '') {
- $r->print('
'.&mt('Unable to determine response type').'
');
- return;
+ foreach my $row ('problem','part','response','student') {
+ print $outputfile '"'.
+ join('","',
+ map {
+ &Apache::loncommon::csv_translate($_);
+ } @{$headers{$row}}).'"'.$/;
}
#
- my $header = &csv_headers(\@Columns).','.&csv_generic_headers();
- print $outputfile $header.$/;
- #
- foreach my $student (@$Students) {
+ # Main loop
+ foreach my $student (@$students) {
last if ($c->aborted());
- my $results = &Apache::loncoursedata::get_response_data_by_student
- ($student,$resource->{'symb'},$respid);
- next if (! defined($results) || ref($results) ne 'ARRAY');
- for (my $i=0;$i[$i];
- if ($ENV{'form.last_sub_only'} eq 'true' &&
- $i < (scalar(@$results)-1)) {
- next;
+ my @rows;
+ foreach my $prob (@$problems) {
+ my $symb = $prob->symb;
+ foreach my $partid (@{$prob->parts}) {
+ if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
+ next if ($show_named);
+ } else {
+ next unless ($show_named);
+ }
+ my @responses = $prob->responseIds($partid);
+ my @response_type = $prob->responseType($partid);
+ for (my $i=0;$i<=$#responses;$i++) {
+ my $respid = $responses[$i];
+ my $resptype = $response_type[$i];
+ my @headers = &get_headers($prob,$partid,$respid,$resptype,
+ $problem_analysis{$prob->src},
+ 'csv','normal',
+ @extra_resp_headers);
+ my $results =
+ &Apache::loncoursedata::get_response_data_by_student
+ ($student,$prob->symb(),$respid);
+ if (! defined($results)) {
+ $results = [];
+ }
+ for (my $j=0; $j[$idx],
+ $prob,$student,
+ $partid,$respid);
+ my @data = &compile_response_data(\@headers,$response,
+ $prob,$partid,
+ $respid,$resptype,
+ \&csv_format_item);
+ my $resp_start_idx =
+ $start_col{$prob->symb}->{$partid}->{$respid};
+ for (my $k=0;$k<=$#data;$k++) {
+ $rows[$j]->[$resp_start_idx + $k] = $data[$k];
+ }
+ }
+ }
}
- my $data;
- $data->{'username'} = $student->{'username'};
- $data->{'domain'} = $student->{'domain'};
- $data->{'id'} = $student->{'id'};
- $data->{'fullname'} = $student->{'fullanem'};
- $data->{'status'} = $student->{'status'};
- $data->{'time'} = &Apache::lonlocal::locallocaltime
- ($response->[&Apache::loncoursedata::RDs_timestamp()]);
- $data->{'attempt'} =
- $response->[&Apache::loncoursedata::RDs_tries()];
- $data->{'awarded'} =
- $response->[&Apache::loncoursedata::RDs_awarded()];
- $data->{'awarddetail'} =
- $response->[&Apache::loncoursedata::RDs_awarddetail()];
- $data->{'weight'} = &Apache::lonnet::EXT
- ('resource.'.$partid.'.weight',$resource->{'symb'},
- undef,undef,undef);
- $data->{'score'} = $data->{'weight'} * $data->{'awarded'};
- my $rowextra = '';
- my $row;
- foreach my $col (@Columns) {
- $row .= '"'.
- &Apache::loncommon::csv_translate($data->{$col->{'name'}}).'",';
- }
- if ($response_type eq 'option') {
- $row .= &csv_option_results
- ($response->[&Apache::loncoursedata::RDs_submission()],
- $student->{'answer'},
- scalar(@Columns),$rowextra);
- } elsif ($response_type eq 'radiobutton') {
- $row .= &csv_radiobutton_results
- ($response->[&Apache::loncoursedata::RDs_submission()],
- $student->{'answer'},
- scalar(@Columns),$rowextra);
+ }
+ foreach my $row (@rows) {
+ my $student_row_data = '';
+ if ($show_named) {
+ foreach my $field (@student_columns) {
+ my $value = $student->{$field};
+ if ($field eq 'comments') {
+ $value = &Apache::lonmsgdisplay::retrieve_instructor_comments
+ ($student->{'username'},$student->{'domain'});
+ }
+ $student_row_data .= '"'.&Apache::loncommon::csv_translate($value).'",';
+ }
+ $student_row_data =~ s/,$//;
} else {
- $row .= &csv_generic_results
- ($response->[&Apache::loncoursedata::RDs_submission()],
- $student->{'answer'},
- scalar(@Columns),$rowextra);
+ $student_row_data = '"'.&mt('Anonymized').'"';
}
- print $outputfile $row.$/;
+ print $outputfile $student_row_data;
+ for (my $i=$student_column_count;$i<$max_column;$i++) {
+ my $value = &Apache::loncommon::csv_translate($row->[$i]);
+ $value ||='';
+ print $outputfile ',"'.$value.'"';
+ }
+ print $outputfile $/;
}
- &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
- 'last student');
+ undef(@rows);
+ &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
}
close($outputfile);
#
# Close the progress window
&Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
#
- # Tell the user where to get their csv file
+ # Tell the user where to get their CSV file
$r->print(' '.
- ''.&mt('Your csv file.').''."\n");
+ ''.&mt('Your CSV file.').''."\n");
$r->rflush();
return;
}
-sub csv_headers {
- my ($Columns) = @_;
- my $Str;
- foreach my $column (@$Columns) {
- $Str .=
- '"'.&Apache::loncommon::csv_translate($column->{'display'}).'",';
- }
- chop($Str);
- return $Str;
-}
-
-sub csv_generic_headers {
- my ($title) = @_;
- if (! defined($title)) {
- $title = &mt('Submission');
- }
- my $header = '"'.&Apache::loncommon::csv_translate($title).'"';
- if ($ENV{'form.correctans'} eq 'true') {
- $header .= ',"'.&Apache::loncommon::csv_translate(&mt('Correct')).'"';
- }
- return $header;
-}
-
-#------------------------------------------
-sub csv_essay_results {
- my ($submission,$correct,$tablewidth,$rowextra)=@_;
- #
- $submission =~ s|\\r|\\\\r|g;
- $submission =~ s|\\n|\\\\n|g;
- #
- return &csv_generic_results($submission,$correct,$tablewidth);
-}
-
-#------------------------------------------
-sub csv_radiobutton_results {
- my ($submission,$correct,$tablewidth,$rowclass)=@_;
- $submission =~ s/=[^=]*$//;
- return &csv_generic_results($submission,$correct,$tablewidth,$rowclass);
-}
-
-#------------------------------------------
-sub csv_option_results {
- my ($submission,$correct,$tablewidth,$rowclass)=@_;
- $submission = join(',',
- map {
- &Apache::lonnet::unescape($_) ;
- } sort split('&',$submission)
- );
- if (defined($correct) && $correct !~ /^\s*$/) {
- $correct =join(',',
- map {
- &Apache::lonnet::unescape($_) ;
- } sort split('&',$submission));
- }
- return &csv_generic_results($submission,$correct,$tablewidth,$rowclass);
-}
-
-#------------------------------------------
-sub csv_generic_results {
- my ($submission,$correct,$tablewidth,$rowclass)=@_;
- my $Str .=
- '"'.&Apache::loncommon::csv_translate($submission).'"';
- if ($ENV{'form.correctans'} eq 'true') {
- $Str .= ',"'.&Apache::loncommon::csv_translate($correct).'"';
- }
- return $Str;
-}
-
-#########################################################
-#########################################################
-##
-## Excel output of student answers and correct answers
-##
-#########################################################
-#########################################################
-sub prepare_excel_output {
- my ($r,$Problems,$Students) = @_;
- my $c = $r->connection();
- #
- $r->print('
'.
- &mt('Preparing Excel spreadsheet of student responses').
- '
');
- if ($ENV{'form.correctans'} eq 'true') {
- $r->print('
'.
- &mt('See the status bar above for student answer computation progress').
- '