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