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