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

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

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