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