Annotation of loncom/interface/spreadsheet/studentcalc.pm, revision 1.3

1.1       matthew     1: #
1.3     ! matthew     2: # $Id: studentcalc.pm,v 1.2 2003/05/19 14:34:54 matthew Exp $
1.1       matthew     3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: # The LearningOnline Network with CAPA
                     27: # Spreadsheet/Grades Display Handler
                     28: #
                     29: # POD required stuff:
                     30: 
                     31: =head1 NAME
                     32: 
                     33: studentcalc
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: =head1 DESCRIPTION
                     38: 
                     39: =over 4
                     40: 
                     41: =cut
                     42: 
                     43: ###################################################
                     44: ###                 StudentSheet                ###
                     45: ###################################################
                     46: package Apache::studentcalc;
                     47: 
                     48: use strict;
                     49: use Apache::Constants qw(:common :http);
                     50: use Apache::lonnet;
1.3     ! matthew    51: use Apache::loncommon();
        !            52: use Apache::loncoursedata();
1.1       matthew    53: use Apache::lonnavmaps;
1.3     ! matthew    54: use Apache::Spreadsheet();
        !            55: use Apache::assesscalc();
1.1       matthew    56: use HTML::Entities();
                     57: use Spreadsheet::WriteExcel;
                     58: use Time::HiRes;
                     59: 
                     60: @Apache::studentcalc::ISA = ('Apache::Spreadsheet');
                     61: 
                     62: my @Sequences = ();
                     63: my %Exportrows = ();
                     64: 
                     65: my $current_course;
                     66: 
                     67: sub initialize_package {
                     68:     $current_course = $ENV{'request.course.id'};
                     69:     &initialize_sequence_cache();
                     70:     &load_cached_export_rows();
                     71: }
                     72: 
                     73: sub initialize_sequence_cache {
                     74:     #
                     75:     # Set up the sequences and assessments
                     76:     @Sequences = ();
                     77:     my ($top,$sequences,$assessments) = 
                     78:         &Apache::loncoursedata::get_sequence_assessment_data();
                     79:     if (! defined($top) || ! ref($top)) {
                     80:         # There has been an error, better report it
1.2       matthew    81:         &Apache::lonnet::logthis('top is undefined (studentcalc.pm)');
1.1       matthew    82:         return;
                     83:     }
                     84:     @Sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
                     85: }
                     86: 
                     87: sub clear_package {
                     88:     @Sequences = undef;
                     89:     %Exportrows = undef;
                     90: }
                     91: 
                     92: sub get_title {
                     93:     my $self = shift;
                     94:     my $title = '';
1.3     ! matthew    95:     my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
        !            96:                                                       $self->{'domain'});
        !            97:     &Apache::lonnet::logthis('userenv = '.join(' ',%userenv));
        !            98:     my $name = 
        !            99:         join(' ',@userenv{'firstname','middlename','lastname','generation'});
        !           100:     $name =~ s/\s+$//;
        !           101:     $title .= '<h1>'.$name;
        !           102:     if ($ENV{'user.name'} ne $self->{'name'} && 
        !           103:         $ENV{'user.domain'} ne $self->{'domain'}) {
        !           104:         $title .= &Apache::loncommon::aboutmewrapper
        !           105:                                     ($self->{'name'}.'@'.$self->{'domain'},
        !           106:                                      $self->{'name'},$self->{'domain'});
        !           107:     }
        !           108:     $title .= "</h1>\n";
        !           109:     $title .= '<h2>'.$self->{'coursedesc'}."</h2>\n";
        !           110:     $title .= '<h3>'.localtime(time).'</h3>';
1.1       matthew   111:     return $title;
                    112: }
                    113: 
                    114: sub parent_link {
                    115:     my $self = shift;
                    116:     my $link .= '<p><a href="/adm/classcalc?'.
                    117:         'sname='.$self->{'name'}.
                    118:             '&sdomain='.$self->{'domain'}.'">'.
                    119:                 'Course level sheet</a></p>'."\n";
                    120:     return $link;
                    121: }
                    122: 
                    123: sub outsheet_html {
                    124:     my $self = shift;
                    125:     my ($r) = @_;
                    126:     ####################################
                    127:     # Get the list of assessment files #
                    128:     ####################################
                    129:     my @AssessFileNames = $self->othersheets('assesscalc');
1.2       matthew   130:     my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
                    131:                                                 $ENV{'request.course.id'});
1.1       matthew   132:     ####################################
                    133:     # Determine table structure        #
                    134:     ####################################
                    135:     my $num_uneditable = 26;
                    136:     my $num_left = 52-$num_uneditable;
                    137:     my $tableheader =<<"END";
1.2       matthew   138: <p>
1.1       matthew   139: <table border="2">
                    140: <tr>
                    141:   <th colspan="2" rowspan="2"><font size="+2">Student</font></th>
                    142:   <td bgcolor="#FFDDDD" colspan="$num_uneditable">
                    143:       <b><font size="+1">Import</font></b></td>
                    144:   <td colspan="$num_left">
                    145:       <b><font size="+1">Calculations</font></b></td>
                    146: </tr><tr>
                    147: END
                    148:     my $label_num = 0;
                    149:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    150:         if ($label_num<$num_uneditable) { 
                    151:             $tableheader .='<td bgcolor="#FFDDDD">';
                    152:         } else {
                    153:             $tableheader .='<td>';
                    154:         }
                    155:         $tableheader .="<b><font size=+1>$_</font></b></td>";
                    156:         $label_num++;
                    157:     }
                    158:     $tableheader .="</tr>\n";
                    159:     #
                    160:     $r->print($tableheader);
                    161:     #
                    162:     # Print out template row
                    163:     $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
                    164: 	      $self->html_template_row($num_uneditable)."</tr>\n");
                    165:     #
                    166:     # Print out summary/export row
                    167:     $r->print('<tr><td>Export</td><td>0</td>'.
                    168: 	      $self->html_export_row()."</tr>\n");
                    169:     $r->print("</table>\n");
                    170:     #
                    171:     # Prepare to output rows
                    172:     $tableheader =<<"END";
1.2       matthew   173: </p><p>
1.1       matthew   174: <table border="2">
                    175: <tr><th>Row</th><th>Assessment</th>
                    176: END
                    177:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    178: 	if ($label_num<$num_uneditable) { 
                    179:             $tableheader.='<td bgcolor="#FFDDDD">';
                    180:         } else {
                    181:             $tableheader.='<td>';
                    182:         }
                    183:         $tableheader.="<b><font size=+1>$_</font></b></td>";
                    184:     }
                    185:     $tableheader.="\n";
                    186:     #
                    187:     my $num_output = 1;
                    188:     if (scalar(@Sequences)< 1) {
                    189:         &initialize_sequence_cache();
                    190:     }
                    191:     foreach my $Sequence (@Sequences) {
                    192: 	next if ($Sequence->{'num_assess'} < 1);
                    193: 	$r->print("<h3>".$Sequence->{'title'}."</h3>\n");
                    194:  	$r->print($tableheader);
                    195: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    196: 	    next if ($resource->{'type'} ne 'assessment');
                    197: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    198:             my $assess_filename = $self->{'row_source'}->{$rownum};
1.2       matthew   199:             my $row_output = '<tr>';
                    200:             if ($editing_is_allowed) {
                    201:                 $row_output .= '<td>'.$rownum.'</td>';
                    202:                 $row_output .= '<td>'.
                    203:                     '<a href="/adm/assesscalc?sname='.$self->{'name'}.
                    204:                     '&sdomain='.$self->{'domain'}.
                    205:                     '&filename='.$assess_filename.
                    206:                     '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).'">'.
                    207:                     $resource->{'title'}.'</a><br />';
                    208:                 $row_output .= &assess_file_selector($rownum,
                    209:                                                      $assess_filename,
                    210:                                                      \@AssessFileNames).
                    211:                                                          '</td>';
                    212:             } else {
                    213:                 $row_output .= '<td><a href="'.$resource->{'src'}.'?symb='.
                    214:                     &Apache::lonnet::escape($resource->{'symb'}).'>Go To</a>';
                    215:                 $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
                    216:             }
                    217:             $row_output .= $self->html_row($num_uneditable,$rownum).
1.1       matthew   218:                 "</tr>\n";
                    219:             $r->print($row_output);
                    220: 	}
                    221: 	$r->print("</table>\n");
                    222:     }
1.2       matthew   223:     $r->print("</p>\n");
1.1       matthew   224:     return;
                    225: }
                    226: 
                    227: ########################################################
                    228: ########################################################
                    229: 
                    230: =pod
                    231: 
                    232: =item &assess_file_selector()
                    233: 
                    234: =cut
                    235: 
                    236: ########################################################
                    237: ########################################################
                    238: sub assess_file_selector {
                    239:     my ($row,$default,$AssessFiles)=@_;
1.2       matthew   240:     if (!defined($AssessFiles) || ! @$AssessFiles) {
                    241:         return '';
                    242:     }
1.1       matthew   243:     return '' if (! &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}));
                    244:     my $element_name = 'FileSelect_'.$row;
                    245:     my $load_dialog = '<select size="1" name="'.$element_name.'" '.
                    246:         'onchange="'.
                    247:         "document.sheet.cell.value='source_$row';".
                    248:         "document.sheet.newformula.value=document.sheet.$element_name\.value;".
                    249:         'document.sheet.submit()" '.'>'."\n";
                    250:     foreach my $file (@{$AssessFiles}) {
                    251:         $load_dialog .= '    <option name="'.$file.'"';
                    252:         $load_dialog .= ' selected' if ($default eq $file);
                    253:         $load_dialog .= '>'.$file."</option>\n";
                    254:     }
                    255:     $load_dialog .= "</select>\n";
                    256:     return $load_dialog;
                    257: }
                    258: 
                    259: sub modify_cell {
                    260:     my $self = shift;
                    261:     my ($cell,$formula) = @_;
                    262:     if ($cell =~ /^source_(\d+)$/) {
                    263:         # Need to make sure $formula is a valid filename....
                    264:         my $row = $1;
                    265:         $cell = 'A'.$row;
                    266:         $self->{'row_source'}->{$row} = $formula;
                    267:         my $original_source = $self->formula($cell);
                    268:         if ($original_source =~ /__&&&__/) {
                    269:             ($original_source,undef) = split('__&&&__',$original_source);
                    270:         }
                    271:         $formula = $original_source.'__&&&__'.$formula;
                    272:     } elsif ($cell =~ /([A-z])\-/) {
                    273:         $cell = 'template_'.$1;
                    274:     } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
                    275:         return;
                    276:     }
                    277:     $self->set_formula($cell,$formula);
                    278:     $self->rebuild_stats();
                    279:     return;
                    280: }
                    281: 
                    282: sub outsheet_csv {
                    283:     my $self = shift;
                    284:     my ($r) = @_;
                    285: }
                    286: sub outsheet_excel {
                    287:     my $self = shift;
                    288:     my ($r) = @_;
                    289: }
                    290: sub outsheet_recursive_excel {
                    291:     my $self = shift;
                    292:     my ($r) = @_;
                    293: } 
                    294: 
                    295: sub display {
                    296:     my $self = shift;
                    297:     my ($r) = @_;
                    298:     $self->compute();
                    299:     $self->outsheet_html($r);
                    300:     return;
                    301: }
                    302: 
                    303: sub set_row_sources {
                    304:     my $self = shift;
                    305:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
                    306:         next if ($cell !~ /^A(\d+)/ && $1 > 0);
                    307:         my $row = $1;
                    308:         (undef,$value) = split('__&&&__',$value);
                    309:         $value = 'Default' if (! defined($value));
                    310:         $self->{'row_source'}->{$row} = $value;
                    311:     }
                    312:     return;
                    313: }
                    314: 
                    315: sub compute {
                    316:     my $self = shift;
                    317:     $self->logthis('computing');
                    318:     if (! defined($current_course) ||
                    319:         $current_course ne $ENV{'request.course.id'}) {
                    320:         $current_course = $ENV{'request.course.id'};
                    321:         &clear_package();
                    322:         &initialize_sequence_cache();
                    323:     }
                    324:     $self->initialize_safe_space();
                    325:     my @sequences = @Sequences;
                    326:     if (@sequences < 1) {
                    327:         my ($top,$sequences,$assessments) = 
                    328:             &Apache::loncoursedata::get_sequence_assessment_data();
                    329:         if (! defined($top) || ! ref($top)) {
                    330:             &Apache::lonnet::logthis('top is undefined');
                    331:             return;
                    332:         }
                    333:         @sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
                    334:     }
                    335:     &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
                    336:     my %f = $self->formulas();
                    337:     #
                    338:     # Process the formulas list - 
                    339:     #   the formula for the A column of a row is symb__&&__filename
                    340:     my %c = $self->constants();
                    341:     foreach my $seq (@sequences) {
                    342:         next if ($seq->{'num_assess'}<1);
                    343:         foreach my $resource (@{$seq->{'contents'}}) {
                    344:             next if ($resource->{'type'} ne 'assessment');
                    345:             my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    346:             my $cell = 'A'.$rownum;
                    347:             my $assess_filename = 'Default';
                    348:             if (exists($self->{'row_source'}->{$rownum})) {
                    349:                 $assess_filename = $self->{'row_source'}->{$rownum};
                    350:             } else {
                    351:                 $self->{'row_source'}->{$rownum} = $assess_filename;
                    352:             }
                    353:             $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
                    354:             my $assessSheet = Apache::assesscalc->new($self->{'name'},
                    355:                                                       $self->{'domain'},
                    356:                                                       $assess_filename,
                    357:                                                       $resource->{'symb'});
                    358:             my @exportdata = $assessSheet->export_data();
                    359:             #
                    360:             # Be sure not to disturb the formulas in the 'A' column
                    361:             my $data = shift(@exportdata);
                    362:             $c{$cell} = $data if (defined($data));
                    363:             #
                    364:             # Deal with the remaining columns
                    365:             my $i=0;
                    366:             foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
                    367:                 my $cell = $_.$rownum;
                    368:                 my $data = shift(@exportdata);
                    369:                 if (defined($data)) {
                    370:                     $f{$cell} = 'import';
                    371:                     $c{$cell} = $data;
                    372:                 }
                    373:                 $i++;
                    374:             }
                    375:         }
                    376:     }
                    377:     $self->constants(\%c);
                    378:     $self->formulas(\%f);
                    379:     $self->calcsheet();
                    380:     #
                    381:     # Store export row in cache
                    382:     my @exportarray=$self->exportrow();
                    383:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    384:     $Exportrows{$student}->{'time'} = time;
                    385:     $Exportrows{$student}->{'data'} = \@exportarray;
                    386:     # save export row
                    387:     $self->save_export_data();
                    388:     return;
                    389: }
                    390: 
                    391: sub set_row_numbers {
                    392:     my $self = shift;
                    393:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    394:         next if ($cell !~ /^A(\d+)/);
                    395:         my $row = $1;
                    396:         next if ($row == 0);
                    397:         my ($symb,undef) = split('__&&&__',$formula);
                    398:         $self->{'row_numbers'}->{$symb} = $row;
                    399:     }
                    400: }
                    401: 
                    402: sub get_row_number_from_symb {
                    403:     my $self = shift;
                    404:     my ($key) = @_;
                    405:     ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
                    406:     return $self->get_row_number_from_key($key);
                    407: }
                    408: 
                    409: #############################################
                    410: #############################################
                    411: 
                    412: =pod
                    413: 
                    414: =item &load_cached_export_rows
                    415: 
                    416: Retrieves and parsers the export rows of the student spreadsheets.
                    417: These rows are saved in the courses directory in the format:
                    418: 
                    419:  sname:sdom:studentcalc:.time => time
                    420: 
                    421:  sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
                    422: 
                    423: =cut
                    424: 
                    425: #############################################
                    426: #############################################
                    427: sub load_cached_export_rows {
                    428:     %Exportrows = undef;
                    429:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
                    430: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    431: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
                    432:     my %Selected_Assess_Sheet;
                    433:     if ($tmp[0] =~ /^error/) {
                    434:         &Apache::lonnet::logthis('unable to read cached student export rows '.
                    435:                                  'for course '.$ENV{'request.course.id'});
                    436:         return;
                    437:     }
                    438:     my %tmp = @tmp;
                    439:     while (my ($key,$sheetdata) = each(%tmp)) {
                    440:         my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
                    441:         my $student = $sname.':'.$sdom;
                    442:         if ($remainder =~ /\.time/) {
                    443:             $Exportrows{$student}->{'time'} = $sheetdata;
                    444:         } else {
                    445:             $sheetdata =~ s/^___=___//;
                    446:             my @Data = split('___;___',$sheetdata);
                    447:             $Exportrows{$student}->{'data'} = \@Data;
                    448:         }
                    449:     }
                    450: }
                    451: 
                    452: #############################################
                    453: #############################################
                    454: 
                    455: =pod
                    456: 
                    457: =item &save_export_data()
                    458: 
                    459: Writes the export data for this student to the course cache.
                    460: 
                    461: =cut
                    462: 
                    463: #############################################
                    464: #############################################
                    465: sub save_export_data {
                    466:     my $self = shift;
                    467:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    468:     return if (! exists($Exportrows{$student}));
                    469:     return if (! $self->is_default());
                    470:     my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
                    471:     my $timekey = $key.'.time';
                    472:     my $newstore = join('___;___',
                    473:                         @{$Exportrows{$student}->{'data'}});
                    474:     $newstore = '___=___'.$newstore;
                    475:     &Apache::lonnet::put('nohist_calculatedsheets',
                    476:                          { $key     => $newstore,
                    477:                            $timekey => $Exportrows{$student}->{'time'} },
                    478:                          $self->{'cdom'},
                    479:                          $self->{'cnum'});
                    480:     return;
                    481: }
                    482: 
                    483: #############################################
                    484: #############################################
                    485: 
                    486: =pod
                    487: 
                    488: =item &export_data()
                    489: 
                    490: Returns the export data associated with the spreadsheet.  Computes the
                    491: spreadsheet only if necessary.
                    492: 
                    493: =cut
                    494: 
                    495: #############################################
                    496: #############################################
                    497: sub export_data {
                    498:     my $self = shift;
                    499:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    500:     if (! exists($Exportrows{$student}) ||
                    501:         ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
                    502:         $self->compute();
                    503:     }
                    504:     my @Data = @{$Exportrows{$student}->{'data'}};
                    505:     for (my $i=0; $i<=$#Data;$i++) {
                    506:         $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
                    507:     }
                    508:     return @Data;
                    509: }
                    510: 
                    511: 1;
                    512: 
                    513: __END__

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