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