File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.5: download - view: text, annotated - select for diffs
Tue May 27 19:17:07 2003 UTC (21 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Moved load/save dialog generation to seperate subroutine.
Reworked placement of UI elements.
Added 'browse' link for  selection (part of Bug 657) of published
spreadsheets.  These are not currently supported, but this is the first step.

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

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