Annotation of loncom/interface/lontiny.pm, revision 1.17
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Extract domain, courseID, and symb from a shortened URL,
3: # and switch role to a role in designated course.
4: #
1.17 ! raeburn 5: # $Id: lontiny.pm,v 1.16 2022/07/08 03:05:18 raeburn Exp $
1.1 raeburn 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
29:
30: package Apache::lontiny;
31:
32: use strict;
33: use Apache::Constants qw(:common :http);
34: use Apache::lonnet;
35: use Apache::loncommon;
36: use Apache::lonhtmlcommon;
37: use Apache::lonroles;
1.5 raeburn 38: use Apache::lonuserstate;
39: use Apache::lonnavmaps;
1.1 raeburn 40: use Apache::lonlocal;
41: use LONCAPA qw(:DEFAULT :match);
42:
43: sub handler {
44: my $r = shift;
45: my %user;
46: my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
1.6 raeburn 47: if ($handle ne '') {
1.5 raeburn 48: my $lonidsdir=$r->dir_config('lonIDsDir');
49: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.1 raeburn 50: if ($r->uri =~ m{^/tiny/($match_domain)/(\w+)$}) {
51: my ($cdom,$key) = ($1,$2);
52: if (&Apache::lonnet::domain($cdom) ne '') {
53: my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
54: my $tinyurl;
55: my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
56: if (defined($cached)) {
57: $tinyurl = $result;
58: } else {
59: my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
60: if ($currtiny{$key} ne '') {
61: $tinyurl = $currtiny{$key};
62: &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
63: }
64: }
65: if ($tinyurl) {
66: my ($cnum,$symb) = split(/\&/,$tinyurl);
67: if ($cnum =~ /^$match_courseid$/) {
68: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
69: if ($chome ne 'no_host') {
1.8 raeburn 70: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['ttoken']);
1.12 raeburn 71: my ($linkprot,$linkprotuser,$linkprotexit,$ltoken);
1.11 raeburn 72: if ($env{'form.ttoken'}) {
73: my %link_info = &Apache::lonnet::tmpget($env{'form.ttoken'});
74: if ($link_info{'origurl'} eq $r->uri) {
75: if ($link_info{'ltoken'}) {
76: $ltoken = $link_info{'ltoken'};
77: my %ltoken_info = &Apache::lonnet::tmpget($link_info{'ltoken'});
78: $linkprot = $ltoken_info{'linkprot'};
79: $linkprotuser = $ltoken_info{'linkprotuser'};
1.12 raeburn 80: $linkprotexit = $ltoken_info{'linkprotexit'};
1.11 raeburn 81: } elsif ($link_info{'linkprot'}) {
82: $linkprot = $link_info{'linkprot'};
83: if ($link_info{'linkprotuser'}) {
84: $linkprotuser = $link_info{'linkprotuser'};
85: }
1.12 raeburn 86: if ($link_info{'linkprotexit'}) {
87: $linkprotexit = $link_info{'linkprotexit'};
88: }
1.11 raeburn 89: }
90: }
91: }
1.5 raeburn 92: if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
1.8 raeburn 93: # Check for ttoken
94: my $newlauncher = &launch_check($r->uri,$symb);
1.5 raeburn 95: my ($map,$resid,$url) = &Apache::lonnet::decode_symb($symb);
96: if (&Apache::lonnet::is_on_map($url)) {
97: my $realuri;
98: if ((&Apache::lonnet::EXT('resource.0.hiddenresource',$symb) =~ /^yes$/i) &&
99: (!$env{'request.role.adv'})) {
100: $env{'user.error.msg'}=$r->uri.':bre:1:1:Access to resource denied';
101: return HTTP_NOT_ACCEPTABLE;
102: }
103: if ((&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) &&
104: (!$env{'request.role.adv'})) {
105: $realuri = &Apache::lonenc::encrypted(&Apache::lonnet::clutter($url));
106: if (($url =~ /\.sequence$/) &&
107: ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
108: $realuri .= '?navmap=1';
109: } else {
110: $realuri .= '?symb='.&Apache::lonenc::encrypted($symb);
111: }
112: } else {
113: $realuri = &Apache::lonnet::clutter($url);
114: if (($url =~ /\.sequence$/) &&
115: ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
116: $realuri .= '?navmap=1';
117: } else {
118: $realuri .= '?symb='.$symb;
119: }
120: }
1.17 ! raeburn 121: my ($update,$reinitresult);
1.5 raeburn 122: # Check if course needs to be re-initialized
1.7 raeburn 123: if ($newlauncher) {
1.5 raeburn 124: $update = 1;
1.7 raeburn 125: } else {
126: my $loncaparev = $r->dir_config('lonVersion');
1.17 ! raeburn 127: ($reinitresult,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
! 128: if (($reinitresult eq 'main') || ($reinitresult eq 'both')) {
1.7 raeburn 129: $update = 1;
130: } elsif (!-e $env{'request.course.fn'}.'.db') {
131: $update = 1;
132: } elsif (!$env{'request.role.adv'}) {
133: my $navmap = Apache::lonnavmaps::navmap->new();
134: if (ref($navmap)) {
135: my $res = $navmap->getBySymb($symb);
136: if (ref($res)) {
137: my ($enc_in_bighash,$enc_in_parm);
138: $enc_in_bighash = $res->encrypted();
139: if (&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) {
140: $enc_in_parm = 1;
141: }
142: if ($enc_in_bighash ne $enc_in_parm) {
143: $update = 1;
144: }
1.5 raeburn 145: }
146: }
147: }
148: }
149: if ($update) {
150: my ($furl,$ferr)=
151: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
152: if ($ferr) {
153: $env{'user.error.msg'}=$r->uri.':bre:0:0:Course not initialized';
154: $env{'user.reinit'} = 1;
155: return HTTP_NOT_ACCEPTABLE;
156: }
157: }
1.17 ! raeburn 158: if (($reinitresult eq 'both') || ($reinitresult eq 'supp')) {
! 159: my $possdel;
! 160: if ($reinitresult eq 'supp') {
! 161: $possdel = 1;
! 162: }
! 163: my ($supplemental,$refs_updated) = &Apache::lonnet::get_supplemental($cnum,$cdom,'',$possdel);
! 164: unless ($refs_updated) {
! 165: &Apache::loncommon::set_supp_httprefs($cnum,$cdom,$supplemental,$possdel);
! 166: }
! 167: }
1.5 raeburn 168: my $host = $r->headers_in->get('Host');
169: if (!$host) {
170: $r->internal_redirect($realuri);
171: return OK;
172: } else {
173: my $protocol = 'http';
174: if ($r->get_server_port == 443) {
175: $protocol = 'https';
1.1 raeburn 176: }
1.5 raeburn 177: my $location = $protocol.'://'.$host.$realuri;
178: $r->headers_out->set(Location => $location);
179: return REDIRECT;
1.1 raeburn 180: }
181: }
1.5 raeburn 182: } else {
183: my %crsenv = &Apache::lonnet::coursedescription("$cdom/$cnum");
184: my @possroles = ('in','ta','ep','st','cr','ad');
185: if ($crsenv{'type'} eq 'Community') {
186: unshift(@possroles,'co');
187: } else {
188: unshift(@possroles,'cc');
189: }
1.11 raeburn 190: my %roleshash =
191: &Apache::lonnet::get_my_roles($env{'user.uname'},$env{'user.domain'},
192: 'userroles',['previous','active','future'],
193: \@possroles,[$cdom],1);
194: my (%possroles,$hassection,%active,%expired,%future);
1.5 raeburn 195: if (keys(%roleshash)) {
1.11 raeburn 196: my $now = time;
1.5 raeburn 197: foreach my $entry (keys(%roleshash)) {
198: if ($entry =~ /^\Q$cnum:$cdom:\E([^:]+):([^:]*)$/) {
1.11 raeburn 199: my ($role,$sec) = ($1,$2);
200: $possroles{$role} = $sec;
201: if ($sec ne '') {
1.5 raeburn 202: $hassection = 1;
203: }
1.11 raeburn 204: my ($tstart,$tend)=split(/\:/,$roleshash{$entry});
205: my $status = 'active';
206: if (($tend) && ($tend<=$now)) {
207: $status = 'previous';
208: }
209: if (($tstart) && ($now<$tstart)) {
210: $status = 'future';
211: }
212: if ($status eq 'active') {
213: $active{$role} = $sec;
214: } elsif ($status eq 'previous') {
215: $expired{$tend} = $role.':'.$sec;
216: } elsif ($status eq 'future') {
217: $future{$tstart} = $role.':'.$sec;
218: }
1.5 raeburn 219: }
1.4 raeburn 220: }
221: }
1.11 raeburn 222: my @allposs = keys(%active);
1.5 raeburn 223: if ($env{'request.lti.login'}) {
224: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
225: if ($env{'request.lti.target'} eq '') {
226: if ($env{'form.ltitarget'} eq 'iframe') {
227: &Apache::lonnet::appenv({'request.lti.target' => 'iframe'});
228: delete($env{'form.ltitarget'});
1.4 raeburn 229: }
1.5 raeburn 230: }
231: if ($env{'form.selectrole'}) {
232: foreach my $role (@allposs) {
233: my $newrole = "$role./$cdom/$cnum";
234: if ($possroles{$allposs[0]} ne '') {
235: $newrole .= "/$possroles{$role}";
236: }
237: if ($env{"form.$newrole"}) {
238: my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
1.11 raeburn 239: '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
1.5 raeburn 240: if ($env{'form.ltitarget'} eq 'iframe') {
241: $destination .= '<itarget=iframe';
242: }
243: &do_redirect($r,$destination);
244: return OK;
1.4 raeburn 245: }
246: }
247: }
248: }
1.5 raeburn 249: if (@allposs == 0) {
1.12 raeburn 250: &show_roles($r,\%crsenv,\%active,'','',\%future,\%expired,$linkprot,$linkprotuser,$linkprotexit,$ltoken);
1.5 raeburn 251: } elsif (@allposs == 1) {
252: my $newrole = "$allposs[0]./$cdom/$cnum";
253: $newrole = "$allposs[0]./$cdom/$cnum";
254: if ($possroles{$allposs[0]} ne '') {
1.11 raeburn 255: $newrole .= "/$possroles{$allposs[0]}";
1.1 raeburn 256: }
257: my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
1.11 raeburn 258: '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
1.8 raeburn 259: if ($env{'form.ttoken'}) {
260: $destination .= '&ttoken='.$env{'form.ttoken'};
261: }
1.11 raeburn 262: &do_redirect($r,$destination,$linkprot);
1.16 raeburn 263: } elsif (@allposs > 1) {
1.5 raeburn 264: if (grep(/^(cc|co)$/,@allposs)) {
265: my $newrole;
266: if (exists($possroles{'cc'})) {
267: $newrole = 'cc';
268: } else {
269: $newrole = 'co';
270: }
271: $newrole .= "./$cdom/$cnum";
272: my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
1.11 raeburn 273: '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
1.8 raeburn 274: if ($env{'form.ttoken'}) {
275: $destination .= '&ttoken='.$env{'form.ttoken'};
276: }
1.11 raeburn 277: &do_redirect($r,$destination,$linkprot);
1.5 raeburn 278: } else {
279: my $hascustom;
280: if (grep(/^cr\//,@allposs)) {
281: $hascustom = 1;
282: }
1.16 raeburn 283: &show_roles($r,\%crsenv,\%active,$hassection,$hascustom);
1.1 raeburn 284: }
285: }
1.5 raeburn 286: return OK;
1.1 raeburn 287: }
288: }
289: }
290: }
291: }
292: }
293: &generic_error($r);
294: return OK;
295: } else {
296: return FORBIDDEN;
297: }
298: }
299:
1.7 raeburn 300: sub launch_check {
1.8 raeburn 301: my ($linkuri,$symb) = @_;
1.15 raeburn 302: my ($linkprotector,$linkproturi,$linkprotexit,$linkkey,$newlauncher);
1.8 raeburn 303: if ($env{'form.ttoken'}) {
304: my %link_info = &Apache::lonnet::tmpget($env{'form.ttoken'});
305: &Apache::lonnet::tmpdel($env{'form.ttoken'});
306: delete($env{'form.ttoken'});
1.10 raeburn 307: if ($link_info{'ltoken'}) {
308: unless (($link_info{'linkprot'}) || ($link_info{'linkkey'} ne '')) {
309: my %ltoken_info = &Apache::lonnet::tmpget($link_info{'ltoken'});
310: if ($ltoken_info{'linkprot'}) {
311: $link_info{'linkprot'} = $ltoken_info{'linkprot'};
312: } elsif ($ltoken_info{'linkkey'} ne '') {
313: $link_info{'linkkey'} = $ltoken_info{'linkkey'};
314: }
315: }
1.11 raeburn 316: &Apache::lonnet::tmpdel($link_info{'ltoken'});
1.10 raeburn 317: }
1.7 raeburn 318: if ($link_info{'linkprot'}) {
319: ($linkprotector,$linkproturi) = split(/:/,$link_info{'linkprot'},2);
320: if ($env{'user.linkprotector'}) {
321: my @protectors = split(/,/,$env{'user.linkprotector'});
322: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
323: push(@protectors,$linkprotector);
324: @protectors = sort { $a <=> $b } @protectors;
325: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
326: }
327: } else {
328: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
329: }
330: if ($env{'user.linkproturi'}) {
331: my @proturis = split(/,/,$env{'user.linkproturi'});
332: unless(grep(/^\Q$linkproturi\E$/,@proturis)) {
333: push(@proturis,$linkproturi);
334: @proturis = sort(@proturis);
335: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
336: }
337: } else {
338: &Apache::lonnet::appenv({'user.linkproturi' => $linkproturi});
339: }
1.15 raeburn 340: if ($link_info{'linkprotexit'}) {
341: $linkprotexit = $link_info{'linkprotexit'};
342: }
1.8 raeburn 343: } elsif ($link_info{'linkkey'} ne '') {
344: $linkkey = $link_info{'linkkey'};
345: my $keyedlinkuri = $linkuri;
346: if ($env{'user.deeplinkkey'} ne '') {
347: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
348: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
349: push(@linkkeys,$linkkey);
350: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
351: }
352: } else {
353: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
354: }
355: if ($env{'user.keyedlinkuri'}) {
356: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
357: unless (grep(/^\Q$keyedlinkuri\E$/,@keyeduris)) {
358: push(@keyeduris,$keyedlinkuri);
359: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
360: }
361: } else {
362: &Apache::lonnet::appenv({'user.keyedlinkuri' => $keyedlinkuri});
363: }
1.7 raeburn 364: }
1.8 raeburn 365: if ($link_info{'checklaunch'}) {
366: $newlauncher = 1;
1.7 raeburn 367: }
1.8 raeburn 368: }
369: my $currdeeplinklogin = $env{'request.deeplink.login'};
370: my $deeplink;
371: if ($symb =~ /\.(page|sequence)$/) {
372: my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($symb))[2]);
373: my $navmap = Apache::lonnavmaps::navmap->new();
374: if (ref($navmap)) {
375: $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');
1.7 raeburn 376: }
1.8 raeburn 377: } else {
378: $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$symb);
1.7 raeburn 379: }
1.8 raeburn 380: if ($deeplink ne '') {
381: my $disallow;
1.15 raeburn 382: my ($state,$others,$listed,$scope,$protect,$display,$target,$exit) = split(/,/,$deeplink);
1.8 raeburn 383: if (($protect ne 'none') && ($protect ne '')) {
384: my ($acctype,$item) = split(/:/,$protect);
385: if ($acctype =~ /lti(c|d)$/) {
386: my $ltitype = $1;
387: if ($linkprotector) {
388: unless ($linkprotector.':'.$linkproturi eq $item.$ltitype.':'.$linkuri) {
389: $disallow = 1;
390: }
391: } else {
392: $disallow = 1;
1.7 raeburn 393: }
1.8 raeburn 394: } elsif ($acctype eq 'key') {
395: if ($linkkey ne '') {
396: unless ($linkkey eq $item) {
397: $disallow = 1;
1.7 raeburn 398: }
1.8 raeburn 399: } else {
400: $disallow = 1;
1.7 raeburn 401: }
1.8 raeburn 402: }
403: }
404: if ($disallow) {
405: if ($currdeeplinklogin eq $linkuri) {
406: &Apache::lonnet::delenv('request.deeplink.login');
1.10 raeburn 407: if ($env{'request.deeplink.target'} ne '') {
408: &Apache::lonnet::delenv('request.deeplink.target');
409: }
1.15 raeburn 410: if ($env{'request.linkprot'} ne '') {
411: &Apache::lonnet::delenv('request.linkprot');
412: }
413: if ($env{'request.linkprotexit'} ne '') {
414: &Apache::lonnet::delenv('request.linkprotexit');
415: }
1.8 raeburn 416: }
417: } else {
418: unless ($currdeeplinklogin eq $linkuri) {
419: if (($linkprotector) || ($linkkey ne '')) {
420: if ($linkprotector) {
421: &Apache::lonnet::appenv({'request.linkprot' => $linkprotector.':'.$linkproturi});
422: } elsif ($env{'request.linkprot'}) {
1.14 raeburn 423: &Apache::lonnet::delenv('request.linkprot');
1.7 raeburn 424: }
1.15 raeburn 425: if ($linkprotexit) {
426: &Apache::lonnet::appenv({'request.linkprotexit' => $linkprotexit});
427: } elsif ($env{'request.linkprotexit'}) {
428: &Apache::lonnet::delenv('request.linkprotexit');
429: }
1.8 raeburn 430: if ($linkkey ne '') {
431: &Apache::lonnet::appenv({'request.linkkey' => $linkkey});
432: } elsif ($env{'request.linkkey'} ne '') {
1.13 raeburn 433: &Apache::lonnet::delenv('request.linkkey');
1.7 raeburn 434: }
1.8 raeburn 435: $newlauncher = 1;
1.7 raeburn 436: }
437: }
438: &Apache::lonnet::appenv({'request.deeplink.login' => $linkuri});
1.9 raeburn 439: if ($target ne '') {
440: &Apache::lonnet::appenv({'request.deeplink.target' => $target});
1.10 raeburn 441: } elsif ($env{'request.deeplink.target'} ne '') {
442: &Apache::lonnet::delenv('request.deeplink.target');
1.9 raeburn 443: }
1.7 raeburn 444: }
445: } else {
446: if ($linkprotector) {
447: &Apache::lonnet::appenv({'request.linkprot' => $linkprotector.':'.$linkproturi});
1.8 raeburn 448: } elsif ($env{'request.linkprot'}) {
1.14 raeburn 449: &Apache::lonnet::delenv('request.linkprot');
1.8 raeburn 450: }
1.15 raeburn 451: if ($linkprotexit) {
452: &Apache::lonnet::appenv({'request.linkprotexit' => $linkprotexit});
453: } elsif ($env{'request.linkprotexit'}) {
454: &Apache::lonnet::delenv('request.linkprotexit');
455: }
1.8 raeburn 456: if ($linkkey ne '') {
1.7 raeburn 457: &Apache::lonnet::appenv({'request.linkkey' => $linkkey});
1.8 raeburn 458: } else {
1.13 raeburn 459: &Apache::lonnet::delenv('request.linkkey');
1.7 raeburn 460: }
1.8 raeburn 461: &Apache::lonnet::appenv({'request.deeplink.login' => $linkuri});
1.10 raeburn 462: if ($env{'request.deeplink.target'} ne '') {
463: &Apache::lonnet::delenv('request.deeplink.target');
464: }
1.7 raeburn 465: }
466: return $newlauncher;
467: }
468:
1.1 raeburn 469: sub do_redirect {
1.11 raeburn 470: my ($r,$destination,$linkprot) = @_;
1.2 raeburn 471: my $windowname = 'loncapaclient';
472: if ($env{'request.lti.login'}) {
473: $windowname .= 'lti';
474: }
1.1 raeburn 475: my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
476: my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
1.11 raeburn 477: if ($linkprot) {
478: $args = {'only_body' => 1,
479: 'redirect' => [0,$destination],};
480: }
1.1 raeburn 481: &Apache::loncommon::content_type($r,'text/html');
482: $r->send_http_header;
1.11 raeburn 483: if ($linkprot) {
484: $r->print(&Apache::loncommon::start_page('Valid link','',$args).
485: &Apache::loncommon::end_page());
486: } else {
487: $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
488: &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
489: '<h1>'.&mt('Welcome').'</h1>'.
490: '<p>'.&mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>').'</p><p>'.
491: '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
492: &Apache::loncommon::end_page());
493: }
1.1 raeburn 494: return;
495: }
496:
497: sub show_roles {
1.12 raeburn 498: my ($r,$crsenv,$possroles,$hassection,$hascustom,$futureroles,$expiredroles,$linkprot,$linkprotuser,$linkprotexit,$ltoken) = @_;
1.1 raeburn 499: my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
500: if (ref($crsenv) eq 'HASH') {
501: $crsdesc = $crsenv->{'description'};
502: $crstype = $crsenv->{'type'};
503: $cdom = $crsenv->{'domain'};
504: $cnum = $crsenv->{'num'};
505: }
506: if ($crstype eq '') {
507: $crstype = 'Course';
508: }
509: my $lc_crstype = lc($crstype);
510: if ($crsdesc ne '') {
511: $header = &mt("The page you requested belongs to the following $lc_crstype: [_1]",
512: '<i>'.$crsdesc.'</i>');
513: }
514: if (ref($possroles) eq 'HASH') {
515: if (keys(%{$possroles}) > 0) {
516: $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
1.11 raeburn 517: if ($linkprot) {
518: $args = {'only_body' => 1};
519: }
1.1 raeburn 520: $title = 'Choose a role'; #Do not localize.
521: if ($crstype eq 'Community') {
522: $preamble = &mt('You have the following active roles in this community:');
523: } else {
524: $preamble = &mt('You have the following active roles in this course:');
525: }
526: $datatable = '<form name="" action="/adm/roles">'.
1.8 raeburn 527: '<input type="hidden" name="newrole" value="" />'."\n".
528: '<input type="hidden" name="selectrole" value="1" />'."\n".
1.11 raeburn 529: '<input type="hidden" name="destinationurl" value="'.&HTML::Entities::encode($r->uri,'&<>"').'" />'."\n";
1.8 raeburn 530: if ($env{'form.ttoken'}) {
531: $datatable .= '<input type="hidden" name="ttoken" value="'.$env{'form.ttoken'}.'" />'."\n";
532: }
533: $datatable .= &Apache::loncommon::start_data_table().
534: &Apache::loncommon::start_data_table_header_row().
535: '<th></th><th>'.&mt('User role').'</th>';
1.1 raeburn 536: if ($hassection) {
537: $datatable .= '<th>'.&mt('Section').'</th>';
538: }
539: if ($hascustom) {
540: $datatable .= '<th>'.&mt('Information').'</th>';
541: }
542: $datatable .= &Apache::loncommon::end_data_table_header_row();
543: my @available = sort(keys(%{$possroles}));
544: foreach my $role ('ad','in','ta','ep','st','cr') {
545: foreach my $key (@available) {
546: if ($key =~ m{^$role($|/)}) {
547: my $trolecode = "$key./$cdom/$cnum";
548: my $rolename = &Apache::lonnet::plaintext($key,$crstype,$cdom.'_'.$cnum);
549: my $sec = $possroles->{$key};
550: if ($sec ne '') {
551: $trolecode .= '/'.$sec;
552: }
553: my $buttonname=$trolecode;
554: $buttonname=~s/\W//g;
555: $datatable .= &Apache::loncommon::start_data_table_row().
556: '<td><input name="'.$buttonname.'" type="button" value="'.
557: &mt('Select').'" onclick="javascript:enterrole(this.form,'.
558: "'$trolecode','$buttonname'".');" /></td>';
559: if ($key =~ /^cr\//) {
560: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$key);
561: $datatable .= '<td><span class="LC_nobreak">'.$rolename.'</span></td>';
562: if ($hassection) {
563: $datatable .= '<td>'.$sec.'</td>';
564: }
565: $datatable.= '<td><span class="LC_fontsize_small LC_cusr_emph">'.
566: &mt('Custom role defined by [_1]',$rauthor.':'.$rdomain).
567: '</td>';
568: } else {
569: if ($hassection) {
570: $datatable .= '<td>'.$rolename.'</td>';
571: if ($hascustom) {
572: $datatable .= '<td colspan="2">'.$sec.'</td>';
573: } else {
574: $datatable .= '<td>'.$sec.'</td>';
575: }
576: } elsif ($hascustom) {
577: $datatable .= '<td colspan="2">'.$rolename.'</td>';
578: } else {
579: $datatable .= '<td>'.$rolename.'</td>';
580: }
581: }
582: $datatable .= &Apache::loncommon::end_data_table_row();
583: }
584: }
585: }
586: $datatable .= &Apache::loncommon::end_data_table().
587: '</form>';
588: my $standby = &mt('Role selected. Please stand by.');
589: $js = <<"ENDJS";
590: <script type="text/javascript">
591: // <![CDATA[
592:
593: active=true;
594:
595: function enterrole (thisform,rolecode,buttonname) {
596: if (active) {
597: active=false;
598: document.title='$standby';
599: window.status='$standby';
600: thisform.newrole.value=rolecode;
601: thisform.submit();
602: } else {
603: alert('$standby');
604: }
605: }
606:
607: // ]]>
608: </script>
609: ENDJS
610: } else {
1.11 raeburn 611: if ($linkprot) {
612: $title = 'No access';
613: $preamble = '<p>'.&mt('Access unavailable for this LON-CAPA content.').'</p>';
614: $args->{'only_body'} = 1;
615: } else {
616: $title = 'No active role';
617: $preamble = '<p>'.&mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.").'</p>';
618: $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
619: }
620: $header = &mt('No access for: [_1]','<b>'.&Apache::loncommon::plainname($env{'user.name'},
621: $env{'user.domain'}).'</b>');
622: if ((ref($futureroles) eq 'HASH') && (keys(%{$futureroles}) > 0)) {
623: my @future = sort { $a <=> $b } (keys(%{$futureroles}));
624: $preamble .= '<p>'.&mt('Access will begin: [_1].',&Apache::lonlocal::locallocaltime($future[0])).
625: ' '.&mt('Please try again then.').'</p>';
626: } elsif ((ref($expiredroles) eq 'HASH') && (keys(%{$expiredroles}) > 0)) {
627: my @expired = sort { $b <=> $a } (keys(%{$expiredroles}));
628: $preamble .= '<p>'.&mt('Access ended: [_1].',&Apache::lonlocal::locallocaltime($expired[0])).'</p>';
629: } elsif ($linkprot) {
630: if ($linkprotuser) {
631: my ($uname,$udom) = split(/:/,$linkprotuser,2);
632: $preamble .= '<p>'.&mt('As you followed a link from another system, while logged into that other system with the username: [_1], it is recommended that you contact your instructor.','<i>'.$uname.'</i>').'</p>';
633: } else {
634: my $relogin;
635: my %data = (
636: origurl => $r->uri,
637: linkprot => $linkprot,
1.12 raeburn 638: linkprotexit => $linkprotexit,
1.11 raeburn 639: );
640: my $token =
641: &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'retry');
642: unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
643: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
644: $relogin = '/adm/relaunch?rtoken='.$token;
645: }
646: $preamble .= '<p>'.&mt('You might try logging in with a different username and/or domain.').' '.
647: &mt('You are currently logged in as: [_1] in domain: [_2]',
648: '<i>'.$env{'user.name'}.'</i>','<i>'.$env{'user.domain'}.'</i>').'</p>';
649: if ($relogin) {
650: $preamble .= '<p>'.&mt('[_1]Log-in again[_2]','<a href="'.$relogin.'" target="_self">','</a>').'</p>';
651: }
652: }
653: }
654: if ($env{'form.ttoken'}) {
655: &Apache::lonnet::tmpdel($env{'form.ttoken'});
656: }
657: if ($ltoken) {
658: &Apache::lonnet::tmpdel($ltoken);
659: }
1.1 raeburn 660: }
661: }
662: &Apache::loncommon::content_type($r,'text/html');
663: $r->send_http_header;
664: $r->print(&Apache::loncommon::start_page($title,$js,$args).
665: '<h3>'.$header.'</h3>'.
666: '<div>'.$preamble.'</div>'.
667: $datatable.
668: &Apache::loncommon::end_page());
669: return;
670: }
671:
672: sub generic_error {
673: my ($r) = @_;
1.3 raeburn 674: my $continuelink;
675: unless ($env{'request.lti.login'}) {
676: my $linktext;
677: if ($env{'user.adv'}) {
678: $linktext = &mt('Continue to your roles page');
679: } else {
680: $linktext = &mt('Continue to your courses page');
681: }
682: $continuelink='<a href="/adm/roles">'.$linktext.'</a>';
1.1 raeburn 683: }
684: my $msg = &mt('The page you requested does not exist.');
685: &Apache::loncommon::content_type($r,'text/html');
686: $r->send_http_header;
687: my $args = {'bread_crumbs' => [{'href' => '','text' => 'Link status'},],};
688: $r->print(&Apache::loncommon::start_page('Invalid URL',undef,$args).
689: '<div class="LC_error">'.$msg.'</div>'.
690: '<p>'.$continuelink.'</p>'.
691: &Apache::loncommon::end_page());
692: return;
693: }
694:
695: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>