--- loncom/interface/lonextresedit.pm 2012/12/02 18:25:11 1.2
+++ loncom/interface/lonextresedit.pm 2023/07/12 00:05:02 1.34
@@ -1,7 +1,7 @@
# The LearningOnline Network
# Documents
#
-# $Id: lonextresedit.pm,v 1.2 2012/12/02 18:25:11 raeburn Exp $
+# $Id: lonextresedit.pm,v 1.34 2023/07/12 00:05:02 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -56,87 +56,173 @@ sub handler {
my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
- my ($supplementalflag,$updated,$output,$errormsg,$residx,$url,$title,$symb);
- if ((($env{'form.folderpath'} =~ /^supplemental/) && ($env{'form.suppurl'})) ||
- ($env{'form.symb'} =~ /^uploaded/)) {
- $supplementalflag = 1;
- ($updated,$output,$errormsg,$residx,$url,$title,$symb) =
+ my ($supplementalflag,$updated,$output,$errormsg,$residx,$url,$title,
+ $symb,$type);
+ if (($env{'form.folderpath'} =~ /^supplemental/) && ($env{'form.suppurl'})) {
+ $supplementalflag = 1;
+ if (&unescape($env{'form.suppurl'}) =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
+ $type = 'tool';
+ }
+ }
+ if (($supplementalflag) || ($env{'form.symb'} =~ /^uploaded/)) {
+ ($updated,$output,$errormsg,$residx,$url,$title,$symb) =
&process_changes($supplementalflag,$cdom,$cnum,$chome);
if ($supplementalflag) {
- if ($url ne $env{'form.suppurl'}) {
+ if ($url ne &unescape($env{'form.suppurl'})) {
$env{'form.suppurl'} = $url;
}
if ($title ne $env{'form.title'}) {
$env{'form.title'} = $title;
}
+ $env{'form.idx'} = $residx;
} else {
if ($symb ne $env{'form.symb'}) {
$env{'form.symb'} = $symb;
}
+ if ($url =~ m{/adm/$cdom/$cnum/\d+/ext\.tool$}) {
+ $type = 'tool';
+ }
}
} else {
$errormsg = &mt('Information about external resource to edit is missing.');
}
if ($updated) {
- $output = &Apache::lonhtmlcommon::confirm_success(&mt('External Resource updated'));
+ my $msg = &mt('External Resource updated');
+ if ($type eq 'tool') {
+ $msg = &mt('External Tool updated');
+ }
+ $output = &Apache::lonhtmlcommon::confirm_success($msg);
}
if ($errormsg) {
$errormsg = '
'.$errormsg.'
';
}
+ my %ltitools;
+ if ($type eq 'tool') {
+ my (%domtools,%crstools);
+ my %tooltypes = &Apache::loncommon::usable_exttools();
+ if ($tooltypes{'dom'}) {
+ %domtools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
+ }
+ if ($tooltypes{'crs'}) {
+ %crstools = &Apache::lonnet::get_course_lti($cnum,$cdom,'consumer');
+ }
+ %ltitools = (
+ dom => \%domtools,
+ crs => \%crstools,
+ );
+ }
my $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
my $pathitem = '';
- $r->print(&Apache::loncommon::start_page('External Resource Editor',$js).
+ my $description = 'External Resource Editor';
+ if ($type eq 'tool') {
+ $description = 'External Tool Editor';
+ }
+ $r->print(&Apache::loncommon::start_page($description,$js).
''.
$output.
$errormsg.
&extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,
- 'direct',$env{'form.symb'}).
+ 'direct',$env{'form.symb'},$type,$cdom,$cnum,\%ltitools).
'
'.&Apache::loncommon::end_page());
return OK;
}
sub process_changes {
my ($supplementalflag,$cdom,$cnum,$chome) = @_;
- my ($folder,$container,$output,$errormsg,$updated,$symb,$oldidx,$oldurl,
- $oldtitle,$residx,$newurl,$newtitle);
+ my ($folder,$container,$output,$errormsg,$updated,$symb,$oldidx,$oldurl,$type,
+ $oldtitle,$newidx,$newurl,$newtitle,$residx,$url,$title,$marker,$args);
if ($env{'form.symb'}) {
$symb = $env{'form.symb'};
- (my $map,$oldidx,my $url)=&Apache::lonnet::decode_symb($symb);
+ (my $map,$oldidx,$oldurl)=&Apache::lonnet::decode_symb($symb);
if ($map =~ m{^uploaded/$cdom/$cnum/(default(_\d+|))\.(sequence|page)$}) {
$folder = $1;
$container = $3;
}
- if ($url =~ m{^ext/(.+)$}) {
- $oldurl = $1;
- if ($oldurl !~ m{^https://}) {
+ $oldtitle = &Apache::lonnet::gettitle($env{'form.symb'});
+ if ($oldurl =~ m{^ext/(.+)$}) {
+ my $external = $1;
+ if ($external =~ m{^https://}) {
+ $oldurl = $external;
+ } else {
$oldurl = 'http://'.$oldurl;
}
+ $type = 'ext';
+ } else {
+ $type = 'tool';
}
- $oldtitle = &Apache::lonnet::gettitle($env{'form.symb'});
} elsif ($env{'form.folderpath'}) {
$folder = &unescape( (split('&',$env{'form.folderpath'}))[-2] );
$oldurl = &unescape($env{'form.suppurl'});
$oldtitle = &unescape($env{'form.title'});
$container = 'sequence';
$supplementalflag = 1;
+ if ($oldurl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
+ $type = 'tool';
+ } else {
+ $type = 'ext';
+ }
+ }
+ $url = $oldurl;
+ $title = $oldtitle;
+ if ($env{'form.importdetail'}) {
+ ($newtitle,$newurl,$newidx) =
+ map {&unescape($_)} split(/\=/,$env{'form.importdetail'});
+ if ($newurl =~ m{^(/adm/$cdom/$cnum/(\d+)/ext\.tool)\:?(.*)$}) {
+ $newurl = $1;
+ $marker = $2;
+ $args = $3;
+ if ((!$symb) && (!$supplementalflag)) {
+ $symb = "uploaded/$cdom/$cnum/$folder.$container"."___$newidx"."___adm/$cdom/$cnum/$marker/ext.tool";
+ }
+ }
+ }
+ if ($supplementalflag) {
+ $residx = $newidx;
+ } else {
+ $residx = $oldidx;
}
if ($folder && $container) {
if ($env{'form.importdetail'}) {
- my ($errtext,$fatal,$mismatchedid);
- ($newtitle,$newurl, $residx) =
- map {&unescape($_)} split(/\=/,$env{'form.importdetail'});
- if (!$supplementalflag && $oldidx) {
- if ($oldidx != $residx) {
- $mismatchedid = 1;
- $residx = $oldidx;
- }
+ my ($errtext,$fatal,$mismatchedid,$needreload,$newgradable,@imports);
+ if (!$supplementalflag) {
+ if (($oldidx) && ($oldidx != $newidx)) {
+ $mismatchedid = 1;
+ }
}
- my @imports;
if ($mismatchedid) {
$errormsg = 'Wrong item identifier';
} elsif (($newtitle eq $oldtitle) && ($newurl eq $oldurl)) {
- $output = &mt('No change');
+ if ($type eq 'tool') {
+ if ($args) {
+ ($updated,$newgradable,$errormsg) = &update_exttool($marker,$cdom,$cnum,
+ $supplementalflag,$args);
+ if ($updated) {
+ if ($newgradable) {
+ my $map = "/uploaded/$cdom/$cnum/$folder.$container";
+ my ($errtext,$fatal) = &LONCAPA::map::mapread($map);
+ if ($fatal) {
+ $errormsg = &mt('Update failed: [_1].',$errtext);
+ } else {
+ &LONCAPA::map::storeparameter($residx,'parameter_0_gradable',
+ $newgradable,'string_yesno');
+ my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1,1);
+ if ($errtext) {
+ $errormsg = &mt('Update failed: [_1].',$errtext);
+ } else {
+ $needreload = 1;
+ }
+ }
+ }
+ } else {
+ $output = &mt('No change');
+ }
+ } else {
+ $output = &mt('No change');
+ }
+ } else {
+ $output = &mt('No change');
+ }
} else {
my $map = "/uploaded/$cdom/$cnum/$folder.$container";
my ($errtext,$fatal) = &LONCAPA::map::mapread($map);
@@ -145,80 +231,218 @@ sub process_changes {
} else {
my $saveurl = &LONCAPA::map::qtunescape($newurl);
my $savetitle = &LONCAPA::map::qtunescape($newtitle);
+ my $ext = 'true';
+ if ($type eq 'tool') {
+ if ($args) {
+ ($updated,$newgradable,$errormsg) = &update_exttool($marker,$cdom,$cnum,
+ $supplementalflag,$args);
+ if ($newgradable) {
+ &LONCAPA::map::storeparameter($residx,'parameter_0_gradable',$newgradable,
+ 'string_yesno');
+ $needreload = 1;
+ }
+ }
+ $ext = 'false';
+ }
+ my $dotimeupdate;
+ unless ($supplementalflag) {
+ if (($newgradable) || ($newurl ne $oldurl)) {
+ $dotimeupdate = 1;
+ }
+ }
$LONCAPA::map::resources[$residx] =
- join(':', ($savetitle,$saveurl,'true','normal','res'));
- my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1);
+ join(':', ($savetitle,$saveurl,$ext,'normal','res'));
+ my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1,$dotimeupdate);
if ($errtext) {
$errormsg = &mt('Update failed: [_1].',$errtext);
} else {
$updated = 1;
+ $title = $newtitle;
+ if ($newurl ne $oldurl) {
+ $url = $newurl;
+ if ($ext eq 'true') {
+ $newurl =~ s{^http://}{};
+ $newurl = "ext/$newurl";
+ }
+ }
if (!$supplementalflag) {
if ($newurl ne $oldurl) {
- $symb = &Apache::lonnet::encode_symb($map,$residx,"ext/$newurl");
+ $symb = &Apache::lonnet::encode_symb($map,$residx,$newurl);
} else {
$symb = $env{'form.symb'};
if ($symb) {
&Apache::lonnet::devalidate_title_cache($symb);
}
}
- }
- my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
- if ($ferr) {
- $errormsg = &mt('Reload failed: [_1].',$ferr);
- } else {
- &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,
- $cdom.'_'.$cnum);
+ $needreload = 1;
}
}
}
}
+ if ($needreload) {
+ my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
+ if ($ferr) {
+ $errormsg = &mt('Reload failed: [_1].',$ferr);
+ } else {
+ &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,
+ $cdom.'_'.$cnum);
+ }
+ }
+ if (($type eq 'tool') && ($newgradable)) {
+ my $uri = &Apache::lonnet::declutter($url);
+ &Apache::lonnet::devalidate_cache_new('meta',$uri);
+ }
} else {
$output = &mt('No change');
}
} else {
- $errormsg = &mt('Information about current external resource is incomplete.');
+ if ($type eq 'tool') {
+ $errormsg = &mt('Information about current external tool is incomplete.');
+ } else {
+ $errormsg = &mt('Information about current external resource is incomplete.');
+ }
}
- if ($updated) {
- return ($updated,$output,$errormsg,$residx,$newurl,$newtitle,$symb);
- } else {
- return ($updated,$output,$errormsg,$residx,$oldurl,$oldtitle,$symb);
+ return ($updated,$output,$errormsg,$residx,$url,$title,$symb);
+}
+
+sub update_exttool {
+ my ($marker,$cdom,$cnum,$supplementalflag,$args) = @_;
+ my (%newhash,$changed,$newgradable,@deleted,$errormsg);
+ ($newhash{'target'},$newhash{'width'},$newhash{'height'},$newhash{'linktext'},$newhash{'explanation'},
+ $newhash{'crslabel'},$newhash{'crstitle'},$newhash{'crsappend'},$newhash{'gradable'}) = split(/:/,$args);
+ foreach my $item ('linktext','explanation','crslabel','crstitle','crsappend') {
+ $newhash{$item} = &unescape($newhash{$item});
+ }
+ my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
+ foreach my $item ('target','width','height','linktext','explanation','crslabel','crstitle','crsappend','gradable') {
+ $newhash{$item} =~ s/^\s+//;
+ $newhash{$item} =~ s/\s+$//;
+ if (($item eq 'width') || ($item eq 'height') || ($item eq 'linktext') || ($item eq 'explanation')) {
+ if ($newhash{'target'} eq 'iframe') {
+ $newhash{$item} = '';
+ } elsif ($newhash{'target'} eq 'tab') {
+ if (($item eq 'width') || ($item eq 'height')) {
+ $newhash{$item} = '';
+ }
+ }
+ } elsif ($item eq 'gradable') {
+ unless ($newhash{$item} == 1) {
+ $newhash{$item} = '';
+ }
+ }
+ if ($toolhash{$item} ne $newhash{$item}) {
+ if (($item eq 'gradable') && (!$supplementalflag)) {
+ if ($newhash{$item}) {
+ $newgradable = 'yes';
+ } else {
+ $newgradable = 'no';
+ }
+ }
+ if ($newhash{$item} eq '') {
+ unless (($item eq 'target') ||
+ ((($item eq 'width') || ($item eq 'height')) &&
+ (($newhash{'target'} eq 'window') ||
+ (($newhash{'target'} eq '') && ($toolhash{'target'} eq 'window')))) ||
+ ((($item eq 'linktext') || ($item eq 'explanation')) &&
+ ((($newhash{'target'} =~ /^(window|tab)$/)) ||
+ (($newhash{'target'} eq '') && ($toolhash{'target'} =~ /^(window|tab)$/))))) {
+ delete($toolhash{$item});
+ push(@deleted,$item);
+ $changed = 1;
+ }
+ } else {
+ $toolhash{$item} = $newhash{$item};
+ $changed = 1;
+ }
+ }
+ }
+ if ($changed) {
+ my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$cdom,$cnum);
+ unless ($putres eq 'ok') {
+ $errormsg = &mt('Failed to save updated settings.').' '.&mt('Error: [_1].',$putres);
+ }
+ }
+ if (@deleted) {
+ &Apache::lonnet::del('exttool_'.$marker,\@deleted,$cdom,$cnum);
}
+ return ($changed,$newgradable,$errormsg);
}
sub extedit_form {
- my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,$symb) = @_;
+ my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,
+ $symb,$type,$cdom,$cnum,$ltitools,$disabled) = @_;
+ if ($type ne 'tool') {
+ $type = 'ext';
+ }
my %lt = &Apache::lonlocal::texthash(
ex => 'External Resource',
+ et => 'External Tool',
ed => 'Edit',
ee => 'External Resource Editor',
+ te => 'External Tool Editor',
pr => 'Preview',
sv => 'Save',
ul => 'URL',
ti => 'Title',
al => 'Add Link',
+ at => 'Add Tool',
+ dd => 'Defined in domain',
+ dc => 'Defined in course',
);
- my $formname = 'newext';
my $tabid = 'aa';
- my $toggle = 'ext';
- my $fieldsetid = 'uploadextform';
- my $urlid = 'exturl';
my $size = 60;
if ($supplementalflag) {
- $formname = 'newsuppext';
$tabid = 'ee';
- $toggle = 'suppext';
- $fieldsetid = 'uploadsuppextform';
- $urlid = 'suppexturl';
}
- my ($link,$legend,$active,$srcclass,$extsrc,$preview,$title,$save,
- $fieldsetstyle,$action,$hiddenelem);
+ my ($formname,$formid,$toggle,$fieldsetid,$urlid,$subdivid,$dispdivstyle,$dimendivstyle,
+ $windivstyle,$linktextstyle,$explanationstyle,$labelstyle,$titlestyle,
+ $appendstyle,$gradablestyle,$subdivstyle,$legend,$urlelem,$toolelem,%toolattr);
+ $formname = 'new'.$type;
+ $toggle = $type;
+ $fieldsetid = 'external'.$type.'form';
+ $urlid = $type.'url';
+ map { $toolattr{$_} = $type.$_; } ('dispdiv','dimendiv','dimenwidth','dimenheight',
+ 'crstitlediv','crslabeldiv','crsappenddiv',
+ 'gradablediv','crstitle','crslabel','crsappend',
+ 'windiv','linktextdiv','explanationdiv',
+ 'linktext','explanation','providerurl');
+ $dispdivstyle = 'display:none';
+ $dimendivstyle = 'display:none';
+ $windivstyle = 'display:none';
+ $linktextstyle = 'display:none';
+ $explanationstyle = 'display:none';
+ $labelstyle = 'display:none';
+ $titlestyle = 'display:none';
+ $appendstyle = 'display:none';
+ $gradablestyle = 'display:none';
+ $subdivstyle = 'display:block';
+ if ($supplementalflag) {
+ $formname = 'newsupp'.$type;
+ $toggle = 'supp'.$type;
+ $fieldsetid = 'externalsupp'.$type.'form';
+ $urlid = 'supp'.$type.'url';
+ map { $toolattr{$_} = 'supp'.$toolattr{$_}; } (keys(%toolattr));
+ }
+ my ($link,$legend,$active,$srcclass,$extsrc,$preview,$title,$save,$crstitle,$crslabel,
+ $crsappend,$fieldsetstyle,$action,$hiddenelem,$form,$width,$height,$tooltarget,
+ $linktext,$explanation,$providerurl,$chkgrd,$chknogrd,%chkstate);
$fieldsetstyle = 'display: none;';
$action = '/adm/coursedocs';
+ my $protocol = ($ENV{'SERVER_PORT'} == 443?'https':'http');
+ my $rows = 2;
+ my $cols = 20;
if ($residx) {
if ($caller eq 'direct') {
$fieldsetstyle = 'display: block;';
$action = '/adm/extresedit';
- $legend = "";
+ $rows = 10;
+ $cols = 45;
+ if ($type eq 'tool') {
+ $legend = $lt{'te'};
+ } else {
+ $legend = $lt{'ee'};
+ }
+ $legend = '';
if ($symb) {
$hiddenelem = '';
} elsif ($supplementalflag) {
@@ -227,60 +451,333 @@ sub extedit_form {
'';
}
- } else {
- $link = ''.$lt{'ed'}.'';
+ } else {
+ $link = ''.$lt{'ed'}.' '."\n";
$size = 40;
- $active = '';
+ $active = '';
}
- $formname = "editext_$residx";
- $fieldsetid = "uploadext$residx";
- $urlid = "exturl_$residx";
+ $formname = 'edit'.$type.'_'.$residx;
+ $fieldsetid = 'external'.$type.$residx;
+ $urlid = $type.'url_'.$residx;
+ map { $toolattr{$_} .= '_'.$residx; } (keys(%toolattr));
$srcclass = ' class="LC_nobreak"';
- $extsrc = ''.$lt{'ul'}.' ';
- $preview = ' '.$lt{'pr'}.'';
+ if ($type eq 'ext') {
+ $extsrc = ''.$lt{'ul'}.' ';
+ $preview = ' '.$lt{'pr'}.'';
+ }
$title = ''.$lt{'ti'}.' ';
$save = $lt{'sv'};
} else {
- $link = ''.$helpitem;
- $legend = "";
- $extsrc = $lt{'ul'}.':
';
+ $link = $lt{'ex'};
+ if ($type eq 'tool') {
+ $link = $lt{'et'};
+ }
+ $link = ''.$helpitem;
+ if ($type eq 'tool') {
+ $legend = $lt{'te'};
+ } else {
+ $legend = $lt{'ee'};
+ }
+ $legend = '';
$title = $lt{'ti'}.':
';
$residx = 0;
- $orig_url = 'http://';
- $orig_title = $lt{'ex'};
- $preview = '';
- $save = $lt{'al'};
+ if ($type eq 'ext') {
+ $orig_url = 'http://';
+ $orig_title = $lt{'ex'};
+ $extsrc = $lt{'ul'}.':
';
+ $preview = '';
+ $save = $lt{'al'};
+ } else {
+ $orig_title = $lt{'et'};
+ $save = $lt{'at'};
+ $orig_url = "/adm/$cdom/$cnum/new/ext\.tool";
+ }
$pathitem .= '
';
}
- return <
-