File:
[LON-CAPA] /
loncom /
auth /
lonroles.pm
Revision
1.113:
download - view:
text,
annotated -
select for diffs
Fri Dec 3 05:15:30 2004 UTC (19 years, 8 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
Eliminate display of "Recent courses visited by DC" - use Recent Roles instead. Recent roles display will now retrieve role info for recently visited CC roles created on the fly for a DC, if not yet visited this session.
1: # The LearningOnline Network with CAPA
2: # User Roles Screen
3: #
4: # $Id: lonroles.pm,v 1.113 2004/12/03 05:15:30 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: package Apache::lonroles;
31:
32: use strict;
33: use Apache::lonnet();
34: use Apache::lonuserstate();
35: use Apache::Constants qw(:common);
36: use Apache::File();
37: use Apache::lonmenu;
38: use Apache::loncommon;
39: use Apache::lonhtmlcommon;
40: use Apache::lonannounce;
41: use Apache::lonlocal;
42:
43: sub redirect_user {
44: my ($r,$title,$url,$msg,$launch_nav) = @_;
45: $msg = $title if (! defined($msg));
46: &Apache::loncommon::content_type($r,'text/html');
47: &Apache::loncommon::no_cache($r);
48: $r->send_http_header;
49: my $swinfo=&Apache::lonmenu::rawconfig();
50: my $navwindow;
51: if ($launch_nav eq 'on') {
52: $navwindow.=&Apache::lonnavmaps::launch_win('now');
53: } else {
54: $navwindow.=&Apache::lonnavmaps::close();
55: }
56: my $bodytag=&Apache::loncommon::bodytag('Switching Role');
57: # Note to style police:
58: # This must only replace the spaces, nothing else, or it bombs elsewhere.
59: $url=~s/ /\%20/g;
60: $r->print(<<ENDREDIR);
61: <head><title>$title</title>
62: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$url">
63: </head>
64: <html>
65: $bodytag
66: <script type="text/javascript">
67: $swinfo
68: </script>
69: $navwindow
70: <h1>$msg</h1>
71: <a href="$url">Continue</a>
72: </body>
73: </html>
74: ENDREDIR
75: return;
76: }
77:
78: sub handler {
79:
80: my $r = shift;
81:
82: my $now=time;
83: my $then=$ENV{'user.login.time'};
84: my $envkey;
85: my %dcroles = ();
86: my $numdc = &check_fordc(\%dcroles,$then);
87:
88: # ================================================================== Roles Init
89: if ($ENV{'form.selectrole'}) {
90: if ($ENV{'request.course.id'}) {
91: my %temp=('logout_'.$ENV{'request.course.id'} => time);
92: &Apache::lonnet::put('email_status',\%temp);
93: &Apache::lonnet::delenv('user.state.'.$ENV{'request.course.id'});
94: }
95: &Apache::lonnet::appenv("request.course.id" => '',
96: "request.course.fn" => '',
97: "request.course.uri" => '',
98: "request.course.sec" => '',
99: "request.role" => 'cm',
100: "request.role.adv" => $ENV{'user.adv'},
101: "request.role.domain" => $ENV{'user.domain'});
102:
103: # Check if user is a DC trying to enter a course and needs privs to be created
104: if ($numdc > 0) {
105: foreach my $envkey (keys %ENV) {
106: if ($envkey =~ m-^form\.cc\./(\w+)/(\w+)$-) {
107: if ($dcroles{$1}) {
108: my $cckey = 'user.role.cc./'.$1.'/'.$2;
109: &check_privs($cckey,$then,$now);
110: }
111: last;
112: }
113: }
114: }
115:
116: foreach $envkey (keys %ENV) {
117: next if ($envkey!~/^user\.role\./);
118: my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
119: &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
120: if ($ENV{'form.'.$trolecode}) {
121: if ($tstatus eq 'is') {
122: $where=~s/^\///;
123: my ($cdom,$cnum,$csec)=split(/\//,$where);
124: # store role if recent_role list being kept
125: if ($ENV{'environment.recentroles'}) {
126: &Apache::lonhtmlcommon::store_recent('roles',
127: $trolecode,' ');
128: }
129:
130:
131: # check for keyed access
132: if (($role eq 'st') &&
133: ($ENV{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
134: # who is key authority?
135: my $authdom=$cdom;
136: my $authnum=$cnum;
137: if ($ENV{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
138: ($authnum,$authdom)=
139: split(/\W/,$ENV{'course.'.$cdom.'_'.$cnum.'.keyauth'});
140: }
141: # check with key authority
142: unless (&Apache::lonnet::validate_access_key(
143: $ENV{'environment.key.'.$cdom.'_'.$cnum},
144: $authdom,$authnum)) {
145: # there is no valid key
146: if ($ENV{'form.newkey'}) {
147: # student attempts to register a new key
148: &Apache::loncommon::content_type($r,'text/html');
149: &Apache::loncommon::no_cache($r);
150: $r->send_http_header;
151: my $swinfo=&Apache::lonmenu::rawconfig();
152: my $bodytag=&Apache::loncommon::bodytag
153: ('Verifying Access Key to Unlock this Course');
154: my $buttontext=&mt('Enter Course');
155: my $message=&mt('Successfully registered key');
156: my $assignresult=
157: &Apache::lonnet::assign_access_key(
158: $ENV{'form.newkey'},
159: $authdom,$authnum,
160: $cdom,$cnum,
161: $ENV{'user.domain'},
162: $ENV{'user.name'},
163: 'Assigned from '.$ENV{'REMOTE_ADDR'}.' at '.localtime().' for '.
164: $trolecode);
165: unless ($assignresult eq 'ok') {
166: $assignresult=~s/^error\:\s*//;
167: $message=&mt($assignresult).
168: '<br /><a href="/adm/logout">'.
169: &mt('Logout').'</a>';
170: $buttontext=&mt('Re-Enter Key');
171: }
172: $r->print(<<ENDENTEREDKEY);
173: <head><title>Verifying Course Access Key</title>
174: </head>
175: <html>
176: $bodytag
177: <script>
178: $swinfo
179: </script>
180: <form method="post">
181: <input type="hidden" name="selectrole" value="1" />
182: <input type="hidden" name="$trolecode" value="1" />
183: <font size="+2">$message</font><br />
184: <input type="submit" value="$buttontext" />
185: </form>
186: </body></html>
187: ENDENTEREDKEY
188: return OK;
189: } else {
190: # print form to enter a new key
191: &Apache::loncommon::content_type($r,'text/html');
192: &Apache::loncommon::no_cache($r);
193: $r->send_http_header;
194: my $swinfo=&Apache::lonmenu::rawconfig();
195: my $bodytag=&Apache::loncommon::bodytag
196: ('Enter Access Key to Unlock this Course');
197: $r->print(<<ENDENTERKEY);
198: <head><title>Entering Course Access Key</title>
199: </head>
200: <html>
201: $bodytag
202: <script>
203: $swinfo
204: </script>
205: <form method="post">
206: <input type="hidden" name="selectrole" value="1" />
207: <input type="hidden" name="$trolecode" value="1" />
208: <input type="text" size="20" name="newkey" value="$ENV{'form.newkey'}" />
209: <input type="submit" value="Enter key" />
210: </form>
211: </body></html>
212: ENDENTERKEY
213: return OK;
214: }
215: }
216: }
217: &Apache::lonnet::log($ENV{'user.domain'},
218: $ENV{'user.name'},
219: $ENV{'user.home'},
220: "Role ".$trolecode);
221:
222: &Apache::lonnet::appenv(
223: 'request.role' => $trolecode,
224: 'request.role.domain' => $cdom,
225: 'request.course.sec' => $csec);
226: my $tadv=0;
227: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
228: &Apache::lonnet::appenv('request.role.adv' => $tadv);
229:
230: my $msg=&mt('Entering course ...');
231:
232: if (($cnum) && ($role ne 'ca')) {
233: my ($furl,$ferr)=
234: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
235: if (($ENV{'form.orgurl'}) &&
236: ($ENV{'form.orgurl'}!~/^\/adm\/flip/)) {
237: my $dest=$ENV{'form.orgurl'};
238: if ( &Apache::lonnet::mod_perl_version() == 2 ) {
239: &Apache::lonnet::cleanenv();
240: }
241: $r->internal_redirect($dest);
242: return OK;
243: } else {
244: unless ($ENV{'request.course.id'}) {
245: &Apache::lonnet::appenv(
246: "request.course.id" => $cdom.'_'.$cnum);
247: $furl='/adm/roles?tryagain=1';
248: $msg=
249: '<h1><font color=red>'.
250: &mt('Could not initialize course at this time.').
251: '</font></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
252: }
253:
254: # Check to see if the user is a CC entering a course
255: # for the first time
256: my (undef, undef, $role, $courseid) = split(/\./, $envkey);
257: if (substr($courseid, 0, 1) eq '/') {
258: $courseid = substr($courseid, 1);
259: }
260: $courseid =~ s/\//_/;
261: if ($role eq 'cc' && $ENV{'course.' . $courseid .
262: '.course.helper.not.run'}) {
263: $furl = "/adm/helper/course.initialization.helper";
264: }
265: # Send the user to the course they selected
266: &redirect_user($r,&mt('Entering Course'),
267: $furl,$msg,
268: $ENV{'environment.remotenavmap'});
269: return OK;
270: }
271: }
272: #
273: # Send the user to the construction space they selected
274: if ($role =~ /^(au|ca)$/) {
275: my $redirect_url = '/priv/';
276: if ($role eq 'au') {
277: $redirect_url.=$ENV{'user.name'};
278: } else {
279: $where =~ /\/(.*)$/;
280: $redirect_url .= $1;
281: }
282: $redirect_url .= '/';
283: &redirect_user($r,&mt('Entering Construction Space'),
284: $redirect_url);
285: return OK;
286: }
287: if ($role eq 'dc') {
288: my $redirect_url = '/adm/menu/';
289: &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
290: $redirect_url);
291: return OK;
292: }
293: }
294: }
295: }
296: }
297:
298:
299: # =============================================================== No Roles Init
300:
301: &Apache::loncommon::content_type($r,'text/html');
302: &Apache::loncommon::no_cache($r);
303: $r->send_http_header;
304: return OK if $r->header_only;
305:
306: my $swinfo=&Apache::lonmenu::rawconfig();
307: my $bodytag=&Apache::loncommon::bodytag('User Roles');
308: my $helptag='<table><tr><td>'.&Apache::loncommon::help_open_menu('','General Intro','General_Intro','User Roles',1,undef,undef,undef,undef,,&mt("Click here for help")).'</td></td></tr></table>';
309: $r->print(<<ENDHEADER);
310: <html>
311: <head>
312: <title>LON-CAPA User Roles</title>
313: </head>
314: $bodytag
315: $helptag<br />
316: <script>
317: $swinfo
318: window.focus();
319: </script>
320: ENDHEADER
321:
322: # ------------------------------------------ Get Error Message from Environment
323:
324: my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
325: if ($ENV{'user.error.msg'}) {
326: $r->log_reason(
327: "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
328: }
329:
330: # ------------------------------------------------- Can this user re-init, etc?
331:
332: my $advanced=$ENV{'user.adv'};
333: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
334: my $tryagain=$ENV{'form.tryagain'};
335:
336: # -------------------------------------------------------- Generate Page Output
337: # --------------------------------------------------------------- Error Header?
338: if ($error) {
339: $r->print("<h1>LON-CAPA Access Control</h1>");
340: $r->print("<hr><pre>Access : ".
341: Apache::lonnet::plaintext($priv)."\n");
342: $r->print("Resource: $fn\n");
343: $r->print("Action : $msg\n</pre><hr>");
344: } else {
345: if ($ENV{'user.error.msg'}) {
346: $r->print(
347: '<h3><font color=red>'.
348: &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
349: }
350: }
351: # -------------------------------------------------------- Choice or no choice?
352: if ($nochoose) {
353: if ($advanced) {
354: $r->print("<h2>".&mt('Assigned User Roles')."</h2>\n");
355: } else {
356: $r->print("<h2>".&mt('Sorry ...')."</h2>\n".
357: &mt('This resource might be part of'));
358: if ($ENV{'request.course.id'}) {
359: $r->print(&mt(' another'));
360: } else {
361: $r->print(&mt(' a certain'));
362: }
363: $r->print(&mt(' course.').'</body></html>');
364: return OK;
365: }
366: } else {
367: if ($advanced) {
368: $r->print(&mt("Your home server is ").
369: $Apache::lonnet::hostname{&Apache::lonnet::homeserver
370: ($ENV{'user.name'},$ENV{'user.domain'})}.
371: "<br />\n");
372: $r->print(&mt(
373: "Author and Co-Author roles may not be available on servers other than your home server."));
374: }
375: if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
376: $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
377: }
378: $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
379: $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
380: $r->print('<input type=hidden name=selectrole value=1>');
381: }
382: if ($ENV{'user.adv'}) {
383: $r->print(
384: '<br />'.&mt('Show all roles').': <input type="checkbox" name="showall"');
385: if ($ENV{'form.showall'}) { $r->print(' checked'); }
386: $r->print('><input type=submit value="'.&mt('Display').'">');
387: }
388:
389: my (%roletext,%sortrole,%roleclass);
390: my $countactive=0;
391: my $inrole=0;
392: my $possiblerole='';
393: foreach $envkey (sort keys %ENV) {
394: my $button = 1;
395: my $switchserver='';
396: my $roletext;
397: my $sortkey;
398: if ($envkey=~/^user\.role\./) {
399: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
400: &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
401: next if (!defined($role) || $role eq '');
402: $tremark='';
403: $tpstart=' ';
404: $tpend=' ';
405: $tfont='#000000';
406: if ($tstart) {
407: $tpstart=&Apache::lonlocal::locallocaltime($tstart);
408: }
409: if ($tend) {
410: $tpend=&Apache::lonlocal::locallocaltime($tend);
411: }
412: if ($ENV{'request.role'} eq $trolecode) {
413: $tstatus='selected';
414: }
415: my $tbg;
416: if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
417: ($ENV{'form.showall'})) {
418: if ($tstatus eq 'is') {
419: $tbg='#77FF77';
420: $tfont='#003300';
421: $possiblerole=$trolecode;
422: $countactive++;
423: } elsif ($tstatus eq 'future') {
424: $tbg='#FFFF77';
425: $button=0;
426: } elsif ($tstatus eq 'will') {
427: $tbg='#FFAA77';
428: $tremark.=&mt('Active at next login. ');
429: } elsif ($tstatus eq 'expired') {
430: $tbg='#FF7777';
431: $tfont='#330000';
432: $button=0;
433: } elsif ($tstatus eq 'will_not') {
434: $tbg='#AAFF77';
435: $tremark.=&mt('Expired after logout. ');
436: } elsif ($tstatus eq 'selected') {
437: $tbg='#11CC55';
438: $tfont='#002200';
439: $inrole=1;
440: $countactive++;
441: $tremark.=&mt('Currently selected. ');
442: }
443: my $trole;
444: if ($role =~ /^cr\//) {
445: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
446: $tremark.='<br>'.&mt('Defined by ').$rauthor.
447: &mt(' at ').$rdomain.'.';
448: $trole=$rrole;
449: } else {
450: $trole=Apache::lonnet::plaintext($role);
451: }
452: my $ttype;
453: my $twhere;
454: my ($tdom,$trest,$tsection)=
455: split(/\//,Apache::lonnet::declutter($where));
456: # First, Co-Authorship roles
457: if ($role eq 'ca') {
458: my $home = &Apache::lonnet::homeserver($trest,$tdom);
459: my $allowed=0;
460: my @ids=&Apache::lonnet::current_machine_ids();
461: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
462: if (!$allowed) {
463: $button=0;
464: $switchserver=&Apache::lonnet::escape('http://'.
465: $Apache::lonnet::hostname{$home}.
466: '/adm/login?domain='.$ENV{'user.domain'}.
467: '&username='.$ENV{'user.name'}.
468: '&firsturl=/priv/'.$trest.'/');
469: }
470: #next if ($home eq 'no_host');
471: $home = $Apache::lonnet::hostname{$home};
472: $ttype='Construction Space';
473: $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
474: ': '.$tdom.'<br />'.
475: ' '.&mt('Server').': '.$home;
476: $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
477: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
478: $sortkey=$role."$trest:$tdom";
479: } elsif ($role eq 'au') {
480: # Authors
481: my $home = &Apache::lonnet::homeserver
482: ($ENV{'user.name'},$ENV{'user.domain'});
483: my $allowed=0;
484: my @ids=&Apache::lonnet::current_machine_ids();
485: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
486: if (!$allowed) {
487: $button=0;
488: $switchserver=&Apache::lonnet::escape('http://'.
489: $Apache::lonnet::hostname{$home}.
490: '/adm/login?domain='.$ENV{'user.domain'}.
491: '&username='.$ENV{'user.name'}.
492: '&firsturl=/priv/'.$ENV{'user.name'}.'/');
493: }
494: #next if ($home eq 'no_host');
495: $home = $Apache::lonnet::hostname{$home};
496: $ttype='Construction Space';
497: $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
498: ': '.$home;
499: $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
500: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$ENV{'user.name'}.'/');
501: $sortkey=$role;
502: } elsif ($trest) {
503: $ttype='Course';
504: if ($tsection) {
505: $ttype.='<br>'.&mt('Section/Group').': '.$tsection;
506: }
507: my $tcourseid=$tdom.'_'.$trest;
508: if ($ENV{'course.'.$tcourseid.'.description'}) {
509: $twhere=$ENV{'course.'.$tcourseid.'.description'};
510: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
511: unless ($twhere eq &mt('Currently not available')) {
512: $twhere.=' <font size="-2">'.
513: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
514: '</font>';
515: }
516: } else {
517: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
518: if (%newhash) {
519: $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
520: "\0".$envkey;
521: $twhere=$newhash{'description'}.
522: ' <font size="-2">'.
523: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
524: '</font>';
525: } else {
526: $twhere=&mt('Currently not available');
527: $ENV{'course.'.$tcourseid.'.description'}=$twhere;
528: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
529: }
530: }
531: if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
532: } elsif ($tdom) {
533: $ttype='Domain';
534: $twhere=$tdom;
535: $sortkey=$role.$twhere;
536: } else {
537: $ttype='System';
538: $twhere=&mt('system wide');
539: $sortkey=$role.$twhere;
540: }
541:
542: $roletext.=&build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver);
543: $roletext{$envkey}=$roletext;
544: if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
545: $sortrole{$sortkey}=$envkey;
546: $roleclass{$envkey}=$ttype;
547: }
548: }
549: }
550: # No active roles
551: if ($countactive==0) {
552: if ($inrole) {
553: $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
554: } else {
555: $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
556: }
557: $r->print('</form></body></html>');
558: return OK;
559: # Is there only one choice?
560: } elsif (($countactive==1) && ($ENV{'request.role'} eq 'cm')) {
561: $r->print('<h3>'.&mt('Please stand by.').'</h3>'.
562: '<input type="hidden" name="'.$possiblerole.'" value="1" />');
563: $r->print("</form>\n");
564: $r->rflush();
565: $r->print('<script>document.forms.rolechoice.submit();</script>');
566: $r->print('</body></html>');
567: return OK;
568: }
569: # More than one possible role
570: # ----------------------------------------------------------------------- Table
571: unless (($advanced) || ($nochoose)) {
572: $r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
573: }
574: $r->print('<br /><table><tr>');
575: unless ($nochoose) { $r->print('<th> </th>'); }
576: $r->print('<th>'.&mt('User Role').'</th><th colspan=2>'.&mt('Extent').
577: '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th><th>'.
578: &mt('Remarks and Calendar Announcements').'</th></tr>'."\n");
579: my $doheaders=-1;
580: foreach my $type ('Construction Space','Course','Domain','System') {
581: my $haverole=0;
582: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
583: if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) {
584: $haverole=1;
585: }
586: }
587: if ($haverole) { $doheaders++; }
588: }
589:
590: if ($ENV{'environment.recentroles'}) {
591: my %recent_roles =
592: &Apache::lonhtmlcommon::get_recent('roles',$ENV{'environment.recentrolesn'});
593: my $output='';
594: foreach (sort(keys(%recent_roles))) {
595: if (defined($roletext{'user.role.'.$_})) {
596: $output.=$roletext{'user.role.'.$_};
597: } elsif ($numdc > 0) {
598: unless ($_ =~/^error\:/) {
599: $output.=&display_cc_role('user.role.'.$_);
600: }
601: }
602: }
603: if ($output) {
604: $r->print("<tr bgcolor='#BBffBB'><td align='center' colspan='7'>".
605: &mt('Recent Roles')."</td>");
606: $r->print($output);
607: $r->print("</tr>");
608: }
609: }
610:
611: if ($numdc > 0) {
612: $r->print(&coursepick_jscript());
613: $r->print(&Apache::loncommon::coursebrowser_javascript());
614: }
615: foreach my $type ('Construction Space','Course','Domain','System') {
616: my $output;
617: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
618: if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) {
619: $output.=$roletext{$sortrole{$which}};
620: if ($sortrole{$which} =~ m-dc\./(\w+)/-) {
621: if ($dcroles{$1}) {
622: $output .= &allcourses_row($1);
623: }
624: }
625: }
626: }
627: if ($output) {
628: if ($doheaders > 0) {
629: $r->print("<tr bgcolor='#BBffBB'>".
630: "<td align='center' colspan='7'>".&mt($type)."</td></tr>");
631: }
632: $r->print($output);
633: }
634: }
635: my $tremark='';
636: my $tfont='#003300';
637: if ($ENV{'request.role'} eq 'cm') {
638: $r->print('<tr bgcolor="#11CC55">');
639: $tremark=&mt('Currently selected. ');
640: $tfont='#002200';
641: } else {
642: $r->print('<tr bgcolor="#77FF77">');
643: }
644: unless ($nochoose) {
645: if ($ENV{'request.role'} ne 'cm') {
646: $r->print('<td><input type=submit value="'.
647: &mt('Select').'" name="cm"></td>');
648: } else {
649: $r->print('<td> </td>');
650: }
651: }
652: $r->print('<td colspan=5><font color="'.$tfont.'">'.&mt('No role specified').
653: '</font></td><td><font color="'.$tfont.'">'.$tremark.
654: ' </font></td></tr>'."\n");
655:
656: $r->print('</table>');
657: unless ($nochoose) {
658: $r->print("</form>\n");
659: }
660: # ------------------------------------------------------------ Privileges Info
661: if (($advanced) && (($ENV{'user.error.msg'}) || ($error))) {
662: $r->print('<hr><h2>Current Privileges</h2>');
663:
664: foreach $envkey (sort keys %ENV) {
665: if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
666: my $where=$envkey;
667: $where=~s/^user\.priv\.$ENV{'request.role'}\.//;
668: my $ttype;
669: my $twhere;
670: my ($tdom,$trest,$tsec)=
671: split(/\//,Apache::lonnet::declutter($where));
672: if ($trest) {
673: if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
674: $ttype='Construction Space';
675: $twhere='User: '.$trest.', Domain: '.$tdom;
676: } else {
677: $ttype='Course';
678: $twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
679: if ($tsec) {
680: $twhere.=' (Section/Group: '.$tsec.')';
681: }
682: }
683: } elsif ($tdom) {
684: $ttype='Domain';
685: $twhere=$tdom;
686: } else {
687: $ttype='System';
688: $twhere='/';
689: }
690: $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
691: foreach (sort split(/:/,$ENV{$envkey})) {
692: if ($_) {
693: my ($prv,$restr)=split(/\&/,$_);
694: my $trestr='';
695: if ($restr ne 'F') {
696: my $i;
697: $trestr.=' (';
698: for ($i=0;$i<length($restr);$i++) {
699: $trestr.=
700: Apache::lonnet::plaintext(substr($restr,$i,1));
701: if ($i<length($restr)-1) { $trestr.=', '; }
702: }
703: $trestr.=')';
704: }
705: $r->print('<li>'.
706: Apache::lonnet::plaintext($prv).$trestr.
707: '</li>');
708: }
709: }
710: $r->print('</ul>');
711: }
712: }
713: }
714: $r->print(&Apache::lonnet::getannounce());
715: if ($advanced) {
716: $r->print('<p><small><i>This is LON-CAPA '.
717: $r->dir_config('lonVersion').'</i><br />'.
718: '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
719: }
720: $r->print("</body></html>\n");
721: return OK;
722: }
723:
724: sub role_status {
725: my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
726: my @pwhere = ();
727: if (exists($ENV{$rolekey}) && $ENV{$rolekey} ne '') {
728: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
729: unless (!defined($$role) || $$role eq '') {
730: $$where=join('.',@pwhere);
731: $$trolecode=$$role.'.'.$$where;
732: ($$tstart,$$tend)=split(/\./,$ENV{$rolekey});
733: $$tstatus='is';
734: if ($$tstart && $$tstart>$then) {
735: $$tstatus='future';
736: if ($$tstart<$now) { $$tstatus='will'; }
737: }
738: if ($$tend) {
739: if ($$tend<$then) {
740: $$tstatus='expired';
741: } elsif ($$tend<$now) {
742: $$tstatus='will_not';
743: }
744: }
745: }
746: }
747: }
748:
749: sub build_roletext {
750: my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver) = @_;
751: my $roletext='<tr bgcolor="'.$tbg.'">';
752: unless ($nochoose) {
753: if (!$button) {
754: if ($switchserver) {
755: $roletext.='<td><a href="/adm/logout?handover='.
756: $switchserver.'">'.&mt('Switch Server').'</a></td>';
757: } else {
758: $roletext.=('<td> </td>');
759: }
760: } elsif ($tstatus eq 'is') {
761: $roletext.=('<td><input type=submit value="'.
762: &mt('Select').'" name="'.
763: $trolecode.'"></td>');
764: } elsif ($tryagain) {
765: $roletext.=
766: '<td><input type=submit value="'.
767: &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
768: } elsif ($advanced) {
769: $roletext.=
770: '<td><input type=submit value="'.
771: &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
772: } else {
773: $roletext.='<td> </td>';
774: }
775: }
776: $tremark.=&Apache::lonannounce::showday(time,1,
777: &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
778:
779:
780: $roletext.='<td><font color="'.$tfont.'">'.$trole.
781: '</font></td><td><font color="'.$tfont.'">'.$ttype.
782: '</font></td><td><font color="'.$tfont.'">'.$twhere.
783: '</font></td><td><font color="'.$tfont.'">'.$tpstart.
784: '</font></td><td><font color="'.$tfont.'">'.$tpend.
785: '</font></td><td><font color="'.$tfont.'">'.$tremark.
786: ' </font></td></tr>'."\n";
787: return $roletext;
788: }
789:
790: sub check_privs {
791: my ($cckey,$then,$now) = @_;
792: if ($ENV{$cckey}) {
793: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
794: &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
795: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
796: &set_privileges($1,$2);
797: }
798: } else {
799: &set_privileges($1,$2);
800: }
801: }
802:
803: sub check_fordc {
804: my ($dcroles,$then) = @_;
805: my $numdc = 0;
806: if ($ENV{'user.adv'}) {
807: foreach my $envkey (sort keys %ENV) {
808: if ($envkey=~/^user\.role\.dc\.\/(\w+)\/$/) {
809: my $dcdom = $1;
810: my $livedc = 1;
811: my ($tstart,$tend)=split(/\./,$ENV{$envkey});
812: if ($tstart && $tstart>$then) { $livedc = 0; }
813: if ($tend && $tend <$then) { $livedc = 0; }
814: if ($livedc) {
815: $$dcroles{$dcdom} = $envkey;
816: $numdc++;
817: }
818: }
819: }
820: }
821: return $numdc;
822: }
823:
824: sub courselink {
825: my ($dcdom) = @_;
826: my $courseform=&Apache::loncommon::selectcourse_link
827: ('rolechoice','dccourse_'.$dcdom,'dcdomain_'.$dcdom,'coursedesc_'.$dcdom,$dcdom);
828: my $hiddenitems = '<input type="hidden" name="dcdomain_'.$dcdom.'" value="'.$dcdom.'" />'.
829: '<input type="hidden" name="origdom_'.$dcdom.'" value="'.$dcdom.'" />'.
830: '<input type="hidden" name="dccourse_'.$dcdom.'" value="" />'.
831: '<input type="hidden" name="coursedesc_'.$dcdom.'" value="" />';
832: return $courseform.$hiddenitems;
833: }
834:
835: sub coursepick_jscript {
836: my $verify_script = <<"END";
837: <script>
838: function verifyCoursePick(caller) {
839: var numbutton = getIndex(caller)
840: var pickedCourse = document.rolechoice.elements[numbutton+4].value
841: var pickedDomain = document.rolechoice.elements[numbutton+2].value
842: if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
843: if (pickedCourse != '') {
844: if (numbutton != -1) {
845: var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
846: document.rolechoice.elements[numbutton+1].name = courseTarget
847: document.rolechoice.submit()
848: }
849: }
850: else {
851: alert("You must use the 'Select Course' link to open a separate pick course window which you can use to select the course you wish to enter");
852: }
853: }
854: else {
855: alert("You can only use this screen to select courses in the current domain")
856: }
857: }
858: function getIndex(caller) {
859: for (var i=0;i<document.rolechoice.elements.length;i++) {
860: if (document.rolechoice.elements[i] == caller) {
861: return i;
862: }
863: }
864: return -1;
865: }
866: </script>
867: END
868: return $verify_script;
869: }
870:
871: sub processpick {
872: my $dcdom = shift;
873: my $process_pick = <<"END";
874: <script>
875: function process_pick(dom) {
876: var numbutton = getIndex(dom)
877: var pickedCourse = opener.document.rolechoice.dccourse_$dcdom.value
878: var pickedDomain = opener.document.rolechoice.dcdomain_$dcdom.value
879: if (opener.document.rolechoice.dcdomain_$dcdom.value == opener.document.rolechoice.origdom_$dcdom.value) {
880: if (pickedCourse != '') {
881: if (numbutton != -1) {
882: var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
883: opener.document.rolechoice.elements[numbutton+1].name = courseTarget
884: opener.document.rolechoice.submit()
885: }
886: }
887: }
888: }
889:
890: function getIndex(dom) {
891: var callername = 'ccpick_'+dom
892: for (var i=0;i<opener.document.rolechoice.elements.length;i++) {
893: var elemname = opener.document.rolechoice.elements[i].name
894: if (elemname == callername) {
895: return i;
896: }
897: }
898: return -1;
899: }
900: </script>
901: END
902: return $process_pick;
903: }
904:
905: sub display_cc_role {
906: my $rolekey = shift;
907: my $roletext;
908: my $advanced = $ENV{'user.adv'};
909: my $tryagain = $ENV{'form.tryagain'};
910: unless ($rolekey =~/^error\:/) {
911: if ($rolekey =~ m-^user\.role.cc\./(\w+)/(\w+)$-) {
912: my $tcourseid = $1.'_'.$2;
913: my $trolecode = 'cc./'.$1.'/'.$2;
914: my $trole = Apache::lonnet::plaintext('cc');
915: my $twhere;
916: my $tbg='#77FF77';
917: my $tfont='#003300';
918: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
919: if (%newhash) {
920: $twhere=$newhash{'description'}.
921: ' <font size="-2">'.
922: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont).
923: '</font>';
924: } else {
925: $twhere=&mt('Currently not available');
926: $ENV{'course.'.$tcourseid.'.description'}=$twhere;
927: }
928: $twhere.="<br />".&mt('Domain').":".$1;
929: $roletext = &build_roletext($trolecode,$1,$2,'is',$tryagain,$advanced,'',$tbg,$tfont,$trole,&mt('Course'),$twhere,'','','',1,'');
930: }
931: }
932: return $roletext;
933: }
934:
935: sub allcourses_row {
936: my $dcdom = shift;
937: my $ccrole = Apache::lonnet::plaintext('cc');
938: my $selectlink = &courselink($dcdom);
939: my $output = '<tr bgcolor="#77FF77">'.
940: '<td><input type="button" value="'.
941: &mt('Select').'" name="ccpick_'.$dcdom.'"'.
942: 'onClick="verifyCoursePick(this)">'.
943: '<input type="hidden" name="pick_'.$dcdom.'" value="1"></td>'.
944: '<td><font color="#002200">'.
945: $ccrole.'</font></td><td>'.&mt('Course').'</td>'.
946: '<td><font color="#002200">'.&mt('All courses').':<b> '.
947: $selectlink.'</b>'.
948: '<br />'.&mt('Domain').':'.$dcdom.'</font>'.
949: '<td colspan="4"><font color="#002200">'.
950: &mt('Course Coordinator access to all courses in domain').
951: ': <b>'.$dcdom.'</b></font></td></tr>'."\n";
952: return $output;
953: }
954:
955: sub recent_filename {
956: my $area=shift;
957: return 'nohist_recent_'.&Apache::lonnet::escape($area);
958: }
959:
960: sub set_privileges {
961: my ($dcdom,$pickedcourse) = @_;
962: my $area = '/'.$dcdom.'/'.$pickedcourse;
963: my $role = 'cc';
964: my $spec = $role.'.'.$area;
965: my $userroles = &Apache::lonnet::set_arearole($role,$area,'','',$dcdom,$ENV{'user.name'});
966: my %ccrole = ();
967: &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
968: my ($author,$adv)= &Apache::lonnet::set_userprivs(\$userroles,\%ccrole);
969: my @newprivs = split/\n/,$userroles;
970: my %newccroles = ();
971: foreach (@newprivs) {
972: my ($key,$val) = split/=/,$_;
973: $newccroles{$key} = $val;
974: }
975: &Apache::lonnet::appenv(%newccroles);
976: &Apache::lonnet::log($ENV{'user.domain'},
977: $ENV{'user.name'},
978: $ENV{'user.home'},
979: "Role ".$role);
980: &Apache::lonnet::appenv(
981: 'request.role' => $role,
982: 'request.role.domain' => $dcdom,
983: 'request.course.sec' => '');
984: my $tadv=0;
985: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
986: &Apache::lonnet::appenv('request.role.adv' => $tadv);
987: }
988:
989: 1;
990: __END__
991:
992: =head1 NAME
993:
994: Apache::lonroles - User Roles Screen
995:
996: =head1 SYNOPSIS
997:
998: Invoked by /etc/httpd/conf/srm.conf:
999:
1000: <Location /adm/roles>
1001: PerlAccessHandler Apache::lonacc
1002: SetHandler perl-script
1003: PerlHandler Apache::lonroles
1004: ErrorDocument 403 /adm/login
1005: ErrorDocument 500 /adm/errorhandler
1006: </Location>
1007:
1008: =head1 OVERVIEW
1009:
1010: =head2 Choosing Roles
1011:
1012: C<lonroles> is a handler that allows a user to switch roles in
1013: mid-session. LON-CAPA attempts to work with "No Role Specified", the
1014: default role that a user has before selecting a role, as widely as
1015: possible, but certain handlers for example need specification which
1016: course they should act on, etc. Both in this scenario, and when the
1017: handler determines via C<lonnet>'s C<&allowed> function that a certain
1018: action is not allowed, C<lonroles> is used as error handler. This
1019: allows the user to select another role which may have permission to do
1020: what they were trying to do. C<lonroles> can also be accessed via the
1021: B<CRS> button in the Remote Control.
1022:
1023: =begin latex
1024:
1025: \begin{figure}
1026: \begin{center}
1027: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
1028: \caption{\label{Sample_Roles_Screen}Sample Roles Screen}
1029: \end{center}
1030: \end{figure}
1031:
1032: =end latex
1033:
1034: =head2 Role Initialization
1035:
1036: 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.
1037:
1038: =head1 INTRODUCTION
1039:
1040: This module enables a user to select what role he wishes to
1041: operate under (instructor, student, teaching assistant, course
1042: coordinator, etc). These roles are pre-established by the actions
1043: of upper-level users.
1044:
1045: This is part of the LearningOnline Network with CAPA project
1046: described at http://www.lon-capa.org.
1047:
1048: =head1 HANDLER SUBROUTINE
1049:
1050: This routine is called by Apache and mod_perl.
1051:
1052: =over 4
1053:
1054: =item *
1055:
1056: Roles Initialization (yes/no)
1057:
1058: =item *
1059:
1060: Get Error Message from Environment
1061:
1062: =item *
1063:
1064: Who is this?
1065:
1066: =item *
1067:
1068: Generate Page Output
1069:
1070: =item *
1071:
1072: Choice or no choice
1073:
1074: =item *
1075:
1076: Table
1077:
1078: =item *
1079:
1080: Privileges
1081:
1082: =back
1083:
1084: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>