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