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