Annotation of loncom/interface/statistics/lonstudentsubmissions.pm, revision 1.61
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.61 ! raeburn 3: # $Id: lonstudentsubmissions.pm,v 1.60 2010/08/04 01:56:07 raeburn Exp $
1.1 matthew 4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: package Apache::lonstudentsubmissions;
28:
29: use strict;
1.40 albertel 30: use Apache::lonnet;
1.1 matthew 31: use Apache::loncommon();
32: use Apache::lonhtmlcommon();
33: use Apache::loncoursedata();
34: use Apache::lonstatistics;
35: use Apache::lonlocal;
36: use Apache::lonstathelpers;
37: use HTML::Entities();
38: use Time::Local();
39: use Spreadsheet::WriteExcel();
1.45 www 40: use lib '/home/httpd/lib/perl/';
41: use LONCAPA;
42:
1.1 matthew 43:
1.18 matthew 44: my @SubmitButtons = ({ name => 'SelectAnother',
1.1 matthew 45: text => 'Choose a different Problem' },
46: { name => 'Generate',
1.16 matthew 47: text => 'Generate Report'},
1.1 matthew 48: );
49:
50: sub BuildStudentSubmissionsPage {
51: my ($r,$c)=@_;
52: #
53: my %Saveable_Parameters = ('Status' => 'scalar',
54: 'Section' => 'array',
55: 'NumPlots' => 'scalar',
56: );
57: &Apache::loncommon::store_course_settings('student_submissions',
58: \%Saveable_Parameters);
59: &Apache::loncommon::restore_course_settings('student_submissions',
60: \%Saveable_Parameters);
61: #
62: &Apache::lonstatistics::PrepareClasslist();
63: #
64: $r->print(&CreateInterface());
65: #
66: my @Students = @Apache::lonstatistics::Students;
67: #
68: if (@Students < 1) {
1.50 bisitz 69: $r->print('<div class="LC_warning">'
70: .&mt('There are no students in the sections selected.')
71: .'</div>');
1.1 matthew 72: }
73: #
1.11 matthew 74: my @CacheButtonHTML =
1.19 matthew 75: &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status',
1.50 bisitz 76: '<div class="LC_info">'.&mt('Loading student data...').'</div>');
1.1 matthew 77: $r->rflush();
78: #
1.60 raeburn 79: my %anoncounter =
80: &Apache::lonnet::dump('nohist_anonsurveys',
81: $env{'course.'.$env{'request.course.id'}.'.domain'},
82: $env{'course.'.$env{'request.course.id'}.'.num'});
1.40 albertel 83: if (exists($env{'form.problemchoice'}) &&
84: ! exists($env{'form.SelectAnother'})) {
1.1 matthew 85: foreach my $button (@SubmitButtons) {
86: if ($button->{'name'} eq 'break') {
87: $r->print("<br />\n");
88: } else {
89: $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
90: 'value="'.&mt($button->{'text'}).'" />');
91: $r->print(' 'x5);
92: }
93: }
1.11 matthew 94: foreach my $html (@CacheButtonHTML) {
95: $r->print($html.(' 'x5));
96: }
1.1 matthew 97: #
1.18 matthew 98: $r->print('<hr />'.$/);
1.1 matthew 99: $r->rflush();
100: #
1.18 matthew 101: # Determine which problems we are to analyze
102: my @Symbs =
103: &Apache::lonstathelpers::get_selected_symbs('problemchoice');
1.60 raeburn 104:
105: # If there are multi-part problems with anonymous survey and named
106: # parts check if named was picked for display.
107: #
108: my %mixed_named;
109: foreach my $envkey (%env) {
110: if ($envkey =~ /^form\.mixed_(\d+:\d+)$/) {
111: my $item = $1;
112: if ($env{$envkey} =~ /^symb_(.+)$/) {
113: my $symb = &unescape($1);
114: if (ref($mixed_named{$symb}) eq 'ARRAY') {
115: push(@{$mixed_named{$symb}},$item);
116: } else {
117: @{$mixed_named{$symb}} = ($item);
118: }
119: }
120: }
1.18 matthew 121: }
1.1 matthew 122: #
1.18 matthew 123: # Get resource objects
124: my $navmap = Apache::lonnavmaps::navmap->new();
125: if (!defined($navmap)) {
1.60 raeburn 126: foreach my $selected (@Symbs) {
127: $r->print('<input type="hidden" name="problemchoice" value="'.
128: &escape($selected).'" />'.$/);
129: if (ref($mixed_named{$selected}) eq 'ARRAY') {
130: foreach my $item (@{$mixed_named{$selected}}) {
131: $r->print('<input type="hidden" name="mixed_'.$item.'" value="'.&escape($selected).'" />'.$/);
132: }
133: }
134: }
1.50 bisitz 135: $r->print('<div class="LC_error">'.&mt("Internal error").'</div>');
1.18 matthew 136: return;
137: }
138: my %already_seen;
1.60 raeburn 139: my (@Problems,@anonProbs,@namedProbs,$show_named);
1.18 matthew 140: foreach my $symb (@Symbs) {
141: my $resource = $navmap->getBySymb($symb);
1.60 raeburn 142: my ($hasanon,$hasnamed);
143: if (ref($resource)) {
144: foreach my $partid (@{$resource->parts}) {
145: if (($anoncounter{$symb."\0".$partid}) || ($resource->is_anonsurvey($partid))) {
146: unless (exists($mixed_named{$symb})) {
147: $hasanon = 1;
148: }
149: } else {
150: $hasnamed = 1;
151: }
152: }
153: if ($hasanon) {
154: push(@anonProbs,$resource);
155: } elsif ($hasnamed) {
156: push(@namedProbs,$resource);
157: }
158: }
1.1 matthew 159: }
1.60 raeburn 160: if (@namedProbs > 0) {
161: @Problems = @namedProbs;
162: $show_named = 1;
163: } elsif (@anonProbs > 0) {
164: @Problems = @anonProbs;
165: }
166: foreach my $selected (@Symbs) {
167: $r->print('<input type="hidden" name="problemchoice" value="'.
168: &escape($selected).'" />'.$/);
169: if (ref($mixed_named{$selected}) eq 'ARRAY') {
170: foreach my $item (@{$mixed_named{$selected}}) {
171: $r->print('<input type="hidden" name="mixed_'.$item.'" value="'.&escape($selected).'" />'.$/);
172: }
173: }
174: }
175: # If these are to be anonymized, do a random shuffle of @Students.
176: unless ($show_named) {
177: &array_shuffle(\@Students);
178: }
179: #
1.61 ! raeburn 180:
! 181: my $threshold = $env{'course.'.$env{'request.course.id'}.'.internal.anonsurvey_threshold'};
! 182: if ($threshold eq '') {
! 183: my %domconfig =
! 184: &Apache::lonnet::get_dom('configuration',['coursedefaults'],
! 185: $env{'course.'.$env{'request.course.id'}.'.domain'});
! 186: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
! 187: $threshold = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
! 188: if ($threshold eq '') {
! 189: $threshold = 10;
! 190: }
! 191: } else {
! 192: $threshold = 10;
! 193: }
! 194: }
1.37 matthew 195: $r->print('<h4>'.
196: &Apache::lonstatistics::section_and_enrollment_description().
197: '</h4>');
1.18 matthew 198: if (! scalar(@Problems) || ! defined($Problems[0])) {
1.61 ! raeburn 199: $r->print(&mt('resource is undefined'));
! 200: } elsif (!$show_named && @Students < $threshold) {
! 201: $r->print(&mt('The number of students matching the selection criteria is too few for display of submission data for anonymous surveys.').'<br />'.&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.'));
1.1 matthew 202: } else {
1.18 matthew 203: if (scalar(@Problems) == 1) {
204: my $resource = $Problems[0];
205: $r->print('<h1>'.$resource->title.'</h1>');
206: $r->print('<h3>'.$resource->src.'</h3>');
1.40 albertel 207: if ($env{'form.renderprob'} eq 'true') {
1.36 matthew 208: $r->print(&Apache::lonstathelpers::render_resource($resource));
1.18 matthew 209: $r->rflush();
210: }
211: }
1.40 albertel 212: if ($env{'form.output'} eq 'excel') {
1.60 raeburn 213: &prepare_excel_output($r,\@Problems,\@Students,\%anoncounter,$show_named);
1.40 albertel 214: } elsif ($env{'form.output'} eq 'csv') {
1.60 raeburn 215: &prepare_csv_output($r,\@Problems,\@Students,\%anoncounter,$show_named);
1.21 matthew 216: } else {
1.60 raeburn 217: &prepare_html_output($r,\@Problems,\@Students,\%anoncounter,$show_named);
1.21 matthew 218: }
1.1 matthew 219: }
220: $r->print('<hr />');
221: } else {
222: $r->print('<input type="submit" name="Generate" value="'.
1.17 matthew 223: &mt('Prepare Report').'" />');
1.1 matthew 224: $r->print(' 'x5);
1.19 matthew 225: $r->print('<p>'.
1.61 ! raeburn 226: &mt('Computing correct answers greatly increases the amount of time required to prepare a report.').
1.19 matthew 227: '</p>');
228: $r->print('<p>'.
1.50 bisitz 229: &mt('Please select problems and use the [_1]Prepare Report[_2] button to continue.','<b>','</b>').
1.19 matthew 230: '</p>');
1.18 matthew 231: $r->print(&Apache::lonstathelpers::MultipleProblemSelector
1.60 raeburn 232: (undef,'problemchoice','Statistics',\%anoncounter));
1.18 matthew 233: }
234: }
235:
1.60 raeburn 236: sub array_shuffle {
237: my $array = shift;
238: return unless (ref($array) eq 'ARRAY');
239: my $i = scalar(@$array);
240: my $j;
241: foreach my $item (@$array) {
242: --$i;
243: $j = int(rand($i+1));
244: next if($i == $j);
245: @$array [$i,$j] = @$array[$j,$i];
246: }
247: return @$array;
248: }
249:
1.30 matthew 250: ##
1.31 matthew 251: ## get_extra_response_headers
252: ##
253: sub get_extra_response_headers {
1.60 raeburn 254: my ($show_named) = @_;
1.31 matthew 255: my @extra_resp_headers;
1.40 albertel 256: if ($env{'form.correctans'} eq 'true') {
1.31 matthew 257: push(@extra_resp_headers,'Correct');
1.60 raeburn 258: }
259: if ($show_named) {
260: if ($env{'form.prob_status'} eq 'true') {
261: push(@extra_resp_headers,'Award Detail');
262: push(@extra_resp_headers,'Time');
263: push(@extra_resp_headers,'Attempt');
264: push(@extra_resp_headers,'Awarded');
265: }
1.31 matthew 266: }
267: return @extra_resp_headers;
268: }
269:
270: ##
1.30 matthew 271: ## get_headers:
272: ## return the proper headers for the given response
273: sub get_headers {
274: my ($prob,$partid,$respid,$resptype,$analysis,$output,$purpose,
275: @basic_headers) = @_;
276: my @headers;
277: if ($resptype eq 'essay' && $purpose eq 'display' &&
278: ($output eq 'html')) {# || scalar(@{$prob->parts})!=1)) {
279: @headers = ();
280: } elsif ($resptype =~ /^(option|match|rank)$/) {
281: my $prefix = '_';
282: if ($purpose eq 'display') {
283: $prefix = '';
284: }
285: my @foils =
286: map {
287: $prefix.$_;
288: } sort(keys(%{$analysis->{$partid.'.'.$respid}->{'_Foils'}}));
289: if (scalar(@basic_headers) && $basic_headers[0] eq 'Correct') {
290: @foils = map { ($_ , $_.' Correct') } @foils;
291: shift(@basic_headers); # Get rid of 'Correct'
292: }
293: @headers = (@foils,@basic_headers);
1.42 albertel 294: } elsif (lc($resptype) eq 'task') {
295: @headers = ('Grader','Status',@basic_headers,'Submission');
1.30 matthew 296: } else {
297: @headers = ('Submission',@basic_headers);
298: }
299: return @headers;
300: }
301:
1.18 matthew 302: #########################################################
303: #########################################################
1.21 matthew 304: ##
305: ## HTML Output Routines
306: ##
307: #########################################################
308: #########################################################
309: sub prepare_html_output {
1.60 raeburn 310: my ($r,$problems,$students,$anoncounter,$show_named) = @_;
1.21 matthew 311: my $c = $r->connection();
312: #
313: # Set a flag for the case when there is just one problem
314: my $single_response = 0;
315: if (scalar(@$problems) == 1 &&
316: $problems->[0]->countResponses == 1) {
317: $single_response = 1;
318: }
319: #
320: # Compute the number of columns per response
1.60 raeburn 321: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.21 matthew 322: #
323: # Create the table header
1.60 raeburn 324: my @student_columns;
325: if ($show_named) {
326: @student_columns = @Apache::lonstatistics::SelectedStudentData;
327: if (grep(/^all$/,@student_columns)) {
328: @student_columns = qw(fullname username domain id section status groups comments);
1.57 onken 329: }
1.60 raeburn 330: } else {
331: @student_columns = ('username');
1.57 onken 332: }
1.21 matthew 333: #
334: my %headers;
335: my $student_column_count = scalar(@student_columns);
336: $headers{'problem'} = qq{<th colspan="$student_column_count">\ </th>};
337: foreach (@student_columns) {
338: $headers{'student'}.= '<th>'.ucfirst($_).'</th>';
339: }
340: #
341: # we put the headers into the %headers hash
342: my $total_col = scalar(@student_columns);
343: my $nonempty_part_headers = 0;
1.30 matthew 344: #
345: my %problem_analysis;
1.21 matthew 346: foreach my $prob (@$problems) {
1.31 matthew 347: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
1.30 matthew 348: $problem_analysis{$prob->src}=\%analysis;
1.60 raeburn 349: my $symb = $prob->symb();
1.30 matthew 350: #
1.21 matthew 351: my $prob_span = 0;
352: my $single_part = 0;
353: if (scalar(@{$prob->parts}) == 1) {
354: $single_part = 1;
355: }
1.60 raeburn 356: my $shown_parts = 0;
1.21 matthew 357: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 358: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
359: next if ($show_named);
360: } else {
361: next unless ($show_named);
362: }
363: $shown_parts ++;
1.21 matthew 364: my $part_span = 0;
365: my $responses = [$prob->responseIds($partid)];
366: my $resptypes = [$prob->responseType($partid)];
367: for (my $i=0;$i<scalar(@$responses);$i++) {
1.30 matthew 368: my $respid = $responses->[$i];
369: my @headers = &get_headers($prob,$partid,$respid,
370: $resptypes->[$i],
371: $problem_analysis{$prob->src},
372: 'html','display',
373: @extra_resp_headers);
374: if (scalar(@headers)>0) {
375: $total_col += scalar(@headers);
376: $part_span += scalar(@headers);
1.21 matthew 377: $headers{'response'} .=
1.30 matthew 378: '<th colspan="'.scalar(@headers).'">'.
1.21 matthew 379: &mt('Response [_1]',$responses->[$i]).'</th>';
1.52 raeburn 380: $headers{'student'}.= '<th><span class="LC_nobreak">'.
381: join('</span></th><th><span class="LC_nobreak">',
1.30 matthew 382: @headers).
1.51 bisitz 383: '</span></th>';
1.21 matthew 384: }
385: }
1.41 matthew 386: if ($part_span == 0) {
387: next;
388: }
1.21 matthew 389: if (! $single_part) {
390: my $tmpname = $partid;
391: if ($partid =~/^\d+$/) {
1.24 matthew 392: $tmpname = $prob->part_display($partid);
1.21 matthew 393: }
1.35 matthew 394: if ($tmpname !~ /^part/) {
395: $tmpname = 'Part '.$tmpname;
396: }
1.21 matthew 397: $headers{'part'} .= qq{<th colspan="$part_span">$tmpname</th>};
398: $nonempty_part_headers = 1;
399: } else {
1.53 bisitz 400: $headers{'part'} .= qq{<th colspan="$part_span"> </th>};
1.21 matthew 401: }
402: $prob_span += $part_span;
403: }
1.60 raeburn 404: next if (!$shown_parts);
1.23 matthew 405: my $title = $prob->compTitle;
1.21 matthew 406: if ($prob_span > 0) {
407: $headers{'problem'}.= qq{<th colspan="$prob_span">$title</th>};
408: } elsif ($single_response) {
409: $prob_span = scalar(@student_columns);
410: $headers{'problem'} = qq{<th colspan="$prob_span">$title</th>};
411: }
412: }
413: if (exists($headers{'part'})) {
414: $headers{'part'} = qq{<th colspan="$student_column_count">\ </th>}.
415: $headers{'part'};
416: }
417: if (exists($headers{'response'})) {
418: $headers{'response'}=
419: qq{<th colspan="$student_column_count">\ </th>}.
420: $headers{'response'};
421: }
422: my $full_header = $/.'<table>'.$/;
423: $full_header .= '<tr align="left">'.$headers{'problem'}.'</tr>'.$/;
424: if ($nonempty_part_headers) {
425: $full_header .= '<tr align="left">'.$headers{'part'}.'</tr>'.$/;
426: }
427: $full_header .= '<tr align="left">'.$headers{'response'}.'</tr>'.$/;
428: $full_header .= '<tr align="left">'.$headers{'student'}.'</tr>'.$/;
429: #
430: # Main loop
431: my $count;
432: $r->print($/.$full_header.$/);
433: my $row_class = 'odd'; # css
434: foreach my $student (@$students) {
435: my $student_row_data;
436: if ($count++ >= 30) {
437: $r->print('</table>'.$/.$full_header.$/);
438: $count = 0;
439: }
440: last if ($c->aborted());
1.60 raeburn 441: if ($show_named) {
442: foreach my $field (@student_columns) {
443: $student_row_data .= '<td valign="top">';
444: # handle comments like in lonstudentassessment.pm
445: if($field eq 'comments') {
446: $student_row_data .=
1.57 onken 447: '<a href="/adm/'.$student->{'domain'}.'/'.
448: $student->{'username'}.'/'.'aboutme#coursecomment">'.&mt('Comments').'</a>';
1.60 raeburn 449: } else {
450: $student_row_data .= $student->{$field};
451: }
452: $student_row_data .= '</td>';
1.57 onken 453: }
1.60 raeburn 454: } else {
455: $student_row_data = '<td valign="top" colspan="'.$student_column_count.'">'.&mt('Anonymized').'</td>';
1.21 matthew 456: }
457: #
458: # Figure out what it is we need to output for this student
459: my @essays;
1.30 matthew 460: my %prob_data;
1.21 matthew 461: my $maxrow;
462: foreach my $prob (@$problems) {
1.60 raeburn 463: my $symb = $prob->symb;
464: $prob_data{$symb}={};
1.21 matthew 465: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 466: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
467: next if ($show_named);
468: } else {
469: next unless ($show_named);
470: }
1.21 matthew 471: my @responses = $prob->responseIds($partid);
472: my @response_type = $prob->responseType($partid);
473: for (my $i=0;$i<=$#responses;$i++) {
1.30 matthew 474: my $respid = $responses[$i];
1.21 matthew 475: my $results =
476: &Apache::loncoursedata::get_response_data_by_student
477: ($student,$prob->symb(),$respid);
1.30 matthew 478: my $resptype = $response_type[$i];
479: my @headers = &get_headers($prob,$partid,$respid,
480: $resptype,
481: $problem_analysis{$prob->src},
482: 'html','normal',
483: @extra_resp_headers);
484: my $width = scalar(@headers);
1.41 matthew 485: next if ($width < 1);
1.30 matthew 486: my $resp_data;
1.41 matthew 487: $resp_data->{'fake'} = qq{<td colspan="$width"> </td>};
1.21 matthew 488: if (! defined($results)) {
489: $results = [];
490: }
1.30 matthew 491: #
1.21 matthew 492: if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
493: $maxrow = scalar(@$results);
494: }
495: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
1.40 albertel 496: if ($env{'form.all_sub'} ne 'true') {
1.21 matthew 497: next if ($j ne scalar(@$results)-1);
498: }
1.30 matthew 499: my $response = &hashify_response($results->[$j],
500: $prob,
501: $student,
502: $partid,
503: $respid);
1.21 matthew 504: if ($resptype eq 'essay') {
505: push(@essays,
1.30 matthew 506: &html_essay_results(\@headers,
1.21 matthew 507: $prob,$partid,$respid,
508: $response,
509: $single_response).
510: '</td>');
1.42 albertel 511: } elsif (lc($resptype) eq 'task') {
512: my $results =
513: &html_task_results(\@headers,
514: $prob,$partid,$respid,
515: $response,$resptype);
516: if ($results) {
517: push(@{$resp_data->{'real'}},$results);
518: }
1.21 matthew 519: } else {
520: push(@{$resp_data->{'real'}},
1.30 matthew 521: &html_non_essay_results(\@headers,
522: $prob,$partid,$respid,
523: $response,$resptype));
1.21 matthew 524: }
1.30 matthew 525: }
526: $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
1.21 matthew 527: } # end of $i loop
528: } # end of partid loop
529: } # end of prob loop
530: #
531: # if there is no data, skip this student.
532: next if (! $maxrow && ! scalar(@essays));
533: #
534: # Go through the problem data and output a row.
535: if ($row_class eq 'even') {
536: $row_class = 'odd';
537: } else {
538: $row_class = 'even';
539: }
540: my $printed_something;
541: for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
542: my $html;
543: my $no_data = 1;
544: foreach my $prob (@$problems) {
545: foreach my $partid (@{$prob->parts}) {
546: my @responses = $prob->responseIds($partid);
547: my @response_type = $prob->responseType($partid);
548: for (my $i=0;$i<=$#responses;$i++) {
549: my $respid = $responses[$i];
550: my $resp_data =
1.30 matthew 551: $prob_data{$prob->symb}->{$partid}->{$respid};
1.21 matthew 552: next if ($response_type[$i] eq 'essay');
553: if (defined($resp_data->{'real'}->[$rows_output])) {
554: $html .= $resp_data->{'real'}->[$rows_output];
555: $no_data = 0;
556: } else {
557: $html .= $resp_data->{'fake'};
558: }
559: }
560: }
561: }
562: if (! $no_data) {
563: $r->print(qq{<tr class="$row_class">$student_row_data$html</tr>}.$/);
564: $printed_something=1;
565: }
566: }
567: if (@essays) {
568: my $tr = qq{<tr class="$row_class">};
569: my $td = qq{<td valign="top" class="essay" colspan="$total_col">};
570: if (! $printed_something) {
571: $r->print($tr.$student_row_data.'</tr>'.$/);
572: }
573: $r->print($tr.$td.
574: join('</td></tr>'.$/.$tr.$td,@essays).'</td></tr>'.$/);
575: undef(@essays);
576: }
577: } # end of student loop
1.58 bisitz 578: $r->print('</table>'.$/);
1.21 matthew 579: return;
580: }
581:
1.30 matthew 582: sub hashify_response {
583: my ($response,$prob,$student,$partid,$respid) =@_;
584: my $resp_hash = {};
1.40 albertel 585: if ($env{'form.correctans'} eq 'true') {
1.30 matthew 586: $resp_hash->{'Correct'} =
587: &Apache::lonstathelpers::get_student_answer
588: ($prob,$student->{'username'},$student->{'domain'},
589: $partid,$respid);
590: }
591: $resp_hash->{'Submission'} =
592: $response->[&Apache::loncoursedata::RDs_submission()];
593: $resp_hash->{'Time'} =
594: $response->[&Apache::loncoursedata::RDs_timestamp()];
595: $resp_hash->{'Attempt'} =
596: $response->[&Apache::loncoursedata::RDs_tries()];
597: $resp_hash->{'Awarded'} =
598: $response->[&Apache::loncoursedata::RDs_awarded()];
1.42 albertel 599: if ($prob->is_task()) {
600: $resp_hash->{'Grader'} =
601: $response->[&Apache::loncoursedata::RDs_response_eval_2()];
602: if ($resp_hash->{'Attempt'} eq '0') {
603: $resp_hash->{'Attempt'} = '';
604: }
605: $resp_hash->{'Award Detail'} =
606: $response->[&Apache::loncoursedata::RDs_part_award()];
607: $resp_hash->{'Status'} =
608: $response->[&Apache::loncoursedata::RDs_response_eval()];
609: } else {
610: $resp_hash->{'Award Detail'} =
611: $response->[&Apache::loncoursedata::RDs_awarddetail()];
612: }
613:
1.30 matthew 614: return $resp_hash;
615: }
616:
1.21 matthew 617: #####################################################
618: ##
619: ## HTML helper routines
620: ##
621: #####################################################
622: sub html_essay_results {
1.30 matthew 623: my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
624: if (! ref($headers) || ref($headers) ne 'ARRAY') {
625: return '';
1.21 matthew 626: }
1.30 matthew 627: # Start of telling them what problem, part, and response
1.21 matthew 628: my $Str;
629: if (! $single_response) {
1.23 matthew 630: my $id = $prob->compTitle;
1.21 matthew 631: if (defined($partid) && $partid ne '0') {
1.24 matthew 632: $id .= ' '.$prob->part_display($partid);
1.21 matthew 633: }
634: if (defined($respid)) {
635: $id .= ' '.$respid;
636: }
1.51 bisitz 637: $Str .= '<span class="LC_nobreak">'.$id.'</span>'.(' 'x4);
1.21 matthew 638: }
1.30 matthew 639: #
640: shift(@$headers); # Get rid of the Submission header
641: my $correct = '';
642: if ($headers->[0] eq 'Correct') {
643: $correct = &html_format_essay_sub($response->{'Correct'});
644: shift(@$headers);
645: }
1.51 bisitz 646: $Str .= '<span class="LC_nobreak">'.
1.30 matthew 647: join('',
648: map {
649: (' 'x4).&mt($_.': [_1]',$response->{$_});
1.51 bisitz 650: } @$headers).'</span>';
1.30 matthew 651: if (@$headers || ! $single_response) {
652: $Str .= '<br />';
1.21 matthew 653: }
1.30 matthew 654: $Str .= &html_format_essay_sub($response->{'Submission'});
655: #
1.21 matthew 656: if (defined($correct) && $correct !~ /^\s*$/) {
657: $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
658: }
659: return $Str;
660: }
661:
1.30 matthew 662: sub html_format_essay_sub {
663: my ($submission) = @_;
664: return '' if (! defined($submission) || $submission eq '');
665: $submission = &HTML::Entities::decode($submission);
666: $submission =~ s/\\\"/\"/g;
667: $submission =~ s/\\\'/\'/g;
668: $submission =~ s|\\r\\n|$/|g;
669: $submission = &HTML::Entities::encode($submission,'<>&"');
670: $submission =~ s|$/\s*$/|$/</p><p>$/|g;
671: $submission =~ s|\\||g;
672: $submission = '<p>'.$submission.'</p>';
673: return $submission;
1.21 matthew 674: }
675:
1.42 albertel 676: sub html_task_results {
677: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
678: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
679: return '';
680: }
681:
682: my @values;
683: @values = map { $response->{$_}; } @$headers;
684:
685: my $td = '<td valign="top">';
686: my $str = $td.join('</td>'.$td,@values).'</td>';
687: return $str;
688: }
689:
1.30 matthew 690: sub html_non_essay_results {
691: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
692: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
693: return '';
694: }
695: #
1.45 www 696: my $submission = &HTML::Entities::decode(&unescape($response->{'Submission'}));
1.21 matthew 697: return '' if (! defined($submission) || $submission eq '');
1.25 matthew 698: $submission =~ s/\\\"/\"/g;
699: $submission =~ s/\\\'/\'/g;
1.30 matthew 700: if ($resptype eq 'radiobutton') {
1.25 matthew 701: $submission = &HTML::Entities::encode($submission,'<>&"');
1.21 matthew 702: $submission =~ s/=([^=])$//;
1.51 bisitz 703: $submission = '<span class="LC_nobreak">'.$submission.'</span>';
1.30 matthew 704: }
705: $response->{'Submission'} = $submission;
706: #
707: my @values;
708: if ($resptype =~ /^(option|match|rank)$/) {
709: my %submission =
710: map {
1.45 www 711: my ($foil,$value) = split('=',&unescape($_));
1.30 matthew 712: ($foil,$value);
713: } split('&',$response->{'Submission'});
714: my %correct;
715: if (exists($response->{'Correct'})) {
716: %correct =
717: map {
1.45 www 718: my ($foil,$value)=split('=',&unescape($_));
1.30 matthew 719: ($foil,$value);
720: } split('&',$response->{'Correct'});
721: }
722: #
723: foreach my $original_header (@$headers) {
724: if ($original_header =~ /^_/) {
725: # '_' denotes a foil column
726: my ($header) = ($original_header =~ m/^_(.*)$/);
727: my $option = '';
728: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
729: if (exists($correct{$foil})) {
730: $option = $correct{$foil};
731: }
732: } elsif (exists($submission{$header})) {
733: $option = $submission{$header};
734: }
735: push(@values,&HTML::Entities::encode($option));
1.38 matthew 736: } elsif ($original_header eq 'Time') {
737: push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
1.30 matthew 738: } else {
739: # A normal column
740: push(@values,$response->{$original_header});
741: }
742: }
1.25 matthew 743: } else {
1.30 matthew 744: @values = map { $response->{$_}; } @$headers;
1.21 matthew 745: }
1.30 matthew 746: my $td = '<td valign="top">';
747: my $str = $td.join('</td>'.$td,@values).'</td>';
748: return $str;
1.21 matthew 749: }
1.18 matthew 750:
1.30 matthew 751:
1.21 matthew 752: #########################################################
753: #########################################################
754: ##
755: ## Excel Output Routines
756: ##
757: #########################################################
758: #########################################################
759: sub prepare_excel_output {
1.60 raeburn 760: my ($r,$Problems,$Students,$anoncounter,$show_named) = @_;
1.18 matthew 761: my $c = $r->connection();
762: #
1.19 matthew 763: #
764: # Determine the number of columns in the spreadsheet
765: my $columncount = 3; # username, domain, id
1.60 raeburn 766: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.20 matthew 767: my $lastprob;
1.31 matthew 768: my %problem_analysis;
1.19 matthew 769: foreach my $prob (@$Problems) {
1.60 raeburn 770: my $symb = $prob->symb();
1.31 matthew 771: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
772: $problem_analysis{$prob->src}=\%analysis;
773: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 774: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
775: next if ($show_named);
776: } else {
777: next unless ($show_named);
778: }
779:
1.31 matthew 780: my $responses = [$prob->responseIds($partid)];
781: my $resptypes = [$prob->responseType($partid)];
782: for (my $i=0;$i<scalar(@$responses);$i++) {
783: my @headers = &get_headers($prob,$partid,$responses->[$i],
784: $resptypes->[$i],
785: $problem_analysis{$prob->src},
786: 'excel','display',
787: @extra_resp_headers);
788: $columncount += scalar(@headers);
789: }
790: }
1.19 matthew 791: last if ($columncount > 255);
1.20 matthew 792: $lastprob = $prob;
1.19 matthew 793: }
794: if ($columncount > 255) {
795: $r->print('<h1>'.&mt('Unable to complete request').'</h1>'.$/.
796: '<p>'.&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.').'</p>'.$/.
1.60 raeburn 797: '<p>'.&mt('Consider selecting fewer problems to generate reports on, or reducing the number of items per problem. Or use HTML or CSV output.').'</p>'.$/);
798: if (ref($lastprob)) {
799: $r->print('<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
800: }
1.19 matthew 801: $r->rflush();
802: return;
803: }
804: #
805: # Print out a message telling them what we are doing
1.18 matthew 806: if (scalar(@$Problems) > 1) {
807: $r->print('<h2>'.
808: &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
809: scalar(@$Problems)).
810: '</h2>');
811: } else {
812: $r->print('<h2>'.
813: &mt('Preparing Excel spreadsheet of student responses').
814: '</h2>');
815: }
816: $r->rflush();
817: #
818: # Create the excel spreadsheet
1.36 matthew 819: my ($workbook,$filename,$format) =
820: &Apache::loncommon::create_workbook($r);
821: return if (! defined($workbook));
1.18 matthew 822: my $worksheet = $workbook->addworksheet('Student Submission Data');
823: #
824: # Add headers to the worksheet
825: my $rows_output = 0;
826: $worksheet->write($rows_output++,0,
1.40 albertel 827: $env{'course.'.$env{'request.course.id'}.'.description'},
1.18 matthew 828: $format->{'h1'});
829: $rows_output++;
830: my $cols_output = 0;
831: my $title_row = $rows_output++;
832: my $partid_row = $rows_output++;
833: my $respid_row = $rows_output++;
834: my $header_row = $rows_output++;
835: $worksheet->write($title_row ,0,'Problem Title',$format->{'bold'});
836: $worksheet->write($partid_row,0,'Part ID',$format->{'bold'});
837: $worksheet->write($respid_row,0,'Response ID',$format->{'bold'});
838: # Student headers
1.60 raeburn 839: my @StudentColumns;
840: if ($show_named) {
841: @StudentColumns = qw(username domain id section);
842: } else {
843: @StudentColumns = qw(username);
844: }
1.18 matthew 845: foreach (@StudentColumns) {
1.19 matthew 846: $worksheet->write($header_row,$cols_output++,ucfirst($_),
847: $format->{'bold'});
1.18 matthew 848: }
849: # Problem headers
1.31 matthew 850: my %start_col;
1.18 matthew 851: foreach my $prob (@$Problems) {
1.23 matthew 852: my $title = $prob->compTitle;
1.60 raeburn 853: my $symb = $prob->symb();
1.18 matthew 854: $worksheet->write($title_row,$cols_output,
855: $title,$format->{'h3'});
856: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 857: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
858: next if ($show_named);
859: } else {
860: next unless ($show_named);
861: }
1.24 matthew 862: $worksheet->write($partid_row,$cols_output,
863: $prob->part_display($partid));
1.18 matthew 864: my $responses = [$prob->responseIds($partid)];
865: my $resptypes = [$prob->responseType($partid)];
866: for (my $i=0;$i<scalar(@$responses);$i++) {
1.31 matthew 867: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
868: $cols_output;
1.18 matthew 869: $worksheet->write($respid_row,$cols_output,
870: $resptypes->[$i].', '.$responses->[$i]);
1.31 matthew 871: my @headers = &get_headers($prob,$partid,$responses->[$i],
872: $resptypes->[$i],
873: $problem_analysis{$prob->src},
874: 'excel','display',
875: @extra_resp_headers);
876: foreach my $text (@headers) {
877: if ($text eq 'Time') {
878: $worksheet->set_column($cols_output,$cols_output,undef,
879: $format->{'date'});
880: }
881: $worksheet->write($header_row,$cols_output++,$text);
1.20 matthew 882: }
1.18 matthew 883: }
884: }
885: }
886: #
887: # Populate the worksheet with the student data
888: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
889: ($r,'Excel File Compilation Status',
890: 'Excel File Compilation Progress',
891: scalar(@$Students),'inline',undef,'Statistics','stats_status');
1.20 matthew 892: my $max_row = $rows_output;
1.18 matthew 893: foreach my $student (@$Students) {
894: last if ($c->aborted());
895: $cols_output = 0;
1.20 matthew 896: my $student_row = $max_row;
1.18 matthew 897: foreach my $field (@StudentColumns) {
1.60 raeburn 898: if ($show_named) {
899: $worksheet->write($student_row,$cols_output++,
900: $student->{$field});
901: } else {
902: $worksheet->write($student_row,$cols_output++,
903: &mt('Anonymized'));
904: }
1.18 matthew 905: }
1.20 matthew 906: my $last_student_col = $cols_output-1;
1.18 matthew 907: foreach my $prob (@$Problems) {
1.60 raeburn 908: my $symb = $prob->symb();
1.18 matthew 909: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 910: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
911: next if ($show_named);
912: } else {
913: next unless ($show_named);
914: }
1.18 matthew 915: my @Response = $prob->responseIds($partid);
916: my @ResponseType = $prob->responseType($partid);
917: for (my $i=0;$i<=$#Response;$i++) {
918: my $respid = $Response[$i];
919: my $resptype = $ResponseType[$i];
920: my $results =
921: &Apache::loncoursedata::get_response_data_by_student
922: ($student,$prob->symb(),$respid);
1.31 matthew 923: my @headers = &get_headers($prob,$partid,$respid,
924: $resptype,
925: $problem_analysis{$prob->src},
926: 'excel','normal',
927: @extra_resp_headers);
928:
1.20 matthew 929: if (! defined($results)) {
930: $results = [];
931: }
932: #
933: $rows_output = $student_row;
934: #
1.31 matthew 935: my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
1.20 matthew 936: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
937: $cols_output = $response_start_col;
1.40 albertel 938: if ($env{'form.all_sub'} ne 'true') {
1.20 matthew 939: next if ($j ne scalar(@$results)-1);
940: }
1.31 matthew 941: my $response = &hashify_response($results->[$j],
942: $prob,
943: $student,
944: $partid,
945: $respid);
946: my @response_data =
1.33 matthew 947: &compile_response_data(\@headers,$response,
948: $prob,$partid,$respid,
949: $resptype,
950: \&excel_format_item);
1.31 matthew 951: $worksheet->write_row($rows_output++,$cols_output,
952: \@response_data);
953: $cols_output+=scalar(@response_data);
1.20 matthew 954: if ($rows_output > $max_row) {
955: $max_row = $rows_output;
956: }
1.19 matthew 957: }
1.18 matthew 958: }
959: }
960: }
1.28 matthew 961: # Fill in the remaining rows with the students data
1.34 matthew 962: for (my $row = $student_row+1;$row<$max_row;$row++) {
1.28 matthew 963: my $cols = 0;
964: foreach my $field (@StudentColumns) {
1.60 raeburn 965: if ($show_named) {
966: $worksheet->write($row,$cols++,
967: $student->{$field});
968: } else {
969: $worksheet->write($row,$cols++,
970: &mt('Anonymized'));
971: }
1.28 matthew 972: }
973: }
1.18 matthew 974: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
975: 'last student');
976: }
977: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
978: #
979: # Close the excel file
980: $workbook->close();
981: #
982: # Write a link to allow them to download it
983: $r->print('<p><a href="'.$filename.'">'.
984: &mt('Your Excel spreadsheet.').
985: '</a></p>'."\n");
986: $r->print('<script>'.
987: 'window.document.Statistics.stats_status.value="'.
988: 'Done compiling spreadsheet. See link below to download.'.
989: '";</script>');
990: $r->rflush();
991: return;
992: }
993:
1.33 matthew 994: sub compile_response_data {
995: my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
1.31 matthew 996: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
997: return ();
998: }
1.33 matthew 999: if (ref($format) ne 'CODE') {
1000: $format = sub { return $_[0]; };
1001: }
1.31 matthew 1002: #
1.33 matthew 1003: my $submission =
1004: &HTML::Entities::decode
1.45 www 1005: (&unescape($response->{'Submission'}));
1.42 albertel 1006: if (!$prob->is_task()) {
1007: return () if (! defined($submission) || $submission eq '');
1008: }
1.31 matthew 1009: $submission =~ s/\\\"/\"/g;
1010: $submission =~ s/\\\'/\'/g;
1011: if ($resptype eq 'radiobutton') {
1012: $submission =~ s/=([^=])$//;
1.20 matthew 1013: }
1.31 matthew 1014: $response->{'Submission'} = $submission;
1015: #
1016: my @values;
1017: if ($resptype =~ /^(option|match|rank)$/) {
1018: my %submission =
1019: map {
1.45 www 1020: my ($foil,$value) = split('=',&unescape($_));
1.31 matthew 1021: ($foil,$value);
1022: } split('&',$response->{'Submission'});
1023: my %correct;
1024: if (exists($response->{'Correct'})) {
1025: %correct =
1026: map {
1.45 www 1027: my ($foil,$value)=split('=',&unescape($_));
1.31 matthew 1028: ($foil,$value);
1029: } split('&',$response->{'Correct'});
1030: }
1031: #
1032: foreach my $original_header (@$headers) {
1033: if ($original_header =~ /^_/) {
1034: # '_' denotes a foil column
1035: my ($header) = ($original_header =~ m/^_(.*)$/);
1036: my $option = '';
1037: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
1038: if (exists($correct{$foil})) {
1039: $option = $correct{$foil};
1040: }
1041: } elsif (exists($submission{$header})) {
1042: $option = $submission{$header};
1043: }
1.33 matthew 1044: push(@values,&{$format}($option,$header));
1.31 matthew 1045: } else {
1046: # A normal column
1.33 matthew 1047: push(@values,&{$format}($response->{$original_header},
1.31 matthew 1048: $original_header));
1049: }
1050: }
1051: } else {
1.33 matthew 1052: @values = map { &{$format}($response->{$_},$_); } @$headers;
1.20 matthew 1053: }
1.31 matthew 1054: return @values;
1.20 matthew 1055: }
1056:
1.31 matthew 1057: sub excel_format_item {
1058: my ($item,$type) = @_;
1059: if ($type eq 'Time') {
1.33 matthew 1060: $item = &Apache::lonstathelpers::calc_serial($item);
1.31 matthew 1061: } else {
1062: if ($item =~ m/^=/) {
1063: $item = ' '.$item;
1064: }
1065: $item =~ s/\\r//g;
1066: $item =~ s/\\n/\n/g;
1067: $item =~ s/(\s*$|^\s*)//g;
1068: $item =~ s/\\\'/\'/g;
1.18 matthew 1069: }
1.31 matthew 1070: return $item;
1.1 matthew 1071: }
1072:
1073: #########################################################
1074: #########################################################
1.17 matthew 1075: ##
1076: ## CSV output of student answers
1077: ##
1078: #########################################################
1079: #########################################################
1080: sub prepare_csv_output {
1.60 raeburn 1081: my ($r,$problems,$students,$anoncounter,$show_named) = @_;
1.17 matthew 1082: my $c = $r->connection();
1083: #
1084: $r->print('<h2>'.
1085: &mt('Generating CSV report of student responses').'</h2>');
1086: #
1087: # Progress window
1088: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1089: ($r,'CSV File Compilation Status',
1090: 'CSV File Compilation Progress',
1.22 matthew 1091: scalar(@$students),'inline',undef,'Statistics','stats_status');
1092:
1.17 matthew 1093: $r->rflush();
1094: #
1095: # Open a file
1096: my $outputfile;
1097: my $filename = '/prtspool/'.
1.40 albertel 1098: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.17 matthew 1099: time.'_'.rand(1000000000).'.csv';
1100: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
1101: $r->log_error("Couldn't open $filename for output $!");
1.55 bisitz 1102: $r->print(
1103: '<p class="LC_error">'
1104: .&mt('Problems occurred in writing the CSV file.')
1105: .' '.&mt('This error has been logged.')
1106: .' '.&mt('Please alert your LON-CAPA administrator.')
1107: .'</p>'
1108: );
1.17 matthew 1109: $outputfile = undef;
1110: }
1111: #
1.22 matthew 1112: # Compute the number of columns per response
1.60 raeburn 1113: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.22 matthew 1114: #
1115: # Create the table header
1.38 matthew 1116: my @student_columns = ('username','domain','id','section');
1.60 raeburn 1117: if ($show_named) {
1118: @student_columns = qw(username domain id section);
1119: } else {
1120: @student_columns = qw(username);
1121: }
1122: my $student_column_count = scalar(@student_columns);
1.17 matthew 1123: #
1.22 matthew 1124: my %headers;
1125: push(@{$headers{'student'}},@student_columns);
1126: # Pad for the student data
1127: foreach my $row ('problem','part','response') {
1.32 matthew 1128: $headers{$row}=[map {''} @student_columns];
1.22 matthew 1129: }
1130: #
1131: # we put the headers into the %headers hash
1.32 matthew 1132: my %problem_analysis;
1133: my %start_col;
1134: my $max_column = scalar(@student_columns);
1.22 matthew 1135: foreach my $prob (@$problems) {
1.60 raeburn 1136: my $symb = $prob->symb();
1.32 matthew 1137: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
1138: $problem_analysis{$prob->src}=\%analysis;
1139: $headers{'problem'}->[$max_column] = $prob->compTitle;
1.22 matthew 1140: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 1141: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
1142: next if ($show_named);
1143: } else {
1144: next unless ($show_named);
1145: }
1.32 matthew 1146: $headers{'part'}->[$max_column] = $prob->part_display($partid);
1.22 matthew 1147: my $responses = [$prob->responseIds($partid)];
1.32 matthew 1148: my $resptypes = [$prob->responseType($partid)];
1.22 matthew 1149: for (my $i=0;$i<scalar(@$responses);$i++) {
1.32 matthew 1150: my @headers = &get_headers($prob,$partid,$responses->[$i],
1151: $resptypes->[$i],
1152: $problem_analysis{$prob->src},
1153: 'csv','display',
1154: @extra_resp_headers);
1155: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
1156: $max_column;
1157: $headers{'response'}->[$max_column]=
1.22 matthew 1158: &mt('Response [_1]',$responses->[$i]);
1.32 matthew 1159: for (my $j=0;$j<=$#headers;$j++) {
1160: $headers{'student'}->[$max_column+$j]=$headers[$j];
1.22 matthew 1161: }
1.32 matthew 1162: $max_column += scalar(@headers);
1.17 matthew 1163: }
1164: }
1165: }
1.22 matthew 1166: foreach my $row ('problem','part','response','student') {
1.32 matthew 1167: print $outputfile '"'.
1.22 matthew 1168: join('","',
1169: map {
1170: &Apache::loncommon::csv_translate($_);
1171: } @{$headers{$row}}).'"'.$/;
1.17 matthew 1172: }
1173: #
1.22 matthew 1174: # Main loop
1175: foreach my $student (@$students) {
1.27 matthew 1176: last if ($c->aborted());
1.22 matthew 1177: my @rows;
1178: foreach my $prob (@$problems) {
1.60 raeburn 1179: my $symb = $prob->symb;
1.22 matthew 1180: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 1181: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
1182: next if ($show_named);
1183: } else {
1184: next unless ($show_named);
1185: }
1.22 matthew 1186: my @responses = $prob->responseIds($partid);
1187: my @response_type = $prob->responseType($partid);
1188: for (my $i=0;$i<=$#responses;$i++) {
1189: my $respid = $responses[$i];
1.32 matthew 1190: my $resptype = $response_type[$i];
1191: my @headers = &get_headers($prob,$partid,$respid,$resptype,
1192: $problem_analysis{$prob->src},
1193: 'csv','normal',
1194: @extra_resp_headers);
1.22 matthew 1195: my $results =
1196: &Apache::loncoursedata::get_response_data_by_student
1197: ($student,$prob->symb(),$respid);
1198: if (! defined($results)) {
1199: $results = [];
1200: }
1201: for (my $j=0; $j<scalar(@$results);$j++) {
1.40 albertel 1202: if ($env{'form.all_sub'} ne 'true') {
1.22 matthew 1203: next if ($j != 0);
1204: }
1205: my $idx = scalar(@$results) - $j - 1;
1.32 matthew 1206: my $response = &hashify_response($results->[$idx],
1207: $prob,$student,
1208: $partid,$respid);
1209: my @data = &compile_response_data(\@headers,$response,
1.22 matthew 1210: $prob,$partid,
1.33 matthew 1211: $respid,$resptype,
1212: \&csv_format_item);
1.32 matthew 1213: my $resp_start_idx =
1214: $start_col{$prob->symb}->{$partid}->{$respid};
1.22 matthew 1215: for (my $k=0;$k<=$#data;$k++) {
1.32 matthew 1216: $rows[$j]->[$resp_start_idx + $k] = $data[$k];
1.22 matthew 1217: }
1218: }
1219: }
1.17 matthew 1220: }
1.22 matthew 1221: }
1222: foreach my $row (@rows) {
1.60 raeburn 1223: my $student_row_data = '';
1224: if ($show_named) {
1225: $student_row_data = '"'.join('","',
1226: map { $student->{$_}; }
1227: @student_columns).'"';
1228: } else {
1229: $student_row_data = '"'.&mt('Anonymized').'"';
1230: }
1231: print $outputfile $student_row_data;
1232: for (my $i=$student_column_count;$i<$max_column;$i++) {
1.22 matthew 1233: my $value = &Apache::loncommon::csv_translate($row->[$i]);
1234: $value ||='';
1235: print $outputfile ',"'.$value.'"';
1.17 matthew 1236: }
1.22 matthew 1237: print $outputfile $/;
1.17 matthew 1238: }
1.22 matthew 1239: undef(@rows);
1.17 matthew 1240: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1241: 'last student');
1242: }
1243: close($outputfile);
1244: #
1245: # Close the progress window
1246: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1247: #
1.48 bisitz 1248: # Tell the user where to get their CSV file
1.17 matthew 1249: $r->print('<br />'.
1.48 bisitz 1250: '<a href="'.$filename.'">'.&mt('Your CSV file.').'</a>'."\n");
1.17 matthew 1251: $r->rflush();
1252: return;
1253: }
1254:
1.32 matthew 1255: sub csv_format_item {
1256: my ($item,$type) = @_;
1257: if ($type eq 'Time') {
1258: $item = localtime($item);
1.33 matthew 1259: }
1.32 matthew 1260: $item =&Apache::loncommon::csv_translate($item);
1261: return $item;
1.15 matthew 1262: }
1263:
1.1 matthew 1264: #########################################################
1265: #########################################################
1266: ##
1267: ## Generic Interface Routines
1268: ##
1269: #########################################################
1270: #########################################################
1271: sub CreateInterface {
1272: ##
1.16 matthew 1273: ## Output Selection
1.19 matthew 1274: my $output_selector = $/.'<select name="output">'.$/;
1.22 matthew 1275: foreach ('HTML','Excel','CSV') {
1.19 matthew 1276: $output_selector .= ' <option value="'.lc($_).'"';
1.40 albertel 1277: if ($env{'form.output'} eq lc($_)) {
1.59 bisitz 1278: $output_selector .= ' selected="selected"';
1.16 matthew 1279: }
1.19 matthew 1280: $output_selector .='>'.&mt($_).'</option>'.$/;
1.16 matthew 1281: }
1.19 matthew 1282: $output_selector .= '</select>'.$/;
1.16 matthew 1283: ##
1.1 matthew 1284: ## Environment variable initialization
1285: my $Str = '';
1.43 albertel 1286: $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
1.59 bisitz 1287: $Str .= '<br />';
1.50 bisitz 1288: $Str .= &Apache::loncommon::start_data_table();
1289: $Str .= &Apache::loncommon::start_data_table_header_row();
1.16 matthew 1290: $Str .= '<th>'.&mt('Sections').'</th>';
1.44 raeburn 1291: $Str .= '<th>'.&mt('Groups').'</th>';
1.57 onken 1292: $Str .= '<th>'.&mt('Student Data').&Apache::loncommon::help_open_topic("Chart_Student_Data").'</th>';
1.46 raeburn 1293: $Str .= '<th>'.&mt('Access Status').'</th>';
1.50 bisitz 1294: $Str .= '<th>'.&mt('Options').'</th>';
1295: $Str .= '<th>'.&mt('Output Format').'</th>';
1296: $Str .= &Apache::loncommon::end_data_table_header_row();
1.11 matthew 1297: #
1.50 bisitz 1298: $Str .= &Apache::loncommon::start_data_table_row();
1299: $Str .= '<td align="center">'."\n";
1.1 matthew 1300: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1301: $Str .= '</td>';
1302: #
1.44 raeburn 1303: $Str .= '<td align="center">'."\n";
1304: $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
1305: $Str .= '</td>';
1306: #
1.57 onken 1307: $Str .= '<td align="center">'."\n";
1308: $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple', 5,undef);
1309: $Str .= '</td>';
1310: #
1.1 matthew 1311: $Str .= '<td align="center">';
1312: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1313: $Str .= '</td>';
1.6 matthew 1314: #
1.15 matthew 1315: # Render problem checkbox
1316: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1.40 albertel 1317: if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1.50 bisitz 1318: $prob_checkbox .= 'checked="checked" ';
1.15 matthew 1319: }
1320: $prob_checkbox .= 'value="true" />';
1321: #
1322: # Compute correct answers checkbox
1323: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1.40 albertel 1324: if (exists($env{'form.correctans'}) && $env{'form.correctans'} eq 'true') {
1.50 bisitz 1325: $ans_checkbox .= 'checked="checked" ';
1.13 matthew 1326: }
1.15 matthew 1327: $ans_checkbox .= 'value="true" />';
1328: #
1.19 matthew 1329: # Show all submissions checkbox
1330: my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
1.40 albertel 1331: if (exists($env{'form.all_sub'}) &&
1332: $env{'form.all_sub'} eq 'true') {
1.50 bisitz 1333: $all_sub_checkbox .= 'checked="checked" ';
1.15 matthew 1334: }
1.19 matthew 1335: $all_sub_checkbox.= 'value="true" />';
1.15 matthew 1336: #
1.20 matthew 1337: # problem status checkbox
1338: my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
1.40 albertel 1339: if (exists($env{'form.prob_status'}) &&
1340: $env{'form.prob_status'} eq 'true') {
1.50 bisitz 1341: $prob_status_checkbox .= 'checked="checked" ';
1.15 matthew 1342: }
1.20 matthew 1343: $prob_status_checkbox .= 'value="true" />';
1.15 matthew 1344: #
1.56 bisitz 1345: $Str .=
1346: '<td valign="top">'
1347: .'<label>'
1348: .$prob_checkbox.&mt('Show problem')
1349: .'</label><br />'
1350: .'<label>'
1351: .' '.$ans_checkbox.&mt('Show correct answers')
1352: .'</label><br />'
1353: .'<label>'
1354: .$all_sub_checkbox.&mt('Show all submissions')
1355: .'</label><br />'
1356: .'<label>'
1357: .$prob_status_checkbox.&mt('Show problem grading')
1358: .'</label>'
1359: .'</td>';
1.13 matthew 1360: #
1.50 bisitz 1361: $Str .= '<td align="center" valign="top">'.$output_selector.'</td>';
1362: #
1363: $Str .= &Apache::loncommon::end_data_table_row();
1364: $Str .= &Apache::loncommon::end_data_table();
1.1 matthew 1365: #
1.49 bisitz 1366: $Str .= '<p><span class="LC_nobreak">'
1367: .&mt('Status: [_1]',
1368: '<input type="text" name="stats_status"'
1369: .' size="60" value="" readonly="readonly" />')
1370: .'</span></p>';
1.11 matthew 1371: ##
1.1 matthew 1372: return $Str;
1373: }
1374:
1375: 1;
1376:
1377: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>