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