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