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