File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.34: download - view: text, annotated - select for diffs
Tue Feb 24 20:47:14 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
These fixes should go on S10 as soon as possible.

Spreadsheet.pm: Fix to handle the case when a spreadsheet object is
created without a valid username/domain passed to new().

assesscalc.pm: Fix to ignore stored data which does not contain the username
and domain.

lonspreadsheet.pm: Fix to not do stupid things that got us in this mess
in the first place (only accept form parameters which have values and do not
send out form parameters without values).

    1: #
    2: # $Id: lonspreadsheet.pm,v 1.34 2004/02/24 20:47:14 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.='>'.&mt($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>'.&mt('Cell').' '.$ENV{'form.cell'}.' = </pre></td>'.
  126:               '<td><pre>'.$ENV{'form.newformula'}."</pre></td></tr></table>\n";
  127:         } else {
  128:             $message .= &mt('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="'.&mt('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="'.&mt('Save as & Make This Sheet the Default').'"/>';
  149:     #
  150:     my $link = '<a href="javascript:openbrowser'.
  151:         "('sheet','loadfilename','spreadsheet')\">".&mt('Select Spreadsheet File')."</a>";
  152:     my $load=&mt('Load:');
  153:     my $load_dialog = <<END;
  154: <table bgcolor="$bgcolor">
  155: <tr><td><input type="submit" name="load" value="$load" /></td>
  156:     <td><nobr>
  157:         <input type="text" name="loadfilename" size="20" value="$filename" />
  158:         $link</nobr>
  159:     </td></tr>
  160: <tr><td>&nbsp;</td><td>
  161:     <select name="fileselect" onchange="document.sheet.loadfilename.value=document.sheet.fileselect.value" >
  162: END
  163:     my $default_filename_set = 0;
  164:     foreach my $sheetfilename ($spreadsheet->othersheets()) {
  165:         $load_dialog .= '    <option value="'.$sheetfilename.'"';
  166:         if ($filename eq $sheetfilename) {
  167:             $load_dialog .= ' selected';
  168:             $default_filename_set = 1;
  169:         }
  170:         $load_dialog .= '>'.$sheetfilename."</option>\n";
  171:     }
  172:     $load_dialog .= "</select>\n</td><td>&nbsp;</td></tr>\n</table>\n";
  173:         #
  174:     $result .=<<END;
  175: <!-- 
  176:     <fieldset title="File Dialogs" >
  177:     <legend>File Dialogs</legend>
  178:   -->
  179: <!-- load / save dialogs -->
  180: <table cellspacing="2">
  181: <tr>
  182:     <td>$load_dialog</td>
  183:     <td>
  184:         <table bgcolor="$bgcolor">
  185:         <tr><td>$save_dialog</td></tr>
  186:         <tr><td align="center">$makedefault_dialog</td></tr>
  187:         </table>
  188:     </td>
  189: </tr>
  190: </table>
  191: <!--
  192:     </fieldset>
  193:   -->
  194: END
  195:     return ($result,$message);
  196: }
  197: 
  198: sub handler {
  199:     my $r=shift;
  200:     #
  201:     # Overload checking
  202:     #
  203:     # Check this server
  204:     my $loaderror=&Apache::lonnet::overloaderror($r);
  205:     if ($loaderror) { return $loaderror; }
  206:     # Check the course homeserver
  207:     $loaderror= &Apache::lonnet::overloaderror($r,
  208:                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
  209: #    if ($loaderror) { return $loaderror; } 
  210:     #
  211:     # HTML Header
  212:     #
  213:     if ($r->header_only) {
  214:         &Apache::loncommon::content_type($r,'text/html');
  215:         $r->send_http_header;
  216:         return OK;
  217:     }
  218:     #
  219:     # Roles Checking
  220:     #
  221:     # Needs to be in a course
  222:     if (! $ENV{'request.course.fn'}) { 
  223:         # Not in a course, or not allowed to modify parms
  224:         $ENV{'user.error.msg'}=
  225:             $r->uri.":opa:0:0:Cannot modify spreadsheet";
  226:         return HTTP_NOT_ACCEPTABLE; 
  227:     }
  228:     my $courseid = $ENV{'request.course.id'};
  229:     #
  230:     # Do not allow students to continue if standard or external grading is in
  231:     # effect.
  232:     #
  233:     if ($ENV{'request.role'} =~ /^st\./) {
  234:         if ($ENV{'course.'.$courseid.'.grading'} eq 'standard' ||
  235: 	    $ENV{'course.'.$courseid.'.grading'} eq 'external' ) {
  236:             return HTTP_NOT_ACCEPTABLE;
  237:         }
  238:     }
  239:     #
  240:     # Get query string for limited number of parameters
  241:     #
  242:     &Apache::loncommon::get_unprocessed_cgi
  243:         ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename','recalc']);
  244:     #
  245:     # Deal with restricted student permissions 
  246:     #
  247:     if ($ENV{'request.role'} =~ /^st\./) {
  248:         delete $ENV{'form.cell'}       if (exists($ENV{'form.cell'}));
  249:         delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
  250:     }
  251:     #
  252:     # Determine basic information about the spreadsheet
  253:     my ($sheettype) = ($r->uri=~/\/(\w+)$/);
  254:     #
  255:     my $symb   = undef;
  256:     $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
  257:     my $name   = $ENV{'user.name'};
  258:     my $domain = $ENV{'user.domain'};
  259:     if (exists($ENV{'form.sname'}) && $ENV{'form.sname'} ne '') {
  260:         $name   = $ENV{'form.sname'};
  261:         $domain = $ENV{'form.sdomain'};
  262:     }
  263:     $ENV{'form.sname'} = $name;
  264:     $ENV{'form.sdomain'} = $domain;
  265:     #
  266:     &Apache::lonnet::logthis('name   = "'.$name.'"');
  267:     &Apache::lonnet::logthis('domain = "'.$domain.'"');
  268:     ##
  269:     ## Check permissions
  270:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
  271:                                                 $ENV{'request.course.id'});
  272:     # Only those instructors/tas/whatevers with complete access
  273:     # (not section restricted) are able to modify spreadsheets.
  274:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
  275:                                                 $ENV{'request.course.id'});
  276:     if (! $allowed_to_view) {
  277:         $allowed_to_view = &Apache::lonnet::allowed('vgr',
  278:                     $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'});
  279:         # Those who are restricted by section are allowed to view.
  280:         # The routines in lonstatistics which decide which students' 
  281:         # will be shown take care of the restriction by section.
  282:     }
  283:     #
  284:     # Only those able to view others grades will be allowed to continue 
  285:     # if they are not requesting their own.
  286:     if ($sheettype eq 'classcalc') {
  287:         if (! $allowed_to_view) {
  288: 	    $ENV{'user.error.msg'}=
  289: 		$r->uri.":vgr:0:0:Access Permission Denied";
  290: 	    return HTTP_NOT_ACCEPTABLE; 
  291: 	}
  292:     }
  293:     if ((($name   ne $ENV{'user.name'} ) ||
  294:          ($domain ne $ENV{'user.domain'})) && $sheettype ne 'classcalc') {
  295:         # Check that the student is in their section?
  296:         if (exists($ENV{'request.course.sec'}) && 
  297:             $ENV{'request.course.sec'} ne '' ) {
  298:             my $stu_sec = &Apache::lonnet::usection($domain,$name,
  299:                                                     $ENV{'request.course.id'});
  300:             if ($stu_sec ne $ENV{'request.course.sec'}) {
  301: 		$ENV{'user.error.msg'}=
  302: 		    $r->uri.":vgr:0:0:Requested student not in your section.";
  303: 		return HTTP_NOT_ACCEPTABLE; 
  304:             }
  305:         }
  306:     }
  307: 
  308:     #
  309:     # Open page, try to prevent browser cache.
  310:     #
  311:     &Apache::loncommon::content_type($r,'text/html');
  312:     &Apache::loncommon::no_cache($r);
  313:     $r->send_http_header;
  314: 
  315:     #
  316:     # Header....
  317:     #
  318:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
  319:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
  320:     ##
  321:     ## Spit out the javascript required for editing
  322:     ##
  323:     if ($allowed_to_edit) {
  324: 	my %lt=(
  325: 		'ce' => 'Cell',
  326: 		'ac' => 'Accept',
  327: 		'dc' => 'Discard Changes'
  328: 	);
  329:         my $extra_javascript = 
  330:             &Apache::loncommon::browser_and_searcher_javascript();
  331:         $r->print(<<ENDSCRIPT);
  332: <script type="text/javascript">
  333: //<!--
  334:     $extra_javascript
  335: 
  336:     var editwin;
  337: 
  338:     function celledit(cellname,cellformula) {
  339:         var edit_text = '';
  340:         // cellformula may contain less-than and greater-than symbols, so
  341:         // we need to escape them?  
  342:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
  343:         edit_text += '<form name="editwinform">';
  344:         edit_text += '<center><h3>$lt{'ce'} '+cellname+'</h3>';
  345:         edit_text += '<textarea name="newformula" cols="60" rows="12"';
  346:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
  347:         edit_text += '</br>';
  348:         edit_text += '<input type="button" name="accept" value="$lt{'ac'}"';
  349:         edit_text += ' onClick=\\\'javascript:';
  350:         edit_text += 'opener.document.sheet.cell.value=';
  351:         edit_text +=     '"'+cellname+'";';
  352:         edit_text += 'opener.document.sheet.newformula.value=';
  353:         edit_text +=     'document.editwinform.newformula.value;';
  354:         edit_text += 'opener.document.sheet.submit();';
  355:         edit_text += 'self.close()\\\' />';
  356:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  357:         edit_text += '<input type="button" name="abort" ';
  358:         edit_text +=     'value="$lt{'dc'}"';
  359:         edit_text += ' onClick="javascript:self.close()" />';
  360:         edit_text += '</center></body></html>';
  361: 
  362:         if (editwin != null && !(editwin.closed) ) {
  363:             editwin.close();
  364:         }
  365: 
  366:         editwin = window.open($nothing,'CellEditWin','height=280,width=480,scrollbars=no,resizable=yes,alwaysRaised=yes,dependent=yes',true);
  367:         editwin.document.write(edit_text);
  368:     }
  369: //-->
  370: </script>
  371: ENDSCRIPT
  372:     }
  373:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  374:     &Apache::lonhtmlcommon::add_breadcrumb
  375:         ({href  => $r->uri,
  376:           title => &mt('Spreadsheet'),
  377:           text  => &mt('Spreadsheet'),
  378:           faq   => 134,
  379:           bug   => 'Spreadsheet'});
  380:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
  381:               &Apache::lonhtmlcommon::breadcrumbs(undef,&mt('Spreadsheet')).
  382:               '<form action="'.$r->uri.'" name="sheet" method="post">');
  383:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
  384:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
  385:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
  386:     $r->rflush();
  387:     ##
  388:     ## Determine the filename to use
  389:     my $filename = undef;
  390:     if ($allowed_to_edit) {
  391:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
  392:         #
  393:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
  394:             $filename = $ENV{'form.loadfilename'};
  395:             $ENV{'form.workcopy'} = 'no';
  396:         }
  397:     }
  398:     ##
  399:     ## Take care of "backdoor" spreadsheet expiration / recalc stuff
  400:     if ($allowed_to_edit && exists($ENV{'form.recalc'})) {
  401:         if (exists($ENV{'form.recalc'})) {
  402:             &Apache::loncoursedata::delete_caches($ENV{'requres.course.id'});
  403:         }
  404:         if ($ENV{'form.recalc'} eq 'ilovewastingtime') {
  405:             &Apache::lonnet::logthis('ilovewastingtime');
  406:             # expire ALL spreadsheets
  407:             &Apache::lonnet::expirespread('','','studentcalc');
  408:             &Apache::lonnet::expirespread('','','assesscalc');
  409:         } elsif ($ENV{'form.recalc'} =~ /^symb:/) {
  410:             # expire for all students on this symb
  411:             my ($symb) = ($ENV{'form.recalc'} =~ /^symb:(.*)$/);
  412:             &Apache::lonnet::logthis('symb = '.$symb);
  413:             &Apache::lonnet::expirespread('','','assesscalc',$symb);
  414:             &Apache::lonnet::expirespread('','','studentcalc');
  415:         } elsif ($ENV{'form.recalc'} =~ /^student:/) {
  416:             # expire all assessment spreadsheets for this user
  417:             my ($sname,$sdom) = ($ENV{'form.recalc'}=~/^student:(.*):(.*)$/);
  418:             &Apache::lonnet::logthis('student = '.$sname.':'.$sdom);
  419:             if (defined($sname) && defined($sdom)) {
  420:                 &Apache::lonnet::expirespread($sname,$sdom,'assesscalc');
  421:                 &Apache::lonnet::expirespread($sname,$sdom,'studentcalc');
  422:             }
  423:         }
  424:     }
  425:     ##
  426:     ## Make the spreadsheet
  427:     &Apache::Spreadsheet::initialize_spreadsheet_package();
  428:     my $spreadsheet = undef;
  429:     if ($sheettype eq 'classcalc') {
  430:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
  431:     } elsif ($sheettype eq 'studentcalc') {
  432:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
  433:     } elsif ($sheettype eq 'assesscalc' && 
  434:              defined($symb) && 
  435:              $allowed_to_edit) {
  436:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
  437:     } else {
  438:         return HTTP_NOT_ACCEPTABLE;
  439:     }
  440:     if (! defined($spreadsheet)) {
  441:         # error error - run in circles, scream and shout
  442:         return;
  443:     }
  444:     $spreadsheet->initialize();
  445:     #
  446:     # Output selector
  447:     ##
  448:     ## Editing/loading/saving
  449:     if ($allowed_to_edit) {
  450:         my ($html,$action_message) = &file_dialogs($spreadsheet);
  451:         if ($ENV{'form.makedefault'}) {
  452:             $spreadsheet->make_default();
  453:             if ($action_message) {
  454:                 $action_message .= '<br />';
  455:             }
  456:             $action_message .= &mt('Made this spreadsheet the default');
  457:             if ($sheettype eq 'classcalc') {
  458:                 $action_message .= ' '.&mt('for the course');
  459:             } elsif ($sheettype eq 'studentcalc') {
  460:                 $action_message .= ' '.&mt('for all students');
  461:             } elsif ($sheettype eq 'assesscalc') {
  462:                 $action_message .= ' '.&mt('for all assessments');
  463:             }
  464:             $action_message .= '.';
  465:         }
  466:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
  467:                   '<td valign="bottom">'.$html."</td></tr></table>\n");
  468:         if ($action_message ne '') {
  469:             $r->print(<<END);
  470: <table>
  471: <tr><td valign="top"><b>Last Action:</b></td>
  472:     <td>&nbsp;</td>
  473:     <td>$action_message</td>
  474: </tr>
  475: </table>
  476: END
  477:         }
  478:         $r->rflush();
  479:     } else {
  480:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
  481:                   "</td></tr></table>\n");
  482:     }
  483:     $r->rflush();
  484:     #
  485:     $r->print("<table><tr>");
  486:     $r->print('<td><input type="submit" value="'.
  487:               &mt('Generate Spreadsheet').'" />'.
  488:               '</td>');
  489:     if ($allowed_to_view) {
  490:         $r->print('<td>'.
  491:                   &Apache::loncommon::help_open_topic("Spreadsheet_About",
  492:                                                       'Spreadsheet Help').
  493:                   '</td>');
  494:     }
  495:     if ($allowed_to_edit) {
  496:         $r->print('<td>'.
  497:                   &Apache::loncommon::help_open_topic("Spreadsheet_Editing",
  498:                                                       'Editing Help').
  499:                   '</td>');
  500:     }
  501:     $r->print('</tr></table>');
  502:     #
  503:     # Keep track of the filename
  504:     $r->print(&hiddenfield('filename',$filename));
  505:     #
  506:     # Keep track of the number of times we have been called, sort of.
  507:     $r->print(&hiddenfield('not_first_run','whatever'));
  508:     #
  509:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
  510:         $r->print($spreadsheet->get_html_title());
  511:         if ($allowed_to_view || $allowed_to_edit) {
  512:             $r->print($spreadsheet->parent_link());
  513:         }
  514:         $r->rflush();
  515:         $spreadsheet->display($r);
  516:     }
  517:     $r->print('</form></body></html>');
  518:     return OK;
  519: }
  520: 
  521: 1;
  522: 
  523: __END__
  524: 

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