File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.7: download - view: text, annotated - select for diffs
Sun Mar 7 20:41:28 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removed the lonstathelpers::limit_by_time_form.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentsubmissions.pm,v 1.7 2004/03/07 20:41:28 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 ($resource,$respid,$partid) = ($problem->{'resource'},
  167:                                       $problem->{'respid'},
  168:                                       $problem->{'part'});
  169:     $r->print('<h2>'.
  170:               &mt('Preparing Excel spreadsheet of student responses').
  171:               '</h2>');
  172:     #
  173:     &GetStudentAnswers($r,$problem,$Students);
  174:     #
  175:     my @Columns = ( 'username','domain','attempt','time',
  176:                     'submission','correct', 'grading','awarded','weight',
  177:                     'score');
  178:     my $awarded_col = 7;
  179:     my $weight_col  = 8;
  180:     #
  181:     # Create excel worksheet
  182:     my $filename = '/prtspool/'.
  183:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  184:         time.'_'.rand(1000000000).'.xls';
  185:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  186:     if (! defined($workbook)) {
  187:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  188:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
  189:                             "This error has been logged.  ".
  190:                             "Please alert your LON-CAPA administrator").
  191:                   '</p>');
  192:         return undef;
  193:     }
  194:     #
  195:     $workbook->set_tempdir('/home/httpd/perl/tmp');
  196:     #
  197:     my $format = &Apache::loncommon::define_excel_formats($workbook);
  198:     my $worksheet  = $workbook->addworksheet('Student Submission Data');
  199:     #
  200:     # Make sure we get new weight data instead of data on a 10 minute delay
  201:     &Apache::lonnet::clear_EXT_cache_status();
  202:     #
  203:     # Put on the standard headers and whatnot
  204:     my $rows_output=0;
  205:     $worksheet->write($rows_output++,0,$resource->{'title'},$format->{'h1'});
  206:     $worksheet->write($rows_output++,0,$resource->{'src'},$format->{'h3'});
  207:     $rows_output++;
  208:     $worksheet->write_row($rows_output++,0,\@Columns,$format->{'bold'});
  209:     #
  210:     # Populate the worksheet with the student data
  211:     foreach my $student (@$Students) {
  212:         my $results = &Apache::loncoursedata::get_response_data_by_student
  213:             ($student,$resource->{'symb'},$respid);
  214:         my %row;
  215:         $row{'username'} = $student->{'username'};
  216:         $row{'domain'}   = $student->{'domain'};
  217:         $row{'correct'} = $student->{'answer'};
  218:         $row{'weight'} = &Apache::lonnet::EXT
  219:             ('resource.'.$partid.'.weight',$resource->{'symb'},
  220:              undef,undef,undef);
  221:         if (! defined($results) || ref($results) ne 'ARRAY') {
  222:             $row{'score'} = '='.
  223:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  224:                     ($rows_output,$awarded_col)
  225:                 .'*'.
  226:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  227:                     ($rows_output,$weight_col);
  228:             my $cols_output = 0;
  229:             foreach my $col (@Columns) {
  230:                 if (! exists($row{$col})) {
  231:                     $cols_output++;
  232:                     next;
  233:                 }
  234:                 $worksheet->write($rows_output,$cols_output++,$row{$col});
  235:             }
  236:             $rows_output++;
  237:         } else {
  238:             foreach my $response (@$results) {
  239:                 delete($row{'time'});
  240:                 delete($row{'attempt'});
  241:                 delete($row{'submission'});
  242:                 delete($row{'awarded'});
  243:                 delete($row{'grading'});
  244:                 delete($row{'score'});
  245:                 my %row_format;
  246:                 #
  247:                 # Time is handled differently
  248:                 $row{'time'} = &Apache::lonstathelpers::calc_serial
  249:                     ($response->[&Apache::loncoursedata::RDs_timestamp()]);
  250:                 $row_format{'time'}=$format->{'date'};
  251:                 #
  252:                 $row{'attempt'}  = $response->[
  253:                      &Apache::loncoursedata::RDs_tries()];
  254:                 $row{'submission'} = $response->[
  255:                      &Apache::loncoursedata::RDs_submission()];
  256:                 if ($row{'submission'} =~ m/^=/) {
  257:                     # This will be interpreted as a formula.  That is bad!
  258:                     $row{'submission'} = " ".$row{'submission'};
  259:                 }
  260:                 $row{'grading'} = $response->[
  261:                      &Apache::loncoursedata::RDs_awarddetail()];
  262:                 $row{'awarded'} = $response->[
  263:                      &Apache::loncoursedata::RDs_awarded()];
  264:                 $row{'score'} = '='.
  265:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  266:                         ($rows_output,$awarded_col)
  267:                     .'*'.
  268:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  269:                         ($rows_output,$weight_col);
  270:                 my $cols_output = 0;
  271:                 foreach my $col (@Columns) {
  272:                     $worksheet->write($rows_output,$cols_output++,$row{$col},
  273:                                       $row_format{$col});
  274:                 }
  275:                 $rows_output++;
  276:             }
  277:         } # End of else clause on if (! defined($results) ....
  278:     }
  279:     #
  280:     # Close the excel file
  281:     $workbook->close();
  282:     #
  283:     # Write a link to allow them to download it
  284:     $r->print('<p><a href="'.$filename.'">'.
  285:               &mt('Your Excel spreadsheet.').
  286:               '</a></p>'."\n");
  287: }
  288: 
  289: sub GetStudentAnswers {
  290:     my ($r,$problem,$Students) = @_;
  291:     my %Answers;
  292:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  293:                                       $problem->{'part'},
  294:                                       $problem->{'respid'});
  295:     # Open progress window
  296:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  297:         ($r,'Student Answer Compilation Status',
  298:          'Student Answer Compilation Progress', scalar(@$Students));
  299:     $r->print("<table>\n");
  300:     $r->rflush();
  301:     foreach my $student (@$Students) {
  302:         my $sname = $student->{'username'};
  303:         my $sdom = $student->{'domain'};
  304:         my $answer = &Apache::lonstathelpers::analyze_problem_as_student
  305:             ($resource,$sname,$sdom,$partid,$respid);
  306:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  307:                                                  &mt('last student'));
  308:         $student->{'answer'} = $answer;
  309:     }
  310:     $r->print("</table>\n");
  311:     $r->rflush();
  312:     # close progress window
  313:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  314:     return;
  315: }
  316: 
  317: 
  318: #########################################################
  319: #########################################################
  320: ##
  321: ##   Generic Interface Routines
  322: ##
  323: #########################################################
  324: #########################################################
  325: sub CreateInterface {
  326:     ##
  327:     ## Environment variable initialization
  328:     my $Str = '';
  329:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  330:         (undef,'Student Submission Reports');
  331:     $Str .= '<table cellspacing="5">'."\n";
  332:     $Str .= '<tr>';
  333:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  334:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  335:     $Str .= '<td align="center">&nbsp;</td>';
  336:     $Str .= '</tr>'."\n";
  337:     ##
  338:     ## 
  339:     $Str .= '<tr><td align="center">'."\n";
  340:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  341:     $Str .= '</td>';
  342:     #
  343:     $Str .= '<td align="center">';
  344:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  345:     $Str .= '</td>';
  346:     #
  347:     $Str .= '<td></td>';
  348:     #
  349:     my $only_seq_with_assessments = sub { 
  350:         my $s=shift;
  351:         if ($s->{'num_assess'} < 1) { 
  352:             return 0;
  353:         } else { 
  354:             return 1;
  355:         }
  356:     };
  357:     ##
  358:     ##
  359:     $Str .= '</tr>'."\n";
  360:     $Str .= '</table>'."\n";
  361:     #
  362:     # We do this to make sure the sequence information is initialized
  363:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  364:                                               $only_seq_with_assessments);
  365:     
  366:     #
  367:     return $Str;
  368: }
  369: 
  370: 
  371: 
  372: 1;
  373: 
  374: __END__

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