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