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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.150   ! www         4: # $Id: lonroles.pm,v 1.149 2006/05/30 12:45:24 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.120     albertel   42: use GDBM_File;
1.149     www        43: use lib '/home/httpd/lib/perl/';
                     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: 				}
        !           309: 				&redirect_user($r,&mt('Entering Course'),
        !           310: 					       $furl,$msg,
        !           311: 					       $env{'environment.remotenavmap'});
1.58      bowersj2  312: 			    }
1.124     albertel  313: 			    return OK;
1.55      albertel  314: 			}
                    315: 		    }
1.62      matthew   316:                     #
                    317:                     # Send the user to the construction space they selected
1.125     www       318:                     if ($role =~ /^(au|ca|aa)$/) {
1.62      matthew   319:                         my $redirect_url = '/priv/';
                    320:                         if ($role eq 'au') {
1.118     albertel  321:                             $redirect_url.=$env{'user.name'};
1.62      matthew   322:                         } else {
                    323:                             $where =~ /\/(.*)$/;
                    324:                             $redirect_url .= $1;
                    325:                         }
                    326:                         $redirect_url .= '/';
1.78      sakharuk  327:                         &redirect_user($r,&mt('Entering Construction Space'),
1.62      matthew   328:                                        $redirect_url);
                    329:                         return OK;
                    330:                     }
1.104     raeburn   331:                     if ($role eq 'dc') {
1.108     raeburn   332:                         my $redirect_url = '/adm/menu/';
                    333:                         &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104     raeburn   334:                                        $redirect_url);
1.108     raeburn   335:                         return OK;
1.104     raeburn   336:                     }
1.55      albertel  337: 		}
                    338:             }
1.6       www       339:         }
1.40      matthew   340:     }
1.44      www       341: 
1.10      www       342: 
1.6       www       343: # =============================================================== No Roles Init
1.10      www       344: 
1.73      www       345:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel  346:     &Apache::loncommon::no_cache($r);
1.10      www       347:     $r->send_http_header;
                    348:     return OK if $r->header_only;
                    349: 
1.52      www       350:     my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  351:     my $start_page=&Apache::loncommon::start_page('User Roles');
1.134     www       352:     my $standby=&mt('Role selected. Please stand by.');
1.135     albertel  353:     $standby=~s/\n/\\n/g;
1.94      albertel  354:     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       355:     $r->print(<<ENDHEADER);
1.147     albertel  356: $start_page
1.45      www       357: $helptag<br />
1.26      www       358: <script>
                    359: $swinfo
                    360: window.focus();
1.134     www       361: 
                    362: active=true;
                    363: 
                    364: function enterrole (thisform,rolecode,buttonname) {
                    365:     if (active) {
                    366: 	active=false;
                    367:         document.title='$standby';
                    368:         window.status='$standby';
                    369: 	thisform.newrole.value=rolecode;
                    370: 	thisform.submit();
                    371:     } else {
                    372:        alert('$standby');
                    373:     }   
                    374: }
1.26      www       375: </script>
1.10      www       376: ENDHEADER
1.6       www       377: 
1.2       www       378: # ------------------------------------------ Get Error Message from Environment
                    379: 
1.118     albertel  380:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
                    381:     if ($env{'user.error.msg'}) {
1.55      albertel  382: 	$r->log_reason(
1.118     albertel  383:    "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12      www       384:     }
1.1       harris41  385: 
1.61      www       386: # ------------------------------------------------- Can this user re-init, etc?
1.6       www       387: 
1.118     albertel  388:     my $advanced=$env{'user.adv'};
1.61      www       389:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118     albertel  390:     my $tryagain=$env{'form.tryagain'};
1.6       www       391: 
1.2       www       392: # -------------------------------------------------------- Generate Page Output
1.6       www       393: # --------------------------------------------------------------- Error Header?
1.2       www       394:     if ($error) {
                    395: 	$r->print("<h1>LON-CAPA Access Control</h1>");
1.126     www       396:         $r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>Access  : ".
1.4       www       397:                   Apache::lonnet::plaintext($priv)."\n");
1.115     albertel  398:         $r->print("Resource: ".&Apache::lonenc::check_encrypt($fn)."\n");
1.120     albertel  399:         $r->print("Action  : $msg\n</pre><hr />");
                    400: 	my $url=$fn;
                    401: 	my $last;
                    402: 	if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                    403: 		&GDBM_READER(),0640)) {
                    404: 	    $last=$hash{'last_known'};
                    405: 	    untie(%hash);
                    406: 	}
1.149     www       407: 	if ($last) { $fn.='?symb='.&escape($last); }
1.120     albertel  408: 
                    409: 	&Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
                    410: 					&Apache::lonenc::check_encrypt($fn));
1.2       www       411:     } else {
1.118     albertel  412:         if ($env{'user.error.msg'}) {
1.25      www       413: 	    $r->print(
1.131     albertel  414:  '<h3><font color="red">'.
1.72      www       415:  &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
1.25      www       416: 	}
1.2       www       417:     }
1.6       www       418: # -------------------------------------------------------- Choice or no choice?
1.2       www       419:     if ($nochoose) {
1.150   ! www       420: 	$r->print("<h2>".&mt('Sorry ...')."</h2>\n".
        !           421: 		  &mt('This action is currently not authorized.').
        !           422: 		  &Apache::loncommon::end_page());
        !           423: 	return OK;
1.6       www       424:     } else {
                    425:         if ($advanced) {
1.72      www       426: 	    $r->print(&mt("Your home server is ").
1.55      albertel  427: 		      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
1.118     albertel  428:                       ($env{'user.name'},$env{'user.domain'})}.
1.55      albertel  429: 		      "<br />\n");
1.72      www       430: 	    $r->print(&mt(
1.126     www       431:       "Author and Co-Author roles are not available on servers other than their respective home servers."));
1.17      www       432:         }
1.18      www       433:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                    434:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www       435:         }
1.84      www       436:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116     albertel  437:         $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
                    438:         $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134     www       439:         $r->print('<input type="hidden" name="newrole" value="" />');
1.6       www       440:     }
1.118     albertel  441:     if ($env{'user.adv'}) {
1.63      www       442: 	$r->print(
1.116     albertel  443: 	      '<br /><label>'.&mt('Show all roles').': <input type="checkbox" name="showall"');
1.118     albertel  444: 	if ($env{'form.showall'}) { $r->print(' checked="checked" '); }
1.116     albertel  445: 	$r->print(' /></label><input type="submit" value="'.&mt('Display').'" />');
1.63      www       446:     }
1.4       www       447: 
1.75      albertel  448:     my (%roletext,%sortrole,%roleclass);
1.84      www       449:     my $countactive=0;
                    450:     my $inrole=0;
                    451:     my $possiblerole='';
1.118     albertel  452:     foreach $envkey (sort keys %env) {
1.35      matthew   453:         my $button = 1;
1.49      www       454:         my $switchserver='';
1.75      albertel  455: 	my $roletext;
                    456: 	my $sortkey;
1.2       www       457:         if ($envkey=~/^user\.role\./) {
1.102     raeburn   458:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
                    459:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.136     raeburn   460:             next if (!defined($role) || $role eq '' || $role =~ /^gr/);
1.102     raeburn   461:             $tremark='';
                    462:             $tpstart='&nbsp;';
                    463:             $tpend='&nbsp;';
                    464:             $tfont='#000000';
1.4       www       465:             if ($tstart) {
1.74      www       466:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
1.4       www       467:             }
                    468:             if ($tend) {
1.74      www       469:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
1.4       www       470:             }
1.118     albertel  471:             if ($env{'request.role'} eq $trolecode) {
1.6       www       472: 		$tstatus='selected';
                    473:             }
1.4       www       474:             my $tbg;
1.35      matthew   475:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
1.118     albertel  476:                 ($env{'form.showall'})) {
1.35      matthew   477:                 if ($tstatus eq 'is') {
                    478:                     $tbg='#77FF77';
1.47      www       479:                     $tfont='#003300';
1.84      www       480: 		    $possiblerole=$trolecode;
                    481: 		    $countactive++;
1.35      matthew   482:                 } elsif ($tstatus eq 'future') {
                    483:                     $tbg='#FFFF77';
1.49      www       484:                     $button=0;
1.35      matthew   485:                 } elsif ($tstatus eq 'will') {
                    486:                     $tbg='#FFAA77';
1.72      www       487:                     $tremark.=&mt('Active at next login. ');
1.35      matthew   488:                 } elsif ($tstatus eq 'expired') {
                    489:                     $tbg='#FF7777';
1.47      www       490:                     $tfont='#330000';
1.49      www       491:                     $button=0;
1.35      matthew   492:                 } elsif ($tstatus eq 'will_not') {
                    493:                     $tbg='#AAFF77';
1.72      www       494:                     $tremark.=&mt('Expired after logout. ');
1.35      matthew   495:                 } elsif ($tstatus eq 'selected') {
                    496:                     $tbg='#11CC55';
1.47      www       497:                     $tfont='#002200';
1.84      www       498: 		    $inrole=1;
1.86      albertel  499: 		    $countactive++;
1.72      www       500:                     $tremark.=&mt('Currently selected. ');
1.35      matthew   501:                 }
                    502:                 my $trole;
                    503:                 if ($role =~ /^cr\//) {
                    504:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.132     albertel  505: 		    if ($tremark) { $tremark.='<br />'; }
                    506:                     $tremark.=&mt('Defined by ').$rauthor.
1.72      www       507: 			&mt(' at ').$rdomain.'.';
1.35      matthew   508:                     $trole=$rrole;
1.8       www       509:                 } else {
1.35      matthew   510:                     $trole=Apache::lonnet::plaintext($role);
                    511:                 }
                    512:                 my $ttype;
                    513:                 my $twhere;
                    514:                 my ($tdom,$trest,$tsection)=
                    515:                     split(/\//,Apache::lonnet::declutter($where));
                    516:                 # First, Co-Authorship roles
1.125     www       517:                 if (($role eq 'ca') || ($role eq 'aa')) {
1.39      stredwic  518:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
1.83      albertel  519: 		    my $allowed=0;
                    520: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    521: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    522:                     if (!$allowed) {
1.49      www       523: 			$button=0;
1.130     albertel  524:                         $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49      www       525:                     }
1.35      matthew   526:                     #next if ($home eq 'no_host');
                    527:                     $home = $Apache::lonnet::hostname{$home};
1.78      sakharuk  528:                     $ttype='Construction Space';
1.72      www       529:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                    530: 			': '.$tdom.'<br />'.
                    531:                         ' '.&mt('Server').':&nbsp;'.$home;
1.118     albertel  532:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.82      www       533: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
1.75      albertel  534: 		    $sortkey=$role."$trest:$tdom";
1.35      matthew   535:                 } elsif ($role eq 'au') {
                    536:                     # Authors
                    537:                     my $home = &Apache::lonnet::homeserver
1.118     albertel  538:                         ($env{'user.name'},$env{'user.domain'});
1.83      albertel  539: 		    my $allowed=0;
                    540: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    541: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    542:                     if (!$allowed) {
1.49      www       543: 			$button=0;
1.143     albertel  544:                         $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49      www       545:                     }
1.35      matthew   546:                     #next if ($home eq 'no_host');
                    547:                     $home = $Apache::lonnet::hostname{$home};
1.78      sakharuk  548:                     $ttype='Construction Space';
1.72      www       549:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                    550: 			':&nbsp;'.$home;
1.118     albertel  551:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                    552: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
1.75      albertel  553: 		    $sortkey=$role;
1.35      matthew   554:                 } elsif ($trest) {
1.78      sakharuk  555:                     $ttype='Course';
1.35      matthew   556:                     my $tcourseid=$tdom.'_'.$trest;
1.118     albertel  557:                     if ($env{'course.'.$tcourseid.'.description'}) {
                    558:                         $twhere=$env{'course.'.$tcourseid.'.description'};
1.80      albertel  559: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.72      www       560:                         unless ($twhere eq &mt('Currently not available')) {
1.55      albertel  561: 			    $twhere.=' <font size="-2">'.
1.72      www       562:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       563:                                     '</font>';
1.55      albertel  564: 			}
1.8       www       565:                     } else {
1.105     raeburn   566:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1.35      matthew   567:                         if (%newhash) {
1.80      albertel  568: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
1.77      albertel  569: 				"\0".$envkey;
1.49      www       570:                             $twhere=$newhash{'description'}.
                    571:                               ' <font size="-2">'.
1.72      www       572:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       573:                               '</font>';
1.35      matthew   574:                         } else {
1.72      www       575:                             $twhere=&mt('Currently not available');
1.118     albertel  576:                             $env{'course.'.$tcourseid.'.description'}=$twhere;
1.80      albertel  577: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.35      matthew   578:                         }
1.8       www       579:                     }
1.121     albertel  580:                     if ($tsection) {
                    581:                         $twhere.='<br />'.&mt('Section/Group').': '.$tsection;
                    582: 		    }
                    583: 
1.72      www       584: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
1.35      matthew   585:                 } elsif ($tdom) {
1.78      sakharuk  586:                     $ttype='Domain';
1.35      matthew   587:                     $twhere=$tdom;
1.75      albertel  588: 		    $sortkey=$role.$twhere;
1.35      matthew   589:                 } else {
1.78      sakharuk  590:                     $ttype='System';
1.72      www       591:                     $twhere=&mt('system wide');
1.75      albertel  592: 		    $sortkey=$role.$twhere;
1.13      www       593:                 }
1.35      matthew   594:  
1.127     albertel  595:                 $roletext.=&build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver);
1.75      albertel  596: 		$roletext{$envkey}=$roletext;
                    597: 		if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                    598: 		$sortrole{$sortkey}=$envkey;
                    599: 		$roleclass{$envkey}=$ttype;
1.55      albertel  600: 	    }
1.4       www       601:         }
1.75      albertel  602:     }
1.84      www       603: # No active roles
                    604:     if ($countactive==0) {
                    605: 	if ($inrole) {
                    606: 	    $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
                    607: 	} else {
                    608: 	    $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
                    609: 	}
1.147     albertel  610: 	$r->print('</form>'.&Apache::loncommon::end_page());
1.84      www       611: 	return OK;
                    612: # Is there only one choice?
1.118     albertel  613:     } elsif (($countactive==1) && ($env{'request.role'} eq 'cm')) {
1.84      www       614: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>'.
                    615: 	    '<input type="hidden" name="'.$possiblerole.'" value="1" />');
                    616: 	$r->print("</form>\n");
                    617: 	$r->rflush();
                    618: 	$r->print('<script>document.forms.rolechoice.submit();</script>');
1.147     albertel  619: 	$r->print(&Apache::loncommon::end_page());
1.84      www       620: 	return OK;
                    621:     }
                    622: # More than one possible role
                    623: # ----------------------------------------------------------------------- Table
                    624:     unless (($advanced) || ($nochoose)) {
                    625: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
                    626:     }
                    627:     $r->print('<br /><table><tr>');
                    628:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
1.121     albertel  629:     $r->print('<th>'.&mt('User Role').'</th><th>'.&mt('Extent').
1.132     albertel  630:          '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th></tr>'."\n");
1.76      albertel  631:     my $doheaders=-1;
1.129     albertel  632:     foreach my $type ('Domain','Construction Space','Course','System') {
1.76      albertel  633: 	my $haverole=0;
1.75      albertel  634: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    635: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
1.76      albertel  636: 		$haverole=1;
1.75      albertel  637: 	    }
1.76      albertel  638: 	}
                    639: 	if ($haverole) { $doheaders++; }
                    640:     }
1.111     albertel  641: 
1.118     albertel  642:     if ($env{'environment.recentroles'}) {
1.111     albertel  643:         my %recent_roles =
1.118     albertel  644:                &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111     albertel  645: 	my $output='';
                    646: 	foreach (sort(keys(%recent_roles))) {
                    647: 	    if (defined($roletext{'user.role.'.$_})) {
                    648: 		$output.=$roletext{'user.role.'.$_};
1.133     albertel  649:                 if ($_ =~ m-dc\./(\w+)/- && $dcroles{$1}) {
1.138     raeburn   650: 		    $output .= &allcourses_row($1,'recent');
1.133     albertel  651:                 }
1.113     raeburn   652: 	    } elsif ($numdc > 0) {
                    653:                 unless ($_ =~/^error\:/) {
                    654:                     $output.=&display_cc_role('user.role.'.$_);
                    655:                 }
                    656:             } 
1.111     albertel  657: 	}
                    658: 	if ($output) {
1.132     albertel  659: 	    $r->print("<tr><td align='center' colspan='5'><font face='arial'>".
1.126     www       660: 		      &mt('Recent Roles')."</font></td>");
1.111     albertel  661: 	    $r->print($output);
                    662: 	    $r->print("</tr>");
1.114     raeburn   663:             $doheaders ++;
1.111     albertel  664: 	}
                    665:     }
                    666: 
1.104     raeburn   667:     if ($numdc > 0) {
1.112     raeburn   668:         $r->print(&coursepick_jscript());
                    669:         $r->print(&Apache::loncommon::coursebrowser_javascript());
1.108     raeburn   670:     }
1.132     albertel  671:     foreach my $type ('Construction Space','Domain','Course','System') {
1.108     raeburn   672: 	my $output;
                    673: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    674: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
                    675: 		$output.=$roletext{$sortrole{$which}};
                    676:                 if ($sortrole{$which} =~ m-dc\./(\w+)/-) {
                    677:                     if ($dcroles{$1}) {
1.138     raeburn   678:                         $output .= &allcourses_row($1,'');
1.104     raeburn   679:                     }
                    680:                 }
1.76      albertel  681: 	    }
1.108     raeburn   682: 	}
                    683: 	if ($output) {
                    684: 	    if ($doheaders > 0) {
1.126     www       685: 		$r->print("<tr>".
1.132     albertel  686: 			  "<td align='center' colspan='5'><font face='arial'>".&mt($type)."</font></td></tr>");
1.76      albertel  687: 	    }
1.108     raeburn   688: 	    $r->print($output);	
                    689: 	}
1.4       www       690:     }
1.14      www       691:     my $tremark='';
1.47      www       692:     my $tfont='#003300';
1.118     albertel  693:     if ($env{'request.role'} eq 'cm') {
1.19      www       694: 	$r->print('<tr bgcolor="#11CC55">');
1.72      www       695:         $tremark=&mt('Currently selected. ');
1.47      www       696:         $tfont='#002200';
1.14      www       697:     } else {
                    698:         $r->print('<tr bgcolor="#77FF77">');
                    699:     }
                    700:     unless ($nochoose) {
1.118     albertel  701: 	if ($env{'request.role'} ne 'cm') {
1.134     www       702: 	    $r->print('<td><input type="submit" value="'.
1.72      www       703: 		      &mt('Select').'" name="cm"></td>');
1.55      albertel  704: 	} else {
                    705: 	    $r->print('<td>&nbsp;</td>');
                    706: 	}
1.14      www       707:     }
1.132     albertel  708:     $r->print('<td colspan="3"><font color="'.$tfont.'">'.&mt('No role specified').
1.47      www       709:       '</font></td><td><font color="'.$tfont.'">'.$tremark.
                    710:       '&nbsp;</font></td></tr>'."\n");
1.4       www       711: 
                    712:     $r->print('</table>');
                    713:     unless ($nochoose) {
                    714: 	$r->print("</form>\n");
                    715:     }
1.22      harris41  716: # ------------------------------------------------------------ Privileges Info
1.118     albertel  717:     if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.127     albertel  718: 	$r->print('<hr /><h2>Current Privileges</h2>');
1.4       www       719: 
1.118     albertel  720: 	foreach $envkey (sort keys %env) {
                    721: 	    if ($envkey=~/^user\.priv\.$env{'request.role'}\./) {
1.55      albertel  722: 		my $where=$envkey;
1.118     albertel  723: 		$where=~s/^user\.priv\.$env{'request.role'}\.//;
1.55      albertel  724: 		my $ttype;
                    725: 		my $twhere;
                    726: 		my ($tdom,$trest,$tsec)=
                    727: 		    split(/\//,Apache::lonnet::declutter($where));
                    728: 		if ($trest) {
1.118     albertel  729: 		    if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
1.55      albertel  730: 			$ttype='Construction Space';
                    731: 			$twhere='User: '.$trest.', Domain: '.$tdom;
                    732: 		    } else {
                    733: 			$ttype='Course';
1.118     albertel  734: 			$twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
1.55      albertel  735: 			if ($tsec) {
                    736: 			    $twhere.=' (Section/Group: '.$tsec.')';
                    737: 			}
                    738: 		    }
                    739: 		} elsif ($tdom) {
                    740: 		    $ttype='Domain';
                    741: 		    $twhere=$tdom;
                    742: 		} else {
                    743: 		    $ttype='System';
                    744: 		    $twhere='/';
                    745: 		}
                    746: 		$r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
1.118     albertel  747: 		foreach (sort split(/:/,$env{$envkey})) {
1.55      albertel  748: 		    if ($_) {
                    749: 			my ($prv,$restr)=split(/\&/,$_);
                    750: 			my $trestr='';
                    751: 			if ($restr ne 'F') {
                    752: 			    my $i;
                    753: 			    $trestr.=' (';
                    754: 			    for ($i=0;$i<length($restr);$i++) {
                    755: 				$trestr.=
                    756: 			       Apache::lonnet::plaintext(substr($restr,$i,1));
                    757: 				if ($i<length($restr)-1) { $trestr.=', '; }
                    758: 			    }
                    759: 			    $trestr.=')';
                    760: 			}
                    761: 			$r->print('<li>'.
                    762: 				  Apache::lonnet::plaintext($prv).$trestr.
                    763: 				  '</li>');
                    764: 		    }
                    765: 		}
                    766: 		$r->print('</ul>');
                    767: 	    }
                    768: 	}
1.4       www       769:     }
1.66      www       770:     $r->print(&Apache::lonnet::getannounce());
1.65      www       771:     if ($advanced) {
                    772: 	$r->print('<p><small><i>This is LON-CAPA '.
1.85      www       773: 		  $r->dir_config('lonVersion').'</i><br />'.
                    774: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
1.65      www       775:     }
1.147     albertel  776:     $r->print(&Apache::loncommon::end_page());
1.1       harris41  777:     return OK;
1.102     raeburn   778: }
                    779: 
                    780: sub role_status {
                    781:     my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
                    782:     my @pwhere = ();
1.118     albertel  783:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.102     raeburn   784:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                    785:         unless (!defined($$role) || $$role eq '') {
                    786:             $$where=join('.',@pwhere);
                    787:             $$trolecode=$$role.'.'.$$where;
1.118     albertel  788:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
1.102     raeburn   789:             $$tstatus='is';
1.105     raeburn   790:             if ($$tstart && $$tstart>$then) {
                    791: 		$$tstatus='future';
                    792: 		if ($$tstart<$now) { $$tstatus='will'; }
1.102     raeburn   793:             }
                    794:             if ($$tend) {
                    795:                 if ($$tend<$then) {
                    796:                     $$tstatus='expired';
1.103     raeburn   797:                 } elsif ($$tend<$now) {
1.104     raeburn   798:                     $$tstatus='will_not';
1.102     raeburn   799:                 }
                    800:             }
                    801:         }
                    802:     }
                    803: }
1.1       harris41  804: 
1.110     raeburn   805: sub build_roletext {
1.127     albertel  806:     my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver) = @_;
                    807:     my $roletext='<tr bgcolor="'.$tbg.'">';
1.132     albertel  808:     my $is_dc=($trolecode =~ m/^dc\./);
                    809:     my $rowspan=($is_dc) ? ''
                    810:                          : ' rowspan="2" ';
                    811: 
1.110     raeburn   812:     unless ($nochoose) {
1.134     www       813:         my $buttonname=$trolecode;
                    814:         $buttonname=~s/\W//g;
1.110     raeburn   815:         if (!$button) {
                    816:             if ($switchserver) {
1.132     albertel  817:                 $roletext.='<td'.$rowspan.'><a href="/adm/switchserver?'.
1.110     raeburn   818:                 $switchserver.'">'.&mt('Switch Server').'</a></td>';
                    819:             } else {
                    820:                 $roletext.=('<td>&nbsp;</td>');
                    821:             }
                    822:         } elsif ($tstatus eq 'is') {
1.134     www       823:             $roletext.='<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    824:                         &mt('Select').'" onClick="javascript:enterrole(this.form,\''.
                    825:                         $trolecode."','".$buttonname.'\');"></td>';
1.110     raeburn   826:         } elsif ($tryagain) {
                    827:             $roletext.=
1.134     www       828:                 '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    829:                 &mt('Try Selecting Again').'" onClick="javascript:enterrole(this.form,\''.
                    830:                         $trolecode."','".$buttonname.'\');"></td>';
1.110     raeburn   831:         } elsif ($advanced) {
                    832:             $roletext.=
1.134     www       833:                 '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    834:                 &mt('Re-Initialize').'" onClick="javascript:enterrole(this.form,\''.
                    835:                         $trolecode."','".$buttonname.'\');"></td>';
1.110     raeburn   836:         } else {
1.132     albertel  837:             $roletext.='<td'.$rowspan.'>&nbsp;</td>';
1.110     raeburn   838:         }
                    839:     }
                    840:     $tremark.=&Apache::lonannounce::showday(time,1,
                    841:                  &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                    842: 
                    843:     $roletext.='<td><font color="'.$tfont.'">'.$trole.
1.121     albertel  844: 	       '</font></td><td><font color="'.$tfont.'">'.$twhere.
1.110     raeburn   845:                '</font></td><td><font color="'.$tfont.'">'.$tpstart.
                    846:                '</font></td><td><font color="'.$tfont.'">'.$tpend.
1.132     albertel  847:                '</font></td></tr>';
                    848:     if (!$is_dc) {
                    849: 	$roletext.='<tr bgcolor="'.$tbg.'"><td colspan="4"><font color="'.$tfont.'">'.$tremark.
                    850: 	    '&nbsp;</font></td></tr><tr><td colspan="5" height="3"></td></tr>'."\n";
                    851:     }
1.110     raeburn   852:     return $roletext;
                    853: }
                    854: 
                    855: sub check_privs {
1.146     raeburn   856:     my ($cdom,$cnum,$then,$now) = @_;
                    857:     my $cckey = 'user.role.cc./'.$cdom.'/'.$cnum; 
1.118     albertel  858:     if ($env{$cckey}) {
1.110     raeburn   859:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
                    860:         &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
                    861:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.146     raeburn   862:             &set_privileges($cdom,$cnum);
1.110     raeburn   863:         }
                    864:     } else {
1.146     raeburn   865:         &set_privileges($cdom,$cnum);
1.110     raeburn   866:     }
                    867: }
                    868: 
1.104     raeburn   869: sub check_fordc {
                    870:     my ($dcroles,$then) = @_;
                    871:     my $numdc = 0;
1.118     albertel  872:     if ($env{'user.adv'}) {
                    873:         foreach my $envkey (sort keys %env) {
1.104     raeburn   874:             if ($envkey=~/^user\.role\.dc\.\/(\w+)\/$/) {
                    875:                 my $dcdom = $1;
                    876:                 my $livedc = 1;
1.118     albertel  877:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
1.105     raeburn   878:                 if ($tstart && $tstart>$then) { $livedc = 0; }
                    879:                 if ($tend   && $tend  <$then) { $livedc = 0; }
1.104     raeburn   880:                 if ($livedc) {
                    881:                     $$dcroles{$dcdom} = $envkey;
1.105     raeburn   882:                     $numdc++;
1.104     raeburn   883:                 }
                    884:             }
                    885:         }
                    886:     }
                    887:     return $numdc;
                    888: }
                    889: 
1.108     raeburn   890: sub courselink {
1.138     raeburn   891:     my ($dcdom,$rowtype) = @_;
1.109     raeburn   892:     my $courseform=&Apache::loncommon::selectcourse_link
1.138     raeburn   893:                      ('rolechoice','dccourse'.$rowtype.'_'.$dcdom,'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'.$dcdom,$dcdom);
                    894:     my $hiddenitems = '<input type="hidden" name="dcdomain'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                    895:                       '<input type="hidden" name="origdom'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                    896:                       '<input type="hidden" name="dccourse'.$rowtype.'_'.$dcdom.'" value="" />'.
                    897:                       '<input type="hidden" name="coursedesc'.$rowtype.'_'.$dcdom.'" value="" />';
1.112     raeburn   898:     return $courseform.$hiddenitems;
1.109     raeburn   899: }
                    900: 
                    901: sub coursepick_jscript {
1.104     raeburn   902:     my $verify_script = <<"END";
                    903: <script>
1.108     raeburn   904: function verifyCoursePick(caller) {
                    905:     var numbutton = getIndex(caller)
1.112     raeburn   906:     var pickedCourse = document.rolechoice.elements[numbutton+4].value
                    907:     var pickedDomain = document.rolechoice.elements[numbutton+2].value
                    908:     if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104     raeburn   909:         if (pickedCourse != '') {
1.108     raeburn   910:             if (numbutton != -1) {
                    911:                 var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                    912:                 document.rolechoice.elements[numbutton+1].name = courseTarget
                    913:                 document.rolechoice.submit()
                    914:             }
1.104     raeburn   915:         }
                    916:         else {
1.114     raeburn   917:             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   918:         }
                    919:     }
                    920:     else {
                    921:         alert("You can only use this screen to select courses in the current domain")
                    922:     }
                    923: }
1.109     raeburn   924: function getIndex(caller) {
1.108     raeburn   925:     for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109     raeburn   926:         if (document.rolechoice.elements[i] == caller) {
1.108     raeburn   927:             return i;
                    928:         }
                    929:     }
                    930:     return -1;
                    931: }
1.104     raeburn   932: </script>
                    933: END
1.109     raeburn   934:     return $verify_script;
1.104     raeburn   935: }
                    936: 
1.109     raeburn   937: sub processpick {
                    938:     my $process_pick = <<"END";
                    939: <script>
                    940: function process_pick(dom) {
1.138     raeburn   941:     var pickedCourse=opener.document.rolechoice.$env{'form.cnumelement'}.value;
                    942:     var pickedDomain=opener.document.rolechoice.$env{'form.cdomelement'}.value;
                    943:     var okDomain = 0;
                    944: 
                    945:     if (pickedDomain == dom) {
1.109     raeburn   946:         if (pickedCourse != '') {
1.138     raeburn   947:             var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                    948:             opener.document.title='Role selected. Please stand by.';
                    949:             opener.status='Role selected. Please stand by.';
                    950: 	    opener.document.rolechoice.newrole.value=courseTarget
                    951:             opener.document.rolechoice.submit()
1.109     raeburn   952:         }
1.138     raeburn   953:     } else {
                    954:         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   955:     }
                    956: }
                    957:  
                    958: </script>
                    959: END
                    960:     return $process_pick;
                    961: }
1.108     raeburn   962: 
1.113     raeburn   963: sub display_cc_role {
                    964:     my $rolekey = shift;
                    965:     my $roletext;
1.118     albertel  966:     my $advanced = $env{'user.adv'};
                    967:     my $tryagain = $env{'form.tryagain'};
1.113     raeburn   968:     unless ($rolekey =~/^error\:/) {
                    969:         if ($rolekey =~ m-^user\.role.cc\./(\w+)/(\w+)$-) {
                    970:             my $tcourseid = $1.'_'.$2;
                    971:             my $trolecode = 'cc./'.$1.'/'.$2;
                    972:             my $trole = Apache::lonnet::plaintext('cc');
                    973:             my $twhere;
                    974:             my $tbg='#77FF77';
                    975:             my $tfont='#003300';
                    976:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                    977:             if (%newhash) {
                    978:                 $twhere=$newhash{'description'}.
                    979:                         ' <font size="-2">'.
                    980:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont).
                    981:                         '</font>';
                    982:             } else {
                    983:                 $twhere=&mt('Currently not available');
1.118     albertel  984:                 $env{'course.'.$tcourseid.'.description'}=$twhere;
1.110     raeburn   985:             }
1.113     raeburn   986:             $twhere.="<br />".&mt('Domain').":".$1;
1.127     albertel  987:             $roletext = &build_roletext($trolecode,$1,$2,'is',$tryagain,$advanced,'',$tbg,$tfont,$trole,&mt('Course'),$twhere,'','','',1,'');
1.104     raeburn   988:         }
                    989:     }
1.113     raeburn   990:     return $roletext;
1.104     raeburn   991: }
                    992: 
1.108     raeburn   993: sub allcourses_row {
1.138     raeburn   994:     my ($dcdom,$rowtype) = @_;
1.108     raeburn   995:     my $ccrole = Apache::lonnet::plaintext('cc');
1.138     raeburn   996:     my $selectlink = &courselink($dcdom,$rowtype);
1.132     albertel  997:     my $output = '<tr bgcolor="#77FF77">'.
                    998: 	'<td colspan="5">'.
                    999: 	'<font color="#002200">'.$ccrole.'</font>'.
                   1000: 	' <b>'.$selectlink.'</b>'.
                   1001: 	' from '.&mt('Domain').' '.$dcdom.
                   1002: 	'<tr><td colspan="5" height="3"></td></tr>'."\n";
1.108     raeburn  1003:     return $output;
                   1004: }
                   1005: 
1.104     raeburn  1006: sub recent_filename {
                   1007:     my $area=shift;
1.149     www      1008:     return 'nohist_recent_'.&escape($area);
1.104     raeburn  1009: }
                   1010: 
1.106     raeburn  1011: sub set_privileges {
                   1012:     my ($dcdom,$pickedcourse) = @_;
                   1013:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   1014:     my $role = 'cc';
                   1015:     my $spec = $role.'.'.$area;
1.118     albertel 1016:     my $userroles = &Apache::lonnet::set_arearole($role,$area,'','',$dcdom,$env{'user.name'});
1.106     raeburn  1017:     my %ccrole = ();
                   1018:     &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
1.107     raeburn  1019:     my ($author,$adv)= &Apache::lonnet::set_userprivs(\$userroles,\%ccrole);
                   1020:     my @newprivs = split/\n/,$userroles;
1.106     raeburn  1021:     my %newccroles = ();
                   1022:     foreach (@newprivs) {
                   1023:         my ($key,$val) = split/=/,$_;
                   1024:         $newccroles{$key} = $val;
                   1025:     }
                   1026:     &Apache::lonnet::appenv(%newccroles);
1.118     albertel 1027:     &Apache::lonnet::log($env{'user.domain'},
                   1028:                          $env{'user.name'},
                   1029:                          $env{'user.home'},
1.106     raeburn  1030:                         "Role ".$role);
                   1031:     &Apache::lonnet::appenv(
1.146     raeburn  1032:                           'request.role'        => $spec,
1.106     raeburn  1033:                           'request.role.domain' => $dcdom,
                   1034:                           'request.course.sec'  => '');
                   1035:     my $tadv=0;
                   1036:     if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
                   1037:     &Apache::lonnet::appenv('request.role.adv'    => $tadv);
                   1038: }
                   1039: 
1.139     raeburn  1040: sub courseloadpage {
                   1041:     my ($courseid) = @_;
                   1042:     my $startpage;
1.144     albertel 1043:     my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
                   1044: 					      [$courseid.':courseinit']);
1.139     raeburn  1045:     my ($tmp) = %entry_settings;
1.144     albertel 1046:     unless ($tmp =~ /^error: 2 /) {
1.139     raeburn  1047:         $startpage = $entry_settings{$courseid.':courseinit'};
                   1048:     }
                   1049:     if ($startpage eq '') {
                   1050:         if (exists($env{'environment.course_init_display'})) {
                   1051:             $startpage = $env{'environment.course_init_display'};
                   1052:         }
                   1053:     }
                   1054:     return $startpage;
                   1055: }
                   1056: 
1.1       harris41 1057: 1;
                   1058: __END__
1.32      harris41 1059: 
                   1060: =head1 NAME
                   1061: 
                   1062: Apache::lonroles - User Roles Screen
                   1063: 
                   1064: =head1 SYNOPSIS
                   1065: 
                   1066: Invoked by /etc/httpd/conf/srm.conf:
                   1067: 
                   1068:  <Location /adm/roles>
                   1069:  PerlAccessHandler       Apache::lonacc
                   1070:  SetHandler perl-script
                   1071:  PerlHandler Apache::lonroles
                   1072:  ErrorDocument     403 /adm/login
                   1073:  ErrorDocument	  500 /adm/errorhandler
                   1074:  </Location>
1.64      bowersj2 1075: 
                   1076: =head1 OVERVIEW
                   1077: 
                   1078: =head2 Choosing Roles
                   1079: 
                   1080: C<lonroles> is a handler that allows a user to switch roles in
                   1081: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                   1082: default role that a user has before selecting a role, as widely as
                   1083: possible, but certain handlers for example need specification which
                   1084: course they should act on, etc. Both in this scenario, and when the
                   1085: handler determines via C<lonnet>'s C<&allowed> function that a certain
                   1086: action is not allowed, C<lonroles> is used as error handler. This
                   1087: allows the user to select another role which may have permission to do
                   1088: what they were trying to do. C<lonroles> can also be accessed via the
                   1089: B<CRS> button in the Remote Control. 
                   1090: 
                   1091: =begin latex
                   1092: 
                   1093: \begin{figure}
                   1094: \begin{center}
                   1095: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                   1096:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                   1097: \end{center}
                   1098: \end{figure}
                   1099: 
                   1100: =end latex
                   1101: 
                   1102: =head2 Role Initialization
                   1103: 
                   1104: 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 1105: 
                   1106: =head1 INTRODUCTION
                   1107: 
                   1108: This module enables a user to select what role he wishes to
                   1109: operate under (instructor, student, teaching assistant, course
                   1110: coordinator, etc).  These roles are pre-established by the actions
                   1111: of upper-level users.
                   1112: 
                   1113: This is part of the LearningOnline Network with CAPA project
                   1114: described at http://www.lon-capa.org.
                   1115: 
                   1116: =head1 HANDLER SUBROUTINE
                   1117: 
                   1118: This routine is called by Apache and mod_perl.
                   1119: 
                   1120: =over 4
                   1121: 
                   1122: =item *
                   1123: 
                   1124: Roles Initialization (yes/no)
                   1125: 
                   1126: =item *
                   1127: 
                   1128: Get Error Message from Environment
                   1129: 
                   1130: =item *
                   1131: 
                   1132: Who is this?
                   1133: 
                   1134: =item *
                   1135: 
                   1136: Generate Page Output
                   1137: 
                   1138: =item *
                   1139: 
                   1140: Choice or no choice
                   1141: 
                   1142: =item *
                   1143: 
                   1144: Table
                   1145: 
                   1146: =item *
                   1147: 
                   1148: Privileges
                   1149: 
                   1150: =back
                   1151: 
                   1152: =cut

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