Annotation of loncom/interface/lontiny.pm, revision 1.4

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.4     ! raeburn     5: # $Id: lontiny.pm,v 1.3 2018/05/10 03:49:51 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;
                     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);
1.4     ! raeburn    89:                             if ($env{'request.lti.login'}) {
        !            90:                                 &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
        !            91:                                 if ($env{'request.lti.target'} eq '') {
        !            92:                                     if ($env{'form.ltitarget'} eq 'iframe') {
        !            93:                                         &Apache::lonnet::appenv({'request.lti.target' => 'iframe'});
        !            94:                                         delete($env{'form.ltitarget'});
        !            95:                                     }
        !            96:                                 }
        !            97:                                 if ($env{'form.selectrole'}) {
        !            98:                                     foreach my $role (@allposs) {
        !            99:                                         my $newrole = "$role./$cdom/$cnum";
        !           100:                                         if ($possroles{$allposs[0]} ne '') {
        !           101:                                             $newrole .= "/$possroles{$role}";
        !           102:                                         }
        !           103:                                         if ($env{"form.$newrole"}) {
        !           104:                                             my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
        !           105:                                                                '&destinationurl='.&HTML::Entities::encode($r->uri);
        !           106:                                             if ($env{'form.ltitarget'} eq 'iframe') {
        !           107:                                                 $destination .= '&ltitarget=iframe';
        !           108:                                             }
        !           109:                                             &do_redirect($r,$destination);
        !           110:                                             return OK;
        !           111:                                         }
        !           112:                                     }
        !           113:                                 }
        !           114:                             }
1.1       raeburn   115:                             if (@allposs == 0) {
1.4     ! raeburn   116:                                 &show_roles($r,\%crsenv,\%possroles);
1.1       raeburn   117:                             } elsif (@allposs == 1) {
                    118:                                 my $newrole = "$allposs[0]./$cdom/$cnum";
                    119:                                 $newrole = "$allposs[0]./$cdom/$cnum";
                    120:                                 if ($possroles{$allposs[0]} ne '') {
                    121:                                     $newrole .= "/$possroles{$allposs[0]}"; 
                    122:                                 }
                    123:                                 my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
                    124:                                                    '&destinationurl='.&HTML::Entities::encode($r->uri);
                    125:                                 &do_redirect($r,$destination);
                    126:                             } elsif (keys(%possroles) > 1) {
                    127:                                 if (grep(/^(cc|co)$/,@allposs)) {
                    128:                                     my $newrole;
                    129:                                     if (exists($possroles{'cc'})) {
                    130:                                         $newrole = 'cc';
                    131:                                     } else {
                    132:                                         $newrole = 'co';
                    133:                                     }
                    134:                                     $newrole .= "./$cdom/$cnum";
                    135:                                     my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
                    136:                                                        '&destinationurl='.&HTML::Entities::encode($r->uri);
                    137:                                     &do_redirect($r,$destination);
                    138:                                 } else {
                    139:                                     my $hascustom;
                    140:                                     if (grep(/^cr\//,@allposs)) {
                    141:                                         $hascustom = 1;
                    142:                                     }
                    143:                                     &show_roles($r,\%crsenv,\%possroles,$hassection,$hascustom);
                    144:                                 }
                    145:                             }
                    146:                             return OK;
                    147:                         }
                    148:                     }
                    149:                 }
                    150:             }
                    151:         }
                    152:         &generic_error($r);
                    153:         return OK;
                    154:     } else {
                    155:         return FORBIDDEN;
                    156:     }
                    157: }
                    158: 
                    159: sub do_redirect {
                    160:     my ($r,$destination) = @_;
1.2       raeburn   161:     my $windowname = 'loncapaclient';
                    162:     if ($env{'request.lti.login'}) {
                    163:         $windowname .= 'lti';
                    164:     }
1.1       raeburn   165:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
                    166:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
                    167:     &Apache::loncommon::content_type($r,'text/html');
                    168:     $r->send_http_header;
                    169:     $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
1.2       raeburn   170:               &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
1.1       raeburn   171:               '<h1>'.&mt('Welcome').'</h1>'.
                    172:               '<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>'.
                    173:               '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
                    174:               &Apache::loncommon::end_page());
                    175:     return;
                    176: }
                    177: 
                    178: sub show_roles {
                    179:     my ($r,$crsenv,$possroles,$hassection,$hascustom) = @_;
                    180:     &Apache::loncommon::content_type($r,'text/html');
                    181:     $r->send_http_header;
                    182:     my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
                    183:     if (ref($crsenv) eq 'HASH') {
                    184:         $crsdesc = $crsenv->{'description'};
                    185:         $crstype = $crsenv->{'type'};
                    186:         $cdom = $crsenv->{'domain'};
                    187:         $cnum = $crsenv->{'num'};
                    188:     }
                    189:     if ($crstype eq '') {
                    190:         $crstype = 'Course';
                    191:     }
                    192:     my $lc_crstype = lc($crstype);
                    193:     if ($crsdesc ne '') {
                    194:         $header = &mt("The page you requested belongs to the following $lc_crstype: [_1]",
                    195:                       '<i>'.$crsdesc.'</i>');
                    196:     }
                    197:     if (ref($possroles) eq 'HASH') {
                    198:         if (keys(%{$possroles}) > 0) {
                    199:             $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
                    200:             $title = 'Choose a role'; #Do not localize.
                    201:             if ($crstype eq 'Community') {
                    202:                 $preamble = &mt('You have the following active roles in this community:');
                    203:             } else { 
                    204:                 $preamble = &mt('You have the following active roles in this course:');
                    205:             }
                    206:             $datatable = '<form name="" action="/adm/roles">'.
                    207:                          '<input type="hidden" name="newrole" value="" />'.
                    208:                          '<input type="hidden" name="selectrole" value="1" />'.
                    209:                          '<input type="hidden" name="destinationurl" value="'.$r->uri.'" />'.
                    210:                          &Apache::loncommon::start_data_table().
                    211:                          &Apache::loncommon::start_data_table_header_row().
                    212:                          '<th></th><th>'.&mt('User role').'</th>';
                    213:             if ($hassection) {
                    214:                 $datatable .= '<th>'.&mt('Section').'</th>';
                    215:             }
                    216:             if ($hascustom) {
                    217:                 $datatable .= '<th>'.&mt('Information').'</th>';
                    218:             }
                    219:             $datatable .= &Apache::loncommon::end_data_table_header_row();
                    220:             my @available = sort(keys(%{$possroles}));
                    221:             foreach my $role ('ad','in','ta','ep','st','cr') {
                    222:                 foreach my $key (@available) {
                    223:                     if ($key =~ m{^$role($|/)}) {
                    224:                         my $trolecode = "$key./$cdom/$cnum";
                    225:                         my $rolename = &Apache::lonnet::plaintext($key,$crstype,$cdom.'_'.$cnum);
                    226:                         my $sec = $possroles->{$key};
                    227:                         if ($sec ne '') {
                    228:                             $trolecode .= '/'.$sec;
                    229:                         }
                    230:                         my $buttonname=$trolecode;
                    231:                         $buttonname=~s/\W//g;
                    232:                         $datatable .= &Apache::loncommon::start_data_table_row().
                    233:                                       '<td><input name="'.$buttonname.'" type="button" value="'.
                    234:                                       &mt('Select').'" onclick="javascript:enterrole(this.form,'.
                    235:                                       "'$trolecode','$buttonname'".');" /></td>';
                    236:                         if ($key =~ /^cr\//) {
                    237:                             my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$key);
                    238:                             $datatable .= '<td><span class="LC_nobreak">'.$rolename.'</span></td>';
                    239:                             if ($hassection) {
                    240:                                 $datatable .= '<td>'.$sec.'</td>';
                    241:                             }
                    242:                             $datatable.= '<td><span class="LC_fontsize_small LC_cusr_emph">'.
                    243:                                           &mt('Custom role defined by [_1]',$rauthor.':'.$rdomain).
                    244:                                           '</td>';
                    245:                         } else {
                    246:                             if ($hassection) {
                    247:                                 $datatable .= '<td>'.$rolename.'</td>';
                    248:                                 if ($hascustom) {
                    249:                                     $datatable .= '<td colspan="2">'.$sec.'</td>';
                    250:                                 } else {
                    251:                                     $datatable .= '<td>'.$sec.'</td>';
                    252:                                 }
                    253:                             } elsif ($hascustom) {
                    254:                                 $datatable .= '<td colspan="2">'.$rolename.'</td>';
                    255:                             } else {
                    256:                                 $datatable .= '<td>'.$rolename.'</td>';
                    257:                             }
                    258:                         }
                    259:                         $datatable .= &Apache::loncommon::end_data_table_row();
                    260:                     }
                    261:                 }
                    262:             }
                    263:             $datatable .= &Apache::loncommon::end_data_table().
                    264:                           '</form>';
                    265:             my $standby = &mt('Role selected. Please stand by.');
                    266:             $js = <<"ENDJS";
                    267: <script type="text/javascript">
                    268: // <![CDATA[
                    269: 
                    270: active=true;
                    271: 
                    272: function enterrole (thisform,rolecode,buttonname) {
                    273:     if (active) {
                    274:         active=false;
                    275:         document.title='$standby';
                    276:         window.status='$standby';
                    277:         thisform.newrole.value=rolecode;
                    278:         thisform.submit();
                    279:     } else {
                    280:        alert('$standby');
                    281:     }
                    282: }
                    283: 
                    284: // ]]>
                    285: </script>
                    286: ENDJS
                    287:         } else {
                    288:             $title = 'No active role';
                    289:             $preamble = &mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.");
                    290:             $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
                    291:         }
                    292:     }
                    293:     &Apache::loncommon::content_type($r,'text/html');
                    294:     $r->send_http_header;
                    295:     $r->print(&Apache::loncommon::start_page($title,$js,$args).
                    296:               '<h3>'.$header.'</h3>'.
                    297:               '<div>'.$preamble.'</div>'.
                    298:               $datatable.
                    299:               &Apache::loncommon::end_page());
                    300:     return;
                    301: }
                    302: 
                    303: sub generic_error {
                    304:     my ($r) = @_;
1.3       raeburn   305:     my $continuelink;
                    306:     unless ($env{'request.lti.login'}) {
                    307:         my $linktext;
                    308:         if ($env{'user.adv'}) {
                    309:             $linktext = &mt('Continue to your roles page');
                    310:         } else {
                    311:             $linktext = &mt('Continue to your courses page');
                    312:         }
                    313:         $continuelink='<a href="/adm/roles">'.$linktext.'</a>';
1.1       raeburn   314:     }
                    315:     my $msg = &mt('The page you requested does not exist.');
                    316:     &Apache::loncommon::content_type($r,'text/html');
                    317:     $r->send_http_header;
                    318:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Link status'},],};
                    319:     $r->print(&Apache::loncommon::start_page('Invalid URL',undef,$args).
                    320:               '<div class="LC_error">'.$msg.'</div>'.
                    321:               '<p>'.$continuelink.'</p>'.
                    322:               &Apache::loncommon::end_page());
                    323:     return;
                    324: }
                    325: 
                    326: 1;

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