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

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

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