--- loncom/auth/lonroles.pm 2014/01/08 17:18:15 1.294 +++ loncom/auth/lonroles.pm 2015/06/09 21:22:44 1.312 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # User Roles Screen # -# $Id: lonroles.pm,v 1.294 2014/01/08 17:18:15 bisitz Exp $ +# $Id: lonroles.pm,v 1.312 2015/06/09 21:22:44 damieng 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); +use Apache::Constants qw(:common REDIRECT); use Apache::File(); use Apache::lonmenu; use Apache::loncommon; @@ -202,6 +202,14 @@ 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'}; @@ -247,7 +255,7 @@ sub handler { my $envkey; my %dcroles = (); my $numdc = &check_fordc(\%dcroles,$update,$then); - my $loncaparev = $Apache::lonnet::perlvar{'lonVersion'}; + my $loncaparev = $r->dir_config('lonVersion'); # ================================================================== Roles Init if ($env{'form.selectrole'}) { @@ -278,17 +286,18 @@ 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.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.course.timechecked" => '', + "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)$-)) { @@ -358,7 +367,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, @@ -374,30 +383,13 @@ sub handler { my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired'); if ($curr_reqd_hash{'internal.releaserequired'} ne '') { my ($switchserver,$switchwarning) = - &check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode,$curr_reqd_hash{'internal.releaserequired'}); + &Apache::loncommon::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; - 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()); + $r->print(&Apache::loncommon::check_release_result($switchwarning,$switchserver)); return OK; } } @@ -591,13 +583,8 @@ 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.' . $courseid .'.course.helper.not.run'})) { + && ($env{'course.'.$cdom.'_'.$cnum.'.course.helper.not.run'})) { $furl = "/adm/helper/course.initialization.helper"; # Send the user to the course they selected } elsif ($env{'request.course.id'}) { @@ -649,7 +636,7 @@ ENDENTERKEY } } &redirect_user($r, &mt('Entering [_1]', - $env{'course.'.$courseid.'.description'}), + $env{'course.'.$cdom.'_'.$cnum.'.description'}), $dest, $msg); return OK; } @@ -659,25 +646,39 @@ ENDENTERKEY $env{'request.course.id'}.'/' .$env{'request.course.sec'}) ) { - my $startpage = &courseloadpage($courseid); + my $startpage = &courseloadpage($env{'request.course.id'}); unless ($startpage eq 'firstres') { $msg = &mt('Entering [_1] ...', - $env{'course.'.$courseid.'.description'}); + $env{'course.'.$env{'request.course.id'}.'.description'}); &redirect_user($r, &mt('New in course'), '/adm/whatsnew?refpage=start', $msg); return OK; } } } -# Are we allowed to look at the first resource? - if ($furl !~ m|^/adm/|) { -# Guess not ... - $furl=&Apache::lonpageflip::first_accessible_resource(); - } + # 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'; + } $msg = &mt('Entering [_1] ...', - $env{'course.'.$courseid.'.description'}); + $env{'course.'.$cdom.'_'.$cnum.'.description'}); &redirect_user($r, &mt('Entering [_1]', - $env{'course.'.$courseid.'.description'}), + $env{'course.'.$cdom.'_'.$cnum.'.description'}), $furl, $msg); } return OK; @@ -740,8 +741,13 @@ ENDENTERKEY my $swinfo=&Apache::lonmenu::rawconfig(); my $start_page=&Apache::loncommon::start_page($pagetitle,undef,{bread_crumbs=>$brcrum}); - my $funcs = &get_roles_functions($showcount); - $standby=~s/\n/\\n/g; + 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 $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(< $refresh}); - unless ($env{'user.adv'}) { + if ((($cattype eq 'std') || ($cattype eq 'domonly')) && (!$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]' - .' to view all [_4] LON-CAPA courses and communities.' + .&mt('[_1]Visit the [_2]Course/Community Catalog[_3][_4]' + .' to view all [_5] 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.' @@ -905,13 +913,7 @@ ENDHEADER # No active roles if ($countactive==0) { - 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); + &requestcourse_advice($r,$cattype,$inrole); $r->print(''); if ($countfuture) { $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture)); @@ -1052,11 +1054,7 @@ 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; @@ -1140,7 +1138,7 @@ sub gather_roles { if ($role =~ /^cr\//) { my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role); if ($tremark) { $tremark.='
'; } - $tremark.=&mt('Customrole defined by [_1].',$rauthor.':'.$rdomain); + $tremark.=&mt('Custom role defined by [_1].',$rauthor.':'.$rdomain); } $trole=Apache::lonnet::plaintext($role); my $ttype; @@ -1200,7 +1198,7 @@ sub gather_roles { my $required = $env{'course.'.$tcourseid.'.internal.releaserequired'}; if ($required ne '') { ($switchserver,$switchwarning) = - &check_release_required($loncaparev,$tcourseid,$trolecode,$required); + &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required); if ($switchserver || $switchwarning) { $button = 0; } @@ -1223,7 +1221,7 @@ sub gather_roles { my $required = $newhash{'internal.releaserequired'}; if ($required ne '') { ($switchserver,$switchwarning) = - &check_release_required($loncaparev,$tcourseid,$trolecode,$required); + &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required); if ($switchserver || $switchwarning) { $button = 0; } @@ -1399,7 +1397,7 @@ sub print_rolerows { } sub findcourse_advice { - my ($r) = @_; + my ($r,$cattype) = @_; 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'})) { @@ -1414,37 +1412,36 @@ 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.').'
'); } - $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()); + 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()); + } return; } sub requestcourse_advice { - my ($r) = @_; + my ($r,$cattype,$inrole) = @_; my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); - my (%can_request,%request_doms); + my (%can_request,%request_doms,$output); &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.','','').'

'); + } + if ($output) { + $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.', + '',''). + '

'); + } + } + } elsif (!$env{'user.adv'}) { + 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,$cattype); } return; } @@ -1602,24 +1616,6 @@ 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 '')) { @@ -1641,7 +1637,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; @@ -1685,7 +1681,7 @@ sub adhoc_course_role { } } if ($setprivs) { - if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) { + if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)(.*?)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) { my $role = $1; my $custom_role = $2; my $usec = $3; @@ -1750,58 +1746,6 @@ 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 @@ -1816,10 +1760,11 @@ sub courselink { } sub coursepick_jscript { - my %lt = &Apache::lonlocal::texthash( + my %js_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";