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