File:
[LON-CAPA] /
loncom /
interface /
statistics /
lonstudentsubmissions.pm
Revision
1.54:
download - view:
text,
annotated -
select for diffs
Tue Apr 21 13:28:23 2009 UTC (15 years, 4 months ago) by
bisitz
Branches:
MAIN
CVS tags:
version_2_8_99_0,
bz5969,
bz2851,
HEAD,
GCI_3,
GCI_2,
BZ5971-printing-apage,
BZ5434-fox
Moved output options checkboxes from translation phrases to script code.
This ensures that
- the checkboxes are always properly displayed, independent from the translation phrases
- the place of the checkboxes is given by the script code to have a consistent screen layout, independent from the translation of the corresponding text
1: # The LearningOnline Network with CAPA
2: #
3: # $Id: lonstudentsubmissions.pm,v 1.54 2009/04/21 13:28:23 bisitz Exp $
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;
30: use Apache::lonnet;
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();
40: use lib '/home/httpd/lib/perl/';
41: use LONCAPA;
42:
43:
44: my @SubmitButtons = ({ name => 'SelectAnother',
45: text => 'Choose a different Problem' },
46: { name => 'Generate',
47: text => 'Generate Report'},
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) {
69: $r->print('<div class="LC_warning">'
70: .&mt('There are no students in the sections selected.')
71: .'</div>');
72: }
73: #
74: my @CacheButtonHTML =
75: &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status',
76: '<div class="LC_info">'.&mt('Loading student data...').'</div>');
77: $r->rflush();
78: #
79: if (exists($env{'form.problemchoice'}) &&
80: ! exists($env{'form.SelectAnother'})) {
81: foreach my $button (@SubmitButtons) {
82: if ($button->{'name'} eq 'break') {
83: $r->print("<br />\n");
84: } else {
85: $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
86: 'value="'.&mt($button->{'text'}).'" />');
87: $r->print(' 'x5);
88: }
89: }
90: foreach my $html (@CacheButtonHTML) {
91: $r->print($html.(' 'x5));
92: }
93: #
94: $r->print('<hr />'.$/);
95: $r->rflush();
96: #
97: # Determine which problems we are to analyze
98: my @Symbs =
99: &Apache::lonstathelpers::get_selected_symbs('problemchoice');
100: foreach my $selected (@Symbs) {
101: $r->print('<input type="hidden" name="problemchoice" value="'.
102: $selected.'" />'.$/);
103: }
104: #
105: # Get resource objects
106: my $navmap = Apache::lonnavmaps::navmap->new();
107: if (!defined($navmap)) {
108: $r->print('<div class="LC_error">'.&mt("Internal error").'</div>');
109: return;
110: }
111: my %already_seen;
112: my @Problems;
113: foreach my $symb (@Symbs) {
114: my $resource = $navmap->getBySymb($symb);
115: push(@Problems,$resource);
116: }
117: #
118: $r->print('<h4>'.
119: &Apache::lonstatistics::section_and_enrollment_description().
120: '</h4>');
121: if (! scalar(@Problems) || ! defined($Problems[0])) {
122: $r->print('resource is undefined');
123: } else {
124: if (scalar(@Problems) == 1) {
125: my $resource = $Problems[0];
126: $r->print('<h1>'.$resource->title.'</h1>');
127: $r->print('<h3>'.$resource->src.'</h3>');
128: if ($env{'form.renderprob'} eq 'true') {
129: $r->print(&Apache::lonstathelpers::render_resource($resource));
130: $r->rflush();
131: }
132: }
133: if ($env{'form.output'} eq 'excel') {
134: &prepare_excel_output($r,\@Problems,\@Students);
135: } elsif ($env{'form.output'} eq 'csv') {
136: &prepare_csv_output($r,\@Problems,\@Students);
137: } else {
138: &prepare_html_output($r,\@Problems,\@Students);
139: }
140: }
141: $r->print('<hr />');
142: } else {
143: $r->print('<input type="submit" name="Generate" value="'.
144: &mt('Prepare Report').'" />');
145: $r->print(' 'x5);
146: $r->print('<p>'.
147: &mt('Computing correct answers greatly increasese the amount of time required to prepare a report.').
148: '</p>');
149: $r->print('<p>'.
150: &mt('Please select problems and use the [_1]Prepare Report[_2] button to continue.','<b>','</b>').
151: '</p>');
152: $r->print(&Apache::lonstathelpers::MultipleProblemSelector
153: (undef,'problemchoice','Statistics'));
154: }
155: }
156:
157: ##
158: ## get_extra_response_headers
159: ##
160: sub get_extra_response_headers {
161: my @extra_resp_headers;
162: if ($env{'form.correctans'} eq 'true') {
163: push(@extra_resp_headers,'Correct');
164: }
165: if ($env{'form.prob_status'} eq 'true') {
166: push(@extra_resp_headers,'Award Detail');
167: push(@extra_resp_headers,'Time');
168: push(@extra_resp_headers,'Attempt');
169: push(@extra_resp_headers,'Awarded');
170: }
171: return @extra_resp_headers;
172: }
173:
174: ##
175: ## get_headers:
176: ## return the proper headers for the given response
177: sub get_headers {
178: my ($prob,$partid,$respid,$resptype,$analysis,$output,$purpose,
179: @basic_headers) = @_;
180: my @headers;
181: if ($resptype eq 'essay' && $purpose eq 'display' &&
182: ($output eq 'html')) {# || scalar(@{$prob->parts})!=1)) {
183: @headers = ();
184: } elsif ($resptype =~ /^(option|match|rank)$/) {
185: my $prefix = '_';
186: if ($purpose eq 'display') {
187: $prefix = '';
188: }
189: my @foils =
190: map {
191: $prefix.$_;
192: } sort(keys(%{$analysis->{$partid.'.'.$respid}->{'_Foils'}}));
193: if (scalar(@basic_headers) && $basic_headers[0] eq 'Correct') {
194: @foils = map { ($_ , $_.' Correct') } @foils;
195: shift(@basic_headers); # Get rid of 'Correct'
196: }
197: @headers = (@foils,@basic_headers);
198: } elsif (lc($resptype) eq 'task') {
199: @headers = ('Grader','Status',@basic_headers,'Submission');
200: } else {
201: @headers = ('Submission',@basic_headers);
202: }
203: return @headers;
204: }
205:
206: #########################################################
207: #########################################################
208: ##
209: ## HTML Output Routines
210: ##
211: #########################################################
212: #########################################################
213: sub prepare_html_output {
214: my ($r,$problems,$students) = @_;
215: my $c = $r->connection();
216: #
217: # Set a flag for the case when there is just one problem
218: my $single_response = 0;
219: if (scalar(@$problems) == 1 &&
220: $problems->[0]->countResponses == 1) {
221: $single_response = 1;
222: }
223: #
224: # Compute the number of columns per response
225: my @extra_resp_headers = &get_extra_response_headers();
226: #
227: # Create the table header
228: my @student_columns = ('username','domain','id','section');
229: #
230: my %headers;
231: my $student_column_count = scalar(@student_columns);
232: $headers{'problem'} = qq{<th colspan="$student_column_count">\ </th>};
233: foreach (@student_columns) {
234: $headers{'student'}.= '<th>'.ucfirst($_).'</th>';
235: }
236: #
237: # we put the headers into the %headers hash
238: my $total_col = scalar(@student_columns);
239: my $nonempty_part_headers = 0;
240: #
241: my %problem_analysis;
242: foreach my $prob (@$problems) {
243: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
244: $problem_analysis{$prob->src}=\%analysis;
245: #
246: my $prob_span = 0;
247: my $single_part = 0;
248: if (scalar(@{$prob->parts}) == 1) {
249: $single_part = 1;
250: }
251: foreach my $partid (@{$prob->parts}) {
252: my $part_span = 0;
253: my $responses = [$prob->responseIds($partid)];
254: my $resptypes = [$prob->responseType($partid)];
255: for (my $i=0;$i<scalar(@$responses);$i++) {
256: my $respid = $responses->[$i];
257: my @headers = &get_headers($prob,$partid,$respid,
258: $resptypes->[$i],
259: $problem_analysis{$prob->src},
260: 'html','display',
261: @extra_resp_headers);
262: if (scalar(@headers)>0) {
263: $total_col += scalar(@headers);
264: $part_span += scalar(@headers);
265: $headers{'response'} .=
266: '<th colspan="'.scalar(@headers).'">'.
267: &mt('Response [_1]',$responses->[$i]).'</th>';
268: $headers{'student'}.= '<th><span class="LC_nobreak">'.
269: join('</span></th><th><span class="LC_nobreak">',
270: @headers).
271: '</span></th>';
272: }
273: }
274: if ($part_span == 0) {
275: next;
276: }
277: if (! $single_part) {
278: my $tmpname = $partid;
279: if ($partid =~/^\d+$/) {
280: $tmpname = $prob->part_display($partid);
281: }
282: if ($tmpname !~ /^part/) {
283: $tmpname = 'Part '.$tmpname;
284: }
285: $headers{'part'} .= qq{<th colspan="$part_span">$tmpname</th>};
286: $nonempty_part_headers = 1;
287: } else {
288: $headers{'part'} .= qq{<th colspan="$part_span"> </th>};
289: }
290: $prob_span += $part_span;
291: }
292: my $title = $prob->compTitle;
293: if ($prob_span > 0) {
294: $headers{'problem'}.= qq{<th colspan="$prob_span">$title</th>};
295: } elsif ($single_response) {
296: $prob_span = scalar(@student_columns);
297: $headers{'problem'} = qq{<th colspan="$prob_span">$title</th>};
298: }
299: }
300: if (exists($headers{'part'})) {
301: $headers{'part'} = qq{<th colspan="$student_column_count">\ </th>}.
302: $headers{'part'};
303: }
304: if (exists($headers{'response'})) {
305: $headers{'response'}=
306: qq{<th colspan="$student_column_count">\ </th>}.
307: $headers{'response'};
308: }
309: my $full_header = $/.'<table>'.$/;
310: $full_header .= '<tr align="left">'.$headers{'problem'}.'</tr>'.$/;
311: if ($nonempty_part_headers) {
312: $full_header .= '<tr align="left">'.$headers{'part'}.'</tr>'.$/;
313: }
314: $full_header .= '<tr align="left">'.$headers{'response'}.'</tr>'.$/;
315: $full_header .= '<tr align="left">'.$headers{'student'}.'</tr>'.$/;
316: #
317: # Main loop
318: my $count;
319: $r->print($/.$full_header.$/);
320: my $row_class = 'odd'; # css
321: foreach my $student (@$students) {
322: my $student_row_data;
323: if ($count++ >= 30) {
324: $r->print('</table>'.$/.$full_header.$/);
325: $count = 0;
326: }
327: last if ($c->aborted());
328: foreach my $field (@student_columns) {
329: $student_row_data .=
330: '<td valign="top">'.$student->{$field}.'</td>';
331: }
332: #
333: # Figure out what it is we need to output for this student
334: my @essays;
335: my %prob_data;
336: my $maxrow;
337: foreach my $prob (@$problems) {
338: $prob_data{$prob->symb}={};
339: foreach my $partid (@{$prob->parts}) {
340: my @responses = $prob->responseIds($partid);
341: my @response_type = $prob->responseType($partid);
342: for (my $i=0;$i<=$#responses;$i++) {
343: my $respid = $responses[$i];
344: my $results =
345: &Apache::loncoursedata::get_response_data_by_student
346: ($student,$prob->symb(),$respid);
347: my $resptype = $response_type[$i];
348: my @headers = &get_headers($prob,$partid,$respid,
349: $resptype,
350: $problem_analysis{$prob->src},
351: 'html','normal',
352: @extra_resp_headers);
353: my $width = scalar(@headers);
354: next if ($width < 1);
355: my $resp_data;
356: $resp_data->{'fake'} = qq{<td colspan="$width"> </td>};
357: if (! defined($results)) {
358: $results = [];
359: }
360: #
361: if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
362: $maxrow = scalar(@$results);
363: }
364: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
365: if ($env{'form.all_sub'} ne 'true') {
366: next if ($j ne scalar(@$results)-1);
367: }
368: my $response = &hashify_response($results->[$j],
369: $prob,
370: $student,
371: $partid,
372: $respid);
373: if ($resptype eq 'essay') {
374: push(@essays,
375: &html_essay_results(\@headers,
376: $prob,$partid,$respid,
377: $response,
378: $single_response).
379: '</td>');
380: } elsif (lc($resptype) eq 'task') {
381: my $results =
382: &html_task_results(\@headers,
383: $prob,$partid,$respid,
384: $response,$resptype);
385: if ($results) {
386: push(@{$resp_data->{'real'}},$results);
387: }
388: } else {
389: push(@{$resp_data->{'real'}},
390: &html_non_essay_results(\@headers,
391: $prob,$partid,$respid,
392: $response,$resptype));
393: }
394: }
395: $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
396: } # end of $i loop
397: } # end of partid loop
398: } # end of prob loop
399: #
400: # if there is no data, skip this student.
401: next if (! $maxrow && ! scalar(@essays));
402: #
403: # Go through the problem data and output a row.
404: if ($row_class eq 'even') {
405: $row_class = 'odd';
406: } else {
407: $row_class = 'even';
408: }
409: my $printed_something;
410: for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
411: my $html;
412: my $no_data = 1;
413: foreach my $prob (@$problems) {
414: foreach my $partid (@{$prob->parts}) {
415: my @responses = $prob->responseIds($partid);
416: my @response_type = $prob->responseType($partid);
417: for (my $i=0;$i<=$#responses;$i++) {
418: my $respid = $responses[$i];
419: my $resp_data =
420: $prob_data{$prob->symb}->{$partid}->{$respid};
421: next if ($response_type[$i] eq 'essay');
422: if (defined($resp_data->{'real'}->[$rows_output])) {
423: $html .= $resp_data->{'real'}->[$rows_output];
424: $no_data = 0;
425: } else {
426: $html .= $resp_data->{'fake'};
427: }
428: }
429: }
430: }
431: if (! $no_data) {
432: $r->print(qq{<tr class="$row_class">$student_row_data$html</tr>}.$/);
433: $printed_something=1;
434: }
435: }
436: if (@essays) {
437: my $tr = qq{<tr class="$row_class">};
438: my $td = qq{<td valign="top" class="essay" colspan="$total_col">};
439: if (! $printed_something) {
440: $r->print($tr.$student_row_data.'</tr>'.$/);
441: }
442: $r->print($tr.$td.
443: join('</td></tr>'.$/.$tr.$td,@essays).'</td></tr>'.$/);
444: undef(@essays);
445: }
446: } # end of student loop
447: return;
448: }
449:
450: sub hashify_response {
451: my ($response,$prob,$student,$partid,$respid) =@_;
452: my $resp_hash = {};
453: if ($env{'form.correctans'} eq 'true') {
454: $resp_hash->{'Correct'} =
455: &Apache::lonstathelpers::get_student_answer
456: ($prob,$student->{'username'},$student->{'domain'},
457: $partid,$respid);
458: }
459: $resp_hash->{'Submission'} =
460: $response->[&Apache::loncoursedata::RDs_submission()];
461: $resp_hash->{'Time'} =
462: $response->[&Apache::loncoursedata::RDs_timestamp()];
463: $resp_hash->{'Attempt'} =
464: $response->[&Apache::loncoursedata::RDs_tries()];
465: $resp_hash->{'Awarded'} =
466: $response->[&Apache::loncoursedata::RDs_awarded()];
467: if ($prob->is_task()) {
468: $resp_hash->{'Grader'} =
469: $response->[&Apache::loncoursedata::RDs_response_eval_2()];
470: if ($resp_hash->{'Attempt'} eq '0') {
471: $resp_hash->{'Attempt'} = '';
472: }
473: $resp_hash->{'Award Detail'} =
474: $response->[&Apache::loncoursedata::RDs_part_award()];
475: $resp_hash->{'Status'} =
476: $response->[&Apache::loncoursedata::RDs_response_eval()];
477: } else {
478: $resp_hash->{'Award Detail'} =
479: $response->[&Apache::loncoursedata::RDs_awarddetail()];
480: }
481:
482: return $resp_hash;
483: }
484:
485: #####################################################
486: ##
487: ## HTML helper routines
488: ##
489: #####################################################
490: sub html_essay_results {
491: my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
492: if (! ref($headers) || ref($headers) ne 'ARRAY') {
493: return '';
494: }
495: # Start of telling them what problem, part, and response
496: my $Str;
497: if (! $single_response) {
498: my $id = $prob->compTitle;
499: if (defined($partid) && $partid ne '0') {
500: $id .= ' '.$prob->part_display($partid);
501: }
502: if (defined($respid)) {
503: $id .= ' '.$respid;
504: }
505: $Str .= '<span class="LC_nobreak">'.$id.'</span>'.(' 'x4);
506: }
507: #
508: shift(@$headers); # Get rid of the Submission header
509: my $correct = '';
510: if ($headers->[0] eq 'Correct') {
511: $correct = &html_format_essay_sub($response->{'Correct'});
512: shift(@$headers);
513: }
514: $Str .= '<span class="LC_nobreak">'.
515: join('',
516: map {
517: (' 'x4).&mt($_.': [_1]',$response->{$_});
518: } @$headers).'</span>';
519: if (@$headers || ! $single_response) {
520: $Str .= '<br />';
521: }
522: $Str .= &html_format_essay_sub($response->{'Submission'});
523: #
524: if (defined($correct) && $correct !~ /^\s*$/) {
525: $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
526: }
527: return $Str;
528: }
529:
530: sub html_format_essay_sub {
531: my ($submission) = @_;
532: return '' if (! defined($submission) || $submission eq '');
533: $submission = &HTML::Entities::decode($submission);
534: $submission =~ s/\\\"/\"/g;
535: $submission =~ s/\\\'/\'/g;
536: $submission =~ s|\\r\\n|$/|g;
537: $submission = &HTML::Entities::encode($submission,'<>&"');
538: $submission =~ s|$/\s*$/|$/</p><p>$/|g;
539: $submission =~ s|\\||g;
540: $submission = '<p>'.$submission.'</p>';
541: return $submission;
542: }
543:
544: sub html_task_results {
545: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
546: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
547: return '';
548: }
549:
550: my @values;
551: @values = map { $response->{$_}; } @$headers;
552:
553: my $td = '<td valign="top">';
554: my $str = $td.join('</td>'.$td,@values).'</td>';
555: return $str;
556: }
557:
558: sub html_non_essay_results {
559: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
560: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
561: return '';
562: }
563: #
564: my $submission = &HTML::Entities::decode(&unescape($response->{'Submission'}));
565: return '' if (! defined($submission) || $submission eq '');
566: $submission =~ s/\\\"/\"/g;
567: $submission =~ s/\\\'/\'/g;
568: if ($resptype eq 'radiobutton') {
569: $submission = &HTML::Entities::encode($submission,'<>&"');
570: $submission =~ s/=([^=])$//;
571: $submission = '<span class="LC_nobreak">'.$submission.'</span>';
572: }
573: $response->{'Submission'} = $submission;
574: #
575: my @values;
576: if ($resptype =~ /^(option|match|rank)$/) {
577: my %submission =
578: map {
579: my ($foil,$value) = split('=',&unescape($_));
580: ($foil,$value);
581: } split('&',$response->{'Submission'});
582: my %correct;
583: if (exists($response->{'Correct'})) {
584: %correct =
585: map {
586: my ($foil,$value)=split('=',&unescape($_));
587: ($foil,$value);
588: } split('&',$response->{'Correct'});
589: }
590: #
591: foreach my $original_header (@$headers) {
592: if ($original_header =~ /^_/) {
593: # '_' denotes a foil column
594: my ($header) = ($original_header =~ m/^_(.*)$/);
595: my $option = '';
596: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
597: if (exists($correct{$foil})) {
598: $option = $correct{$foil};
599: }
600: } elsif (exists($submission{$header})) {
601: $option = $submission{$header};
602: }
603: push(@values,&HTML::Entities::encode($option));
604: } elsif ($original_header eq 'Time') {
605: push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
606: } else {
607: # A normal column
608: push(@values,$response->{$original_header});
609: }
610: }
611: } else {
612: @values = map { $response->{$_}; } @$headers;
613: }
614: my $td = '<td valign="top">';
615: my $str = $td.join('</td>'.$td,@values).'</td>';
616: return $str;
617: }
618:
619:
620: #########################################################
621: #########################################################
622: ##
623: ## Excel Output Routines
624: ##
625: #########################################################
626: #########################################################
627: sub prepare_excel_output {
628: my ($r,$Problems,$Students) = @_;
629: my $c = $r->connection();
630: #
631: #
632: # Determine the number of columns in the spreadsheet
633: my $columncount = 3; # username, domain, id
634: my @extra_resp_headers = &get_extra_response_headers();
635: my $lastprob;
636: my %problem_analysis;
637: foreach my $prob (@$Problems) {
638: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
639: $problem_analysis{$prob->src}=\%analysis;
640: foreach my $partid (@{$prob->parts}) {
641: my $responses = [$prob->responseIds($partid)];
642: my $resptypes = [$prob->responseType($partid)];
643: for (my $i=0;$i<scalar(@$responses);$i++) {
644: my @headers = &get_headers($prob,$partid,$responses->[$i],
645: $resptypes->[$i],
646: $problem_analysis{$prob->src},
647: 'excel','display',
648: @extra_resp_headers);
649: $columncount += scalar(@headers);
650: }
651: }
652: last if ($columncount > 255);
653: $lastprob = $prob;
654: }
655: if ($columncount > 255) {
656: $r->print('<h1>'.&mt('Unable to complete request').'</h1>'.$/.
657: '<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>'.$/.
658: '<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>'.$/.
659: '<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
660: $r->rflush();
661: return;
662: }
663: #
664: # Print out a message telling them what we are doing
665: if (scalar(@$Problems) > 1) {
666: $r->print('<h2>'.
667: &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
668: scalar(@$Problems)).
669: '</h2>');
670: } else {
671: $r->print('<h2>'.
672: &mt('Preparing Excel spreadsheet of student responses').
673: '</h2>');
674: }
675: $r->rflush();
676: #
677: # Create the excel spreadsheet
678: my ($workbook,$filename,$format) =
679: &Apache::loncommon::create_workbook($r);
680: return if (! defined($workbook));
681: my $worksheet = $workbook->addworksheet('Student Submission Data');
682: #
683: # Add headers to the worksheet
684: my $rows_output = 0;
685: $worksheet->write($rows_output++,0,
686: $env{'course.'.$env{'request.course.id'}.'.description'},
687: $format->{'h1'});
688: $rows_output++;
689: my $cols_output = 0;
690: my $title_row = $rows_output++;
691: my $partid_row = $rows_output++;
692: my $respid_row = $rows_output++;
693: my $header_row = $rows_output++;
694: $worksheet->write($title_row ,0,'Problem Title',$format->{'bold'});
695: $worksheet->write($partid_row,0,'Part ID',$format->{'bold'});
696: $worksheet->write($respid_row,0,'Response ID',$format->{'bold'});
697: # Student headers
698: my @StudentColumns = ('username','domain','id','section');
699: foreach (@StudentColumns) {
700: $worksheet->write($header_row,$cols_output++,ucfirst($_),
701: $format->{'bold'});
702: }
703: # Problem headers
704: my %start_col;
705: foreach my $prob (@$Problems) {
706: my $title = $prob->compTitle;
707: $worksheet->write($title_row,$cols_output,
708: $title,$format->{'h3'});
709: foreach my $partid (@{$prob->parts}) {
710: $worksheet->write($partid_row,$cols_output,
711: $prob->part_display($partid));
712: my $responses = [$prob->responseIds($partid)];
713: my $resptypes = [$prob->responseType($partid)];
714: for (my $i=0;$i<scalar(@$responses);$i++) {
715: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
716: $cols_output;
717: $worksheet->write($respid_row,$cols_output,
718: $resptypes->[$i].', '.$responses->[$i]);
719: my @headers = &get_headers($prob,$partid,$responses->[$i],
720: $resptypes->[$i],
721: $problem_analysis{$prob->src},
722: 'excel','display',
723: @extra_resp_headers);
724: foreach my $text (@headers) {
725: if ($text eq 'Time') {
726: $worksheet->set_column($cols_output,$cols_output,undef,
727: $format->{'date'});
728: }
729: $worksheet->write($header_row,$cols_output++,$text);
730: }
731: }
732: }
733: }
734: #
735: # Populate the worksheet with the student data
736: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
737: ($r,'Excel File Compilation Status',
738: 'Excel File Compilation Progress',
739: scalar(@$Students),'inline',undef,'Statistics','stats_status');
740: my $max_row = $rows_output;
741: foreach my $student (@$Students) {
742: last if ($c->aborted());
743: $cols_output = 0;
744: my $student_row = $max_row;
745: foreach my $field (@StudentColumns) {
746: $worksheet->write($student_row,$cols_output++,
747: $student->{$field});
748: }
749: my $last_student_col = $cols_output-1;
750: foreach my $prob (@$Problems) {
751: foreach my $partid (@{$prob->parts}) {
752: my @Response = $prob->responseIds($partid);
753: my @ResponseType = $prob->responseType($partid);
754: for (my $i=0;$i<=$#Response;$i++) {
755: my $respid = $Response[$i];
756: my $resptype = $ResponseType[$i];
757: my $results =
758: &Apache::loncoursedata::get_response_data_by_student
759: ($student,$prob->symb(),$respid);
760: my @headers = &get_headers($prob,$partid,$respid,
761: $resptype,
762: $problem_analysis{$prob->src},
763: 'excel','normal',
764: @extra_resp_headers);
765:
766: if (! defined($results)) {
767: $results = [];
768: }
769: #
770: $rows_output = $student_row;
771: #
772: my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
773: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
774: $cols_output = $response_start_col;
775: if ($env{'form.all_sub'} ne 'true') {
776: next if ($j ne scalar(@$results)-1);
777: }
778: my $response = &hashify_response($results->[$j],
779: $prob,
780: $student,
781: $partid,
782: $respid);
783: my @response_data =
784: &compile_response_data(\@headers,$response,
785: $prob,$partid,$respid,
786: $resptype,
787: \&excel_format_item);
788: $worksheet->write_row($rows_output++,$cols_output,
789: \@response_data);
790: $cols_output+=scalar(@response_data);
791: if ($rows_output > $max_row) {
792: $max_row = $rows_output;
793: }
794: }
795: }
796: }
797: }
798: # Fill in the remaining rows with the students data
799: for (my $row = $student_row+1;$row<$max_row;$row++) {
800: my $cols = 0;
801: foreach my $field (@StudentColumns) {
802: $worksheet->write($row,$cols++,
803: $student->{$field});
804: }
805: }
806: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
807: 'last student');
808: }
809: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
810: #
811: # Close the excel file
812: $workbook->close();
813: #
814: # Write a link to allow them to download it
815: $r->print('<p><a href="'.$filename.'">'.
816: &mt('Your Excel spreadsheet.').
817: '</a></p>'."\n");
818: $r->print('<script>'.
819: 'window.document.Statistics.stats_status.value="'.
820: 'Done compiling spreadsheet. See link below to download.'.
821: '";</script>');
822: $r->rflush();
823: return;
824: }
825:
826: sub compile_response_data {
827: my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
828: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
829: return ();
830: }
831: if (ref($format) ne 'CODE') {
832: $format = sub { return $_[0]; };
833: }
834: #
835: my $submission =
836: &HTML::Entities::decode
837: (&unescape($response->{'Submission'}));
838: if (!$prob->is_task()) {
839: return () if (! defined($submission) || $submission eq '');
840: }
841: $submission =~ s/\\\"/\"/g;
842: $submission =~ s/\\\'/\'/g;
843: if ($resptype eq 'radiobutton') {
844: $submission =~ s/=([^=])$//;
845: }
846: $response->{'Submission'} = $submission;
847: #
848: my @values;
849: if ($resptype =~ /^(option|match|rank)$/) {
850: my %submission =
851: map {
852: my ($foil,$value) = split('=',&unescape($_));
853: ($foil,$value);
854: } split('&',$response->{'Submission'});
855: my %correct;
856: if (exists($response->{'Correct'})) {
857: %correct =
858: map {
859: my ($foil,$value)=split('=',&unescape($_));
860: ($foil,$value);
861: } split('&',$response->{'Correct'});
862: }
863: #
864: foreach my $original_header (@$headers) {
865: if ($original_header =~ /^_/) {
866: # '_' denotes a foil column
867: my ($header) = ($original_header =~ m/^_(.*)$/);
868: my $option = '';
869: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
870: if (exists($correct{$foil})) {
871: $option = $correct{$foil};
872: }
873: } elsif (exists($submission{$header})) {
874: $option = $submission{$header};
875: }
876: push(@values,&{$format}($option,$header));
877: } else {
878: # A normal column
879: push(@values,&{$format}($response->{$original_header},
880: $original_header));
881: }
882: }
883: } else {
884: @values = map { &{$format}($response->{$_},$_); } @$headers;
885: }
886: return @values;
887: }
888:
889: sub excel_format_item {
890: my ($item,$type) = @_;
891: if ($type eq 'Time') {
892: $item = &Apache::lonstathelpers::calc_serial($item);
893: } else {
894: if ($item =~ m/^=/) {
895: $item = ' '.$item;
896: }
897: $item =~ s/\\r//g;
898: $item =~ s/\\n/\n/g;
899: $item =~ s/(\s*$|^\s*)//g;
900: $item =~ s/\\\'/\'/g;
901: }
902: return $item;
903: }
904:
905: #########################################################
906: #########################################################
907: ##
908: ## CSV output of student answers
909: ##
910: #########################################################
911: #########################################################
912: sub prepare_csv_output {
913: my ($r,$problems,$students) = @_;
914: my $c = $r->connection();
915: #
916: $r->print('<h2>'.
917: &mt('Generating CSV report of student responses').'</h2>');
918: #
919: # Progress window
920: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
921: ($r,'CSV File Compilation Status',
922: 'CSV File Compilation Progress',
923: scalar(@$students),'inline',undef,'Statistics','stats_status');
924:
925: $r->rflush();
926: #
927: # Open a file
928: my $outputfile;
929: my $filename = '/prtspool/'.
930: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
931: time.'_'.rand(1000000000).'.csv';
932: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
933: $r->log_error("Couldn't open $filename for output $!");
934: $r->print('<div class="LC_error">'
935: .&mt('Problems occurred in writing the CSV file. '
936: .'This error has been logged. '
937: .'Please alert your LON-CAPA administrator.')
938: .'</div>');
939: $outputfile = undef;
940: }
941: #
942: # Compute the number of columns per response
943: my @extra_resp_headers = &get_extra_response_headers();
944: #
945: # Create the table header
946: my @student_columns = ('username','domain','id','section');
947: #
948: my %headers;
949: push(@{$headers{'student'}},@student_columns);
950: # Pad for the student data
951: foreach my $row ('problem','part','response') {
952: $headers{$row}=[map {''} @student_columns];
953: }
954: #
955: # we put the headers into the %headers hash
956: my %problem_analysis;
957: my %start_col;
958: my $max_column = scalar(@student_columns);
959: foreach my $prob (@$problems) {
960: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
961: $problem_analysis{$prob->src}=\%analysis;
962: $headers{'problem'}->[$max_column] = $prob->compTitle;
963: foreach my $partid (@{$prob->parts}) {
964: $headers{'part'}->[$max_column] = $prob->part_display($partid);
965: my $responses = [$prob->responseIds($partid)];
966: my $resptypes = [$prob->responseType($partid)];
967: for (my $i=0;$i<scalar(@$responses);$i++) {
968: my @headers = &get_headers($prob,$partid,$responses->[$i],
969: $resptypes->[$i],
970: $problem_analysis{$prob->src},
971: 'csv','display',
972: @extra_resp_headers);
973: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
974: $max_column;
975: $headers{'response'}->[$max_column]=
976: &mt('Response [_1]',$responses->[$i]);
977: for (my $j=0;$j<=$#headers;$j++) {
978: $headers{'student'}->[$max_column+$j]=$headers[$j];
979: }
980: $max_column += scalar(@headers);
981: }
982: }
983: }
984: foreach my $row ('problem','part','response','student') {
985: print $outputfile '"'.
986: join('","',
987: map {
988: &Apache::loncommon::csv_translate($_);
989: } @{$headers{$row}}).'"'.$/;
990: }
991: #
992: # Main loop
993: foreach my $student (@$students) {
994: last if ($c->aborted());
995: my @rows;
996: foreach my $prob (@$problems) {
997: foreach my $partid (@{$prob->parts}) {
998: my @responses = $prob->responseIds($partid);
999: my @response_type = $prob->responseType($partid);
1000: for (my $i=0;$i<=$#responses;$i++) {
1001: my $respid = $responses[$i];
1002: my $resptype = $response_type[$i];
1003: my @headers = &get_headers($prob,$partid,$respid,$resptype,
1004: $problem_analysis{$prob->src},
1005: 'csv','normal',
1006: @extra_resp_headers);
1007: my $results =
1008: &Apache::loncoursedata::get_response_data_by_student
1009: ($student,$prob->symb(),$respid);
1010: if (! defined($results)) {
1011: $results = [];
1012: }
1013: for (my $j=0; $j<scalar(@$results);$j++) {
1014: if ($env{'form.all_sub'} ne 'true') {
1015: next if ($j != 0);
1016: }
1017: my $idx = scalar(@$results) - $j - 1;
1018: my $response = &hashify_response($results->[$idx],
1019: $prob,$student,
1020: $partid,$respid);
1021: my @data = &compile_response_data(\@headers,$response,
1022: $prob,$partid,
1023: $respid,$resptype,
1024: \&csv_format_item);
1025: my $resp_start_idx =
1026: $start_col{$prob->symb}->{$partid}->{$respid};
1027: for (my $k=0;$k<=$#data;$k++) {
1028: $rows[$j]->[$resp_start_idx + $k] = $data[$k];
1029: }
1030: }
1031: }
1032: }
1033: }
1034: foreach my $row (@rows) {
1035: print $outputfile '"'.join('","',
1036: map { $student->{$_}; }
1037: @student_columns).'"';
1038: for (my $i=scalar(@student_columns);$i<$max_column;$i++) {
1039: my $value = &Apache::loncommon::csv_translate($row->[$i]);
1040: $value ||='';
1041: print $outputfile ',"'.$value.'"';
1042: }
1043: print $outputfile $/;
1044: }
1045: undef(@rows);
1046: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1047: 'last student');
1048: }
1049: close($outputfile);
1050: #
1051: # Close the progress window
1052: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1053: #
1054: # Tell the user where to get their CSV file
1055: $r->print('<br />'.
1056: '<a href="'.$filename.'">'.&mt('Your CSV file.').'</a>'."\n");
1057: $r->rflush();
1058: return;
1059: }
1060:
1061: sub csv_format_item {
1062: my ($item,$type) = @_;
1063: if ($type eq 'Time') {
1064: $item = localtime($item);
1065: }
1066: $item =&Apache::loncommon::csv_translate($item);
1067: return $item;
1068: }
1069:
1070: #########################################################
1071: #########################################################
1072: ##
1073: ## Generic Interface Routines
1074: ##
1075: #########################################################
1076: #########################################################
1077: sub CreateInterface {
1078: ##
1079: ## Output Selection
1080: my $output_selector = $/.'<select name="output">'.$/;
1081: foreach ('HTML','Excel','CSV') {
1082: $output_selector .= ' <option value="'.lc($_).'"';
1083: if ($env{'form.output'} eq lc($_)) {
1084: $output_selector .= ' selected ';
1085: }
1086: $output_selector .='>'.&mt($_).'</option>'.$/;
1087: }
1088: $output_selector .= '</select>'.$/;
1089: ##
1090: ## Environment variable initialization
1091: my $Str = '';
1092: $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
1093: $Str .= '<p>';
1094: $Str .= &Apache::loncommon::start_data_table();
1095: $Str .= &Apache::loncommon::start_data_table_header_row();
1096: $Str .= '<th>'.&mt('Sections').'</th>';
1097: $Str .= '<th>'.&mt('Groups').'</th>';
1098: $Str .= '<th>'.&mt('Access Status').'</th>';
1099: $Str .= '<th>'.&mt('Options').'</th>';
1100: $Str .= '<th>'.&mt('Output Format').'</th>';
1101: $Str .= &Apache::loncommon::end_data_table_header_row();
1102: #
1103: $Str .= &Apache::loncommon::start_data_table_row();
1104: $Str .= '<td align="center">'."\n";
1105: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1106: $Str .= '</td>';
1107: #
1108: $Str .= '<td align="center">'."\n";
1109: $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
1110: $Str .= '</td>';
1111: #
1112: $Str .= '<td align="center">';
1113: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1114: $Str .= '</td>';
1115: #
1116: # Render problem checkbox
1117: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1118: if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1119: $prob_checkbox .= 'checked="checked" ';
1120: }
1121: $prob_checkbox .= 'value="true" />';
1122: #
1123: # Compute correct answers checkbox
1124: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1125: if (exists($env{'form.correctans'}) && $env{'form.correctans'} eq 'true') {
1126: $ans_checkbox .= 'checked="checked" ';
1127: }
1128: $ans_checkbox .= 'value="true" />';
1129: #
1130: # Show all submissions checkbox
1131: my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
1132: if (exists($env{'form.all_sub'}) &&
1133: $env{'form.all_sub'} eq 'true') {
1134: $all_sub_checkbox .= 'checked="checked" ';
1135: }
1136: $all_sub_checkbox.= 'value="true" />';
1137: #
1138: # problem status checkbox
1139: my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
1140: if (exists($env{'form.prob_status'}) &&
1141: $env{'form.prob_status'} eq 'true') {
1142: $prob_status_checkbox .= 'checked="checked" ';
1143: }
1144: $prob_status_checkbox .= 'value="true" />';
1145: #
1146: $Str .= '<td align="right" valign="top">'.
1147: '<label><b>'.
1148: &mt('Show problem').' '.$prob_checkbox.'</b></label><br />'.
1149: '<label><b>'.
1150: &mt('Show correct answers').' '.$ans_checkbox.'</b></label><br />'.
1151: '<label><b>'.
1152: &mt('Show all submissions').' '.$all_sub_checkbox.
1153: '</b></label><br />'.
1154: '<label><b>'.
1155: &mt('Show problem grading').' '.$prob_status_checkbox.
1156: '</b></label><br />'.
1157: '</td>';
1158: #
1159: $Str .= '<td align="center" valign="top">'.$output_selector.'</td>';
1160: #
1161: $Str .= &Apache::loncommon::end_data_table_row();
1162: $Str .= &Apache::loncommon::end_data_table();
1163: #
1164: $Str .= '<p><span class="LC_nobreak">'
1165: .&mt('Status: [_1]',
1166: '<input type="text" name="stats_status"'
1167: .' size="60" value="" readonly="readonly" />')
1168: .'</span></p>';
1169: $Str .= '</p>';
1170: ##
1171: return $Str;
1172: }
1173:
1174: 1;
1175:
1176: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>