File:  [LON-CAPA] / loncom / interface / spreadsheet / lonspreadsheet.pm
Revision 1.23: download - view: text, annotated - select for diffs
Fri Sep 5 01:06:45 2003 UTC (20 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Minor modifications brought about due to making warnings fatal.

    1: #
    2: # $Id: lonspreadsheet.pm,v 1.23 2003/09/05 01:06:45 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 warnings FATAL=>'all';
   58: no warnings 'uninitialized';
   59: use Apache::classcalc();
   60: use Apache::studentcalc();
   61: use Apache::assesscalc();
   62: use Apache::Constants qw(:common :http);
   63: use Apache::lonnet;
   64: use Apache::lonhtmlcommon;
   65: use HTML::Entities();
   66: 
   67: ##
   68: ## HTML utility subroutines really should go in lonhtmlcommon
   69: ##
   70: 
   71: sub textfield {
   72:     my ($title,$name,$value)=@_;
   73:     return "\n<p><b>$title:</b><br>".
   74:         '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
   75: }
   76: 
   77: sub hiddenfield {
   78:     my ($name,$value)=@_;
   79:     return '<input type=hidden name="'.$name.'" value="'.$value.'" />'."\n";
   80: }
   81: 
   82: sub selectbox {
   83:     my ($title,$name,$value,%options)=@_;
   84:     my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
   85:     foreach (sort keys(%options)) {
   86:         $selout.='<option value="'.$_.'"';
   87:         if ($_ eq $value) { $selout.=' selected'; }
   88:         $selout.='>'.$options{$_}.'</option>';
   89:     }
   90:     return $selout.'</select>';
   91: }
   92: 
   93: sub file_dialogs {
   94:     my ($spreadsheet) = @_;
   95:     my $bgcolor = "#FFFFFF";
   96:     my $sheettype = $spreadsheet->{'type'};
   97:     my $result = '';
   98:     my $message = '';
   99:     ##
  100:     ## Deal with saving the spreadsheet
  101:     if ((exists($ENV{'form.save'}) || exists($ENV{'form.makedefault'})) && 
  102:         exists($ENV{'form.savefilename'})) {
  103:         $spreadsheet->filename($ENV{'form.savefilename'});
  104:         my $save_status = $spreadsheet->save();
  105:         if ($save_status ne 'ok') {
  106:             $message .= "An error occurred while saving the spreadsheet".
  107:                 "There error is:".$save_status;
  108:             return $result;
  109:         } else {
  110:             $message .= "Spreadsheet saved as ".$ENV{'form.savefilename'};
  111:         }
  112:     } elsif (exists($ENV{'form.newformula'}) && 
  113:              exists($ENV{'form.cell'})       && 
  114:              $ENV{'form.cell'} ne '' ) {
  115:         ##
  116:         ## Make any requested modifications to the spreadsheet
  117:         $spreadsheet->modify_cell($ENV{'form.cell'},
  118:                                   $ENV{'form.newformula'});
  119:         $spreadsheet->save_tmp();
  120:         # output that we are dealing with a temporary file
  121:         $result .=&hiddenfield('workcopy',$sheettype);
  122:         if ($ENV{'form.newformula'} !~ /^\s*$/) {
  123:             $message .='<table><tr>'.
  124:               '<td valign="top"><pre>Cell '.$ENV{'form.cell'}.' = </pre></td>'.
  125:               '<td><pre>'.$ENV{'form.newformula'}."</pre></td></tr></table>\n";
  126:         } else {
  127:             $message .= 'Deleted contents of cell '.$ENV{'form.cell'}.'.';
  128:         }
  129:     }
  130:     ##
  131:     ## Editing code
  132:     $result .=&hiddenfield('cell','').
  133:               &hiddenfield('newformula','');
  134:     ##
  135:     ## Create the save and load dialogs
  136:     my $filename = $spreadsheet->filename();
  137:     my $truefilename = $filename;
  138:     if ($spreadsheet->is_default()) {
  139:         $filename = 'Default';
  140:     }
  141:     my $save_dialog = '<nobr>'.
  142:         '<input type="submit" name="save" value="Save as" /> '.
  143:         '<input type="text" name="savefilename" size="30" value="'.
  144:         $truefilename.'" />'.
  145:         '</nobr>';
  146:     my $makedefault_dialog = '<input type="submit" name="makedefault" '.
  147:         'value="Save as & Make This Sheet the Default"/>';
  148:     #
  149:     my $link = '<a href="javascript:openbrowser'.
  150:         "('sheet','loadfilename','spreadsheet')\">Select Spreadsheet File</a>";
  151:     my $load_dialog = <<END;
  152: <table bgcolor="$bgcolor">
  153: <tr><td><input type="submit" name="load" value="Load" /></td>
  154:     <td><nobr>
  155:         <input type="text" name="loadfilename" size="20" value="$filename" />
  156:         $link</nobr>
  157:     </td></tr>
  158: <tr><td>&nbsp;</td><td>
  159:     <select name="fileselect" onchange="document.sheet.loadfilename.value=document.sheet.fileselect.value" >
  160: END
  161:     my $default_filename_set = 0;
  162:     foreach my $sheetfilename ($spreadsheet->othersheets()) {
  163:         $load_dialog .= '    <option name="'.$sheetfilename.'"';
  164:         if ($filename eq $sheetfilename) {
  165:             $load_dialog .= ' selected';
  166:             $default_filename_set = 1;
  167:         }
  168:         $load_dialog .= '>'.$sheetfilename."</option>\n";
  169:     }
  170:     $load_dialog .= "</select>\n</td><td>&nbsp;</td></tr>\n</table>\n";
  171:         #
  172:     $result .=<<END;
  173: <!-- 
  174:     <fieldset title="File Dialogs" >
  175:     <legend>File Dialogs</legend>
  176:   -->
  177: <!-- load / save dialogs -->
  178: <table cellspacing="2">
  179: <tr>
  180:     <td>$load_dialog</td>
  181:     <td>
  182:         <table bgcolor="$bgcolor">
  183:         <tr><td>$save_dialog</td></tr>
  184:         <tr><td align="center">$makedefault_dialog</td></tr>
  185:         </table>
  186:     </td>
  187: </tr>
  188: </table>
  189: <!--
  190:     </fieldset>
  191:   -->
  192: END
  193:     return ($result,$message);
  194: }
  195: 
  196: sub handler {
  197:     my $r=shift;
  198:     #
  199:     # Overload checking
  200:     #
  201:     # Check this server
  202:     my $loaderror=&Apache::lonnet::overloaderror($r);
  203:     if ($loaderror) { return $loaderror; }
  204:     # Check the course homeserver
  205:     $loaderror= &Apache::lonnet::overloaderror($r,
  206:                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
  207: #    if ($loaderror) { return $loaderror; } 
  208:     #
  209:     # HTML Header
  210:     #
  211:     if ($r->header_only) {
  212:         $r->content_type('text/html');
  213:         $r->send_http_header;
  214:         return OK;
  215:     }
  216:     #
  217:     # Roles Checking
  218:     #
  219:     # Needs to be in a course
  220:     if (! $ENV{'request.course.fn'}) { 
  221:         # Not in a course, or not allowed to modify parms
  222:         $ENV{'user.error.msg'}=
  223:             $r->uri.":opa:0:0:Cannot modify spreadsheet";
  224:         return HTTP_NOT_ACCEPTABLE; 
  225:     }
  226:     my $courseid = $ENV{'request.course.id'};
  227:     #
  228:     # Do not allow students to continue if standard grading is in effect.
  229:     if ($ENV{'request.role'} =~ /^st\./) {
  230:         if ($ENV{'course.'.$courseid.'.grading'} eq 'standard') {
  231:             return HTTP_NOT_ACCEPTABLE;
  232:         }
  233:     }
  234:     #
  235:     # Get query string for limited number of parameters
  236:     #
  237:     &Apache::loncommon::get_unprocessed_cgi
  238:         ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename','recalc']);
  239:     #
  240:     # Deal with restricted student permissions 
  241:     #
  242:     if ($ENV{'request.role'} =~ /^st\./) {
  243:         delete $ENV{'form.cell'}       if (exists($ENV{'form.cell'}));
  244:         delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
  245:     }
  246:     #
  247:     # Determine basic information about the spreadsheet
  248:     my ($sheettype) = ($r->uri=~/\/(\w+)$/);
  249:     #
  250:     my $symb   = undef;
  251:     $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
  252:     my $name   = $ENV{'user.name'};
  253:     my $domain = $ENV{'user.domain'};
  254:     if (exists($ENV{'form.sname'})) {
  255:         $name   = $ENV{'form.sname'};
  256:         $domain = $ENV{'form.sdomain'};
  257:     }
  258:     ##
  259:     ## Check permissions
  260:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
  261:                                                 $ENV{'request.course.id'});
  262:     # Only those instructors/tas/whatevers with complete access
  263:     # (not section restricted) are able to modify spreadsheets.
  264:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
  265:                                                 $ENV{'request.course.id'});
  266:     if (! $allowed_to_view) {
  267:         $allowed_to_view = &Apache::lonnet::allowed('vgr',
  268:                     $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'});
  269:         # Those who are restricted by section are allowed to view.
  270:         # The routines in lonstatistics which decide which students' 
  271:         # will be shown take care of the restriction by section.
  272:     }
  273:     #
  274:     # Only those able to view others grades will be allowed to continue 
  275:     # if they are not requesting their own.
  276:     if ($sheettype eq 'classcalc') {
  277:         if (! $allowed_to_view) {
  278: 	    $ENV{'user.error.msg'}=
  279: 		$r->uri.":vgr:0:0:Access Permission Denied";
  280: 	    return HTTP_NOT_ACCEPTABLE; 
  281: 	}
  282:     }
  283:     if ((($name   ne $ENV{'user.name'} ) ||
  284:          ($domain ne $ENV{'user.domain'})) && $sheettype ne 'classcalc') {
  285:         # Check that the student is in their section?
  286:         if (exists($ENV{'request.course.sec'}) && 
  287:             $ENV{'request.course.sec'} ne '' ) {
  288:             my $stu_sec = &Apache::lonnet::usection($domain,$name,
  289:                                                     $ENV{'request.course.id'});
  290:             if ($stu_sec ne $ENV{'request.course.sec'}) {
  291: 		$ENV{'user.error.msg'}=
  292: 		    $r->uri.":vgr:0:0:Requested student not in your section.";
  293: 		return HTTP_NOT_ACCEPTABLE; 
  294:             }
  295:         }
  296:     }
  297: 
  298:     #
  299:     # Open page, try to prevent browser cache.
  300:     #
  301:     $r->content_type('text/html');
  302:     &Apache::loncommon::no_cache($r);
  303:     $r->send_http_header;
  304: 
  305:     #
  306:     # Header....
  307:     #
  308:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
  309:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
  310:     ##
  311:     ## Spit out the javascript required for editing
  312:     ##
  313:     if ($allowed_to_edit) {
  314:         my $extra_javascript = 
  315:             &Apache::loncommon::browser_and_searcher_javascript();
  316:         $r->print(<<ENDSCRIPT);
  317: <script language="JavaScript">
  318: 
  319:     $extra_javascript
  320: 
  321:     var editwin;
  322: 
  323:     function celledit(cellname,cellformula) {
  324:         var edit_text = '';
  325:         // cellformula may contain less-than and greater-than symbols, so
  326:         // we need to escape them?  
  327:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
  328:         edit_text += '<form name="editwinform">';
  329:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
  330:         edit_text += '<textarea name="newformula" cols="60" rows="12"';
  331:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
  332:         edit_text += '</br>';
  333:         edit_text += '<input type="button" name="accept" value="Accept"';
  334:         edit_text += ' onClick=\\\'javascript:';
  335:         edit_text += 'opener.document.sheet.cell.value=';
  336:         edit_text +=     '"'+cellname+'";';
  337:         edit_text += 'opener.document.sheet.newformula.value=';
  338:         edit_text +=     'document.editwinform.newformula.value;';
  339:         edit_text += 'opener.document.sheet.submit();';
  340:         edit_text += 'self.close()\\\' />';
  341:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  342:         edit_text += '<input type="button" name="abort" ';
  343:         edit_text +=     'value="Discard Changes"';
  344:         edit_text += ' onClick="javascript:self.close()" />';
  345:         edit_text += '</center></body></html>';
  346: 
  347:         if (editwin != null && !(editwin.closed) ) {
  348:             editwin.close();
  349:         }
  350: 
  351:         editwin = window.open($nothing,'CellEditWin','height=280,width=480,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
  352:         editwin.document.write(edit_text);
  353:     }
  354: </script>
  355: ENDSCRIPT
  356:     }
  357:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
  358:               '<form action="'.$r->uri.'" name="sheet" method="post">');
  359:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
  360:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
  361:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
  362:     $r->rflush();
  363:     ##
  364:     ## Determine the filename to use
  365:     my $filename = undef;
  366:     if ($allowed_to_edit) {
  367:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
  368:         #
  369:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
  370:             $filename = $ENV{'form.loadfilename'};
  371:             $ENV{'form.workcopy'} = 'no';
  372:         }
  373:     }
  374:     ##
  375:     ## Take care of "backdoor" spreadsheet expiration / recalc stuff
  376:     if ($allowed_to_edit && exists($ENV{'form.recalc'})) {
  377:         if ($ENV{'form.recalc'} eq 'ilovewastingtime') {
  378:             &Apache::lonnet::logthis('ilovewastingtime');
  379:             # expire ALL spreadsheets
  380:             &Apache::lonnet::expirespread('','','studentcalc');
  381:             &Apache::lonnet::expirespread('','','assesscalc');
  382:         } elsif ($ENV{'form.recalc'} =~ /^symb:/) {
  383:             # expire for all students on this symb
  384:             my ($symb) = ($ENV{'form.recalc'} =~ /^symb:(.*)$/);
  385:             &Apache::lonnet::logthis('symb = '.$symb);
  386:             &Apache::lonnet::expirespread('','','assesscalc',$symb);
  387:             &Apache::lonnet::expirespread('','','studentcalc');
  388:         } elsif ($ENV{'form.recalc'} =~ /^student:/) {
  389:             # expire all assessment spreadsheets for this user
  390:             my ($sname,$sdom) = ($ENV{'form.recalc'}=~/^student:(.*):(.*)$/);
  391:             &Apache::lonnet::logthis('student = '.$sname.':'.$sdom);
  392:             if (defined($sname) && defined($sdom)) {
  393:                 &Apache::lonnet::expirespread($sname,$sdom,'assesscalc');
  394:                 &Apache::lonnet::expirespread($sname,$sdom,'studentcalc');
  395:             }
  396:         }
  397:     }
  398:     ##
  399:     ## Make the spreadsheet
  400:     &Apache::Spreadsheet::initialize_spreadsheet_package();
  401:     my $spreadsheet = undef;
  402:     if ($sheettype eq 'classcalc') {
  403:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
  404:     } elsif ($sheettype eq 'studentcalc') {
  405:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
  406:     } elsif ($sheettype eq 'assesscalc' && 
  407:              defined($symb) && 
  408:              $allowed_to_edit) {
  409:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
  410:     } else {
  411:         return HTTP_NOT_ACCEPTABLE;
  412:     }
  413:     if (! defined($spreadsheet)) {
  414:         # error error - run in circles, scream and shout
  415:         return;
  416:     }
  417:     $spreadsheet->initialize();
  418:     #
  419:     # Output selector
  420:     ##
  421:     ## Editing/loading/saving
  422:     if ($allowed_to_edit) {
  423:         my ($html,$action_message) = &file_dialogs($spreadsheet);
  424:         if ($ENV{'form.makedefault'}) {
  425:             $spreadsheet->make_default();
  426:             if ($action_message) {
  427:                 $action_message .= '<br />';
  428:             }
  429:             $action_message .= 'Made this spreadsheet the default';
  430:             if ($sheettype eq 'classcalc') {
  431:                 $action_message .= ' for the course';
  432:             } elsif ($sheettype eq 'studentcalc') {
  433:                 $action_message .= ' for all students';
  434:             } elsif ($sheettype eq 'assesscalc') {
  435:                 $action_message .= ' for all assessments';
  436:             }
  437:             $action_message .= '.';
  438:         }
  439:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
  440:                   '<td valign="bottom">'.$html."</td></tr></table>\n");
  441:         if ($action_message ne '') {
  442:             $r->print(<<END);
  443: <table>
  444: <tr><td valign="top"><b>Last Action:</b></td>
  445:     <td>&nbsp;</td>
  446:     <td>$action_message</td>
  447: </tr>
  448: </table>
  449: END
  450:         }
  451:         $r->rflush();
  452:     } else {
  453:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
  454:                   "</td></tr></table>\n");
  455:     }
  456:     $r->rflush();
  457:     #
  458:     $r->print("<table><tr>");
  459:     if ($sheettype eq 'classcalc') {
  460:         $r->print('<td><input type="submit" value="Generate Spreadsheet" />'.
  461:                   '</td>');
  462:     }
  463:     if ($allowed_to_view) {
  464:         $r->print('<td>'.
  465:                   &Apache::loncommon::help_open_topic("Spreadsheet_About",
  466:                                                       'Spreadsheet Help').
  467:                   '</td>');
  468:     }
  469:     if ($allowed_to_edit) {
  470:         $r->print('<td>'.
  471:                   &Apache::loncommon::help_open_topic("Spreadsheet_Editing",
  472:                                                       'Editing Help').
  473:                   '</td>');
  474:     }
  475:     $r->print('</tr></table>');
  476:     #
  477:     # Keep track of the filename
  478:     $r->print(&hiddenfield('filename',$filename));
  479:     #
  480:     # Keep track of the number of times we have been called, sort of.
  481:     $r->print(&hiddenfield('not_first_run','whatever'));
  482:     #
  483:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
  484:         $r->print($spreadsheet->get_html_title());
  485:         if ($allowed_to_view || $allowed_to_edit) {
  486:             $r->print($spreadsheet->parent_link());
  487:         }
  488:         $r->rflush();
  489:         $spreadsheet->display($r);
  490:     }
  491:     $r->print('</form></body></html>');
  492:     return OK;
  493: }
  494: 
  495: 1;
  496: 
  497: __END__
  498: 

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