Annotation of loncom/interface/lonextresedit.pm, revision 1.8.2.3

1.1       raeburn     1: # The LearningOnline Network
                      2: # Documents
                      3: #
1.8.2.3 ! raeburn     4: # $Id: lonextresedit.pm,v 1.8.2.2 2017/03/14 21:09:20 raeburn Exp $
1.1       raeburn     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,$symb);
1.5       raeburn    60:     if (($env{'form.folderpath'} =~ /^supplemental/) && ($env{'form.suppurl'})) {
                     61:         $supplementalflag = 1;
1.7       raeburn    62:     }
1.5       raeburn    63:     if (($supplementalflag) || ($env{'form.symb'} =~ /^uploaded/)) {
1.7       raeburn    64:         ($updated,$output,$errormsg,$residx,$url,$title,$symb) =
1.1       raeburn    65:             &process_changes($supplementalflag,$cdom,$cnum,$chome);
                     66:         if ($supplementalflag) {
                     67:             if ($url ne $env{'form.suppurl'}) {
                     68:                  $env{'form.suppurl'} = $url;
                     69:             }
                     70:             if ($title ne $env{'form.title'}) {
                     71:                 $env{'form.title'} = $title;
                     72:             }
1.5       raeburn    73:             $env{'form.idx'} = $residx;
1.1       raeburn    74:         } else {
                     75:             if ($symb ne $env{'form.symb'}) {
                     76:                 $env{'form.symb'} = $symb;
                     77:             }
                     78:         }
                     79:     } else {
                     80:         $errormsg = &mt('Information about external resource to edit is missing.');
                     81:     }
                     82:     if ($updated) {
                     83:         $output = &Apache::lonhtmlcommon::confirm_success(&mt('External Resource updated'));
                     84:     }
                     85:     if ($errormsg) {
                     86:         $errormsg = '<p class="LC_error">'.$errormsg.'</p>';
                     87:     }
                     88:     my $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
                     89:     my $pathitem = '<input type="hidden" name="folderpath" value="'.
                     90:                    &HTML::Entities::encode($env{'form.folderpath'},'<>&"').'" />';
                     91:     $r->print(&Apache::loncommon::start_page('External Resource Editor',$js).
                     92:               '<div class="LC_left_float">'.
                     93:               $output.
                     94:               $errormsg.
                     95:               &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,
                     96:                            'direct',$env{'form.symb'}).
                     97:               '</div>'.&Apache::loncommon::end_page());
                     98:     return OK;
                     99: }
                    100: 
                    101: sub process_changes {
                    102:     my ($supplementalflag,$cdom,$cnum,$chome) = @_;
                    103:     my ($folder,$container,$output,$errormsg,$updated,$symb,$oldidx,$oldurl,
1.5       raeburn   104:         $oldtitle,$newidx,$newurl,$newtitle,$residx,$url,$title);
1.1       raeburn   105:     if ($env{'form.symb'}) {
                    106:         $symb = $env{'form.symb'};
1.5       raeburn   107:         (my $map,$oldidx,$oldurl)=&Apache::lonnet::decode_symb($symb);
1.1       raeburn   108:         if ($map =~ m{^uploaded/$cdom/$cnum/(default(_\d+|))\.(sequence|page)$}) {
                    109:             $folder = $1;
                    110:             $container = $3;
                    111:         }
                    112:         $oldtitle = &Apache::lonnet::gettitle($env{'form.symb'});
                    113:     } elsif ($env{'form.folderpath'}) {
                    114:         $folder = &unescape( (split('&',$env{'form.folderpath'}))[-2] );
                    115:         $oldurl = &unescape($env{'form.suppurl'});
                    116:         $oldtitle = &unescape($env{'form.title'});
                    117:         $container = 'sequence';
                    118:         $supplementalflag = 1;
                    119:     }
1.5       raeburn   120:     if ($oldurl =~ m{^ext/(.+)$}) {
                    121:         my $external = $1; 
                    122:         if ($external =~ m{^https://}) {
                    123:             $oldurl = $external;
                    124:         } else {
                    125:             $oldurl = 'http://'.$oldurl;
                    126:         }
                    127:     }
                    128:     $url = $oldurl;
                    129:     $title = $oldtitle;
                    130:     if ($env{'form.importdetail'}) {
                    131:         ($newtitle,$newurl,$newidx) =
                    132:             map {&unescape($_)} split(/\=/,$env{'form.importdetail'});
                    133:     }
                    134:     if ($supplementalflag) {
                    135:         $residx = $newidx;
                    136:     } else {
                    137:         $residx = $oldidx;
                    138:     }
1.1       raeburn   139:     if ($folder && $container) {
                    140:         if ($env{'form.importdetail'}) {
1.5       raeburn   141:             my ($errtext,$fatal,$mismatchedid,@imports);
                    142:             if (!$supplementalflag) {
                    143:                 if (($oldidx) && ($oldidx != $newidx)) {
                    144:                     $mismatchedid = 1;
                    145:                 }
1.1       raeburn   146:             }
                    147:             if ($mismatchedid) {
                    148:                 $errormsg = 'Wrong item identifier';
                    149:             } elsif (($newtitle eq $oldtitle) && ($newurl eq $oldurl)) {
                    150:                 $output = &mt('No change');
                    151:             } else {
                    152:                 my $map = "/uploaded/$cdom/$cnum/$folder.$container";
                    153:                 my ($errtext,$fatal) = &LONCAPA::map::mapread($map);
                    154:                 if ($fatal) {
                    155:                     $errormsg = &mt('Update failed: [_1].',$errtext);
                    156:                 } else {
                    157:                     my $saveurl = &LONCAPA::map::qtunescape($newurl);
                    158:                     my $savetitle = &LONCAPA::map::qtunescape($newtitle);
                    159:                     $LONCAPA::map::resources[$residx] =
                    160:                         join(':', ($savetitle,$saveurl,'true','normal','res'));
                    161:                     my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                    162:                     if ($errtext) {
                    163:                         $errormsg = &mt('Update failed: [_1].',$errtext);
                    164:                     } else {
                    165:                         $updated = 1;
1.5       raeburn   166:                         $title = $newtitle;
                    167:                         if ($newurl ne $oldurl) {
                    168:                             $url = $newurl;
                    169:                             $newurl =~ s{^http://}{};
                    170:                             $newurl = "ext/$newurl";
                    171:                         }
1.1       raeburn   172:                         if (!$supplementalflag) {
                    173:                             if ($newurl ne $oldurl) {
1.5       raeburn   174:                                 $symb = &Apache::lonnet::encode_symb($map,$residx,$newurl);
1.1       raeburn   175:                             } else {
                    176:                                 $symb = $env{'form.symb'};
                    177:                                 if ($symb) {
                    178:                                     &Apache::lonnet::devalidate_title_cache($symb);
                    179:                                 }
                    180:                             }
                    181:                         }
1.5       raeburn   182:                         my ($furl,$ferr) = 
                    183:                             &Apache::lonuserstate::readmap("$cdom/$cnum");
1.1       raeburn   184:                         if ($ferr) {
                    185:                             $errormsg = &mt('Reload failed: [_1].',$ferr);
                    186:                         } else {
1.5       raeburn   187:                             unless ($supplementalflag) {
                    188:                                 &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,$cdom.'_'.$cnum);
                    189:                             }
1.1       raeburn   190:                         }
                    191:                     }
                    192:                 }
                    193:             }
                    194:         } else {
                    195:             $output = &mt('No change');
                    196:         }
                    197:     } else {
                    198:         $errormsg = &mt('Information about current external resource is incomplete.');
                    199:     }
1.5       raeburn   200:     return ($updated,$output,$errormsg,$residx,$url,$title,$symb);
1.1       raeburn   201: }
                    202: 
                    203: sub extedit_form {
1.8.2.1   raeburn   204:     my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,$symb,$disabled) = @_;
1.1       raeburn   205:     my %lt = &Apache::lonlocal::texthash(
                    206:         ex => 'External Resource',
                    207:         ed => 'Edit',
                    208:         ee => 'External Resource Editor',
                    209:         pr => 'Preview',
                    210:         sv => 'Save',
                    211:         ul => 'URL',
                    212:         ti => 'Title',
                    213:         al => 'Add Link',
                    214:     );
                    215:     my $formname = 'newext';
                    216:     my $tabid = 'aa';
                    217:     my $toggle = 'ext';
                    218:     my $fieldsetid = 'uploadextform';
                    219:     my $urlid = 'exturl';
                    220:     my $size = 60;
                    221:     if ($supplementalflag) {
                    222:         $formname = 'newsuppext';
                    223:         $tabid = 'ee';
                    224:         $toggle = 'suppext';
                    225:         $fieldsetid = 'uploadsuppextform';
                    226:         $urlid = 'suppexturl';
                    227:     }
                    228:     my ($link,$legend,$active,$srcclass,$extsrc,$preview,$title,$save,
1.3       raeburn   229:         $fieldsetstyle,$action,$hiddenelem,$form);
1.1       raeburn   230:     $fieldsetstyle = 'display: none;';
                    231:     $action = '/adm/coursedocs';
1.8.2.2   raeburn   232:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https':'http');
1.1       raeburn   233:     if ($residx) {
                    234:         if ($caller eq 'direct') {
                    235:             $fieldsetstyle = 'display: block;';
                    236:             $action = '/adm/extresedit';
                    237:             $legend = "<legend>$lt{'ee'}</legend>";
                    238:             if ($symb) {
                    239:                 $hiddenelem = '<input type="hidden" name="symb" value="'.$symb.'" />';
                    240:             } elsif ($supplementalflag) {
                    241:                 $hiddenelem = '<input type="hidden" name="suppurl" value="'.
                    242:                               &HTML::Entities::encode(&escape($orig_url),'<>&"').'" />'."\n".
                    243:                               '<input type="hidden" name="title" value="'.
                    244:                               &HTML::Entities::encode(&escape($orig_title),'<>&"').'" />';
                    245:             }
                    246:         } else {        
1.3       raeburn   247:             $link = '<a class="LC_docs_ext_edit" href="javascript:editext('."'$residx'".');">'.$lt{'ed'}.'</a>&nbsp;'."\n";
1.1       raeburn   248:             $size = 40;
1.3       raeburn   249:             $active = '<input type="hidden" name="active" value="'.$tabid.'" />';
1.1       raeburn   250:         }
                    251:         $formname = "editext_$residx";
                    252:         $fieldsetid = "uploadext$residx";
                    253:         $urlid = "exturl_$residx";
                    254:         $srcclass = ' class="LC_nobreak"';
                    255:         $extsrc = '<span class="LC_docs_ext_edit">'.$lt{'ul'}.'&nbsp;</span>';
1.8.2.2   raeburn   256:         $preview = '&nbsp;<a class="LC_docs_ext_edit" href="javascript:extUrlPreview('."'$urlid','$protocol'".');">'.$lt{'pr'}.'</a>';
1.1       raeburn   257:         $title = '<span class="LC_docs_ext_edit">'.$lt{'ti'}.'&nbsp;</span>';
                    258:         $save = $lt{'sv'};
                    259:     } else {
                    260:         $link = '<a class="LC_menubuttons_link" href="javascript:toggleUpload('."'$toggle'".');">'.$lt{'ex'}.'</a>'.$helpitem;
                    261:         $legend = "<legend>$lt{'ex'}</legend>";
                    262:         $extsrc = $lt{'ul'}.':<br />';
                    263:         $title = $lt{'ti'}.':<br />';
                    264:         $residx = 0;
                    265:         $orig_url = 'http://';
                    266:         $orig_title = $lt{'ex'};
1.8.2.2   raeburn   267:         $preview = '<input type="button" name="view" value="'.$lt{'pr'}.'" onclick="javascript:extUrlPreview('."'$urlid','$protocol'".');"'.$disabled.' />';
1.1       raeburn   268:         $save = $lt{'al'};
                    269:         $pathitem .= '<br />';
                    270:     }
1.3       raeburn   271:     $form = <<ENDFORM;
1.1       raeburn   272: <form action="$action" method="post" name="$formname">
1.2       raeburn   273: <fieldset id="$fieldsetid" style="$fieldsetstyle">
1.1       raeburn   274: $legend
                    275: $active
                    276: <span$srcclass>
                    277: $extsrc
1.8.2.1   raeburn   278: <input type="text" size="$size" name="exturl" id="$urlid" value="$orig_url" $disabled />
1.1       raeburn   279: $preview
                    280: </span> 
                    281: <br />
                    282: <span$srcclass>
                    283: $title
1.8.2.1   raeburn   284: <input type="text" size="$size" name="exttitle" value="$orig_title" $disabled />
1.1       raeburn   285: <input type="hidden" name="importdetail" value="" />
                    286: $pathitem
                    287: $hiddenelem
1.8.2.1   raeburn   288: <input type="button" value="$save" onclick="javascript:setExternal(this.form,'$residx');" $disabled />
1.1       raeburn   289: </span>
                    290: </fieldset>
                    291: </form>
                    292: ENDFORM
1.3       raeburn   293:     if (wantarray) {
                    294:         return ($link,$form);
                    295:     } else {
                    296:         return $link.$form;
                    297:     }
1.1       raeburn   298: }
                    299: 
                    300: sub display_editor {
1.8.2.3 ! raeburn   301:     my ($url,$folderpath,$symb,$idx,$type,$cdom,$cnum,$hostname) = @_;
1.8.2.1   raeburn   302:     my ($residx,$supplementalflag,$title,$pathitem,$output,$js,$navmap);
1.1       raeburn   303:     if ($folderpath =~ /^supplemental/) {
                    304:         $supplementalflag = 1;
                    305:         $residx = $idx;
                    306:         $title = &unescape($env{'form.title'});
                    307:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($folderpath,'<>&"').'" />';
                    308:     } elsif ($symb =~ /^uploaded/) {
                    309:         (my $map,$residx,my $res) =
                    310:             &Apache::lonnet::decode_symb($symb);
                    311:         $title = &Apache::lonnet::gettitle($symb);
1.8.2.1   raeburn   312:         my $path = &Apache::loncommon::symb_to_docspath($symb,\$navmap);
1.4       raeburn   313:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($path,'<>&"').'" />';
1.1       raeburn   314:     }
1.8.2.1   raeburn   315:     $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
1.1       raeburn   316:     my $args = { 'force_register' => $env{'form.register'} };
1.8.2.3 ! raeburn   317:     if ($hostname) {
        !           318:         $args->{'hostname'} = $hostname;
        !           319:     }
1.1       raeburn   320:     return &Apache::loncommon::start_page('External Resource Editor',$js,$args).
                    321:            '<div class="LC_left_float">'.
                    322:            &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',$symb).
                    323:            '</div>'.
                    324:            &Apache::loncommon::end_page();
                    325: }
                    326: 
                    327: sub extedit_javascript {
1.8       damieng   328:     my %js_lt = &Apache::lonlocal::texthash(
1.1       raeburn   329:         invurl  => 'Invalid URL',
                    330:         titbl   => 'Title is blank',
                    331:     );
1.8       damieng   332:     &js_escape(\%js_lt);
1.1       raeburn   333: 
                    334:     my $urlregexp = <<'ENDREGEXP';
                    335: /^([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
                    336: ENDREGEXP
                    337: 
                    338:     return <<ENDJS;
                    339: 
                    340: var regexp = $urlregexp;
                    341: 
                    342: function setExternal(extform,residx) {
                    343:     var title=extform.exttitle.value;
                    344:     if (!String.trim) {
                    345:         String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");};    }
                    346:     var url=extform.exturl.value;
                    347:     if (title == null || title.trim()=="") {
1.8       damieng   348:         alert("$js_lt{'titbl'}");
1.1       raeburn   349:         extform.exttitle.focus();
                    350:         return;
                    351:     }
                    352:     if (regexp.test(url)) {
                    353:         url = escape(url);
1.7       raeburn   354:         title = escape(title);
1.1       raeburn   355:         if (residx > 0) {
                    356:             eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
                    357:         } else {
                    358:             eval("extform.importdetail.value=title+'='+url;extform.submit();");
                    359:         }
                    360:     } else {
1.8       damieng   361:         alert("$js_lt{'invurl'}");
1.1       raeburn   362:         extform.exturl.focus();
                    363:         return;
                    364:     }
                    365: }
                    366: 
                    367: function editext(residx) {
                    368:     if (document.getElementById('uploadext'+residx)) {
                    369:         var curr = document.getElementById('uploadext'+residx).style.display;
                    370:         if (curr == 'none') {
                    371:             disp = 'block';
                    372:         } else {
                    373:             disp = 'none';
                    374:         }
                    375:         document.getElementById('uploadext'+residx).style.display=disp;
                    376:     }
                    377:     resize_scrollbox('contentscroll','1','1');
                    378:     return;
                    379: }
                    380: 
1.8.2.2   raeburn   381: function extUrlPreview(caller,protocol) {
1.1       raeburn   382:     if (document.getElementById(caller)) {
                    383:         var url = document.getElementById(caller).value;
                    384:         if (regexp.test(url)) {
1.8.2.2   raeburn   385:             var http_regex = /^http\:\/\//gi;
                    386:             if ((protocol == 'https') && (http_regex.test(url))) {
                    387:                 window.open(url,"externalpreview","height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1");
                    388:             } else {
                    389:                 openMyModal(url,500,400,'yes');
                    390:             }
1.1       raeburn   391:         } else {
1.8       damieng   392:             alert("$js_lt{'invurl'}");
1.1       raeburn   393:         }
                    394:     }
                    395: }
                    396: 
                    397: ENDJS
                    398: 
                    399: }
                    400: 
                    401: 1;

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