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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.151   ! www         4: # $Id: lonroles.pm,v 1.150 2006/05/30 17:15:00 www 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.1       harris41   30: package Apache::lonroles;
                     31: 
                     32: use strict;
1.118     albertel   33: use Apache::lonnet;
1.7       www        34: use Apache::lonuserstate();
1.1       harris41   35: use Apache::Constants qw(:common);
1.2       www        36: use Apache::File();
1.26      www        37: use Apache::lonmenu;
1.29      albertel   38: use Apache::loncommon;
1.104     raeburn    39: use Apache::lonhtmlcommon;
1.57      www        40: use Apache::lonannounce;
1.72      www        41: use Apache::lonlocal;
1.151   ! www        42: use Apache::lonpageflip();
1.120     albertel   43: use GDBM_File;
1.149     www        44: use LONCAPA;
                     45:  
1.1       harris41   46: 
1.62      matthew    47: sub redirect_user {
1.95      albertel   48:     my ($r,$title,$url,$msg,$launch_nav) = @_;
1.62      matthew    49:     $msg = $title if (! defined($msg));
1.73      www        50:     &Apache::loncommon::content_type($r,'text/html');
1.62      matthew    51:     &Apache::loncommon::no_cache($r);
                     52:     $r->send_http_header;
                     53:     my $swinfo=&Apache::lonmenu::rawconfig();
1.96      albertel   54:     my $navwindow;
1.95      albertel   55:     if ($launch_nav eq 'on') {
1.128     albertel   56: 	$navwindow.=&Apache::lonnavmaps::launch_win('now',undef,undef,
1.140     raeburn    57: 						  ($url =~ m-^/adm/whatsnew-));
1.96      albertel   58:     } else {
                     59: 	$navwindow.=&Apache::lonnavmaps::close();
1.95      albertel   60:     }
1.147     albertel   61:     my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
                     62: 						    {'redirect' => [1,$url],});
                     63:     my $end_page   = &Apache::loncommon::end_page();
                     64: 
1.92      www        65: # Note to style police: 
                     66: # This must only replace the spaces, nothing else, or it bombs elsewhere.
                     67:     $url=~s/ /\%20/g;
1.93      albertel   68:     $r->print(<<ENDREDIR);
1.147     albertel   69: $start_page
1.96      albertel   70: <script type="text/javascript">
1.62      matthew    71: $swinfo
                     72: </script>
1.96      albertel   73: $navwindow
1.62      matthew    74: <h1>$msg</h1>
1.147     albertel   75: $end_page
1.62      matthew    76: ENDREDIR
                     77:     return;
                     78: }
                     79: 
1.150     www        80: sub error_page {
                     81:     my ($r,$error,$dest)=@_;
                     82:     &Apache::loncommon::content_type($r,'text/html');
                     83:     &Apache::loncommon::no_cache($r);
                     84:     $r->send_http_header;
                     85:     return OK if $r->header_only;
                     86:     $r->print(&Apache::loncommon::start_page('Problems during Course Initialization').
                     87: 	      '<script type="text/javascript">'.
                     88: 	      &Apache::lonmenu::rawconfig().'</script>'.
                     89: 	      '<p>'.&mt('The following problems occurred:').
                     90: 	      $error.
                     91: 	      '</p><br /><a href="'.$dest.'>'.&mt('Continue').'</a>'.
                     92: 	      &Apache::loncommon::end_page());
                     93: }
                     94: 
1.1       harris41   95: sub handler {
1.10      www        96: 
1.1       harris41   97:     my $r = shift;
                     98: 
1.6       www        99:     my $now=time;
1.118     albertel  100:     my $then=$env{'user.login.time'};
1.6       www       101:     my $envkey;
1.107     raeburn   102:     my %dcroles = ();
                    103:     my $numdc = &check_fordc(\%dcroles,$then);
1.148     albertel  104:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.10      www       105: 
1.6       www       106: # ================================================================== Roles Init
1.118     albertel  107:     if ($env{'form.selectrole'}) {
1.134     www       108:         if ($env{'form.newrole'}) {
                    109:             $env{'form.'.$env{'form.newrole'}}=1;
                    110: 	}
1.118     albertel  111: 	if ($env{'request.course.id'}) {
                    112: 	    my %temp=('logout_'.$env{'request.course.id'} => time);
1.33      www       113: 	    &Apache::lonnet::put('email_status',\%temp);
1.118     albertel  114: 	    &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
1.100     albertel  115: 	}
1.55      albertel  116: 	&Apache::lonnet::appenv("request.course.id"   => '',
                    117: 				"request.course.fn"   => '',
                    118: 				"request.course.uri"  => '',
                    119: 				"request.course.sec"  => '',
                    120: 				"request.role"        => 'cm',
1.118     albertel  121:                                 "request.role.adv"    => $env{'user.adv'},
                    122: 				"request.role.domain" => $env{'user.domain'});
1.106     raeburn   123: 
1.110     raeburn   124: # Check if user is a DC trying to enter a course and needs privs to be created
1.107     raeburn   125:         if ($numdc > 0) {
1.118     albertel  126:             foreach my $envkey (keys %env) {
1.146     raeburn   127:                 if (my ($domain,$coursenum) =
                    128:                                     ($envkey =~ m-^form\.cc\./(\w+)/(\w+)$-)) {
                    129:                     if ($dcroles{$domain}) {
                    130:                         &check_privs($domain,$coursenum,$then,$now);
1.107     raeburn   131:                     }
                    132:                     last;
                    133:                 }
                    134:             }
                    135:         }
                    136: 
1.118     albertel  137:         foreach $envkey (keys %env) {
1.40      matthew   138:             next if ($envkey!~/^user\.role\./);
1.102     raeburn   139:             my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
                    140:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.118     albertel  141:             if ($env{'form.'.$trolecode}) {
1.55      albertel  142: 		if ($tstatus eq 'is') {
                    143: 		    $where=~s/^\///;
                    144: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
1.137     raeburn   145: # check for course groups
                    146:                     my %coursegroups = &Apache::lonnet::get_active_groups(
                    147:                           $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
                    148:                     my $cgrps = join(':',keys(%coursegroups));
                    149: 
1.111     albertel  150: # store role if recent_role list being kept
1.118     albertel  151:                     if ($env{'environment.recentroles'}) {
1.111     albertel  152: 			&Apache::lonhtmlcommon::store_recent('roles',
                    153: 							     $trolecode,' ');
                    154:                     }
                    155: 
                    156: 
1.53      www       157: # check for keyed access
1.55      albertel  158: 		    if (($role eq 'st') && 
1.118     albertel  159:                        ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
1.89      www       160: # who is key authority?
                    161: 			my $authdom=$cdom;
                    162: 			my $authnum=$cnum;
1.118     albertel  163: 			if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
1.89      www       164: 			    ($authnum,$authdom)=
1.118     albertel  165: 				split(/\W/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
1.89      www       166: 			}
                    167: # check with key authority
                    168: 			unless (&Apache::lonnet::validate_access_key(
1.118     albertel  169: 				     $env{'environment.key.'.$cdom.'_'.$cnum},
1.89      www       170: 					     $authdom,$authnum)) {
1.53      www       171: # there is no valid key
1.118     albertel  172: 			     if ($env{'form.newkey'}) {
1.53      www       173: # student attempts to register a new key
1.89      www       174: 				 &Apache::loncommon::content_type($r,'text/html');
                    175: 				 &Apache::loncommon::no_cache($r);
                    176: 				 $r->send_http_header;
                    177: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  178: 				 my $start_page=&Apache::loncommon::start_page
1.89      www       179: 				    ('Verifying Access Key to Unlock this Course');
1.147     albertel  180: 				 my $end_page=&Apache::loncommon::end_page();
1.90      www       181: 				 my $buttontext=&mt('Enter Course');
                    182: 				 my $message=&mt('Successfully registered key');
                    183: 				 my $assignresult=
                    184: 				     &Apache::lonnet::assign_access_key(
1.118     albertel  185: 						     $env{'form.newkey'},
1.90      www       186: 						     $authdom,$authnum,
1.91      www       187: 						     $cdom,$cnum,
1.118     albertel  188:                                                      $env{'user.domain'},
                    189: 						     $env{'user.name'},
1.90      www       190: 	      'Assigned from '.$ENV{'REMOTE_ADDR'}.' at '.localtime().' for '.
                    191:                                                      $trolecode);
                    192: 				 unless ($assignresult eq 'ok') {
                    193: 				     $assignresult=~s/^error\:\s*//;
                    194: 				     $message=&mt($assignresult).
                    195: 				     '<br /><a href="/adm/logout">'.
1.89      www       196: 				     &mt('Logout').'</a>';
1.90      www       197: 				     $buttontext=&mt('Re-Enter Key');
                    198: 				 }
1.89      www       199: 				 $r->print(<<ENDENTEREDKEY);
1.147     albertel  200: $start_page
1.89      www       201: <script>
                    202: $swinfo
                    203: </script>
                    204: <form method="post">
                    205: <input type="hidden" name="selectrole" value="1" />
                    206: <input type="hidden" name="$trolecode" value="1" />
1.90      www       207: <font size="+2">$message</font><br />
1.89      www       208: <input type="submit" value="$buttontext" />
                    209: </form>
1.147     albertel  210: $end_page
1.89      www       211: ENDENTEREDKEY
                    212:                                  return OK;
1.55      albertel  213: 			     } else {
1.53      www       214: # print form to enter a new key
1.73      www       215: 				 &Apache::loncommon::content_type($r,'text/html');
1.55      albertel  216: 				 &Apache::loncommon::no_cache($r);
                    217: 				 $r->send_http_header;
                    218: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  219: 				 my $start_page=&Apache::loncommon::start_page
1.55      albertel  220: 				    ('Enter Access Key to Unlock this Course');
1.147     albertel  221: 				 my $end_page=&Apache::loncommon::end_page();
1.55      albertel  222: 				 $r->print(<<ENDENTERKEY);
1.147     albertel  223: $start_page
1.53      www       224: <script>
                    225: $swinfo
                    226: </script>
                    227: <form method="post">
1.89      www       228: <input type="hidden" name="selectrole" value="1" />
                    229: <input type="hidden" name="$trolecode" value="1" />
1.118     albertel  230: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
1.53      www       231: <input type="submit" value="Enter key" />
                    232: </form>
1.147     albertel  233: $end_page
1.53      www       234: ENDENTERKEY
1.55      albertel  235: 				 return OK;
                    236: 			     }
                    237: 			 }
                    238: 		     }
1.118     albertel  239: 		    &Apache::lonnet::log($env{'user.domain'},
                    240: 					 $env{'user.name'},
                    241: 					 $env{'user.home'},
1.87      www       242: 					 "Role ".$trolecode);
1.101     albertel  243: 		    
1.56      www       244: 		    &Apache::lonnet::appenv(
1.101     albertel  245: 					   'request.role'        => $trolecode,
1.56      www       246: 					   'request.role.domain' => $cdom,
1.137     raeburn   247: 					   'request.course.sec'  => $csec,
                    248:                                            'request.course.groups' => $cgrps);
1.101     albertel  249:                     my $tadv=0;
1.72      www       250: 		    my $msg=&mt('Entering course ...');
1.62      matthew   251: 
1.125     www       252: 		    if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
1.55      albertel  253: 			my ($furl,$ferr)=
                    254: 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
1.118     albertel  255: 			if (($env{'form.orgurl'}) && 
                    256: 			    ($env{'form.orgurl'}!~/^\/adm\/flip/)) {
                    257: 			    my $dest=$env{'form.orgurl'};
1.117     albertel  258: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
                    259: 			    &Apache::lonnet::appenv('request.role.adv'=>$tadv);
1.150     www       260:                             if (($ferr) && ($tadv)) {
                    261: 				&error_page($r,$ferr,$dest);
                    262: 			    } else {
                    263: 				$r->internal_redirect($dest);
                    264: 			    }
1.55      albertel  265: 			    return OK;
                    266: 			} else {
1.118     albertel  267: 			    unless ($env{'request.course.id'}) {
1.55      albertel  268: 				&Apache::lonnet::appenv(
                    269: 				      "request.course.id"  => $cdom.'_'.$cnum);
1.61      www       270: 				$furl='/adm/roles?tryagain=1';
1.55      albertel  271: 				$msg=
1.131     albertel  272: 				    '<h1><font color="red">'.
1.72      www       273: 			 &mt('Could not initialize course at this time.').
                    274: 		    '</font></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
1.55      albertel  275: 			    }
1.117     albertel  276: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
                    277: 			    &Apache::lonnet::appenv('request.role.adv'=>$tadv);
1.150     www       278: 			    if (($ferr) && ($tadv)) {
                    279: 				&error_page($r,$ferr,$furl);
                    280: 			    } else {
                    281: 				# Check to see if the user is a CC entering a course 
                    282: 				# for the first time
                    283: 				my (undef, undef, $role, $courseid) = split(/\./, $envkey);
                    284: 				if (substr($courseid, 0, 1) eq '/') {
                    285: 				    $courseid = substr($courseid, 1);
                    286: 				}
                    287: 				$courseid =~ s/\//_/;
                    288: 				if ($role eq 'cc' && $env{'course.' . $courseid . 
                    289: 							      '.course.helper.not.run'}) {
                    290: 				    $furl = "/adm/helper/course.initialization.helper";
                    291: 				    # Send the user to the course they selected
                    292: 				} elsif ($env{'request.course.id'}) {
                    293: 				    if (&Apache::lonnet::allowed('whn',
                    294: 								 $env{'request.course.id'})
                    295: 					|| &Apache::lonnet::allowed('whn',
                    296: 								    $env{'request.course.id'}.'/'
                    297: 								    .$env{'request.course.sec'})
                    298: 					) {
                    299: 					my $startpage = &courseloadpage($courseid);
                    300: 					unless ($startpage eq 'firstres') {         
                    301: 					    $msg = &mt('Entering course ....');
                    302: 					    &redirect_user($r,&mt('New in course'),
                    303: 							   '/adm/whatsnew?refpage=start',$msg,
                    304: 							   $env{'environment.remotenavmap'});
                    305: 					    return OK;
                    306: 					}
                    307: 				    }
                    308: 				}
1.151   ! www       309: # Are we allowed to look at the first resource?
        !           310: 				if (!&Apache::lonnet::allowed('bre',$furl)) {
        !           311: # Guess not ...
        !           312: 				    $furl=&Apache::lonpageflip::first_accessible_resource();
        !           313: 				}
1.150     www       314: 				&redirect_user($r,&mt('Entering Course'),
                    315: 					       $furl,$msg,
                    316: 					       $env{'environment.remotenavmap'});
1.58      bowersj2  317: 			    }
1.124     albertel  318: 			    return OK;
1.55      albertel  319: 			}
                    320: 		    }
1.62      matthew   321:                     #
                    322:                     # Send the user to the construction space they selected
1.125     www       323:                     if ($role =~ /^(au|ca|aa)$/) {
1.62      matthew   324:                         my $redirect_url = '/priv/';
                    325:                         if ($role eq 'au') {
1.118     albertel  326:                             $redirect_url.=$env{'user.name'};
1.62      matthew   327:                         } else {
                    328:                             $where =~ /\/(.*)$/;
                    329:                             $redirect_url .= $1;
                    330:                         }
                    331:                         $redirect_url .= '/';
1.78      sakharuk  332:                         &redirect_user($r,&mt('Entering Construction Space'),
1.62      matthew   333:                                        $redirect_url);
                    334:                         return OK;
                    335:                     }
1.104     raeburn   336:                     if ($role eq 'dc') {
1.108     raeburn   337:                         my $redirect_url = '/adm/menu/';
                    338:                         &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104     raeburn   339:                                        $redirect_url);
1.108     raeburn   340:                         return OK;
1.104     raeburn   341:                     }
1.55      albertel  342: 		}
                    343:             }
1.6       www       344:         }
1.40      matthew   345:     }
1.44      www       346: 
1.10      www       347: 
1.6       www       348: # =============================================================== No Roles Init
1.10      www       349: 
1.73      www       350:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel  351:     &Apache::loncommon::no_cache($r);
1.10      www       352:     $r->send_http_header;
                    353:     return OK if $r->header_only;
                    354: 
1.52      www       355:     my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  356:     my $start_page=&Apache::loncommon::start_page('User Roles');
1.134     www       357:     my $standby=&mt('Role selected. Please stand by.');
1.135     albertel  358:     $standby=~s/\n/\\n/g;
1.94      albertel  359:     my $helptag='<table><tr><td>'.&Apache::loncommon::help_open_menu('','General Intro','General_Intro','User Roles',1,undef,undef,undef,undef,,&mt("Click here for help")).'</td></td></tr></table>';
1.10      www       360:     $r->print(<<ENDHEADER);
1.147     albertel  361: $start_page
1.45      www       362: $helptag<br />
1.26      www       363: <script>
                    364: $swinfo
                    365: window.focus();
1.134     www       366: 
                    367: active=true;
                    368: 
                    369: function enterrole (thisform,rolecode,buttonname) {
                    370:     if (active) {
                    371: 	active=false;
                    372:         document.title='$standby';
                    373:         window.status='$standby';
                    374: 	thisform.newrole.value=rolecode;
                    375: 	thisform.submit();
                    376:     } else {
                    377:        alert('$standby');
                    378:     }   
                    379: }
1.26      www       380: </script>
1.10      www       381: ENDHEADER
1.6       www       382: 
1.2       www       383: # ------------------------------------------ Get Error Message from Environment
                    384: 
1.118     albertel  385:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
                    386:     if ($env{'user.error.msg'}) {
1.55      albertel  387: 	$r->log_reason(
1.118     albertel  388:    "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12      www       389:     }
1.1       harris41  390: 
1.61      www       391: # ------------------------------------------------- Can this user re-init, etc?
1.6       www       392: 
1.118     albertel  393:     my $advanced=$env{'user.adv'};
1.61      www       394:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118     albertel  395:     my $tryagain=$env{'form.tryagain'};
1.6       www       396: 
1.2       www       397: # -------------------------------------------------------- Generate Page Output
1.6       www       398: # --------------------------------------------------------------- Error Header?
1.2       www       399:     if ($error) {
                    400: 	$r->print("<h1>LON-CAPA Access Control</h1>");
1.126     www       401:         $r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>Access  : ".
1.4       www       402:                   Apache::lonnet::plaintext($priv)."\n");
1.115     albertel  403:         $r->print("Resource: ".&Apache::lonenc::check_encrypt($fn)."\n");
1.120     albertel  404:         $r->print("Action  : $msg\n</pre><hr />");
                    405: 	my $url=$fn;
                    406: 	my $last;
                    407: 	if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                    408: 		&GDBM_READER(),0640)) {
                    409: 	    $last=$hash{'last_known'};
                    410: 	    untie(%hash);
                    411: 	}
1.149     www       412: 	if ($last) { $fn.='?symb='.&escape($last); }
1.120     albertel  413: 
                    414: 	&Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
                    415: 					&Apache::lonenc::check_encrypt($fn));
1.2       www       416:     } else {
1.118     albertel  417:         if ($env{'user.error.msg'}) {
1.25      www       418: 	    $r->print(
1.131     albertel  419:  '<h3><font color="red">'.
1.72      www       420:  &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
1.25      www       421: 	}
1.2       www       422:     }
1.6       www       423: # -------------------------------------------------------- Choice or no choice?
1.2       www       424:     if ($nochoose) {
1.150     www       425: 	$r->print("<h2>".&mt('Sorry ...')."</h2>\n".
                    426: 		  &mt('This action is currently not authorized.').
                    427: 		  &Apache::loncommon::end_page());
                    428: 	return OK;
1.6       www       429:     } else {
                    430:         if ($advanced) {
1.72      www       431: 	    $r->print(&mt("Your home server is ").
1.55      albertel  432: 		      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
1.118     albertel  433:                       ($env{'user.name'},$env{'user.domain'})}.
1.55      albertel  434: 		      "<br />\n");
1.72      www       435: 	    $r->print(&mt(
1.126     www       436:       "Author and Co-Author roles are not available on servers other than their respective home servers."));
1.17      www       437:         }
1.18      www       438:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                    439:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www       440:         }
1.84      www       441:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116     albertel  442:         $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
                    443:         $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134     www       444:         $r->print('<input type="hidden" name="newrole" value="" />');
1.6       www       445:     }
1.118     albertel  446:     if ($env{'user.adv'}) {
1.63      www       447: 	$r->print(
1.116     albertel  448: 	      '<br /><label>'.&mt('Show all roles').': <input type="checkbox" name="showall"');
1.118     albertel  449: 	if ($env{'form.showall'}) { $r->print(' checked="checked" '); }
1.116     albertel  450: 	$r->print(' /></label><input type="submit" value="'.&mt('Display').'" />');
1.63      www       451:     }
1.4       www       452: 
1.75      albertel  453:     my (%roletext,%sortrole,%roleclass);
1.84      www       454:     my $countactive=0;
                    455:     my $inrole=0;
                    456:     my $possiblerole='';
1.118     albertel  457:     foreach $envkey (sort keys %env) {
1.35      matthew   458:         my $button = 1;
1.49      www       459:         my $switchserver='';
1.75      albertel  460: 	my $roletext;
                    461: 	my $sortkey;
1.2       www       462:         if ($envkey=~/^user\.role\./) {
1.102     raeburn   463:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
                    464:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.136     raeburn   465:             next if (!defined($role) || $role eq '' || $role =~ /^gr/);
1.102     raeburn   466:             $tremark='';
                    467:             $tpstart='&nbsp;';
                    468:             $tpend='&nbsp;';
                    469:             $tfont='#000000';
1.4       www       470:             if ($tstart) {
1.74      www       471:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
1.4       www       472:             }
                    473:             if ($tend) {
1.74      www       474:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
1.4       www       475:             }
1.118     albertel  476:             if ($env{'request.role'} eq $trolecode) {
1.6       www       477: 		$tstatus='selected';
                    478:             }
1.4       www       479:             my $tbg;
1.35      matthew   480:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
1.118     albertel  481:                 ($env{'form.showall'})) {
1.35      matthew   482:                 if ($tstatus eq 'is') {
                    483:                     $tbg='#77FF77';
1.47      www       484:                     $tfont='#003300';
1.84      www       485: 		    $possiblerole=$trolecode;
                    486: 		    $countactive++;
1.35      matthew   487:                 } elsif ($tstatus eq 'future') {
                    488:                     $tbg='#FFFF77';
1.49      www       489:                     $button=0;
1.35      matthew   490:                 } elsif ($tstatus eq 'will') {
                    491:                     $tbg='#FFAA77';
1.72      www       492:                     $tremark.=&mt('Active at next login. ');
1.35      matthew   493:                 } elsif ($tstatus eq 'expired') {
                    494:                     $tbg='#FF7777';
1.47      www       495:                     $tfont='#330000';
1.49      www       496:                     $button=0;
1.35      matthew   497:                 } elsif ($tstatus eq 'will_not') {
                    498:                     $tbg='#AAFF77';
1.72      www       499:                     $tremark.=&mt('Expired after logout. ');
1.35      matthew   500:                 } elsif ($tstatus eq 'selected') {
                    501:                     $tbg='#11CC55';
1.47      www       502:                     $tfont='#002200';
1.84      www       503: 		    $inrole=1;
1.86      albertel  504: 		    $countactive++;
1.72      www       505:                     $tremark.=&mt('Currently selected. ');
1.35      matthew   506:                 }
                    507:                 my $trole;
                    508:                 if ($role =~ /^cr\//) {
                    509:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.132     albertel  510: 		    if ($tremark) { $tremark.='<br />'; }
                    511:                     $tremark.=&mt('Defined by ').$rauthor.
1.72      www       512: 			&mt(' at ').$rdomain.'.';
1.35      matthew   513:                     $trole=$rrole;
1.8       www       514:                 } else {
1.35      matthew   515:                     $trole=Apache::lonnet::plaintext($role);
                    516:                 }
                    517:                 my $ttype;
                    518:                 my $twhere;
                    519:                 my ($tdom,$trest,$tsection)=
                    520:                     split(/\//,Apache::lonnet::declutter($where));
                    521:                 # First, Co-Authorship roles
1.125     www       522:                 if (($role eq 'ca') || ($role eq 'aa')) {
1.39      stredwic  523:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
1.83      albertel  524: 		    my $allowed=0;
                    525: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    526: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    527:                     if (!$allowed) {
1.49      www       528: 			$button=0;
1.130     albertel  529:                         $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49      www       530:                     }
1.35      matthew   531:                     #next if ($home eq 'no_host');
                    532:                     $home = $Apache::lonnet::hostname{$home};
1.78      sakharuk  533:                     $ttype='Construction Space';
1.72      www       534:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                    535: 			': '.$tdom.'<br />'.
                    536:                         ' '.&mt('Server').':&nbsp;'.$home;
1.118     albertel  537:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.82      www       538: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
1.75      albertel  539: 		    $sortkey=$role."$trest:$tdom";
1.35      matthew   540:                 } elsif ($role eq 'au') {
                    541:                     # Authors
                    542:                     my $home = &Apache::lonnet::homeserver
1.118     albertel  543:                         ($env{'user.name'},$env{'user.domain'});
1.83      albertel  544: 		    my $allowed=0;
                    545: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    546: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    547:                     if (!$allowed) {
1.49      www       548: 			$button=0;
1.143     albertel  549:                         $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49      www       550:                     }
1.35      matthew   551:                     #next if ($home eq 'no_host');
                    552:                     $home = $Apache::lonnet::hostname{$home};
1.78      sakharuk  553:                     $ttype='Construction Space';
1.72      www       554:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                    555: 			':&nbsp;'.$home;
1.118     albertel  556:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                    557: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
1.75      albertel  558: 		    $sortkey=$role;
1.35      matthew   559:                 } elsif ($trest) {
1.78      sakharuk  560:                     $ttype='Course';
1.35      matthew   561:                     my $tcourseid=$tdom.'_'.$trest;
1.118     albertel  562:                     if ($env{'course.'.$tcourseid.'.description'}) {
                    563:                         $twhere=$env{'course.'.$tcourseid.'.description'};
1.80      albertel  564: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.72      www       565:                         unless ($twhere eq &mt('Currently not available')) {
1.55      albertel  566: 			    $twhere.=' <font size="-2">'.
1.72      www       567:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       568:                                     '</font>';
1.55      albertel  569: 			}
1.8       www       570:                     } else {
1.105     raeburn   571:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1.35      matthew   572:                         if (%newhash) {
1.80      albertel  573: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
1.77      albertel  574: 				"\0".$envkey;
1.49      www       575:                             $twhere=$newhash{'description'}.
                    576:                               ' <font size="-2">'.
1.72      www       577:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       578:                               '</font>';
1.35      matthew   579:                         } else {
1.72      www       580:                             $twhere=&mt('Currently not available');
1.118     albertel  581:                             $env{'course.'.$tcourseid.'.description'}=$twhere;
1.80      albertel  582: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.35      matthew   583:                         }
1.8       www       584:                     }
1.121     albertel  585:                     if ($tsection) {
                    586:                         $twhere.='<br />'.&mt('Section/Group').': '.$tsection;
                    587: 		    }
                    588: 
1.72      www       589: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
1.35      matthew   590:                 } elsif ($tdom) {
1.78      sakharuk  591:                     $ttype='Domain';
1.35      matthew   592:                     $twhere=$tdom;
1.75      albertel  593: 		    $sortkey=$role.$twhere;
1.35      matthew   594:                 } else {
1.78      sakharuk  595:                     $ttype='System';
1.72      www       596:                     $twhere=&mt('system wide');
1.75      albertel  597: 		    $sortkey=$role.$twhere;
1.13      www       598:                 }
1.35      matthew   599:  
1.127     albertel  600:                 $roletext.=&build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver);
1.75      albertel  601: 		$roletext{$envkey}=$roletext;
                    602: 		if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                    603: 		$sortrole{$sortkey}=$envkey;
                    604: 		$roleclass{$envkey}=$ttype;
1.55      albertel  605: 	    }
1.4       www       606:         }
1.75      albertel  607:     }
1.84      www       608: # No active roles
                    609:     if ($countactive==0) {
                    610: 	if ($inrole) {
                    611: 	    $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
                    612: 	} else {
                    613: 	    $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
                    614: 	}
1.147     albertel  615: 	$r->print('</form>'.&Apache::loncommon::end_page());
1.84      www       616: 	return OK;
                    617: # Is there only one choice?
1.118     albertel  618:     } elsif (($countactive==1) && ($env{'request.role'} eq 'cm')) {
1.84      www       619: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>'.
                    620: 	    '<input type="hidden" name="'.$possiblerole.'" value="1" />');
                    621: 	$r->print("</form>\n");
                    622: 	$r->rflush();
                    623: 	$r->print('<script>document.forms.rolechoice.submit();</script>');
1.147     albertel  624: 	$r->print(&Apache::loncommon::end_page());
1.84      www       625: 	return OK;
                    626:     }
                    627: # More than one possible role
                    628: # ----------------------------------------------------------------------- Table
                    629:     unless (($advanced) || ($nochoose)) {
                    630: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
                    631:     }
                    632:     $r->print('<br /><table><tr>');
                    633:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
1.121     albertel  634:     $r->print('<th>'.&mt('User Role').'</th><th>'.&mt('Extent').
1.132     albertel  635:          '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th></tr>'."\n");
1.76      albertel  636:     my $doheaders=-1;
1.129     albertel  637:     foreach my $type ('Domain','Construction Space','Course','System') {
1.76      albertel  638: 	my $haverole=0;
1.75      albertel  639: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    640: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
1.76      albertel  641: 		$haverole=1;
1.75      albertel  642: 	    }
1.76      albertel  643: 	}
                    644: 	if ($haverole) { $doheaders++; }
                    645:     }
1.111     albertel  646: 
1.118     albertel  647:     if ($env{'environment.recentroles'}) {
1.111     albertel  648:         my %recent_roles =
1.118     albertel  649:                &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111     albertel  650: 	my $output='';
                    651: 	foreach (sort(keys(%recent_roles))) {
                    652: 	    if (defined($roletext{'user.role.'.$_})) {
                    653: 		$output.=$roletext{'user.role.'.$_};
1.133     albertel  654:                 if ($_ =~ m-dc\./(\w+)/- && $dcroles{$1}) {
1.138     raeburn   655: 		    $output .= &allcourses_row($1,'recent');
1.133     albertel  656:                 }
1.113     raeburn   657: 	    } elsif ($numdc > 0) {
                    658:                 unless ($_ =~/^error\:/) {
                    659:                     $output.=&display_cc_role('user.role.'.$_);
                    660:                 }
                    661:             } 
1.111     albertel  662: 	}
                    663: 	if ($output) {
1.132     albertel  664: 	    $r->print("<tr><td align='center' colspan='5'><font face='arial'>".
1.126     www       665: 		      &mt('Recent Roles')."</font></td>");
1.111     albertel  666: 	    $r->print($output);
                    667: 	    $r->print("</tr>");
1.114     raeburn   668:             $doheaders ++;
1.111     albertel  669: 	}
                    670:     }
                    671: 
1.104     raeburn   672:     if ($numdc > 0) {
1.112     raeburn   673:         $r->print(&coursepick_jscript());
                    674:         $r->print(&Apache::loncommon::coursebrowser_javascript());
1.108     raeburn   675:     }
1.132     albertel  676:     foreach my $type ('Construction Space','Domain','Course','System') {
1.108     raeburn   677: 	my $output;
                    678: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    679: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
                    680: 		$output.=$roletext{$sortrole{$which}};
                    681:                 if ($sortrole{$which} =~ m-dc\./(\w+)/-) {
                    682:                     if ($dcroles{$1}) {
1.138     raeburn   683:                         $output .= &allcourses_row($1,'');
1.104     raeburn   684:                     }
                    685:                 }
1.76      albertel  686: 	    }
1.108     raeburn   687: 	}
                    688: 	if ($output) {
                    689: 	    if ($doheaders > 0) {
1.126     www       690: 		$r->print("<tr>".
1.132     albertel  691: 			  "<td align='center' colspan='5'><font face='arial'>".&mt($type)."</font></td></tr>");
1.76      albertel  692: 	    }
1.108     raeburn   693: 	    $r->print($output);	
                    694: 	}
1.4       www       695:     }
1.14      www       696:     my $tremark='';
1.47      www       697:     my $tfont='#003300';
1.118     albertel  698:     if ($env{'request.role'} eq 'cm') {
1.19      www       699: 	$r->print('<tr bgcolor="#11CC55">');
1.72      www       700:         $tremark=&mt('Currently selected. ');
1.47      www       701:         $tfont='#002200';
1.14      www       702:     } else {
                    703:         $r->print('<tr bgcolor="#77FF77">');
                    704:     }
                    705:     unless ($nochoose) {
1.118     albertel  706: 	if ($env{'request.role'} ne 'cm') {
1.134     www       707: 	    $r->print('<td><input type="submit" value="'.
1.72      www       708: 		      &mt('Select').'" name="cm"></td>');
1.55      albertel  709: 	} else {
                    710: 	    $r->print('<td>&nbsp;</td>');
                    711: 	}
1.14      www       712:     }
1.132     albertel  713:     $r->print('<td colspan="3"><font color="'.$tfont.'">'.&mt('No role specified').
1.47      www       714:       '</font></td><td><font color="'.$tfont.'">'.$tremark.
                    715:       '&nbsp;</font></td></tr>'."\n");
1.4       www       716: 
                    717:     $r->print('</table>');
                    718:     unless ($nochoose) {
                    719: 	$r->print("</form>\n");
                    720:     }
1.22      harris41  721: # ------------------------------------------------------------ Privileges Info
1.118     albertel  722:     if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.127     albertel  723: 	$r->print('<hr /><h2>Current Privileges</h2>');
1.4       www       724: 
1.118     albertel  725: 	foreach $envkey (sort keys %env) {
                    726: 	    if ($envkey=~/^user\.priv\.$env{'request.role'}\./) {
1.55      albertel  727: 		my $where=$envkey;
1.118     albertel  728: 		$where=~s/^user\.priv\.$env{'request.role'}\.//;
1.55      albertel  729: 		my $ttype;
                    730: 		my $twhere;
                    731: 		my ($tdom,$trest,$tsec)=
                    732: 		    split(/\//,Apache::lonnet::declutter($where));
                    733: 		if ($trest) {
1.118     albertel  734: 		    if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
1.55      albertel  735: 			$ttype='Construction Space';
                    736: 			$twhere='User: '.$trest.', Domain: '.$tdom;
                    737: 		    } else {
                    738: 			$ttype='Course';
1.118     albertel  739: 			$twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
1.55      albertel  740: 			if ($tsec) {
                    741: 			    $twhere.=' (Section/Group: '.$tsec.')';
                    742: 			}
                    743: 		    }
                    744: 		} elsif ($tdom) {
                    745: 		    $ttype='Domain';
                    746: 		    $twhere=$tdom;
                    747: 		} else {
                    748: 		    $ttype='System';
                    749: 		    $twhere='/';
                    750: 		}
                    751: 		$r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
1.118     albertel  752: 		foreach (sort split(/:/,$env{$envkey})) {
1.55      albertel  753: 		    if ($_) {
                    754: 			my ($prv,$restr)=split(/\&/,$_);
                    755: 			my $trestr='';
                    756: 			if ($restr ne 'F') {
                    757: 			    my $i;
                    758: 			    $trestr.=' (';
                    759: 			    for ($i=0;$i<length($restr);$i++) {
                    760: 				$trestr.=
                    761: 			       Apache::lonnet::plaintext(substr($restr,$i,1));
                    762: 				if ($i<length($restr)-1) { $trestr.=', '; }
                    763: 			    }
                    764: 			    $trestr.=')';
                    765: 			}
                    766: 			$r->print('<li>'.
                    767: 				  Apache::lonnet::plaintext($prv).$trestr.
                    768: 				  '</li>');
                    769: 		    }
                    770: 		}
                    771: 		$r->print('</ul>');
                    772: 	    }
                    773: 	}
1.4       www       774:     }
1.66      www       775:     $r->print(&Apache::lonnet::getannounce());
1.65      www       776:     if ($advanced) {
                    777: 	$r->print('<p><small><i>This is LON-CAPA '.
1.85      www       778: 		  $r->dir_config('lonVersion').'</i><br />'.
                    779: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
1.65      www       780:     }
1.147     albertel  781:     $r->print(&Apache::loncommon::end_page());
1.1       harris41  782:     return OK;
1.102     raeburn   783: }
                    784: 
                    785: sub role_status {
                    786:     my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
                    787:     my @pwhere = ();
1.118     albertel  788:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.102     raeburn   789:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                    790:         unless (!defined($$role) || $$role eq '') {
                    791:             $$where=join('.',@pwhere);
                    792:             $$trolecode=$$role.'.'.$$where;
1.118     albertel  793:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
1.102     raeburn   794:             $$tstatus='is';
1.105     raeburn   795:             if ($$tstart && $$tstart>$then) {
                    796: 		$$tstatus='future';
                    797: 		if ($$tstart<$now) { $$tstatus='will'; }
1.102     raeburn   798:             }
                    799:             if ($$tend) {
                    800:                 if ($$tend<$then) {
                    801:                     $$tstatus='expired';
1.103     raeburn   802:                 } elsif ($$tend<$now) {
1.104     raeburn   803:                     $$tstatus='will_not';
1.102     raeburn   804:                 }
                    805:             }
                    806:         }
                    807:     }
                    808: }
1.1       harris41  809: 
1.110     raeburn   810: sub build_roletext {
1.127     albertel  811:     my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver) = @_;
                    812:     my $roletext='<tr bgcolor="'.$tbg.'">';
1.132     albertel  813:     my $is_dc=($trolecode =~ m/^dc\./);
                    814:     my $rowspan=($is_dc) ? ''
                    815:                          : ' rowspan="2" ';
                    816: 
1.110     raeburn   817:     unless ($nochoose) {
1.134     www       818:         my $buttonname=$trolecode;
                    819:         $buttonname=~s/\W//g;
1.110     raeburn   820:         if (!$button) {
                    821:             if ($switchserver) {
1.132     albertel  822:                 $roletext.='<td'.$rowspan.'><a href="/adm/switchserver?'.
1.110     raeburn   823:                 $switchserver.'">'.&mt('Switch Server').'</a></td>';
                    824:             } else {
                    825:                 $roletext.=('<td>&nbsp;</td>');
                    826:             }
                    827:         } elsif ($tstatus eq 'is') {
1.134     www       828:             $roletext.='<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    829:                         &mt('Select').'" onClick="javascript:enterrole(this.form,\''.
                    830:                         $trolecode."','".$buttonname.'\');"></td>';
1.110     raeburn   831:         } elsif ($tryagain) {
                    832:             $roletext.=
1.134     www       833:                 '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    834:                 &mt('Try Selecting Again').'" onClick="javascript:enterrole(this.form,\''.
                    835:                         $trolecode."','".$buttonname.'\');"></td>';
1.110     raeburn   836:         } elsif ($advanced) {
                    837:             $roletext.=
1.134     www       838:                 '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    839:                 &mt('Re-Initialize').'" onClick="javascript:enterrole(this.form,\''.
                    840:                         $trolecode."','".$buttonname.'\');"></td>';
1.110     raeburn   841:         } else {
1.132     albertel  842:             $roletext.='<td'.$rowspan.'>&nbsp;</td>';
1.110     raeburn   843:         }
                    844:     }
                    845:     $tremark.=&Apache::lonannounce::showday(time,1,
                    846:                  &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                    847: 
                    848:     $roletext.='<td><font color="'.$tfont.'">'.$trole.
1.121     albertel  849: 	       '</font></td><td><font color="'.$tfont.'">'.$twhere.
1.110     raeburn   850:                '</font></td><td><font color="'.$tfont.'">'.$tpstart.
                    851:                '</font></td><td><font color="'.$tfont.'">'.$tpend.
1.132     albertel  852:                '</font></td></tr>';
                    853:     if (!$is_dc) {
                    854: 	$roletext.='<tr bgcolor="'.$tbg.'"><td colspan="4"><font color="'.$tfont.'">'.$tremark.
                    855: 	    '&nbsp;</font></td></tr><tr><td colspan="5" height="3"></td></tr>'."\n";
                    856:     }
1.110     raeburn   857:     return $roletext;
                    858: }
                    859: 
                    860: sub check_privs {
1.146     raeburn   861:     my ($cdom,$cnum,$then,$now) = @_;
                    862:     my $cckey = 'user.role.cc./'.$cdom.'/'.$cnum; 
1.118     albertel  863:     if ($env{$cckey}) {
1.110     raeburn   864:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
                    865:         &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
                    866:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.146     raeburn   867:             &set_privileges($cdom,$cnum);
1.110     raeburn   868:         }
                    869:     } else {
1.146     raeburn   870:         &set_privileges($cdom,$cnum);
1.110     raeburn   871:     }
                    872: }
                    873: 
1.104     raeburn   874: sub check_fordc {
                    875:     my ($dcroles,$then) = @_;
                    876:     my $numdc = 0;
1.118     albertel  877:     if ($env{'user.adv'}) {
                    878:         foreach my $envkey (sort keys %env) {
1.104     raeburn   879:             if ($envkey=~/^user\.role\.dc\.\/(\w+)\/$/) {
                    880:                 my $dcdom = $1;
                    881:                 my $livedc = 1;
1.118     albertel  882:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
1.105     raeburn   883:                 if ($tstart && $tstart>$then) { $livedc = 0; }
                    884:                 if ($tend   && $tend  <$then) { $livedc = 0; }
1.104     raeburn   885:                 if ($livedc) {
                    886:                     $$dcroles{$dcdom} = $envkey;
1.105     raeburn   887:                     $numdc++;
1.104     raeburn   888:                 }
                    889:             }
                    890:         }
                    891:     }
                    892:     return $numdc;
                    893: }
                    894: 
1.108     raeburn   895: sub courselink {
1.138     raeburn   896:     my ($dcdom,$rowtype) = @_;
1.109     raeburn   897:     my $courseform=&Apache::loncommon::selectcourse_link
1.138     raeburn   898:                      ('rolechoice','dccourse'.$rowtype.'_'.$dcdom,'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'.$dcdom,$dcdom);
                    899:     my $hiddenitems = '<input type="hidden" name="dcdomain'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                    900:                       '<input type="hidden" name="origdom'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                    901:                       '<input type="hidden" name="dccourse'.$rowtype.'_'.$dcdom.'" value="" />'.
                    902:                       '<input type="hidden" name="coursedesc'.$rowtype.'_'.$dcdom.'" value="" />';
1.112     raeburn   903:     return $courseform.$hiddenitems;
1.109     raeburn   904: }
                    905: 
                    906: sub coursepick_jscript {
1.104     raeburn   907:     my $verify_script = <<"END";
                    908: <script>
1.108     raeburn   909: function verifyCoursePick(caller) {
                    910:     var numbutton = getIndex(caller)
1.112     raeburn   911:     var pickedCourse = document.rolechoice.elements[numbutton+4].value
                    912:     var pickedDomain = document.rolechoice.elements[numbutton+2].value
                    913:     if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104     raeburn   914:         if (pickedCourse != '') {
1.108     raeburn   915:             if (numbutton != -1) {
                    916:                 var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                    917:                 document.rolechoice.elements[numbutton+1].name = courseTarget
                    918:                 document.rolechoice.submit()
                    919:             }
1.104     raeburn   920:         }
                    921:         else {
1.114     raeburn   922:             alert("Please use the 'Select Course' link to open a separate pick course window where you may select the course you wish to enter.");
1.104     raeburn   923:         }
                    924:     }
                    925:     else {
                    926:         alert("You can only use this screen to select courses in the current domain")
                    927:     }
                    928: }
1.109     raeburn   929: function getIndex(caller) {
1.108     raeburn   930:     for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109     raeburn   931:         if (document.rolechoice.elements[i] == caller) {
1.108     raeburn   932:             return i;
                    933:         }
                    934:     }
                    935:     return -1;
                    936: }
1.104     raeburn   937: </script>
                    938: END
1.109     raeburn   939:     return $verify_script;
1.104     raeburn   940: }
                    941: 
1.109     raeburn   942: sub processpick {
                    943:     my $process_pick = <<"END";
                    944: <script>
                    945: function process_pick(dom) {
1.138     raeburn   946:     var pickedCourse=opener.document.rolechoice.$env{'form.cnumelement'}.value;
                    947:     var pickedDomain=opener.document.rolechoice.$env{'form.cdomelement'}.value;
                    948:     var okDomain = 0;
                    949: 
                    950:     if (pickedDomain == dom) {
1.109     raeburn   951:         if (pickedCourse != '') {
1.138     raeburn   952:             var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                    953:             opener.document.title='Role selected. Please stand by.';
                    954:             opener.status='Role selected. Please stand by.';
                    955: 	    opener.document.rolechoice.newrole.value=courseTarget
                    956:             opener.document.rolechoice.submit()
1.109     raeburn   957:         }
1.138     raeburn   958:     } else {
                    959:         alert("You may only use this screen to select courses in the current domain: "+dom+"\\nPlease return to the roles page window and click the 'Select Course' link for domain: "+pickedDomain+",\\n if you are a Domain Coordinator in that domain, and wish to become a Course Coordinator in a course in the domain");
1.109     raeburn   960:     }
                    961: }
                    962:  
                    963: </script>
                    964: END
                    965:     return $process_pick;
                    966: }
1.108     raeburn   967: 
1.113     raeburn   968: sub display_cc_role {
                    969:     my $rolekey = shift;
                    970:     my $roletext;
1.118     albertel  971:     my $advanced = $env{'user.adv'};
                    972:     my $tryagain = $env{'form.tryagain'};
1.113     raeburn   973:     unless ($rolekey =~/^error\:/) {
                    974:         if ($rolekey =~ m-^user\.role.cc\./(\w+)/(\w+)$-) {
                    975:             my $tcourseid = $1.'_'.$2;
                    976:             my $trolecode = 'cc./'.$1.'/'.$2;
                    977:             my $trole = Apache::lonnet::plaintext('cc');
                    978:             my $twhere;
                    979:             my $tbg='#77FF77';
                    980:             my $tfont='#003300';
                    981:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                    982:             if (%newhash) {
                    983:                 $twhere=$newhash{'description'}.
                    984:                         ' <font size="-2">'.
                    985:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont).
                    986:                         '</font>';
                    987:             } else {
                    988:                 $twhere=&mt('Currently not available');
1.118     albertel  989:                 $env{'course.'.$tcourseid.'.description'}=$twhere;
1.110     raeburn   990:             }
1.113     raeburn   991:             $twhere.="<br />".&mt('Domain').":".$1;
1.127     albertel  992:             $roletext = &build_roletext($trolecode,$1,$2,'is',$tryagain,$advanced,'',$tbg,$tfont,$trole,&mt('Course'),$twhere,'','','',1,'');
1.104     raeburn   993:         }
                    994:     }
1.113     raeburn   995:     return $roletext;
1.104     raeburn   996: }
                    997: 
1.108     raeburn   998: sub allcourses_row {
1.138     raeburn   999:     my ($dcdom,$rowtype) = @_;
1.108     raeburn  1000:     my $ccrole = Apache::lonnet::plaintext('cc');
1.138     raeburn  1001:     my $selectlink = &courselink($dcdom,$rowtype);
1.132     albertel 1002:     my $output = '<tr bgcolor="#77FF77">'.
                   1003: 	'<td colspan="5">'.
                   1004: 	'<font color="#002200">'.$ccrole.'</font>'.
                   1005: 	' <b>'.$selectlink.'</b>'.
                   1006: 	' from '.&mt('Domain').' '.$dcdom.
                   1007: 	'<tr><td colspan="5" height="3"></td></tr>'."\n";
1.108     raeburn  1008:     return $output;
                   1009: }
                   1010: 
1.104     raeburn  1011: sub recent_filename {
                   1012:     my $area=shift;
1.149     www      1013:     return 'nohist_recent_'.&escape($area);
1.104     raeburn  1014: }
                   1015: 
1.106     raeburn  1016: sub set_privileges {
                   1017:     my ($dcdom,$pickedcourse) = @_;
                   1018:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   1019:     my $role = 'cc';
                   1020:     my $spec = $role.'.'.$area;
1.118     albertel 1021:     my $userroles = &Apache::lonnet::set_arearole($role,$area,'','',$dcdom,$env{'user.name'});
1.106     raeburn  1022:     my %ccrole = ();
                   1023:     &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
1.107     raeburn  1024:     my ($author,$adv)= &Apache::lonnet::set_userprivs(\$userroles,\%ccrole);
                   1025:     my @newprivs = split/\n/,$userroles;
1.106     raeburn  1026:     my %newccroles = ();
                   1027:     foreach (@newprivs) {
                   1028:         my ($key,$val) = split/=/,$_;
                   1029:         $newccroles{$key} = $val;
                   1030:     }
                   1031:     &Apache::lonnet::appenv(%newccroles);
1.118     albertel 1032:     &Apache::lonnet::log($env{'user.domain'},
                   1033:                          $env{'user.name'},
                   1034:                          $env{'user.home'},
1.106     raeburn  1035:                         "Role ".$role);
                   1036:     &Apache::lonnet::appenv(
1.146     raeburn  1037:                           'request.role'        => $spec,
1.106     raeburn  1038:                           'request.role.domain' => $dcdom,
                   1039:                           'request.course.sec'  => '');
                   1040:     my $tadv=0;
                   1041:     if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
                   1042:     &Apache::lonnet::appenv('request.role.adv'    => $tadv);
                   1043: }
                   1044: 
1.139     raeburn  1045: sub courseloadpage {
                   1046:     my ($courseid) = @_;
                   1047:     my $startpage;
1.144     albertel 1048:     my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
                   1049: 					      [$courseid.':courseinit']);
1.139     raeburn  1050:     my ($tmp) = %entry_settings;
1.144     albertel 1051:     unless ($tmp =~ /^error: 2 /) {
1.139     raeburn  1052:         $startpage = $entry_settings{$courseid.':courseinit'};
                   1053:     }
                   1054:     if ($startpage eq '') {
                   1055:         if (exists($env{'environment.course_init_display'})) {
                   1056:             $startpage = $env{'environment.course_init_display'};
                   1057:         }
                   1058:     }
                   1059:     return $startpage;
                   1060: }
                   1061: 
1.1       harris41 1062: 1;
                   1063: __END__
1.32      harris41 1064: 
                   1065: =head1 NAME
                   1066: 
                   1067: Apache::lonroles - User Roles Screen
                   1068: 
                   1069: =head1 SYNOPSIS
                   1070: 
                   1071: Invoked by /etc/httpd/conf/srm.conf:
                   1072: 
                   1073:  <Location /adm/roles>
                   1074:  PerlAccessHandler       Apache::lonacc
                   1075:  SetHandler perl-script
                   1076:  PerlHandler Apache::lonroles
                   1077:  ErrorDocument     403 /adm/login
                   1078:  ErrorDocument	  500 /adm/errorhandler
                   1079:  </Location>
1.64      bowersj2 1080: 
                   1081: =head1 OVERVIEW
                   1082: 
                   1083: =head2 Choosing Roles
                   1084: 
                   1085: C<lonroles> is a handler that allows a user to switch roles in
                   1086: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                   1087: default role that a user has before selecting a role, as widely as
                   1088: possible, but certain handlers for example need specification which
                   1089: course they should act on, etc. Both in this scenario, and when the
                   1090: handler determines via C<lonnet>'s C<&allowed> function that a certain
                   1091: action is not allowed, C<lonroles> is used as error handler. This
                   1092: allows the user to select another role which may have permission to do
                   1093: what they were trying to do. C<lonroles> can also be accessed via the
                   1094: B<CRS> button in the Remote Control. 
                   1095: 
                   1096: =begin latex
                   1097: 
                   1098: \begin{figure}
                   1099: \begin{center}
                   1100: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                   1101:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                   1102: \end{center}
                   1103: \end{figure}
                   1104: 
                   1105: =end latex
                   1106: 
                   1107: =head2 Role Initialization
                   1108: 
                   1109: 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 1110: 
                   1111: =head1 INTRODUCTION
                   1112: 
                   1113: This module enables a user to select what role he wishes to
                   1114: operate under (instructor, student, teaching assistant, course
                   1115: coordinator, etc).  These roles are pre-established by the actions
                   1116: of upper-level users.
                   1117: 
                   1118: This is part of the LearningOnline Network with CAPA project
                   1119: described at http://www.lon-capa.org.
                   1120: 
                   1121: =head1 HANDLER SUBROUTINE
                   1122: 
                   1123: This routine is called by Apache and mod_perl.
                   1124: 
                   1125: =over 4
                   1126: 
                   1127: =item *
                   1128: 
                   1129: Roles Initialization (yes/no)
                   1130: 
                   1131: =item *
                   1132: 
                   1133: Get Error Message from Environment
                   1134: 
                   1135: =item *
                   1136: 
                   1137: Who is this?
                   1138: 
                   1139: =item *
                   1140: 
                   1141: Generate Page Output
                   1142: 
                   1143: =item *
                   1144: 
                   1145: Choice or no choice
                   1146: 
                   1147: =item *
                   1148: 
                   1149: Table
                   1150: 
                   1151: =item *
                   1152: 
                   1153: Privileges
                   1154: 
                   1155: =back
                   1156: 
                   1157: =cut

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