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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.377   ! raeburn     4: # $Id: lonroles.pm,v 1.376 2025/02/23 05:16:01 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.338     raeburn   148: my $registered_cleanup;
                    149: my $rosterupdates;
1.1       harris41  150: 
1.347     raeburn   151: sub start_loading_course {
1.365     raeburn   152:     my ($r,$title,$only_body) = @_;
1.347     raeburn   153:     &Apache::loncommon::content_type($r,'text/html');
                    154:     &Apache::loncommon::no_cache($r);
                    155:     $r->send_http_header;
1.365     raeburn   156:     if ($only_body) {
                    157:         $r->print(&Apache::loncommon::start_page($title,undef,{'only_body' => 1,
                    158:                                                                'add_progressbar' => 1}));
                    159:     } else {
                    160:         my $swinfo=&Apache::lonmenu::rawconfig();
                    161:         # Breadcrumbs
                    162:         my $brcrum = [{'href' => '',
                    163:                        'text' => $title},];
                    164:         my $start_page = &Apache::loncommon::start_page($title,undef,
                    165:                                                         {'bread_crumbs' => $brcrum,
                    166:                                                          'bread_crumbs_nomenu' => 1,
                    167:                                                          'links_disabled' => 1});
                    168:         $r->print(<<ENDREDIR);
1.347     raeburn   169: $start_page
                    170: <script type="text/javascript">
                    171: // <![CDATA[
                    172: $swinfo
1.354     raeburn   173: 
                    174: document.body.addEventListener('click', function (event) {
                    175:   // filter out clicks on any other elements
                    176:   if (event.target.nodeName == 'A' && event.target.getAttribute('aria-disabled') == 'true') {
                    177:     event.preventDefault();
                    178:   }
                    179: });
1.347     raeburn   180: // ]]>
                    181: </script>
                    182: ENDREDIR
1.365     raeburn   183:     }
1.376     raeburn   184:     $r->print('<div class="LC_landmark" role="main">'."\n");
1.347     raeburn   185:     return;
                    186: }
                    187: 
                    188: sub finish_loading_course {
1.365     raeburn   189:     my ($r,$msg,$url,$only_body) = @_;
1.362     raeburn   190:     my $link = '<div id="LC_course_loaded" style="display:none"><a href="'.
                    191:                &HTML::Entities::encode($url,'"<>&').'">'.&mt('Continue').'</a></div>';
1.347     raeburn   192:     my $end_page = &Apache::loncommon::end_page();
                    193:     my $js_url = &js_escape($url);
1.365     raeburn   194:     my $reenable;
                    195:     unless ($only_body) {
                    196:         $reenable = <<REENABLE;
                    197:     \$('.isDisabled > a').removeAttr("aria-disabled");
                    198:     \$('.isDisabled').removeClass("isDisabled");
                    199: REENABLE
                    200:     }
1.347     raeburn   201:     $r->print(<<END);
                    202: $msg
                    203: <script type="text/javascript">
                    204: // <![CDATA[
                    205: \$(document).ready(function() {
1.354     raeburn   206:     \$("#LC_course_loaded").css("display","block");
1.365     raeburn   207:     $reenable
1.347     raeburn   208:     var url = "$js_url";
                    209:     \$(location).attr('href',url);
                    210: });
1.360     raeburn   211: // ]]>
1.347     raeburn   212: </script>
                    213: $link
1.376     raeburn   214: </div>
1.347     raeburn   215: $end_page
                    216: END
                    217:     return;
                    218: }
                    219: 
1.62      matthew   220: sub redirect_user {
1.245     droeschl  221:     my ($r,$title,$url,$msg) = @_;
1.62      matthew   222:     $msg = $title if (! defined($msg));
1.73      www       223:     &Apache::loncommon::content_type($r,'text/html');
1.62      matthew   224:     &Apache::loncommon::no_cache($r);
                    225:     $r->send_http_header;
1.228     bisitz    226: 
1.337     raeburn   227:     my $start_page;
                    228:     if ($env{'request.lti.login'}) {
                    229:         $start_page = &Apache::loncommon::start_page(undef,undef,
                    230:                                                      {'redirect' => [0,$url],}).$msg;
                    231:     } else {
                    232:         # Breadcrumbs
                    233:         my $brcrum = [{'href' => $url,
                    234:                        'text' => 'Switching Role'},];
                    235:         $start_page = &Apache::loncommon::start_page('Switching Role',undef,
                    236:                                                      {'redirect' => [1,$url],
                    237:                                                       'bread_crumbs' => $brcrum,}).
                    238:                       "\n<p>$msg</p>";
                    239:     }
                    240:     my $end_page = &Apache::loncommon::end_page();
1.147     albertel  241: 
1.92      www       242: # Note to style police: 
                    243: # This must only replace the spaces, nothing else, or it bombs elsewhere.
                    244:     $url=~s/ /\%20/g;
1.93      albertel  245:     $r->print(<<ENDREDIR);
1.147     albertel  246: $start_page
                    247: $end_page
1.62      matthew   248: ENDREDIR
                    249:     return;
                    250: }
                    251: 
1.150     www       252: sub error_page {
                    253:     my ($r,$error,$dest)=@_;
1.347     raeburn   254:     my %lt = &Apache::lonlocal::texthash(
                    255:         pdc => 'Problems during Course Initialization',
                    256:         tfp => 'The following problems occurred:',
                    257:         con => 'Continue',
1.228     bisitz    258:     );
1.347     raeburn   259:     my $end_page = &Apache::loncommon::end_page();
                    260:     $dest = &HTML::Entities::encode($dest,'"<>&');
                    261:     $r->print(<<END);
                    262: <h3>$lt{'pdc'}</h3>
                    263: <p class="LC_error">$lt{'tfp'}
                    264: <br />
                    265: $error
                    266: </p><br /><a href="$dest">$lt{'con'}</a>
                    267: $end_page
                    268: END
                    269:     return;
1.150     www       270: }
                    271: 
1.1       harris41  272: sub handler {
1.10      www       273: 
1.1       harris41  274:     my $r = shift;
                    275: 
1.308     raeburn   276:     # Check for critical messages and redirect if present.
1.332     raeburn   277:     my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'roles');
1.304     musolffc  278:     if ($redirect) {
                    279:         &Apache::loncommon::content_type($r,'text/html');
                    280:         $r->header_out(Location => $url);
                    281:         return REDIRECT;
                    282:     }
                    283: 
1.6       www       284:     my $now=time;
1.118     albertel  285:     my $then=$env{'user.login.time'};
1.226     raeburn   286:     my $refresh=$env{'user.refresh.time'};
1.260     raeburn   287:     my $update=$env{'user.update.time'};
1.226     raeburn   288:     if (!$refresh) {
                    289:         $refresh = $then;
                    290:     }
1.260     raeburn   291:     if (!$update) {
                    292:         $update = $then;
                    293:     }
                    294: 
1.359     raeburn   295:     my ($norolelist,$blocked_by_ip,$blocked_type,$clientip);
                    296:     $clientip = &Apache::lonnet::get_requestor_ip($r);
1.352     raeburn   297:     if (($env{'request.course.id'}) && ($env{'request.deeplink.login'})) {
                    298:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    299:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    300:         my $crstype = $env{'course.'.$env{'request.course.id'}.'.type'};
                    301:         my $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
                    302:         if ($deeplink_symb) {
                    303:             my ($menucoll,$deeplinkmenu,$menuref) = &Apache::loncommon::menucoll_in_effect();
                    304:             if (ref($menuref) eq 'HASH') {
                    305:                 unless (($menuref->{'role'}) || ($env{'request.role.adv'})) {
                    306:                     foreach my $envkey (keys(%env)) {
                    307:                         next unless ($envkey =~ /^form\./);
                    308:                         if ($envkey =~ m{\./($match_domain)/($match_courseid)(?:/(\w+)|$)}) {
                    309:                             unless (($1 eq $cdom) && ($2 eq $cnum)) {
                    310:                                 delete($env{$envkey});
                    311:                             }
                    312:                         }
                    313:                     }
                    314:                     if ($env{'form.selectrole'}) {
                    315:                         if ($env{'form.switchrole'} =~ m{\./($match_domain)/($match_courseid)(?:/(\w+)|$)}) {
                    316:                             unless (($1 eq $cdom) && ($2 eq $cnum)) {
                    317:                                 delete($env{'form.selectrole'});
                    318:                                 delete($env{'form.switchrole'});
                    319:                             }
                    320:                         } elsif ($env{'form.newrole'} =~ m{\./($match_domain)/($match_courseid)(?:/(\w+)|$)}) {
                    321:                             unless (($1 eq $cdom) && ($2 eq $cnum)) {
                    322:                                 delete($env{'form.selectrole'});
                    323:                                 delete($env{'form.newrole'});
                    324:                             }
                    325:                         }
                    326:                     }
                    327:                     $norolelist = 1;
                    328:                 }
                    329:             }
                    330:         }
                    331:     }
                    332: 
1.357     raeburn   333:     if ($env{'form.selectrole'}) {
                    334:         my ($role,$cdom,$cnum,$rest);
                    335:         if ($env{'form.switchrole'} =~ m{^(co|cc|in|ta|ep|ad|st|cr).*?\./($match_domain)/($match_courseid)(/(\w+)|$)}) {
                    336:             ($role,$cdom,$cnum,$rest) = ($1,$2,$3,$4);
                    337:         } elsif ($env{'form.newrole'} =~ m{^(co|cc|in|ta|ep|ad|st|cr).*?\./($match_domain)/($match_courseid)(/(\w+)|$)}) {
                    338:             ($role,$cdom,$cnum,$rest) = ($1,$2,$3,$4);
                    339:         }
                    340:         if ($cdom ne '') {
                    341:             my ($has_evb,$check_ipaccess,$showrole);
                    342:             $showrole = 1;
                    343:             my $checkrole = "cm./$cdom/$cnum";
                    344:             if ($rest ne '') {
                    345:                 $checkrole .= "/$rest";
                    346:             }
                    347:             if ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
                    348:                 ($role ne 'st')) {
                    349:                 $has_evb = 1;
                    350:             }
                    351:             unless ($has_evb) {
                    352:                 my @machinedoms = &Apache::lonnet::current_machine_domains();
                    353:                 my $udom = $env{'user.domain'};
                    354:                 if ($udom eq $cdom) {
                    355:                     $check_ipaccess = 1;
                    356:                 } elsif (($udom ne '') && (grep(/^\Q$udom\E$/,@machinedoms))) {
                    357:                     $check_ipaccess = 1;
                    358:                 } else {
                    359:                     my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
                    360:                     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
                    361:                     my $cprim = &Apache::lonnet::domain($cdom,'primary');
                    362:                     my $cintdom = &Apache::lonnet::internet_dom($cprim);
                    363:                     if (($cintdom ne '') && (ref($internet_names) eq 'ARRAY')) {
                    364:                         if (grep(/^\Q$cintdom\E$/,@{$internet_names})) {
                    365:                             $check_ipaccess = 1;
                    366:                         }
                    367:                     }
                    368:                 }
                    369:                 if ($check_ipaccess) {
                    370:                     my ($ipaccessref,$cached)=&Apache::lonnet::is_cached_new('ipaccess',$cdom);
                    371:                     unless (defined($cached)) {
                    372:                         my %domconfig =
                    373:                             &Apache::lonnet::get_dom('configuration',['ipaccess'],$cdom);
1.358     raeburn   374:                         $ipaccessref = &Apache::lonnet::do_cache_new('ipaccess',$cdom,$domconfig{'ipaccess'},1800);
1.357     raeburn   375:                     }
                    376:                     if (ref($ipaccessref) eq 'HASH') {
                    377:                         foreach my $id (keys(%{$ipaccessref})) {
                    378:                             if (ref($ipaccessref->{$id}) eq 'HASH') {
                    379:                                 my $range = $ipaccessref->{$id}->{'ip'};
                    380:                                 if ($range) {
                    381:                                     my $type = 'exclude';
1.359     raeburn   382:                                     if (&Apache::lonnet::ip_match($clientip,$range)) {
1.357     raeburn   383:                                         $type = 'include';
                    384:                                     }
                    385:                                     if (ref($ipaccessref->{$id}->{'courses'}) eq 'HASH') {
                    386:                                         if ($ipaccessref->{$id}->{'courses'}{$cdom.'_'.$cnum}) {
                    387:                                             if ($type eq 'include') {
                    388:                                                 $showrole = 1;
                    389:                                                 last;
                    390:                                             } else {
                    391:                                                 $showrole = 0;
                    392:                                             }
                    393:                                         } else {
                    394:                                             if ($type eq 'include') {
                    395:                                                 $showrole = 0;
                    396:                                             } else {
                    397:                                                 $showrole = 1;
                    398:                                             }
                    399:                                         }
                    400:                                     }
                    401:                                 }
                    402:                             }
                    403:                         }
                    404:                     }
                    405:                 }
                    406:             }
                    407:             unless ($showrole) {
                    408:                 $blocked_by_ip = 1;
                    409:                 $blocked_type = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                    410:                 delete($env{'form.selectrole'});
                    411:                 delete($env{'form.newrole'});
                    412:             }
                    413:         }
                    414:     }
                    415: 
1.338     raeburn   416:     $registered_cleanup=0;
                    417:     @{$rosterupdates}=();
1.274     raeburn   418:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    419: 
1.347     raeburn   420: # -------------------------------------------------- Check if setting hot list
1.274     raeburn   421:     my $hotlist;
                    422:     if ($env{'form.action'} eq 'verify_and_change_rolespref') {
                    423:         $hotlist = &Apache::lonpreferences::verify_and_change_rolespref($r);
                    424:     }
                    425: 
1.260     raeburn   426: # -------------------------------------------------------- Check for new roles
                    427:     my $updateresult;
1.274     raeburn   428:     if ($env{'form.state'} eq 'doupdate') {
1.260     raeburn   429:         my $show_course=&Apache::loncommon::show_course();
                    430:         my $checkingtxt;
                    431:         if ($show_course) {
                    432:             $checkingtxt = &mt('Checking for new courses ...');
                    433:         } else {
                    434:             $checkingtxt = &mt('Checking for new roles ...');
                    435:         }
1.274     raeburn   436:         $updateresult = $checkingtxt;
1.260     raeburn   437:         $updateresult .= &update_session_roles();
                    438:         &Apache::lonnet::appenv({'user.update.time'  => $now});
                    439:         $update = $now;
1.272     raeburn   440:         &Apache::loncoursequeueadmin::reqauthor_check();
1.270     raeburn   441:     }
                    442: 
                    443: # -------------------------------------------------- Check for author requests
                    444:     my $reqauthor;
1.274     raeburn   445:     if ($env{'form.state'} eq 'requestauthor') {
1.272     raeburn   446:        $reqauthor = &Apache::loncoursequeueadmin::process_reqauthor(\$update);
1.260     raeburn   447:     }
                    448: 
1.370     raeburn   449: # ------------------------------------------------- Check for approval results
                    450:     my $approvalresult;
                    451:     if (($env{'form.approvals'} eq 'show') && ($env{'form.state'} eq 'done')) {
                    452:         $approvalresult = &Apache::loncoursequeueadmin::update_request_queue('othdombyuser',  
                    453:                                                                              $env{'user.domain'});
                    454:     }
1.6       www       455:     my $envkey;
1.107     raeburn   456:     my %dcroles = ();
1.325     raeburn   457:     my %helpdeskroles = ();
                    458:     my ($numdc,$numhelpdesk,$numadhoc) = 
                    459:         &check_for_adhoc(\%dcroles,\%helpdeskroles,$update,$then);
1.304     musolffc  460:     my $loncaparev = $r->dir_config('lonVersion');
1.10      www       461: 
1.6       www       462: # ================================================================== Roles Init
1.118     albertel  463:     if ($env{'form.selectrole'}) {
1.339     raeburn   464:         if (($env{'request.lti.login'}) && ($env{'request.lti.target'} eq '')) {
                    465:             if ($env{'form.ltitarget'} eq 'iframe') {
                    466:                 &Apache::lonnet::appenv({'request.lti.target' => 'iframe'});
                    467:                 delete($env{'form.ltitarget'});
                    468:             }
                    469:         }
1.188     www       470: 
                    471:         my $locknum=&Apache::lonnet::get_locks();
                    472:         if ($locknum) { return 409; }
                    473: 
1.315     raeburn   474:         my $custom_adhoc;
1.134     www       475:         if ($env{'form.newrole'}) {
                    476:             $env{'form.'.$env{'form.newrole'}}=1;
1.325     raeburn   477: # Check if this is a Domain Helpdesk or Domain Helpdesk Assistant role trying to enter a course
1.315     raeburn   478:             if ($env{'form.newrole'} =~ m{^cr/($match_domain)/\1\-domainconfig/\w+\./\1/$match_courseid$}) {
1.325     raeburn   479:                 if ($helpdeskroles{$1}) {
1.315     raeburn   480:                     $custom_adhoc = 1;
                    481:                 }
                    482:             }
1.134     www       483: 	}
1.118     albertel  484: 	if ($env{'request.course.id'}) {
1.185     raeburn   485:             # Check if user is CC trying to select a course role
                    486:             if ($env{'form.switchrole'}) {
1.252     raeburn   487:                 my $switch_is_active;
                    488:                 if (defined($env{'user.role.'.$env{'form.switchrole'}})) {
                    489:                     my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
                    490:                     if (!$end || $end > $now) {
1.260     raeburn   491:                         if (!$start || $start < $update) {
1.252     raeburn   492:                             $switch_is_active = 1;
                    493:                         }
                    494:                     }
                    495:                 }
                    496:                 unless ($switch_is_active) {
1.260     raeburn   497:                     &adhoc_course_role($refresh,$update,$then);
1.185     raeburn   498:                 }
                    499:             }
1.118     albertel  500: 	    my %temp=('logout_'.$env{'request.course.id'} => time);
1.33      www       501: 	    &Apache::lonnet::put('email_status',\%temp);
1.118     albertel  502: 	    &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
1.100     albertel  503: 	}
1.310     raeburn   504: 	&Apache::lonnet::appenv({"request.course.id"           => '',
                    505: 			 	 "request.course.fn"           => '',
                    506: 				 "request.course.uri"          => '',
                    507: 				 "request.course.sec"          => '',
                    508:                                  "request.course.tied"         => '',
                    509:                                  "request.course.timechecked"  => '',
1.368     raeburn   510:                                  "request.course.suppupdated"  => '',
1.377   ! raeburn   511:                                  "request.course.deeponlyprot" => '',
1.310     raeburn   512: 				 "request.role"                => 'cm',
                    513:                                  "request.role.adv"            => $env{'user.adv'},
                    514: 				 "request.role.domain"         => $env{'user.domain'}});
1.315     raeburn   515: # Check if Domain Helpdesk role trying to enter a course needs privs to be created
1.322     raeburn   516:         if ($env{'form.newrole'} =~ m{^cr/($match_domain)/\1\-domainconfig/(\w+)\./\1/($match_courseid)(?:/(\w+)|$)}) {
1.315     raeburn   517:             my $cdom = $1;
                    518:             my $rolename = $2;
                    519:             my $cnum = $3;
1.322     raeburn   520:             my $sec = $4;
1.315     raeburn   521:             if ($custom_adhoc) {
1.324     raeburn   522:                 my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($cdom.'_'.$cnum,1);
1.323     raeburn   523:                 if (ref($possroles) eq 'ARRAY') {
                    524:                     if (grep(/^\Q$rolename\E$/,@{$possroles})) { 
1.315     raeburn   525:                         if (&Apache::lonnet::check_adhoc_privs($cdom,$cnum,$update,$refresh,$now,
1.322     raeburn   526:                                                                "cr/$cdom/$cdom".'-domainconfig/'.$rolename,undef,$sec)) {
1.316     raeburn   527:                             &Apache::lonnet::appenv({"environment.internal.$cdom.$cnum.cr/$cdom/$cdom".'-domainconfig/'."$rolename.adhoc" => time});
1.315     raeburn   528:                         }
                    529:                     }
                    530:                 }
                    531:             }
1.325     raeburn   532:         } elsif (($numdc > 0) || ($numhelpdesk > 0)) {
1.182     www       533: # Check if user is a DC trying to enter a course or author space and needs privs to be created
1.325     raeburn   534: # Check if user is a DH or DA trying to enter a course and needs privs to be created
1.296     raeburn   535:             foreach my $envkey (keys(%env)) {
1.240     raeburn   536: # Is this an ad-hoc Coordinator role?
1.319     raeburn   537:                 if ($numdc) {
                    538:                     if (my ($ccrole,$domain,$coursenum) =
                    539: 		        ($envkey =~ m-^form\.(cc|co)\./($match_domain)/($match_courseid)$-)) {
                    540:                         if ($dcroles{$domain}) {
                    541:                             if (&Apache::lonnet::check_adhoc_privs($domain,$coursenum,
                    542:                                                                    $update,$refresh,$now,$ccrole)) {
                    543:                                 &Apache::lonnet::appenv({"environment.internal.$domain.$coursenum.$ccrole.adhoc" => time});
                    544:                             }
1.275     raeburn   545:                         }
1.319     raeburn   546:                         last;
1.182     www       547:                     }
1.193     raeburn   548: # Is this an ad-hoc CA-role?
1.319     raeburn   549:                     if (my ($domain,$user) =
                    550: 		        ($envkey =~ m-^form\.ca\./($match_domain)/($match_username)$-)) {
                    551:                         if (($domain eq $env{'user.domain'}) && ($user eq $env{'user.name'})) {
                    552:                             delete($env{$envkey});
                    553:                             $env{'form.au./'.$domain.'/'} = 1;
1.206     raeburn   554:                             my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    555:                             if ($server_status eq 'switchserver') {
1.319     raeburn   556:                                 my $trolecode = 'au./'.$domain.'/';
1.248     raeburn   557:                                 my $switchserver = '/adm/switchserver?otherserver='.$home.'&amp;role='.$trolecode;
1.206     raeburn   558:                                 $r->internal_redirect($switchserver);
1.285     raeburn   559:                                 return OK;
1.206     raeburn   560:                             }
                    561:                             last;
                    562:                         }
1.319     raeburn   563:                         if (my ($castart,$caend) = ($env{'user.role.ca./'.$domain.'/'.$user} =~ /^(\d*)\.(\d*)$/)) {
                    564:                             if (((($castart) && ($castart < $now)) || !$castart) && 
                    565:                                 ((!$caend) || (($caend) && ($caend > $now)))) {
                    566:                                 my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    567:                                 if ($server_status eq 'switchserver') {
                    568:                                     my $trolecode = 'ca./'.$domain.'/'.$user;
                    569:                                     my $switchserver = '/adm/switchserver?otherserver='.$home.'&amp;role='.$trolecode;
                    570:                                     $r->internal_redirect($switchserver);
                    571:                                     return OK;
                    572:                                 }
                    573:                                 last;
                    574:                             }
                    575:                         }
                    576:                         # Check if author blocked ca-access
                    577:                         my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],$domain,$user);
                    578:                         if ($blocked{'domcoord.author'} eq 'blocked') {
                    579:                             delete($env{$envkey});
                    580:                             $env{'user.error.msg'}=':::1:User '.$user.' in domain '.$domain.' blocked domain coordinator access';
                    581:                             last;
                    582:                         }
                    583:                         if ($dcroles{$domain}) {
                    584:                             my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    585:                             if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
                    586:                                 &Apache::lonnet::check_adhoc_privs($domain,$user,$update,
                    587:                                                                    $refresh,$now,'ca');
                    588:                                 if ($server_status eq 'switchserver') {
                    589:                                     my $trolecode = 'ca./'.$domain.'/'.$user; 
                    590:                                     my $switchserver = '/adm/switchserver?'
                    591:                                                       .'otherserver='.$home.'&amp;role='.$trolecode;
                    592:                                     $r->internal_redirect($switchserver);
                    593:                                     return OK;
                    594:                                 }
                    595:                             } else {
                    596:                                 delete($env{$envkey});
1.193     raeburn   597:                             }
                    598:                         } else {
                    599:                             delete($env{$envkey});
                    600:                         }
1.319     raeburn   601:                         last;
1.182     www       602:                     }
                    603:                 }
1.325     raeburn   604:                 if ($numhelpdesk) {
1.319     raeburn   605: # Is this an ad hoc custom role in a course/community?
1.322     raeburn   606:                     if (my ($domain,$rolename,$coursenum,$sec) = ($envkey =~ m{^form\.cr/($match_domain)/\1\-domainconfig/(\w+)\./\1/($match_courseid)(?:/(\w+)|$)})) {
1.325     raeburn   607:                         if ($helpdeskroles{$domain}) {
1.324     raeburn   608:                             my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($domain.'_'.$coursenum,1);
1.323     raeburn   609:                             if (ref($possroles) eq 'ARRAY') {
                    610:                                 if (grep(/^\Q$rolename\E$/,@{$possroles})) {
1.320     raeburn   611:                                     if (&Apache::lonnet::check_adhoc_privs($domain,$coursenum,$update,$refresh,$now,
1.322     raeburn   612:                                                                            "cr/$domain/$domain".'-domainconfig/'.$rolename,
                    613:                                                                            undef,$sec)) {
1.320     raeburn   614:                                         &Apache::lonnet::appenv({"environment.internal.$domain.$coursenum.cr/$domain/$domain".
                    615:                                                                  '-domainconfig/'."$rolename.adhoc" => time});
                    616:                                     }
                    617:                                 } else {
                    618:                                     delete($env{$envkey});
                    619:                                 }
                    620:                             } else {
                    621:                                 delete($env{$envkey});
                    622:                             }
                    623:                         } else {
                    624:                             delete($env{$envkey});
                    625:                         }
                    626:                         last;
                    627:                     }
                    628:                 }
                    629:             }
1.107     raeburn   630:         }
1.296     raeburn   631:         foreach $envkey (keys(%env)) {
1.40      matthew   632:             next if ($envkey!~/^user\.role\./);
1.102     raeburn   633:             my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
1.260     raeburn   634:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.218     raeburn   635:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
1.118     albertel  636:             if ($env{'form.'.$trolecode}) {
1.55      albertel  637: 		if ($tstatus eq 'is') {
                    638: 		    $where=~s/^\///;
                    639: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
1.255     raeburn   640:                     if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
                    641:                         my $home = $env{'course.'.$cdom.'_'.$cnum.'.home'};
                    642:                         my @ids = &Apache::lonnet::current_machine_ids();
                    643:                         unless ($loncaparev eq '' && $home && grep(/^\Q$home\E$/,@ids)) {
                    644:                             my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
1.256     raeburn   645:                             if ($curr_reqd_hash{'internal.releaserequired'} ne '') {
1.255     raeburn   646:                                 my ($switchserver,$switchwarning) =
1.310     raeburn   647:                                     &Apache::loncommon::check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode,
                    648:                                                                                $curr_reqd_hash{'internal.releaserequired'});
1.256     raeburn   649:                                 if ($switchwarning ne '' || $switchserver ne '') {
                    650:                                     &Apache::loncommon::content_type($r,'text/html');
                    651:                                     &Apache::loncommon::no_cache($r);
                    652:                                     $r->send_http_header;
1.310     raeburn   653:                                     $r->print(&Apache::loncommon::check_release_result($switchwarning,$switchserver));
1.256     raeburn   654:                                     return OK;
1.255     raeburn   655:                                 }
                    656:                             }
                    657:                         }
                    658:                     }
1.137     raeburn   659: # check for course groups
                    660:                     my %coursegroups = &Apache::lonnet::get_active_groups(
                    661:                           $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
                    662:                     my $cgrps = join(':',keys(%coursegroups));
                    663: 
1.111     albertel  664: # store role if recent_role list being kept
1.118     albertel  665:                     if ($env{'environment.recentroles'}) {
1.158     albertel  666:                         my %frozen_roles =
                    667:                            &Apache::lonhtmlcommon::get_recent_frozen('roles',$env{'environment.recentrolesn'});
1.111     albertel  668: 			&Apache::lonhtmlcommon::store_recent('roles',
1.158     albertel  669: 							     $trolecode,' ',$frozen_roles{$trolecode});
1.111     albertel  670:                     }
                    671: 
                    672: 
1.53      www       673: # check for keyed access
1.55      albertel  674: 		    if (($role eq 'st') && 
1.118     albertel  675:                        ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
1.89      www       676: # who is key authority?
                    677: 			my $authdom=$cdom;
                    678: 			my $authnum=$cnum;
1.118     albertel  679: 			if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
1.89      www       680: 			    ($authnum,$authdom)=
1.172     albertel  681: 				split(/:/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
1.89      www       682: 			}
                    683: # check with key authority
                    684: 			unless (&Apache::lonnet::validate_access_key(
1.118     albertel  685: 				     $env{'environment.key.'.$cdom.'_'.$cnum},
1.89      www       686: 					     $authdom,$authnum)) {
1.53      www       687: # there is no valid key
1.376     raeburn   688:                              my $swinfo=&Apache::lonmenu::rawconfig();
                    689:                              my $crumbtext = 'User Roles';
                    690:                              my $show_course=&Apache::loncommon::show_course();
                    691:                              if ($show_course) {
                    692:                                  $crumbtext = 'Courses';
                    693:                              }
1.118     albertel  694: 			     if ($env{'form.newkey'}) {
1.53      www       695: # student attempts to register a new key
1.89      www       696: 				 &Apache::loncommon::content_type($r,'text/html');
                    697: 				 &Apache::loncommon::no_cache($r);
                    698: 				 $r->send_http_header;
1.376     raeburn   699:                                  my $pagetitle = 'Verifying Access Key to Unlock this Course';
                    700:                                  my $brcrum =[{href=>"/adm/roles",text=>$crumbtext},
                    701:                                               {href=>"/adm/roles",text=>'Verify Access Key'}];
1.147     albertel  702: 				 my $start_page=&Apache::loncommon::start_page
1.376     raeburn   703: 				    ($pagetitle, undef,{bread_crumbs=>$brcrum,});
1.147     albertel  704: 				 my $end_page=&Apache::loncommon::end_page();
1.376     raeburn   705:                                  my ($buttontext,$message,$inputitem,$accessheader);
1.344     raeburn   706:                                  my $ip = &Apache::lonnet::get_requestor_ip();
1.90      www       707: 				 my $assignresult=
                    708: 				     &Apache::lonnet::assign_access_key(
1.118     albertel  709: 						     $env{'form.newkey'},
1.90      www       710: 						     $authdom,$authnum,
1.91      www       711: 						     $cdom,$cnum,
1.118     albertel  712:                                                      $env{'user.domain'},
                    713: 						     $env{'user.name'},
1.204     bisitz    714:                                                      &mt('Assigned from [_1] at [_2] for [_3]'
1.344     raeburn   715:                                                         ,$ip
1.373     raeburn   716:                                                         ,&Apache::lonlocal::locallocaltime($now)
1.204     bisitz    717:                                                         ,$trolecode)
                    718:                                                      );
1.376     raeburn   719: 				 if ($assignresult eq 'ok') {
                    720:                                      $buttontext=&mt('Enter Course');
                    721:                                      $message=&mt('Successfully registered key');
                    722:                                      $accessheader = &mt('Access key validation complete');
                    723:                                  } else {
                    724:                                      $buttontext=&mt('Submit');
                    725:                                      $assignresult=~s/^error\:\s*//;
                    726:                                      $accessheader = &mt('Access key validation incomplete');
                    727:                                      $message = &mt('Key: [_1]',
                    728:                                                 &HTML::Entities::encode($env{'form.newkey'},
                    729:                                                                         '\':<>&"')).' <br />'.
                    730:                                                 &mt('Result').': '.&mt($assignresult);
                    731:                                      my $labeltext = &mt('Enter access key');
                    732:                                      $inputitem = '<label>'.$labeltext.':'.
                    733:                                          '<input type="text" size="20" name="newkey" value="'.
                    734:                                          $env{'form.newkey'}.'" /></label>';
                    735:                                  }
                    736:                                  $r->print(<<"ENDREGKEY");
1.147     albertel  737: $start_page
1.179     raeburn   738: <script type="text/javascript">
1.225     bisitz    739: // <![CDATA[
1.89      www       740: $swinfo
1.225     bisitz    741: // ]]>
1.89      www       742: </script>
1.376     raeburn   743: <div class="LC_landmark" role="contentinfo">
                    744: <h2 class="LC_heading_2">$accessheader</h2>
                    745: <p>$message</p>
                    746: </div>
                    747: <div class="LC_landmark" role="main">
                    748: <form action="/adm/roles" method="post">
1.89      www       749: <input type="hidden" name="selectrole" value="1" />
                    750: <input type="hidden" name="$trolecode" value="1" />
1.376     raeburn   751: $inputitem
1.89      www       752: <input type="submit" value="$buttontext" />
                    753: </form>
1.376     raeburn   754: </div>
1.147     albertel  755: $end_page
1.376     raeburn   756: ENDREGKEY
1.89      www       757:                                  return OK;
1.55      albertel  758: 			     } else {
1.53      www       759: # print form to enter a new key
1.73      www       760: 				 &Apache::loncommon::content_type($r,'text/html');
1.55      albertel  761: 				 &Apache::loncommon::no_cache($r);
                    762: 				 $r->send_http_header;
1.376     raeburn   763:                                  my $pagetitle = 'Enter Access Key to Unlock this Course';
                    764:                                  my $brcrum =[{href=>"/adm/roles",text=>$crumbtext},
                    765:                                               {href=>"/adm/roles",text=>'Enter Access Key'}];
1.147     albertel  766: 				 my $start_page=&Apache::loncommon::start_page
1.376     raeburn   767: 				    ($pagetitle,undef,{bread_crumbs=>$brcrum,});
1.147     albertel  768: 				 my $end_page=&Apache::loncommon::end_page();
1.376     raeburn   769:                                  my $accessheader = &mt('Access to this course requires an access key');
                    770:                                  my $preamble = &mt('Once you have successfully entered a valid key, you will no longer be prompted for one when entering the course.');
                    771:                                  my $labeltext = &mt('Enter access key');
                    772:                                  my $submittext = &mt('Submit');
1.55      albertel  773: 				 $r->print(<<ENDENTERKEY);
1.147     albertel  774: $start_page
1.179     raeburn   775: <script type="text/javascript">
1.225     bisitz    776: // <![CDATA[
1.53      www       777: $swinfo
1.225     bisitz    778: // ]]>
1.53      www       779: </script>
1.376     raeburn   780: <div class="LC_landmark" role="main">
                    781: <h2 class="LC_heading_2">$accessheader</h2>
                    782: <p>$preamble</p>
1.225     bisitz    783: <form action="" method="post">
1.89      www       784: <input type="hidden" name="selectrole" value="1" />
                    785: <input type="hidden" name="$trolecode" value="1" />
1.376     raeburn   786: <label>$labeltext:
                    787: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" /></label>
                    788: <input type="submit" value="$submittext" />
1.53      www       789: </form>
1.376     raeburn   790: </div>
1.147     albertel  791: $end_page
1.53      www       792: ENDENTERKEY
1.55      albertel  793: 				 return OK;
                    794: 			     }
                    795: 			 }
                    796: 		     }
1.118     albertel  797: 		    &Apache::lonnet::log($env{'user.domain'},
                    798: 					 $env{'user.name'},
                    799: 					 $env{'user.home'},
1.87      www       800: 					 "Role ".$trolecode);
1.323     raeburn   801: 
1.56      www       802: 		    &Apache::lonnet::appenv(
1.186     raeburn   803: 					   {'request.role'        => $trolecode,
                    804: 					    'request.role.domain' => $cdom,
                    805: 					    'request.course.sec'  => $csec,
                    806:                                             'request.course.groups' => $cgrps});
1.101     albertel  807:                     my $tadv=0;
1.62      matthew   808: 
1.125     www       809: 		    if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
1.323     raeburn   810:                         if ($role =~ m{^\Qcr/$cdom/$cdom\E\-domainconfig/(\w+)$}) {
                    811:                             my $rolename = $1;
                    812:                             my %domdef = &Apache::lonnet::get_domain_defaults($cdom);
                    813:                             if (ref($domdef{'adhocroles'}) eq 'HASH') {
                    814:                                 if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                    815:                                     &Apache::lonnet::appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'}});
                    816:                                 }
                    817:                             }
                    818:                         }
1.347     raeburn   819:                         my $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                    820:                         $crstype = lc($crstype);
1.365     raeburn   821:                         my ($msg,$critmsg_check,$title,$loadmsg,$only_body);
                    822:                         $critmsg_check = 1;
                    823:                         $title = &mt("Loading $crstype");
                    824:                         $loadmsg = &mt("Please be patient while your $crstype loads");
                    825:                         if (($env{'request.deeplink.login'}) && ($env{'request.linkprot'})) {
                    826:                             if ($env{'request.linkprot'} =~ /^\d+(c|d):\Q$env{'form.destinationurl'}\E$/) {
                    827:                                 $title = &mt('Loading LON-CAPA session');
                    828:                                 $loadmsg = &mt('Please be patient while LON-CAPA loads');
                    829:                                 $only_body = 1;
                    830:                                 $critmsg_check = 0;
                    831:                             }
                    832:                         }
1.347     raeburn   833:                         my $preamble = '<div id="LC_update_'.$cdom.'_'.$cnum.'" class="LC_info">'.
                    834:                                        '<br />'.
1.365     raeburn   835:                                        $loadmsg.
1.347     raeburn   836:                                        '<br /></div>'.
                    837:                                        '<div style="padding:0;clear:both;margin:0;border:0"></div>';
                    838:                         my $closure = <<ENDCLOSE;
                    839: <script type="text/javascript">
                    840: // <![CDATA[
                    841: \$("#LC_update_${cdom}_${cnum}").hide('slow');
                    842: // ]]>
                    843: </script>
                    844: ENDCLOSE
1.365     raeburn   845:                         &start_loading_course($r,$title,$only_body);
1.376     raeburn   846:                         if ($only_body) {
                    847:                             $r->print('<h1 class="LC_visually_hidden">'.$title.'</h1>');
                    848:                         }
1.347     raeburn   849:                         my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
                    850:                         &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Loading ...'));
                    851:                         $r->rflush();
1.365     raeburn   852:                         if ($critmsg_check) {
                    853:                             my $blockcrit = &Apache::loncommon::blocking_status('alert',$clientip,$cnum,$cdom,undef,1);
                    854:                             if ($blockcrit) {
                    855:                                 my $checkrole = "cm./$cdom/$cnum";
                    856:                                 if ($csec ne '') {
                    857:                                     $checkrole .= "/$csec";
                    858:                                 }
                    859:                                 unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
                    860:                                         ($trolecode !~ m{^st\./$cdom/$cnum})) {
                    861:                                     $critmsg_check = 0;
                    862:                                 }
1.346     raeburn   863:                             }
                    864:                         }
1.347     raeburn   865:                         my ($furl,$ferr)=
                    866:                             &Apache::lonuserstate::readmap($cdom.'/'.$cnum,$critmsg_check);
                    867:                         &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!'));
                    868:                         &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    869:                         $r->print($closure);
                    870:                         $r->rflush();
                    871:                         if ($ferr) {
                    872:                             $furl = '/adm/roles?tryagain=1';
                    873:                         } else {
1.335     raeburn   874:                             &Apache::lonnet::appenv({'request.course.timechecked'=>$now});
1.284     raeburn   875:                             unless (($env{'form.switchrole'}) || 
                    876:                                     ($env{"environment.internal.$cdom.$cnum.$role.adhoc"})) {
                    877:                                 &Apache::lonnet::put('nohist_crslastlogin',
                    878:                                     {$env{'user.name'}.':'.$env{'user.domain'}.
                    879:                                      ':'.$csec.':'.$role => $now},$cdom,$cnum);
                    880:                             }
1.334     raeburn   881:                             if (($env{"environment.internal.$cdom.$cnum.$role.adhoc"}) &&
                    882:                                 (&Apache::lonnet::allowed('vxc',$cdom.'_'.$cnum))) {
                    883:                                 my $owner = $env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'};
                    884:                                 my @coowners = split(/,/,$env{'course.'.$env{'request.course.id'}.'.internal.co-owners'});
                    885:                                 my %auaccess;
                    886:                                 foreach my $user ($owner,@coowners) {
                    887:                                     my ($cpname,$cpdom) = split(/:/,$user);
                    888:                                     my %auroles = &Apache::lonnet::get_my_roles($cpname,$cpdom,'userroles',undef,['au','ca','aa'],[$cdom]);
                    889:                                     foreach my $key (keys(%auroles)) {
                    890:                                         my ($auname,$audom,$aurole) = split(/:/,$key);
                    891:                                         if ($aurole eq 'au') {
                    892:                                             $auaccess{$cpname} = 1;
                    893:                                         } else {
                    894:                                             $auaccess{$auname} = 1;
                    895:                                         }
                    896:                                     }
                    897:                                 }
                    898:                                 &Apache::lonnet::appenv({'request.course.adhocsrcaccess' => join(',',sort(keys(%auaccess))) });
                    899:                             }
1.284     raeburn   900:                             my ($feeds,$syllabus_time);
1.283     raeburn   901:                             &Apache::lonrss::advertisefeeds($cnum,$cdom,undef,\$feeds);
1.284     raeburn   902:                             &Apache::lonnet::appenv({'request.course.feeds' => $feeds});
                    903:                             unless ($env{'course.'.$cdom.'_'.$cnum.'.updatedsyllabus'}) {
                    904:                                 unless (($env{'course.'.$cdom.'_'.$cnum.'.externalsyllabus'}) ||
                    905:                                         ($env{'course.'.$cdom.'_'.$cnum.'.uploadedsyllabus'})) {
                    906:                                     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);
                    907:                                     $syllabus_time = $syllabus{'uploaded.lastmodified'};
                    908:                                     if ($syllabus_time) {
                    909:                                         &Apache::lonnet::appenv({'request.course.syllabustime' => $syllabus_time});
                    910:                                     }
                    911:                                 }
                    912:                             }
1.275     raeburn   913:                         }
1.118     albertel  914: 			if (($env{'form.orgurl'}) && 
1.292     raeburn   915: 			    ($env{'form.orgurl'}!~/^\/adm\/flip/) &&
                    916: 			    ($env{'form.orgurl'} ne '/adm/roles')) {
1.118     albertel  917: 			    my $dest=$env{'form.orgurl'};
1.219     raeburn   918:                             if ($env{'form.symb'}) {
                    919:                                 if ($dest =~ /\?/) {
                    920:                                     $dest .= '&';
                    921:                                 } else {
1.292     raeburn   922:                                     $dest .= '?';
1.219     raeburn   923:                                 }
                    924:                                 $dest .= 'symb='.$env{'form.symb'};
                    925:                             }
1.117     albertel  926: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   927: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.347     raeburn   928:                             if ($ferr) {
                    929:                                 if ($env{'form.orgurl'}) {
                    930:                                     $furl .= '&orgurl='.&HTML::Entities::encode($env{'form.orgurl'},'<>&"');
                    931:                                 }
                    932:                                 if ($env{'form.symb'}) {
1.348     raeburn   933:                                     $furl .= '&symb='.&HTML::Entities::encode($env{'form.symb'},'<>&"');
1.347     raeburn   934:                                 }
1.368     raeburn   935:                             } else {
                    936:                                 &set_supplemental_access($cnum,$cdom);
1.347     raeburn   937:                             }
1.150     www       938:                             if (($ferr) && ($tadv)) {
1.347     raeburn   939: 				&error_page($r,$ferr,$furl);
1.150     www       940: 			    } else {
1.345     raeburn   941:                                 if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                    942:                                     if (($env{'form.orgurl'} ne '') && ($env{'form.symb'} ne '')) {
1.348     raeburn   943:                                         unless (&Apache::lonnet::symbverify($env{'form.symb'},$env{'form.orgurl'})) {
1.345     raeburn   944:                                             $dest=$env{'form.orgurl'};
                    945:                                         }
                    946:                                     } 
                    947:                                 }
1.255     raeburn   948:                                 if ($dest =~ m{^/adm/coursedocs\?folderpath}) {
                    949:                                     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) { 
                    950:                                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.268     raeburn   951:                                         &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,
                    952:                                                                                        $cdom.'_'.$cnum);
1.255     raeburn   953:                                     }
                    954:                                 }
1.347     raeburn   955:                                 if ($ferr) {
                    956:                                     if (!$env{'request.course.id'}) {
                    957:                                         &Apache::lonnet::appenv(
                    958:                                            {"request.course.id"  => $cdom.'_'.$cnum});
                    959:                                         $r->print('<p class="LC_error">'.
                    960:                                                   &mt('Could not initialize [_1] at this time.',
                    961:                                                       $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                    962:                                                   '</p>'.
                    963:                                                   '<p><a href="'.$furl.'">'.
                    964:                                                   &mt('Please try again.').'</a></p>'.
                    965:                                                   &Apache::loncommon::end_page());
                    966:                                     }
                    967:                                 } else {
                    968:                                     if (($env{'request.lti.login'}) &&
                    969:                                         ($env{'request.lti.rosterid'} || $env{'request.lti.passbackid'})) {
                    970:                                         &process_lti($r,$cdom,$cnum);
                    971:                                     }
1.363     raeburn   972:                                     if ($env{'request.deeplink.login'}) {
                    973:                                         &set_deeplink_target($cnum,$cdom);
                    974:                                     }
1.347     raeburn   975:                                     $msg = '<p>'.&mt('Entering [_1] ...',
                    976:                                                      $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                    977:                                            '</p>';
1.365     raeburn   978:                                     &finish_loading_course($r,$msg,$dest,$only_body);
1.338     raeburn   979:                                 }
1.150     www       980: 			    }
1.347     raeburn   981:                             $r->rflush();
1.55      albertel  982: 			    return OK;
                    983: 			} else {
1.155     albertel  984: 			    if (!$env{'request.course.id'}) {
1.55      albertel  985: 				&Apache::lonnet::appenv(
1.186     raeburn   986: 				      {"request.course.id"  => $cdom.'_'.$cnum});
1.55      albertel  987: 			    }
1.117     albertel  988: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   989: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.347     raeburn   990: 			    if ($ferr) {
                    991:                                 if ($tadv) {
                    992: 				    &error_page($r,$ferr,$furl);
                    993:                                 } else {
                    994:                                     $r->print('<p class="LC_error">'.
                    995:                                               &mt('Could not initialize [_1] at this time.',
                    996:                                                   $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                    997:                                               '</p>'.
                    998:                                               '<p><a href="'.$furl.'">'.&mt('Please try again.').'</a></p>'.
1.376     raeburn   999:                                               '</div>'.
1.347     raeburn  1000:                                               &Apache::loncommon::end_page());
                   1001:                                 }
1.150     www      1002: 			    } else {
1.368     raeburn  1003:                                 &set_supplemental_access($cnum,$cdom);
1.338     raeburn  1004:                                 if (($env{'request.lti.login'}) &&
                   1005:                                     ($env{'request.lti.rosterid'} || $env{'request.lti.passbackid'})) {
                   1006:                                     &process_lti($r,$cdom,$cnum);
                   1007:                                 }
1.363     raeburn  1008:                                 if ($env{'request.deeplink.login'}) {
                   1009:                                     &set_deeplink_target($cnum,$cdom);
                   1010:                                 }
1.150     www      1011: 				# Check to see if the user is a CC entering a course 
                   1012: 				# for the first time
1.240     raeburn  1013: 				if ((($role eq 'cc') || ($role eq 'co')) 
1.297     raeburn  1014:                                     && ($env{'course.'.$cdom.'_'.$cnum.'.course.helper.not.run'})) { 
1.150     www      1015: 				    $furl = "/adm/helper/course.initialization.helper";
                   1016: 				    # Send the user to the course they selected
                   1017: 				} elsif ($env{'request.course.id'}) {
1.313     raeburn  1018:                                     if ((&Apache::loncommon::course_type() eq 'Placement') && 
                   1019:                                         (!$env{'request.role.adv'})) {
                   1020:                                         my ($score,$incomplete) = 
                   1021:                                             &Apache::lonplacementtest::check_completion(undef,undef,1);
                   1022:                                         if (($incomplete) && ($incomplete < 100)) {
1.347     raeburn  1023:                                             $msg = '<p>'.&mt('Entering [_1] ...',
                   1024:                                                              $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                   1025:                                                    '</p>';
1.365     raeburn  1026:                                             &finish_loading_course($r,$msg,'/adm/placement',$only_body);
1.347     raeburn  1027:                                             $r->rflush();
1.313     raeburn  1028:                                             return OK;
                   1029:                                         }
                   1030:                                     }
1.276     raeburn  1031:                                     my ($dest,$destsymb,$checkenc);
                   1032:                                     $dest = $env{'form.destinationurl'};
                   1033:                                     $destsymb = $env{'form.destsymb'};
                   1034:                                     if ($dest ne '') {
                   1035:                                         if ($env{'form.switchrole'}) {
                   1036:                                             if ($destsymb ne '') {
                   1037:                                                 if ($destsymb !~ m{^/enc/}) {
                   1038:                                                     unless ($env{'request.role.adv'}) {
                   1039:                                                         $checkenc = 1;
                   1040:                                                     }
                   1041:                                                 }
                   1042:                                             }
1.327     raeburn  1043:                                             if (($dest =~ m{^\Q/public/$cdom/$cnum/syllabus\E.*(\?|\&)usehttp=1}) ||
                   1044:                                                 ($dest =~ m{^\Q/adm/wrapper/ext/\E(?!https:)})) {
1.326     raeburn  1045:                                                 if ($ENV{'SERVER_PORT'} == 443) {
1.349     raeburn  1046:                                                     my $hostname = $r->hostname();
                   1047:                                                     unless ((&Apache::lonnet::uses_sts()) ||
                   1048:                                                             (&Apache::lonnet::waf_allssl($hostname))) {
1.342     raeburn  1049:                                                         if ($hostname ne '') {
                   1050:                                                             $dest = 'http://'.$hostname.$dest;
                   1051:                                                         }
1.326     raeburn  1052:                                                     }
                   1053:                                                 }
                   1054:                                             }
1.276     raeburn  1055:                                             if ($dest =~ m{^/enc/}) {
                   1056:                                                 if ($env{'request.role.adv'}) {
                   1057:                                                     $dest = &Apache::lonenc::unencrypted($dest);
                   1058:                                                     if ($destsymb eq '') {
1.277     raeburn  1059:                                                         ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]*)/);
1.276     raeburn  1060:                                                         $destsymb = &unescape($destsymb);
                   1061:                                                     }
                   1062:                                                 }
                   1063:                                             } else {
                   1064:                                                 if ($destsymb eq '') {
1.280     raeburn  1065:                                                     ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]+)/);
1.276     raeburn  1066:                                                     $destsymb = &unescape($destsymb);
                   1067:                                                 }
                   1068:                                                 unless ($env{'request.role.adv'}) {
                   1069:                                                     $checkenc = 1;
                   1070:                                                 }
                   1071:                                             }
                   1072:                                             if (($checkenc) && ($destsymb ne '')) {
                   1073:                                                 my ($encstate,$unencsymb,$res);
1.281     raeburn  1074:                                                 $unencsymb = &Apache::lonnet::symbclean($destsymb);
1.276     raeburn  1075:                                                 (undef,undef,$res) = &Apache::lonnet::decode_symb($unencsymb);
                   1076:                                                 &Apache::lonnet::symbverify($unencsymb,$res,\$encstate);
                   1077:                                                 if ($encstate) {
                   1078:                                                     if (($dest ne '') && ($dest !~ m{^/enc/})) {
                   1079:                                                         $dest=&Apache::lonenc::encrypted($dest);
                   1080:                                                     }
                   1081:                                                 }
                   1082:                                             }
                   1083:                                         }
1.277     raeburn  1084:                                         unless (($dest =~ m{^/enc/}) || ($dest =~ /(\?|\&)symb=.+___\d+___.+/)) {
1.276     raeburn  1085:                                             if (($destsymb ne '') && ($destsymb !~ m{^/enc/})) {
                   1086:                                                 my $esc_symb = &escape($destsymb);
1.326     raeburn  1087:                                                 $dest .= (($dest =~/\?/)? '&':'?').'symb='.$esc_symb;
1.276     raeburn  1088:                                             }
1.203     raeburn  1089:                                         }
1.353     raeburn  1090:                                         if ($env{'form.ttoken'}) {
                   1091:                                             $dest .= (($dest =~/\?/)? '&':'?').'ttoken='.$env{'form.ttoken'};
                   1092:                                         }
1.366     raeburn  1093:                                         unless (($env{'request.lti.login'}) || ($env{'request.deeplink.login'})) {
1.347     raeburn  1094:                                             $msg = '<p>'.&mt('Entering [_1] ...',
                   1095:                                                              $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                   1096:                                                    '</p>';
1.337     raeburn  1097:                                         }
1.365     raeburn  1098:                                         &finish_loading_course($r,$msg,$dest,$only_body);
1.347     raeburn  1099:                                         $r->rflush();
1.185     raeburn  1100:                                         return OK;
                   1101:                                     }
1.150     www      1102: 				    if (&Apache::lonnet::allowed('whn',
                   1103: 								 $env{'request.course.id'})
                   1104: 					|| &Apache::lonnet::allowed('whn',
                   1105: 								    $env{'request.course.id'}.'/'
                   1106: 								    .$env{'request.course.sec'})
                   1107: 					) {
1.297     raeburn  1108: 					my $startpage = &courseloadpage($env{'request.course.id'});
1.150     www      1109: 					unless ($startpage eq 'firstres') {         
1.347     raeburn  1110: 					    $msg = '<p>'.&mt('Entering [_1] ...',
                   1111: 						             $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                   1112:                                                    '</p>';
1.365     raeburn  1113:                                             &finish_loading_course($r,$msg,'/adm/whatsnew?refpage=start',$only_body);
1.347     raeburn  1114:                                             $r->rflush();
1.150     www      1115: 					    return OK;
                   1116: 					}
                   1117: 				    }
                   1118: 				}
1.300     raeburn  1119:                                 # Are we allowed to look at the first resource?
1.351     raeburn  1120:                                 #
                   1121:                                 # $furl returned by lonuserstate::readmap() has format:
                   1122:                                 # $url?symb=escaped($symb). If the resource has the 
                   1123:                                 # encrypturl parameter in effect, the entire string
                   1124:                                 # $url?symb=escaped($symb) is encrypted as a string
                   1125:                                 # beginning /enc/.
                   1126:                                 # 
                   1127:                                 my ($access,$unencfurl,$unencsymb);
                   1128:                                 if ($furl =~ m{^(.+)(?:\?|\&)symb=([^&]+)(?:$|&)}) {
                   1129:                                     my ($poss_url,$poss_symb) = ($1,$2);
                   1130:                                     $unencsymb = &unescape($poss_symb);
                   1131:                                     $unencfurl = $poss_url;
                   1132:                                 } elsif ($furl =~ m{^/enc/}) {
                   1133:                                     my $unenc = &Apache::lonenc::unencrypted($furl);
                   1134:                                     if ($unenc =~ m{^(.+)(?:\?|\&)symb=([^&]+)(?:$|&)}) {
                   1135:                                         ($unencfurl,$unencsymb) = ($1,$2);
                   1136:                                         $unencsymb = &unescape($unencsymb);
                   1137:                                     } else {
                   1138:                                         $unencfurl = $unenc;
                   1139:                                     }
1.311     raeburn  1140:                                 } else {
1.351     raeburn  1141:                                     $unencfurl = $furl;
1.311     raeburn  1142:                                 }
1.351     raeburn  1143:                                 if ($unencsymb) {
                   1144:                                     my $symb = &Apache::lonnet::symbclean($unencsymb);
                   1145:                                     if (($symb ne '') && (&Apache::lonnet::symbverify($symb,$unencfurl))) {
                   1146:                                         $access = &Apache::lonnet::allowed('bre',$unencfurl,$symb);
                   1147:                                     } else {
                   1148:                                         $access = &Apache::lonnet::allowed('bre',$unencfurl);
                   1149:                                     }
                   1150:                                 } else {
                   1151:                                     $access = &Apache::lonnet::allowed('bre',$unencfurl);
                   1152:                                 }
                   1153:                                 if ((!$access) || ($access eq 'B') || ($access eq 'D')) {
1.299     musolffc 1154:                                     $furl = &Apache::lonpageflip::first_accessible_resource();
1.351     raeburn  1155:                                     if ($furl eq '') {
                   1156:                                         $furl = '/adm/navmaps?showOnlyHomework=1';
                   1157:                                     }
1.299     musolffc 1158:                                 }
1.337     raeburn  1159:                                 if ($env{'request.lti.login'}) {
                   1160:                                     undef($msg);
1.365     raeburn  1161:                                     &finish_loading_course($r,$msg,$furl,$only_body);
1.337     raeburn  1162:                                 } else {
1.347     raeburn  1163:                                     $msg = '<p>'.&mt('Entering [_1] ...',
                   1164: 					              $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                   1165:                                            '</p>';
1.365     raeburn  1166:                                     &finish_loading_course($r,$msg,$furl,$only_body);
1.337     raeburn  1167:                                 }
1.58      bowersj2 1168: 			    }
1.347     raeburn  1169:                             $r->rflush();
1.124     albertel 1170: 			    return OK;
1.55      albertel 1171: 			}
                   1172: 		    }
1.62      matthew  1173:                     #
                   1174:                     # Send the user to the construction space they selected
1.125     www      1175:                     if ($role =~ /^(au|ca|aa)$/) {
1.62      matthew  1176:                         my $redirect_url = '/priv/';
                   1177:                         if ($role eq 'au') {
1.262     www      1178:                             $redirect_url.=$env{'user.domain'}.'/'.$env{'user.name'};
1.62      matthew  1179:                         } else {
1.263     www      1180:                             $redirect_url .= $where;
1.62      matthew  1181:                         }
                   1182:                         $redirect_url .= '/';
1.367     raeburn  1183:                         if ($env{'form.orgurl'} =~ /^\Q$redirect_url\E/) {
                   1184:                             my ($path) = ($env{'form.orgurl'} =~ m{^(.+)/[^/]+$});
                   1185:                             if (($path ne '') && (-e $Apache::lonnet::perlvar{'lonDocRoot'}.$path)) {
                   1186:                                 $redirect_url = $env{'form.orgurl'};
                   1187:                             }
                   1188:                         }
1.288     raeburn  1189:                         &redirect_user($r,&mt('Entering Authoring Space'),
1.62      matthew  1190:                                        $redirect_url);
                   1191:                         return OK;
                   1192:                     }
1.104     raeburn  1193:                     if ($role eq 'dc') {
1.108     raeburn  1194:                         my $redirect_url = '/adm/menu/';
                   1195:                         &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104     raeburn  1196:                                        $redirect_url);
1.108     raeburn  1197:                         return OK;
1.104     raeburn  1198:                     }
1.315     raeburn  1199:                     if ($role eq 'dh') {
                   1200:                         my $redirect_url = '/adm/menu/';
                   1201:                         &redirect_user($r,&mt('Loading Domain Helpdesk Menu'),
                   1202:                                        $redirect_url);
                   1203:                         return OK;
                   1204:                     }
1.325     raeburn  1205:                     if ($role eq 'da') {
                   1206:                         my $redirect_url = '/adm/menu/';
                   1207:                         &redirect_user($r,&mt('Loading Domain Helpdesk Assistant Menu'),
                   1208:                                        $redirect_url);
                   1209:                         return OK;
                   1210:                     }
1.220     raeburn  1211:                     if ($role eq 'sc') {
                   1212:                         my $redirect_url = '/adm/grades?command=scantronupload';
                   1213:                         &redirect_user($r,&mt('Loading Data Upload Page'),
                   1214:                                        $redirect_url);
                   1215:                         return OK;
                   1216:                     }
1.55      albertel 1217: 		}
                   1218:             }
1.6       www      1219:         }
1.40      matthew  1220:     }
1.44      www      1221: 
1.10      www      1222: 
1.6       www      1223: # =============================================================== No Roles Init
1.10      www      1224: 
1.73      www      1225:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel 1226:     &Apache::loncommon::no_cache($r);
1.10      www      1227:     $r->send_http_header;
                   1228:     return OK if $r->header_only;
                   1229: 
1.224     raeburn  1230:     my $crumbtext = 'User Roles';
                   1231:     my $pagetitle = 'My Roles';
                   1232:     my $recent = &mt('Recent Roles');
1.287     raeburn  1233:     my $standby = &mt('Role selected. Please stand by.');
1.224     raeburn  1234:     my $show_course=&Apache::loncommon::show_course();
                   1235:     if ($show_course) {
                   1236:         $crumbtext = 'Courses';
                   1237:         $pagetitle = 'My Courses';
                   1238:         $recent = &mt('Recent Courses');
1.347     raeburn  1239:         $standby = &mt('Course selected. Please stand by.');
1.224     raeburn  1240:     }
1.355     raeburn  1241:     if (($norolelist) && ((split(/:/,$env{'user.error.msg'}))[2])) {
                   1242:         $crumbtext = 'Access Denied';
                   1243:         $pagetitle = 'Unauthorized';
                   1244:     }
1.224     raeburn  1245:     my $brcrum =[{href=>"/adm/roles",text=>$crumbtext}];
1.274     raeburn  1246: 
                   1247:     my %roles_in_env;
                   1248:     my $showcount = &roles_from_env(\%roles_in_env,$update); 
                   1249: 
1.52      www      1250:     my $swinfo=&Apache::lonmenu::rawconfig();
1.302     raeburn  1251:     my %domdefs=&Apache::lonnet::get_domain_defaults($env{'user.domain'}); 
                   1252:     my $cattype = 'std';
                   1253:     if ($domdefs{'catauth'}) {
                   1254:         $cattype = $domdefs{'catauth'};
                   1255:     }
1.313     raeburn  1256:     my $placementonly;
                   1257:     if ($showcount == 1) {
                   1258:         if ($env{'request.course.id'}) {
                   1259:             if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
                   1260:                 $placementonly = 1;
                   1261:             }
                   1262:         } else {
                   1263:             foreach my $rolecode (keys(%roles_in_env)) {
                   1264:                 my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
                   1265:                 if ($cid) {
                   1266:                     my %coursedescription =
                   1267:                         &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
                   1268:                     if ($coursedescription{'type'} eq 'Placement') {
                   1269:                         $placementonly = 1;
                   1270:                     }
                   1271:                     last;
                   1272:                 }
                   1273:             }
                   1274:         }
                   1275:     }
                   1276:     my ($start_page,$funcs);
                   1277:     if ($placementonly) {
                   1278:         $start_page=&Apache::loncommon::start_page($pagetitle,undef,
                   1279:                                                   {bread_crumbs=>$brcrum,crstype=>'Placement'});
                   1280:     } else {
1.374     raeburn  1281:         my ($crumbsright,$crumbs_style);
1.355     raeburn  1282:         unless (($norolelist) && ((split(/:/,$env{'user.error.msg'}))[2])) {
1.370     raeburn  1283:             $funcs = &get_roles_functions($showcount,$cattype,$domdefs{'userapprovals'});
1.355     raeburn  1284:             if ($env{'browser.mobile'}) {
                   1285:                 $crumbsright = $funcs;
                   1286:                 undef($funcs);
1.374     raeburn  1287:                 $crumbs_style = 'overflow: visible;';
1.355     raeburn  1288:             }
1.314     raeburn  1289:         }
                   1290:         $start_page=&Apache::loncommon::start_page($pagetitle,undef,{bread_crumbs=>$brcrum,
1.374     raeburn  1291:                                                                      bread_crumbs_component=>$crumbsright,
                   1292:                                                                      bread_crumbs_style=>$crumbs_style,});
1.313     raeburn  1293:     }
1.312     damieng  1294:     &js_escape(\$standby);
1.274     raeburn  1295:     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      1296: 
1.10      www      1297:     $r->print(<<ENDHEADER);
1.147     albertel 1298: $start_page
1.274     raeburn  1299: $funcs
1.179     raeburn  1300: <noscript>
                   1301: $noscript
                   1302: </noscript>
                   1303: <script type="text/javascript">
1.225     bisitz   1304: // <![CDATA[
1.26      www      1305: $swinfo
                   1306: window.focus();
1.134     www      1307: 
                   1308: active=true;
                   1309: 
                   1310: function enterrole (thisform,rolecode,buttonname) {
                   1311:     if (active) {
                   1312: 	active=false;
                   1313:         document.title='$standby';
                   1314:         window.status='$standby';
                   1315: 	thisform.newrole.value=rolecode;
                   1316: 	thisform.submit();
                   1317:     } else {
                   1318:        alert('$standby');
1.260     raeburn  1319:     }
                   1320: }
                   1321: 
1.274     raeburn  1322: function rolesView (caller) {
1.370     raeburn  1323:     if (caller == 'approvals') {
                   1324:         document.rolechoice.approvals.value = 'show';
                   1325:     } else if (caller == 'noapprovals') {
                   1326:         document.rolechoice.approvals.value = 'hide';
                   1327:     } else if ((caller == 'showall') || (caller == 'noshowall')) {
1.274     raeburn  1328:         document.rolechoice.display.value = caller;
                   1329:     } else {
                   1330:         if ((caller == 'doupdate') || (caller == 'requestauthor') ||
                   1331:             (caller == 'queued')) { 
                   1332:             document.rolechoice.state.value = caller;
                   1333:         }
                   1334:     }
                   1335:     document.rolechoice.selectrole.value='';
                   1336:     document.rolechoice.submit();
1.270     raeburn  1337: }
                   1338: 
1.225     bisitz   1339: // ]]>
1.26      www      1340: </script>
1.10      www      1341: ENDHEADER
1.6       www      1342: 
1.2       www      1343: # ------------------------------------------ Get Error Message from Environment
                   1344: 
1.118     albertel 1345:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
                   1346:     if ($env{'user.error.msg'}) {
1.55      albertel 1347: 	$r->log_reason(
1.118     albertel 1348:    "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12      www      1349:     }
1.1       harris41 1350: 
1.61      www      1351: # ------------------------------------------------- Can this user re-init, etc?
1.6       www      1352: 
1.118     albertel 1353:     my $advanced=$env{'user.adv'};
1.61      www      1354:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118     albertel 1355:     my $tryagain=$env{'form.tryagain'};
1.209     raeburn  1356:     my $reinit=$env{'user.reinit'};
                   1357:     delete $env{'user.reinit'};
1.6       www      1358: 
1.2       www      1359: # -------------------------------------------------------- Generate Page Output
1.6       www      1360: # --------------------------------------------------------------- Error Header?
1.2       www      1361:     if ($error) {
1.187     bisitz   1362:         $r->print("<h1>".&mt('LON-CAPA Access Control')."</h1>");
1.174     albertel 1363: 	$r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>");
                   1364: 	if ($priv ne '') {
1.187     bisitz   1365:             $r->print(&mt('Access  : ').&Apache::lonnet::plaintext($priv)."\n");
1.174     albertel 1366: 	}
                   1367: 	if ($fn ne '') {
1.187     bisitz   1368:             $r->print(&mt('Resource: ').&Apache::lonenc::check_encrypt($fn)."\n");
1.174     albertel 1369: 	}
                   1370: 	if ($msg ne '') {
1.187     bisitz   1371:             $r->print(&mt('Action  : ').$msg."\n");
1.174     albertel 1372: 	}
                   1373: 	$r->print("</pre><hr />");
1.120     albertel 1374: 	my $url=$fn;
                   1375: 	my $last;
                   1376: 	if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                   1377: 		&GDBM_READER(),0640)) {
                   1378: 	    $last=$hash{'last_known'};
                   1379: 	    untie(%hash);
                   1380: 	}
1.149     www      1381: 	if ($last) { $fn.='?symb='.&escape($last); }
1.120     albertel 1382: 
                   1383: 	&Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
                   1384: 					&Apache::lonenc::check_encrypt($fn));
1.2       www      1385:     } else {
1.118     albertel 1386:         if ($env{'user.error.msg'}) {
1.209     raeburn  1387:             if ($reinit) {
                   1388:                 $r->print(
                   1389:  '<h3><span class="LC_error">'.
1.234     raeburn  1390:  &mt('As your session file for the course or community has expired, you will need to re-select it.').'</span></h3>');
1.209     raeburn  1391:             } else {
                   1392: 	        $r->print(
1.157     albertel 1393:  '<h3><span class="LC_error">'.
1.235     bisitz   1394:  &mt('You need to choose another user role or enter a specific course or community for this function.').
                   1395:  '</span></h3>');
1.209     raeburn  1396: 	    }
                   1397:         }
1.2       www      1398:     }
                   1399:     if ($nochoose) {
1.376     raeburn  1400: 	$r->print("<div class=\"LC_landmark\" role=\"main\">
                   1401:                   <h2>".&mt('Sorry ...')."</h2>\n<span class='LC_error'>".
1.355     raeburn  1402: 		  &mt('This action is currently not authorized.').'</span>');
                   1403:         if ($error && $norolelist) {
1.376     raeburn  1404:             $r->print('<br /><br /><h3 class="LC_heading_3"><span class="LC_error">'.
1.355     raeburn  1405:                       &mt('As your session was launched from a web page external to LON-CAPA some course content may be unavailable, including the resource you were trying to access.').
1.376     raeburn  1406:                      '</span></h3>'.
                   1407:                      '<h3 class="LC_heading_3"><span class="LC_error">'.
1.355     raeburn  1408:                      &mt('You may need to login to LON-CAPA directly, or re-launch from a different external system.').
1.376     raeburn  1409:                      '</span></h3>');
1.355     raeburn  1410:         }
1.376     raeburn  1411:         $r->print('</div>'.&Apache::loncommon::end_page());
1.150     www      1412: 	return OK;
1.6       www      1413:     } else {
1.274     raeburn  1414:         if ($updateresult || $reqauthor || $hotlist) {
                   1415:             my $showresult = '<div>';
                   1416:             if ($updateresult) {
                   1417:                 $showresult .= &Apache::lonhtmlcommon::confirm_success($updateresult);
                   1418:             }
                   1419:             if ($reqauthor) {
                   1420:                 $showresult .= &Apache::lonhtmlcommon::confirm_success($reqauthor);
                   1421:             }
                   1422:             if ($hotlist) {
                   1423:                 $showresult .= $hotlist;
1.370     raeburn  1424:             }
1.274     raeburn  1425:             $showresult .= '</div>';
                   1426:             $r->print($showresult);
                   1427:         } elsif ($env{'form.state'} eq 'queued') {
                   1428:             $r->print(&get_queued());
1.370     raeburn  1429:         } elsif ($env{'form.approvals'} eq 'show') {
                   1430:             if ($env{'form.state'} eq 'done') {
                   1431:                 $r->print($approvalresult).'<br />';
                   1432:             }
                   1433:             $r->print('<div class="LC_left_float"><fieldset><legend>'.&mt('Role assignments queued pending your acceptance').'</legend>'.
                   1434:                      &get_approvals().
                   1435:                      '</fieldset></div><br clear="all" />');
1.270     raeburn  1436:         }
1.18      www      1437:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                   1438:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www      1439:         }
1.274     raeburn  1440:         my $display = ($env{'form.display'} =~ /^(showall)$/);
1.370     raeburn  1441:         my $approvals = ($env{'form.approvals'} =~ /^(show)$/);
1.84      www      1442:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116     albertel 1443:         $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
                   1444:         $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134     www      1445:         $r->print('<input type="hidden" name="newrole" value="" />');
1.274     raeburn  1446:         $r->print('<input type="hidden" name="display" value="'.$display.'" />');
                   1447:         $r->print('<input type="hidden" name="state" value="" />');
1.370     raeburn  1448:         $r->print('<input type="hidden" name="approvals" value="'.$approvals.'" />');
1.357     raeburn  1449:         if ($blocked_by_ip) {
                   1450:             my $blocked_role = 'student';
                   1451:             if ($blocked_type eq 'Community') {
                   1452:                 $blocked_role = 'member';
                   1453:             }
                   1454:             $r->print('<h3><span class="LC_error">'.
                   1455:                       &mt('The [_1] you selected is not available for access with a [_2] role from your current IP address: [_3].',
1.359     raeburn  1456:                           lc($blocked_type),$blocked_role,$clientip).
1.357     raeburn  1457:                       '</span></h3>');
                   1458:         }
1.6       www      1459:     }
1.259     raeburn  1460:     $r->rflush();
1.226     raeburn  1461: 
                   1462:     my (%roletext,%sortrole,%roleclass,%futureroles,%timezones);
                   1463:     my ($countactive,$countfuture,$inrole,$possiblerole) = 
1.274     raeburn  1464:         &gather_roles($update,$refresh,$now,$reinit,$nochoose,\%roles_in_env,\%roletext,
                   1465:                       \%sortrole,\%roleclass,\%futureroles,\%timezones,$loncaparev);
1.226     raeburn  1466:     $refresh = $now;
                   1467:     &Apache::lonnet::appenv({'user.refresh.time'  => $refresh});
1.313     raeburn  1468:     if ($countactive == 1) {
                   1469:         if ($env{'request.course.id'}) {
                   1470:             if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
                   1471:                 $placementonly = 1;
                   1472:             }
                   1473:         } elsif ($possiblerole) {
                   1474:             if ($possiblerole =~ m{^st\./($match_domain)/($match_courseid)(?:/|$)}) {
                   1475:                 if ($env{'course.'.$1.'_'.$2.'.type'} eq 'Placement') {
                   1476:                     $placementonly = 1;
                   1477:                 }
                   1478:             }
                   1479:         }
                   1480:     }
                   1481:     if ((($cattype eq 'std') || ($cattype eq 'domonly')) && (!$env{'user.adv'}) &&
                   1482:           (!$placementonly)) {
1.196     raeburn  1483:         if ($countactive > 0) {
                   1484:             my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.201     raeburn  1485:             my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); 
1.233     bisitz   1486:             $r->print(
                   1487:                 '<p>'
1.295     bisitz   1488:                .&mt('[_1]Visit the [_2]Course/Community Catalog[_3][_4]'
                   1489:                    .' to view all [_5] LON-CAPA courses and communities.'
1.233     bisitz   1490:                    ,'<b>'
                   1491:                    ,'<a href="/adm/coursecatalog?showdom='.$esc_dom.'">'
1.295     bisitz   1492:                    ,'</a>'
                   1493:                    ,'</b>'
                   1494:                    ,'"'.$domdesc.'"')
1.233     bisitz   1495:                .'<br />'
1.235     bisitz   1496:                .&mt('If a course or community is [_1]not[_2] in your list of current courses and communities below,'
1.233     bisitz   1497:                    .' you may be able to enroll if self-enrollment is permitted.'
                   1498:                    ,'<b>','</b>')
                   1499:                .'</p>'
                   1500:             );
1.196     raeburn  1501:         }
                   1502:     }
                   1503: 
1.352     raeburn  1504:     if ($norolelist) {
                   1505:         if ($env{'request.role'}) {
                   1506:             my ($roletext,$role_text_end) = &display_curr_role($env{'request.role'});
                   1507:             if ($roletext) {
                   1508:                 $r->print(&Apache::loncommon::start_data_table('LC_textsize_mobile').
                   1509:                           &Apache::loncommon::start_data_table_row().
                   1510:                           $roletext.
                   1511:                           &Apache::loncommon::end_data_table_row());
                   1512:                 if ($role_text_end) {
                   1513:                     $r->print(&Apache::loncommon::continue_data_table_row().
                   1514:                               $role_text_end.
                   1515:                               &Apache::loncommon::end_data_table_row());
                   1516:                 }
                   1517:                 $r->print(&Apache::loncommon::end_data_table());
                   1518:             }
                   1519:         }
                   1520:         $r->print(&Apache::loncommon::end_page());
                   1521:         return OK;
                   1522:     }
                   1523: 
1.84      www      1524: # No active roles
                   1525:     if ($countactive==0) {
1.343     raeburn  1526:         my $elapsed = 0;
                   1527:         if ($now && $update) {
                   1528:             $elapsed = $now - $update;
                   1529:         }
                   1530:         &requestcourse_advice($r,$cattype,$inrole,$elapsed); 
1.191     raeburn  1531: 	$r->print('</form>');
                   1532:         if ($countfuture) {
                   1533:             $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
                   1534:             my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
                   1535:                                                $nochoose);
                   1536:             &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
1.323     raeburn  1537:                             \%roletext,$update,$then);
1.191     raeburn  1538:             my $tremark='';
1.212     bisitz   1539:             my $tbg;
1.191     raeburn  1540:             if ($env{'request.role'} eq 'cm') {
1.212     bisitz   1541:                 $tbg="LC_roles_selected";
1.204     bisitz   1542:                 $tremark=&mt('Currently selected.').' ';
1.191     raeburn  1543:             } else {
1.212     bisitz   1544:                 $tbg="LC_roles_is";
1.191     raeburn  1545:             }
1.212     bisitz   1546:             $r->print(&Apache::loncommon::start_data_table_row()
                   1547:                      .'<td class="'.$tbg.'">&nbsp;</td>'
                   1548:                      .'<td colspan="3">'
                   1549:                      .&mt('No role specified')
                   1550:                      .'</td>'
                   1551:                      .'<td>'.$tremark.'&nbsp;</td>'
                   1552:                      .&Apache::loncommon::end_data_table_row()
                   1553:             );
1.191     raeburn  1554: 
1.212     bisitz   1555:             $r->print(&Apache::loncommon::end_data_table());
1.191     raeburn  1556:         }
                   1557:         $r->print(&Apache::loncommon::end_page());
1.84      www      1558: 	return OK;
1.313     raeburn  1559:     } elsif (($placementonly) && ($env{'request.role'} eq 'cm')) {
                   1560: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>
                   1561: 	          <input type="hidden" name="'.$possiblerole.'" value="1" />
                   1562:                   <noscript><br />
                   1563:                   <input type="submit" name="submit" value="'.&mt('Continue').'" />
                   1564:                   </noscript></form>');
                   1565: 	$r->rflush();
                   1566: 	$r->print('<script type="text/javascript">document.forms.rolechoice.submit();</script>');
                   1567: 	$r->print(&Apache::loncommon::end_page());
                   1568: 	return OK;
1.84      www      1569:     }
                   1570: # ----------------------------------------------------------------------- Table
1.247     raeburn  1571: 
1.325     raeburn  1572:     if (($numdc > 0) || (($numhelpdesk > 0) && ($numadhoc > 0))) {
1.329     raeburn  1573:         $r->print(&coursepick_jscript().
                   1574:                   &Apache::loncommon::coursebrowser_javascript());
                   1575:     }
                   1576:     if ($numdc > 0) {
                   1577:         $r->print(&Apache::loncommon::authorbrowser_javascript());
1.247     raeburn  1578:     }
                   1579: 
1.224     raeburn  1580:     unless ((!&Apache::loncommon::show_course()) || ($nochoose) || ($countactive==1)) {
1.173     albertel 1581: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
1.84      www      1582:     }
1.229     raeburn  1583:     if ($env{'form.destinationurl'}) {
                   1584:         $r->print('<input type="hidden" name="destinationurl" value="'.
                   1585:                   $env{'form.destinationurl'}.'" />');
                   1586:         if ($env{'form.destsymb'} ne '') {
                   1587:             $r->print('<input type="hidden" name="destsymb" value="'.
                   1588:                       $env{'form.destsymb'}.'" />');
                   1589:         }
                   1590:     }
1.247     raeburn  1591: 
1.191     raeburn  1592:     my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,$nochoose);
1.118     albertel 1593:     if ($env{'environment.recentroles'}) {
1.111     albertel 1594:         my %recent_roles =
1.118     albertel 1595:                &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111     albertel 1596: 	my $output='';
1.247     raeburn  1597: 	foreach my $role (sort(keys(%recent_roles))) {
                   1598: 	    if (ref($roletext{'user.role.'.$role}) eq 'ARRAY') {
1.223     raeburn  1599: 		$output.= &Apache::loncommon::start_data_table_row().
1.247     raeburn  1600:                           $roletext{'user.role.'.$role}->[0].
1.223     raeburn  1601:                           &Apache::loncommon::end_data_table_row();
1.249     raeburn  1602:                 if ($roletext{'user.role.'.$role}->[1] ne '') {
                   1603:                     $output .= &Apache::loncommon::continue_data_table_row().
                   1604:                                $roletext{'user.role.'.$role}->[1].
                   1605:                                &Apache::loncommon::end_data_table_row();
                   1606:                 }
1.318     raeburn  1607:                 if ($role =~ m{^dc\./($match_domain)/$} 
1.170     albertel 1608: 		    && $dcroles{$1}) {
1.192     raeburn  1609: 		    $output .= &adhoc_roles_row($1,'recent');
1.325     raeburn  1610:                 } elsif ($role =~ m{^(dh|da)\./($match_domain)/$}) {
1.323     raeburn  1611:                     $output .= &adhoc_customroles_row($1,$2,'recent',$update,$then);
1.133     albertel 1612:                 }
1.113     raeburn  1613: 	    } elsif ($numdc > 0) {
1.247     raeburn  1614:                 unless ($role =~/^error\:/) {
1.249     raeburn  1615:                     my ($roletext,$role_text_end) = &display_cc_role('user.role.'.$role);
1.259     raeburn  1616:                     if ($roletext) {
                   1617:                         $output.= &Apache::loncommon::start_data_table_row().
                   1618:                                   $roletext.
                   1619:                                   &Apache::loncommon::end_data_table_row();
                   1620:                         if ($role_text_end) {
                   1621:                             $output .= &Apache::loncommon::continue_data_table_row().
                   1622:                                        $role_text_end.
                   1623:                                        &Apache::loncommon::end_data_table_row();
                   1624:                         }
                   1625:                     }
1.113     raeburn  1626:                 }
1.247     raeburn  1627:             }
1.111     albertel 1628: 	}
                   1629: 	if ($output) {
1.212     bisitz   1630: 	    $r->print(&Apache::loncommon::start_data_table_empty_row()
                   1631:                      .'<td align="center" colspan="5">'
1.224     raeburn  1632:                      .$recent
1.212     bisitz   1633:                      .'</td>'
                   1634:                      .&Apache::loncommon::end_data_table_empty_row()
                   1635:             );
1.111     albertel 1636: 	    $r->print($output);
1.114     raeburn  1637:             $doheaders ++;
1.111     albertel 1638: 	}
                   1639:     }
1.323     raeburn  1640:     &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext,$update,$then);
1.202     raeburn  1641:     if ($countactive > 1) {
                   1642:         my $tremark='';
1.212     bisitz   1643:         my $tbg;
1.202     raeburn  1644:         if ($env{'request.role'} eq 'cm') {
1.212     bisitz   1645:             $tbg="LC_roles_selected";
1.204     bisitz   1646:             $tremark=&mt('Currently selected.').' ';
1.202     raeburn  1647:         } else {
1.212     bisitz   1648:                 $tbg="LC_roles_is";
1.202     raeburn  1649:         }
1.212     bisitz   1650:         $r->print(&Apache::loncommon::start_data_table_row());
1.202     raeburn  1651:         unless ($nochoose) {
                   1652: 	    if ($env{'request.role'} ne 'cm') {
1.212     bisitz   1653: 	        $r->print('<td class="'.$tbg.'"><input type="submit" value="'.
1.202     raeburn  1654: 		          &mt('Select').'" name="cm" /></td>');
                   1655: 	    } else {
1.212     bisitz   1656: 	        $r->print('<td class="'.$tbg.'">&nbsp;</td>');
1.202     raeburn  1657: 	    }
                   1658:         }
1.212     bisitz   1659:         $r->print('<td colspan="3">'
                   1660:                  .&mt('No role specified')
                   1661:                  .'</td>'
                   1662:                  .'<td>'.$tremark.'&nbsp;</td>'
                   1663:                  .&Apache::loncommon::end_data_table_row()
                   1664:         );
1.202     raeburn  1665:     } 
1.212     bisitz   1666:     $r->print(&Apache::loncommon::end_data_table());
1.4       www      1667:     unless ($nochoose) {
                   1668: 	$r->print("</form>\n");
                   1669:     }
1.22      harris41 1670: # ------------------------------------------------------------ Privileges Info
1.118     albertel 1671:     if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.212     bisitz   1672: 	$r->print('<hr /><h2>'.&mt('Current Privileges').'</h2>');
1.175     albertel 1673: 	$r->print(&privileges_info());
1.4       www      1674:     }
1.267     bisitz   1675:     my $announcements = &Apache::lonnet::getannounce();
                   1676:     $r->print(
                   1677:         '<br />'.
                   1678:         '<h2>'.&mt('Announcements').'</h2>'.
                   1679:         $announcements
                   1680:     ) unless (!$announcements);
1.65      www      1681:     if ($advanced) {
1.201     raeburn  1682:         my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.231     bisitz   1683:         $r->print('<p><small><i>'
                   1684:                  .&mt('This LON-CAPA server is version [_1]',$r->dir_config('lonVersion'))
1.308     raeburn  1685:                  .'</i></small></p>');
1.65      www      1686:     }
1.147     albertel 1687:     $r->print(&Apache::loncommon::end_page());
1.1       harris41 1688:     return OK;
1.102     raeburn  1689: }
                   1690: 
1.274     raeburn  1691: sub roles_from_env {
                   1692:     my ($roleshash,$update) = @_;
                   1693:     my $count = 0;
                   1694:     if (ref($roleshash) eq 'HASH') {
                   1695:         foreach my $envkey (keys(%env)) {
                   1696:             if ($envkey =~ m{^user\.role\.(\w+)[./]}) {
                   1697:                 next if ($1 eq 'gr');
                   1698:                 $roleshash->{$envkey} = $env{$envkey};
                   1699:                 my ($start,$end) = split(/\./,$env{$envkey});
                   1700:                 unless ($end && $end<$update) {
                   1701:                     $count ++;
                   1702:                 }
                   1703:             }
                   1704:         }
                   1705:     }
                   1706:     return $count;
                   1707: }
                   1708: 
1.226     raeburn  1709: sub gather_roles {
1.274     raeburn  1710:     my ($update,$refresh,$now,$reinit,$nochoose,$roles_in_env,$roletext,$sortrole,$roleclass,$futureroles,
                   1711:         $timezones,$loncaparev) = @_;
1.226     raeburn  1712:     my ($countactive,$countfuture,$inrole,$possiblerole) = (0,0,0,'');
                   1713:     my $advanced = $env{'user.adv'};
                   1714:     my $tryagain = $env{'form.tryagain'};
1.254     raeburn  1715:     my @ids = &Apache::lonnet::current_machine_ids();
1.333     raeburn  1716:     my (%willtrust,%trustchecked);
1.274     raeburn  1717:     if (ref($roles_in_env) eq 'HASH') {
1.323     raeburn  1718:         my %adhocdesc;
1.274     raeburn  1719:         foreach my $envkey (sort(keys(%{$roles_in_env}))) {
                   1720:             my $button = 1;
                   1721:             my $switchserver='';
                   1722:             my $switchwarning;
                   1723:             my ($role_text,$role_text_end,$sortkey,$role,$where,$trolecode,$tstart,
                   1724:                 $tend,$tremark,$tstatus,$tpstart,$tpend);
1.260     raeburn  1725:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.226     raeburn  1726:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
                   1727:             next if (!defined($role) || $role eq '' || $role =~ /^gr/);
                   1728:             $tremark='';
                   1729:             $tpstart='&nbsp;';
                   1730:             $tpend='&nbsp;';
                   1731:             if ($env{'request.role'} eq $trolecode) {
                   1732:                 $tstatus='selected';
                   1733:             }
                   1734:             my $tbg;
                   1735:             if (($tstatus eq 'is')
                   1736:                 || ($tstatus eq 'selected')
                   1737:                 || ($tstatus eq 'future')
1.274     raeburn  1738:                 || ($env{'form.display'} eq 'showall')) {
1.259     raeburn  1739:                 my $timezone = &role_timezone($where,$timezones);
                   1740:                 if ($tstart) {
                   1741:                     $tpstart=&Apache::lonlocal::locallocaltime($tstart,$timezone);
                   1742:                 }
                   1743:                 if ($tend) {
                   1744:                     $tpend=&Apache::lonlocal::locallocaltime($tend,$timezone);
                   1745:                 }
1.226     raeburn  1746:                 if ($tstatus eq 'is') {
                   1747:                     $tbg='LC_roles_is';
                   1748:                     $possiblerole=$trolecode;
                   1749:                     $countactive++;
                   1750:                 } elsif ($tstatus eq 'future') {
                   1751:                     $tbg='LC_roles_future';
                   1752:                     $button=0;
                   1753:                     $futureroles->{$trolecode} = $tstart.':'.$tend;
                   1754:                     $countfuture ++;
                   1755:                 } elsif ($tstatus eq 'expired') {
                   1756:                     $tbg='LC_roles_expired';
                   1757:                     $button=0;
                   1758:                 } elsif ($tstatus eq 'will_not') {
                   1759:                     $tbg='LC_roles_will_not';
                   1760:                     $tremark.=&mt('Expired after logout.').' ';
                   1761:                 } elsif ($tstatus eq 'selected') {
                   1762:                     $tbg='LC_roles_selected';
                   1763:                     $inrole=1;
                   1764:                     $countactive++;
                   1765:                     $tremark.=&mt('Currently selected.').' ';
                   1766:                 }
                   1767:                 my $trole;
                   1768:                 if ($role =~ /^cr\//) {
                   1769:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.322     raeburn  1770:                     unless ($rauthor eq $rdomain.'-domainconfig') {
                   1771:                         if ($tremark) { $tremark.='<br />'; }
                   1772:                         $tremark.=&mt('Custom role defined by [_1].',$rauthor.':'.$rdomain);
                   1773:                     }
1.226     raeburn  1774:                 }
                   1775:                 $trole=Apache::lonnet::plaintext($role);
                   1776:                 my $ttype;
                   1777:                 my $twhere;
1.313     raeburn  1778:                 my $skipcal;
1.226     raeburn  1779:                 my ($tdom,$trest,$tsection)=
                   1780:                     split(/\//,Apache::lonnet::declutter($where));
                   1781:                 # First, Co-Authorship roles
                   1782:                 if (($role eq 'ca') || ($role eq 'aa')) {
                   1783:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
                   1784:                     my $allowed=0;
1.333     raeburn  1785:                     my $prohibited;
1.226     raeburn  1786:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   1787:                     if (!$allowed) {
                   1788:                         $button=0;
1.333     raeburn  1789:                         unless ($trustchecked{$tdom}) {
1.340     raeburn  1790:                             if ((&Apache::lonnet::will_trust('othcoau',$env{'user.domain'},$tdom)) &&
1.341     raeburn  1791:                                 (&Apache::lonnet::will_trust('coaurem',$tdom,$env{'user.domain'}))) {
1.333     raeburn  1792:                                 $willtrust{$tdom} = 1;
                   1793:                                 $trustchecked{$tdom} = 1;
                   1794:                             }
                   1795:                         } 
                   1796:                         if ($willtrust{$tdom}) {
                   1797:                             $switchserver='otherserver='.$home.'&amp;role='.$trolecode;
                   1798:                         } else {
                   1799:                             $prohibited = 1;
                   1800:                             $tremark .= &mt('Session switch required but prohibited.');
                   1801:                         }
1.226     raeburn  1802:                     }
                   1803:                     #next if ($home eq 'no_host');
                   1804:                     $home = &Apache::lonnet::hostname($home);
1.288     raeburn  1805:                     $ttype='Authoring Space';
1.226     raeburn  1806:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                   1807:                         ': '.$tdom.'<br />'.
                   1808:                         ' '.&mt('Server').':&nbsp;'.$home;
                   1809:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.333     raeburn  1810:                     unless ($prohibited) {
                   1811:                         $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
                   1812:                     }
1.226     raeburn  1813:                     $sortkey=$role."$trest:$tdom";
                   1814:                 } elsif ($role eq 'au') {
                   1815:                     # Authors
                   1816:                     my $home = &Apache::lonnet::homeserver
                   1817:                         ($env{'user.name'},$env{'user.domain'});
                   1818:                     my $allowed=0;
                   1819:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   1820:                     if (!$allowed) {
                   1821:                         $button=0;
1.248     raeburn  1822:                         $switchserver='otherserver='.$home.'&amp;role='.$trolecode;
1.226     raeburn  1823:                     }
                   1824:                     #next if ($home eq 'no_host');
                   1825:                     $home = &Apache::lonnet::hostname($home);
1.288     raeburn  1826:                     $ttype='Authoring Space';
1.226     raeburn  1827:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                   1828:                         ':&nbsp;'.$home;
                   1829:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                   1830:                     $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
                   1831:                     $sortkey=$role;
                   1832:                 } elsif ($trest) {
                   1833:                     my $tcourseid=$tdom.'_'.$trest;
                   1834:                     $ttype = &Apache::loncommon::course_type($tcourseid);
1.322     raeburn  1835:                     if ($role !~ /^cr/) {
                   1836:                         $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1.323     raeburn  1837:                     } elsif ($role =~ m{^\Qcr/$tdom/$tdom\E\-domainconfig/(\w+)$}) {
                   1838:                         my $rolename = $1;
                   1839:                         my $desc;
                   1840:                         if (ref($adhocdesc{$tdom}) eq 'HASH') {
                   1841:                             $desc = $adhocdesc{$tdom}{$rolename};
                   1842:                         } else {
                   1843:                             my %domdef = &Apache::lonnet::get_domain_defaults($tdom);
                   1844:                             if (ref($domdef{'adhocroles'}) eq 'HASH') {
                   1845:                                 foreach my $rolename (sort(keys(%{$domdef{'adhocroles'}}))) {
                   1846:                                     if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                   1847:                                         $adhocdesc{$tdom}{$rolename} = $domdef{'adhocroles'}{$rolename}{'desc'};
                   1848:                                     }
                   1849:                                 }
1.371     raeburn  1850:                                 $desc = $adhocdesc{$tdom}{$rolename};
1.323     raeburn  1851:                             }
                   1852:                         }
                   1853:                         if ($desc ne '') {
                   1854:                             $trole = $desc;
                   1855:                         } else {
                   1856:                             $trole = &mt('Helpdesk[_1]','&nbsp;'.$rolename);
                   1857:                         }
1.322     raeburn  1858:                     } else {
                   1859:                         $trole = (split(/\//,$role,4))[-1];
                   1860:                     }
1.226     raeburn  1861:                     if ($env{'course.'.$tcourseid.'.description'}) {
1.254     raeburn  1862:                         my $home=$env{'course.'.$tcourseid.'.home'};
1.226     raeburn  1863:                         $twhere=$env{'course.'.$tcourseid.'.description'};
                   1864:                         $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.248     raeburn  1865:                         $twhere = &HTML::Entities::encode($twhere,'"<>&');
1.226     raeburn  1866:                         unless ($twhere eq &mt('Currently not available')) {
                   1867:                             $twhere.=' <span class="LC_fontsize_small">'.
                   1868:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
                   1869:                                     '</span>';
1.254     raeburn  1870:                             unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1.255     raeburn  1871:                                 my $required = $env{'course.'.$tcourseid.'.internal.releaserequired'};
1.259     raeburn  1872:                                 if ($required ne '') {
                   1873:                                     ($switchserver,$switchwarning) = 
1.310     raeburn  1874:                                         &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1.259     raeburn  1875:                                     if ($switchserver || $switchwarning) {
                   1876:                                         $button = 0;
                   1877:                                     }
1.254     raeburn  1878:                                 }
                   1879:                             }
1.226     raeburn  1880:                         }
                   1881:                     } else {
                   1882:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   1883:                         if (%newhash) {
                   1884:                             $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
                   1885:                                 "\0".$envkey;
1.248     raeburn  1886:                             $twhere=&HTML::Entities::encode($newhash{'description'},'"<>&').
                   1887:                                     ' <span class="LC_fontsize_small">'.
                   1888:                                      &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
                   1889:                                     '</span>';
1.226     raeburn  1890:                             $ttype = $newhash{'type'};
1.243     raeburn  1891:                             $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1.254     raeburn  1892:                             my $home = $newhash{'home'};
                   1893:                             unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1.255     raeburn  1894:                                 my $required = $newhash{'internal.releaserequired'};
1.259     raeburn  1895:                                 if ($required ne '') {
                   1896:                                     ($switchserver,$switchwarning) =
1.310     raeburn  1897:                                         &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1.259     raeburn  1898:                                     if ($switchserver || $switchwarning) {
                   1899:                                         $button = 0;
                   1900:                                     }
1.254     raeburn  1901:                                 }
                   1902:                             }
1.226     raeburn  1903:                         } else {
                   1904:                             $twhere=&mt('Currently not available');
                   1905:                             $env{'course.'.$tcourseid.'.description'}=$twhere;
                   1906:                             $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
                   1907:                             $ttype = 'Unavailable';
1.313     raeburn  1908:                             $skipcal = 1;
1.226     raeburn  1909:                         }
                   1910:                     }
1.313     raeburn  1911:                     if ($ttype eq 'Placement') {
                   1912:                         $ttype = 'Placement Test';
                   1913:                     }
1.226     raeburn  1914:                     if ($tsection) {
                   1915:                         $twhere.='<br />'.&mt('Section').': '.$tsection;
                   1916:                     }
                   1917:                     if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
                   1918:                 } elsif ($tdom) {
                   1919:                     $ttype='Domain';
                   1920:                     $twhere=$tdom;
                   1921:                     $sortkey=$role.$twhere;
                   1922:                 } else {
                   1923:                     $ttype='System';
                   1924:                     $twhere=&mt('system wide');
                   1925:                     $sortkey=$role.$twhere;
                   1926:                 }
                   1927:                 ($role_text,$role_text_end) =
                   1928:                     &build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,
                   1929:                                     $advanced,$tremark,$tbg,$trole,$twhere,$tpstart,
1.313     raeburn  1930:                                     $tpend,$nochoose,$button,$switchserver,$reinit,
                   1931:                                     $switchwarning,$skipcal);
1.226     raeburn  1932:                 $roletext->{$envkey}=[$role_text,$role_text_end];
                   1933:                 if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                   1934:                 $sortrole->{$sortkey}=$envkey;
                   1935:                 $roleclass->{$envkey}=$ttype;
                   1936:             }
                   1937:         }
                   1938:     }
                   1939:     return ($countactive,$countfuture,$inrole,$possiblerole);
                   1940: }
                   1941: 
1.215     raeburn  1942: sub role_timezone {
                   1943:     my ($where,$timezones) = @_;
                   1944:     my $timezone;
                   1945:     if (ref($timezones) eq 'HASH') { 
                   1946:         if ($where =~ m{^/($match_domain)/($match_courseid)}) {
                   1947:             my $cdom = $1;
                   1948:             my $cnum = $2;
                   1949:             if ($cdom && $cnum) {
                   1950:                 if (!exists($timezones->{$cdom.'_'.$cnum})) {
1.259     raeburn  1951:                     my $tz;
                   1952:                     if ($env{'course.'.$cdom.'_'.$cnum.'.description'}) {
                   1953:                         $tz = $env{'course.'.$cdom.'_'.$cnum.'.timezone'};
                   1954:                     } else {
                   1955:                         my %timehash =
                   1956:                             &Apache::lonnet::get('environment',['timezone'],$cdom,$cnum);
                   1957:                         $tz = $timehash{'timezone'};
                   1958:                     }
                   1959:                     if ($tz eq '') {
1.215     raeburn  1960:                         if (!exists($timezones->{$cdom})) {
                   1961:                             my %domdefaults = 
                   1962:                                 &Apache::lonnet::get_domain_defaults($cdom);
                   1963:                             if ($domdefaults{'timezone_def'} eq '') {
                   1964:                                 $timezones->{$cdom} = 'local';
                   1965:                             } else {
                   1966:                                 $timezones->{$cdom} = $domdefaults{'timezone_def'};
                   1967:                             }
                   1968:                         }
                   1969:                         $timezones->{$cdom.'_'.$cnum} = $timezones->{$cdom};
                   1970:                     } else {
                   1971:                         $timezones->{$cdom.'_'.$cnum} = 
1.259     raeburn  1972:                             &Apache::lonlocal::gettimezone($tz);
1.215     raeburn  1973:                     }
                   1974:                 }
                   1975:                 $timezone = $timezones->{$cdom.'_'.$cnum};
                   1976:             }
                   1977:         } else {
                   1978:             my ($tdom) = ($where =~ m{^/($match_domain)});
                   1979:             if ($tdom) {
                   1980:                 if (!exists($timezones->{$tdom})) {
                   1981:                     my %domdefaults = &Apache::lonnet::get_domain_defaults($tdom);
                   1982:                     if ($domdefaults{'timezone_def'} eq '') {
                   1983:                         $timezones->{$tdom} = 'local';
                   1984:                     } else {
                   1985:                         $timezones->{$tdom} = $domdefaults{'timezone_def'};
                   1986:                     }
                   1987:                 }
                   1988:                 $timezone = $timezones->{$tdom};
                   1989:             }
                   1990:         }
                   1991:         if ($timezone eq 'local') {
                   1992:             $timezone = undef;
                   1993:         }
                   1994:     }
                   1995:     return $timezone;
                   1996: }
                   1997: 
1.191     raeburn  1998: sub roletable_headers {
                   1999:     my ($r,$roleclass,$sortrole,$nochoose) = @_;
                   2000:     my $doheaders;
                   2001:     if ((ref($sortrole) eq 'HASH') && (ref($roleclass) eq 'HASH')) {
1.212     bisitz   2002:         $r->print('<br />'
1.314     raeburn  2003:                  .&Apache::loncommon::start_data_table('LC_textsize_mobile')
1.212     bisitz   2004:                  .&Apache::loncommon::start_data_table_header_row()
                   2005:         );
1.376     raeburn  2006:         if (!$nochoose) { $r->print('<th><span class="LC_visually_hidden">'.&mt('Action').'</span></th>'); }
1.212     bisitz   2007:         $r->print('<th>'.&mt('User Role').'</th>'
                   2008:                  .'<th>'.&mt('Extent').'</th>'
                   2009:                  .'<th>'.&mt('Start').'</th>'
                   2010:                  .'<th>'.&mt('End').'</th>'
                   2011:                  .&Apache::loncommon::end_data_table_header_row()
                   2012:         );
1.191     raeburn  2013:         $doheaders=-1;
                   2014:         my @roletypes = &roletypes();
                   2015:         foreach my $type (@roletypes) {
                   2016:             my $haverole=0;
                   2017:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                   2018:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                   2019:                     $haverole=1;
                   2020:                 }
                   2021:             }
                   2022:             if ($haverole) { $doheaders++; }
                   2023:         }
                   2024:     }
                   2025:     return $doheaders;
                   2026: }
                   2027: 
                   2028: sub roletypes {
1.313     raeburn  2029:     my @types = ('Domain','Authoring Space','Course','Placement Test','Community','Unavailable','System');
1.191     raeburn  2030:     return @types; 
                   2031: }
                   2032: 
                   2033: sub print_rolerows {
1.323     raeburn  2034:     my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext,$update,$then) = @_;
1.191     raeburn  2035:     if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
                   2036:         my @types = &roletypes();
                   2037:         foreach my $type (@types) {
                   2038:             my $output;
                   2039:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                   2040:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                   2041:                     if (ref($roletext) eq 'HASH') {
1.223     raeburn  2042:                         if (ref($roletext->{$sortrole->{$which}}) eq 'ARRAY') {
                   2043:                             $output.= &Apache::loncommon::start_data_table_row().
                   2044:                                       $roletext->{$sortrole->{$which}}->[0].
                   2045:                                       &Apache::loncommon::end_data_table_row();
1.251     raeburn  2046:                             if ($roletext->{$sortrole->{$which}}->[1] ne '') {
                   2047:                                 $output .= &Apache::loncommon::continue_data_table_row().
                   2048:                                            $roletext->{$sortrole->{$which}}->[1].
                   2049:                                            &Apache::loncommon::end_data_table_row();
                   2050:                             }
1.223     raeburn  2051:                         }
1.317     raeburn  2052:                         if ($sortrole->{$which} =~ m{^user\.role\.dc\./($match_domain)/}) {
1.191     raeburn  2053:                             if (ref($dcroles) eq 'HASH') {
                   2054:                                 if ($dcroles->{$1}) {
1.192     raeburn  2055:                                     $output .= &adhoc_roles_row($1,'');
1.191     raeburn  2056:                                 }
                   2057:                             }
1.325     raeburn  2058:                         } elsif ($sortrole->{$which} =~ m{^user\.role\.(dh|da)\./($match_domain)/}) {
1.323     raeburn  2059:                             $output .= &adhoc_customroles_row($1,$2,'',$update,$then);
1.191     raeburn  2060:                         }
                   2061:                     }
                   2062:                 }
                   2063:             }
                   2064:             if ($output) {
                   2065:                 if ($doheaders > 0) {
1.212     bisitz   2066:                     $r->print(&Apache::loncommon::start_data_table_empty_row()
                   2067:                              .'<td align="center" colspan="5">'
                   2068:                              .&mt($type)
                   2069:                              .'</td>'
                   2070:                              .&Apache::loncommon::end_data_table_empty_row()
                   2071:                     );
1.191     raeburn  2072:                 }
                   2073:                 $r->print($output);
                   2074:             }
                   2075:         }
                   2076:     }
                   2077: }
                   2078: 
                   2079: sub findcourse_advice {
1.343     raeburn  2080:     my ($r,$cattype,$elapsed) = @_;
1.191     raeburn  2081:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.201     raeburn  2082:     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.200     raeburn  2083:     if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) {
1.343     raeburn  2084:         $r->print('<p>'.&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).'
1.191     raeburn  2085: <ul>
                   2086:  <li>'.&mt('The course has yet to be created.').'</li>
                   2087:  <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
                   2088:  <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
                   2089:  <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
                   2090:  <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>
1.343     raeburn  2091:  <li>'.&mt('Automated enrollment added you to the course in the time since you last logged-in.').' '.&mt('If that is the case you can use the "Check for changes" link in the gray Functions bar to update the list of your available course roles.').'</li>   
                   2092:  </ul></p>');
1.191     raeburn  2093:     } else {
1.343     raeburn  2094:         $r->print('<p>'.&mt('If you were expecting to see an active role listed for a particular course, that course may not have been created yet.').'</p>');
                   2095:         if ($elapsed > 600) {
1.361     raeburn  2096:             $r->print('<p>'.&mt('You may also have been assigned to a course in the time since you last logged-in, or checked for changes.').
1.343     raeburn  2097:                       '<br />'.
                   2098:                       &mt('If that is the case you can use the "Check for changes" link in the gray Functions bar to update the list of your available course roles.').'</p>');
                   2099:         }  
1.191     raeburn  2100:     }
1.302     raeburn  2101:     if (($cattype eq 'std') || ($cattype eq 'domonly')) {
                   2102:         $r->print('<h3>'.&mt('Self-Enrollment').'</h3>'.
                   2103:                   '<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 />');
                   2104:         $r->print(&mt('You can search for courses and communities which permit self-enrollment, if you would like to enroll in one.').'</p>'.
                   2105:         &Apache::loncoursequeueadmin::queued_selfenrollment());
                   2106:     }
1.216     raeburn  2107:     return;
                   2108: }
                   2109: 
1.234     raeburn  2110: sub requestcourse_advice {
1.343     raeburn  2111:     my ($r,$cattype,$inrole,$elapsed) = @_;
1.234     raeburn  2112:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                   2113:     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.306     raeburn  2114:     my (%can_request,%request_doms,$output);
1.234     raeburn  2115:     &Apache::lonnet::check_can_request($env{'user.domain'},\%can_request,\%request_doms);
                   2116:     if (keys(%request_doms) > 0) {
                   2117:         my ($types,$typename) = &Apache::loncommon::course_types();
                   2118:         if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) { 
                   2119:             my (@reqdoms,@reqtypes);
                   2120:             foreach my $type (sort(keys(%request_doms))) {
                   2121:                 push(@reqtypes,$type); 
                   2122:                 if (ref($request_doms{$type}) eq 'ARRAY') {
                   2123:                     my $domstr = join(', ',map { &Apache::lonnet::domain($_) } sort(@{$request_doms{$type}}));
1.306     raeburn  2124:                     $output .=
1.238     bisitz   2125:                         '<li>'
                   2126:                        .&mt('[_1]'.$typename->{$type}.'[_2] in domain: [_3]',
                   2127:                             '<i>',
                   2128:                             '</i>',
                   2129:                             '<b>'.$domstr.'</b>')
1.306     raeburn  2130:                        .'</li>';
1.234     raeburn  2131:                     foreach my $dom (@{$request_doms{$type}}) {
                   2132:                         unless (grep(/^\Q$dom\E/,@reqdoms)) {
                   2133:                             push(@reqdoms,$dom);
                   2134:                         }
                   2135:                     }
                   2136:                 }
                   2137:             }
                   2138:             my @showtypes;
                   2139:             foreach my $type (@{$types}) {
                   2140:                 if (grep(/^\Q$type\E$/,@reqtypes)) {
                   2141:                     push(@showtypes,$type);
                   2142:                 }
                   2143:             }
                   2144:             my $requrl = '/adm/requestcourse';
                   2145:             if (@reqdoms == 1) {
                   2146:                 $requrl .= '?showdom='.$reqdoms[0];
                   2147:             }
                   2148:             if (@showtypes > 0) {
                   2149:                 $requrl.=(($requrl=~/\?/)?'&':'?').'crstype='.$showtypes[0];
                   2150:             }
                   2151:             if (@reqdoms == 1 || @showtypes > 0) {
                   2152:                 $requrl .= '&state=crstype&action=new';
1.306     raeburn  2153:             }
1.307     raeburn  2154:             if ($output) {
                   2155:                 $r->print('<h3>'.&mt('Request creation of a course or community').'</h3>'.
                   2156:                           '<p>'.
                   2157:                           &mt('You have rights to request the creation of courses and/or communities in the following domain(s):').
                   2158:                           '<ul>'.
                   2159:                           $output.
                   2160:                           '</ul>'.
                   2161:                           &mt('Use the [_1]request form[_2] to submit a request for creation of a new course or community.',
                   2162:                               '<a href="'.$requrl.'">','</a>').
                   2163:                           '</p>');
                   2164:             }
1.234     raeburn  2165:         }
1.302     raeburn  2166:     } elsif (!$env{'user.adv'}) {
1.306     raeburn  2167:        if ($inrole) {
                   2168:             $r->print('<h3>'.&mt('Currently no additional roles, courses or communities').'</h3>');
                   2169:         } else {
                   2170:             $r->print('<h3>'.&mt('Currently no active roles, courses or communities').'</h3>');
                   2171:         }
1.343     raeburn  2172:         &findcourse_advice($r,$cattype,$elapsed);
1.234     raeburn  2173:     }
                   2174:     return;
                   2175: }
                   2176: 
1.175     albertel 2177: sub privileges_info {
                   2178:     my ($which) = @_;
                   2179:     my $output;
                   2180: 
                   2181:     $which ||= $env{'request.role'};
                   2182: 
                   2183:     foreach my $envkey (sort(keys(%env))) {
                   2184: 	next if ($envkey!~/^user\.priv\.\Q$which\E\.(.*)/);
                   2185: 
                   2186: 	my $where=$1;
                   2187: 	my $ttype;
                   2188: 	my $twhere;
                   2189: 	my (undef,$tdom,$trest,$tsec)=split(m{/},$where);
                   2190: 	if ($trest) {
                   2191: 	    if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
1.288     raeburn  2192: 		$ttype='Authoring Space';
1.175     albertel 2193: 		$twhere='User: '.$trest.', Domain: '.$tdom;
                   2194: 	    } else {
                   2195: 		$ttype= &Apache::loncommon::course_type($tdom.'_'.$trest);
                   2196: 		$twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
                   2197: 		if ($tsec) {
                   2198: 		    my $sec_type = 'Section';
                   2199: 		    if (exists($env{"user.role.gr.$where"})) {
                   2200: 			$sec_type = 'Group';
                   2201: 		    }
                   2202: 		    $twhere.=' ('.$sec_type.': '.$tsec.')';
                   2203: 		}
                   2204: 	    }
                   2205: 	} elsif ($tdom) {
                   2206: 	    $ttype='Domain';
                   2207: 	    $twhere=$tdom;
                   2208: 	} else {
                   2209: 	    $ttype='System';
                   2210: 	    $twhere='/';
                   2211: 	}
1.204     bisitz   2212: 	$output .= "\n<h3>".&mt($ttype).': '.$twhere.'</h3>'."\n<ul>";
1.175     albertel 2213: 	foreach my $priv (sort(split(/:/,$env{$envkey}))) {
                   2214: 	    next if (!$priv);
                   2215: 
                   2216: 	    my ($prv,$restr)=split(/\&/,$priv);
                   2217: 	    my $trestr='';
                   2218: 	    if ($restr ne 'F') {
                   2219: 		$trestr.=' ('.
                   2220: 		    join(', ',
                   2221: 			 map { &Apache::lonnet::plaintext($_) } 
                   2222: 			     (split('',$restr))).') ';
                   2223: 	    }
                   2224: 	    $output .= "\n\t".
                   2225: 		'<li>'.&Apache::lonnet::plaintext($prv).$trestr.'</li>';
                   2226: 	}
                   2227: 	$output .= "\n".'</ul>';
                   2228:     }
                   2229:     return $output;
                   2230: }
                   2231: 
1.110     raeburn  2232: sub build_roletext {
1.313     raeburn  2233:     my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$trole,$twhere,
                   2234:         $tpstart,$tpend,$nochoose,$button,$switchserver,$reinit,$switchwarning,$skipcal) = @_;
1.328     raeburn  2235:     my ($roletext,$roletext_end,$poss_adhoc);
                   2236:     if ($trolecode =~ m/^d(c|h|a)\./) {
                   2237:         $poss_adhoc = 1;
                   2238:     }
                   2239:     my $rowspan=($poss_adhoc) ? ''
1.132     albertel 2240:                          : ' rowspan="2" ';
                   2241: 
1.110     raeburn  2242:     unless ($nochoose) {
1.134     www      2243:         my $buttonname=$trolecode;
                   2244:         $buttonname=~s/\W//g;
1.110     raeburn  2245:         if (!$button) {
                   2246:             if ($switchserver) {
1.212     bisitz   2247:                 $roletext.='<td'.$rowspan.' class="'.$tbg.'">'
                   2248:                           .'<a href="/adm/switchserver?'.$switchserver.'">'
                   2249:                           .&mt('Switch Server')
                   2250:                           .'</a></td>';
1.110     raeburn  2251:             } else {
1.212     bisitz   2252:                 $roletext.=('<td'.$rowspan.' class="'.$tbg.'">&nbsp;</td>');
1.110     raeburn  2253:             }
1.255     raeburn  2254:             if ($switchwarning) {
                   2255:                 if ($tremark eq '') {
                   2256:                     $tremark = $switchwarning;
                   2257:                 } else {
                   2258:                     $tremark .= '<br />'.$switchwarning;
                   2259:                 }
                   2260:             }
1.110     raeburn  2261:         } elsif ($tstatus eq 'is') {
1.212     bisitz   2262:             $roletext.='<td'.$rowspan.' class="'.$tbg.'">'.
                   2263:                         '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   2264:                         &mt('Select').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  2265:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  2266:         } elsif ($tryagain) {
                   2267:             $roletext.=
1.212     bisitz   2268:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   2269:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   2270:                 &mt('Try Selecting Again').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  2271:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  2272:         } elsif ($advanced) {
                   2273:             $roletext.=
1.212     bisitz   2274:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   2275:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   2276:                 &mt('Re-Initialize').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  2277:                         $trolecode."','".$buttonname.'\');" /></td>';
1.209     raeburn  2278:         } elsif ($reinit) {
                   2279:             $roletext.= 
1.212     bisitz   2280:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   2281:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   2282:                 &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1.209     raeburn  2283:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  2284:         } else {
1.209     raeburn  2285:             $roletext.=
1.212     bisitz   2286:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   2287:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   2288:                 &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1.209     raeburn  2289:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  2290:         }
                   2291:     }
1.373     raeburn  2292:     if (($trolecode !~ m/^(ca|aa)\./) && ($trest ne '') && (!$skipcal)) {
1.165     albertel 2293: 	$tremark.=&Apache::lonannounce::showday(time,1,
                   2294: 			 &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                   2295:     }
1.212     bisitz   2296:     $roletext.='<td>'.$trole.'</td>'
                   2297:               .'<td>'.$twhere.'</td>'
                   2298:               .'<td>'.$tpstart.'</td>'
1.223     raeburn  2299:               .'<td>'.$tpend.'</td>';
1.328     raeburn  2300:     unless ($poss_adhoc) {
1.223     raeburn  2301:         $roletext_end = '<td colspan="4">'.
                   2302:                         $tremark.'&nbsp;'.
                   2303:                         '</td>';
1.132     albertel 2304:     }
1.223     raeburn  2305:     return ($roletext,$roletext_end);
1.110     raeburn  2306: }
                   2307: 
1.193     raeburn  2308: sub check_author_homeserver {
1.183     www      2309:     my ($uname,$udom)=@_;
1.193     raeburn  2310:     if (($uname eq '') || ($udom eq '')) {
                   2311:         return ('fail','');
                   2312:     }
1.183     www      2313:     my $home = &Apache::lonnet::homeserver($uname,$udom);
1.193     raeburn  2314:     if (&Apache::lonnet::host_domain($home) ne $udom) {
                   2315:         return ('fail',$home);
                   2316:     }
1.183     www      2317:     my @ids=&Apache::lonnet::current_machine_ids();
1.193     raeburn  2318:     if (grep(/^\Q$home\E$/,@ids)) {
                   2319:         return ('ok',$home);
                   2320:     } else {
                   2321:         return ('switchserver',$home);
1.183     www      2322:     }
                   2323: }
                   2324: 
1.317     raeburn  2325: sub check_for_adhoc {
1.325     raeburn  2326:     my ($dcroles,$helpdeskroles,$update,$then) = @_;
1.104     raeburn  2327:     my $numdc = 0;
1.325     raeburn  2328:     my $numhelpdesk = 0;
1.317     raeburn  2329:     my $numadhoc = 0;
                   2330:     my $num_custom_adhoc = 0; 
1.328     raeburn  2331:     if (($env{'user.adv'}) || ($env{'user.rar'})) {
1.309     raeburn  2332:         foreach my $envkey (sort(keys(%env))) {
1.325     raeburn  2333:             if ($envkey=~/^user\.role\.(dc|dh|da)\.\/($match_domain)\/$/) {
1.317     raeburn  2334:                 my $role = $1;
                   2335:                 my $roledom = $2;
                   2336:                 my $liverole = 1;
1.118     albertel 2337:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
1.260     raeburn  2338:                 my $limit = $update;
1.325     raeburn  2339:                 if ($env{'request.role'} eq "$role./$roledom/") {
1.260     raeburn  2340:                     $limit = $then;
                   2341:                 }
1.317     raeburn  2342:                 if ($tstart && $tstart>$limit) { $liverole = 0; }
                   2343:                 if ($tend   && $tend  <$limit) { $liverole = 0; }
                   2344:                 if ($liverole) {
                   2345:                     if ($role eq 'dc') {
                   2346:                         $dcroles->{$roledom} = $envkey;
                   2347:                         $numdc++;
                   2348:                     } else {
1.325     raeburn  2349:                         $helpdeskroles->{$roledom} = $envkey;
1.323     raeburn  2350:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($roledom);
                   2351:                         if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   2352:                             if (keys(%{$domdefaults{'adhocroles'}})) {
                   2353:                                 $numadhoc ++;
                   2354:                             }
1.317     raeburn  2355:                         }
1.325     raeburn  2356:                         $numhelpdesk++;
1.317     raeburn  2357:                     }
1.104     raeburn  2358:                 }
                   2359:             }
                   2360:         }
                   2361:     }
1.325     raeburn  2362:     return ($numdc,$numhelpdesk,$numadhoc);
1.104     raeburn  2363: }
                   2364: 
1.185     raeburn  2365: sub adhoc_course_role {
1.260     raeburn  2366:     my ($refresh,$update,$then) = @_;
1.239     raeburn  2367:     my ($cdom,$cnum,$crstype);
1.201     raeburn  2368:     $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2369:     $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.239     raeburn  2370:     $crstype = &Apache::loncommon::course_type();
1.260     raeburn  2371:     if (&check_forcc($cdom,$cnum,$refresh,$update,$then,$crstype)) {
1.185     raeburn  2372:         my $setprivs;
1.198     raeburn  2373:         if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
1.185     raeburn  2374:             $setprivs = 1;
                   2375:         } else {
1.198     raeburn  2376:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
1.232     raeburn  2377:             if (($start && ($start>$refresh || $start == -1)) ||
1.260     raeburn  2378:                 ($end && $end<$update)) {
1.185     raeburn  2379:                 $setprivs = 1;
                   2380:             }
1.232     raeburn  2381:         }
1.278     raeburn  2382:         unless ($setprivs) {
                   2383:             if (!exists($env{'user.priv.'.$env{'form.switchrole'}.'./'})) {
                   2384:                 $setprivs = 1;
                   2385:             }
                   2386:         }
1.185     raeburn  2387:         if ($setprivs) {
1.297     raeburn  2388:             if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)(.*?)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
1.185     raeburn  2389:                 my $role = $1;
                   2390:                 my $custom_role = $2;
                   2391:                 my $usec = $3;
                   2392:                 if ($role eq 'cr') {
1.199     raeburn  2393:                     if ($custom_role =~ m-^/$match_domain/$match_username/\w+$-) {
1.185     raeburn  2394:                         $role .= $custom_role;
                   2395:                     } else {
                   2396:                         return;
                   2397:                     }
                   2398:                 }
1.208     raeburn  2399:                 my (%userroles,%newrole,%newgroups,%group_privs);
                   2400:                 my %cgroups =
                   2401:                     &Apache::lonnet::get_active_groups($env{'user.domain'},
                   2402:                                             $env{'user.name'},$cdom,$cnum);
1.321     raeburn  2403:                 my $ccrole;
                   2404:                 if ($crstype eq 'Community') {
                   2405:                     $ccrole = 'co';
                   2406:                 } else {
                   2407:                     $ccrole = 'cc';
                   2408:                 }
1.208     raeburn  2409:                 foreach my $group (keys(%cgroups)) {
                   2410:                     $group_privs{$group} =
1.321     raeburn  2411:                         $env{'user.priv.'.$ccrole.'./'.$cdom.'/'.$cnum.'./'.$cdom.'/'.$cnum.'/'.$group};
1.208     raeburn  2412:                 }
                   2413:                 $newgroups{'/'.$cdom.'/'.$cnum} = \%group_privs;
1.185     raeburn  2414:                 my $area = '/'.$cdom.'/'.$cnum;
                   2415:                 my $spec = $role.'.'.$area;
                   2416:                 if ($usec ne '') {
                   2417:                     $spec .= '/'.$usec;
                   2418:                     $area .= '/'.$usec;
                   2419:                 }
1.278     raeburn  2420:                 if ($role =~ /^cr/) {
                   2421:                     &Apache::lonnet::custom_roleprivs(\%newrole,$role,$cdom,$cnum,$spec,$area);
                   2422:                 } else {
                   2423:                     &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
                   2424:                 }
1.208     raeburn  2425:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1.232     raeburn  2426:                 my $adhocstart = $refresh-1;
1.185     raeburn  2427:                 $userroles{'user.role.'.$spec} = $adhocstart.'.';
1.186     raeburn  2428:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.185     raeburn  2429:             }
                   2430:         }
                   2431:     }
                   2432:     return;
                   2433: }
                   2434: 
                   2435: sub check_forcc {
1.260     raeburn  2436:     my ($cdom,$cnum,$refresh,$update,$then,$crstype) = @_;
1.239     raeburn  2437:     my ($is_cc,$ccrole);
                   2438:     if ($crstype eq 'Community') {
                   2439:         $ccrole = 'co';
                   2440:     } else {
                   2441:         $ccrole = 'cc';
                   2442:     }
1.266     droeschl 2443:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
                   2444:         my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum;
                   2445:         if (defined($env{$envkey})) {
                   2446:             $is_cc = 1;
                   2447:             my ($tstart,$tend)=split(/\./,$env{$envkey});
                   2448:             my $limit = $update;
                   2449:             if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) {
                   2450:                 $limit = $then;
1.185     raeburn  2451:             }
1.266     droeschl 2452:             if ($tstart && $tstart>$refresh) { $is_cc = 0; }
                   2453:             if ($tend   && $tend  <$limit) { $is_cc = 0; }
1.185     raeburn  2454:         }
                   2455:     }
                   2456:     return $is_cc;
                   2457: }
                   2458: 
1.108     raeburn  2459: sub courselink {
1.317     raeburn  2460:     my ($roledom,$rowtype,$role) = @_;
1.109     raeburn  2461:     my $courseform=&Apache::loncommon::selectcourse_link
1.317     raeburn  2462:                    ('rolechoice','course'.$rowtype.'_'.$roledom.'_'.$role,
                   2463:                     'domain'.$rowtype.'_'.$roledom.'_'.$role,
                   2464:                     'coursedesc'.$rowtype.'_'.$roledom.'_'.$role,
                   2465:                     $roledom.':'.$role,undef,'Course/Community');
                   2466:     my $hiddenitems = '<input type="hidden" name="domain'.$rowtype.'_'.$roledom.'_'.$role.'" value="'.$roledom.'" />'.
                   2467:                       '<input type="hidden" name="origdom'.$rowtype.'_'.$roledom.'_'.$role.'" value="'.$roledom.'" />'.
                   2468:                       '<input type="hidden" name="course'.$rowtype.'_'.$roledom.'_'.$role.'" value="" />'.
                   2469:                       '<input type="hidden" name="coursedesc'.$rowtype.'_'.$roledom.'_'.$role.'" value="" />';
1.112     raeburn  2470:     return $courseform.$hiddenitems;
1.109     raeburn  2471: }
                   2472: 
                   2473: sub coursepick_jscript {
1.312     damieng  2474:     my %js_lt = &Apache::lonlocal::texthash(
1.239     raeburn  2475:                   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  2476:                   youc => 'You can only use this screen to select courses and communities in the current domain.',
1.184     raeburn  2477:              );
1.312     damieng  2478:     &js_escape(\%js_lt);
1.104     raeburn  2479:     my $verify_script = <<"END";
1.179     raeburn  2480: <script type="text/javascript">
1.225     bisitz   2481: // <![CDATA[
1.108     raeburn  2482: function verifyCoursePick(caller) {
                   2483:     var numbutton = getIndex(caller)
1.112     raeburn  2484:     var pickedCourse = document.rolechoice.elements[numbutton+4].value
                   2485:     var pickedDomain = document.rolechoice.elements[numbutton+2].value
                   2486:     if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104     raeburn  2487:         if (pickedCourse != '') {
1.108     raeburn  2488:             if (numbutton != -1) {
                   2489:                 var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                   2490:                 document.rolechoice.elements[numbutton+1].name = courseTarget
                   2491:                 document.rolechoice.submit()
                   2492:             }
1.104     raeburn  2493:         }
                   2494:         else {
1.312     damieng  2495:             alert("$js_lt{'plsu'}");
1.104     raeburn  2496:         }
                   2497:     }
                   2498:     else {
1.312     damieng  2499:         alert("$js_lt{'youc'}")
1.104     raeburn  2500:     }
                   2501: }
1.109     raeburn  2502: function getIndex(caller) {
1.108     raeburn  2503:     for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109     raeburn  2504:         if (document.rolechoice.elements[i] == caller) {
1.108     raeburn  2505:             return i;
                   2506:         }
                   2507:     }
                   2508:     return -1;
                   2509: }
1.225     bisitz   2510: // ]]>
1.104     raeburn  2511: </script>
                   2512: END
1.109     raeburn  2513:     return $verify_script;
1.104     raeburn  2514: }
                   2515: 
1.193     raeburn  2516: sub coauthorlink {
                   2517:     my ($dcdom,$rowtype) = @_;
                   2518:     my $coauthorform=&Apache::loncommon::selectauthor_link('rolechoice',$dcdom);
                   2519:     my $hiddenitems = '<input type="hidden" name="adhoccauname'.$rowtype.'_'.$dcdom.'" value="" />';
                   2520:     return $coauthorform.$hiddenitems;
                   2521: }
                   2522: 
1.113     raeburn  2523: sub display_cc_role {
                   2524:     my $rolekey = shift;
1.223     raeburn  2525:     my ($roletext,$roletext_end);
1.118     albertel 2526:     my $advanced = $env{'user.adv'};
                   2527:     my $tryagain = $env{'form.tryagain'};
1.113     raeburn  2528:     unless ($rolekey =~/^error\:/) {
1.240     raeburn  2529:         if ($rolekey =~ m{^user\.role\.(cc|co)\./($match_domain)/($match_courseid)$}) {
                   2530:             my $ccrole = $1;
1.249     raeburn  2531:             my $tdom = $2;
                   2532:             my $trest = $3;
                   2533:             my $tcourseid = $tdom.'_'.$trest;
                   2534:             my $trolecode = $ccrole.'./'.$tdom.'/'.$trest;
1.113     raeburn  2535:             my $twhere;
1.152     raeburn  2536:             my $ttype;
1.313     raeburn  2537:             my $skipcal;
1.212     bisitz   2538:             my $tbg='LC_roles_is';
1.113     raeburn  2539:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   2540:             if (%newhash) {
                   2541:                 $twhere=$newhash{'description'}.
1.261     bisitz   2542:                         ' <span class="LC_fontsize_small">'.
1.249     raeburn  2543:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
1.211     tempelho 2544:                         '</span>';
1.153     raeburn  2545:                 $ttype = $newhash{'type'};
1.113     raeburn  2546:             } else {
                   2547:                 $twhere=&mt('Currently not available');
1.118     albertel 2548:                 $env{'course.'.$tcourseid.'.description'}=$twhere;
1.313     raeburn  2549:                 $skipcal = 1;
1.110     raeburn  2550:             }
1.242     raeburn  2551:             my $trole = &Apache::lonnet::plaintext($ccrole,$ttype,$tcourseid);
1.258     raeburn  2552:             $twhere.="<br />".&mt('Domain').":".$tdom;
1.313     raeburn  2553:             ($roletext,$roletext_end) = &build_roletext($trolecode,$tdom,$trest,'is',$tryagain,$advanced,'',$tbg,$trole,$twhere,'','','',1,'','','',$skipcal);
1.104     raeburn  2554:         }
                   2555:     }
1.223     raeburn  2556:     return ($roletext,$roletext_end);
1.104     raeburn  2557: }
                   2558: 
1.352     raeburn  2559: sub display_curr_role {
                   2560:     my ($currentrole) = @_;
                   2561:     my ($roletext,$roletext_end);
                   2562:     my $advanced = $env{'user.adv'};
                   2563:     my $tryagain = $env{'form.tryagain'};
                   2564:     my ($role,$rest) = split(m{\./},$currentrole,2);
                   2565:     unless (!defined($role) || $role eq '') {
                   2566:         if ($rest =~ m{^($match_domain)/($match_courseid)(?:/(\w+)|$)}) {
                   2567:             my $cdom = $1;
                   2568:             my $cnum = $2;
                   2569:             my $csec = $3;
                   2570:             my $cid = $cdom.'_'.$cnum;
                   2571:             my $ttype = $env{'course.'.$cid.'.type'};
                   2572:             my $skipcal = 1;
                   2573:             my $tbg='LC_roles_is';
                   2574:             my $twhere = $env{'course.'.$cid.'.description'}. 
                   2575:                         ' <span class="LC_fontsize_small">'.
                   2576:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$cnum,$cdom).
                   2577:                         '</span>';
                   2578:             my $trole = &Apache::lonnet::plaintext($role,$ttype,$cid);
                   2579:             if ($csec) {
                   2580:                 $twhere.= '&nbsp; '.&mt('Section').':&nbsp;'.$csec;
                   2581:             }
                   2582:             if ($role ne 'st') {
                   2583:                 $twhere.= '&nbsp; '.&mt('Domain').':&nbsp;'.$cdom;
                   2584:             }
                   2585:             ($roletext,$roletext_end) = &build_roletext($currentrole,$cdom,$cnum,'is',$tryagain,$advanced,'',$tbg,$trole,$twhere,'','','',1,'','','',$skipcal);
                   2586:         }
                   2587:     }
                   2588:     return ($roletext,$roletext_end);
                   2589: }
                   2590: 
1.192     raeburn  2591: sub adhoc_roles_row {
1.138     raeburn  2592:     my ($dcdom,$rowtype) = @_;
1.212     bisitz   2593:     my $output = &Apache::loncommon::continue_data_table_row()
1.314     raeburn  2594:                  .' <td colspan="5" class="LC_textsize_mobile">'
1.330     raeburn  2595:                  .&mt('[_1]Ad hoc[_2] roles in domain [_3]'
1.212     bisitz   2596:                      ,'<span class="LC_cusr_emph">','</span>',$dcdom)
1.330     raeburn  2597:                  .' -- ';
1.317     raeburn  2598:     my $role = 'cc';
                   2599:     my $selectcclink = &courselink($dcdom,$rowtype,$role);
1.239     raeburn  2600:     my $ccrole = &Apache::lonnet::plaintext('co',undef,undef,1);
1.182     www      2601:     my $carole = &Apache::lonnet::plaintext('ca');
1.193     raeburn  2602:     my $selectcalink = &coauthorlink($dcdom,$rowtype);
1.227     bisitz   2603:     $output.=$ccrole.': '.$selectcclink
1.249     raeburn  2604:             .' | '.$carole.': '.$selectcalink.'</td>'
1.212     bisitz   2605:             .&Apache::loncommon::end_data_table_row();
1.108     raeburn  2606:     return $output;
                   2607: }
                   2608: 
1.317     raeburn  2609: sub adhoc_customroles_row {
1.323     raeburn  2610:     my ($role,$dhdom,$rowtype,$update,$then) = @_;
                   2611:     my $liverole = 1;
                   2612:     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$dhdom/"});
                   2613:     my $limit = $update;
                   2614:     if (($role eq 'dh') && ($env{'request.role'} eq 'dh./'.$dhdom.'/')) {
                   2615:         $limit = $then;
                   2616:     }
                   2617:     if ($tstart && $tstart>$limit) { $liverole = 0; }
                   2618:     if ($tend   && $tend  <$limit) { $liverole = 0; }
                   2619:     return unless ($liverole);
                   2620:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dhdom); 
                   2621:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   2622:         if (scalar(keys(%{$domdefaults{'adhocroles'}})) > 0) {
                   2623:             return &Apache::loncommon::continue_data_table_row()
                   2624:                   .' <td colspan="5" class="LC_textsize_mobile">'
1.331     raeburn  2625:                   .&mt('[_1]Ad hoc[_2] course/community roles in domain [_3]',
1.323     raeburn  2626:                        '<span class="LC_cusr_emph">','</span>',$dhdom)
1.331     raeburn  2627:                   .' -- '.&courselink($dhdom,$rowtype,$role);
1.323     raeburn  2628:         }
1.317     raeburn  2629:     }
                   2630:     return;
                   2631: }
                   2632: 
1.104     raeburn  2633: sub recent_filename {
                   2634:     my $area=shift;
1.149     www      2635:     return 'nohist_recent_'.&escape($area);
1.104     raeburn  2636: }
                   2637: 
1.139     raeburn  2638: sub courseloadpage {
                   2639:     my ($courseid) = @_;
                   2640:     my $startpage;
1.144     albertel 2641:     my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
                   2642: 					      [$courseid.':courseinit']);
1.139     raeburn  2643:     my ($tmp) = %entry_settings;
1.144     albertel 2644:     unless ($tmp =~ /^error: 2 /) {
1.139     raeburn  2645:         $startpage = $entry_settings{$courseid.':courseinit'};
                   2646:     }
                   2647:     if ($startpage eq '') {
                   2648:         if (exists($env{'environment.course_init_display'})) {
                   2649:             $startpage = $env{'environment.course_init_display'};
                   2650:         }
                   2651:     }
                   2652:     return $startpage;
                   2653: }
                   2654: 
1.260     raeburn  2655: sub update_session_roles {
                   2656:     my $then=$env{'user.login.time'};
                   2657:     my $refresh=$env{'user.refresh.time'};
                   2658:     if (!$refresh) {
                   2659:         $refresh = $then;
                   2660:     }
                   2661:     my $update = $env{'user.update.time'};
                   2662:     if (!$update) {
                   2663:         $update = $then;
                   2664:     }
                   2665:     my $now = time;
                   2666:     my %roleshash =
                   2667:         &Apache::lonnet::get_my_roles('','','userroles',
                   2668:                                       ['active','future','previous'],
                   2669:                                       undef,undef,1);
                   2670:     my ($msg,@newsec,$oldsec,$currrole_expired,@changed_roles,
1.264     raeburn  2671:         %changed_groups,%dbroles,%deletedroles,%allroles,%allgroups,
1.260     raeburn  2672:         %userroles,%checkedgroup,%crprivs,$hasgroups,%rolechange,
                   2673:         %groupchange,%newrole,%newgroup,%customprivchg,%groups_roles,
                   2674:         @rolecodes);
                   2675:     my @possroles = ('cr','st','ta','ad','ep','in','co','cc');
                   2676:     my %courseroles;
                   2677:     foreach my $item (keys(%roleshash)) {
                   2678:         my ($uname,$udom,$role,$remainder) = split(/:/,$item,4);
                   2679:         my ($tstart,$tend) = split(/:/,$roleshash{$item});
                   2680:         my ($section,$group,@group_privs);
                   2681:         if ($role =~ m{^gr/(\w*)$}) {
                   2682:             $role = 'gr';
                   2683:             my $priv = $1;
                   2684:             next if ($tstart eq '-1');
                   2685:             if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
                   2686:                 if ($priv ne '') {
                   2687:                     push(@group_privs,$priv);
                   2688:                 }
                   2689:             }
                   2690:             if ($remainder =~ /:/) {
                   2691:                 (my $additional_privs,$group) =
                   2692:                     ($remainder =~ /^([\w:]+):([^:]+)$/);
                   2693:                 if ($additional_privs ne '') {
                   2694:                     if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
                   2695:                         push(@group_privs,split(/:/,$additional_privs));
                   2696:                         @group_privs = sort(@group_privs);
                   2697:                     }
                   2698:                 }
                   2699:             } else {
                   2700:                 $group = $remainder;
                   2701:             }
                   2702:         } else {
                   2703:             $section = $remainder;
                   2704:         }
                   2705:         my $where = "/$udom/$uname";
                   2706:         if ($section ne '') {
                   2707:             $where .= "/$section";
                   2708:         } elsif ($group ne '') {
                   2709:             $where .= "/$group";
                   2710:         }
                   2711:         my $rolekey = "$role.$where";
                   2712:         my $envkey = "user.role.$rolekey";
                   2713:         $dbroles{$envkey} = 1;
                   2714:         if (($env{'request.role'} eq $rolekey) && ($role ne 'st')) {
                   2715:             if (&curr_role_status($tstart,$tend,$refresh,$now) ne 'active') {
                   2716:                 $currrole_expired = 1;
                   2717:             }
                   2718:         }
                   2719:         if ($env{$envkey} eq '') {
                   2720:             my $status_in_db =
1.271     raeburn  2721:                 &curr_role_status($tstart,$tend,$now,$now);
1.260     raeburn  2722:                 &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2723:             if (($role eq 'st') && ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
                   2724:                 if ($status_in_db eq 'active') {
                   2725:                     if ($section eq '') {
                   2726:                         push(@newsec,'none');
                   2727:                     } else {
                   2728:                         push(@newsec,$section);
                   2729:                     }
                   2730:                 }
                   2731:             } else {
                   2732:                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2733:                     push(@changed_roles,$role);
                   2734:                 }
                   2735:                 if ($status_in_db ne 'previous') {
                   2736:                     if ($role eq 'gr') {
                   2737:                         $newgroup{$rolekey} = $status_in_db;
                   2738:                         if ($status_in_db eq 'active') {
                   2739:                             unless (ref($courseroles{$udom}) eq 'HASH') {
                   2740:                                 %{$courseroles{$udom}} =
                   2741:                                     &Apache::lonnet::get_my_roles('','','userroles',
                   2742:                                                                   ['active'],\@possroles,
                   2743:                                                                   [$udom],1);
                   2744:                             }
                   2745:                             &Apache::lonnet::get_groups_roles($udom,$uname,
                   2746:                                                               $courseroles{$udom},
                   2747:                                                               \@rolecodes,\%groups_roles);
                   2748:                         }
                   2749:                     } else {
                   2750:                         $newrole{$rolekey} = $status_in_db;
1.372     raeburn  2751:                         if ($role eq 'au') {
                   2752:                             my %userenv =
                   2753:                                 &Apache::lonnet::userenvironment($env{'user.domain'},
                   2754:                                                                  $env{'user.name'},'authoreditors');
                   2755:                             if ($userenv{'authoreditors'}) {
                   2756:                                 $userenv{'editors'} = $userenv{'authoreditors'};
                   2757:                             } else {
                   2758:                                 my %domdef = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
                   2759:                                 if ($domdef{'editors'} ne '') {
                   2760:                                     $userenv{'editors'} = $domdef{'editors'};
                   2761:                                 } else {
                   2762:                                     $userenv{'editors'} = 'edit,xml';
                   2763:                                 }
                   2764:                             }
                   2765:                             &Apache::lonnet::appenv(\%userenv);
                   2766:                         }
1.260     raeburn  2767:                     }
                   2768:                 }
                   2769:             }
                   2770:         } else {
                   2771:             my ($currstart,$currend) = split(/\./,$env{$envkey});
                   2772:             if ($role eq 'gr') {
                   2773:                 if (&curr_role_status($currstart,$currend,$refresh,$update) ne 'previous') {
                   2774:                     $hasgroups = 1;
                   2775:                 }
                   2776:             }
                   2777:             if (($currstart ne $tstart) || ($currend ne $tend)) {
                   2778:                 my $status_in_env =
                   2779:                     &curr_role_status($currstart,$currend,$refresh,$update);
                   2780:                 my $status_in_db =
1.271     raeburn  2781:                     &curr_role_status($tstart,$tend,$now,$now);
1.260     raeburn  2782:                 if ($status_in_env ne $status_in_db) {
                   2783:                     if ($status_in_env eq 'active') {
                   2784:                         if ($role eq 'st') {
                   2785:                             if ($env{'request.role'} eq $rolekey) {
                   2786:                                 my $switchsection;
                   2787:                                 unless (ref($courseroles{$udom}) eq 'HASH') {
                   2788:                                     %{$courseroles{$udom}} =
                   2789:                                         &Apache::lonnet::get_my_roles('','','userroles',
                   2790:                                                                       ['active'],
                   2791:                                                                       \@possroles,[$udom],1);
                   2792:                                 }
                   2793:                                 foreach my $crsrole (keys(%{$courseroles{$udom}})) {
                   2794:                                     if ($crsrole =~ /^\Q$uname\E:\Q$udom\E:st/) {
                   2795:                                         $switchsection = 1;
                   2796:                                         last;
                   2797:                                     }
                   2798:                                 }
                   2799:                                 if ($switchsection) {
                   2800:                                     if ($section eq '') {
                   2801:                                         $oldsec = 'none';
                   2802:                                     } else {
                   2803:                                         $oldsec = $section;
                   2804:                                     }
                   2805:                                     &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2806:                                 } else {
                   2807:                                     $currrole_expired = 1;
                   2808:                                     next;
                   2809:                                 }
                   2810:                             }
                   2811:                         }
                   2812:                         unless ($rolekey eq $env{'request.role'}) {
                   2813:                             if ($role eq 'gr') {
                   2814:                                 &Apache::lonnet::delete_env_groupprivs($where,\%courseroles,\@possroles);
                   2815:                             } else {
                   2816:                                 &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
                   2817:                                 &Apache::lonnet::delenv("user.priv.cm.$where",undef,['cm']);
                   2818:                             }
                   2819:                             &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2820:                         }
                   2821:                     } elsif ($status_in_db eq 'active') {
                   2822:                         if (($role eq 'st') &&
                   2823:                             ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
                   2824:                             if ($section eq '') {
                   2825:                                 push(@newsec,'none');
                   2826:                             } else {
                   2827:                                 push(@newsec,$section);
                   2828:                             }
                   2829:                         } elsif ($role eq 'gr') {
                   2830:                             unless (ref($courseroles{$udom}) eq 'HASH') {
                   2831:                                 %{$courseroles{$udom}} =
                   2832:                                     &Apache::lonnet::get_my_roles('','','userroles',
                   2833:                                                                   ['active'],
                   2834:                                                                   \@possroles,[$udom],1);
                   2835:                             }
                   2836:                             &Apache::lonnet::get_groups_roles($udom,$uname,
                   2837:                                                               $courseroles{$udom},
                   2838:                                                               \@rolecodes,\%groups_roles);
                   2839:                         }
                   2840:                         &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2841:                     }
                   2842:                     unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2843:                         push(@changed_roles,$role);
                   2844:                     }
                   2845:                     if ($role eq 'gr') {
                   2846:                         $groupchange{"/$udom/$uname"}{$group} = $status_in_db;
                   2847:                     } else {
                   2848:                         $rolechange{$rolekey} = $status_in_db;
                   2849:                     }
                   2850:                 }
                   2851:             } else {
                   2852:                 if ($role eq 'gr') {
                   2853:                     unless ($checkedgroup{$where}) {
                   2854:                         my $status_in_db =
                   2855:                             &curr_role_status($tstart,$tend,$refresh,$now);
                   2856:                         if ($tstart eq '-1') {
                   2857:                             $status_in_db = 'deleted';
                   2858:                         }
                   2859:                         unless (ref($courseroles{$udom}) eq 'HASH') {
                   2860:                             %{$courseroles{$udom}} =
                   2861:                                 &Apache::lonnet::get_my_roles('','','userroles',
                   2862:                                                               ['active'],
                   2863:                                                               \@possroles,[$udom],1);
                   2864:                         }
                   2865:                         if (ref($courseroles{$udom}) eq 'HASH') {
                   2866:                             foreach my $item (keys(%{$courseroles{$udom}})) {
                   2867:                                 next unless ($item =~ /^\Q$uname\E/);
                   2868:                                 my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   2869:                                 my $area = '/'.$cdom.'/'.$cnum;
                   2870:                                 if ($crssec ne '') {
                   2871:                                     $area .= '/'.$crssec;
                   2872:                                 }
                   2873:                                 my $crsrolekey = $crsrole.'.'.$area;
                   2874:                                 my $currprivs = $env{'user.priv.'.$crsrole.'.'.$area.'.'.$where};
                   2875:                                 $currprivs =~ s/^://;
                   2876:                                 $currprivs =~ s/\&F$//;
                   2877:                                 my @curr_grp_privs = split(/\&F:/,$currprivs);
                   2878:                                 @curr_grp_privs = sort(@curr_grp_privs);
                   2879:                                 my @diffs;
                   2880:                                 if (@group_privs > 0 || @curr_grp_privs > 0) {
                   2881:                                     @diffs = &Apache::loncommon::compare_arrays(\@group_privs,\@curr_grp_privs);
                   2882:                                 }
                   2883:                                 if (@diffs == 0) {
                   2884:                                     last;
                   2885:                                 } else {
                   2886:                                     unless(grep(/^\Qgr\E$/,@rolecodes)) {
                   2887:                                         push(@rolecodes,'gr');
                   2888:                                     }
                   2889:                                     &gather_roleprivs(\%allroles,\%allgroups,
                   2890:                                                       \%userroles,$where,$role,
                   2891:                                                       $tstart,$tend,$status_in_db);
                   2892:                                     if ($status_in_db eq 'active') {
                   2893:                                         &Apache::lonnet::get_groups_roles($udom,$uname,
                   2894:                                                                           $courseroles{$udom},
                   2895:                                                                           \@rolecodes,\%groups_roles);
                   2896:                                     }
                   2897:                                     $changed_groups{$udom.'_'.$uname}{$group} = $status_in_db;
                   2898:                                     last;
                   2899:                                 }
                   2900:                             }
                   2901:                         }
                   2902:                         $checkedgroup{$where} = 1;
                   2903:                     }
                   2904:                 } elsif ($role =~ /^cr/) {
                   2905:                     my $status_in_db =
                   2906:                         &curr_role_status($tstart,$tend,$refresh,$now);
                   2907:                     my ($rdummy,$rest) = split(/\//,$role,2);
                   2908:                     my %currpriv;
                   2909:                     unless (exists($crprivs{$rest})) {
                   2910:                         my ($rdomain,$rauthor,$rrole)=split(/\//,$rest);
                   2911:                         my $homsvr=&Apache::lonnet::homeserver($rauthor,$rdomain);
                   2912:                         if (&Apache::lonnet::hostname($homsvr) ne '') {
                   2913:                             my ($rdummy,$roledef)=
                   2914:                             &Apache::lonnet::get('roles',["rolesdef_$rrole"],
                   2915:                                                  $rdomain,$rauthor);
                   2916:                             if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   2917:                                 my $i = 0;
                   2918:                                 my @scopes = ('sys','dom','crs');
                   2919:                                 my @privs = split(/\_/,$roledef);
                   2920:                                 foreach my $priv (@privs) {
                   2921:                                     my ($blank,@prv) = split(/:/,$priv);
                   2922:                                     @prv = map { $_ .= (/\&\w+$/ ? '':'&F') } @prv;
1.264     raeburn  2923:                                     if (@prv) {
                   2924:                                         $priv = ':'.join(':',sort(@prv));
                   2925:                                     }
1.260     raeburn  2926:                                     $crprivs{$rest}{$scopes[$i]} = $priv;
                   2927:                                     $i++;
                   2928:                                 }
                   2929:                             }
                   2930:                         }
                   2931:                     }
1.279     raeburn  2932:                     my $status_in_env =
                   2933:                         &curr_role_status($currstart,$currend,$refresh,$update);
                   2934:                     if ($status_in_env eq 'active') {
                   2935:                         $currpriv{sys} = $env{"user.priv.$rolekey./"};
                   2936:                         $currpriv{dom} = $env{"user.priv.$rolekey./$udom/"};
                   2937:                         $currpriv{crs} = $env{"user.priv.$rolekey.$where"};
                   2938:                         if (keys(%crprivs)) {
                   2939:                             if (($crprivs{$rest}{sys} ne $currpriv{sys}) ||
                   2940:                                 ($crprivs{$rest}{dom} ne $currpriv{dom})
1.260     raeburn  2941:  ||
1.279     raeburn  2942:                                 ($crprivs{$rest}{crs} ne $currpriv{crs})) {
                   2943:                                 &gather_roleprivs(\%allroles,\%allgroups,
                   2944:                                                   \%userroles,$where,$role,
                   2945:                                                   $tstart,$tend,$status_in_db);
                   2946:                                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2947:                                     push(@changed_roles,$role);
                   2948:                                 }
1.260     raeburn  2949:                                 $customprivchg{$rolekey} = $status_in_env;
                   2950:                             }
                   2951:                         }
                   2952:                     }
                   2953:                 }
                   2954:             }
                   2955:         }
                   2956:     }
                   2957:     foreach my $envkey (keys(%env)) {
                   2958:         next unless ($envkey =~ /^user\.role\./);
                   2959:         next if ($dbroles{$envkey});
                   2960:         next if ($envkey eq 'user.role.'.$env{'request.role'});
                   2961:         my ($currstart,$currend) = split(/\./,$env{$envkey});
                   2962:         my $status_in_env =
                   2963:             &curr_role_status($currstart,$currend,$refresh,$update);
                   2964:         my ($rolekey) = ($envkey =~ /^user\.role\.(.+)$/);
1.297     raeburn  2965:         my ($role,$rest)=split(m{\./},$rolekey,2);
                   2966:         $rest = '/'.$rest;
1.260     raeburn  2967:         if (&Apache::lonnet::delenv($envkey,undef,[$role])) {
                   2968:             if ($status_in_env eq 'active') {
                   2969:                 if ($role eq 'gr') {
                   2970:                     &Apache::lonnet::delete_env_groupprivs($rest,\%courseroles,
                   2971:                                                            \@possroles);
                   2972:                 } else {
                   2973:                     &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
                   2974:                     &Apache::lonnet::delenv("user.priv.cm.$rest",undef,['cm']);
                   2975:                 }
                   2976:                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2977:                     push(@changed_roles,$role);
                   2978:                 }
                   2979:                 $deletedroles{$rolekey} = 1;
                   2980:             }
                   2981:         }
                   2982:     }
                   2983:     if (($oldsec) && (@newsec > 0)) {
                   2984:         if (@newsec > 1) {
1.274     raeburn  2985:             $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  2986:         } else {
                   2987:             my $newrole = $env{'request.role'};
                   2988:             if ($newsec[0] eq 'none') {
                   2989:                 $newrole =~ s{(/[^/])$}{};
                   2990:             } elsif ($oldsec eq 'none') {
                   2991:                 $newrole .= '/'.$newsec[0];
                   2992:             } else {
                   2993:                 $newrole =~ s{([^/]+)$}{$newsec[0]};
                   2994:             }
                   2995:             my $coursedesc = $env{'course.'.$env{'request.course.id'}.'.description'};
                   2996:             my ($curr_role) = ($env{'request.role'} =~ m{^(\w+)\./$match_domain/$match_courseid});
                   2997:             my %temp=('logout_'.$env{'request.course.id'} => time);
                   2998:             &Apache::lonnet::put('email_status',\%temp);
                   2999:             &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
                   3000:             &Apache::lonnet::appenv({"request.course.id"   => '',
                   3001:                                      "request.course.fn"   => '',
                   3002:                                      "request.course.uri"  => '',
                   3003:                                      "request.course.sec"  => '',
                   3004:                                      "request.role"        => 'cm',
                   3005:                                      "request.role.adv"    => $env{'user.adv'},
                   3006:                                      "request.role.domain" => $env{'user.domain'}});
                   3007:             my $rolename = &Apache::loncommon::plainname($curr_role);
                   3008:             $msg = '<p><form name="reselectrole" action="/adm/roles" method="post" />'.
                   3009:                    '<input type="hidden" name="newrole" value="" />'.
                   3010:                    '<input type="hidden" name="selectrole" value="1" />'.
                   3011:                    '<span class="LC_info">'.
                   3012:                    &mt('Your section has changed for your current [_1] role in [_2].',$rolename,$coursedesc).'</span><br />';
                   3013:             my $button = '<input type="button" name="sectionchanged" value="'.
                   3014:                          &mt('Re-Select').'" onclick="javascript:enterrole(this.form,'."'$newrole','sectionchanged'".')" />';
                   3015:             if ($newsec[0] eq 'none') {
                   3016:                 $msg .= &mt('[_1] to continue with your new section-less role.',$button);
                   3017:             } else {
                   3018:                 $msg .= &mt('[_1] to continue with your new role in section ([_2]).',$button,$newsec[0]);
                   3019:             }
                   3020:             $msg .= '</form></p>';
                   3021:         }
                   3022:     } elsif ($currrole_expired) {
1.274     raeburn  3023:         $msg .= '<p class="LC_warning">';
1.260     raeburn  3024:         if (&Apache::loncommon::show_course()) {
                   3025:             $msg .= &mt('Your role in the current course has expired.');
                   3026:         } else {
                   3027:             $msg .= &mt('Your current role has expired.');
                   3028:         }
1.274     raeburn  3029:         $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  3030:     }
1.279     raeburn  3031:     &Apache::lonnet::set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   3032:     my ($curr_is_adv,$curr_role_adv,$curr_author,$curr_role_author);
                   3033:     $curr_author = $env{'user.author'};
                   3034:     if (($env{'request.role'} =~/^au/) || ($env{'request.role'} =~/^ca/) ||
                   3035:         ($env{'request.role'} =~/^aa/)) {
                   3036:         $curr_role_author=1;
                   3037:     }
                   3038:     $curr_is_adv = $env{'user.adv'};
                   3039:     $curr_role_adv = $env{'request.role.adv'};
                   3040:     if (keys(%userroles) > 0) {
                   3041:         foreach my $role (@changed_roles) {
                   3042:             unless(grep(/^\Q$role\E$/,@rolecodes)) {
                   3043:                 push(@rolecodes,$role);
                   3044:             }
                   3045:         }
                   3046:         unless(grep(/^\Qcm\E$/,@rolecodes)) {
                   3047:             push(@rolecodes,'cm');
                   3048:         }
                   3049:         &Apache::lonnet::appenv(\%userroles,\@rolecodes);
                   3050:     }
                   3051:     my %newenv;
                   3052:     if (&Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'})) {
                   3053:         unless ($curr_is_adv) {
                   3054:             $newenv{'user.adv'} = 1;
                   3055:         }
                   3056:     } elsif ($curr_is_adv && !$curr_role_adv) {
                   3057:         &Apache::lonnet::delenv('user.adv');
                   3058:     }
                   3059:     my %authorroleshash =
                   3060:         &Apache::lonnet::get_my_roles('','','userroles',['active'],['au','ca','aa']);
                   3061:     if (keys(%authorroleshash)) {
                   3062:         unless ($curr_author) {
                   3063:             $newenv{'user.author'} = 1;
                   3064:         }
                   3065:     } elsif ($curr_author && !$curr_role_author) {
                   3066:         &Apache::lonnet::delenv('user.author');
                   3067:     }
                   3068:     if ($env{'request.course.id'}) {
                   3069:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3070:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3071:         my (@activecrsgroups,$crsgroupschanged);
                   3072:         if ($env{'request.course.groups'}) {
                   3073:             @activecrsgroups = split(/:/,$env{'request.course.groups'});
                   3074:             foreach my $item (keys(%deletedroles)) {
                   3075:                 if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
                   3076:                     if (grep(/^\Q$1\E$/,@activecrsgroups)) {
                   3077:                         $crsgroupschanged = 1;
                   3078:                         last;
                   3079:                     }
                   3080:                 }
                   3081:             }
                   3082:         }
                   3083:         unless ($crsgroupschanged) {
                   3084:             foreach my $item (keys(%newgroup)) {
                   3085:                 if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
                   3086:                     if ($newgroup{$item} eq 'active') {
                   3087:                         $crsgroupschanged = 1;
                   3088:                         last;
                   3089:                     }
                   3090:                 }
                   3091:             }
                   3092:         }
                   3093:         if ((ref($changed_groups{$env{'request.course.id'}}) eq 'HASH') ||
                   3094:             (ref($groupchange{"/$cdom/$cnum"}) eq 'HASH') ||
                   3095:             ($crsgroupschanged)) {
                   3096:             my %grouproles =  &Apache::lonnet::get_my_roles('','','userroles',
                   3097:                                                             ['active'],['gr'],[$cdom],1);
                   3098:             my @activegroups;
                   3099:             foreach my $item (keys(%grouproles)) {
                   3100:                 next unless($item =~ /^\Q$cnum\E:\Q$cdom\E/);
                   3101:                 my $group;
                   3102:                 my ($crsn,$crsd,$role,$remainder) = split(/:/,$item,4);
                   3103:                 if ($remainder =~ /:/) {
                   3104:                     (my $other,$group) = ($remainder =~ /^([\w:]+):([^:]+)$/);
                   3105:                 } else {
                   3106:                     $group = $remainder;
                   3107:                 }
                   3108:                 if ($group ne '') {
                   3109:                     push(@activegroups,$group);
                   3110:                 }
                   3111:             }
                   3112:             $newenv{'request.course.groups'} = join(':',@activegroups);
                   3113:         }
                   3114:     }
                   3115:     if (keys(%newenv)) {
                   3116:         &Apache::lonnet::appenv(\%newenv);
                   3117:     }
1.260     raeburn  3118:     if (!@changed_roles || !(keys(%changed_groups))) {
1.264     raeburn  3119:         my ($rolesmsg,$groupsmsg);
1.260     raeburn  3120:         if (!@changed_roles) {
                   3121:             if (&Apache::loncommon::show_course()) {
1.264     raeburn  3122:                 $rolesmsg = &mt('No new courses or communities');
1.260     raeburn  3123:             } else {
1.264     raeburn  3124:                 $rolesmsg = &mt('No role changes');
1.260     raeburn  3125:             }
                   3126:         }
                   3127:         if ($hasgroups && !(keys(%changed_groups)) && !(grep(/gr/,@changed_roles))) {
1.264     raeburn  3128:             $groupsmsg = &mt('No changes in course/community groups');
1.260     raeburn  3129:         }
                   3130:         if (!@changed_roles && !(keys(%changed_groups))) {
1.264     raeburn  3131:             if (($msg ne '') || ($groupsmsg ne '')) {
                   3132:                 $msg .= '<ul>';
                   3133:                 if ($rolesmsg) {
                   3134:                     $msg .= '<li>'.$rolesmsg.'</li>';
                   3135:                 }
                   3136:                 if ($groupsmsg) {
                   3137:                     $msg .= '<li>'.$groupsmsg.'</li>';
                   3138:                 }
                   3139:                 $msg .= '</ul>';
                   3140:             } else {
1.270     raeburn  3141:                 $msg = '&nbsp;<span class="LC_cusr_emph">'.$rolesmsg.'</span><br />';
1.264     raeburn  3142:             }
1.260     raeburn  3143:             return $msg;
                   3144:         }
                   3145:     }
                   3146:     my $changemsg;
                   3147:     if (@changed_roles > 0) {
                   3148:         if (keys(%newgroup) > 0) {
                   3149:             my $groupmsg;
1.279     raeburn  3150:             my (%curr_groups,%groupdescs,$currcrs);
1.260     raeburn  3151:             foreach my $item (sort(keys(%newgroup))) {
                   3152:                 if (&is_active_course($item,$refresh,$update,\%roleshash)) {
1.279     raeburn  3153:                     if ($item =~ m{^gr\./($match_domain/$match_courseid)/(\w+)$}) {
                   3154:                         my ($cdom,$cnum) = split(/\//,$1);
                   3155:                         my $group = $2;
                   3156:                         if ($currcrs ne $cdom.'_'.$cnum) {
                   3157:                             if ($currcrs) {
                   3158:                                 $groupmsg .= '</ul><li>';
                   3159:                             }
                   3160:                             $groupmsg .= '<li><b>'.
                   3161:                                          $env{'course.'.$cdom.'_'.$cnum.'.description'}.'</b><ul>';
1.281     raeburn  3162:                             $currcrs = $cdom.'_'.$cnum;
1.279     raeburn  3163:                         }
                   3164:                         my $groupdesc;
                   3165:                         unless (ref($curr_groups{$cdom.'_'.$cnum}) eq 'HASH') {
                   3166:                             %{$curr_groups{$cdom.'_'.$cnum}} = 
                   3167:                                 &Apache::longroup::coursegroups($cdom,$cnum);
                   3168:                         }
                   3169:                         unless ((ref($groupdescs{$cdom.'_'.$cnum}) eq 'HASH') &&
                   3170:                             ($groupdescs{$cdom.'_'.$cnum}{$group})) {
                   3171: 
                   3172:                             my %groupinfo = 
                   3173:                                 &Apache::longroup::get_group_settings($curr_groups{$cdom.'_'.$cnum}{$group});
                   3174:                             $groupdescs{$cdom.'_'.$cnum}{$group} = 
                   3175:                                 &unescape($groupinfo{'description'});
                   3176:                         }
                   3177:                         $groupdesc = $groupdescs{$cdom.'_'.$cnum}{$group};
1.286     raeburn  3178:                         if ($groupdesc) {
                   3179:                             $groupmsg .= '<li>'.
                   3180:                                          &mt('[_1] with status: [_2].',
                   3181:                                          '<b>'.$groupdesc.'</b>',$newgroup{$item}).'</li>';
                   3182:                         }
1.279     raeburn  3183:                     }
                   3184:                 }
                   3185:                 if ($groupmsg) {
                   3186:                     $groupmsg .= '</ul></li>';
1.260     raeburn  3187:                 }
                   3188:             }
                   3189:             if ($groupmsg) {
                   3190:                 $changemsg .= '<li>'.
                   3191:                               &mt('Courses with new groups').'</li>'.
                   3192:                               '<ul>'.$groupmsg.'</ul></li>';
                   3193:             }
                   3194:         }
                   3195:         if (keys(%newrole) > 0) {
1.286     raeburn  3196:             my $newmsg;
1.260     raeburn  3197:             foreach my $item (sort(keys(%newrole))) {
1.279     raeburn  3198:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  3199:                 if ($desc) {
                   3200:                     $newmsg .= '<li>'.
                   3201:                                &mt('[_1] with status: [_2].',
1.293     bisitz   3202:                                $desc,&mt($newrole{$item})).'</li>';
1.286     raeburn  3203:                 }
                   3204:             }
                   3205:             if ($newmsg) {
                   3206:                 $changemsg .= '<li>'.&mt('New roles').
                   3207:                               '<ul>'.$newmsg.'</ul>'.
                   3208:                               '</li>';
1.260     raeburn  3209:             }
                   3210:         }
                   3211:         if (keys(%customprivchg) > 0) {
1.286     raeburn  3212:             my $privmsg;
1.260     raeburn  3213:             foreach my $item (sort(keys(%customprivchg))) {
1.279     raeburn  3214:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  3215:                 if ($desc) {
                   3216:                     $privmsg .= '<li>'.$desc.'</li>';
                   3217:                 }
1.260     raeburn  3218:             }
1.286     raeburn  3219:             if ($privmsg) {
                   3220:                 $changemsg .= '<li>'.
                   3221:                               &mt('Custom roles with privilege changes').
                   3222:                               '<ul>'.$privmsg.'</ul>'.
                   3223:                               '</li>';
                   3224:              }
1.260     raeburn  3225:         }
                   3226:         if (keys(%rolechange) > 0) {
1.286     raeburn  3227:             my $rolemsg;
1.260     raeburn  3228:             foreach my $item (sort(keys(%rolechange))) {
1.279     raeburn  3229:                 my $desc = &role_desc($item,$update,$refresh,$now);  
1.286     raeburn  3230:                 if ($desc) {
                   3231:                     $rolemsg .= '<li>'.
                   3232:                                 &mt('[_1] status now: [_2].',$desc,
                   3233:                                 $rolechange{$item}).'</li>';
                   3234:                 }
                   3235:             }
                   3236:             if ($rolemsg) {
1.260     raeburn  3237:                 $changemsg .= '<li>'.
1.286     raeburn  3238:                               &mt('Existing roles with status changes').'</li>'.
                   3239:                               '<ul>'.$rolemsg.'</ul>'.
                   3240:                               '</li>';
1.260     raeburn  3241:             }
                   3242:         }
                   3243:         if (keys(%deletedroles) > 0) {
1.286     raeburn  3244:             my $delmsg;
1.260     raeburn  3245:             foreach my $item (sort(keys(%deletedroles))) {
1.279     raeburn  3246:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  3247:                 if ($desc) {
                   3248:                     $delmsg .= '<li>'.$desc.'</li>';
                   3249:                 }
                   3250:             }
                   3251:             if ($delmsg) {
                   3252:                 $changemsg .= '<li>'.
                   3253:                               &mt('Existing roles now expired').'</li>'.
                   3254:                               '<ul>'.$delmsg.'</ul>'.
                   3255:                               '</li>';
1.260     raeburn  3256:             }
                   3257:         }
                   3258:     }
                   3259:     if ((keys(%changed_groups) > 0) || (keys(%groupchange) > 0)) {
                   3260:         my $groupchgmsg;
                   3261:         foreach my $key (sort(keys(%changed_groups))) {
                   3262:             my $crs = 'gr/'.$key;
                   3263:             $crs =~ s/_/\//;
                   3264:             if (&is_active_course($crs,$refresh,$update,\%roleshash)) {
                   3265:                 if (ref($changed_groups{$key}) eq 'HASH') {
                   3266:                     my @showgroups;
                   3267:                     foreach my $group (sort(keys(%{$changed_groups{$key}}))) {
                   3268:                         if ($changed_groups{$key}{$group} eq 'active') {
                   3269:                             push(@showgroups,$group);
                   3270:                         }
                   3271:                     }
                   3272:                     if (@showgroups > 0) {
                   3273:                         $groupchgmsg .= '<li>'.
                   3274:                                         &mt('Course: [_1], groups: [_2].',$key,
                   3275:                                         join(', ',@showgroups)).
                   3276:                                         '</li>';
                   3277:                     }
                   3278:                 }
                   3279:             }
                   3280:         }
                   3281:         if (keys(%groupchange) > 0) {
                   3282:             $groupchgmsg .= '<li>'.
                   3283:                           &mt('Existing course/community groups with status changes').'</li>'.
                   3284:                           '<ul>';
                   3285:             foreach my $crs (sort(keys(%groupchange))) {
1.279     raeburn  3286:                 my $cid = $crs;
                   3287:                 $cid=~s{^/}{};
                   3288:                 $cid=~s{/}{_};
                   3289:                 my $crsdesc = $env{'course.'.$cid.'.description'};
                   3290:                 my $cdom = $env{'course.'.$cid.'.domain'};
                   3291:                 my $cnum = $env{'course.'.$cid.'.num'};
                   3292:                 my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   3293:                 my %groupdesc; 
1.260     raeburn  3294:                 if (ref($groupchange{$crs}) eq 'HASH') {
1.279     raeburn  3295:                     $groupchgmsg .= '<li>'.&mt('Course/Community: [_1]','<b>'.$crsdesc.'</b><ul>');
1.260     raeburn  3296:                     foreach my $group (sort(keys(%{$groupchange{$crs}}))) {
1.279     raeburn  3297:                         unless ($groupdesc{$group}) {
                   3298:                             my %groupinfo = &Apache::longroup::get_group_settings($curr_groups{$group});
                   3299:                             $groupdesc{$group} =  &unescape($groupinfo{'description'});
                   3300:                         }
                   3301:                         $groupchgmsg .= '<li>'.&mt('Group: [_1] status now: [_2].','<b>'.$groupdesc{$group}.'</b>',$groupchange{$crs}{$group}).'</li>';
1.260     raeburn  3302:                     }
                   3303:                     $groupchgmsg .= '</ul></li>';
                   3304:                 }
                   3305:             }
                   3306:             $groupchgmsg .= '</ul></li>';
                   3307:         }
                   3308:         if ($groupchgmsg) {
                   3309:             $changemsg .= '<li>'.
                   3310:                           &mt('Courses with changes in groups').'</li>'.
                   3311:                           '<ul>'.$groupchgmsg.'</ul></li>';
                   3312:         }
                   3313:     }
                   3314:     if ($changemsg) {
                   3315:         $msg .= '<ul>'.$changemsg.'</ul>';
1.286     raeburn  3316:     } else {
                   3317:         if (&Apache::loncommon::show_course()) {
                   3318:             $msg = &mt('No new courses or communities');
                   3319:         } else {
                   3320:             $msg = &mt('No role changes');
                   3321:         }
1.260     raeburn  3322:     }
1.279     raeburn  3323:     return $msg;
                   3324: }
                   3325: 
                   3326: sub role_desc {
                   3327:     my ($item,$update,$refresh,$now) = @_;
                   3328:     my ($where,$trolecode,$role,$tstatus,$tend,$tstart,$twhere,
                   3329:         $trole,$tremark);
1.282     raeburn  3330:     &Apache::lonnet::role_status('user.role.'.$item,$update,$refresh,
                   3331:                                  $now,\$role,\$where,\$trolecode,
1.279     raeburn  3332:                                  \$tstatus,\$tstart,\$tend);
1.286     raeburn  3333:     return unless ($role);
1.279     raeburn  3334:     if ($role =~ /^cr\//) {
                   3335:         my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.298     bisitz   3336:         $tremark = &mt('Custom role defined by [_1].',$rauthor.':'.$rdomain);
1.279     raeburn  3337:     }
                   3338:     $trole=Apache::lonnet::plaintext($role);
                   3339:     my ($tdom,$trest,$tsection)=
                   3340:         split(/\//,Apache::lonnet::declutter($where));
                   3341:     if (($role eq 'ca') || ($role eq 'aa')) {
                   3342:         my $home = &Apache::lonnet::homeserver($trest,$tdom);
                   3343:         $home = &Apache::lonnet::hostname($home);
                   3344:         $twhere=&mt('User').':&nbsp;'.$trest.'&nbsp; '.&mt('Domain').
                   3345:                 ':&nbsp;'.$tdom.'&nbsp; '.&mt('Server').':&nbsp;'.$home;
                   3346:     } elsif ($role eq 'au') {
                   3347:         my $home = &Apache::lonnet::homeserver
                   3348:                        ($env{'user.name'},$env{'user.domain'});
                   3349:         $home = &Apache::lonnet::hostname($home);
                   3350:         $twhere=&mt('Domain').':&nbsp;'.$tdom.'&nbsp; '.&mt('Server').
                   3351:                         ':&nbsp;'.$home;
                   3352:     } elsif ($trest) {
                   3353:         my $tcourseid=$tdom.'_'.$trest;
                   3354:         my $crstype = &Apache::loncommon::course_type($tcourseid);
                   3355:         $trole = &Apache::lonnet::plaintext($role,$crstype,$tcourseid);
                   3356:         if ($env{'course.'.$tcourseid.'.description'}) {
                   3357:             $twhere=$env{'course.'.$tcourseid.'.description'};
                   3358:         } else {
                   3359:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   3360:             if (%newhash) {
                   3361:                 $twhere=$newhash{'description'};
                   3362:             } else {
                   3363:                 $twhere=&mt('Currently not available');
1.260     raeburn  3364:             }
                   3365:         }
1.279     raeburn  3366:         if ($tsection) {
                   3367:             $twhere.= '&nbsp; '.&mt('Section').':&nbsp;'.$tsection;
1.260     raeburn  3368:         }
1.279     raeburn  3369:         if ($role ne 'st') {
                   3370:             $twhere.= '&nbsp; '.&mt('Domain').':&nbsp;'.$tdom;
1.260     raeburn  3371:         }
1.279     raeburn  3372:     } elsif ($tdom) {
                   3373:         $twhere = &mt('Domain').':&nbsp;'.$tdom;
1.260     raeburn  3374:     }
1.286     raeburn  3375:     my $output;
                   3376:     if ($trole) {
                   3377:         $output = $trole;
                   3378:         if ($twhere) {
                   3379:             $output .= " -- $twhere";
                   3380:         }
                   3381:         if ($tremark) {
                   3382:             $output .= '<br />'.$tremark;
                   3383:         }
1.260     raeburn  3384:     }
1.279     raeburn  3385:     return $output;
1.260     raeburn  3386: }
                   3387: 
                   3388: sub curr_role_status {
                   3389:     my ($start,$end,$refresh,$update) = @_;
                   3390:     if (($start) && ($start<0)) { return 'deleted' };
                   3391:     my $status = 'active';
                   3392:     if (($end) && ($end<=$update)) {
                   3393:         $status = 'previous';
                   3394:     }
                   3395:     if (($start) && ($refresh<$start)) {
                   3396:         $status = 'future';
                   3397:     }
                   3398:     return $status;
                   3399: }
                   3400: 
                   3401: sub gather_roleprivs {
                   3402:     my ($allroles,$allgroups,$userroles,$area,$role,$tstart,$tend,$status) = @_;
                   3403:     return unless ((ref($allroles) eq 'HASH') && (ref($allgroups) eq 'HASH') && (ref($userroles) eq 'HASH'));
                   3404:     if (($area ne '') && ($role ne '')) {
                   3405:         &Apache::lonnet::userrolelog($role,$env{'user.name'},$env{'user.domain'},
                   3406:                                      $area,$tstart,$tend);
                   3407:         my $spec=$role.'.'.$area;
                   3408:         $userroles->{'user.role.'.$spec} = $tstart.'.'.$tend;
                   3409:         my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   3410:         if ($status eq 'active') { 
                   3411:             if ($role =~ /^cr\//) {
                   3412:                 &Apache::lonnet::custom_roleprivs($allroles,$role,$tdomain,$trest,$spec,$area);
                   3413:             } elsif ($role eq 'gr') {
                   3414:                 my %rolehash = &Apache::lonnet::get('roles',[$area.'_'.$role],
                   3415:                                                     $env{'user.domain'},
                   3416:                                                     $env{'user.name'});
                   3417:                 my ($trole) = split(/_/,$rolehash{$area.'_'.$role},2);
                   3418:                 (undef,my $group_privs) = split(/\//,$trole);
                   3419:                 $group_privs = &unescape($group_privs);
                   3420:                 &Apache::lonnet::group_roleprivs($allgroups,$area,$group_privs,$tend,$tstart);
                   3421:             } else {
                   3422:                 &Apache::lonnet::standard_roleprivs($allroles,$role,$tdomain,$spec,$trest,$area);
                   3423:             }
                   3424:         }
                   3425:     }
                   3426:     return;
                   3427: }
                   3428: 
                   3429: sub is_active_course {
                   3430:     my ($rolekey,$refresh,$update,$roleshashref) = @_;
                   3431:     return unless(ref($roleshashref) eq 'HASH');
                   3432:     my ($role,$cdom,$cnum) = split(/\//,$rolekey);
                   3433:     my $is_active;
                   3434:     foreach my $key (keys(%{$roleshashref})) {
                   3435:         if ($key =~ /^\Q$cnum\E:\Q$cdom\E:/) {
                   3436:             my ($tstart,$tend) = split(/:/,$roleshashref->{$key});
                   3437:             my $status = &curr_role_status($tstart,$tend,$refresh,$update);
                   3438:             if ($status eq 'active') {
                   3439:                 $is_active = 1;
                   3440:                 last;
                   3441:             }
                   3442:         }
                   3443:     }
                   3444:     return $is_active;
                   3445: }
                   3446: 
1.274     raeburn  3447: sub get_roles_functions {
1.370     raeburn  3448:     my ($rolescount,$cattype,$userapprovals) = @_;
1.274     raeburn  3449:     my @links;
                   3450:     push(@links,["javascript:rolesView('doupdate');",'start-here-22x22',&mt('Check for changes')]);
                   3451:     if ($env{'environment.canrequest.author'}) {
                   3452:         unless (&Apache::loncoursequeueadmin::is_active_author()) {
                   3453:             push(@links,["javascript:rolesView('requestauthor');",'list-add-22x22',&mt('Request author role')]);
                   3454:         }
                   3455:     }
1.279     raeburn  3456:     if (($rolescount > 3) || ($env{'environment.recentroles'})) {
                   3457:         push(@links,['/adm/preferences?action=changerolespref&amp;returnurl=/adm/roles','role_hotlist-22x22',&mt('Hotlist')]);
                   3458:     }
1.274     raeburn  3459:     if (&Apache::lonmenu::check_for_rcrs()) {
                   3460:         push(@links,['/adm/requestcourse','rcrs-22x22',&mt('Request course')]);
                   3461:     }
                   3462:     if ($env{'form.state'} eq 'queued') {
                   3463:         push(@links,["javascript:rolesView('noqueued');",'selfenrl-queue-22x22',&mt('Hide queued')]);
                   3464:     } else {
                   3465:         push(@links,["javascript:rolesView('queued');",'selfenrl-queue-22x22',&mt('Show queued')]);
                   3466:     }
1.279     raeburn  3467:     if ($env{'user.adv'}) {
                   3468:         if ($env{'form.display'} eq 'showall') {
1.290     raeburn  3469:             push(@links,["javascript:rolesView('noshowall');",'edit-redo-22x22',&mt('Exclude expired')]);
1.279     raeburn  3470:         } else {
1.290     raeburn  3471:             push(@links,["javascript:rolesView('showall');",'edit-undo-22x22',&mt('Include expired')]);
1.279     raeburn  3472:         }
1.274     raeburn  3473:     }
1.302     raeburn  3474:     unless ($cattype eq 'none') {
1.291     raeburn  3475:         push(@links,['/adm/coursecatalog','ccat-22x22',&mt('Course catalog')]);
1.290     raeburn  3476:     }
1.370     raeburn  3477:     if ($userapprovals) {
                   3478:         if ($env{'form.approvals'} eq 'show') {
                   3479:             push(@links,["javascript:rolesView('noapprovals');",'list-add-22x22',&mt('Hide pending')]);
                   3480:         } else {
                   3481:             push(@links,["javascript:rolesView('approvals');",'list-add-22x22',&mt('Show pending')]);
                   3482:         }
                   3483:     }
1.314     raeburn  3484:     my $funcs;
                   3485:     if ($env{'browser.mobile'}) {
                   3486:         my @functions;
                   3487:         foreach my $link (@links) {
                   3488:             push(@functions,[$link->[0],$link->[2]]);
                   3489:         }
                   3490:         my $title = 'Display options';
                   3491:         if ($env{'user.adv'}) {
                   3492:             $title = 'Roles options';
                   3493:         }
                   3494:         $funcs = &Apache::lonmenu::create_submenu('','',$title,\@functions,1,'LC_breadcrumbs_hoverable');
                   3495:         $funcs = '<ol class="LC_primary_menu LC_floatright">'.$funcs.'</ol>';
                   3496:     } else {
                   3497:         $funcs = &Apache::lonhtmlcommon::start_funclist();
                   3498:         foreach my $link (@links) {
                   3499:             $funcs .= &Apache::lonhtmlcommon::add_item_funclist(
                   3500:                           '<a href="'.$link->[0].'" class="LC_menubuttons_link">'.
1.376     raeburn  3501:                           '<img src="/res/adm/pages/'.$link->[1].'.png" class="LC_icon" alt="'.$link->[2].' '.&mt('icon').'" aria-hidden="true" />'.
1.314     raeburn  3502:                           $link->[2].'</a>');
                   3503:         }
                   3504:         $funcs .= &Apache::lonhtmlcommon::end_funclist();
                   3505:         $funcs = &Apache::loncommon::head_subbox($funcs);
1.274     raeburn  3506:     }
1.314     raeburn  3507:     return $funcs;
1.274     raeburn  3508: }
                   3509: 
                   3510: sub get_queued {
                   3511:     my ($output,%reqcrs);
                   3512:     my ($types,$typenames) = &Apache::loncommon::course_types();
                   3513:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
                   3514:                                            $env{'user.name'},'^status:');
                   3515:     foreach my $key (keys(%statusinfo)) {
                   3516:         next unless (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending'));
1.297     raeburn  3517:         (undef,my($cdom,$cnum)) = split(/:/,$key);
1.274     raeburn  3518:         my $requestkey = $cdom.'_'.$cnum;
                   3519:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   3520:             my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   3521:                                                    $env{'user.domain'},$env{'user.name'});
                   3522:             next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
                   3523:             my $reqtime = $history{'reqtime'};
                   3524:             my $lastupdate = $history{'timestamp'};
                   3525:             my $showtype = $history{'crstype'};
                   3526:             if (defined($typenames->{$history{'crstype'}})) {
                   3527:                 $showtype = $typenames->{$history{'crstype'}};
                   3528:             }
                   3529:             my $description;
                   3530:             if (ref($history{'details'}) eq 'HASH') {
                   3531:                 $description = $history{details}{'cdescr'};
                   3532:             }
                   3533:             @{$reqcrs{$reqtime}} = ($description,$showtype); 
                   3534:         }
                   3535:     }
                   3536:     my @sortedtimes = sort {$a <=> $b} (keys(%reqcrs));
                   3537:     if (@sortedtimes > 0) {
                   3538:         $output .= '<p><b>'.&mt('Course/Community requests').'</b><br />'.
                   3539:                    &Apache::loncommon::start_data_table().
                   3540:                    &Apache::loncommon::start_data_table_header_row().
                   3541:                    '<th>'.&mt('Date requested').'</th>'.
                   3542:                    '<th>'.&mt('Course title').'</th>'.
                   3543:                    '<th>'.&mt('Course type').'</th>';
                   3544:                    &Apache::loncommon::end_data_table_header_row();
                   3545:         foreach my $reqtime (@sortedtimes) {
                   3546:             next unless (ref($reqcrs{$reqtime}) eq 'ARRAY');
                   3547:             $output .= &Apache::loncommon::start_data_table_row().
                   3548:                        '<td>'.&Apache::lonlocal::locallocaltime($reqtime).'</td>'.
                   3549:                        '<td>'.join('</td><td>',@{$reqcrs{$reqtime}}).'</td>'.
                   3550:                        &Apache::loncommon::end_data_table_row();
                   3551:         }
                   3552:         $output .= &Apache::loncommon::end_data_table().
                   3553:                    '<br /></p>';
                   3554:     }
                   3555:     my $queuedselfenroll = &Apache::loncoursequeueadmin::queued_selfenrollment(1);
                   3556:     if ($queuedselfenroll) {
                   3557:         $output .= '<p><b>'.&mt('Enrollment requests').'</b><br />'.
                   3558:                    $queuedselfenroll.'<br /></p>';
                   3559:     }
                   3560:     if ($env{'environment.canrequest.author'}) {
                   3561:         unless (&Apache::loncoursequeueadmin::is_active_author()) {
                   3562:             my $requestauthor;
                   3563:             my ($status,$timestamp) = split(/:/,$env{'environment.requestauthorqueued'});
                   3564:             if (($status eq 'approval') || ($status eq 'approved')) {
                   3565:                 $output .= '<p><b>'.&mt('Author role request').'</b><br />';
                   3566:                 if ($status eq 'approval') {
1.294     bisitz   3567:                     $output .= &mt('A request for Authoring Space submitted on [_1] is awaiting approval',
1.274     raeburn  3568:                                   &Apache::lonlocal::locallocaltime($timestamp));
                   3569:                 } elsif ($status eq 'approved') {
                   3570:                     my %roleshash =
                   3571:                         &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
                   3572:                                                       ['active'],['au'],[$env{'user.domain'}]);
                   3573:                     if (keys(%roleshash)) {
                   3574:                         $output .= '<span class="LC_info">'.
                   3575:                                    &mt('Your request for an author role has been approved.').'<br />'.
                   3576:                                    &mt('Use the "Check for changes" link to update your list of roles.').
                   3577:                                    '</span>';
                   3578:                     }
                   3579:                 }
                   3580:                 $output .= '</p>';
                   3581:             }
                   3582:         }
                   3583:     }
                   3584:     unless ($output) {
                   3585:         if ($env{'environment.canrequest.author'} || $env{'environment.canrequest.official'} ||
                   3586:             $env{'environment.canrequest.unofficial'} || $env{'environment.canrequest.community'}) {
1.370     raeburn  3587:             $output = '<span class="LC_info">'.
                   3588:                       &mt('No requests for courses, communities or authoring currently queued').
                   3589:                       '</span>';
1.274     raeburn  3590:         } else {
1.370     raeburn  3591:             $output = '<span class="LC_info">'.
                   3592:                       &mt('No enrollment requests currently queued awaiting approval').
                   3593:                       '</span>';
1.274     raeburn  3594:         }
                   3595:     }
                   3596:     return '<div class="LC_left_float"><fieldset><legend>'.&mt('Queued requests').'</legend>'.
                   3597:            $output.'</fieldset></div><br clear="all" />';
                   3598: }
                   3599: 
1.370     raeburn  3600: sub get_approvals {
                   3601:     return &Apache::loncoursequeueadmin::display_queued_requests('othdomaction',$env{'user.domain'},'','user');
                   3602: }
                   3603: 
1.338     raeburn  3604: sub process_lti {
                   3605:     my ($r,$cdom,$cnum) = @_;
                   3606:     my %lti = &Apache::lonnet::get_domain_lti($cdom,'provider');
                   3607:     my $uriscope = &LONCAPA::ltiutils::lti_provider_scope($env{'request.lti.uri'},
                   3608:                                                           $cdom,$cnum);
                   3609:     my $lonhost = $r->dir_config('lonHostID');
                   3610:     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
                   3611:     if ($env{'request.lti.rosterid'} &&
                   3612:         $env{'request.lti.rosterurl'}) {
                   3613:         if (ref($lti{$env{'request.lti.login'}}) eq 'HASH') {
                   3614:             if ($lti{$env{'request.lti.login'}}{'roster'}) {
                   3615:                 my @lcroles = ('in','ta','ep','st');
                   3616:                 my @possibleroles;
                   3617:                 foreach my $role (@lcroles) {
                   3618:                     if (&Apache::lonnet::allowed('c'.$role,"$cdom/$cnum")) {
                   3619:                         push(@possibleroles,$role);
                   3620:                     }
                   3621:                 }
                   3622:                 my $owner = $env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'};
                   3623:                 if ($owner eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   3624:                     my $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                   3625:                     if ($crstype eq 'Community') {
                   3626:                         unshift(@possibleroles,'co');
                   3627:                     } else {
                   3628:                         unshift(@possibleroles,'cc');
                   3629:                     }
                   3630:                 }
                   3631:                 if (@possibleroles) {
                   3632:                     push(@{$rosterupdates},{cid        => $cdom.'_'.$cnum,
                   3633:                                             lti        => $env{'request.lti.login'},
                   3634:                                             ltiref     => $lti{$env{'request.lti.login'}},
                   3635:                                             id         => $env{'request.lti.rosterid'},
                   3636:                                             url        => $env{'request.lti.rosterurl'},
                   3637:                                             sourcecrs  => $env{'request.lti.sourcecrs'},
                   3638:                                             uriscope   => $uriscope,
                   3639:                                             possroles  => \@possibleroles,
                   3640:                                             intdoms    => $internet_names,
                   3641:                                            });
                   3642:                     unless ($registered_cleanup) {
                   3643:                         my $handlers = $r->get_handlers('PerlCleanupHandler');
                   3644:                         $r->set_handlers('PerlCleanupHandler' =>
                   3645:                                          [\&ltienroll,@{$handlers}]);
                   3646:                         $registered_cleanup=1;
                   3647:                     }
                   3648:                 }
                   3649:             }
                   3650:         }
                   3651:     }
                   3652:     if ($env{'request.lti.passbackid'} &&
                   3653:         $env{'request.lti.passbackurl'}) {
                   3654:         if (ref($lti{$env{'request.lti.login'}}) eq 'HASH') {
                   3655:             if ($lti{$env{'request.lti.login'}}{'passback'}) {
                   3656:                 my ($pbnum,$error) =
                   3657:                     &LONCAPA::ltiutils::store_passbackurl($env{'request.lti.login'},
                   3658:                                                           $env{'request.lti.passbackurl'},
                   3659:                                                           $cdom,$cnum);
                   3660:                 if ($pbnum eq '') {
                   3661:                     $pbnum = $env{'request.lti.passbackurl'};
                   3662:                 }
                   3663:                 &Apache::lonnet::put('nohist_'.$cdom.'_'.$cnum.'_passback',
                   3664:                                      {"$uriscope\0$env{'request.lti.sourcecrs'}\0$env{'request.lti.login'}" =>
                   3665:                                      "$pbnum\0$env{'request.lti.passbackid'}"});
                   3666:             }
                   3667:         }
                   3668:     }
                   3669:     return;
                   3670: }
                   3671: 
                   3672: sub ltienroll {
                   3673:     if (ref($rosterupdates) eq 'ARRAY') {
                   3674:         foreach my $item (@{$rosterupdates}) {
                   3675:             if (ref($item) eq 'HASH') {
                   3676:                 &LONCAPA::ltiutils::batchaddroster($item);
                   3677:             }
                   3678:         }
1.375     raeburn  3679:         $rosterupdates = []; 
1.338     raeburn  3680:     }
1.375     raeburn  3681:     return OK;
1.338     raeburn  3682: }
                   3683: 
1.363     raeburn  3684: sub set_deeplink_target {
                   3685:     my ($cnum,$cdom) = @_;
                   3686:     if (($cnum ne '') && ($cdom ne '')) {
                   3687:         my $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
                   3688:         if ($deeplink_symb ne '') {
                   3689:             my $deeplink;
                   3690:             if ($deeplink_symb =~ /\.(page|sequence)$/) {
                   3691:                 my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($deeplink_symb))[2]);
                   3692:                 my $navmap = Apache::lonnavmaps::navmap->new();
                   3693:                 if (ref($navmap)) {
                   3694:                     $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');
                   3695:                 }
                   3696:             } elsif ($deeplink_symb ne '') {
                   3697:                 $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$deeplink_symb);
                   3698:             }
                   3699:             if ($deeplink ne '') {
                   3700:                 my ($state,$others,$listed,$scope,$protect,$display,$target) = split(/,/,$deeplink);
                   3701:                 if ($target ne '') {
                   3702:                     &Apache::lonnet::appenv({'request.deeplink.target' => $target});
1.364     raeburn  3703:                 } elsif (exists($env{'request.deeplink.target'})) {
                   3704:                     &Apache::lonnet::delenv('request.deeplink.target');
1.363     raeburn  3705:                 }
                   3706:             }
                   3707:         }
                   3708:     }
                   3709:     return;
                   3710: }
                   3711: 
1.368     raeburn  3712: sub set_supplemental_access {
                   3713:     my ($cnum,$cdom) = @_;
1.369     raeburn  3714:     my ($supplemental,$refs_updated) = &Apache::loncommon::get_supplemental($cnum,$cdom);
1.368     raeburn  3715:     unless ($refs_updated) {
                   3716:         &Apache::loncommon::set_supp_httprefs($cnum,$cdom,$supplemental);
                   3717:     }
                   3718: }
                   3719: 
1.1       harris41 3720: 1;
                   3721: __END__
1.32      harris41 3722: 
                   3723: =head1 NAME
                   3724: 
                   3725: Apache::lonroles - User Roles Screen
                   3726: 
                   3727: =head1 SYNOPSIS
                   3728: 
                   3729: Invoked by /etc/httpd/conf/srm.conf:
                   3730: 
                   3731:  <Location /adm/roles>
                   3732:  PerlAccessHandler       Apache::lonacc
                   3733:  SetHandler perl-script
                   3734:  PerlHandler Apache::lonroles
                   3735:  ErrorDocument     403 /adm/login
                   3736:  ErrorDocument	  500 /adm/errorhandler
                   3737:  </Location>
1.64      bowersj2 3738: 
                   3739: =head1 OVERVIEW
                   3740: 
                   3741: =head2 Choosing Roles
                   3742: 
                   3743: C<lonroles> is a handler that allows a user to switch roles in
                   3744: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                   3745: default role that a user has before selecting a role, as widely as
                   3746: possible, but certain handlers for example need specification which
                   3747: course they should act on, etc. Both in this scenario, and when the
                   3748: handler determines via C<lonnet>'s C<&allowed> function that a certain
                   3749: action is not allowed, C<lonroles> is used as error handler. This
                   3750: allows the user to select another role which may have permission to do
1.246     droeschl 3751: what they were trying to do.
1.64      bowersj2 3752: 
                   3753: =begin latex
                   3754: 
                   3755: \begin{figure}
                   3756: \begin{center}
                   3757: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                   3758:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                   3759: \end{center}
                   3760: \end{figure}
                   3761: 
                   3762: =end latex
                   3763: 
                   3764: =head2 Role Initialization
                   3765: 
                   3766: 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 3767: 
                   3768: =head1 INTRODUCTION
                   3769: 
                   3770: This module enables a user to select what role he wishes to
                   3771: operate under (instructor, student, teaching assistant, course
                   3772: coordinator, etc).  These roles are pre-established by the actions
                   3773: of upper-level users.
                   3774: 
                   3775: This is part of the LearningOnline Network with CAPA project
                   3776: described at http://www.lon-capa.org.
                   3777: 
                   3778: =head1 HANDLER SUBROUTINE
                   3779: 
                   3780: This routine is called by Apache and mod_perl.
                   3781: 
                   3782: =over 4
                   3783: 
                   3784: =item *
                   3785: 
                   3786: Roles Initialization (yes/no)
                   3787: 
                   3788: =item *
                   3789: 
                   3790: Get Error Message from Environment
                   3791: 
                   3792: =item *
                   3793: 
                   3794: Who is this?
                   3795: 
                   3796: =item *
                   3797: 
                   3798: Generate Page Output
                   3799: 
                   3800: =item *
                   3801: 
                   3802: Choice or no choice
                   3803: 
                   3804: =item *
                   3805: 
                   3806: Table
                   3807: 
                   3808: =item *
                   3809: 
                   3810: Privileges
                   3811: 
                   3812: =back
                   3813: 
                   3814: =cut

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