--- loncom/interface/Attic/lonspreadsheet.pm 2003/01/13 21:52:11 1.162 +++ loncom/interface/Attic/lonspreadsheet.pm 2003/02/13 19:07:46 1.168 @@ -1,5 +1,5 @@ # -# $Id: lonspreadsheet.pm,v 1.162 2003/01/13 21:52:11 matthew Exp $ +# $Id: lonspreadsheet.pm,v 1.168 2003/02/13 19:07:46 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -50,12 +50,434 @@ built-in functions. =cut + package Apache::lonspreadsheet; use strict; use Apache::Constants qw(:common :http); use Apache::lonnet; use Apache::lonhtmlcommon; +use HTML::Entities(); + +# --------------------------------------------------------- Various form fields + +sub textfield { + my ($title,$name,$value)=@_; + return "\n

$title:
". + ''; +} + +sub hiddenfield { + my ($name,$value)=@_; + return "\n".''; +} + +sub selectbox { + my ($title,$name,$value,%options)=@_; + my $selout="\n

$title:
".''; +} + +my %oldsheets; +my %loadedcaches; + +# ================================================================ Main handler +# +# Interactive call to screen +# +# +sub handler { + my $r=shift; + + my ($sheettype) = ($r->uri=~/\/(\w+)$/); + + if (! exists($ENV{'form.Status'})) { + $ENV{'form.Status'} = 'Active'; + } + if ( ! exists($ENV{'form.output'}) || + ($sheettype ne 'classcalc' && + lc($ENV{'form.output'}) eq 'recursive excel')) { + $ENV{'form.output'} = 'HTML'; + } + # + # Overload checking + # + # Check this server + my $loaderror=&Apache::lonnet::overloaderror($r); + if ($loaderror) { return $loaderror; } + # Check the course homeserver + $loaderror= &Apache::lonnet::overloaderror($r, + $ENV{'course.'.$ENV{'request.course.id'}.'.home'}); + if ($loaderror) { return $loaderror; } + # + # HTML Header + # + if ($r->header_only) { + $r->content_type('text/html'); + $r->send_http_header; + return OK; + } + # + # Roles Checking + # + # Needs to be in a course + if (! $ENV{'request.course.fn'}) { + # Not in a course, or not allowed to modify parms + $ENV{'user.error.msg'}= + $r->uri.":opa:0:0:Cannot modify spreadsheet"; + return HTTP_NOT_ACCEPTABLE; + } + # + # Get query string for limited number of parameters + # + &Apache::loncommon::get_unprocessed_cgi + ($ENV{'QUERY_STRING'},['uname','udom','usymb','ufn','mapid','resid']); + # + # Deal with restricted student permissions + # + if ($ENV{'request.role'} =~ /^st\./) { + delete $ENV{'form.unewfield'} if (exists($ENV{'form.unewfield'})); + delete $ENV{'form.unewformula'} if (exists($ENV{'form.unewformula'})); + } + # + # Look for special assessment spreadsheets - '_feedback', etc. + # + if (($ENV{'form.usymb'}=~/^\_(\w+)/) && (!$ENV{'form.ufn'} || + $ENV{'form.ufn'} eq '' || + $ENV{'form.ufn'} eq 'default')) { + $ENV{'form.ufn'}='default_'.$1; + } + if (!$ENV{'form.ufn'} || $ENV{'form.ufn'} eq 'default') { + $ENV{'form.ufn'}='course_default_'.$sheettype; + } + # + # Interactive loading of specific sheet? + # + if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) { + $ENV{'form.ufn'}=$ENV{'form.loadthissheet'}; + } + # + # Determine the user name and domain for the sheet. + my $aname; + my $adom; + unless ($ENV{'form.uname'}) { + $aname=$ENV{'user.name'}; + $adom=$ENV{'user.domain'}; + } else { + $aname=$ENV{'form.uname'}; + $adom=$ENV{'form.udom'}; + } + # + # Open page, try to prevent browser cache. + # + $r->content_type('text/html'); + $r->header_out('Cache-control','no-cache'); + $r->header_out('Pragma','no-cache'); + $r->send_http_header; + # + # Header.... + # + $r->print('LON-CAPA Spreadsheet'); + my $nothing = "''"; + if ($ENV{'browser.type'} eq 'explorer') { + $nothing = "'javascript:void(0);'"; + } + + if ($ENV{'request.role'} !~ /^st\./) { + $r->print(< + + var editwin; + + function celledit(cellname,cellformula) { + var edit_text = ''; + // cellformula may contain less-than and greater-than symbols, so + // we need to escape them? + edit_text +='Cell Edit Window'; + edit_text += '

'; + edit_text += '

Cell '+cellname+'

'; + edit_text += ''; - edit_text += '
'; - edit_text += ''; - edit_text += '      '; - edit_text += ' -ENDSCRIPT - } - $r->print(''.&Apache::loncommon::bodytag('Grades Spreadsheet'). - ''); - $r->print(&hiddenfield('uname',$ENV{'form.uname'}). - &hiddenfield('udom',$ENV{'form.udom'}). - &hiddenfield('usymb',$ENV{'form.usymb'}). - &hiddenfield('unewfield',''). - &hiddenfield('unewformula','')); - $r->rflush(); - # - # Full recalc? - # - if ($ENV{'form.forcerecalc'}) { - $r->print('

Completely Recalculating Sheet ...

'); - undef %spreadsheets; - undef %courserdatas; - undef %userrdatas; - undef %defaultsheets; - undef %rowlabel_cache; - } - # Read new sheet or modified worksheet - my ($sheet)=&makenewsheet($aname,$adom,$sheettype,$ENV{'form.usymb'}); - # - # Check user permissions - if (($sheet->{'sheettype'} eq 'classcalc' ) || - ($sheet->{'uname'} ne $ENV{'user.name'} ) || - ($sheet->{'udom'} ne $ENV{'user.domain'})) { - unless (&Apache::lonnet::allowed('vgr',$sheet->{'cid'})) { - $r->print('

Access Permission Denied

'. - ''); - return OK; - } - } - # Print out user information - $r->print('

'.$sheet->{'coursedesc'}.'

'); - if ($sheet->{'sheettype'} ne 'classcalc') { - $r->print('

'.&gettitle($sheet).'

'); - } - if ($sheet->{'sheettype'} eq 'assesscalc') { - $r->print('User: '.$sheet->{'uname'}. - '
Domain: '.$sheet->{'udom'}.'
'); - } - if ($sheet->{'sheettype'} eq 'studentcalc' || - $sheet->{'sheettype'} eq 'assesscalc') { - $r->print('Section/Group:'.$sheet->{'csec'}.'

'); - } - # - # If a new formula had been entered, go from work copy - if ($ENV{'form.unewfield'}) { - $r->print('

Modified Workcopy

'); - #$ENV{'form.unewformula'}=~s/\'/\"/g; - $r->print('

Cell '.$ENV{'form.unewfield'}.' =

');
-        $r->print(&HTML::Entities::encode($ENV{'form.unewformula'}).
-                  '

'); - $sheet->{'filename'} = $ENV{'form.ufn'}; - &tmpread($sheet,$ENV{'form.unewfield'},$ENV{'form.unewformula'}); - } elsif ($ENV{'form.saveas'}) { - $sheet->{'filename'} = $ENV{'form.ufn'}; - &tmpread($sheet); - } else { - &readsheet($sheet,$ENV{'form.ufn'}); - } - # Additional options - if ($sheet->{'sheettype'} eq 'assesscalc') { - $r->print('

'. - ''. - 'Level up: Student Sheet

'); - } - if (($sheet->{'sheettype'} eq 'studentcalc') && - (&Apache::lonnet::allowed('vgr',$sheet->{'cid'}))) { - $r->print ('

'. - 'Level up: Course Sheet

'); - } - # Recalc button - $r->print('
'. - '

'); - # Save dialog - if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) { - my $fname=$ENV{'form.ufn'}; - $fname=~s/\_[^\_]+$//; - if ($fname eq 'default') { $fname='course_default'; } - $r->print(''. - ''. - 'make default:

'); - } - $r->print(&hiddenfield('ufn',$sheet->{'filename'})); - # Load dialog - if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) { - $r->print('

'. - '

'); - if ($sheet->{'sheettype'} eq 'studentcalc') { - &setothersheets($sheet, - &othersheets($sheet,'assesscalc')); - } - } - # - # Set up caching mechanisms - # - &load_spreadsheet_expirationdates(); - # Clear out old caches if we have not seen this class before. - if (exists($oldsheets{'course'}) && - $oldsheets{'course'} ne $sheet->{'cid'}) { - undef %oldsheets; - undef %loadedcaches; - } - $oldsheets{'course'} = $sheet->{'cid'}; - # - if ($sheet->{'sheettype'} eq 'classcalc') { - $r->print("Loading previously calculated student sheets ...\n"); - $r->rflush(); - &cachedcsheets(); - } elsif ($sheet->{'sheettype'} eq 'studentcalc') { - $r->print("Loading previously calculated assessment sheets ...\n"); - $r->rflush(); - &cachedssheets($sheet); - } - # Update sheet, load rows - $r->print("Loaded sheet(s), updating rows ...
\n"); - $r->rflush(); - # - &updatesheet($sheet); - $r->print("Updated rows, loading row data ...\n"); - $r->rflush(); - # - &loadrows($sheet,$r); - $r->print("Loaded row data, calculating sheet ...
\n"); - $r->rflush(); - # - my $calcoutput=&calcsheet($sheet); - $r->print('

'.$calcoutput.'

'); - # See if something to save - if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) { - my $fname=''; - if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) { - $fname=~s/\W/\_/g; - if ($fname eq 'default') { $fname='course_default'; } - $fname.='_'.$sheet->{'sheettype'}; - $sheet->{'filename'} = $fname; - $ENV{'form.ufn'}=$fname; - $r->print('

Saving spreadsheet: '. - &writesheet($sheet,$ENV{'form.makedefufn'}). - '

'); - } - } - # - # Write the modified worksheet - $r->print('Current sheet: '.$sheet->{'filename'}.'

'); - &tmpwrite($sheet); - if ($sheet->{'sheettype'} eq 'assesscalc') { - $r->print('

Show rows with empty A column: '); - } else { - $r->print('

Show empty rows: '); - } - # - $r->print(&hiddenfield('userselhidden','true'). - 'print(' checked'); - } else { - unless ($ENV{'form.userselhidden'}) { - unless - ($ENV{'course.'.$sheet->{'cid'}.'.hideemptyrows'} eq 'yes') { - $r->print(' checked'); - $ENV{'form.showall'}=1; - } - } - } - $r->print('>'); - # - # output format select box - $r->print(' Output as \n"); - # - if ($sheet->{'sheettype'} eq 'classcalc') { - $r->print(' Student Status: '. - &Apache::lonhtmlcommon::StatusOptions - ($ENV{'form.Status'},'sheet')); - } - # - # Buttons to insert rows -# $r->print(< -# -#
-#ENDINSERTBUTTONS - # Print out sheet - &outsheet($sheet,$r); - $r->print(''); - # Done - return OK; -} - 1; + __END__