File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.6: download - view: text, annotated - select for diffs
Tue May 27 20:05:46 2003 UTC (21 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removing lonmenu call.

    1: #
    2: # $Id: lonspreadsheet.pm,v 1.6 2003/05/27 20:05:46 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:     ##
   97:     ## Deal with saving the spreadsheet
   98:     if (exists($ENV{'form.save'}) && 
   99:         exists($ENV{'form.savefilename'})) {
  100:         $spreadsheet->filename($ENV{'form.savefilename'});
  101:         my $save_status = $spreadsheet->save();
  102:         if ($save_status ne 'ok') {
  103:             $result .= "An error occurred while saving the spreadsheet".
  104:                 "There error is:".$save_status;
  105:             return $result;
  106:         } else {
  107:             $result .= "Spreadsheet saved as ".$ENV{'form.savefilename'};
  108:         }
  109:     } elsif (exists($ENV{'form.newformula'}) && 
  110:              exists($ENV{'form.cell'})       && 
  111:              $ENV{'form.cell'} ne '' ) {
  112:         ##
  113:         ## Make any requested modifications to the spreadsheet
  114:         $spreadsheet->modify_cell($ENV{'form.cell'},
  115:                                   $ENV{'form.newformula'});
  116:         $spreadsheet->save_tmp();
  117:         # output that we are dealing with a temporary file
  118:         $result .=&hiddenfield('workcopy',$sheettype);
  119:         $result .='<pre>'.$ENV{'form.cell'}.' = '.
  120:                   $ENV{'form.newformula'}."</pre>\n";
  121:     }
  122:     ##
  123:     ## Editing code
  124:     $result .=&hiddenfield('cell','').
  125:               &hiddenfield('newformula','');
  126:     ##
  127:     ## Create the save and load dialogs
  128:     my $filename = $spreadsheet->filename();
  129:     $filename = '' if ($filename =~ /^default\.$sheettype/i);
  130:     $filename =~ s/_$sheettype$//;
  131:     my $save_dialog = '<nobr>'.
  132:         '<input type="submit" name="save" value="Save as" /> '.
  133:         '<input type="text" name="savefilename" size="30" value="'.
  134:         $filename.'" />'.
  135:         '</nobr>';
  136:     my $makedefault_dialog = '<input type="submit" name="makedefault" '.
  137:         'value="Make This Sheet the Default"/>';
  138:     #
  139:     my $link = '<a href="javascript:openbrowser'.
  140:         "('sheet','loadfilename','spreadsheet')\">Browse</a>";
  141:     my $load_dialog = <<END;
  142: <table bgcolor="$bgcolor">
  143: <tr><td><input type="submit" name="load" value="Load" /></td>
  144:     <td><nobr>
  145:         <input type="text" name="loadfilename" size="25" value="$filename" />
  146:         $link</nobr>
  147:     </td></tr>
  148: <tr><td>&nbsp;</td><td>
  149:     <select name="fileselect" onchange="document.sheet.loadfilename.value=document.sheet.fileselect.value" >
  150:     <option name="Default">Default</option>
  151: END
  152:     foreach my $sheetfilename ($spreadsheet->othersheets()) {
  153:         $sheetfilename =~ s/_$sheettype$//;
  154:         $load_dialog .= '    <option name="'.$sheetfilename.'"';
  155:         if ($filename eq $sheetfilename) {
  156:             $load_dialog .= ' selected';
  157:         }
  158:         $load_dialog .= '>'.$sheetfilename."</option>\n";
  159:     }
  160:     $load_dialog .= "</td><td>&nbsp;</td></tr>\n</table>\n";
  161:         #
  162:     $result .=<<END;
  163: <!-- load / save dialogs -->
  164: <table cellspacing="2">
  165: <tr>
  166:     <td>$load_dialog</td>
  167:     <td>
  168:         <table bgcolor="$bgcolor">
  169:         <tr><td>$save_dialog</td></tr>
  170:         <tr><td align="center">$makedefault_dialog</td></tr>
  171:         </table>
  172:     </td>
  173: </tr>
  174: </table>
  175: END
  176:     return $result;
  177: }
  178: 
  179: sub handler {
  180:     my $r=shift;
  181:     #
  182:     # Overload checking
  183:     #
  184:     # Check this server
  185:     my $loaderror=&Apache::lonnet::overloaderror($r);
  186:     if ($loaderror) { return $loaderror; }
  187:     # Check the course homeserver
  188:     $loaderror= &Apache::lonnet::overloaderror($r,
  189:                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
  190: #    if ($loaderror) { return $loaderror; } 
  191:     #
  192:     # HTML Header
  193:     #
  194:     if ($r->header_only) {
  195:         $r->content_type('text/html');
  196:         $r->send_http_header;
  197:         return OK;
  198:     }
  199:     #
  200:     # Roles Checking
  201:     #
  202:     # Needs to be in a course
  203:     if (! $ENV{'request.course.fn'}) { 
  204:         # Not in a course, or not allowed to modify parms
  205:         $ENV{'user.error.msg'}=
  206:             $r->uri.":opa:0:0:Cannot modify spreadsheet";
  207:         return HTTP_NOT_ACCEPTABLE; 
  208:     }
  209:     #
  210:     # Get query string for limited number of parameters
  211:     #
  212:     &Apache::loncommon::get_unprocessed_cgi
  213:         ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename']);
  214:     #
  215:     # Deal with restricted student permissions 
  216:     #
  217:     if ($ENV{'request.role'} =~ /^st\./) {
  218:         delete $ENV{'form.cell'}       if (exists($ENV{'form.cell'}));
  219:         delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
  220:     }
  221:     #
  222:     # Determine basic information about the spreadsheet
  223:     my ($sheettype) = ($r->uri=~/\/(\w+)$/);
  224:     #
  225:     my $symb   = undef;
  226:     $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
  227:     my $name   = $ENV{'user.name'};
  228:     my $domain = $ENV{'user.domain'};
  229:     if (exists($ENV{'form.sname'})) {
  230:         $name   = $ENV{'form.sname'};
  231:         $domain = $ENV{'form.sdomain'};
  232:     }
  233:     #
  234:     # Open page, try to prevent browser cache.
  235:     #
  236:     $r->content_type('text/html');
  237:     $r->header_out('Cache-control','no-cache');
  238:     $r->header_out('Pragma','no-cache');
  239:     $r->send_http_header;
  240:     ##
  241:     ## Check permissions
  242:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
  243:                                                 $ENV{'request.course.id'});
  244:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
  245:                                                 $ENV{'request.course.id'});
  246: 
  247:     #
  248:     # Only those able to view others grades will be allowed to continue 
  249:     # if they are not requesting their own.
  250:     if (($sheettype eq 'classcalc') || 
  251:         ($name   ne $ENV{'user.name'} ) ||
  252:         ($domain ne $ENV{'user.domain'})) {
  253:         if (! $allowed_to_view) {
  254:             $r->print('<h1>Access Permission Denied</h1>'.
  255:                       '</form></body></html>');
  256:             return OK;
  257:         }
  258:     }
  259:     #
  260:     # Header....
  261:     #
  262:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
  263:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
  264:     ##
  265:     ## Spit out the javascript required for editing
  266:     ##
  267:     if ($allowed_to_edit) {
  268:         my $extra_javascript = 
  269:             &Apache::loncommon::browser_and_searcher_javascript();
  270:         $r->print(<<ENDSCRIPT);
  271: <script language="JavaScript">
  272: 
  273:     $extra_javascript
  274: 
  275:     var editwin;
  276: 
  277:     function celledit(cellname,cellformula) {
  278:         var edit_text = '';
  279:         // cellformula may contain less-than and greater-than symbols, so
  280:         // we need to escape them?  
  281:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
  282:         edit_text += '<form name="editwinform">';
  283:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
  284:         edit_text += '<textarea name="newformula" cols="40" rows="6"';
  285:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
  286:         edit_text += '</br>';
  287:         edit_text += '<input type="button" name="accept" value="Accept"';
  288:         edit_text += ' onClick=\\\'javascript:';
  289:         edit_text += 'opener.document.sheet.cell.value=';
  290:         edit_text +=     '"'+cellname+'";';
  291:         edit_text += 'opener.document.sheet.newformula.value=';
  292:         edit_text +=     'document.editwinform.newformula.value;';
  293:         edit_text += 'opener.document.sheet.submit();';
  294:         edit_text += 'self.close()\\\' />';
  295:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  296:         edit_text += '<input type="button" name="abort" ';
  297:         edit_text +=     'value="Discard Changes"';
  298:         edit_text += ' onClick="javascript:self.close()" />';
  299:         edit_text += '</center></body></html>';
  300: 
  301:         if (editwin != null && !(editwin.closed) ) {
  302:             editwin.close();
  303:         }
  304: 
  305:         editwin = window.open($nothing,'CellEditWin','height=200,width=350,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
  306:         editwin.document.write(edit_text);
  307:     }
  308: </script>
  309: ENDSCRIPT
  310:     }
  311:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
  312:               '<form action="'.$r->uri.'" name="sheet" method="post">');
  313:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
  314:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
  315:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
  316:     $r->rflush();
  317:     ##
  318:     ## Determine the filename to use
  319:     my $filename = undef;
  320:     if ($allowed_to_edit) {
  321:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
  322:         #
  323:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
  324:             $filename = $ENV{'form.loadfilename'};
  325:         }
  326:     }
  327:     ##
  328:     ## Make the spreadsheet
  329:     &Apache::Spreadsheet::initialize_spreadsheet_package();
  330:     my $spreadsheet = undef;
  331:     if ($sheettype eq 'classcalc') {
  332:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
  333:     } elsif ($sheettype eq 'studentcalc') {
  334:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
  335:     } elsif ($sheettype eq 'assesscalc' && 
  336:              defined($symb) && 
  337:              $allowed_to_edit) {
  338:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
  339:     } else {
  340:         return HTTP_NOT_ACCEPTABLE;
  341:     }
  342:     if (! defined($spreadsheet)) {
  343:         # error error - run in circles, scream and shout
  344:         return;
  345:     }
  346:     #
  347:     # Output selector
  348:     $r->print('<input type="submit" value="Update Display" /><br />');
  349:     ##
  350:     ## Editing/loading/saving
  351:     if ($allowed_to_edit) {
  352:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
  353:                   '<td valign="bottom">'.
  354:                   &file_dialogs($spreadsheet)."</td></tr></table>\n");
  355:         $r->rflush();
  356:     } else {
  357:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
  358:                   "</td></tr></table>\n");
  359:     }
  360:     #
  361:     # Keep track of the filename
  362:     $r->print(&hiddenfield('filename',$filename));
  363:     #
  364:     # Keep track of the number of times we have been called, sort of.
  365:     $r->print(&hiddenfield('not_first_run','whatever'));
  366:     #
  367:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
  368:         $r->print($spreadsheet->get_html_title());
  369:         if ($allowed_to_view || $allowed_to_edit) {
  370:             $r->print($spreadsheet->parent_link());
  371:         }
  372:         $spreadsheet->display($r);
  373:     } else {
  374:         $r->print("<h2>Make your selections and bonk the 'update display' button</h2>");
  375:     }
  376:     $r->print('</form></body></html>');
  377:     return OK;
  378: }
  379: 
  380: 1;
  381: 
  382: __END__
  383: 

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