--- loncom/auth/lonroles.pm 2016/08/08 13:39:26 1.269.2.25 +++ loncom/auth/lonroles.pm 2014/01/08 17:18:15 1.294 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # User Roles Screen # -# $Id: lonroles.pm,v 1.269.2.25 2016/08/08 13:39:26 raeburn Exp $ +# $Id: lonroles.pm,v 1.294 2014/01/08 17:18:15 bisitz Exp $ # # Copyright Michigan State University Board of Trustees # @@ -128,7 +128,7 @@ package Apache::lonroles; use strict; use Apache::lonnet; use Apache::lonuserstate(); -use Apache::Constants qw(:common REDIRECT); +use Apache::Constants qw(:common); use Apache::File(); use Apache::lonmenu; use Apache::loncommon; @@ -143,7 +143,7 @@ use Apache::lonrss; use GDBM_File; use LONCAPA qw(:DEFAULT :match); use HTML::Entities; - + sub redirect_user { my ($r,$title,$url,$msg) = @_; @@ -151,7 +151,6 @@ sub redirect_user { &Apache::loncommon::content_type($r,'text/html'); &Apache::loncommon::no_cache($r); $r->send_http_header; - my $swinfo=&Apache::lonmenu::rawconfig(); # Breadcrumbs my $brcrum = [{'href' => $url, @@ -166,11 +165,6 @@ sub redirect_user { $url=~s/ /\%20/g; $r->print(< -// -

$msg

$end_page ENDREDIR @@ -208,14 +202,6 @@ sub handler { my $r = shift; - # Check for critical messages and redirect if present. - my ($redirect,$url) = &Apache::loncommon::critical_redirect(300); - if ($redirect) { - &Apache::loncommon::content_type($r,'text/html'); - $r->header_out(Location => $url); - return REDIRECT; - } - my $now=time; my $then=$env{'user.login.time'}; my $refresh=$env{'user.refresh.time'}; @@ -229,7 +215,7 @@ sub handler { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}); -# -------------------------------------------------- Check if setting hot list +# -------------------------------------------------- Check if setting hot list my $hotlist; if ($env{'form.action'} eq 'verify_and_change_rolespref') { $hotlist = &Apache::lonpreferences::verify_and_change_rolespref($r); @@ -261,7 +247,7 @@ sub handler { my $envkey; my %dcroles = (); my $numdc = &check_fordc(\%dcroles,$update,$then); - my $loncaparev = $r->dir_config('lonVersion'); + my $loncaparev = $Apache::lonnet::perlvar{'lonVersion'}; # ================================================================== Roles Init if ($env{'form.selectrole'}) { @@ -292,18 +278,17 @@ sub handler { &Apache::lonnet::put('email_status',\%temp); &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'}); } - &Apache::lonnet::appenv({"request.course.id" => '', - "request.course.fn" => '', - "request.course.uri" => '', - "request.course.sec" => '', - "request.course.tied" => '', - "request.course.timechecked" => '', - "request.role" => 'cm', - "request.role.adv" => $env{'user.adv'}, - "request.role.domain" => $env{'user.domain'}}); + &Apache::lonnet::appenv({"request.course.id" => '', + "request.course.fn" => '', + "request.course.uri" => '', + "request.course.sec" => '', + "request.course.tied" => '', + "request.role" => 'cm', + "request.role.adv" => $env{'user.adv'}, + "request.role.domain" => $env{'user.domain'}}); # Check if user is a DC trying to enter a course or author space and needs privs to be created if ($numdc > 0) { - foreach my $envkey (keys(%env)) { + foreach my $envkey (keys %env) { # Is this an ad-hoc Coordinator role? if (my ($ccrole,$domain,$coursenum) = ($envkey =~ m-^form\.(cc|co)\./($match_domain)/($match_courseid)$-)) { @@ -373,7 +358,7 @@ sub handler { } } - foreach $envkey (keys(%env)) { + foreach $envkey (keys %env) { next if ($envkey!~/^user\.role\./); my ($where,$trolecode,$role,$tstatus,$tend,$tstart); &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where, @@ -389,13 +374,30 @@ sub handler { my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired'); if ($curr_reqd_hash{'internal.releaserequired'} ne '') { my ($switchserver,$switchwarning) = - &Apache::loncommon::check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode, - $curr_reqd_hash{'internal.releaserequired'}); + &check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode,$curr_reqd_hash{'internal.releaserequired'}); if ($switchwarning ne '' || $switchserver ne '') { &Apache::loncommon::content_type($r,'text/html'); &Apache::loncommon::no_cache($r); $r->send_http_header; - $r->print(&Apache::loncommon::check_release_result($switchwarning,$switchserver)); + my $end_page=&Apache::loncommon::end_page(); + $r->print(&Apache::loncommon::start_page('Selected course unavailable on this server'). + '

'); + if ($switchwarning) { + $r->print($switchwarning.'
'); + if (&Apache::loncommon::show_course()) { + $r->print(&mt('Display courses')); + } else { + $r->print(&mt('Display roles')); + } + $r->print(''); + } elsif ($switchserver) { + $r->print(&mt('This course requires a newer version of LON-CAPA than is installed on this server.'). + '
'. + ''. + &mt('Switch Server'). + ''); + } + $r->print('

'.&Apache::loncommon::end_page()); return OK; } } @@ -518,10 +520,10 @@ ENDENTERKEY if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) { my $msg; - my ($furl,$ferr)= - &Apache::lonuserstate::readmap($cdom.'/'.$cnum); + my ($furl,$ferr)= + &Apache::lonuserstate::readmap($cdom.'/'.$cnum); unless ($ferr) { - unless (($env{'form.switchrole'}) || + unless (($env{'form.switchrole'}) || ($env{"environment.internal.$cdom.$cnum.$role.adhoc"})) { &Apache::lonnet::put('nohist_crslastlogin', {$env{'user.name'}.':'.$env{'user.domain'}. @@ -544,7 +546,7 @@ ENDENTERKEY } if (($env{'form.orgurl'}) && ($env{'form.orgurl'}!~/^\/adm\/flip/) && - ($env{'form.orgurl'} ne '/adm/roles')) { + ($env{'form.orgurl'} ne '/adm/roles')) { my $dest=$env{'form.orgurl'}; if ($env{'form.symb'}) { if ($dest =~ /\?/) { @@ -589,8 +591,13 @@ ENDENTERKEY } else { # Check to see if the user is a CC entering a course # for the first time + my (undef, undef, $role, $courseid) = split(/\./, $envkey); + if (substr($courseid, 0, 1) eq '/') { + $courseid = substr($courseid, 1); + } + $courseid =~ s/\//_/; if ((($role eq 'cc') || ($role eq 'co')) - && ($env{'course.'.$cdom.'_'.$cnum.'.course.helper.not.run'})) { + && ($env{'course.' . $courseid .'.course.helper.not.run'})) { $furl = "/adm/helper/course.initialization.helper"; # Send the user to the course they selected } elsif ($env{'request.course.id'}) { @@ -610,7 +617,7 @@ ENDENTERKEY if ($env{'request.role.adv'}) { $dest = &Apache::lonenc::unencrypted($dest); if ($destsymb eq '') { - ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]*)/); + ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]*)/); $destsymb = &unescape($destsymb); } } @@ -635,14 +642,14 @@ ENDENTERKEY } } } - unless (($dest =~ m{^/enc/}) || ($dest =~ /(\?|\&)symb=.+___\d+___.+/)) { + unless (($dest =~ m{^/enc/}) || ($dest =~ /(\?|\&)symb=.+___\d+___.+/)) { if (($destsymb ne '') && ($destsymb !~ m{^/enc/})) { my $esc_symb = &escape($destsymb); $dest .= '?symb='.$esc_symb; } } &redirect_user($r, &mt('Entering [_1]', - $env{'course.'.$cdom.'_'.$cnum.'.description'}), + $env{'course.'.$courseid.'.description'}), $dest, $msg); return OK; } @@ -652,39 +659,25 @@ ENDENTERKEY $env{'request.course.id'}.'/' .$env{'request.course.sec'}) ) { - my $startpage = &courseloadpage($env{'request.course.id'}); + my $startpage = &courseloadpage($courseid); unless ($startpage eq 'firstres') { $msg = &mt('Entering [_1] ...', - $env{'course.'.$env{'request.course.id'}.'.description'}); + $env{'course.'.$courseid.'.description'}); &redirect_user($r, &mt('New in course'), '/adm/whatsnew?refpage=start', $msg); return OK; } } } - # Are we allowed to look at the first resource? - my $access; - if ($furl =~ m{^(/adm/wrapper|)/ext/}) { - # If it's an external resource, - # strip off the symb argument and possible query - my ($exturl,$symb) = ($furl =~ m{^(.+)(?:\?|\&)symb=(.+)$}); - # Unencode $symb - $symb = &unescape($symb); - # Then check for permission - $access = &Apache::lonnet::allowed('bre',$exturl,$symb); - # For other resources just check for permission - } else { - $access = &Apache::lonnet::allowed('bre',$furl); - } - if (!$access) { - $furl = &Apache::lonpageflip::first_accessible_resource(); - } elsif ($access eq 'B') { - $furl = '/adm/navmaps?showOnlyHomework=1'; - } +# Are we allowed to look at the first resource? + if ($furl !~ m|^/adm/|) { +# Guess not ... + $furl=&Apache::lonpageflip::first_accessible_resource(); + } $msg = &mt('Entering [_1] ...', - $env{'course.'.$cdom.'_'.$cnum.'.description'}); + $env{'course.'.$courseid.'.description'}); &redirect_user($r, &mt('Entering [_1]', - $env{'course.'.$cdom.'_'.$cnum.'.description'}), + $env{'course.'.$courseid.'.description'}), $furl, $msg); } return OK; @@ -738,7 +731,7 @@ ENDENTERKEY $crumbtext = 'Courses'; $pagetitle = 'My Courses'; $recent = &mt('Recent Courses'); - $standby = &mt('Course selected. Please stand by.'); + $standby = &mt('Course selected. Please stand by.'); } my $brcrum =[{href=>"/adm/roles",text=>$crumbtext}]; @@ -747,13 +740,8 @@ ENDENTERKEY my $swinfo=&Apache::lonmenu::rawconfig(); my $start_page=&Apache::loncommon::start_page($pagetitle,undef,{bread_crumbs=>$brcrum}); - my %domdefs=&Apache::lonnet::get_domain_defaults($env{'user.domain'}); - my $cattype = 'std'; - if ($domdefs{'catauth'}) { - $cattype = $domdefs{'catauth'}; - } - my $funcs = &get_roles_functions($showcount,$cattype); - &js_escape(\$standby); + my $funcs = &get_roles_functions($showcount); + $standby=~s/\n/\\n/g; my $noscript='
'.&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.').'
'.&mt('As this is not the case, most functionality in the system will be unavailable.').'

'; $r->print(<print($showresult); } elsif ($env{'form.state'} eq 'queued') { @@ -895,19 +883,17 @@ ENDHEADER \%sortrole,\%roleclass,\%futureroles,\%timezones,$loncaparev); $refresh = $now; &Apache::lonnet::appenv({'user.refresh.time' => $refresh}); - if ((($cattype eq 'std') || ($cattype eq 'domonly')) && (!$env{'user.adv'})) { + unless ($env{'user.adv'}) { if ($countactive > 0) { my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); $r->print( '

' - .&mt('[_1]Visit the [_2]Course/Community Catalog[_3][_4]' - .' to view all [_5] LON-CAPA courses and communities.' + .&mt('[_1]Visit the [_2]Course/Community Catalog[_3]' + .' to view all [_4] LON-CAPA courses and communities.' ,'' ,'' - ,'' - ,'' - ,'"'.$domdesc.'"') + ,'',$domdesc) .'
' .&mt('If a course or community is [_1]not[_2] in your list of current courses and communities below,' .' you may be able to enroll if self-enrollment is permitted.' @@ -919,7 +905,13 @@ ENDHEADER # No active roles if ($countactive==0) { - &requestcourse_advice($r,$cattype,$inrole); + if ($inrole) { + $r->print('

'.&mt('Currently no additional roles, courses or communities').'

'); + } else { + $r->print('

'.&mt('Currently no active roles, courses or communities').'

'); + } + &findcourse_advice($r); + &requestcourse_advice($r); $r->print(''); if ($countfuture) { $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture)); @@ -1060,7 +1052,11 @@ ENDHEADER my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); $r->print('

' .&mt('This LON-CAPA server is version [_1]',$r->dir_config('lonVersion')) - .'

'); + .'
' + .''.&mt('Logout').'  ' + .'' + .&mt('Course/Community Catalog') + .'

'); } $r->print(&Apache::loncommon::end_page()); return OK; @@ -1144,7 +1140,7 @@ sub gather_roles { if ($role =~ /^cr\//) { my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role); if ($tremark) { $tremark.='
'; } - $tremark.=&mt('Custom role defined by [_1].',$rauthor.':'.$rdomain); + $tremark.=&mt('Customrole defined by [_1].',$rauthor.':'.$rdomain); } $trole=Apache::lonnet::plaintext($role); my $ttype; @@ -1204,7 +1200,7 @@ sub gather_roles { my $required = $env{'course.'.$tcourseid.'.internal.releaserequired'}; if ($required ne '') { ($switchserver,$switchwarning) = - &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required); + &check_release_required($loncaparev,$tcourseid,$trolecode,$required); if ($switchserver || $switchwarning) { $button = 0; } @@ -1227,7 +1223,7 @@ sub gather_roles { my $required = $newhash{'internal.releaserequired'}; if ($required ne '') { ($switchserver,$switchwarning) = - &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required); + &check_release_required($loncaparev,$tcourseid,$trolecode,$required); if ($switchserver || $switchwarning) { $button = 0; } @@ -1403,7 +1399,7 @@ sub print_rolerows { } sub findcourse_advice { - my ($r,$cattype) = @_; + my ($r) = @_; my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) { @@ -1418,36 +1414,37 @@ sub findcourse_advice { } else { $r->print(&mt('If you were expecting to see an active role listed for a particular course, that course may not have been created yet.').'
'); } - if (($cattype eq 'std') || ($cattype eq 'domonly')) { - $r->print('

'.&mt('Self-Enrollment').'

'. - '

'.&mt('The [_1]Course/Community Catalog[_2] provides information about all [_3] classes for which LON-CAPA courses have been created, as well as any communities in the domain.','','',$domdesc).'
'); - $r->print(&mt('You can search for courses and communities which permit self-enrollment, if you would like to enroll in one.').'

'. - &Apache::loncoursequeueadmin::queued_selfenrollment()); - } + $r->print('

'.&mt('Self-Enrollment').'

'. + '

'.&mt('The [_1]Course/Community Catalog[_2] provides information about all [_3] classes for which LON-CAPA courses have been created, as well as any communities in the domain.','','',$domdesc).'
'); + $r->print(&mt('You can search for courses and communities which permit self-enrollment, if you would like to enroll in one.').'

'. + &Apache::loncoursequeueadmin::queued_selfenrollment()); return; } sub requestcourse_advice { - my ($r,$cattype,$inrole) = @_; + my ($r) = @_; my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); - my (%can_request,%request_doms,$output); + my (%can_request,%request_doms); &Apache::lonnet::check_can_request($env{'user.domain'},\%can_request,\%request_doms); if (keys(%request_doms) > 0) { my ($types,$typename) = &Apache::loncommon::course_types(); if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) { + $r->print('

'.&mt('Request creation of a course or community').'

'. + '

'.&mt('You have rights to request the creation of courses and/or communities in the following domain(s):').'

'.&mt('Use the [_1]request form[_2] to submit a request for creation of a new course or community.','','').'

'); } - &findcourse_advice($r,$cattype); } return; } @@ -1622,6 +1602,24 @@ sub build_roletext { return ($roletext,$roletext_end); } +sub check_needs_switchserver { + my ($possiblerole) = @_; + my $needs_switchserver; + my ($role,$where) = split(/\./,$possiblerole,2); + my (undef,$tdom,$twho) = split(/\//,$where); + my ($server_status,$home); + if (($role eq 'ca') || ($role eq 'aa')) { + ($server_status,$home) = &check_author_homeserver($twho,$tdom); + } else { + ($server_status,$home) = &check_author_homeserver($env{'user.name'}, + $env{'user.domain'}); + } + if ($server_status eq 'switchserver') { + $needs_switchserver = 1; + } + return $needs_switchserver; +} + sub check_author_homeserver { my ($uname,$udom)=@_; if (($uname eq '') || ($udom eq '')) { @@ -1643,7 +1641,7 @@ sub check_fordc { my ($dcroles,$update,$then) = @_; my $numdc = 0; if ($env{'user.adv'}) { - foreach my $envkey (sort(keys(%env))) { + foreach my $envkey (sort keys %env) { if ($envkey=~/^user\.role\.dc\.\/($match_domain)\/$/) { my $dcdom = $1; my $livedc = 1; @@ -1687,7 +1685,7 @@ sub adhoc_course_role { } } if ($setprivs) { - if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)(.*?)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) { + if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) { my $role = $1; my $custom_role = $2; my $usec = $3; @@ -1752,6 +1750,58 @@ sub check_forcc { return $is_cc; } +sub check_release_required { + my ($loncaparev,$tcourseid,$trolecode,$required) = @_; + my ($switchserver,$warning); + if ($required ne '') { + my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/); + my ($major,$minor) = ($loncaparev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/); + if ($reqdmajor ne '' && $reqdminor ne '') { + my $otherserver; + if (($major eq '' && $minor eq '') || + (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) { + my ($userdomserver) = &Apache::lonnet::choose_server($env{'user.domain'}); + my $switchlcrev = + &Apache::lonnet::get_server_loncaparev($env{'user.domain'}, + $userdomserver); + my ($swmajor,$swminor) = ($switchlcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/); + if (($swmajor eq '' && $swminor eq '') || ($reqdmajor > $swmajor) || + (($reqdmajor == $swmajor) && ($reqdminor > $swminor))) { + my $cdom = $env{'course.'.$tcourseid.'.domain'}; + if ($cdom ne $env{'user.domain'}) { + my ($coursedomserver,$coursehostname) = &Apache::lonnet::choose_server($cdom); + my $serverhomeID = &Apache::lonnet::get_server_homeID($coursehostname); + my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID); + my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom); + my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'}); + my $remoterev = &Apache::lonnet::get_server_loncaparev($serverhomedom,$coursedomserver); + my $canhost = + &Apache::lonnet::can_host_session($env{'user.domain'}, + $coursedomserver, + $remoterev, + $udomdefaults{'remotesessions'}, + $defdomdefaults{'hostedsessions'}); + + if ($canhost) { + $otherserver = $coursedomserver; + } else { + $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$tcourseid.'.internal.releaserequired'}).'
'. &mt("No suitable server could be found amongst servers in either your own domain or in the course's domain."); + } + } else { + $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$tcourseid.'.internal.releaserequired'}).'
'.&mt("No suitable server could be found amongst servers in your own domain (which is also the course's domain)."); + } + } else { + $otherserver = $userdomserver; + } + } + if ($otherserver ne '') { + $switchserver = 'otherserver='.$otherserver.'&role='.$trolecode; + } + } + } + return ($switchserver,$warning); +} + sub courselink { my ($dcdom,$rowtype) = @_; my $courseform=&Apache::loncommon::selectcourse_link @@ -1766,11 +1816,10 @@ sub courselink { } sub coursepick_jscript { - my %js_lt = &Apache::lonlocal::texthash( + my %lt = &Apache::lonlocal::texthash( plsu => "Please use the 'Select Course/Community' link to open a separate pick course window where you may select the course or community you wish to enter.", youc => 'You can only use this screen to select courses and communities in the current domain.', ); - &js_escape(\%js_lt); my $verify_script = <<"END";