File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.12: download - view: text, annotated - select for diffs
Fri Jun 25 20:43:33 2004 UTC (20 years ago) by matthew
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
Added extra verbage to alleviate confusion as to current status of
spreadsheet compilation.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentsubmissions.pm,v 1.12 2004/06/25 20:43:33 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 => 'SelectAnother',
   47:                        text => 'Choose a different Problem' },
   48:                      { name => 'Generate',
   49:                        text => 'Generate Spreadsheet'},
   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:     #
   74:     my @CacheButtonHTML = 
   75:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
   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('&nbsp;'x5);
   87:             }
   88:         }
   89:         foreach my $html (@CacheButtonHTML) {
   90:             $r->print($html.('&nbsp;'x5));
   91:         }
   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>');
  125:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  126:             $r->rflush();
  127:             my %Data = &Apache::lonstathelpers::get_problem_data
  128:                 ($resource->{'src'});
  129:             my $ProblemData = $Data{$current_problem->{'part'}.
  130:                                     '.'.
  131:                                     $current_problem->{'respid'}};
  132:             &prepare_excel_output($r,$current_problem,
  133:                                   $ProblemData,\@Students);
  134:         }
  135:         $r->print('<hr />');
  136:     } else {
  137:         $r->print('<input type="submit" name="Generate" value="'.
  138:                   &mt('Generate Spreadsheet').'" />');
  139:         $r->print('&nbsp;'x5);
  140:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
  141:         $r->print(&Apache::lonstathelpers::ProblemSelector('.'));
  142:     }
  143: }
  144: 
  145: #########################################################
  146: #########################################################
  147: ##
  148: ##      Excel output of student answers and correct answers
  149: ##
  150: #########################################################
  151: #########################################################
  152: sub prepare_excel_output {
  153:     my ($r,$problem,$ProblemData,$Students) = @_;
  154:     my $c = $r->connection();
  155:     my ($resource,$respid,$partid) = ($problem->{'resource'},
  156:                                       $problem->{'respid'},
  157:                                       $problem->{'part'});
  158:     $r->print('<h2>'.
  159:               &mt('Preparing Excel spreadsheet of student responses').
  160:               '</h2>'.
  161:               '<p>'.
  162:               &mt('See the status bar above for student answer computation progress').
  163:               '</p>');
  164:     #
  165:     &Apache::lonstathelpers::GetStudentAnswers($r,$problem,$Students,
  166:                                                'Statistics','stats_status');
  167:     #
  168:     $r->print('<script>'.
  169:               'window.document.Statistics.stats_status.value="'.
  170:               'Done computing student answers.  Compiling spreadsheet.'.
  171:               '";</script>');
  172:     $r->rflush();
  173:     my @Columns = ( 'username','domain','attempt','time',
  174:                     'submission','correct', 'grading','awarded','weight',
  175:                     'score');
  176:     my $awarded_col = 7;
  177:     my $weight_col  = 8;
  178:     #
  179:     # Create excel worksheet
  180:     my $filename = '/prtspool/'.
  181:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  182:         time.'_'.rand(1000000000).'.xls';
  183:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  184:     if (! defined($workbook)) {
  185:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  186:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
  187:                             "This error has been logged.  ".
  188:                             "Please alert your LON-CAPA administrator").
  189:                   '</p>');
  190:         return undef;
  191:     }
  192:     #
  193:     $workbook->set_tempdir('/home/httpd/perl/tmp');
  194:     #
  195:     my $format = &Apache::loncommon::define_excel_formats($workbook);
  196:     my $worksheet  = $workbook->addworksheet('Student Submission Data');
  197:     #
  198:     # Make sure we get new weight data instead of data on a 10 minute delay
  199:     &Apache::lonnet::clear_EXT_cache_status();
  200:     #
  201:     # Put on the standard headers and whatnot
  202:     my $rows_output=0;
  203:     $worksheet->write($rows_output++,0,$resource->{'title'},$format->{'h1'});
  204:     $worksheet->write($rows_output++,0,$resource->{'src'},$format->{'h3'});
  205:     $rows_output++;
  206:     $worksheet->write_row($rows_output++,0,\@Columns,$format->{'bold'});
  207:     #
  208:     # Populate the worksheet with the student data
  209:     foreach my $student (@$Students) {
  210:         last if ($c->aborted());
  211:         my $results = &Apache::loncoursedata::get_response_data_by_student
  212:             ($student,$resource->{'symb'},$respid);
  213:         my %row;
  214:         $row{'username'} = $student->{'username'};
  215:         $row{'domain'}   = $student->{'domain'};
  216:         $row{'correct'} = $student->{'answer'};
  217:         $row{'weight'} = &Apache::lonnet::EXT
  218:             ('resource.'.$partid.'.weight',$resource->{'symb'},
  219:              undef,undef,undef);
  220:         if (! defined($results) || ref($results) ne 'ARRAY') {
  221:             $row{'score'} = '='.
  222:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  223:                     ($rows_output,$awarded_col)
  224:                 .'*'.
  225:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  226:                     ($rows_output,$weight_col);
  227:             my $cols_output = 0;
  228:             foreach my $col (@Columns) {
  229:                 if (! exists($row{$col})) {
  230:                     $cols_output++;
  231:                     next;
  232:                 }
  233:                 $worksheet->write($rows_output,$cols_output++,$row{$col});
  234:             }
  235:             $rows_output++;
  236:         } else {
  237:             foreach my $response (@$results) {
  238:                 delete($row{'time'});
  239:                 delete($row{'attempt'});
  240:                 delete($row{'submission'});
  241:                 delete($row{'awarded'});
  242:                 delete($row{'grading'});
  243:                 delete($row{'score'});
  244:                 my %row_format;
  245:                 #
  246:                 # Time is handled differently
  247:                 $row{'time'} = &Apache::lonstathelpers::calc_serial
  248:                     ($response->[&Apache::loncoursedata::RDs_timestamp()]);
  249:                 $row_format{'time'}=$format->{'date'};
  250:                 #
  251:                 $row{'attempt'}  = $response->[
  252:                      &Apache::loncoursedata::RDs_tries()];
  253:                 $row{'submission'} = $response->[
  254:                      &Apache::loncoursedata::RDs_submission()];
  255:                 if ($row{'submission'} =~ m/^=/) {
  256:                     # This will be interpreted as a formula.  That is bad!
  257:                     $row{'submission'} = " ".$row{'submission'};
  258:                 }
  259:                 $row{'grading'} = $response->[
  260:                      &Apache::loncoursedata::RDs_awarddetail()];
  261:                 $row{'awarded'} = $response->[
  262:                      &Apache::loncoursedata::RDs_awarded()];
  263:                 $row{'score'} = '='.
  264:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  265:                         ($rows_output,$awarded_col)
  266:                     .'*'.
  267:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
  268:                         ($rows_output,$weight_col);
  269:                 my $cols_output = 0;
  270:                 foreach my $col (@Columns) {
  271:                     $worksheet->write($rows_output,$cols_output++,$row{$col},
  272:                                       $row_format{$col});
  273:                 }
  274:                 $rows_output++;
  275:             }
  276:         } # End of else clause on if (! defined($results) ....
  277:     }
  278:     #
  279:     # Close the excel file
  280:     $workbook->close();
  281:     #
  282:     # Write a link to allow them to download it
  283:     $r->print('<p><a href="'.$filename.'">'.
  284:               &mt('Your Excel spreadsheet.').
  285:               '</a></p>'."\n");
  286:     $r->print('<script>'.
  287:               'window.document.Statistics.stats_status.value="'.
  288:               'Done compiling spreadsheet.  See link below to download.'.
  289:               '";</script>');
  290:     $r->rflush();
  291: 
  292: }
  293: 
  294: #########################################################
  295: #########################################################
  296: ##
  297: ##   Generic Interface Routines
  298: ##
  299: #########################################################
  300: #########################################################
  301: sub CreateInterface {
  302:     ##
  303:     ## Environment variable initialization
  304:     my $Str = '';
  305:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  306:         (undef,'Student Submission Reports');
  307:     $Str .= '<p>';
  308:     $Str .= '<table cellspacing="5">'."\n";
  309:     $Str .= '<tr>';
  310:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  311:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  312:     $Str .= '</tr>'."\n";
  313:     #
  314:     $Str .= '<tr><td align="center">'."\n";
  315:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  316:     $Str .= '</td>';
  317:     #
  318:     $Str .= '<td align="center">';
  319:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  320:     $Str .= '</td>';
  321:     #
  322:     $Str .= '</tr>'."\n";
  323:     $Str .= '</table>'."\n";
  324:     #
  325:     $Str .= '<nobr>'.&mt('Status: [_1]',
  326:                          '<input type="text" '.
  327:                          'name="stats_status" size="60" value="" />').
  328:             '</nobr>'.'</p>';    
  329:     ##
  330:     return $Str;
  331: }
  332: 
  333: 
  334: 
  335: 1;
  336: 
  337: __END__

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