[LON-CAPA-cvs] cvs: loncom /interface domainprefs.pm londocs.pm lonexttool.pm

raeburn raeburn at source.lon-capa.org
Wed Nov 29 21:17:51 EST 2017


raeburn		Thu Nov 30 02:17:51 2017 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm londocs.pm lonexttool.pm 
  Log:
  - LON-CAPA as LTI Consumer.
    - Unique lis_result_sourcedid item created on tool launch for passback
      of grades from tool provider to LTI Consumer.
    - Unique 'ext_ims_lis_memberships_id created on tool launch for tool 
      provider to use to obtain course roster from LTI Consumer.
    - Domain configuration to specify how long after launch the two items can 
      be used -- days for passback of grades; seconds for retrieval of roster.
  - LONCAPA-prerequistes requires perl-UUID-Tiny to be added.
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.318 loncom/interface/domainprefs.pm:1.319
--- loncom/interface/domainprefs.pm:1.318	Thu Nov 30 01:52:14 2017
+++ loncom/interface/domainprefs.pm	Thu Nov 30 02:17:50 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.318 2017/11/30 01:52:14 raeburn Exp $
+# $Id: domainprefs.pm,v 1.319 2017/11/30 02:17:50 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2586,7 +2586,10 @@
 
 sub ltitools_javascript {
     my ($settings) = @_;
-    return unless(ref($settings) eq 'HASH');
+    my $togglejs = &ltitools_toggle_js();
+    unless (ref($settings) eq 'HASH') {
+        return $togglejs;
+    }
     my (%ordered,$total,%jstext);
     $total = 0;
     foreach my $item (keys(%{$settings})) {
@@ -2604,7 +2607,7 @@
     return <<"ENDSCRIPT";
 <script type="text/javascript">
 // <![CDATA[
-function reorderLTI(form,item) {
+function reorderLTITools(form,item) {
     var changedVal;
 $jstext
     var newpos = 'ltitools_add_pos';
@@ -2649,6 +2652,48 @@
 // ]]>
 </script>
 
+$togglejs
+
+ENDSCRIPT
+}
+
+sub ltitools_toggle_js {
+    return <<"ENDSCRIPT";
+<script type="text/javascript">
+// <![CDATA[
+
+function toggleLTITools(form,setting,item) {
+    var radioname = '';
+    var divid = '';
+    if ((setting == 'passback') || (setting == 'roster')) {
+        radioname = 'ltitools_'+setting+'_'+item;
+        divid = 'ltitools_'+setting+'time_'+item;
+        var num = form.elements[radioname].length;
+        if (num) {
+            var setvis = '';
+            for (var i=0; i<num; i++) {
+                if (form.elements[radioname][i].checked) {
+                    if (form.elements[radioname][i].value == '1') {
+                        if (document.getElementById(divid)) {
+                            document.getElementById(divid).style.display = 'inline-block';
+                        }
+                        setvis = 1;
+                    }
+                    break;
+                }
+            }
+        }
+        if (!setvis) {
+            if (document.getElementById(divid)) {
+                document.getElementById(divid).style.display = 'none';
+            }
+        }
+    }
+    return;
+}
+// ]]>
+</script>
+
 ENDSCRIPT
 }
 
@@ -3896,7 +3941,7 @@
                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
                 }
             }
-            my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
+            my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
             for (my $k=0; $k<=$maxnum; $k++) {
@@ -3923,7 +3968,7 @@
                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
                 ' value="'.$url.'" /></span>'.
                 (' 'x2).
-                '<span class="LC_nobreak">'.$lt{'key'}.
+                '<span class="LC_nobreak">'.$lt{'key'}.':'.
                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
                 (' 'x2).
                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
@@ -3969,21 +4014,36 @@
                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
                           '</textarea></div><div style=""></div><br />';
-            $datatable .= '<br />';
+            my %units = (
+                          'passback' => 'days',
+                          'roster'   => 'seconds',
+                        );
             foreach my $extra ('passback','roster') {
+                my $validsty = 'none';
+                my $currvalid;
                 my $checkedon = '';
                 my $checkedoff = ' checked="checked"';
                 if ($settings->{$item}->{$extra}) {
                     $checkedon = $checkedoff;
                     $checkedoff = '';
-                }
-                $datatable .= $lt{$extra}.' '.
-                              '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
-                              &mt('Yes').'</label>'.(' 'x2).
-                              '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
-                              &mt('No').'</label>'.(' 'x4);
+                    $validsty = 'inline-block';
+                    if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
+                        $currvalid = $settings->{$item}->{$extra.'valid'};
+                    }
+                }
+                my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
+                $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.' '.
+                              '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
+                              &mt('No').'</label>'.(' 'x2).
+                              '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
+                              &mt('Yes').'</label></span></div>'.
+                              '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
+                              '<span class="LC_nobreak">'.
+                              &mt("at least [_1] $units{$extra} after launch",
+                                  '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
+                              '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
             }
-            $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.': ';
+            $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.': ';
             if ($imgsrc) {
                 $datatable .= $imgsrc.
                               '<label><input type="checkbox" name="ltitools_image_del"'.
@@ -4083,7 +4143,7 @@
         }
     }
     $css_class = $itemcount%2?' class="LC_odd_row"':'';
-    my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
+    my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
                   '<select name="ltitools_add_pos"'.$chgstr.'>';
@@ -4134,14 +4194,28 @@
                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
                   '</div><div style=""></div><br />';
+    my %units = (
+                  'passback' => 'days',
+                  'roster'   => 'seconds',
+                );
+    my %defaulttimes = (
+                     'passback' => '7',
+                     'roster' => '300',
+                   );
     foreach my $extra ('passback','roster') {
-        $datatable .= $lt{$extra}.' '.
-                      '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
-                      &mt('Yes').'</label>'.(' 'x2).
-                      '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
-                      &mt('No').'</label>'.(' 'x4);
+        my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
+        $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.' '.
+                      '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
+                      &mt('No').'</label></span>'.(' 'x2).'<span class="LC_nobreak">'.
+                      '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
+                      &mt('Yes').'</label></span></div>'.
+                      '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
+                      '<span class="LC_nobreak">'.
+                      &mt("at least [_1] $units{$extra} after launch",
+                          '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
+                      '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
     }
-    $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.': '.
+    $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.': '.
                   '('.&mt('if larger than 21x21 pixels, image will be scaled').') ';
     if ($switchserver) {
         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
@@ -10104,8 +10178,15 @@
                 $confhash{$newid}{'display'}{'target'} = 'iframe';
             }
             foreach my $item ('passback','roster') {
-                if ($env{'form.ltitools_add_'.$item}) {
+                if ($env{'form.ltitools_'.$item.'_add'}) {
                     $confhash{$newid}{$item} = 1;
+                    if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
+                        my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
+                        $lifetime =~ s/^\s+|\s+$//g;
+                        if ($lifetime =~ /^\d+\.?\d*$/) {
+                            $confhash{$newid}{$item.'valid'} = $lifetime;
+                        }
+                    }
                 }
             }
             if ($env{'form.ltitools_add_image.filename'} ne '') {
@@ -10255,10 +10336,20 @@
                     foreach my $extra ('passback','roster') {
                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
                             $confhash{$itemid}{$extra} = 1;
+                            if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
+                                my $lifetime = $env{'form.ltitools_'.$extra.'valid_add'};
+                                $lifetime =~ s/^\s+|\s+$//g;
+                                if ($lifetime =~ /^\d+\.?\d*$/) {
+                                    $confhash{$itemid}{$extra.'valid'} = $lifetime;
+                                }
+                            }
                         }
                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
                             $changes{$itemid} = 1;
                         }
+                        if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
+                            $changes{$itemid} = 1;
+                        }
                     }
                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
                     foreach my $item ('label','title','target','linktext','explanation','append') {
@@ -10470,6 +10561,15 @@
                         $resulttext .= '<li>'.$lt{$item}.' ';
                         if ($confhash{$itemid}{$item}) {
                             $resulttext .= &mt('Yes');
+                            if ($confhash{$itemid}{$item.'valid'}) {
+                                if ($item eq 'passback') {
+                                    $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
+                                                           $confhash{$itemid}{$item.'valid'});
+                                } else {
+                                    $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
+                                                           $confhash{$itemid}{$item.'valid'});
+                                }
+                            }
                         } else {
                             $resulttext .= &mt('No');
                         }
Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.642 loncom/interface/londocs.pm:1.643
--- loncom/interface/londocs.pm:1.642	Thu Nov 30 01:52:14 2017
+++ loncom/interface/londocs.pm	Thu Nov 30 02:17:50 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.642 2017/11/30 01:52:14 raeburn Exp $
+# $Id: londocs.pm,v 1.643 2017/11/30 02:17:50 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -52,6 +52,7 @@
 use File::Copy;
 use Apache::lonlocal;
 use Cwd;
+use UUID::Tiny ':std';
 use LONCAPA qw(:DEFAULT :match);
 
 my $iconpath;
@@ -675,8 +676,8 @@
                     $toolhash{$item} = &unescape($toolhash{$item});
                 }
                 if (ref($ltitoolsref) eq 'HASH') {
-                    my @deleted;
                     if (ref($ltitoolsref->{$toolid}) eq 'HASH') {
+                        my @deleted;
                         $toolhash{'id'} = $toolid;
                         if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'tab') ||
                             ($toolhash{'target'} eq 'window')) {
@@ -751,6 +752,16 @@
                                 }
                             }
                         }
+                        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 $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$coursedom,$coursenum);
                         if ($putres eq 'ok') {
                             if (@deleted) {
Index: loncom/interface/lonexttool.pm
diff -u loncom/interface/lonexttool.pm:1.5 loncom/interface/lonexttool.pm:1.6
--- loncom/interface/lonexttool.pm:1.5	Thu Nov 30 01:52:14 2017
+++ loncom/interface/lonexttool.pm	Thu Nov 30 02:17:50 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Launch External Tool Provider (LTI)
 #
-# $Id: lonexttool.pm,v 1.5 2017/11/30 01:52:14 raeburn Exp $
+# $Id: lonexttool.pm,v 1.6 2017/11/30 02:17:50 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -46,6 +46,7 @@
 use Net::OAuth;
 use Encode;
 use Digest::SHA;
+use UUID::Tiny ':std';
 use HTML::Entities;
 use Apache::lonlocal;
 use Apache::lonnet;
@@ -94,9 +95,10 @@
     if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
         my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
         if ($toolsettings{'id'}) {
+            my $idx = $toolsettings{'id'};
             my %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
-            if (ref($ltitools{$toolsettings{'id'}}) eq 'HASH') {
-                my %toolhash = %{$ltitools{$toolsettings{'id'}}}; 
+            if (ref($ltitools{$idx}) eq 'HASH') {
+                my %toolhash = %{$ltitools{$idx}}; 
                 $toolhash{'display'} = {
                                            target => $toolsettings{'target'},
                                            width  => $toolsettings{'width'},
@@ -106,12 +108,25 @@
                 $toolhash{'crstitle'} = $toolsettings{'crstitle'};
                 $toolhash{'crsappend'} = $toolsettings{'crsappend'};
                 $is_tool = 1;
+                my $launchok = 1;
                 if ($target eq 'tex') {
                     $r->print(&mt('External Tool'));
                 } else {
+                    my $now = time;
+                    if ($toolhash{'passback'}) {
+                        unless (&set_callback_secret($cdom,$cnum,$marker,'grade',$now,
+                                                     \%toolsettings,\%toolhash) eq 'ok') {
+                            undef($launchok);
+                        }
+                    }
+                    if ($toolhash{'roster'}) {
+                        &set_callback_secret($cdom,$cnum,$marker,'roster',$now,
+                                             \%toolsettings,\%toolhash);
+                    }
                     my $submittext = &mt('Launch [_1]',$toolhash{'title'});
-                    if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) {
-                        my %lti = &lti_params($r,$cnum,$cdom,$submittext,\%toolhash);
+                    if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && 
+                        ($toolhash{'url'} ne '') && ($launchok)) {
+                        my %lti = &lti_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash);
                         my $url = $toolhash{'url'};
                         if ($toolhash{'crsappend'} ne '') {
                             $url .= $toolhash{'crsappend'};
@@ -135,15 +150,66 @@
     return OK;
 }
 
+sub set_callback_secret {
+    my ($cdom,$cnum,$marker,$name,$now,$toolsettings,$toolhash) = @_;
+    return unless ((ref($toolsettings) eq 'HASH') && (ref($toolhash) eq 'HASH'));
+    my $warning;
+    my ($needsnew,$oldsecret,$lifetime);
+    if ($name eq 'grade') {  
+        $lifetime = $toolhash->{'passbackvalid'}
+    } elsif ($name eq 'roster') {
+        $lifetime = $toolhash->{'rostervalid'};
+    }  
+    if ($toolsettings->{$name} eq '') {
+        $needsnew = 1;
+    } elsif (($toolsettings->{$name.'date'} + $lifetime) < $now) {
+        $oldsecret = $toolsettings->{$name.'secret'};
+        $needsnew = 1;
+    }
+    if ($needsnew) {
+        if (&get_tool_lock($cdom,$cnum,$marker,$now) eq 'ok') {
+            my $secret = UUID::Tiny::create_uuid_as_string(UUID_V4);
+            $toolhash->{$name.'secret'} = $secret;
+            my %secrethash = (
+                           $name.'secret' => $secret,
+                           $name.'secretdate' => $now,
+                          );
+            if ($oldsecret ne '') {
+                $secrethash{'old'.$name.'secret'} = $oldsecret;
+            }
+            my $putres = &Apache::lonnet::put('exttool_'.$marker,
+                                              \%secrethash,$cdom,$cnum);
+            my $delresult = &release_tool_lock($cdom,$cnum,$marker);
+            if ($delresult ne 'ok') {
+                $warning = $delresult ;
+            }
+            if ($putres eq 'ok') {
+                return 'ok';
+            }
+        } else {
+            $warning = '<span class="LC_error">'.
+                       &mt('Could not obtain exclusive lock').
+                       '</span>';
+        }
+    } else {
+        $toolhash->{$name.'secret'} = $toolsettings->{$name.'secret'};
+        return 'ok';
+    }
+    return;
+}
+
 sub lti_params {
-    my ($r,$cnum,$cdom,$submittext,$toolsref) = @_;
+    my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_;
     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
-        $crslabel,$crstitle,%fields,%rolesmap,%display,%custom, at userlangs);
+        $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
+        %display,%custom, at userlangs);
     if (ref($toolsref) eq 'HASH') {
         $version = $toolsref->{'version'};
         $toolname = $toolsref->{'title'};
         $passback = $toolsref->{'passback'};
+        $gradesecret = $toolsref->{'gradesecret'};
         $roster = $toolsref->{'roster'};
+        $rostersecret = $toolsref->{'rostersecret'};
         $msgtype = $toolsref->{'messagetype'};
         if (ref($toolsref->{'fields'}) eq 'HASH') {
             %fields = %{$toolsref->{'fields'}};
@@ -247,18 +313,42 @@
         my $crsprotocol = $Apache::lonnet::protocol{$crshome};
         unless ($crsprotocol eq 'https') {
             $crsprotocol = 'http';
-        } 
-        if ($passback) {
-            if ($ltirole eq 'Learner') {
+        }
+        if (($passback) || ($roster)) {
+            if ($passback) {
                 $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/passback';
                 $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
-                $ltiparams{'lis_result_sourcedid'} = ''; #FIXME
+                if ($gradesecret) {
+                    my $result_sig = 
+                        Digest::SHA::sha1_hex($gradesecret.':::'.$digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'});
+                    $ltiparams{'lis_result_sourcedid'} =
+                        $result_sig.':::'.$digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
+                }
             }
-        }
-        if ($roster) {
-            if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
-                $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/roster';
-                $ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME
+            if ($roster) {
+                if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
+                    $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/roster';
+                    if ($rostersecret) {
+                        my $roster_sig = Digest::SHA::sha1_hex($gradesecret.':::'.$digest_symb.':::'.$env{'request.course.id'});
+                        $ltiparams{'ext_ims_lis_memberships_id'} = $roster_sig.':::'.$digest_symb.':::'.$env{'request.course.id'};
+                    }
+                }
+            }
+            my %digesthash;
+            if ($ltiparams{'lis_result_sourcedid'}) {
+                $digesthash{$ltiparams{'lis_result_sourcedid'}} = "$idx\0".time; 
+            }
+            if ($ltiparams{'ext_ims_lis_memberships_id'}) {
+                $digesthash{$ltiparams{'ext_ims_lis_memberships_id'}} = "$idx\0".time; 
+            }
+            if (($digest_symb) && ($gradesecret || $rostersecret)) {
+                $digesthash{$digest_symb} = $symb;
+            }
+            if (($passback) && ($gradesecret)) {
+                $digesthash{$digest_user} = $uname.':'.$udom;
+            }
+            if (keys(%digesthash)) {
+                &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum);
             }
         }
     }
@@ -292,7 +382,7 @@
         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
     }
     if ($fields{'user'}) {
-        $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; 
+        $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
     }
     if (keys(%custom)) {
         foreach my $key (keys(%custom)) {
@@ -352,6 +442,7 @@
 sub sign_params {
     my ($url,$key,$secret,$paramsref) = @_;
     my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0)));
+
     my $request = Net::OAuth->request("request token")->new(
             consumer_key => $key,
             consumer_secret => $secret,
@@ -368,4 +459,37 @@
     return $request->to_hash();
 }
 
+sub get_tool_lock {
+    my ($cdom,$cnum,$marker,$now) = @_;
+    # get lock for tool for which gradesecret is being set
+    my $lockhash = {
+                  $marker."\0".'lock' => $now.':'.$env{'user.name'}.
+                                         ':'.$env{'user.domain'},
+                   };
+    my $tries = 0;
+    my $gotlock = &Apache::lonnet::newput('exttools',$lockhash,$cdom,$cnum);
+
+    while (($gotlock ne 'ok') && $tries <3) {
+        $tries ++;
+        sleep(1);
+        $gotlock = &Apache::lonnet::newput('exttools',$lockhash,$cdom,$cnum);
+    }
+    return $gotlock;
+}
+
+sub release_tool_lock {
+    my ($cdom,$cnum,$marker) = @_;
+    #  remove lock
+    my @del_lock = ($marker."\0".'lock');
+    my $dellockoutcome=&Apache::lonnet::del('exttools',\@del_lock,$cdom,$cnum);
+    if ($dellockoutcome ne 'ok') {
+        return ('<div class="LC_error">'
+               .&mt('Warning: failed to release lock for exttool: [_1].','<tt>'.$marker.'</tt>')
+               .'</div>'
+               );
+    } else {
+        return 'ok';
+    }
+}
+
 1;


More information about the LON-CAPA-cvs mailing list