Annotation of loncom/interface/lonextresedit.pm, revision 1.11
1.1 raeburn 1: # The LearningOnline Network
2: # Documents
3: #
1.11 ! raeburn 4: # $Id: lonextresedit.pm,v 1.10 2016/06/06 17:40:48 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';
350: if ($residx) {
351: if ($caller eq 'direct') {
352: $fieldsetstyle = 'display: block;';
353: $action = '/adm/extresedit';
1.9 raeburn 354: if ($type eq 'tool') {
355: $legend = $lt{'ee'};
356: } else {
357: $legend = $lt{'te'};
358: }
359: $legend = '<legend>'.$legend.'</legend>';
1.1 raeburn 360: if ($symb) {
361: $hiddenelem = '<input type="hidden" name="symb" value="'.$symb.'" />';
362: } elsif ($supplementalflag) {
363: $hiddenelem = '<input type="hidden" name="suppurl" value="'.
364: &HTML::Entities::encode(&escape($orig_url),'<>&"').'" />'."\n".
365: '<input type="hidden" name="title" value="'.
366: &HTML::Entities::encode(&escape($orig_title),'<>&"').'" />';
367: }
1.9 raeburn 368: } else {
369: $link = '<a class="LC_docs_ext_edit" href="javascript:editext('."'$residx','$type'".');">'.$lt{'ed'}.'</a> '."\n";
1.1 raeburn 370: $size = 40;
1.3 raeburn 371: $active = '<input type="hidden" name="active" value="'.$tabid.'" />';
1.1 raeburn 372: }
1.9 raeburn 373: $formname = 'edit'.$type.'_'.$residx;
374: $fieldsetid = 'upload'.$type.$residx;
375: $urlid = $type.'url_'.$residx;
376: map { $toolattr{$_} .= '_'.$residx; } (keys(%toolattr));
1.1 raeburn 377: $srcclass = ' class="LC_nobreak"';
1.9 raeburn 378: if ($type eq 'ext') {
379: $extsrc = '<span class="LC_docs_ext_edit">'.$lt{'ul'}.' </span>';
380: $preview = ' <a class="LC_docs_ext_edit" href="javascript:extUrlPreview('."'$urlid'".');">'.$lt{'pr'}.'</a>';
381: }
1.1 raeburn 382: $title = '<span class="LC_docs_ext_edit">'.$lt{'ti'}.' </span>';
383: $save = $lt{'sv'};
384: } else {
1.9 raeburn 385: $link = $lt{'ex'};
386: if ($type eq 'tool') {
387: $link = $lt{'et'};
388: }
389: $link = '<a class="LC_menubuttons_link" href="javascript:toggleUpload('."'$toggle'".');">'.$link.'</a>'.$helpitem;
390: if ($type eq 'tool') {
391: $legend = $lt{'te'};
392: } else {
393: $legend = $lt{'ee'};
394: }
395: $legend = '<legend>'.$legend.'</legend>';
1.1 raeburn 396: $title = $lt{'ti'}.':<br />';
397: $residx = 0;
1.9 raeburn 398: if ($type eq 'ext') {
399: $orig_url = 'http://';
400: $orig_title = $lt{'ex'};
401: $extsrc = $lt{'ul'}.':<br />';
1.11 ! raeburn 402: $preview = '<input type="button" name="view" value="'.$lt{'pr'}.'" onclick="javascript:extUrlPreview('."'$urlid'".');"'.$disabled.' />';
1.9 raeburn 403: $save = $lt{'al'};
404: } else {
405: $orig_title = $lt{'et'};
406: $save = $lt{'at'};
407: $orig_url = "/adm/$cdom/$cnum/new/exttool";
408: }
1.1 raeburn 409: $pathitem .= '<br />';
410: }
1.9 raeburn 411: $formid = $formname;
412: if ($type eq 'ext') {
1.11 ! raeburn 413: $urlelem = '<input type="text" size="'.$size.'" name="exturl" id="'.$urlid.'" value="'.$orig_url.'"'.$disabled.' />';
1.9 raeburn 414: } else {
415: my $class = 'LC_nobreak';
416: if ($residx) {
417: $class = 'LC_docs_ext_edit LC_nobreak';
418: if ($orig_url =~ m{^/adm/$cdom/$cnum/(\d+)/exttools?$}) {
419: my $marker = $1;
420: my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
421: if ($toolhash{'id'}) {
422: if (ref($ltitools) eq 'HASH') {
423: if (keys(%{$ltitools})) {
424: if (ref($ltitools->{$toolhash{'id'}}) eq 'HASH') {
425: my $tooltitle = $ltitools->{$toolhash{'id'}}->{'title'};
426: my $icon = $ltitools->{$toolhash{'id'}}->{'image'};
427: my $image;
428: if ($icon) {
429: $image = '<img src="'.$icon.'" alt="'.$tooltitle.'" />';
430: }
431: $tooltarget = $toolhash{'target'};
432: if ($tooltarget eq 'window') {
433: $dimendivstyle = 'display:block';
434: $chkstate{'window'} = 'checked="checked" ';
435: } else {
436: $chkstate{'iframe'} = 'checked="checked" ';
437: }
438: $width = $toolhash{'width'};
439: $height = $toolhash{'height'};
1.10 raeburn 440: if (ref($ltitools->{$toolhash{'id'}}->{'crsconf'}) eq 'HASH') {
441: if ($ltitools->{$toolhash{'id'}}->{'crsconf'}->{'title'}) {
442: $crstitle = $toolhash{'crstitle'};
443: $titlestyle = 'display:inline';
444: }
445: if ($ltitools->{$toolhash{'id'}}->{'crsconf'}->{'label'}) {
446: $crslabel = $toolhash{'crslabel'};
447: $labelstyle = 'display:inline';
448: }
449: if ($ltitools->{$toolhash{'id'}}->{'crsconf'}->{'target'}) {
450: $dispdivstyle = 'display:block';
451: }
452: }
1.9 raeburn 453: $toolelem = '<span class="LC_nobreak">'.$image.' '.$tooltitle.'</span><br />';
454: }
455: }
456: }
457: }
458: }
459: } else {
460: $toolelem = '<span class="LC_docs_ext_edit">'."\n".
461: '<select name="exttoolid" id="LC_exttoolid" onchange="javascript:updateExttool(this,'.
1.11 ! raeburn 462: 'this.form,'."'$supplementalflag'".');"'.$disabled.'>'."\n".
1.9 raeburn 463: '<option value="" selected="selected">'.&mt('Select').'</option>';
464: my %bynum;
465: if (ref($ltitools) eq 'HASH') {
466: foreach my $id (keys(%{$ltitools})) {
467: if (ref($ltitools->{$id}) eq 'HASH') {
468: my $order = $ltitools->{$id}->{'order'};
469: $bynum{$order} = [$id,$ltitools->{$id}];
470: }
471: }
472: }
473: foreach my $item (sort { $a <=> $b } keys(%bynum)) {
474: if (ref($bynum{$item}) eq 'ARRAY') {
475: if (ref($bynum{$item}->[1]) eq 'HASH') {
476: my $tooltitle = $bynum{$item}->[1]->{'title'};
477: my $icon = $bynum{$item}->[1]->{'image'};
478: $toolelem .= '<option value="'.$bynum{$item}->[0].'">'.$tooltitle.'</option>';
479: }
480: }
481: }
1.10 raeburn 482: $toolelem .= '</select></span><br />';
483: $crslabel = $env{'course.'.$cdom.'_'.$cnum.'.internal.coursecode'};
484: $crstitle = $env{'course.'.$cdom.'_'.$cnum.'.description'};
1.9 raeburn 485: }
486: $toolelem .= '<div id="'.$toolattr{'dispdiv'}.'" style="'.$dispdivstyle.'">'.
487: '<span class="'.$class.'">'.&mt('Display target:').' '.
488: '<label><input type="radio" name="exttooltarget" value="iframe" '.$chkstate{'iframe'}.'onclick="updateTooldim(this.form,'.
1.11 ! raeburn 489: "'$toolattr{dimendiv}','$toolattr{dimenwidth}','$toolattr{dimenheight}'".');"'.$disabled.'>'.&mt('iframe').'</label>'.(' 'x2).
1.9 raeburn 490: '<label><input type="radio" name="exttooltarget" value="window" '.$chkstate{'window'}.'onclick="updateTooldim(this.form,'.
1.11 ! raeburn 491: "'$toolattr{dimendiv}','$toolattr{dimenwidth}','$toolattr{dimenheight}'".');"'.$disabled.'>'.&mt('window').'</label>'.
1.9 raeburn 492: '</span><div id="'.$toolattr{'dimendiv'}.'" style="'.$dimendivstyle.'">'.
493: '<span class="'.$class.'">'.
1.11 ! raeburn 494: &mt('Width').'<input type="text" id="'.$toolattr{'dimenwidth'}.'" name="exttoolwidth" value="'.$width.'"'.$disabled.'>'.(' 'x2).
! 495: &mt('Height').'<input type="text" id="'.$toolattr{'dimenheight'}.'" name="exttoolheight" value="'.$height.'"'.$disabled.'></span>'."\n".
1.10 raeburn 496: '</div></div>'.
497: '<div id="'.$toolattr{'crslabeldiv'}.'" style="'.$labelstyle.'">'.
498: '<span class="'.$class.'">'.&mt('Course label:').' '.
1.11 ! raeburn 499: '<input type="text" id="'.$toolattr{'crslabel'}.'" name="exttoollabel" value="'.$crslabel.'"'.$disabled.'><br />'.
1.10 raeburn 500: '</div>'.
501: '<div id="'.$toolattr{'crstitlediv'}.'" style="'.$titlestyle.'">'.
502: '<span class="'.$class.'">'.&mt('Course title:').' '.
1.11 ! raeburn 503: '<input type="text" id="'.$toolattr{'crstitle'}.'" name="exttooltitle" value="'.$crstitle.'"'.$disabled.'><br />'.
1.10 raeburn 504: '</div>';
1.9 raeburn 505: }
506: my $chooser = $toolelem;
507: if ($type eq 'ext') {
508: $chooser = "
509: <div>
510: <span$srcclass>
511: $extsrc
512: $urlelem
513: $preview
514: </span>
515: </div>
516: ";
517: }
1.3 raeburn 518: $form = <<ENDFORM;
1.9 raeburn 519: <form action="$action" method="post" name="$formname" id="$formid">
1.2 raeburn 520: <fieldset id="$fieldsetid" style="$fieldsetstyle">
1.1 raeburn 521: $legend
522: $active
1.9 raeburn 523: $chooser
524: <div>
1.1 raeburn 525: <span$srcclass>
526: $title
1.11 ! raeburn 527: <input type="text" size="$size" name="exttitle" value="$orig_title" $disabled />
1.1 raeburn 528: <input type="hidden" name="importdetail" value="" />
529: $pathitem
530: $hiddenelem
1.11 ! raeburn 531: <input type="button" value="$save" onclick="javascript:setExternal(this.form,'$residx','$type','$orig_url','$supplementalflag');"'.$disabled.' />
1.1 raeburn 532: </span>
1.9 raeburn 533: </div>
1.1 raeburn 534: </fieldset>
535: </form>
536: ENDFORM
1.3 raeburn 537: if (wantarray) {
538: return ($link,$form);
539: } else {
540: return $link.$form;
541: }
1.1 raeburn 542: }
543:
544: sub display_editor {
1.9 raeburn 545: my ($url,$folderpath,$symb,$idx,$type,$cdom,$cnum) = @_;
546: my ($residx,$supplementalflag,$title,$pathitem,$output,$js);
1.1 raeburn 547: if ($folderpath =~ /^supplemental/) {
548: $supplementalflag = 1;
549: $residx = $idx;
550: $title = &unescape($env{'form.title'});
551: $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($folderpath,'<>&"').'" />';
552: } elsif ($symb =~ /^uploaded/) {
553: (my $map,$residx,my $res) =
554: &Apache::lonnet::decode_symb($symb);
555: $title = &Apache::lonnet::gettitle($symb);
1.6 raeburn 556: my $path = &Apache::loncommon::symb_to_docspath($symb);
1.4 raeburn 557: $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($path,'<>&"').'" />';
1.1 raeburn 558: }
1.9 raeburn 559: my %ltitools;
560: if ($type eq 'tool') {
561: %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
562: }
563: $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
1.1 raeburn 564: my $args = { 'force_register' => $env{'form.register'} };
1.9 raeburn 565: my $description = 'External Resource Editor';
566: if ($type eq 'tool') {
567: $description = 'External Tool Editor';
568: }
569: return &Apache::loncommon::start_page($description,$js,$args).
1.1 raeburn 570: '<div class="LC_left_float">'.
1.9 raeburn 571: &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',
572: $symb,$type,$cdom,$cnum,\%ltitools).
1.1 raeburn 573: '</div>'.
574: &Apache::loncommon::end_page();
575: }
576:
577: sub extedit_javascript {
1.9 raeburn 578: my ($toolsref) = @_;
579: my $toolsjs;
580: if (ref($toolsref) eq 'HASH') {
581: my $num = scalar(keys(%{$toolsref}));
582: $toolsjs = " var ltitools = new Array($num);\n".
583: " var ltitoolsTarget = new Array($num);\n".
584: " var ltitoolsWidth = new Array($num);\n".
1.10 raeburn 585: " var ltitoolsHeight = new Array($num);\n".
586: " var ltitoolsDisplay = new Array($num);\n".
587: " var ltitoolsLabel = new Array($num);\n".
588: " var ltitoolsTitle = new Array($num);\n";
1.9 raeburn 589: my $i = 0;
590: foreach my $key (sort { $a <=> $b } keys(%{$toolsref})) {
591: if (ref($toolsref->{$key})) {
592: my $target = $toolsref->{$key}->{'target'};
593: my $width = $toolsref->{$key}->{'width'};
594: my $height = $toolsref->{$key}->{'height'};
595: $toolsjs .= ' ltitools['.$i.'] = '."'$key';\n".
596: ' ltitoolsTarget['.$i.'] = '."'$target';\n".
597: ' ltitoolsWidth['.$i.'] = '."'$width';\n".
598: ' ltitoolsHeight['.$i.'] = '."'$height';\n";
1.10 raeburn 599: my %courseconfig;
600: if (ref($toolsref->{$key}->{'crsconf'}) eq 'HASH') {
601: my $display = $toolsref->{$key}->{'crsconf'}->{'target'};
602: $toolsjs .= ' ltitoolsDisplay['.$i.'] = '."'$display';\n";
603: my $label = $toolsref->{$key}->{'crsconf'}->{'label'};
604: $toolsjs .= ' ltitoolsLabel['.$i.'] = '."'$label';\n";
605: my $title = $toolsref->{$key}->{'crsconf'}->{'title'};
606: $toolsjs .= ' ltitoolsTitle['.$i.'] = '."'$title';\n";
607: }
1.9 raeburn 608: $i++;
609: }
610: }
611: }
1.8 damieng 612: my %js_lt = &Apache::lonlocal::texthash(
1.1 raeburn 613: invurl => 'Invalid URL',
614: titbl => 'Title is blank',
1.9 raeburn 615: invtool => 'Please select an external tool',
1.1 raeburn 616: );
1.8 damieng 617: &js_escape(\%js_lt);
1.1 raeburn 618:
619: my $urlregexp = <<'ENDREGEXP';
620: /^([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
621: ENDREGEXP
622:
623: return <<ENDJS;
624:
625: var regexp = $urlregexp;
626:
1.10 raeburn 627: function setExternal(extform,residx,type,exttoolurl,supplementalflag) {
1.1 raeburn 628: var title=extform.exttitle.value;
629: if (!String.trim) {
630: String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");}; }
631: if (title == null || title.trim()=="") {
1.8 damieng 632: alert("$js_lt{'titbl'}");
1.1 raeburn 633: extform.exttitle.focus();
634: return;
635: }
1.9 raeburn 636: if (type == 'ext') {
637: var url=extform.exturl.value;
638: if (!regexp.test(url)) {
639: alert("$js_lt{'invurl'}");
640: extform.exturl.focus();
641: return;
642: } else {
643: url = escape(url);
644: title = escape(title);
645: if (residx > 0) {
646: eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
647: } else {
648: eval("extform.importdetail.value=title+'='+url;extform.submit();");
649: }
650: }
651: } else {
1.7 raeburn 652: title = escape(title);
1.9 raeburn 653: var info = exttoolurl;
654: if (residx == 0) {
655: var toolid = parseInt(extform.exttoolid.options[extform.exttoolid.selectedIndex].value);
656: if (isNaN(toolid)) {
657: alert("$js_lt{'invtool'}");
658: return;
659: }
660: info += ':'+toolid;
661: }
1.10 raeburn 662: var prefix = '';
663: if (supplementalflag == 1) {
664: prefix = 'supp';
665: }
666: var dispdiv = prefix+'tooldispdiv';
667: if (residx > 0) {
668: dispdiv += '_'+residx;
669: }
670: if (document.getElementById(dispdiv)) {
671: if (document.getElementById(dispdiv).style.display == 'block') {
672: if (extform.exttooltarget.length) {
673: for (var i=0; i<extform.exttooltarget.length; i++) {
674: if (extform.exttooltarget[i].checked) {
675: if (extform.exttooltarget[i].value == 'window') {
676: var width = extform.exttoolwidth.value;
677: width.trim();
678: var height = extform.exttoolheight.value;
679: height.trim();
680: info += ':window:'+width+':'+height;
681: } else {
682: info += ':iframe::';
683: }
684: }
1.9 raeburn 685: }
686: }
1.10 raeburn 687: } else {
688: info += ':::';
1.9 raeburn 689: }
1.10 raeburn 690: } else {
691: info += ':::';
692: }
693: var labelinput = prefix+'toolcrslabel';
694: var titleinput = prefix+'toolcrstitle';
695: if (residx > 0) {
696: labelinput += '_'+residx;
697: titleinput += '_'+residx;
698: }
699: if (document.getElementById(labelinput)) {
700: var crslabel = document.getElementById(labelinput).value;
701: crslabel.trim();
702: info += ':'+escape(crslabel);
703: } else {
704: info += ':';
705: }
706: if (document.getElementById(titleinput)) {
707: var crstitle = document.getElementById(titleinput).value;
708: crstitle.trim();
709: info += ':'+escape(crstitle);
710: } else {
711: info += ':';
1.9 raeburn 712: }
713: info=escape(info);
1.1 raeburn 714: if (residx > 0) {
1.9 raeburn 715: eval("extform.importdetail.value=title+'='+info+'='+residx;extform.submit();");
1.1 raeburn 716: } else {
1.9 raeburn 717: eval("extform.importdetail.value=title+'='+info;extform.submit();");
1.1 raeburn 718: }
719: }
720: }
721:
1.9 raeburn 722: function editext(residx,type) {
723: if (document.getElementById('upload'+type+residx)) {
724: var curr = document.getElementById('upload'+type+residx).style.display;
1.1 raeburn 725: if (curr == 'none') {
726: disp = 'block';
727: } else {
728: disp = 'none';
729: }
1.9 raeburn 730: document.getElementById('upload'+type+residx).style.display=disp;
1.1 raeburn 731: }
732: resize_scrollbox('contentscroll','1','1');
733: return;
734: }
735:
736: function extUrlPreview(caller) {
737: if (document.getElementById(caller)) {
738: var url = document.getElementById(caller).value;
739: if (regexp.test(url)) {
740: openMyModal(url,500,400,'yes');
741: } else {
1.8 damieng 742: alert("$js_lt{'invurl'}");
1.1 raeburn 743: }
744: }
745: }
746:
1.9 raeburn 747: function updateExttool(caller,form,supplementalflag) {
748: var prefix = '';
749: if (supplementalflag == 1) {
750: prefix = 'supp';
751: }
752: dispdiv = prefix+'tooldispdiv';
753: dimendiv = prefix+'tooldimendiv';
754: widthinput = prefix+'toolwidth';
755: heightinput = prefix+'toolheight';
1.10 raeburn 756: labeldiv = prefix+'toolcrslabeldiv';
757: titlediv = prefix+'toolcrstitlediv';
758: labelinput = prefix+'toolcrslabel';
759: titleinput = prefix+'toolcrstitle';
1.9 raeburn 760: if (document.getElementById(dispdiv)) {
761: var toolpick = caller.options[caller.selectedIndex].value;
762: $toolsjs
763: if (toolpick == '') {
764: if (document.getElementById(dispdiv)) {
765: document.getElementById(dispdiv).style.display = 'none';
766: }
767: if (document.getElementById(dimendiv)) {
768: document.getElementById(dimendiv).style.display = 'none';
769: }
1.10 raeburn 770: if (document.getElementById(labeldiv)) {
771: document.getElementById(labeldiv).style.display = 'none';
772: }
773: if (document.getElementById(titlediv)) {
774: document.getElementById(titlediv).style.display = 'none';
775: }
1.9 raeburn 776: } else {
777: if (ltitools.length > 0) {
778: for (var j=0; j<ltitools.length; j++) {
779: if (ltitools[j] == toolpick) {
1.10 raeburn 780: if (document.getElementById(dispdiv)) {
781: if (ltitoolsDisplay[j]) {
782: document.getElementById(dispdiv).style.display = 'block';
783: if (form.exttooltarget.length) {
784: for (var k=0; k<form.exttooltarget.length; k++) {
785: if (form.exttooltarget[k].value == ltitoolsTarget[j]) {
786: form.exttooltarget[k].checked = true;
787: break;
788: }
789: }
790: }
791: if (ltitoolsTarget[j] == 'window') {
792: dimen = 'block';
793: dimenwidth = ltitoolsWidth[j];
794: dimenheight = ltitoolsHeight[j];
795: } else {
796: dimen = 'none';
797: dimenwidth = '';
798: dimenheight = '';
799: }
800: if (document.getElementById(dimendiv)) {
801: document.getElementById(dimendiv).style.display = dimen;
802: }
803: if (document.getElementById(widthinput)) {
804: document.getElementById(widthinput).value = dimenwidth;
805: }
806: if (document.getElementById(heightinput)) {
807: document.getElementById(heightinput).value = dimenheight;
1.9 raeburn 808: }
809: }
810: }
1.10 raeburn 811: if (document.getElementById(labeldiv)) {
812: if (ltitoolsLabel[j]) {
813: document.getElementById(labeldiv).style.display = 'inline';
814: } else {
815: document.getElementById(labeldiv).style.display = 'none';
816: }
1.9 raeburn 817: }
1.10 raeburn 818: if (document.getElementById(titlediv)) {
819: if (ltitoolsTitle[j]) {
820: document.getElementById(titlediv).style.display = 'inline';
821: } else {
822: document.getElementById(titlediv).style.display = 'none';
823: }
1.9 raeburn 824: }
1.10 raeburn 825: break;
1.9 raeburn 826: }
827: }
828: }
829: }
830: }
831: }
832:
833: function updateTooldim(form,dimendiv,widthinput,heightinput) {
834: if (form.exttooltarget.length) {
835: for (var i=0; i<form.exttooltarget.length; i++) {
836: if (form.exttooltarget[i].checked) {
837: var dimen = 'none';
838: if (form.exttooltarget[i].value == 'window') {
839: dimen = 'block';
840: } else {
841: if (document.getElementById(widthinput)) {
842: document.getElementById(widthinput).value = '';
843: }
844: if (document.getElementById(heightinput)) {
845: document.getElementById(heightinput).value = '';
846: }
847: }
848: if (document.getElementById(dimendiv)) {
849: document.getElementById(dimendiv).style.display = dimen;
850: }
851: break;
852: }
853: }
854: }
855: }
856:
1.1 raeburn 857: ENDJS
858:
859: }
860:
861: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>