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