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