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