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

1.1       matthew     1: #
1.4     ! matthew     2: # $Id: lonspreadsheet.pm,v 1.3 2003/05/19 15:53:07 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: 
                     91: sub handler {
                     92:     my $r=shift;
                     93:     #
                     94:     # Overload checking
                     95:     #
                     96:     # Check this server
                     97:     my $loaderror=&Apache::lonnet::overloaderror($r);
                     98:     if ($loaderror) { return $loaderror; }
                     99:     # Check the course homeserver
                    100:     $loaderror= &Apache::lonnet::overloaderror($r,
                    101:                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
1.4     ! matthew   102: #    if ($loaderror) { return $loaderror; } 
1.1       matthew   103:     #
                    104:     # HTML Header
                    105:     #
                    106:     if ($r->header_only) {
                    107:         $r->content_type('text/html');
                    108:         $r->send_http_header;
                    109:         return OK;
                    110:     }
                    111:     #
                    112:     # Roles Checking
                    113:     #
                    114:     # Needs to be in a course
                    115:     if (! $ENV{'request.course.fn'}) { 
                    116:         # Not in a course, or not allowed to modify parms
                    117:         $ENV{'user.error.msg'}=
                    118:             $r->uri.":opa:0:0:Cannot modify spreadsheet";
                    119:         return HTTP_NOT_ACCEPTABLE; 
                    120:     }
                    121:     #
                    122:     # Get query string for limited number of parameters
                    123:     #
                    124:     &Apache::loncommon::get_unprocessed_cgi
                    125:         ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename']);
                    126:     #
                    127:     # Deal with restricted student permissions 
                    128:     #
                    129:     if ($ENV{'request.role'} =~ /^st\./) {
                    130:         delete $ENV{'form.cell'}       if (exists($ENV{'form.cell'}));
                    131:         delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
                    132:     }
                    133:     #
                    134:     # Determine basic information about the spreadsheet
                    135:     my ($sheettype) = ($r->uri=~/\/(\w+)$/);
                    136:     #
                    137:     my $symb   = undef;
                    138:     $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
                    139:     my $name   = $ENV{'user.name'};
                    140:     my $domain = $ENV{'user.domain'};
                    141:     if (exists($ENV{'form.sname'})) {
                    142:         $name   = $ENV{'form.sname'};
                    143:         $domain = $ENV{'form.sdomain'};
                    144:     }
                    145:     #
                    146:     # Open page, try to prevent browser cache.
                    147:     #
                    148:     $r->content_type('text/html');
                    149:     $r->header_out('Cache-control','no-cache');
                    150:     $r->header_out('Pragma','no-cache');
                    151:     $r->send_http_header;
1.3       matthew   152:     ##
                    153:     ## Check permissions
                    154:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
                    155:                                                 $ENV{'request.course.id'});
                    156:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
                    157:                                                 $ENV{'request.course.id'});
1.1       matthew   158:     #
1.3       matthew   159:     # Only those able to view others grades will be allowed to continue 
                    160:     # if they are not requesting their own.
1.1       matthew   161:     if (($sheettype eq 'classcalc') || 
                    162:         ($name   ne $ENV{'user.name'} ) ||
                    163:         ($domain ne $ENV{'user.domain'})) {
1.3       matthew   164:         if (! $allowed_to_view) {
1.1       matthew   165:             $r->print('<h1>Access Permission Denied</h1>'.
                    166:                       '</form></body></html>');
                    167:             return OK;
                    168:         }
                    169:     }
                    170:     #
                    171:     # Header....
                    172:     #
                    173:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
                    174:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
                    175:     ##
                    176:     ## Spit out the javascript required for editing
                    177:     ##
                    178:     if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
                    179:         $r->print(<<ENDSCRIPT);
                    180: <script language="JavaScript">
                    181: 
                    182:     var editwin;
                    183: 
                    184:     function celledit(cellname,cellformula) {
                    185:         var edit_text = '';
                    186:         // cellformula may contain less-than and greater-than symbols, so
                    187:         // we need to escape them?  
                    188:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
                    189:         edit_text += '<form name="editwinform">';
                    190:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
                    191:         edit_text += '<textarea name="newformula" cols="40" rows="6"';
                    192:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
                    193:         edit_text += '</br>';
                    194:         edit_text += '<input type="button" name="accept" value="Accept"';
                    195:         edit_text += ' onClick=\\\'javascript:';
                    196:         edit_text += 'opener.document.sheet.cell.value=';
                    197:         edit_text +=     '"'+cellname+'";';
                    198:         edit_text += 'opener.document.sheet.newformula.value=';
                    199:         edit_text +=     'document.editwinform.newformula.value;';
                    200:         edit_text += 'opener.document.sheet.submit();';
                    201:         edit_text += 'self.close()\\\' />';
                    202:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    203:         edit_text += '<input type="button" name="abort" ';
                    204:         edit_text +=     'value="Discard Changes"';
                    205:         edit_text += ' onClick="javascript:self.close()" />';
                    206:         edit_text += '</center></body></html>';
                    207: 
                    208:         if (editwin != null && !(editwin.closed) ) {
                    209:             editwin.close();
                    210:         }
                    211: 
                    212:         editwin = window.open($nothing,'CellEditWin','height=200,width=350,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
                    213:         editwin.document.write(edit_text);
                    214:     }
                    215: </script>
                    216: ENDSCRIPT
                    217:     }
                    218:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
                    219:               '<form action="'.$r->uri.'" name="sheet" method="post">');
                    220:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
                    221:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
                    222:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
                    223:     $r->rflush();
                    224:     ##
                    225:     ## Determine the filename to use
                    226:     my $filename = undef;
1.3       matthew   227:     if ($allowed_to_edit) {
1.1       matthew   228:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
                    229:         #
                    230:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
                    231:             $filename = $ENV{'form.loadfilename'};
                    232:         }
                    233:     }
                    234:     ##
                    235:     ## Make the spreadsheet
                    236:     &Apache::Spreadsheet::initialize_spreadsheet_package();
                    237:     my $spreadsheet = undef;
                    238:     if ($sheettype eq 'classcalc') {
                    239:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
                    240:     } elsif ($sheettype eq 'studentcalc') {
                    241:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
1.2       matthew   242:     } elsif ($sheettype eq 'assesscalc' && 
                    243:              defined($symb) && 
1.3       matthew   244:              $allowed_to_edit) {
1.1       matthew   245:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
                    246:     } else {
1.2       matthew   247:         return HTTP_NOT_ACCEPTABLE;
                    248:     }
                    249:     if (! defined($spreadsheet)) {
                    250:         # error error - run in circles, scream and shout
1.1       matthew   251:         return;
                    252:     }
                    253:     ##
                    254:     ## Editing/loading/saving
1.3       matthew   255:     if ($allowed_to_edit) {
1.1       matthew   256:         ##
                    257:         ## Deal with saving the spreadsheet
                    258:         if (exists($ENV{'form.save'}) && 
                    259:             exists($ENV{'form.savefilename'})) {
                    260:             $spreadsheet->filename($ENV{'form.savefilename'});
                    261:             my $save_status = $spreadsheet->save();
                    262:             if ($save_status ne 'ok') {
                    263:                 $r->print("An error occurred while saving the spreadsheet".
                    264:                           "There error is:".$save_status);
                    265:             } else {
                    266:                 $r->print("Spreadsheet saved as ".$ENV{'form.savefilename'});
                    267:             }
                    268:         } elsif (exists($ENV{'form.newformula'}) && 
                    269:                  exists($ENV{'form.cell'})       && 
                    270:                  $ENV{'form.cell'} ne '' ) {
                    271:             ##
                    272:             ## Make any requested modifications to the spreadsheet
                    273:             $spreadsheet->modify_cell($ENV{'form.cell'},
                    274:                                       $ENV{'form.newformula'});
                    275:             $spreadsheet->save_tmp();
                    276:             # output that we are dealing with a temporary file
                    277:             $r->print(&hiddenfield('workcopy',$sheettype));
                    278:             $r->print('<pre>'.$ENV{'form.cell'}.' = '.
                    279:                       $ENV{'form.newformula'}.'</pre>'."\n");
                    280:         }
                    281:         ##
                    282:         ## Editing code
                    283:         $r->print(&hiddenfield('cell','').
                    284:                   &hiddenfield('newformula',''));
                    285:         ##
                    286:         ## Create the save and load dialogs
                    287:         $filename = $spreadsheet->filename();
                    288:         $filename = '' if ($filename =~ /^default\.$sheettype/i);
                    289:         $filename =~ s/_$sheettype$//;
                    290:         my $save_dialog = 
                    291:             '<input type="submit" name="save" value="Save as ..." /> '.
                    292:                 '<input type="text" name="savefilename" size="30" value="'.
                    293:                     $filename.'" />';
                    294:         my $makedefault_dialog = 
                    295:             '<input type="submit" name="makedefault" value="Make Default"/>';
                    296:         #
                    297:         my $load_dialog = 
                    298:             '<input type="submit" name="load" value="Load ..." />'.
                    299:                 '<select name="loadfilename">'.
                    300:                     '<option name="Default">Default</option>'."\n";
                    301:         foreach my $sheetfilename ($spreadsheet->othersheets()) {
                    302:             $sheetfilename =~ s/_$sheettype$//;
                    303:             $load_dialog .= '<option name="'.$sheetfilename.'"';
                    304:             if ($filename eq $sheetfilename) {
                    305:                 $load_dialog .= ' selected';
                    306:             }
                    307:             $load_dialog .= '>'.$sheetfilename."</option>\n";
                    308:         }
                    309:         #
                    310:         $r->print(<<END);
                    311: <!-- load / save dialogs -->
                    312: <table cellspacing="3">
                    313: <tr>
                    314: <td>$load_dialog</td>
                    315: <td>$save_dialog</td>
                    316: <td>$makedefault_dialog</td>
                    317: </tr>
                    318: </table>
                    319: END
                    320:         $r->rflush();
                    321:     }
                    322:     #
1.4     ! matthew   323:     # Output selector
        !           324:     $r->print($spreadsheet->html_header());
        !           325:     #
1.1       matthew   326:     # Keep track of the filename
                    327:     $r->print(&hiddenfield('filename',$filename));
                    328:     #
1.4     ! matthew   329:     $r->print($spreadsheet->get_html_title());
1.3       matthew   330:     if ($allowed_to_view || $allowed_to_edit) {
                    331:         $r->print($spreadsheet->parent_link());
1.1       matthew   332:     }
1.3       matthew   333:     $spreadsheet->display($r);
1.1       matthew   334:     $r->print('</form></body></html>');
                    335:     return OK;
                    336: }
                    337: 
                    338: 1;
                    339: 
                    340: __END__
                    341: 

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