File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.13: download - view: text, annotated - select for diffs
Thu Jun 19 16:04:06 2003 UTC (21 years ago) by matthew
Branches: MAIN
CVS tags: HEAD
Need the 'generate spreadsheet' button to be present in case you need
to change the section or enrollment status.

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

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