File:
[LON-CAPA] /
loncom /
auth /
lonroles.pm
Revision
1.260:
download - view:
text,
annotated -
select for diffs
Thu Feb 24 23:29:10 2011 UTC (13 years, 5 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Bug 5932. Roles Screen usability improvements
- Work in progress.
- Check for new roles button to check for new roles or groups or privilege changes for existing groups since last log-in.
- Handles special cases of:
(a) expiration/deletion of current role
(b) section switching for student role
- New routines: &update_session_roles(), &curr_role_status(),
&gather_roleprivs(), &is_active_course().
- New item in %env - user.update.time records when last new role check occurred.
1: # The LearningOnline Network with CAPA
2: # User Roles Screen
3: #
4: # $Id: lonroles.pm,v 1.260 2011/02/24 23:29:10 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.
61:
62: =begin latex
63:
64: \begin{figure}
65: \begin{center}
66: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
67: \caption{\label{Sample_Roles_Screen}Sample Roles Screen}
68: \end{center}
69: \end{figure}
70:
71: =end latex
72:
73: =head2 Role Initialization
74:
75: 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.
76:
77: =head1 INTRODUCTION
78:
79: This module enables a user to select what role he wishes to
80: operate under (instructor, student, teaching assistant, course
81: coordinator, etc). These roles are pre-established by the actions
82: of upper-level users.
83:
84: This is part of the LearningOnline Network with CAPA project
85: described at http://www.lon-capa.org.
86:
87: =head1 HANDLER SUBROUTINE
88:
89: This routine is called by Apache and mod_perl.
90:
91: =over 4
92:
93: =item *
94:
95: Roles Initialization (yes/no)
96:
97: =item *
98:
99: Get Error Message from Environment
100:
101: =item *
102:
103: Who is this?
104:
105: =item *
106:
107: Generate Page Output
108:
109: =item *
110:
111: Choice or no choice
112:
113: =item *
114:
115: Table
116:
117: =item *
118:
119: Privileges
120:
121: =back
122:
123: =cut
124:
125:
126: package Apache::lonroles;
127:
128: use strict;
129: use Apache::lonnet;
130: use Apache::lonuserstate();
131: use Apache::Constants qw(:common);
132: use Apache::File();
133: use Apache::lonmenu;
134: use Apache::loncommon;
135: use Apache::lonhtmlcommon;
136: use Apache::lonannounce;
137: use Apache::lonlocal;
138: use Apache::lonpageflip();
139: use Apache::lonnavdisplay();
140: use Apache::loncoursequeueadmin;
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) = @_;
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:
153: # Breadcrumbs
154: my $brcrum = [{'href' => $url,
155: 'text' => 'Switching Role'},];
156: my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
157: {'redirect' => [1,$url],
158: 'bread_crumbs' => $brcrum,});
159: my $end_page = &Apache::loncommon::end_page();
160:
161: # Note to style police:
162: # This must only replace the spaces, nothing else, or it bombs elsewhere.
163: $url=~s/ /\%20/g;
164: $r->print(<<ENDREDIR);
165: $start_page
166: <p>$msg</p>
167: $end_page
168: ENDREDIR
169: return;
170: }
171:
172: sub error_page {
173: my ($r,$error,$dest)=@_;
174: &Apache::loncommon::content_type($r,'text/html');
175: &Apache::loncommon::no_cache($r);
176: $r->send_http_header;
177: return OK if $r->header_only;
178: # Breadcrumbs
179: my $brcrum = [{'href' => $dest,
180: 'text' => 'Problems during Course Initialization'},];
181: $r->print(&Apache::loncommon::start_page('Problems during Course Initialization',
182: undef,
183: {'bread_crumbs' => $brcrum,})
184: );
185: $r->print(
186: '<script type="text/javascript">'.
187: '// <![CDATA['.
188: &Apache::lonmenu::rawconfig().
189: '// ]]>'.
190: '</script>'.
191: '<p class="LC_error">'.&mt('The following problems occurred:').
192: '<br />'.
193: $error.
194: '</p><br /><a href="'.$dest.'">'.&mt('Continue').'</a>'
195: );
196: $r->print(&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: my $update=$env{'user.update.time'};
207: if (!$refresh) {
208: $refresh = $then;
209: }
210: if (!$update) {
211: $update = $then;
212: }
213:
214: # -------------------------------------------------------- Check for new roles
215: my $updateresult;
216: if ($env{'form.doupdate'}) {
217: my $show_course=&Apache::loncommon::show_course();
218: my $checkingtxt;
219: if ($show_course) {
220: $checkingtxt = &mt('Checking for new courses ...');
221: } else {
222: $checkingtxt = &mt('Checking for new roles ...');
223: }
224: $updateresult = '<div class="LC_info">'.$checkingtxt.'</div>';
225: $updateresult .= &update_session_roles();
226: &Apache::lonnet::appenv({'user.update.time' => $now});
227: $update = $now;
228: }
229:
230: my $envkey;
231: my %dcroles = ();
232: my $numdc = &check_fordc(\%dcroles,$update,$then);
233: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
234: my $loncaparev = $Apache::lonnet::perlvar{'lonVersion'};
235:
236: # ================================================================== Roles Init
237: if ($env{'form.selectrole'}) {
238:
239: my $locknum=&Apache::lonnet::get_locks();
240: if ($locknum) { return 409; }
241:
242: if ($env{'form.newrole'}) {
243: $env{'form.'.$env{'form.newrole'}}=1;
244: }
245: if ($env{'request.course.id'}) {
246: # Check if user is CC trying to select a course role
247: if ($env{'form.switchrole'}) {
248: my $switch_is_active;
249: if (defined($env{'user.role.'.$env{'form.switchrole'}})) {
250: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
251: if (!$end || $end > $now) {
252: if (!$start || $start < $update) {
253: $switch_is_active = 1;
254: }
255: }
256: }
257: unless ($switch_is_active) {
258: &adhoc_course_role($refresh,$update,$then);
259: }
260: }
261: my %temp=('logout_'.$env{'request.course.id'} => time);
262: &Apache::lonnet::put('email_status',\%temp);
263: &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
264: }
265: &Apache::lonnet::appenv({"request.course.id" => '',
266: "request.course.fn" => '',
267: "request.course.uri" => '',
268: "request.course.sec" => '',
269: "request.role" => 'cm',
270: "request.role.adv" => $env{'user.adv'},
271: "request.role.domain" => $env{'user.domain'}});
272: # Check if user is a DC trying to enter a course or author space and needs privs to be created
273: if ($numdc > 0) {
274: foreach my $envkey (keys %env) {
275: # Is this an ad-hoc Coordinator role?
276: if (my ($ccrole,$domain,$coursenum) =
277: ($envkey =~ m-^form\.(cc|co)\./($match_domain)/($match_courseid)$-)) {
278: if ($dcroles{$domain}) {
279: &Apache::lonnet::check_adhoc_privs($domain,$coursenum,
280: $update,$refresh,$now,$ccrole);
281: }
282: last;
283: }
284: # Is this an ad-hoc CA-role?
285: if (my ($domain,$user) =
286: ($envkey =~ m-^form\.ca\./($match_domain)/($match_username)$-)) {
287: if (($domain eq $env{'user.domain'}) && ($user eq $env{'user.name'})) {
288: delete($env{$envkey});
289: $env{'form.au./'.$domain.'/'} = 1;
290: my ($server_status,$home) = &check_author_homeserver($user,$domain);
291: if ($server_status eq 'switchserver') {
292: my $trolecode = 'au./'.$domain.'/';
293: my $switchserver = '/adm/switchserver?otherserver='.$home.'&role='.$trolecode;
294: $r->internal_redirect($switchserver);
295: }
296: last;
297: }
298: if (my ($castart,$caend) = ($env{'user.role.ca./'.$domain.'/'.$user} =~ /^(\d*)\.(\d*)$/)) {
299: if (((($castart) && ($castart < $now)) || !$castart) &&
300: ((!$caend) || (($caend) && ($caend > $now)))) {
301: my ($server_status,$home) = &check_author_homeserver($user,$domain);
302: if ($server_status eq 'switchserver') {
303: my $trolecode = 'ca./'.$domain.'/'.$user;
304: my $switchserver = '/adm/switchserver?otherserver='.$home.'&role='.$trolecode;
305: $r->internal_redirect($switchserver);
306: }
307: last;
308: }
309: }
310: # Check if author blocked ca-access
311: my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],$domain,$user);
312: if ($blocked{'domcoord.author'} eq 'blocked') {
313: delete($env{$envkey});
314: $env{'user.error.msg'}=':::1:User '.$user.' in domain '.$domain.' blocked domain coordinator access';
315: last;
316: }
317: if ($dcroles{$domain}) {
318: my ($server_status,$home) = &check_author_homeserver($user,$domain);
319: if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
320: &Apache::lonnet::check_adhoc_privs($domain,$user,$update,
321: $refresh,$now,'ca');
322: if ($server_status eq 'switchserver') {
323: my $trolecode = 'ca./'.$domain.'/'.$user;
324: my $switchserver = '/adm/switchserver?'
325: .'otherserver='.$home.'&role='.$trolecode;
326: $r->internal_redirect($switchserver);
327: }
328: } else {
329: delete($env{$envkey});
330: }
331: } else {
332: delete($env{$envkey});
333: }
334: last;
335: }
336: }
337: }
338:
339: foreach $envkey (keys %env) {
340: next if ($envkey!~/^user\.role\./);
341: my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
342: &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
343: \$trolecode,\$tstatus,\$tstart,\$tend);
344: if ($env{'form.'.$trolecode}) {
345: if ($tstatus eq 'is') {
346: $where=~s/^\///;
347: my ($cdom,$cnum,$csec)=split(/\//,$where);
348: if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
349: my $home = $env{'course.'.$cdom.'_'.$cnum.'.home'};
350: my @ids = &Apache::lonnet::current_machine_ids();
351: unless ($loncaparev eq '' && $home && grep(/^\Q$home\E$/,@ids)) {
352: my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
353: if ($curr_reqd_hash{'internal.releaserequired'} ne '') {
354: my ($switchserver,$switchwarning) =
355: &check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode,$curr_reqd_hash{'internal.releaserequired'});
356: if ($switchwarning ne '' || $switchserver ne '') {
357: &Apache::loncommon::content_type($r,'text/html');
358: &Apache::loncommon::no_cache($r);
359: $r->send_http_header;
360: my $end_page=&Apache::loncommon::end_page();
361: $r->print(&Apache::loncommon::start_page('Selected course unavailable on this server').
362: '<p class="LC_warning">');
363: if ($switchwarning) {
364: $r->print($switchwarning.'<br /><a href="/adm/roles">');
365: if (&Apache::loncommon::show_course()) {
366: $r->print(&mt('Display courses'));
367: } else {
368: $r->print(&mt('Display roles'));
369: }
370: $r->print('</a>');
371: } elsif ($switchserver) {
372: $r->print(&mt('This course requires a newer version of LON-CAPA than is installed on this server.').
373: '<br />'.
374: '<a href="/adm/switchserver?'.$switchserver.'">'.
375: &mt('Switch Server').
376: '</a>');
377: }
378: $r->print('</p>'.&Apache::loncommon::end_page());
379: return OK;
380: }
381: }
382: }
383: }
384: # check for course groups
385: my %coursegroups = &Apache::lonnet::get_active_groups(
386: $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
387: my $cgrps = join(':',keys(%coursegroups));
388:
389: # store role if recent_role list being kept
390: if ($env{'environment.recentroles'}) {
391: my %frozen_roles =
392: &Apache::lonhtmlcommon::get_recent_frozen('roles',$env{'environment.recentrolesn'});
393: &Apache::lonhtmlcommon::store_recent('roles',
394: $trolecode,' ',$frozen_roles{$trolecode});
395: }
396:
397:
398: # check for keyed access
399: if (($role eq 'st') &&
400: ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
401: # who is key authority?
402: my $authdom=$cdom;
403: my $authnum=$cnum;
404: if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
405: ($authnum,$authdom)=
406: split(/:/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
407: }
408: # check with key authority
409: unless (&Apache::lonnet::validate_access_key(
410: $env{'environment.key.'.$cdom.'_'.$cnum},
411: $authdom,$authnum)) {
412: # there is no valid key
413: if ($env{'form.newkey'}) {
414: # student attempts to register a new key
415: &Apache::loncommon::content_type($r,'text/html');
416: &Apache::loncommon::no_cache($r);
417: $r->send_http_header;
418: my $swinfo=&Apache::lonmenu::rawconfig();
419: my $start_page=&Apache::loncommon::start_page
420: ('Verifying Access Key to Unlock this Course');
421: my $end_page=&Apache::loncommon::end_page();
422: my $buttontext=&mt('Enter Course');
423: my $message=&mt('Successfully registered key');
424: my $assignresult=
425: &Apache::lonnet::assign_access_key(
426: $env{'form.newkey'},
427: $authdom,$authnum,
428: $cdom,$cnum,
429: $env{'user.domain'},
430: $env{'user.name'},
431: &mt('Assigned from [_1] at [_2] for [_3]'
432: ,$ENV{'REMOTE_ADDR'}
433: ,&Apache::lonlocal::locallocaltime()
434: ,$trolecode)
435: );
436: unless ($assignresult eq 'ok') {
437: $assignresult=~s/^error\:\s*//;
438: $message=&mt($assignresult).
439: '<br /><a href="/adm/logout">'.
440: &mt('Logout').'</a>';
441: $buttontext=&mt('Re-Enter Key');
442: }
443: $r->print(<<ENDENTEREDKEY);
444: $start_page
445: <script type="text/javascript">
446: // <![CDATA[
447: $swinfo
448: // ]]>
449: </script>
450: <form action="" method="post">
451: <input type="hidden" name="selectrole" value="1" />
452: <input type="hidden" name="$trolecode" value="1" />
453: <span class="LC_fontsize_large">$message</span><br />
454: <input type="submit" value="$buttontext" />
455: </form>
456: $end_page
457: ENDENTEREDKEY
458: return OK;
459: } else {
460: # print form to enter a new key
461: &Apache::loncommon::content_type($r,'text/html');
462: &Apache::loncommon::no_cache($r);
463: $r->send_http_header;
464: my $swinfo=&Apache::lonmenu::rawconfig();
465: my $start_page=&Apache::loncommon::start_page
466: ('Enter Access Key to Unlock this Course');
467: my $end_page=&Apache::loncommon::end_page();
468: $r->print(<<ENDENTERKEY);
469: $start_page
470: <script type="text/javascript">
471: // <![CDATA[
472: $swinfo
473: // ]]>
474: </script>
475: <form action="" method="post">
476: <input type="hidden" name="selectrole" value="1" />
477: <input type="hidden" name="$trolecode" value="1" />
478: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
479: <input type="submit" value="Enter key" />
480: </form>
481: $end_page
482: ENDENTERKEY
483: return OK;
484: }
485: }
486: }
487: &Apache::lonnet::log($env{'user.domain'},
488: $env{'user.name'},
489: $env{'user.home'},
490: "Role ".$trolecode);
491:
492: &Apache::lonnet::appenv(
493: {'request.role' => $trolecode,
494: 'request.role.domain' => $cdom,
495: 'request.course.sec' => $csec,
496: 'request.course.groups' => $cgrps});
497: my $tadv=0;
498:
499: if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
500: my $msg;
501: my ($furl,$ferr)=
502: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
503: if (($env{'form.orgurl'}) &&
504: ($env{'form.orgurl'}!~/^\/adm\/flip/)) {
505: my $dest=$env{'form.orgurl'};
506: if ($env{'form.symb'}) {
507: if ($dest =~ /\?/) {
508: $dest .= '&';
509: } else {
510: $dest .= '?'
511: }
512: $dest .= 'symb='.$env{'form.symb'};
513: }
514: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
515: &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
516: if (($ferr) && ($tadv)) {
517: &error_page($r,$ferr,$dest);
518: } else {
519: if ($dest =~ m{^/adm/coursedocs\?folderpath}) {
520: if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
521: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
522: &update_content_constraints($cdom,$cnum,$chome,$cdom.'_'.$cnum);
523: }
524: }
525: $r->internal_redirect($dest);
526: }
527: return OK;
528: } else {
529: if (!$env{'request.course.id'}) {
530: &Apache::lonnet::appenv(
531: {"request.course.id" => $cdom.'_'.$cnum});
532: $furl='/adm/roles?tryagain=1';
533: $msg='<p><span class="LC_error">'
534: .&mt('Could not initialize [_1] at this time.',
535: $env{'course.'.$cdom.'_'.$cnum.'.description'})
536: .'</span></p>'
537: .'<p>'.&mt('Please try again.').'</p>'
538: .'<p>'.$ferr.'</p>';
539: }
540: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
541: &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
542:
543: if (($ferr) && ($tadv)) {
544: &error_page($r,$ferr,$furl);
545: } else {
546: # Check to see if the user is a CC entering a course
547: # for the first time
548: my (undef, undef, $role, $courseid) = split(/\./, $envkey);
549: if (substr($courseid, 0, 1) eq '/') {
550: $courseid = substr($courseid, 1);
551: }
552: $courseid =~ s/\//_/;
553: if ((($role eq 'cc') || ($role eq 'co'))
554: && ($env{'course.' . $courseid .'.course.helper.not.run'})) {
555: $furl = "/adm/helper/course.initialization.helper";
556: # Send the user to the course they selected
557: } elsif ($env{'request.course.id'}) {
558: if ($env{'form.destinationurl'}) {
559: my $dest = $env{'form.destinationurl'};
560: if ($env{'form.destsymb'} ne '') {
561: my $esc_symb = &HTML::Entities::encode($env{'form.destsymb'},'"<>&');
562: $dest .= '?symb='.$esc_symb;
563: }
564: &redirect_user($r, &mt('Entering [_1]',
565: $env{'course.'.$courseid.'.description'}),
566: $dest, $msg);
567: return OK;
568: }
569: if (&Apache::lonnet::allowed('whn',
570: $env{'request.course.id'})
571: || &Apache::lonnet::allowed('whn',
572: $env{'request.course.id'}.'/'
573: .$env{'request.course.sec'})
574: ) {
575: my $startpage = &courseloadpage($courseid);
576: unless ($startpage eq 'firstres') {
577: $msg = &mt('Entering [_1] ...',
578: $env{'course.'.$courseid.'.description'});
579: &redirect_user($r, &mt('New in course'),
580: '/adm/whatsnew?refpage=start', $msg);
581: return OK;
582: }
583: }
584: }
585: # Are we allowed to look at the first resource?
586: if ($furl !~ m|^/adm/|) {
587: # Guess not ...
588: $furl=&Apache::lonpageflip::first_accessible_resource();
589: }
590: $msg = &mt('Entering [_1] ...',
591: $env{'course.'.$courseid.'.description'});
592: &redirect_user($r, &mt('Entering [_1]',
593: $env{'course.'.$courseid.'.description'}),
594: $furl, $msg);
595: }
596: return OK;
597: }
598: }
599: #
600: # Send the user to the construction space they selected
601: if ($role =~ /^(au|ca|aa)$/) {
602: my $redirect_url = '/priv/';
603: if ($role eq 'au') {
604: $redirect_url.=$env{'user.name'};
605: } else {
606: $where =~ /\/(.*)$/;
607: $redirect_url .= $1;
608: }
609: $redirect_url .= '/';
610: &redirect_user($r,&mt('Entering Construction Space'),
611: $redirect_url);
612: return OK;
613: }
614: if ($role eq 'dc') {
615: my $redirect_url = '/adm/menu/';
616: &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
617: $redirect_url);
618: return OK;
619: }
620: if ($role eq 'sc') {
621: my $redirect_url = '/adm/grades?command=scantronupload';
622: &redirect_user($r,&mt('Loading Data Upload Page'),
623: $redirect_url);
624: return OK;
625: }
626: }
627: }
628: }
629: }
630:
631:
632: # =============================================================== No Roles Init
633:
634: &Apache::loncommon::content_type($r,'text/html');
635: &Apache::loncommon::no_cache($r);
636: $r->send_http_header;
637: return OK if $r->header_only;
638:
639: my $crumbtext = 'User Roles';
640: my $pagetitle = 'My Roles';
641: my $recent = &mt('Recent Roles');
642: my $show_course=&Apache::loncommon::show_course();
643: if ($show_course) {
644: $crumbtext = 'Courses';
645: $pagetitle = 'My Courses';
646: $recent = &mt('Recent Courses');
647: }
648: my $brcrum =[{href=>"/adm/roles",text=>$crumbtext}];
649: my $swinfo=&Apache::lonmenu::rawconfig();
650: my $start_page=&Apache::loncommon::start_page($pagetitle,undef,{bread_crumbs=>$brcrum});
651: my $standby=&mt('Role selected. Please stand by.');
652: $standby=~s/\n/\\n/g;
653: 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 />';
654:
655: $r->print(<<ENDHEADER);
656: $start_page
657: <br />
658: <noscript>
659: $noscript
660: </noscript>
661: <script type="text/javascript">
662: // <![CDATA[
663: $swinfo
664: window.focus();
665:
666: active=true;
667:
668: function enterrole (thisform,rolecode,buttonname) {
669: if (active) {
670: active=false;
671: document.title='$standby';
672: window.status='$standby';
673: thisform.newrole.value=rolecode;
674: thisform.submit();
675: } else {
676: alert('$standby');
677: }
678: }
679:
680: function setToUpdate(thisform) {
681: thisform.doupdate.value='1';
682: thisform.selectrole.value='';
683: thisform.submit();
684: }
685:
686: // ]]>
687: </script>
688: ENDHEADER
689:
690: # ------------------------------------------ Get Error Message from Environment
691:
692: my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
693: if ($env{'user.error.msg'}) {
694: $r->log_reason(
695: "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
696: }
697:
698: # ------------------------------------------------- Can this user re-init, etc?
699:
700: my $advanced=$env{'user.adv'};
701: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
702: my $tryagain=$env{'form.tryagain'};
703: my $reinit=$env{'user.reinit'};
704: delete $env{'user.reinit'};
705:
706: # -------------------------------------------------------- Generate Page Output
707: # --------------------------------------------------------------- Error Header?
708: if ($error) {
709: $r->print("<h1>".&mt('LON-CAPA Access Control')."</h1>");
710: $r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>");
711: if ($priv ne '') {
712: $r->print(&mt('Access : ').&Apache::lonnet::plaintext($priv)."\n");
713: }
714: if ($fn ne '') {
715: $r->print(&mt('Resource: ').&Apache::lonenc::check_encrypt($fn)."\n");
716: }
717: if ($msg ne '') {
718: $r->print(&mt('Action : ').$msg."\n");
719: }
720: $r->print("</pre><hr />");
721: my $url=$fn;
722: my $last;
723: if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
724: &GDBM_READER(),0640)) {
725: $last=$hash{'last_known'};
726: untie(%hash);
727: }
728: if ($last) { $fn.='?symb='.&escape($last); }
729:
730: &Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
731: &Apache::lonenc::check_encrypt($fn));
732: } else {
733: if ($env{'user.error.msg'}) {
734: if ($reinit) {
735: $r->print(
736: '<h3><span class="LC_error">'.
737: &mt('As your session file for the course or community has expired, you will need to re-select it.').'</span></h3>');
738: } else {
739: $r->print(
740: '<h3><span class="LC_error">'.
741: &mt('You need to choose another user role or enter a specific course or community for this function.').
742: '</span></h3>');
743: }
744: }
745: }
746: if ($nochoose) {
747: $r->print("<h2>".&mt('Sorry ...')."</h2>\n<span class='LC_error'>".
748: &mt('This action is currently not authorized.').'</span>'.
749: &Apache::loncommon::end_page());
750: return OK;
751: } else {
752: $r->print($updateresult);
753: if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
754: $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
755: }
756: $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
757: $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
758: $r->print('<input type="hidden" name="selectrole" value="1" />');
759: $r->print('<input type="hidden" name="newrole" value="" />');
760: }
761: $r->rflush();
762:
763: my (%roletext,%sortrole,%roleclass,%futureroles,%timezones);
764: my ($countactive,$countfuture,$inrole,$possiblerole) =
765: &gather_roles($update,$refresh,$now,$reinit,$nochoose,\%roletext,\%sortrole,\%roleclass,
766: \%futureroles,\%timezones,$loncaparev);
767: $refresh = $now;
768: &Apache::lonnet::appenv({'user.refresh.time' => $refresh});
769: my $updatebutton = &mt('Check for role changes');
770: my $show_course=&Apache::loncommon::show_course();
771: if ($show_course) {
772: $updatebutton = &mt('Check for new courses');
773: }
774: my $do_update;
775: unless (($env{'form.source'} eq 'login') || ($env{'form.doupdate'})) {
776: $do_update = '<input type="hidden" name="doupdate" value="" />'.
777: '<input type="button" name="update" value="'.
778: $updatebutton.'" onclick="javascript:setToUpdate(this.form)" />';
779: }
780: if ($env{'user.adv'}) {
781: my $showall = '<label><input type="checkbox" name="showall"';
782: if ($env{'form.showall'}) {
783: $showall .= ' checked="checked" ';
784: }
785: $showall .= ' />'.&mt('Show all roles').'</label>'.
786: '<input type="submit" value="'.&mt('Update display').'" />';
787: if ($do_update) {
788: $r->print('<div class="LC_left_float"><fieldset>'.
789: '<legend>'. &mt('Display').'</legend>'.
790: $showall.'</fieldset></div>'.
791: '<div class="LC_left_float"><fieldset><legend>'.
792: &mt('Changes?').'</legend>'.
793: $do_update.'</fieldset></div><br clear="all" />');
794: } else {
795: $r->print($showall);
796: }
797: } else {
798: $r->print('<p>'.$do_update.'</p>');
799: if ($countactive > 0) {
800: $r->print(&Apache::loncoursequeueadmin::queued_selfenrollment());
801: my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
802: my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
803: $r->print(
804: '<p>'
805: .&mt('[_1]Visit the [_2]Course/Community Catalog[_3]'
806: .' to view all [_4] LON-CAPA courses and communities.'
807: ,'<b>'
808: ,'<a href="/adm/coursecatalog?showdom='.$esc_dom.'">'
809: ,'</a></b>',$domdesc)
810: .'<br />'
811: .&mt('If a course or community is [_1]not[_2] in your list of current courses and communities below,'
812: .' you may be able to enroll if self-enrollment is permitted.'
813: ,'<b>','</b>')
814: .'</p>'
815: );
816: }
817: }
818:
819: # No active roles
820: if ($countactive==0) {
821: if ($inrole) {
822: $r->print('<h2>'.&mt('Currently no additional roles, courses or communities').'</h2>');
823: } else {
824: $r->print('<h2>'.&mt('Currently no active roles, courses or communities').'</h2>');
825: }
826: &findcourse_advice($r);
827: &requestcourse_advice($r);
828: $r->print('</form>');
829: if ($countfuture) {
830: $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
831: my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
832: $nochoose);
833: &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
834: \%roletext);
835: my $tremark='';
836: my $tbg;
837: if ($env{'request.role'} eq 'cm') {
838: $tbg="LC_roles_selected";
839: $tremark=&mt('Currently selected.').' ';
840: } else {
841: $tbg="LC_roles_is";
842: }
843: $r->print(&Apache::loncommon::start_data_table_row()
844: .'<td class="'.$tbg.'"> </td>'
845: .'<td colspan="3">'
846: .&mt('No role specified')
847: .'</td>'
848: .'<td>'.$tremark.' </td>'
849: .&Apache::loncommon::end_data_table_row()
850: );
851:
852: $r->print(&Apache::loncommon::end_data_table());
853: }
854: $r->print(&Apache::loncommon::end_page());
855: return OK;
856: }
857: # ----------------------------------------------------------------------- Table
858:
859: if ($numdc > 0) {
860: $r->print(&coursepick_jscript());
861: $r->print(&Apache::loncommon::coursebrowser_javascript().
862: &Apache::loncommon::authorbrowser_javascript());
863: }
864:
865: unless ((!&Apache::loncommon::show_course()) || ($nochoose) || ($countactive==1)) {
866: $r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
867: }
868: if ($env{'form.destinationurl'}) {
869: $r->print('<input type="hidden" name="destinationurl" value="'.
870: $env{'form.destinationurl'}.'" />');
871: if ($env{'form.destsymb'} ne '') {
872: $r->print('<input type="hidden" name="destsymb" value="'.
873: $env{'form.destsymb'}.'" />');
874: }
875: }
876:
877: my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,$nochoose);
878: if ($env{'environment.recentroles'}) {
879: my %recent_roles =
880: &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
881: my $output='';
882: foreach my $role (sort(keys(%recent_roles))) {
883: if (ref($roletext{'user.role.'.$role}) eq 'ARRAY') {
884: $output.= &Apache::loncommon::start_data_table_row().
885: $roletext{'user.role.'.$role}->[0].
886: &Apache::loncommon::end_data_table_row();
887: if ($roletext{'user.role.'.$role}->[1] ne '') {
888: $output .= &Apache::loncommon::continue_data_table_row().
889: $roletext{'user.role.'.$role}->[1].
890: &Apache::loncommon::end_data_table_row();
891: }
892: if ($role =~ m{dc\./($match_domain)/}
893: && $dcroles{$1}) {
894: $output .= &adhoc_roles_row($1,'recent');
895: }
896: } elsif ($numdc > 0) {
897: unless ($role =~/^error\:/) {
898: my ($roletext,$role_text_end) = &display_cc_role('user.role.'.$role);
899: if ($roletext) {
900: $output.= &Apache::loncommon::start_data_table_row().
901: $roletext.
902: &Apache::loncommon::end_data_table_row();
903: if ($role_text_end) {
904: $output .= &Apache::loncommon::continue_data_table_row().
905: $role_text_end.
906: &Apache::loncommon::end_data_table_row();
907: }
908: }
909: }
910: }
911: }
912: if ($output) {
913: $r->print(&Apache::loncommon::start_data_table_empty_row()
914: .'<td align="center" colspan="5">'
915: .$recent
916: .'</td>'
917: .&Apache::loncommon::end_data_table_empty_row()
918: );
919: $r->print($output);
920: $doheaders ++;
921: }
922: }
923: &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext);
924: if ($countactive > 1) {
925: my $tremark='';
926: my $tbg;
927: if ($env{'request.role'} eq 'cm') {
928: $tbg="LC_roles_selected";
929: $tremark=&mt('Currently selected.').' ';
930: } else {
931: $tbg="LC_roles_is";
932: }
933: $r->print(&Apache::loncommon::start_data_table_row());
934: unless ($nochoose) {
935: if ($env{'request.role'} ne 'cm') {
936: $r->print('<td class="'.$tbg.'"><input type="submit" value="'.
937: &mt('Select').'" name="cm" /></td>');
938: } else {
939: $r->print('<td class="'.$tbg.'"> </td>');
940: }
941: }
942: $r->print('<td colspan="3">'
943: .&mt('No role specified')
944: .'</td>'
945: .'<td>'.$tremark.' </td>'
946: .&Apache::loncommon::end_data_table_row()
947: );
948: }
949: $r->print(&Apache::loncommon::end_data_table());
950: unless ($nochoose) {
951: $r->print("</form>\n");
952: }
953: # ------------------------------------------------------------ Privileges Info
954: if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
955: $r->print('<hr /><h2>'.&mt('Current Privileges').'</h2>');
956: $r->print(&privileges_info());
957: }
958: $r->print(&Apache::lonnet::getannounce());
959: if ($advanced) {
960: my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
961: $r->print('<p><small><i>'
962: .&mt('This LON-CAPA server is version [_1]',$r->dir_config('lonVersion'))
963: .'</i><br />'
964: .'<a href="/adm/logout">'.&mt('Logout').'</a> '
965: .'<a href="/adm/coursecatalog?showdom='.$esc_dom.'">'
966: .&mt('Course/Community Catalog')
967: .'</a></small></p>');
968: }
969: $r->print(&Apache::loncommon::end_page());
970: return OK;
971: }
972:
973: sub gather_roles {
974: my ($update,$refresh,$now,$reinit,$nochoose,$roletext,$sortrole,$roleclass,$futureroles,$timezones,$loncaparev) = @_;
975: my ($countactive,$countfuture,$inrole,$possiblerole) = (0,0,0,'');
976: my $advanced = $env{'user.adv'};
977: my $tryagain = $env{'form.tryagain'};
978: my @ids = &Apache::lonnet::current_machine_ids();
979: foreach my $envkey (sort(keys(%env))) {
980: my $button = 1;
981: my $switchserver='';
982: my $switchwarning;
983: my ($role_text,$role_text_end,$sortkey);
984: if ($envkey=~/^user\.role\./) {
985: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
986: &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
987: \$trolecode,\$tstatus,\$tstart,\$tend);
988: next if (!defined($role) || $role eq '' || $role =~ /^gr/);
989: $tremark='';
990: $tpstart=' ';
991: $tpend=' ';
992: if ($env{'request.role'} eq $trolecode) {
993: $tstatus='selected';
994: }
995: my $tbg;
996: if (($tstatus eq 'is')
997: || ($tstatus eq 'selected')
998: || ($tstatus eq 'future')
999: || ($env{'form.showall'})) {
1000: my $timezone = &role_timezone($where,$timezones);
1001: if ($tstart) {
1002: $tpstart=&Apache::lonlocal::locallocaltime($tstart,$timezone);
1003: }
1004: if ($tend) {
1005: $tpend=&Apache::lonlocal::locallocaltime($tend,$timezone);
1006: }
1007: if ($tstatus eq 'is') {
1008: $tbg='LC_roles_is';
1009: $possiblerole=$trolecode;
1010: $countactive++;
1011: } elsif ($tstatus eq 'future') {
1012: $tbg='LC_roles_future';
1013: $button=0;
1014: $futureroles->{$trolecode} = $tstart.':'.$tend;
1015: $countfuture ++;
1016: } elsif ($tstatus eq 'expired') {
1017: $tbg='LC_roles_expired';
1018: $button=0;
1019: } elsif ($tstatus eq 'will_not') {
1020: $tbg='LC_roles_will_not';
1021: $tremark.=&mt('Expired after logout.').' ';
1022: } elsif ($tstatus eq 'selected') {
1023: $tbg='LC_roles_selected';
1024: $inrole=1;
1025: $countactive++;
1026: $tremark.=&mt('Currently selected.').' ';
1027: }
1028: my $trole;
1029: if ($role =~ /^cr\//) {
1030: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1031: if ($tremark) { $tremark.='<br />'; }
1032: $tremark.=&mt('Customrole defined by [_1].',$rauthor.':'.$rdomain);
1033: }
1034: $trole=Apache::lonnet::plaintext($role);
1035: my $ttype;
1036: my $twhere;
1037: my ($tdom,$trest,$tsection)=
1038: split(/\//,Apache::lonnet::declutter($where));
1039: # First, Co-Authorship roles
1040: if (($role eq 'ca') || ($role eq 'aa')) {
1041: my $home = &Apache::lonnet::homeserver($trest,$tdom);
1042: my $allowed=0;
1043: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
1044: if (!$allowed) {
1045: $button=0;
1046: $switchserver='otherserver='.$home.'&role='.$trolecode;
1047: }
1048: #next if ($home eq 'no_host');
1049: $home = &Apache::lonnet::hostname($home);
1050: $ttype='Construction Space';
1051: $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
1052: ': '.$tdom.'<br />'.
1053: ' '.&mt('Server').': '.$home;
1054: $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1055: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
1056: $sortkey=$role."$trest:$tdom";
1057: } elsif ($role eq 'au') {
1058: # Authors
1059: my $home = &Apache::lonnet::homeserver
1060: ($env{'user.name'},$env{'user.domain'});
1061: my $allowed=0;
1062: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
1063: if (!$allowed) {
1064: $button=0;
1065: $switchserver='otherserver='.$home.'&role='.$trolecode;
1066: }
1067: #next if ($home eq 'no_host');
1068: $home = &Apache::lonnet::hostname($home);
1069: $ttype='Construction Space';
1070: $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
1071: ': '.$home;
1072: $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1073: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
1074: $sortkey=$role;
1075: } elsif ($trest) {
1076: my $tcourseid=$tdom.'_'.$trest;
1077: $ttype = &Apache::loncommon::course_type($tcourseid);
1078: $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1079: if ($env{'course.'.$tcourseid.'.description'}) {
1080: my $home=$env{'course.'.$tcourseid.'.home'};
1081: $twhere=$env{'course.'.$tcourseid.'.description'};
1082: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1083: $twhere = &HTML::Entities::encode($twhere,'"<>&');
1084: unless ($twhere eq &mt('Currently not available')) {
1085: $twhere.=' <span class="LC_fontsize_small">'.
1086: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
1087: '</span>';
1088: unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1089: my $required = $env{'course.'.$tcourseid.'.internal.releaserequired'};
1090: if ($required ne '') {
1091: ($switchserver,$switchwarning) =
1092: &check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1093: if ($switchserver || $switchwarning) {
1094: $button = 0;
1095: }
1096: }
1097: }
1098: }
1099: } else {
1100: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1101: if (%newhash) {
1102: $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
1103: "\0".$envkey;
1104: $twhere=&HTML::Entities::encode($newhash{'description'},'"<>&').
1105: ' <span class="LC_fontsize_small">'.
1106: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
1107: '</span>';
1108: $ttype = $newhash{'type'};
1109: $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1110: my $home = $newhash{'home'};
1111: unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1112: my $required = $newhash{'internal.releaserequired'};
1113: if ($required ne '') {
1114: ($switchserver,$switchwarning) =
1115: &check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1116: if ($switchserver || $switchwarning) {
1117: $button = 0;
1118: }
1119: }
1120: }
1121: } else {
1122: $twhere=&mt('Currently not available');
1123: $env{'course.'.$tcourseid.'.description'}=$twhere;
1124: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1125: $ttype = 'Unavailable';
1126: }
1127: }
1128: if ($tsection) {
1129: $twhere.='<br />'.&mt('Section').': '.$tsection;
1130: }
1131: if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
1132: } elsif ($tdom) {
1133: $ttype='Domain';
1134: $twhere=$tdom;
1135: $sortkey=$role.$twhere;
1136: } else {
1137: $ttype='System';
1138: $twhere=&mt('system wide');
1139: $sortkey=$role.$twhere;
1140: }
1141: ($role_text,$role_text_end) =
1142: &build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,
1143: $advanced,$tremark,$tbg,$trole,$twhere,$tpstart,
1144: $tpend,$nochoose,$button,$switchserver,$reinit,$switchwarning);
1145: $roletext->{$envkey}=[$role_text,$role_text_end];
1146: if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
1147: $sortrole->{$sortkey}=$envkey;
1148: $roleclass->{$envkey}=$ttype;
1149: }
1150: }
1151: }
1152: return ($countactive,$countfuture,$inrole,$possiblerole);
1153: }
1154:
1155: sub role_timezone {
1156: my ($where,$timezones) = @_;
1157: my $timezone;
1158: if (ref($timezones) eq 'HASH') {
1159: if ($where =~ m{^/($match_domain)/($match_courseid)}) {
1160: my $cdom = $1;
1161: my $cnum = $2;
1162: if ($cdom && $cnum) {
1163: if (!exists($timezones->{$cdom.'_'.$cnum})) {
1164: my $tz;
1165: if ($env{'course.'.$cdom.'_'.$cnum.'.description'}) {
1166: $tz = $env{'course.'.$cdom.'_'.$cnum.'.timezone'};
1167: } else {
1168: my %timehash =
1169: &Apache::lonnet::get('environment',['timezone'],$cdom,$cnum);
1170: $tz = $timehash{'timezone'};
1171: }
1172: if ($tz eq '') {
1173: if (!exists($timezones->{$cdom})) {
1174: my %domdefaults =
1175: &Apache::lonnet::get_domain_defaults($cdom);
1176: if ($domdefaults{'timezone_def'} eq '') {
1177: $timezones->{$cdom} = 'local';
1178: } else {
1179: $timezones->{$cdom} = $domdefaults{'timezone_def'};
1180: }
1181: }
1182: $timezones->{$cdom.'_'.$cnum} = $timezones->{$cdom};
1183: } else {
1184: $timezones->{$cdom.'_'.$cnum} =
1185: &Apache::lonlocal::gettimezone($tz);
1186: }
1187: }
1188: $timezone = $timezones->{$cdom.'_'.$cnum};
1189: }
1190: } else {
1191: my ($tdom) = ($where =~ m{^/($match_domain)});
1192: if ($tdom) {
1193: if (!exists($timezones->{$tdom})) {
1194: my %domdefaults = &Apache::lonnet::get_domain_defaults($tdom);
1195: if ($domdefaults{'timezone_def'} eq '') {
1196: $timezones->{$tdom} = 'local';
1197: } else {
1198: $timezones->{$tdom} = $domdefaults{'timezone_def'};
1199: }
1200: }
1201: $timezone = $timezones->{$tdom};
1202: }
1203: }
1204: if ($timezone eq 'local') {
1205: $timezone = undef;
1206: }
1207: }
1208: return $timezone;
1209: }
1210:
1211: sub roletable_headers {
1212: my ($r,$roleclass,$sortrole,$nochoose) = @_;
1213: my $doheaders;
1214: if ((ref($sortrole) eq 'HASH') && (ref($roleclass) eq 'HASH')) {
1215: $r->print('<br />'
1216: .&Apache::loncommon::start_data_table()
1217: .&Apache::loncommon::start_data_table_header_row()
1218: );
1219: if (!$nochoose) { $r->print('<th> </th>'); }
1220: $r->print('<th>'.&mt('User Role').'</th>'
1221: .'<th>'.&mt('Extent').'</th>'
1222: .'<th>'.&mt('Start').'</th>'
1223: .'<th>'.&mt('End').'</th>'
1224: .&Apache::loncommon::end_data_table_header_row()
1225: );
1226: $doheaders=-1;
1227: my @roletypes = &roletypes();
1228: foreach my $type (@roletypes) {
1229: my $haverole=0;
1230: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
1231: if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
1232: $haverole=1;
1233: }
1234: }
1235: if ($haverole) { $doheaders++; }
1236: }
1237: }
1238: return $doheaders;
1239: }
1240:
1241: sub roletypes {
1242: my @types = ('Domain','Construction Space','Course','Community','Unavailable','System');
1243: return @types;
1244: }
1245:
1246: sub print_rolerows {
1247: my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_;
1248: if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
1249: my @types = &roletypes();
1250: foreach my $type (@types) {
1251: my $output;
1252: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
1253: if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
1254: if (ref($roletext) eq 'HASH') {
1255: if (ref($roletext->{$sortrole->{$which}}) eq 'ARRAY') {
1256: $output.= &Apache::loncommon::start_data_table_row().
1257: $roletext->{$sortrole->{$which}}->[0].
1258: &Apache::loncommon::end_data_table_row();
1259: if ($roletext->{$sortrole->{$which}}->[1] ne '') {
1260: $output .= &Apache::loncommon::continue_data_table_row().
1261: $roletext->{$sortrole->{$which}}->[1].
1262: &Apache::loncommon::end_data_table_row();
1263: }
1264: }
1265: if ($sortrole->{$which} =~ m-dc\./($match_domain)/-) {
1266: if (ref($dcroles) eq 'HASH') {
1267: if ($dcroles->{$1}) {
1268: $output .= &adhoc_roles_row($1,'');
1269: }
1270: }
1271: }
1272: }
1273: }
1274: }
1275: if ($output) {
1276: if ($doheaders > 0) {
1277: $r->print(&Apache::loncommon::start_data_table_empty_row()
1278: .'<td align="center" colspan="5">'
1279: .&mt($type)
1280: .'</td>'
1281: .&Apache::loncommon::end_data_table_empty_row()
1282: );
1283: }
1284: $r->print($output);
1285: }
1286: }
1287: }
1288: }
1289:
1290: sub findcourse_advice {
1291: my ($r) = @_;
1292: my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1293: my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1294: if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) {
1295: $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).'
1296: <ul>
1297: <li>'.&mt('The course has yet to be created.').'</li>
1298: <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
1299: <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
1300: <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
1301: <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>
1302: </ul>');
1303: } else {
1304: $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 />');
1305: }
1306: $r->print('<h3>'.&mt('Self-Enrollment').'</h3>'.
1307: '<p>'.&mt('The [_1]Course/Community Catalog[_2] provides information about all [_3] classes for which LON-CAPA courses have been created, as well as any communities in the domain.','<a href="/adm/coursecatalog?showdom='.$esc_dom.'">','</a>',$domdesc).'<br />');
1308: $r->print(&mt('You can search for courses and communities which permit self-enrollment, if you would like to enroll in one.').'</p>'.
1309: &Apache::loncoursequeueadmin::queued_selfenrollment());
1310: return;
1311: }
1312:
1313: sub requestcourse_advice {
1314: my ($r) = @_;
1315: my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1316: my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1317: my (%can_request,%request_doms);
1318: &Apache::lonnet::check_can_request($env{'user.domain'},\%can_request,\%request_doms);
1319: if (keys(%request_doms) > 0) {
1320: my ($types,$typename) = &Apache::loncommon::course_types();
1321: if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) {
1322: $r->print('<h3>'.&mt('Request creation of a course or community').'</h3>'.
1323: '<p>'.&mt('You have rights to request the creation of courses and/or communities in the following domain(s):').'<ul>');
1324: my (@reqdoms,@reqtypes);
1325: foreach my $type (sort(keys(%request_doms))) {
1326: push(@reqtypes,$type);
1327: if (ref($request_doms{$type}) eq 'ARRAY') {
1328: my $domstr = join(', ',map { &Apache::lonnet::domain($_) } sort(@{$request_doms{$type}}));
1329: $r->print(
1330: '<li>'
1331: .&mt('[_1]'.$typename->{$type}.'[_2] in domain: [_3]',
1332: '<i>',
1333: '</i>',
1334: '<b>'.$domstr.'</b>')
1335: .'</li>'
1336: );
1337: foreach my $dom (@{$request_doms{$type}}) {
1338: unless (grep(/^\Q$dom\E/,@reqdoms)) {
1339: push(@reqdoms,$dom);
1340: }
1341: }
1342: }
1343: }
1344: my @showtypes;
1345: foreach my $type (@{$types}) {
1346: if (grep(/^\Q$type\E$/,@reqtypes)) {
1347: push(@showtypes,$type);
1348: }
1349: }
1350: my $requrl = '/adm/requestcourse';
1351: if (@reqdoms == 1) {
1352: $requrl .= '?showdom='.$reqdoms[0];
1353: }
1354: if (@showtypes > 0) {
1355: $requrl.=(($requrl=~/\?/)?'&':'?').'crstype='.$showtypes[0];
1356: }
1357: if (@reqdoms == 1 || @showtypes > 0) {
1358: $requrl .= '&state=crstype&action=new';
1359: }
1360: $r->print('</ul>'.&mt('Use the [_1]request form[_2] to submit a request for creation of a new course or community.','<a href="'.$requrl.'">','</a>').'</p>');
1361: }
1362: }
1363: return;
1364: }
1365:
1366: sub privileges_info {
1367: my ($which) = @_;
1368: my $output;
1369:
1370: $which ||= $env{'request.role'};
1371:
1372: foreach my $envkey (sort(keys(%env))) {
1373: next if ($envkey!~/^user\.priv\.\Q$which\E\.(.*)/);
1374:
1375: my $where=$1;
1376: my $ttype;
1377: my $twhere;
1378: my (undef,$tdom,$trest,$tsec)=split(m{/},$where);
1379: if ($trest) {
1380: if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
1381: $ttype='Construction Space';
1382: $twhere='User: '.$trest.', Domain: '.$tdom;
1383: } else {
1384: $ttype= &Apache::loncommon::course_type($tdom.'_'.$trest);
1385: $twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
1386: if ($tsec) {
1387: my $sec_type = 'Section';
1388: if (exists($env{"user.role.gr.$where"})) {
1389: $sec_type = 'Group';
1390: }
1391: $twhere.=' ('.$sec_type.': '.$tsec.')';
1392: }
1393: }
1394: } elsif ($tdom) {
1395: $ttype='Domain';
1396: $twhere=$tdom;
1397: } else {
1398: $ttype='System';
1399: $twhere='/';
1400: }
1401: $output .= "\n<h3>".&mt($ttype).': '.$twhere.'</h3>'."\n<ul>";
1402: foreach my $priv (sort(split(/:/,$env{$envkey}))) {
1403: next if (!$priv);
1404:
1405: my ($prv,$restr)=split(/\&/,$priv);
1406: my $trestr='';
1407: if ($restr ne 'F') {
1408: $trestr.=' ('.
1409: join(', ',
1410: map { &Apache::lonnet::plaintext($_) }
1411: (split('',$restr))).') ';
1412: }
1413: $output .= "\n\t".
1414: '<li>'.&Apache::lonnet::plaintext($prv).$trestr.'</li>';
1415: }
1416: $output .= "\n".'</ul>';
1417: }
1418: return $output;
1419: }
1420:
1421: sub build_roletext {
1422: my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$trole,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver,$reinit,$switchwarning) = @_;
1423: my ($roletext,$roletext_end);
1424: my $is_dc=($trolecode =~ m/^dc\./);
1425: my $rowspan=($is_dc) ? ''
1426: : ' rowspan="2" ';
1427:
1428: unless ($nochoose) {
1429: my $buttonname=$trolecode;
1430: $buttonname=~s/\W//g;
1431: if (!$button) {
1432: if ($switchserver) {
1433: $roletext.='<td'.$rowspan.' class="'.$tbg.'">'
1434: .'<a href="/adm/switchserver?'.$switchserver.'">'
1435: .&mt('Switch Server')
1436: .'</a></td>';
1437: } else {
1438: $roletext.=('<td'.$rowspan.' class="'.$tbg.'"> </td>');
1439: }
1440: if ($switchwarning) {
1441: if ($tremark eq '') {
1442: $tremark = $switchwarning;
1443: } else {
1444: $tremark .= '<br />'.$switchwarning;
1445: }
1446: }
1447: } elsif ($tstatus eq 'is') {
1448: $roletext.='<td'.$rowspan.' class="'.$tbg.'">'.
1449: '<input name="'.$buttonname.'" type="button" value="'.
1450: &mt('Select').'" onclick="javascript:enterrole(this.form,\''.
1451: $trolecode."','".$buttonname.'\');" /></td>';
1452: } elsif ($tryagain) {
1453: $roletext.=
1454: '<td'.$rowspan.' class="'.$tbg.'">'.
1455: '<input name="'.$buttonname.'" type="button" value="'.
1456: &mt('Try Selecting Again').'" onclick="javascript:enterrole(this.form,\''.
1457: $trolecode."','".$buttonname.'\');" /></td>';
1458: } elsif ($advanced) {
1459: $roletext.=
1460: '<td'.$rowspan.' class="'.$tbg.'">'.
1461: '<input name="'.$buttonname.'" type="button" value="'.
1462: &mt('Re-Initialize').'" onclick="javascript:enterrole(this.form,\''.
1463: $trolecode."','".$buttonname.'\');" /></td>';
1464: } elsif ($reinit) {
1465: $roletext.=
1466: '<td'.$rowspan.' class="'.$tbg.'">'.
1467: '<input name="'.$buttonname.'" type="button" value="'.
1468: &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1469: $trolecode."','".$buttonname.'\');" /></td>';
1470: } else {
1471: $roletext.=
1472: '<td'.$rowspan.' class="'.$tbg.'">'.
1473: '<input name="'.$buttonname.'" type="button" value="'.
1474: &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1475: $trolecode."','".$buttonname.'\');" /></td>';
1476: }
1477: }
1478: if ($trolecode !~ m/^(dc|ca|au|aa)\./) {
1479: $tremark.=&Apache::lonannounce::showday(time,1,
1480: &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
1481: }
1482: $roletext.='<td>'.$trole.'</td>'
1483: .'<td>'.$twhere.'</td>'
1484: .'<td>'.$tpstart.'</td>'
1485: .'<td>'.$tpend.'</td>';
1486: if (!$is_dc) {
1487: $roletext_end = '<td colspan="4">'.
1488: $tremark.' '.
1489: '</td>';
1490: }
1491: return ($roletext,$roletext_end);
1492: }
1493:
1494: sub check_needs_switchserver {
1495: my ($possiblerole) = @_;
1496: my $needs_switchserver;
1497: my ($role,$where) = split(/\./,$possiblerole,2);
1498: my (undef,$tdom,$twho) = split(/\//,$where);
1499: my ($server_status,$home);
1500: if (($role eq 'ca') || ($role eq 'aa')) {
1501: ($server_status,$home) = &check_author_homeserver($twho,$tdom);
1502: } else {
1503: ($server_status,$home) = &check_author_homeserver($env{'user.name'},
1504: $env{'user.domain'});
1505: }
1506: if ($server_status eq 'switchserver') {
1507: $needs_switchserver = 1;
1508: }
1509: return $needs_switchserver;
1510: }
1511:
1512: sub check_author_homeserver {
1513: my ($uname,$udom)=@_;
1514: if (($uname eq '') || ($udom eq '')) {
1515: return ('fail','');
1516: }
1517: my $home = &Apache::lonnet::homeserver($uname,$udom);
1518: if (&Apache::lonnet::host_domain($home) ne $udom) {
1519: return ('fail',$home);
1520: }
1521: my @ids=&Apache::lonnet::current_machine_ids();
1522: if (grep(/^\Q$home\E$/,@ids)) {
1523: return ('ok',$home);
1524: } else {
1525: return ('switchserver',$home);
1526: }
1527: }
1528:
1529: sub check_fordc {
1530: my ($dcroles,$update,$then) = @_;
1531: my $numdc = 0;
1532: if ($env{'user.adv'}) {
1533: foreach my $envkey (sort keys %env) {
1534: if ($envkey=~/^user\.role\.dc\.\/($match_domain)\/$/) {
1535: my $dcdom = $1;
1536: my $livedc = 1;
1537: my ($tstart,$tend)=split(/\./,$env{$envkey});
1538: my $limit = $update;
1539: if ($env{'request.role'} eq 'dc./'.$dcdom.'/') {
1540: $limit = $then;
1541: }
1542: if ($tstart && $tstart>$limit) { $livedc = 0; }
1543: if ($tend && $tend <$limit) { $livedc = 0; }
1544: if ($livedc) {
1545: $$dcroles{$dcdom} = $envkey;
1546: $numdc++;
1547: }
1548: }
1549: }
1550: }
1551: return $numdc;
1552: }
1553:
1554: sub adhoc_course_role {
1555: my ($refresh,$update,$then) = @_;
1556: my ($cdom,$cnum,$crstype);
1557: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1558: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1559: $crstype = &Apache::loncommon::course_type();
1560: if (&check_forcc($cdom,$cnum,$refresh,$update,$then,$crstype)) {
1561: my $setprivs;
1562: if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
1563: $setprivs = 1;
1564: } else {
1565: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
1566: if (($start && ($start>$refresh || $start == -1)) ||
1567: ($end && $end<$update)) {
1568: $setprivs = 1;
1569: }
1570: }
1571: if ($setprivs) {
1572: if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
1573: my $role = $1;
1574: my $custom_role = $2;
1575: my $usec = $3;
1576: if ($role eq 'cr') {
1577: if ($custom_role =~ m-^/$match_domain/$match_username/\w+$-) {
1578: $role .= $custom_role;
1579: } else {
1580: return;
1581: }
1582: }
1583: my (%userroles,%newrole,%newgroups,%group_privs);
1584: my %cgroups =
1585: &Apache::lonnet::get_active_groups($env{'user.domain'},
1586: $env{'user.name'},$cdom,$cnum);
1587: foreach my $group (keys(%cgroups)) {
1588: $group_privs{$group} =
1589: $env{'user.priv.cc./'.$cdom.'/'.$cnum.'./'.$cdom.'/'.$cnum.'/'.$group};
1590: }
1591: $newgroups{'/'.$cdom.'/'.$cnum} = \%group_privs;
1592: my $area = '/'.$cdom.'/'.$cnum;
1593: my $spec = $role.'.'.$area;
1594: if ($usec ne '') {
1595: $spec .= '/'.$usec;
1596: $area .= '/'.$usec;
1597: }
1598: &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
1599: &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1600: my $adhocstart = $refresh-1;
1601: $userroles{'user.role.'.$spec} = $adhocstart.'.';
1602: &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1603: }
1604: }
1605: }
1606: return;
1607: }
1608:
1609: sub check_forcc {
1610: my ($cdom,$cnum,$refresh,$update,$then,$crstype) = @_;
1611: my ($is_cc,$ccrole);
1612: if ($crstype eq 'Community') {
1613: $ccrole = 'co';
1614: } else {
1615: $ccrole = 'cc';
1616: }
1617: if ($cdom ne '' && $cnum ne '') {
1618: if (&Apache::lonnet::is_course($cdom,$cnum)) {
1619: my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum;
1620: if (defined($env{$envkey})) {
1621: $is_cc = 1;
1622: my ($tstart,$tend)=split(/\./,$env{$envkey});
1623: my $limit = $update;
1624: if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) {
1625: $limit = $then;
1626: }
1627: if ($tstart && $tstart>$refresh) { $is_cc = 0; }
1628: if ($tend && $tend <$limit) { $is_cc = 0; }
1629: }
1630: }
1631: }
1632: return $is_cc;
1633: }
1634:
1635: sub check_release_required {
1636: my ($loncaparev,$tcourseid,$trolecode,$required) = @_;
1637: my ($switchserver,$warning);
1638: if ($required ne '') {
1639: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
1640: my ($major,$minor) = ($loncaparev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
1641: if ($reqdmajor ne '' && $reqdminor ne '') {
1642: my $otherserver;
1643: if (($major eq '' && $minor eq '') ||
1644: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
1645: my ($userdomserver) = &Apache::lonnet::choose_server($env{'user.domain'});
1646: my $switchlcrev =
1647: &Apache::lonnet::get_server_loncaparev($env{'user.domain'},
1648: $userdomserver);
1649: my ($swmajor,$swminor) = ($switchlcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
1650: if (($swmajor eq '' && $swminor eq '') || ($reqdmajor > $swmajor) ||
1651: (($reqdmajor == $swmajor) && ($reqdminor > $swminor))) {
1652: my $cdom = $env{'course.'.$tcourseid.'.domain'};
1653: if ($cdom ne $env{'user.domain'}) {
1654: my ($coursedomserver,$coursehostname) = &Apache::lonnet::choose_server($cdom);
1655: my $serverhomeID = &Apache::lonnet::get_server_homeID($coursehostname);
1656: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
1657: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
1658: my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
1659: my $remoterev = &Apache::lonnet::get_server_loncaparev($serverhomedom,$coursedomserver);
1660: my $canhost =
1661: &Apache::lonnet::can_host_session($env{'user.domain'},
1662: $coursedomserver,
1663: $remoterev,
1664: $udomdefaults{'remotesessions'},
1665: $defdomdefaults{'hostedsessions'});
1666:
1667: if ($canhost) {
1668: $otherserver = $coursedomserver;
1669: } else {
1670: $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$tcourseid.'.internal.releaserequired'}).'<br />'. &mt("No suitable server could be found amongst servers in either your own domain or in the course's domain.");
1671: }
1672: } else {
1673: $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$tcourseid.'.internal.releaserequired'}).'<br />'.&mt("No suitable server could be found amongst servers in your own domain (which is also the course's domain).");
1674: }
1675: } else {
1676: $otherserver = $userdomserver;
1677: }
1678: }
1679: if ($otherserver ne '') {
1680: $switchserver = 'otherserver='.$otherserver.'&role='.$trolecode;
1681: }
1682: }
1683: }
1684: return ($switchserver,$warning);
1685: }
1686:
1687: sub update_content_constraints {
1688: my ($cdom,$cnum,$chome,$cid) = @_;
1689: my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
1690: my ($reqdmajor,$reqdminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
1691: my %checkresponsetypes;
1692: foreach my $key (keys(%Apache::lonnet::needsrelease)) {
1693: my ($item,$name,$value) = split(/:/,$key);
1694: if ($item eq 'resourcetag') {
1695: if ($name eq 'responsetype') {
1696: $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
1697: }
1698: }
1699: }
1700: my $navmap = Apache::lonnavmaps::navmap->new();
1701: if (defined($navmap)) {
1702: my %allresponses;
1703: foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
1704: my %responses = $res->responseTypes();
1705: foreach my $key (keys(%responses)) {
1706: next unless(exists($checkresponsetypes{$key}));
1707: $allresponses{$key} += $responses{$key};
1708: }
1709: }
1710: foreach my $key (keys(%allresponses)) {
1711: my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
1712: if (($major > $reqdmajor) || ($major == $reqdmajor && $minor > $reqdminor)) {
1713: ($reqdmajor,$reqdminor) = ($major,$minor);
1714: }
1715: }
1716: undef($navmap);
1717: }
1718: unless (($reqdmajor eq '') && ($reqdminor eq '')) {
1719: &Apache::lonnet::update_released_required($reqdmajor.'.'.$reqdminor,$cdom,$cnum,$chome,$cid);
1720: }
1721: return;
1722: }
1723:
1724: sub courselink {
1725: my ($dcdom,$rowtype) = @_;
1726: my $courseform=&Apache::loncommon::selectcourse_link
1727: ('rolechoice','dccourse'.$rowtype.'_'.$dcdom,
1728: 'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'.
1729: $dcdom,$dcdom,undef,'Course/Community');
1730: my $hiddenitems = '<input type="hidden" name="dcdomain'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
1731: '<input type="hidden" name="origdom'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
1732: '<input type="hidden" name="dccourse'.$rowtype.'_'.$dcdom.'" value="" />'.
1733: '<input type="hidden" name="coursedesc'.$rowtype.'_'.$dcdom.'" value="" />';
1734: return $courseform.$hiddenitems;
1735: }
1736:
1737: sub coursepick_jscript {
1738: my %lt = &Apache::lonlocal::texthash(
1739: plsu => "Please use the 'Select Course/Community' link to open a separate pick course window where you may select the course or community you wish to enter.",
1740: youc => 'You can only use this screen to select courses and communities in the current domain.',
1741: );
1742: my $verify_script = <<"END";
1743: <script type="text/javascript">
1744: // <![CDATA[
1745: function verifyCoursePick(caller) {
1746: var numbutton = getIndex(caller)
1747: var pickedCourse = document.rolechoice.elements[numbutton+4].value
1748: var pickedDomain = document.rolechoice.elements[numbutton+2].value
1749: if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1750: if (pickedCourse != '') {
1751: if (numbutton != -1) {
1752: var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
1753: document.rolechoice.elements[numbutton+1].name = courseTarget
1754: document.rolechoice.submit()
1755: }
1756: }
1757: else {
1758: alert("$lt{'plsu'}");
1759: }
1760: }
1761: else {
1762: alert("$lt{'youc'}")
1763: }
1764: }
1765: function getIndex(caller) {
1766: for (var i=0;i<document.rolechoice.elements.length;i++) {
1767: if (document.rolechoice.elements[i] == caller) {
1768: return i;
1769: }
1770: }
1771: return -1;
1772: }
1773: // ]]>
1774: </script>
1775: END
1776: return $verify_script;
1777: }
1778:
1779: sub coauthorlink {
1780: my ($dcdom,$rowtype) = @_;
1781: my $coauthorform=&Apache::loncommon::selectauthor_link('rolechoice',$dcdom);
1782: my $hiddenitems = '<input type="hidden" name="adhoccauname'.$rowtype.'_'.$dcdom.'" value="" />';
1783: return $coauthorform.$hiddenitems;
1784: }
1785:
1786: sub display_cc_role {
1787: my $rolekey = shift;
1788: my ($roletext,$roletext_end);
1789: my $advanced = $env{'user.adv'};
1790: my $tryagain = $env{'form.tryagain'};
1791: unless ($rolekey =~/^error\:/) {
1792: if ($rolekey =~ m{^user\.role\.(cc|co)\./($match_domain)/($match_courseid)$}) {
1793: my $ccrole = $1;
1794: my $tdom = $2;
1795: my $trest = $3;
1796: my $tcourseid = $tdom.'_'.$trest;
1797: my $trolecode = $ccrole.'./'.$tdom.'/'.$trest;
1798: my $twhere;
1799: my $ttype;
1800: my $tbg='LC_roles_is';
1801: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1802: if (%newhash) {
1803: $twhere=$newhash{'description'}.
1804: ' <span style="LC_fontsize_small">'.
1805: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
1806: '</span>';
1807: $ttype = $newhash{'type'};
1808: } else {
1809: $twhere=&mt('Currently not available');
1810: $env{'course.'.$tcourseid.'.description'}=$twhere;
1811: }
1812: my $trole = &Apache::lonnet::plaintext($ccrole,$ttype,$tcourseid);
1813: $twhere.="<br />".&mt('Domain').":".$tdom;
1814: ($roletext,$roletext_end) = &build_roletext($trolecode,$tdom,$trest,'is',$tryagain,$advanced,'',$tbg,$trole,$twhere,'','','',1,'');
1815: }
1816: }
1817: return ($roletext,$roletext_end);
1818: }
1819:
1820: sub adhoc_roles_row {
1821: my ($dcdom,$rowtype) = @_;
1822: my $output = &Apache::loncommon::continue_data_table_row()
1823: .' <td colspan="5">'
1824: .&mt('[_1]Ad hoc[_2] roles in domain [_3] --'
1825: ,'<span class="LC_cusr_emph">','</span>',$dcdom)
1826: .' ';
1827: my $selectcclink = &courselink($dcdom,$rowtype);
1828: my $ccrole = &Apache::lonnet::plaintext('co',undef,undef,1);
1829: my $carole = &Apache::lonnet::plaintext('ca');
1830: my $selectcalink = &coauthorlink($dcdom,$rowtype);
1831: $output.=$ccrole.': '.$selectcclink
1832: .' | '.$carole.': '.$selectcalink.'</td>'
1833: .&Apache::loncommon::end_data_table_row();
1834: return $output;
1835: }
1836:
1837: sub recent_filename {
1838: my $area=shift;
1839: return 'nohist_recent_'.&escape($area);
1840: }
1841:
1842: sub courseloadpage {
1843: my ($courseid) = @_;
1844: my $startpage;
1845: my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
1846: [$courseid.':courseinit']);
1847: my ($tmp) = %entry_settings;
1848: unless ($tmp =~ /^error: 2 /) {
1849: $startpage = $entry_settings{$courseid.':courseinit'};
1850: }
1851: if ($startpage eq '') {
1852: if (exists($env{'environment.course_init_display'})) {
1853: $startpage = $env{'environment.course_init_display'};
1854: }
1855: }
1856: return $startpage;
1857: }
1858:
1859: sub update_session_roles {
1860: my $then=$env{'user.login.time'};
1861: my $refresh=$env{'user.refresh.time'};
1862: if (!$refresh) {
1863: $refresh = $then;
1864: }
1865: my $update = $env{'user.update.time'};
1866: if (!$update) {
1867: $update = $then;
1868: }
1869: my $now = time;
1870: my %roleshash =
1871: &Apache::lonnet::get_my_roles('','','userroles',
1872: ['active','future','previous'],
1873: undef,undef,1);
1874: my ($msg,@newsec,$oldsec,$currrole_expired,@changed_roles,
1875: %changed_groups,%dbroles, %deletedroles,%allroles,%allgroups,
1876: %userroles,%checkedgroup,%crprivs,$hasgroups,%rolechange,
1877: %groupchange,%newrole,%newgroup,%customprivchg,%groups_roles,
1878: @rolecodes);
1879: my @possroles = ('cr','st','ta','ad','ep','in','co','cc');
1880: my %courseroles;
1881: foreach my $item (keys(%roleshash)) {
1882: my ($uname,$udom,$role,$remainder) = split(/:/,$item,4);
1883: my ($tstart,$tend) = split(/:/,$roleshash{$item});
1884: my ($section,$group,@group_privs);
1885: if ($role =~ m{^gr/(\w*)$}) {
1886: $role = 'gr';
1887: my $priv = $1;
1888: next if ($tstart eq '-1');
1889: if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
1890: if ($priv ne '') {
1891: push(@group_privs,$priv);
1892: }
1893: }
1894: if ($remainder =~ /:/) {
1895: (my $additional_privs,$group) =
1896: ($remainder =~ /^([\w:]+):([^:]+)$/);
1897: if ($additional_privs ne '') {
1898: if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
1899: push(@group_privs,split(/:/,$additional_privs));
1900: @group_privs = sort(@group_privs);
1901: }
1902: }
1903: } else {
1904: $group = $remainder;
1905: }
1906: } else {
1907: $section = $remainder;
1908: }
1909: my $where = "/$udom/$uname";
1910: if ($section ne '') {
1911: $where .= "/$section";
1912: } elsif ($group ne '') {
1913: $where .= "/$group";
1914: }
1915: my $rolekey = "$role.$where";
1916: my $envkey = "user.role.$rolekey";
1917: $dbroles{$envkey} = 1;
1918: if (($env{'request.role'} eq $rolekey) && ($role ne 'st')) {
1919: if (&curr_role_status($tstart,$tend,$refresh,$now) ne 'active') {
1920: $currrole_expired = 1;
1921: }
1922: }
1923: if ($env{$envkey} eq '') {
1924: my $status_in_db =
1925: &curr_role_status($tstart,$tend,$refresh,$now);
1926: &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
1927: if (($role eq 'st') && ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
1928: if ($status_in_db eq 'active') {
1929: if ($section eq '') {
1930: push(@newsec,'none');
1931: } else {
1932: push(@newsec,$section);
1933: }
1934: }
1935: } else {
1936: unless (grep(/^\Q$role\E$/,@changed_roles)) {
1937: push(@changed_roles,$role);
1938: }
1939: if ($status_in_db ne 'previous') {
1940: if ($role eq 'gr') {
1941: $newgroup{$rolekey} = $status_in_db;
1942: if ($status_in_db eq 'active') {
1943: unless (ref($courseroles{$udom}) eq 'HASH') {
1944: %{$courseroles{$udom}} =
1945: &Apache::lonnet::get_my_roles('','','userroles',
1946: ['active'],\@possroles,
1947: [$udom],1);
1948: }
1949: &Apache::lonnet::get_groups_roles($udom,$uname,
1950: $courseroles{$udom},
1951: \@rolecodes,\%groups_roles);
1952: }
1953: } else {
1954: $newrole{$rolekey} = $status_in_db;
1955: }
1956: }
1957: }
1958: } else {
1959: my ($currstart,$currend) = split(/\./,$env{$envkey});
1960: if ($role eq 'gr') {
1961: if (&curr_role_status($currstart,$currend,$refresh,$update) ne 'previous') {
1962: $hasgroups = 1;
1963: }
1964: }
1965: if (($currstart ne $tstart) || ($currend ne $tend)) {
1966: my $status_in_env =
1967: &curr_role_status($currstart,$currend,$refresh,$update);
1968: my $status_in_db =
1969: &curr_role_status($tstart,$tend,$refresh,$now);
1970: if ($status_in_env ne $status_in_db) {
1971: if ($status_in_env eq 'active') {
1972: if ($role eq 'st') {
1973: if ($env{'request.role'} eq $rolekey) {
1974: my $switchsection;
1975: unless (ref($courseroles{$udom}) eq 'HASH') {
1976: %{$courseroles{$udom}} =
1977: &Apache::lonnet::get_my_roles('','','userroles',
1978: ['active'],
1979: \@possroles,[$udom],1);
1980: }
1981: foreach my $crsrole (keys(%{$courseroles{$udom}})) {
1982: if ($crsrole =~ /^\Q$uname\E:\Q$udom\E:st/) {
1983: $switchsection = 1;
1984: last;
1985: }
1986: }
1987: if ($switchsection) {
1988: if ($section eq '') {
1989: $oldsec = 'none';
1990: } else {
1991: $oldsec = $section;
1992: }
1993: &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
1994: } else {
1995: $currrole_expired = 1;
1996: next;
1997: }
1998: }
1999: }
2000: unless ($rolekey eq $env{'request.role'}) {
2001: if ($role eq 'gr') {
2002: &Apache::lonnet::delete_env_groupprivs($where,\%courseroles,\@possroles);
2003: } else {
2004: &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
2005: &Apache::lonnet::delenv("user.priv.cm.$where",undef,['cm']);
2006: }
2007: &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
2008: }
2009: } elsif ($status_in_db eq 'active') {
2010: if (($role eq 'st') &&
2011: ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
2012: if ($section eq '') {
2013: push(@newsec,'none');
2014: } else {
2015: push(@newsec,$section);
2016: }
2017: } elsif ($role eq 'gr') {
2018: unless (ref($courseroles{$udom}) eq 'HASH') {
2019: %{$courseroles{$udom}} =
2020: &Apache::lonnet::get_my_roles('','','userroles',
2021: ['active'],
2022: \@possroles,[$udom],1);
2023: }
2024: &Apache::lonnet::get_groups_roles($udom,$uname,
2025: $courseroles{$udom},
2026: \@rolecodes,\%groups_roles);
2027: }
2028: &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
2029: }
2030: unless (grep(/^\Q$role\E$/,@changed_roles)) {
2031: push(@changed_roles,$role);
2032: }
2033: if ($role eq 'gr') {
2034: $groupchange{"/$udom/$uname"}{$group} = $status_in_db;
2035: } else {
2036: $rolechange{$rolekey} = $status_in_db;
2037: }
2038: }
2039: } else {
2040: if ($role eq 'gr') {
2041: unless ($checkedgroup{$where}) {
2042: my $status_in_db =
2043: &curr_role_status($tstart,$tend,$refresh,$now);
2044: if ($tstart eq '-1') {
2045: $status_in_db = 'deleted';
2046: }
2047: unless (ref($courseroles{$udom}) eq 'HASH') {
2048: %{$courseroles{$udom}} =
2049: &Apache::lonnet::get_my_roles('','','userroles',
2050: ['active'],
2051: \@possroles,[$udom],1);
2052: }
2053: if (ref($courseroles{$udom}) eq 'HASH') {
2054: foreach my $item (keys(%{$courseroles{$udom}})) {
2055: next unless ($item =~ /^\Q$uname\E/);
2056: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
2057: my $area = '/'.$cdom.'/'.$cnum;
2058: if ($crssec ne '') {
2059: $area .= '/'.$crssec;
2060: }
2061: my $crsrolekey = $crsrole.'.'.$area;
2062: my $currprivs = $env{'user.priv.'.$crsrole.'.'.$area.'.'.$where};
2063: $currprivs =~ s/^://;
2064: $currprivs =~ s/\&F$//;
2065: my @curr_grp_privs = split(/\&F:/,$currprivs);
2066: @curr_grp_privs = sort(@curr_grp_privs);
2067: my @diffs;
2068: if (@group_privs > 0 || @curr_grp_privs > 0) {
2069: @diffs = &Apache::loncommon::compare_arrays(\@group_privs,\@curr_grp_privs);
2070: }
2071: if (@diffs == 0) {
2072: last;
2073: } else {
2074: unless(grep(/^\Qgr\E$/,@rolecodes)) {
2075: push(@rolecodes,'gr');
2076: }
2077: &gather_roleprivs(\%allroles,\%allgroups,
2078: \%userroles,$where,$role,
2079: $tstart,$tend,$status_in_db);
2080: if ($status_in_db eq 'active') {
2081: &Apache::lonnet::get_groups_roles($udom,$uname,
2082: $courseroles{$udom},
2083: \@rolecodes,\%groups_roles);
2084: }
2085: $changed_groups{$udom.'_'.$uname}{$group} = $status_in_db;
2086: last;
2087: }
2088: }
2089: }
2090: $checkedgroup{$where} = 1;
2091: }
2092: } elsif ($role =~ /^cr/) {
2093: my $status_in_db =
2094: &curr_role_status($tstart,$tend,$refresh,$now);
2095: my ($rdummy,$rest) = split(/\//,$role,2);
2096: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
2097: my %currpriv;
2098: unless (exists($crprivs{$rest})) {
2099: my ($rdomain,$rauthor,$rrole)=split(/\//,$rest);
2100: my $homsvr=&Apache::lonnet::homeserver($rauthor,$rdomain);
2101: if (&Apache::lonnet::hostname($homsvr) ne '') {
2102: my ($rdummy,$roledef)=
2103: &Apache::lonnet::get('roles',["rolesdef_$rrole"],
2104: $rdomain,$rauthor);
2105: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
2106: my $i = 0;
2107: my @scopes = ('sys','dom','crs');
2108: my @privs = split(/\_/,$roledef);
2109: foreach my $priv (@privs) {
2110: my ($blank,@prv) = split(/:/,$priv);
2111: @prv = map { $_ .= (/\&\w+$/ ? '':'&F') } @prv;
2112: $priv = ':'.join(':',sort(@prv));
2113: $crprivs{$rest}{$scopes[$i]} = $priv;
2114: $i++;
2115: }
2116: }
2117: }
2118: }
2119: $currpriv{sys} = $env{"user.priv.$rolekey./"};
2120: $currpriv{dom} = $env{"user.priv.$rolekey./$udom/"};
2121: $currpriv{crs} = $env{"user.priv.$rolekey.$where"};
2122: if (keys(%crprivs)) {
2123: if (($crprivs{$rest}{sys} ne $currpriv{sys}) ||
2124: ($crprivs{$rest}{dom} ne $currpriv{dom})
2125: ||
2126: ($crprivs{$rest}{crs} ne $currpriv{crs})) {
2127: &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
2128: unless (grep(/^\Q$role\E$/,@changed_roles)) {
2129: push(@changed_roles,$role);
2130: }
2131: my $status_in_env =
2132: &curr_role_status($currstart,$currend,$refresh,$update);
2133: if ($status_in_env eq 'active') {
2134: $customprivchg{$rolekey} = $status_in_env;
2135: }
2136: }
2137: }
2138: }
2139: }
2140: }
2141: }
2142: foreach my $envkey (keys(%env)) {
2143: next unless ($envkey =~ /^user\.role\./);
2144: next if ($dbroles{$envkey});
2145: next if ($envkey eq 'user.role.'.$env{'request.role'});
2146: my ($currstart,$currend) = split(/\./,$env{$envkey});
2147: my $status_in_env =
2148: &curr_role_status($currstart,$currend,$refresh,$update);
2149: my ($rolekey) = ($envkey =~ /^user\.role\.(.+)$/);
2150: my ($role,$rest)=split(/\./,$rolekey,2);
2151: if (&Apache::lonnet::delenv($envkey,undef,[$role])) {
2152: if ($status_in_env eq 'active') {
2153: if ($role eq 'gr') {
2154: &Apache::lonnet::delete_env_groupprivs($rest,\%courseroles,
2155: \@possroles);
2156: } else {
2157: &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
2158: &Apache::lonnet::delenv("user.priv.cm.$rest",undef,['cm']);
2159: }
2160: unless (grep(/^\Q$role\E$/,@changed_roles)) {
2161: push(@changed_roles,$role);
2162: }
2163: $deletedroles{$rolekey} = 1;
2164: }
2165: }
2166: }
2167: if (($oldsec) && (@newsec > 0)) {
2168: if (@newsec > 1) {
2169: $msg = '<div class="LC_warning">'.&mt('The section has changed for your current role. Log-out and log-in again to select a role for the new section.').'</div>';
2170: } else {
2171: my $newrole = $env{'request.role'};
2172: if ($newsec[0] eq 'none') {
2173: $newrole =~ s{(/[^/])$}{};
2174: } elsif ($oldsec eq 'none') {
2175: $newrole .= '/'.$newsec[0];
2176: } else {
2177: $newrole =~ s{([^/]+)$}{$newsec[0]};
2178: }
2179: my $coursedesc = $env{'course.'.$env{'request.course.id'}.'.description'};
2180: my ($curr_role) = ($env{'request.role'} =~ m{^(\w+)\./$match_domain/$match_courseid});
2181: my %temp=('logout_'.$env{'request.course.id'} => time);
2182: &Apache::lonnet::put('email_status',\%temp);
2183: &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
2184: &Apache::lonnet::appenv({"request.course.id" => '',
2185: "request.course.fn" => '',
2186: "request.course.uri" => '',
2187: "request.course.sec" => '',
2188: "request.role" => 'cm',
2189: "request.role.adv" => $env{'user.adv'},
2190: "request.role.domain" => $env{'user.domain'}});
2191: my $rolename = &Apache::loncommon::plainname($curr_role);
2192: $msg = '<p><form name="reselectrole" action="/adm/roles" method="post" />'.
2193: '<input type="hidden" name="newrole" value="" />'.
2194: '<input type="hidden" name="selectrole" value="1" />'.
2195: '<span class="LC_info">'.
2196: &mt('Your section has changed for your current [_1] role in [_2].',$rolename,$coursedesc).'</span><br />';
2197: my $button = '<input type="button" name="sectionchanged" value="'.
2198: &mt('Re-Select').'" onclick="javascript:enterrole(this.form,'."'$newrole','sectionchanged'".')" />';
2199: if ($newsec[0] eq 'none') {
2200: $msg .= &mt('[_1] to continue with your new section-less role.',$button);
2201: } else {
2202: $msg .= &mt('[_1] to continue with your new role in section ([_2]).',$button,$newsec[0]);
2203: }
2204: $msg .= '</form></p>';
2205: }
2206: } elsif ($currrole_expired) {
2207: $msg .= '<div class="LC_warning">';
2208: if (&Apache::loncommon::show_course()) {
2209: $msg .= &mt('Your role in the current course has expired.');
2210: } else {
2211: $msg .= &mt('Your current role has expired.');
2212: }
2213: $msg .= '<br />'.&mt('However you can continue to use this role until you logout, click the "Re-Select" button, or your session has been idle for more than 24 hours.').'</div>';
2214: }
2215: if (!@changed_roles || !(keys(%changed_groups))) {
2216: $msg .= '<ul>';
2217: if (!@changed_roles) {
2218: $msg .= '<li>';
2219: if (&Apache::loncommon::show_course()) {
2220: $msg .= &mt('No new courses or communities');
2221: } else {
2222: $msg .= &mt('No role changes');
2223: }
2224: $msg .= '</li>';
2225: }
2226: if ($hasgroups && !(keys(%changed_groups)) && !(grep(/gr/,@changed_roles))) {
2227: $msg .= '<li>'.&mt('No changes in course/community groups').'</li>';
2228: }
2229: $msg .= '</ul>';
2230: if (!@changed_roles && !(keys(%changed_groups))) {
2231: return $msg;
2232: }
2233: }
2234: my $changemsg;
2235: if (@changed_roles > 0) {
2236: if (keys(%newgroup) > 0) {
2237: my $groupmsg;
2238: foreach my $item (sort(keys(%newgroup))) {
2239: if (&is_active_course($item,$refresh,$update,\%roleshash)) {
2240: $groupmsg .= '<li>'.
2241: &mt('[_1] with status: [_2].',
2242: $item,$newgroup{$item}).'</li>';
2243: }
2244: }
2245: if ($groupmsg) {
2246: $changemsg .= '<li>'.
2247: &mt('Courses with new groups').'</li>'.
2248: '<ul>'.$groupmsg.'</ul></li>';
2249: }
2250: }
2251: if (keys(%newrole) > 0) {
2252: $changemsg .= '<li>'.&mt('New roles').
2253: '<ul>';
2254: foreach my $item (sort(keys(%newrole))) {
2255: $changemsg .= '<li>'.
2256: &mt('[_1] with status: [_2].',
2257: $item,$newrole{$item}).'</li>';
2258: }
2259: $changemsg .= '</ul></li>';
2260: }
2261: if (keys(%customprivchg) > 0) {
2262: $changemsg .= '<li>'.
2263: &mt('Custom roles with privilege changes').
2264: '<ul>';
2265: foreach my $item (sort(keys(%customprivchg))) {
2266: $changemsg .= '<li>'.$item.'</li>';
2267: }
2268: $changemsg .= '</ul></li>';
2269: }
2270: if (keys(%rolechange) > 0) {
2271: $changemsg .= '<li>'.
2272: &mt('Existing roles with status changes').'</li>'.
2273: '<ul>';
2274: foreach my $item (sort(keys(%rolechange))) {
2275: $changemsg .= '<li>'.
2276: &mt('[_1] status now: [_2].',$item,
2277: $rolechange{$item}).'</li>';
2278: }
2279: $changemsg .= '</ul></li>';
2280: }
2281: if (keys(%deletedroles) > 0) {
2282: $changemsg .= '<li>'.
2283: &mt('Existing roles deleted').'</li>'.
2284: '<ul>';
2285: foreach my $item (sort(keys(%deletedroles))) {
2286: $changemsg .= '<li>'.$item.'</li>';
2287: }
2288: $changemsg .= '</ul></li>';
2289: }
2290: }
2291: if ((keys(%changed_groups) > 0) || (keys(%groupchange) > 0)) {
2292: my $groupchgmsg;
2293: foreach my $key (sort(keys(%changed_groups))) {
2294: my $crs = 'gr/'.$key;
2295: $crs =~ s/_/\//;
2296: if (&is_active_course($crs,$refresh,$update,\%roleshash)) {
2297: if (ref($changed_groups{$key}) eq 'HASH') {
2298: my @showgroups;
2299: foreach my $group (sort(keys(%{$changed_groups{$key}}))) {
2300: if ($changed_groups{$key}{$group} eq 'active') {
2301: push(@showgroups,$group);
2302: }
2303: }
2304: if (@showgroups > 0) {
2305: $groupchgmsg .= '<li>'.
2306: &mt('Course: [_1], groups: [_2].',$key,
2307: join(', ',@showgroups)).
2308: '</li>';
2309: }
2310: }
2311: }
2312: }
2313: if (keys(%groupchange) > 0) {
2314: $groupchgmsg .= '<li>'.
2315: &mt('Existing course/community groups with status changes').'</li>'.
2316: '<ul>';
2317: foreach my $crs (sort(keys(%groupchange))) {
2318: if (ref($groupchange{$crs}) eq 'HASH') {
2319: $groupchgmsg .= '<li>'.&mt('Course/Community: [_1]','<b>'.$crs.'</b><ul>');
2320: foreach my $group (sort(keys(%{$groupchange{$crs}}))) {
2321: $groupchgmsg .= '<li>'.&mt('Group: [_1] status now: [_2].','<b>'.$group.'</b>',$groupchange{$crs}{$group}).'</li>';
2322: }
2323: $groupchgmsg .= '</ul></li>';
2324: }
2325: }
2326: $groupchgmsg .= '</ul></li>';
2327: }
2328: if ($groupchgmsg) {
2329: $changemsg .= '<li>'.
2330: &mt('Courses with changes in groups').'</li>'.
2331: '<ul>'.$groupchgmsg.'</ul></li>';
2332: }
2333: }
2334: if ($changemsg) {
2335: $msg .= '<ul>'.$changemsg.'</ul>';
2336: }
2337: &Apache::lonnet::set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
2338: my ($curr_is_adv,$curr_role_adv,$curr_author,$curr_role_author);
2339: $curr_author = $env{'user.author'};
2340: if (($env{'request.role'} =~/^au/) || ($env{'request.role'} =~/^ca/) ||
2341: ($env{'request.role'} =~/^aa/)) {
2342: $curr_role_author=1;
2343: }
2344: $curr_is_adv = $env{'user.adv'};
2345: $curr_role_adv = $env{'request.role.adv'};
2346: if (keys(%userroles) > 0) {
2347: foreach my $role (@changed_roles) {
2348: unless(grep(/^\Q$role\E$/,@rolecodes)) {
2349: push(@rolecodes,$role);
2350: }
2351: }
2352: unless(grep(/^\Qcm\E$/,@rolecodes)) {
2353: push(@rolecodes,'cm');
2354: }
2355: &Apache::lonnet::appenv(\%userroles,\@rolecodes);
2356: }
2357: my %newenv;
2358: if (&Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'})) {
2359: unless ($curr_is_adv) {
2360: $newenv{'user.adv'} = 1;
2361: }
2362: } elsif ($curr_is_adv && !$curr_role_adv) {
2363: &Apache::lonnet::delenv('user.adv');
2364: }
2365: my %authorroleshash =
2366: &Apache::lonnet::get_my_roles('','','userroles',['active'],['au','ca','aa']);
2367: if (keys(%authorroleshash)) {
2368: unless ($curr_author) {
2369: $newenv{'user.author'} = 1;
2370: }
2371: } elsif ($curr_author && !$curr_role_author) {
2372: &Apache::lonnet::delenv('user.author');
2373: }
2374: if ($env{'request.course.id'}) {
2375: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
2376: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
2377: my (@activecrsgroups,$crsgroupschanged);
2378: if ($env{'request.course.groups'}) {
2379: @activecrsgroups = split(/:/,$env{'request.course.groups'});
2380: foreach my $item (keys(%deletedroles)) {
2381: if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
2382: if (grep(/^\Q$1\E$/,@activecrsgroups)) {
2383: $crsgroupschanged = 1;
2384: last;
2385: }
2386: }
2387: }
2388: }
2389: unless ($crsgroupschanged) {
2390: foreach my $item (keys(%newgroup)) {
2391: if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
2392: if ($newgroup{$item} eq 'active') {
2393: $crsgroupschanged = 1;
2394: last;
2395: }
2396: }
2397: }
2398: }
2399: if ((ref($changed_groups{$env{'request.course.id'}}) eq 'HASH') ||
2400: (ref($groupchange{"/$cdom/$cnum"}) eq 'HASH') ||
2401: ($crsgroupschanged)) {
2402: my %grouproles = &Apache::lonnet::get_my_roles('','','userroles',
2403: ['active'],['gr'],[$cdom],1);
2404: my @activegroups;
2405: foreach my $item (keys(%grouproles)) {
2406: next unless($item =~ /^\Q$cnum\E:\Q$cdom\E/);
2407: my $group;
2408: my ($crsn,$crsd,$role,$remainder) = split(/:/,$item,4);
2409: if ($remainder =~ /:/) {
2410: (my $other,$group) = ($remainder =~ /^([\w:]+):([^:]+)$/);
2411: } else {
2412: $group = $remainder;
2413: }
2414: if ($group ne '') {
2415: push(@activegroups,$group);
2416: }
2417: }
2418: $newenv{'request.course.groups'} = join(':',@activegroups);
2419: }
2420: }
2421: if (keys(%newenv)) {
2422: &Apache::lonnet::appenv(\%newenv);
2423: }
2424: return $msg;
2425: }
2426:
2427: sub curr_role_status {
2428: my ($start,$end,$refresh,$update) = @_;
2429: if (($start) && ($start<0)) { return 'deleted' };
2430: my $status = 'active';
2431: if (($end) && ($end<=$update)) {
2432: $status = 'previous';
2433: }
2434: if (($start) && ($refresh<$start)) {
2435: $status = 'future';
2436: }
2437: return $status;
2438: }
2439:
2440: sub gather_roleprivs {
2441: my ($allroles,$allgroups,$userroles,$area,$role,$tstart,$tend,$status) = @_;
2442: return unless ((ref($allroles) eq 'HASH') && (ref($allgroups) eq 'HASH') && (ref($userroles) eq 'HASH'));
2443: if (($area ne '') && ($role ne '')) {
2444: &Apache::lonnet::userrolelog($role,$env{'user.name'},$env{'user.domain'},
2445: $area,$tstart,$tend);
2446: my $spec=$role.'.'.$area;
2447: $userroles->{'user.role.'.$spec} = $tstart.'.'.$tend;
2448: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
2449: if ($status eq 'active') {
2450: if ($role =~ /^cr\//) {
2451: &Apache::lonnet::custom_roleprivs($allroles,$role,$tdomain,$trest,$spec,$area);
2452: } elsif ($role eq 'gr') {
2453: my %rolehash = &Apache::lonnet::get('roles',[$area.'_'.$role],
2454: $env{'user.domain'},
2455: $env{'user.name'});
2456: my ($trole) = split(/_/,$rolehash{$area.'_'.$role},2);
2457: (undef,my $group_privs) = split(/\//,$trole);
2458: $group_privs = &unescape($group_privs);
2459: &Apache::lonnet::group_roleprivs($allgroups,$area,$group_privs,$tend,$tstart);
2460: } else {
2461: &Apache::lonnet::standard_roleprivs($allroles,$role,$tdomain,$spec,$trest,$area);
2462: }
2463: }
2464: }
2465: return;
2466: }
2467:
2468: sub is_active_course {
2469: my ($rolekey,$refresh,$update,$roleshashref) = @_;
2470: return unless(ref($roleshashref) eq 'HASH');
2471: my ($role,$cdom,$cnum) = split(/\//,$rolekey);
2472: my $is_active;
2473: foreach my $key (keys(%{$roleshashref})) {
2474: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:/) {
2475: my ($tstart,$tend) = split(/:/,$roleshashref->{$key});
2476: my $status = &curr_role_status($tstart,$tend,$refresh,$update);
2477: if ($status eq 'active') {
2478: $is_active = 1;
2479: last;
2480: }
2481: }
2482: }
2483: return $is_active;
2484: }
2485:
2486: 1;
2487: __END__
2488:
2489: =head1 NAME
2490:
2491: Apache::lonroles - User Roles Screen
2492:
2493: =head1 SYNOPSIS
2494:
2495: Invoked by /etc/httpd/conf/srm.conf:
2496:
2497: <Location /adm/roles>
2498: PerlAccessHandler Apache::lonacc
2499: SetHandler perl-script
2500: PerlHandler Apache::lonroles
2501: ErrorDocument 403 /adm/login
2502: ErrorDocument 500 /adm/errorhandler
2503: </Location>
2504:
2505: =head1 OVERVIEW
2506:
2507: =head2 Choosing Roles
2508:
2509: C<lonroles> is a handler that allows a user to switch roles in
2510: mid-session. LON-CAPA attempts to work with "No Role Specified", the
2511: default role that a user has before selecting a role, as widely as
2512: possible, but certain handlers for example need specification which
2513: course they should act on, etc. Both in this scenario, and when the
2514: handler determines via C<lonnet>'s C<&allowed> function that a certain
2515: action is not allowed, C<lonroles> is used as error handler. This
2516: allows the user to select another role which may have permission to do
2517: what they were trying to do.
2518:
2519: =begin latex
2520:
2521: \begin{figure}
2522: \begin{center}
2523: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
2524: \caption{\label{Sample_Roles_Screen}Sample Roles Screen}
2525: \end{center}
2526: \end{figure}
2527:
2528: =end latex
2529:
2530: =head2 Role Initialization
2531:
2532: 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.
2533:
2534: =head1 INTRODUCTION
2535:
2536: This module enables a user to select what role he wishes to
2537: operate under (instructor, student, teaching assistant, course
2538: coordinator, etc). These roles are pre-established by the actions
2539: of upper-level users.
2540:
2541: This is part of the LearningOnline Network with CAPA project
2542: described at http://www.lon-capa.org.
2543:
2544: =head1 HANDLER SUBROUTINE
2545:
2546: This routine is called by Apache and mod_perl.
2547:
2548: =over 4
2549:
2550: =item *
2551:
2552: Roles Initialization (yes/no)
2553:
2554: =item *
2555:
2556: Get Error Message from Environment
2557:
2558: =item *
2559:
2560: Who is this?
2561:
2562: =item *
2563:
2564: Generate Page Output
2565:
2566: =item *
2567:
2568: Choice or no choice
2569:
2570: =item *
2571:
2572: Table
2573:
2574: =item *
2575:
2576: Privileges
2577:
2578: =back
2579:
2580: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>