File:  [LON-CAPA] / loncom / interface / lontiny.pm
Revision 1.2: download - view: text, annotated - select for diffs
Sat Apr 14 02:29:44 2018 UTC (6 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LON-CAPA as LTI Provider
  - Original LTI launch of LON-CAPA is for a resource, a map, or an entire
    course, and display is for iframe, tab or window; retain this in %env.

    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.2 2018/04/14 02:29:44 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::lonlocal;
   39: use LONCAPA qw(:DEFAULT :match);
   40: 
   41: sub handler {
   42:     my $r = shift;
   43:     my %user;
   44:     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
   45:     if ($handle ne '') { 
   46:         if ($r->uri =~ m{^/tiny/($match_domain)/(\w+)$}) {
   47:             my ($cdom,$key) = ($1,$2);
   48:             if (&Apache::lonnet::domain($cdom) ne '') {
   49:                 my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
   50:                 my $tinyurl;
   51:                 my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
   52:                 if (defined($cached)) {
   53:                     $tinyurl = $result;
   54:                 } else {
   55:                     my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
   56:                     if ($currtiny{$key} ne '') {
   57:                         $tinyurl = $currtiny{$key};
   58:                         &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
   59:                     }
   60:                 }
   61:                 if ($tinyurl) {
   62:                     my ($cnum,$symb) = split(/\&/,$tinyurl);
   63:                     if ($cnum =~ /^$match_courseid$/) {
   64:                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
   65:                         if ($chome ne 'no_host') {
   66:                             my %crsenv = &Apache::lonnet::coursedescription("$cdom/$cnum");
   67:                             my @possroles = ('in','ta','ep','st','cr','ad');
   68:                             if ($crsenv{'type'} eq 'Community') {
   69:                                 unshift(@possroles,'co');
   70:                             } else {
   71:                                 unshift(@possroles,'cc');
   72:                             }
   73:                             my %roleshash = &Apache::lonnet::get_my_roles($env{'user.uname'},
   74:                                                                           $env{'user.domain'},
   75:                                                                           'userroles',undef,
   76:                                                                           \@possroles,[$cdom],1);
   77:                             my (%possroles,$hassection);
   78:                             if (keys(%roleshash)) {
   79:                                 foreach my $entry (keys(%roleshash)) {
   80:                                     if ($entry =~ /^\Q$cnum:$cdom:\E([^:]+):([^:]*)$/) {
   81:                                         $possroles{$1} = $2;
   82:                                         if ($2 ne '') {
   83:                                             $hassection = 1;
   84:                                         }
   85:                                     }
   86:                                 }
   87:                             }
   88:                             my @allposs = keys(%possroles);
   89:                             if (@allposs == 0) {
   90:                                 &show_roles($r,\%crsenv,\%possroles)
   91:                             } elsif (@allposs == 1) {
   92:                                 my $newrole = "$allposs[0]./$cdom/$cnum";
   93:                                 $newrole = "$allposs[0]./$cdom/$cnum";
   94:                                 if ($possroles{$allposs[0]} ne '') {
   95:                                     $newrole .= "/$possroles{$allposs[0]}"; 
   96:                                 }
   97:                                 my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
   98:                                                    '&destinationurl='.&HTML::Entities::encode($r->uri);
   99:                                 &do_redirect($r,$destination);
  100:                             } elsif (keys(%possroles) > 1) {
  101:                                 if (grep(/^(cc|co)$/,@allposs)) {
  102:                                     my $newrole;
  103:                                     if (exists($possroles{'cc'})) {
  104:                                         $newrole = 'cc';
  105:                                     } else {
  106:                                         $newrole = 'co';
  107:                                     }
  108:                                     $newrole .= "./$cdom/$cnum";
  109:                                     my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
  110:                                                        '&destinationurl='.&HTML::Entities::encode($r->uri);
  111:                                     &do_redirect($r,$destination);
  112:                                 } else {
  113:                                     my $hascustom;
  114:                                     if (grep(/^cr\//,@allposs)) {
  115:                                         $hascustom = 1;
  116:                                     }
  117:                                     &show_roles($r,\%crsenv,\%possroles,$hassection,$hascustom);
  118:                                 }
  119:                             }
  120:                             return OK;
  121:                         }
  122:                     }
  123:                 }
  124:             }
  125:         }
  126:         &generic_error($r);
  127:         return OK;
  128:     } else {
  129:         return FORBIDDEN;
  130:     }
  131: }
  132: 
  133: sub do_redirect {
  134:     my ($r,$destination) = @_;
  135:     my $windowname = 'loncapaclient';
  136:     if ($env{'request.lti.login'}) {
  137:         $windowname .= 'lti';
  138:     }
  139:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
  140:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
  141:     &Apache::loncommon::content_type($r,'text/html');
  142:     $r->send_http_header;
  143:     $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
  144:               &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
  145:               '<h1>'.&mt('Welcome').'</h1>'.
  146:               '<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>'.
  147:               '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
  148:               &Apache::loncommon::end_page());
  149:     return;
  150: }
  151: 
  152: sub show_roles {
  153:     my ($r,$crsenv,$possroles,$hassection,$hascustom) = @_;
  154:     &Apache::loncommon::content_type($r,'text/html');
  155:     $r->send_http_header;
  156:     my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
  157:     if (ref($crsenv) eq 'HASH') {
  158:         $crsdesc = $crsenv->{'description'};
  159:         $crstype = $crsenv->{'type'};
  160:         $cdom = $crsenv->{'domain'};
  161:         $cnum = $crsenv->{'num'};
  162:     }
  163:     if ($crstype eq '') {
  164:         $crstype = 'Course';
  165:     }
  166:     my $lc_crstype = lc($crstype);
  167:     if ($crsdesc ne '') {
  168:         $header = &mt("The page you requested belongs to the following $lc_crstype: [_1]",
  169:                       '<i>'.$crsdesc.'</i>');
  170:     }
  171:     if (ref($possroles) eq 'HASH') {
  172:         if (keys(%{$possroles}) > 0) {
  173:             $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
  174:             $title = 'Choose a role'; #Do not localize.
  175:             if ($crstype eq 'Community') {
  176:                 $preamble = &mt('You have the following active roles in this community:');
  177:             } else { 
  178:                 $preamble = &mt('You have the following active roles in this course:');
  179:             }
  180:             $datatable = '<form name="" action="/adm/roles">'.
  181:                          '<input type="hidden" name="newrole" value="" />'.
  182:                          '<input type="hidden" name="selectrole" value="1" />'.
  183:                          '<input type="hidden" name="destinationurl" value="'.$r->uri.'" />'.
  184:                          &Apache::loncommon::start_data_table().
  185:                          &Apache::loncommon::start_data_table_header_row().
  186:                          '<th></th><th>'.&mt('User role').'</th>';
  187:             if ($hassection) {
  188:                 $datatable .= '<th>'.&mt('Section').'</th>';
  189:             }
  190:             if ($hascustom) {
  191:                 $datatable .= '<th>'.&mt('Information').'</th>';
  192:             }
  193:             $datatable .= &Apache::loncommon::end_data_table_header_row();
  194:             my @available = sort(keys(%{$possroles}));
  195:             foreach my $role ('ad','in','ta','ep','st','cr') {
  196:                 foreach my $key (@available) {
  197:                     if ($key =~ m{^$role($|/)}) {
  198:                         my $trolecode = "$key./$cdom/$cnum";
  199:                         my $rolename = &Apache::lonnet::plaintext($key,$crstype,$cdom.'_'.$cnum);
  200:                         my $sec = $possroles->{$key};
  201:                         if ($sec ne '') {
  202:                             $trolecode .= '/'.$sec;
  203:                         }
  204:                         my $buttonname=$trolecode;
  205:                         $buttonname=~s/\W//g;
  206:                         $datatable .= &Apache::loncommon::start_data_table_row().
  207:                                       '<td><input name="'.$buttonname.'" type="button" value="'.
  208:                                       &mt('Select').'" onclick="javascript:enterrole(this.form,'.
  209:                                       "'$trolecode','$buttonname'".');" /></td>';
  210:                         if ($key =~ /^cr\//) {
  211:                             my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$key);
  212:                             $datatable .= '<td><span class="LC_nobreak">'.$rolename.'</span></td>';
  213:                             if ($hassection) {
  214:                                 $datatable .= '<td>'.$sec.'</td>';
  215:                             }
  216:                             $datatable.= '<td><span class="LC_fontsize_small LC_cusr_emph">'.
  217:                                           &mt('Custom role defined by [_1]',$rauthor.':'.$rdomain).
  218:                                           '</td>';
  219:                         } else {
  220:                             if ($hassection) {
  221:                                 $datatable .= '<td>'.$rolename.'</td>';
  222:                                 if ($hascustom) {
  223:                                     $datatable .= '<td colspan="2">'.$sec.'</td>';
  224:                                 } else {
  225:                                     $datatable .= '<td>'.$sec.'</td>';
  226:                                 }
  227:                             } elsif ($hascustom) {
  228:                                 $datatable .= '<td colspan="2">'.$rolename.'</td>';
  229:                             } else {
  230:                                 $datatable .= '<td>'.$rolename.'</td>';
  231:                             }
  232:                         }
  233:                         $datatable .= &Apache::loncommon::end_data_table_row();
  234:                     }
  235:                 }
  236:             }
  237:             $datatable .= &Apache::loncommon::end_data_table().
  238:                           '</form>';
  239:             my $standby = &mt('Role selected. Please stand by.');
  240:             $js = <<"ENDJS";
  241: <script type="text/javascript">
  242: // <![CDATA[
  243: 
  244: active=true;
  245: 
  246: function enterrole (thisform,rolecode,buttonname) {
  247:     if (active) {
  248:         active=false;
  249:         document.title='$standby';
  250:         window.status='$standby';
  251:         thisform.newrole.value=rolecode;
  252:         thisform.submit();
  253:     } else {
  254:        alert('$standby');
  255:     }
  256: }
  257: 
  258: // ]]>
  259: </script>
  260: ENDJS
  261:         } else {
  262:             $title = 'No active role';
  263:             $preamble = &mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.");
  264:             $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
  265:         }
  266:     }
  267:     &Apache::loncommon::content_type($r,'text/html');
  268:     $r->send_http_header;
  269:     $r->print(&Apache::loncommon::start_page($title,$js,$args).
  270:               '<h3>'.$header.'</h3>'.
  271:               '<div>'.$preamble.'</div>'.
  272:               $datatable.
  273:               &Apache::loncommon::end_page());
  274:     return;
  275: }
  276: 
  277: sub generic_error {
  278:     my ($r) = @_;
  279:     my $linktext;
  280:     if ($env{'user.adv'}) {
  281:         $linktext = &mt('Continue to your roles page');
  282:     } else {
  283:         $linktext = &mt('Continue to your courses page');
  284:     }
  285:     my $continuelink='<a href="/adm/roles">'.$linktext.'</a>';
  286:     my $msg = &mt('The page you requested does not exist.');
  287:     &Apache::loncommon::content_type($r,'text/html');
  288:     $r->send_http_header;
  289:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Link status'},],};
  290:     $r->print(&Apache::loncommon::start_page('Invalid URL',undef,$args).
  291:               '<div class="LC_error">'.$msg.'</div>'.
  292:               '<p>'.$continuelink.'</p>'.
  293:               &Apache::loncommon::end_page());
  294:     return;
  295: }
  296: 
  297: 1;

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