--- loncom/interface/courseprefs.pm 2021/06/07 02:19:51 1.92 +++ loncom/interface/courseprefs.pm 2021/08/04 19:59:10 1.93 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Handler to set configuration settings for a course # -# $Id: courseprefs.pm,v 1.92 2021/06/07 02:19:51 raeburn Exp $ +# $Id: courseprefs.pm,v 1.93 2021/08/04 19:59:10 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -365,9 +365,15 @@ sub handler { } my %values=&Apache::lonnet::dump('environment',$cdom,$cnum); + my %courselti=&Apache::lonnet::dump('lti',$cdom,$cnum,undef,undef,undef,1); + if ($courselti{'lock'}) { + delete($courselti{'lock'}); + } + $values{'linkprotection'} = \%courselti; my @prefs_order = ('courseinfo','localization','feedback','discussion', 'classlists','appearance','grading','printouts', - 'menuitems','spreadsheet','bridgetasks','lti','other'); + 'menuitems','linkprotection','spreadsheet','bridgetasks', + 'lti','other'); my %prefs = ( 'courseinfo' => @@ -557,6 +563,14 @@ sub handler { menucollections => 'Menu collections', }, }, + 'linkprotection' => + { + text => 'Link protection', + help => 'Course_Prefs_Linkprotection', + header => [{col1 => 'Item', + col2 => 'Settings', + }], + }, 'other' => { text => 'Other settings', help => 'Course_Prefs_Other', @@ -764,6 +778,8 @@ sub print_config_box { $output .= &print_lti($cdom,$settings,$ordered,$itemtext,\$rowtotal,$crstype,$noedit); } elsif ($action eq 'menuitems') { $output .= &print_menuitems('bottom',$cdom,$settings,$itemtext,\$rowtotal,$crstype,$noedit); + } elsif ($action eq 'linkprotection') { + $output .= &print_linkprotection($cdom,$settings,\$rowtotal,$crstype,$noedit); } elsif ($action eq 'other') { $output .= &print_other($cdom,$settings,$allitems,\$rowtotal,$crstype,$noedit); } @@ -776,8 +792,8 @@ sub print_config_box { } sub process_changes { - my ($cdom,$action,$values,$item,$changes,$allitems,$disallowed,$crstype) = @_; - my %newvalues; + my ($cdom,$cnum,$action,$values,$item,$changes,$allitems,$disallowed,$crstype) = @_; + my (%newvalues,%courselti,$errors); if (ref($item) eq 'HASH') { if (ref($changes) eq 'HASH') { my @ordered; @@ -794,6 +810,21 @@ sub process_changes { } } } + } elsif ($action eq 'linkprotection') { + if (ref($values->{'linkprotection'}) eq 'HASH') { + foreach my $id (keys(%{$values->{'linkprotection'}})) { + if ($id =~ /^\d+$/) { + push(@ordered,$id); + unless (ref($values->{'linkprotection'}->{$id}) eq 'HASH') { + $courselti{$id} = ''; + } + } + } + } + @ordered = sort { $a <=> $b } @ordered; + if (($env{'form.linkprot_add'}) && ($env{'form.linkprot_maxnum'} =~ /^\d+$/)) { + push(@ordered,$env{'form.linkprot_maxnum'}); + } } elsif (ref($item->{'ordered'}) eq 'ARRAY') { if ($action eq 'courseinfo') { my ($can_toggle_cat,$can_categorize) = @@ -931,6 +962,78 @@ sub process_changes { } elsif ($values->{'menucollections'}) { $changes->{'menucollections'} = ''; } + } elsif ($action eq 'linkprotection') { + my %menutitles = <imenu_titles(); + my (@items,%deletions,%itemids,%haschanges); + if ($env{'form.linkprot_add'}) { + my $name = $env{'form.linkprot_name_add'}; + $name =~ s/(`)/'/g; + my ($newid,$error) = &get_courselti_id($cdom,$cnum,$name); + if ($newid) { + $itemids{'add'} = $newid; + push(@items,'add'); + $haschanges{$newid} = 1; + } else { + $errors .= ''. + &mt('Failed to acquire unique ID for link protection'). + ''; + } + } + if (ref($values->{'linkprotection'}) eq 'HASH') { + my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_del'); + my $maxnum = $env{'form.linkprot_maxnum'}; + for (my $i=0; $i<=$maxnum; $i++) { + my $itemid = $env{'form.linkprot_id_'.$i}; + $itemid =~ s/\D+//g; + if ($itemid) { + if (ref($values->{'linkprotection'}->{$itemid}) eq 'HASH') { + push(@items,$i); + $itemids{$i} = $itemid; + if ((@todelete > 0) && (grep(/^$i$/,@todelete))) { + $deletions{$itemid} = $values->{'linkprotection'}->{$itemid}->{'name'}; + } + } + } + } + + } + foreach my $idx (@items) { + my $itemid = $itemids{$idx}; + next unless ($itemid); + if (exists($deletions{$itemid})) { + $courselti{$itemid} = $deletions{$itemid}; + $haschanges{$itemid} = 1; + next; + } + my %current; + if (ref($values->{'linkprotection'}) eq 'HASH') { + if (ref($values->{'linkprotection'}->{$itemid}) eq 'HASH') { + foreach my $key (keys(%{$values->{'linkprotection'}->{$itemid}})) { + $current{$key} = $values->{'linkprotection'}->{$itemid}->{$key}; + } + } + } + foreach my $inner ('name','key','secret','lifetime','version') { + my $formitem = 'form.linkprot_'.$inner.'_'.$idx; + $env{$formitem} =~ s/(`)/'/g; + if ($inner eq 'lifetime') { + $env{$formitem} =~ s/[^\d.]//g; + } + unless ($idx eq 'add') { + if ($current{$inner} ne $env{$formitem}) { + $haschanges{$itemid} = 1; + } + } + if ($env{$formitem} ne '') { + $courselti{$itemid}{$inner} = $env{$formitem}; + } + } + } + if (keys(%haschanges)) { + foreach my $entry (keys(%haschanges)) { + $changes->{$entry} = $courselti{$entry}; + } + } } else { foreach my $entry (@ordered) { if ($entry eq 'cloners') { @@ -971,7 +1074,7 @@ sub process_changes { my $clonedom = $env{'form.cloners_newdom'}; if (&check_clone($clonedom,$disallowed) eq 'ok') { my $newdom = '*:'.$env{'form.cloners_newdom'}; - if (@clonedoms) { + if (@clonedoms) { if (!grep(/^\Q$newdom\E$/,@clonedoms)) { $newvalues{$entry} .= ','.$newdom; } @@ -1417,7 +1520,51 @@ sub process_changes { } } } - return; + return $errors; +} + +sub get_courselti_id { + my ($cdom,$cnum,$name) = @_; + # get lock on lti db in course + my $lockhash = { + lock => $env{'user.name'}. + ':'.$env{'user.domain'}, + }; + my $tries = 0; + my $gotlock = &Apache::lonnet::newput('lti',$lockhash,$cdom,$cnum); + my ($id,$error); + while (($gotlock ne 'ok') && ($tries<10)) { + $tries ++; + sleep (0.1); + $gotlock = &Apache::lonnet::newput('lti',$lockhash,$cdom,$cnum); + } + if ($gotlock eq 'ok') { + my %currids = &Apache::lonnet::dump('lti',$cdom,$cnum,undef,undef,undef,1); + if ($currids{'lock'}) { + delete($currids{'lock'}); + if (keys(%currids)) { + my @curr = sort { $a <=> $b } keys(%currids); + if ($curr[-1] =~ /^\d+$/) { + $id = 1 + $curr[-1]; + } else { + $id = 1; + } + } else { + $id = 1; + } + if ($id) { + unless (&Apache::lonnet::newput('lti',{ $id => $name },$cdom,$cnum) eq 'ok') { + $error = 'nostore'; + } + } else { + $error = 'nonumber'; + } + } + my $dellockoutcome = &Apache::lonnet::del('lti',['lock'],$cdom,$cnum); + } else { + $error = 'nolock'; + } + return ($id,$error); } sub get_sec_str { @@ -1462,8 +1609,12 @@ sub check_clone { sub store_changes { my ($cdom,$cnum,$prefs_order,$actions,$prefs,$values,$changes,$crstype) = @_; my ($chome,$output); - my (%storehash,@delkeys,@need_env_update,@oldcloner); + my (%storehash,@delkeys,@need_env_update,@oldcloner,%oldlinkprot); if ((ref($values) eq 'HASH') && (ref($changes) eq 'HASH')) { + if (ref($values->{'linkprotection'}) eq 'HASH') { + %oldlinkprot = %{$values->{'linkprotection'}}; + } + delete($values->{'linkprotection'}); %storehash = %{$values}; } else { if ($crstype eq 'Community') { @@ -1473,6 +1624,20 @@ sub store_changes { } return $output; } + my ($numchanges,$skipstore); + if (ref($changes) eq 'HASH') { + $numchanges = scalar(keys(%{$changes})); + if (($numchanges == 1) && (exists($changes->{'linkprotection'}))) { + $skipstore = 1; + } elsif (!$numchanges) { + if ($crstype eq 'Community') { + $output = &mt('No changes made to community settings.'); + } else { + $output = &mt('No changes made to course settings.'); + } + return $output; + } + } my %yesno = ( hidefromcat => '1', problem_stream_switch => '1', @@ -1485,7 +1650,7 @@ sub store_changes { if (grep(/^\Q$item\E$/,@{$actions})) { $output .= '

'.&mt($prefs->{$item}{'text'}).'

'; if (ref($changes->{$item}) eq 'HASH') { - if (keys(%{$changes->{$item}}) > 0) { + if ((keys(%{$changes->{$item}}) > 0) || ($item eq 'linkprotection')) { $output .= &mt('Changes made:').'