Annotation of loncom/interface/statistics/lonstudentsubmissions.pm, revision 1.17
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.17 ! matthew 3: # $Id: lonstudentsubmissions.pm,v 1.16 2004/09/02 21:02:54 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:
41: my @SubmitButtons = ({ name => 'PrevProblem',
42: text => 'Previous Problem' },
43: { name => 'NextProblem',
44: text => 'Next Problem' },
45: { name => 'break'},
46: { name => 'SelectAnother',
47: text => 'Choose a different Problem' },
48: { name => 'Generate',
1.16 matthew 49: text => 'Generate Report'},
1.1 matthew 50: );
51:
52: sub BuildStudentSubmissionsPage {
53: my ($r,$c)=@_;
54: #
55: my %Saveable_Parameters = ('Status' => 'scalar',
56: 'Section' => 'array',
57: 'NumPlots' => 'scalar',
58: );
59: &Apache::loncommon::store_course_settings('student_submissions',
60: \%Saveable_Parameters);
61: &Apache::loncommon::restore_course_settings('student_submissions',
62: \%Saveable_Parameters);
63: #
64: &Apache::lonstatistics::PrepareClasslist();
65: #
66: $r->print(&CreateInterface());
67: #
68: my @Students = @Apache::lonstatistics::Students;
69: #
70: if (@Students < 1) {
71: $r->print('<h2>There are no students in the sections selected</h2>');
72: }
73: #
1.11 matthew 74: my @CacheButtonHTML =
75: &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
1.1 matthew 76: $r->rflush();
77: #
78: if (exists($ENV{'form.problemchoice'}) &&
79: ! exists($ENV{'form.SelectAnother'})) {
80: foreach my $button (@SubmitButtons) {
81: if ($button->{'name'} eq 'break') {
82: $r->print("<br />\n");
83: } else {
84: $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
85: 'value="'.&mt($button->{'text'}).'" />');
86: $r->print(' 'x5);
87: }
88: }
1.11 matthew 89: foreach my $html (@CacheButtonHTML) {
90: $r->print($html.(' 'x5));
91: }
1.1 matthew 92: #
93: $r->print('<hr />');
94: $r->rflush();
95: #
96: # Determine which problem we are to analyze
97: my $current_problem = &Apache::lonstathelpers::get_target_from_id
98: ($ENV{'form.problemchoice'});
99: #
100: my ($prev,$curr,$next) =
101: &Apache::lonstathelpers::get_prev_curr_next($current_problem,
102: '.',
103: 'response',
104: );
105: if (exists($ENV{'form.PrevProblem'}) && defined($prev)) {
106: $current_problem = $prev;
107: } elsif (exists($ENV{'form.NextProblem'}) && defined($next)) {
108: $current_problem = $next;
109: } else {
110: $current_problem = $curr;
111: }
112: #
113: # Store the current problem choice and send it out in the form
114: $ENV{'form.problemchoice'} =
115: &Apache::lonstathelpers::make_target_id($current_problem);
116: $r->print('<input type="hidden" name="problemchoice" value="'.
117: $ENV{'form.problemchoice'}.'" />');
118: #
119: if (! defined($current_problem->{'resource'})) {
120: $r->print('resource is undefined');
121: } else {
122: my $resource = $current_problem->{'resource'};
123: $r->print('<h1>'.$resource->{'title'}.'</h1>');
124: $r->print('<h3>'.$resource->{'src'}.'</h3>');
1.15 matthew 125: if ($ENV{'form.renderprob'} eq 'true') {
126: $r->print(&Apache::lonstathelpers::render_resource($resource));
127: }
1.1 matthew 128: $r->rflush();
129: my %Data = &Apache::lonstathelpers::get_problem_data
130: ($resource->{'src'});
131: my $ProblemData = $Data{$current_problem->{'part'}.
132: '.'.
133: $current_problem->{'respid'}};
1.16 matthew 134: if ($ENV{'form.output'} eq 'excel') {
135: &prepare_excel_output($r,$current_problem,
136: $ProblemData,\@Students);
1.17 ! matthew 137: } elsif ($ENV{'form.output'} eq 'csv') {
! 138: &prepare_csv_output($r,$current_problem,
! 139: $ProblemData,\@Students);
1.16 matthew 140: } else {
141: &prepare_html_output($r,$current_problem,
142: $ProblemData,\@Students);
143: }
1.1 matthew 144: }
145: $r->print('<hr />');
146: } else {
147: $r->print('<input type="submit" name="Generate" value="'.
1.17 ! matthew 148: &mt('Prepare Report').'" />');
1.1 matthew 149: $r->print(' 'x5);
150: $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
151: $r->print(&Apache::lonstathelpers::ProblemSelector('.'));
152: }
153: }
154:
155: #########################################################
156: #########################################################
1.16 matthew 157:
158: my @DefaultColumns =
159: (
160: {name=>'username',
161: display=>'Student'},
162: {name=>'domain',
163: display=>'Domain'},
164: {name => 'id',
165: display => 'Id'},
166: {name => 'time',
167: display =>'Time'},
168: {name => 'attempt',
169: display =>'Attempt'},
170: {name => 'awarddetail',
171: display =>'Awarddetail'},
1.17 ! matthew 172: {name => 'awarded',
! 173: display =>'Award'},
! 174: # FIXME: Probably need to add score
1.16 matthew 175: );
176:
1.17 ! matthew 177: sub get_response_type {
! 178: my ($resource,$partid,$respid) = @_;
! 179: my $response_type = '';
! 180: for (my $i=0;
! 181: $i<scalar(@{$resource->{'partdata'}->{$partid}->{'ResponseIds'}});
! 182: $i++) {
! 183: if($resource->{'partdata'}->{$partid}->{'ResponseIds'}->[$i] eq $respid){
! 184: $response_type =
! 185: $resource->{'partdata'}->{$partid}->{'ResponseTypes'}->[$i];
! 186: last;
! 187: }
! 188: }
! 189: return $response_type;
! 190: }
! 191:
! 192:
1.16 matthew 193: #########################################################
194: #########################################################
1.1 matthew 195: ##
1.15 matthew 196: ## prepare_html_output
197: ##
198: #########################################################
199: #########################################################
200: sub prepare_html_output {
201: my ($r,$problem,$ProblemData,$Students) = @_;
202: my $c = $r->connection();
203: my ($resource,$respid,$partid) = ($problem->{'resource'},
204: $problem->{'respid'},
205: $problem->{'part'});
206: #
207: if ($ENV{'form.correctans'} eq 'true') {
1.16 matthew 208: $r->print('<h2>'.&mt('Generating Correct Answers').'</h2>');
1.15 matthew 209: &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$Students,
210: 'Statistics',
211: 'stats_status');
212: }
213: #
1.16 matthew 214: $r->print('<h2>'.&mt('Student Responses').'</h2>');
215: #
1.15 matthew 216: $r->rflush();
1.17 ! matthew 217: my $response_type = &get_response_type($resource,$partid,$respid);
1.15 matthew 218: if (! defined($response_type)) {
219: $r->print('<h2>'.&mt('Unable to determine response type').'</h2>');
1.17 ! matthew 220: return;
! 221: }
! 222: my $count = 0;
! 223: my @Columns;
! 224: if (exists($ENV{'form.concise'}) && $ENV{'form.concise'} eq 'true') {
! 225: foreach (@DefaultColumns) {
! 226: if ($_->{'name'} =~ /^(username|domain|id)$/){
! 227: push(@Columns,$_);
1.16 matthew 228: }
229: }
1.17 ! matthew 230: } else {
! 231: @Columns = @DefaultColumns;
! 232: }
! 233: my $header = '<table>'.$/.&html_headers(\@Columns);
! 234: if ($response_type eq 'essay') {
! 235: $header .= &html_essay_headers();
! 236: } elsif ($response_type eq 'option') {
! 237: $header .= &html_option_headers();
! 238: } else {
! 239: $header .= &html_generic_headers();
! 240: }
! 241: $header = '<tr>'.$header.'</tr>';
! 242: #
! 243: $r->print($/.$header.$/);
! 244: foreach my $student (@$Students) {
! 245: if ($count >= 50) {
! 246: $r->print('</table>'.$/.$header.$/);
! 247: $count = 0;
1.15 matthew 248: }
1.17 ! matthew 249: last if ($c->aborted());
! 250: my $results = &Apache::loncoursedata::get_response_data_by_student
! 251: ($student,$resource->{'symb'},$respid);
! 252: next if (! defined($results) || ref($results) ne 'ARRAY');
! 253: for (my $i=0;$i<scalar(@$results);$i++) {
! 254: my $response = $results->[$i];
! 255: if ($ENV{'form.last_sub_only'} eq 'true' &&
! 256: $i < (scalar(@$results)-1)) {
! 257: next;
! 258: }
! 259: my $data;
! 260: $data->{'username'} = $student->{'username'};
! 261: $data->{'domain'} = $student->{'domain'};
! 262: $data->{'id'} = $student->{'id'};
! 263: $data->{'fullname'} = $student->{'fullanem'};
! 264: $data->{'status'} = $student->{'status'};
! 265: $data->{'time'} = &Apache::lonlocal::locallocaltime
! 266: ($response->[&Apache::loncoursedata::RDs_timestamp()]);
! 267: $data->{'attempt'} =
! 268: $response->[&Apache::loncoursedata::RDs_tries()];
! 269: $data->{'awarded'} =
! 270: $response->[&Apache::loncoursedata::RDs_awarded()];
! 271: $data->{'awarddetail'} =
! 272: $response->[&Apache::loncoursedata::RDs_awarddetail()];
! 273: my $rowextra = 'bgcolor="#CCCCCC"';
! 274: if ($count % 2 == 1) {
! 275: $rowextra = 'bgcolor="#EEEEEE"';
! 276: }
! 277: my $row = '<tr '.$rowextra.'>';
! 278: foreach my $col (@Columns) {
! 279: $row .= '<td valign="top">'.
! 280: $data->{$col->{'name'}}.'</td>';
! 281: }
! 282: if ($response_type eq 'essay') {
! 283: $row .= &html_essay_results
! 284: ($response->[&Apache::loncoursedata::RDs_submission()],
! 285: $student->{'answer'},
! 286: scalar(@Columns),$rowextra);
! 287: } elsif ($response_type eq 'option') {
! 288: $row .= &html_option_results
! 289: ($response->[&Apache::loncoursedata::RDs_submission()],
! 290: $student->{'answer'},
! 291: scalar(@Columns),$rowextra);
! 292: } else {
! 293: $row .= &html_generic_results
! 294: ($response->[&Apache::loncoursedata::RDs_submission()],
! 295: $student->{'answer'},
! 296: scalar(@Columns),$rowextra);
1.15 matthew 297: }
1.17 ! matthew 298: $row .= '</tr>';
! 299: $r->print($row.$/);
! 300: $count++;
1.15 matthew 301: }
302: }
1.17 ! matthew 303: $r->print('</table>'.$/);
1.15 matthew 304: return;
305: }
306:
307: #####################################################
308: ##
309: ## HTML helper routines
310: ##
311: #####################################################
1.16 matthew 312: sub html_headers {
313: my ($Columns) = @_;
314: my $Str;
315: foreach my $column (@$Columns) {
316: $Str .= '<th align="left">'.$column->{'display'}.'</th>';
1.15 matthew 317: }
1.16 matthew 318: return $Str;
1.15 matthew 319: }
320:
321: sub html_essay {
1.16 matthew 322: my ($submission,$correct,$tablewidth,$rowextra)=@_;
1.15 matthew 323: #
1.16 matthew 324: $submission =~ s|\\r\\n|$/|g;
325: $submission = &HTML::Entities::encode($submission,'<>&"');
326: $submission =~ s|$/\s*$/|$/</p><p>$/|g;
327: $submission =~ s|\\||g;
328: $submission = '<p>'.$submission.'</p>';
329: #
330: my $Str = '</tr><tr '.$rowextra.'>'.
331: '<td colspan="'.$tablewidth.'">'.$submission.'</td>';
1.15 matthew 332: if ($ENV{'form.correctans'} eq 'true') {
333: $Str .= '</tr>';
1.16 matthew 334: if (defined($correct) && $correct !~ /^\s*$/) {
335: $Str .= '<tr '.$rowextra.'><td colspan="'.$tablewidth.'">'.
336: '<b>'.&mt('Correct Answer:').'</b>'.$correct.'</td>';
1.15 matthew 337: }
338: }
339: $Str .= '</tr>';
340: #
341: return $Str;
342: }
343:
1.16 matthew 344: sub html_essay_headers {
345: return '';
346: }
347:
348: sub html_generic_headers {
349: my $header ='<th>'.&mt('Submission').'</th>';
1.15 matthew 350: if ($ENV{'form.correctans'} eq 'true') {
351: $header .= '<th>'.&mt('Correct').'</th>';
352: }
353: return $header;
354: }
355:
1.17 ! matthew 356: sub html_option_headers {
! 357: return &html_generic_headers();
! 358: }
! 359:
! 360: sub html_radiobutton_results {
1.16 matthew 361: my ($submission,$correct,$tablewidth,$rowclass)=@_;
362: $submission =~ s/=([^=])$//;
363: return &html_generic_results($submission,$correct,$tablewidth,$rowclass);
1.15 matthew 364: }
365:
1.17 ! matthew 366: sub html_generic_results {
1.16 matthew 367: my ($submission,$correct,$tablewidth,$rowclass)=@_;
368: my $Str .= '<td>'.$submission.'</td>';
1.15 matthew 369: if ($ENV{'form.correctans'} eq 'true') {
1.16 matthew 370: $Str .= '<td>'.$correct.'</td>';
1.15 matthew 371: }
372: $Str .= '</tr>';
373: return $Str;
374: }
375:
1.17 ! matthew 376: sub html_option_results {
1.16 matthew 377: my ($submission,$correct,$tablewidth,$rowclass)=@_;
378: $submission =
379: '<ul class="sub_studentans">'.
1.15 matthew 380: '<li>'.join('</li><li>',
381: map {
382: &Apache::lonnet::unescape($_) ;
1.16 matthew 383: } sort split('&',$submission)
384: ).
385: '</li><ul>';
386: if (defined($correct) && $correct !~ /^\s*$/) {
387: $correct = '<ul class="sub_correctans">'.
388: '<li>'.join('</li><li>',
389: map {
390: &Apache::lonnet::unescape($_) ;
391: } sort split('&',$correct)
392: ).
393: '</li></ul>';
1.15 matthew 394: }
395: #
1.17 ! matthew 396: return &html_generic_results($submission,$correct,$tablewidth,$rowclass);
! 397: }
! 398:
! 399: #########################################################
! 400: #########################################################
! 401: ##
! 402: ## CSV output of student answers
! 403: ##
! 404: #########################################################
! 405: #########################################################
! 406: sub prepare_csv_output {
! 407: my ($r,$problem,$ProblemData,$Students) = @_;
! 408: #
! 409: my $c = $r->connection();
! 410: my ($resource,$respid,$partid) = ($problem->{'resource'},
! 411: $problem->{'respid'},
! 412: $problem->{'part'});
! 413: #
! 414: if ($ENV{'form.correctans'} eq 'true') {
! 415: $r->print('<h2>'.&mt('Generating Correct Answers').'</h2>');
! 416: &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$Students,
! 417: 'Statistics',
! 418: 'stats_status');
! 419: }
! 420: #
! 421: $r->print('<h2>'.
! 422: &mt('Generating CSV report of student responses').'</h2>');
! 423: #
! 424: # Progress window
! 425: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
! 426: ($r,'CSV File Compilation Status',
! 427: 'CSV File Compilation Progress',
! 428: scalar(@$Students),'inline',undef,'Statistics','stats_status');
! 429:
! 430: $r->rflush();
! 431: #
! 432: # Open a file
! 433: my $outputfile;
! 434: my $filename = '/prtspool/'.
! 435: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
! 436: time.'_'.rand(1000000000).'.csv';
! 437: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
! 438: $r->log_error("Couldn't open $filename for output $!");
! 439: $r->print("Problems occured in writing the csv file. ".
! 440: "This error has been logged. ".
! 441: "Please alert your LON-CAPA administrator.");
! 442: $outputfile = undef;
! 443: }
! 444: #
! 445: #
! 446: my @Columns;
! 447: if (exists($ENV{'form.concise'}) && $ENV{'form.concise'} eq 'true') {
! 448: foreach (@DefaultColumns) {
! 449: if ($_->{'name'} =~ /^(username|domain|id)$/){
! 450: push(@Columns,$_);
! 451: }
! 452: }
! 453: } else {
! 454: @Columns = @DefaultColumns;
! 455: }
! 456: #
! 457: my $response_type = &get_response_type($resource,$partid,$respid);
! 458: if (! defined($response_type) || $response_type eq '') {
! 459: $r->print('<h2>'.&mt('Unable to determine response type').'</h2>');
! 460: return;
! 461: }
! 462: #
! 463: my $header = &csv_headers(\@Columns).','.&csv_generic_headers();
! 464: print $outputfile $header.$/;
! 465: #
! 466: foreach my $student (@$Students) {
! 467: last if ($c->aborted());
! 468: my $results = &Apache::loncoursedata::get_response_data_by_student
! 469: ($student,$resource->{'symb'},$respid);
! 470: next if (! defined($results) || ref($results) ne 'ARRAY');
! 471: for (my $i=0;$i<scalar(@$results);$i++) {
! 472: my $response = $results->[$i];
! 473: if ($ENV{'form.last_sub_only'} eq 'true' &&
! 474: $i < (scalar(@$results)-1)) {
! 475: next;
! 476: }
! 477: my $data;
! 478: $data->{'username'} = $student->{'username'};
! 479: $data->{'domain'} = $student->{'domain'};
! 480: $data->{'id'} = $student->{'id'};
! 481: $data->{'fullname'} = $student->{'fullanem'};
! 482: $data->{'status'} = $student->{'status'};
! 483: $data->{'time'} = &Apache::lonlocal::locallocaltime
! 484: ($response->[&Apache::loncoursedata::RDs_timestamp()]);
! 485: $data->{'attempt'} =
! 486: $response->[&Apache::loncoursedata::RDs_tries()];
! 487: $data->{'awarded'} =
! 488: $response->[&Apache::loncoursedata::RDs_awarded()];
! 489: $data->{'awarddetail'} =
! 490: $response->[&Apache::loncoursedata::RDs_awarddetail()];
! 491: my $rowextra = '';
! 492: my $row;
! 493: foreach my $col (@Columns) {
! 494: $row .= '"'.
! 495: &Apache::loncommon::csv_translate($data->{$col->{'name'}}).'",';
! 496: }
! 497: if ($response_type eq 'option') {
! 498: $row .= &csv_option_results
! 499: ($response->[&Apache::loncoursedata::RDs_submission()],
! 500: $student->{'answer'},
! 501: scalar(@Columns),$rowextra);
! 502: } elsif ($response_type eq 'radiobutton') {
! 503: $row .= &csv_radiobutton_results
! 504: ($response->[&Apache::loncoursedata::RDs_submission()],
! 505: $student->{'answer'},
! 506: scalar(@Columns),$rowextra);
! 507: } else {
! 508: $row .= &csv_generic_results
! 509: ($response->[&Apache::loncoursedata::RDs_submission()],
! 510: $student->{'answer'},
! 511: scalar(@Columns),$rowextra);
! 512: }
! 513: print $outputfile $row.$/;
! 514: }
! 515: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
! 516: 'last student');
! 517: }
! 518: close($outputfile);
! 519: #
! 520: # Close the progress window
! 521: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
! 522: #
! 523: # Tell the user where to get their csv file
! 524: $r->print('<br />'.
! 525: '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
! 526: $r->rflush();
! 527: return;
! 528: }
! 529:
! 530: sub csv_headers {
! 531: my ($Columns) = @_;
! 532: my $Str;
! 533: foreach my $column (@$Columns) {
! 534: $Str .=
! 535: '"'.&Apache::loncommon::csv_translate($column->{'display'}).'",';
! 536: }
! 537: chop($Str);
! 538: return $Str;
! 539: }
! 540:
! 541: sub csv_generic_headers {
! 542: my ($title) = @_;
! 543: if (! defined($title)) {
! 544: $title = &mt('Submission');
! 545: }
! 546: my $header = '"'.&Apache::loncommon::csv_translate($title).'"';
! 547: if ($ENV{'form.correctans'} eq 'true') {
! 548: $header .= ',"'.&Apache::loncommon::csv_translate(&mt('Correct')).'"';
! 549: }
! 550: return $header;
! 551: }
! 552:
! 553: #------------------------------------------
! 554: sub csv_essay_results {
! 555: my ($submission,$correct,$tablewidth,$rowextra)=@_;
! 556: #
! 557: $submission =~ s|\\r|\\\\r|g;
! 558: $submission =~ s|\\n|\\\\n|g;
! 559: #
! 560: return &csv_generic_results($submission,$correct,$tablewidth);
! 561: }
! 562:
! 563: #------------------------------------------
! 564: sub csv_radiobutton_results {
! 565: my ($submission,$correct,$tablewidth,$rowclass)=@_;
! 566: $submission =~ s/=[^=]*$//;
! 567: return &csv_generic_results($submission,$correct,$tablewidth,$rowclass);
! 568: }
! 569:
! 570: #------------------------------------------
! 571: sub csv_option_results {
! 572: my ($submission,$correct,$tablewidth,$rowclass)=@_;
! 573: $submission = join(',',
! 574: map {
! 575: &Apache::lonnet::unescape($_) ;
! 576: } sort split('&',$submission)
! 577: );
! 578: if (defined($correct) && $correct !~ /^\s*$/) {
! 579: $correct =join(',',
! 580: map {
! 581: &Apache::lonnet::unescape($_) ;
! 582: } sort split('&',$submission));
! 583: }
! 584: return &csv_generic_results($submission,$correct,$tablewidth,$rowclass);
! 585: }
! 586:
! 587: #------------------------------------------
! 588: sub csv_generic_results {
! 589: my ($submission,$correct,$tablewidth,$rowclass)=@_;
! 590: my $Str .=
! 591: '"'.&Apache::loncommon::csv_translate($submission).'"';
! 592: if ($ENV{'form.correctans'} eq 'true') {
! 593: $Str .= ',"'.&Apache::loncommon::csv_translate($correct).'"';
! 594: }
! 595: return $Str;
1.15 matthew 596: }
597:
598: #########################################################
599: #########################################################
600: ##
1.1 matthew 601: ## Excel output of student answers and correct answers
602: ##
603: #########################################################
604: #########################################################
605: sub prepare_excel_output {
606: my ($r,$problem,$ProblemData,$Students) = @_;
1.8 matthew 607: my $c = $r->connection();
1.1 matthew 608: my ($resource,$respid,$partid) = ($problem->{'resource'},
609: $problem->{'respid'},
610: $problem->{'part'});
611: $r->print('<h2>'.
612: &mt('Preparing Excel spreadsheet of student responses').
1.12 matthew 613: '</h2>'.
614: '<p>'.
615: &mt('See the status bar above for student answer computation progress').
616: '</p>');
1.1 matthew 617: #
1.13 matthew 618: if ($ENV{'form.correctans'} eq 'true') {
619: &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$Students,
620: 'Statistics',
621: 'stats_status');
1.16 matthew 622: $r->print('<script>'.
623: 'window.document.Statistics.stats_status.value="'.
624: 'Done computing student answers. Compiling spreadsheet.'.
625: '";</script>');
1.13 matthew 626: }
1.1 matthew 627: #
1.11 matthew 628: $r->rflush();
1.13 matthew 629: my @Columns;
1.17 ! matthew 630: if (exists($ENV{'form.concise'}) && $ENV{'form.concise'} eq 'true') {
! 631: foreach (@DefaultColumns) {
! 632: if ($_->{'name'} =~ /^(username|domain|id)$/){
! 633: push(@Columns,$_);
! 634: }
! 635: }
! 636: } else {
! 637: @Columns = @DefaultColumns;
! 638: }
! 639: my ($awarded_col,$weight_col);
! 640: for (my $i=0;$i<=$#Columns;$i++) {
! 641: if ($Columns[$i]->{'name'} eq 'weight' ) { $weight_col = $i; }
! 642: if ($Columns[$i]->{'name'} eq 'awarded') { $awarded_col = $i; }
! 643: }
1.1 matthew 644: #
645: # Create excel worksheet
646: my $filename = '/prtspool/'.
647: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
648: time.'_'.rand(1000000000).'.xls';
649: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
650: if (! defined($workbook)) {
651: $r->log_error("Error creating excel spreadsheet $filename: $!");
652: $r->print('<p>'.&mt("Unable to create new Excel file. ".
653: "This error has been logged. ".
654: "Please alert your LON-CAPA administrator").
655: '</p>');
656: return undef;
657: }
658: #
659: $workbook->set_tempdir('/home/httpd/perl/tmp');
660: #
661: my $format = &Apache::loncommon::define_excel_formats($workbook);
662: my $worksheet = $workbook->addworksheet('Student Submission Data');
663: #
664: # Make sure we get new weight data instead of data on a 10 minute delay
665: &Apache::lonnet::clear_EXT_cache_status();
666: #
667: # Put on the standard headers and whatnot
668: my $rows_output=0;
669: $worksheet->write($rows_output++,0,$resource->{'title'},$format->{'h1'});
670: $worksheet->write($rows_output++,0,$resource->{'src'},$format->{'h3'});
671: $rows_output++;
1.17 ! matthew 672: $worksheet->write_row($rows_output++,0,
! 673: [map {$_->{'display'}} @Columns],
! 674: $format->{'bold'});
1.1 matthew 675: #
676: # Populate the worksheet with the student data
1.16 matthew 677: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
678: ($r,'Excel File Compilation Status',
679: 'Excel File Compilation Progress',
680: scalar(@$Students),'inline',undef,'Statistics','stats_status');
1.1 matthew 681: foreach my $student (@$Students) {
1.8 matthew 682: last if ($c->aborted());
1.16 matthew 683:
1.1 matthew 684: my $results = &Apache::loncoursedata::get_response_data_by_student
685: ($student,$resource->{'symb'},$respid);
686: my %row;
687: $row{'username'} = $student->{'username'};
688: $row{'domain'} = $student->{'domain'};
1.17 ! matthew 689: $row{'id'} = $student->{'id'};
1.13 matthew 690: $row{'correct'} = $student->{'answer'};
1.1 matthew 691: $row{'weight'} = &Apache::lonnet::EXT
692: ('resource.'.$partid.'.weight',$resource->{'symb'},
693: undef,undef,undef);
694: if (! defined($results) || ref($results) ne 'ARRAY') {
695: $row{'score'} = '='.
696: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
697: ($rows_output,$awarded_col)
698: .'*'.
699: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
700: ($rows_output,$weight_col);
701: my $cols_output = 0;
702: foreach my $col (@Columns) {
1.17 ! matthew 703: if (! exists($row{$col->{'name'}})) {
1.1 matthew 704: $cols_output++;
705: next;
706: }
1.17 ! matthew 707: $worksheet->write($rows_output,$cols_output++,
! 708: $row{$col->{'name'}});
1.1 matthew 709: }
710: $rows_output++;
711: } else {
1.15 matthew 712: for (my $i=0;$i<scalar(@$results);$i++) {
713: my $response = $results->[$i];
714: if ($ENV{'form.last_sub_only'} eq 'true' &&
715: $i < (scalar(@$results)-1)) {
716: next;
717: }
1.1 matthew 718: delete($row{'time'});
719: delete($row{'attempt'});
720: delete($row{'submission'});
721: delete($row{'awarded'});
722: delete($row{'grading'});
723: delete($row{'score'});
724: my %row_format;
725: #
726: # Time is handled differently
727: $row{'time'} = &Apache::lonstathelpers::calc_serial
728: ($response->[&Apache::loncoursedata::RDs_timestamp()]);
729: $row_format{'time'}=$format->{'date'};
730: #
731: $row{'attempt'} = $response->[
732: &Apache::loncoursedata::RDs_tries()];
733: $row{'submission'} = $response->[
734: &Apache::loncoursedata::RDs_submission()];
735: if ($row{'submission'} =~ m/^=/) {
736: # This will be interpreted as a formula. That is bad!
737: $row{'submission'} = " ".$row{'submission'};
738: }
1.17 ! matthew 739: $row{'awarddetail'} = $response->[
1.1 matthew 740: &Apache::loncoursedata::RDs_awarddetail()];
741: $row{'awarded'} = $response->[
742: &Apache::loncoursedata::RDs_awarded()];
743: $row{'score'} = '='.
744: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
745: ($rows_output,$awarded_col)
746: .'*'.
747: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
748: ($rows_output,$weight_col);
749: my $cols_output = 0;
750: foreach my $col (@Columns) {
751: $worksheet->write($rows_output,$cols_output++,$row{$col},
1.17 ! matthew 752: $row_format{$col->{'name'}});
1.1 matthew 753: }
754: $rows_output++;
755: }
756: } # End of else clause on if (! defined($results) ....
1.16 matthew 757: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
758: 'last student');
1.1 matthew 759: }
1.16 matthew 760: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.1 matthew 761: #
762: # Close the excel file
763: $workbook->close();
764: #
765: # Write a link to allow them to download it
766: $r->print('<p><a href="'.$filename.'">'.
767: &mt('Your Excel spreadsheet.').
768: '</a></p>'."\n");
1.11 matthew 769: $r->print('<script>'.
770: 'window.document.Statistics.stats_status.value="'.
771: 'Done compiling spreadsheet. See link below to download.'.
772: '";</script>');
773: $r->rflush();
1.15 matthew 774: return;
1.1 matthew 775: }
776:
777: #########################################################
778: #########################################################
779: ##
780: ## Generic Interface Routines
781: ##
782: #########################################################
783: #########################################################
784: sub CreateInterface {
785: ##
1.16 matthew 786: ## Output Selection
787: my $OutputSelector = $/.'<select name="output">'.$/;
1.17 ! matthew 788: foreach ('HTML','Excel','CSV') {
1.16 matthew 789: $OutputSelector .= ' <option value="'.lc($_).'"';
790: if ($ENV{'form.output'} eq lc($_)) {
791: $OutputSelector .= ' selected ';
792: }
793: $OutputSelector .='>'.&mt($_).'</option>'.$/;
794: }
795: $OutputSelector .= '</select>'.$/;
796: ##
1.1 matthew 797: ## Environment variable initialization
798: my $Str = '';
1.2 matthew 799: $Str .= &Apache::lonhtmlcommon::breadcrumbs
1.5 matthew 800: (undef,'Student Submission Reports');
1.11 matthew 801: $Str .= '<p>';
1.1 matthew 802: $Str .= '<table cellspacing="5">'."\n";
803: $Str .= '<tr>';
1.16 matthew 804: $Str .= '<th>'.&mt('Sections').'</th>';
805: $Str .= '<th>'.&mt('Enrollment Status').'</th>';
806: $Str .= '<th>'.&mt('Output Options').'</th>';
1.1 matthew 807: $Str .= '</tr>'."\n";
1.11 matthew 808: #
1.1 matthew 809: $Str .= '<tr><td align="center">'."\n";
810: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
811: $Str .= '</td>';
812: #
813: $Str .= '<td align="center">';
814: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
815: $Str .= '</td>';
1.6 matthew 816: #
1.15 matthew 817: # Render problem checkbox
818: my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
819: if (exists($ENV{'form.renderprob'}) && $ENV{'form.renderprob'} eq 'true') {
820: $prob_checkbox .= 'checked ';
821: }
822: $prob_checkbox .= 'value="true" />';
823: #
824: # Compute correct answers checkbox
825: my $ans_checkbox = '<input type="checkbox" name="correctans" ';
1.13 matthew 826: if (exists($ENV{'form.correctans'}) && $ENV{'form.correctans'} eq 'true') {
1.15 matthew 827: $ans_checkbox .= 'checked ';
1.13 matthew 828: }
1.15 matthew 829: $ans_checkbox .= 'value="true" />';
830: #
831: # Only show last submission checkbox
832: my $last_sub_checkbox = '<input type="checkbox" name="last_sub_only" ';
833: if (exists($ENV{'form.last_sub_only'}) &&
834: $ENV{'form.last_sub_only'} eq 'true') {
835: $last_sub_checkbox .= 'checked ';
836: }
837: $last_sub_checkbox.= 'value="true" />';
838: #
1.16 matthew 839: # Concise view checkbox
840: my $concise_view_checkbox = '<input type="checkbox" name="concise" ';
841: if (exists($ENV{'form.concise'}) && $ENV{'form.concise'} eq 'true') {
842: $concise_view_checkbox .= 'checked ';
1.15 matthew 843: }
1.16 matthew 844: $concise_view_checkbox .= 'value="true" />';
1.15 matthew 845: #
1.16 matthew 846: $Str .= '<td align="right" halign="top">'.
847: '<b>'.&mt('Output Format: [_1]',$OutputSelector).'</b><br />'.$/.
848: '<label><b>'.
1.15 matthew 849: &mt('show problem [_1]',$prob_checkbox).'</b></label><br />'.
850: '<label><b>'.
851: &mt('compute correct answers [_1]',$ans_checkbox).'</b></label><br />'.
852: '<label><b>'.
853: &mt('final answer only [_1]',$last_sub_checkbox).'</b></label><br />'.
1.16 matthew 854: '<label><b>'.
855: &mt('concise view [_1]',$concise_view_checkbox).'</b></label><br />'.
1.15 matthew 856: '</td>';
1.13 matthew 857: #
1.1 matthew 858: $Str .= '</tr>'."\n";
859: $Str .= '</table>'."\n";
860: #
1.11 matthew 861: $Str .= '<nobr>'.&mt('Status: [_1]',
862: '<input type="text" '.
863: 'name="stats_status" size="60" value="" />').
864: '</nobr>'.'</p>';
865: ##
1.1 matthew 866: return $Str;
867: }
868:
869: 1;
870:
871: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>