File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.11: download - view: text, annotated - select for diffs
Wed Jun 18 19:44:22 2003 UTC (20 years, 11 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Interface changes:
make default has been changed to 'save and make default' and this functionality
has been implemented.
The description of the action last taken is no longer displayed in the middle
of the load/save table and instead appears below it.
The description of the last action taken is clearer and more cleanly presented.

    1: #
    2: # $Id: lonspreadsheet.pm,v 1.11 2003/06/18 19:44:22 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:     # Open page, try to prevent browser cache.
  243:     #
  244:     $r->content_type('text/html');
  245:     $r->header_out('Cache-control','no-cache');
  246:     $r->header_out('Pragma','no-cache');
  247:     $r->send_http_header;
  248:     ##
  249:     ## Check permissions
  250:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
  251:                                                 $ENV{'request.course.id'});
  252:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
  253:                                                 $ENV{'request.course.id'});
  254: 
  255:     #
  256:     # Only those able to view others grades will be allowed to continue 
  257:     # if they are not requesting their own.
  258:     if (($sheettype eq 'classcalc') || 
  259:         ($name   ne $ENV{'user.name'} ) ||
  260:         ($domain ne $ENV{'user.domain'})) {
  261:         if (! $allowed_to_view) {
  262:             $r->print('<h1>Access Permission Denied</h1>'.
  263:                       '</form></body></html>');
  264:             return OK;
  265:         }
  266:     }
  267:     #
  268:     # Header....
  269:     #
  270:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
  271:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
  272:     ##
  273:     ## Spit out the javascript required for editing
  274:     ##
  275:     if ($allowed_to_edit) {
  276:         my $extra_javascript = 
  277:             &Apache::loncommon::browser_and_searcher_javascript();
  278:         $r->print(<<ENDSCRIPT);
  279: <script language="JavaScript">
  280: 
  281:     $extra_javascript
  282: 
  283:     var editwin;
  284: 
  285:     function celledit(cellname,cellformula) {
  286:         var edit_text = '';
  287:         // cellformula may contain less-than and greater-than symbols, so
  288:         // we need to escape them?  
  289:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
  290:         edit_text += '<form name="editwinform">';
  291:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
  292:         edit_text += '<textarea name="newformula" cols="40" rows="6"';
  293:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
  294:         edit_text += '</br>';
  295:         edit_text += '<input type="button" name="accept" value="Accept"';
  296:         edit_text += ' onClick=\\\'javascript:';
  297:         edit_text += 'opener.document.sheet.cell.value=';
  298:         edit_text +=     '"'+cellname+'";';
  299:         edit_text += 'opener.document.sheet.newformula.value=';
  300:         edit_text +=     'document.editwinform.newformula.value;';
  301:         edit_text += 'opener.document.sheet.submit();';
  302:         edit_text += 'self.close()\\\' />';
  303:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  304:         edit_text += '<input type="button" name="abort" ';
  305:         edit_text +=     'value="Discard Changes"';
  306:         edit_text += ' onClick="javascript:self.close()" />';
  307:         edit_text += '</center></body></html>';
  308: 
  309:         if (editwin != null && !(editwin.closed) ) {
  310:             editwin.close();
  311:         }
  312: 
  313:         editwin = window.open($nothing,'CellEditWin','height=200,width=350,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
  314:         editwin.document.write(edit_text);
  315:     }
  316: </script>
  317: ENDSCRIPT
  318:     }
  319:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
  320:               '<form action="'.$r->uri.'" name="sheet" method="post">');
  321:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
  322:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
  323:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
  324:     $r->rflush();
  325:     ##
  326:     ## Determine the filename to use
  327:     my $filename = undef;
  328:     if ($allowed_to_edit) {
  329:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
  330:         #
  331:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
  332:             $filename = $ENV{'form.loadfilename'};
  333:             $ENV{'form.workcopy'} = 'no';
  334:         }
  335:     }
  336:     ##
  337:     ## Make the spreadsheet
  338:     &Apache::Spreadsheet::initialize_spreadsheet_package();
  339:     my $spreadsheet = undef;
  340:     if ($sheettype eq 'classcalc') {
  341:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
  342:     } elsif ($sheettype eq 'studentcalc') {
  343:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
  344:     } elsif ($sheettype eq 'assesscalc' && 
  345:              defined($symb) && 
  346:              $allowed_to_edit) {
  347:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
  348:     } else {
  349:         return HTTP_NOT_ACCEPTABLE;
  350:     }
  351:     if (! defined($spreadsheet)) {
  352:         # error error - run in circles, scream and shout
  353:         return;
  354:     }
  355:     $spreadsheet->initialize();
  356:     #
  357:     # Output selector
  358:     ##
  359:     ## Editing/loading/saving
  360:     if ($allowed_to_edit) {
  361:         my ($html,$action_message) = &file_dialogs($spreadsheet);
  362:         if ($ENV{'form.makedefault'}) {
  363:             $spreadsheet->make_default();
  364:             if ($action_message) {
  365:                 $action_message .= '<br />';
  366:             }
  367:             $action_message .= 'Made this spreadsheet the default';
  368:             if ($sheettype eq 'classcalc') {
  369:                 $action_message .= ' for the course';
  370:             } elsif ($sheettype eq 'studentcalc') {
  371:                 $action_message .= ' for all students';
  372:             } elsif ($sheettype eq 'assesscalc') {
  373:                 $action_message .= ' for all assessments';
  374:             }
  375:             $action_message .= '.';
  376:         }
  377:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
  378:                   '<td valign="bottom">'.$html."</td></tr></table>\n");
  379:         $r->print(<<END);
  380: <table>
  381: <tr><td valign="top"><b>Last Action:</b></td>
  382:     <td>&nbsp;</td>
  383:     <td>$action_message</td>
  384: </tr>
  385: </table>
  386: END
  387:         $r->rflush();
  388:     } else {
  389:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
  390:                   "</td></tr></table>\n");
  391:     }
  392:     $r->rflush();
  393:     #
  394:     if (! exists($ENV{'form.not_first_run'}) && $sheettype eq 'classcalc') {
  395:         $r->print('<input type="submit" value="Generate Spreadsheet" /><br />');
  396:     }
  397:     #
  398:     # Keep track of the filename
  399:     $r->print(&hiddenfield('filename',$filename));
  400:     #
  401:     # Keep track of the number of times we have been called, sort of.
  402:     $r->print(&hiddenfield('not_first_run','whatever'));
  403:     #
  404:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
  405:         $r->print($spreadsheet->get_html_title());
  406:         if ($allowed_to_view || $allowed_to_edit) {
  407:             $r->print($spreadsheet->parent_link());
  408:         }
  409:         $spreadsheet->display($r);
  410:     }
  411:     $r->print('</form></body></html>');
  412:     return OK;
  413: }
  414: 
  415: 1;
  416: 
  417: __END__
  418: 

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