File:  [LON-CAPA] / loncom / interface / lontiny.pm
Revision 1.6.2.1: download - view: text, annotated - select for diffs
Thu Feb 7 14:56:05 2019 UTC (5 years, 2 months ago) by raeburn
Branches: version_2_11_2_msu
CVS tags: version_2_11_3_msu
- For 2.11.2 (modified)
  LTI-specific code not needed.

    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: #
    5: # $Id: lontiny.pm,v 1.6.2.1 2019/02/07 14:56:05 raeburn Exp $
    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;
   38: use Apache::lonuserstate;
   39: use Apache::lonnavmaps;
   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);
   47:     if ($handle ne '') {
   48:         my $lonidsdir=$r->dir_config('lonIDsDir');
   49:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   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') {
   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');
  100:                                     my ($result,@reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
  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';
  138:                                         }
  139:                                         my $location = $protocol.'://'.$host.$realuri;
  140:                                         $r->headers_out->set(Location => $location);
  141:                                         return REDIRECT;
  142:                                     }
  143:                                 }
  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:                                         }
  165:                                     }
  166:                                 }
  167:                                 my @allposs = keys(%possroles);
  168:                                 if (@allposs == 0) {
  169:                                     &show_roles($r,\%crsenv,\%possroles);
  170:                                 } elsif (@allposs == 1) {
  171:                                     my $newrole = "$allposs[0]./$cdom/$cnum";
  172:                                     $newrole = "$allposs[0]./$cdom/$cnum";
  173:                                     if ($possroles{$allposs[0]} ne '') {
  174:                                         $newrole .= "/$possroles{$allposs[0]}"; 
  175:                                     }
  176:                                     my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
  177:                                                        '&destinationurl='.&HTML::Entities::encode($r->uri);
  178:                                     &do_redirect($r,$destination);
  179:                                 } elsif (keys(%possroles) > 1) {
  180:                                     if (grep(/^(cc|co)$/,@allposs)) {
  181:                                         my $newrole;
  182:                                         if (exists($possroles{'cc'})) {
  183:                                             $newrole = 'cc';
  184:                                         } else {
  185:                                             $newrole = 'co';
  186:                                         }
  187:                                         $newrole .= "./$cdom/$cnum";
  188:                                         my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
  189:                                                            '&destinationurl='.&HTML::Entities::encode($r->uri);
  190:                                         &do_redirect($r,$destination);
  191:                                     } else {
  192:                                         my $hascustom;
  193:                                         if (grep(/^cr\//,@allposs)) {
  194:                                             $hascustom = 1;
  195:                                         }
  196:                                         &show_roles($r,\%crsenv,\%possroles,$hassection,$hascustom);
  197:                                     }
  198:                                 }
  199:                                 return OK;
  200:                             }
  201:                         }
  202:                     }
  203:                 }
  204:             }
  205:         }
  206:         &generic_error($r);
  207:         return OK;
  208:     } else {
  209:         return FORBIDDEN;
  210:     }
  211: }
  212: 
  213: sub do_redirect {
  214:     my ($r,$destination) = @_;
  215:     my $windowname = 'loncapaclient';
  216:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
  217:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
  218:     &Apache::loncommon::content_type($r,'text/html');
  219:     $r->send_http_header;
  220:     $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
  221:               &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
  222:               '<h1>'.&mt('Welcome').'</h1>'.
  223:               '<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>'.
  224:               '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
  225:               &Apache::loncommon::end_page());
  226:     return;
  227: }
  228: 
  229: sub show_roles {
  230:     my ($r,$crsenv,$possroles,$hassection,$hascustom) = @_;
  231:     &Apache::loncommon::content_type($r,'text/html');
  232:     $r->send_http_header;
  233:     my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
  234:     if (ref($crsenv) eq 'HASH') {
  235:         $crsdesc = $crsenv->{'description'};
  236:         $crstype = $crsenv->{'type'};
  237:         $cdom = $crsenv->{'domain'};
  238:         $cnum = $crsenv->{'num'};
  239:     }
  240:     if ($crstype eq '') {
  241:         $crstype = 'Course';
  242:     }
  243:     my $lc_crstype = lc($crstype);
  244:     if ($crsdesc ne '') {
  245:         $header = &mt("The page you requested belongs to the following $lc_crstype: [_1]",
  246:                       '<i>'.$crsdesc.'</i>');
  247:     }
  248:     if (ref($possroles) eq 'HASH') {
  249:         if (keys(%{$possroles}) > 0) {
  250:             $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
  251:             $title = 'Choose a role'; #Do not localize.
  252:             if ($crstype eq 'Community') {
  253:                 $preamble = &mt('You have the following active roles in this community:');
  254:             } else { 
  255:                 $preamble = &mt('You have the following active roles in this course:');
  256:             }
  257:             $datatable = '<form name="" action="/adm/roles">'.
  258:                          '<input type="hidden" name="newrole" value="" />'.
  259:                          '<input type="hidden" name="selectrole" value="1" />'.
  260:                          '<input type="hidden" name="destinationurl" value="'.$r->uri.'" />'.
  261:                          &Apache::loncommon::start_data_table().
  262:                          &Apache::loncommon::start_data_table_header_row().
  263:                          '<th></th><th>'.&mt('User role').'</th>';
  264:             if ($hassection) {
  265:                 $datatable .= '<th>'.&mt('Section').'</th>';
  266:             }
  267:             if ($hascustom) {
  268:                 $datatable .= '<th>'.&mt('Information').'</th>';
  269:             }
  270:             $datatable .= &Apache::loncommon::end_data_table_header_row();
  271:             my @available = sort(keys(%{$possroles}));
  272:             foreach my $role ('ad','in','ta','ep','st','cr') {
  273:                 foreach my $key (@available) {
  274:                     if ($key =~ m{^$role($|/)}) {
  275:                         my $trolecode = "$key./$cdom/$cnum";
  276:                         my $rolename = &Apache::lonnet::plaintext($key,$crstype,$cdom.'_'.$cnum);
  277:                         my $sec = $possroles->{$key};
  278:                         if ($sec ne '') {
  279:                             $trolecode .= '/'.$sec;
  280:                         }
  281:                         my $buttonname=$trolecode;
  282:                         $buttonname=~s/\W//g;
  283:                         $datatable .= &Apache::loncommon::start_data_table_row().
  284:                                       '<td><input name="'.$buttonname.'" type="button" value="'.
  285:                                       &mt('Select').'" onclick="javascript:enterrole(this.form,'.
  286:                                       "'$trolecode','$buttonname'".');" /></td>';
  287:                         if ($key =~ /^cr\//) {
  288:                             my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$key);
  289:                             $datatable .= '<td><span class="LC_nobreak">'.$rolename.'</span></td>';
  290:                             if ($hassection) {
  291:                                 $datatable .= '<td>'.$sec.'</td>';
  292:                             }
  293:                             $datatable.= '<td><span class="LC_fontsize_small LC_cusr_emph">'.
  294:                                           &mt('Custom role defined by [_1]',$rauthor.':'.$rdomain).
  295:                                           '</td>';
  296:                         } else {
  297:                             if ($hassection) {
  298:                                 $datatable .= '<td>'.$rolename.'</td>';
  299:                                 if ($hascustom) {
  300:                                     $datatable .= '<td colspan="2">'.$sec.'</td>';
  301:                                 } else {
  302:                                     $datatable .= '<td>'.$sec.'</td>';
  303:                                 }
  304:                             } elsif ($hascustom) {
  305:                                 $datatable .= '<td colspan="2">'.$rolename.'</td>';
  306:                             } else {
  307:                                 $datatable .= '<td>'.$rolename.'</td>';
  308:                             }
  309:                         }
  310:                         $datatable .= &Apache::loncommon::end_data_table_row();
  311:                     }
  312:                 }
  313:             }
  314:             $datatable .= &Apache::loncommon::end_data_table().
  315:                           '</form>';
  316:             my $standby = &mt('Role selected. Please stand by.');
  317:             $js = <<"ENDJS";
  318: <script type="text/javascript">
  319: // <![CDATA[
  320: 
  321: active=true;
  322: 
  323: function enterrole (thisform,rolecode,buttonname) {
  324:     if (active) {
  325:         active=false;
  326:         document.title='$standby';
  327:         window.status='$standby';
  328:         thisform.newrole.value=rolecode;
  329:         thisform.submit();
  330:     } else {
  331:        alert('$standby');
  332:     }
  333: }
  334: 
  335: // ]]>
  336: </script>
  337: ENDJS
  338:         } else {
  339:             $title = 'No active role';
  340:             $preamble = &mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.");
  341:             $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
  342:         }
  343:     }
  344:     &Apache::loncommon::content_type($r,'text/html');
  345:     $r->send_http_header;
  346:     $r->print(&Apache::loncommon::start_page($title,$js,$args).
  347:               '<h3>'.$header.'</h3>'.
  348:               '<div>'.$preamble.'</div>'.
  349:               $datatable.
  350:               &Apache::loncommon::end_page());
  351:     return;
  352: }
  353: 
  354: sub generic_error {
  355:     my ($r) = @_;
  356:     my $continuelink;
  357:     my $linktext;
  358:     if ($env{'user.adv'}) {
  359:         $linktext = &mt('Continue to your roles page');
  360:     } else {
  361:         $linktext = &mt('Continue to your courses page');
  362:     }
  363:     $continuelink='<a href="/adm/roles">'.$linktext.'</a>';
  364:     my $msg = &mt('The page you requested does not exist.');
  365:     &Apache::loncommon::content_type($r,'text/html');
  366:     $r->send_http_header;
  367:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Link status'},],};
  368:     $r->print(&Apache::loncommon::start_page('Invalid URL',undef,$args).
  369:               '<div class="LC_error">'.$msg.'</div>'.
  370:               '<p>'.$continuelink.'</p>'.
  371:               &Apache::loncommon::end_page());
  372:     return;
  373: }
  374: 
  375: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>