--- loncom/interface/londocs.pm 2022/10/19 00:03:10 1.682 +++ loncom/interface/londocs.pm 2023/07/11 22:24:29 1.702 @@ -1,7 +1,7 @@ # The LearningOnline Network # Documents # -# $Id: londocs.pm,v 1.682 2022/10/19 00:03:10 raeburn Exp $ +# $Id: londocs.pm,v 1.702 2023/07/11 22:24:29 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -45,7 +45,6 @@ use Apache::lontemplate(); use Apache::lonsimplepage(); use Apache::lonhomework(); use Apache::lonpublisher(); -use Apache::lonparmset(); use Apache::loncourserespicker(); use HTML::Entities; use HTML::TokeParser; @@ -183,43 +182,62 @@ sub default_folderpath { } } -sub validate_folderpath { - my ($supplementalflag) = @_; - if ($env{'form.folderpath'} ne '') { - my @items = split(/\&/,$env{'form.folderpath'}); - my $badpath; - for (my $i=0; $i<@items; $i++) { - my $odd = $i%2; - if (($odd) && (!$supplementalflag) && ($items[$i] !~ /^[^:]*:(|\d+):(|1):(|1):(|1):(|1)$/)) { - $badpath = 1; - } elsif ((!$odd) && ($items[$i] !~ /^(default|supplemental)(|_\d+)$/)) { - $badpath = 1; - } - last if ($badpath); - } - if ($badpath) { - delete($env{'form.folderpath'}); - } - } - return; -} - -sub validate_suppath { +sub validate_supppath { + my ($coursenum,$coursedom) = @_; + my $backto; if ($env{'form.supppath'} ne '') { my @items = split(/\&/,$env{'form.supppath'}); - my $badpath; + my ($badpath,$got_supp,$supppath,%supphidden,%suppids); for (my $i=0; $i<@items; $i++) { my $odd = $i%2; if ((!$odd) && ($items[$i] !~ /^supplemental(|_\d+)$/)) { $badpath = 1; + last; + } elsif ($odd) { + my $suffix; + my $idx = $i-1; + if ($items[$i] =~ /^([^:]*)::(|1):::$/) { + $backto .= '&'.$1; + } elsif ($items[$idx] eq 'supplemental') { + $backto .= '&'.$items[$i]; + } else { + $backto .= '&'.$items[$i]; + my $is_hidden; + unless ($got_supp) { + my ($supplemental) = &Apache::loncommon::get_supplemental($coursenum,$coursedom); + if (ref($supplemental) eq 'HASH') { + if (ref($supplemental->{'hidden'}) eq 'HASH') { + %supphidden = %{$supplemental->{'hidden'}}; + } + if (ref($supplemental->{'ids'}) eq 'HASH') { + %suppids = %{$supplemental->{'ids'}}; + } + } + $got_supp = 1; + } + if (ref($suppids{"/uploaded/$coursedom/$coursenum/$items[$idx].sequence"}) eq 'ARRAY') { + my $mapid = $suppids{"/uploaded/$coursedom/$coursenum/$items[$idx].sequence"}->[0]; + if ($supphidden{$mapid}) { + $is_hidden = 1; + } + } + $suffix = '::'.$is_hidden.':::'; + } + $supppath .= '&'.$items[$i].$suffix; + } else { + $supppath .= '&'.$items[$i]; + $backto .= '&'.$items[$i]; } - last if ($badpath); } if ($badpath) { delete($env{'form.supppath'}); + } else { + $supppath =~ s/^\&//; + $backto =~ s/^\&//; + $env{'form.supppath'} = $supppath; } } - return; + return $backto; } sub dumpcourse { @@ -700,7 +718,7 @@ sub group_import { $url = $1; my $marker = $2; my $info = $3; - my ($toolid,%toolhash,%toolsettings); + my ($toolid,$toolprefix,$tooltype,%toolhash,%toolsettings); my @extras = ('linktext','explanation','crslabel','crstitle','crsappend'); my @toolinfo = split(/:/,$info); if ($residx) { @@ -709,6 +727,12 @@ sub group_import { } else { $toolid = shift(@toolinfo); } + if ($toolid =~ /^c/) { + $tooltype = 'crs'; + $toolprefix = 'c'; + } else { + $tooltype = 'dom'; + } $toolid =~ s/\D//g; ($toolhash{'target'},$toolhash{'width'},$toolhash{'height'}, $toolhash{'linktext'},$toolhash{'explanation'},$toolhash{'crslabel'}, @@ -722,127 +746,130 @@ sub group_import { $toolhash{'gradable'} =~ s/\D+//g; } if (ref($ltitoolsref) eq 'HASH') { - if (ref($ltitoolsref->{$toolid}) eq 'HASH') { - my @deleted; - $toolhash{'id'} = $toolid; - if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'tab') || - ($toolhash{'target'} eq 'window')) { - if ($toolhash{'target'} eq 'window') { - foreach my $item ('width','height') { - $toolhash{$item} =~ s/^\s+//; - $toolhash{$item} =~ s/\s+$//; - if ($toolhash{$item} =~ /\D/) { - delete($toolhash{$item}); - if ($residx) { - if ($toolsettings{$item}) { - push(@deleted,$item); + if (ref($ltitoolsref->{$tooltype}) eq 'HASH') { + if (ref($ltitoolsref->{$tooltype}->{$toolid}) eq 'HASH') { + my %tools = %{$ltitoolsref->{$tooltype}->{$toolid}}; + my @deleted; + $toolhash{'id'} = $toolprefix.$toolid; + if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'tab') || + ($toolhash{'target'} eq 'window')) { + if ($toolhash{'target'} eq 'window') { + foreach my $item ('width','height') { + $toolhash{$item} =~ s/^\s+//; + $toolhash{$item} =~ s/\s+$//; + if ($toolhash{$item} =~ /\D/) { + delete($toolhash{$item}); + if ($residx) { + if ($toolsettings{$item}) { + push(@deleted,$item); + } } } } } - } - } elsif ($residx) { - $toolhash{'target'} = $toolsettings{'target'}; - if ($toolhash{'target'} eq 'window') { - foreach my $item ('width','height') { - $toolhash{$item} = $toolsettings{$item}; + } elsif ($residx) { + $toolhash{'target'} = $toolsettings{'target'}; + if ($toolhash{'target'} eq 'window') { + foreach my $item ('width','height') { + $toolhash{$item} = $toolsettings{$item}; + } + } + } elsif (ref($tools{'display'}) eq 'HASH') { + $toolhash{'target'} = $tools{'display'}{'target'}; + if ($toolhash{'target'} eq 'window') { + $toolhash{'width'} = $tools{'display'}{'width'}; + $toolhash{'height'} = $tools{'display'}{'height'}; } } - } elsif (ref($ltitoolsref->{$toolid}->{'display'}) eq 'HASH') { - $toolhash{'target'} = $ltitoolsref->{$toolid}->{'display'}->{'target'}; - if ($toolhash{'target'} eq 'window') { - $toolhash{'width'} = $ltitoolsref->{$toolid}->{'display'}->{'width'}; - $toolhash{'height'} = $ltitoolsref->{$toolid}->{'display'}->{'height'}; - } - } - if ($toolhash{'target'} eq 'iframe') { - foreach my $item ('width','height','linktext','explanation') { - delete($toolhash{$item}); - if ($residx) { - if ($toolsettings{$item}) { - push(@deleted,$item); + if ($toolhash{'target'} eq 'iframe') { + foreach my $item ('width','height','linktext','explanation') { + delete($toolhash{$item}); + if ($residx) { + if ($toolsettings{$item}) { + push(@deleted,$item); + } } } - } - } elsif ($toolhash{'target'} eq 'tab') { - foreach my $item ('width','height') { - delete($toolhash{$item}); - if ($residx) { - if ($toolsettings{$item}) { - push(@deleted,$item); + } elsif ($toolhash{'target'} eq 'tab') { + foreach my $item ('width','height') { + delete($toolhash{$item}); + if ($residx) { + if ($toolsettings{$item}) { + push(@deleted,$item); + } } } } - } - if (ref($ltitoolsref->{$toolid}->{'crsconf'}) eq 'HASH') { - foreach my $item ('label','title','linktext','explanation') { - my $crsitem; - if (($item eq 'label') || ($item eq 'title')) { - $crsitem = 'crs'.$item; - } else { - $crsitem = $item; - } - if ($ltitoolsref->{$toolid}->{'crsconf'}->{$item}) { - $toolhash{$crsitem} =~ s/^\s+//; - $toolhash{$crsitem} =~ s/\s+$//; - if ($toolhash{$crsitem} eq '') { + if (ref($tools{'crsconf'}) eq 'HASH') { + foreach my $item ('label','title','linktext','explanation') { + my $crsitem; + if (($item eq 'label') || ($item eq 'title')) { + $crsitem = 'crs'.$item; + } else { + $crsitem = $item; + } + if ($tools{'crsconf'}{$item}) { + $toolhash{$crsitem} =~ s/^\s+//; + $toolhash{$crsitem} =~ s/\s+$//; + if ($toolhash{$crsitem} eq '') { + delete($toolhash{$crsitem}); + } + } else { delete($toolhash{$crsitem}); } - } else { - delete($toolhash{$crsitem}); - } - if (($residx) && (exists($toolsettings{$crsitem}))) { - unless (exists($toolhash{$crsitem})) { - push(@deleted,$crsitem); + if (($residx) && (exists($toolsettings{$crsitem}))) { + unless (exists($toolhash{$crsitem})) { + push(@deleted,$crsitem); + } } } } - } - if ($toolhash{'passback'}) { - my $gradesecret = UUID::Tiny::create_uuid_as_string(UUID_V4); - $toolhash{'gradesecret'} = $gradesecret; - $toolhash{'gradesecretdate'} = time; - } - if ($toolhash{'roster'}) { - my $rostersecret = UUID::Tiny::create_uuid_as_string(UUID_V4); - $toolhash{'rostersecret'} = $rostersecret; - $toolhash{'rostersecretdate'} = time; - } - my $changegradable; - if (($residx) && ($folder =~ /^default/)) { - if ($toolsettings{'gradable'}) { - unless (($toolhash{'gradable'}) || (defined($LONCAPA::map::zombies[$residx]))) { - push(@deleted,'gradable'); - $changegradable = 1; - } - } elsif ($toolhash{'gradable'}) { - $changegradable = 1; + if ($toolhash{'passback'}) { + my $gradesecret = UUID::Tiny::create_uuid_as_string(UUID_V4); + $toolhash{'gradesecret'} = $gradesecret; + $toolhash{'gradesecretdate'} = time; + } + if ($toolhash{'roster'}) { + my $rostersecret = UUID::Tiny::create_uuid_as_string(UUID_V4); + $toolhash{'rostersecret'} = $rostersecret; + $toolhash{'rostersecretdate'} = time; } - if (($caller eq 'londocs') && (defined($LONCAPA::map::zombies[$residx]))) { - $changegradable = 1; + my $changegradable; + if (($residx) && ($folder =~ /^default/)) { if ($toolsettings{'gradable'}) { - $toolhash{'gradable'} = 1; + unless (($toolhash{'gradable'}) || (defined($LONCAPA::map::zombies[$residx]))) { + push(@deleted,'gradable'); + $changegradable = 1; + } + } elsif ($toolhash{'gradable'}) { + $changegradable = 1; + } + if (($caller eq 'londocs') && (defined($LONCAPA::map::zombies[$residx]))) { + $changegradable = 1; + if ($toolsettings{'gradable'}) { + $toolhash{'gradable'} = 1; + } } } - } - my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$coursedom,$coursenum); - if ($putres eq 'ok') { - if (@deleted) { - &Apache::lonnet::del('exttool_'.$marker,\@deleted,$coursedom,$coursenum); - } - if (($changegradable) && ($folder =~ /^default/)) { - my $val; - if ($toolhash{'gradable'}) { - $val = 'yes'; - } else { - $val = 'no'; + my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$coursedom,$coursenum); + if ($putres eq 'ok') { + if (@deleted) { + &Apache::lonnet::del('exttool_'.$marker,\@deleted,$coursedom,$coursenum); } - &LONCAPA::map::storeparameter($residx,'parameter_0_gradable',$val, - 'string_yesno'); - &remember_parms($residx,'gradable','set',$val); + if (($changegradable) && ($folder =~ /^default/)) { + my $val; + if ($toolhash{'gradable'}) { + $val = 'yes'; + } else { + $val = 'no'; + } + &LONCAPA::map::storeparameter($residx,'parameter_0_gradable',$val, + 'string_yesno'); + &remember_parms($residx,'gradable','set',$val); + } + } else { + return (&mt('Failed to save update to external tool.'),1); } - } else { - return (&mt('Failed to save update to external tool.'),1); } } } @@ -1188,7 +1215,13 @@ sub docs_change_log { } $r->print(''); if ($docslog{$id}{'logentry'}{'parameter_res'}) { - $r->print(&LONCAPA::map::qtescape((split(/\:/,$docslog{$id}{'logentry'}{'parameter_res'}))[0]).':
'.$error.'
'); } if ($suppchanges) { - my %servers = &Apache::lonnet::internet_dom_servers($coursedom); - my @ids=&Apache::lonnet::current_machine_ids(); - foreach my $server (keys(%servers)) { - next if (grep(/^\Q$server\E$/,@ids)); - my $hashid=$coursenum.':'.$coursedom; - my $cachekey = &escape('showsupp').':'.&escape($hashid); - &Apache::lonnet::remote_devalidate_cache($server,[$cachekey]); - } - &Apache::lonnet::has_unhidden_suppfiles($coursenum,$coursedom,1); - &Apache::lonnet::count_supptools($coursenum,$coursedom,1); - my $now = time; - if ($env{'request.course.id'} eq $coursedom.'_'.$coursenum) { - &Apache::lonnet::appenv({'request.course.suppupdated' => $now}); - } - &Apache::lonnet::put('environment',{'internal.supplementalchange' => $now}, - $coursedom,$coursenum); - &Apache::lonnet::appenv( - {'course.'.$coursedom.'_'.$coursenum.'.internal.supplementalchange' => $now}); - &Apache::lonnet::do_cache_new('suppchange',$coursedom.'_'.$coursenum,$now,600); - &Apache::lonnet::get_numsuppfiles($coursenum,$coursedom,1); + &Apache::lonnet::update_supp_caches($coursedom,$coursenum); undef($suppchanges); } } @@ -7093,6 +7176,10 @@ sub remove_archive { if ($url eq $env{'form.archiveurl'}) { if (&handle_edit_cmd($docuname,$docudom)) { ($errtext,$fatal) = &storemap($docuname,$docudom,$map,1); + if ($suppchanges) { + &Apache::lonnet::update_supp_caches($docudom,$docuname); + undef($suppchanges); + } if ($fatal) { if ($container eq 'page') { $delwarning = &mt('An error occurred updating the contents of the current page.'); @@ -7367,6 +7454,8 @@ sub editing_js { tinc => 'Title in course', sunm => 'Sub-directory name', edri => 'Editing rights unavailable for your current role.', + sele => 'Select', + swit => 'Switch server required', ); &js_escape(\%js_lt); my $crstype = &Apache::loncommon::course_type(); @@ -7378,7 +7467,9 @@ sub editing_js { my $backtourl; my $toplevelmain = &escape(&default_folderpath($coursenum,$coursedom,$navmapref)); my $toplevelsupp = &supplemental_base(); - + my $showfile_js = &Apache::loncommon::show_crsfiles_js(); + my @ids=&Apache::lonnet::current_machine_ids(); + my $machines_str = "'".join("','",@ids)."'"; if ($env{'docs.exit.'.$env{'request.course.id'}} =~ /^direct_(.+)$/) { my $caller = $1; if ($caller =~ /^supplemental/) { @@ -7730,27 +7821,21 @@ function toggleMap(caller) { return; } -function toggleCrsRes(caller,numauthorrole,numcrsdirs) { +function toggleCrsRes(caller) { var disp = 'none'; if (document.getElementById('crsresform')) { if (caller == 'res') { - var curr = document.getElementById('crsresform').style.display; + var form = document.getElementById('crsresform'); + var curr = form.style.display; if (curr == 'none') { disp='block'; - numauthor = parseInt(numauthorrole); - if (numauthor > 0) { - document.courseresform.authorrole.selectedIndex = 0; - select1priv_changed(); - document.courseresform.authorpath.selectedIndex = 0; - document.courseresform.newresourceadd.selectedIndex = 0; - toggleNewInCourse(document.courseresform); - if (document.getElementById('newresource')) { - document.getElementById('newresource').style.display = 'none'; - } - } else { - if (numcrsdirs) { - document.courseresform.authorpath.selectedIndex = 0; - } + document.courseresform.authorrole.selectedIndex = 0; + document.courseresform.authorpath.selectedIndex = 0; + document.courseresform.newresourceadd.selectedIndex = 0; + populateDirSelects(form,'authorrole','authorpath',1,0,0); + toggleNewInCourse(document.courseresform); + if (document.getElementById('newresource')) { + document.getElementById('newresource').style.display = 'none'; } if (document.courseresform.newresusetemp.length) { document.courseresform.newresusetemp[0].checked = true; @@ -7804,14 +7889,28 @@ function toggleNewsubdir(form) { function toggleCrsResTitle() { if (document.getElementById('newresource')) { - if (document.courseresform.authorrole.options[document.courseresform.authorrole.selectedIndex].value == 'course') { + var selloc = document.courseresform.authorrole.options[document.courseresform.authorrole.selectedIndex].value; + if (selloc == 'course') { document.getElementById('newresource').style.display = 'inline'; document.courseresform.newresourceadd[0].checked = true; toggleNewInCourse(document.courseresform); } else { document.getElementById('newresource').style.display = 'none'; } - } + } + if (document.getElementById('newstdproblem')) { + if (document.courseresform.authorpath.options[document.courseresform.authorpath.selectedIndex].value == 'switch') { + document.getElementById('newstdproblem').style.display = 'none'; + if (document.getElementById('stdprobswitch')) { + document.getElementById('stdprobswitch').style.display = 'block'; + } + } else { + document.getElementById('newstdproblem').style.display = 'block'; + if (document.getElementById('stdprobswitch')) { + document.getElementById('stdprobswitch').style.display = 'none'; + } + } + } } function toggleNewInCourse(form) { @@ -7891,16 +7990,28 @@ function getExample(width,height,scrolli } } -function toggleImportCrsres(caller,dircount) { +function toggleImportCrsres(caller) { var disp = 'none'; if (document.getElementById('importcrsresform')) { if (caller == 'res') { - var numdirs = parseInt(dircount); var curr = document.getElementById('importcrsresform').style.display; if (curr == 'none') { disp='block'; - if (numdirs > 1) { - select1res_changed(); + populateCrsSelects(document.crsresimportform,'coursepath','coursefile',1,'',1,0,1,1,0); + if ((document.getElementById('importcrsrescontent')) && + (document.getElementById('importcrsresempty'))) { + var selelem = document.crsresimportform.elements['coursepath']; + var numdirs = 0; + if (selelem.options.length) { + numdirs = selelem.options.length - 1; + } + if (numdirs) { + document.getElementById('importcrsrescontent').style.display='block'; + document.getElementById('importcrsresempty').style.display='none'; + } else { + document.getElementById('importcrsrescontent').style.display='none'; + document.getElementById('importcrsresempty').style.display='block'; + } } } } @@ -7909,6 +8020,152 @@ function toggleImportCrsres(caller,dirco } return; } + +$showfile_js + +function populateDirSelects(form,locsel,dirsel,setdir,recurse,nonemptydir) { + var location = form.elements[locsel].options[form.elements[locsel].selectedIndex].value; + if ((setdir) && (dirsel != null) && (dirsel != 'undefined') && (dirsel != '')) { + var selelem = form.elements[dirsel]; + var i, numfiles = selelem.options.length -1; + if (numfiles >=0) { + for (i = numfiles; i >= 0; i--) { + selelem.remove(i); + } + } + if ((location == '') || (location == null) || (location == 'undefined')) { + if (selelem.options.length == 0) { + selelem.options[selelem.options.length] = new Option('',''); + selelem.selectedIndex = 0; + } + if (document.getElementById('newstdproblem')) { + document.getElementById('newstdproblem').style.display = 'none'; + } + return; + } + var machineIds = new Array($machines_str); + var athome = 0; + var role = location; + if ((location == 'author') || (location == 'course')) { + if (document.getElementById('rolehome_'+location)) { + var currhome = document.getElementById('rolehome_'+location).value; + if ((currhome != '') && (currhome != null) && (currhome != 'undefined')) { + if (machineIds.includes(currhome)) { + athome = 1; + } + } + } + } else { + const roleinfo = location.split('___'); + role = encodeURIComponent(roleinfo[0]+'./'+roleinfo[1]); + if (document.getElementById('rolehome_coauthor_'+roleinfo[1]+'_'+roleinfo[0])) { + var currhome = document.getElementById('rolehome_coauthor_'+roleinfo[1]+'_'+roleinfo[0]).value; + if ((currhome != '') && (currhome != null) && (currhome != 'undefined')) { + if (machineIds.includes(currhome)) { + athome = 1; + } + } + } + } + if (athome) { + if (document.getElementById('stdprobswitch')) { + document.getElementById('stdprobswitch').style.display = 'none'; + } + if (document.getElementById('newstdproblem')) { + document.getElementById('newstdproblem').style.display = 'none'; + } + var http = new XMLHttpRequest(); + var url = "/adm/courseauthor"; + var params = "role="+role+"&rec="+recurse+"&nonempty="+nonemptydir+"&addtop=1"; + http.open("POST", url, true); + http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + http.onreadystatechange = function() { + if (http.readyState == 4 && http.status == 200) { + var data = JSON.parse(http.responseText); + if (Array.isArray(data.dirs)) { + var len = data.dirs.length; + if (len) { + if (len > 1) { + selelem.options[selelem.options.length] = new Option('$js_lt{sele}',''); + } + } + if (len) { + var j; + for (j = 0; j < len; j++) { + selelem.options[selelem.options.length] = new Option(data.dirs[j],data.dirs[j]); + } + selelem.selectedIndex = 0; + if (len == 1) { + toggleCrsResTitle(); + } + } + } + } + } + http.send(params); + } else { + selelem.options[selelem.options.length] = new Option('$js_lt{swit}','switch'); + selelem.selectedIndex = 0; + if (document.getElementById('stdprobswitch')) { + document.getElementById('stdprobswitch').style.display = 'block'; + } + if (document.getElementById('newstdproblem')) { + document.getElementById('newstdproblem').style.display = 'none'; + } + } + } + return; +} + +function switchForProb() { + if (document.courseresform.authorpath.options[document.courseresform.authorpath.selectedIndex].value == 'switch') { + var url = '/adm/switchserver?otherserver='; + var newhostid = ''; + var role = ''; + var selloc = document.courseresform.authorrole.options[document.courseresform.authorrole.selectedIndex].value; + if (selloc == 'author') { + newhostid = document.courseresform.rolehome_author.value; + role = "au./&js_escape($env{'user.domain'})/"; + } else if (selloc == 'course') { + newhostid = document.courseresform.rolehome_course.value; + role = "&js_escape($env{'request.role'})"; + } else { + var items = new Array(); + items = selloc.split('___'); + var len = document.courseresform.rolehome_coauthor.length; + if (null == len) { + var currval = document.courseresform.rolehome_coauthor.value; + if (null != currval) { + var info = new Array(); + info = currval.split('='); + newhostid = info[2]; + role = info[0]+'./'+info[1]; + } + } else { + for (var i=0; i