Annotation of loncom/interface/spreadsheet/assesscalc.pm, revision 1.39

1.1       matthew     1: #
1.39    ! albertel    2: # $Id: assesscalc.pm,v 1.38 2005/03/10 17:33:57 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: assesscalc
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: =head1 DESCRIPTION
                     38: 
                     39: =cut
                     40: 
                     41: ###################################################
                     42: ###                 AssessSheet                 ###
                     43: ###################################################
                     44: package Apache::assesscalc;
                     45: 
                     46: use strict;
1.18      matthew    47: use warnings FATAL=>'all';
                     48: no warnings 'uninitialized';
1.1       matthew    49: use Apache::Constants qw(:common :http);
                     50: use Apache::lonnet;
1.2       matthew    51: use Apache::loncommon;
1.1       matthew    52: use Apache::Spreadsheet;
1.25      matthew    53: use Apache::loncoursedata();
1.1       matthew    54: use HTML::Entities();
                     55: use Spreadsheet::WriteExcel;
                     56: use GDBM_File;
                     57: use Time::HiRes;
1.26      www        58: use Apache::lonlocal;
1.1       matthew    59: 
                     60: @Apache::assesscalc::ISA = ('Apache::Spreadsheet');
                     61: 
                     62: ########################################################
                     63: ########################################################
                     64: 
                     65: =pod
                     66: 
                     67: =head2 Package Variables
                     68: 
                     69: =over 4
                     70: 
                     71: =item %Exportrows
                     72: 
                     73: =item $current_name
                     74: 
                     75: =item $current_domain
                     76: 
                     77: =item $current_course
                     78: 
                     79: =item %parmhash
                     80: 
                     81: =item %nice_parameter_name
                     82: 
                     83: =item %useropt
                     84: 
                     85: =item %courseopt
                     86: 
                     87: =back 
                     88: 
                     89: =cut
                     90: 
                     91: ########################################################
                     92: ########################################################
                     93: 
                     94: my %Exportrows;
1.22      matthew    95: my %newExportrows;
1.1       matthew    96: 
                     97: my $current_name;
                     98: my $current_domain;
                     99: my $current_course;
                    100: 
                    101: my %parmhash;
                    102: my %nice_parameter_name;
                    103: 
                    104: my %useropt;
1.22      matthew   105: my %userdata;
1.1       matthew   106: my %courseopt;
1.37      matthew   107: my $navmap;
1.1       matthew   108: 
                    109: ########################################################
                    110: ########################################################
                    111: 
                    112: =pod
                    113: 
                    114: =head2 Package Subroutines
                    115: 
                    116: =item &clear_package()
                    117: 
1.22      matthew   118: Reset all package variables and clean up caches.
1.1       matthew   119: 
                    120: =cut
                    121: 
                    122: ########################################################
                    123: ########################################################
                    124: sub clear_package {
1.22      matthew   125:     if (defined($current_name) &&
                    126:         defined($current_domain) &&
                    127:         defined($current_course) &&
1.39    ! albertel  128:         $current_course eq $env{'request.course.id'} &&
1.22      matthew   129:         %newExportrows) {
                    130:         &save_cached_export_rows($current_name,$current_domain);
                    131:     }
1.1       matthew   132:     undef(%Exportrows);
1.22      matthew   133:     undef(%newExportrows);
1.1       matthew   134:     undef($current_name);
                    135:     undef($current_domain);
                    136:     undef($current_course);
                    137:     undef(%useropt);
1.22      matthew   138:     undef(%userdata);
1.1       matthew   139:     undef(%courseopt);
1.37      matthew   140:     undef($navmap);
1.1       matthew   141: }
                    142: 
1.22      matthew   143: sub save_cached_export_rows {
                    144:     my ($sname,$sdomain) = @_;
                    145:     my $result = &Apache::lonnet::put
1.39    ! albertel  146:         ('nohist_calculatedsheets_'.$env{'request.course.id'},
1.22      matthew   147:          $newExportrows{$sname.':'.$sdomain},
                    148:          $sdomain,$sname);
                    149:     delete($newExportrows{$sname.':'.$sdomain});
                    150: }
                    151: 
1.11      matthew   152: sub initialize {
1.37      matthew   153:     my ($in_navmap) = @_;
1.11      matthew   154:     &clear_package();
1.37      matthew   155:     $navmap = $in_navmap;
                    156:     if (! defined($navmap)) {
                    157:         $navmap = Apache::lonnavmaps::navmap->new();
                    158:     }
                    159:     if (!defined($navmap)) {
                    160:         &Apache::lonnet::logthis('assesscalc:Can not open Coursemap');
                    161:     }
1.25      matthew   162:     &Apache::loncoursedata::clear_internal_caches();
1.11      matthew   163: }
                    164: 
1.1       matthew   165: ########################################################
                    166: ########################################################
                    167: 
                    168: =pod
                    169: 
                    170: =item &initialize_package()
                    171: 
                    172: =cut
                    173: 
                    174: ########################################################
                    175: ########################################################
                    176: sub initialize_package {
1.37      matthew   177:     my ($sname,$sdomain,$in_navmap) = @_;
1.1       matthew   178:     $current_name   = $sname;
                    179:     $current_domain = $sdomain;
1.37      matthew   180:     $navmap = $in_navmap;
1.21      matthew   181:     undef(%useropt);
1.22      matthew   182:     undef(%userdata);
1.39    ! albertel  183:     if ($current_course ne $env{'request.course.id'}) {
        !           184:         $current_course = $env{'request.course.id'};
1.13      matthew   185:         undef(%courseopt);
                    186:     }
1.1       matthew   187:     &load_cached_export_rows();
                    188:     &load_parameter_caches();
1.25      matthew   189:     &Apache::loncoursedata::clear_internal_caches();
1.1       matthew   190: }
                    191: 
1.22      matthew   192: 
1.1       matthew   193: ########################################################
                    194: ########################################################
                    195: 
                    196: =pod
                    197: 
                    198: =item &load_parameter_caches()
                    199: 
                    200: =cut
                    201: 
                    202: ########################################################
                    203: ########################################################
                    204: sub load_parameter_caches {
                    205:     my $userprefix = $current_name.':'.$current_domain.'_';
                    206:     $userprefix =~ s/:/_/g;
                    207:     #
                    208:     # Course Parameters Cache
                    209:     if (! %courseopt) {
1.39    ! albertel  210:         $current_course = $env{'request.course.id'};
1.1       matthew   211:         undef(%courseopt);
                    212:         if (! defined($current_name) || ! defined($current_domain)) {
                    213:             return;
                    214:         }
1.39    ! albertel  215:         my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
        !           216:         my $id  = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1       matthew   217:         my %Tmp = &Apache::lonnet::dump('resourcedata',$dom,$id);
                    218:         while (my ($name,$value) = each(%Tmp)) {
1.3       matthew   219:             $courseopt{$name}=$value;
1.1       matthew   220:         }
                    221:     }
                    222:     if (! %useropt) {
                    223:         my %Tmp = &Apache::lonnet::dump('resourcedata',
                    224:                                         $current_domain,$current_name);
                    225:         while (my ($name,$value) = each(%Tmp)) {
                    226:             if ($name =~ /^error: 2/ || $name =~ /no such file/) {
                    227:                 undef(%useropt);
                    228:                 last;
                    229:             }
                    230:             $useropt{$userprefix.$name}=$value;
                    231:         }
1.21      matthew   232:         $useropt{'loadtime'} = time;
1.1       matthew   233:     }
1.22      matthew   234:     if (! %userdata) {
                    235:         %userdata = &Apache::loncoursedata::get_current_state($current_name,
                    236:                                                               $current_domain);
                    237:         $userdata{'loadtime'} = time;
                    238:     }
                    239:     return;
1.1       matthew   240: }
                    241: 
                    242: ########################################################
                    243: ########################################################
                    244: 
                    245: =pod
                    246: 
                    247: =head2 assesscalc object methods
                    248: 
                    249: =cut
                    250: 
                    251: ########################################################
                    252: ########################################################
1.22      matthew   253: sub ensure_current_caches {
1.1       matthew   254:     my $self = shift;
1.19      matthew   255:     ##
                    256:     ## Check for a modified parameters
                    257:     ##
1.1       matthew   258:     if (! defined($current_course) || 
1.39    ! albertel  259:         $current_course ne $env{'request.course.id'} ) {
        !           260:         $current_course = $env{'request.course.id'};
1.1       matthew   261:         undef(%courseopt); 
1.22      matthew   262:         undef(%useropt);
                    263:         undef(%userdata);
1.1       matthew   264:     }
1.19      matthew   265:     ##
                    266:     ## Check for new user
                    267:     ##
1.1       matthew   268:     if (! defined($current_name)   || $current_name ne $self->{'name'} ||
                    269:         ! defined($current_domain) || $current_domain ne $self->{'domain'}) {
                    270:         $current_domain = $self->{'domain'};
                    271:         $current_name   = $self->{'name'};
                    272:         undef(%useropt);
1.22      matthew   273:         undef(%userdata);
1.1       matthew   274:     }
                    275:     &load_parameter_caches();
                    276: }
                    277: 
                    278: ##################################################
                    279: ##################################################
                    280: 
                    281: =pod
                    282: 
                    283: =item &parmval()
                    284: 
                    285: Determine the value of a parameter.
                    286: 
                    287: Inputs: $what, the parameter needed, $symb, $uname, $udom, $csec 
                    288: 
                    289: Returns: The value of a parameter, or '' if none.
                    290: 
                    291: This function cascades through the possible levels searching for a value for
                    292: a parameter.  The levels are checked in the following order:
                    293: user, course (at section level and course level), map, and lonnet::metadata.
                    294: This function uses %parmhash, which must be tied prior to calling it.
                    295: This function also requires %courseopt and %useropt to be initialized for
                    296: this user and course.
                    297: 
                    298: =cut
                    299: 
                    300: ##################################################
                    301: ##################################################
                    302: sub parmval {
                    303:     my $self = shift;
1.17      albertel  304:     my ($what,$symb,$uname,$udom,$csec,$recurse)=@_;
1.1       matthew   305:     $uname = $self->{'name'}    if (! defined($uname));
                    306:     $udom  = $self->{'domain'}  if (! defined($udom));
                    307:     $csec  = $self->{'section'} if (! defined($csec));
                    308:     $symb  = $self->{'symb'}    if (! defined($symb));
                    309:     #
                    310:     my $result='';
                    311:     #
                    312:     # This should be a 
1.20      www       313:     my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
1.1       matthew   314:     # Cascading lookup scheme
                    315:     my $rwhat=$what;
                    316:     $what =~ s/^parameter\_//;
                    317:     $what =~ s/\_([^\_]+)$/\.$1/;
                    318:     #
                    319:     my $symbparm = $symb.'.'.$what;
                    320:     my $mapparm  = $mapname.'___(all).'.$what;
1.3       matthew   321:     my $courseprefix = $self->{'cid'};
1.1       matthew   322:     my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
                    323:     #
1.3       matthew   324:     my $seclevel  = $courseprefix.'.['.$csec.'].'.$what;
                    325:     my $seclevelr = $courseprefix.'.['.$csec.'].'.$symbparm;
                    326:     my $seclevelm = $courseprefix.'.['.$csec.'].'.$mapparm;
                    327:     #
                    328:     my $courselevel  = $courseprefix.'.'.$what;
                    329:     my $courselevelr = $courseprefix.'.'.$symbparm;
                    330:     my $courselevelm = $courseprefix.'.'.$mapparm;
                    331:     #
                    332:     my $ucourselevel  = $usercourseprefix.'.'.$what;
                    333:     my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
                    334:     my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
1.1       matthew   335:    # check user
                    336:     if (defined($uname)) {
1.3       matthew   337:         return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
                    338:         return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
                    339:         return $useropt{$ucourselevel}  if (defined($useropt{$ucourselevel}));
1.1       matthew   340:     }
                    341:     # check section
                    342:     if (defined($csec)) {
                    343:         return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
                    344:         return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
                    345:         return $courseopt{$seclevel}  if (defined($courseopt{$seclevel}));
                    346:     }
                    347:     #
                    348:     # check course
                    349:     return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
                    350:     # check map parms
                    351:     my $thisparm = $parmhash{$symbparm};
                    352:     return $thisparm if (defined($thisparm));
                    353:     # check default
                    354:     $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
                    355:     return $thisparm if (defined($thisparm));
1.36      albertel  356:     # check more course
                    357:     return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
                    358:     return $courseopt{$courselevel}  if (defined($courseopt{$courselevel}));
                    359: 
1.1       matthew   360:     # Cascade Up
                    361:     my $space=$what;
1.35      albertel  362:     $space=~s/\.[^._]+$//;
1.1       matthew   363:     if ($space ne '0') {
                    364: 	my @parts=split(/_/,$space);
                    365: 	my $id=pop(@parts);
                    366: 	my $part=join('_',@parts);
                    367: 	if ($part eq '') { $part='0'; }
                    368: 	my $newwhat=$rwhat;
                    369: 	$newwhat=~s/\Q$space\E/$part/;
1.17      albertel  370: 	my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1);
1.1       matthew   371: 	if (defined($partgeneral)) { return $partgeneral; }
                    372:     }
1.17      albertel  373:     if ($recurse) { return undef; }
                    374:     my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
                    375:     if (defined($pack_def)) { return $pack_def; }
1.1       matthew   376:     #nothing defined
                    377:     return '';
                    378: }
                    379: 
1.8       matthew   380: sub get_html_title {
                    381:     my $self = shift;
1.29      matthew   382:     my ($assess_title,$name,$time) = $self->get_full_title();
1.8       matthew   383:     my $title = '<h1>'.$assess_title.'</h1>'.
                    384:         '<h2>'.$name.', '.
                    385:         &Apache::loncommon::aboutmewrapper
                    386:                          ($self->{'name'}.'@'.$self->{'domain'},
                    387:                           $self->{'name'},$self->{'domain'});
                    388:     $title .= '<h3>'.$time.'</h3>';
                    389:     return $title;
                    390: }
                    391: 
1.1       matthew   392: sub get_title {
                    393:     my $self = shift;
1.16      matthew   394:     if (($self->{'symb'} eq '_feedback') ||
                    395:         ($self->{'symb'} eq '_evaluation') ||
                    396:         ($self->{'symb'} eq '_discussion') ||
                    397:         ($self->{'symb'} eq '_tutoring')) {
                    398:         my $assess_title = ucfirst($self->{'symb'});
1.8       matthew   399:         $assess_title =~ s/^_//;
1.29      matthew   400:         return $assess_title;
1.1       matthew   401:     } else {
1.29      matthew   402:         return &Apache::lonnet::gettitle($self->{'symb'});
1.1       matthew   403:     }
1.29      matthew   404: }
                    405: 
                    406: sub get_full_title {
                    407:     my $self = shift;
                    408:     my @title = ($self->get_title());
1.2       matthew   409:     # Look up the users identifying information
                    410:     # Get the users information
1.34      albertel  411:     my $name = &Apache::loncommon::plainname($self->{'name'},
                    412: 					     $self->{'domain'});
1.8       matthew   413:     push (@title,$name);
1.26      www       414:     push (@title,&Apache::lonlocal::locallocaltime(time));
1.8       matthew   415:     return @title;
1.1       matthew   416: }
                    417: 
                    418: sub parent_link {
                    419:     my $self = shift;
                    420:     my $link .= '<p><a href="/adm/studentcalc?'.
                    421:         'sname='.$self->{'name'}.
                    422:             '&sdomain='.$self->{'domain'}.'">'.
1.26      www       423:                 &mt('Student level sheet').'</a></p>'."\n";
1.1       matthew   424:     return $link;
                    425: }
                    426: 
                    427: sub outsheet_html {
                    428:     my $self = shift;
                    429:     my ($r) = @_;
1.29      matthew   430:     ####################################
                    431:     # Report any calculation errors    #
                    432:     ####################################
                    433:     $r->print($self->html_report_error());
1.1       matthew   434:     ###################################
                    435:     # Determine table structure
                    436:     ###################################
1.14      matthew   437:     my $importcolor = '#FFFFFF';
1.15      matthew   438:     my $exportcolor = '#FFFFAA';
1.1       matthew   439:     my $num_uneditable = 1;
                    440:     my $num_left = 52-$num_uneditable;
1.26      www       441:     my %lt=&Apache::lonlocal::texthash(
                    442: 				       'as' => 'Assessment',
                    443: 				       'ca' => 'Calculations',
                    444: 				       );
1.1       matthew   445:     my $tableheader =<<"END";
                    446: <table border="2">
                    447: <tr>
1.26      www       448:   <th colspan="2" rowspan="2"><font size="+2">$lt{'as'}</font></th>
1.14      matthew   449:   <td bgcolor="$importcolor" colspan="$num_uneditable">&nbsp;</td>
1.1       matthew   450:   <td colspan="$num_left">
1.26      www       451:       <b><font size="+1">$lt{'ca'}</font></b></td>
1.1       matthew   452: </tr><tr>
                    453: END
                    454:     my $label_num = 0;
                    455:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    456:         if ($label_num<$num_uneditable) { 
1.14      matthew   457:             $tableheader .= '<td bgcolor="'.$importcolor.'">';
1.1       matthew   458:         } else {
                    459:             $tableheader .= '<td>';
                    460:         }
                    461:         $tableheader .= "<b><font size=+1>$_</font></b></td>";
                    462:         $label_num++;
                    463:     }
                    464:     $tableheader.="</tr>\n";
                    465:     #
                    466:     $r->print($tableheader);
                    467:     #
                    468:     # Print out template row
                    469:     $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
1.14      matthew   470: 	      $self->html_template_row($num_uneditable,$importcolor).
                    471:               "</tr>\n");
1.1       matthew   472:     #
                    473:     # Print out summary/export row
                    474:     $r->print('<tr><td>Export</td><td>0</td>'.
1.14      matthew   475: 	      $self->html_export_row($exportcolor)."</tr>\n");
1.1       matthew   476:     #
                    477:     # Prepare to output rows
                    478:     $tableheader =<<"END";
                    479: <table border="2">
                    480: <tr><th>row</th><th>Item</th>
                    481: END
                    482:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    483: 	if ($label_num<$num_uneditable) { 
1.14      matthew   484:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   485:         } else {
                    486:             $tableheader.='<th>';
                    487:         }
                    488:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    489:     }
                    490:     #
                    491:     my $num_output = 0;
1.8       matthew   492:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
1.28      matthew   493:         if (! $self->parameter_part_is_valid(
                    494:                                              $self->{'formulas'}->{'A'.$rownum}
                    495:                                              )) {
                    496:             next;
                    497:         }
1.1       matthew   498: 	if ($num_output++ % 50 == 0) {
                    499: 	    $r->print("</table>\n".$tableheader);
                    500: 	}
                    501: 	$r->print('<tr><td>'.$rownum.'</td>'.
1.14      matthew   502:                   $self->assess_html_row($rownum,$importcolor)."</tr>\n");
1.1       matthew   503:     }
                    504:     $r->print("</table>\n");
                    505:     return;
                    506: }
                    507: 
                    508: sub assess_html_row {
                    509:     my $self = shift();
1.14      matthew   510:     my ($row,$importcolor) = @_;
1.1       matthew   511:     my $parameter_name = $self->{'formulas'}->{'A'.$row};
                    512:     my @rowdata = $self->get_row($row);
                    513:     my $num_cols_output = 0;
                    514:     my $row_html;
                    515:     if (exists($nice_parameter_name{$parameter_name})) {
                    516:         my $name = $nice_parameter_name{$parameter_name};
                    517:         $name =~ s/ /\&nbsp;/g;
                    518:         $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
                    519:     } else {
                    520:         $row_html .= '<td>'.$parameter_name.'</td>';
                    521:     }
                    522:     foreach my $cell (@rowdata) {
1.10      matthew   523:         if ($num_cols_output < 1) {
1.14      matthew   524:             $row_html .= '<td bgcolor="'.$importcolor.'">';
1.10      matthew   525:             $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
                    526:                                                                     '#FFDDDD');
                    527:         } else {
                    528:             $row_html .= '<td bgcolor="#EOFFDD">';
                    529:             $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
                    530:                                                                   '#E0FFDD',1);
                    531:         }
1.1       matthew   532: 	$row_html .= '</td>';
1.10      matthew   533:         $num_cols_output++;
1.1       matthew   534:     }
                    535:     return $row_html;
                    536: }
                    537: 
1.9       matthew   538: sub csv_rows {
                    539:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    540:     # by Spreadsheet::outsheet_excel;
1.1       matthew   541:     my $self = shift;
1.30      matthew   542:     my ($connection,$filehandle) = @_;
1.9       matthew   543:     #
                    544:     # Write a header row
                    545:     $self->csv_output_row($filehandle,undef,
1.26      www       546:                           (&mt('Parameter'),&mt('Description'),&mt('Value')));
1.9       matthew   547:     #
                    548:     # Write each row
                    549:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    550:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    551:         my $description = '';
                    552:         if (exists($nice_parameter_name{$parameter_name})) {
                    553:             $description = $nice_parameter_name{$parameter_name};
                    554:         }
                    555:         $self->csv_output_row($filehandle,$rownum,
                    556:                               $parameter_name,$description);
                    557:     }
                    558:     return;
1.1       matthew   559: }
                    560: 
1.8       matthew   561: sub excel_rows {
                    562:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    563:     # by Spreadsheet::outsheet_excel;
1.1       matthew   564:     my $self = shift;
1.38      matthew   565:     my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
1.30      matthew   566:     return if (! ref($worksheet));
1.8       matthew   567:     #
                    568:     # Write a header row
                    569:     $cols_output = 0;
                    570:     foreach my $value ('Parameter','Description','Value') {
1.38      matthew   571:         $worksheet->write($rows_output,$cols_output++,$value,$format->{'h4'});
1.8       matthew   572:     }
1.38      matthew   573:     $rows_output++;
1.8       matthew   574:     #
                    575:     # Write each row
                    576:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    577:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    578:         my $description = '';
                    579:         if (exists($nice_parameter_name{$parameter_name})) {
                    580:             $description = $nice_parameter_name{$parameter_name};
                    581:         }
                    582:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    583:                                 $parameter_name,$description);
                    584:     }
                    585:     return;
1.1       matthew   586: }
                    587: 
1.21      matthew   588: ##
                    589: ## Routines to support assesscalc::compute
                    590: ##
                    591: sub get_parm_names {
                    592:     my $self = shift;
                    593:     my @Mandatory_parameters = @_;
                    594:     my %parameters_and_names;
                    595:     #
1.23      albertel  596:     my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
1.21      matthew   597:     my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
                    598:     foreach my $parm (@Mandatory_parameters,@Metadata) {
                    599:         next if ($parm !~ /^(resource\.|stores|parameter)_/);
                    600:         my $cleaned_name = $parm;
                    601:         $cleaned_name =~ s/^resource\./stores_/;
                    602:         $cleaned_name =~ s/\./_/g;
                    603:         my $display = &Apache::lonnet::metadata($srcf,
                    604:                                                 $cleaned_name.'.display');
                    605:         if (! $display) {
                    606:             $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
                    607:         }
                    608:         $parameters_and_names{$cleaned_name}=$display;
                    609:     }
                    610:     return (%parameters_and_names);
                    611: }
                    612: 
                    613: sub get_parameter_values {
                    614:     my $self = shift();
                    615:     my @Parameters;
                    616:     my ($parameters) = @_;
                    617:     if (!ref($parameters)) {
                    618:         @Parameters = @_;
                    619:     } elsif (ref($parameters) eq 'ARRAY') {
                    620:         @Parameters = @$parameters;
                    621:     } elsif (ref($parameters) eq 'HASH') {
                    622:         @Parameters = keys(%$parameters);
                    623:     }
                    624:     #
                    625:     my %parameters;
                    626:     #
                    627:     my $filename = $self->{'coursefilename'}.'_parms.db';
                    628:     if (tie(%parmhash,'GDBM_File',
                    629:             $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
                    630:         foreach my $parmname (@Parameters) {
                    631:             my $value = $self->parmval($parmname);
                    632:             $parameters{$parmname} =$value;
                    633:         }
                    634:         untie(%parmhash);
                    635:     } else {
                    636:         $self->logthis('unable to tie '.$filename);
                    637:     }
                    638:     return %parameters;
                    639: }
                    640: 
                    641: sub deal_with_export_row {
                    642:     my $self = shift();
                    643:     my @exportarray = @_;
                    644:     $Exportrows{$self->{'symb'}}->{'time'} = time;
                    645:     $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
                    646:     #
                    647:     # Save the export data
                    648:     $self->save_export_data();
                    649:     return;
                    650: }
                    651: 
1.22      matthew   652: sub get_problem_state {
                    653:     my $self = shift;
                    654:     my %student_parameters;
                    655:     if (exists($userdata{$self->{'symb'}}) && 
                    656:         ref($userdata{$self->{'symb'}}) eq 'HASH') {
                    657:         %student_parameters = %{$userdata{$self->{'symb'}}};
                    658:     }
                    659:     return %student_parameters;
                    660: }
1.21      matthew   661: 
1.28      matthew   662: sub determine_parts {
                    663:     my $self = shift;
                    664:     if (exists($self->{'Parts'}) && ref($self->{'Parts'}) eq 'HASH') {
                    665:         return;
                    666:     }
                    667:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($self->{'symb'});
                    668:     my $src = &Apache::lonnet::clutter($url);
                    669:     return if (! defined($src));
                    670:     my %Parts;
                    671:     my $metadata = &Apache::lonnet::metadata($src,'packages');
                    672:     foreach (split(',',$metadata)) {
                    673:         my ($part) = (/^part_(.*)$/);
                    674:         if (defined($part) && 
                    675:             ! &Apache::loncommon::check_if_partid_hidden
                    676:                 ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
                    677:             ) {
                    678:             $Parts{$part}++;
                    679:         }
                    680:     }
                    681:     # Make sure part 0 is defined.
                    682:     $Parts{'0'}++;
                    683:     $self->{'Parts'} = \%Parts;
                    684:     return;
                    685: }
                    686: 
                    687: sub parameter_part_is_valid {
                    688:     my $self = shift;
                    689:     my ($parameter) = @_;
                    690:     return 1 if ($parameter eq 'timestamp');
                    691:     if (! defined($self->{'Parts'}) || 
                    692:         ! ref ($self->{'Parts'})    ||
                    693:         ref($self->{'Parts'}) ne 'HASH') {
                    694:         return 1;
                    695:     }
                    696:     #
                    697:     my (undef,$part) = 
                    698:         ($parameter =~ m/^(resource|stores|parameter)_([^_]+)_.*/);
                    699:     if (exists($self->{'Parts'})          && 
                    700:         exists($self->{'Parts'}->{$part}) &&
                    701:         $self->{'Parts'}->{$part} ) {
                    702:         return 1;
                    703:     } else {
                    704:         return 0;
                    705:     }
                    706: }
                    707: 
1.1       matthew   708: sub compute {
                    709:     my $self = shift;
1.19      matthew   710:     my ($r) = @_;
                    711:     my $connection = $r->connection();
                    712:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   713:     $self->initialize_safe_space();
1.11      matthew   714:     #########################################
                    715:     #########################################
                    716:     ###                                   ###
                    717:     ###  Retrieve the problem parameters  ###
                    718:     ###                                   ###
                    719:     #########################################
                    720:     #########################################
                    721:     my @Mandatory_parameters = ("stores_0_solved",
                    722:                                 "stores_0_awarddetail",
                    723:                                 "stores_0_awarded",
                    724:                                 "timestamp",
                    725:                                 "stores_0_tries",
                    726:                                 "stores_0_award");
1.1       matthew   727:     #
                    728:     # Definitions
                    729:     undef(%nice_parameter_name);
                    730:     my %parameters;   # holds underscored parameters by name
                    731:     #
                    732:     # Get the metadata fields and determine their proper names
1.21      matthew   733:     my %nice_parm_names = $self->get_parm_names(@Mandatory_parameters);
                    734:     while (my($cleaned_name,$display) = each(%nice_parm_names)) {
1.1       matthew   735:         $parameters{$cleaned_name}++;
                    736:         $nice_parameter_name{$cleaned_name} = $display;
                    737:     }
                    738:     #
                    739:     # Get the values of the metadata fields
1.19      matthew   740:     if ($connection->aborted()) { $self->cleanup(); return; }
1.22      matthew   741:     $self->ensure_current_caches();
1.19      matthew   742:     if ($connection->aborted()) { $self->cleanup(); return; }
1.21      matthew   743:     %parameters = $self->get_parameter_values(keys(%parameters));
1.19      matthew   744:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   745:     #
                    746:     # Clean out unnecessary parameters
                    747:     foreach (keys(%parameters)) {
                    748:         delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
                    749:     }
                    750:     #
                    751:     # Get the students performance data
1.22      matthew   752:     my %student_parameters = $self->get_problem_state();
1.1       matthew   753:     while (my ($parm,$value) = each(%student_parameters)) {
                    754:         $parm =~ s/^resource\./stores_/;
                    755:         $parm =~ s/\./_/g;
                    756:         $parameters{$parm} = $value;
1.28      matthew   757:     }
                    758:     #
                    759:     # Clean out any bad parameters
                    760:     $self->determine_parts();
                    761:     foreach my $param (keys(%parameters)) {
                    762:         if (! $self->parameter_part_is_valid($param)) {
                    763:             delete ($parameters{$param});
                    764:         }
1.1       matthew   765:     }
                    766:     #
                    767:     # Set up the formulas and parameter values
                    768:     my %f=$self->formulas();
                    769:     my %c;
                    770:     #
1.5       matthew   771:     # Check for blackout requirements
1.39    ! albertel  772:     if ((!exists($env{'request.role.adv'}) || !$env{'request.role.adv'})) {
1.5       matthew   773:         while (my ($parm,$value) = each(%parameters)) {
                    774:             last if ($self->blackout());
                    775:             next if ($parm !~ /^(parameter_.*)_problemstatus$/);
1.24      matthew   776:             if ($parameters{$1.'_answerdate'} ne '' &&
1.21      matthew   777:                 $parameters{$1.'_answerdate'} < time) {
                    778:                 next;
                    779:             }
1.5       matthew   780:             if (lc($value) eq 'no') {
                    781:                 # We must blackout this sheet
                    782:                 $self->blackout(1);
                    783:             }
                    784:         }
                    785:     }
1.19      matthew   786:     if ($connection->aborted()) { $self->cleanup(); return; }
1.5       matthew   787:     #
                    788:     # Move the parameters into the spreadsheet
1.1       matthew   789:     while (my ($parm,$value) = each(%parameters)) {
                    790:         my $cell = 'A'.$self->get_row_number_from_key($parm);
                    791:         $f{$cell} = $parm;
1.31      matthew   792:         if ($parm =~ /_submission$/ && $value =~ /(\{|\})/) {
                    793:             $value = 'witheld';
                    794:         }
1.32      matthew   795:         $value = 'q{'.$value.'}' if ($value =~/([^\d\.]|\.\.)/);
1.6       matthew   796:         $c{$parm} = $value;
1.1       matthew   797:     }
1.6       matthew   798:     $self->formulas(\%f);
                    799:     $self->constants(\%c);
1.19      matthew   800:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   801:     $self->calcsheet();
                    802:     #
                    803:     # Store export row in cache
                    804:     my @exportarray = $self->exportrow();
1.21      matthew   805:     $self->deal_with_export_row(@exportarray);
1.12      matthew   806:     $self->save() if ($self->need_to_save());
1.19      matthew   807:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   808:     return;
                    809: }
                    810: 
                    811: ##
                    812: ## sett overrides Spreadsheet::sett
                    813: ##
                    814: sub sett {
                    815:     my $self = shift;
                    816:     my %t=();
                    817:     #
                    818:     # Deal with the template row by copying the template formulas into each
                    819:     # row.
                    820:     foreach my $col ($self->template_cells()) {
                    821:         next if ($col=~/^A/);
                    822:         foreach my $row ($self->rows()) {
                    823:             # Get the name of this cell
                    824:             my $cell=$col.$row;
                    825:             # Grab the template declaration
                    826:             $t{$cell}=$self->formula('template_'.$col);
                    827:             # Replace '#' with the row number
                    828:             $t{$cell}=~s/\#/$row/g;
                    829:             # Replace '....' with ','
                    830:             $t{$cell}=~s/\.\.+/\,/g;
                    831:             # Replace 'A0' with the value from 'A0'
                    832:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    833:             # Replace parameters
                    834:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    835:         }
                    836:     }
                    837:     #
                    838:     # Deal with the cells which have formulas
                    839:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    840: 	next if ($cell =~ /template_/);
                    841:         if ($cell =~ /^A/ && $cell ne 'A0') {
                    842:             if ($formula !~ /^\!/) {
                    843:                 $t{$cell}=$self->{'constants'}->{$formula};
                    844:             }
                    845:         } else {
                    846:             $t{$cell}=$formula;
                    847:             $t{$cell}=~s/\.\.+/\,/g;
                    848:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    849:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    850:         }
                    851:     }
                    852:     # Put %t into the safe space
                    853:     %{$self->{'safe'}->varglob('t')}=%t;
                    854: }
                    855: 
                    856: 
                    857: ########################################################
                    858: ########################################################
                    859: 
                    860: =pod
                    861: 
                    862: =item &load_cached_export_rows()
                    863: 
                    864: Retrieves and parsers the export rows of the assessment spreadsheets.
                    865: These rows are saved in the students directory in the format:
                    866: 
                    867:  sname:sdom:assesscalc:symb.time => time
                    868: 
                    869:  sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
                    870: 
                    871: =cut
                    872: 
                    873: ########################################################
                    874: ########################################################
                    875: sub load_cached_export_rows {
1.18      matthew   876:     undef(%Exportrows);
1.1       matthew   877:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
1.39    ! albertel  878:                                     $env{'request.course.id'},
1.1       matthew   879:                                     $current_domain,$current_name,undef);
                    880:     if ($tmp[0]!~/^error/) {
                    881:         my %tmp = @tmp;
1.39    ! albertel  882:         my $default_filename =  $env{'course.'.$env{'request.course.id'}.
1.1       matthew   883:                                          '.spreadsheet_default_assesscalc'};
                    884:         # We only got one key, so we will access it directly.
                    885:         while (my ($key,$sheetdata) = each(%tmp)) {
                    886:             my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
1.33      matthew   887:             if (! defined($sname) || $sname eq '' ||
                    888:                 ! defined($sdom)  || $sdom eq '' ) {
                    889:                 next;
                    890:             }
1.1       matthew   891:             if ($symb =~ /\.time$/) {
                    892:                 $symb =~ s/\.time$//;
                    893:                 $Exportrows{$symb}->{'time'} = $sheetdata;
                    894:             } else {
                    895:                 $sheetdata =~ s/^(.*)___=___//;
                    896:                 my $filename = $1;
                    897:                 $filename = $default_filename if (! defined($filename));
                    898:                 my @Data = split('___;___',$sheetdata);
                    899:                 $Exportrows{$symb}->{$filename} = \@Data;
                    900:             }
                    901:         }
                    902:     }
                    903: }
                    904: 
                    905: #############################################
                    906: #############################################
                    907: 
                    908: =pod
                    909: 
                    910: =item &export_data
                    911: 
                    912: Returns the export data associated with the spreadsheet.  Computes the
                    913: spreadsheet only if necessary.
                    914: 
                    915: =cut
                    916: 
                    917: #############################################
                    918: #############################################
                    919: sub export_data {
                    920:     my $self = shift;
1.19      matthew   921:     my ($r) = @_;
                    922:     my $connection = $r->connection();
1.1       matthew   923:     my $symb = $self->{'symb'};
1.39    ! albertel  924:     if (! exists($env{'request.role.adv'}) || ! $env{'request.role.adv'} ||
1.5       matthew   925:         ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb})  ||
1.1       matthew   926:         ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
                    927:         ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
1.18      matthew   928:         ! defined($Exportrows{$symb}->{$self->{'filename'}}) ||
                    929:         ! ref($Exportrows{$symb}->{$self->{'filename'}}) 
                    930:         ) {
1.19      matthew   931:         $self->compute($r);
1.1       matthew   932:     }
1.19      matthew   933:     if ($connection->aborted()) { $self->cleanup(); return; }
1.29      matthew   934:     my @Data;
                    935:     if ($self->badcalc()) {
                    936:         @Data = ();
                    937:     } else {
                    938:         @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
                    939:         if ($Data[0] =~ /^(.*)___=___/) {
                    940:             $self->{'sheetname'} = $1;
                    941:             $Data[0] =~ s/^(.*)___=___//;
                    942:         }
                    943:         for (my $i=0;$i<$#Data;$i++) {
                    944:             if ($Data[$i]=~/\D/ && defined($Data[$i])) {
                    945:                 $Data[$i]="'".$Data[$i]."'";
                    946:             }
                    947:         }
1.1       matthew   948:     }
                    949:     return @Data;
                    950: }
                    951: 
                    952: #############################################
                    953: #############################################
                    954: 
                    955: =pod
                    956: 
                    957: =item &save_export_data()
                    958: 
                    959: Writes the export data for this spreadsheet to the students cache.
                    960: 
                    961: =cut
                    962: 
                    963: #############################################
                    964: #############################################
                    965: sub save_export_data {
                    966:     my $self = shift;
1.7       matthew   967:     return if ($self->temporary());
1.1       matthew   968:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    969:     my $symb    = $self->{'symb'};
1.29      matthew   970:     if ($self->badcalc()){
                    971:         # do not save data away when calculations have not been done properly.
                    972:         delete($Exportrows{$symb});
                    973:         return;
                    974:     }
1.1       matthew   975:     if (! exists($Exportrows{$symb}) || 
                    976:         ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
                    977:         return;
                    978:     }
                    979:     my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
                    980:     my $timekey = $key.'.time';
1.22      matthew   981:     my $newstore= join('___;___',
                    982:                        map {s/[^[:print:]]//g;$_;} # strip out unprintable
                    983:                                 @{$Exportrows{$symb}->{$self->{'filename'}}});
1.1       matthew   984:     $newstore = $self->{'filename'}.'___=___'.$newstore;
1.22      matthew   985:     $newExportrows{$student}->{$key} = $newstore;
                    986:     $newExportrows{$student}->{$timekey} = $Exportrows{$symb}->{'time'};
1.1       matthew   987:     return;
                    988: }
                    989: 
                    990: 1;
                    991: 
                    992: __END__

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