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

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

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