Annotation of loncom/auth/lonroles.pm, revision 1.315

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.315   ! raeburn     4: # $Id: lonroles.pm,v 1.314 2016/05/04 05:14:49 raeburn Exp $
1.31      www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.32      harris41   28: ###
1.22      harris41   29: 
1.210     jms        30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
                     34: Apache::lonroles - User Roles Screen
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: Invoked by /etc/httpd/conf/srm.conf:
                     39: 
                     40:  <Location /adm/roles>
                     41:  PerlAccessHandler       Apache::lonacc
                     42:  SetHandler perl-script
                     43:  PerlHandler Apache::lonroles
                     44:  ErrorDocument     403 /adm/login
                     45:  ErrorDocument	  500 /adm/errorhandler
                     46:  </Location>
                     47: 
                     48: =head1 OVERVIEW
                     49: 
                     50: =head2 Choosing Roles
                     51: 
                     52: C<lonroles> is a handler that allows a user to switch roles in
                     53: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                     54: default role that a user has before selecting a role, as widely as
                     55: possible, but certain handlers for example need specification which
                     56: course they should act on, etc. Both in this scenario, and when the
                     57: handler determines via C<lonnet>'s C<&allowed> function that a certain
                     58: action is not allowed, C<lonroles> is used as error handler. This
                     59: allows the user to select another role which may have permission to do
1.246     droeschl   60: what they were trying to do.
1.210     jms        61: 
                     62: =begin latex
                     63: 
                     64: \begin{figure}
                     65: \begin{center}
                     66: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                     67:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                     68: \end{center}
                     69: \end{figure}
                     70: 
                     71: =end latex
                     72: 
                     73: =head2 Role Initialization
                     74: 
                     75: The privileges for a user are established at login time and stored in the session environment. As a consequence, a new role does not become active till the next login. Handlers are able to query for privileges using C<lonnet>'s C<&allowed> function. When a user first logs in, their role is the "common" role, which means that they have the sum of all of their privileges. During a session it might become necessary to choose a particular role, which as a consequence also limits the user to only the privileges in that particular role.
                     76: 
                     77: =head1 INTRODUCTION
                     78: 
                     79: This module enables a user to select what role he wishes to
                     80: operate under (instructor, student, teaching assistant, course
                     81: coordinator, etc).  These roles are pre-established by the actions
                     82: of upper-level users.
                     83: 
                     84: This is part of the LearningOnline Network with CAPA project
                     85: described at http://www.lon-capa.org.
                     86: 
                     87: =head1 HANDLER SUBROUTINE
                     88: 
                     89: This routine is called by Apache and mod_perl.
                     90: 
                     91: =over 4
                     92: 
                     93: =item *
                     94: 
                     95: Roles Initialization (yes/no)
                     96: 
                     97: =item *
                     98: 
                     99: Get Error Message from Environment
                    100: 
                    101: =item *
                    102: 
                    103: Who is this?
                    104: 
                    105: =item *
                    106: 
                    107: Generate Page Output
                    108: 
                    109: =item *
                    110: 
                    111: Choice or no choice
                    112: 
                    113: =item *
                    114: 
                    115: Table
                    116: 
                    117: =item *
                    118: 
                    119: Privileges
                    120: 
                    121: =back
                    122: 
                    123: =cut
                    124: 
                    125: 
1.1       harris41  126: package Apache::lonroles;
                    127: 
                    128: use strict;
1.118     albertel  129: use Apache::lonnet;
1.7       www       130: use Apache::lonuserstate();
1.304     musolffc  131: use Apache::Constants qw(:common REDIRECT);
1.2       www       132: use Apache::File();
1.26      www       133: use Apache::lonmenu;
1.29      albertel  134: use Apache::loncommon;
1.104     raeburn   135: use Apache::lonhtmlcommon;
1.57      www       136: use Apache::lonannounce;
1.72      www       137: use Apache::lonlocal;
1.151     www       138: use Apache::lonpageflip();
1.167     albertel  139: use Apache::lonnavdisplay();
1.241     raeburn   140: use Apache::loncoursequeueadmin;
1.279     raeburn   141: use Apache::longroup;
1.283     raeburn   142: use Apache::lonrss;
1.313     raeburn   143: use Apache::lonplacementtest;
1.120     albertel  144: use GDBM_File;
1.170     albertel  145: use LONCAPA qw(:DEFAULT :match);
1.201     raeburn   146: use HTML::Entities;
1.276     raeburn   147: 
1.1       harris41  148: 
1.62      matthew   149: sub redirect_user {
1.245     droeschl  150:     my ($r,$title,$url,$msg) = @_;
1.62      matthew   151:     $msg = $title if (! defined($msg));
1.73      www       152:     &Apache::loncommon::content_type($r,'text/html');
1.62      matthew   153:     &Apache::loncommon::no_cache($r);
                    154:     $r->send_http_header;
1.228     bisitz    155: 
                    156:     # Breadcrumbs
                    157:     my $brcrum = [{'href' => $url,
                    158:                    'text' => 'Switching Role'},];
1.147     albertel  159:     my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
1.228     bisitz    160:                                                     {'redirect' => [1,$url],
                    161:                                                      'bread_crumbs' => $brcrum,});
1.147     albertel  162:     my $end_page   = &Apache::loncommon::end_page();
                    163: 
1.92      www       164: # Note to style police: 
                    165: # This must only replace the spaces, nothing else, or it bombs elsewhere.
                    166:     $url=~s/ /\%20/g;
1.93      albertel  167:     $r->print(<<ENDREDIR);
1.147     albertel  168: $start_page
1.222     bisitz    169: <p>$msg</p>
1.147     albertel  170: $end_page
1.62      matthew   171: ENDREDIR
                    172:     return;
                    173: }
                    174: 
1.150     www       175: sub error_page {
                    176:     my ($r,$error,$dest)=@_;
                    177:     &Apache::loncommon::content_type($r,'text/html');
                    178:     &Apache::loncommon::no_cache($r);
                    179:     $r->send_http_header;
                    180:     return OK if $r->header_only;
1.228     bisitz    181:     # Breadcrumbs
                    182:     my $brcrum = [{'href' => $dest,
                    183:                    'text' => 'Problems during Course Initialization'},];
                    184:     $r->print(&Apache::loncommon::start_page('Problems during Course Initialization',
                    185:                                              undef,
                    186:                                              {'bread_crumbs' => $brcrum,})
                    187:     );
                    188:     $r->print(
1.225     bisitz    189:         '<script type="text/javascript">'.
                    190:         '// <![CDATA['.
                    191:         &Apache::lonmenu::rawconfig().
                    192:         '// ]]>'.
                    193:         '</script>'.
                    194: 	      '<p class="LC_error">'.&mt('The following problems occurred:').
1.228     bisitz    195:           '<br />'.
1.150     www       196: 	      $error.
1.228     bisitz    197: 	      '</p><br /><a href="'.$dest.'">'.&mt('Continue').'</a>'
                    198:     );
                    199:     $r->print(&Apache::loncommon::end_page());
1.150     www       200: }
                    201: 
1.1       harris41  202: sub handler {
1.10      www       203: 
1.1       harris41  204:     my $r = shift;
                    205: 
1.308     raeburn   206:     # Check for critical messages and redirect if present.
1.304     musolffc  207:     my ($redirect,$url) = &Apache::loncommon::critical_redirect(300);
                    208:     if ($redirect) {
                    209:         &Apache::loncommon::content_type($r,'text/html');
                    210:         $r->header_out(Location => $url);
                    211:         return REDIRECT;
                    212:     }
                    213: 
1.6       www       214:     my $now=time;
1.118     albertel  215:     my $then=$env{'user.login.time'};
1.226     raeburn   216:     my $refresh=$env{'user.refresh.time'};
1.260     raeburn   217:     my $update=$env{'user.update.time'};
1.226     raeburn   218:     if (!$refresh) {
                    219:         $refresh = $then;
                    220:     }
1.260     raeburn   221:     if (!$update) {
                    222:         $update = $then;
                    223:     }
                    224: 
1.274     raeburn   225:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    226: 
                    227: # -------------------------------------------------- Check if setting hot list 
                    228:     my $hotlist;
                    229:     if ($env{'form.action'} eq 'verify_and_change_rolespref') {
                    230:         $hotlist = &Apache::lonpreferences::verify_and_change_rolespref($r);
                    231:     }
                    232: 
1.260     raeburn   233: # -------------------------------------------------------- Check for new roles
                    234:     my $updateresult;
1.274     raeburn   235:     if ($env{'form.state'} eq 'doupdate') {
1.260     raeburn   236:         my $show_course=&Apache::loncommon::show_course();
                    237:         my $checkingtxt;
                    238:         if ($show_course) {
                    239:             $checkingtxt = &mt('Checking for new courses ...');
                    240:         } else {
                    241:             $checkingtxt = &mt('Checking for new roles ...');
                    242:         }
1.274     raeburn   243:         $updateresult = $checkingtxt;
1.260     raeburn   244:         $updateresult .= &update_session_roles();
                    245:         &Apache::lonnet::appenv({'user.update.time'  => $now});
                    246:         $update = $now;
1.272     raeburn   247:         &Apache::loncoursequeueadmin::reqauthor_check();
1.270     raeburn   248:     }
                    249: 
                    250: # -------------------------------------------------- Check for author requests
                    251:     my $reqauthor;
1.274     raeburn   252:     if ($env{'form.state'} eq 'requestauthor') {
1.272     raeburn   253:        $reqauthor = &Apache::loncoursequeueadmin::process_reqauthor(\$update);
1.260     raeburn   254:     }
                    255: 
1.6       www       256:     my $envkey;
1.107     raeburn   257:     my %dcroles = ();
1.260     raeburn   258:     my $numdc = &check_fordc(\%dcroles,$update,$then);
1.304     musolffc  259:     my $loncaparev = $r->dir_config('lonVersion');
1.10      www       260: 
1.6       www       261: # ================================================================== Roles Init
1.118     albertel  262:     if ($env{'form.selectrole'}) {
1.188     www       263: 
                    264:         my $locknum=&Apache::lonnet::get_locks();
                    265:         if ($locknum) { return 409; }
                    266: 
1.315   ! raeburn   267:         my $custom_adhoc;
1.134     www       268:         if ($env{'form.newrole'}) {
                    269:             $env{'form.'.$env{'form.newrole'}}=1;
1.315   ! raeburn   270: # Check if this is a Domain Helpdesk role trying to enter a course
        !           271:             if ($env{'form.newrole'} =~ m{^cr/($match_domain)/\1\-domainconfig/\w+\./\1/$match_courseid$}) {
        !           272:                 if (&Apache::lonnet::allowed('rar',$1)) {
        !           273:                     $custom_adhoc = 1;
        !           274:                 }
        !           275:             }
1.134     www       276: 	}
1.118     albertel  277: 	if ($env{'request.course.id'}) {
1.185     raeburn   278:             # Check if user is CC trying to select a course role
                    279:             if ($env{'form.switchrole'}) {
1.252     raeburn   280:                 my $switch_is_active;
                    281:                 if (defined($env{'user.role.'.$env{'form.switchrole'}})) {
                    282:                     my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
                    283:                     if (!$end || $end > $now) {
1.260     raeburn   284:                         if (!$start || $start < $update) {
1.252     raeburn   285:                             $switch_is_active = 1;
                    286:                         }
                    287:                     }
                    288:                 }
                    289:                 unless ($switch_is_active) {
1.260     raeburn   290:                     &adhoc_course_role($refresh,$update,$then);
1.185     raeburn   291:                 }
                    292:             }
1.118     albertel  293: 	    my %temp=('logout_'.$env{'request.course.id'} => time);
1.33      www       294: 	    &Apache::lonnet::put('email_status',\%temp);
1.118     albertel  295: 	    &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
1.100     albertel  296: 	}
1.310     raeburn   297: 	&Apache::lonnet::appenv({"request.course.id"           => '',
                    298: 			 	 "request.course.fn"           => '',
                    299: 				 "request.course.uri"          => '',
                    300: 				 "request.course.sec"          => '',
                    301:                                  "request.course.tied"         => '',
                    302:                                  "request.course.timechecked"  => '',
                    303: 				 "request.role"                => 'cm',
                    304:                                  "request.role.adv"            => $env{'user.adv'},
                    305: 				 "request.role.domain"         => $env{'user.domain'}});
1.315   ! raeburn   306: # Check if Domain Helpdesk role trying to enter a course needs privs to be created
        !           307:         if ($env{'form.newrole'} =~ m{^cr/($match_domain)/\1\-domainconfig/(\w+)\./\1/($match_courseid)$}) {
        !           308:             my $cdom = $1;
        !           309:             my $rolename = $2;
        !           310:             my $cnum = $3;
        !           311:             if ($custom_adhoc) {
        !           312:                 my %adhocroles = &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
        !           313:                                                                   'adhocroles.'.$cdom);
        !           314:                 if (keys(%adhocroles)) {
        !           315:                     my @adhoc = split(',',$adhocroles{'adhocroles.'.$cdom});
        !           316:                     if (grep(/^\Q$rolename\E$/,@adhoc)) {
        !           317:                         if (&Apache::lonnet::check_adhoc_privs($cdom,$cnum,$update,$refresh,$now,
        !           318:                                                                "cr/$cdom/$cdom".'-domainconfig/'.$rolename)) {
        !           319:                             &Apache::lonnet::appenv({"environment.internal.$cdom.$cnum.cr/$cdom/$cdom".'-domainconfig'."$rolename.adhoc" => time});
        !           320:                         }
        !           321:                     }
        !           322:                 }
        !           323:             }
        !           324:         } elsif ($numdc > 0) {
1.182     www       325: # Check if user is a DC trying to enter a course or author space and needs privs to be created
1.296     raeburn   326:             foreach my $envkey (keys(%env)) {
1.240     raeburn   327: # Is this an ad-hoc Coordinator role?
                    328:                 if (my ($ccrole,$domain,$coursenum) =
                    329: 		    ($envkey =~ m-^form\.(cc|co)\./($match_domain)/($match_courseid)$-)) {
1.146     raeburn   330:                     if ($dcroles{$domain}) {
1.275     raeburn   331:                         if (&Apache::lonnet::check_adhoc_privs($domain,$coursenum,
                    332:                                                            $update,$refresh,$now,$ccrole)) {
                    333:                             &Apache::lonnet::appenv({"environment.internal.$domain.$coursenum.$ccrole.adhoc" => time});
                    334:                         }
1.182     www       335:                     }
                    336:                     last;
                    337:                 }
1.193     raeburn   338: # Is this an ad-hoc CA-role?
1.183     www       339:                 if (my ($domain,$user) =
                    340: 		    ($envkey =~ m-^form\.ca\./($match_domain)/($match_username)$-)) {
1.206     raeburn   341:                     if (($domain eq $env{'user.domain'}) && ($user eq $env{'user.name'})) {
                    342:                         delete($env{$envkey});
                    343:                         $env{'form.au./'.$domain.'/'} = 1;
                    344:                         my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    345:                         if ($server_status eq 'switchserver') {
                    346:                             my $trolecode = 'au./'.$domain.'/';
1.248     raeburn   347:                             my $switchserver = '/adm/switchserver?otherserver='.$home.'&amp;role='.$trolecode;
1.206     raeburn   348:                             $r->internal_redirect($switchserver);
1.285     raeburn   349:                             return OK;
1.206     raeburn   350:                         }
                    351:                         last;
                    352:                     }
                    353:                     if (my ($castart,$caend) = ($env{'user.role.ca./'.$domain.'/'.$user} =~ /^(\d*)\.(\d*)$/)) {
                    354:                         if (((($castart) && ($castart < $now)) || !$castart) && 
                    355:                             ((!$caend) || (($caend) && ($caend > $now)))) {
                    356:                             my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    357:                             if ($server_status eq 'switchserver') {
                    358:                                 my $trolecode = 'ca./'.$domain.'/'.$user;
1.248     raeburn   359:                                 my $switchserver = '/adm/switchserver?otherserver='.$home.'&amp;role='.$trolecode;
1.206     raeburn   360:                                 $r->internal_redirect($switchserver);
1.285     raeburn   361:                                 return OK;
1.206     raeburn   362:                             }
                    363:                             last;
                    364:                         }
                    365:                     }
                    366:                     # Check if author blocked ca-access
1.190     www       367:                     my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],$domain,$user);
                    368:                     if ($blocked{'domcoord.author'} eq 'blocked') {
1.206     raeburn   369:                         delete($env{$envkey});
                    370:                         $env{'user.error.msg'}=':::1:User '.$user.' in domain '.$domain.' blocked domain coordinator access';
                    371:                         last;
1.190     www       372:                     }
1.193     raeburn   373:                     if ($dcroles{$domain}) {
                    374:                         my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    375:                         if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
1.260     raeburn   376:                             &Apache::lonnet::check_adhoc_privs($domain,$user,$update,
1.230     raeburn   377:                                                                $refresh,$now,'ca');
1.193     raeburn   378:                             if ($server_status eq 'switchserver') {
                    379:                                 my $trolecode = 'ca./'.$domain.'/'.$user; 
                    380:                                 my $switchserver = '/adm/switchserver?'
1.248     raeburn   381:                                                   .'otherserver='.$home.'&amp;role='.$trolecode;
1.193     raeburn   382:                                 $r->internal_redirect($switchserver);
1.285     raeburn   383:                                 return OK;
1.193     raeburn   384:                             }
                    385:                         } else {
                    386:                             delete($env{$envkey});
                    387:                         }
1.183     www       388:                     } else {
                    389:                         delete($env{$envkey});
1.182     www       390:                     }
                    391:                     last;
                    392:                 }
1.107     raeburn   393:             }
                    394:         }
                    395: 
1.296     raeburn   396:         foreach $envkey (keys(%env)) {
1.40      matthew   397:             next if ($envkey!~/^user\.role\./);
1.102     raeburn   398:             my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
1.260     raeburn   399:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.218     raeburn   400:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
1.118     albertel  401:             if ($env{'form.'.$trolecode}) {
1.55      albertel  402: 		if ($tstatus eq 'is') {
                    403: 		    $where=~s/^\///;
                    404: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
1.255     raeburn   405:                     if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
                    406:                         my $home = $env{'course.'.$cdom.'_'.$cnum.'.home'};
                    407:                         my @ids = &Apache::lonnet::current_machine_ids();
                    408:                         unless ($loncaparev eq '' && $home && grep(/^\Q$home\E$/,@ids)) {
                    409:                             my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
1.256     raeburn   410:                             if ($curr_reqd_hash{'internal.releaserequired'} ne '') {
1.255     raeburn   411:                                 my ($switchserver,$switchwarning) =
1.310     raeburn   412:                                     &Apache::loncommon::check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode,
                    413:                                                                                $curr_reqd_hash{'internal.releaserequired'});
1.256     raeburn   414:                                 if ($switchwarning ne '' || $switchserver ne '') {
                    415:                                     &Apache::loncommon::content_type($r,'text/html');
                    416:                                     &Apache::loncommon::no_cache($r);
                    417:                                     $r->send_http_header;
1.310     raeburn   418:                                     $r->print(&Apache::loncommon::check_release_result($switchwarning,$switchserver));
1.256     raeburn   419:                                     return OK;
1.255     raeburn   420:                                 }
                    421:                             }
                    422:                         }
                    423:                     }
1.137     raeburn   424: # check for course groups
                    425:                     my %coursegroups = &Apache::lonnet::get_active_groups(
                    426:                           $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
                    427:                     my $cgrps = join(':',keys(%coursegroups));
                    428: 
1.111     albertel  429: # store role if recent_role list being kept
1.118     albertel  430:                     if ($env{'environment.recentroles'}) {
1.158     albertel  431:                         my %frozen_roles =
                    432:                            &Apache::lonhtmlcommon::get_recent_frozen('roles',$env{'environment.recentrolesn'});
1.111     albertel  433: 			&Apache::lonhtmlcommon::store_recent('roles',
1.158     albertel  434: 							     $trolecode,' ',$frozen_roles{$trolecode});
1.111     albertel  435:                     }
                    436: 
                    437: 
1.53      www       438: # check for keyed access
1.55      albertel  439: 		    if (($role eq 'st') && 
1.118     albertel  440:                        ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
1.89      www       441: # who is key authority?
                    442: 			my $authdom=$cdom;
                    443: 			my $authnum=$cnum;
1.118     albertel  444: 			if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
1.89      www       445: 			    ($authnum,$authdom)=
1.172     albertel  446: 				split(/:/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
1.89      www       447: 			}
                    448: # check with key authority
                    449: 			unless (&Apache::lonnet::validate_access_key(
1.118     albertel  450: 				     $env{'environment.key.'.$cdom.'_'.$cnum},
1.89      www       451: 					     $authdom,$authnum)) {
1.53      www       452: # there is no valid key
1.118     albertel  453: 			     if ($env{'form.newkey'}) {
1.53      www       454: # student attempts to register a new key
1.89      www       455: 				 &Apache::loncommon::content_type($r,'text/html');
                    456: 				 &Apache::loncommon::no_cache($r);
                    457: 				 $r->send_http_header;
                    458: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  459: 				 my $start_page=&Apache::loncommon::start_page
1.89      www       460: 				    ('Verifying Access Key to Unlock this Course');
1.147     albertel  461: 				 my $end_page=&Apache::loncommon::end_page();
1.90      www       462: 				 my $buttontext=&mt('Enter Course');
                    463: 				 my $message=&mt('Successfully registered key');
                    464: 				 my $assignresult=
                    465: 				     &Apache::lonnet::assign_access_key(
1.118     albertel  466: 						     $env{'form.newkey'},
1.90      www       467: 						     $authdom,$authnum,
1.91      www       468: 						     $cdom,$cnum,
1.118     albertel  469:                                                      $env{'user.domain'},
                    470: 						     $env{'user.name'},
1.204     bisitz    471:                                                      &mt('Assigned from [_1] at [_2] for [_3]'
                    472:                                                         ,$ENV{'REMOTE_ADDR'}
                    473:                                                         ,&Apache::lonlocal::locallocaltime()
                    474:                                                         ,$trolecode)
                    475:                                                      );
1.90      www       476: 				 unless ($assignresult eq 'ok') {
                    477: 				     $assignresult=~s/^error\:\s*//;
                    478: 				     $message=&mt($assignresult).
                    479: 				     '<br /><a href="/adm/logout">'.
1.89      www       480: 				     &mt('Logout').'</a>';
1.90      www       481: 				     $buttontext=&mt('Re-Enter Key');
                    482: 				 }
1.89      www       483: 				 $r->print(<<ENDENTEREDKEY);
1.147     albertel  484: $start_page
1.179     raeburn   485: <script type="text/javascript">
1.225     bisitz    486: // <![CDATA[
1.89      www       487: $swinfo
1.225     bisitz    488: // ]]>
1.89      www       489: </script>
1.225     bisitz    490: <form action="" method="post">
1.89      www       491: <input type="hidden" name="selectrole" value="1" />
                    492: <input type="hidden" name="$trolecode" value="1" />
1.211     tempelho  493: <span class="LC_fontsize_large">$message</span><br />
1.89      www       494: <input type="submit" value="$buttontext" />
                    495: </form>
1.147     albertel  496: $end_page
1.89      www       497: ENDENTEREDKEY
                    498:                                  return OK;
1.55      albertel  499: 			     } else {
1.53      www       500: # print form to enter a new key
1.73      www       501: 				 &Apache::loncommon::content_type($r,'text/html');
1.55      albertel  502: 				 &Apache::loncommon::no_cache($r);
                    503: 				 $r->send_http_header;
                    504: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  505: 				 my $start_page=&Apache::loncommon::start_page
1.55      albertel  506: 				    ('Enter Access Key to Unlock this Course');
1.147     albertel  507: 				 my $end_page=&Apache::loncommon::end_page();
1.55      albertel  508: 				 $r->print(<<ENDENTERKEY);
1.147     albertel  509: $start_page
1.179     raeburn   510: <script type="text/javascript">
1.225     bisitz    511: // <![CDATA[
1.53      www       512: $swinfo
1.225     bisitz    513: // ]]>
1.53      www       514: </script>
1.225     bisitz    515: <form action="" method="post">
1.89      www       516: <input type="hidden" name="selectrole" value="1" />
                    517: <input type="hidden" name="$trolecode" value="1" />
1.118     albertel  518: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
1.53      www       519: <input type="submit" value="Enter key" />
                    520: </form>
1.147     albertel  521: $end_page
1.53      www       522: ENDENTERKEY
1.55      albertel  523: 				 return OK;
                    524: 			     }
                    525: 			 }
                    526: 		     }
1.118     albertel  527: 		    &Apache::lonnet::log($env{'user.domain'},
                    528: 					 $env{'user.name'},
                    529: 					 $env{'user.home'},
1.87      www       530: 					 "Role ".$trolecode);
1.101     albertel  531: 		    
1.56      www       532: 		    &Apache::lonnet::appenv(
1.186     raeburn   533: 					   {'request.role'        => $trolecode,
                    534: 					    'request.role.domain' => $cdom,
                    535: 					    'request.course.sec'  => $csec,
                    536:                                             'request.course.groups' => $cgrps});
1.101     albertel  537:                     my $tadv=0;
1.62      matthew   538: 
1.125     www       539: 		    if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
1.152     raeburn   540:                         my $msg;
1.55      albertel  541: 			my ($furl,$ferr)=
                    542: 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
1.284     raeburn   543:                         unless ($ferr) {
                    544:                             unless (($env{'form.switchrole'}) || 
                    545:                                     ($env{"environment.internal.$cdom.$cnum.$role.adhoc"})) {
                    546:                                 &Apache::lonnet::put('nohist_crslastlogin',
                    547:                                     {$env{'user.name'}.':'.$env{'user.domain'}.
                    548:                                      ':'.$csec.':'.$role => $now},$cdom,$cnum);
                    549:                             }
                    550:                             my ($feeds,$syllabus_time);
1.283     raeburn   551:                             &Apache::lonrss::advertisefeeds($cnum,$cdom,undef,\$feeds);
1.284     raeburn   552:                             &Apache::lonnet::appenv({'request.course.feeds' => $feeds});
1.289     raeburn   553:                             &Apache::lonnet::get_numsuppfiles($cnum,$cdom,1);
1.284     raeburn   554:                             unless ($env{'course.'.$cdom.'_'.$cnum.'.updatedsyllabus'}) {
                    555:                                 unless (($env{'course.'.$cdom.'_'.$cnum.'.externalsyllabus'}) ||
                    556:                                         ($env{'course.'.$cdom.'_'.$cnum.'.uploadedsyllabus'})) {
                    557:                                     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);
                    558:                                     $syllabus_time = $syllabus{'uploaded.lastmodified'};
                    559:                                     if ($syllabus_time) {
                    560:                                         &Apache::lonnet::appenv({'request.course.syllabustime' => $syllabus_time});
                    561:                                     }
                    562:                                 }
                    563:                             }
1.275     raeburn   564:                         }
1.118     albertel  565: 			if (($env{'form.orgurl'}) && 
1.292     raeburn   566: 			    ($env{'form.orgurl'}!~/^\/adm\/flip/) &&
                    567: 			    ($env{'form.orgurl'} ne '/adm/roles')) {
1.118     albertel  568: 			    my $dest=$env{'form.orgurl'};
1.219     raeburn   569:                             if ($env{'form.symb'}) {
                    570:                                 if ($dest =~ /\?/) {
                    571:                                     $dest .= '&';
                    572:                                 } else {
1.292     raeburn   573:                                     $dest .= '?';
1.219     raeburn   574:                                 }
                    575:                                 $dest .= 'symb='.$env{'form.symb'};
                    576:                             }
1.117     albertel  577: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   578: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.150     www       579:                             if (($ferr) && ($tadv)) {
                    580: 				&error_page($r,$ferr,$dest);
                    581: 			    } else {
1.255     raeburn   582:                                 if ($dest =~ m{^/adm/coursedocs\?folderpath}) {
                    583:                                     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) { 
                    584:                                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.268     raeburn   585:                                         &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,
                    586:                                                                                        $cdom.'_'.$cnum);
1.255     raeburn   587:                                     }
                    588:                                 }
1.150     www       589: 				$r->internal_redirect($dest);
                    590: 			    }
1.55      albertel  591: 			    return OK;
                    592: 			} else {
1.155     albertel  593: 			    if (!$env{'request.course.id'}) {
1.55      albertel  594: 				&Apache::lonnet::appenv(
1.186     raeburn   595: 				      {"request.course.id"  => $cdom.'_'.$cnum});
1.61      www       596: 				$furl='/adm/roles?tryagain=1';
1.221     bisitz    597:                 $msg='<p><span class="LC_error">'
                    598:                     .&mt('Could not initialize [_1] at this time.',
                    599:                          $env{'course.'.$cdom.'_'.$cnum.'.description'})
                    600:                     .'</span></p>'
                    601:                     .'<p>'.&mt('Please try again.').'</p>'
                    602:                     .'<p>'.$ferr.'</p>';
1.55      albertel  603: 			    }
1.117     albertel  604: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   605: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.152     raeburn   606: 
1.150     www       607: 			    if (($ferr) && ($tadv)) {
                    608: 				&error_page($r,$ferr,$furl);
                    609: 			    } else {
                    610: 				# Check to see if the user is a CC entering a course 
                    611: 				# for the first time
1.240     raeburn   612: 				if ((($role eq 'cc') || ($role eq 'co')) 
1.297     raeburn   613:                                     && ($env{'course.'.$cdom.'_'.$cnum.'.course.helper.not.run'})) { 
1.150     www       614: 				    $furl = "/adm/helper/course.initialization.helper";
                    615: 				    # Send the user to the course they selected
                    616: 				} elsif ($env{'request.course.id'}) {
1.313     raeburn   617:                                     if ((&Apache::loncommon::course_type() eq 'Placement') && 
                    618:                                         (!$env{'request.role.adv'})) {
                    619:                                         my ($score,$incomplete) = 
                    620:                                             &Apache::lonplacementtest::check_completion(undef,undef,1);
                    621:                                         if (($incomplete) && ($incomplete < 100)) {
                    622:                                             &redirect_user($r, &mt('Entering [_1]',
                    623:                                                           $env{'course.'.$cdom.'_'.$cnum.'.description'}),
                    624:                                                           '/adm/placement', $msg);
                    625:                                             return OK;
                    626:                                         }
                    627:                                     }
1.276     raeburn   628:                                     my ($dest,$destsymb,$checkenc);
                    629:                                     $dest = $env{'form.destinationurl'};
                    630:                                     $destsymb = $env{'form.destsymb'};
                    631:                                     if ($dest ne '') {
                    632:                                         if ($env{'form.switchrole'}) {
                    633:                                             if ($destsymb ne '') {
                    634:                                                 if ($destsymb !~ m{^/enc/}) {
                    635:                                                     unless ($env{'request.role.adv'}) {
                    636:                                                         $checkenc = 1;
                    637:                                                     }
                    638:                                                 }
                    639:                                             }
                    640:                                             if ($dest =~ m{^/enc/}) {
                    641:                                                 if ($env{'request.role.adv'}) {
                    642:                                                     $dest = &Apache::lonenc::unencrypted($dest);
                    643:                                                     if ($destsymb eq '') {
1.277     raeburn   644:                                                         ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]*)/);
1.276     raeburn   645:                                                         $destsymb = &unescape($destsymb);
                    646:                                                     }
                    647:                                                 }
                    648:                                             } else {
                    649:                                                 if ($destsymb eq '') {
1.280     raeburn   650:                                                     ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]+)/);
1.276     raeburn   651:                                                     $destsymb = &unescape($destsymb);
                    652:                                                 }
                    653:                                                 unless ($env{'request.role.adv'}) {
                    654:                                                     $checkenc = 1;
                    655:                                                 }
                    656:                                             }
                    657:                                             if (($checkenc) && ($destsymb ne '')) {
                    658:                                                 my ($encstate,$unencsymb,$res);
1.281     raeburn   659:                                                 $unencsymb = &Apache::lonnet::symbclean($destsymb);
1.276     raeburn   660:                                                 (undef,undef,$res) = &Apache::lonnet::decode_symb($unencsymb);
                    661:                                                 &Apache::lonnet::symbverify($unencsymb,$res,\$encstate);
                    662:                                                 if ($encstate) {
                    663:                                                     if (($dest ne '') && ($dest !~ m{^/enc/})) {
                    664:                                                         $dest=&Apache::lonenc::encrypted($dest);
                    665:                                                     }
                    666:                                                 }
                    667:                                             }
                    668:                                         }
1.277     raeburn   669:                                         unless (($dest =~ m{^/enc/}) || ($dest =~ /(\?|\&)symb=.+___\d+___.+/)) {
1.276     raeburn   670:                                             if (($destsymb ne '') && ($destsymb !~ m{^/enc/})) {
                    671:                                                 my $esc_symb = &escape($destsymb);
                    672:                                                 $dest .= '?symb='.$esc_symb;
                    673:                                             }
1.203     raeburn   674:                                         }
1.245     droeschl  675:                                         &redirect_user($r, &mt('Entering [_1]',
1.297     raeburn   676:                                                        $env{'course.'.$cdom.'_'.$cnum.'.description'}),
1.245     droeschl  677:                                                        $dest, $msg);
1.185     raeburn   678:                                         return OK;
                    679:                                     }
1.150     www       680: 				    if (&Apache::lonnet::allowed('whn',
                    681: 								 $env{'request.course.id'})
                    682: 					|| &Apache::lonnet::allowed('whn',
                    683: 								    $env{'request.course.id'}.'/'
                    684: 								    .$env{'request.course.sec'})
                    685: 					) {
1.297     raeburn   686: 					my $startpage = &courseloadpage($env{'request.course.id'});
1.150     www       687: 					unless ($startpage eq 'firstres') {         
1.204     bisitz    688: 					    $msg = &mt('Entering [_1] ...',
1.297     raeburn   689: 						       $env{'course.'.$env{'request.course.id'}.'.description'});
1.245     droeschl  690: 					    &redirect_user($r, &mt('New in course'),
                    691:                                        '/adm/whatsnew?refpage=start', $msg);
1.150     www       692: 					    return OK;
                    693: 					}
                    694: 				    }
                    695: 				}
1.300     raeburn   696:                                 # Are we allowed to look at the first resource?
1.311     raeburn   697:                                 my $access;
1.299     musolffc  698:                                 if ($furl =~ m{^(/adm/wrapper|)/ext/}) {
1.300     raeburn   699:                                     # If it's an external resource,
1.299     musolffc  700:                                     # strip off the symb argument and possible query
                    701:                                     my ($exturl,$symb) = ($furl =~ m{^(.+)(?:\?|\&)symb=(.+)$});
                    702:                                     # Unencode $symb
                    703:                                     $symb = &unescape($symb);
                    704:                                     # Then check for permission
1.311     raeburn   705:                                     $access = &Apache::lonnet::allowed('bre',$exturl,$symb);
1.300     raeburn   706:                                 # For other resources just check for permission
1.311     raeburn   707:                                 } else {
                    708:                                     $access = &Apache::lonnet::allowed('bre',$furl);
                    709:                                 }
                    710:                                 if (!$access) {
1.299     musolffc  711:                                     $furl = &Apache::lonpageflip::first_accessible_resource();
1.311     raeburn   712:                                 } elsif ($access eq 'B') {
                    713:                                     $furl = '/adm/navmaps?showOnlyHomework=1';
1.299     musolffc  714:                                 }
1.162     albertel  715:                                 $msg = &mt('Entering [_1] ...',
1.297     raeburn   716: 					   $env{'course.'.$cdom.'_'.$cnum.'.description'});
1.245     droeschl  717: 				&redirect_user($r, &mt('Entering [_1]',
1.297     raeburn   718:                                $env{'course.'.$cdom.'_'.$cnum.'.description'}),
1.245     droeschl  719:                                $furl, $msg);
1.58      bowersj2  720: 			    }
1.124     albertel  721: 			    return OK;
1.55      albertel  722: 			}
                    723: 		    }
1.62      matthew   724:                     #
                    725:                     # Send the user to the construction space they selected
1.125     www       726:                     if ($role =~ /^(au|ca|aa)$/) {
1.62      matthew   727:                         my $redirect_url = '/priv/';
                    728:                         if ($role eq 'au') {
1.262     www       729:                             $redirect_url.=$env{'user.domain'}.'/'.$env{'user.name'};
1.62      matthew   730:                         } else {
1.263     www       731:                             $redirect_url .= $where;
1.62      matthew   732:                         }
                    733:                         $redirect_url .= '/';
1.288     raeburn   734:                         &redirect_user($r,&mt('Entering Authoring Space'),
1.62      matthew   735:                                        $redirect_url);
                    736:                         return OK;
                    737:                     }
1.104     raeburn   738:                     if ($role eq 'dc') {
1.108     raeburn   739:                         my $redirect_url = '/adm/menu/';
                    740:                         &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104     raeburn   741:                                        $redirect_url);
1.108     raeburn   742:                         return OK;
1.104     raeburn   743:                     }
1.315   ! raeburn   744:                     if ($role eq 'dh') {
        !           745:                         my $redirect_url = '/adm/menu/';
        !           746:                         &redirect_user($r,&mt('Loading Domain Helpdesk Menu'),
        !           747:                                        $redirect_url);
        !           748:                         return OK;
        !           749:                     }
1.220     raeburn   750:                     if ($role eq 'sc') {
                    751:                         my $redirect_url = '/adm/grades?command=scantronupload';
                    752:                         &redirect_user($r,&mt('Loading Data Upload Page'),
                    753:                                        $redirect_url);
                    754:                         return OK;
                    755:                     }
1.55      albertel  756: 		}
                    757:             }
1.6       www       758:         }
1.40      matthew   759:     }
1.44      www       760: 
1.10      www       761: 
1.6       www       762: # =============================================================== No Roles Init
1.10      www       763: 
1.73      www       764:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel  765:     &Apache::loncommon::no_cache($r);
1.10      www       766:     $r->send_http_header;
                    767:     return OK if $r->header_only;
                    768: 
1.224     raeburn   769:     my $crumbtext = 'User Roles';
                    770:     my $pagetitle = 'My Roles';
                    771:     my $recent = &mt('Recent Roles');
1.287     raeburn   772:     my $standby = &mt('Role selected. Please stand by.');
1.224     raeburn   773:     my $show_course=&Apache::loncommon::show_course();
                    774:     if ($show_course) {
                    775:         $crumbtext = 'Courses';
                    776:         $pagetitle = 'My Courses';
                    777:         $recent = &mt('Recent Courses');
1.287     raeburn   778:         $standby = &mt('Course selected. Please stand by.'); 
1.224     raeburn   779:     }
                    780:     my $brcrum =[{href=>"/adm/roles",text=>$crumbtext}];
1.274     raeburn   781: 
                    782:     my %roles_in_env;
                    783:     my $showcount = &roles_from_env(\%roles_in_env,$update); 
                    784: 
1.52      www       785:     my $swinfo=&Apache::lonmenu::rawconfig();
1.302     raeburn   786:     my %domdefs=&Apache::lonnet::get_domain_defaults($env{'user.domain'}); 
                    787:     my $cattype = 'std';
                    788:     if ($domdefs{'catauth'}) {
                    789:         $cattype = $domdefs{'catauth'};
                    790:     }
1.313     raeburn   791:     my $placementonly;
                    792:     if ($showcount == 1) {
                    793:         if ($env{'request.course.id'}) {
                    794:             if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
                    795:                 $placementonly = 1;
                    796:             }
                    797:         } else {
                    798:             foreach my $rolecode (keys(%roles_in_env)) {
                    799:                 my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
                    800:                 if ($cid) {
                    801:                     my %coursedescription =
                    802:                         &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
                    803:                     if ($coursedescription{'type'} eq 'Placement') {
                    804:                         $placementonly = 1;
                    805:                     }
                    806:                     last;
                    807:                 }
                    808:             }
                    809:         }
                    810:     }
                    811:     my ($start_page,$funcs);
                    812:     if ($placementonly) {
                    813:         $start_page=&Apache::loncommon::start_page($pagetitle,undef,
                    814:                                                   {bread_crumbs=>$brcrum,crstype=>'Placement'});
                    815:     } else {
                    816:         $funcs = &get_roles_functions($showcount,$cattype);
1.314     raeburn   817:         my $crumbsright;
                    818:         if ($env{'browser.mobile'}) {
                    819:             $crumbsright = $funcs;
                    820:             undef($funcs);
                    821:         }
                    822:         $start_page=&Apache::loncommon::start_page($pagetitle,undef,{bread_crumbs=>$brcrum,
                    823:                                                                      bread_crumbs_component=>$crumbsright});
1.313     raeburn   824:     }
1.312     damieng   825:     &js_escape(\$standby);
1.274     raeburn   826:     my $noscript='<br /><span class="LC_error">'.&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.').'<br />'.&mt('As this is not the case, most functionality in the system will be unavailable.').'</span><br />';
1.163     www       827: 
1.10      www       828:     $r->print(<<ENDHEADER);
1.147     albertel  829: $start_page
1.274     raeburn   830: $funcs
1.179     raeburn   831: <noscript>
                    832: $noscript
                    833: </noscript>
                    834: <script type="text/javascript">
1.225     bisitz    835: // <![CDATA[
1.26      www       836: $swinfo
                    837: window.focus();
1.134     www       838: 
                    839: active=true;
                    840: 
                    841: function enterrole (thisform,rolecode,buttonname) {
                    842:     if (active) {
                    843: 	active=false;
                    844:         document.title='$standby';
                    845:         window.status='$standby';
                    846: 	thisform.newrole.value=rolecode;
                    847: 	thisform.submit();
                    848:     } else {
                    849:        alert('$standby');
1.260     raeburn   850:     }
                    851: }
                    852: 
1.274     raeburn   853: function rolesView (caller) {
                    854:     if ((caller == 'showall') || (caller == 'noshowall')) {
                    855:         document.rolechoice.display.value = caller;
                    856:     } else {
                    857:         if ((caller == 'doupdate') || (caller == 'requestauthor') ||
                    858:             (caller == 'queued')) { 
                    859:             document.rolechoice.state.value = caller;
                    860:         }
                    861:     }
                    862:     document.rolechoice.selectrole.value='';
                    863:     document.rolechoice.submit();
1.270     raeburn   864: }
                    865: 
1.225     bisitz    866: // ]]>
1.26      www       867: </script>
1.10      www       868: ENDHEADER
1.6       www       869: 
1.2       www       870: # ------------------------------------------ Get Error Message from Environment
                    871: 
1.118     albertel  872:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
                    873:     if ($env{'user.error.msg'}) {
1.55      albertel  874: 	$r->log_reason(
1.118     albertel  875:    "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12      www       876:     }
1.1       harris41  877: 
1.61      www       878: # ------------------------------------------------- Can this user re-init, etc?
1.6       www       879: 
1.118     albertel  880:     my $advanced=$env{'user.adv'};
1.61      www       881:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118     albertel  882:     my $tryagain=$env{'form.tryagain'};
1.209     raeburn   883:     my $reinit=$env{'user.reinit'};
                    884:     delete $env{'user.reinit'};
1.6       www       885: 
1.2       www       886: # -------------------------------------------------------- Generate Page Output
1.6       www       887: # --------------------------------------------------------------- Error Header?
1.2       www       888:     if ($error) {
1.187     bisitz    889:         $r->print("<h1>".&mt('LON-CAPA Access Control')."</h1>");
1.174     albertel  890: 	$r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>");
                    891: 	if ($priv ne '') {
1.187     bisitz    892:             $r->print(&mt('Access  : ').&Apache::lonnet::plaintext($priv)."\n");
1.174     albertel  893: 	}
                    894: 	if ($fn ne '') {
1.187     bisitz    895:             $r->print(&mt('Resource: ').&Apache::lonenc::check_encrypt($fn)."\n");
1.174     albertel  896: 	}
                    897: 	if ($msg ne '') {
1.187     bisitz    898:             $r->print(&mt('Action  : ').$msg."\n");
1.174     albertel  899: 	}
                    900: 	$r->print("</pre><hr />");
1.120     albertel  901: 	my $url=$fn;
                    902: 	my $last;
                    903: 	if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                    904: 		&GDBM_READER(),0640)) {
                    905: 	    $last=$hash{'last_known'};
                    906: 	    untie(%hash);
                    907: 	}
1.149     www       908: 	if ($last) { $fn.='?symb='.&escape($last); }
1.120     albertel  909: 
                    910: 	&Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
                    911: 					&Apache::lonenc::check_encrypt($fn));
1.2       www       912:     } else {
1.118     albertel  913:         if ($env{'user.error.msg'}) {
1.209     raeburn   914:             if ($reinit) {
                    915:                 $r->print(
                    916:  '<h3><span class="LC_error">'.
1.234     raeburn   917:  &mt('As your session file for the course or community has expired, you will need to re-select it.').'</span></h3>');
1.209     raeburn   918:             } else {
                    919: 	        $r->print(
1.157     albertel  920:  '<h3><span class="LC_error">'.
1.235     bisitz    921:  &mt('You need to choose another user role or enter a specific course or community for this function.').
                    922:  '</span></h3>');
1.209     raeburn   923: 	    }
                    924:         }
1.2       www       925:     }
                    926:     if ($nochoose) {
1.177     www       927: 	$r->print("<h2>".&mt('Sorry ...')."</h2>\n<span class='LC_error'>".
                    928: 		  &mt('This action is currently not authorized.').'</span>'.
1.150     www       929: 		  &Apache::loncommon::end_page());
                    930: 	return OK;
1.6       www       931:     } else {
1.274     raeburn   932:         if ($updateresult || $reqauthor || $hotlist) {
                    933:             my $showresult = '<div>';
                    934:             if ($updateresult) {
                    935:                 $showresult .= &Apache::lonhtmlcommon::confirm_success($updateresult);
                    936:             }
                    937:             if ($reqauthor) {
                    938:                 $showresult .= &Apache::lonhtmlcommon::confirm_success($reqauthor);
                    939:             }
                    940:             if ($hotlist) {
                    941:                 $showresult .= $hotlist;
                    942:             } 
                    943:             $showresult .= '</div>';
                    944:             $r->print($showresult);
                    945:         } elsif ($env{'form.state'} eq 'queued') {
                    946:             $r->print(&get_queued());
1.270     raeburn   947:         }
1.18      www       948:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                    949:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www       950:         }
1.274     raeburn   951:         my $display = ($env{'form.display'} =~ /^(showall)$/);
1.84      www       952:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116     albertel  953:         $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
                    954:         $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134     www       955:         $r->print('<input type="hidden" name="newrole" value="" />');
1.274     raeburn   956:         $r->print('<input type="hidden" name="display" value="'.$display.'" />');
                    957:         $r->print('<input type="hidden" name="state" value="" />');
1.6       www       958:     }
1.259     raeburn   959:     $r->rflush();
1.226     raeburn   960: 
                    961:     my (%roletext,%sortrole,%roleclass,%futureroles,%timezones);
                    962:     my ($countactive,$countfuture,$inrole,$possiblerole) = 
1.274     raeburn   963:         &gather_roles($update,$refresh,$now,$reinit,$nochoose,\%roles_in_env,\%roletext,
                    964:                       \%sortrole,\%roleclass,\%futureroles,\%timezones,$loncaparev);
1.226     raeburn   965:     $refresh = $now;
                    966:     &Apache::lonnet::appenv({'user.refresh.time'  => $refresh});
1.313     raeburn   967:     if ($countactive == 1) {
                    968:         if ($env{'request.course.id'}) {
                    969:             if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
                    970:                 $placementonly = 1;
                    971:             }
                    972:         } elsif ($possiblerole) {
                    973:             if ($possiblerole =~ m{^st\./($match_domain)/($match_courseid)(?:/|$)}) {
                    974:                 if ($env{'course.'.$1.'_'.$2.'.type'} eq 'Placement') {
                    975:                     $placementonly = 1;
                    976:                 }
                    977:             }
                    978:         }
                    979:     }
                    980:     if ((($cattype eq 'std') || ($cattype eq 'domonly')) && (!$env{'user.adv'}) &&
                    981:           (!$placementonly)) {
1.196     raeburn   982:         if ($countactive > 0) {
                    983:             my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.201     raeburn   984:             my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); 
1.233     bisitz    985:             $r->print(
                    986:                 '<p>'
1.295     bisitz    987:                .&mt('[_1]Visit the [_2]Course/Community Catalog[_3][_4]'
                    988:                    .' to view all [_5] LON-CAPA courses and communities.'
1.233     bisitz    989:                    ,'<b>'
                    990:                    ,'<a href="/adm/coursecatalog?showdom='.$esc_dom.'">'
1.295     bisitz    991:                    ,'</a>'
                    992:                    ,'</b>'
                    993:                    ,'"'.$domdesc.'"')
1.233     bisitz    994:                .'<br />'
1.235     bisitz    995:                .&mt('If a course or community is [_1]not[_2] in your list of current courses and communities below,'
1.233     bisitz    996:                    .' you may be able to enroll if self-enrollment is permitted.'
                    997:                    ,'<b>','</b>')
                    998:                .'</p>'
                    999:             );
1.196     raeburn  1000:         }
                   1001:     }
                   1002: 
1.84      www      1003: # No active roles
                   1004:     if ($countactive==0) {
1.306     raeburn  1005:         &requestcourse_advice($r,$cattype,$inrole); 
1.191     raeburn  1006: 	$r->print('</form>');
                   1007:         if ($countfuture) {
                   1008:             $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
                   1009:             my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
                   1010:                                                $nochoose);
                   1011:             &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
                   1012:                             \%roletext);
                   1013:             my $tremark='';
1.212     bisitz   1014:             my $tbg;
1.191     raeburn  1015:             if ($env{'request.role'} eq 'cm') {
1.212     bisitz   1016:                 $tbg="LC_roles_selected";
1.204     bisitz   1017:                 $tremark=&mt('Currently selected.').' ';
1.191     raeburn  1018:             } else {
1.212     bisitz   1019:                 $tbg="LC_roles_is";
1.191     raeburn  1020:             }
1.212     bisitz   1021:             $r->print(&Apache::loncommon::start_data_table_row()
                   1022:                      .'<td class="'.$tbg.'">&nbsp;</td>'
                   1023:                      .'<td colspan="3">'
                   1024:                      .&mt('No role specified')
                   1025:                      .'</td>'
                   1026:                      .'<td>'.$tremark.'&nbsp;</td>'
                   1027:                      .&Apache::loncommon::end_data_table_row()
                   1028:             );
1.191     raeburn  1029: 
1.212     bisitz   1030:             $r->print(&Apache::loncommon::end_data_table());
1.191     raeburn  1031:         }
                   1032:         $r->print(&Apache::loncommon::end_page());
1.84      www      1033: 	return OK;
1.313     raeburn  1034:     } elsif (($placementonly) && ($env{'request.role'} eq 'cm')) {
                   1035: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>
                   1036: 	          <input type="hidden" name="'.$possiblerole.'" value="1" />
                   1037:                   <noscript><br />
                   1038:                   <input type="submit" name="submit" value="'.&mt('Continue').'" />
                   1039:                   </noscript></form>');
                   1040: 	$r->rflush();
                   1041: 	$r->print('<script type="text/javascript">document.forms.rolechoice.submit();</script>');
                   1042: 	$r->print(&Apache::loncommon::end_page());
                   1043: 	return OK;
1.84      www      1044:     }
                   1045: # ----------------------------------------------------------------------- Table
1.247     raeburn  1046: 
                   1047:     if ($numdc > 0) {
                   1048:         $r->print(&coursepick_jscript());
                   1049:         $r->print(&Apache::loncommon::coursebrowser_javascript().
                   1050:                   &Apache::loncommon::authorbrowser_javascript());
                   1051:     }
                   1052: 
1.224     raeburn  1053:     unless ((!&Apache::loncommon::show_course()) || ($nochoose) || ($countactive==1)) {
1.173     albertel 1054: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
1.84      www      1055:     }
1.229     raeburn  1056:     if ($env{'form.destinationurl'}) {
                   1057:         $r->print('<input type="hidden" name="destinationurl" value="'.
                   1058:                   $env{'form.destinationurl'}.'" />');
                   1059:         if ($env{'form.destsymb'} ne '') {
                   1060:             $r->print('<input type="hidden" name="destsymb" value="'.
                   1061:                       $env{'form.destsymb'}.'" />');
                   1062:         }
                   1063:     }
1.247     raeburn  1064: 
1.191     raeburn  1065:     my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,$nochoose);
1.118     albertel 1066:     if ($env{'environment.recentroles'}) {
1.111     albertel 1067:         my %recent_roles =
1.118     albertel 1068:                &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111     albertel 1069: 	my $output='';
1.247     raeburn  1070: 	foreach my $role (sort(keys(%recent_roles))) {
                   1071: 	    if (ref($roletext{'user.role.'.$role}) eq 'ARRAY') {
1.223     raeburn  1072: 		$output.= &Apache::loncommon::start_data_table_row().
1.247     raeburn  1073:                           $roletext{'user.role.'.$role}->[0].
1.223     raeburn  1074:                           &Apache::loncommon::end_data_table_row();
1.249     raeburn  1075:                 if ($roletext{'user.role.'.$role}->[1] ne '') {
                   1076:                     $output .= &Apache::loncommon::continue_data_table_row().
                   1077:                                $roletext{'user.role.'.$role}->[1].
                   1078:                                &Apache::loncommon::end_data_table_row();
                   1079:                 }
1.247     raeburn  1080:                 if ($role =~ m{dc\./($match_domain)/} 
1.170     albertel 1081: 		    && $dcroles{$1}) {
1.192     raeburn  1082: 		    $output .= &adhoc_roles_row($1,'recent');
1.133     albertel 1083:                 }
1.113     raeburn  1084: 	    } elsif ($numdc > 0) {
1.247     raeburn  1085:                 unless ($role =~/^error\:/) {
1.249     raeburn  1086:                     my ($roletext,$role_text_end) = &display_cc_role('user.role.'.$role);
1.259     raeburn  1087:                     if ($roletext) {
                   1088:                         $output.= &Apache::loncommon::start_data_table_row().
                   1089:                                   $roletext.
                   1090:                                   &Apache::loncommon::end_data_table_row();
                   1091:                         if ($role_text_end) {
                   1092:                             $output .= &Apache::loncommon::continue_data_table_row().
                   1093:                                        $role_text_end.
                   1094:                                        &Apache::loncommon::end_data_table_row();
                   1095:                         }
                   1096:                     }
1.113     raeburn  1097:                 }
1.247     raeburn  1098:             }
1.111     albertel 1099: 	}
                   1100: 	if ($output) {
1.212     bisitz   1101: 	    $r->print(&Apache::loncommon::start_data_table_empty_row()
                   1102:                      .'<td align="center" colspan="5">'
1.224     raeburn  1103:                      .$recent
1.212     bisitz   1104:                      .'</td>'
                   1105:                      .&Apache::loncommon::end_data_table_empty_row()
                   1106:             );
1.111     albertel 1107: 	    $r->print($output);
1.114     raeburn  1108:             $doheaders ++;
1.111     albertel 1109: 	}
                   1110:     }
1.191     raeburn  1111:     &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext);
1.202     raeburn  1112:     if ($countactive > 1) {
                   1113:         my $tremark='';
1.212     bisitz   1114:         my $tbg;
1.202     raeburn  1115:         if ($env{'request.role'} eq 'cm') {
1.212     bisitz   1116:             $tbg="LC_roles_selected";
1.204     bisitz   1117:             $tremark=&mt('Currently selected.').' ';
1.202     raeburn  1118:         } else {
1.212     bisitz   1119:                 $tbg="LC_roles_is";
1.202     raeburn  1120:         }
1.212     bisitz   1121:         $r->print(&Apache::loncommon::start_data_table_row());
1.202     raeburn  1122:         unless ($nochoose) {
                   1123: 	    if ($env{'request.role'} ne 'cm') {
1.212     bisitz   1124: 	        $r->print('<td class="'.$tbg.'"><input type="submit" value="'.
1.202     raeburn  1125: 		          &mt('Select').'" name="cm" /></td>');
                   1126: 	    } else {
1.212     bisitz   1127: 	        $r->print('<td class="'.$tbg.'">&nbsp;</td>');
1.202     raeburn  1128: 	    }
                   1129:         }
1.212     bisitz   1130:         $r->print('<td colspan="3">'
                   1131:                  .&mt('No role specified')
                   1132:                  .'</td>'
                   1133:                  .'<td>'.$tremark.'&nbsp;</td>'
                   1134:                  .&Apache::loncommon::end_data_table_row()
                   1135:         );
1.202     raeburn  1136:     } 
1.212     bisitz   1137:     $r->print(&Apache::loncommon::end_data_table());
1.4       www      1138:     unless ($nochoose) {
                   1139: 	$r->print("</form>\n");
                   1140:     }
1.22      harris41 1141: # ------------------------------------------------------------ Privileges Info
1.118     albertel 1142:     if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.212     bisitz   1143: 	$r->print('<hr /><h2>'.&mt('Current Privileges').'</h2>');
1.175     albertel 1144: 	$r->print(&privileges_info());
1.4       www      1145:     }
1.267     bisitz   1146:     my $announcements = &Apache::lonnet::getannounce();
                   1147:     $r->print(
                   1148:         '<br />'.
                   1149:         '<h2>'.&mt('Announcements').'</h2>'.
                   1150:         $announcements
                   1151:     ) unless (!$announcements);
1.65      www      1152:     if ($advanced) {
1.201     raeburn  1153:         my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.231     bisitz   1154:         $r->print('<p><small><i>'
                   1155:                  .&mt('This LON-CAPA server is version [_1]',$r->dir_config('lonVersion'))
1.308     raeburn  1156:                  .'</i></small></p>');
1.65      www      1157:     }
1.147     albertel 1158:     $r->print(&Apache::loncommon::end_page());
1.1       harris41 1159:     return OK;
1.102     raeburn  1160: }
                   1161: 
1.274     raeburn  1162: sub roles_from_env {
                   1163:     my ($roleshash,$update) = @_;
                   1164:     my $count = 0;
                   1165:     if (ref($roleshash) eq 'HASH') {
                   1166:         foreach my $envkey (keys(%env)) {
                   1167:             if ($envkey =~ m{^user\.role\.(\w+)[./]}) {
                   1168:                 next if ($1 eq 'gr');
                   1169:                 $roleshash->{$envkey} = $env{$envkey};
                   1170:                 my ($start,$end) = split(/\./,$env{$envkey});
                   1171:                 unless ($end && $end<$update) {
                   1172:                     $count ++;
                   1173:                 }
                   1174:             }
                   1175:         }
                   1176:     }
                   1177:     return $count;
                   1178: }
                   1179: 
1.226     raeburn  1180: sub gather_roles {
1.274     raeburn  1181:     my ($update,$refresh,$now,$reinit,$nochoose,$roles_in_env,$roletext,$sortrole,$roleclass,$futureroles,
                   1182:         $timezones,$loncaparev) = @_;
1.226     raeburn  1183:     my ($countactive,$countfuture,$inrole,$possiblerole) = (0,0,0,'');
                   1184:     my $advanced = $env{'user.adv'};
                   1185:     my $tryagain = $env{'form.tryagain'};
1.254     raeburn  1186:     my @ids = &Apache::lonnet::current_machine_ids();
1.274     raeburn  1187:     if (ref($roles_in_env) eq 'HASH') {
                   1188:         foreach my $envkey (sort(keys(%{$roles_in_env}))) {
                   1189:             my $button = 1;
                   1190:             my $switchserver='';
                   1191:             my $switchwarning;
                   1192:             my ($role_text,$role_text_end,$sortkey,$role,$where,$trolecode,$tstart,
                   1193:                 $tend,$tremark,$tstatus,$tpstart,$tpend);
1.260     raeburn  1194:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.226     raeburn  1195:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
                   1196:             next if (!defined($role) || $role eq '' || $role =~ /^gr/);
                   1197:             $tremark='';
                   1198:             $tpstart='&nbsp;';
                   1199:             $tpend='&nbsp;';
                   1200:             if ($env{'request.role'} eq $trolecode) {
                   1201:                 $tstatus='selected';
                   1202:             }
                   1203:             my $tbg;
                   1204:             if (($tstatus eq 'is')
                   1205:                 || ($tstatus eq 'selected')
                   1206:                 || ($tstatus eq 'future')
1.274     raeburn  1207:                 || ($env{'form.display'} eq 'showall')) {
1.259     raeburn  1208:                 my $timezone = &role_timezone($where,$timezones);
                   1209:                 if ($tstart) {
                   1210:                     $tpstart=&Apache::lonlocal::locallocaltime($tstart,$timezone);
                   1211:                 }
                   1212:                 if ($tend) {
                   1213:                     $tpend=&Apache::lonlocal::locallocaltime($tend,$timezone);
                   1214:                 }
1.226     raeburn  1215:                 if ($tstatus eq 'is') {
                   1216:                     $tbg='LC_roles_is';
                   1217:                     $possiblerole=$trolecode;
                   1218:                     $countactive++;
                   1219:                 } elsif ($tstatus eq 'future') {
                   1220:                     $tbg='LC_roles_future';
                   1221:                     $button=0;
                   1222:                     $futureroles->{$trolecode} = $tstart.':'.$tend;
                   1223:                     $countfuture ++;
                   1224:                 } elsif ($tstatus eq 'expired') {
                   1225:                     $tbg='LC_roles_expired';
                   1226:                     $button=0;
                   1227:                 } elsif ($tstatus eq 'will_not') {
                   1228:                     $tbg='LC_roles_will_not';
                   1229:                     $tremark.=&mt('Expired after logout.').' ';
                   1230:                 } elsif ($tstatus eq 'selected') {
                   1231:                     $tbg='LC_roles_selected';
                   1232:                     $inrole=1;
                   1233:                     $countactive++;
                   1234:                     $tremark.=&mt('Currently selected.').' ';
                   1235:                 }
                   1236:                 my $trole;
                   1237:                 if ($role =~ /^cr\//) {
                   1238:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
                   1239:                     if ($tremark) { $tremark.='<br />'; }
1.298     bisitz   1240:                     $tremark.=&mt('Custom role defined by [_1].',$rauthor.':'.$rdomain);
1.226     raeburn  1241:                 }
                   1242:                 $trole=Apache::lonnet::plaintext($role);
                   1243:                 my $ttype;
                   1244:                 my $twhere;
1.313     raeburn  1245:                 my $skipcal;
1.226     raeburn  1246:                 my ($tdom,$trest,$tsection)=
                   1247:                     split(/\//,Apache::lonnet::declutter($where));
                   1248:                 # First, Co-Authorship roles
                   1249:                 if (($role eq 'ca') || ($role eq 'aa')) {
                   1250:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
                   1251:                     my $allowed=0;
                   1252:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   1253:                     if (!$allowed) {
                   1254:                         $button=0;
1.248     raeburn  1255:                         $switchserver='otherserver='.$home.'&amp;role='.$trolecode;
1.226     raeburn  1256:                     }
                   1257:                     #next if ($home eq 'no_host');
                   1258:                     $home = &Apache::lonnet::hostname($home);
1.288     raeburn  1259:                     $ttype='Authoring Space';
1.226     raeburn  1260:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                   1261:                         ': '.$tdom.'<br />'.
                   1262:                         ' '.&mt('Server').':&nbsp;'.$home;
                   1263:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                   1264:                     $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
                   1265:                     $sortkey=$role."$trest:$tdom";
                   1266:                 } elsif ($role eq 'au') {
                   1267:                     # Authors
                   1268:                     my $home = &Apache::lonnet::homeserver
                   1269:                         ($env{'user.name'},$env{'user.domain'});
                   1270:                     my $allowed=0;
                   1271:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   1272:                     if (!$allowed) {
                   1273:                         $button=0;
1.248     raeburn  1274:                         $switchserver='otherserver='.$home.'&amp;role='.$trolecode;
1.226     raeburn  1275:                     }
                   1276:                     #next if ($home eq 'no_host');
                   1277:                     $home = &Apache::lonnet::hostname($home);
1.288     raeburn  1278:                     $ttype='Authoring Space';
1.226     raeburn  1279:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                   1280:                         ':&nbsp;'.$home;
                   1281:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                   1282:                     $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
                   1283:                     $sortkey=$role;
                   1284:                 } elsif ($trest) {
                   1285:                     my $tcourseid=$tdom.'_'.$trest;
                   1286:                     $ttype = &Apache::loncommon::course_type($tcourseid);
1.242     raeburn  1287:                     $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1.226     raeburn  1288:                     if ($env{'course.'.$tcourseid.'.description'}) {
1.254     raeburn  1289:                         my $home=$env{'course.'.$tcourseid.'.home'};
1.226     raeburn  1290:                         $twhere=$env{'course.'.$tcourseid.'.description'};
                   1291:                         $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.248     raeburn  1292:                         $twhere = &HTML::Entities::encode($twhere,'"<>&');
1.226     raeburn  1293:                         unless ($twhere eq &mt('Currently not available')) {
                   1294:                             $twhere.=' <span class="LC_fontsize_small">'.
                   1295:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
                   1296:                                     '</span>';
1.254     raeburn  1297:                             unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1.255     raeburn  1298:                                 my $required = $env{'course.'.$tcourseid.'.internal.releaserequired'};
1.259     raeburn  1299:                                 if ($required ne '') {
                   1300:                                     ($switchserver,$switchwarning) = 
1.310     raeburn  1301:                                         &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1.259     raeburn  1302:                                     if ($switchserver || $switchwarning) {
                   1303:                                         $button = 0;
                   1304:                                     }
1.254     raeburn  1305:                                 }
                   1306:                             }
1.226     raeburn  1307:                         }
                   1308:                     } else {
                   1309:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   1310:                         if (%newhash) {
                   1311:                             $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
                   1312:                                 "\0".$envkey;
1.248     raeburn  1313:                             $twhere=&HTML::Entities::encode($newhash{'description'},'"<>&').
                   1314:                                     ' <span class="LC_fontsize_small">'.
                   1315:                                      &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
                   1316:                                     '</span>';
1.226     raeburn  1317:                             $ttype = $newhash{'type'};
1.243     raeburn  1318:                             $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1.254     raeburn  1319:                             my $home = $newhash{'home'};
                   1320:                             unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1.255     raeburn  1321:                                 my $required = $newhash{'internal.releaserequired'};
1.259     raeburn  1322:                                 if ($required ne '') {
                   1323:                                     ($switchserver,$switchwarning) =
1.310     raeburn  1324:                                         &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1.259     raeburn  1325:                                     if ($switchserver || $switchwarning) {
                   1326:                                         $button = 0;
                   1327:                                     }
1.254     raeburn  1328:                                 }
                   1329:                             }
1.226     raeburn  1330:                         } else {
                   1331:                             $twhere=&mt('Currently not available');
                   1332:                             $env{'course.'.$tcourseid.'.description'}=$twhere;
                   1333:                             $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
                   1334:                             $ttype = 'Unavailable';
1.313     raeburn  1335:                             $skipcal = 1;
1.226     raeburn  1336:                         }
                   1337:                     }
1.313     raeburn  1338:                     if ($ttype eq 'Placement') {
                   1339:                         $ttype = 'Placement Test';
                   1340:                     }
1.226     raeburn  1341:                     if ($tsection) {
                   1342:                         $twhere.='<br />'.&mt('Section').': '.$tsection;
                   1343:                     }
                   1344:                     if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
                   1345:                 } elsif ($tdom) {
                   1346:                     $ttype='Domain';
                   1347:                     $twhere=$tdom;
                   1348:                     $sortkey=$role.$twhere;
                   1349:                 } else {
                   1350:                     $ttype='System';
                   1351:                     $twhere=&mt('system wide');
                   1352:                     $sortkey=$role.$twhere;
                   1353:                 }
                   1354:                 ($role_text,$role_text_end) =
                   1355:                     &build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,
                   1356:                                     $advanced,$tremark,$tbg,$trole,$twhere,$tpstart,
1.313     raeburn  1357:                                     $tpend,$nochoose,$button,$switchserver,$reinit,
                   1358:                                     $switchwarning,$skipcal);
1.226     raeburn  1359:                 $roletext->{$envkey}=[$role_text,$role_text_end];
                   1360:                 if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                   1361:                 $sortrole->{$sortkey}=$envkey;
                   1362:                 $roleclass->{$envkey}=$ttype;
                   1363:             }
                   1364:         }
                   1365:     }
                   1366:     return ($countactive,$countfuture,$inrole,$possiblerole);
                   1367: }
                   1368: 
1.215     raeburn  1369: sub role_timezone {
                   1370:     my ($where,$timezones) = @_;
                   1371:     my $timezone;
                   1372:     if (ref($timezones) eq 'HASH') { 
                   1373:         if ($where =~ m{^/($match_domain)/($match_courseid)}) {
                   1374:             my $cdom = $1;
                   1375:             my $cnum = $2;
                   1376:             if ($cdom && $cnum) {
                   1377:                 if (!exists($timezones->{$cdom.'_'.$cnum})) {
1.259     raeburn  1378:                     my $tz;
                   1379:                     if ($env{'course.'.$cdom.'_'.$cnum.'.description'}) {
                   1380:                         $tz = $env{'course.'.$cdom.'_'.$cnum.'.timezone'};
                   1381:                     } else {
                   1382:                         my %timehash =
                   1383:                             &Apache::lonnet::get('environment',['timezone'],$cdom,$cnum);
                   1384:                         $tz = $timehash{'timezone'};
                   1385:                     }
                   1386:                     if ($tz eq '') {
1.215     raeburn  1387:                         if (!exists($timezones->{$cdom})) {
                   1388:                             my %domdefaults = 
                   1389:                                 &Apache::lonnet::get_domain_defaults($cdom);
                   1390:                             if ($domdefaults{'timezone_def'} eq '') {
                   1391:                                 $timezones->{$cdom} = 'local';
                   1392:                             } else {
                   1393:                                 $timezones->{$cdom} = $domdefaults{'timezone_def'};
                   1394:                             }
                   1395:                         }
                   1396:                         $timezones->{$cdom.'_'.$cnum} = $timezones->{$cdom};
                   1397:                     } else {
                   1398:                         $timezones->{$cdom.'_'.$cnum} = 
1.259     raeburn  1399:                             &Apache::lonlocal::gettimezone($tz);
1.215     raeburn  1400:                     }
                   1401:                 }
                   1402:                 $timezone = $timezones->{$cdom.'_'.$cnum};
                   1403:             }
                   1404:         } else {
                   1405:             my ($tdom) = ($where =~ m{^/($match_domain)});
                   1406:             if ($tdom) {
                   1407:                 if (!exists($timezones->{$tdom})) {
                   1408:                     my %domdefaults = &Apache::lonnet::get_domain_defaults($tdom);
                   1409:                     if ($domdefaults{'timezone_def'} eq '') {
                   1410:                         $timezones->{$tdom} = 'local';
                   1411:                     } else {
                   1412:                         $timezones->{$tdom} = $domdefaults{'timezone_def'};
                   1413:                     }
                   1414:                 }
                   1415:                 $timezone = $timezones->{$tdom};
                   1416:             }
                   1417:         }
                   1418:         if ($timezone eq 'local') {
                   1419:             $timezone = undef;
                   1420:         }
                   1421:     }
                   1422:     return $timezone;
                   1423: }
                   1424: 
1.191     raeburn  1425: sub roletable_headers {
                   1426:     my ($r,$roleclass,$sortrole,$nochoose) = @_;
                   1427:     my $doheaders;
                   1428:     if ((ref($sortrole) eq 'HASH') && (ref($roleclass) eq 'HASH')) {
1.212     bisitz   1429:         $r->print('<br />'
1.314     raeburn  1430:                  .&Apache::loncommon::start_data_table('LC_textsize_mobile')
1.212     bisitz   1431:                  .&Apache::loncommon::start_data_table_header_row()
                   1432:         );
1.191     raeburn  1433:         if (!$nochoose) { $r->print('<th>&nbsp;</th>'); }
1.212     bisitz   1434:         $r->print('<th>'.&mt('User Role').'</th>'
                   1435:                  .'<th>'.&mt('Extent').'</th>'
                   1436:                  .'<th>'.&mt('Start').'</th>'
                   1437:                  .'<th>'.&mt('End').'</th>'
                   1438:                  .&Apache::loncommon::end_data_table_header_row()
                   1439:         );
1.191     raeburn  1440:         $doheaders=-1;
                   1441:         my @roletypes = &roletypes();
                   1442:         foreach my $type (@roletypes) {
                   1443:             my $haverole=0;
                   1444:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                   1445:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                   1446:                     $haverole=1;
                   1447:                 }
                   1448:             }
                   1449:             if ($haverole) { $doheaders++; }
                   1450:         }
                   1451:     }
                   1452:     return $doheaders;
                   1453: }
                   1454: 
                   1455: sub roletypes {
1.313     raeburn  1456:     my @types = ('Domain','Authoring Space','Course','Placement Test','Community','Unavailable','System');
1.191     raeburn  1457:     return @types; 
                   1458: }
                   1459: 
                   1460: sub print_rolerows {
                   1461:     my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_;
                   1462:     if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
                   1463:         my @types = &roletypes();
                   1464:         foreach my $type (@types) {
                   1465:             my $output;
                   1466:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                   1467:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                   1468:                     if (ref($roletext) eq 'HASH') {
1.223     raeburn  1469:                         if (ref($roletext->{$sortrole->{$which}}) eq 'ARRAY') {
                   1470:                             $output.= &Apache::loncommon::start_data_table_row().
                   1471:                                       $roletext->{$sortrole->{$which}}->[0].
                   1472:                                       &Apache::loncommon::end_data_table_row();
1.251     raeburn  1473:                             if ($roletext->{$sortrole->{$which}}->[1] ne '') {
                   1474:                                 $output .= &Apache::loncommon::continue_data_table_row().
                   1475:                                            $roletext->{$sortrole->{$which}}->[1].
                   1476:                                            &Apache::loncommon::end_data_table_row();
                   1477:                             }
1.223     raeburn  1478:                         }
1.191     raeburn  1479:                         if ($sortrole->{$which} =~ m-dc\./($match_domain)/-) {
                   1480:                             if (ref($dcroles) eq 'HASH') {
                   1481:                                 if ($dcroles->{$1}) {
1.192     raeburn  1482:                                     $output .= &adhoc_roles_row($1,'');
1.191     raeburn  1483:                                 }
                   1484:                             }
                   1485:                         }
                   1486:                     }
                   1487:                 }
                   1488:             }
                   1489:             if ($output) {
                   1490:                 if ($doheaders > 0) {
1.212     bisitz   1491:                     $r->print(&Apache::loncommon::start_data_table_empty_row()
                   1492:                              .'<td align="center" colspan="5">'
                   1493:                              .&mt($type)
                   1494:                              .'</td>'
                   1495:                              .&Apache::loncommon::end_data_table_empty_row()
                   1496:                     );
1.191     raeburn  1497:                 }
                   1498:                 $r->print($output);
                   1499:             }
                   1500:         }
                   1501:     }
                   1502: }
                   1503: 
                   1504: sub findcourse_advice {
1.302     raeburn  1505:     my ($r,$cattype) = @_;
1.191     raeburn  1506:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.201     raeburn  1507:     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.200     raeburn  1508:     if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) {
1.191     raeburn  1509:         $r->print(&mt('If you were expecting to see an active role listed for a particular course in the [_1] domain, it may be missing for one of the following reasons:',$domdesc).'
                   1510: <ul>
                   1511:  <li>'.&mt('The course has yet to be created.').'</li>
                   1512:  <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
                   1513:  <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
                   1514:  <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
                   1515:  <li>'.&mt('You registered for the course recently and there is a time lag between the time you register, and the time this information becomes available for the update of LON-CAPA course rosters.').'</li>
                   1516:  </ul>');
                   1517:     } else {
                   1518:         $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.').'<br />');
                   1519:     }
1.302     raeburn  1520:     if (($cattype eq 'std') || ($cattype eq 'domonly')) {
                   1521:         $r->print('<h3>'.&mt('Self-Enrollment').'</h3>'.
                   1522:                   '<p>'.&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.','<a href="/adm/coursecatalog?showdom='.$esc_dom.'">','</a>',$domdesc).'<br />');
                   1523:         $r->print(&mt('You can search for courses and communities which permit self-enrollment, if you would like to enroll in one.').'</p>'.
                   1524:         &Apache::loncoursequeueadmin::queued_selfenrollment());
                   1525:     }
1.216     raeburn  1526:     return;
                   1527: }
                   1528: 
1.234     raeburn  1529: sub requestcourse_advice {
1.306     raeburn  1530:     my ($r,$cattype,$inrole) = @_;
1.234     raeburn  1531:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                   1532:     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.306     raeburn  1533:     my (%can_request,%request_doms,$output);
1.234     raeburn  1534:     &Apache::lonnet::check_can_request($env{'user.domain'},\%can_request,\%request_doms);
                   1535:     if (keys(%request_doms) > 0) {
                   1536:         my ($types,$typename) = &Apache::loncommon::course_types();
                   1537:         if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) { 
                   1538:             my (@reqdoms,@reqtypes);
                   1539:             foreach my $type (sort(keys(%request_doms))) {
                   1540:                 push(@reqtypes,$type); 
                   1541:                 if (ref($request_doms{$type}) eq 'ARRAY') {
                   1542:                     my $domstr = join(', ',map { &Apache::lonnet::domain($_) } sort(@{$request_doms{$type}}));
1.306     raeburn  1543:                     $output .=
1.238     bisitz   1544:                         '<li>'
                   1545:                        .&mt('[_1]'.$typename->{$type}.'[_2] in domain: [_3]',
                   1546:                             '<i>',
                   1547:                             '</i>',
                   1548:                             '<b>'.$domstr.'</b>')
1.306     raeburn  1549:                        .'</li>';
1.234     raeburn  1550:                     foreach my $dom (@{$request_doms{$type}}) {
                   1551:                         unless (grep(/^\Q$dom\E/,@reqdoms)) {
                   1552:                             push(@reqdoms,$dom);
                   1553:                         }
                   1554:                     }
                   1555:                 }
                   1556:             }
                   1557:             my @showtypes;
                   1558:             foreach my $type (@{$types}) {
                   1559:                 if (grep(/^\Q$type\E$/,@reqtypes)) {
                   1560:                     push(@showtypes,$type);
                   1561:                 }
                   1562:             }
                   1563:             my $requrl = '/adm/requestcourse';
                   1564:             if (@reqdoms == 1) {
                   1565:                 $requrl .= '?showdom='.$reqdoms[0];
                   1566:             }
                   1567:             if (@showtypes > 0) {
                   1568:                 $requrl.=(($requrl=~/\?/)?'&':'?').'crstype='.$showtypes[0];
                   1569:             }
                   1570:             if (@reqdoms == 1 || @showtypes > 0) {
                   1571:                 $requrl .= '&state=crstype&action=new';
1.306     raeburn  1572:             }
1.307     raeburn  1573:             if ($output) {
                   1574:                 $r->print('<h3>'.&mt('Request creation of a course or community').'</h3>'.
                   1575:                           '<p>'.
                   1576:                           &mt('You have rights to request the creation of courses and/or communities in the following domain(s):').
                   1577:                           '<ul>'.
                   1578:                           $output.
                   1579:                           '</ul>'.
                   1580:                           &mt('Use the [_1]request form[_2] to submit a request for creation of a new course or community.',
                   1581:                               '<a href="'.$requrl.'">','</a>').
                   1582:                           '</p>');
                   1583:             }
1.234     raeburn  1584:         }
1.302     raeburn  1585:     } elsif (!$env{'user.adv'}) {
1.306     raeburn  1586:        if ($inrole) {
                   1587:             $r->print('<h3>'.&mt('Currently no additional roles, courses or communities').'</h3>');
                   1588:         } else {
                   1589:             $r->print('<h3>'.&mt('Currently no active roles, courses or communities').'</h3>');
                   1590:         }
1.302     raeburn  1591:         &findcourse_advice($r,$cattype);
1.234     raeburn  1592:     }
                   1593:     return;
                   1594: }
                   1595: 
1.175     albertel 1596: sub privileges_info {
                   1597:     my ($which) = @_;
                   1598:     my $output;
                   1599: 
                   1600:     $which ||= $env{'request.role'};
                   1601: 
                   1602:     foreach my $envkey (sort(keys(%env))) {
                   1603: 	next if ($envkey!~/^user\.priv\.\Q$which\E\.(.*)/);
                   1604: 
                   1605: 	my $where=$1;
                   1606: 	my $ttype;
                   1607: 	my $twhere;
                   1608: 	my (undef,$tdom,$trest,$tsec)=split(m{/},$where);
                   1609: 	if ($trest) {
                   1610: 	    if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
1.288     raeburn  1611: 		$ttype='Authoring Space';
1.175     albertel 1612: 		$twhere='User: '.$trest.', Domain: '.$tdom;
                   1613: 	    } else {
                   1614: 		$ttype= &Apache::loncommon::course_type($tdom.'_'.$trest);
                   1615: 		$twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
                   1616: 		if ($tsec) {
                   1617: 		    my $sec_type = 'Section';
                   1618: 		    if (exists($env{"user.role.gr.$where"})) {
                   1619: 			$sec_type = 'Group';
                   1620: 		    }
                   1621: 		    $twhere.=' ('.$sec_type.': '.$tsec.')';
                   1622: 		}
                   1623: 	    }
                   1624: 	} elsif ($tdom) {
                   1625: 	    $ttype='Domain';
                   1626: 	    $twhere=$tdom;
                   1627: 	} else {
                   1628: 	    $ttype='System';
                   1629: 	    $twhere='/';
                   1630: 	}
1.204     bisitz   1631: 	$output .= "\n<h3>".&mt($ttype).': '.$twhere.'</h3>'."\n<ul>";
1.175     albertel 1632: 	foreach my $priv (sort(split(/:/,$env{$envkey}))) {
                   1633: 	    next if (!$priv);
                   1634: 
                   1635: 	    my ($prv,$restr)=split(/\&/,$priv);
                   1636: 	    my $trestr='';
                   1637: 	    if ($restr ne 'F') {
                   1638: 		$trestr.=' ('.
                   1639: 		    join(', ',
                   1640: 			 map { &Apache::lonnet::plaintext($_) } 
                   1641: 			     (split('',$restr))).') ';
                   1642: 	    }
                   1643: 	    $output .= "\n\t".
                   1644: 		'<li>'.&Apache::lonnet::plaintext($prv).$trestr.'</li>';
                   1645: 	}
                   1646: 	$output .= "\n".'</ul>';
                   1647:     }
                   1648:     return $output;
                   1649: }
                   1650: 
1.110     raeburn  1651: sub build_roletext {
1.313     raeburn  1652:     my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$trole,$twhere,
                   1653:         $tpstart,$tpend,$nochoose,$button,$switchserver,$reinit,$switchwarning,$skipcal) = @_;
1.223     raeburn  1654:     my ($roletext,$roletext_end);
1.132     albertel 1655:     my $is_dc=($trolecode =~ m/^dc\./);
                   1656:     my $rowspan=($is_dc) ? ''
                   1657:                          : ' rowspan="2" ';
                   1658: 
1.110     raeburn  1659:     unless ($nochoose) {
1.134     www      1660:         my $buttonname=$trolecode;
                   1661:         $buttonname=~s/\W//g;
1.110     raeburn  1662:         if (!$button) {
                   1663:             if ($switchserver) {
1.212     bisitz   1664:                 $roletext.='<td'.$rowspan.' class="'.$tbg.'">'
                   1665:                           .'<a href="/adm/switchserver?'.$switchserver.'">'
                   1666:                           .&mt('Switch Server')
                   1667:                           .'</a></td>';
1.110     raeburn  1668:             } else {
1.212     bisitz   1669:                 $roletext.=('<td'.$rowspan.' class="'.$tbg.'">&nbsp;</td>');
1.110     raeburn  1670:             }
1.255     raeburn  1671:             if ($switchwarning) {
                   1672:                 if ($tremark eq '') {
                   1673:                     $tremark = $switchwarning;
                   1674:                 } else {
                   1675:                     $tremark .= '<br />'.$switchwarning;
                   1676:                 }
                   1677:             }
1.110     raeburn  1678:         } elsif ($tstatus eq 'is') {
1.212     bisitz   1679:             $roletext.='<td'.$rowspan.' class="'.$tbg.'">'.
                   1680:                         '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1681:                         &mt('Select').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  1682:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1683:         } elsif ($tryagain) {
                   1684:             $roletext.=
1.212     bisitz   1685:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1686:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1687:                 &mt('Try Selecting Again').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  1688:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1689:         } elsif ($advanced) {
                   1690:             $roletext.=
1.212     bisitz   1691:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1692:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1693:                 &mt('Re-Initialize').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  1694:                         $trolecode."','".$buttonname.'\');" /></td>';
1.209     raeburn  1695:         } elsif ($reinit) {
                   1696:             $roletext.= 
1.212     bisitz   1697:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1698:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1699:                 &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1.209     raeburn  1700:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1701:         } else {
1.209     raeburn  1702:             $roletext.=
1.212     bisitz   1703:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1704:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1705:                 &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1.209     raeburn  1706:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1707:         }
                   1708:     }
1.313     raeburn  1709:     if (($trolecode !~ m/^(dc|ca|au|aa)\./)  && (!$skipcal)) {
1.165     albertel 1710: 	$tremark.=&Apache::lonannounce::showday(time,1,
                   1711: 			 &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                   1712:     }
1.212     bisitz   1713:     $roletext.='<td>'.$trole.'</td>'
                   1714:               .'<td>'.$twhere.'</td>'
                   1715:               .'<td>'.$tpstart.'</td>'
1.223     raeburn  1716:               .'<td>'.$tpend.'</td>';
1.132     albertel 1717:     if (!$is_dc) {
1.223     raeburn  1718:         $roletext_end = '<td colspan="4">'.
                   1719:                         $tremark.'&nbsp;'.
                   1720:                         '</td>';
1.132     albertel 1721:     }
1.223     raeburn  1722:     return ($roletext,$roletext_end);
1.110     raeburn  1723: }
                   1724: 
1.193     raeburn  1725: sub check_author_homeserver {
1.183     www      1726:     my ($uname,$udom)=@_;
1.193     raeburn  1727:     if (($uname eq '') || ($udom eq '')) {
                   1728:         return ('fail','');
                   1729:     }
1.183     www      1730:     my $home = &Apache::lonnet::homeserver($uname,$udom);
1.193     raeburn  1731:     if (&Apache::lonnet::host_domain($home) ne $udom) {
                   1732:         return ('fail',$home);
                   1733:     }
1.183     www      1734:     my @ids=&Apache::lonnet::current_machine_ids();
1.193     raeburn  1735:     if (grep(/^\Q$home\E$/,@ids)) {
                   1736:         return ('ok',$home);
                   1737:     } else {
                   1738:         return ('switchserver',$home);
1.183     www      1739:     }
                   1740: }
                   1741: 
1.104     raeburn  1742: sub check_fordc {
1.260     raeburn  1743:     my ($dcroles,$update,$then) = @_;
1.104     raeburn  1744:     my $numdc = 0;
1.118     albertel 1745:     if ($env{'user.adv'}) {
1.309     raeburn  1746:         foreach my $envkey (sort(keys(%env))) {
1.170     albertel 1747:             if ($envkey=~/^user\.role\.dc\.\/($match_domain)\/$/) {
1.104     raeburn  1748:                 my $dcdom = $1;
                   1749:                 my $livedc = 1;
1.118     albertel 1750:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
1.260     raeburn  1751:                 my $limit = $update;
                   1752:                 if ($env{'request.role'} eq 'dc./'.$dcdom.'/') {
                   1753:                     $limit = $then;
                   1754:                 }
                   1755:                 if ($tstart && $tstart>$limit) { $livedc = 0; }
                   1756:                 if ($tend   && $tend  <$limit) { $livedc = 0; }
1.104     raeburn  1757:                 if ($livedc) {
                   1758:                     $$dcroles{$dcdom} = $envkey;
1.105     raeburn  1759:                     $numdc++;
1.104     raeburn  1760:                 }
                   1761:             }
                   1762:         }
                   1763:     }
                   1764:     return $numdc;
                   1765: }
                   1766: 
1.185     raeburn  1767: sub adhoc_course_role {
1.260     raeburn  1768:     my ($refresh,$update,$then) = @_;
1.239     raeburn  1769:     my ($cdom,$cnum,$crstype);
1.201     raeburn  1770:     $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1771:     $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.239     raeburn  1772:     $crstype = &Apache::loncommon::course_type();
1.260     raeburn  1773:     if (&check_forcc($cdom,$cnum,$refresh,$update,$then,$crstype)) {
1.185     raeburn  1774:         my $setprivs;
1.198     raeburn  1775:         if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
1.185     raeburn  1776:             $setprivs = 1;
                   1777:         } else {
1.198     raeburn  1778:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
1.232     raeburn  1779:             if (($start && ($start>$refresh || $start == -1)) ||
1.260     raeburn  1780:                 ($end && $end<$update)) {
1.185     raeburn  1781:                 $setprivs = 1;
                   1782:             }
1.232     raeburn  1783:         }
1.278     raeburn  1784:         unless ($setprivs) {
                   1785:             if (!exists($env{'user.priv.'.$env{'form.switchrole'}.'./'})) {
                   1786:                 $setprivs = 1;
                   1787:             }
                   1788:         }
1.185     raeburn  1789:         if ($setprivs) {
1.297     raeburn  1790:             if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)(.*?)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
1.185     raeburn  1791:                 my $role = $1;
                   1792:                 my $custom_role = $2;
                   1793:                 my $usec = $3;
                   1794:                 if ($role eq 'cr') {
1.199     raeburn  1795:                     if ($custom_role =~ m-^/$match_domain/$match_username/\w+$-) {
1.185     raeburn  1796:                         $role .= $custom_role;
                   1797:                     } else {
                   1798:                         return;
                   1799:                     }
                   1800:                 }
1.208     raeburn  1801:                 my (%userroles,%newrole,%newgroups,%group_privs);
                   1802:                 my %cgroups =
                   1803:                     &Apache::lonnet::get_active_groups($env{'user.domain'},
                   1804:                                             $env{'user.name'},$cdom,$cnum);
                   1805:                 foreach my $group (keys(%cgroups)) {
                   1806:                     $group_privs{$group} =
                   1807:                         $env{'user.priv.cc./'.$cdom.'/'.$cnum.'./'.$cdom.'/'.$cnum.'/'.$group};
                   1808:                 }
                   1809:                 $newgroups{'/'.$cdom.'/'.$cnum} = \%group_privs;
1.185     raeburn  1810:                 my $area = '/'.$cdom.'/'.$cnum;
                   1811:                 my $spec = $role.'.'.$area;
                   1812:                 if ($usec ne '') {
                   1813:                     $spec .= '/'.$usec;
                   1814:                     $area .= '/'.$usec;
                   1815:                 }
1.278     raeburn  1816:                 if ($role =~ /^cr/) {
                   1817:                     &Apache::lonnet::custom_roleprivs(\%newrole,$role,$cdom,$cnum,$spec,$area);
                   1818:                 } else {
                   1819:                     &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
                   1820:                 }
1.208     raeburn  1821:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1.232     raeburn  1822:                 my $adhocstart = $refresh-1;
1.185     raeburn  1823:                 $userroles{'user.role.'.$spec} = $adhocstart.'.';
1.186     raeburn  1824:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.185     raeburn  1825:             }
                   1826:         }
                   1827:     }
                   1828:     return;
                   1829: }
                   1830: 
                   1831: sub check_forcc {
1.260     raeburn  1832:     my ($cdom,$cnum,$refresh,$update,$then,$crstype) = @_;
1.239     raeburn  1833:     my ($is_cc,$ccrole);
                   1834:     if ($crstype eq 'Community') {
                   1835:         $ccrole = 'co';
                   1836:     } else {
                   1837:         $ccrole = 'cc';
                   1838:     }
1.266     droeschl 1839:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
                   1840:         my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum;
                   1841:         if (defined($env{$envkey})) {
                   1842:             $is_cc = 1;
                   1843:             my ($tstart,$tend)=split(/\./,$env{$envkey});
                   1844:             my $limit = $update;
                   1845:             if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) {
                   1846:                 $limit = $then;
1.185     raeburn  1847:             }
1.266     droeschl 1848:             if ($tstart && $tstart>$refresh) { $is_cc = 0; }
                   1849:             if ($tend   && $tend  <$limit) { $is_cc = 0; }
1.185     raeburn  1850:         }
                   1851:     }
                   1852:     return $is_cc;
                   1853: }
                   1854: 
1.108     raeburn  1855: sub courselink {
1.193     raeburn  1856:     my ($dcdom,$rowtype) = @_;
1.109     raeburn  1857:     my $courseform=&Apache::loncommon::selectcourse_link
1.152     raeburn  1858:                    ('rolechoice','dccourse'.$rowtype.'_'.$dcdom,
                   1859:                     'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'.
1.239     raeburn  1860:                     $dcdom,$dcdom,undef,'Course/Community');
1.138     raeburn  1861:     my $hiddenitems = '<input type="hidden" name="dcdomain'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                   1862:                       '<input type="hidden" name="origdom'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                   1863:                       '<input type="hidden" name="dccourse'.$rowtype.'_'.$dcdom.'" value="" />'.
                   1864:                       '<input type="hidden" name="coursedesc'.$rowtype.'_'.$dcdom.'" value="" />';
1.112     raeburn  1865:     return $courseform.$hiddenitems;
1.109     raeburn  1866: }
                   1867: 
                   1868: sub coursepick_jscript {
1.312     damieng  1869:     my %js_lt = &Apache::lonlocal::texthash(
1.239     raeburn  1870:                   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.",
1.234     raeburn  1871:                   youc => 'You can only use this screen to select courses and communities in the current domain.',
1.184     raeburn  1872:              );
1.312     damieng  1873:     &js_escape(\%js_lt);
1.104     raeburn  1874:     my $verify_script = <<"END";
1.179     raeburn  1875: <script type="text/javascript">
1.225     bisitz   1876: // <![CDATA[
1.108     raeburn  1877: function verifyCoursePick(caller) {
                   1878:     var numbutton = getIndex(caller)
1.112     raeburn  1879:     var pickedCourse = document.rolechoice.elements[numbutton+4].value
                   1880:     var pickedDomain = document.rolechoice.elements[numbutton+2].value
                   1881:     if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104     raeburn  1882:         if (pickedCourse != '') {
1.108     raeburn  1883:             if (numbutton != -1) {
                   1884:                 var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                   1885:                 document.rolechoice.elements[numbutton+1].name = courseTarget
                   1886:                 document.rolechoice.submit()
                   1887:             }
1.104     raeburn  1888:         }
                   1889:         else {
1.312     damieng  1890:             alert("$js_lt{'plsu'}");
1.104     raeburn  1891:         }
                   1892:     }
                   1893:     else {
1.312     damieng  1894:         alert("$js_lt{'youc'}")
1.104     raeburn  1895:     }
                   1896: }
1.109     raeburn  1897: function getIndex(caller) {
1.108     raeburn  1898:     for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109     raeburn  1899:         if (document.rolechoice.elements[i] == caller) {
1.108     raeburn  1900:             return i;
                   1901:         }
                   1902:     }
                   1903:     return -1;
                   1904: }
1.225     bisitz   1905: // ]]>
1.104     raeburn  1906: </script>
                   1907: END
1.109     raeburn  1908:     return $verify_script;
1.104     raeburn  1909: }
                   1910: 
1.193     raeburn  1911: sub coauthorlink {
                   1912:     my ($dcdom,$rowtype) = @_;
                   1913:     my $coauthorform=&Apache::loncommon::selectauthor_link('rolechoice',$dcdom);
                   1914:     my $hiddenitems = '<input type="hidden" name="adhoccauname'.$rowtype.'_'.$dcdom.'" value="" />';
                   1915:     return $coauthorform.$hiddenitems;
                   1916: }
                   1917: 
1.113     raeburn  1918: sub display_cc_role {
                   1919:     my $rolekey = shift;
1.223     raeburn  1920:     my ($roletext,$roletext_end);
1.118     albertel 1921:     my $advanced = $env{'user.adv'};
                   1922:     my $tryagain = $env{'form.tryagain'};
1.113     raeburn  1923:     unless ($rolekey =~/^error\:/) {
1.240     raeburn  1924:         if ($rolekey =~ m{^user\.role\.(cc|co)\./($match_domain)/($match_courseid)$}) {
                   1925:             my $ccrole = $1;
1.249     raeburn  1926:             my $tdom = $2;
                   1927:             my $trest = $3;
                   1928:             my $tcourseid = $tdom.'_'.$trest;
                   1929:             my $trolecode = $ccrole.'./'.$tdom.'/'.$trest;
1.113     raeburn  1930:             my $twhere;
1.152     raeburn  1931:             my $ttype;
1.313     raeburn  1932:             my $skipcal;
1.212     bisitz   1933:             my $tbg='LC_roles_is';
1.113     raeburn  1934:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   1935:             if (%newhash) {
                   1936:                 $twhere=$newhash{'description'}.
1.261     bisitz   1937:                         ' <span class="LC_fontsize_small">'.
1.249     raeburn  1938:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
1.211     tempelho 1939:                         '</span>';
1.153     raeburn  1940:                 $ttype = $newhash{'type'};
1.113     raeburn  1941:             } else {
                   1942:                 $twhere=&mt('Currently not available');
1.118     albertel 1943:                 $env{'course.'.$tcourseid.'.description'}=$twhere;
1.313     raeburn  1944:                 $skipcal = 1;
1.110     raeburn  1945:             }
1.242     raeburn  1946:             my $trole = &Apache::lonnet::plaintext($ccrole,$ttype,$tcourseid);
1.258     raeburn  1947:             $twhere.="<br />".&mt('Domain').":".$tdom;
1.313     raeburn  1948:             ($roletext,$roletext_end) = &build_roletext($trolecode,$tdom,$trest,'is',$tryagain,$advanced,'',$tbg,$trole,$twhere,'','','',1,'','','',$skipcal);
1.104     raeburn  1949:         }
                   1950:     }
1.223     raeburn  1951:     return ($roletext,$roletext_end);
1.104     raeburn  1952: }
                   1953: 
1.192     raeburn  1954: sub adhoc_roles_row {
1.138     raeburn  1955:     my ($dcdom,$rowtype) = @_;
1.212     bisitz   1956:     my $output = &Apache::loncommon::continue_data_table_row()
1.314     raeburn  1957:                  .' <td colspan="5" class="LC_textsize_mobile">'
1.212     bisitz   1958:                  .&mt('[_1]Ad hoc[_2] roles in domain [_3] --'
                   1959:                      ,'<span class="LC_cusr_emph">','</span>',$dcdom)
1.227     bisitz   1960:                  .' ';
1.193     raeburn  1961:     my $selectcclink = &courselink($dcdom,$rowtype);
1.239     raeburn  1962:     my $ccrole = &Apache::lonnet::plaintext('co',undef,undef,1);
1.182     www      1963:     my $carole = &Apache::lonnet::plaintext('ca');
1.193     raeburn  1964:     my $selectcalink = &coauthorlink($dcdom,$rowtype);
1.227     bisitz   1965:     $output.=$ccrole.': '.$selectcclink
1.249     raeburn  1966:             .' | '.$carole.': '.$selectcalink.'</td>'
1.212     bisitz   1967:             .&Apache::loncommon::end_data_table_row();
1.108     raeburn  1968:     return $output;
                   1969: }
                   1970: 
1.104     raeburn  1971: sub recent_filename {
                   1972:     my $area=shift;
1.149     www      1973:     return 'nohist_recent_'.&escape($area);
1.104     raeburn  1974: }
                   1975: 
1.139     raeburn  1976: sub courseloadpage {
                   1977:     my ($courseid) = @_;
                   1978:     my $startpage;
1.144     albertel 1979:     my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
                   1980: 					      [$courseid.':courseinit']);
1.139     raeburn  1981:     my ($tmp) = %entry_settings;
1.144     albertel 1982:     unless ($tmp =~ /^error: 2 /) {
1.139     raeburn  1983:         $startpage = $entry_settings{$courseid.':courseinit'};
                   1984:     }
                   1985:     if ($startpage eq '') {
                   1986:         if (exists($env{'environment.course_init_display'})) {
                   1987:             $startpage = $env{'environment.course_init_display'};
                   1988:         }
                   1989:     }
                   1990:     return $startpage;
                   1991: }
                   1992: 
1.260     raeburn  1993: sub update_session_roles {
                   1994:     my $then=$env{'user.login.time'};
                   1995:     my $refresh=$env{'user.refresh.time'};
                   1996:     if (!$refresh) {
                   1997:         $refresh = $then;
                   1998:     }
                   1999:     my $update = $env{'user.update.time'};
                   2000:     if (!$update) {
                   2001:         $update = $then;
                   2002:     }
                   2003:     my $now = time;
                   2004:     my %roleshash =
                   2005:         &Apache::lonnet::get_my_roles('','','userroles',
                   2006:                                       ['active','future','previous'],
                   2007:                                       undef,undef,1);
                   2008:     my ($msg,@newsec,$oldsec,$currrole_expired,@changed_roles,
1.264     raeburn  2009:         %changed_groups,%dbroles,%deletedroles,%allroles,%allgroups,
1.260     raeburn  2010:         %userroles,%checkedgroup,%crprivs,$hasgroups,%rolechange,
                   2011:         %groupchange,%newrole,%newgroup,%customprivchg,%groups_roles,
                   2012:         @rolecodes);
                   2013:     my @possroles = ('cr','st','ta','ad','ep','in','co','cc');
                   2014:     my %courseroles;
                   2015:     foreach my $item (keys(%roleshash)) {
                   2016:         my ($uname,$udom,$role,$remainder) = split(/:/,$item,4);
                   2017:         my ($tstart,$tend) = split(/:/,$roleshash{$item});
                   2018:         my ($section,$group,@group_privs);
                   2019:         if ($role =~ m{^gr/(\w*)$}) {
                   2020:             $role = 'gr';
                   2021:             my $priv = $1;
                   2022:             next if ($tstart eq '-1');
                   2023:             if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
                   2024:                 if ($priv ne '') {
                   2025:                     push(@group_privs,$priv);
                   2026:                 }
                   2027:             }
                   2028:             if ($remainder =~ /:/) {
                   2029:                 (my $additional_privs,$group) =
                   2030:                     ($remainder =~ /^([\w:]+):([^:]+)$/);
                   2031:                 if ($additional_privs ne '') {
                   2032:                     if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
                   2033:                         push(@group_privs,split(/:/,$additional_privs));
                   2034:                         @group_privs = sort(@group_privs);
                   2035:                     }
                   2036:                 }
                   2037:             } else {
                   2038:                 $group = $remainder;
                   2039:             }
                   2040:         } else {
                   2041:             $section = $remainder;
                   2042:         }
                   2043:         my $where = "/$udom/$uname";
                   2044:         if ($section ne '') {
                   2045:             $where .= "/$section";
                   2046:         } elsif ($group ne '') {
                   2047:             $where .= "/$group";
                   2048:         }
                   2049:         my $rolekey = "$role.$where";
                   2050:         my $envkey = "user.role.$rolekey";
                   2051:         $dbroles{$envkey} = 1;
                   2052:         if (($env{'request.role'} eq $rolekey) && ($role ne 'st')) {
                   2053:             if (&curr_role_status($tstart,$tend,$refresh,$now) ne 'active') {
                   2054:                 $currrole_expired = 1;
                   2055:             }
                   2056:         }
                   2057:         if ($env{$envkey} eq '') {
                   2058:             my $status_in_db =
1.271     raeburn  2059:                 &curr_role_status($tstart,$tend,$now,$now);
1.260     raeburn  2060:                 &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2061:             if (($role eq 'st') && ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
                   2062:                 if ($status_in_db eq 'active') {
                   2063:                     if ($section eq '') {
                   2064:                         push(@newsec,'none');
                   2065:                     } else {
                   2066:                         push(@newsec,$section);
                   2067:                     }
                   2068:                 }
                   2069:             } else {
                   2070:                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2071:                     push(@changed_roles,$role);
                   2072:                 }
                   2073:                 if ($status_in_db ne 'previous') {
                   2074:                     if ($role eq 'gr') {
                   2075:                         $newgroup{$rolekey} = $status_in_db;
                   2076:                         if ($status_in_db eq 'active') {
                   2077:                             unless (ref($courseroles{$udom}) eq 'HASH') {
                   2078:                                 %{$courseroles{$udom}} =
                   2079:                                     &Apache::lonnet::get_my_roles('','','userroles',
                   2080:                                                                   ['active'],\@possroles,
                   2081:                                                                   [$udom],1);
                   2082:                             }
                   2083:                             &Apache::lonnet::get_groups_roles($udom,$uname,
                   2084:                                                               $courseroles{$udom},
                   2085:                                                               \@rolecodes,\%groups_roles);
                   2086:                         }
                   2087:                     } else {
                   2088:                         $newrole{$rolekey} = $status_in_db;
                   2089:                     }
                   2090:                 }
                   2091:             }
                   2092:         } else {
                   2093:             my ($currstart,$currend) = split(/\./,$env{$envkey});
                   2094:             if ($role eq 'gr') {
                   2095:                 if (&curr_role_status($currstart,$currend,$refresh,$update) ne 'previous') {
                   2096:                     $hasgroups = 1;
                   2097:                 }
                   2098:             }
                   2099:             if (($currstart ne $tstart) || ($currend ne $tend)) {
                   2100:                 my $status_in_env =
                   2101:                     &curr_role_status($currstart,$currend,$refresh,$update);
                   2102:                 my $status_in_db =
1.271     raeburn  2103:                     &curr_role_status($tstart,$tend,$now,$now);
1.260     raeburn  2104:                 if ($status_in_env ne $status_in_db) {
                   2105:                     if ($status_in_env eq 'active') {
                   2106:                         if ($role eq 'st') {
                   2107:                             if ($env{'request.role'} eq $rolekey) {
                   2108:                                 my $switchsection;
                   2109:                                 unless (ref($courseroles{$udom}) eq 'HASH') {
                   2110:                                     %{$courseroles{$udom}} =
                   2111:                                         &Apache::lonnet::get_my_roles('','','userroles',
                   2112:                                                                       ['active'],
                   2113:                                                                       \@possroles,[$udom],1);
                   2114:                                 }
                   2115:                                 foreach my $crsrole (keys(%{$courseroles{$udom}})) {
                   2116:                                     if ($crsrole =~ /^\Q$uname\E:\Q$udom\E:st/) {
                   2117:                                         $switchsection = 1;
                   2118:                                         last;
                   2119:                                     }
                   2120:                                 }
                   2121:                                 if ($switchsection) {
                   2122:                                     if ($section eq '') {
                   2123:                                         $oldsec = 'none';
                   2124:                                     } else {
                   2125:                                         $oldsec = $section;
                   2126:                                     }
                   2127:                                     &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2128:                                 } else {
                   2129:                                     $currrole_expired = 1;
                   2130:                                     next;
                   2131:                                 }
                   2132:                             }
                   2133:                         }
                   2134:                         unless ($rolekey eq $env{'request.role'}) {
                   2135:                             if ($role eq 'gr') {
                   2136:                                 &Apache::lonnet::delete_env_groupprivs($where,\%courseroles,\@possroles);
                   2137:                             } else {
                   2138:                                 &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
                   2139:                                 &Apache::lonnet::delenv("user.priv.cm.$where",undef,['cm']);
                   2140:                             }
                   2141:                             &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2142:                         }
                   2143:                     } elsif ($status_in_db eq 'active') {
                   2144:                         if (($role eq 'st') &&
                   2145:                             ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
                   2146:                             if ($section eq '') {
                   2147:                                 push(@newsec,'none');
                   2148:                             } else {
                   2149:                                 push(@newsec,$section);
                   2150:                             }
                   2151:                         } elsif ($role eq 'gr') {
                   2152:                             unless (ref($courseroles{$udom}) eq 'HASH') {
                   2153:                                 %{$courseroles{$udom}} =
                   2154:                                     &Apache::lonnet::get_my_roles('','','userroles',
                   2155:                                                                   ['active'],
                   2156:                                                                   \@possroles,[$udom],1);
                   2157:                             }
                   2158:                             &Apache::lonnet::get_groups_roles($udom,$uname,
                   2159:                                                               $courseroles{$udom},
                   2160:                                                               \@rolecodes,\%groups_roles);
                   2161:                         }
                   2162:                         &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2163:                     }
                   2164:                     unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2165:                         push(@changed_roles,$role);
                   2166:                     }
                   2167:                     if ($role eq 'gr') {
                   2168:                         $groupchange{"/$udom/$uname"}{$group} = $status_in_db;
                   2169:                     } else {
                   2170:                         $rolechange{$rolekey} = $status_in_db;
                   2171:                     }
                   2172:                 }
                   2173:             } else {
                   2174:                 if ($role eq 'gr') {
                   2175:                     unless ($checkedgroup{$where}) {
                   2176:                         my $status_in_db =
                   2177:                             &curr_role_status($tstart,$tend,$refresh,$now);
                   2178:                         if ($tstart eq '-1') {
                   2179:                             $status_in_db = 'deleted';
                   2180:                         }
                   2181:                         unless (ref($courseroles{$udom}) eq 'HASH') {
                   2182:                             %{$courseroles{$udom}} =
                   2183:                                 &Apache::lonnet::get_my_roles('','','userroles',
                   2184:                                                               ['active'],
                   2185:                                                               \@possroles,[$udom],1);
                   2186:                         }
                   2187:                         if (ref($courseroles{$udom}) eq 'HASH') {
                   2188:                             foreach my $item (keys(%{$courseroles{$udom}})) {
                   2189:                                 next unless ($item =~ /^\Q$uname\E/);
                   2190:                                 my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   2191:                                 my $area = '/'.$cdom.'/'.$cnum;
                   2192:                                 if ($crssec ne '') {
                   2193:                                     $area .= '/'.$crssec;
                   2194:                                 }
                   2195:                                 my $crsrolekey = $crsrole.'.'.$area;
                   2196:                                 my $currprivs = $env{'user.priv.'.$crsrole.'.'.$area.'.'.$where};
                   2197:                                 $currprivs =~ s/^://;
                   2198:                                 $currprivs =~ s/\&F$//;
                   2199:                                 my @curr_grp_privs = split(/\&F:/,$currprivs);
                   2200:                                 @curr_grp_privs = sort(@curr_grp_privs);
                   2201:                                 my @diffs;
                   2202:                                 if (@group_privs > 0 || @curr_grp_privs > 0) {
                   2203:                                     @diffs = &Apache::loncommon::compare_arrays(\@group_privs,\@curr_grp_privs);
                   2204:                                 }
                   2205:                                 if (@diffs == 0) {
                   2206:                                     last;
                   2207:                                 } else {
                   2208:                                     unless(grep(/^\Qgr\E$/,@rolecodes)) {
                   2209:                                         push(@rolecodes,'gr');
                   2210:                                     }
                   2211:                                     &gather_roleprivs(\%allroles,\%allgroups,
                   2212:                                                       \%userroles,$where,$role,
                   2213:                                                       $tstart,$tend,$status_in_db);
                   2214:                                     if ($status_in_db eq 'active') {
                   2215:                                         &Apache::lonnet::get_groups_roles($udom,$uname,
                   2216:                                                                           $courseroles{$udom},
                   2217:                                                                           \@rolecodes,\%groups_roles);
                   2218:                                     }
                   2219:                                     $changed_groups{$udom.'_'.$uname}{$group} = $status_in_db;
                   2220:                                     last;
                   2221:                                 }
                   2222:                             }
                   2223:                         }
                   2224:                         $checkedgroup{$where} = 1;
                   2225:                     }
                   2226:                 } elsif ($role =~ /^cr/) {
                   2227:                     my $status_in_db =
                   2228:                         &curr_role_status($tstart,$tend,$refresh,$now);
                   2229:                     my ($rdummy,$rest) = split(/\//,$role,2);
                   2230:                     my %currpriv;
                   2231:                     unless (exists($crprivs{$rest})) {
                   2232:                         my ($rdomain,$rauthor,$rrole)=split(/\//,$rest);
                   2233:                         my $homsvr=&Apache::lonnet::homeserver($rauthor,$rdomain);
                   2234:                         if (&Apache::lonnet::hostname($homsvr) ne '') {
                   2235:                             my ($rdummy,$roledef)=
                   2236:                             &Apache::lonnet::get('roles',["rolesdef_$rrole"],
                   2237:                                                  $rdomain,$rauthor);
                   2238:                             if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   2239:                                 my $i = 0;
                   2240:                                 my @scopes = ('sys','dom','crs');
                   2241:                                 my @privs = split(/\_/,$roledef);
                   2242:                                 foreach my $priv (@privs) {
                   2243:                                     my ($blank,@prv) = split(/:/,$priv);
                   2244:                                     @prv = map { $_ .= (/\&\w+$/ ? '':'&F') } @prv;
1.264     raeburn  2245:                                     if (@prv) {
                   2246:                                         $priv = ':'.join(':',sort(@prv));
                   2247:                                     }
1.260     raeburn  2248:                                     $crprivs{$rest}{$scopes[$i]} = $priv;
                   2249:                                     $i++;
                   2250:                                 }
                   2251:                             }
                   2252:                         }
                   2253:                     }
1.279     raeburn  2254:                     my $status_in_env =
                   2255:                         &curr_role_status($currstart,$currend,$refresh,$update);
                   2256:                     if ($status_in_env eq 'active') {
                   2257:                         $currpriv{sys} = $env{"user.priv.$rolekey./"};
                   2258:                         $currpriv{dom} = $env{"user.priv.$rolekey./$udom/"};
                   2259:                         $currpriv{crs} = $env{"user.priv.$rolekey.$where"};
                   2260:                         if (keys(%crprivs)) {
                   2261:                             if (($crprivs{$rest}{sys} ne $currpriv{sys}) ||
                   2262:                                 ($crprivs{$rest}{dom} ne $currpriv{dom})
1.260     raeburn  2263:  ||
1.279     raeburn  2264:                                 ($crprivs{$rest}{crs} ne $currpriv{crs})) {
                   2265:                                 &gather_roleprivs(\%allroles,\%allgroups,
                   2266:                                                   \%userroles,$where,$role,
                   2267:                                                   $tstart,$tend,$status_in_db);
                   2268:                                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2269:                                     push(@changed_roles,$role);
                   2270:                                 }
1.260     raeburn  2271:                                 $customprivchg{$rolekey} = $status_in_env;
                   2272:                             }
                   2273:                         }
                   2274:                     }
                   2275:                 }
                   2276:             }
                   2277:         }
                   2278:     }
                   2279:     foreach my $envkey (keys(%env)) {
                   2280:         next unless ($envkey =~ /^user\.role\./);
                   2281:         next if ($dbroles{$envkey});
                   2282:         next if ($envkey eq 'user.role.'.$env{'request.role'});
                   2283:         my ($currstart,$currend) = split(/\./,$env{$envkey});
                   2284:         my $status_in_env =
                   2285:             &curr_role_status($currstart,$currend,$refresh,$update);
                   2286:         my ($rolekey) = ($envkey =~ /^user\.role\.(.+)$/);
1.297     raeburn  2287:         my ($role,$rest)=split(m{\./},$rolekey,2);
                   2288:         $rest = '/'.$rest;
1.260     raeburn  2289:         if (&Apache::lonnet::delenv($envkey,undef,[$role])) {
                   2290:             if ($status_in_env eq 'active') {
                   2291:                 if ($role eq 'gr') {
                   2292:                     &Apache::lonnet::delete_env_groupprivs($rest,\%courseroles,
                   2293:                                                            \@possroles);
                   2294:                 } else {
                   2295:                     &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
                   2296:                     &Apache::lonnet::delenv("user.priv.cm.$rest",undef,['cm']);
                   2297:                 }
                   2298:                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2299:                     push(@changed_roles,$role);
                   2300:                 }
                   2301:                 $deletedroles{$rolekey} = 1;
                   2302:             }
                   2303:         }
                   2304:     }
                   2305:     if (($oldsec) && (@newsec > 0)) {
                   2306:         if (@newsec > 1) {
1.274     raeburn  2307:             $msg = '<p class="LC_warning">'.&mt('The section has changed for your current role. Log-out and log-in again to select a role for the new section.').'</p>';
1.260     raeburn  2308:         } else {
                   2309:             my $newrole = $env{'request.role'};
                   2310:             if ($newsec[0] eq 'none') {
                   2311:                 $newrole =~ s{(/[^/])$}{};
                   2312:             } elsif ($oldsec eq 'none') {
                   2313:                 $newrole .= '/'.$newsec[0];
                   2314:             } else {
                   2315:                 $newrole =~ s{([^/]+)$}{$newsec[0]};
                   2316:             }
                   2317:             my $coursedesc = $env{'course.'.$env{'request.course.id'}.'.description'};
                   2318:             my ($curr_role) = ($env{'request.role'} =~ m{^(\w+)\./$match_domain/$match_courseid});
                   2319:             my %temp=('logout_'.$env{'request.course.id'} => time);
                   2320:             &Apache::lonnet::put('email_status',\%temp);
                   2321:             &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
                   2322:             &Apache::lonnet::appenv({"request.course.id"   => '',
                   2323:                                      "request.course.fn"   => '',
                   2324:                                      "request.course.uri"  => '',
                   2325:                                      "request.course.sec"  => '',
                   2326:                                      "request.role"        => 'cm',
                   2327:                                      "request.role.adv"    => $env{'user.adv'},
                   2328:                                      "request.role.domain" => $env{'user.domain'}});
                   2329:             my $rolename = &Apache::loncommon::plainname($curr_role);
                   2330:             $msg = '<p><form name="reselectrole" action="/adm/roles" method="post" />'.
                   2331:                    '<input type="hidden" name="newrole" value="" />'.
                   2332:                    '<input type="hidden" name="selectrole" value="1" />'.
                   2333:                    '<span class="LC_info">'.
                   2334:                    &mt('Your section has changed for your current [_1] role in [_2].',$rolename,$coursedesc).'</span><br />';
                   2335:             my $button = '<input type="button" name="sectionchanged" value="'.
                   2336:                          &mt('Re-Select').'" onclick="javascript:enterrole(this.form,'."'$newrole','sectionchanged'".')" />';
                   2337:             if ($newsec[0] eq 'none') {
                   2338:                 $msg .= &mt('[_1] to continue with your new section-less role.',$button);
                   2339:             } else {
                   2340:                 $msg .= &mt('[_1] to continue with your new role in section ([_2]).',$button,$newsec[0]);
                   2341:             }
                   2342:             $msg .= '</form></p>';
                   2343:         }
                   2344:     } elsif ($currrole_expired) {
1.274     raeburn  2345:         $msg .= '<p class="LC_warning">';
1.260     raeburn  2346:         if (&Apache::loncommon::show_course()) {
                   2347:             $msg .= &mt('Your role in the current course has expired.');
                   2348:         } else {
                   2349:             $msg .= &mt('Your current role has expired.');
                   2350:         }
1.274     raeburn  2351:         $msg .= '<br />'.&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.').'</p>';
1.260     raeburn  2352:     }
1.279     raeburn  2353:     &Apache::lonnet::set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   2354:     my ($curr_is_adv,$curr_role_adv,$curr_author,$curr_role_author);
                   2355:     $curr_author = $env{'user.author'};
                   2356:     if (($env{'request.role'} =~/^au/) || ($env{'request.role'} =~/^ca/) ||
                   2357:         ($env{'request.role'} =~/^aa/)) {
                   2358:         $curr_role_author=1;
                   2359:     }
                   2360:     $curr_is_adv = $env{'user.adv'};
                   2361:     $curr_role_adv = $env{'request.role.adv'};
                   2362:     if (keys(%userroles) > 0) {
                   2363:         foreach my $role (@changed_roles) {
                   2364:             unless(grep(/^\Q$role\E$/,@rolecodes)) {
                   2365:                 push(@rolecodes,$role);
                   2366:             }
                   2367:         }
                   2368:         unless(grep(/^\Qcm\E$/,@rolecodes)) {
                   2369:             push(@rolecodes,'cm');
                   2370:         }
                   2371:         &Apache::lonnet::appenv(\%userroles,\@rolecodes);
                   2372:     }
                   2373:     my %newenv;
                   2374:     if (&Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'})) {
                   2375:         unless ($curr_is_adv) {
                   2376:             $newenv{'user.adv'} = 1;
                   2377:         }
                   2378:     } elsif ($curr_is_adv && !$curr_role_adv) {
                   2379:         &Apache::lonnet::delenv('user.adv');
                   2380:     }
                   2381:     my %authorroleshash =
                   2382:         &Apache::lonnet::get_my_roles('','','userroles',['active'],['au','ca','aa']);
                   2383:     if (keys(%authorroleshash)) {
                   2384:         unless ($curr_author) {
                   2385:             $newenv{'user.author'} = 1;
                   2386:         }
                   2387:     } elsif ($curr_author && !$curr_role_author) {
                   2388:         &Apache::lonnet::delenv('user.author');
                   2389:     }
                   2390:     if ($env{'request.course.id'}) {
                   2391:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2392:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2393:         my (@activecrsgroups,$crsgroupschanged);
                   2394:         if ($env{'request.course.groups'}) {
                   2395:             @activecrsgroups = split(/:/,$env{'request.course.groups'});
                   2396:             foreach my $item (keys(%deletedroles)) {
                   2397:                 if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
                   2398:                     if (grep(/^\Q$1\E$/,@activecrsgroups)) {
                   2399:                         $crsgroupschanged = 1;
                   2400:                         last;
                   2401:                     }
                   2402:                 }
                   2403:             }
                   2404:         }
                   2405:         unless ($crsgroupschanged) {
                   2406:             foreach my $item (keys(%newgroup)) {
                   2407:                 if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
                   2408:                     if ($newgroup{$item} eq 'active') {
                   2409:                         $crsgroupschanged = 1;
                   2410:                         last;
                   2411:                     }
                   2412:                 }
                   2413:             }
                   2414:         }
                   2415:         if ((ref($changed_groups{$env{'request.course.id'}}) eq 'HASH') ||
                   2416:             (ref($groupchange{"/$cdom/$cnum"}) eq 'HASH') ||
                   2417:             ($crsgroupschanged)) {
                   2418:             my %grouproles =  &Apache::lonnet::get_my_roles('','','userroles',
                   2419:                                                             ['active'],['gr'],[$cdom],1);
                   2420:             my @activegroups;
                   2421:             foreach my $item (keys(%grouproles)) {
                   2422:                 next unless($item =~ /^\Q$cnum\E:\Q$cdom\E/);
                   2423:                 my $group;
                   2424:                 my ($crsn,$crsd,$role,$remainder) = split(/:/,$item,4);
                   2425:                 if ($remainder =~ /:/) {
                   2426:                     (my $other,$group) = ($remainder =~ /^([\w:]+):([^:]+)$/);
                   2427:                 } else {
                   2428:                     $group = $remainder;
                   2429:                 }
                   2430:                 if ($group ne '') {
                   2431:                     push(@activegroups,$group);
                   2432:                 }
                   2433:             }
                   2434:             $newenv{'request.course.groups'} = join(':',@activegroups);
                   2435:         }
                   2436:     }
                   2437:     if (keys(%newenv)) {
                   2438:         &Apache::lonnet::appenv(\%newenv);
                   2439:     }
1.260     raeburn  2440:     if (!@changed_roles || !(keys(%changed_groups))) {
1.264     raeburn  2441:         my ($rolesmsg,$groupsmsg);
1.260     raeburn  2442:         if (!@changed_roles) {
                   2443:             if (&Apache::loncommon::show_course()) {
1.264     raeburn  2444:                 $rolesmsg = &mt('No new courses or communities');
1.260     raeburn  2445:             } else {
1.264     raeburn  2446:                 $rolesmsg = &mt('No role changes');
1.260     raeburn  2447:             }
                   2448:         }
                   2449:         if ($hasgroups && !(keys(%changed_groups)) && !(grep(/gr/,@changed_roles))) {
1.264     raeburn  2450:             $groupsmsg = &mt('No changes in course/community groups');
1.260     raeburn  2451:         }
                   2452:         if (!@changed_roles && !(keys(%changed_groups))) {
1.264     raeburn  2453:             if (($msg ne '') || ($groupsmsg ne '')) {
                   2454:                 $msg .= '<ul>';
                   2455:                 if ($rolesmsg) {
                   2456:                     $msg .= '<li>'.$rolesmsg.'</li>';
                   2457:                 }
                   2458:                 if ($groupsmsg) {
                   2459:                     $msg .= '<li>'.$groupsmsg.'</li>';
                   2460:                 }
                   2461:                 $msg .= '</ul>';
                   2462:             } else {
1.270     raeburn  2463:                 $msg = '&nbsp;<span class="LC_cusr_emph">'.$rolesmsg.'</span><br />';
1.264     raeburn  2464:             }
1.260     raeburn  2465:             return $msg;
                   2466:         }
                   2467:     }
                   2468:     my $changemsg;
                   2469:     if (@changed_roles > 0) {
                   2470:         if (keys(%newgroup) > 0) {
                   2471:             my $groupmsg;
1.279     raeburn  2472:             my (%curr_groups,%groupdescs,$currcrs);
1.260     raeburn  2473:             foreach my $item (sort(keys(%newgroup))) {
                   2474:                 if (&is_active_course($item,$refresh,$update,\%roleshash)) {
1.279     raeburn  2475:                     if ($item =~ m{^gr\./($match_domain/$match_courseid)/(\w+)$}) {
                   2476:                         my ($cdom,$cnum) = split(/\//,$1);
                   2477:                         my $group = $2;
                   2478:                         if ($currcrs ne $cdom.'_'.$cnum) {
                   2479:                             if ($currcrs) {
                   2480:                                 $groupmsg .= '</ul><li>';
                   2481:                             }
                   2482:                             $groupmsg .= '<li><b>'.
                   2483:                                          $env{'course.'.$cdom.'_'.$cnum.'.description'}.'</b><ul>';
1.281     raeburn  2484:                             $currcrs = $cdom.'_'.$cnum;
1.279     raeburn  2485:                         }
                   2486:                         my $groupdesc;
                   2487:                         unless (ref($curr_groups{$cdom.'_'.$cnum}) eq 'HASH') {
                   2488:                             %{$curr_groups{$cdom.'_'.$cnum}} = 
                   2489:                                 &Apache::longroup::coursegroups($cdom,$cnum);
                   2490:                         }
                   2491:                         unless ((ref($groupdescs{$cdom.'_'.$cnum}) eq 'HASH') &&
                   2492:                             ($groupdescs{$cdom.'_'.$cnum}{$group})) {
                   2493: 
                   2494:                             my %groupinfo = 
                   2495:                                 &Apache::longroup::get_group_settings($curr_groups{$cdom.'_'.$cnum}{$group});
                   2496:                             $groupdescs{$cdom.'_'.$cnum}{$group} = 
                   2497:                                 &unescape($groupinfo{'description'});
                   2498:                         }
                   2499:                         $groupdesc = $groupdescs{$cdom.'_'.$cnum}{$group};
1.286     raeburn  2500:                         if ($groupdesc) {
                   2501:                             $groupmsg .= '<li>'.
                   2502:                                          &mt('[_1] with status: [_2].',
                   2503:                                          '<b>'.$groupdesc.'</b>',$newgroup{$item}).'</li>';
                   2504:                         }
1.279     raeburn  2505:                     }
                   2506:                 }
                   2507:                 if ($groupmsg) {
                   2508:                     $groupmsg .= '</ul></li>';
1.260     raeburn  2509:                 }
                   2510:             }
                   2511:             if ($groupmsg) {
                   2512:                 $changemsg .= '<li>'.
                   2513:                               &mt('Courses with new groups').'</li>'.
                   2514:                               '<ul>'.$groupmsg.'</ul></li>';
                   2515:             }
                   2516:         }
                   2517:         if (keys(%newrole) > 0) {
1.286     raeburn  2518:             my $newmsg;
1.260     raeburn  2519:             foreach my $item (sort(keys(%newrole))) {
1.279     raeburn  2520:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  2521:                 if ($desc) {
                   2522:                     $newmsg .= '<li>'.
                   2523:                                &mt('[_1] with status: [_2].',
1.293     bisitz   2524:                                $desc,&mt($newrole{$item})).'</li>';
1.286     raeburn  2525:                 }
                   2526:             }
                   2527:             if ($newmsg) {
                   2528:                 $changemsg .= '<li>'.&mt('New roles').
                   2529:                               '<ul>'.$newmsg.'</ul>'.
                   2530:                               '</li>';
1.260     raeburn  2531:             }
                   2532:         }
                   2533:         if (keys(%customprivchg) > 0) {
1.286     raeburn  2534:             my $privmsg;
1.260     raeburn  2535:             foreach my $item (sort(keys(%customprivchg))) {
1.279     raeburn  2536:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  2537:                 if ($desc) {
                   2538:                     $privmsg .= '<li>'.$desc.'</li>';
                   2539:                 }
1.260     raeburn  2540:             }
1.286     raeburn  2541:             if ($privmsg) {
                   2542:                 $changemsg .= '<li>'.
                   2543:                               &mt('Custom roles with privilege changes').
                   2544:                               '<ul>'.$privmsg.'</ul>'.
                   2545:                               '</li>';
                   2546:              }
1.260     raeburn  2547:         }
                   2548:         if (keys(%rolechange) > 0) {
1.286     raeburn  2549:             my $rolemsg;
1.260     raeburn  2550:             foreach my $item (sort(keys(%rolechange))) {
1.279     raeburn  2551:                 my $desc = &role_desc($item,$update,$refresh,$now);  
1.286     raeburn  2552:                 if ($desc) {
                   2553:                     $rolemsg .= '<li>'.
                   2554:                                 &mt('[_1] status now: [_2].',$desc,
                   2555:                                 $rolechange{$item}).'</li>';
                   2556:                 }
                   2557:             }
                   2558:             if ($rolemsg) {
1.260     raeburn  2559:                 $changemsg .= '<li>'.
1.286     raeburn  2560:                               &mt('Existing roles with status changes').'</li>'.
                   2561:                               '<ul>'.$rolemsg.'</ul>'.
                   2562:                               '</li>';
1.260     raeburn  2563:             }
                   2564:         }
                   2565:         if (keys(%deletedroles) > 0) {
1.286     raeburn  2566:             my $delmsg;
1.260     raeburn  2567:             foreach my $item (sort(keys(%deletedroles))) {
1.279     raeburn  2568:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  2569:                 if ($desc) {
                   2570:                     $delmsg .= '<li>'.$desc.'</li>';
                   2571:                 }
                   2572:             }
                   2573:             if ($delmsg) {
                   2574:                 $changemsg .= '<li>'.
                   2575:                               &mt('Existing roles now expired').'</li>'.
                   2576:                               '<ul>'.$delmsg.'</ul>'.
                   2577:                               '</li>';
1.260     raeburn  2578:             }
                   2579:         }
                   2580:     }
                   2581:     if ((keys(%changed_groups) > 0) || (keys(%groupchange) > 0)) {
                   2582:         my $groupchgmsg;
                   2583:         foreach my $key (sort(keys(%changed_groups))) {
                   2584:             my $crs = 'gr/'.$key;
                   2585:             $crs =~ s/_/\//;
                   2586:             if (&is_active_course($crs,$refresh,$update,\%roleshash)) {
                   2587:                 if (ref($changed_groups{$key}) eq 'HASH') {
                   2588:                     my @showgroups;
                   2589:                     foreach my $group (sort(keys(%{$changed_groups{$key}}))) {
                   2590:                         if ($changed_groups{$key}{$group} eq 'active') {
                   2591:                             push(@showgroups,$group);
                   2592:                         }
                   2593:                     }
                   2594:                     if (@showgroups > 0) {
                   2595:                         $groupchgmsg .= '<li>'.
                   2596:                                         &mt('Course: [_1], groups: [_2].',$key,
                   2597:                                         join(', ',@showgroups)).
                   2598:                                         '</li>';
                   2599:                     }
                   2600:                 }
                   2601:             }
                   2602:         }
                   2603:         if (keys(%groupchange) > 0) {
                   2604:             $groupchgmsg .= '<li>'.
                   2605:                           &mt('Existing course/community groups with status changes').'</li>'.
                   2606:                           '<ul>';
                   2607:             foreach my $crs (sort(keys(%groupchange))) {
1.279     raeburn  2608:                 my $cid = $crs;
                   2609:                 $cid=~s{^/}{};
                   2610:                 $cid=~s{/}{_};
                   2611:                 my $crsdesc = $env{'course.'.$cid.'.description'};
                   2612:                 my $cdom = $env{'course.'.$cid.'.domain'};
                   2613:                 my $cnum = $env{'course.'.$cid.'.num'};
                   2614:                 my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   2615:                 my %groupdesc; 
1.260     raeburn  2616:                 if (ref($groupchange{$crs}) eq 'HASH') {
1.279     raeburn  2617:                     $groupchgmsg .= '<li>'.&mt('Course/Community: [_1]','<b>'.$crsdesc.'</b><ul>');
1.260     raeburn  2618:                     foreach my $group (sort(keys(%{$groupchange{$crs}}))) {
1.279     raeburn  2619:                         unless ($groupdesc{$group}) {
                   2620:                             my %groupinfo = &Apache::longroup::get_group_settings($curr_groups{$group});
                   2621:                             $groupdesc{$group} =  &unescape($groupinfo{'description'});
                   2622:                         }
                   2623:                         $groupchgmsg .= '<li>'.&mt('Group: [_1] status now: [_2].','<b>'.$groupdesc{$group}.'</b>',$groupchange{$crs}{$group}).'</li>';
1.260     raeburn  2624:                     }
                   2625:                     $groupchgmsg .= '</ul></li>';
                   2626:                 }
                   2627:             }
                   2628:             $groupchgmsg .= '</ul></li>';
                   2629:         }
                   2630:         if ($groupchgmsg) {
                   2631:             $changemsg .= '<li>'.
                   2632:                           &mt('Courses with changes in groups').'</li>'.
                   2633:                           '<ul>'.$groupchgmsg.'</ul></li>';
                   2634:         }
                   2635:     }
                   2636:     if ($changemsg) {
                   2637:         $msg .= '<ul>'.$changemsg.'</ul>';
1.286     raeburn  2638:     } else {
                   2639:         if (&Apache::loncommon::show_course()) {
                   2640:             $msg = &mt('No new courses or communities');
                   2641:         } else {
                   2642:             $msg = &mt('No role changes');
                   2643:         }
1.260     raeburn  2644:     }
1.279     raeburn  2645:     return $msg;
                   2646: }
                   2647: 
                   2648: sub role_desc {
                   2649:     my ($item,$update,$refresh,$now) = @_;
                   2650:     my ($where,$trolecode,$role,$tstatus,$tend,$tstart,$twhere,
                   2651:         $trole,$tremark);
1.282     raeburn  2652:     &Apache::lonnet::role_status('user.role.'.$item,$update,$refresh,
                   2653:                                  $now,\$role,\$where,\$trolecode,
1.279     raeburn  2654:                                  \$tstatus,\$tstart,\$tend);
1.286     raeburn  2655:     return unless ($role);
1.279     raeburn  2656:     if ($role =~ /^cr\//) {
                   2657:         my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.298     bisitz   2658:         $tremark = &mt('Custom role defined by [_1].',$rauthor.':'.$rdomain);
1.279     raeburn  2659:     }
                   2660:     $trole=Apache::lonnet::plaintext($role);
                   2661:     my ($tdom,$trest,$tsection)=
                   2662:         split(/\//,Apache::lonnet::declutter($where));
                   2663:     if (($role eq 'ca') || ($role eq 'aa')) {
                   2664:         my $home = &Apache::lonnet::homeserver($trest,$tdom);
                   2665:         $home = &Apache::lonnet::hostname($home);
                   2666:         $twhere=&mt('User').':&nbsp;'.$trest.'&nbsp; '.&mt('Domain').
                   2667:                 ':&nbsp;'.$tdom.'&nbsp; '.&mt('Server').':&nbsp;'.$home;
                   2668:     } elsif ($role eq 'au') {
                   2669:         my $home = &Apache::lonnet::homeserver
                   2670:                        ($env{'user.name'},$env{'user.domain'});
                   2671:         $home = &Apache::lonnet::hostname($home);
                   2672:         $twhere=&mt('Domain').':&nbsp;'.$tdom.'&nbsp; '.&mt('Server').
                   2673:                         ':&nbsp;'.$home;
                   2674:     } elsif ($trest) {
                   2675:         my $tcourseid=$tdom.'_'.$trest;
                   2676:         my $crstype = &Apache::loncommon::course_type($tcourseid);
                   2677:         $trole = &Apache::lonnet::plaintext($role,$crstype,$tcourseid);
                   2678:         if ($env{'course.'.$tcourseid.'.description'}) {
                   2679:             $twhere=$env{'course.'.$tcourseid.'.description'};
                   2680:         } else {
                   2681:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   2682:             if (%newhash) {
                   2683:                 $twhere=$newhash{'description'};
                   2684:             } else {
                   2685:                 $twhere=&mt('Currently not available');
1.260     raeburn  2686:             }
                   2687:         }
1.279     raeburn  2688:         if ($tsection) {
                   2689:             $twhere.= '&nbsp; '.&mt('Section').':&nbsp;'.$tsection;
1.260     raeburn  2690:         }
1.279     raeburn  2691:         if ($role ne 'st') {
                   2692:             $twhere.= '&nbsp; '.&mt('Domain').':&nbsp;'.$tdom;
1.260     raeburn  2693:         }
1.279     raeburn  2694:     } elsif ($tdom) {
                   2695:         $twhere = &mt('Domain').':&nbsp;'.$tdom;
1.260     raeburn  2696:     }
1.286     raeburn  2697:     my $output;
                   2698:     if ($trole) {
                   2699:         $output = $trole;
                   2700:         if ($twhere) {
                   2701:             $output .= " -- $twhere";
                   2702:         }
                   2703:         if ($tremark) {
                   2704:             $output .= '<br />'.$tremark;
                   2705:         }
1.260     raeburn  2706:     }
1.279     raeburn  2707:     return $output;
1.260     raeburn  2708: }
                   2709: 
                   2710: sub curr_role_status {
                   2711:     my ($start,$end,$refresh,$update) = @_;
                   2712:     if (($start) && ($start<0)) { return 'deleted' };
                   2713:     my $status = 'active';
                   2714:     if (($end) && ($end<=$update)) {
                   2715:         $status = 'previous';
                   2716:     }
                   2717:     if (($start) && ($refresh<$start)) {
                   2718:         $status = 'future';
                   2719:     }
                   2720:     return $status;
                   2721: }
                   2722: 
                   2723: sub gather_roleprivs {
                   2724:     my ($allroles,$allgroups,$userroles,$area,$role,$tstart,$tend,$status) = @_;
                   2725:     return unless ((ref($allroles) eq 'HASH') && (ref($allgroups) eq 'HASH') && (ref($userroles) eq 'HASH'));
                   2726:     if (($area ne '') && ($role ne '')) {
                   2727:         &Apache::lonnet::userrolelog($role,$env{'user.name'},$env{'user.domain'},
                   2728:                                      $area,$tstart,$tend);
                   2729:         my $spec=$role.'.'.$area;
                   2730:         $userroles->{'user.role.'.$spec} = $tstart.'.'.$tend;
                   2731:         my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   2732:         if ($status eq 'active') { 
                   2733:             if ($role =~ /^cr\//) {
                   2734:                 &Apache::lonnet::custom_roleprivs($allroles,$role,$tdomain,$trest,$spec,$area);
                   2735:             } elsif ($role eq 'gr') {
                   2736:                 my %rolehash = &Apache::lonnet::get('roles',[$area.'_'.$role],
                   2737:                                                     $env{'user.domain'},
                   2738:                                                     $env{'user.name'});
                   2739:                 my ($trole) = split(/_/,$rolehash{$area.'_'.$role},2);
                   2740:                 (undef,my $group_privs) = split(/\//,$trole);
                   2741:                 $group_privs = &unescape($group_privs);
                   2742:                 &Apache::lonnet::group_roleprivs($allgroups,$area,$group_privs,$tend,$tstart);
                   2743:             } else {
                   2744:                 &Apache::lonnet::standard_roleprivs($allroles,$role,$tdomain,$spec,$trest,$area);
                   2745:             }
                   2746:         }
                   2747:     }
                   2748:     return;
                   2749: }
                   2750: 
                   2751: sub is_active_course {
                   2752:     my ($rolekey,$refresh,$update,$roleshashref) = @_;
                   2753:     return unless(ref($roleshashref) eq 'HASH');
                   2754:     my ($role,$cdom,$cnum) = split(/\//,$rolekey);
                   2755:     my $is_active;
                   2756:     foreach my $key (keys(%{$roleshashref})) {
                   2757:         if ($key =~ /^\Q$cnum\E:\Q$cdom\E:/) {
                   2758:             my ($tstart,$tend) = split(/:/,$roleshashref->{$key});
                   2759:             my $status = &curr_role_status($tstart,$tend,$refresh,$update);
                   2760:             if ($status eq 'active') {
                   2761:                 $is_active = 1;
                   2762:                 last;
                   2763:             }
                   2764:         }
                   2765:     }
                   2766:     return $is_active;
                   2767: }
                   2768: 
1.274     raeburn  2769: sub get_roles_functions {
1.302     raeburn  2770:     my ($rolescount,$cattype) = @_;
1.274     raeburn  2771:     my @links;
                   2772:     push(@links,["javascript:rolesView('doupdate');",'start-here-22x22',&mt('Check for changes')]);
                   2773:     if ($env{'environment.canrequest.author'}) {
                   2774:         unless (&Apache::loncoursequeueadmin::is_active_author()) {
                   2775:             push(@links,["javascript:rolesView('requestauthor');",'list-add-22x22',&mt('Request author role')]);
                   2776:         }
                   2777:     }
1.279     raeburn  2778:     if (($rolescount > 3) || ($env{'environment.recentroles'})) {
                   2779:         push(@links,['/adm/preferences?action=changerolespref&amp;returnurl=/adm/roles','role_hotlist-22x22',&mt('Hotlist')]);
                   2780:     }
1.274     raeburn  2781:     if (&Apache::lonmenu::check_for_rcrs()) {
                   2782:         push(@links,['/adm/requestcourse','rcrs-22x22',&mt('Request course')]);
                   2783:     }
                   2784:     if ($env{'form.state'} eq 'queued') {
                   2785:         push(@links,["javascript:rolesView('noqueued');",'selfenrl-queue-22x22',&mt('Hide queued')]);
                   2786:     } else {
                   2787:         push(@links,["javascript:rolesView('queued');",'selfenrl-queue-22x22',&mt('Show queued')]);
                   2788:     }
1.279     raeburn  2789:     if ($env{'user.adv'}) {
                   2790:         if ($env{'form.display'} eq 'showall') {
1.290     raeburn  2791:             push(@links,["javascript:rolesView('noshowall');",'edit-redo-22x22',&mt('Exclude expired')]);
1.279     raeburn  2792:         } else {
1.290     raeburn  2793:             push(@links,["javascript:rolesView('showall');",'edit-undo-22x22',&mt('Include expired')]);
1.279     raeburn  2794:         }
1.274     raeburn  2795:     }
1.302     raeburn  2796:     unless ($cattype eq 'none') {
1.291     raeburn  2797:         push(@links,['/adm/coursecatalog','ccat-22x22',&mt('Course catalog')]);
1.290     raeburn  2798:     }
1.314     raeburn  2799:     my $funcs;
                   2800:     if ($env{'browser.mobile'}) {
                   2801:         my @functions;
                   2802:         foreach my $link (@links) {
                   2803:             push(@functions,[$link->[0],$link->[2]]);
                   2804:         }
                   2805:         my $title = 'Display options';
                   2806:         if ($env{'user.adv'}) {
                   2807:             $title = 'Roles options';
                   2808:         }
                   2809:         $funcs = &Apache::lonmenu::create_submenu('','',$title,\@functions,1,'LC_breadcrumbs_hoverable');
                   2810:         $funcs = '<ol class="LC_primary_menu LC_floatright">'.$funcs.'</ol>';
                   2811:     } else {
                   2812:         $funcs = &Apache::lonhtmlcommon::start_funclist();
                   2813:         foreach my $link (@links) {
                   2814:             $funcs .= &Apache::lonhtmlcommon::add_item_funclist(
                   2815:                           '<a href="'.$link->[0].'" class="LC_menubuttons_link">'.
                   2816:                           '<img src="/res/adm/pages/'.$link->[1].'.png" class="LC_icon" alt="'.$link->[2].'" />'.
                   2817:                           $link->[2].'</a>');
                   2818:         }
                   2819:         $funcs .= &Apache::lonhtmlcommon::end_funclist();
                   2820:         $funcs = &Apache::loncommon::head_subbox($funcs);
1.274     raeburn  2821:     }
1.314     raeburn  2822:     return $funcs;
1.274     raeburn  2823: }
                   2824: 
                   2825: sub get_queued {
                   2826:     my ($output,%reqcrs);
                   2827:     my ($types,$typenames) = &Apache::loncommon::course_types();
                   2828:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
                   2829:                                            $env{'user.name'},'^status:');
                   2830:     foreach my $key (keys(%statusinfo)) {
                   2831:         next unless (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending'));
1.297     raeburn  2832:         (undef,my($cdom,$cnum)) = split(/:/,$key);
1.274     raeburn  2833:         my $requestkey = $cdom.'_'.$cnum;
                   2834:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   2835:             my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   2836:                                                    $env{'user.domain'},$env{'user.name'});
                   2837:             next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
                   2838:             my $reqtime = $history{'reqtime'};
                   2839:             my $lastupdate = $history{'timestamp'};
                   2840:             my $showtype = $history{'crstype'};
                   2841:             if (defined($typenames->{$history{'crstype'}})) {
                   2842:                 $showtype = $typenames->{$history{'crstype'}};
                   2843:             }
                   2844:             my $description;
                   2845:             if (ref($history{'details'}) eq 'HASH') {
                   2846:                 $description = $history{details}{'cdescr'};
                   2847:             }
                   2848:             @{$reqcrs{$reqtime}} = ($description,$showtype); 
                   2849:         }
                   2850:     }
                   2851:     my @sortedtimes = sort {$a <=> $b} (keys(%reqcrs));
                   2852:     if (@sortedtimes > 0) {
                   2853:         $output .= '<p><b>'.&mt('Course/Community requests').'</b><br />'.
                   2854:                    &Apache::loncommon::start_data_table().
                   2855:                    &Apache::loncommon::start_data_table_header_row().
                   2856:                    '<th>'.&mt('Date requested').'</th>'.
                   2857:                    '<th>'.&mt('Course title').'</th>'.
                   2858:                    '<th>'.&mt('Course type').'</th>';
                   2859:                    &Apache::loncommon::end_data_table_header_row();
                   2860:         foreach my $reqtime (@sortedtimes) {
                   2861:             next unless (ref($reqcrs{$reqtime}) eq 'ARRAY');
                   2862:             $output .= &Apache::loncommon::start_data_table_row().
                   2863:                        '<td>'.&Apache::lonlocal::locallocaltime($reqtime).'</td>'.
                   2864:                        '<td>'.join('</td><td>',@{$reqcrs{$reqtime}}).'</td>'.
                   2865:                        &Apache::loncommon::end_data_table_row();
                   2866:         }
                   2867:         $output .= &Apache::loncommon::end_data_table().
                   2868:                    '<br /></p>';
                   2869:     }
                   2870:     my $queuedselfenroll = &Apache::loncoursequeueadmin::queued_selfenrollment(1);
                   2871:     if ($queuedselfenroll) {
                   2872:         $output .= '<p><b>'.&mt('Enrollment requests').'</b><br />'.
                   2873:                    $queuedselfenroll.'<br /></p>';
                   2874:     }
                   2875:     if ($env{'environment.canrequest.author'}) {
                   2876:         unless (&Apache::loncoursequeueadmin::is_active_author()) {
                   2877:             my $requestauthor;
                   2878:             my ($status,$timestamp) = split(/:/,$env{'environment.requestauthorqueued'});
                   2879:             if (($status eq 'approval') || ($status eq 'approved')) {
                   2880:                 $output .= '<p><b>'.&mt('Author role request').'</b><br />';
                   2881:                 if ($status eq 'approval') {
1.294     bisitz   2882:                     $output .= &mt('A request for Authoring Space submitted on [_1] is awaiting approval',
1.274     raeburn  2883:                                   &Apache::lonlocal::locallocaltime($timestamp));
                   2884:                 } elsif ($status eq 'approved') {
                   2885:                     my %roleshash =
                   2886:                         &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
                   2887:                                                       ['active'],['au'],[$env{'user.domain'}]);
                   2888:                     if (keys(%roleshash)) {
                   2889:                         $output .= '<span class="LC_info">'.
                   2890:                                    &mt('Your request for an author role has been approved.').'<br />'.
                   2891:                                    &mt('Use the "Check for changes" link to update your list of roles.').
                   2892:                                    '</span>';
                   2893:                     }
                   2894:                 }
                   2895:                 $output .= '</p>';
                   2896:             }
                   2897:         }
                   2898:     }
                   2899:     unless ($output) {
                   2900:         if ($env{'environment.canrequest.author'} || $env{'environment.canrequest.official'} ||
                   2901:             $env{'environment.canrequest.unofficial'} || $env{'environment.canrequest.community'}) {
                   2902:             $output = &mt('No requests for courses, communities or authoring currently queued');
                   2903:         } else {
                   2904:             $output = &mt('No enrollment requests currently queued awaiting approval');
                   2905:         }
                   2906:     }
                   2907:     return '<div class="LC_left_float"><fieldset><legend>'.&mt('Queued requests').'</legend>'.
                   2908:            $output.'</fieldset></div><br clear="all" />';
                   2909: }
                   2910: 
1.1       harris41 2911: 1;
                   2912: __END__
1.32      harris41 2913: 
                   2914: =head1 NAME
                   2915: 
                   2916: Apache::lonroles - User Roles Screen
                   2917: 
                   2918: =head1 SYNOPSIS
                   2919: 
                   2920: Invoked by /etc/httpd/conf/srm.conf:
                   2921: 
                   2922:  <Location /adm/roles>
                   2923:  PerlAccessHandler       Apache::lonacc
                   2924:  SetHandler perl-script
                   2925:  PerlHandler Apache::lonroles
                   2926:  ErrorDocument     403 /adm/login
                   2927:  ErrorDocument	  500 /adm/errorhandler
                   2928:  </Location>
1.64      bowersj2 2929: 
                   2930: =head1 OVERVIEW
                   2931: 
                   2932: =head2 Choosing Roles
                   2933: 
                   2934: C<lonroles> is a handler that allows a user to switch roles in
                   2935: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                   2936: default role that a user has before selecting a role, as widely as
                   2937: possible, but certain handlers for example need specification which
                   2938: course they should act on, etc. Both in this scenario, and when the
                   2939: handler determines via C<lonnet>'s C<&allowed> function that a certain
                   2940: action is not allowed, C<lonroles> is used as error handler. This
                   2941: allows the user to select another role which may have permission to do
1.246     droeschl 2942: what they were trying to do.
1.64      bowersj2 2943: 
                   2944: =begin latex
                   2945: 
                   2946: \begin{figure}
                   2947: \begin{center}
                   2948: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                   2949:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                   2950: \end{center}
                   2951: \end{figure}
                   2952: 
                   2953: =end latex
                   2954: 
                   2955: =head2 Role Initialization
                   2956: 
                   2957: The privileges for a user are established at login time and stored in the session environment. As a consequence, a new role does not become active till the next login. Handlers are able to query for privileges using C<lonnet>'s C<&allowed> function. When a user first logs in, their role is the "common" role, which means that they have the sum of all of their privileges. During a session it might become necessary to choose a particular role, which as a consequence also limits the user to only the privileges in that particular role.
1.32      harris41 2958: 
                   2959: =head1 INTRODUCTION
                   2960: 
                   2961: This module enables a user to select what role he wishes to
                   2962: operate under (instructor, student, teaching assistant, course
                   2963: coordinator, etc).  These roles are pre-established by the actions
                   2964: of upper-level users.
                   2965: 
                   2966: This is part of the LearningOnline Network with CAPA project
                   2967: described at http://www.lon-capa.org.
                   2968: 
                   2969: =head1 HANDLER SUBROUTINE
                   2970: 
                   2971: This routine is called by Apache and mod_perl.
                   2972: 
                   2973: =over 4
                   2974: 
                   2975: =item *
                   2976: 
                   2977: Roles Initialization (yes/no)
                   2978: 
                   2979: =item *
                   2980: 
                   2981: Get Error Message from Environment
                   2982: 
                   2983: =item *
                   2984: 
                   2985: Who is this?
                   2986: 
                   2987: =item *
                   2988: 
                   2989: Generate Page Output
                   2990: 
                   2991: =item *
                   2992: 
                   2993: Choice or no choice
                   2994: 
                   2995: =item *
                   2996: 
                   2997: Table
                   2998: 
                   2999: =item *
                   3000: 
                   3001: Privileges
                   3002: 
                   3003: =back
                   3004: 
                   3005: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>