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