File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.8: download - view: text, annotated - select for diffs
Fri Mar 12 21:13:11 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Check for aborted connections.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentsubmissions.pm,v 1.8 2004/03/12 21:13:11 matthew Exp $
    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 => 'ClearCache',
   47:                        text => 'Clear Caches' },
   48:                      { name => 'updatecaches',
   49:                        text => 'Update Student Data' },
   50:                      { name => 'SelectAnother',
   51:                        text => 'Choose a different Problem' },
   52:                      { name => 'Generate',
   53:                        text => 'Generate Spreadsheet'},
   54:                      );
   55: 
   56: sub BuildStudentSubmissionsPage {
   57:     my ($r,$c)=@_;
   58:     #
   59:     my %Saveable_Parameters = ('Status' => 'scalar',
   60:                                'Section' => 'array',
   61:                                'NumPlots' => 'scalar',
   62:                                );
   63:     &Apache::loncommon::store_course_settings('student_submissions',
   64:                                               \%Saveable_Parameters);
   65:     &Apache::loncommon::restore_course_settings('student_submissions',
   66:                                                 \%Saveable_Parameters);
   67:     #
   68:     &Apache::lonstatistics::PrepareClasslist();
   69:     #
   70:     $r->print(&CreateInterface());
   71:     #
   72:     my @Students = @Apache::lonstatistics::Students;
   73:     #
   74:     if (@Students < 1) {
   75:         $r->print('<h2>There are no students in the sections selected</h2>');
   76:     }
   77:     #
   78:     &Apache::loncoursedata::clear_internal_caches();
   79:     if (exists($ENV{'form.ClearCache'}) || 
   80:         exists($ENV{'form.updatecaches'}) ||
   81:         (exists($ENV{'form.firstanalysis'}) &&
   82:          $ENV{'form.firstanalysis'} ne 'no')) {
   83:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   84:     }
   85:     if (! exists($ENV{'form.firstanalysis'})) {
   86:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   87:     } else {
   88:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   89:     }
   90:     $r->rflush();
   91:     #
   92:     if (exists($ENV{'form.problemchoice'}) && 
   93:         ! exists($ENV{'form.SelectAnother'})) {
   94:         foreach my $button (@SubmitButtons) {
   95:             if ($button->{'name'} eq 'break') {
   96:                 $r->print("<br />\n");
   97:             } else {
   98:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
   99:                           'value="'.&mt($button->{'text'}).'" />');
  100:                 $r->print('&nbsp;'x5);
  101:             }
  102:         }
  103:         #
  104:         $r->print('<hr />');
  105:         $r->rflush();
  106:         #
  107:         # Determine which problem we are to analyze
  108:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  109:             ($ENV{'form.problemchoice'});
  110:         #
  111:         my ($prev,$curr,$next) = 
  112:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  113:                                                         '.',
  114:                                                         'response',
  115:                                                         );
  116:         if (exists($ENV{'form.PrevProblem'}) && defined($prev)) {
  117:             $current_problem = $prev;
  118:         } elsif (exists($ENV{'form.NextProblem'}) && defined($next)) {
  119:             $current_problem = $next;
  120:         } else {
  121:             $current_problem = $curr;
  122:         }
  123:         #
  124:         # Store the current problem choice and send it out in the form
  125:         $ENV{'form.problemchoice'} = 
  126:             &Apache::lonstathelpers::make_target_id($current_problem);
  127:         $r->print('<input type="hidden" name="problemchoice" value="'.
  128:                   $ENV{'form.problemchoice'}.'" />');
  129:         #
  130:         if (! defined($current_problem->{'resource'})) {
  131:             $r->print('resource is undefined');
  132:         } else {
  133:             my $resource = $current_problem->{'resource'};
  134:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  135:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  136:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  137:             $r->rflush();
  138:             my %Data = &Apache::lonstathelpers::get_problem_data
  139:                 ($resource->{'src'});
  140:             my $ProblemData = $Data{$current_problem->{'part'}.
  141:                                     '.'.
  142:                                     $current_problem->{'respid'}};
  143:             &prepare_excel_output($r,$current_problem,
  144:                                   $ProblemData,\@Students);
  145:         }
  146:         $r->print('<hr />');
  147:     } else {
  148:         $r->print('<input type="submit" name="Generate" value="'.
  149:                   &mt('Generate Spreadsheet').'" />');
  150:         $r->print('&nbsp;'x5);
  151:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  152:         $r->print(&Apache::lonstathelpers::ProblemSelector('.'));
  153:     }
  154: }
  155: 
  156: 
  157: #########################################################
  158: #########################################################
  159: ##
  160: ##      Excel output of student answers and correct answers
  161: ##
  162: #########################################################
  163: #########################################################
  164: sub prepare_excel_output {
  165:     my ($r,$problem,$ProblemData,$Students) = @_;
  166:     my $c = $r->connection();
  167:     my ($resource,$respid,$partid) = ($problem->{'resource'},
  168:                                       $problem->{'respid'},
  169:                                       $problem->{'part'});
  170:     $r->print('<h2>'.
  171:               &mt('Preparing Excel spreadsheet of student responses').
  172:               '</h2>');
  173:     #
  174:     &GetStudentAnswers($r,$problem,$Students);
  175:     #
  176:     my @Columns = ( 'username','domain','attempt','time',
  177:                     'submission','correct', 'grading','awarded','weight',
  178:                     'score');
  179:     my $awarded_col = 7;
  180:     my $weight_col  = 8;
  181:     #
  182:     # Create excel worksheet
  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:     # Make sure we get new weight data instead of data on a 10 minute delay
  202:     &Apache::lonnet::clear_EXT_cache_status();
  203:     #
  204:     # Put on the standard headers and whatnot
  205:     my $rows_output=0;
  206:     $worksheet->write($rows_output++,0,$resource->{'title'},$format->{'h1'});
  207:     $worksheet->write($rows_output++,0,$resource->{'src'},$format->{'h3'});
  208:     $rows_output++;
  209:     $worksheet->write_row($rows_output++,0,\@Columns,$format->{'bold'});
  210:     #
  211:     # Populate the worksheet with the student data
  212:     foreach my $student (@$Students) {
  213:         last if ($c->aborted());
  214:         my $results = &Apache::loncoursedata::get_response_data_by_student
  215:             ($student,$resource->{'symb'},$respid);
  216:         my %row;
  217:         $row{'username'} = $student->{'username'};
  218:         $row{'domain'}   = $student->{'domain'};
  219:         $row{'correct'} = $student->{'answer'};
  220:         $row{'weight'} = &Apache::lonnet::EXT
  221:             ('resource.'.$partid.'.weight',$resource->{'symb'},
  222:              undef,undef,undef);
  223:         if (! defined($results) || ref($results) ne 'ARRAY') {
  224:             $row{'score'} = '='.
  225:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  226:                     ($rows_output,$awarded_col)
  227:                 .'*'.
  228:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  229:                     ($rows_output,$weight_col);
  230:             my $cols_output = 0;
  231:             foreach my $col (@Columns) {
  232:                 if (! exists($row{$col})) {
  233:                     $cols_output++;
  234:                     next;
  235:                 }
  236:                 $worksheet->write($rows_output,$cols_output++,$row{$col});
  237:             }
  238:             $rows_output++;
  239:         } else {
  240:             foreach my $response (@$results) {
  241:                 delete($row{'time'});
  242:                 delete($row{'attempt'});
  243:                 delete($row{'submission'});
  244:                 delete($row{'awarded'});
  245:                 delete($row{'grading'});
  246:                 delete($row{'score'});
  247:                 my %row_format;
  248:                 #
  249:                 # Time is handled differently
  250:                 $row{'time'} = &Apache::lonstathelpers::calc_serial
  251:                     ($response->[&Apache::loncoursedata::RDs_timestamp()]);
  252:                 $row_format{'time'}=$format->{'date'};
  253:                 #
  254:                 $row{'attempt'}  = $response->[
  255:                      &Apache::loncoursedata::RDs_tries()];
  256:                 $row{'submission'} = $response->[
  257:                      &Apache::loncoursedata::RDs_submission()];
  258:                 if ($row{'submission'} =~ m/^=/) {
  259:                     # This will be interpreted as a formula.  That is bad!
  260:                     $row{'submission'} = " ".$row{'submission'};
  261:                 }
  262:                 $row{'grading'} = $response->[
  263:                      &Apache::loncoursedata::RDs_awarddetail()];
  264:                 $row{'awarded'} = $response->[
  265:                      &Apache::loncoursedata::RDs_awarded()];
  266:                 $row{'score'} = '='.
  267:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  268:                         ($rows_output,$awarded_col)
  269:                     .'*'.
  270:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  271:                         ($rows_output,$weight_col);
  272:                 my $cols_output = 0;
  273:                 foreach my $col (@Columns) {
  274:                     $worksheet->write($rows_output,$cols_output++,$row{$col},
  275:                                       $row_format{$col});
  276:                 }
  277:                 $rows_output++;
  278:             }
  279:         } # End of else clause on if (! defined($results) ....
  280:     }
  281:     #
  282:     # Close the excel file
  283:     $workbook->close();
  284:     #
  285:     # Write a link to allow them to download it
  286:     $r->print('<p><a href="'.$filename.'">'.
  287:               &mt('Your Excel spreadsheet.').
  288:               '</a></p>'."\n");
  289: }
  290: 
  291: sub GetStudentAnswers {
  292:     my ($r,$problem,$Students) = @_;
  293:     my $c = $r->connection();
  294:     my %Answers;
  295:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  296:                                       $problem->{'part'},
  297:                                       $problem->{'respid'});
  298:     # Open progress window
  299:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  300:         ($r,'Student Answer Compilation Status',
  301:          'Student Answer Compilation Progress', scalar(@$Students));
  302:     $r->print("<table>\n");
  303:     $r->rflush();
  304:     foreach my $student (@$Students) {
  305:         last if ($c->aborted());
  306:         my $sname = $student->{'username'};
  307:         my $sdom = $student->{'domain'};
  308:         my $answer = &Apache::lonstathelpers::analyze_problem_as_student
  309:             ($resource,$sname,$sdom,$partid,$respid);
  310:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  311:                                                  &mt('last student'));
  312:         $student->{'answer'} = $answer;
  313:     }
  314:     return if ($c->aborted());
  315:     $r->print("</table>\n");
  316:     $r->rflush();
  317:     # close progress window
  318:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  319:     return;
  320: }
  321: 
  322: 
  323: #########################################################
  324: #########################################################
  325: ##
  326: ##   Generic Interface Routines
  327: ##
  328: #########################################################
  329: #########################################################
  330: sub CreateInterface {
  331:     ##
  332:     ## Environment variable initialization
  333:     my $Str = '';
  334:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  335:         (undef,'Student Submission Reports');
  336:     $Str .= '<table cellspacing="5">'."\n";
  337:     $Str .= '<tr>';
  338:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  339:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  340:     $Str .= '<td align="center">&nbsp;</td>';
  341:     $Str .= '</tr>'."\n";
  342:     ##
  343:     ## 
  344:     $Str .= '<tr><td align="center">'."\n";
  345:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  346:     $Str .= '</td>';
  347:     #
  348:     $Str .= '<td align="center">';
  349:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  350:     $Str .= '</td>';
  351:     #
  352:     $Str .= '<td></td>';
  353:     #
  354:     my $only_seq_with_assessments = sub { 
  355:         my $s=shift;
  356:         if ($s->{'num_assess'} < 1) { 
  357:             return 0;
  358:         } else { 
  359:             return 1;
  360:         }
  361:     };
  362:     ##
  363:     ##
  364:     $Str .= '</tr>'."\n";
  365:     $Str .= '</table>'."\n";
  366:     #
  367:     # We do this to make sure the sequence information is initialized
  368:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  369:                                               $only_seq_with_assessments);
  370:     
  371:     #
  372:     return $Str;
  373: }
  374: 
  375: 
  376: 
  377: 1;
  378: 
  379: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>