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