Annotation of loncom/interface/lonextresedit.pm, revision 1.14
1.1 raeburn 1: # The LearningOnline Network
2: # Documents
3: #
1.14 ! raeburn 4: # $Id: lonextresedit.pm,v 1.13 2016/11/29 16:03:03 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'};
1.9 raeburn 59: my ($supplementalflag,$updated,$output,$errormsg,$residx,$url,$title,
60: $symb,$type);
1.5 raeburn 61: if (($env{'form.folderpath'} =~ /^supplemental/) && ($env{'form.suppurl'})) {
62: $supplementalflag = 1;
1.9 raeburn 63: if (&unescape($env{'form.suppurl'}) =~ m{^/adm/$cdom/$cnum/\d+/exttools?$}) {
64: $type = 'tool';
65: }
1.7 raeburn 66: }
1.5 raeburn 67: if (($supplementalflag) || ($env{'form.symb'} =~ /^uploaded/)) {
1.7 raeburn 68: ($updated,$output,$errormsg,$residx,$url,$title,$symb) =
1.1 raeburn 69: &process_changes($supplementalflag,$cdom,$cnum,$chome);
70: if ($supplementalflag) {
1.9 raeburn 71: if ($url ne &unescape($env{'form.suppurl'})) {
1.1 raeburn 72: $env{'form.suppurl'} = $url;
73: }
74: if ($title ne $env{'form.title'}) {
75: $env{'form.title'} = $title;
76: }
1.5 raeburn 77: $env{'form.idx'} = $residx;
1.1 raeburn 78: } else {
79: if ($symb ne $env{'form.symb'}) {
80: $env{'form.symb'} = $symb;
81: }
1.9 raeburn 82: if ($url =~ m{/adm/$cdom/$cnum/\d+/exttools?$}) {
83: $type = 'tool';
84: }
1.1 raeburn 85: }
86: } else {
87: $errormsg = &mt('Information about external resource to edit is missing.');
88: }
89: if ($updated) {
1.9 raeburn 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);
1.1 raeburn 95: }
96: if ($errormsg) {
97: $errormsg = '<p class="LC_error">'.$errormsg.'</p>';
98: }
1.9 raeburn 99: my %ltitools;
100: if ($type eq 'tool') {
101: %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
102: }
1.1 raeburn 103: my $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
104: my $pathitem = '<input type="hidden" name="folderpath" value="'.
105: &HTML::Entities::encode($env{'form.folderpath'},'<>&"').'" />';
1.9 raeburn 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).
1.1 raeburn 111: '<div class="LC_left_float">'.
112: $output.
113: $errormsg.
114: &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,
1.9 raeburn 115: 'direct',$env{'form.symb'},$type,$cdom,$cnum,\%ltitools).
1.1 raeburn 116: '</div>'.&Apache::loncommon::end_page());
117: return OK;
118: }
119:
120: sub process_changes {
121: my ($supplementalflag,$cdom,$cnum,$chome) = @_;
1.9 raeburn 122: my ($folder,$container,$output,$errormsg,$updated,$symb,$oldidx,$oldurl,$type,
123: $oldtitle,$newidx,$newurl,$newtitle,$residx,$url,$title,$marker,$args);
1.1 raeburn 124: if ($env{'form.symb'}) {
125: $symb = $env{'form.symb'};
1.5 raeburn 126: (my $map,$oldidx,$oldurl)=&Apache::lonnet::decode_symb($symb);
1.1 raeburn 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'});
1.9 raeburn 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: }
1.1 raeburn 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;
1.9 raeburn 149: if ($oldurl =~ m{^/adm/$cdom/$cnum/\d+/exttools?$}) {
150: $type = 'tool';
1.5 raeburn 151: } else {
1.9 raeburn 152: $type = 'ext';
1.5 raeburn 153: }
154: }
155: $url = $oldurl;
156: $title = $oldtitle;
157: if ($env{'form.importdetail'}) {
158: ($newtitle,$newurl,$newidx) =
159: map {&unescape($_)} split(/\=/,$env{'form.importdetail'});
1.9 raeburn 160: if ($newurl =~ m{^(/adm/$cdom/$cnum/(\d+)/exttools?)\:?(.*)$}) {
161: $newurl = $1;
162: $marker = $2;
163: $args = $3;
164: }
1.5 raeburn 165: }
166: if ($supplementalflag) {
167: $residx = $newidx;
168: } else {
169: $residx = $oldidx;
170: }
1.1 raeburn 171: if ($folder && $container) {
172: if ($env{'form.importdetail'}) {
1.5 raeburn 173: my ($errtext,$fatal,$mismatchedid,@imports);
174: if (!$supplementalflag) {
175: if (($oldidx) && ($oldidx != $newidx)) {
176: $mismatchedid = 1;
177: }
1.1 raeburn 178: }
179: if ($mismatchedid) {
180: $errormsg = 'Wrong item identifier';
181: } elsif (($newtitle eq $oldtitle) && ($newurl eq $oldurl)) {
1.9 raeburn 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: }
1.1 raeburn 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);
1.9 raeburn 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: }
1.1 raeburn 209: $LONCAPA::map::resources[$residx] =
1.9 raeburn 210: join(':', ($savetitle,$saveurl,$ext,'normal','res'));
1.1 raeburn 211: my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1);
212: if ($errtext) {
213: $errormsg = &mt('Update failed: [_1].',$errtext);
214: } else {
215: $updated = 1;
1.5 raeburn 216: $title = $newtitle;
217: if ($newurl ne $oldurl) {
218: $url = $newurl;
1.9 raeburn 219: if ($ext eq 'true') {
220: $newurl =~ s{^http://}{};
221: $newurl = "ext/$newurl";
222: }
1.5 raeburn 223: }
1.1 raeburn 224: if (!$supplementalflag) {
225: if ($newurl ne $oldurl) {
1.5 raeburn 226: $symb = &Apache::lonnet::encode_symb($map,$residx,$newurl);
1.1 raeburn 227: } else {
228: $symb = $env{'form.symb'};
229: if ($symb) {
230: &Apache::lonnet::devalidate_title_cache($symb);
231: }
232: }
233: }
1.5 raeburn 234: my ($furl,$ferr) =
235: &Apache::lonuserstate::readmap("$cdom/$cnum");
1.1 raeburn 236: if ($ferr) {
237: $errormsg = &mt('Reload failed: [_1].',$ferr);
238: } else {
1.5 raeburn 239: unless ($supplementalflag) {
240: &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,$cdom.'_'.$cnum);
241: }
1.1 raeburn 242: }
243: }
244: }
245: }
246: } else {
247: $output = &mt('No change');
248: }
249: } else {
1.9 raeburn 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: }
1.1 raeburn 255: }
1.5 raeburn 256: return ($updated,$output,$errormsg,$residx,$url,$title,$symb);
1.1 raeburn 257: }
258:
1.9 raeburn 259: sub update_exttool {
260: my ($marker,$cdom,$cnum,$args) = @_;
261: my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
1.10 raeburn 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'});
1.9 raeburn 266: my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
1.10 raeburn 267: foreach my $item ('target','width','height','crslabel','crstitle') {
1.9 raeburn 268: $newhash{$item} =~ s/^\s+//;
269: $newhash{$item} =~ s/\s+$//;
1.10 raeburn 270: if (($item eq 'width') || ($item eq 'height')) {
1.9 raeburn 271: if ($newhash{'target'} eq 'iframe') {
272: $newhash{$item} = '';
273: }
274: }
275: if ($toolhash{$item} ne $newhash{$item}) {
276: if ($newhash{$item} eq '') {
1.10 raeburn 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: }
1.9 raeburn 285: } else {
286: $toolhash{$item} = $newhash{$item};
1.10 raeburn 287: $changed = 1;
1.9 raeburn 288: }
289: }
290: }
291: if ($changed) {
292: my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$cdom,$cnum);
293: unless ($putres eq 'ok') {
1.10 raeburn 294: $errormsg = &mt('Failed to save updated settings.').' '.&mt('Error: [_1].',$putres);
1.9 raeburn 295: }
296: }
1.10 raeburn 297: if (@deleted) {
298: &Apache::lonnet::del('exttool_'.$marker,\@deleted,$cdom,$cnum);
299: }
1.9 raeburn 300: return ($changed,$errormsg);
301: }
302:
1.1 raeburn 303: sub extedit_form {
1.9 raeburn 304: my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,
1.11 raeburn 305: $symb,$type,$cdom,$cnum,$ltitools,$disabled) = @_;
1.9 raeburn 306: if ($type ne 'tool') {
307: $type = 'ext';
308: }
1.1 raeburn 309: my %lt = &Apache::lonlocal::texthash(
310: ex => 'External Resource',
1.9 raeburn 311: et => 'External Tool',
1.1 raeburn 312: ed => 'Edit',
313: ee => 'External Resource Editor',
1.9 raeburn 314: te => 'External Tool Editor',
1.1 raeburn 315: pr => 'Preview',
316: sv => 'Save',
317: ul => 'URL',
318: ti => 'Title',
319: al => 'Add Link',
1.9 raeburn 320: at => 'Add Tool',
1.1 raeburn 321: );
322: my $tabid = 'aa';
323: my $size = 60;
324: if ($supplementalflag) {
325: $tabid = 'ee';
1.9 raeburn 326: }
327: my ($formname,$formid,$toggle,$fieldsetid,$urlid,$dispdivstyle,$dimendivstyle,
1.10 raeburn 328: $labelstyle,$titlestyle,$legend,$urlelem,$toolelem,%toolattr);
1.9 raeburn 329: $formname = 'new'.$type;
330: $toggle = $type;
331: $fieldsetid = 'upload'.$type.'form';
332: $urlid = $type.'url';
1.10 raeburn 333: map { $toolattr{$_} = $type.$_; } ('dispdiv','dimendiv','dimenwidth','dimenheight',
334: 'crstitlediv','crslabeldiv','crstitle','crslabel');
1.9 raeburn 335: $dispdivstyle = 'display:none';
336: $dimendivstyle = 'display:none';
1.10 raeburn 337: $labelstyle = 'display:none';
338: $titlestyle = 'display:none';
1.9 raeburn 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));
1.1 raeburn 345: }
1.10 raeburn 346: my ($link,$legend,$active,$srcclass,$extsrc,$preview,$title,$save,$crstitle,$crslabel,
1.9 raeburn 347: $fieldsetstyle,$action,$hiddenelem,$form,$width,$height,$tooltarget,%chkstate);
1.1 raeburn 348: $fieldsetstyle = 'display: none;';
349: $action = '/adm/coursedocs';
1.14 ! raeburn 350: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https':'http');
1.1 raeburn 351: if ($residx) {
352: if ($caller eq 'direct') {
353: $fieldsetstyle = 'display: block;';
354: $action = '/adm/extresedit';
1.9 raeburn 355: if ($type eq 'tool') {
356: $legend = $lt{'ee'};
357: } else {
358: $legend = $lt{'te'};
359: }
360: $legend = '<legend>'.$legend.'</legend>';
1.1 raeburn 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: }
1.9 raeburn 369: } else {
370: $link = '<a class="LC_docs_ext_edit" href="javascript:editext('."'$residx','$type'".');">'.$lt{'ed'}.'</a> '."\n";
1.1 raeburn 371: $size = 40;
1.3 raeburn 372: $active = '<input type="hidden" name="active" value="'.$tabid.'" />';
1.1 raeburn 373: }
1.9 raeburn 374: $formname = 'edit'.$type.'_'.$residx;
375: $fieldsetid = 'upload'.$type.$residx;
376: $urlid = $type.'url_'.$residx;
377: map { $toolattr{$_} .= '_'.$residx; } (keys(%toolattr));
1.1 raeburn 378: $srcclass = ' class="LC_nobreak"';
1.9 raeburn 379: if ($type eq 'ext') {
380: $extsrc = '<span class="LC_docs_ext_edit">'.$lt{'ul'}.' </span>';
1.14 ! raeburn 381: $preview = ' <a class="LC_docs_ext_edit" href="javascript:extUrlPreview('."'$urlid','$protocol'".');">'.$lt{'pr'}.'</a>';
1.9 raeburn 382: }
1.1 raeburn 383: $title = '<span class="LC_docs_ext_edit">'.$lt{'ti'}.' </span>';
384: $save = $lt{'sv'};
385: } else {
1.9 raeburn 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>';
1.1 raeburn 397: $title = $lt{'ti'}.':<br />';
398: $residx = 0;
1.9 raeburn 399: if ($type eq 'ext') {
400: $orig_url = 'http://';
401: $orig_title = $lt{'ex'};
402: $extsrc = $lt{'ul'}.':<br />';
1.14 ! raeburn 403: $preview = '<input type="button" name="view" value="'.$lt{'pr'}.'" onclick="javascript:extUrlPreview('."'$urlid','$protocol'".');"'.$disabled.' />';
1.9 raeburn 404: $save = $lt{'al'};
405: } else {
406: $orig_title = $lt{'et'};
407: $save = $lt{'at'};
408: $orig_url = "/adm/$cdom/$cnum/new/exttool";
409: }
1.1 raeburn 410: $pathitem .= '<br />';
411: }
1.9 raeburn 412: $formid = $formname;
413: if ($type eq 'ext') {
1.11 raeburn 414: $urlelem = '<input type="text" size="'.$size.'" name="exturl" id="'.$urlid.'" value="'.$orig_url.'"'.$disabled.' />';
1.9 raeburn 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'};
1.10 raeburn 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: }
1.9 raeburn 454: $toolelem = '<span class="LC_nobreak">'.$image.' '.$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,'.
1.11 raeburn 463: 'this.form,'."'$supplementalflag'".');"'.$disabled.'>'."\n".
1.9 raeburn 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: }
1.10 raeburn 483: $toolelem .= '</select></span><br />';
484: $crslabel = $env{'course.'.$cdom.'_'.$cnum.'.internal.coursecode'};
485: $crstitle = $env{'course.'.$cdom.'_'.$cnum.'.description'};
1.9 raeburn 486: }
487: $toolelem .= '<div id="'.$toolattr{'dispdiv'}.'" style="'.$dispdivstyle.'">'.
488: '<span class="'.$class.'">'.&mt('Display target:').' '.
489: '<label><input type="radio" name="exttooltarget" value="iframe" '.$chkstate{'iframe'}.'onclick="updateTooldim(this.form,'.
1.11 raeburn 490: "'$toolattr{dimendiv}','$toolattr{dimenwidth}','$toolattr{dimenheight}'".');"'.$disabled.'>'.&mt('iframe').'</label>'.(' 'x2).
1.9 raeburn 491: '<label><input type="radio" name="exttooltarget" value="window" '.$chkstate{'window'}.'onclick="updateTooldim(this.form,'.
1.11 raeburn 492: "'$toolattr{dimendiv}','$toolattr{dimenwidth}','$toolattr{dimenheight}'".');"'.$disabled.'>'.&mt('window').'</label>'.
1.9 raeburn 493: '</span><div id="'.$toolattr{'dimendiv'}.'" style="'.$dimendivstyle.'">'.
494: '<span class="'.$class.'">'.
1.11 raeburn 495: &mt('Width').'<input type="text" id="'.$toolattr{'dimenwidth'}.'" name="exttoolwidth" value="'.$width.'"'.$disabled.'>'.(' 'x2).
496: &mt('Height').'<input type="text" id="'.$toolattr{'dimenheight'}.'" name="exttoolheight" value="'.$height.'"'.$disabled.'></span>'."\n".
1.10 raeburn 497: '</div></div>'.
498: '<div id="'.$toolattr{'crslabeldiv'}.'" style="'.$labelstyle.'">'.
499: '<span class="'.$class.'">'.&mt('Course label:').' '.
1.11 raeburn 500: '<input type="text" id="'.$toolattr{'crslabel'}.'" name="exttoollabel" value="'.$crslabel.'"'.$disabled.'><br />'.
1.10 raeburn 501: '</div>'.
502: '<div id="'.$toolattr{'crstitlediv'}.'" style="'.$titlestyle.'">'.
503: '<span class="'.$class.'">'.&mt('Course title:').' '.
1.11 raeburn 504: '<input type="text" id="'.$toolattr{'crstitle'}.'" name="exttooltitle" value="'.$crstitle.'"'.$disabled.'><br />'.
1.10 raeburn 505: '</div>';
1.9 raeburn 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: }
1.3 raeburn 519: $form = <<ENDFORM;
1.9 raeburn 520: <form action="$action" method="post" name="$formname" id="$formid">
1.2 raeburn 521: <fieldset id="$fieldsetid" style="$fieldsetstyle">
1.1 raeburn 522: $legend
523: $active
1.9 raeburn 524: $chooser
525: <div>
1.1 raeburn 526: <span$srcclass>
527: $title
1.11 raeburn 528: <input type="text" size="$size" name="exttitle" value="$orig_title" $disabled />
1.1 raeburn 529: <input type="hidden" name="importdetail" value="" />
530: $pathitem
531: $hiddenelem
1.13 raeburn 532: <input type="button" value="$save" onclick="javascript:setExternal(this.form,'$residx','$type','$orig_url','$supplementalflag');" $disabled />
1.1 raeburn 533: </span>
1.9 raeburn 534: </div>
1.1 raeburn 535: </fieldset>
536: </form>
537: ENDFORM
1.3 raeburn 538: if (wantarray) {
539: return ($link,$form);
540: } else {
541: return $link.$form;
542: }
1.1 raeburn 543: }
544:
545: sub display_editor {
1.9 raeburn 546: my ($url,$folderpath,$symb,$idx,$type,$cdom,$cnum) = @_;
1.12 raeburn 547: my ($residx,$supplementalflag,$title,$pathitem,$output,$js,$navmap);
1.1 raeburn 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);
1.12 raeburn 557: my $path = &Apache::loncommon::symb_to_docspath($symb,\$navmap);
1.4 raeburn 558: $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($path,'<>&"').'" />';
1.1 raeburn 559: }
1.9 raeburn 560: my %ltitools;
561: if ($type eq 'tool') {
562: %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
563: }
564: $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
1.1 raeburn 565: my $args = { 'force_register' => $env{'form.register'} };
1.9 raeburn 566: my $description = 'External Resource Editor';
567: if ($type eq 'tool') {
568: $description = 'External Tool Editor';
569: }
570: return &Apache::loncommon::start_page($description,$js,$args).
1.1 raeburn 571: '<div class="LC_left_float">'.
1.9 raeburn 572: &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',
573: $symb,$type,$cdom,$cnum,\%ltitools).
1.1 raeburn 574: '</div>'.
575: &Apache::loncommon::end_page();
576: }
577:
578: sub extedit_javascript {
1.9 raeburn 579: my ($toolsref) = @_;
580: my $toolsjs;
581: if (ref($toolsref) eq 'HASH') {
582: my $num = scalar(keys(%{$toolsref}));
583: $toolsjs = " var ltitools = new Array($num);\n".
584: " var ltitoolsTarget = new Array($num);\n".
585: " var ltitoolsWidth = new Array($num);\n".
1.10 raeburn 586: " var ltitoolsHeight = new Array($num);\n".
587: " var ltitoolsDisplay = new Array($num);\n".
588: " var ltitoolsLabel = new Array($num);\n".
589: " var ltitoolsTitle = new Array($num);\n";
1.9 raeburn 590: my $i = 0;
591: foreach my $key (sort { $a <=> $b } keys(%{$toolsref})) {
592: if (ref($toolsref->{$key})) {
593: my $target = $toolsref->{$key}->{'target'};
594: my $width = $toolsref->{$key}->{'width'};
595: my $height = $toolsref->{$key}->{'height'};
596: $toolsjs .= ' ltitools['.$i.'] = '."'$key';\n".
597: ' ltitoolsTarget['.$i.'] = '."'$target';\n".
598: ' ltitoolsWidth['.$i.'] = '."'$width';\n".
599: ' ltitoolsHeight['.$i.'] = '."'$height';\n";
1.10 raeburn 600: my %courseconfig;
601: if (ref($toolsref->{$key}->{'crsconf'}) eq 'HASH') {
602: my $display = $toolsref->{$key}->{'crsconf'}->{'target'};
603: $toolsjs .= ' ltitoolsDisplay['.$i.'] = '."'$display';\n";
604: my $label = $toolsref->{$key}->{'crsconf'}->{'label'};
605: $toolsjs .= ' ltitoolsLabel['.$i.'] = '."'$label';\n";
606: my $title = $toolsref->{$key}->{'crsconf'}->{'title'};
607: $toolsjs .= ' ltitoolsTitle['.$i.'] = '."'$title';\n";
608: }
1.9 raeburn 609: $i++;
610: }
611: }
612: }
1.8 damieng 613: my %js_lt = &Apache::lonlocal::texthash(
1.1 raeburn 614: invurl => 'Invalid URL',
615: titbl => 'Title is blank',
1.9 raeburn 616: invtool => 'Please select an external tool',
1.1 raeburn 617: );
1.8 damieng 618: &js_escape(\%js_lt);
1.1 raeburn 619:
620: my $urlregexp = <<'ENDREGEXP';
621: /^([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
622: ENDREGEXP
623:
624: return <<ENDJS;
625:
626: var regexp = $urlregexp;
627:
1.10 raeburn 628: function setExternal(extform,residx,type,exttoolurl,supplementalflag) {
1.1 raeburn 629: var title=extform.exttitle.value;
630: if (!String.trim) {
631: String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");}; }
632: if (title == null || title.trim()=="") {
1.8 damieng 633: alert("$js_lt{'titbl'}");
1.1 raeburn 634: extform.exttitle.focus();
635: return;
636: }
1.9 raeburn 637: if (type == 'ext') {
638: var url=extform.exturl.value;
639: if (!regexp.test(url)) {
640: alert("$js_lt{'invurl'}");
641: extform.exturl.focus();
642: return;
643: } else {
644: url = escape(url);
645: title = escape(title);
646: if (residx > 0) {
647: eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
648: } else {
649: eval("extform.importdetail.value=title+'='+url;extform.submit();");
650: }
651: }
652: } else {
1.7 raeburn 653: title = escape(title);
1.9 raeburn 654: var info = exttoolurl;
655: if (residx == 0) {
656: var toolid = parseInt(extform.exttoolid.options[extform.exttoolid.selectedIndex].value);
657: if (isNaN(toolid)) {
658: alert("$js_lt{'invtool'}");
659: return;
660: }
661: info += ':'+toolid;
662: }
1.10 raeburn 663: var prefix = '';
664: if (supplementalflag == 1) {
665: prefix = 'supp';
666: }
667: var dispdiv = prefix+'tooldispdiv';
668: if (residx > 0) {
669: dispdiv += '_'+residx;
670: }
671: if (document.getElementById(dispdiv)) {
672: if (document.getElementById(dispdiv).style.display == 'block') {
673: if (extform.exttooltarget.length) {
674: for (var i=0; i<extform.exttooltarget.length; i++) {
675: if (extform.exttooltarget[i].checked) {
676: if (extform.exttooltarget[i].value == 'window') {
677: var width = extform.exttoolwidth.value;
678: width.trim();
679: var height = extform.exttoolheight.value;
680: height.trim();
681: info += ':window:'+width+':'+height;
682: } else {
683: info += ':iframe::';
684: }
685: }
1.9 raeburn 686: }
687: }
1.10 raeburn 688: } else {
689: info += ':::';
1.9 raeburn 690: }
1.10 raeburn 691: } else {
692: info += ':::';
693: }
694: var labelinput = prefix+'toolcrslabel';
695: var titleinput = prefix+'toolcrstitle';
696: if (residx > 0) {
697: labelinput += '_'+residx;
698: titleinput += '_'+residx;
699: }
700: if (document.getElementById(labelinput)) {
701: var crslabel = document.getElementById(labelinput).value;
702: crslabel.trim();
703: info += ':'+escape(crslabel);
704: } else {
705: info += ':';
706: }
707: if (document.getElementById(titleinput)) {
708: var crstitle = document.getElementById(titleinput).value;
709: crstitle.trim();
710: info += ':'+escape(crstitle);
711: } else {
712: info += ':';
1.9 raeburn 713: }
714: info=escape(info);
1.1 raeburn 715: if (residx > 0) {
1.9 raeburn 716: eval("extform.importdetail.value=title+'='+info+'='+residx;extform.submit();");
1.1 raeburn 717: } else {
1.9 raeburn 718: eval("extform.importdetail.value=title+'='+info;extform.submit();");
1.1 raeburn 719: }
720: }
721: }
722:
1.9 raeburn 723: function editext(residx,type) {
724: if (document.getElementById('upload'+type+residx)) {
725: var curr = document.getElementById('upload'+type+residx).style.display;
1.1 raeburn 726: if (curr == 'none') {
727: disp = 'block';
728: } else {
729: disp = 'none';
730: }
1.9 raeburn 731: document.getElementById('upload'+type+residx).style.display=disp;
1.1 raeburn 732: }
733: resize_scrollbox('contentscroll','1','1');
734: return;
735: }
736:
1.14 ! raeburn 737: function extUrlPreview(caller,protocol) {
1.1 raeburn 738: if (document.getElementById(caller)) {
739: var url = document.getElementById(caller).value;
740: if (regexp.test(url)) {
1.14 ! raeburn 741: var http_regex = /^http\:\/\//gi;
! 742: if ((protocol == 'https') && (http_regex.test(url))) {
! 743: window.open(url,"externalpreview","height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1");
! 744: } else {
! 745: openMyModal(url,500,400,'yes');
! 746: }
1.1 raeburn 747: } else {
1.8 damieng 748: alert("$js_lt{'invurl'}");
1.1 raeburn 749: }
750: }
751: }
752:
1.9 raeburn 753: function updateExttool(caller,form,supplementalflag) {
754: var prefix = '';
755: if (supplementalflag == 1) {
756: prefix = 'supp';
757: }
758: dispdiv = prefix+'tooldispdiv';
759: dimendiv = prefix+'tooldimendiv';
760: widthinput = prefix+'toolwidth';
761: heightinput = prefix+'toolheight';
1.10 raeburn 762: labeldiv = prefix+'toolcrslabeldiv';
763: titlediv = prefix+'toolcrstitlediv';
764: labelinput = prefix+'toolcrslabel';
765: titleinput = prefix+'toolcrstitle';
1.9 raeburn 766: if (document.getElementById(dispdiv)) {
767: var toolpick = caller.options[caller.selectedIndex].value;
768: $toolsjs
769: if (toolpick == '') {
770: if (document.getElementById(dispdiv)) {
771: document.getElementById(dispdiv).style.display = 'none';
772: }
773: if (document.getElementById(dimendiv)) {
774: document.getElementById(dimendiv).style.display = 'none';
775: }
1.10 raeburn 776: if (document.getElementById(labeldiv)) {
777: document.getElementById(labeldiv).style.display = 'none';
778: }
779: if (document.getElementById(titlediv)) {
780: document.getElementById(titlediv).style.display = 'none';
781: }
1.9 raeburn 782: } else {
783: if (ltitools.length > 0) {
784: for (var j=0; j<ltitools.length; j++) {
785: if (ltitools[j] == toolpick) {
1.10 raeburn 786: if (document.getElementById(dispdiv)) {
787: if (ltitoolsDisplay[j]) {
788: document.getElementById(dispdiv).style.display = 'block';
789: if (form.exttooltarget.length) {
790: for (var k=0; k<form.exttooltarget.length; k++) {
791: if (form.exttooltarget[k].value == ltitoolsTarget[j]) {
792: form.exttooltarget[k].checked = true;
793: break;
794: }
795: }
796: }
797: if (ltitoolsTarget[j] == 'window') {
798: dimen = 'block';
799: dimenwidth = ltitoolsWidth[j];
800: dimenheight = ltitoolsHeight[j];
801: } else {
802: dimen = 'none';
803: dimenwidth = '';
804: dimenheight = '';
805: }
806: if (document.getElementById(dimendiv)) {
807: document.getElementById(dimendiv).style.display = dimen;
808: }
809: if (document.getElementById(widthinput)) {
810: document.getElementById(widthinput).value = dimenwidth;
811: }
812: if (document.getElementById(heightinput)) {
813: document.getElementById(heightinput).value = dimenheight;
1.9 raeburn 814: }
815: }
816: }
1.10 raeburn 817: if (document.getElementById(labeldiv)) {
818: if (ltitoolsLabel[j]) {
819: document.getElementById(labeldiv).style.display = 'inline';
820: } else {
821: document.getElementById(labeldiv).style.display = 'none';
822: }
1.9 raeburn 823: }
1.10 raeburn 824: if (document.getElementById(titlediv)) {
825: if (ltitoolsTitle[j]) {
826: document.getElementById(titlediv).style.display = 'inline';
827: } else {
828: document.getElementById(titlediv).style.display = 'none';
829: }
1.9 raeburn 830: }
1.10 raeburn 831: break;
1.9 raeburn 832: }
833: }
834: }
835: }
836: }
837: }
838:
839: function updateTooldim(form,dimendiv,widthinput,heightinput) {
840: if (form.exttooltarget.length) {
841: for (var i=0; i<form.exttooltarget.length; i++) {
842: if (form.exttooltarget[i].checked) {
843: var dimen = 'none';
844: if (form.exttooltarget[i].value == 'window') {
845: dimen = 'block';
846: } else {
847: if (document.getElementById(widthinput)) {
848: document.getElementById(widthinput).value = '';
849: }
850: if (document.getElementById(heightinput)) {
851: document.getElementById(heightinput).value = '';
852: }
853: }
854: if (document.getElementById(dimendiv)) {
855: document.getElementById(dimendiv).style.display = dimen;
856: }
857: break;
858: }
859: }
860: }
861: }
862:
1.1 raeburn 863: ENDJS
864:
865: }
866:
867: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>