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