Annotation of loncom/interface/statistics/lonstudentsubmissions.pm, revision 1.22
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.22 ! matthew 3: # $Id: lonstudentsubmissions.pm,v 1.21 2004/09/22 15:38:49 matthew Exp $
1.1 matthew 4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: package Apache::lonstudentsubmissions;
28:
29: use strict;
30: use Apache::lonnet();
31: use Apache::loncommon();
32: use Apache::lonhtmlcommon();
33: use Apache::loncoursedata();
34: use Apache::lonstatistics;
35: use Apache::lonlocal;
36: use Apache::lonstathelpers;
37: use HTML::Entities();
38: use Time::Local();
39: use Spreadsheet::WriteExcel();
40:
1.18 matthew 41: my @SubmitButtons = ({ name => 'SelectAnother',
1.1 matthew 42: text => 'Choose a different Problem' },
43: { name => 'Generate',
1.16 matthew 44: text => 'Generate Report'},
1.1 matthew 45: );
46:
47: sub BuildStudentSubmissionsPage {
48: my ($r,$c)=@_;
49: #
50: my %Saveable_Parameters = ('Status' => 'scalar',
51: 'Section' => 'array',
52: 'NumPlots' => 'scalar',
53: );
54: &Apache::loncommon::store_course_settings('student_submissions',
55: \%Saveable_Parameters);
56: &Apache::loncommon::restore_course_settings('student_submissions',
57: \%Saveable_Parameters);
58: #
59: &Apache::lonstatistics::PrepareClasslist();
60: #
61: $r->print(&CreateInterface());
62: #
63: my @Students = @Apache::lonstatistics::Students;
64: #
65: if (@Students < 1) {
66: $r->print('<h2>There are no students in the sections selected</h2>');
67: }
68: #
1.11 matthew 69: my @CacheButtonHTML =
1.19 matthew 70: &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status',
71: '<h3>'.&mt('Loading student data').'</h3>');
1.1 matthew 72: $r->rflush();
73: #
74: if (exists($ENV{'form.problemchoice'}) &&
75: ! exists($ENV{'form.SelectAnother'})) {
76: foreach my $button (@SubmitButtons) {
77: if ($button->{'name'} eq 'break') {
78: $r->print("<br />\n");
79: } else {
80: $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
81: 'value="'.&mt($button->{'text'}).'" />');
82: $r->print(' 'x5);
83: }
84: }
1.11 matthew 85: foreach my $html (@CacheButtonHTML) {
86: $r->print($html.(' 'x5));
87: }
1.1 matthew 88: #
1.18 matthew 89: $r->print('<hr />'.$/);
1.1 matthew 90: $r->rflush();
91: #
1.18 matthew 92: # Determine which problems we are to analyze
93: my @Symbs =
94: &Apache::lonstathelpers::get_selected_symbs('problemchoice');
95: foreach my $selected (@Symbs) {
96: $r->print('<input type="hidden" name="problemchoice" value="'.
97: $selected.'" />'.$/);
98: }
1.1 matthew 99: #
1.18 matthew 100: # Get resource objects
101: my $navmap = Apache::lonnavmaps::navmap->new();
102: if (!defined($navmap)) {
103: $r->print('<h1>'.&mt("Internal error").'</h1>');
104: return;
105: }
106: my %already_seen;
107: my @Problems;
108: foreach my $symb (@Symbs) {
109: my $resource = $navmap->getBySymb($symb);
110: push(@Problems,$resource);
1.1 matthew 111: }
112: #
1.18 matthew 113: if (! scalar(@Problems) || ! defined($Problems[0])) {
1.1 matthew 114: $r->print('resource is undefined');
115: } else {
1.18 matthew 116: if (scalar(@Problems) == 1) {
117: my $resource = $Problems[0];
118: $r->print('<h1>'.$resource->title.'</h1>');
119: $r->print('<h3>'.$resource->src.'</h3>');
120: if ($ENV{'form.renderprob'} eq 'true') {
121: $r->print(
122: &Apache::lonstathelpers::render_resource({src => $resource->src})
123: );
124: $r->rflush();
125: }
126: }
1.21 matthew 127: if ($ENV{'form.output'} eq 'excel') {
128: &prepare_excel_output($r,\@Problems,\@Students);
1.22 ! matthew 129: } elsif ($ENV{'form.output'} eq 'csv') {
! 130: &prepare_csv_output($r,\@Problems,\@Students);
1.21 matthew 131: } else {
132: &prepare_html_output($r,\@Problems,\@Students);
133: }
1.1 matthew 134: }
135: $r->print('<hr />');
136: } else {
137: $r->print('<input type="submit" name="Generate" value="'.
1.17 matthew 138: &mt('Prepare Report').'" />');
1.1 matthew 139: $r->print(' 'x5);
1.19 matthew 140: $r->print('<p>'.
141: &mt('Computing correct answers greatly increasese the amount of time required to prepare a report.').
142: '</p>');
143: $r->print('<p>'.
144: &mt('please select problems and use the <b>Prepare Report</b> button to continue.').
145: '</p>');
1.18 matthew 146: $r->print(&Apache::lonstathelpers::MultipleProblemSelector
1.19 matthew 147: (undef,'problemchoice','Statistics'));
1.18 matthew 148: }
149: }
150:
151: #########################################################
152: #########################################################
1.21 matthew 153: ##
154: ## HTML Output Routines
155: ##
156: #########################################################
157: #########################################################
158: sub prepare_html_output {
159: my ($r,$problems,$students) = @_;
160: my $c = $r->connection();
161: #
162: # Set a flag for the case when there is just one problem
163: my $single_response = 0;
164: if (scalar(@$problems) == 1 &&
165: $problems->[0]->countResponses == 1) {
166: $single_response = 1;
167: }
168: #
169: # Compute the number of columns per response
170: my @response_headers = ('Submission');
171: if ($ENV{'form.correctans'} eq 'true') {
172: push(@response_headers,'Correct');
173: }
174: if ($ENV{'form.prob_status'} eq 'true') {
175: push(@response_headers,'Award Detail');
176: push(@response_headers,'Time');
177: push(@response_headers,'Attempt');
178: push(@response_headers,'Awarded');
179: }
180: my $response_multiplier = scalar(@response_headers);
181: #
182: # Create the table header
183: my @student_columns = ('username','domain','id');
184: #
185: my %headers;
186: my $student_column_count = scalar(@student_columns);
187: $headers{'problem'} = qq{<th colspan="$student_column_count">\ </th>};
188: foreach (@student_columns) {
189: $headers{'student'}.= '<th>'.ucfirst($_).'</th>';
190: }
191: #
192: # we put the headers into the %headers hash
193: my $total_col = scalar(@student_columns);
194: my $nonempty_part_headers = 0;
195: foreach my $prob (@$problems) {
196: my $prob_span = 0;
197: my $single_part = 0;
198: if (scalar(@{$prob->parts}) == 1) {
199: $single_part = 1;
200: }
201: foreach my $partid (@{$prob->parts}) {
202: my $part_span = 0;
203: my $responses = [$prob->responseIds($partid)];
204: my $resptypes = [$prob->responseType($partid)];
205: for (my $i=0;$i<scalar(@$responses);$i++) {
206: if ($resptypes->[$i] eq 'essay') {
207: # do something clever, like shut up.
208: } else {
209: $total_col += scalar(@response_headers);
210: $headers{'response'} .=
211: '<th colspan="'.scalar(@response_headers).'">'.
212: &mt('Response [_1]',$responses->[$i]).'</th>';
213: $headers{'student'}.= '<th>'.join('</th><th>',
214: @response_headers).
215: '</th>';
216: $part_span += scalar(@response_headers);
217: }
218: }
219: if (! $single_part) {
220: my $tmpname = $partid;
221: if ($partid =~/^\d+$/) {
222: $tmpname = &mt('Part [_1]',$partid);
223: }
224: $headers{'part'} .= qq{<th colspan="$part_span">$tmpname</th>};
225: $nonempty_part_headers = 1;
226: } else {
227: $headers{'part'} .= qq{<th colspan="$part_span"> </th>};
228: }
229: $prob_span += $part_span;
230: }
231: my $title = &get_title($prob->title,$prob->src);
232: if ($prob_span > 0) {
233: $headers{'problem'}.= qq{<th colspan="$prob_span">$title</th>};
234: } elsif ($single_response) {
235: $prob_span = scalar(@student_columns);
236: $headers{'problem'} = qq{<th colspan="$prob_span">$title</th>};
237: }
238: }
239: if (exists($headers{'part'})) {
240: $headers{'part'} = qq{<th colspan="$student_column_count">\ </th>}.
241: $headers{'part'};
242: }
243: if (exists($headers{'response'})) {
244: $headers{'response'}=
245: qq{<th colspan="$student_column_count">\ </th>}.
246: $headers{'response'};
247: }
248: my $full_header = $/.'<table>'.$/;
249: $full_header .= '<tr align="left">'.$headers{'problem'}.'</tr>'.$/;
250: if ($nonempty_part_headers) {
251: $full_header .= '<tr align="left">'.$headers{'part'}.'</tr>'.$/;
252: }
253: $full_header .= '<tr align="left">'.$headers{'response'}.'</tr>'.$/;
254: $full_header .= '<tr align="left">'.$headers{'student'}.'</tr>'.$/;
255: #
256: # Main loop
257: my $count;
258: $r->print($/.$full_header.$/);
259: my $row_class = 'odd'; # css
260: foreach my $student (@$students) {
261: my $student_row_data;
262: if ($count++ >= 30) {
263: $r->print('</table>'.$/.$full_header.$/);
264: $count = 0;
265: }
266: last if ($c->aborted());
267: foreach my $field (@student_columns) {
268: $student_row_data .=
269: '<td valign="top">'.$student->{$field}.'</td>';
270: }
271: #
272: # Figure out what it is we need to output for this student
273: my @essays;
274: my %problem_data;
275: my $maxrow;
276: foreach my $prob (@$problems) {
277: my $symb = $prob->symb;
278: $problem_data{$symb}={};
279: foreach my $partid (@{$prob->parts}) {
280: my @responses = $prob->responseIds($partid);
281: my @response_type = $prob->responseType($partid);
282: for (my $i=0;$i<=$#responses;$i++) {
283: my $respid = $responses[$i];
284: my $resptype = $response_type[$i];
285: my $width = scalar(@response_headers);
286: $problem_data{$symb}->{$partid}->{$respid}={};
287: my $resp_data = $problem_data{$symb}->{$partid}->{$respid};
288: $resp_data->{'fake'} = qq{<td colspan="$width"> </td>};
289: my $results =
290: &Apache::loncoursedata::get_response_data_by_student
291: ($student,$prob->symb(),$respid);
292: if (! defined($results)) {
293: $results = [];
294: }
295: #
296: if (scalar(@$results) > $maxrow && $resptype ne 'essay') {
297: $maxrow = scalar(@$results);
298: }
299: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
300: my $response = $results->[$j];
301: if ($ENV{'form.all_sub'} ne 'true') {
302: next if ($j ne scalar(@$results)-1);
303: }
304: if ($resptype eq 'essay') {
305: push(@essays,
306: &html_essay_results($student,
307: $prob,$partid,$respid,
308: $response,
309: $single_response).
310: '</td>');
311: } else {
312: push(@{$resp_data->{'real'}},
313: &html_results($student,
314: $prob,$partid,$respid,
315: $response,$resptype));
316: }
317: }
318: } # end of $i loop
319: } # end of partid loop
320: } # end of prob loop
321: #
322: # if there is no data, skip this student.
323: next if (! $maxrow && ! scalar(@essays));
324: #
325: # Go through the problem data and output a row.
326: if ($row_class eq 'even') {
327: $row_class = 'odd';
328: } else {
329: $row_class = 'even';
330: }
331: my $printed_something;
332: for (my $rows_output = 0;$rows_output<$maxrow;$rows_output++) {
333: my $html;
334: my $no_data = 1;
335: foreach my $prob (@$problems) {
336: my $symb = $prob->symb;
337: foreach my $partid (@{$prob->parts}) {
338: my @responses = $prob->responseIds($partid);
339: my @response_type = $prob->responseType($partid);
340: for (my $i=0;$i<=$#responses;$i++) {
341: my $respid = $responses[$i];
342: my $resp_data =
343: $problem_data{$symb}->{$partid}->{$respid};
344: next if ($response_type[$i] eq 'essay');
345: if (defined($resp_data->{'real'}->[$rows_output])) {
346: $html .= $resp_data->{'real'}->[$rows_output];
347: $no_data = 0;
348: } else {
349: $html .= $resp_data->{'fake'};
350: }
351: }
352: }
353: }
354: if (! $no_data) {
355: $r->print(qq{<tr class="$row_class">$student_row_data$html</tr>}.$/);
356: $printed_something=1;
357: }
358: }
359: if (@essays) {
360: my $tr = qq{<tr class="$row_class">};
361: my $td = qq{<td valign="top" class="essay" colspan="$total_col">};
362: if (! $printed_something) {
363: $r->print($tr.$student_row_data.'</tr>'.$/);
364: }
365: $r->print($tr.$td.
366: join('</td></tr>'.$/.$tr.$td,@essays).'</td></tr>'.$/);
367: undef(@essays);
368: }
369: } # end of student loop
370: return;
371: }
372:
373: #####################################################
374: ##
375: ## HTML helper routines
376: ##
377: #####################################################
378: sub html_essay_results {
379: my ($student,$prob,$partid,$respid,$response,$single_response)=@_;
380: #
381: my $submission =$response->[&Apache::loncoursedata::RDs_submission()];
382: $submission = &html_format_sub($submission,'essay');
383: #
384: my $correct = '';
385: if ($ENV{'form.correctans'} eq 'true') {
386: $correct = &Apache::lonstathelpers::analyze_problem_as_student
387: ($prob,$student->{'username'},$student->{'domain'},
388: $partid,$respid);
389: $correct = &html_format_sub($correct,'essay');
390: }
391: my $Str;
392: if (! $single_response) {
393: my $id = &get_title($prob->title,$prob->src);
394: if (defined($partid) && $partid ne '0') {
395: $id .= ' '.$partid;
396: }
397: if (defined($respid)) {
398: $id .= ' '.$respid;
399: }
400: $Str .= '<nobr>'.$id.'</nobr>'.(' 'x4);
401: }
402: if ($ENV{'form.prob_status'} eq 'true') {
403: $Str .= '<nobr>';
404: $Str .= $response->[&Apache::loncoursedata::RDs_awarddetail()].
405: (' 'x4);
406: $Str .= &mt('Attempt [_1]',
407: $response->[&Apache::loncoursedata::RDs_tries()]).
408: (' 'x4);
409: $Str .= &Apache::lonlocal::locallocaltime
410: ($response->[&Apache::loncoursedata::RDs_timestamp()]).
411: (' 'x4);
412: $Str .= &mt('Awarded: [_1]',
413: $response->[&Apache::loncoursedata::RDs_awarded()]).
414: (' 'x4);
415: $Str .= '</nobr><br />';
416: }
417: $Str .= $submission;
418: if (defined($correct) && $correct !~ /^\s*$/) {
419: $Str .= '<hr /><b>'.&mt('Correct').'</b>'.$correct
420: }
421: return $Str;
422: }
423:
424: sub html_results {
425: my ($student,$prob,$partid,$respid,$response,$resptype) = @_;
426: my $submission =$response->[&Apache::loncoursedata::RDs_submission()];
427: $submission = &html_format_sub($submission,$resptype);
428: my $correct = '';
429: if ($ENV{'form.correctans'} eq 'true') {
430: $correct = &Apache::lonstathelpers::analyze_problem_as_student
431: ($prob,$student->{'username'},$student->{'domain'},
432: $partid,$respid);
433: $correct = &html_format_sub($correct,$resptype);
434: }
435: my $Str;
436: $Str .= '<td valign="top">'.$submission.'</td>';
437: if ($ENV{'form.correctans'} eq 'true') {
438: $Str .= '<td valign="top">'.$correct.'</td>';
439: }
440: if ($ENV{'form.prob_status'} eq 'true') {
441: $Str .= '<td valign="top">'.
442: $response->[&Apache::loncoursedata::RDs_awarddetail()].
443: '</td>';
444: $Str .= '<td valign="top"><nobr>'.
445: &Apache::lonlocal::locallocaltime
446: ($response->[&Apache::loncoursedata::RDs_timestamp()]).
447: '</nobr></td>';
448: $Str .= '<td valign="top">'.
449: $response->[&Apache::loncoursedata::RDs_tries()].
450: '</td>';
451: $Str .= '<td valign="top">'.
452: $response->[&Apache::loncoursedata::RDs_awarded()].
453: '</td>';
454: }
455: return $Str;
456: }
457:
458: sub html_format_sub {
459: my ($submission,$resptype) = @_;
460: return '' if (! defined($submission) || $submission eq '');
461: if ($resptype eq 'essay') {
462: $submission =~ s|\\r\\n|$/|g;
463: $submission = &HTML::Entities::encode($submission,'<>&"');
464: $submission =~ s|$/\s*$/|$/</p><p>$/|g;
465: $submission =~ s|\\||g;
466: $submission = '<p>'.$submission.'</p>';
467: } elsif ($resptype eq 'radiobutton') {
468: $submission =~ s/=([^=])$//;
469: } elsif ($resptype =~ /^(option|match|rank)$/) {
470: $submission =
471: '<ul class="sub_studentans">'.
472: '<li>'.join('</li><li>',
473: map {
474: &Apache::lonnet::unescape($_) ;
475: } sort split('&',$submission)
476: ).
477: '</li><ul>';
478: }
479: return $submission;
480: }
1.18 matthew 481:
1.21 matthew 482: #########################################################
483: #########################################################
484: ##
485: ## Excel Output Routines
486: ##
487: #########################################################
488: #########################################################
489: sub prepare_excel_output {
1.19 matthew 490: my ($r,$Problems,$Students) = @_;
1.18 matthew 491: my $c = $r->connection();
492: #
1.19 matthew 493: #
494: # Determine the number of columns in the spreadsheet
495: my $columncount = 3; # username, domain, id
1.20 matthew 496: my $response_multiplier = 1;
497: $response_multiplier ++ if ($ENV{'form.correctans'} eq 'true');
498: $response_multiplier += 4 if ($ENV{'form.prob_status'} eq 'true');
499: my $lastprob;
1.19 matthew 500: foreach my $prob (@$Problems) {
1.20 matthew 501: $columncount += ($response_multiplier * $prob->countResponses);
1.19 matthew 502: last if ($columncount > 255);
1.20 matthew 503: $lastprob = $prob;
1.19 matthew 504: }
505: if ($columncount > 255) {
506: $r->print('<h1>'.&mt('Unable to complete request').'</h1>'.$/.
507: '<p>'.&mt('LON-CAPA is unable to produce your Excel spreadsheet because your selections will result in more than 255 columns. Excel allows only 255 columns in a spreadsheet.').'</p>'.$/.
1.20 matthew 508: '<p>'.&mt('Consider selecting fewer problems to generate reports on, or reducing the number of items per problem. Or use HTML or CSV output.').'</p>'.$/.
509: '<p>'.&mt('The last problem that will fit in the current spreadsheet is [_1].',&get_title($lastprob->title,$lastprob->src)).'</p>');
1.19 matthew 510: $r->rflush();
511: return;
512: }
513: #
514: # Print out a message telling them what we are doing
1.18 matthew 515: if (scalar(@$Problems) > 1) {
516: $r->print('<h2>'.
517: &mt('Preparing Excel spreadsheet of student responses to [_1] problems',
518: scalar(@$Problems)).
519: '</h2>');
520: } else {
521: $r->print('<h2>'.
522: &mt('Preparing Excel spreadsheet of student responses').
523: '</h2>');
524: }
525: $r->rflush();
526: #
527: # Create the excel spreadsheet
528: my $filename = '/prtspool/'.
529: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
530: time.'_'.rand(1000000000).'.xls';
531: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
532: if (! defined($workbook)) {
533: $r->log_error("Error creating excel spreadsheet $filename: $!");
534: $r->print('<p>'.&mt("Unable to create new Excel file. ".
535: "This error has been logged. ".
536: "Please alert your LON-CAPA administrator").
537: '</p>');
538: return undef;
539: }
540: #
541: $workbook->set_tempdir('/home/httpd/perl/tmp');
542: #
543: my $format = &Apache::loncommon::define_excel_formats($workbook);
544: my $worksheet = $workbook->addworksheet('Student Submission Data');
545: #
546: # Add headers to the worksheet
547: my $rows_output = 0;
548: $worksheet->write($rows_output++,0,
549: $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
550: $format->{'h1'});
551: $rows_output++;
552: my $cols_output = 0;
553: my $title_row = $rows_output++;
554: my $partid_row = $rows_output++;
555: my $respid_row = $rows_output++;
556: my $header_row = $rows_output++;
557: $worksheet->write($title_row ,0,'Problem Title',$format->{'bold'});
558: $worksheet->write($partid_row,0,'Part ID',$format->{'bold'});
559: $worksheet->write($respid_row,0,'Response ID',$format->{'bold'});
560: # Student headers
561: my @StudentColumns = ('username','domain','id');
562: foreach (@StudentColumns) {
1.19 matthew 563: $worksheet->write($header_row,$cols_output++,ucfirst($_),
564: $format->{'bold'});
1.18 matthew 565: }
566: # Problem headers
567: foreach my $prob (@$Problems) {
568: my $title = &get_title($prob->title,$prob->src);
569: $worksheet->write($title_row,$cols_output,
570: $title,$format->{'h3'});
571: foreach my $partid (@{$prob->parts}) {
572: $worksheet->write($partid_row,$cols_output,$partid);
573: my $responses = [$prob->responseIds($partid)];
574: my $resptypes = [$prob->responseType($partid)];
575: for (my $i=0;$i<scalar(@$responses);$i++) {
576: $worksheet->write($respid_row,$cols_output,
577: $resptypes->[$i].', '.$responses->[$i]);
578: $worksheet->write($header_row,$cols_output,'Submission');
579: $cols_output++;
1.19 matthew 580: if ($ENV{'form.correctans'} eq 'true') {
581: $worksheet->write($header_row,$cols_output,'Correct');
582: $cols_output++;
583: }
1.20 matthew 584: if ($ENV{'form.prob_status'} eq 'true') {
585: $worksheet->write($header_row,$cols_output++,
586: 'Award Detail');
587: $worksheet->write($header_row,$cols_output++,'Attempt');
588: $worksheet->write($header_row,$cols_output++,'Time');
589: $worksheet->write($header_row,$cols_output++,'Awarded');
590: }
1.18 matthew 591: }
592: }
593: }
594: #
595: # Populate the worksheet with the student data
596: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
597: ($r,'Excel File Compilation Status',
598: 'Excel File Compilation Progress',
599: scalar(@$Students),'inline',undef,'Statistics','stats_status');
1.20 matthew 600: my $max_row = $rows_output;
1.18 matthew 601: foreach my $student (@$Students) {
602: last if ($c->aborted());
603: $cols_output = 0;
1.20 matthew 604: my $student_row = $max_row;
1.18 matthew 605: foreach my $field (@StudentColumns) {
1.20 matthew 606: $worksheet->write($student_row,$cols_output++,
1.18 matthew 607: $student->{$field});
608: }
1.20 matthew 609: my $last_student_col = $cols_output-1;
610: my $response_count;
1.18 matthew 611: foreach my $prob (@$Problems) {
612: foreach my $partid (@{$prob->parts}) {
613: my @Response = $prob->responseIds($partid);
614: my @ResponseType = $prob->responseType($partid);
615: for (my $i=0;$i<=$#Response;$i++) {
1.20 matthew 616: my $response_start_col = $last_student_col +
617: $response_count * $response_multiplier + 1;
618: $response_count++;
1.18 matthew 619: my $respid = $Response[$i];
620: my $resptype = $ResponseType[$i];
621: my $results =
622: &Apache::loncoursedata::get_response_data_by_student
623: ($student,$prob->symb(),$respid);
1.20 matthew 624: if (! defined($results)) {
625: $results = [];
626: }
627: #
628: $rows_output = $student_row;
629: #
630: for (my $j=scalar(@$results)-1;$j>=0;$j--) {
631: $cols_output = $response_start_col;
632: my $response = $results->[$j];
633: if ($ENV{'form.all_sub'} ne 'true') {
634: next if ($j ne scalar(@$results)-1);
635: }
636: my $cols_output = &write_excel_row
637: ($worksheet,
638: $rows_output++,$cols_output,
639: $response,$student,
640: $prob,$partid,$respid,
641: $format,$resptype);
642: if ($rows_output > $max_row) {
643: $max_row = $rows_output;
644: }
1.19 matthew 645: }
1.18 matthew 646: }
647: }
648: }
649: $rows_output++;
650: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
651: 'last student');
652: }
653: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
654: #
655: # Close the excel file
656: $workbook->close();
657: #
658: # Write a link to allow them to download it
659: $r->print('<p><a href="'.$filename.'">'.
660: &mt('Your Excel spreadsheet.').
661: '</a></p>'."\n");
662: $r->print('<script>'.
663: 'window.document.Statistics.stats_status.value="'.
664: 'Done compiling spreadsheet. See link below to download.'.
665: '";</script>');
666: $r->rflush();
667: return;
668: }
669:
1.20 matthew 670: sub write_excel_row {
671: my ($worksheet,$row,$col,$response,$student,$prob,$partid,$respid,
672: $format,$resptype) = @_;
673: #
674: my $submission =$response->[&Apache::loncoursedata::RDs_submission()];
675: $submission = &excel_format_response($submission,$resptype);
676: $worksheet->write($row,$col++,$submission);
677: if ($ENV{'form.correctans'} eq 'true') {
678: my $correct = &Apache::lonstathelpers::analyze_problem_as_student
679: ($prob,$student->{'username'},$student->{'domain'},
680: $partid,$respid);
681: $correct =&excel_format_response($correct,$resptype);
682: $worksheet->write($row,$col++,$correct);
683: }
684: if ($ENV{'form.prob_status'} eq 'true') {
685: $worksheet->write
686: ($row,$col++,
687: $response->[&Apache::loncoursedata::RDs_awarddetail()]);
688: $worksheet->write
689: ($row,$col++,$response->[&Apache::loncoursedata::RDs_tries()]);
690: $worksheet->write
691: ($row,$col++,
692: &Apache::lonstathelpers::calc_serial
693: ($response->[&Apache::loncoursedata::RDs_timestamp()]),
694: $format->{'date'});
695: $worksheet->write
696: ($row,$col++,$response->[&Apache::loncoursedata::RDs_awarded()]);
697: }
698: return $col;
699: }
700:
1.18 matthew 701: sub get_title {
702: my ($title,$src) = @_;
703: if ($title eq '') {
704: ($title) = ($src =~ m|/([^/]+)$|);
705: } else {
706: $title =~ s/\:/:/g;
707: }
708: return $title;
709: }
710:
711: sub excel_format_response {
712: my ($answer,$responsetype) = @_;
713: if ($responsetype eq 'radiobutton') {
714: $answer =~ s/=([^=])$//;
1.19 matthew 715: } elsif ($responsetype =~ /^(option|match)$/) {
1.18 matthew 716: $answer = join("\n",
717: map {
718: &Apache::lonnet::unescape($_) ;
719: } sort split('&',$answer)
720: );
1.1 matthew 721: }
1.18 matthew 722: if ($answer =~ m/^=/) {
723: $answer = ' '.$answer;
724: }
725: return $answer;
1.1 matthew 726: }
727:
728: #########################################################
729: #########################################################
1.17 matthew 730: ##
731: ## CSV output of student answers
732: ##
733: #########################################################
734: #########################################################
735: sub prepare_csv_output {
1.22 ! matthew 736: my ($r,$problems,$students) = @_;
1.17 matthew 737: my $c = $r->connection();
738: #
739: $r->print('<h2>'.
740: &mt('Generating CSV report of student responses').'</h2>');
741: #
742: # Progress window
743: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
744: ($r,'CSV File Compilation Status',
745: 'CSV File Compilation Progress',
1.22 ! matthew 746: scalar(@$students),'inline',undef,'Statistics','stats_status');
! 747:
1.17 matthew 748: $r->rflush();
749: #
750: # Open a file
751: my $outputfile;
752: my $filename = '/prtspool/'.
753: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
754: time.'_'.rand(1000000000).'.csv';
755: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
756: $r->log_error("Couldn't open $filename for output $!");
757: $r->print("Problems occured in writing the csv file. ".
758: "This error has been logged. ".
759: "Please alert your LON-CAPA administrator.");
760: $outputfile = undef;
761: }
762: #
1.22 ! matthew 763: # Compute the number of columns per response
! 764: my @response_headers = ('Submission');
! 765: if ($ENV{'form.correctans'} eq 'true') {
! 766: push(@response_headers,'Correct');
! 767: }
! 768: if ($ENV{'form.prob_status'} eq 'true') {
! 769: push(@response_headers,'Award Detail');
! 770: push(@response_headers,'Time');
! 771: push(@response_headers,'Attempt');
! 772: push(@response_headers,'Awarded');
! 773: }
! 774: my $response_multiplier = scalar(@response_headers);
! 775: #
! 776: # Create the table header
! 777: my @student_columns = ('username','domain','id');
1.17 matthew 778: #
1.22 ! matthew 779: my %headers;
! 780: push(@{$headers{'student'}},@student_columns);
! 781: # Pad for the student data
! 782: foreach my $row ('problem','part','response') {
! 783: foreach (@student_columns) {
! 784: push(@{$headers{$row}},'');
! 785: }
! 786: }
! 787: #
! 788: # we put the headers into the %headers hash
! 789: my $prob_start_idx = 0;
! 790: foreach my $prob (@$problems) {
! 791: $headers{'problem'}->[$prob_start_idx] =
! 792: &get_title($prob->title,$prob->src);
! 793: my $part_start_idx = $prob_start_idx;
! 794: foreach my $partid (@{$prob->parts}) {
! 795: $headers{'part'}->[$part_start_idx] = &mt('Part [_1]',$partid);
! 796: my $responses = [$prob->responseIds($partid)];
! 797: for (my $i=0;$i<scalar(@$responses);$i++) {
! 798: my $resp_idx = $prob_start_idx + $response_multiplier * $i;
! 799: $headers{'response'}->[$resp_idx]=
! 800: &mt('Response [_1]',$responses->[$i]);
! 801: for (my $j=0;$j<=$#response_headers;$j++) {
! 802: $headers{'student'}->[$resp_idx+$j]=$response_headers[$j];
! 803: }
1.17 matthew 804: }
1.22 ! matthew 805: $part_start_idx += scalar(@$responses)*$response_multiplier;
1.17 matthew 806: }
1.22 ! matthew 807: $prob_start_idx += $prob->countResponses * $response_multiplier;
1.17 matthew 808: }
1.22 ! matthew 809: foreach my $row ('problem','part','response','student') {
! 810: print $outputfile '"'.join('","',map { ''; } @student_columns).'","'.
! 811: join('","',
! 812: map {
! 813: &Apache::loncommon::csv_translate($_);
! 814: } @{$headers{$row}}).'"'.$/;
1.17 matthew 815: }
816: #
1.22 ! matthew 817: # Main loop
! 818: foreach my $student (@$students) {
! 819: my @rows;
! 820: my $prob_start_idx = 0;
! 821: foreach my $prob (@$problems) {
! 822: my $part_start_idx = 0;
! 823: foreach my $partid (@{$prob->parts}) {
! 824: my @responses = $prob->responseIds($partid);
! 825: my @response_type = $prob->responseType($partid);
! 826: for (my $i=0;$i<=$#responses;$i++) {
! 827: my $resp_start_idx = $response_multiplier * $i;
! 828: my $respid = $responses[$i];
! 829: my $results =
! 830: &Apache::loncoursedata::get_response_data_by_student
! 831: ($student,$prob->symb(),$respid);
! 832: if (! defined($results)) {
! 833: $results = [];
! 834: }
! 835: for (my $j=0; $j<scalar(@$results);$j++) {
! 836: if ($ENV{'form.all_sub'} ne 'true') {
! 837: next if ($j != 0);
! 838: }
! 839: my $idx = scalar(@$results) - $j - 1;
! 840: my $response = $results->[$idx];
! 841: my @data = &compile_response_data($response,$student,
! 842: $prob,$partid,
! 843: $respid);
! 844: for (my $k=0;$k<=$#data;$k++) {
! 845: $rows[$j]->[$prob_start_idx + $part_start_idx +
! 846: $resp_start_idx + $k] = $data[$k];
! 847: }
! 848: }
! 849: }
! 850: $part_start_idx += scalar(@responses)*$response_multiplier;
1.17 matthew 851: }
1.22 ! matthew 852: $prob_start_idx += $prob->countResponses * $response_multiplier;
! 853: }
! 854: foreach my $row (@rows) {
! 855: print $outputfile '"'.join('","',
! 856: map { $student->{$_}; }
! 857: @student_columns).'"';
! 858:
! 859: for (my $i=0;$i<$prob_start_idx;$i++) {
! 860: my $value = &Apache::loncommon::csv_translate($row->[$i]);
! 861: $value ||='';
! 862: print $outputfile ',"'.$value.'"';
1.17 matthew 863: }
1.22 ! matthew 864: print $outputfile $/;
1.17 matthew 865: }
1.22 ! matthew 866: undef(@rows);
1.17 matthew 867: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
868: 'last student');
869: }
870: close($outputfile);
871: #
872: # Close the progress window
873: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
874: #
875: # Tell the user where to get their csv file
876: $r->print('<br />'.
877: '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
878: $r->rflush();
879: return;
880: }
881:
1.22 ! matthew 882: sub compile_response_data {
! 883: my ($response,$student,$prob,$partid,$respid) = @_;
! 884: my @rowdata;
! 885: push(@rowdata,$response->[&Apache::loncoursedata::RDs_submission()]);
1.17 matthew 886: if ($ENV{'form.correctans'} eq 'true') {
1.22 ! matthew 887: my $correct = &Apache::lonstathelpers::analyze_problem_as_student
! 888: ($prob,$student->{'username'},$student->{'domain'},
! 889: $partid,$respid);
! 890: push(@rowdata,$correct);
1.17 matthew 891: }
1.22 ! matthew 892: if ($ENV{'form.prob_status'}) {
! 893: push(@rowdata,$response->[&Apache::loncoursedata::RDs_awarddetail()]);
! 894: push(@rowdata,&Apache::lonlocal::locallocaltime
! 895: ($response->[&Apache::loncoursedata::RDs_timestamp()]));
! 896: push(@rowdata,$response->[&Apache::loncoursedata::RDs_tries()]);
! 897: push(@rowdata,$response->[&Apache::loncoursedata::RDs_awarded()]);
1.17 matthew 898: }
1.22 ! matthew 899: return @rowdata;
1.15 matthew 900: }
901:
1.1 matthew 902: #########################################################
903: #########################################################
904: ##
905: ## Generic Interface Routines
906: ##
907: #########################################################
908: #########################################################
909: sub CreateInterface {
910: ##
1.16 matthew 911: ## Output Selection
1.19 matthew 912: my $output_selector = $/.'<select name="output">'.$/;
1.22 ! matthew 913: foreach ('HTML','Excel','CSV') {
1.19 matthew 914: $output_selector .= ' <option value="'.lc($_).'"';
1.16 matthew 915: if ($ENV{'form.output'} eq lc($_)) {
1.19 matthew 916: $output_selector .= ' selected ';
1.16 matthew 917: }
1.19 matthew 918: $output_selector .='>'.&mt($_).'</option>'.$/;
1.16 matthew 919: }
1.19 matthew 920: $output_selector .= '</select>'.$/;
1.16 matthew 921: ##
1.1 matthew 922: ## Environment variable initialization
923: my $Str = '';
1.2 matthew 924: $Str .= &Apache::lonhtmlcommon::breadcrumbs
1.5 matthew 925: (undef,'Student Submission Reports');
1.11 matthew 926: $Str .= '<p>';
1.1 matthew 927: $Str .= '<table cellspacing="5">'."\n";
928: $Str .= '<tr>';
1.16 matthew 929: $Str .= '<th>'.&mt('Sections').'</th>';
930: $Str .= '<th>'.&mt('Enrollment Status').'</th>';
1.21 matthew 931: $Str .= '<th>'.&mt('Output as [_1]',$output_selector).'</th>';
1.1 matthew 932: $Str .= '</tr>'."\n";
1.11 matthew 933: #
1.1 matthew 934: $Str .= '<tr><td align="center">'."\n";
935: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
936: $Str .= '</td>';
937: #
938: $Str .= '<td align="center">';
939: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
940: $Str .= '</td>';
1.6 matthew 941: #
1.15 matthew 942: # Render problem checkbox
943: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
944: if (exists($ENV{'form.renderprob'}) && $ENV{'form.renderprob'} eq 'true') {
945: $prob_checkbox .= 'checked ';
946: }
947: $prob_checkbox .= 'value="true" />';
948: #
949: # Compute correct answers checkbox
950: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1.13 matthew 951: if (exists($ENV{'form.correctans'}) && $ENV{'form.correctans'} eq 'true') {
1.15 matthew 952: $ans_checkbox .= 'checked ';
1.13 matthew 953: }
1.15 matthew 954: $ans_checkbox .= 'value="true" />';
955: #
1.19 matthew 956: # Show all submissions checkbox
957: my $all_sub_checkbox = '<input type="checkbox" name="all_sub" ';
958: if (exists($ENV{'form.all_sub'}) &&
959: $ENV{'form.all_sub'} eq 'true') {
960: $all_sub_checkbox .= 'checked ';
1.15 matthew 961: }
1.19 matthew 962: $all_sub_checkbox.= 'value="true" />';
1.15 matthew 963: #
1.20 matthew 964: # problem status checkbox
965: my $prob_status_checkbox = '<input type="checkbox" name="prob_status" ';
966: if (exists($ENV{'form.prob_status'}) &&
967: $ENV{'form.prob_status'} eq 'true') {
968: $prob_status_checkbox .= 'checked ';
1.15 matthew 969: }
1.20 matthew 970: $prob_status_checkbox .= 'value="true" />';
1.15 matthew 971: #
1.16 matthew 972: $Str .= '<td align="right" halign="top">'.
973: '<label><b>'.
1.20 matthew 974: &mt('Show problem [_1]',$prob_checkbox).'</b></label><br />'.
975: '<label><b>'.
976: &mt('Show correct answers [_1]',$ans_checkbox).'</b></label><br />'.
977: '<label><b>'.
978: &mt('Show all submissions [_1]',$all_sub_checkbox).
979: '</b></label><br />'.
1.15 matthew 980: '<label><b>'.
1.20 matthew 981: &mt('Show problem grading [_1]',$prob_status_checkbox).
982: '</b></label><br />'.
1.15 matthew 983: '</td>';
1.13 matthew 984: #
1.1 matthew 985: $Str .= '</tr>'."\n";
986: $Str .= '</table>'."\n";
987: #
1.11 matthew 988: $Str .= '<nobr>'.&mt('Status: [_1]',
989: '<input type="text" '.
990: 'name="stats_status" size="60" value="" />').
991: '</nobr>'.'</p>';
992: ##
1.1 matthew 993: return $Str;
994: }
995:
996: 1;
997:
998: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>