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

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

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