File:  [LON-CAPA] / loncom / interface / statistics / lonstudentsubmissions.pm
Revision 1.14: download - view: text, annotated - select for diffs
Tue Aug 31 15:52:13 2004 UTC (19 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Reworked previous commit to be more concise and maintainable.

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

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