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