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

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

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