File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.15: download - view: text, annotated - select for diffs
Fri Jun 20 17:24:04 2003 UTC (21 years ago) by matthew
Branches: MAIN
CVS tags: version_0_99_3, HEAD
Use loncommon::no_cache to plead not to be cached.

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

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