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