Annotation of loncom/interface/lonextresedit.pm, revision 1.15
1.1 raeburn 1: # The LearningOnline Network
2: # Documents
3: #
1.15 ! raeburn 4: # $Id: lonextresedit.pm,v 1.14 2017/02/19 00:15:51 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.15 ! raeburn 546: my ($url,$folderpath,$symb,$idx,$type,$cdom,$cnum,$hostname) = @_;
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.15 ! raeburn 566: if ($hostname) {
! 567: $args->{'hostname'} = $hostname;
! 568: }
1.9 raeburn 569: my $description = 'External Resource Editor';
570: if ($type eq 'tool') {
571: $description = 'External Tool Editor';
572: }
573: return &Apache::loncommon::start_page($description,$js,$args).
1.1 raeburn 574: '<div class="LC_left_float">'.
1.9 raeburn 575: &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',
576: $symb,$type,$cdom,$cnum,\%ltitools).
1.1 raeburn 577: '</div>'.
578: &Apache::loncommon::end_page();
579: }
580:
581: sub extedit_javascript {
1.9 raeburn 582: my ($toolsref) = @_;
583: my $toolsjs;
584: if (ref($toolsref) eq 'HASH') {
585: my $num = scalar(keys(%{$toolsref}));
586: $toolsjs = " var ltitools = new Array($num);\n".
587: " var ltitoolsTarget = new Array($num);\n".
588: " var ltitoolsWidth = new Array($num);\n".
1.10 raeburn 589: " var ltitoolsHeight = new Array($num);\n".
590: " var ltitoolsDisplay = new Array($num);\n".
591: " var ltitoolsLabel = new Array($num);\n".
592: " var ltitoolsTitle = new Array($num);\n";
1.9 raeburn 593: my $i = 0;
594: foreach my $key (sort { $a <=> $b } keys(%{$toolsref})) {
595: if (ref($toolsref->{$key})) {
596: my $target = $toolsref->{$key}->{'target'};
597: my $width = $toolsref->{$key}->{'width'};
598: my $height = $toolsref->{$key}->{'height'};
599: $toolsjs .= ' ltitools['.$i.'] = '."'$key';\n".
600: ' ltitoolsTarget['.$i.'] = '."'$target';\n".
601: ' ltitoolsWidth['.$i.'] = '."'$width';\n".
602: ' ltitoolsHeight['.$i.'] = '."'$height';\n";
1.10 raeburn 603: my %courseconfig;
604: if (ref($toolsref->{$key}->{'crsconf'}) eq 'HASH') {
605: my $display = $toolsref->{$key}->{'crsconf'}->{'target'};
606: $toolsjs .= ' ltitoolsDisplay['.$i.'] = '."'$display';\n";
607: my $label = $toolsref->{$key}->{'crsconf'}->{'label'};
608: $toolsjs .= ' ltitoolsLabel['.$i.'] = '."'$label';\n";
609: my $title = $toolsref->{$key}->{'crsconf'}->{'title'};
610: $toolsjs .= ' ltitoolsTitle['.$i.'] = '."'$title';\n";
611: }
1.9 raeburn 612: $i++;
613: }
614: }
615: }
1.8 damieng 616: my %js_lt = &Apache::lonlocal::texthash(
1.1 raeburn 617: invurl => 'Invalid URL',
618: titbl => 'Title is blank',
1.9 raeburn 619: invtool => 'Please select an external tool',
1.1 raeburn 620: );
1.8 damieng 621: &js_escape(\%js_lt);
1.1 raeburn 622:
623: my $urlregexp = <<'ENDREGEXP';
624: /^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
625: ENDREGEXP
626:
627: return <<ENDJS;
628:
629: var regexp = $urlregexp;
630:
1.10 raeburn 631: function setExternal(extform,residx,type,exttoolurl,supplementalflag) {
1.1 raeburn 632: var title=extform.exttitle.value;
633: if (!String.trim) {
634: String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");}; }
635: if (title == null || title.trim()=="") {
1.8 damieng 636: alert("$js_lt{'titbl'}");
1.1 raeburn 637: extform.exttitle.focus();
638: return;
639: }
1.9 raeburn 640: if (type == 'ext') {
641: var url=extform.exturl.value;
642: if (!regexp.test(url)) {
643: alert("$js_lt{'invurl'}");
644: extform.exturl.focus();
645: return;
646: } else {
647: url = escape(url);
648: title = escape(title);
649: if (residx > 0) {
650: eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
651: } else {
652: eval("extform.importdetail.value=title+'='+url;extform.submit();");
653: }
654: }
655: } else {
1.7 raeburn 656: title = escape(title);
1.9 raeburn 657: var info = exttoolurl;
658: if (residx == 0) {
659: var toolid = parseInt(extform.exttoolid.options[extform.exttoolid.selectedIndex].value);
660: if (isNaN(toolid)) {
661: alert("$js_lt{'invtool'}");
662: return;
663: }
664: info += ':'+toolid;
665: }
1.10 raeburn 666: var prefix = '';
667: if (supplementalflag == 1) {
668: prefix = 'supp';
669: }
670: var dispdiv = prefix+'tooldispdiv';
671: if (residx > 0) {
672: dispdiv += '_'+residx;
673: }
674: if (document.getElementById(dispdiv)) {
675: if (document.getElementById(dispdiv).style.display == 'block') {
676: if (extform.exttooltarget.length) {
677: for (var i=0; i<extform.exttooltarget.length; i++) {
678: if (extform.exttooltarget[i].checked) {
679: if (extform.exttooltarget[i].value == 'window') {
680: var width = extform.exttoolwidth.value;
681: width.trim();
682: var height = extform.exttoolheight.value;
683: height.trim();
684: info += ':window:'+width+':'+height;
685: } else {
686: info += ':iframe::';
687: }
688: }
1.9 raeburn 689: }
690: }
1.10 raeburn 691: } else {
692: info += ':::';
1.9 raeburn 693: }
1.10 raeburn 694: } else {
695: info += ':::';
696: }
697: var labelinput = prefix+'toolcrslabel';
698: var titleinput = prefix+'toolcrstitle';
699: if (residx > 0) {
700: labelinput += '_'+residx;
701: titleinput += '_'+residx;
702: }
703: if (document.getElementById(labelinput)) {
704: var crslabel = document.getElementById(labelinput).value;
705: crslabel.trim();
706: info += ':'+escape(crslabel);
707: } else {
708: info += ':';
709: }
710: if (document.getElementById(titleinput)) {
711: var crstitle = document.getElementById(titleinput).value;
712: crstitle.trim();
713: info += ':'+escape(crstitle);
714: } else {
715: info += ':';
1.9 raeburn 716: }
717: info=escape(info);
1.1 raeburn 718: if (residx > 0) {
1.9 raeburn 719: eval("extform.importdetail.value=title+'='+info+'='+residx;extform.submit();");
1.1 raeburn 720: } else {
1.9 raeburn 721: eval("extform.importdetail.value=title+'='+info;extform.submit();");
1.1 raeburn 722: }
723: }
724: }
725:
1.9 raeburn 726: function editext(residx,type) {
727: if (document.getElementById('upload'+type+residx)) {
728: var curr = document.getElementById('upload'+type+residx).style.display;
1.1 raeburn 729: if (curr == 'none') {
730: disp = 'block';
731: } else {
732: disp = 'none';
733: }
1.9 raeburn 734: document.getElementById('upload'+type+residx).style.display=disp;
1.1 raeburn 735: }
736: resize_scrollbox('contentscroll','1','1');
737: return;
738: }
739:
1.14 raeburn 740: function extUrlPreview(caller,protocol) {
1.1 raeburn 741: if (document.getElementById(caller)) {
742: var url = document.getElementById(caller).value;
743: if (regexp.test(url)) {
1.14 raeburn 744: var http_regex = /^http\:\/\//gi;
745: if ((protocol == 'https') && (http_regex.test(url))) {
746: window.open(url,"externalpreview","height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1");
747: } else {
748: openMyModal(url,500,400,'yes');
749: }
1.1 raeburn 750: } else {
1.8 damieng 751: alert("$js_lt{'invurl'}");
1.1 raeburn 752: }
753: }
754: }
755:
1.9 raeburn 756: function updateExttool(caller,form,supplementalflag) {
757: var prefix = '';
758: if (supplementalflag == 1) {
759: prefix = 'supp';
760: }
761: dispdiv = prefix+'tooldispdiv';
762: dimendiv = prefix+'tooldimendiv';
763: widthinput = prefix+'toolwidth';
764: heightinput = prefix+'toolheight';
1.10 raeburn 765: labeldiv = prefix+'toolcrslabeldiv';
766: titlediv = prefix+'toolcrstitlediv';
767: labelinput = prefix+'toolcrslabel';
768: titleinput = prefix+'toolcrstitle';
1.9 raeburn 769: if (document.getElementById(dispdiv)) {
770: var toolpick = caller.options[caller.selectedIndex].value;
771: $toolsjs
772: if (toolpick == '') {
773: if (document.getElementById(dispdiv)) {
774: document.getElementById(dispdiv).style.display = 'none';
775: }
776: if (document.getElementById(dimendiv)) {
777: document.getElementById(dimendiv).style.display = 'none';
778: }
1.10 raeburn 779: if (document.getElementById(labeldiv)) {
780: document.getElementById(labeldiv).style.display = 'none';
781: }
782: if (document.getElementById(titlediv)) {
783: document.getElementById(titlediv).style.display = 'none';
784: }
1.9 raeburn 785: } else {
786: if (ltitools.length > 0) {
787: for (var j=0; j<ltitools.length; j++) {
788: if (ltitools[j] == toolpick) {
1.10 raeburn 789: if (document.getElementById(dispdiv)) {
790: if (ltitoolsDisplay[j]) {
791: document.getElementById(dispdiv).style.display = 'block';
792: if (form.exttooltarget.length) {
793: for (var k=0; k<form.exttooltarget.length; k++) {
794: if (form.exttooltarget[k].value == ltitoolsTarget[j]) {
795: form.exttooltarget[k].checked = true;
796: break;
797: }
798: }
799: }
800: if (ltitoolsTarget[j] == 'window') {
801: dimen = 'block';
802: dimenwidth = ltitoolsWidth[j];
803: dimenheight = ltitoolsHeight[j];
804: } else {
805: dimen = 'none';
806: dimenwidth = '';
807: dimenheight = '';
808: }
809: if (document.getElementById(dimendiv)) {
810: document.getElementById(dimendiv).style.display = dimen;
811: }
812: if (document.getElementById(widthinput)) {
813: document.getElementById(widthinput).value = dimenwidth;
814: }
815: if (document.getElementById(heightinput)) {
816: document.getElementById(heightinput).value = dimenheight;
1.9 raeburn 817: }
818: }
819: }
1.10 raeburn 820: if (document.getElementById(labeldiv)) {
821: if (ltitoolsLabel[j]) {
822: document.getElementById(labeldiv).style.display = 'inline';
823: } else {
824: document.getElementById(labeldiv).style.display = 'none';
825: }
1.9 raeburn 826: }
1.10 raeburn 827: if (document.getElementById(titlediv)) {
828: if (ltitoolsTitle[j]) {
829: document.getElementById(titlediv).style.display = 'inline';
830: } else {
831: document.getElementById(titlediv).style.display = 'none';
832: }
1.9 raeburn 833: }
1.10 raeburn 834: break;
1.9 raeburn 835: }
836: }
837: }
838: }
839: }
840: }
841:
842: function updateTooldim(form,dimendiv,widthinput,heightinput) {
843: if (form.exttooltarget.length) {
844: for (var i=0; i<form.exttooltarget.length; i++) {
845: if (form.exttooltarget[i].checked) {
846: var dimen = 'none';
847: if (form.exttooltarget[i].value == 'window') {
848: dimen = 'block';
849: } else {
850: if (document.getElementById(widthinput)) {
851: document.getElementById(widthinput).value = '';
852: }
853: if (document.getElementById(heightinput)) {
854: document.getElementById(heightinput).value = '';
855: }
856: }
857: if (document.getElementById(dimendiv)) {
858: document.getElementById(dimendiv).style.display = dimen;
859: }
860: break;
861: }
862: }
863: }
864: }
865:
1.1 raeburn 866: ENDJS
867:
868: }
869:
870: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>