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