--- loncom/auth/lonroles.pm 2009/05/18 15:40:20 1.223
+++ loncom/auth/lonroles.pm 2011/02/24 23:29:10 1.260
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# User Roles Screen
#
-# $Id: lonroles.pm,v 1.223 2009/05/18 15:40:20 raeburn Exp $
+# $Id: lonroles.pm,v 1.260 2011/02/24 23:29:10 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -57,8 +57,7 @@ course they should act on, etc. Both in
handler determines via C's C<&allowed> function that a certain
action is not allowed, C is used as error handler. This
allows the user to select another role which may have permission to do
-what they were trying to do. C can also be accessed via the
-B button in the Remote Control.
+what they were trying to do.
=begin latex
@@ -138,27 +137,25 @@ use Apache::lonannounce;
use Apache::lonlocal;
use Apache::lonpageflip();
use Apache::lonnavdisplay();
+use Apache::loncoursequeueadmin;
use GDBM_File;
use LONCAPA qw(:DEFAULT :match);
use HTML::Entities;
sub redirect_user {
- my ($r,$title,$url,$msg,$launch_nav) = @_;
+ my ($r,$title,$url,$msg) = @_;
$msg = $title if (! defined($msg));
&Apache::loncommon::content_type($r,'text/html');
&Apache::loncommon::no_cache($r);
$r->send_http_header;
- my $swinfo=&Apache::lonmenu::rawconfig();
- my $navwindow;
- if ($launch_nav eq 'on') {
- $navwindow.=&Apache::lonnavdisplay::launch_win('now',undef,undef,
- ($url =~ m-^/adm/whatsnew-));
- } else {
- $navwindow.=&Apache::lonnavmaps::close();
- }
+
+ # Breadcrumbs
+ my $brcrum = [{'href' => $url,
+ 'text' => 'Switching Role'},];
my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
- {'redirect' => [1,$url],});
+ {'redirect' => [1,$url],
+ 'bread_crumbs' => $brcrum,});
my $end_page = &Apache::loncommon::end_page();
# Note to style police:
@@ -166,10 +163,6 @@ sub redirect_user {
$url=~s/ /\%20/g;
$r->print(<
-$swinfo
-
-$navwindow
$msg
$end_page
ENDREDIR
@@ -182,13 +175,25 @@ sub error_page {
&Apache::loncommon::no_cache($r);
$r->send_http_header;
return OK if $r->header_only;
- $r->print(&Apache::loncommon::start_page('Problems during Course Initialization').
- ''.
- ''.&mt('The following problems occurred:').
+ # Breadcrumbs
+ my $brcrum = [{'href' => $dest,
+ 'text' => 'Problems during Course Initialization'},];
+ $r->print(&Apache::loncommon::start_page('Problems during Course Initialization',
+ undef,
+ {'bread_crumbs' => $brcrum,})
+ );
+ $r->print(
+ ''.
+ '
'.&mt('The following problems occurred:').
+ '
'.
$error.
- '
'.&mt('Continue').''.
- &Apache::loncommon::end_page());
+ '
'.&mt('Continue').''
+ );
+ $r->print(&Apache::loncommon::end_page());
}
sub handler {
@@ -197,10 +202,36 @@ sub handler {
my $now=time;
my $then=$env{'user.login.time'};
+ my $refresh=$env{'user.refresh.time'};
+ my $update=$env{'user.update.time'};
+ if (!$refresh) {
+ $refresh = $then;
+ }
+ if (!$update) {
+ $update = $then;
+ }
+
+# -------------------------------------------------------- Check for new roles
+ my $updateresult;
+ if ($env{'form.doupdate'}) {
+ my $show_course=&Apache::loncommon::show_course();
+ my $checkingtxt;
+ if ($show_course) {
+ $checkingtxt = &mt('Checking for new courses ...');
+ } else {
+ $checkingtxt = &mt('Checking for new roles ...');
+ }
+ $updateresult = ''.$checkingtxt.'
';
+ $updateresult .= &update_session_roles();
+ &Apache::lonnet::appenv({'user.update.time' => $now});
+ $update = $now;
+ }
+
my $envkey;
my %dcroles = ();
- my $numdc = &check_fordc(\%dcroles,$then);
+ my $numdc = &check_fordc(\%dcroles,$update,$then);
&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
+ my $loncaparev = $Apache::lonnet::perlvar{'lonVersion'};
# ================================================================== Roles Init
if ($env{'form.selectrole'}) {
@@ -214,8 +245,17 @@ sub handler {
if ($env{'request.course.id'}) {
# Check if user is CC trying to select a course role
if ($env{'form.switchrole'}) {
- if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
- &adhoc_course_role($then);
+ my $switch_is_active;
+ if (defined($env{'user.role.'.$env{'form.switchrole'}})) {
+ my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
+ if (!$end || $end > $now) {
+ if (!$start || $start < $update) {
+ $switch_is_active = 1;
+ }
+ }
+ }
+ unless ($switch_is_active) {
+ &adhoc_course_role($refresh,$update,$then);
}
}
my %temp=('logout_'.$env{'request.course.id'} => time);
@@ -232,12 +272,12 @@ sub handler {
# 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) {
-# Is this an ad-hoc CC-role?
- if (my ($domain,$coursenum) =
- ($envkey =~ m-^form\.cc\./($match_domain)/($match_courseid)$-)) {
+# Is this an ad-hoc Coordinator role?
+ if (my ($ccrole,$domain,$coursenum) =
+ ($envkey =~ m-^form\.(cc|co)\./($match_domain)/($match_courseid)$-)) {
if ($dcroles{$domain}) {
&Apache::lonnet::check_adhoc_privs($domain,$coursenum,
- $then,$now,'cc');
+ $update,$refresh,$now,$ccrole);
}
last;
}
@@ -250,7 +290,7 @@ sub handler {
my ($server_status,$home) = &check_author_homeserver($user,$domain);
if ($server_status eq 'switchserver') {
my $trolecode = 'au./'.$domain.'/';
- my $switchserver = '/adm/switchserver?otherserver='.$home.'&role='.$trolecode;
+ my $switchserver = '/adm/switchserver?otherserver='.$home.'&role='.$trolecode;
$r->internal_redirect($switchserver);
}
last;
@@ -261,7 +301,7 @@ sub handler {
my ($server_status,$home) = &check_author_homeserver($user,$domain);
if ($server_status eq 'switchserver') {
my $trolecode = 'ca./'.$domain.'/'.$user;
- my $switchserver = '/adm/switchserver?otherserver='.$home.'&role='.$trolecode;
+ my $switchserver = '/adm/switchserver?otherserver='.$home.'&role='.$trolecode;
$r->internal_redirect($switchserver);
}
last;
@@ -277,12 +317,12 @@ sub handler {
if ($dcroles{$domain}) {
my ($server_status,$home) = &check_author_homeserver($user,$domain);
if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
- &Apache::lonnet::check_adhoc_privs($domain,$user,$then,
- $now,'ca');
+ &Apache::lonnet::check_adhoc_privs($domain,$user,$update,
+ $refresh,$now,'ca');
if ($server_status eq 'switchserver') {
my $trolecode = 'ca./'.$domain.'/'.$user;
my $switchserver = '/adm/switchserver?'
- .'otherserver='.$home.'&role='.$trolecode;
+ .'otherserver='.$home.'&role='.$trolecode;
$r->internal_redirect($switchserver);
}
} else {
@@ -299,12 +339,48 @@ sub handler {
foreach $envkey (keys %env) {
next if ($envkey!~/^user\.role\./);
my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
- &Apache::lonnet::role_status($envkey,$then,$now,\$role,\$where,
+ &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
\$trolecode,\$tstatus,\$tstart,\$tend);
if ($env{'form.'.$trolecode}) {
if ($tstatus eq 'is') {
$where=~s/^\///;
my ($cdom,$cnum,$csec)=split(/\//,$where);
+ if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
+ my $home = $env{'course.'.$cdom.'_'.$cnum.'.home'};
+ my @ids = &Apache::lonnet::current_machine_ids();
+ unless ($loncaparev eq '' && $home && grep(/^\Q$home\E$/,@ids)) {
+ 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'});
+ 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());
+ return OK;
+ }
+ }
+ }
+ }
# check for course groups
my %coursegroups = &Apache::lonnet::get_active_groups(
$env{'user.domain'},$env{'user.name'},$cdom, $cnum);
@@ -367,9 +443,11 @@ sub handler {
$r->print(<
+//
-';
+ }
+ } elsif ($currrole_expired) {
+ $msg .= '';
+ if (&Apache::loncommon::show_course()) {
+ $msg .= &mt('Your role in the current course has expired.');
+ } else {
+ $msg .= &mt('Your current role has expired.');
+ }
+ $msg .= '
'.&mt('However you can continue to use this role until you logout, click the "Re-Select" button, or your session has been idle for more than 24 hours.').'
';
+ }
+ if (!@changed_roles || !(keys(%changed_groups))) {
+ $msg .= '';
+ if (!@changed_roles) {
+ $msg .= '- ';
+ if (&Apache::loncommon::show_course()) {
+ $msg .= &mt('No new courses or communities');
+ } else {
+ $msg .= &mt('No role changes');
+ }
+ $msg .= '
';
+ }
+ if ($hasgroups && !(keys(%changed_groups)) && !(grep(/gr/,@changed_roles))) {
+ $msg .= '- '.&mt('No changes in course/community groups').'
';
+ }
+ $msg .= '
';
+ if (!@changed_roles && !(keys(%changed_groups))) {
+ return $msg;
+ }
+ }
+ my $changemsg;
+ if (@changed_roles > 0) {
+ if (keys(%newgroup) > 0) {
+ my $groupmsg;
+ foreach my $item (sort(keys(%newgroup))) {
+ if (&is_active_course($item,$refresh,$update,\%roleshash)) {
+ $groupmsg .= ''.
+ &mt('[_1] with status: [_2].',
+ $item,$newgroup{$item}).'';
+ }
+ }
+ if ($groupmsg) {
+ $changemsg .= ''.
+ &mt('Courses with new groups').''.
+ '';
+ }
+ }
+ if (keys(%newrole) > 0) {
+ $changemsg .= ''.&mt('New roles').
+ '';
+ foreach my $item (sort(keys(%newrole))) {
+ $changemsg .= '- '.
+ &mt('[_1] with status: [_2].',
+ $item,$newrole{$item}).'
';
+ }
+ $changemsg .= '
';
+ }
+ if (keys(%customprivchg) > 0) {
+ $changemsg .= ''.
+ &mt('Custom roles with privilege changes').
+ '';
+ foreach my $item (sort(keys(%customprivchg))) {
+ $changemsg .= '- '.$item.'
';
+ }
+ $changemsg .= '
';
+ }
+ if (keys(%rolechange) > 0) {
+ $changemsg .= ''.
+ &mt('Existing roles with status changes').''.
+ '';
+ foreach my $item (sort(keys(%rolechange))) {
+ $changemsg .= '- '.
+ &mt('[_1] status now: [_2].',$item,
+ $rolechange{$item}).'
';
+ }
+ $changemsg .= '
';
+ }
+ if (keys(%deletedroles) > 0) {
+ $changemsg .= ''.
+ &mt('Existing roles deleted').''.
+ '';
+ foreach my $item (sort(keys(%deletedroles))) {
+ $changemsg .= '- '.$item.'
';
+ }
+ $changemsg .= '
';
+ }
+ }
+ if ((keys(%changed_groups) > 0) || (keys(%groupchange) > 0)) {
+ my $groupchgmsg;
+ foreach my $key (sort(keys(%changed_groups))) {
+ my $crs = 'gr/'.$key;
+ $crs =~ s/_/\//;
+ if (&is_active_course($crs,$refresh,$update,\%roleshash)) {
+ if (ref($changed_groups{$key}) eq 'HASH') {
+ my @showgroups;
+ foreach my $group (sort(keys(%{$changed_groups{$key}}))) {
+ if ($changed_groups{$key}{$group} eq 'active') {
+ push(@showgroups,$group);
+ }
+ }
+ if (@showgroups > 0) {
+ $groupchgmsg .= ''.
+ &mt('Course: [_1], groups: [_2].',$key,
+ join(', ',@showgroups)).
+ '';
+ }
+ }
+ }
+ }
+ if (keys(%groupchange) > 0) {
+ $groupchgmsg .= ''.
+ &mt('Existing course/community groups with status changes').''.
+ '';
+ foreach my $crs (sort(keys(%groupchange))) {
+ if (ref($groupchange{$crs}) eq 'HASH') {
+ $groupchgmsg .= '- '.&mt('Course/Community: [_1]',''.$crs.'
');
+ foreach my $group (sort(keys(%{$groupchange{$crs}}))) {
+ $groupchgmsg .= '- '.&mt('Group: [_1] status now: [_2].',''.$group.'',$groupchange{$crs}{$group}).'
';
+ }
+ $groupchgmsg .= '
';
+ }
+ }
+ $groupchgmsg .= '
';
+ }
+ if ($groupchgmsg) {
+ $changemsg .= ''.
+ &mt('Courses with changes in groups').''.
+ '';
+ }
+ }
+ if ($changemsg) {
+ $msg .= '';
+ }
+ &Apache::lonnet::set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
+ my ($curr_is_adv,$curr_role_adv,$curr_author,$curr_role_author);
+ $curr_author = $env{'user.author'};
+ if (($env{'request.role'} =~/^au/) || ($env{'request.role'} =~/^ca/) ||
+ ($env{'request.role'} =~/^aa/)) {
+ $curr_role_author=1;
+ }
+ $curr_is_adv = $env{'user.adv'};
+ $curr_role_adv = $env{'request.role.adv'};
+ if (keys(%userroles) > 0) {
+ foreach my $role (@changed_roles) {
+ unless(grep(/^\Q$role\E$/,@rolecodes)) {
+ push(@rolecodes,$role);
+ }
+ }
+ unless(grep(/^\Qcm\E$/,@rolecodes)) {
+ push(@rolecodes,'cm');
+ }
+ &Apache::lonnet::appenv(\%userroles,\@rolecodes);
+ }
+ my %newenv;
+ if (&Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'})) {
+ unless ($curr_is_adv) {
+ $newenv{'user.adv'} = 1;
+ }
+ } elsif ($curr_is_adv && !$curr_role_adv) {
+ &Apache::lonnet::delenv('user.adv');
+ }
+ my %authorroleshash =
+ &Apache::lonnet::get_my_roles('','','userroles',['active'],['au','ca','aa']);
+ if (keys(%authorroleshash)) {
+ unless ($curr_author) {
+ $newenv{'user.author'} = 1;
+ }
+ } elsif ($curr_author && !$curr_role_author) {
+ &Apache::lonnet::delenv('user.author');
+ }
+ if ($env{'request.course.id'}) {
+ my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+ my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+ my (@activecrsgroups,$crsgroupschanged);
+ if ($env{'request.course.groups'}) {
+ @activecrsgroups = split(/:/,$env{'request.course.groups'});
+ foreach my $item (keys(%deletedroles)) {
+ if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
+ if (grep(/^\Q$1\E$/,@activecrsgroups)) {
+ $crsgroupschanged = 1;
+ last;
+ }
+ }
+ }
+ }
+ unless ($crsgroupschanged) {
+ foreach my $item (keys(%newgroup)) {
+ if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
+ if ($newgroup{$item} eq 'active') {
+ $crsgroupschanged = 1;
+ last;
+ }
+ }
+ }
+ }
+ if ((ref($changed_groups{$env{'request.course.id'}}) eq 'HASH') ||
+ (ref($groupchange{"/$cdom/$cnum"}) eq 'HASH') ||
+ ($crsgroupschanged)) {
+ my %grouproles = &Apache::lonnet::get_my_roles('','','userroles',
+ ['active'],['gr'],[$cdom],1);
+ my @activegroups;
+ foreach my $item (keys(%grouproles)) {
+ next unless($item =~ /^\Q$cnum\E:\Q$cdom\E/);
+ my $group;
+ my ($crsn,$crsd,$role,$remainder) = split(/:/,$item,4);
+ if ($remainder =~ /:/) {
+ (my $other,$group) = ($remainder =~ /^([\w:]+):([^:]+)$/);
+ } else {
+ $group = $remainder;
+ }
+ if ($group ne '') {
+ push(@activegroups,$group);
+ }
+ }
+ $newenv{'request.course.groups'} = join(':',@activegroups);
+ }
+ }
+ if (keys(%newenv)) {
+ &Apache::lonnet::appenv(\%newenv);
+ }
+ return $msg;
+}
+
+sub curr_role_status {
+ my ($start,$end,$refresh,$update) = @_;
+ if (($start) && ($start<0)) { return 'deleted' };
+ my $status = 'active';
+ if (($end) && ($end<=$update)) {
+ $status = 'previous';
+ }
+ if (($start) && ($refresh<$start)) {
+ $status = 'future';
+ }
+ return $status;
+}
+
+sub gather_roleprivs {
+ my ($allroles,$allgroups,$userroles,$area,$role,$tstart,$tend,$status) = @_;
+ return unless ((ref($allroles) eq 'HASH') && (ref($allgroups) eq 'HASH') && (ref($userroles) eq 'HASH'));
+ if (($area ne '') && ($role ne '')) {
+ &Apache::lonnet::userrolelog($role,$env{'user.name'},$env{'user.domain'},
+ $area,$tstart,$tend);
+ my $spec=$role.'.'.$area;
+ $userroles->{'user.role.'.$spec} = $tstart.'.'.$tend;
+ my ($tdummy,$tdomain,$trest)=split(/\//,$area);
+ if ($status eq 'active') {
+ if ($role =~ /^cr\//) {
+ &Apache::lonnet::custom_roleprivs($allroles,$role,$tdomain,$trest,$spec,$area);
+ } elsif ($role eq 'gr') {
+ my %rolehash = &Apache::lonnet::get('roles',[$area.'_'.$role],
+ $env{'user.domain'},
+ $env{'user.name'});
+ my ($trole) = split(/_/,$rolehash{$area.'_'.$role},2);
+ (undef,my $group_privs) = split(/\//,$trole);
+ $group_privs = &unescape($group_privs);
+ &Apache::lonnet::group_roleprivs($allgroups,$area,$group_privs,$tend,$tstart);
+ } else {
+ &Apache::lonnet::standard_roleprivs($allroles,$role,$tdomain,$spec,$trest,$area);
+ }
+ }
+ }
+ return;
+}
+
+sub is_active_course {
+ my ($rolekey,$refresh,$update,$roleshashref) = @_;
+ return unless(ref($roleshashref) eq 'HASH');
+ my ($role,$cdom,$cnum) = split(/\//,$rolekey);
+ my $is_active;
+ foreach my $key (keys(%{$roleshashref})) {
+ if ($key =~ /^\Q$cnum\E:\Q$cdom\E:/) {
+ my ($tstart,$tend) = split(/:/,$roleshashref->{$key});
+ my $status = &curr_role_status($tstart,$tend,$refresh,$update);
+ if ($status eq 'active') {
+ $is_active = 1;
+ last;
+ }
+ }
+ }
+ return $is_active;
+}
+
1;
__END__
@@ -1575,8 +2514,7 @@ course they should act on, etc. Both in
handler determines via C's C<&allowed> function that a certain
action is not allowed, C is used as error handler. This
allows the user to select another role which may have permission to do
-what they were trying to do. C can also be accessed via the
-B button in the Remote Control.
+what they were trying to do.
=begin latex