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