File:  [LON-CAPA] / loncom / interface / lonextresedit.pm
Revision 1.15: download - view: text, annotated - select for diffs
Wed Mar 8 02:51:08 2017 UTC (7 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- For servers using Apache/SSL where an External Resource points at an
  http:// URL, links to display the page use http:// to avoid mixed active
  content issue, unless editing the resource (when https:// is used).

    1: # The LearningOnline Network
    2: # Documents
    3: #
    4: # $Id: lonextresedit.pm,v 1.15 2017/03/08 02:51:08 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonextresedit;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :http);
   33: use HTML::Entities;
   34: use Apache::lonlocal;
   35: use Apache::lonnet;
   36: use Apache::loncommon;
   37: use Apache::lonhtmlcommon;
   38: use Apache::lonuserstate;
   39: use LONCAPA::map();
   40: use LONCAPA qw(:DEFAULT :match);
   41: 
   42: sub handler {
   43:     my $r=shift;
   44:     &Apache::loncommon::content_type($r,'text/html');
   45:     $r->send_http_header;
   46: 
   47:     return OK if $r->header_only;
   48: 
   49:     # Check for access
   50:     if (! &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
   51:         $env{'user.error.msg'}=
   52:             $r->uri.":mdc:0:0:Cannot modify course content.";
   53:             return HTTP_NOT_ACCEPTABLE;
   54:     }
   55: 
   56:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   57:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   58:     my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
   59:     my ($supplementalflag,$updated,$output,$errormsg,$residx,$url,$title,
   60:         $symb,$type);
   61:     if (($env{'form.folderpath'} =~ /^supplemental/) && ($env{'form.suppurl'})) {
   62:         $supplementalflag = 1;
   63:         if (&unescape($env{'form.suppurl'}) =~ m{^/adm/$cdom/$cnum/\d+/exttools?$}) {
   64:             $type = 'tool';
   65:         }
   66:     }
   67:     if (($supplementalflag) || ($env{'form.symb'} =~ /^uploaded/)) {
   68:         ($updated,$output,$errormsg,$residx,$url,$title,$symb) =
   69:             &process_changes($supplementalflag,$cdom,$cnum,$chome);
   70:         if ($supplementalflag) {
   71:             if ($url ne &unescape($env{'form.suppurl'})) {
   72:                  $env{'form.suppurl'} = $url;
   73:             }
   74:             if ($title ne $env{'form.title'}) {
   75:                 $env{'form.title'} = $title;
   76:             }
   77:             $env{'form.idx'} = $residx;
   78:         } else {
   79:             if ($symb ne $env{'form.symb'}) {
   80:                 $env{'form.symb'} = $symb;
   81:             }
   82:             if ($url =~ m{/adm/$cdom/$cnum/\d+/exttools?$}) {
   83:                 $type = 'tool';
   84:             }
   85:         }
   86:     } else {
   87:         $errormsg = &mt('Information about external resource to edit is missing.');
   88:     }
   89:     if ($updated) {
   90:         my $msg = &mt('External Resource updated');
   91:         if ($type eq 'tool') {
   92:             $msg = &mt('External Tool updated');
   93:         }
   94:         $output = &Apache::lonhtmlcommon::confirm_success($msg);
   95:     }
   96:     if ($errormsg) {
   97:         $errormsg = '<p class="LC_error">'.$errormsg.'</p>';
   98:     }
   99:     my %ltitools;
  100:     if ($type eq 'tool') {
  101:         %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
  102:     }
  103:     my $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
  104:     my $pathitem = '<input type="hidden" name="folderpath" value="'.
  105:                    &HTML::Entities::encode($env{'form.folderpath'},'<>&"').'" />';
  106:     my $description = 'External Resource Editor';
  107:     if ($type eq 'tool') {
  108:         $description = 'External Tool Editor'; 
  109:     }
  110:     $r->print(&Apache::loncommon::start_page($description,$js).
  111:               '<div class="LC_left_float">'.
  112:               $output.
  113:               $errormsg.
  114:               &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,
  115:                            'direct',$env{'form.symb'},$type,$cdom,$cnum,\%ltitools).
  116:               '</div>'.&Apache::loncommon::end_page());
  117:     return OK;
  118: }
  119: 
  120: sub process_changes {
  121:     my ($supplementalflag,$cdom,$cnum,$chome) = @_;
  122:     my ($folder,$container,$output,$errormsg,$updated,$symb,$oldidx,$oldurl,$type,
  123:         $oldtitle,$newidx,$newurl,$newtitle,$residx,$url,$title,$marker,$args);
  124:     if ($env{'form.symb'}) {
  125:         $symb = $env{'form.symb'};
  126:         (my $map,$oldidx,$oldurl)=&Apache::lonnet::decode_symb($symb);
  127:         if ($map =~ m{^uploaded/$cdom/$cnum/(default(_\d+|))\.(sequence|page)$}) {
  128:             $folder = $1;
  129:             $container = $3;
  130:         }
  131:         $oldtitle = &Apache::lonnet::gettitle($env{'form.symb'});
  132:         if ($oldurl =~ m{^ext/(.+)$}) {
  133:             my $external = $1;
  134:             if ($external =~ m{^https://}) {
  135:                 $oldurl = $external;
  136:             } else {
  137:                 $oldurl = 'http://'.$oldurl;
  138:             }
  139:             $type = 'ext';
  140:         } else {
  141:             $type = 'tool';
  142:         }
  143:     } elsif ($env{'form.folderpath'}) {
  144:         $folder = &unescape( (split('&',$env{'form.folderpath'}))[-2] );
  145:         $oldurl = &unescape($env{'form.suppurl'});
  146:         $oldtitle = &unescape($env{'form.title'});
  147:         $container = 'sequence';
  148:         $supplementalflag = 1;
  149:         if ($oldurl =~ m{^/adm/$cdom/$cnum/\d+/exttools?$}) {
  150:             $type = 'tool';
  151:         } else {
  152:             $type = 'ext';
  153:         }
  154:     }
  155:     $url = $oldurl;
  156:     $title = $oldtitle;
  157:     if ($env{'form.importdetail'}) {
  158:         ($newtitle,$newurl,$newidx) =
  159:             map {&unescape($_)} split(/\=/,$env{'form.importdetail'});
  160:         if ($newurl =~ m{^(/adm/$cdom/$cnum/(\d+)/exttools?)\:?(.*)$}) {
  161:             $newurl = $1;
  162:             $marker = $2;
  163:             $args = $3;
  164:         }
  165:     }
  166:     if ($supplementalflag) {
  167:         $residx = $newidx;
  168:     } else {
  169:         $residx = $oldidx;
  170:     }
  171:     if ($folder && $container) {
  172:         if ($env{'form.importdetail'}) {
  173:             my ($errtext,$fatal,$mismatchedid,@imports);
  174:             if (!$supplementalflag) {
  175:                 if (($oldidx) && ($oldidx != $newidx)) {
  176:                     $mismatchedid = 1;
  177:                 }
  178:             }
  179:             if ($mismatchedid) {
  180:                 $errormsg = 'Wrong item identifier';
  181:             } elsif (($newtitle eq $oldtitle) && ($newurl eq $oldurl)) {
  182:                 if ($type eq 'tool') {
  183:                     if ($args) {
  184:                         ($updated,$errormsg) = &update_exttool($marker,$cdom,$cnum,$args);
  185:                         unless ($updated) {
  186:                             $output = &mt('No change');      
  187:                         }
  188:                     } else {
  189:                         $output = &mt('No change');
  190:                     }
  191:                 } else {
  192:                     $output = &mt('No change');
  193:                 }
  194:             } else {
  195:                 my $map = "/uploaded/$cdom/$cnum/$folder.$container";
  196:                 my ($errtext,$fatal) = &LONCAPA::map::mapread($map);
  197:                 if ($fatal) {
  198:                     $errormsg = &mt('Update failed: [_1].',$errtext);
  199:                 } else {
  200:                     my $saveurl = &LONCAPA::map::qtunescape($newurl);
  201:                     my $savetitle = &LONCAPA::map::qtunescape($newtitle);
  202:                     my $ext = 'true';
  203:                     if ($type eq 'tool') {
  204:                         if ($args) {
  205:                             ($updated,$errormsg) = &update_exttool($marker,$cdom,$cnum,$args);
  206:                         }
  207:                         $ext = 'false';
  208:                     }
  209:                     $LONCAPA::map::resources[$residx] =
  210:                         join(':', ($savetitle,$saveurl,$ext,'normal','res'));
  211:                     my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1);
  212:                     if ($errtext) {
  213:                         $errormsg = &mt('Update failed: [_1].',$errtext);
  214:                     } else {
  215:                         $updated = 1;
  216:                         $title = $newtitle;
  217:                         if ($newurl ne $oldurl) {
  218:                             $url = $newurl;
  219:                             if ($ext eq 'true') {
  220:                                 $newurl =~ s{^http://}{};
  221:                                 $newurl = "ext/$newurl";
  222:                             }
  223:                         }
  224:                         if (!$supplementalflag) {
  225:                             if ($newurl ne $oldurl) {
  226:                                 $symb = &Apache::lonnet::encode_symb($map,$residx,$newurl);
  227:                             } else {
  228:                                 $symb = $env{'form.symb'};
  229:                                 if ($symb) {
  230:                                     &Apache::lonnet::devalidate_title_cache($symb);
  231:                                 }
  232:                             }
  233:                         }
  234:                         my ($furl,$ferr) = 
  235:                             &Apache::lonuserstate::readmap("$cdom/$cnum");
  236:                         if ($ferr) {
  237:                             $errormsg = &mt('Reload failed: [_1].',$ferr);
  238:                         } else {
  239:                             unless ($supplementalflag) {
  240:                                 &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,$cdom.'_'.$cnum);
  241:                             }
  242:                         }
  243:                     }
  244:                 }
  245:             }
  246:         } else {
  247:             $output = &mt('No change');
  248:         }
  249:     } else {
  250:         if ($type eq 'tool') {
  251:             $errormsg = &mt('Information about current external tool is incomplete.');
  252:         } else {
  253:             $errormsg = &mt('Information about current external resource is incomplete.');
  254:         }
  255:     }
  256:     return ($updated,$output,$errormsg,$residx,$url,$title,$symb);
  257: }
  258: 
  259: sub update_exttool {
  260:     my ($marker,$cdom,$cnum,$args) = @_;
  261:     my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
  262:     my (%newhash,$changed,@deleted,$errormsg);
  263:     ($newhash{'target'},$newhash{'width'},$newhash{'height'},$newhash{'crslabel'},$newhash{'crstitle'}) = split(/:/,$args);
  264:     $newhash{'crslabel'} = &unescape($newhash{'crslabel'});
  265:     $newhash{'crstitle'} = &unescape($newhash{'crstitle'});
  266:     my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
  267:     foreach my $item ('target','width','height','crslabel','crstitle') {
  268:         $newhash{$item} =~ s/^\s+//;
  269:         $newhash{$item} =~ s/\s+$//;
  270:         if (($item eq 'width') || ($item eq 'height')) {
  271:             if ($newhash{'target'} eq 'iframe') {
  272:                 $newhash{$item} = '';
  273:             }
  274:         }
  275:         if ($toolhash{$item} ne $newhash{$item}) {
  276:             if ($newhash{$item} eq '') {
  277:                 unless (($item eq 'target') ||
  278:                         ((($item eq 'width') || ($item eq 'height')) &&
  279:                          (($newhash{'target'} eq 'window') || 
  280:                           (($newhash{'target'} eq '') && ($toolhash{'target'} eq 'window'))))) {
  281:                     delete($toolhash{$item});
  282:                     push(@deleted,$item);
  283:                     $changed = 1;
  284:                 }
  285:             } else {
  286:                 $toolhash{$item} = $newhash{$item};
  287:                 $changed = 1; 
  288:             }
  289:         }
  290:     }
  291:     if ($changed) {
  292:         my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$cdom,$cnum);
  293:         unless ($putres eq 'ok') {
  294:             $errormsg = &mt('Failed to save updated settings.').' '.&mt('Error: [_1].',$putres);
  295:         }
  296:     }
  297:     if (@deleted) {
  298:         &Apache::lonnet::del('exttool_'.$marker,\@deleted,$cdom,$cnum);
  299:     }
  300:     return ($changed,$errormsg);
  301: }
  302: 
  303: sub extedit_form {
  304:     my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,
  305:         $symb,$type,$cdom,$cnum,$ltitools,$disabled) = @_;
  306:     if ($type ne 'tool') {
  307:         $type = 'ext';
  308:     }
  309:     my %lt = &Apache::lonlocal::texthash(
  310:         ex => 'External Resource',
  311:         et => 'External Tool',
  312:         ed => 'Edit',
  313:         ee => 'External Resource Editor',
  314:         te => 'External Tool Editor',
  315:         pr => 'Preview',
  316:         sv => 'Save',
  317:         ul => 'URL',
  318:         ti => 'Title',
  319:         al => 'Add Link',
  320:         at => 'Add Tool',
  321:     );
  322:     my $tabid = 'aa';
  323:     my $size = 60;
  324:     if ($supplementalflag) {
  325:         $tabid = 'ee';
  326:     }
  327:     my ($formname,$formid,$toggle,$fieldsetid,$urlid,$dispdivstyle,$dimendivstyle,
  328:         $labelstyle,$titlestyle,$legend,$urlelem,$toolelem,%toolattr);
  329:     $formname = 'new'.$type;
  330:     $toggle = $type;
  331:     $fieldsetid = 'upload'.$type.'form';
  332:     $urlid = $type.'url';
  333:     map { $toolattr{$_} = $type.$_; } ('dispdiv','dimendiv','dimenwidth','dimenheight',
  334:                                        'crstitlediv','crslabeldiv','crstitle','crslabel');
  335:     $dispdivstyle = 'display:none';
  336:     $dimendivstyle = 'display:none';
  337:     $labelstyle = 'display:none';
  338:     $titlestyle = 'display:none';
  339:     if ($supplementalflag) {
  340:         $formname = 'newsupp'.$type;
  341:         $toggle = 'supp'.$type;
  342:         $fieldsetid = 'uploadsupp'.$type.'form';
  343:         $urlid = 'supp'.$type.'url';
  344:         map { $toolattr{$_} = 'supp'.$toolattr{$_}; } (keys(%toolattr));
  345:     }
  346:     my ($link,$legend,$active,$srcclass,$extsrc,$preview,$title,$save,$crstitle,$crslabel,
  347:         $fieldsetstyle,$action,$hiddenelem,$form,$width,$height,$tooltarget,%chkstate);
  348:     $fieldsetstyle = 'display: none;';
  349:     $action = '/adm/coursedocs';
  350:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https':'http');
  351:     if ($residx) {
  352:         if ($caller eq 'direct') {
  353:             $fieldsetstyle = 'display: block;';
  354:             $action = '/adm/extresedit';
  355:             if ($type eq 'tool') {
  356:                 $legend = $lt{'ee'};
  357:             } else {
  358:                 $legend = $lt{'te'};
  359:             }
  360:             $legend = '<legend>'.$legend.'</legend>';
  361:             if ($symb) {
  362:                 $hiddenelem = '<input type="hidden" name="symb" value="'.$symb.'" />';
  363:             } elsif ($supplementalflag) {
  364:                 $hiddenelem = '<input type="hidden" name="suppurl" value="'.
  365:                               &HTML::Entities::encode(&escape($orig_url),'<>&"').'" />'."\n".
  366:                               '<input type="hidden" name="title" value="'.
  367:                               &HTML::Entities::encode(&escape($orig_title),'<>&"').'" />';
  368:             }
  369:         } else {
  370:             $link = '<a class="LC_docs_ext_edit" href="javascript:editext('."'$residx','$type'".');">'.$lt{'ed'}.'</a>&nbsp;'."\n";
  371:             $size = 40;
  372:             $active = '<input type="hidden" name="active" value="'.$tabid.'" />';
  373:         }
  374:         $formname = 'edit'.$type.'_'.$residx;
  375:         $fieldsetid = 'upload'.$type.$residx;
  376:         $urlid = $type.'url_'.$residx;
  377:         map { $toolattr{$_} .= '_'.$residx; } (keys(%toolattr));
  378:         $srcclass = ' class="LC_nobreak"';
  379:         if ($type eq 'ext') {
  380:             $extsrc = '<span class="LC_docs_ext_edit">'.$lt{'ul'}.'&nbsp;</span>';
  381:             $preview = '&nbsp;<a class="LC_docs_ext_edit" href="javascript:extUrlPreview('."'$urlid','$protocol'".');">'.$lt{'pr'}.'</a>';
  382:         }
  383:         $title = '<span class="LC_docs_ext_edit">'.$lt{'ti'}.'&nbsp;</span>';
  384:         $save = $lt{'sv'};
  385:     } else {
  386:         $link = $lt{'ex'};
  387:         if ($type eq 'tool') {
  388:             $link = $lt{'et'};
  389:         }
  390:         $link = '<a class="LC_menubuttons_link" href="javascript:toggleUpload('."'$toggle'".');">'.$link.'</a>'.$helpitem;
  391:         if ($type eq 'tool') {
  392:             $legend = $lt{'te'};
  393:         } else {
  394:             $legend = $lt{'ee'};
  395:         }
  396:         $legend = '<legend>'.$legend.'</legend>';
  397:         $title = $lt{'ti'}.':<br />';
  398:         $residx = 0;
  399:         if ($type eq 'ext') {
  400:             $orig_url = 'http://';
  401:             $orig_title = $lt{'ex'};
  402:             $extsrc = $lt{'ul'}.':<br />';
  403:             $preview = '<input type="button" name="view" value="'.$lt{'pr'}.'" onclick="javascript:extUrlPreview('."'$urlid','$protocol'".');"'.$disabled.' />';
  404:             $save = $lt{'al'};
  405:         } else {
  406:             $orig_title = $lt{'et'};
  407:             $save = $lt{'at'};
  408:             $orig_url = "/adm/$cdom/$cnum/new/exttool"; 
  409:         }
  410:         $pathitem .= '<br />';
  411:     }
  412:     $formid = $formname;
  413:     if ($type eq 'ext') {
  414:         $urlelem = '<input type="text" size="'.$size.'" name="exturl" id="'.$urlid.'" value="'.$orig_url.'"'.$disabled.' />';
  415:     } else {
  416:         my $class = 'LC_nobreak';
  417:         if ($residx) {
  418:             $class = 'LC_docs_ext_edit LC_nobreak'; 
  419:             if ($orig_url =~ m{^/adm/$cdom/$cnum/(\d+)/exttools?$}) {
  420:                 my $marker = $1;
  421:                 my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
  422:                 if ($toolhash{'id'}) {
  423:                     if (ref($ltitools) eq 'HASH') {
  424:                         if (keys(%{$ltitools})) {
  425:                             if (ref($ltitools->{$toolhash{'id'}}) eq 'HASH') {
  426:                                 my $tooltitle = $ltitools->{$toolhash{'id'}}->{'title'};
  427:                                 my $icon = $ltitools->{$toolhash{'id'}}->{'image'};
  428:                                 my $image;
  429:                                 if ($icon) {
  430:                                     $image = '<img src="'.$icon.'" alt="'.$tooltitle.'" />';
  431:                                 }
  432:                                 $tooltarget = $toolhash{'target'};
  433:                                 if ($tooltarget eq 'window') {
  434:                                     $dimendivstyle = 'display:block';
  435:                                     $chkstate{'window'} = 'checked="checked" ';
  436:                                 } else {
  437:                                     $chkstate{'iframe'} = 'checked="checked" ';
  438:                                 }
  439:                                 $width = $toolhash{'width'};
  440:                                 $height = $toolhash{'height'};
  441:                                 if (ref($ltitools->{$toolhash{'id'}}->{'crsconf'}) eq 'HASH') {
  442:                                     if ($ltitools->{$toolhash{'id'}}->{'crsconf'}->{'title'}) {
  443:                                         $crstitle = $toolhash{'crstitle'};
  444:                                         $titlestyle = 'display:inline';
  445:                                     }
  446:                                     if ($ltitools->{$toolhash{'id'}}->{'crsconf'}->{'label'}) {  
  447:                                         $crslabel = $toolhash{'crslabel'};
  448:                                         $labelstyle = 'display:inline';
  449:                                     }
  450:                                     if ($ltitools->{$toolhash{'id'}}->{'crsconf'}->{'target'}) {
  451:                                         $dispdivstyle = 'display:block';
  452:                                     }
  453:                                 }
  454:                                 $toolelem = '<span class="LC_nobreak">'.$image.'&nbsp;'.$tooltitle.'</span><br />';
  455:                             }
  456:                         }
  457:                     }
  458:                 }
  459:             }
  460:         } else {
  461:             $toolelem = '<span class="LC_docs_ext_edit">'."\n".
  462:                        '<select name="exttoolid" id="LC_exttoolid" onchange="javascript:updateExttool(this,'.
  463:                        'this.form,'."'$supplementalflag'".');"'.$disabled.'>'."\n".
  464:                        '<option value="" selected="selected">'.&mt('Select').'</option>';
  465:             my %bynum;
  466:             if (ref($ltitools) eq 'HASH') {
  467:                 foreach my $id (keys(%{$ltitools})) {
  468:                     if (ref($ltitools->{$id}) eq 'HASH') {
  469:                         my $order = $ltitools->{$id}->{'order'};
  470:                         $bynum{$order} = [$id,$ltitools->{$id}];
  471:                     }
  472:                 }
  473:             }
  474:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
  475:                 if (ref($bynum{$item}) eq 'ARRAY') {
  476:                     if (ref($bynum{$item}->[1]) eq 'HASH') {
  477:                         my $tooltitle = $bynum{$item}->[1]->{'title'};
  478:                         my $icon =  $bynum{$item}->[1]->{'image'};
  479:                         $toolelem .= '<option value="'.$bynum{$item}->[0].'">'.$tooltitle.'</option>';
  480:                     }
  481:                 }
  482:             }
  483:             $toolelem .= '</select></span><br />';
  484:             $crslabel = $env{'course.'.$cdom.'_'.$cnum.'.internal.coursecode'};
  485:             $crstitle = $env{'course.'.$cdom.'_'.$cnum.'.description'};
  486:         }
  487:         $toolelem .= '<div id="'.$toolattr{'dispdiv'}.'" style="'.$dispdivstyle.'">'.
  488:                     '<span class="'.$class.'">'.&mt('Display target:').'&nbsp;'.
  489:                     '<label><input type="radio" name="exttooltarget" value="iframe" '.$chkstate{'iframe'}.'onclick="updateTooldim(this.form,'.
  490:                     "'$toolattr{dimendiv}','$toolattr{dimenwidth}','$toolattr{dimenheight}'".');"'.$disabled.'>'.&mt('iframe').'</label>'.('&nbsp;'x2).
  491:                     '<label><input type="radio" name="exttooltarget" value="window" '.$chkstate{'window'}.'onclick="updateTooldim(this.form,'.
  492:                     "'$toolattr{dimendiv}','$toolattr{dimenwidth}','$toolattr{dimenheight}'".');"'.$disabled.'>'.&mt('window').'</label>'.
  493:                     '</span><div id="'.$toolattr{'dimendiv'}.'" style="'.$dimendivstyle.'">'. 
  494:                     '<span class="'.$class.'">'.
  495:                     &mt('Width').'<input type="text" id="'.$toolattr{'dimenwidth'}.'" name="exttoolwidth" value="'.$width.'"'.$disabled.'>'.('&nbsp;'x2).
  496:                     &mt('Height').'<input type="text" id="'.$toolattr{'dimenheight'}.'" name="exttoolheight" value="'.$height.'"'.$disabled.'></span>'."\n".
  497:                     '</div></div>'.
  498:                     '<div id="'.$toolattr{'crslabeldiv'}.'" style="'.$labelstyle.'">'.
  499:                     '<span class="'.$class.'">'.&mt('Course label:').'&nbsp;'.
  500:                     '<input type="text" id="'.$toolattr{'crslabel'}.'" name="exttoollabel" value="'.$crslabel.'"'.$disabled.'><br />'.
  501:                     '</div>'.
  502:                     '<div id="'.$toolattr{'crstitlediv'}.'" style="'.$titlestyle.'">'.
  503:                     '<span class="'.$class.'">'.&mt('Course title:').'&nbsp;'.
  504:                     '<input type="text" id="'.$toolattr{'crstitle'}.'" name="exttooltitle" value="'.$crstitle.'"'.$disabled.'><br />'.
  505:                     '</div>';
  506:     }
  507:     my $chooser = $toolelem;
  508:     if ($type eq 'ext') {
  509:         $chooser = "
  510: <div>
  511: <span$srcclass>
  512: $extsrc
  513: $urlelem
  514: $preview
  515: </span>
  516: </div>
  517: ";
  518:     }
  519:     $form = <<ENDFORM;
  520: <form action="$action" method="post" name="$formname" id="$formid">
  521: <fieldset id="$fieldsetid" style="$fieldsetstyle">
  522: $legend
  523: $active
  524: $chooser
  525: <div>
  526: <span$srcclass>
  527: $title
  528: <input type="text" size="$size" name="exttitle" value="$orig_title" $disabled />
  529: <input type="hidden" name="importdetail" value="" />
  530: $pathitem
  531: $hiddenelem
  532: <input type="button" value="$save" onclick="javascript:setExternal(this.form,'$residx','$type','$orig_url','$supplementalflag');" $disabled />
  533: </span>
  534: </div>
  535: </fieldset>
  536: </form>
  537: ENDFORM
  538:     if (wantarray) {
  539:         return ($link,$form);
  540:     } else {
  541:         return $link.$form;
  542:     }
  543: }
  544: 
  545: sub display_editor {
  546:     my ($url,$folderpath,$symb,$idx,$type,$cdom,$cnum,$hostname) = @_;
  547:     my ($residx,$supplementalflag,$title,$pathitem,$output,$js,$navmap);
  548:     if ($folderpath =~ /^supplemental/) {
  549:         $supplementalflag = 1;
  550:         $residx = $idx;
  551:         $title = &unescape($env{'form.title'});
  552:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($folderpath,'<>&"').'" />';
  553:     } elsif ($symb =~ /^uploaded/) {
  554:         (my $map,$residx,my $res) =
  555:             &Apache::lonnet::decode_symb($symb);
  556:         $title = &Apache::lonnet::gettitle($symb);
  557:         my $path = &Apache::loncommon::symb_to_docspath($symb,\$navmap);
  558:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($path,'<>&"').'" />';
  559:     }
  560:     my %ltitools;
  561:     if ($type eq 'tool') {
  562:         %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
  563:     }
  564:     $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
  565:     my $args = { 'force_register' => $env{'form.register'} };
  566:     if ($hostname) {
  567:         $args->{'hostname'} = $hostname;
  568:     }
  569:     my $description = 'External Resource Editor';
  570:     if ($type eq 'tool') {
  571:         $description = 'External Tool Editor';
  572:     }
  573:     return &Apache::loncommon::start_page($description,$js,$args).
  574:            '<div class="LC_left_float">'.
  575:            &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',
  576:                          $symb,$type,$cdom,$cnum,\%ltitools).
  577:            '</div>'.
  578:            &Apache::loncommon::end_page();
  579: }
  580: 
  581: sub extedit_javascript {
  582:     my ($toolsref) = @_;
  583:     my $toolsjs;
  584:     if (ref($toolsref) eq 'HASH') {
  585:         my $num = scalar(keys(%{$toolsref}));
  586:         $toolsjs = "        var ltitools = new Array($num);\n".
  587:                    "        var ltitoolsTarget = new Array($num);\n".
  588:                    "        var ltitoolsWidth = new Array($num);\n".
  589:                    "        var ltitoolsHeight = new Array($num);\n".
  590:                    "        var ltitoolsDisplay = new Array($num);\n".
  591:                    "        var ltitoolsLabel = new Array($num);\n".
  592:                    "        var ltitoolsTitle = new Array($num);\n";
  593:         my $i = 0;
  594:         foreach my $key (sort { $a <=> $b } keys(%{$toolsref})) {
  595:             if (ref($toolsref->{$key})) {
  596:                 my $target = $toolsref->{$key}->{'target'};
  597:                 my $width = $toolsref->{$key}->{'width'};
  598:                 my $height = $toolsref->{$key}->{'height'};
  599:                 $toolsjs .= '        ltitools['.$i.'] = '."'$key';\n".
  600:                             '        ltitoolsTarget['.$i.'] = '."'$target';\n".
  601:                             '        ltitoolsWidth['.$i.'] = '."'$width';\n".
  602:                             '        ltitoolsHeight['.$i.'] = '."'$height';\n";
  603:                 my %courseconfig;
  604:                 if (ref($toolsref->{$key}->{'crsconf'}) eq 'HASH') {
  605:                     my $display = $toolsref->{$key}->{'crsconf'}->{'target'};
  606:                     $toolsjs .= '         ltitoolsDisplay['.$i.'] = '."'$display';\n";
  607:                     my $label = $toolsref->{$key}->{'crsconf'}->{'label'};
  608:                     $toolsjs .= '         ltitoolsLabel['.$i.'] = '."'$label';\n";
  609:                     my $title = $toolsref->{$key}->{'crsconf'}->{'title'};
  610:                     $toolsjs .= '         ltitoolsTitle['.$i.'] = '."'$title';\n";
  611:                 }
  612:                 $i++;
  613:             }
  614:         }
  615:     }
  616:     my %js_lt = &Apache::lonlocal::texthash(
  617:         invurl  => 'Invalid URL',
  618:         titbl   => 'Title is blank',
  619:         invtool => 'Please select an external tool',
  620:     );
  621:     &js_escape(\%js_lt);
  622: 
  623:     my $urlregexp = <<'ENDREGEXP';
  624: /^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
  625: ENDREGEXP
  626: 
  627:     return <<ENDJS;
  628: 
  629: var regexp = $urlregexp;
  630: 
  631: function setExternal(extform,residx,type,exttoolurl,supplementalflag) {
  632:     var title=extform.exttitle.value;
  633:     if (!String.trim) {
  634:         String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");};    }
  635:     if (title == null || title.trim()=="") {
  636:         alert("$js_lt{'titbl'}");
  637:         extform.exttitle.focus();
  638:         return;
  639:     }
  640:     if (type == 'ext') {
  641:         var url=extform.exturl.value;
  642:         if (!regexp.test(url)) {
  643:             alert("$js_lt{'invurl'}");
  644:             extform.exturl.focus();
  645:             return;
  646:         } else {
  647:             url = escape(url);
  648:             title = escape(title);
  649:             if (residx > 0) {
  650:                eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
  651:             } else {
  652:                eval("extform.importdetail.value=title+'='+url;extform.submit();");
  653:             }
  654:         }
  655:     } else {
  656:         title = escape(title);
  657:         var info = exttoolurl;
  658:         if (residx == 0) {
  659:             var toolid = parseInt(extform.exttoolid.options[extform.exttoolid.selectedIndex].value);
  660:             if (isNaN(toolid)) {
  661:                 alert("$js_lt{'invtool'}");
  662:                 return;
  663:             }
  664:             info += ':'+toolid;
  665:         }
  666:         var prefix = '';
  667:         if (supplementalflag == 1) {
  668:            prefix = 'supp';
  669:         }
  670:         var dispdiv = prefix+'tooldispdiv';
  671:         if (residx > 0) {
  672:             dispdiv += '_'+residx;
  673:         }
  674:         if (document.getElementById(dispdiv)) {
  675:             if (document.getElementById(dispdiv).style.display == 'block') {
  676:                 if (extform.exttooltarget.length) {
  677:                     for (var i=0; i<extform.exttooltarget.length; i++) {
  678:                         if (extform.exttooltarget[i].checked) {
  679:                             if (extform.exttooltarget[i].value == 'window') {
  680:                                 var width = extform.exttoolwidth.value;
  681:                                 width.trim();
  682:                                 var height = extform.exttoolheight.value;
  683:                                 height.trim();
  684:                                 info += ':window:'+width+':'+height;  
  685:                             } else {
  686:                                 info += ':iframe::';
  687:                             }
  688:                         }
  689:                     }
  690:                 }
  691:             } else {
  692:                 info += ':::';
  693:             }
  694:         } else {
  695:             info += ':::';
  696:         }
  697:         var labelinput = prefix+'toolcrslabel';
  698:         var titleinput = prefix+'toolcrstitle';
  699:         if (residx > 0) {
  700:             labelinput += '_'+residx;
  701:             titleinput += '_'+residx;
  702:         }
  703:         if (document.getElementById(labelinput)) {
  704:             var crslabel = document.getElementById(labelinput).value;
  705:             crslabel.trim();
  706:             info += ':'+escape(crslabel);
  707:         } else {
  708:             info += ':';
  709:         } 
  710:         if (document.getElementById(titleinput)) {
  711:             var crstitle = document.getElementById(titleinput).value;
  712:             crstitle.trim();
  713:             info += ':'+escape(crstitle);
  714:         } else {
  715:             info += ':';
  716:         }
  717:         info=escape(info);
  718:         if (residx > 0) {
  719:             eval("extform.importdetail.value=title+'='+info+'='+residx;extform.submit();");
  720:         } else {
  721:             eval("extform.importdetail.value=title+'='+info;extform.submit();");
  722:         }
  723:     }
  724: }
  725: 
  726: function editext(residx,type) {
  727:     if (document.getElementById('upload'+type+residx)) {
  728:         var curr = document.getElementById('upload'+type+residx).style.display;
  729:         if (curr == 'none') {
  730:             disp = 'block';
  731:         } else {
  732:             disp = 'none';
  733:         }
  734:         document.getElementById('upload'+type+residx).style.display=disp;
  735:     }
  736:     resize_scrollbox('contentscroll','1','1');
  737:     return;
  738: }
  739: 
  740: function extUrlPreview(caller,protocol) {
  741:     if (document.getElementById(caller)) {
  742:         var url = document.getElementById(caller).value;
  743:         if (regexp.test(url)) {
  744:             var http_regex = /^http\:\/\//gi;
  745:             if ((protocol == 'https') && (http_regex.test(url))) {
  746:                 window.open(url,"externalpreview","height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1");
  747:             } else {
  748:                 openMyModal(url,500,400,'yes');
  749:             }
  750:         } else {
  751:             alert("$js_lt{'invurl'}");
  752:         }
  753:     }
  754: }
  755: 
  756: function updateExttool(caller,form,supplementalflag) {
  757:     var prefix = '';
  758:     if (supplementalflag == 1) {
  759:         prefix = 'supp';
  760:     }
  761:     dispdiv = prefix+'tooldispdiv';
  762:     dimendiv = prefix+'tooldimendiv';
  763:     widthinput = prefix+'toolwidth';
  764:     heightinput = prefix+'toolheight';
  765:     labeldiv = prefix+'toolcrslabeldiv';
  766:     titlediv = prefix+'toolcrstitlediv';
  767:     labelinput = prefix+'toolcrslabel';
  768:     titleinput = prefix+'toolcrstitle';
  769:     if (document.getElementById(dispdiv)) {
  770:         var toolpick = caller.options[caller.selectedIndex].value;
  771:         $toolsjs
  772:         if (toolpick == '') {
  773:             if (document.getElementById(dispdiv)) {
  774:                 document.getElementById(dispdiv).style.display = 'none';    
  775:             }
  776:             if (document.getElementById(dimendiv)) {
  777:                 document.getElementById(dimendiv).style.display = 'none';
  778:             }
  779:             if (document.getElementById(labeldiv)) {
  780:                 document.getElementById(labeldiv).style.display = 'none';
  781:             }
  782:             if (document.getElementById(titlediv)) {
  783:                 document.getElementById(titlediv).style.display = 'none';
  784:             }
  785:         } else {
  786:             if (ltitools.length > 0) {
  787:                 for (var j=0; j<ltitools.length; j++) {
  788:                     if (ltitools[j] == toolpick) {
  789:                         if (document.getElementById(dispdiv)) {
  790:                             if (ltitoolsDisplay[j]) {
  791:                                 document.getElementById(dispdiv).style.display = 'block';
  792:                                 if (form.exttooltarget.length) {
  793:                                     for (var k=0; k<form.exttooltarget.length; k++) {
  794:                                         if (form.exttooltarget[k].value == ltitoolsTarget[j]) {
  795:                                             form.exttooltarget[k].checked = true;
  796:                                             break;
  797:                                         }
  798:                                     }
  799:                                 }
  800:                                 if (ltitoolsTarget[j] == 'window') {
  801:                                     dimen = 'block';
  802:                                     dimenwidth = ltitoolsWidth[j];
  803:                                     dimenheight = ltitoolsHeight[j];                    
  804:                                 } else {
  805:                                     dimen = 'none';
  806:                                     dimenwidth = '';
  807:                                     dimenheight = '';
  808:                                 }
  809:                                 if (document.getElementById(dimendiv)) {
  810:                                     document.getElementById(dimendiv).style.display = dimen;
  811:                                 }
  812:                                 if (document.getElementById(widthinput)) {
  813:                                     document.getElementById(widthinput).value = dimenwidth;
  814:                                 }
  815:                                 if (document.getElementById(heightinput)) {
  816:                                     document.getElementById(heightinput).value = dimenheight;
  817:                                 }
  818:                             }
  819:                         }
  820:                         if (document.getElementById(labeldiv)) {
  821:                             if (ltitoolsLabel[j]) {
  822:                                 document.getElementById(labeldiv).style.display = 'inline';
  823:                             } else {
  824:                                 document.getElementById(labeldiv).style.display = 'none';
  825:                             } 
  826:                         }
  827:                         if (document.getElementById(titlediv)) {
  828:                             if (ltitoolsTitle[j]) {
  829:                                 document.getElementById(titlediv).style.display = 'inline';
  830:                             } else {
  831:                                 document.getElementById(titlediv).style.display = 'none';
  832:                             }
  833:                         }
  834:                         break;
  835:                     }
  836:                 }
  837:             }
  838:         }
  839:     }
  840: }
  841: 
  842: function updateTooldim(form,dimendiv,widthinput,heightinput) {
  843:     if (form.exttooltarget.length) {
  844:         for (var i=0; i<form.exttooltarget.length; i++) {
  845:             if (form.exttooltarget[i].checked) {
  846:                 var dimen = 'none';
  847:                 if (form.exttooltarget[i].value == 'window') {
  848:                     dimen = 'block';
  849:                 } else {
  850:                     if (document.getElementById(widthinput)) {
  851:                         document.getElementById(widthinput).value = '';
  852:                     }
  853:                     if (document.getElementById(heightinput)) {
  854:                         document.getElementById(heightinput).value = '';
  855:                     }
  856:                 }
  857:                 if (document.getElementById(dimendiv)) {
  858:                     document.getElementById(dimendiv).style.display = dimen;
  859:                 }
  860:                 break;
  861:             }
  862:         }
  863:     }
  864: }
  865: 
  866: ENDJS
  867: 
  868: }
  869: 
  870: 1;

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