Annotation of loncom/interface/lontiny.pm, revision 1.6
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.6 ! raeburn 5: # $Id: lontiny.pm,v 1.5 2019/01/20 02:42:41 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.5 raeburn 70: if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
71: my ($map,$resid,$url) = &Apache::lonnet::decode_symb($symb);
72: if (&Apache::lonnet::is_on_map($url)) {
73: my $realuri;
74: if ((&Apache::lonnet::EXT('resource.0.hiddenresource',$symb) =~ /^yes$/i) &&
75: (!$env{'request.role.adv'})) {
76: $env{'user.error.msg'}=$r->uri.':bre:1:1:Access to resource denied';
77: return HTTP_NOT_ACCEPTABLE;
78: }
79: if ((&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) &&
80: (!$env{'request.role.adv'})) {
81: $realuri = &Apache::lonenc::encrypted(&Apache::lonnet::clutter($url));
82: if (($url =~ /\.sequence$/) &&
83: ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
84: $realuri .= '?navmap=1';
85: } else {
86: $realuri .= '?symb='.&Apache::lonenc::encrypted($symb);
87: }
88: } else {
89: $realuri = &Apache::lonnet::clutter($url);
90: if (($url =~ /\.sequence$/) &&
91: ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
92: $realuri .= '?navmap=1';
93: } else {
94: $realuri .= '?symb='.$symb;
95: }
96: }
97: my $update;
98: # Check if course needs to be re-initialized
99: my $loncaparev = $r->dir_config('lonVersion');
1.6 ! raeburn 100: my ($result,@reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
1.5 raeburn 101: if ($result eq 'update') {
102: $update = 1;
103: } elsif (!-e $env{'request.course.fn'}.'.db') {
104: $update = 1;
105: } elsif (!$env{'request.role.adv'}) {
106: my $navmap = Apache::lonnavmaps::navmap->new();
107: if (ref($navmap)) {
108: my $res = $navmap->getBySymb($symb);
109: if (ref($res)) {
110: my ($enc_in_bighash,$enc_in_parm);
111: $enc_in_bighash = $res->encrypted();
112: if (&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) {
113: $enc_in_parm = 1;
114: }
115: if ($enc_in_bighash ne $enc_in_parm) {
116: $update = 1;
117: }
118: }
119: }
120: }
121: if ($update) {
122: my ($furl,$ferr)=
123: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
124: if ($ferr) {
125: $env{'user.error.msg'}=$r->uri.':bre:0:0:Course not initialized';
126: $env{'user.reinit'} = 1;
127: return HTTP_NOT_ACCEPTABLE;
128: }
129: }
130: my $host = $r->headers_in->get('Host');
131: if (!$host) {
132: $r->internal_redirect($realuri);
133: return OK;
134: } else {
135: my $protocol = 'http';
136: if ($r->get_server_port == 443) {
137: $protocol = 'https';
1.1 raeburn 138: }
1.5 raeburn 139: my $location = $protocol.'://'.$host.$realuri;
140: $r->headers_out->set(Location => $location);
141: return REDIRECT;
1.1 raeburn 142: }
143: }
1.5 raeburn 144: } else {
145: my %crsenv = &Apache::lonnet::coursedescription("$cdom/$cnum");
146: my @possroles = ('in','ta','ep','st','cr','ad');
147: if ($crsenv{'type'} eq 'Community') {
148: unshift(@possroles,'co');
149: } else {
150: unshift(@possroles,'cc');
151: }
152: my %roleshash = &Apache::lonnet::get_my_roles($env{'user.uname'},
153: $env{'user.domain'},
154: 'userroles',undef,
155: \@possroles,[$cdom],1);
156: my (%possroles,$hassection);
157: if (keys(%roleshash)) {
158: foreach my $entry (keys(%roleshash)) {
159: if ($entry =~ /^\Q$cnum:$cdom:\E([^:]+):([^:]*)$/) {
160: $possroles{$1} = $2;
161: if ($2 ne '') {
162: $hassection = 1;
163: }
164: }
1.4 raeburn 165: }
166: }
1.5 raeburn 167: my @allposs = keys(%possroles);
168: if ($env{'request.lti.login'}) {
169: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
170: if ($env{'request.lti.target'} eq '') {
171: if ($env{'form.ltitarget'} eq 'iframe') {
172: &Apache::lonnet::appenv({'request.lti.target' => 'iframe'});
173: delete($env{'form.ltitarget'});
1.4 raeburn 174: }
1.5 raeburn 175: }
176: if ($env{'form.selectrole'}) {
177: foreach my $role (@allposs) {
178: my $newrole = "$role./$cdom/$cnum";
179: if ($possroles{$allposs[0]} ne '') {
180: $newrole .= "/$possroles{$role}";
181: }
182: if ($env{"form.$newrole"}) {
183: my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
184: '&destinationurl='.&HTML::Entities::encode($r->uri);
185: if ($env{'form.ltitarget'} eq 'iframe') {
186: $destination .= '<itarget=iframe';
187: }
188: &do_redirect($r,$destination);
189: return OK;
1.4 raeburn 190: }
191: }
192: }
193: }
1.5 raeburn 194: if (@allposs == 0) {
195: &show_roles($r,\%crsenv,\%possroles);
196: } elsif (@allposs == 1) {
197: my $newrole = "$allposs[0]./$cdom/$cnum";
198: $newrole = "$allposs[0]./$cdom/$cnum";
199: if ($possroles{$allposs[0]} ne '') {
200: $newrole .= "/$possroles{$allposs[0]}";
1.1 raeburn 201: }
202: my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
203: '&destinationurl='.&HTML::Entities::encode($r->uri);
204: &do_redirect($r,$destination);
1.5 raeburn 205: } elsif (keys(%possroles) > 1) {
206: if (grep(/^(cc|co)$/,@allposs)) {
207: my $newrole;
208: if (exists($possroles{'cc'})) {
209: $newrole = 'cc';
210: } else {
211: $newrole = 'co';
212: }
213: $newrole .= "./$cdom/$cnum";
214: my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
215: '&destinationurl='.&HTML::Entities::encode($r->uri);
216: &do_redirect($r,$destination);
217: } else {
218: my $hascustom;
219: if (grep(/^cr\//,@allposs)) {
220: $hascustom = 1;
221: }
222: &show_roles($r,\%crsenv,\%possroles,$hassection,$hascustom);
1.1 raeburn 223: }
224: }
1.5 raeburn 225: return OK;
1.1 raeburn 226: }
227: }
228: }
229: }
230: }
231: }
232: &generic_error($r);
233: return OK;
234: } else {
235: return FORBIDDEN;
236: }
237: }
238:
239: sub do_redirect {
240: my ($r,$destination) = @_;
1.2 raeburn 241: my $windowname = 'loncapaclient';
242: if ($env{'request.lti.login'}) {
243: $windowname .= 'lti';
244: }
1.1 raeburn 245: my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
246: my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
247: &Apache::loncommon::content_type($r,'text/html');
248: $r->send_http_header;
249: $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
1.2 raeburn 250: &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
1.1 raeburn 251: '<h1>'.&mt('Welcome').'</h1>'.
252: '<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>'.
253: '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
254: &Apache::loncommon::end_page());
255: return;
256: }
257:
258: sub show_roles {
259: my ($r,$crsenv,$possroles,$hassection,$hascustom) = @_;
260: &Apache::loncommon::content_type($r,'text/html');
261: $r->send_http_header;
262: my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
263: if (ref($crsenv) eq 'HASH') {
264: $crsdesc = $crsenv->{'description'};
265: $crstype = $crsenv->{'type'};
266: $cdom = $crsenv->{'domain'};
267: $cnum = $crsenv->{'num'};
268: }
269: if ($crstype eq '') {
270: $crstype = 'Course';
271: }
272: my $lc_crstype = lc($crstype);
273: if ($crsdesc ne '') {
274: $header = &mt("The page you requested belongs to the following $lc_crstype: [_1]",
275: '<i>'.$crsdesc.'</i>');
276: }
277: if (ref($possroles) eq 'HASH') {
278: if (keys(%{$possroles}) > 0) {
279: $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
280: $title = 'Choose a role'; #Do not localize.
281: if ($crstype eq 'Community') {
282: $preamble = &mt('You have the following active roles in this community:');
283: } else {
284: $preamble = &mt('You have the following active roles in this course:');
285: }
286: $datatable = '<form name="" action="/adm/roles">'.
287: '<input type="hidden" name="newrole" value="" />'.
288: '<input type="hidden" name="selectrole" value="1" />'.
289: '<input type="hidden" name="destinationurl" value="'.$r->uri.'" />'.
290: &Apache::loncommon::start_data_table().
291: &Apache::loncommon::start_data_table_header_row().
292: '<th></th><th>'.&mt('User role').'</th>';
293: if ($hassection) {
294: $datatable .= '<th>'.&mt('Section').'</th>';
295: }
296: if ($hascustom) {
297: $datatable .= '<th>'.&mt('Information').'</th>';
298: }
299: $datatable .= &Apache::loncommon::end_data_table_header_row();
300: my @available = sort(keys(%{$possroles}));
301: foreach my $role ('ad','in','ta','ep','st','cr') {
302: foreach my $key (@available) {
303: if ($key =~ m{^$role($|/)}) {
304: my $trolecode = "$key./$cdom/$cnum";
305: my $rolename = &Apache::lonnet::plaintext($key,$crstype,$cdom.'_'.$cnum);
306: my $sec = $possroles->{$key};
307: if ($sec ne '') {
308: $trolecode .= '/'.$sec;
309: }
310: my $buttonname=$trolecode;
311: $buttonname=~s/\W//g;
312: $datatable .= &Apache::loncommon::start_data_table_row().
313: '<td><input name="'.$buttonname.'" type="button" value="'.
314: &mt('Select').'" onclick="javascript:enterrole(this.form,'.
315: "'$trolecode','$buttonname'".');" /></td>';
316: if ($key =~ /^cr\//) {
317: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$key);
318: $datatable .= '<td><span class="LC_nobreak">'.$rolename.'</span></td>';
319: if ($hassection) {
320: $datatable .= '<td>'.$sec.'</td>';
321: }
322: $datatable.= '<td><span class="LC_fontsize_small LC_cusr_emph">'.
323: &mt('Custom role defined by [_1]',$rauthor.':'.$rdomain).
324: '</td>';
325: } else {
326: if ($hassection) {
327: $datatable .= '<td>'.$rolename.'</td>';
328: if ($hascustom) {
329: $datatable .= '<td colspan="2">'.$sec.'</td>';
330: } else {
331: $datatable .= '<td>'.$sec.'</td>';
332: }
333: } elsif ($hascustom) {
334: $datatable .= '<td colspan="2">'.$rolename.'</td>';
335: } else {
336: $datatable .= '<td>'.$rolename.'</td>';
337: }
338: }
339: $datatable .= &Apache::loncommon::end_data_table_row();
340: }
341: }
342: }
343: $datatable .= &Apache::loncommon::end_data_table().
344: '</form>';
345: my $standby = &mt('Role selected. Please stand by.');
346: $js = <<"ENDJS";
347: <script type="text/javascript">
348: // <![CDATA[
349:
350: active=true;
351:
352: function enterrole (thisform,rolecode,buttonname) {
353: if (active) {
354: active=false;
355: document.title='$standby';
356: window.status='$standby';
357: thisform.newrole.value=rolecode;
358: thisform.submit();
359: } else {
360: alert('$standby');
361: }
362: }
363:
364: // ]]>
365: </script>
366: ENDJS
367: } else {
368: $title = 'No active role';
369: $preamble = &mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.");
370: $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
371: }
372: }
373: &Apache::loncommon::content_type($r,'text/html');
374: $r->send_http_header;
375: $r->print(&Apache::loncommon::start_page($title,$js,$args).
376: '<h3>'.$header.'</h3>'.
377: '<div>'.$preamble.'</div>'.
378: $datatable.
379: &Apache::loncommon::end_page());
380: return;
381: }
382:
383: sub generic_error {
384: my ($r) = @_;
1.3 raeburn 385: my $continuelink;
386: unless ($env{'request.lti.login'}) {
387: my $linktext;
388: if ($env{'user.adv'}) {
389: $linktext = &mt('Continue to your roles page');
390: } else {
391: $linktext = &mt('Continue to your courses page');
392: }
393: $continuelink='<a href="/adm/roles">'.$linktext.'</a>';
1.1 raeburn 394: }
395: my $msg = &mt('The page you requested does not exist.');
396: &Apache::loncommon::content_type($r,'text/html');
397: $r->send_http_header;
398: my $args = {'bread_crumbs' => [{'href' => '','text' => 'Link status'},],};
399: $r->print(&Apache::loncommon::start_page('Invalid URL',undef,$args).
400: '<div class="LC_error">'.$msg.'</div>'.
401: '<p>'.$continuelink.'</p>'.
402: &Apache::loncommon::end_page());
403: return;
404: }
405:
406: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>