File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.9: download - view: text, annotated - select for diffs
Mon Jun 16 13:51:46 2003 UTC (21 years ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #1689: Users get confused between browsing the local file system and
browsing RES.

The official word is now "Select".

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

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