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