Annotation of loncom/auth/lonroles.pm, revision 1.200
1.1 harris41 1: # The LearningOnline Network with CAPA
2: # User Roles Screen
1.31 www 3: #
1.200 ! raeburn 4: # $Id: lonroles.pm,v 1.199 2008/07/05 23:27:34 raeburn 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.151 www 42: use Apache::lonpageflip();
1.167 albertel 43: use Apache::lonnavdisplay();
1.120 albertel 44: use GDBM_File;
1.170 albertel 45: use LONCAPA qw(:DEFAULT :match);
1.149 www 46:
1.1 harris41 47:
1.62 matthew 48: sub redirect_user {
1.95 albertel 49: my ($r,$title,$url,$msg,$launch_nav) = @_;
1.62 matthew 50: $msg = $title if (! defined($msg));
1.73 www 51: &Apache::loncommon::content_type($r,'text/html');
1.62 matthew 52: &Apache::loncommon::no_cache($r);
53: $r->send_http_header;
54: my $swinfo=&Apache::lonmenu::rawconfig();
1.96 albertel 55: my $navwindow;
1.95 albertel 56: if ($launch_nav eq 'on') {
1.166 albertel 57: $navwindow.=&Apache::lonnavdisplay::launch_win('now',undef,undef,
58: ($url =~ m-^/adm/whatsnew-));
1.96 albertel 59: } else {
60: $navwindow.=&Apache::lonnavmaps::close();
1.95 albertel 61: }
1.147 albertel 62: my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
63: {'redirect' => [1,$url],});
64: my $end_page = &Apache::loncommon::end_page();
65:
1.92 www 66: # Note to style police:
67: # This must only replace the spaces, nothing else, or it bombs elsewhere.
68: $url=~s/ /\%20/g;
1.93 albertel 69: $r->print(<<ENDREDIR);
1.147 albertel 70: $start_page
1.96 albertel 71: <script type="text/javascript">
1.62 matthew 72: $swinfo
73: </script>
1.96 albertel 74: $navwindow
1.62 matthew 75: <h1>$msg</h1>
1.147 albertel 76: $end_page
1.62 matthew 77: ENDREDIR
78: return;
79: }
80:
1.150 www 81: sub error_page {
82: my ($r,$error,$dest)=@_;
83: &Apache::loncommon::content_type($r,'text/html');
84: &Apache::loncommon::no_cache($r);
85: $r->send_http_header;
86: return OK if $r->header_only;
87: $r->print(&Apache::loncommon::start_page('Problems during Course Initialization').
88: '<script type="text/javascript">'.
89: &Apache::lonmenu::rawconfig().'</script>'.
90: '<p>'.&mt('The following problems occurred:').
91: $error.
1.156 albertel 92: '</p><br /><a href="'.$dest.'">'.&mt('Continue').'</a>'.
1.150 www 93: &Apache::loncommon::end_page());
94: }
95:
1.1 harris41 96: sub handler {
1.10 www 97:
1.1 harris41 98: my $r = shift;
99:
1.6 www 100: my $now=time;
1.118 albertel 101: my $then=$env{'user.login.time'};
1.6 www 102: my $envkey;
1.107 raeburn 103: my %dcroles = ();
104: my $numdc = &check_fordc(\%dcroles,$then);
1.148 albertel 105: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.10 www 106:
1.6 www 107: # ================================================================== Roles Init
1.118 albertel 108: if ($env{'form.selectrole'}) {
1.188 www 109:
110: my $locknum=&Apache::lonnet::get_locks();
111: if ($locknum) { return 409; }
112:
1.134 www 113: if ($env{'form.newrole'}) {
114: $env{'form.'.$env{'form.newrole'}}=1;
115: }
1.118 albertel 116: if ($env{'request.course.id'}) {
1.185 raeburn 117: # Check if user is CC trying to select a course role
118: if ($env{'form.switchrole'}) {
119: if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
120: &adhoc_course_role($then);
121: }
122: }
1.118 albertel 123: my %temp=('logout_'.$env{'request.course.id'} => time);
1.33 www 124: &Apache::lonnet::put('email_status',\%temp);
1.118 albertel 125: &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
1.100 albertel 126: }
1.186 raeburn 127: &Apache::lonnet::appenv({"request.course.id" => '',
128: "request.course.fn" => '',
129: "request.course.uri" => '',
130: "request.course.sec" => '',
131: "request.role" => 'cm',
132: "request.role.adv" => $env{'user.adv'},
133: "request.role.domain" => $env{'user.domain'}});
1.182 www 134: # Check if user is a DC trying to enter a course or author space and needs privs to be created
1.107 raeburn 135: if ($numdc > 0) {
1.118 albertel 136: foreach my $envkey (keys %env) {
1.182 www 137: # Is this an ad-hoc CC-role?
1.146 raeburn 138: if (my ($domain,$coursenum) =
1.171 albertel 139: ($envkey =~ m-^form\.cc\./($match_domain)/($match_courseid)$-)) {
1.146 raeburn 140: if ($dcroles{$domain}) {
1.182 www 141: &check_privs($domain,$coursenum,$then,$now,'cc');
142: }
143: last;
144: }
1.193 raeburn 145: # Is this an ad-hoc CA-role?
1.183 www 146: if (my ($domain,$user) =
147: ($envkey =~ m-^form\.ca\./($match_domain)/($match_username)$-)) {
1.193 raeburn 148: # Check if author blocked ca-access
1.190 www 149: my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],$domain,$user);
150: if ($blocked{'domcoord.author'} eq 'blocked') {
151: delete($env{$envkey});
152: $env{'user.error.msg'}=':::1:User '.$user.' in domain '.$domain.' blocked domain coordinator access';
153: last;
154: }
1.193 raeburn 155: if ($dcroles{$domain}) {
156: my ($server_status,$home) = &check_author_homeserver($user,$domain);
157: if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
158: &check_privs($domain,$user,$then,$now,'ca');
159: if ($server_status eq 'switchserver') {
160: my $trolecode = 'ca./'.$domain.'/'.$user;
161: my $switchserver = '/adm/switchserver?'
162: .'otherserver='.$home.'&role='.$trolecode;
163: $r->internal_redirect($switchserver);
164: }
165: } else {
166: delete($env{$envkey});
167: }
1.183 www 168: } else {
169: delete($env{$envkey});
1.182 www 170: }
171: last;
172: }
1.107 raeburn 173: }
174: }
175:
1.118 albertel 176: foreach $envkey (keys %env) {
1.40 matthew 177: next if ($envkey!~/^user\.role\./);
1.102 raeburn 178: my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
179: &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.118 albertel 180: if ($env{'form.'.$trolecode}) {
1.55 albertel 181: if ($tstatus eq 'is') {
182: $where=~s/^\///;
183: my ($cdom,$cnum,$csec)=split(/\//,$where);
1.137 raeburn 184: # check for course groups
185: my %coursegroups = &Apache::lonnet::get_active_groups(
186: $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
187: my $cgrps = join(':',keys(%coursegroups));
188:
1.111 albertel 189: # store role if recent_role list being kept
1.118 albertel 190: if ($env{'environment.recentroles'}) {
1.158 albertel 191: my %frozen_roles =
192: &Apache::lonhtmlcommon::get_recent_frozen('roles',$env{'environment.recentrolesn'});
1.111 albertel 193: &Apache::lonhtmlcommon::store_recent('roles',
1.158 albertel 194: $trolecode,' ',$frozen_roles{$trolecode});
1.111 albertel 195: }
196:
197:
1.53 www 198: # check for keyed access
1.55 albertel 199: if (($role eq 'st') &&
1.118 albertel 200: ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
1.89 www 201: # who is key authority?
202: my $authdom=$cdom;
203: my $authnum=$cnum;
1.118 albertel 204: if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
1.89 www 205: ($authnum,$authdom)=
1.172 albertel 206: split(/:/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
1.89 www 207: }
208: # check with key authority
209: unless (&Apache::lonnet::validate_access_key(
1.118 albertel 210: $env{'environment.key.'.$cdom.'_'.$cnum},
1.89 www 211: $authdom,$authnum)) {
1.53 www 212: # there is no valid key
1.118 albertel 213: if ($env{'form.newkey'}) {
1.53 www 214: # student attempts to register a new key
1.89 www 215: &Apache::loncommon::content_type($r,'text/html');
216: &Apache::loncommon::no_cache($r);
217: $r->send_http_header;
218: my $swinfo=&Apache::lonmenu::rawconfig();
1.147 albertel 219: my $start_page=&Apache::loncommon::start_page
1.89 www 220: ('Verifying Access Key to Unlock this Course');
1.147 albertel 221: my $end_page=&Apache::loncommon::end_page();
1.90 www 222: my $buttontext=&mt('Enter Course');
223: my $message=&mt('Successfully registered key');
224: my $assignresult=
225: &Apache::lonnet::assign_access_key(
1.118 albertel 226: $env{'form.newkey'},
1.90 www 227: $authdom,$authnum,
1.91 www 228: $cdom,$cnum,
1.118 albertel 229: $env{'user.domain'},
230: $env{'user.name'},
1.90 www 231: 'Assigned from '.$ENV{'REMOTE_ADDR'}.' at '.localtime().' for '.
232: $trolecode);
233: unless ($assignresult eq 'ok') {
234: $assignresult=~s/^error\:\s*//;
235: $message=&mt($assignresult).
236: '<br /><a href="/adm/logout">'.
1.89 www 237: &mt('Logout').'</a>';
1.90 www 238: $buttontext=&mt('Re-Enter Key');
239: }
1.89 www 240: $r->print(<<ENDENTEREDKEY);
1.147 albertel 241: $start_page
1.179 raeburn 242: <script type="text/javascript">
1.89 www 243: $swinfo
244: </script>
245: <form method="post">
246: <input type="hidden" name="selectrole" value="1" />
247: <input type="hidden" name="$trolecode" value="1" />
1.90 www 248: <font size="+2">$message</font><br />
1.89 www 249: <input type="submit" value="$buttontext" />
250: </form>
1.147 albertel 251: $end_page
1.89 www 252: ENDENTEREDKEY
253: return OK;
1.55 albertel 254: } else {
1.53 www 255: # print form to enter a new key
1.73 www 256: &Apache::loncommon::content_type($r,'text/html');
1.55 albertel 257: &Apache::loncommon::no_cache($r);
258: $r->send_http_header;
259: my $swinfo=&Apache::lonmenu::rawconfig();
1.147 albertel 260: my $start_page=&Apache::loncommon::start_page
1.55 albertel 261: ('Enter Access Key to Unlock this Course');
1.147 albertel 262: my $end_page=&Apache::loncommon::end_page();
1.55 albertel 263: $r->print(<<ENDENTERKEY);
1.147 albertel 264: $start_page
1.179 raeburn 265: <script type="text/javascript">
1.53 www 266: $swinfo
267: </script>
268: <form method="post">
1.89 www 269: <input type="hidden" name="selectrole" value="1" />
270: <input type="hidden" name="$trolecode" value="1" />
1.118 albertel 271: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
1.53 www 272: <input type="submit" value="Enter key" />
273: </form>
1.147 albertel 274: $end_page
1.53 www 275: ENDENTERKEY
1.55 albertel 276: return OK;
277: }
278: }
279: }
1.118 albertel 280: &Apache::lonnet::log($env{'user.domain'},
281: $env{'user.name'},
282: $env{'user.home'},
1.87 www 283: "Role ".$trolecode);
1.101 albertel 284:
1.56 www 285: &Apache::lonnet::appenv(
1.186 raeburn 286: {'request.role' => $trolecode,
287: 'request.role.domain' => $cdom,
288: 'request.course.sec' => $csec,
289: 'request.course.groups' => $cgrps});
1.101 albertel 290: my $tadv=0;
1.62 matthew 291:
1.125 www 292: if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
1.152 raeburn 293: my $msg;
1.55 albertel 294: my ($furl,$ferr)=
295: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
1.118 albertel 296: if (($env{'form.orgurl'}) &&
297: ($env{'form.orgurl'}!~/^\/adm\/flip/)) {
298: my $dest=$env{'form.orgurl'};
1.117 albertel 299: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186 raeburn 300: &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.150 www 301: if (($ferr) && ($tadv)) {
302: &error_page($r,$ferr,$dest);
303: } else {
304: $r->internal_redirect($dest);
305: }
1.55 albertel 306: return OK;
307: } else {
1.155 albertel 308: if (!$env{'request.course.id'}) {
1.55 albertel 309: &Apache::lonnet::appenv(
1.186 raeburn 310: {"request.course.id" => $cdom.'_'.$cnum});
1.61 www 311: $furl='/adm/roles?tryagain=1';
1.55 albertel 312: $msg=
1.157 albertel 313: '<h1><span class="LC_error">'.
1.162 albertel 314: &mt('Could not initialize [_1] at this time.',
315: $env{'course.'.$cdom.'_'.$cnum.'.description'}).
1.157 albertel 316: '</span></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
1.55 albertel 317: }
1.117 albertel 318: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186 raeburn 319: &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.152 raeburn 320:
1.150 www 321: if (($ferr) && ($tadv)) {
322: &error_page($r,$ferr,$furl);
323: } else {
324: # Check to see if the user is a CC entering a course
325: # for the first time
326: my (undef, undef, $role, $courseid) = split(/\./, $envkey);
327: if (substr($courseid, 0, 1) eq '/') {
328: $courseid = substr($courseid, 1);
329: }
330: $courseid =~ s/\//_/;
331: if ($role eq 'cc' && $env{'course.' . $courseid .
332: '.course.helper.not.run'}) {
333: $furl = "/adm/helper/course.initialization.helper";
334: # Send the user to the course they selected
335: } elsif ($env{'request.course.id'}) {
1.185 raeburn 336: if ($env{'form.destinationurl'}) {
337: my $dest = $env{'form.destinationurl'};
338: &redirect_user($r,&mt('Entering [_1]',
339: $env{'course.'.$courseid.'.description'}),
340: $dest,$msg,
341: $env{'environment.remotenavmap'});
342: return OK;
343: }
1.150 www 344: if (&Apache::lonnet::allowed('whn',
345: $env{'request.course.id'})
346: || &Apache::lonnet::allowed('whn',
347: $env{'request.course.id'}.'/'
348: .$env{'request.course.sec'})
349: ) {
350: my $startpage = &courseloadpage($courseid);
351: unless ($startpage eq 'firstres') {
1.162 albertel 352: $msg = &mt('Entering [_1] ....',
353: $env{'course.'.$courseid.'.description'});
1.150 www 354: &redirect_user($r,&mt('New in course'),
355: '/adm/whatsnew?refpage=start',$msg,
356: $env{'environment.remotenavmap'});
357: return OK;
358: }
359: }
360: }
1.151 www 361: # Are we allowed to look at the first resource?
1.169 albertel 362: if ($furl !~ m|^/adm/|) {
1.151 www 363: # Guess not ...
364: $furl=&Apache::lonpageflip::first_accessible_resource();
365: }
1.162 albertel 366: $msg = &mt('Entering [_1] ...',
367: $env{'course.'.$courseid.'.description'});
368: &redirect_user($r,&mt('Entering [_1]',
369: $env{'course.'.$courseid.'.description'}),
1.150 www 370: $furl,$msg,
371: $env{'environment.remotenavmap'});
1.58 bowersj2 372: }
1.124 albertel 373: return OK;
1.55 albertel 374: }
375: }
1.62 matthew 376: #
377: # Send the user to the construction space they selected
1.125 www 378: if ($role =~ /^(au|ca|aa)$/) {
1.62 matthew 379: my $redirect_url = '/priv/';
380: if ($role eq 'au') {
1.118 albertel 381: $redirect_url.=$env{'user.name'};
1.62 matthew 382: } else {
383: $where =~ /\/(.*)$/;
384: $redirect_url .= $1;
385: }
386: $redirect_url .= '/';
1.78 sakharuk 387: &redirect_user($r,&mt('Entering Construction Space'),
1.62 matthew 388: $redirect_url);
389: return OK;
390: }
1.104 raeburn 391: if ($role eq 'dc') {
1.108 raeburn 392: my $redirect_url = '/adm/menu/';
393: &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104 raeburn 394: $redirect_url);
1.108 raeburn 395: return OK;
1.104 raeburn 396: }
1.55 albertel 397: }
398: }
1.6 www 399: }
1.40 matthew 400: }
1.44 www 401:
1.10 www 402:
1.6 www 403: # =============================================================== No Roles Init
1.10 www 404:
1.73 www 405: &Apache::loncommon::content_type($r,'text/html');
1.30 albertel 406: &Apache::loncommon::no_cache($r);
1.10 www 407: $r->send_http_header;
408: return OK if $r->header_only;
409:
1.52 www 410: my $swinfo=&Apache::lonmenu::rawconfig();
1.147 albertel 411: my $start_page=&Apache::loncommon::start_page('User Roles');
1.134 www 412: my $standby=&mt('Role selected. Please stand by.');
1.135 albertel 413: $standby=~s/\n/\\n/g;
1.184 raeburn 414: my $noscript='<span class="LC_error">'.&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.').'<br />'.&mt('As this is not the case, most functionality in the system will be unavailable.').'</span><br />';
1.163 www 415:
1.10 www 416: $r->print(<<ENDHEADER);
1.147 albertel 417: $start_page
1.163 www 418: <br />
1.179 raeburn 419: <noscript>
420: $noscript
421: </noscript>
422: <script type="text/javascript">
1.26 www 423: $swinfo
424: window.focus();
1.134 www 425:
426: active=true;
427:
428: function enterrole (thisform,rolecode,buttonname) {
429: if (active) {
430: active=false;
431: document.title='$standby';
432: window.status='$standby';
433: thisform.newrole.value=rolecode;
434: thisform.submit();
435: } else {
436: alert('$standby');
437: }
438: }
1.26 www 439: </script>
1.10 www 440: ENDHEADER
1.6 www 441:
1.2 www 442: # ------------------------------------------ Get Error Message from Environment
443:
1.118 albertel 444: my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
445: if ($env{'user.error.msg'}) {
1.55 albertel 446: $r->log_reason(
1.118 albertel 447: "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12 www 448: }
1.1 harris41 449:
1.61 www 450: # ------------------------------------------------- Can this user re-init, etc?
1.6 www 451:
1.118 albertel 452: my $advanced=$env{'user.adv'};
1.61 www 453: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118 albertel 454: my $tryagain=$env{'form.tryagain'};
1.6 www 455:
1.2 www 456: # -------------------------------------------------------- Generate Page Output
1.6 www 457: # --------------------------------------------------------------- Error Header?
1.2 www 458: if ($error) {
1.187 bisitz 459: $r->print("<h1>".&mt('LON-CAPA Access Control')."</h1>");
1.174 albertel 460: $r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>");
461: if ($priv ne '') {
1.187 bisitz 462: $r->print(&mt('Access : ').&Apache::lonnet::plaintext($priv)."\n");
1.174 albertel 463: }
464: if ($fn ne '') {
1.187 bisitz 465: $r->print(&mt('Resource: ').&Apache::lonenc::check_encrypt($fn)."\n");
1.174 albertel 466: }
467: if ($msg ne '') {
1.187 bisitz 468: $r->print(&mt('Action : ').$msg."\n");
1.174 albertel 469: }
470: $r->print("</pre><hr />");
1.120 albertel 471: my $url=$fn;
472: my $last;
473: if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
474: &GDBM_READER(),0640)) {
475: $last=$hash{'last_known'};
476: untie(%hash);
477: }
1.149 www 478: if ($last) { $fn.='?symb='.&escape($last); }
1.120 albertel 479:
480: &Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
481: &Apache::lonenc::check_encrypt($fn));
1.2 www 482: } else {
1.118 albertel 483: if ($env{'user.error.msg'}) {
1.25 www 484: $r->print(
1.157 albertel 485: '<h3><span class="LC_error">'.
486: &mt('You need to choose another user role or enter a specific course for this function').'</span></h3>');
1.25 www 487: }
1.2 www 488: }
1.6 www 489: # -------------------------------------------------------- Choice or no choice?
1.2 www 490: if ($nochoose) {
1.177 www 491: $r->print("<h2>".&mt('Sorry ...')."</h2>\n<span class='LC_error'>".
492: &mt('This action is currently not authorized.').'</span>'.
1.150 www 493: &Apache::loncommon::end_page());
494: return OK;
1.6 www 495: } else {
1.18 www 496: if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
497: $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6 www 498: }
1.84 www 499: $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116 albertel 500: $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
501: $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134 www 502: $r->print('<input type="hidden" name="newrole" value="" />');
1.6 www 503: }
1.75 albertel 504: my (%roletext,%sortrole,%roleclass);
1.84 www 505: my $countactive=0;
1.191 raeburn 506: my $countfuture=0;
507: my $countwill=0;
1.84 www 508: my $inrole=0;
509: my $possiblerole='';
1.191 raeburn 510: my %futureroles;
511: my %roles_nextlogin;
1.118 albertel 512: foreach $envkey (sort keys %env) {
1.35 matthew 513: my $button = 1;
1.49 www 514: my $switchserver='';
1.75 albertel 515: my $roletext;
516: my $sortkey;
1.2 www 517: if ($envkey=~/^user\.role\./) {
1.102 raeburn 518: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
519: &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.136 raeburn 520: next if (!defined($role) || $role eq '' || $role =~ /^gr/);
1.102 raeburn 521: $tremark='';
522: $tpstart=' ';
523: $tpend=' ';
524: $tfont='#000000';
1.4 www 525: if ($tstart) {
1.74 www 526: $tpstart=&Apache::lonlocal::locallocaltime($tstart);
1.4 www 527: }
528: if ($tend) {
1.74 www 529: $tpend=&Apache::lonlocal::locallocaltime($tend);
1.4 www 530: }
1.118 albertel 531: if ($env{'request.role'} eq $trolecode) {
1.6 www 532: $tstatus='selected';
533: }
1.4 www 534: my $tbg;
1.164 albertel 535: if (($tstatus eq 'is')
536: || ($tstatus eq 'selected')
537: || ($tstatus eq 'will')
538: || ($tstatus eq 'future')
539: || ($env{'form.showall'})) {
1.35 matthew 540: if ($tstatus eq 'is') {
541: $tbg='#77FF77';
1.47 www 542: $tfont='#003300';
1.84 www 543: $possiblerole=$trolecode;
544: $countactive++;
1.35 matthew 545: } elsif ($tstatus eq 'future') {
546: $tbg='#FFFF77';
1.49 www 547: $button=0;
1.191 raeburn 548: $futureroles{$trolecode} = $tstart.':'.$tend;
549: $countfuture ++;
1.35 matthew 550: } elsif ($tstatus eq 'will') {
551: $tbg='#FFAA77';
1.72 www 552: $tremark.=&mt('Active at next login. ');
1.191 raeburn 553: $roles_nextlogin{$trolecode} = $tstart.':'.$tend;
554: $countwill ++;
1.35 matthew 555: } elsif ($tstatus eq 'expired') {
556: $tbg='#FF7777';
1.47 www 557: $tfont='#330000';
1.49 www 558: $button=0;
1.35 matthew 559: } elsif ($tstatus eq 'will_not') {
560: $tbg='#AAFF77';
1.72 www 561: $tremark.=&mt('Expired after logout. ');
1.35 matthew 562: } elsif ($tstatus eq 'selected') {
563: $tbg='#11CC55';
1.47 www 564: $tfont='#002200';
1.84 www 565: $inrole=1;
1.86 albertel 566: $countactive++;
1.72 www 567: $tremark.=&mt('Currently selected. ');
1.35 matthew 568: }
569: my $trole;
570: if ($role =~ /^cr\//) {
571: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.132 albertel 572: if ($tremark) { $tremark.='<br />'; }
573: $tremark.=&mt('Defined by ').$rauthor.
1.72 www 574: &mt(' at ').$rdomain.'.';
1.159 albertel 575: }
576: $trole=Apache::lonnet::plaintext($role);
1.35 matthew 577: my $ttype;
578: my $twhere;
579: my ($tdom,$trest,$tsection)=
580: split(/\//,Apache::lonnet::declutter($where));
581: # First, Co-Authorship roles
1.125 www 582: if (($role eq 'ca') || ($role eq 'aa')) {
1.39 stredwic 583: my $home = &Apache::lonnet::homeserver($trest,$tdom);
1.83 albertel 584: my $allowed=0;
585: my @ids=&Apache::lonnet::current_machine_ids();
586: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
587: if (!$allowed) {
1.49 www 588: $button=0;
1.130 albertel 589: $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49 www 590: }
1.35 matthew 591: #next if ($home eq 'no_host');
1.176 albertel 592: $home = &Apache::lonnet::hostname($home);
1.78 sakharuk 593: $ttype='Construction Space';
1.72 www 594: $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
595: ': '.$tdom.'<br />'.
596: ' '.&mt('Server').': '.$home;
1.118 albertel 597: $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.82 www 598: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
1.75 albertel 599: $sortkey=$role."$trest:$tdom";
1.35 matthew 600: } elsif ($role eq 'au') {
601: # Authors
602: my $home = &Apache::lonnet::homeserver
1.118 albertel 603: ($env{'user.name'},$env{'user.domain'});
1.83 albertel 604: my $allowed=0;
605: my @ids=&Apache::lonnet::current_machine_ids();
606: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
607: if (!$allowed) {
1.49 www 608: $button=0;
1.143 albertel 609: $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49 www 610: }
1.35 matthew 611: #next if ($home eq 'no_host');
1.176 albertel 612: $home = &Apache::lonnet::hostname($home);
1.78 sakharuk 613: $ttype='Construction Space';
1.72 www 614: $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
615: ': '.$home;
1.118 albertel 616: $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
617: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
1.75 albertel 618: $sortkey=$role;
1.35 matthew 619: } elsif ($trest) {
620: my $tcourseid=$tdom.'_'.$trest;
1.153 raeburn 621: $ttype = &Apache::loncommon::course_type($tcourseid);
622: $trole = &Apache::lonnet::plaintext($role,$ttype);
1.118 albertel 623: if ($env{'course.'.$tcourseid.'.description'}) {
624: $twhere=$env{'course.'.$tcourseid.'.description'};
1.80 albertel 625: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.72 www 626: unless ($twhere eq &mt('Currently not available')) {
1.55 albertel 627: $twhere.=' <font size="-2">'.
1.72 www 628: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49 www 629: '</font>';
1.55 albertel 630: }
1.8 www 631: } else {
1.105 raeburn 632: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1.35 matthew 633: if (%newhash) {
1.80 albertel 634: $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
1.77 albertel 635: "\0".$envkey;
1.49 www 636: $twhere=$newhash{'description'}.
637: ' <font size="-2">'.
1.72 www 638: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49 www 639: '</font>';
1.152 raeburn 640: $ttype = $newhash{'type'};
1.153 raeburn 641: $trole = &Apache::lonnet::plaintext($role,$ttype);
1.35 matthew 642: } else {
1.72 www 643: $twhere=&mt('Currently not available');
1.118 albertel 644: $env{'course.'.$tcourseid.'.description'}=$twhere;
1.80 albertel 645: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.152 raeburn 646: $ttype = 'Unavailable';
1.35 matthew 647: }
1.8 www 648: }
1.121 albertel 649: if ($tsection) {
1.175 albertel 650: $twhere.='<br />'.&mt('Section').': '.$tsection;
1.121 albertel 651: }
1.72 www 652: if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
1.35 matthew 653: } elsif ($tdom) {
1.78 sakharuk 654: $ttype='Domain';
1.35 matthew 655: $twhere=$tdom;
1.75 albertel 656: $sortkey=$role.$twhere;
1.35 matthew 657: } else {
1.78 sakharuk 658: $ttype='System';
1.72 www 659: $twhere=&mt('system wide');
1.75 albertel 660: $sortkey=$role.$twhere;
1.13 www 661: }
1.152 raeburn 662: $roletext.=&build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver);
1.75 albertel 663: $roletext{$envkey}=$roletext;
664: if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
665: $sortrole{$sortkey}=$envkey;
666: $roleclass{$envkey}=$ttype;
1.55 albertel 667: }
1.4 www 668: }
1.75 albertel 669: }
1.196 raeburn 670: if ($env{'user.adv'}) {
671: $r->print(
672: '<br /><span class="LC_rolesinfo"><label>'.&mt('Show all roles').': <input type="checkbox" name="showall"');
673: if ($env{'form.showall'}) { $r->print(' checked="checked" '); }
674: $r->print(' /></label><input type="submit" value="'.&mt('Display').'" /></span>');
675: } else {
676: if ($countactive > 0) {
677: my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
678: $r->print('<p>'.&mt('[_1]Visit the [_2]Course Catalog[_3] to view all [_4] LON-CAPA courses.','<b>','<a href="/adm/coursecatalog">','</a></b>',$domdesc).'<br />'.&mt('If a course is [_1]not[_2] in your list of current courses below, you may be able to enroll if self-enrollment is permitted.','<b>','</b>').'</p>');
679: }
680: }
681:
1.84 www 682: # No active roles
683: if ($countactive==0) {
684: if ($inrole) {
685: $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
686: } else {
687: $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
688: }
1.191 raeburn 689: &findcourse_advice($r);
690: $r->print('</form>');
691: if ($countfuture) {
692: $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
693: my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
694: $nochoose);
695: &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
696: \%roletext);
697: my $tremark='';
698: my $tfont='#003300';
699: if ($env{'request.role'} eq 'cm') {
700: $r->print('<tr bgcolor="#11CC55">');
701: $tremark=&mt('Currently selected. ');
702: $tfont='#002200';
703: } else {
704: $r->print('<tr bgcolor="#77FF77">');
705: }
706: $r->print('<td></td><td colspan="3"><font color="'.$tfont.'"><span class="LC_rolesinfo">'.&mt('No role specified').
1.192 raeburn 707: '</span></font></td><td><font color="'.$tfont.'">'.$tremark.
1.191 raeburn 708: ' </font></td></tr>'."\n");
709:
710: $r->print('</table>');
711: }
712: $r->print(&Apache::loncommon::end_page());
1.84 www 713: return OK;
714: # Is there only one choice?
1.118 albertel 715: } elsif (($countactive==1) && ($env{'request.role'} eq 'cm')) {
1.84 www 716: $r->print('<h3>'.&mt('Please stand by.').'</h3>'.
1.179 raeburn 717: '<input type="hidden" name="'.$possiblerole.'" value="1" />'.
718: '<noscript><br /><input type="submit" name="submit" value="'.&mt('Continue').'" /></noscript>');
1.84 www 719: $r->print("</form>\n");
720: $r->rflush();
1.179 raeburn 721: $r->print('<script type="text/javascript">document.forms.rolechoice.submit();</script>');
1.147 albertel 722: $r->print(&Apache::loncommon::end_page());
1.84 www 723: return OK;
724: }
725: # More than one possible role
726: # ----------------------------------------------------------------------- Table
1.181 albertel 727: unless ((!&Apache::lonmenu::show_course()) || ($nochoose)) {
1.173 albertel 728: $r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
1.84 www 729: }
1.191 raeburn 730: my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,$nochoose);
1.118 albertel 731: if ($env{'environment.recentroles'}) {
1.111 albertel 732: my %recent_roles =
1.118 albertel 733: &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111 albertel 734: my $output='';
735: foreach (sort(keys(%recent_roles))) {
736: if (defined($roletext{'user.role.'.$_})) {
737: $output.=$roletext{'user.role.'.$_};
1.170 albertel 738: if ($_ =~ m-dc\./($match_domain)/-
739: && $dcroles{$1}) {
1.192 raeburn 740: $output .= &adhoc_roles_row($1,'recent');
1.133 albertel 741: }
1.113 raeburn 742: } elsif ($numdc > 0) {
743: unless ($_ =~/^error\:/) {
744: $output.=&display_cc_role('user.role.'.$_);
745: }
746: }
1.111 albertel 747: }
748: if ($output) {
1.132 albertel 749: $r->print("<tr><td align='center' colspan='5'><font face='arial'>".
1.192 raeburn 750: &mt('Recent Roles')."</font></td></tr>");
1.111 albertel 751: $r->print($output);
1.114 raeburn 752: $doheaders ++;
1.111 albertel 753: }
754: }
755:
1.104 raeburn 756: if ($numdc > 0) {
1.112 raeburn 757: $r->print(&coursepick_jscript());
1.193 raeburn 758: $r->print(&Apache::loncommon::coursebrowser_javascript().
759: &Apache::loncommon::authorbrowser_javascript());
1.108 raeburn 760: }
1.191 raeburn 761: &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext);
1.14 www 762: my $tremark='';
1.47 www 763: my $tfont='#003300';
1.118 albertel 764: if ($env{'request.role'} eq 'cm') {
1.19 www 765: $r->print('<tr bgcolor="#11CC55">');
1.72 www 766: $tremark=&mt('Currently selected. ');
1.47 www 767: $tfont='#002200';
1.14 www 768: } else {
769: $r->print('<tr bgcolor="#77FF77">');
770: }
771: unless ($nochoose) {
1.118 albertel 772: if ($env{'request.role'} ne 'cm') {
1.134 www 773: $r->print('<td><input type="submit" value="'.
1.192 raeburn 774: &mt('Select').'" name="cm" /></td>');
1.55 albertel 775: } else {
776: $r->print('<td> </td>');
777: }
1.14 www 778: }
1.177 www 779: $r->print('<td colspan="3"><font color="'.$tfont.'"><span class="LC_rolesinfo">'.&mt('No role specified').
1.192 raeburn 780: '</span></font></td><td><font color="'.$tfont.'">'.$tremark.
1.47 www 781: ' </font></td></tr>'."\n");
1.4 www 782:
783: $r->print('</table>');
784: unless ($nochoose) {
785: $r->print("</form>\n");
786: }
1.22 harris41 787: # ------------------------------------------------------------ Privileges Info
1.118 albertel 788: if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.127 albertel 789: $r->print('<hr /><h2>Current Privileges</h2>');
1.175 albertel 790: $r->print(&privileges_info());
1.4 www 791: }
1.66 www 792: $r->print(&Apache::lonnet::getannounce());
1.65 www 793: if ($advanced) {
1.195 bisitz 794: $r->print('<p><small><i>'
795: .&mt('This is LON-CAPA [_1]',$r->dir_config('lonVersion'))
796: .'</i><br />'
1.197 raeburn 797: .'<a href="/adm/logout">'.&mt('Logout').'</a> '
798: .'<a href="/adm/coursecatalog">'.&mt('Course Catalog')
799: .'</small></p>');
1.65 www 800: }
1.147 albertel 801: $r->print(&Apache::loncommon::end_page());
1.1 harris41 802: return OK;
1.102 raeburn 803: }
804:
1.191 raeburn 805: sub roletable_headers {
806: my ($r,$roleclass,$sortrole,$nochoose) = @_;
807: my $doheaders;
808: if ((ref($sortrole) eq 'HASH') && (ref($roleclass) eq 'HASH')) {
809: $r->print('<br /><table id="LC_rolesmenu"><tr>');
810: if (!$nochoose) { $r->print('<th> </th>'); }
811: $r->print('<th>'.&mt('User Role').'</th><th>'.&mt('Extent')
812: .'</th><th>'.&mt('Start').'</th><th>'.&mt('End')
813: .'</th></tr>'."\n");
814: $doheaders=-1;
815: my @roletypes = &roletypes();
816: foreach my $type (@roletypes) {
817: my $haverole=0;
818: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
819: if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
820: $haverole=1;
821: }
822: }
823: if ($haverole) { $doheaders++; }
824: }
825: }
826: return $doheaders;
827: }
828:
829: sub roletypes {
830: my @types = ('Domain','Construction Space','Course','Unavailable','System');
831: return @types;
832: }
833:
834: sub print_rolerows {
835: my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_;
836: if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
837: my @types = &roletypes();
838: foreach my $type (@types) {
839: my $output;
840: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
841: if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
842: if (ref($roletext) eq 'HASH') {
843: $output.=$roletext->{$sortrole->{$which}};
844: if ($sortrole->{$which} =~ m-dc\./($match_domain)/-) {
845: if (ref($dcroles) eq 'HASH') {
846: if ($dcroles->{$1}) {
1.192 raeburn 847: $output .= &adhoc_roles_row($1,'');
1.191 raeburn 848: }
849: }
850: }
851: }
852: }
853: }
854: if ($output) {
855: if ($doheaders > 0) {
856: $r->print("<tr>".
857: "<td align='center' colspan='5'><font face='arial'>".
858: &mt($type)."</font></td></tr>");
859: }
860: $r->print($output);
861: }
862: }
863: }
864: }
865:
866: sub findcourse_advice {
867: my ($r) = @_;
868: my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.200 ! raeburn 869: if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) {
1.191 raeburn 870: $r->print(&mt('If you were expecting to see an active role listed for a particular course in the [_1] domain, it may be missing for one of the following reasons:',$domdesc).'
871: <ul>
872: <li>'.&mt('The course has yet to be created.').'</li>
873: <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
874: <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
875: <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
876: <li>'.&mt('You registered for the course recently and there is a time lag between the time you register, and the time this information becomes available for the update of LON-CAPA course rosters.').'</li>
877: </ul>');
878: } else {
879: $r->print(&mt('If you were expecting to see an active role listed for a particular course, that course may not have been created yet.').'<br />');
880: }
881: $r->print('<p>'.&mt('The [_1]Course Catalog[_2] provides information about all [_3] classes for which LON-CAPA courses have been created.','<a href="/adm/coursecatalog">','</a>',$domdesc).'<br />');
882: $r->print(&mt('You can search the course catalog for courses which permit self-enrollment, if you would like to enroll in a course.').'</p>');
883: return;
884: }
885:
1.175 albertel 886: sub privileges_info {
887: my ($which) = @_;
888: my $output;
889:
890: $which ||= $env{'request.role'};
891:
892: foreach my $envkey (sort(keys(%env))) {
893: next if ($envkey!~/^user\.priv\.\Q$which\E\.(.*)/);
894:
895: my $where=$1;
896: my $ttype;
897: my $twhere;
898: my (undef,$tdom,$trest,$tsec)=split(m{/},$where);
899: if ($trest) {
900: if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
901: $ttype='Construction Space';
902: $twhere='User: '.$trest.', Domain: '.$tdom;
903: } else {
904: $ttype= &Apache::loncommon::course_type($tdom.'_'.$trest);
905: $twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
906: if ($tsec) {
907: my $sec_type = 'Section';
908: if (exists($env{"user.role.gr.$where"})) {
909: $sec_type = 'Group';
910: }
911: $twhere.=' ('.$sec_type.': '.$tsec.')';
912: }
913: }
914: } elsif ($tdom) {
915: $ttype='Domain';
916: $twhere=$tdom;
917: } else {
918: $ttype='System';
919: $twhere='/';
920: }
921: $output .= "\n<h3>".$ttype.': '.$twhere.'</h3>'."\n<ul>";
922: foreach my $priv (sort(split(/:/,$env{$envkey}))) {
923: next if (!$priv);
924:
925: my ($prv,$restr)=split(/\&/,$priv);
926: my $trestr='';
927: if ($restr ne 'F') {
928: $trestr.=' ('.
929: join(', ',
930: map { &Apache::lonnet::plaintext($_) }
931: (split('',$restr))).') ';
932: }
933: $output .= "\n\t".
934: '<li>'.&Apache::lonnet::plaintext($prv).$trestr.'</li>';
935: }
936: $output .= "\n".'</ul>';
937: }
938: return $output;
939: }
940:
1.102 raeburn 941: sub role_status {
942: my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
943: my @pwhere = ();
1.118 albertel 944: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.102 raeburn 945: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
946: unless (!defined($$role) || $$role eq '') {
947: $$where=join('.',@pwhere);
948: $$trolecode=$$role.'.'.$$where;
1.118 albertel 949: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
1.102 raeburn 950: $$tstatus='is';
1.105 raeburn 951: if ($$tstart && $$tstart>$then) {
952: $$tstatus='future';
953: if ($$tstart<$now) { $$tstatus='will'; }
1.102 raeburn 954: }
955: if ($$tend) {
956: if ($$tend<$then) {
957: $$tstatus='expired';
1.103 raeburn 958: } elsif ($$tend<$now) {
1.104 raeburn 959: $$tstatus='will_not';
1.102 raeburn 960: }
961: }
962: }
963: }
964: }
1.1 harris41 965:
1.110 raeburn 966: sub build_roletext {
1.152 raeburn 967: my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver) = @_;
1.177 www 968: my $roletext='<tr bgcolor="'.$tbg.'" class="LC_rolesmenu_'.$tstatus.'">';
1.132 albertel 969: my $is_dc=($trolecode =~ m/^dc\./);
970: my $rowspan=($is_dc) ? ''
971: : ' rowspan="2" ';
972:
1.110 raeburn 973: unless ($nochoose) {
1.134 www 974: my $buttonname=$trolecode;
975: $buttonname=~s/\W//g;
1.110 raeburn 976: if (!$button) {
977: if ($switchserver) {
1.177 www 978: $roletext.='<td'.$rowspan.'><span class="LC_rolesinfo"><a href="/adm/switchserver?'.
979: $switchserver.'">'.&mt('Switch Server').'</a></span></td>';
1.110 raeburn 980: } else {
1.164 albertel 981: $roletext.=('<td'.$rowspan.'> </td>');
1.110 raeburn 982: }
983: } elsif ($tstatus eq 'is') {
1.134 www 984: $roletext.='<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
985: &mt('Select').'" onClick="javascript:enterrole(this.form,\''.
1.192 raeburn 986: $trolecode."','".$buttonname.'\');" /></td>';
1.110 raeburn 987: } elsif ($tryagain) {
988: $roletext.=
1.134 www 989: '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
990: &mt('Try Selecting Again').'" onClick="javascript:enterrole(this.form,\''.
1.192 raeburn 991: $trolecode."','".$buttonname.'\');" /></td>';
1.110 raeburn 992: } elsif ($advanced) {
993: $roletext.=
1.134 www 994: '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
995: &mt('Re-Initialize').'" onClick="javascript:enterrole(this.form,\''.
1.192 raeburn 996: $trolecode."','".$buttonname.'\');" /></td>';
1.110 raeburn 997: } else {
1.132 albertel 998: $roletext.='<td'.$rowspan.'> </td>';
1.110 raeburn 999: }
1000: }
1.165 albertel 1001: if ($trolecode !~ m/^(dc|ca|au|aa)\./) {
1002: $tremark.=&Apache::lonannounce::showday(time,1,
1003: &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
1004: }
1.110 raeburn 1005: $roletext.='<td><font color="'.$tfont.'">'.$trole.
1.121 albertel 1006: '</font></td><td><font color="'.$tfont.'">'.$twhere.
1.110 raeburn 1007: '</font></td><td><font color="'.$tfont.'">'.$tpstart.
1008: '</font></td><td><font color="'.$tfont.'">'.$tpend.
1.132 albertel 1009: '</font></td></tr>';
1010: if (!$is_dc) {
1.177 www 1011: $roletext.='<tr bgcolor="'.$tbg.'"><td colspan="4"><font color="'.$tfont.'"><span class="LC_rolesinfo">'.$tremark.
1012: '</span> </font></td></tr><tr><td colspan="5" height="3"></td></tr>'."\n";
1.132 albertel 1013: }
1.110 raeburn 1014: return $roletext;
1015: }
1016:
1.193 raeburn 1017: sub check_author_homeserver {
1.183 www 1018: my ($uname,$udom)=@_;
1.193 raeburn 1019: if (($uname eq '') || ($udom eq '')) {
1020: return ('fail','');
1021: }
1.183 www 1022: my $home = &Apache::lonnet::homeserver($uname,$udom);
1.193 raeburn 1023: if (&Apache::lonnet::host_domain($home) ne $udom) {
1024: return ('fail',$home);
1025: }
1.183 www 1026: my @ids=&Apache::lonnet::current_machine_ids();
1.193 raeburn 1027: if (grep(/^\Q$home\E$/,@ids)) {
1028: return ('ok',$home);
1029: } else {
1030: return ('switchserver',$home);
1.183 www 1031: }
1032: }
1033:
1.110 raeburn 1034: sub check_privs {
1.182 www 1035: my ($cdom,$cnum,$then,$now,$checkrole) = @_;
1036: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.118 albertel 1037: if ($env{$cckey}) {
1.110 raeburn 1038: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
1039: &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1040: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.182 www 1041: &set_privileges($cdom,$cnum,$checkrole);
1.110 raeburn 1042: }
1043: } else {
1.182 www 1044: &set_privileges($cdom,$cnum,$checkrole);
1.110 raeburn 1045: }
1046: }
1047:
1.104 raeburn 1048: sub check_fordc {
1049: my ($dcroles,$then) = @_;
1050: my $numdc = 0;
1.118 albertel 1051: if ($env{'user.adv'}) {
1052: foreach my $envkey (sort keys %env) {
1.170 albertel 1053: if ($envkey=~/^user\.role\.dc\.\/($match_domain)\/$/) {
1.104 raeburn 1054: my $dcdom = $1;
1055: my $livedc = 1;
1.118 albertel 1056: my ($tstart,$tend)=split(/\./,$env{$envkey});
1.105 raeburn 1057: if ($tstart && $tstart>$then) { $livedc = 0; }
1058: if ($tend && $tend <$then) { $livedc = 0; }
1.104 raeburn 1059: if ($livedc) {
1060: $$dcroles{$dcdom} = $envkey;
1.105 raeburn 1061: $numdc++;
1.104 raeburn 1062: }
1063: }
1064: }
1065: }
1066: return $numdc;
1067: }
1068:
1.185 raeburn 1069: sub adhoc_course_role {
1070: my ($then) = @_;
1071: my ($cdom,$cnum);
1072: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1073: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1074: if (&check_forcc($cdom,$cnum,$then)) {
1075: my $setprivs;
1.198 raeburn 1076: if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
1.185 raeburn 1077: $setprivs = 1;
1078: } else {
1.198 raeburn 1079: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
1.185 raeburn 1080: if (($start && ($start>$then || $start == -1)) ||
1081: ($end && $end<$then)) {
1082: $setprivs = 1;
1083: }
1084: }
1085: if ($setprivs) {
1.198 raeburn 1086: if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
1.185 raeburn 1087: my $role = $1;
1088: my $custom_role = $2;
1089: my $usec = $3;
1090: if ($role eq 'cr') {
1.199 raeburn 1091: if ($custom_role =~ m-^/$match_domain/$match_username/\w+$-) {
1.185 raeburn 1092: $role .= $custom_role;
1093: } else {
1094: return;
1095: }
1096: }
1097: my (%userroles,%newrole,%newgroups);
1098: my $area = '/'.$cdom.'/'.$cnum;
1099: my $spec = $role.'.'.$area;
1100: if ($usec ne '') {
1101: $spec .= '/'.$usec;
1102: $area .= '/'.$usec;
1103: }
1104: &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
1105: &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
1106: my $adhocstart = $then-1;
1107: $userroles{'user.role.'.$spec} = $adhocstart.'.';
1.186 raeburn 1108: &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.185 raeburn 1109: }
1110: }
1111: }
1112: return;
1113: }
1114:
1115: sub check_forcc {
1116: my ($cdom,$cnum,$then) = @_;
1117: my $is_cc;
1118: if ($cdom ne '' && $cnum ne '') {
1119: if (&Apache::lonnet::is_course($cdom,$cnum)) {
1120: my $envkey = 'user.role.cc./'.$cdom.'/'.$cnum;
1121: if (defined($env{$envkey})) {
1122: $is_cc = 1;
1123: my ($tstart,$tend)=split(/\./,$env{$envkey});
1124: if ($tstart && $tstart>$then) { $is_cc = 0; }
1125: if ($tend && $tend <$then) { $is_cc = 0; }
1126: }
1127: }
1128: }
1129: return $is_cc;
1130: }
1131:
1.108 raeburn 1132: sub courselink {
1.193 raeburn 1133: my ($dcdom,$rowtype) = @_;
1.109 raeburn 1134: my $courseform=&Apache::loncommon::selectcourse_link
1.152 raeburn 1135: ('rolechoice','dccourse'.$rowtype.'_'.$dcdom,
1136: 'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'.
1.173 albertel 1137: $dcdom,$dcdom,undef);
1.138 raeburn 1138: my $hiddenitems = '<input type="hidden" name="dcdomain'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
1139: '<input type="hidden" name="origdom'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
1140: '<input type="hidden" name="dccourse'.$rowtype.'_'.$dcdom.'" value="" />'.
1141: '<input type="hidden" name="coursedesc'.$rowtype.'_'.$dcdom.'" value="" />';
1.112 raeburn 1142: return $courseform.$hiddenitems;
1.109 raeburn 1143: }
1144:
1145: sub coursepick_jscript {
1.184 raeburn 1146: my %lt = &Apache::lonlocal::texthash(
1147: plsu => "Please use the 'Select Course' link to open a separate pick course window where you may select the course you wish to enter.",
1148: youc => 'You can only use this screen to select courses in the current domain.',
1149: );
1.104 raeburn 1150: my $verify_script = <<"END";
1.179 raeburn 1151: <script type="text/javascript">
1.108 raeburn 1152: function verifyCoursePick(caller) {
1153: var numbutton = getIndex(caller)
1.112 raeburn 1154: var pickedCourse = document.rolechoice.elements[numbutton+4].value
1155: var pickedDomain = document.rolechoice.elements[numbutton+2].value
1156: if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104 raeburn 1157: if (pickedCourse != '') {
1.108 raeburn 1158: if (numbutton != -1) {
1159: var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
1160: document.rolechoice.elements[numbutton+1].name = courseTarget
1161: document.rolechoice.submit()
1162: }
1.104 raeburn 1163: }
1164: else {
1.184 raeburn 1165: alert("$lt{'plsu'}");
1.104 raeburn 1166: }
1167: }
1168: else {
1.184 raeburn 1169: alert("$lt{'youc'}")
1.104 raeburn 1170: }
1171: }
1.109 raeburn 1172: function getIndex(caller) {
1.108 raeburn 1173: for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109 raeburn 1174: if (document.rolechoice.elements[i] == caller) {
1.108 raeburn 1175: return i;
1176: }
1177: }
1178: return -1;
1179: }
1.104 raeburn 1180: </script>
1181: END
1.109 raeburn 1182: return $verify_script;
1.104 raeburn 1183: }
1184:
1.193 raeburn 1185: sub coauthorlink {
1186: my ($dcdom,$rowtype) = @_;
1187: my $coauthorform=&Apache::loncommon::selectauthor_link('rolechoice',$dcdom);
1188: my $hiddenitems = '<input type="hidden" name="adhoccauname'.$rowtype.'_'.$dcdom.'" value="" />';
1189: return $coauthorform.$hiddenitems;
1190: }
1191:
1.113 raeburn 1192: sub display_cc_role {
1193: my $rolekey = shift;
1194: my $roletext;
1.118 albertel 1195: my $advanced = $env{'user.adv'};
1196: my $tryagain = $env{'form.tryagain'};
1.113 raeburn 1197: unless ($rolekey =~/^error\:/) {
1.171 albertel 1198: if ($rolekey =~ m-^user\.role.cc\./($match_domain)/($match_courseid)$-) {
1.113 raeburn 1199: my $tcourseid = $1.'_'.$2;
1200: my $trolecode = 'cc./'.$1.'/'.$2;
1201: my $twhere;
1.152 raeburn 1202: my $ttype;
1.113 raeburn 1203: my $tbg='#77FF77';
1204: my $tfont='#003300';
1205: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1206: if (%newhash) {
1207: $twhere=$newhash{'description'}.
1208: ' <font size="-2">'.
1209: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont).
1210: '</font>';
1.153 raeburn 1211: $ttype = $newhash{'type'};
1.113 raeburn 1212: } else {
1213: $twhere=&mt('Currently not available');
1.118 albertel 1214: $env{'course.'.$tcourseid.'.description'}=$twhere;
1.110 raeburn 1215: }
1.153 raeburn 1216: my $trole = &Apache::lonnet::plaintext('cc',$ttype);
1.113 raeburn 1217: $twhere.="<br />".&mt('Domain').":".$1;
1.152 raeburn 1218: $roletext = &build_roletext($trolecode,$1,$2,'is',$tryagain,$advanced,'',$tbg,$tfont,$trole,$twhere,'','','',1,'');
1.104 raeburn 1219: }
1220: }
1.152 raeburn 1221: return ($roletext);
1.104 raeburn 1222: }
1223:
1.192 raeburn 1224: sub adhoc_roles_row {
1.138 raeburn 1225: my ($dcdom,$rowtype) = @_;
1.132 albertel 1226: my $output = '<tr bgcolor="#77FF77">'.
1.192 raeburn 1227: ' <td colspan="5"><table><tr><td><span class="LC_rolesinfo">'
1228: .&mt('[_1]Ad hoc[_2] roles in domain [_3] --',
1229: '<span class="LC_cusr_emph">','</span>',$dcdom).'</span></td><td>';
1.193 raeburn 1230: my $selectcclink = &courselink($dcdom,$rowtype);
1.173 albertel 1231: my $ccrole = &Apache::lonnet::plaintext('cc');
1.182 www 1232: my $carole = &Apache::lonnet::plaintext('ca');
1.193 raeburn 1233: my $selectcalink = &coauthorlink($dcdom,$rowtype);
1.182 www 1234: $output.= '<span class="LC_rolesinfo">'.
1.193 raeburn 1235: &mt('[_1]: [_2]',$ccrole,$selectcclink).
1.192 raeburn 1236: '</span><br /></td><td> </td><td><span class="LC_rolesinfo">'.
1.193 raeburn 1237: &mt('[_1]: [_2]',$carole,$selectcalink).
1.192 raeburn 1238: '</span><br /></td></tr></table></td></tr>'.
1239: '<tr><td colspan="5" height="3"></td></tr>'."\n";
1.108 raeburn 1240: return $output;
1241: }
1242:
1.104 raeburn 1243: sub recent_filename {
1244: my $area=shift;
1.149 www 1245: return 'nohist_recent_'.&escape($area);
1.104 raeburn 1246: }
1247:
1.106 raeburn 1248: sub set_privileges {
1.182 www 1249: # role can be cc or ca
1250: my ($dcdom,$pickedcourse,$role) = @_;
1.106 raeburn 1251: my $area = '/'.$dcdom.'/'.$pickedcourse;
1252: my $spec = $role.'.'.$area;
1.168 albertel 1253: my %userroles = &Apache::lonnet::set_arearole($role,$area,'','',
1254: $env{'user.domain'},
1255: $env{'user.name'});
1.106 raeburn 1256: my %ccrole = ();
1257: &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
1.154 albertel 1258: my ($author,$adv)= &Apache::lonnet::set_userprivs(\%userroles,\%ccrole);
1.186 raeburn 1259: &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.184 raeburn 1260:
1.118 albertel 1261: &Apache::lonnet::log($env{'user.domain'},
1262: $env{'user.name'},
1263: $env{'user.home'},
1.106 raeburn 1264: "Role ".$role);
1265: &Apache::lonnet::appenv(
1.186 raeburn 1266: {'request.role' => $spec,
1.106 raeburn 1267: 'request.role.domain' => $dcdom,
1.186 raeburn 1268: 'request.course.sec' => ''});
1.106 raeburn 1269: my $tadv=0;
1270: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186 raeburn 1271: &Apache::lonnet::appenv({'request.role.adv' => $tadv});
1.106 raeburn 1272: }
1273:
1.139 raeburn 1274: sub courseloadpage {
1275: my ($courseid) = @_;
1276: my $startpage;
1.144 albertel 1277: my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
1278: [$courseid.':courseinit']);
1.139 raeburn 1279: my ($tmp) = %entry_settings;
1.144 albertel 1280: unless ($tmp =~ /^error: 2 /) {
1.139 raeburn 1281: $startpage = $entry_settings{$courseid.':courseinit'};
1282: }
1283: if ($startpage eq '') {
1284: if (exists($env{'environment.course_init_display'})) {
1285: $startpage = $env{'environment.course_init_display'};
1286: }
1287: }
1288: return $startpage;
1289: }
1290:
1.1 harris41 1291: 1;
1292: __END__
1.32 harris41 1293:
1294: =head1 NAME
1295:
1296: Apache::lonroles - User Roles Screen
1297:
1298: =head1 SYNOPSIS
1299:
1300: Invoked by /etc/httpd/conf/srm.conf:
1301:
1302: <Location /adm/roles>
1303: PerlAccessHandler Apache::lonacc
1304: SetHandler perl-script
1305: PerlHandler Apache::lonroles
1306: ErrorDocument 403 /adm/login
1307: ErrorDocument 500 /adm/errorhandler
1308: </Location>
1.64 bowersj2 1309:
1310: =head1 OVERVIEW
1311:
1312: =head2 Choosing Roles
1313:
1314: C<lonroles> is a handler that allows a user to switch roles in
1315: mid-session. LON-CAPA attempts to work with "No Role Specified", the
1316: default role that a user has before selecting a role, as widely as
1317: possible, but certain handlers for example need specification which
1318: course they should act on, etc. Both in this scenario, and when the
1319: handler determines via C<lonnet>'s C<&allowed> function that a certain
1320: action is not allowed, C<lonroles> is used as error handler. This
1321: allows the user to select another role which may have permission to do
1322: what they were trying to do. C<lonroles> can also be accessed via the
1323: B<CRS> button in the Remote Control.
1324:
1325: =begin latex
1326:
1327: \begin{figure}
1328: \begin{center}
1329: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
1330: \caption{\label{Sample_Roles_Screen}Sample Roles Screen}
1331: \end{center}
1332: \end{figure}
1333:
1334: =end latex
1335:
1336: =head2 Role Initialization
1337:
1338: 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 1339:
1340: =head1 INTRODUCTION
1341:
1342: This module enables a user to select what role he wishes to
1343: operate under (instructor, student, teaching assistant, course
1344: coordinator, etc). These roles are pre-established by the actions
1345: of upper-level users.
1346:
1347: This is part of the LearningOnline Network with CAPA project
1348: described at http://www.lon-capa.org.
1349:
1350: =head1 HANDLER SUBROUTINE
1351:
1352: This routine is called by Apache and mod_perl.
1353:
1354: =over 4
1355:
1356: =item *
1357:
1358: Roles Initialization (yes/no)
1359:
1360: =item *
1361:
1362: Get Error Message from Environment
1363:
1364: =item *
1365:
1366: Who is this?
1367:
1368: =item *
1369:
1370: Generate Page Output
1371:
1372: =item *
1373:
1374: Choice or no choice
1375:
1376: =item *
1377:
1378: Table
1379:
1380: =item *
1381:
1382: Privileges
1383:
1384: =back
1385:
1386: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>