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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Documents
                      3: #
1.7     ! raeburn     4: # $Id: lonextresedit.pm,v 1.6 2013/05/21 18:57:40 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 {
                    204:     my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,$symb) = @_;
                    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';
                    232:     if ($residx) {
                    233:         if ($caller eq 'direct') {
                    234:             $fieldsetstyle = 'display: block;';
                    235:             $action = '/adm/extresedit';
                    236:             $legend = "<legend>$lt{'ee'}</legend>";
                    237:             if ($symb) {
                    238:                 $hiddenelem = '<input type="hidden" name="symb" value="'.$symb.'" />';
                    239:             } elsif ($supplementalflag) {
                    240:                 $hiddenelem = '<input type="hidden" name="suppurl" value="'.
                    241:                               &HTML::Entities::encode(&escape($orig_url),'<>&"').'" />'."\n".
                    242:                               '<input type="hidden" name="title" value="'.
                    243:                               &HTML::Entities::encode(&escape($orig_title),'<>&"').'" />';
                    244:             }
                    245:         } else {        
1.3       raeburn   246:             $link = '<a class="LC_docs_ext_edit" href="javascript:editext('."'$residx'".');">'.$lt{'ed'}.'</a>&nbsp;'."\n";
1.1       raeburn   247:             $size = 40;
1.3       raeburn   248:             $active = '<input type="hidden" name="active" value="'.$tabid.'" />';
1.1       raeburn   249:         }
                    250:         $formname = "editext_$residx";
                    251:         $fieldsetid = "uploadext$residx";
                    252:         $urlid = "exturl_$residx";
                    253:         $srcclass = ' class="LC_nobreak"';
                    254:         $extsrc = '<span class="LC_docs_ext_edit">'.$lt{'ul'}.'&nbsp;</span>';
                    255:         $preview = '&nbsp;<a class="LC_docs_ext_edit" href="javascript:extUrlPreview('."'$urlid'".');">'.$lt{'pr'}.'</a>';
                    256:         $title = '<span class="LC_docs_ext_edit">'.$lt{'ti'}.'&nbsp;</span>';
                    257:         $save = $lt{'sv'};
                    258:     } else {
                    259:         $link = '<a class="LC_menubuttons_link" href="javascript:toggleUpload('."'$toggle'".');">'.$lt{'ex'}.'</a>'.$helpitem;
                    260:         $legend = "<legend>$lt{'ex'}</legend>";
                    261:         $extsrc = $lt{'ul'}.':<br />';
                    262:         $title = $lt{'ti'}.':<br />';
                    263:         $residx = 0;
                    264:         $orig_url = 'http://';
                    265:         $orig_title = $lt{'ex'};
                    266:         $preview = '<input type="button" name="view" value="'.$lt{'pr'}.'" onclick="javascript:extUrlPreview('."'$urlid'".');" />';
                    267:         $save = $lt{'al'};
                    268:         $pathitem .= '<br />';
                    269:     }
1.3       raeburn   270:     $form = <<ENDFORM;
1.1       raeburn   271: <form action="$action" method="post" name="$formname">
1.2       raeburn   272: <fieldset id="$fieldsetid" style="$fieldsetstyle">
1.1       raeburn   273: $legend
                    274: $active
                    275: <span$srcclass>
                    276: $extsrc
                    277: <input type="text" size="$size" name="exturl" id="$urlid" value="$orig_url" />
                    278: $preview
                    279: </span> 
                    280: <br />
                    281: <span$srcclass>
                    282: $title
                    283: <input type="text" size="$size" name="exttitle" value="$orig_title" />
                    284: <input type="hidden" name="importdetail" value="" />
                    285: $pathitem
                    286: $hiddenelem
                    287: <input type="button" value="$save" onclick="javascript:setExternal(this.form,'$residx');" />
                    288: </span>
                    289: </fieldset>
                    290: </form>
                    291: ENDFORM
1.3       raeburn   292:     if (wantarray) {
                    293:         return ($link,$form);
                    294:     } else {
                    295:         return $link.$form;
                    296:     }
1.1       raeburn   297: }
                    298: 
                    299: sub display_editor {
                    300:     my ($url,$folderpath,$symb,$idx) = @_;
                    301:     my ($residx,$supplementalflag,$title,$pathitem,$output);
                    302:     if ($folderpath =~ /^supplemental/) {
                    303:         $supplementalflag = 1;
                    304:         $residx = $idx;
                    305:         $title = &unescape($env{'form.title'});
                    306:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($folderpath,'<>&"').'" />';
                    307:     } elsif ($symb =~ /^uploaded/) {
                    308:         (my $map,$residx,my $res) =
                    309:             &Apache::lonnet::decode_symb($symb);
                    310:         $title = &Apache::lonnet::gettitle($symb);
1.6       raeburn   311:         my $path = &Apache::loncommon::symb_to_docspath($symb);
1.4       raeburn   312:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($path,'<>&"').'" />';
1.1       raeburn   313:     }
                    314:     my $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
                    315:     my $args = { 'force_register' => $env{'form.register'} };
                    316:     return &Apache::loncommon::start_page('External Resource Editor',$js,$args).
                    317:            '<div class="LC_left_float">'.
                    318:            &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',$symb).
                    319:            '</div>'.
                    320:            &Apache::loncommon::end_page();
                    321: }
                    322: 
                    323: sub extedit_javascript {
                    324:     my %lt = &Apache::lonlocal::texthash(
                    325:         invurl  => 'Invalid URL',
                    326:         titbl   => 'Title is blank',
                    327:     );
                    328: 
                    329:     my $urlregexp = <<'ENDREGEXP';
                    330: /^([a-z]([a-z]|\d|\+|-|\.)*):(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uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
                    331: ENDREGEXP
                    332: 
                    333:     return <<ENDJS;
                    334: 
                    335: var regexp = $urlregexp;
                    336: 
                    337: function setExternal(extform,residx) {
                    338:     var title=extform.exttitle.value;
                    339:     if (!String.trim) {
                    340:         String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");};    }
                    341:     var url=extform.exturl.value;
                    342:     if (title == null || title.trim()=="") {
                    343:         alert("$lt{'titbl'}");
                    344:         extform.exttitle.focus();
                    345:         return;
                    346:     }
                    347:     if (regexp.test(url)) {
                    348:         url = escape(url);
1.7     ! raeburn   349:         title = escape(title);
1.1       raeburn   350:         if (residx > 0) {
                    351:             eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
                    352:         } else {
                    353:             eval("extform.importdetail.value=title+'='+url;extform.submit();");
                    354:         }
                    355:     } else {
                    356:         alert("$lt{'invurl'}");
                    357:         extform.exturl.focus();
                    358:         return;
                    359:     }
                    360: }
                    361: 
                    362: function editext(residx) {
                    363:     if (document.getElementById('uploadext'+residx)) {
                    364:         var curr = document.getElementById('uploadext'+residx).style.display;
                    365:         if (curr == 'none') {
                    366:             disp = 'block';
                    367:         } else {
                    368:             disp = 'none';
                    369:         }
                    370:         document.getElementById('uploadext'+residx).style.display=disp;
                    371:     }
                    372:     resize_scrollbox('contentscroll','1','1');
                    373:     return;
                    374: }
                    375: 
                    376: function extUrlPreview(caller) {
                    377:     if (document.getElementById(caller)) {
                    378:         var url = document.getElementById(caller).value;
                    379:         if (regexp.test(url)) {
                    380:             openMyModal(url,500,400,'yes');
                    381:         } else {
                    382:             alert("$lt{'invurl'}");
                    383:         }
                    384:     }
                    385: }
                    386: 
                    387: ENDJS
                    388: 
                    389: }
                    390: 
                    391: 1;

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