File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.26: download - view: text, annotated - select for diffs
Tue Sep 30 15:40:09 2003 UTC (20 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Clear out data caches when we do any recalculations.

    1: #
    2: # $Id: lonspreadsheet.pm,v 1.26 2003/09/30 15:40:09 matthew Exp $
    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: lonspreadsheet
   34: 
   35: =head1 SYNOPSIS
   36: 
   37: Spreadsheet interface to internal LON-CAPA data
   38: 
   39: =head1 DESCRIPTION
   40: 
   41: Lonspreadsheet provides course coordinators the ability to manage their
   42: students grades online.  The students are able to view their own grades, but
   43: not the grades of their peers.  The spreadsheet is highly customizable,
   44: offering the ability to use Perl code to manipulate data, as well as many
   45: built-in functions.
   46: 
   47: =head2 Functions available to user of lonspreadsheet
   48: 
   49: =over 4
   50: 
   51: =cut
   52: 
   53: 
   54: package Apache::lonspreadsheet;
   55:             
   56: use strict;
   57: use warnings FATAL=>'all';
   58: no warnings 'uninitialized';
   59: use Apache::classcalc();
   60: use Apache::studentcalc();
   61: use Apache::assesscalc();
   62: use Apache::Constants qw(:common :http);
   63: use Apache::lonnet;
   64: use Apache::lonhtmlcommon;
   65: use Apache::lonlocal;
   66: use Apache::loncoursedata();
   67: use HTML::Entities();
   68: 
   69: ##
   70: ## HTML utility subroutines really should go in lonhtmlcommon
   71: ##
   72: 
   73: sub textfield {
   74:     my ($title,$name,$value)=@_;
   75:     return "\n<p><b>$title:</b><br>".
   76:         '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
   77: }
   78: 
   79: sub hiddenfield {
   80:     my ($name,$value)=@_;
   81:     return '<input type=hidden name="'.$name.'" value="'.$value.'" />'."\n";
   82: }
   83: 
   84: sub selectbox {
   85:     my ($title,$name,$value,%options)=@_;
   86:     my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
   87:     foreach (sort keys(%options)) {
   88:         $selout.='<option value="'.$_.'"';
   89:         if ($_ eq $value) { $selout.=' selected'; }
   90:         $selout.='>'.$options{$_}.'</option>';
   91:     }
   92:     return $selout.'</select>';
   93: }
   94: 
   95: sub file_dialogs {
   96:     my ($spreadsheet) = @_;
   97:     my $bgcolor = "#FFFFFF";
   98:     my $sheettype = $spreadsheet->{'type'};
   99:     my $result = '';
  100:     my $message = '';
  101:     ##
  102:     ## Deal with saving the spreadsheet
  103:     if ((exists($ENV{'form.save'}) || exists($ENV{'form.makedefault'})) && 
  104:         exists($ENV{'form.savefilename'})) {
  105:         $spreadsheet->filename($ENV{'form.savefilename'});
  106:         my $save_status = $spreadsheet->save();
  107:         if ($save_status ne 'ok') {
  108:             $message .= "An error occurred while saving the spreadsheet".
  109:                 "There error is:".$save_status;
  110:         } else {
  111:             $message .= "Spreadsheet saved as ".$spreadsheet->filename();
  112:         }
  113:     } elsif (exists($ENV{'form.newformula'}) && 
  114:              exists($ENV{'form.cell'})       && 
  115:              $ENV{'form.cell'} ne '' ) {
  116:         ##
  117:         ## Make any requested modifications to the spreadsheet
  118:         $spreadsheet->modify_cell($ENV{'form.cell'},
  119:                                   $ENV{'form.newformula'});
  120:         $spreadsheet->save_tmp();
  121:         # output that we are dealing with a temporary file
  122:         $result .=&hiddenfield('workcopy',$sheettype);
  123:         if ($ENV{'form.newformula'} !~ /^\s*$/) {
  124:             $message .='<table><tr>'.
  125:               '<td valign="top"><pre>Cell '.$ENV{'form.cell'}.' = </pre></td>'.
  126:               '<td><pre>'.$ENV{'form.newformula'}."</pre></td></tr></table>\n";
  127:         } else {
  128:             $message .= 'Deleted contents of cell '.$ENV{'form.cell'}.'.';
  129:         }
  130:     }
  131:     ##
  132:     ## Editing code
  133:     $result .=&hiddenfield('cell','').
  134:               &hiddenfield('newformula','');
  135:     ##
  136:     ## Create the save and load dialogs
  137:     my $filename = $spreadsheet->filename();
  138:     my $truefilename = $filename;
  139:     if ($spreadsheet->is_default()) {
  140:         $filename = 'Default';
  141:     }
  142:     my $save_dialog = '<nobr>'.
  143:         '<input type="submit" name="save" value="Save as" /> '.
  144:         '<input type="text" name="savefilename" size="30" value="'.
  145:         $truefilename.'" />'.
  146:         '</nobr>';
  147:     my $makedefault_dialog = '<input type="submit" name="makedefault" '.
  148:         'value="Save as & Make This Sheet the Default"/>';
  149:     #
  150:     my $link = '<a href="javascript:openbrowser'.
  151:         "('sheet','loadfilename','spreadsheet')\">Select Spreadsheet File</a>";
  152:     my $load_dialog = <<END;
  153: <table bgcolor="$bgcolor">
  154: <tr><td><input type="submit" name="load" value="Load" /></td>
  155:     <td><nobr>
  156:         <input type="text" name="loadfilename" size="20" value="$filename" />
  157:         $link</nobr>
  158:     </td></tr>
  159: <tr><td>&nbsp;</td><td>
  160:     <select name="fileselect" onchange="document.sheet.loadfilename.value=document.sheet.fileselect.value" >
  161: END
  162:     my $default_filename_set = 0;
  163:     foreach my $sheetfilename ($spreadsheet->othersheets()) {
  164:         $load_dialog .= '    <option name="'.$sheetfilename.'"';
  165:         if ($filename eq $sheetfilename) {
  166:             $load_dialog .= ' selected';
  167:             $default_filename_set = 1;
  168:         }
  169:         $load_dialog .= '>'.$sheetfilename."</option>\n";
  170:     }
  171:     $load_dialog .= "</select>\n</td><td>&nbsp;</td></tr>\n</table>\n";
  172:         #
  173:     $result .=<<END;
  174: <!-- 
  175:     <fieldset title="File Dialogs" >
  176:     <legend>File Dialogs</legend>
  177:   -->
  178: <!-- load / save dialogs -->
  179: <table cellspacing="2">
  180: <tr>
  181:     <td>$load_dialog</td>
  182:     <td>
  183:         <table bgcolor="$bgcolor">
  184:         <tr><td>$save_dialog</td></tr>
  185:         <tr><td align="center">$makedefault_dialog</td></tr>
  186:         </table>
  187:     </td>
  188: </tr>
  189: </table>
  190: <!--
  191:     </fieldset>
  192:   -->
  193: END
  194:     return ($result,$message);
  195: }
  196: 
  197: sub handler {
  198:     my $r=shift;
  199:     #
  200:     # Overload checking
  201:     #
  202:     # Check this server
  203:     my $loaderror=&Apache::lonnet::overloaderror($r);
  204:     if ($loaderror) { return $loaderror; }
  205:     # Check the course homeserver
  206:     $loaderror= &Apache::lonnet::overloaderror($r,
  207:                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
  208: #    if ($loaderror) { return $loaderror; } 
  209:     #
  210:     # HTML Header
  211:     #
  212:     if ($r->header_only) {
  213:         &Apache::loncommon::content_type($r,'text/html');
  214:         $r->send_http_header;
  215:         return OK;
  216:     }
  217:     #
  218:     # Roles Checking
  219:     #
  220:     # Needs to be in a course
  221:     if (! $ENV{'request.course.fn'}) { 
  222:         # Not in a course, or not allowed to modify parms
  223:         $ENV{'user.error.msg'}=
  224:             $r->uri.":opa:0:0:Cannot modify spreadsheet";
  225:         return HTTP_NOT_ACCEPTABLE; 
  226:     }
  227:     my $courseid = $ENV{'request.course.id'};
  228:     #
  229:     # Do not allow students to continue if standard grading is in effect.
  230:     if ($ENV{'request.role'} =~ /^st\./) {
  231:         if ($ENV{'course.'.$courseid.'.grading'} eq 'standard') {
  232:             return HTTP_NOT_ACCEPTABLE;
  233:         }
  234:     }
  235:     #
  236:     # Get query string for limited number of parameters
  237:     #
  238:     &Apache::loncommon::get_unprocessed_cgi
  239:         ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename','recalc']);
  240:     #
  241:     # Deal with restricted student permissions 
  242:     #
  243:     if ($ENV{'request.role'} =~ /^st\./) {
  244:         delete $ENV{'form.cell'}       if (exists($ENV{'form.cell'}));
  245:         delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
  246:     }
  247:     #
  248:     # Determine basic information about the spreadsheet
  249:     my ($sheettype) = ($r->uri=~/\/(\w+)$/);
  250:     #
  251:     my $symb   = undef;
  252:     $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
  253:     my $name   = $ENV{'user.name'};
  254:     my $domain = $ENV{'user.domain'};
  255:     if (exists($ENV{'form.sname'})) {
  256:         $name   = $ENV{'form.sname'};
  257:         $domain = $ENV{'form.sdomain'};
  258:     }
  259:     ##
  260:     ## Check permissions
  261:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
  262:                                                 $ENV{'request.course.id'});
  263:     # Only those instructors/tas/whatevers with complete access
  264:     # (not section restricted) are able to modify spreadsheets.
  265:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
  266:                                                 $ENV{'request.course.id'});
  267:     if (! $allowed_to_view) {
  268:         $allowed_to_view = &Apache::lonnet::allowed('vgr',
  269:                     $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'});
  270:         # Those who are restricted by section are allowed to view.
  271:         # The routines in lonstatistics which decide which students' 
  272:         # will be shown take care of the restriction by section.
  273:     }
  274:     #
  275:     # Only those able to view others grades will be allowed to continue 
  276:     # if they are not requesting their own.
  277:     if ($sheettype eq 'classcalc') {
  278:         if (! $allowed_to_view) {
  279: 	    $ENV{'user.error.msg'}=
  280: 		$r->uri.":vgr:0:0:Access Permission Denied";
  281: 	    return HTTP_NOT_ACCEPTABLE; 
  282: 	}
  283:     }
  284:     if ((($name   ne $ENV{'user.name'} ) ||
  285:          ($domain ne $ENV{'user.domain'})) && $sheettype ne 'classcalc') {
  286:         # Check that the student is in their section?
  287:         if (exists($ENV{'request.course.sec'}) && 
  288:             $ENV{'request.course.sec'} ne '' ) {
  289:             my $stu_sec = &Apache::lonnet::usection($domain,$name,
  290:                                                     $ENV{'request.course.id'});
  291:             if ($stu_sec ne $ENV{'request.course.sec'}) {
  292: 		$ENV{'user.error.msg'}=
  293: 		    $r->uri.":vgr:0:0:Requested student not in your section.";
  294: 		return HTTP_NOT_ACCEPTABLE; 
  295:             }
  296:         }
  297:     }
  298: 
  299:     #
  300:     # Open page, try to prevent browser cache.
  301:     #
  302:     &Apache::loncommon::content_type($r,'text/html');
  303:     &Apache::loncommon::no_cache($r);
  304:     $r->send_http_header;
  305: 
  306:     #
  307:     # Header....
  308:     #
  309:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
  310:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
  311:     ##
  312:     ## Spit out the javascript required for editing
  313:     ##
  314:     if ($allowed_to_edit) {
  315:         my $extra_javascript = 
  316:             &Apache::loncommon::browser_and_searcher_javascript();
  317:         $r->print(<<ENDSCRIPT);
  318: <script language="JavaScript">
  319: 
  320:     $extra_javascript
  321: 
  322:     var editwin;
  323: 
  324:     function celledit(cellname,cellformula) {
  325:         var edit_text = '';
  326:         // cellformula may contain less-than and greater-than symbols, so
  327:         // we need to escape them?  
  328:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
  329:         edit_text += '<form name="editwinform">';
  330:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
  331:         edit_text += '<textarea name="newformula" cols="60" rows="12"';
  332:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
  333:         edit_text += '</br>';
  334:         edit_text += '<input type="button" name="accept" value="Accept"';
  335:         edit_text += ' onClick=\\\'javascript:';
  336:         edit_text += 'opener.document.sheet.cell.value=';
  337:         edit_text +=     '"'+cellname+'";';
  338:         edit_text += 'opener.document.sheet.newformula.value=';
  339:         edit_text +=     'document.editwinform.newformula.value;';
  340:         edit_text += 'opener.document.sheet.submit();';
  341:         edit_text += 'self.close()\\\' />';
  342:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  343:         edit_text += '<input type="button" name="abort" ';
  344:         edit_text +=     'value="Discard Changes"';
  345:         edit_text += ' onClick="javascript:self.close()" />';
  346:         edit_text += '</center></body></html>';
  347: 
  348:         if (editwin != null && !(editwin.closed) ) {
  349:             editwin.close();
  350:         }
  351: 
  352:         editwin = window.open($nothing,'CellEditWin','height=280,width=480,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
  353:         editwin.document.write(edit_text);
  354:     }
  355: </script>
  356: ENDSCRIPT
  357:     }
  358:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
  359:               '<form action="'.$r->uri.'" name="sheet" method="post">');
  360:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
  361:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
  362:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
  363:     $r->rflush();
  364:     ##
  365:     ## Determine the filename to use
  366:     my $filename = undef;
  367:     if ($allowed_to_edit) {
  368:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
  369:         #
  370:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
  371:             $filename = $ENV{'form.loadfilename'};
  372:             $ENV{'form.workcopy'} = 'no';
  373:         }
  374:     }
  375:     ##
  376:     ## Take care of "backdoor" spreadsheet expiration / recalc stuff
  377:     if ($allowed_to_edit && exists($ENV{'form.recalc'})) {
  378:         if (exists($ENV{'form.recalc'})) {
  379:             &Apache::loncoursedata::delete_caches($ENV{'requres.course.id'});
  380:         }
  381:         if ($ENV{'form.recalc'} eq 'ilovewastingtime') {
  382:             &Apache::lonnet::logthis('ilovewastingtime');
  383:             # expire ALL spreadsheets
  384:             &Apache::lonnet::expirespread('','','studentcalc');
  385:             &Apache::lonnet::expirespread('','','assesscalc');
  386:         } elsif ($ENV{'form.recalc'} =~ /^symb:/) {
  387:             # expire for all students on this symb
  388:             my ($symb) = ($ENV{'form.recalc'} =~ /^symb:(.*)$/);
  389:             &Apache::lonnet::logthis('symb = '.$symb);
  390:             &Apache::lonnet::expirespread('','','assesscalc',$symb);
  391:             &Apache::lonnet::expirespread('','','studentcalc');
  392:         } elsif ($ENV{'form.recalc'} =~ /^student:/) {
  393:             # expire all assessment spreadsheets for this user
  394:             my ($sname,$sdom) = ($ENV{'form.recalc'}=~/^student:(.*):(.*)$/);
  395:             &Apache::lonnet::logthis('student = '.$sname.':'.$sdom);
  396:             if (defined($sname) && defined($sdom)) {
  397:                 &Apache::lonnet::expirespread($sname,$sdom,'assesscalc');
  398:                 &Apache::lonnet::expirespread($sname,$sdom,'studentcalc');
  399:             }
  400:         }
  401:     }
  402:     ##
  403:     ## Make the spreadsheet
  404:     &Apache::Spreadsheet::initialize_spreadsheet_package();
  405:     my $spreadsheet = undef;
  406:     if ($sheettype eq 'classcalc') {
  407:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
  408:     } elsif ($sheettype eq 'studentcalc') {
  409:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
  410:     } elsif ($sheettype eq 'assesscalc' && 
  411:              defined($symb) && 
  412:              $allowed_to_edit) {
  413:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
  414:     } else {
  415:         return HTTP_NOT_ACCEPTABLE;
  416:     }
  417:     if (! defined($spreadsheet)) {
  418:         # error error - run in circles, scream and shout
  419:         return;
  420:     }
  421:     $spreadsheet->initialize();
  422:     #
  423:     # Output selector
  424:     ##
  425:     ## Editing/loading/saving
  426:     if ($allowed_to_edit) {
  427:         my ($html,$action_message) = &file_dialogs($spreadsheet);
  428:         if ($ENV{'form.makedefault'}) {
  429:             $spreadsheet->make_default();
  430:             if ($action_message) {
  431:                 $action_message .= '<br />';
  432:             }
  433:             $action_message .= 'Made this spreadsheet the default';
  434:             if ($sheettype eq 'classcalc') {
  435:                 $action_message .= ' for the course';
  436:             } elsif ($sheettype eq 'studentcalc') {
  437:                 $action_message .= ' for all students';
  438:             } elsif ($sheettype eq 'assesscalc') {
  439:                 $action_message .= ' for all assessments';
  440:             }
  441:             $action_message .= '.';
  442:         }
  443:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
  444:                   '<td valign="bottom">'.$html."</td></tr></table>\n");
  445:         if ($action_message ne '') {
  446:             $r->print(<<END);
  447: <table>
  448: <tr><td valign="top"><b>Last Action:</b></td>
  449:     <td>&nbsp;</td>
  450:     <td>$action_message</td>
  451: </tr>
  452: </table>
  453: END
  454:         }
  455:         $r->rflush();
  456:     } else {
  457:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
  458:                   "</td></tr></table>\n");
  459:     }
  460:     $r->rflush();
  461:     #
  462:     $r->print("<table><tr>");
  463:     if ($sheettype eq 'classcalc') {
  464:         $r->print('<td><input type="submit" value="'.
  465: 		  &mt('Generate Spreadsheet').'" />'.
  466:                   '</td>');
  467:     }
  468:     if ($allowed_to_view) {
  469:         $r->print('<td>'.
  470:                   &Apache::loncommon::help_open_topic("Spreadsheet_About",
  471:                                                       'Spreadsheet Help').
  472:                   '</td>');
  473:     }
  474:     if ($allowed_to_edit) {
  475:         $r->print('<td>'.
  476:                   &Apache::loncommon::help_open_topic("Spreadsheet_Editing",
  477:                                                       'Editing Help').
  478:                   '</td>');
  479:     }
  480:     $r->print('</tr></table>');
  481:     #
  482:     # Keep track of the filename
  483:     $r->print(&hiddenfield('filename',$filename));
  484:     #
  485:     # Keep track of the number of times we have been called, sort of.
  486:     $r->print(&hiddenfield('not_first_run','whatever'));
  487:     #
  488:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
  489:         $r->print($spreadsheet->get_html_title());
  490:         if ($allowed_to_view || $allowed_to_edit) {
  491:             $r->print($spreadsheet->parent_link());
  492:         }
  493:         $r->rflush();
  494:         $spreadsheet->display($r);
  495:     }
  496:     $r->print('</form></body></html>');
  497:     return OK;
  498: }
  499: 
  500: 1;
  501: 
  502: __END__
  503: 

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