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