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