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