1: # The LearningOnline Network with CAPA
2: #
3: # $Id: lonstudentsubmissions.pm,v 1.57 2010/03/26 13:25:17 onken 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="center">';
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: return;
461: }
462:
463: sub hashify_response {
464: my ($response,$prob,$student,$partid,$respid) =@_;
465: my $resp_hash = {};
466: if ($env{'form.correctans'} eq 'true') {
467: $resp_hash->{'Correct'} =
468: &Apache::lonstathelpers::get_student_answer
469: ($prob,$student->{'username'},$student->{'domain'},
470: $partid,$respid);
471: }
472: $resp_hash->{'Submission'} =
473: $response->[&Apache::loncoursedata::RDs_submission()];
474: $resp_hash->{'Time'} =
475: $response->[&Apache::loncoursedata::RDs_timestamp()];
476: $resp_hash->{'Attempt'} =
477: $response->[&Apache::loncoursedata::RDs_tries()];
478: $resp_hash->{'Awarded'} =
479: $response->[&Apache::loncoursedata::RDs_awarded()];
480: if ($prob->is_task()) {
481: $resp_hash->{'Grader'} =
482: $response->[&Apache::loncoursedata::RDs_response_eval_2()];
483: if ($resp_hash->{'Attempt'} eq '0') {
484: $resp_hash->{'Attempt'} = '';
485: }
486: $resp_hash->{'Award Detail'} =
487: $response->[&Apache::loncoursedata::RDs_part_award()];
488: $resp_hash->{'Status'} =
489: $response->[&Apache::loncoursedata::RDs_response_eval()];
490: } else {
491: $resp_hash->{'Award Detail'} =
492: $response->[&Apache::loncoursedata::RDs_awarddetail()];
493: }
494:
495: return $resp_hash;
496: }
497:
498: #####################################################
499: ##
500: ## HTML helper routines
501: ##
502: #####################################################
503: sub html_essay_results {
504: my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
505: if (! ref($headers) || ref($headers) ne 'ARRAY') {
506: return '';
507: }
508: # Start of telling them what problem, part, and response
509: my $Str;
510: if (! $single_response) {
511: my $id = $prob->compTitle;
512: if (defined($partid) && $partid ne '0') {
513: $id .= ' '.$prob->part_display($partid);
514: }
515: if (defined($respid)) {
516: $id .= ' '.$respid;
517: }
518: $Str .= '<span class="LC_nobreak">'.$id.'</span>'.(' 'x4);
519: }
520: #
521: shift(@$headers); # Get rid of the Submission header
522: my $correct = '';
523: if ($headers->[0] eq 'Correct') {
524: $correct = &html_format_essay_sub($response->{'Correct'});
525: shift(@$headers);
526: }
527: $Str .= '<span class="LC_nobreak">'.
528: join('',
529: map {
530: (' 'x4).&mt($_.': [_1]',$response->{$_});
531: } @$headers).'</span>';
532: if (@$headers || ! $single_response) {
533: $Str .= '<br />';
534: }
535: $Str .= &html_format_essay_sub($response->{'Submission'});
536: #
537: if (defined($correct) && $correct !~ /^\s*$/) {
538: $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
539: }
540: return $Str;
541: }
542:
543: sub html_format_essay_sub {
544: my ($submission) = @_;
545: return '' if (! defined($submission) || $submission eq '');
546: $submission = &HTML::Entities::decode($submission);
547: $submission =~ s/\\\"/\"/g;
548: $submission =~ s/\\\'/\'/g;
549: $submission =~ s|\\r\\n|$/|g;
550: $submission = &HTML::Entities::encode($submission,'<>&"');
551: $submission =~ s|$/\s*$/|$/</p><p>$/|g;
552: $submission =~ s|\\||g;
553: $submission = '<p>'.$submission.'</p>';
554: return $submission;
555: }
556:
557: sub html_task_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 @values;
564: @values = map { $response->{$_}; } @$headers;
565:
566: my $td = '<td valign="top">';
567: my $str = $td.join('</td>'.$td,@values).'</td>';
568: return $str;
569: }
570:
571: sub html_non_essay_results {
572: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
573: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
574: return '';
575: }
576: #
577: my $submission = &HTML::Entities::decode(&unescape($response->{'Submission'}));
578: return '' if (! defined($submission) || $submission eq '');
579: $submission =~ s/\\\"/\"/g;
580: $submission =~ s/\\\'/\'/g;
581: if ($resptype eq 'radiobutton') {
582: $submission = &HTML::Entities::encode($submission,'<>&"');
583: $submission =~ s/=([^=])$//;
584: $submission = '<span class="LC_nobreak">'.$submission.'</span>';
585: }
586: $response->{'Submission'} = $submission;
587: #
588: my @values;
589: if ($resptype =~ /^(option|match|rank)$/) {
590: my %submission =
591: map {
592: my ($foil,$value) = split('=',&unescape($_));
593: ($foil,$value);
594: } split('&',$response->{'Submission'});
595: my %correct;
596: if (exists($response->{'Correct'})) {
597: %correct =
598: map {
599: my ($foil,$value)=split('=',&unescape($_));
600: ($foil,$value);
601: } split('&',$response->{'Correct'});
602: }
603: #
604: foreach my $original_header (@$headers) {
605: if ($original_header =~ /^_/) {
606: # '_' denotes a foil column
607: my ($header) = ($original_header =~ m/^_(.*)$/);
608: my $option = '';
609: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
610: if (exists($correct{$foil})) {
611: $option = $correct{$foil};
612: }
613: } elsif (exists($submission{$header})) {
614: $option = $submission{$header};
615: }
616: push(@values,&HTML::Entities::encode($option));
617: } elsif ($original_header eq 'Time') {
618: push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
619: } else {
620: # A normal column
621: push(@values,$response->{$original_header});
622: }
623: }
624: } else {
625: @values = map { $response->{$_}; } @$headers;
626: }
627: my $td = '<td valign="top">';
628: my $str = $td.join('</td>'.$td,@values).'</td>';
629: return $str;
630: }
631:
632:
633: #########################################################
634: #########################################################
635: ##
636: ## Excel Output Routines
637: ##
638: #########################################################
639: #########################################################
640: sub prepare_excel_output {
641: my ($r,$Problems,$Students) = @_;
642: my $c = $r->connection();
643: #
644: #
645: # Determine the number of columns in the spreadsheet
646: my $columncount = 3; # username, domain, id
647: my @extra_resp_headers = &get_extra_response_headers();
648: my $lastprob;
649: my %problem_analysis;
650: foreach my $prob (@$Problems) {
651: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
652: $problem_analysis{$prob->src}=\%analysis;
653: foreach my $partid (@{$prob->parts}) {
654: my $responses = [$prob->responseIds($partid)];
655: my $resptypes = [$prob->responseType($partid)];
656: for (my $i=0;$i<scalar(@$responses);$i++) {
657: my @headers = &get_headers($prob,$partid,$responses->[$i],
658: $resptypes->[$i],
659: $problem_analysis{$prob->src},
660: 'excel','display',
661: @extra_resp_headers);
662: $columncount += scalar(@headers);
663: }
664: }
665: last if ($columncount > 255);
666: $lastprob = $prob;
667: }
668: if ($columncount > 255) {
669: $r->print('<h1>'.&mt('Unable to complete request').'</h1>'.$/.
670: '<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>'.$/.
671: '<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>'.$/.
672: '<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
673: $r->rflush();
674: return;
675: }
676: #
677: # Print out a message telling them what we are doing
678: if (scalar(@$Problems) > 1) {
679: $r->print('<h2>'.
680: &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
681: scalar(@$Problems)).
682: '</h2>');
683: } else {
684: $r->print('<h2>'.
685: &mt('Preparing Excel spreadsheet of student responses').
686: '</h2>');
687: }
688: $r->rflush();
689: #
690: # Create the excel spreadsheet
691: my ($workbook,$filename,$format) =
692: &Apache::loncommon::create_workbook($r);
693: return if (! defined($workbook));
694: my $worksheet = $workbook->addworksheet('Student Submission Data');
695: #
696: # Add headers to the worksheet
697: my $rows_output = 0;
698: $worksheet->write($rows_output++,0,
699: $env{'course.'.$env{'request.course.id'}.'.description'},
700: $format->{'h1'});
701: $rows_output++;
702: my $cols_output = 0;
703: my $title_row = $rows_output++;
704: my $partid_row = $rows_output++;
705: my $respid_row = $rows_output++;
706: my $header_row = $rows_output++;
707: $worksheet->write($title_row ,0,'Problem Title',$format->{'bold'});
708: $worksheet->write($partid_row,0,'Part ID',$format->{'bold'});
709: $worksheet->write($respid_row,0,'Response ID',$format->{'bold'});
710: # Student headers
711: my @StudentColumns = ('username','domain','id','section');
712: foreach (@StudentColumns) {
713: $worksheet->write($header_row,$cols_output++,ucfirst($_),
714: $format->{'bold'});
715: }
716: # Problem headers
717: my %start_col;
718: foreach my $prob (@$Problems) {
719: my $title = $prob->compTitle;
720: $worksheet->write($title_row,$cols_output,
721: $title,$format->{'h3'});
722: foreach my $partid (@{$prob->parts}) {
723: $worksheet->write($partid_row,$cols_output,
724: $prob->part_display($partid));
725: my $responses = [$prob->responseIds($partid)];
726: my $resptypes = [$prob->responseType($partid)];
727: for (my $i=0;$i<scalar(@$responses);$i++) {
728: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
729: $cols_output;
730: $worksheet->write($respid_row,$cols_output,
731: $resptypes->[$i].', '.$responses->[$i]);
732: my @headers = &get_headers($prob,$partid,$responses->[$i],
733: $resptypes->[$i],
734: $problem_analysis{$prob->src},
735: 'excel','display',
736: @extra_resp_headers);
737: foreach my $text (@headers) {
738: if ($text eq 'Time') {
739: $worksheet->set_column($cols_output,$cols_output,undef,
740: $format->{'date'});
741: }
742: $worksheet->write($header_row,$cols_output++,$text);
743: }
744: }
745: }
746: }
747: #
748: # Populate the worksheet with the student data
749: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
750: ($r,'Excel File Compilation Status',
751: 'Excel File Compilation Progress',
752: scalar(@$Students),'inline',undef,'Statistics','stats_status');
753: my $max_row = $rows_output;
754: foreach my $student (@$Students) {
755: last if ($c->aborted());
756: $cols_output = 0;
757: my $student_row = $max_row;
758: foreach my $field (@StudentColumns) {
759: $worksheet->write($student_row,$cols_output++,
760: $student->{$field});
761: }
762: my $last_student_col = $cols_output-1;
763: foreach my $prob (@$Problems) {
764: foreach my $partid (@{$prob->parts}) {
765: my @Response = $prob->responseIds($partid);
766: my @ResponseType = $prob->responseType($partid);
767: for (my $i=0;$i<=$#Response;$i++) {
768: my $respid = $Response[$i];
769: my $resptype = $ResponseType[$i];
770: my $results =
771: &Apache::loncoursedata::get_response_data_by_student
772: ($student,$prob->symb(),$respid);
773: my @headers = &get_headers($prob,$partid,$respid,
774: $resptype,
775: $problem_analysis{$prob->src},
776: 'excel','normal',
777: @extra_resp_headers);
778:
779: if (! defined($results)) {
780: $results = [];
781: }
782: #
783: $rows_output = $student_row;
784: #
785: my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
786: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
787: $cols_output = $response_start_col;
788: if ($env{'form.all_sub'} ne 'true') {
789: next if ($j ne scalar(@$results)-1);
790: }
791: my $response = &hashify_response($results->[$j],
792: $prob,
793: $student,
794: $partid,
795: $respid);
796: my @response_data =
797: &compile_response_data(\@headers,$response,
798: $prob,$partid,$respid,
799: $resptype,
800: \&excel_format_item);
801: $worksheet->write_row($rows_output++,$cols_output,
802: \@response_data);
803: $cols_output+=scalar(@response_data);
804: if ($rows_output > $max_row) {
805: $max_row = $rows_output;
806: }
807: }
808: }
809: }
810: }
811: # Fill in the remaining rows with the students data
812: for (my $row = $student_row+1;$row<$max_row;$row++) {
813: my $cols = 0;
814: foreach my $field (@StudentColumns) {
815: $worksheet->write($row,$cols++,
816: $student->{$field});
817: }
818: }
819: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
820: 'last student');
821: }
822: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
823: #
824: # Close the excel file
825: $workbook->close();
826: #
827: # Write a link to allow them to download it
828: $r->print('<p><a href="'.$filename.'">'.
829: &mt('Your Excel spreadsheet.').
830: '</a></p>'."\n");
831: $r->print('<script>'.
832: 'window.document.Statistics.stats_status.value="'.
833: 'Done compiling spreadsheet. See link below to download.'.
834: '";</script>');
835: $r->rflush();
836: return;
837: }
838:
839: sub compile_response_data {
840: my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
841: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
842: return ();
843: }
844: if (ref($format) ne 'CODE') {
845: $format = sub { return $_[0]; };
846: }
847: #
848: my $submission =
849: &HTML::Entities::decode
850: (&unescape($response->{'Submission'}));
851: if (!$prob->is_task()) {
852: return () if (! defined($submission) || $submission eq '');
853: }
854: $submission =~ s/\\\"/\"/g;
855: $submission =~ s/\\\'/\'/g;
856: if ($resptype eq 'radiobutton') {
857: $submission =~ s/=([^=])$//;
858: }
859: $response->{'Submission'} = $submission;
860: #
861: my @values;
862: if ($resptype =~ /^(option|match|rank)$/) {
863: my %submission =
864: map {
865: my ($foil,$value) = split('=',&unescape($_));
866: ($foil,$value);
867: } split('&',$response->{'Submission'});
868: my %correct;
869: if (exists($response->{'Correct'})) {
870: %correct =
871: map {
872: my ($foil,$value)=split('=',&unescape($_));
873: ($foil,$value);
874: } split('&',$response->{'Correct'});
875: }
876: #
877: foreach my $original_header (@$headers) {
878: if ($original_header =~ /^_/) {
879: # '_' denotes a foil column
880: my ($header) = ($original_header =~ m/^_(.*)$/);
881: my $option = '';
882: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
883: if (exists($correct{$foil})) {
884: $option = $correct{$foil};
885: }
886: } elsif (exists($submission{$header})) {
887: $option = $submission{$header};
888: }
889: push(@values,&{$format}($option,$header));
890: } else {
891: # A normal column
892: push(@values,&{$format}($response->{$original_header},
893: $original_header));
894: }
895: }
896: } else {
897: @values = map { &{$format}($response->{$_},$_); } @$headers;
898: }
899: return @values;
900: }
901:
902: sub excel_format_item {
903: my ($item,$type) = @_;
904: if ($type eq 'Time') {
905: $item = &Apache::lonstathelpers::calc_serial($item);
906: } else {
907: if ($item =~ m/^=/) {
908: $item = ' '.$item;
909: }
910: $item =~ s/\\r//g;
911: $item =~ s/\\n/\n/g;
912: $item =~ s/(\s*$|^\s*)//g;
913: $item =~ s/\\\'/\'/g;
914: }
915: return $item;
916: }
917:
918: #########################################################
919: #########################################################
920: ##
921: ## CSV output of student answers
922: ##
923: #########################################################
924: #########################################################
925: sub prepare_csv_output {
926: my ($r,$problems,$students) = @_;
927: my $c = $r->connection();
928: #
929: $r->print('<h2>'.
930: &mt('Generating CSV report of student responses').'</h2>');
931: #
932: # Progress window
933: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
934: ($r,'CSV File Compilation Status',
935: 'CSV File Compilation Progress',
936: scalar(@$students),'inline',undef,'Statistics','stats_status');
937:
938: $r->rflush();
939: #
940: # Open a file
941: my $outputfile;
942: my $filename = '/prtspool/'.
943: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
944: time.'_'.rand(1000000000).'.csv';
945: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
946: $r->log_error("Couldn't open $filename for output $!");
947: $r->print(
948: '<p class="LC_error">'
949: .&mt('Problems occurred in writing the CSV file.')
950: .' '.&mt('This error has been logged.')
951: .' '.&mt('Please alert your LON-CAPA administrator.')
952: .'</p>'
953: );
954: $outputfile = undef;
955: }
956: #
957: # Compute the number of columns per response
958: my @extra_resp_headers = &get_extra_response_headers();
959: #
960: # Create the table header
961: my @student_columns = ('username','domain','id','section');
962: #
963: my %headers;
964: push(@{$headers{'student'}},@student_columns);
965: # Pad for the student data
966: foreach my $row ('problem','part','response') {
967: $headers{$row}=[map {''} @student_columns];
968: }
969: #
970: # we put the headers into the %headers hash
971: my %problem_analysis;
972: my %start_col;
973: my $max_column = scalar(@student_columns);
974: foreach my $prob (@$problems) {
975: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
976: $problem_analysis{$prob->src}=\%analysis;
977: $headers{'problem'}->[$max_column] = $prob->compTitle;
978: foreach my $partid (@{$prob->parts}) {
979: $headers{'part'}->[$max_column] = $prob->part_display($partid);
980: my $responses = [$prob->responseIds($partid)];
981: my $resptypes = [$prob->responseType($partid)];
982: for (my $i=0;$i<scalar(@$responses);$i++) {
983: my @headers = &get_headers($prob,$partid,$responses->[$i],
984: $resptypes->[$i],
985: $problem_analysis{$prob->src},
986: 'csv','display',
987: @extra_resp_headers);
988: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
989: $max_column;
990: $headers{'response'}->[$max_column]=
991: &mt('Response [_1]',$responses->[$i]);
992: for (my $j=0;$j<=$#headers;$j++) {
993: $headers{'student'}->[$max_column+$j]=$headers[$j];
994: }
995: $max_column += scalar(@headers);
996: }
997: }
998: }
999: foreach my $row ('problem','part','response','student') {
1000: print $outputfile '"'.
1001: join('","',
1002: map {
1003: &Apache::loncommon::csv_translate($_);
1004: } @{$headers{$row}}).'"'.$/;
1005: }
1006: #
1007: # Main loop
1008: foreach my $student (@$students) {
1009: last if ($c->aborted());
1010: my @rows;
1011: foreach my $prob (@$problems) {
1012: foreach my $partid (@{$prob->parts}) {
1013: my @responses = $prob->responseIds($partid);
1014: my @response_type = $prob->responseType($partid);
1015: for (my $i=0;$i<=$#responses;$i++) {
1016: my $respid = $responses[$i];
1017: my $resptype = $response_type[$i];
1018: my @headers = &get_headers($prob,$partid,$respid,$resptype,
1019: $problem_analysis{$prob->src},
1020: 'csv','normal',
1021: @extra_resp_headers);
1022: my $results =
1023: &Apache::loncoursedata::get_response_data_by_student
1024: ($student,$prob->symb(),$respid);
1025: if (! defined($results)) {
1026: $results = [];
1027: }
1028: for (my $j=0; $j<scalar(@$results);$j++) {
1029: if ($env{'form.all_sub'} ne 'true') {
1030: next if ($j != 0);
1031: }
1032: my $idx = scalar(@$results) - $j - 1;
1033: my $response = &hashify_response($results->[$idx],
1034: $prob,$student,
1035: $partid,$respid);
1036: my @data = &compile_response_data(\@headers,$response,
1037: $prob,$partid,
1038: $respid,$resptype,
1039: \&csv_format_item);
1040: my $resp_start_idx =
1041: $start_col{$prob->symb}->{$partid}->{$respid};
1042: for (my $k=0;$k<=$#data;$k++) {
1043: $rows[$j]->[$resp_start_idx + $k] = $data[$k];
1044: }
1045: }
1046: }
1047: }
1048: }
1049: foreach my $row (@rows) {
1050: print $outputfile '"'.join('","',
1051: map { $student->{$_}; }
1052: @student_columns).'"';
1053: for (my $i=scalar(@student_columns);$i<$max_column;$i++) {
1054: my $value = &Apache::loncommon::csv_translate($row->[$i]);
1055: $value ||='';
1056: print $outputfile ',"'.$value.'"';
1057: }
1058: print $outputfile $/;
1059: }
1060: undef(@rows);
1061: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1062: 'last student');
1063: }
1064: close($outputfile);
1065: #
1066: # Close the progress window
1067: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1068: #
1069: # Tell the user where to get their CSV file
1070: $r->print('<br />'.
1071: '<a href="'.$filename.'">'.&mt('Your CSV file.').'</a>'."\n");
1072: $r->rflush();
1073: return;
1074: }
1075:
1076: sub csv_format_item {
1077: my ($item,$type) = @_;
1078: if ($type eq 'Time') {
1079: $item = localtime($item);
1080: }
1081: $item =&Apache::loncommon::csv_translate($item);
1082: return $item;
1083: }
1084:
1085: #########################################################
1086: #########################################################
1087: ##
1088: ## Generic Interface Routines
1089: ##
1090: #########################################################
1091: #########################################################
1092: sub CreateInterface {
1093: ##
1094: ## Output Selection
1095: my $output_selector = $/.'<select name="output">'.$/;
1096: foreach ('HTML','Excel','CSV') {
1097: $output_selector .= ' <option value="'.lc($_).'"';
1098: if ($env{'form.output'} eq lc($_)) {
1099: $output_selector .= ' selected ';
1100: }
1101: $output_selector .='>'.&mt($_).'</option>'.$/;
1102: }
1103: $output_selector .= '</select>'.$/;
1104: ##
1105: ## Environment variable initialization
1106: my $Str = '';
1107: $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
1108: $Str .= '<p>';
1109: $Str .= &Apache::loncommon::start_data_table();
1110: $Str .= &Apache::loncommon::start_data_table_header_row();
1111: $Str .= '<th>'.&mt('Sections').'</th>';
1112: $Str .= '<th>'.&mt('Groups').'</th>';
1113: $Str .= '<th>'.&mt('Student Data').&Apache::loncommon::help_open_topic("Chart_Student_Data").'</th>';
1114: $Str .= '<th>'.&mt('Access Status').'</th>';
1115: $Str .= '<th>'.&mt('Options').'</th>';
1116: $Str .= '<th>'.&mt('Output Format').'</th>';
1117: $Str .= &Apache::loncommon::end_data_table_header_row();
1118: #
1119: $Str .= &Apache::loncommon::start_data_table_row();
1120: $Str .= '<td align="center">'."\n";
1121: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1122: $Str .= '</td>';
1123: #
1124: $Str .= '<td align="center">'."\n";
1125: $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
1126: $Str .= '</td>';
1127: #
1128: $Str .= '<td align="center">'."\n";
1129: $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple', 5,undef);
1130: $Str .= '</td>';
1131: #
1132: $Str .= '<td align="center">';
1133: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1134: $Str .= '</td>';
1135: #
1136: # Render problem checkbox
1137: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1138: if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1139: $prob_checkbox .= 'checked="checked" ';
1140: }
1141: $prob_checkbox .= 'value="true" />';
1142: #
1143: # Compute correct answers checkbox
1144: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1145: if (exists($env{'form.correctans'}) && $env{'form.correctans'} eq 'true') {
1146: $ans_checkbox .= 'checked="checked" ';
1147: }
1148: $ans_checkbox .= 'value="true" />';
1149: #
1150: # Show all submissions checkbox
1151: my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
1152: if (exists($env{'form.all_sub'}) &&
1153: $env{'form.all_sub'} eq 'true') {
1154: $all_sub_checkbox .= 'checked="checked" ';
1155: }
1156: $all_sub_checkbox.= 'value="true" />';
1157: #
1158: # problem status checkbox
1159: my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
1160: if (exists($env{'form.prob_status'}) &&
1161: $env{'form.prob_status'} eq 'true') {
1162: $prob_status_checkbox .= 'checked="checked" ';
1163: }
1164: $prob_status_checkbox .= 'value="true" />';
1165: #
1166: $Str .=
1167: '<td valign="top">'
1168: .'<label>'
1169: .$prob_checkbox.&mt('Show problem')
1170: .'</label><br />'
1171: .'<label>'
1172: .' '.$ans_checkbox.&mt('Show correct answers')
1173: .'</label><br />'
1174: .'<label>'
1175: .$all_sub_checkbox.&mt('Show all submissions')
1176: .'</label><br />'
1177: .'<label>'
1178: .$prob_status_checkbox.&mt('Show problem grading')
1179: .'</label>'
1180: .'</td>';
1181: #
1182: $Str .= '<td align="center" valign="top">'.$output_selector.'</td>';
1183: #
1184: $Str .= &Apache::loncommon::end_data_table_row();
1185: $Str .= &Apache::loncommon::end_data_table();
1186: #
1187: $Str .= '<p><span class="LC_nobreak">'
1188: .&mt('Status: [_1]',
1189: '<input type="text" name="stats_status"'
1190: .' size="60" value="" readonly="readonly" />')
1191: .'</span></p>';
1192: $Str .= '</p>';
1193: ##
1194: return $Str;
1195: }
1196:
1197: 1;
1198:
1199: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>