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

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

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