Annotation of loncom/interface/statistics/lonstudentsubmissions.pm, revision 1.63.2.1
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.63.2.1! raeburn 3: # $Id: lonstudentsubmissions.pm,v 1.63 2011/09/19 04:02:20 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.63 raeburn 898: $cols_output ++;
1.18 matthew 899: }
900: foreach my $prob (@$Problems) {
1.60 raeburn 901: my $symb = $prob->symb();
1.18 matthew 902: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 903: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
904: next if ($show_named);
905: } else {
906: next unless ($show_named);
907: }
1.18 matthew 908: my @Response = $prob->responseIds($partid);
909: my @ResponseType = $prob->responseType($partid);
910: for (my $i=0;$i<=$#Response;$i++) {
911: my $respid = $Response[$i];
912: my $resptype = $ResponseType[$i];
913: my $results =
914: &Apache::loncoursedata::get_response_data_by_student
915: ($student,$prob->symb(),$respid);
1.31 matthew 916: my @headers = &get_headers($prob,$partid,$respid,
917: $resptype,
918: $problem_analysis{$prob->src},
919: 'excel','normal',
920: @extra_resp_headers);
921:
1.20 matthew 922: if (! defined($results)) {
923: $results = [];
924: }
925: #
926: $rows_output = $student_row;
927: #
1.31 matthew 928: my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
1.20 matthew 929: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
930: $cols_output = $response_start_col;
1.40 albertel 931: if ($env{'form.all_sub'} ne 'true') {
1.20 matthew 932: next if ($j ne scalar(@$results)-1);
933: }
1.31 matthew 934: my $response = &hashify_response($results->[$j],
935: $prob,
936: $student,
937: $partid,
938: $respid);
939: my @response_data =
1.33 matthew 940: &compile_response_data(\@headers,$response,
941: $prob,$partid,$respid,
942: $resptype,
943: \&excel_format_item);
1.31 matthew 944: $worksheet->write_row($rows_output++,$cols_output,
945: \@response_data);
946: $cols_output+=scalar(@response_data);
1.20 matthew 947: if ($rows_output > $max_row) {
948: $max_row = $rows_output;
949: }
1.19 matthew 950: }
1.18 matthew 951: }
952: }
953: }
1.63 raeburn 954: # Prepend current student's user information to all rows
955: for (my $row = $student_row;$row<$max_row;$row++) {
1.28 matthew 956: my $cols = 0;
957: foreach my $field (@StudentColumns) {
1.60 raeburn 958: if ($show_named) {
959: $worksheet->write($row,$cols++,
960: $student->{$field});
961: } else {
962: $worksheet->write($row,$cols++,
963: &mt('Anonymized'));
964: }
1.28 matthew 965: }
966: }
1.18 matthew 967: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
968: 'last student');
969: }
970: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
971: #
972: # Close the excel file
973: $workbook->close();
974: #
975: # Write a link to allow them to download it
976: $r->print('<p><a href="'.$filename.'">'.
977: &mt('Your Excel spreadsheet.').
978: '</a></p>'."\n");
979: $r->print('<script>'.
980: 'window.document.Statistics.stats_status.value="'.
981: 'Done compiling spreadsheet. See link below to download.'.
982: '";</script>');
983: $r->rflush();
984: return;
985: }
986:
1.33 matthew 987: sub compile_response_data {
988: my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
1.31 matthew 989: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
990: return ();
991: }
1.33 matthew 992: if (ref($format) ne 'CODE') {
993: $format = sub { return $_[0]; };
994: }
1.31 matthew 995: #
1.33 matthew 996: my $submission =
997: &HTML::Entities::decode
1.45 www 998: (&unescape($response->{'Submission'}));
1.42 albertel 999: if (!$prob->is_task()) {
1000: return () if (! defined($submission) || $submission eq '');
1001: }
1.31 matthew 1002: $submission =~ s/\\\"/\"/g;
1003: $submission =~ s/\\\'/\'/g;
1004: if ($resptype eq 'radiobutton') {
1005: $submission =~ s/=([^=])$//;
1.20 matthew 1006: }
1.31 matthew 1007: $response->{'Submission'} = $submission;
1008: #
1009: my @values;
1010: if ($resptype =~ /^(option|match|rank)$/) {
1011: my %submission =
1012: map {
1.45 www 1013: my ($foil,$value) = split('=',&unescape($_));
1.31 matthew 1014: ($foil,$value);
1015: } split('&',$response->{'Submission'});
1016: my %correct;
1017: if (exists($response->{'Correct'})) {
1018: %correct =
1019: map {
1.45 www 1020: my ($foil,$value)=split('=',&unescape($_));
1.31 matthew 1021: ($foil,$value);
1022: } split('&',$response->{'Correct'});
1023: }
1024: #
1025: foreach my $original_header (@$headers) {
1026: if ($original_header =~ /^_/) {
1027: # '_' denotes a foil column
1028: my ($header) = ($original_header =~ m/^_(.*)$/);
1029: my $option = '';
1030: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
1031: if (exists($correct{$foil})) {
1032: $option = $correct{$foil};
1033: }
1034: } elsif (exists($submission{$header})) {
1035: $option = $submission{$header};
1036: }
1.33 matthew 1037: push(@values,&{$format}($option,$header));
1.31 matthew 1038: } else {
1039: # A normal column
1.33 matthew 1040: push(@values,&{$format}($response->{$original_header},
1.31 matthew 1041: $original_header));
1042: }
1043: }
1044: } else {
1.33 matthew 1045: @values = map { &{$format}($response->{$_},$_); } @$headers;
1.20 matthew 1046: }
1.31 matthew 1047: return @values;
1.20 matthew 1048: }
1049:
1.31 matthew 1050: sub excel_format_item {
1051: my ($item,$type) = @_;
1052: if ($type eq 'Time') {
1.33 matthew 1053: $item = &Apache::lonstathelpers::calc_serial($item);
1.31 matthew 1054: } else {
1055: if ($item =~ m/^=/) {
1056: $item = ' '.$item;
1057: }
1058: $item =~ s/\\r//g;
1059: $item =~ s/\\n/\n/g;
1060: $item =~ s/(\s*$|^\s*)//g;
1061: $item =~ s/\\\'/\'/g;
1.18 matthew 1062: }
1.31 matthew 1063: return $item;
1.1 matthew 1064: }
1065:
1066: #########################################################
1067: #########################################################
1.17 matthew 1068: ##
1069: ## CSV output of student answers
1070: ##
1071: #########################################################
1072: #########################################################
1073: sub prepare_csv_output {
1.60 raeburn 1074: my ($r,$problems,$students,$anoncounter,$show_named) = @_;
1.17 matthew 1075: my $c = $r->connection();
1076: #
1077: $r->print('<h2>'.
1078: &mt('Generating CSV report of student responses').'</h2>');
1079: #
1080: # Progress window
1081: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1082: ($r,'CSV File Compilation Status',
1083: 'CSV File Compilation Progress',
1.22 matthew 1084: scalar(@$students),'inline',undef,'Statistics','stats_status');
1085:
1.17 matthew 1086: $r->rflush();
1087: #
1088: # Open a file
1089: my $outputfile;
1090: my $filename = '/prtspool/'.
1.40 albertel 1091: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.17 matthew 1092: time.'_'.rand(1000000000).'.csv';
1093: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
1094: $r->log_error("Couldn't open $filename for output $!");
1.55 bisitz 1095: $r->print(
1096: '<p class="LC_error">'
1097: .&mt('Problems occurred in writing the CSV file.')
1098: .' '.&mt('This error has been logged.')
1099: .' '.&mt('Please alert your LON-CAPA administrator.')
1100: .'</p>'
1101: );
1.17 matthew 1102: $outputfile = undef;
1103: }
1104: #
1.22 matthew 1105: # Compute the number of columns per response
1.60 raeburn 1106: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.22 matthew 1107: #
1108: # Create the table header
1.38 matthew 1109: my @student_columns = ('username','domain','id','section');
1.60 raeburn 1110: if ($show_named) {
1111: @student_columns = qw(username domain id section);
1112: } else {
1113: @student_columns = qw(username);
1114: }
1115: my $student_column_count = scalar(@student_columns);
1.17 matthew 1116: #
1.22 matthew 1117: my %headers;
1118: push(@{$headers{'student'}},@student_columns);
1119: # Pad for the student data
1120: foreach my $row ('problem','part','response') {
1.32 matthew 1121: $headers{$row}=[map {''} @student_columns];
1.22 matthew 1122: }
1123: #
1124: # we put the headers into the %headers hash
1.32 matthew 1125: my %problem_analysis;
1126: my %start_col;
1127: my $max_column = scalar(@student_columns);
1.22 matthew 1128: foreach my $prob (@$problems) {
1.60 raeburn 1129: my $symb = $prob->symb();
1.32 matthew 1130: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
1131: $problem_analysis{$prob->src}=\%analysis;
1132: $headers{'problem'}->[$max_column] = $prob->compTitle;
1.22 matthew 1133: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 1134: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
1135: next if ($show_named);
1136: } else {
1137: next unless ($show_named);
1138: }
1.32 matthew 1139: $headers{'part'}->[$max_column] = $prob->part_display($partid);
1.22 matthew 1140: my $responses = [$prob->responseIds($partid)];
1.32 matthew 1141: my $resptypes = [$prob->responseType($partid)];
1.22 matthew 1142: for (my $i=0;$i<scalar(@$responses);$i++) {
1.32 matthew 1143: my @headers = &get_headers($prob,$partid,$responses->[$i],
1144: $resptypes->[$i],
1145: $problem_analysis{$prob->src},
1146: 'csv','display',
1147: @extra_resp_headers);
1148: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
1149: $max_column;
1150: $headers{'response'}->[$max_column]=
1.22 matthew 1151: &mt('Response [_1]',$responses->[$i]);
1.32 matthew 1152: for (my $j=0;$j<=$#headers;$j++) {
1153: $headers{'student'}->[$max_column+$j]=$headers[$j];
1.22 matthew 1154: }
1.32 matthew 1155: $max_column += scalar(@headers);
1.17 matthew 1156: }
1157: }
1158: }
1.22 matthew 1159: foreach my $row ('problem','part','response','student') {
1.32 matthew 1160: print $outputfile '"'.
1.22 matthew 1161: join('","',
1162: map {
1163: &Apache::loncommon::csv_translate($_);
1164: } @{$headers{$row}}).'"'.$/;
1.17 matthew 1165: }
1166: #
1.22 matthew 1167: # Main loop
1168: foreach my $student (@$students) {
1.27 matthew 1169: last if ($c->aborted());
1.22 matthew 1170: my @rows;
1171: foreach my $prob (@$problems) {
1.60 raeburn 1172: my $symb = $prob->symb;
1.22 matthew 1173: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 1174: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
1175: next if ($show_named);
1176: } else {
1177: next unless ($show_named);
1178: }
1.22 matthew 1179: my @responses = $prob->responseIds($partid);
1180: my @response_type = $prob->responseType($partid);
1181: for (my $i=0;$i<=$#responses;$i++) {
1182: my $respid = $responses[$i];
1.32 matthew 1183: my $resptype = $response_type[$i];
1184: my @headers = &get_headers($prob,$partid,$respid,$resptype,
1185: $problem_analysis{$prob->src},
1186: 'csv','normal',
1187: @extra_resp_headers);
1.22 matthew 1188: my $results =
1189: &Apache::loncoursedata::get_response_data_by_student
1190: ($student,$prob->symb(),$respid);
1191: if (! defined($results)) {
1192: $results = [];
1193: }
1194: for (my $j=0; $j<scalar(@$results);$j++) {
1.40 albertel 1195: if ($env{'form.all_sub'} ne 'true') {
1.22 matthew 1196: next if ($j != 0);
1197: }
1198: my $idx = scalar(@$results) - $j - 1;
1.32 matthew 1199: my $response = &hashify_response($results->[$idx],
1200: $prob,$student,
1201: $partid,$respid);
1202: my @data = &compile_response_data(\@headers,$response,
1.22 matthew 1203: $prob,$partid,
1.33 matthew 1204: $respid,$resptype,
1205: \&csv_format_item);
1.32 matthew 1206: my $resp_start_idx =
1207: $start_col{$prob->symb}->{$partid}->{$respid};
1.22 matthew 1208: for (my $k=0;$k<=$#data;$k++) {
1.32 matthew 1209: $rows[$j]->[$resp_start_idx + $k] = $data[$k];
1.22 matthew 1210: }
1211: }
1212: }
1.17 matthew 1213: }
1.22 matthew 1214: }
1215: foreach my $row (@rows) {
1.60 raeburn 1216: my $student_row_data = '';
1217: if ($show_named) {
1218: $student_row_data = '"'.join('","',
1219: map { $student->{$_}; }
1220: @student_columns).'"';
1221: } else {
1222: $student_row_data = '"'.&mt('Anonymized').'"';
1223: }
1224: print $outputfile $student_row_data;
1225: for (my $i=$student_column_count;$i<$max_column;$i++) {
1.22 matthew 1226: my $value = &Apache::loncommon::csv_translate($row->[$i]);
1227: $value ||='';
1228: print $outputfile ',"'.$value.'"';
1.17 matthew 1229: }
1.22 matthew 1230: print $outputfile $/;
1.17 matthew 1231: }
1.22 matthew 1232: undef(@rows);
1.17 matthew 1233: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1234: 'last student');
1235: }
1236: close($outputfile);
1237: #
1238: # Close the progress window
1239: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1240: #
1.48 bisitz 1241: # Tell the user where to get their CSV file
1.17 matthew 1242: $r->print('<br />'.
1.48 bisitz 1243: '<a href="'.$filename.'">'.&mt('Your CSV file.').'</a>'."\n");
1.17 matthew 1244: $r->rflush();
1245: return;
1246: }
1247:
1.32 matthew 1248: sub csv_format_item {
1249: my ($item,$type) = @_;
1250: if ($type eq 'Time') {
1251: $item = localtime($item);
1.33 matthew 1252: }
1.32 matthew 1253: $item =&Apache::loncommon::csv_translate($item);
1254: return $item;
1.15 matthew 1255: }
1256:
1.1 matthew 1257: #########################################################
1258: #########################################################
1259: ##
1260: ## Generic Interface Routines
1261: ##
1262: #########################################################
1263: #########################################################
1264: sub CreateInterface {
1265: ##
1.16 matthew 1266: ## Output Selection
1.19 matthew 1267: my $output_selector = $/.'<select name="output">'.$/;
1.22 matthew 1268: foreach ('HTML','Excel','CSV') {
1.19 matthew 1269: $output_selector .= ' <option value="'.lc($_).'"';
1.40 albertel 1270: if ($env{'form.output'} eq lc($_)) {
1.59 bisitz 1271: $output_selector .= ' selected="selected"';
1.16 matthew 1272: }
1.19 matthew 1273: $output_selector .='>'.&mt($_).'</option>'.$/;
1.16 matthew 1274: }
1.19 matthew 1275: $output_selector .= '</select>'.$/;
1.16 matthew 1276: ##
1.1 matthew 1277: ## Environment variable initialization
1278: my $Str = '';
1.63.2.1! raeburn 1279: $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
1.59 bisitz 1280: $Str .= '<br />';
1.50 bisitz 1281: $Str .= &Apache::loncommon::start_data_table();
1282: $Str .= &Apache::loncommon::start_data_table_header_row();
1.16 matthew 1283: $Str .= '<th>'.&mt('Sections').'</th>';
1.44 raeburn 1284: $Str .= '<th>'.&mt('Groups').'</th>';
1.57 onken 1285: $Str .= '<th>'.&mt('Student Data').&Apache::loncommon::help_open_topic("Chart_Student_Data").'</th>';
1.46 raeburn 1286: $Str .= '<th>'.&mt('Access Status').'</th>';
1.50 bisitz 1287: $Str .= '<th>'.&mt('Options').'</th>';
1288: $Str .= '<th>'.&mt('Output Format').'</th>';
1289: $Str .= &Apache::loncommon::end_data_table_header_row();
1.11 matthew 1290: #
1.50 bisitz 1291: $Str .= &Apache::loncommon::start_data_table_row();
1292: $Str .= '<td align="center">'."\n";
1.1 matthew 1293: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1294: $Str .= '</td>';
1295: #
1.44 raeburn 1296: $Str .= '<td align="center">'."\n";
1297: $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
1298: $Str .= '</td>';
1299: #
1.57 onken 1300: $Str .= '<td align="center">'."\n";
1301: $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple', 5,undef);
1302: $Str .= '</td>';
1303: #
1.1 matthew 1304: $Str .= '<td align="center">';
1305: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1306: $Str .= '</td>';
1.6 matthew 1307: #
1.15 matthew 1308: # Render problem checkbox
1309: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1.40 albertel 1310: if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1.50 bisitz 1311: $prob_checkbox .= 'checked="checked" ';
1.15 matthew 1312: }
1313: $prob_checkbox .= 'value="true" />';
1314: #
1315: # Compute correct answers checkbox
1316: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1.40 albertel 1317: if (exists($env{'form.correctans'}) && $env{'form.correctans'} eq 'true') {
1.50 bisitz 1318: $ans_checkbox .= 'checked="checked" ';
1.13 matthew 1319: }
1.15 matthew 1320: $ans_checkbox .= 'value="true" />';
1321: #
1.19 matthew 1322: # Show all submissions checkbox
1323: my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
1.40 albertel 1324: if (exists($env{'form.all_sub'}) &&
1325: $env{'form.all_sub'} eq 'true') {
1.50 bisitz 1326: $all_sub_checkbox .= 'checked="checked" ';
1.15 matthew 1327: }
1.19 matthew 1328: $all_sub_checkbox.= 'value="true" />';
1.15 matthew 1329: #
1.20 matthew 1330: # problem status checkbox
1331: my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
1.40 albertel 1332: if (exists($env{'form.prob_status'}) &&
1333: $env{'form.prob_status'} eq 'true') {
1.50 bisitz 1334: $prob_status_checkbox .= 'checked="checked" ';
1.15 matthew 1335: }
1.20 matthew 1336: $prob_status_checkbox .= 'value="true" />';
1.15 matthew 1337: #
1.56 bisitz 1338: $Str .=
1339: '<td valign="top">'
1340: .'<label>'
1341: .$prob_checkbox.&mt('Show problem')
1342: .'</label><br />'
1343: .'<label>'
1344: .' '.$ans_checkbox.&mt('Show correct answers')
1345: .'</label><br />'
1346: .'<label>'
1347: .$all_sub_checkbox.&mt('Show all submissions')
1348: .'</label><br />'
1349: .'<label>'
1350: .$prob_status_checkbox.&mt('Show problem grading')
1351: .'</label>'
1352: .'</td>';
1.13 matthew 1353: #
1.50 bisitz 1354: $Str .= '<td align="center" valign="top">'.$output_selector.'</td>';
1355: #
1356: $Str .= &Apache::loncommon::end_data_table_row();
1357: $Str .= &Apache::loncommon::end_data_table();
1.1 matthew 1358: #
1.49 bisitz 1359: $Str .= '<p><span class="LC_nobreak">'
1360: .&mt('Status: [_1]',
1361: '<input type="text" name="stats_status"'
1362: .' size="60" value="" readonly="readonly" />')
1363: .'</span></p>';
1.11 matthew 1364: ##
1.1 matthew 1365: return $Str;
1366: }
1367:
1368: 1;
1369:
1370: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>