--- loncom/interface/lonmenu.pm 2012/10/29 17:38:56 1.388 +++ loncom/interface/lonmenu.pm 2012/10/31 12:54:13 1.389 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines to control the menu # -# $Id: lonmenu.pm,v 1.388 2012/10/29 17:38:56 raeburn Exp $ +# $Id: lonmenu.pm,v 1.389 2012/10/31 12:54:13 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -523,7 +523,7 @@ sub innerregister { ### my $editbutton = ''; my $noeditbutton = 1; - my ($cnum,$cdom); + my ($cnum,$cdom,$cfile,$switchserver,$home,$uploaded); if ($env{'request.course.id'}) { $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; @@ -555,95 +555,50 @@ sub innerregister { my $home = &Apache::lonnet::homeserver($caname,$cadom); my $allowed=0; my @ids=&Apache::lonnet::current_machine_ids(); - foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } } - if (!$allowed) { + if (($home ne 'no_host') && (grep(/^\Q$home\E$/,@ids))) { $editbutton=&switch('','',6,1,$top,,$bottom,$action,$desc); - $noeditbutton = 0; } - } + } else { # # We are an author for some stuff, but currently do not have the role of author. # Figure out if we have authoring privileges for the resource we are looking at. -# This should maybe become a privilege check in lonnet # - ## - ## Determine if user can edit url. - ## - my $cfile=''; - my $cfuname=''; - my $cfudom=''; - my $uploaded; - my $switchserver=''; - my $home; - if ($env{'request.filename'}) { - my $file=&Apache::lonnet::declutter($env{'request.filename'}); - if (defined($cnum) && defined($cdom)) { - $uploaded = &is_course_upload($file,$cnum,$cdom); - } - if (!$uploaded) { - - $file=~s{^(priv/$match_domain/$match_username)}{/$1}; - $file=~s{^($match_domain/$match_username)}{/priv/$1}; - - # Check that the user has permission to edit this resource - my $setpriv = 1; - ($cfuname,$cfudom)=&Apache::lonnet::constructaccess($file,$setpriv); - if (defined($cfudom)) { - $home=&Apache::lonnet::homeserver($cfuname,$cfudom); - my $allowed=0; - my @ids=&Apache::lonnet::current_machine_ids(); - foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } } - if ($allowed) { - $cfile=$file; - } else { - $switchserver=$file; + my ($cfile,$switchserver,$home,$uploaded); + if ($env{'request.filename'}) { + my $file=&Apache::lonnet::declutter($env{'request.filename'}); + ## + ## Determine if user can edit url. + ## + ($cfile,$home,$switchserver,$uploaded) = + &Apache::lonnet::can_edit_resource($file,$cnum,$cdom); + } + # Turn the button on or off + if (($cfile) && ($home ne '') && ($home ne 'no_host')) { + my $nocrsedit; + # Suppress display where CC has switched to student role. + if ($env{'request.course.id'}) { + unless(&Apache::lonnet::allowed('mdc', + $env{'request.course.id'})) { + $nocrsedit = 1; } } - } - } - # Finally, turn the button on or off - if (($cfile || $switchserver) && !$const_space) { - my $nocrsedit; - # Suppress display where CC has switched to student role. - if ($env{'request.course.id'}) { - unless(&Apache::lonnet::allowed('mdc', - $env{'request.course.id'})) { - $nocrsedit = 1; + if ($nocrsedit) { + $editbutton=&clear(6,1); + } else { + $editbutton = &get_editbutton($cfile,$home,$switchserver,$uploaded); } - } - if ($nocrsedit) { + } elsif ($editbutton eq '') { $editbutton=&clear(6,1); - } else { - my $bot = "go('$cfile')"; - if ($switchserver) { - if ( $env{'request.symb'} && $env{'request.course.id'} ) { - $cfile = '/adm/switchserver?otherserver='.$home.'&role='. - &HTML::Entities::encode($env{'request.role'},'"<>&').'&symb='. - &HTML::Entities::encode($env{'request.symb'},'"<>&'); - $bot = "need_switchserver('$cfile');"; - } - } - $editbutton=&switch - ('','',6,1,'pcstr.png','Edit','resource[_2]', - $bot,"Edit this resource"); - $noeditbutton = 0; } - } elsif ($editbutton eq '') { - $editbutton=&clear(6,1); } - } - if (($noeditbutton) && ($env{'request.filename'})) { + } elsif (($env{'request.filename'}) && $env{'request.course.id'}) { if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) { my $file=&Apache::lonnet::declutter($env{'request.filename'}); - if (defined($cnum) && defined($cdom)) { - if (&is_course_upload($file,$cnum,$cdom)) { - my $cfile = &edit_course_upload($file,$cnum,$cdom); - if ($cfile) { - $editbutton=&switch - ('','',6,1,'pcstr.png','Edit', - 'resource[_2]',"go('".$cfile."');", - 'Edit this resource'); - } + if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) { + my ($cfile,$home,$switchserver,$uploaded) = + &Apache::lonnet::can_edit_resource($file,$cnum,$cdom); + if (($uploaded) && ($cfile ne '')) { + $editbutton = &get_editbutton($cfile,$home,$switchserver,$uploaded); } } } @@ -829,40 +784,14 @@ ENDMENUITEMS . &Apache::lonhtmlcommon::scripttag('', 'end'); } -sub is_course_upload { - my ($file,$cnum,$cdom) = @_; - my $uploadpath = &LONCAPA::propath($cdom,$cnum); - $uploadpath =~ s{^\/}{}; - if (($file =~ m{^\Q$uploadpath\E/userfiles/docs/}) || - ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/docs/})) { - return 1; +sub get_editbutton { + my ($cfile,$home,$switchserver,$uploaded) = @_; + my $jscall = &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,$switchserver, + $uploaded,$env{'request.symb'}); + if ($jscall) { + return &switch('','',6,1,'pcstr.png','Edit','resource[_2]', + $jscall,"Edit this resource"); } - return; -} - -sub edit_course_upload { - my ($file,$cnum,$cdom) = @_; - my $cfile; - if ($file =~/\.(htm|html|css|js|txt)$/) { - my $ext = $1; - my $url = &Apache::lonnet::hreflocation('',$file); - my $home = &Apache::lonnet::homeserver($cnum,$cdom); - my @ids=&Apache::lonnet::current_machine_ids(); - my $dest; - if ($home && grep(/^\Q$home\E$/,@ids)) { - $dest = $url.'?forceedit=1'; - } else { - unless (&Apache::lonnet::get_locks()) { - $dest = '/adm/switchserver?otherserver='. - $home.'&role='.$env{'request.role'}. - '&url='.$url.'&forceedit=1'; - } - } - if ($dest) { - $cfile = &HTML::Entities::encode($dest,'"<>&'); - } - } - return $cfile; } # ================================================================== Raw Config @@ -1264,8 +1193,7 @@ sub utilityfunctions { my $end_page_annotate = &Apache::loncommon::end_page({'js_ready' => 1}); - my $confirm_switch = &mt("Editing requires switching to the resource's home server.").'\n'. - &mt('Switch server?'); + my $jumptores = &Apache::lonhtmlcommon::javascript_jumpto_resource(); my $esc_url=&escape($currenturl); my $esc_symb=&escape($currentsymb); @@ -1280,22 +1208,7 @@ return (<