Annotation of loncom/interface/statistics/lonstudentsubmissions.pm, revision 1.70
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.70 ! bisitz 3: # $Id: lonstudentsubmissions.pm,v 1.69 2012/10/12 14:08:42 bisitz 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();
1.62 www 33: use Apache::lonquickgrades();
1.1 matthew 34: use Apache::loncoursedata();
35: use Apache::lonstatistics;
36: use Apache::lonlocal;
37: use Apache::lonstathelpers;
1.65 raeburn 38: use Apache::lonmsgdisplay();
1.1 matthew 39: use HTML::Entities();
40: use Time::Local();
41: use Spreadsheet::WriteExcel();
1.45 www 42: use lib '/home/httpd/lib/perl/';
43: use LONCAPA;
44:
1.1 matthew 45:
1.18 matthew 46: my @SubmitButtons = ({ name => 'SelectAnother',
1.1 matthew 47: text => 'Choose a different Problem' },
48: { name => 'Generate',
1.16 matthew 49: text => 'Generate Report'},
1.1 matthew 50: );
51:
52: sub BuildStudentSubmissionsPage {
53: my ($r,$c)=@_;
54: #
55: my %Saveable_Parameters = ('Status' => 'scalar',
56: 'Section' => 'array',
57: 'NumPlots' => 'scalar',
58: );
59: &Apache::loncommon::store_course_settings('student_submissions',
60: \%Saveable_Parameters);
61: &Apache::loncommon::restore_course_settings('student_submissions',
62: \%Saveable_Parameters);
63: #
64: &Apache::lonstatistics::PrepareClasslist();
65: #
1.62 www 66: $r->print( &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports'));
67: &Apache::lonquickgrades::startGradeScreen($r,'statistics');
1.1 matthew 68: $r->print(&CreateInterface());
69: #
70: my @Students = @Apache::lonstatistics::Students;
71: #
72: if (@Students < 1) {
1.50 bisitz 73: $r->print('<div class="LC_warning">'
74: .&mt('There are no students in the sections selected.')
75: .'</div>');
1.1 matthew 76: }
77: #
1.11 matthew 78: my @CacheButtonHTML =
1.19 matthew 79: &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status',
1.50 bisitz 80: '<div class="LC_info">'.&mt('Loading student data...').'</div>');
1.1 matthew 81: $r->rflush();
82: #
1.60 raeburn 83: my %anoncounter =
84: &Apache::lonnet::dump('nohist_anonsurveys',
85: $env{'course.'.$env{'request.course.id'}.'.domain'},
86: $env{'course.'.$env{'request.course.id'}.'.num'});
1.40 albertel 87: if (exists($env{'form.problemchoice'}) &&
88: ! exists($env{'form.SelectAnother'})) {
1.1 matthew 89: foreach my $button (@SubmitButtons) {
90: if ($button->{'name'} eq 'break') {
91: $r->print("<br />\n");
92: } else {
93: $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
94: 'value="'.&mt($button->{'text'}).'" />');
95: $r->print(' 'x5);
96: }
97: }
1.11 matthew 98: foreach my $html (@CacheButtonHTML) {
99: $r->print($html.(' 'x5));
100: }
1.1 matthew 101: #
1.18 matthew 102: $r->print('<hr />'.$/);
1.1 matthew 103: $r->rflush();
104: #
1.18 matthew 105: # Determine which problems we are to analyze
106: my @Symbs =
107: &Apache::lonstathelpers::get_selected_symbs('problemchoice');
1.60 raeburn 108:
109: # If there are multi-part problems with anonymous survey and named
110: # parts check if named was picked for display.
111: #
112: my %mixed_named;
113: foreach my $envkey (%env) {
114: if ($envkey =~ /^form\.mixed_(\d+:\d+)$/) {
115: my $item = $1;
116: if ($env{$envkey} =~ /^symb_(.+)$/) {
117: my $symb = &unescape($1);
118: if (ref($mixed_named{$symb}) eq 'ARRAY') {
119: push(@{$mixed_named{$symb}},$item);
120: } else {
121: @{$mixed_named{$symb}} = ($item);
122: }
123: }
124: }
1.18 matthew 125: }
1.1 matthew 126: #
1.18 matthew 127: # Get resource objects
128: my $navmap = Apache::lonnavmaps::navmap->new();
129: if (!defined($navmap)) {
1.60 raeburn 130: foreach my $selected (@Symbs) {
131: $r->print('<input type="hidden" name="problemchoice" value="'.
132: &escape($selected).'" />'.$/);
133: if (ref($mixed_named{$selected}) eq 'ARRAY') {
134: foreach my $item (@{$mixed_named{$selected}}) {
135: $r->print('<input type="hidden" name="mixed_'.$item.'" value="'.&escape($selected).'" />'.$/);
136: }
137: }
138: }
1.50 bisitz 139: $r->print('<div class="LC_error">'.&mt("Internal error").'</div>');
1.18 matthew 140: return;
141: }
142: my %already_seen;
1.60 raeburn 143: my (@Problems,@anonProbs,@namedProbs,$show_named);
1.18 matthew 144: foreach my $symb (@Symbs) {
145: my $resource = $navmap->getBySymb($symb);
1.60 raeburn 146: my ($hasanon,$hasnamed);
147: if (ref($resource)) {
148: foreach my $partid (@{$resource->parts}) {
149: if (($anoncounter{$symb."\0".$partid}) || ($resource->is_anonsurvey($partid))) {
150: unless (exists($mixed_named{$symb})) {
151: $hasanon = 1;
152: }
153: } else {
154: $hasnamed = 1;
155: }
156: }
157: if ($hasanon) {
158: push(@anonProbs,$resource);
159: } elsif ($hasnamed) {
160: push(@namedProbs,$resource);
161: }
162: }
1.1 matthew 163: }
1.60 raeburn 164: if (@namedProbs > 0) {
165: @Problems = @namedProbs;
166: $show_named = 1;
167: } elsif (@anonProbs > 0) {
168: @Problems = @anonProbs;
169: }
170: foreach my $selected (@Symbs) {
171: $r->print('<input type="hidden" name="problemchoice" value="'.
172: &escape($selected).'" />'.$/);
173: if (ref($mixed_named{$selected}) eq 'ARRAY') {
174: foreach my $item (@{$mixed_named{$selected}}) {
175: $r->print('<input type="hidden" name="mixed_'.$item.'" value="'.&escape($selected).'" />'.$/);
176: }
177: }
178: }
179: # If these are to be anonymized, do a random shuffle of @Students.
180: unless ($show_named) {
181: &array_shuffle(\@Students);
182: }
183: #
1.61 raeburn 184:
185: my $threshold = $env{'course.'.$env{'request.course.id'}.'.internal.anonsurvey_threshold'};
186: if ($threshold eq '') {
187: my %domconfig =
188: &Apache::lonnet::get_dom('configuration',['coursedefaults'],
189: $env{'course.'.$env{'request.course.id'}.'.domain'});
190: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
191: $threshold = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
192: if ($threshold eq '') {
193: $threshold = 10;
194: }
195: } else {
196: $threshold = 10;
197: }
198: }
1.68 golterma 199: $r->print('<p>'.
1.37 matthew 200: &Apache::lonstatistics::section_and_enrollment_description().
1.68 golterma 201: '</p>');
1.18 matthew 202: if (! scalar(@Problems) || ! defined($Problems[0])) {
1.61 raeburn 203: $r->print(&mt('resource is undefined'));
204: } elsif (!$show_named && @Students < $threshold) {
205: $r->print(&mt('The number of students matching the selection criteria is too few for display of submission data for anonymous surveys.').'<br />'.&mt('There must be at least [quant,_1,student].',$threshold).' '.&mt('Contact a Domain Coordinator if you need the threshold to be changed for this course.'));
1.1 matthew 206: } else {
1.18 matthew 207: if (scalar(@Problems) == 1) {
208: my $resource = $Problems[0];
209: $r->print('<h1>'.$resource->title.'</h1>');
210: $r->print('<h3>'.$resource->src.'</h3>');
1.40 albertel 211: if ($env{'form.renderprob'} eq 'true') {
1.36 matthew 212: $r->print(&Apache::lonstathelpers::render_resource($resource));
1.18 matthew 213: $r->rflush();
214: }
215: }
1.40 albertel 216: if ($env{'form.output'} eq 'excel') {
1.60 raeburn 217: &prepare_excel_output($r,\@Problems,\@Students,\%anoncounter,$show_named);
1.40 albertel 218: } elsif ($env{'form.output'} eq 'csv') {
1.60 raeburn 219: &prepare_csv_output($r,\@Problems,\@Students,\%anoncounter,$show_named);
1.21 matthew 220: } else {
1.60 raeburn 221: &prepare_html_output($r,\@Problems,\@Students,\%anoncounter,$show_named);
1.21 matthew 222: }
1.1 matthew 223: }
224: $r->print('<hr />');
225: } else {
226: $r->print('<input type="submit" name="Generate" value="'.
1.17 matthew 227: &mt('Prepare Report').'" />');
1.1 matthew 228: $r->print(' 'x5);
1.19 matthew 229: $r->print('<p>'.
1.61 raeburn 230: &mt('Computing correct answers greatly increases the amount of time required to prepare a report.').
1.19 matthew 231: '</p>');
232: $r->print('<p>'.
1.50 bisitz 233: &mt('Please select problems and use the [_1]Prepare Report[_2] button to continue.','<b>','</b>').
1.19 matthew 234: '</p>');
1.18 matthew 235: $r->print(&Apache::lonstathelpers::MultipleProblemSelector
1.60 raeburn 236: (undef,'problemchoice','Statistics',\%anoncounter));
1.18 matthew 237: }
238: }
239:
1.60 raeburn 240: sub array_shuffle {
241: my $array = shift;
242: return unless (ref($array) eq 'ARRAY');
243: my $i = scalar(@$array);
244: my $j;
245: foreach my $item (@$array) {
246: --$i;
247: $j = int(rand($i+1));
248: next if($i == $j);
249: @$array [$i,$j] = @$array[$j,$i];
250: }
251: return @$array;
252: }
253:
1.30 matthew 254: ##
1.31 matthew 255: ## get_extra_response_headers
256: ##
257: sub get_extra_response_headers {
1.60 raeburn 258: my ($show_named) = @_;
1.31 matthew 259: my @extra_resp_headers;
1.40 albertel 260: if ($env{'form.correctans'} eq 'true') {
1.31 matthew 261: push(@extra_resp_headers,'Correct');
1.60 raeburn 262: }
263: if ($show_named) {
264: if ($env{'form.prob_status'} eq 'true') {
265: push(@extra_resp_headers,'Award Detail');
266: push(@extra_resp_headers,'Time');
267: push(@extra_resp_headers,'Attempt');
268: push(@extra_resp_headers,'Awarded');
269: }
1.31 matthew 270: }
271: return @extra_resp_headers;
272: }
273:
274: ##
1.30 matthew 275: ## get_headers:
276: ## return the proper headers for the given response
277: sub get_headers {
278: my ($prob,$partid,$respid,$resptype,$analysis,$output,$purpose,
279: @basic_headers) = @_;
280: my @headers;
281: if ($resptype eq 'essay' && $purpose eq 'display' &&
282: ($output eq 'html')) {# || scalar(@{$prob->parts})!=1)) {
283: @headers = ();
284: } elsif ($resptype =~ /^(option|match|rank)$/) {
285: my $prefix = '_';
286: if ($purpose eq 'display') {
287: $prefix = '';
288: }
289: my @foils =
290: map {
291: $prefix.$_;
292: } sort(keys(%{$analysis->{$partid.'.'.$respid}->{'_Foils'}}));
293: if (scalar(@basic_headers) && $basic_headers[0] eq 'Correct') {
294: @foils = map { ($_ , $_.' Correct') } @foils;
295: shift(@basic_headers); # Get rid of 'Correct'
296: }
297: @headers = (@foils,@basic_headers);
1.42 albertel 298: } elsif (lc($resptype) eq 'task') {
299: @headers = ('Grader','Status',@basic_headers,'Submission');
1.30 matthew 300: } else {
301: @headers = ('Submission',@basic_headers);
302: }
303: return @headers;
304: }
305:
1.18 matthew 306: #########################################################
307: #########################################################
1.21 matthew 308: ##
309: ## HTML Output Routines
310: ##
311: #########################################################
312: #########################################################
313: sub prepare_html_output {
1.60 raeburn 314: my ($r,$problems,$students,$anoncounter,$show_named) = @_;
1.21 matthew 315: my $c = $r->connection();
316: #
317: # Set a flag for the case when there is just one problem
318: my $single_response = 0;
319: if (scalar(@$problems) == 1 &&
320: $problems->[0]->countResponses == 1) {
321: $single_response = 1;
322: }
323: #
324: # Compute the number of columns per response
1.60 raeburn 325: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.21 matthew 326: #
327: # Create the table header
1.65 raeburn 328: my @student_columns = &get_student_columns($show_named);
1.21 matthew 329: my %headers;
330: my $student_column_count = scalar(@student_columns);
331: $headers{'problem'} = qq{<th colspan="$student_column_count">\ </th>};
1.65 raeburn 332: foreach my $field (@student_columns) {
1.67 bisitz 333: $headers{'student'}.= '<th>'.&mt(ucfirst($field)).'</th>';
1.21 matthew 334: }
335: #
336: # we put the headers into the %headers hash
1.65 raeburn 337: my $total_col = $student_column_count;
1.21 matthew 338: my $nonempty_part_headers = 0;
1.30 matthew 339: #
340: my %problem_analysis;
1.21 matthew 341: foreach my $prob (@$problems) {
1.31 matthew 342: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
1.30 matthew 343: $problem_analysis{$prob->src}=\%analysis;
1.60 raeburn 344: my $symb = $prob->symb();
1.30 matthew 345: #
1.21 matthew 346: my $prob_span = 0;
347: my $single_part = 0;
348: if (scalar(@{$prob->parts}) == 1) {
349: $single_part = 1;
350: }
1.60 raeburn 351: my $shown_parts = 0;
1.21 matthew 352: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 353: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
354: next if ($show_named);
355: } else {
356: next unless ($show_named);
357: }
358: $shown_parts ++;
1.21 matthew 359: my $part_span = 0;
360: my $responses = [$prob->responseIds($partid)];
361: my $resptypes = [$prob->responseType($partid)];
362: for (my $i=0;$i<scalar(@$responses);$i++) {
1.30 matthew 363: my $respid = $responses->[$i];
364: my @headers = &get_headers($prob,$partid,$respid,
365: $resptypes->[$i],
366: $problem_analysis{$prob->src},
367: 'html','display',
368: @extra_resp_headers);
369: if (scalar(@headers)>0) {
370: $total_col += scalar(@headers);
371: $part_span += scalar(@headers);
1.21 matthew 372: $headers{'response'} .=
1.30 matthew 373: '<th colspan="'.scalar(@headers).'">'.
1.21 matthew 374: &mt('Response [_1]',$responses->[$i]).'</th>';
1.52 raeburn 375: $headers{'student'}.= '<th><span class="LC_nobreak">'.
376: join('</span></th><th><span class="LC_nobreak">',
1.30 matthew 377: @headers).
1.51 bisitz 378: '</span></th>';
1.21 matthew 379: }
380: }
1.41 matthew 381: if ($part_span == 0) {
382: next;
383: }
1.21 matthew 384: if (! $single_part) {
385: my $tmpname = $partid;
386: if ($partid =~/^\d+$/) {
1.24 matthew 387: $tmpname = $prob->part_display($partid);
1.21 matthew 388: }
1.35 matthew 389: if ($tmpname !~ /^part/) {
390: $tmpname = 'Part '.$tmpname;
391: }
1.21 matthew 392: $headers{'part'} .= qq{<th colspan="$part_span">$tmpname</th>};
393: $nonempty_part_headers = 1;
394: } else {
1.53 bisitz 395: $headers{'part'} .= qq{<th colspan="$part_span"> </th>};
1.21 matthew 396: }
397: $prob_span += $part_span;
398: }
1.60 raeburn 399: next if (!$shown_parts);
1.23 matthew 400: my $title = $prob->compTitle;
1.21 matthew 401: if ($prob_span > 0) {
402: $headers{'problem'}.= qq{<th colspan="$prob_span">$title</th>};
403: } elsif ($single_response) {
404: $prob_span = scalar(@student_columns);
405: $headers{'problem'} = qq{<th colspan="$prob_span">$title</th>};
406: }
407: }
408: if (exists($headers{'part'})) {
409: $headers{'part'} = qq{<th colspan="$student_column_count">\ </th>}.
410: $headers{'part'};
411: }
412: if (exists($headers{'response'})) {
413: $headers{'response'}=
414: qq{<th colspan="$student_column_count">\ </th>}.
415: $headers{'response'};
416: }
1.68 golterma 417: #my $full_header = $/.'<table>'.$/;
418: my $full_header = $/.&Apache::loncommon::start_data_table().$/;
1.21 matthew 419: $full_header .= '<tr align="left">'.$headers{'problem'}.'</tr>'.$/;
420: if ($nonempty_part_headers) {
421: $full_header .= '<tr align="left">'.$headers{'part'}.'</tr>'.$/;
422: }
423: $full_header .= '<tr align="left">'.$headers{'response'}.'</tr>'.$/;
424: $full_header .= '<tr align="left">'.$headers{'student'}.'</tr>'.$/;
425: #
426: # Main loop
427: my $count;
428: $r->print($/.$full_header.$/);
429: foreach my $student (@$students) {
430: my $student_row_data;
431: if ($count++ >= 30) {
1.68 golterma 432: $r->print(&Apache::loncommon::end_data_table().$/.$full_header.$/);
1.21 matthew 433: $count = 0;
434: }
435: last if ($c->aborted());
1.60 raeburn 436: if ($show_named) {
437: foreach my $field (@student_columns) {
438: $student_row_data .= '<td valign="top">';
439: # handle comments like in lonstudentassessment.pm
440: if($field eq 'comments') {
441: $student_row_data .=
1.57 onken 442: '<a href="/adm/'.$student->{'domain'}.'/'.
443: $student->{'username'}.'/'.'aboutme#coursecomment">'.&mt('Comments').'</a>';
1.60 raeburn 444: } else {
445: $student_row_data .= $student->{$field};
446: }
447: $student_row_data .= '</td>';
1.57 onken 448: }
1.60 raeburn 449: } else {
450: $student_row_data = '<td valign="top" colspan="'.$student_column_count.'">'.&mt('Anonymized').'</td>';
1.21 matthew 451: }
452: #
453: # Figure out what it is we need to output for this student
454: my @essays;
1.30 matthew 455: my %prob_data;
1.21 matthew 456: my $maxrow;
457: foreach my $prob (@$problems) {
1.60 raeburn 458: my $symb = $prob->symb;
459: $prob_data{$symb}={};
1.21 matthew 460: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 461: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
462: next if ($show_named);
463: } else {
464: next unless ($show_named);
465: }
1.21 matthew 466: my @responses = $prob->responseIds($partid);
467: my @response_type = $prob->responseType($partid);
468: for (my $i=0;$i<=$#responses;$i++) {
1.30 matthew 469: my $respid = $responses[$i];
1.21 matthew 470: my $results =
471: &Apache::loncoursedata::get_response_data_by_student
472: ($student,$prob->symb(),$respid);
1.30 matthew 473: my $resptype = $response_type[$i];
474: my @headers = &get_headers($prob,$partid,$respid,
475: $resptype,
476: $problem_analysis{$prob->src},
477: 'html','normal',
478: @extra_resp_headers);
479: my $width = scalar(@headers);
1.41 matthew 480: next if ($width < 1);
1.30 matthew 481: my $resp_data;
1.41 matthew 482: $resp_data->{'fake'} = qq{<td colspan="$width"> </td>};
1.21 matthew 483: if (! defined($results)) {
484: $results = [];
485: }
1.30 matthew 486: #
1.21 matthew 487: if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
488: $maxrow = scalar(@$results);
489: }
490: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
1.40 albertel 491: if ($env{'form.all_sub'} ne 'true') {
1.21 matthew 492: next if ($j ne scalar(@$results)-1);
493: }
1.30 matthew 494: my $response = &hashify_response($results->[$j],
495: $prob,
496: $student,
497: $partid,
498: $respid);
1.21 matthew 499: if ($resptype eq 'essay') {
500: push(@essays,
1.30 matthew 501: &html_essay_results(\@headers,
1.21 matthew 502: $prob,$partid,$respid,
503: $response,
504: $single_response).
505: '</td>');
1.42 albertel 506: } elsif (lc($resptype) eq 'task') {
507: my $results =
508: &html_task_results(\@headers,
509: $prob,$partid,$respid,
510: $response,$resptype);
511: if ($results) {
512: push(@{$resp_data->{'real'}},$results);
513: }
1.21 matthew 514: } else {
515: push(@{$resp_data->{'real'}},
1.30 matthew 516: &html_non_essay_results(\@headers,
517: $prob,$partid,$respid,
518: $response,$resptype));
1.21 matthew 519: }
1.30 matthew 520: }
521: $prob_data{$prob->symb}->{$partid}->{$respid}=$resp_data;
1.21 matthew 522: } # end of $i loop
523: } # end of partid loop
524: } # end of prob loop
525: #
526: # if there is no data, skip this student.
527: next if (! $maxrow && ! scalar(@essays));
528: #
529: # Go through the problem data and output a row.
530: my $printed_something;
531: for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
532: my $html;
533: my $no_data = 1;
534: foreach my $prob (@$problems) {
535: foreach my $partid (@{$prob->parts}) {
536: my @responses = $prob->responseIds($partid);
537: my @response_type = $prob->responseType($partid);
538: for (my $i=0;$i<=$#responses;$i++) {
539: my $respid = $responses[$i];
540: my $resp_data =
1.30 matthew 541: $prob_data{$prob->symb}->{$partid}->{$respid};
1.21 matthew 542: next if ($response_type[$i] eq 'essay');
543: if (defined($resp_data->{'real'}->[$rows_output])) {
544: $html .= $resp_data->{'real'}->[$rows_output];
545: $no_data = 0;
546: } else {
547: $html .= $resp_data->{'fake'};
548: }
549: }
550: }
551: }
552: if (! $no_data) {
1.68 golterma 553: $r->print(&Apache::loncommon::start_data_table_row().$student_row_data.$html.
554: &Apache::loncommon::end_data_table_row().$/);
1.21 matthew 555: $printed_something=1;
556: }
557: }
558: if (@essays) {
1.68 golterma 559: my $tr = &Apache::loncommon::start_data_table_row();
1.21 matthew 560: my $td = qq{<td valign="top" class="essay" colspan="$total_col">};
561: if (! $printed_something) {
1.68 golterma 562: $r->print($tr.$student_row_data.&Apache::loncommon::end_data_table_row().$/);
1.21 matthew 563: }
564: $r->print($tr.$td.
565: join('</td></tr>'.$/.$tr.$td,@essays).'</td></tr>'.$/);
566: undef(@essays);
567: }
568: } # end of student loop
1.68 golterma 569: $r->print(&Apache::loncommon::end_data_table().$/);
1.21 matthew 570: return;
571: }
572:
1.30 matthew 573: sub hashify_response {
574: my ($response,$prob,$student,$partid,$respid) =@_;
575: my $resp_hash = {};
1.40 albertel 576: if ($env{'form.correctans'} eq 'true') {
1.30 matthew 577: $resp_hash->{'Correct'} =
578: &Apache::lonstathelpers::get_student_answer
579: ($prob,$student->{'username'},$student->{'domain'},
580: $partid,$respid);
581: }
582: $resp_hash->{'Submission'} =
583: $response->[&Apache::loncoursedata::RDs_submission()];
584: $resp_hash->{'Time'} =
585: $response->[&Apache::loncoursedata::RDs_timestamp()];
586: $resp_hash->{'Attempt'} =
587: $response->[&Apache::loncoursedata::RDs_tries()];
588: $resp_hash->{'Awarded'} =
589: $response->[&Apache::loncoursedata::RDs_awarded()];
1.42 albertel 590: if ($prob->is_task()) {
591: $resp_hash->{'Grader'} =
592: $response->[&Apache::loncoursedata::RDs_response_eval_2()];
593: if ($resp_hash->{'Attempt'} eq '0') {
594: $resp_hash->{'Attempt'} = '';
595: }
596: $resp_hash->{'Award Detail'} =
597: $response->[&Apache::loncoursedata::RDs_part_award()];
598: $resp_hash->{'Status'} =
599: $response->[&Apache::loncoursedata::RDs_response_eval()];
600: } else {
601: $resp_hash->{'Award Detail'} =
602: $response->[&Apache::loncoursedata::RDs_awarddetail()];
603: }
604:
1.30 matthew 605: return $resp_hash;
606: }
607:
1.21 matthew 608: #####################################################
609: ##
610: ## HTML helper routines
611: ##
612: #####################################################
613: sub html_essay_results {
1.30 matthew 614: my ($headers,$prob,$partid,$respid,$response,$single_response)=@_;
615: if (! ref($headers) || ref($headers) ne 'ARRAY') {
616: return '';
1.21 matthew 617: }
1.30 matthew 618: # Start of telling them what problem, part, and response
1.21 matthew 619: my $Str;
620: if (! $single_response) {
1.23 matthew 621: my $id = $prob->compTitle;
1.21 matthew 622: if (defined($partid) && $partid ne '0') {
1.24 matthew 623: $id .= ' '.$prob->part_display($partid);
1.21 matthew 624: }
625: if (defined($respid)) {
626: $id .= ' '.$respid;
627: }
1.51 bisitz 628: $Str .= '<span class="LC_nobreak">'.$id.'</span>'.(' 'x4);
1.21 matthew 629: }
1.30 matthew 630: #
631: shift(@$headers); # Get rid of the Submission header
632: my $correct = '';
633: if ($headers->[0] eq 'Correct') {
634: $correct = &html_format_essay_sub($response->{'Correct'});
635: shift(@$headers);
636: }
1.51 bisitz 637: $Str .= '<span class="LC_nobreak">'.
1.30 matthew 638: join('',
639: map {
640: (' 'x4).&mt($_.': [_1]',$response->{$_});
1.51 bisitz 641: } @$headers).'</span>';
1.30 matthew 642: if (@$headers || ! $single_response) {
643: $Str .= '<br />';
1.21 matthew 644: }
1.30 matthew 645: $Str .= &html_format_essay_sub($response->{'Submission'});
646: #
1.21 matthew 647: if (defined($correct) && $correct !~ /^\s*$/) {
648: $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
649: }
650: return $Str;
651: }
652:
1.30 matthew 653: sub html_format_essay_sub {
654: my ($submission) = @_;
655: return '' if (! defined($submission) || $submission eq '');
656: $submission = &HTML::Entities::decode($submission);
657: $submission =~ s/\\\"/\"/g;
658: $submission =~ s/\\\'/\'/g;
659: $submission =~ s|\\r\\n|$/|g;
660: $submission = &HTML::Entities::encode($submission,'<>&"');
661: $submission =~ s|$/\s*$/|$/</p><p>$/|g;
662: $submission =~ s|\\||g;
663: $submission = '<p>'.$submission.'</p>';
664: return $submission;
1.21 matthew 665: }
666:
1.42 albertel 667: sub html_task_results {
668: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
669: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
670: return '';
671: }
672:
673: my @values;
674: @values = map { $response->{$_}; } @$headers;
675:
676: my $td = '<td valign="top">';
677: my $str = $td.join('</td>'.$td,@values).'</td>';
678: return $str;
679: }
680:
1.30 matthew 681: sub html_non_essay_results {
682: my ($headers,$prob,$partid,$respid,$response,$resptype) = @_;
683: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
684: return '';
685: }
686: #
1.45 www 687: my $submission = &HTML::Entities::decode(&unescape($response->{'Submission'}));
1.21 matthew 688: return '' if (! defined($submission) || $submission eq '');
1.25 matthew 689: $submission =~ s/\\\"/\"/g;
690: $submission =~ s/\\\'/\'/g;
1.30 matthew 691: if ($resptype eq 'radiobutton') {
1.25 matthew 692: $submission = &HTML::Entities::encode($submission,'<>&"');
1.21 matthew 693: $submission =~ s/=([^=])$//;
1.51 bisitz 694: $submission = '<span class="LC_nobreak">'.$submission.'</span>';
1.30 matthew 695: }
696: $response->{'Submission'} = $submission;
697: #
698: my @values;
699: if ($resptype =~ /^(option|match|rank)$/) {
700: my %submission =
701: map {
1.45 www 702: my ($foil,$value) = split('=',&unescape($_));
1.30 matthew 703: ($foil,$value);
704: } split('&',$response->{'Submission'});
705: my %correct;
706: if (exists($response->{'Correct'})) {
707: %correct =
708: map {
1.45 www 709: my ($foil,$value)=split('=',&unescape($_));
1.30 matthew 710: ($foil,$value);
711: } split('&',$response->{'Correct'});
712: }
713: #
714: foreach my $original_header (@$headers) {
715: if ($original_header =~ /^_/) {
716: # '_' denotes a foil column
717: my ($header) = ($original_header =~ m/^_(.*)$/);
718: my $option = '';
719: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
720: if (exists($correct{$foil})) {
721: $option = $correct{$foil};
722: }
723: } elsif (exists($submission{$header})) {
724: $option = $submission{$header};
725: }
726: push(@values,&HTML::Entities::encode($option));
1.38 matthew 727: } elsif ($original_header eq 'Time') {
728: push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
1.30 matthew 729: } else {
730: # A normal column
731: push(@values,$response->{$original_header});
732: }
733: }
1.25 matthew 734: } else {
1.69 bisitz 735: foreach my $original_header (@$headers) {
736: if ($original_header eq 'Time') {
737: push(@values,&Apache::lonlocal::locallocaltime($response->{$original_header}));
1.70 ! bisitz 738: } elsif (($original_header eq 'Submission') &&
! 739: !($resptype =~ /^(radiobutton|option|match|rank)$/)) {
! 740: # encode all submissions which have not been encoded above
! 741: push(@values,&HTML::Entities::encode($response->{$original_header},'<>&"'));
! 742: } elsif (($original_header eq 'Correct') &&
! 743: ($resptype eq 'radiobutton')) {
! 744: # encode foil separators
! 745: push(@values,&HTML::Entities::encode($response->{$original_header},'&'));
1.69 bisitz 746: } else {
747: # A normal column
748: push(@values,$response->{$original_header});
749: }
750: }
1.21 matthew 751: }
1.30 matthew 752: my $td = '<td valign="top">';
753: my $str = $td.join('</td>'.$td,@values).'</td>';
754: return $str;
1.21 matthew 755: }
1.18 matthew 756:
1.30 matthew 757:
1.21 matthew 758: #########################################################
759: #########################################################
760: ##
761: ## Excel Output Routines
762: ##
763: #########################################################
764: #########################################################
765: sub prepare_excel_output {
1.60 raeburn 766: my ($r,$Problems,$Students,$anoncounter,$show_named) = @_;
1.18 matthew 767: my $c = $r->connection();
768: #
1.19 matthew 769: #
770: # Determine the number of columns in the spreadsheet
771: my $columncount = 3; # username, domain, id
1.60 raeburn 772: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.20 matthew 773: my $lastprob;
1.31 matthew 774: my %problem_analysis;
1.19 matthew 775: foreach my $prob (@$Problems) {
1.60 raeburn 776: my $symb = $prob->symb();
1.31 matthew 777: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
778: $problem_analysis{$prob->src}=\%analysis;
779: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 780: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
781: next if ($show_named);
782: } else {
783: next unless ($show_named);
784: }
785:
1.31 matthew 786: my $responses = [$prob->responseIds($partid)];
787: my $resptypes = [$prob->responseType($partid)];
788: for (my $i=0;$i<scalar(@$responses);$i++) {
789: my @headers = &get_headers($prob,$partid,$responses->[$i],
790: $resptypes->[$i],
791: $problem_analysis{$prob->src},
792: 'excel','display',
793: @extra_resp_headers);
794: $columncount += scalar(@headers);
795: }
796: }
1.19 matthew 797: last if ($columncount > 255);
1.20 matthew 798: $lastprob = $prob;
1.19 matthew 799: }
800: if ($columncount > 255) {
1.68 golterma 801: $r->print('<p class="LC_error">'.&mt('Unable to complete request').'</p>'.$/.
802: '<p class="LC_warning">'.&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>'.$/.
803: '<p class="LC_warning">'.&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.60 raeburn 804: if (ref($lastprob)) {
805: $r->print('<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',$lastprob->compTitle).'</p>');
806: }
1.19 matthew 807: $r->rflush();
808: return;
809: }
810: #
811: # Print out a message telling them what we are doing
1.18 matthew 812: if (scalar(@$Problems) > 1) {
1.68 golterma 813: $r->print('<p class="LC_info">'.
1.18 matthew 814: &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
815: scalar(@$Problems)).
1.68 golterma 816: '</p>');
1.18 matthew 817: } else {
1.68 golterma 818: $r->print('<p class="LC_info">'.
1.18 matthew 819: &mt('Preparing Excel spreadsheet of student responses').
1.68 golterma 820: '</p>');
1.18 matthew 821: }
822: $r->rflush();
823: #
824: # Create the excel spreadsheet
1.36 matthew 825: my ($workbook,$filename,$format) =
826: &Apache::loncommon::create_workbook($r);
827: return if (! defined($workbook));
1.67 bisitz 828:
829: # Worksheet name
830: # The length of a worksheet name in Excel 95 is limited to 31 characters.
831: # Let's make sure to not exceed the maximum length in the translation files.
832: # Otherwise, no Excel file would be created.
833: my $worksheetname = &mt('Student Submission Data');
834: if (length($worksheetname) > 31) {
835: # nice way to cut off too long name
836: $worksheetname = substr($worksheetname,0,28,).'...';
837: }
838: my $worksheet = $workbook->addworksheet($worksheetname);
1.18 matthew 839: #
840: # Add headers to the worksheet
841: my $rows_output = 0;
842: $worksheet->write($rows_output++,0,
1.40 albertel 843: $env{'course.'.$env{'request.course.id'}.'.description'},
1.18 matthew 844: $format->{'h1'});
845: $rows_output++;
846: my $cols_output = 0;
847: my $title_row = $rows_output++;
848: my $partid_row = $rows_output++;
849: my $respid_row = $rows_output++;
850: my $header_row = $rows_output++;
1.67 bisitz 851: $worksheet->write($title_row ,0,&mt('Problem Title'),$format->{'bold'});
852: $worksheet->write($partid_row,0,&mt('Part ID'),$format->{'bold'});
853: $worksheet->write($respid_row,0,&mt('Response ID'),$format->{'bold'});
1.18 matthew 854: # Student headers
1.65 raeburn 855: my @StudentColumns = &get_student_columns($show_named);
856: foreach my $field (@StudentColumns) {
1.67 bisitz 857: $worksheet->write($header_row,$cols_output++,&mt(ucfirst($field)),
1.19 matthew 858: $format->{'bold'});
1.18 matthew 859: }
860: # Problem headers
1.31 matthew 861: my %start_col;
1.18 matthew 862: foreach my $prob (@$Problems) {
1.23 matthew 863: my $title = $prob->compTitle;
1.60 raeburn 864: my $symb = $prob->symb();
1.18 matthew 865: $worksheet->write($title_row,$cols_output,
866: $title,$format->{'h3'});
867: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 868: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
869: next if ($show_named);
870: } else {
871: next unless ($show_named);
872: }
1.24 matthew 873: $worksheet->write($partid_row,$cols_output,
874: $prob->part_display($partid));
1.18 matthew 875: my $responses = [$prob->responseIds($partid)];
876: my $resptypes = [$prob->responseType($partid)];
877: for (my $i=0;$i<scalar(@$responses);$i++) {
1.31 matthew 878: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
879: $cols_output;
1.18 matthew 880: $worksheet->write($respid_row,$cols_output,
881: $resptypes->[$i].', '.$responses->[$i]);
1.31 matthew 882: my @headers = &get_headers($prob,$partid,$responses->[$i],
883: $resptypes->[$i],
884: $problem_analysis{$prob->src},
885: 'excel','display',
886: @extra_resp_headers);
887: foreach my $text (@headers) {
888: if ($text eq 'Time') {
889: $worksheet->set_column($cols_output,$cols_output,undef,
890: $format->{'date'});
891: }
892: $worksheet->write($header_row,$cols_output++,$text);
1.20 matthew 893: }
1.18 matthew 894: }
895: }
896: }
897: #
898: # Populate the worksheet with the student data
1.66 www 899: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@$Students));
1.20 matthew 900: my $max_row = $rows_output;
1.18 matthew 901: foreach my $student (@$Students) {
902: last if ($c->aborted());
903: $cols_output = 0;
1.20 matthew 904: my $student_row = $max_row;
1.18 matthew 905: foreach my $prob (@$Problems) {
1.60 raeburn 906: my $symb = $prob->symb();
1.18 matthew 907: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 908: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
909: next if ($show_named);
910: } else {
911: next unless ($show_named);
912: }
1.18 matthew 913: my @Response = $prob->responseIds($partid);
914: my @ResponseType = $prob->responseType($partid);
915: for (my $i=0;$i<=$#Response;$i++) {
916: my $respid = $Response[$i];
917: my $resptype = $ResponseType[$i];
918: my $results =
919: &Apache::loncoursedata::get_response_data_by_student
920: ($student,$prob->symb(),$respid);
1.31 matthew 921: my @headers = &get_headers($prob,$partid,$respid,
922: $resptype,
923: $problem_analysis{$prob->src},
924: 'excel','normal',
925: @extra_resp_headers);
926:
1.20 matthew 927: if (! defined($results)) {
928: $results = [];
929: }
930: #
931: $rows_output = $student_row;
932: #
1.31 matthew 933: my $response_start_col = $start_col{$prob->symb}->{$partid}->{$respid};
1.20 matthew 934: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
935: $cols_output = $response_start_col;
1.40 albertel 936: if ($env{'form.all_sub'} ne 'true') {
1.20 matthew 937: next if ($j ne scalar(@$results)-1);
938: }
1.31 matthew 939: my $response = &hashify_response($results->[$j],
940: $prob,
941: $student,
942: $partid,
943: $respid);
944: my @response_data =
1.33 matthew 945: &compile_response_data(\@headers,$response,
946: $prob,$partid,$respid,
947: $resptype,
948: \&excel_format_item);
1.31 matthew 949: $worksheet->write_row($rows_output++,$cols_output,
950: \@response_data);
951: $cols_output+=scalar(@response_data);
1.20 matthew 952: if ($rows_output > $max_row) {
953: $max_row = $rows_output;
954: }
1.19 matthew 955: }
1.18 matthew 956: }
957: }
958: }
1.63 raeburn 959: # Prepend current student's user information to all rows
960: for (my $row = $student_row;$row<$max_row;$row++) {
1.28 matthew 961: my $cols = 0;
962: foreach my $field (@StudentColumns) {
1.60 raeburn 963: if ($show_named) {
1.65 raeburn 964: my $value = $student->{$field};
965: if ($field eq 'comments') {
966: $value = &Apache::lonmsgdisplay::retrieve_instructor_comments
967: ($student->{'username'},$student->{'domain'});
968: }
969: $worksheet->write($row,$cols++,$value);
1.60 raeburn 970: } else {
971: $worksheet->write($row,$cols++,
972: &mt('Anonymized'));
973: }
1.28 matthew 974: }
975: }
1.66 www 976: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1.18 matthew 977: }
978: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
979: #
980: # Close the excel file
981: $workbook->close();
982: #
983: # Write a link to allow them to download it
984: $r->print('<p><a href="'.$filename.'">'.
985: &mt('Your Excel spreadsheet.').
986: '</a></p>'."\n");
987: $r->print('<script>'.
988: 'window.document.Statistics.stats_status.value="'.
989: 'Done compiling spreadsheet. See link below to download.'.
990: '";</script>');
991: $r->rflush();
992: return;
993: }
994:
1.33 matthew 995: sub compile_response_data {
996: my ($headers,$response,$prob,$partid,$respid,$resptype,$format) = @_;
1.31 matthew 997: if (! ref($headers) || ref($headers) ne 'ARRAY' || ! scalar(@$headers)) {
998: return ();
999: }
1.33 matthew 1000: if (ref($format) ne 'CODE') {
1001: $format = sub { return $_[0]; };
1002: }
1.31 matthew 1003: #
1.33 matthew 1004: my $submission =
1005: &HTML::Entities::decode
1.45 www 1006: (&unescape($response->{'Submission'}));
1.42 albertel 1007: if (!$prob->is_task()) {
1008: return () if (! defined($submission) || $submission eq '');
1009: }
1.31 matthew 1010: $submission =~ s/\\\"/\"/g;
1011: $submission =~ s/\\\'/\'/g;
1012: if ($resptype eq 'radiobutton') {
1013: $submission =~ s/=([^=])$//;
1.20 matthew 1014: }
1.31 matthew 1015: $response->{'Submission'} = $submission;
1016: #
1017: my @values;
1018: if ($resptype =~ /^(option|match|rank)$/) {
1019: my %submission =
1020: map {
1.45 www 1021: my ($foil,$value) = split('=',&unescape($_));
1.31 matthew 1022: ($foil,$value);
1023: } split('&',$response->{'Submission'});
1024: my %correct;
1025: if (exists($response->{'Correct'})) {
1026: %correct =
1027: map {
1.45 www 1028: my ($foil,$value)=split('=',&unescape($_));
1.31 matthew 1029: ($foil,$value);
1030: } split('&',$response->{'Correct'});
1031: }
1032: #
1033: foreach my $original_header (@$headers) {
1034: if ($original_header =~ /^_/) {
1035: # '_' denotes a foil column
1036: my ($header) = ($original_header =~ m/^_(.*)$/);
1037: my $option = '';
1038: if ( my ($foil) = ($header =~ /(.*) Correct$/)) {
1039: if (exists($correct{$foil})) {
1040: $option = $correct{$foil};
1041: }
1042: } elsif (exists($submission{$header})) {
1043: $option = $submission{$header};
1044: }
1.33 matthew 1045: push(@values,&{$format}($option,$header));
1.31 matthew 1046: } else {
1047: # A normal column
1.33 matthew 1048: push(@values,&{$format}($response->{$original_header},
1.31 matthew 1049: $original_header));
1050: }
1051: }
1052: } else {
1.33 matthew 1053: @values = map { &{$format}($response->{$_},$_); } @$headers;
1.20 matthew 1054: }
1.31 matthew 1055: return @values;
1.20 matthew 1056: }
1057:
1.31 matthew 1058: sub excel_format_item {
1059: my ($item,$type) = @_;
1060: if ($type eq 'Time') {
1.33 matthew 1061: $item = &Apache::lonstathelpers::calc_serial($item);
1.31 matthew 1062: } else {
1063: if ($item =~ m/^=/) {
1064: $item = ' '.$item;
1065: }
1066: $item =~ s/\\r//g;
1067: $item =~ s/\\n/\n/g;
1068: $item =~ s/(\s*$|^\s*)//g;
1069: $item =~ s/\\\'/\'/g;
1.18 matthew 1070: }
1.31 matthew 1071: return $item;
1.1 matthew 1072: }
1073:
1074: #########################################################
1075: #########################################################
1.17 matthew 1076: ##
1077: ## CSV output of student answers
1078: ##
1079: #########################################################
1080: #########################################################
1081: sub prepare_csv_output {
1.60 raeburn 1082: my ($r,$problems,$students,$anoncounter,$show_named) = @_;
1.17 matthew 1083: my $c = $r->connection();
1084: #
1.68 golterma 1085: $r->print('<p class="LC_info">'.
1086: &mt('Generating CSV report of student responses').'</p>');
1.17 matthew 1087: #
1088: # Progress window
1.66 www 1089: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@$students));
1.22 matthew 1090:
1.17 matthew 1091: $r->rflush();
1092: #
1093: # Open a file
1094: my $outputfile;
1095: my $filename = '/prtspool/'.
1.40 albertel 1096: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.17 matthew 1097: time.'_'.rand(1000000000).'.csv';
1098: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
1099: $r->log_error("Couldn't open $filename for output $!");
1.55 bisitz 1100: $r->print(
1101: '<p class="LC_error">'
1102: .&mt('Problems occurred in writing the CSV file.')
1103: .' '.&mt('This error has been logged.')
1104: .' '.&mt('Please alert your LON-CAPA administrator.')
1105: .'</p>'
1106: );
1.17 matthew 1107: $outputfile = undef;
1108: }
1109: #
1.22 matthew 1110: # Compute the number of columns per response
1.60 raeburn 1111: my @extra_resp_headers = &get_extra_response_headers($show_named);
1.22 matthew 1112: #
1113: # Create the table header
1.65 raeburn 1114: my @student_columns = &get_student_columns($show_named);
1.60 raeburn 1115: my $student_column_count = scalar(@student_columns);
1.17 matthew 1116: #
1.22 matthew 1117: my %headers;
1118: push(@{$headers{'student'}},@student_columns);
1119: # Pad for the student data
1120: foreach my $row ('problem','part','response') {
1.32 matthew 1121: $headers{$row}=[map {''} @student_columns];
1.22 matthew 1122: }
1123: #
1124: # we put the headers into the %headers hash
1.32 matthew 1125: my %problem_analysis;
1126: my %start_col;
1.65 raeburn 1127: my $max_column = $student_column_count;
1.22 matthew 1128: foreach my $prob (@$problems) {
1.60 raeburn 1129: my $symb = $prob->symb();
1.32 matthew 1130: my %analysis = &Apache::lonstathelpers::get_problem_data($prob->src);
1131: $problem_analysis{$prob->src}=\%analysis;
1132: $headers{'problem'}->[$max_column] = $prob->compTitle;
1.22 matthew 1133: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 1134: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
1135: next if ($show_named);
1136: } else {
1137: next unless ($show_named);
1138: }
1.32 matthew 1139: $headers{'part'}->[$max_column] = $prob->part_display($partid);
1.22 matthew 1140: my $responses = [$prob->responseIds($partid)];
1.32 matthew 1141: my $resptypes = [$prob->responseType($partid)];
1.22 matthew 1142: for (my $i=0;$i<scalar(@$responses);$i++) {
1.32 matthew 1143: my @headers = &get_headers($prob,$partid,$responses->[$i],
1144: $resptypes->[$i],
1145: $problem_analysis{$prob->src},
1146: 'csv','display',
1147: @extra_resp_headers);
1148: $start_col{$prob->symb}->{$partid}->{$responses->[$i]}=
1149: $max_column;
1150: $headers{'response'}->[$max_column]=
1.22 matthew 1151: &mt('Response [_1]',$responses->[$i]);
1.32 matthew 1152: for (my $j=0;$j<=$#headers;$j++) {
1153: $headers{'student'}->[$max_column+$j]=$headers[$j];
1.22 matthew 1154: }
1.32 matthew 1155: $max_column += scalar(@headers);
1.17 matthew 1156: }
1157: }
1158: }
1.22 matthew 1159: foreach my $row ('problem','part','response','student') {
1.32 matthew 1160: print $outputfile '"'.
1.22 matthew 1161: join('","',
1162: map {
1163: &Apache::loncommon::csv_translate($_);
1164: } @{$headers{$row}}).'"'.$/;
1.17 matthew 1165: }
1166: #
1.22 matthew 1167: # Main loop
1168: foreach my $student (@$students) {
1.27 matthew 1169: last if ($c->aborted());
1.22 matthew 1170: my @rows;
1171: foreach my $prob (@$problems) {
1.60 raeburn 1172: my $symb = $prob->symb;
1.22 matthew 1173: foreach my $partid (@{$prob->parts}) {
1.60 raeburn 1174: if (($prob->is_anonsurvey($partid)) || ($anoncounter->{$symb."\0".$partid})) {
1175: next if ($show_named);
1176: } else {
1177: next unless ($show_named);
1178: }
1.22 matthew 1179: my @responses = $prob->responseIds($partid);
1180: my @response_type = $prob->responseType($partid);
1181: for (my $i=0;$i<=$#responses;$i++) {
1182: my $respid = $responses[$i];
1.32 matthew 1183: my $resptype = $response_type[$i];
1184: my @headers = &get_headers($prob,$partid,$respid,$resptype,
1185: $problem_analysis{$prob->src},
1186: 'csv','normal',
1187: @extra_resp_headers);
1.22 matthew 1188: my $results =
1189: &Apache::loncoursedata::get_response_data_by_student
1190: ($student,$prob->symb(),$respid);
1191: if (! defined($results)) {
1192: $results = [];
1193: }
1194: for (my $j=0; $j<scalar(@$results);$j++) {
1.40 albertel 1195: if ($env{'form.all_sub'} ne 'true') {
1.22 matthew 1196: next if ($j != 0);
1197: }
1198: my $idx = scalar(@$results) - $j - 1;
1.32 matthew 1199: my $response = &hashify_response($results->[$idx],
1200: $prob,$student,
1201: $partid,$respid);
1202: my @data = &compile_response_data(\@headers,$response,
1.22 matthew 1203: $prob,$partid,
1.33 matthew 1204: $respid,$resptype,
1205: \&csv_format_item);
1.32 matthew 1206: my $resp_start_idx =
1207: $start_col{$prob->symb}->{$partid}->{$respid};
1.22 matthew 1208: for (my $k=0;$k<=$#data;$k++) {
1.32 matthew 1209: $rows[$j]->[$resp_start_idx + $k] = $data[$k];
1.22 matthew 1210: }
1211: }
1212: }
1.17 matthew 1213: }
1.22 matthew 1214: }
1215: foreach my $row (@rows) {
1.60 raeburn 1216: my $student_row_data = '';
1217: if ($show_named) {
1.65 raeburn 1218: foreach my $field (@student_columns) {
1219: my $value = $student->{$field};
1220: if ($field eq 'comments') {
1221: $value = &Apache::lonmsgdisplay::retrieve_instructor_comments
1222: ($student->{'username'},$student->{'domain'});
1223: }
1224: $student_row_data .= '"'.&Apache::loncommon::csv_translate($value).'",';
1225: }
1226: $student_row_data =~ s/,$//;
1.60 raeburn 1227: } else {
1228: $student_row_data = '"'.&mt('Anonymized').'"';
1229: }
1230: print $outputfile $student_row_data;
1231: for (my $i=$student_column_count;$i<$max_column;$i++) {
1.22 matthew 1232: my $value = &Apache::loncommon::csv_translate($row->[$i]);
1233: $value ||='';
1234: print $outputfile ',"'.$value.'"';
1.17 matthew 1235: }
1.22 matthew 1236: print $outputfile $/;
1.17 matthew 1237: }
1.22 matthew 1238: undef(@rows);
1.66 www 1239: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1.17 matthew 1240: }
1241: close($outputfile);
1242: #
1243: # Close the progress window
1244: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1245: #
1.48 bisitz 1246: # Tell the user where to get their CSV file
1.17 matthew 1247: $r->print('<br />'.
1.48 bisitz 1248: '<a href="'.$filename.'">'.&mt('Your CSV file.').'</a>'."\n");
1.17 matthew 1249: $r->rflush();
1250: return;
1251: }
1252:
1.32 matthew 1253: sub csv_format_item {
1254: my ($item,$type) = @_;
1255: if ($type eq 'Time') {
1.69 bisitz 1256: $item = &Apache::lonlocal::locallocaltime($item);
1.33 matthew 1257: }
1.32 matthew 1258: $item =&Apache::loncommon::csv_translate($item);
1259: return $item;
1.15 matthew 1260: }
1261:
1.1 matthew 1262: #########################################################
1263: #########################################################
1264: ##
1265: ## Generic Interface Routines
1266: ##
1267: #########################################################
1268: #########################################################
1269: sub CreateInterface {
1270: ##
1.16 matthew 1271: ## Output Selection
1.19 matthew 1272: my $output_selector = $/.'<select name="output">'.$/;
1.22 matthew 1273: foreach ('HTML','Excel','CSV') {
1.19 matthew 1274: $output_selector .= ' <option value="'.lc($_).'"';
1.40 albertel 1275: if ($env{'form.output'} eq lc($_)) {
1.59 bisitz 1276: $output_selector .= ' selected="selected"';
1.16 matthew 1277: }
1.19 matthew 1278: $output_selector .='>'.&mt($_).'</option>'.$/;
1.16 matthew 1279: }
1.19 matthew 1280: $output_selector .= '</select>'.$/;
1.16 matthew 1281: ##
1.1 matthew 1282: ## Environment variable initialization
1283: my $Str = '';
1.59 bisitz 1284: $Str .= '<br />';
1.50 bisitz 1285: $Str .= &Apache::loncommon::start_data_table();
1286: $Str .= &Apache::loncommon::start_data_table_header_row();
1.16 matthew 1287: $Str .= '<th>'.&mt('Sections').'</th>';
1.44 raeburn 1288: $Str .= '<th>'.&mt('Groups').'</th>';
1.57 onken 1289: $Str .= '<th>'.&mt('Student Data').&Apache::loncommon::help_open_topic("Chart_Student_Data").'</th>';
1.46 raeburn 1290: $Str .= '<th>'.&mt('Access Status').'</th>';
1.50 bisitz 1291: $Str .= '<th>'.&mt('Options').'</th>';
1292: $Str .= '<th>'.&mt('Output Format').'</th>';
1293: $Str .= &Apache::loncommon::end_data_table_header_row();
1.11 matthew 1294: #
1.50 bisitz 1295: $Str .= &Apache::loncommon::start_data_table_row();
1296: $Str .= '<td align="center">'."\n";
1.1 matthew 1297: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1298: $Str .= '</td>';
1299: #
1.44 raeburn 1300: $Str .= '<td align="center">'."\n";
1301: $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
1302: $Str .= '</td>';
1303: #
1.57 onken 1304: $Str .= '<td align="center">'."\n";
1305: $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple', 5,undef);
1306: $Str .= '</td>';
1307: #
1.1 matthew 1308: $Str .= '<td align="center">';
1309: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1310: $Str .= '</td>';
1.6 matthew 1311: #
1.15 matthew 1312: # Render problem checkbox
1313: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1.40 albertel 1314: if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1.50 bisitz 1315: $prob_checkbox .= 'checked="checked" ';
1.15 matthew 1316: }
1317: $prob_checkbox .= 'value="true" />';
1318: #
1319: # Compute correct answers checkbox
1320: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1.40 albertel 1321: if (exists($env{'form.correctans'}) && $env{'form.correctans'} eq 'true') {
1.50 bisitz 1322: $ans_checkbox .= 'checked="checked" ';
1.13 matthew 1323: }
1.15 matthew 1324: $ans_checkbox .= 'value="true" />';
1325: #
1.19 matthew 1326: # Show all submissions checkbox
1327: my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
1.40 albertel 1328: if (exists($env{'form.all_sub'}) &&
1329: $env{'form.all_sub'} eq 'true') {
1.50 bisitz 1330: $all_sub_checkbox .= 'checked="checked" ';
1.15 matthew 1331: }
1.19 matthew 1332: $all_sub_checkbox.= 'value="true" />';
1.15 matthew 1333: #
1.20 matthew 1334: # problem status checkbox
1335: my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
1.40 albertel 1336: if (exists($env{'form.prob_status'}) &&
1337: $env{'form.prob_status'} eq 'true') {
1.50 bisitz 1338: $prob_status_checkbox .= 'checked="checked" ';
1.15 matthew 1339: }
1.20 matthew 1340: $prob_status_checkbox .= 'value="true" />';
1.15 matthew 1341: #
1.56 bisitz 1342: $Str .=
1343: '<td valign="top">'
1344: .'<label>'
1345: .$prob_checkbox.&mt('Show problem')
1346: .'</label><br />'
1347: .'<label>'
1348: .' '.$ans_checkbox.&mt('Show correct answers')
1349: .'</label><br />'
1350: .'<label>'
1351: .$all_sub_checkbox.&mt('Show all submissions')
1352: .'</label><br />'
1353: .'<label>'
1354: .$prob_status_checkbox.&mt('Show problem grading')
1355: .'</label>'
1356: .'</td>';
1.13 matthew 1357: #
1.50 bisitz 1358: $Str .= '<td align="center" valign="top">'.$output_selector.'</td>';
1359: #
1360: $Str .= &Apache::loncommon::end_data_table_row();
1361: $Str .= &Apache::loncommon::end_data_table();
1.1 matthew 1362: #
1363: return $Str;
1364: }
1365:
1.65 raeburn 1366: sub get_student_columns {
1367: my ($show_named) = @_;
1368: my @student_columns;
1369: if ($show_named) {
1370: @student_columns = @Apache::lonstatistics::SelectedStudentData;
1371: if (grep(/^all$/,@student_columns)) {
1372: @student_columns = qw(fullname username domain id section status groups comments);
1373: }
1374: } else {
1375: @student_columns = ('username');
1376: }
1377: return @student_columns;
1378: }
1379:
1.1 matthew 1380: 1;
1381:
1382: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>