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

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

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