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

1.1       matthew     1: #
1.22    ! matthew     2: # $Id: lonspreadsheet.pm,v 1.21 2003/08/29 21:09:35 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:     }
1.22    ! matthew   168:     $load_dialog .= "</select>\n</td><td>&nbsp;</td></tr>\n</table>\n";
1.5       matthew   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; 
1.17      matthew   216:     }
                    217:     my $courseid = $ENV{'request.course.id'};
                    218:     #
                    219:     # Do not allow students to continue if standard grading is in effect.
                    220:     if ($ENV{'request.role'} =~ /^st\./) {
                    221:         if ($ENV{'course.'.$courseid.'.grading'} eq 'standard') {
                    222:             return HTTP_NOT_ACCEPTABLE;
                    223:         }
1.1       matthew   224:     }
                    225:     #
                    226:     # Get query string for limited number of parameters
                    227:     #
                    228:     &Apache::loncommon::get_unprocessed_cgi
1.19      matthew   229:         ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename','recalc']);
1.1       matthew   230:     #
                    231:     # Deal with restricted student permissions 
                    232:     #
                    233:     if ($ENV{'request.role'} =~ /^st\./) {
                    234:         delete $ENV{'form.cell'}       if (exists($ENV{'form.cell'}));
                    235:         delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
                    236:     }
                    237:     #
                    238:     # Determine basic information about the spreadsheet
                    239:     my ($sheettype) = ($r->uri=~/\/(\w+)$/);
                    240:     #
                    241:     my $symb   = undef;
                    242:     $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
                    243:     my $name   = $ENV{'user.name'};
                    244:     my $domain = $ENV{'user.domain'};
                    245:     if (exists($ENV{'form.sname'})) {
                    246:         $name   = $ENV{'form.sname'};
                    247:         $domain = $ENV{'form.sdomain'};
                    248:     }
1.3       matthew   249:     ##
                    250:     ## Check permissions
                    251:     my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
                    252:                                                 $ENV{'request.course.id'});
1.12      matthew   253:     # Only those instructors/tas/whatevers with complete access
                    254:     # (not section restricted) are able to modify spreadsheets.
1.3       matthew   255:     my $allowed_to_view =  &Apache::lonnet::allowed('vgr',
                    256:                                                 $ENV{'request.course.id'});
1.12      matthew   257:     if (! $allowed_to_view) {
                    258:         $allowed_to_view = &Apache::lonnet::allowed('vgr',
                    259:                     $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'});
                    260:         # Those who are restricted by section are allowed to view.
                    261:         # The routines in lonstatistics which decide which students' 
                    262:         # will be shown take care of the restriction by section.
                    263:     }
1.1       matthew   264:     #
1.3       matthew   265:     # Only those able to view others grades will be allowed to continue 
                    266:     # if they are not requesting their own.
1.12      matthew   267:     if ($sheettype eq 'classcalc') {
1.3       matthew   268:         if (! $allowed_to_view) {
1.14      www       269: 	    $ENV{'user.error.msg'}=
                    270: 		$r->uri.":vgr:0:0:Access Permission Denied";
                    271: 	    return HTTP_NOT_ACCEPTABLE; 
                    272: 	}
1.1       matthew   273:     }
1.12      matthew   274:     if ((($name   ne $ENV{'user.name'} ) ||
                    275:          ($domain ne $ENV{'user.domain'})) && $sheettype ne 'classcalc') {
                    276:         # Check that the student is in their section?
                    277:         if (exists($ENV{'request.course.sec'}) && 
                    278:             $ENV{'request.course.sec'} ne '' ) {
                    279:             my $stu_sec = &Apache::lonnet::usection($domain,$name,
                    280:                                                     $ENV{'request.course.id'});
                    281:             if ($stu_sec ne $ENV{'request.course.sec'}) {
1.14      www       282: 		$ENV{'user.error.msg'}=
                    283: 		    $r->uri.":vgr:0:0:Requested student not in your section.";
                    284: 		return HTTP_NOT_ACCEPTABLE; 
1.12      matthew   285:             }
                    286:         }
                    287:     }
1.14      www       288: 
                    289:     #
                    290:     # Open page, try to prevent browser cache.
                    291:     #
                    292:     $r->content_type('text/html');
1.15      matthew   293:     &Apache::loncommon::no_cache($r);
1.14      www       294:     $r->send_http_header;
1.12      matthew   295: 
1.1       matthew   296:     #
                    297:     # Header....
                    298:     #
                    299:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
                    300:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
                    301:     ##
                    302:     ## Spit out the javascript required for editing
                    303:     ##
1.5       matthew   304:     if ($allowed_to_edit) {
                    305:         my $extra_javascript = 
                    306:             &Apache::loncommon::browser_and_searcher_javascript();
1.1       matthew   307:         $r->print(<<ENDSCRIPT);
                    308: <script language="JavaScript">
                    309: 
1.5       matthew   310:     $extra_javascript
                    311: 
1.1       matthew   312:     var editwin;
                    313: 
                    314:     function celledit(cellname,cellformula) {
                    315:         var edit_text = '';
                    316:         // cellformula may contain less-than and greater-than symbols, so
                    317:         // we need to escape them?  
                    318:         edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
                    319:         edit_text += '<form name="editwinform">';
                    320:         edit_text += '<center><h3>Cell '+cellname+'</h3>';
1.18      matthew   321:         edit_text += '<textarea name="newformula" cols="60" rows="12"';
1.1       matthew   322:         edit_text += ' wrap="off" >'+cellformula+'</textarea>';
                    323:         edit_text += '</br>';
                    324:         edit_text += '<input type="button" name="accept" value="Accept"';
                    325:         edit_text += ' onClick=\\\'javascript:';
                    326:         edit_text += 'opener.document.sheet.cell.value=';
                    327:         edit_text +=     '"'+cellname+'";';
                    328:         edit_text += 'opener.document.sheet.newformula.value=';
                    329:         edit_text +=     'document.editwinform.newformula.value;';
                    330:         edit_text += 'opener.document.sheet.submit();';
                    331:         edit_text += 'self.close()\\\' />';
                    332:         edit_text += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                    333:         edit_text += '<input type="button" name="abort" ';
                    334:         edit_text +=     'value="Discard Changes"';
                    335:         edit_text += ' onClick="javascript:self.close()" />';
                    336:         edit_text += '</center></body></html>';
                    337: 
                    338:         if (editwin != null && !(editwin.closed) ) {
                    339:             editwin.close();
                    340:         }
                    341: 
1.18      matthew   342:         editwin = window.open($nothing,'CellEditWin','height=280,width=480,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
1.1       matthew   343:         editwin.document.write(edit_text);
                    344:     }
                    345: </script>
                    346: ENDSCRIPT
                    347:     }
                    348:     $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
                    349:               '<form action="'.$r->uri.'" name="sheet" method="post">');
                    350:     $r->print(&hiddenfield('sname'  ,$ENV{'form.sname'}).
                    351:               &hiddenfield('sdomain',$ENV{'form.sdomain'}).
1.6       matthew   352:               &hiddenfield('usymb'  ,$ENV{'form.usymb'}));
1.1       matthew   353:     $r->rflush();
                    354:     ##
                    355:     ## Determine the filename to use
                    356:     my $filename = undef;
1.3       matthew   357:     if ($allowed_to_edit) {
1.1       matthew   358:         $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
                    359:         #
                    360:         if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
                    361:             $filename = $ENV{'form.loadfilename'};
1.7       matthew   362:             $ENV{'form.workcopy'} = 'no';
1.1       matthew   363:         }
                    364:     }
                    365:     ##
1.19      matthew   366:     ## Take care of "backdoor" spreadsheet expiration / recalc stuff
                    367:     if ($allowed_to_edit && exists($ENV{'form.recalc'})) {
                    368:         if ($ENV{'form.recalc'} eq 'ilovewastingtime') {
                    369:             &Apache::lonnet::logthis('ilovewastingtime');
                    370:             # expire ALL spreadsheets
                    371:             &Apache::lonnet::expirespread('','','studentcalc');
                    372:             &Apache::lonnet::expirespread('','','assesscalc');
                    373:         } elsif ($ENV{'form.recalc'} =~ /^symb:/) {
                    374:             # expire for all students on this symb
                    375:             my ($symb) = ($ENV{'form.recalc'} =~ /^symb:(.*)$/);
                    376:             &Apache::lonnet::logthis('symb = '.$symb);
                    377:             &Apache::lonnet::expirespread('','','assesscalc',$symb);
                    378:             &Apache::lonnet::expirespread('','','studentcalc');
                    379:         } elsif ($ENV{'form.recalc'} =~ /^student:/) {
                    380:             # expire all assessment spreadsheets for this user
                    381:             my ($sname,$sdom) = ($ENV{'form.recalc'}=~/^student:(.*):(.*)$/);
                    382:             &Apache::lonnet::logthis('student = '.$sname.':'.$sdom);
                    383:             if (defined($sname) && defined($sdom)) {
                    384:                 &Apache::lonnet::expirespread($sname,$sdom,'assesscalc');
                    385:                 &Apache::lonnet::expirespread($sname,$sdom,'studentcalc');
                    386:             }
                    387:         }
                    388:     }
                    389:     ##
1.1       matthew   390:     ## Make the spreadsheet
                    391:     &Apache::Spreadsheet::initialize_spreadsheet_package();
                    392:     my $spreadsheet = undef;
                    393:     if ($sheettype eq 'classcalc') {
                    394:         $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
                    395:     } elsif ($sheettype eq 'studentcalc') {
                    396:         $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
1.2       matthew   397:     } elsif ($sheettype eq 'assesscalc' && 
                    398:              defined($symb) && 
1.3       matthew   399:              $allowed_to_edit) {
1.1       matthew   400:         $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
                    401:     } else {
1.2       matthew   402:         return HTTP_NOT_ACCEPTABLE;
                    403:     }
                    404:     if (! defined($spreadsheet)) {
                    405:         # error error - run in circles, scream and shout
1.1       matthew   406:         return;
                    407:     }
1.8       matthew   408:     $spreadsheet->initialize();
1.5       matthew   409:     #
                    410:     # Output selector
1.1       matthew   411:     ##
                    412:     ## Editing/loading/saving
1.3       matthew   413:     if ($allowed_to_edit) {
1.11      matthew   414:         my ($html,$action_message) = &file_dialogs($spreadsheet);
1.10      matthew   415:         if ($ENV{'form.makedefault'}) {
                    416:             $spreadsheet->make_default();
1.11      matthew   417:             if ($action_message) {
                    418:                 $action_message .= '<br />';
                    419:             }
                    420:             $action_message .= 'Made this spreadsheet the default';
                    421:             if ($sheettype eq 'classcalc') {
                    422:                 $action_message .= ' for the course';
                    423:             } elsif ($sheettype eq 'studentcalc') {
                    424:                 $action_message .= ' for all students';
                    425:             } elsif ($sheettype eq 'assesscalc') {
                    426:                 $action_message .= ' for all assessments';
                    427:             }
                    428:             $action_message .= '.';
1.10      matthew   429:         }
1.5       matthew   430:         $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
1.11      matthew   431:                   '<td valign="bottom">'.$html."</td></tr></table>\n");
1.12      matthew   432:         if ($action_message ne '') {
                    433:             $r->print(<<END);
1.11      matthew   434: <table>
                    435: <tr><td valign="top"><b>Last Action:</b></td>
                    436:     <td>&nbsp;</td>
                    437:     <td>$action_message</td>
                    438: </tr>
                    439: </table>
                    440: END
1.12      matthew   441:         }
1.1       matthew   442:         $r->rflush();
1.5       matthew   443:     } else {
                    444:         $r->print('<table><tr><td>'.$spreadsheet->html_header().
                    445:                   "</td></tr></table>\n");
1.1       matthew   446:     }
1.11      matthew   447:     $r->rflush();
1.1       matthew   448:     #
1.19      matthew   449:     $r->print("<table><tr>");
1.13      matthew   450:     if ($sheettype eq 'classcalc') {
1.19      matthew   451:         $r->print('<td><input type="submit" value="Generate Spreadsheet" />'.
                    452:                   '</td>');
                    453:     }
1.21      matthew   454:     if ($allowed_to_view) {
                    455:         $r->print('<td>'.
                    456:                   &Apache::loncommon::help_open_topic("Spreadsheet_About",
                    457:                                                       'Spreadsheet Help').
                    458:                   '</td>');
                    459:     }
                    460:     if ($allowed_to_edit) {
                    461:         $r->print('<td>'.
                    462:                   &Apache::loncommon::help_open_topic("Spreadsheet_Editing",
                    463:                                                       'Editing Help').
                    464:                   '</td>');
                    465:     }
1.19      matthew   466:     $r->print('</tr></table>');
1.10      matthew   467:     #
1.1       matthew   468:     # Keep track of the filename
                    469:     $r->print(&hiddenfield('filename',$filename));
                    470:     #
1.5       matthew   471:     # Keep track of the number of times we have been called, sort of.
                    472:     $r->print(&hiddenfield('not_first_run','whatever'));
                    473:     #
                    474:     if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
                    475:         $r->print($spreadsheet->get_html_title());
                    476:         if ($allowed_to_view || $allowed_to_edit) {
                    477:             $r->print($spreadsheet->parent_link());
                    478:         }
1.16      matthew   479:         $r->rflush();
1.5       matthew   480:         $spreadsheet->display($r);
1.1       matthew   481:     }
                    482:     $r->print('</form></body></html>');
                    483:     return OK;
                    484: }
                    485: 
                    486: 1;
                    487: 
                    488: __END__
                    489: 

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