Annotation of loncom/interface/spreadsheet/lonspreadsheet.pm, revision 1.14

1.1       matthew     1: #
1.14    ! www         2: # $Id: lonspreadsheet.pm,v 1.13 2003/06/19 16:04:06 matthew Exp $
1.1       matthew     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: 
1.5       matthew    91: sub file_dialogs {
                     92:     my ($spreadsheet) = @_;
                     93:     my $bgcolor = "#FFFFFF";
                     94:     my $sheettype = $spreadsheet->{'type'};
                     95:     my $result = '';
1.11      matthew    96:     my $message = '';
1.5       matthew    97:     ##
                     98:     ## Deal with saving the spreadsheet
1.11      matthew    99:     if ((exists($ENV{'form.save'}) || exists($ENV{'form.makedefault'})) && 
1.5       matthew   100:         exists($ENV{'form.savefilename'})) {
                    101:         $spreadsheet->filename($ENV{'form.savefilename'});
                    102:         my $save_status = $spreadsheet->save();
                    103:         if ($save_status ne 'ok') {
1.11      matthew   104:             $message .= "An error occurred while saving the spreadsheet".
1.5       matthew   105:                 "There error is:".$save_status;
                    106:             return $result;
                    107:         } else {
1.11      matthew   108:             $message .= "Spreadsheet saved as ".$ENV{'form.savefilename'};
1.5       matthew   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);
1.11      matthew   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:         }
1.5       matthew   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();
1.10      matthew   135:     my $truefilename = $filename;
                    136:     if ($spreadsheet->is_default()) {
                    137:         $filename = 'Default';
                    138:     }
1.5       matthew   139:     my $save_dialog = '<nobr>'.
                    140:         '<input type="submit" name="save" value="Save as" /> '.
                    141:         '<input type="text" name="savefilename" size="30" value="'.
1.10      matthew   142:         $truefilename.'" />'.
1.5       matthew   143:         '</nobr>';
                    144:     my $makedefault_dialog = '<input type="submit" name="makedefault" '.
1.11      matthew   145:         'value="Save as & Make This Sheet the Default"/>';
1.5       matthew   146:     #
                    147:     my $link = '<a href="javascript:openbrowser'.
1.9       www       148:         "('sheet','loadfilename','spreadsheet')\">Select Spreadsheet File</a>";
1.5       matthew   149:     my $load_dialog = <<END;
                    150: <table bgcolor="$bgcolor">
                    151: <tr><td><input type="submit" name="load" value="Load" /></td>
                    152:     <td><nobr>
1.10      matthew   153:         <input type="text" name="loadfilename" size="20" value="$filename" />
1.5       matthew   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
1.7       matthew   159:     my $default_filename_set = 0;
1.5       matthew   160:     foreach my $sheetfilename ($spreadsheet->othersheets()) {
                    161:         $load_dialog .= '    <option name="'.$sheetfilename.'"';
                    162:         if ($filename eq $sheetfilename) {
                    163:             $load_dialog .= ' selected';
1.7       matthew   164:             $default_filename_set = 1;
1.5       matthew   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
1.11      matthew   184:     return ($result,$message);
1.5       matthew   185: }
                    186: 
1.1       matthew   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'});
1.4       matthew   198: #    if ($loaderror) { return $loaderror; } 
1.1       matthew   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:     }
1.3       matthew   241:     ##
                    242:     ## Check permissions
                    243:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
                    244:                                                 $ENV{'request.course.id'});
1.12      matthew   245:     # Only those instructors/tas/whatevers with complete access
                    246:     # (not section restricted) are able to modify spreadsheets.
1.3       matthew   247:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
                    248:                                                 $ENV{'request.course.id'});
1.12      matthew   249:     if (! $allowed_to_view) {
                    250:         $allowed_to_view = &Apache::lonnet::allowed('vgr',
                    251:                     $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'});
                    252:         # Those who are restricted by section are allowed to view.
                    253:         # The routines in lonstatistics which decide which students' 
                    254:         # will be shown take care of the restriction by section.
                    255:     }
1.1       matthew   256:     #
1.3       matthew   257:     # Only those able to view others grades will be allowed to continue 
                    258:     # if they are not requesting their own.
1.12      matthew   259:     if ($sheettype eq 'classcalc') {
1.3       matthew   260:         if (! $allowed_to_view) {
1.14    ! www       261: 	    $ENV{'user.error.msg'}=
        !           262: 		$r->uri.":vgr:0:0:Access Permission Denied";
        !           263: 	    return HTTP_NOT_ACCEPTABLE; 
        !           264: 	}
1.1       matthew   265:     }
1.12      matthew   266:     if ((($name   ne $ENV{'user.name'} ) ||
                    267:          ($domain ne $ENV{'user.domain'})) && $sheettype ne 'classcalc') {
                    268:         # Check that the student is in their section?
                    269:         if (exists($ENV{'request.course.sec'}) && 
                    270:             $ENV{'request.course.sec'} ne '' ) {
                    271:             my $stu_sec = &Apache::lonnet::usection($domain,$name,
                    272:                                                     $ENV{'request.course.id'});
                    273:             if ($stu_sec ne $ENV{'request.course.sec'}) {
1.14    ! www       274: 		$ENV{'user.error.msg'}=
        !           275: 		    $r->uri.":vgr:0:0:Requested student not in your section.";
        !           276: 		return HTTP_NOT_ACCEPTABLE; 
1.12      matthew   277:             }
                    278:         }
                    279:     }
1.14    ! www       280: 
        !           281: # ================================================================== Good to go
        !           282: 
        !           283:     #
        !           284:     # Open page, try to prevent browser cache.
        !           285:     #
        !           286:     $r->content_type('text/html');
        !           287:     $r->header_out('Cache-control','no-cache');
        !           288:     $r->header_out('Pragma','no-cache');
        !           289:     $r->send_http_header;
1.12      matthew   290: 
1.1       matthew   291:     #
                    292:     # Header....
                    293:     #
                    294:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
                    295:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
                    296:     ##
                    297:     ## Spit out the javascript required for editing
                    298:     ##
1.5       matthew   299:     if ($allowed_to_edit) {
                    300:         my $extra_javascript = 
                    301:             &Apache::loncommon::browser_and_searcher_javascript();
1.1       matthew   302:         $r->print(<<ENDSCRIPT);
                    303: <script language="JavaScript">
                    304: 
1.5       matthew   305:     $extra_javascript
                    306: 
1.1       matthew   307:     var editwin;
                    308: 
                    309:     function celledit(cellname,cellformula) {
                    310:         var edit_text = '';
                    311:         // cellformula may contain less-than and greater-than symbols, so
                    312:         // we need to escape them?  
                    313:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
                    314:         edit_text += '<form name="editwinform">';
                    315:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
                    316:         edit_text += '<textarea name="newformula" cols="40" rows="6"';
                    317:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
                    318:         edit_text += '</br>';
                    319:         edit_text += '<input type="button" name="accept" value="Accept"';
                    320:         edit_text += ' onClick=\\\'javascript:';
                    321:         edit_text += 'opener.document.sheet.cell.value=';
                    322:         edit_text +=     '"'+cellname+'";';
                    323:         edit_text += 'opener.document.sheet.newformula.value=';
                    324:         edit_text +=     'document.editwinform.newformula.value;';
                    325:         edit_text += 'opener.document.sheet.submit();';
                    326:         edit_text += 'self.close()\\\' />';
                    327:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    328:         edit_text += '<input type="button" name="abort" ';
                    329:         edit_text +=     'value="Discard Changes"';
                    330:         edit_text += ' onClick="javascript:self.close()" />';
                    331:         edit_text += '</center></body></html>';
                    332: 
                    333:         if (editwin != null && !(editwin.closed) ) {
                    334:             editwin.close();
                    335:         }
                    336: 
                    337:         editwin = window.open($nothing,'CellEditWin','height=200,width=350,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
                    338:         editwin.document.write(edit_text);
                    339:     }
                    340: </script>
                    341: ENDSCRIPT
                    342:     }
                    343:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
                    344:               '<form action="'.$r->uri.'" name="sheet" method="post">');
                    345:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
                    346:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
1.6       matthew   347:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
1.1       matthew   348:     $r->rflush();
                    349:     ##
                    350:     ## Determine the filename to use
                    351:     my $filename = undef;
1.3       matthew   352:     if ($allowed_to_edit) {
1.1       matthew   353:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
                    354:         #
                    355:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
                    356:             $filename = $ENV{'form.loadfilename'};
1.7       matthew   357:             $ENV{'form.workcopy'} = 'no';
1.1       matthew   358:         }
                    359:     }
                    360:     ##
                    361:     ## Make the spreadsheet
                    362:     &Apache::Spreadsheet::initialize_spreadsheet_package();
                    363:     my $spreadsheet = undef;
                    364:     if ($sheettype eq 'classcalc') {
                    365:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
                    366:     } elsif ($sheettype eq 'studentcalc') {
                    367:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
1.2       matthew   368:     } elsif ($sheettype eq 'assesscalc' && 
                    369:              defined($symb) && 
1.3       matthew   370:              $allowed_to_edit) {
1.1       matthew   371:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
                    372:     } else {
1.2       matthew   373:         return HTTP_NOT_ACCEPTABLE;
                    374:     }
                    375:     if (! defined($spreadsheet)) {
                    376:         # error error - run in circles, scream and shout
1.1       matthew   377:         return;
                    378:     }
1.8       matthew   379:     $spreadsheet->initialize();
1.5       matthew   380:     #
                    381:     # Output selector
1.1       matthew   382:     ##
                    383:     ## Editing/loading/saving
1.3       matthew   384:     if ($allowed_to_edit) {
1.11      matthew   385:         my ($html,$action_message) = &file_dialogs($spreadsheet);
1.10      matthew   386:         if ($ENV{'form.makedefault'}) {
                    387:             $spreadsheet->make_default();
1.11      matthew   388:             if ($action_message) {
                    389:                 $action_message .= '<br />';
                    390:             }
                    391:             $action_message .= 'Made this spreadsheet the default';
                    392:             if ($sheettype eq 'classcalc') {
                    393:                 $action_message .= ' for the course';
                    394:             } elsif ($sheettype eq 'studentcalc') {
                    395:                 $action_message .= ' for all students';
                    396:             } elsif ($sheettype eq 'assesscalc') {
                    397:                 $action_message .= ' for all assessments';
                    398:             }
                    399:             $action_message .= '.';
1.10      matthew   400:         }
1.5       matthew   401:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
1.11      matthew   402:                   '<td valign="bottom">'.$html."</td></tr></table>\n");
1.12      matthew   403:         if ($action_message ne '') {
                    404:             $r->print(<<END);
1.11      matthew   405: <table>
                    406: <tr><td valign="top"><b>Last Action:</b></td>
                    407:     <td>&nbsp;</td>
                    408:     <td>$action_message</td>
                    409: </tr>
                    410: </table>
                    411: END
1.12      matthew   412:         }
1.1       matthew   413:         $r->rflush();
1.5       matthew   414:     } else {
                    415:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
                    416:                   "</td></tr></table>\n");
1.1       matthew   417:     }
1.11      matthew   418:     $r->rflush();
1.1       matthew   419:     #
1.13      matthew   420:     if ($sheettype eq 'classcalc') {
1.10      matthew   421:         $r->print('<input type="submit" value="Generate Spreadsheet" /><br />');
                    422:     }
                    423:     #
1.1       matthew   424:     # Keep track of the filename
                    425:     $r->print(&hiddenfield('filename',$filename));
                    426:     #
1.5       matthew   427:     # Keep track of the number of times we have been called, sort of.
                    428:     $r->print(&hiddenfield('not_first_run','whatever'));
                    429:     #
                    430:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
                    431:         $r->print($spreadsheet->get_html_title());
                    432:         if ($allowed_to_view || $allowed_to_edit) {
                    433:             $r->print($spreadsheet->parent_link());
                    434:         }
                    435:         $spreadsheet->display($r);
1.1       matthew   436:     }
                    437:     $r->print('</form></body></html>');
                    438:     return OK;
                    439: }
                    440: 
                    441: 1;
                    442: 
                    443: __END__
                    444: 

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