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