[LON-CAPA-cvs] cvs: loncom /interface groupboards.pm /lonnet/perl lonnet.pm

raeburn raeburn at source.lon-capa.org
Sun Nov 11 12:33:27 EST 2012


raeburn		Sun Nov 11 17:33:27 2012 EDT

  Modified files:              
    /loncom/interface	groupboards.pm 
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  - Code to obtain unique unix timestamp-based identifier for new group board
    moved to lonnet.pm -- &get_timebased_id() -- to facilitate re-use. 
  - perldoc added for get_timebased_id().  
  
  
-------------- next part --------------
Index: loncom/interface/groupboards.pm
diff -u loncom/interface/groupboards.pm:1.15 loncom/interface/groupboards.pm:1.16
--- loncom/interface/groupboards.pm:1.15	Sat Jul 21 21:20:06 2012
+++ loncom/interface/groupboards.pm	Sun Nov 11 17:33:18 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Group Bulletin Boards Manager
 #
-# $Id: groupboards.pm,v 1.15 2012/07/21 21:20:06 raeburn Exp $
+# $Id: groupboards.pm,v 1.16 2012/11/11 17:33:18 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -105,7 +105,7 @@
         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
             $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
                                      $ucgpterm,$bodytitle,$refarg));
-            my ($outcome,$newurl,$bbtitle,$dellockoutcome) = 
+            my ($outcome,$newurl,$bbtitle,$lockfreed) = 
 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
             if ($outcome eq 'ok') {
                 my ($furl,$ferr)= &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
@@ -117,8 +117,10 @@
                           '<a href="/adm/groupboards?group='.$group.'&'.
                           $refarg.'">'.&mt('View all group discussion boards').
                           '</a></td></tr></table>');
-                if ($dellockoutcome ne 'ok') {
-                    $r->print(&mt('There was a problem removing a lockfile for the group ([_1]). This may prevent creation of additional bulletin boards in this group.  Please contact the system administrator for your LON-CAPA domain.'));
+                if ($lockfreed ne 'ok') {
+                    $r->print(&mt('There was a problem removing a lockfile for the group ([_1]).',$description).'<br />'.
+                              &mt('This may prevent creation of additional bulletin boards in this group.').'<br />'.
+                              &mt('Please contact the domain coordinator for your LON-CAPA domain.'));
                 }
             } else {
                 $r->print(&mt('There was a problem creating the new discussion board - [_1]','<span class="LC_error">'.$outcome.'</span>').'<br /><a href="/adm/groupboards?group='.$group.'">'.
@@ -191,56 +193,18 @@
 
 sub create_board {
     my ($cdom,$cnum,$group,$bbtitle) = @_;
-    my ($outcome,$boardid,$newurl);
+    my ($outcome,$newurl,$idtype);
     $bbtitle=&unescape($bbtitle);
-    # get lock on nohist_groupboards file
-    my $lockhash = {
-                  $group."\0".'locked_boardids' => $env{'user.name'}.
-                                                       ':'.$env{'user.domain'},
-                   };
-    my $tries = 0;
-    my $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
-    my $dellockoutcome;
-    while (($gotlock ne 'ok') && $tries <3) {
-        $tries ++;
-        sleep 1;
-        $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
-    }
-    if ($gotlock eq 'ok') {
-       my %curr_boards = &Apache::lonnet::dump('nohist_groupboards',$cdom,$cnum,$group);
-       $boardid = time;
-       my $idtries = 0;
-       while(exists($curr_boards{$group."\0".$boardid}) && $idtries < 20) {
-           $boardid ++;
-           $idtries ++;
-       }
-       if (!exists($curr_boards{$group."\0".$boardid})) {
-           my %new_board = (
-                          $group."\0".$boardid => $env{'user.name'}.':'.
-                                                  $env{'user.domain'},
-           );
-           my $putresult = &Apache::lonnet::put('nohist_groupboards',\%new_board,
-                                                $cdom,$cnum);
-           if ($putresult ne 'ok') {
-               $outcome = 'error saving new board: '.$putresult;
-           } else {
-               $newurl = '/adm/'.$cdom.'/'.$cnum.'/'.$boardid.
-                         '/bulletinboard';
-           }
-       } else {
-            $outcome = ('error: no unique ID for the new board available.');
-       }
-       #  remove lock
-       my @del_lock = ($group."\0".'locked_boardids');
-       $dellockoutcome = &Apache::lonnet::del('nohist_groupboards',\@del_lock,$cdom,$cnum);
+    $idtype = 'inc';
+    my ($boardid,$dellock,$error) = 
+        &Apache::lonnet::get_timebased_id($group,'boardids','groupboards',
+                                          $cdom,$cnum);
+    if ($boardid) {
+         $newurl = '/adm/'.$cdom.'/'.$cnum.'/'.$boardid.'/bulletinboard';
     } else {
-        $outcome = "error: could not obtain lockfile\n";
-        $dellockoutcome = 'ok';
+         return ($error,$newurl,$bbtitle,$dellock);
     }
-    if (!$newurl) {
-        return ($outcome,$newurl,$bbtitle,$dellockoutcome);
-    }
-    $newurl=&unescape($newurl);
+
     # need to check here if group_boards_$group.sequence is in the course
     # if not - add it as an item in group_folder_$group.sequence 
     my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
@@ -268,7 +232,7 @@
         $outcome = 'error: discussion boards folder absent, '.
                    'or in unexpected location - '.$allbbsmap."\n";
     }
-    return ($outcome,$newurl,$bbtitle,$dellockoutcome);
+    return ($outcome,$newurl,$bbtitle,$dellock);
 }
 
 sub display_error {
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1197 loncom/lonnet/perl/lonnet.pm:1.1198
--- loncom/lonnet/perl/lonnet.pm:1.1197	Sun Nov 11 01:48:33 2012
+++ loncom/lonnet/perl/lonnet.pm	Sun Nov 11 17:33:26 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1197 2012/11/11 01:48:33 raeburn Exp $
+# $Id: lonnet.pm,v 1.1198 2012/11/11 17:33:26 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2694,7 +2694,7 @@
             my $allowed = 0;
             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                 if ((&allowed('mdg',$env{'request.course.id'}.
-                            ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
+                              ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                     $allowed = 1;
                 }
@@ -5691,6 +5691,88 @@
     return &reply("tmpdel:$token",$server);
 }
 
+# ------------------------------------------------------------ get_timebased_id 
+
+sub get_timebased_id {
+    my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
+        $maxtries) = @_;
+    my ($newid,$error,$dellock);
+    unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
+        return ('','ok','invalid call to get suffix');
+    }
+
+# set defaults for any optional args for which values were not supplied
+    if ($who eq '') {
+        $who = $env{'user.name'}.':'.$env{'user.domain'};
+    }
+    if (!$locktries) {
+        $locktries = 3;
+    }
+    if (!$maxtries) {
+        $maxtries = 10;
+    }
+    
+    if (($cdom eq '') || ($cnum eq '')) {
+        if ($env{'request.course.id'}) {
+            $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+            $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+        }
+        if (($cdom eq '') || ($cnum eq '')) {
+            return ('','ok','call to get suffix not in course context');
+        }
+    }
+
+# construct locking item
+    my $lockhash = {
+                      $prefix."\0".'locked_'.$keyid => $who,
+                   };
+    my $tries = 0;
+
+# attempt to get lock on nohist_$namespace file
+    my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
+    while (($gotlock ne 'ok') && $tries <$locktries) {
+        $tries ++;
+        sleep 1;
+        $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
+    }
+
+# attempt to get unique identifier, based on current timestamp
+    if ($gotlock eq 'ok') {
+        my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
+        my $id = time;
+        $newid = $id;
+        my $idtries = 0;
+        while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
+            if ($idtype eq 'concat') {
+                $newid = $id.$idtries;
+            } else {
+                $newid ++;
+            }
+            $idtries ++;
+        }
+        if (!exists($inuse{$prefix."\0".$newid})) {
+            my %new_item =  (
+                              $prefix."\0".$newid => $who,
+                            );
+            my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
+                                                 $cdom,$cnum);
+            if ($putresult ne 'ok') {
+                undef($newid);
+                $error = 'error saving new item: '.$putresult;
+            }
+        } else {
+             $error = ('error: no unique suffix available for the new item ');
+        }
+#  remove lock
+        my @del_lock = ($prefix."\0".'locked_'.$keyid);
+        $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
+    } else {
+        $error = "error: could not obtain lockfile\n";
+        $dellock = 'ok';
+    }
+    return ($newid,$dellock,$error);
+}
+
 # -------------------------------------------------- portfolio access checking
 
 sub portfolio_access {
@@ -13216,6 +13298,8 @@
  
  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
 
+=item *
+
 modify_access_controls():
 
 Modifies access controls for a portfolio file
@@ -13233,7 +13317,51 @@
 3. reference to hash of any new or updated access controls.
 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
    key = integer (inbound ID)
-   value = uniqueID  
+   value = uniqueID
+
+=item *
+
+get_timebased_id():
+
+Attempts to get a unique timestamp-based suffix for use with items added to a 
+course via the Course Editor (e.g., folders, composite pages, 
+group bulletin boards).
+
+Args: (first three required; six others optional)
+
+1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
+   docssequence, or name of group
+
+2. keyid (alphanumeric): name of temporary locking key in hash,
+   e.g., num, boardids
+
+3. namespace: name of gdbm file used to store suffixes already assigned;  
+   file will be named nohist_namespace.db
+
+4. cdom: domain of course; default is current course domain from %env
+
+5. cnum: course number; default is current course number from %env
+
+6. idtype: set to concat if an additional digit is to be appended to the 
+   unix timestamp to form the suffix, if the plain timestamp is already
+   in use.  Default is to not do this, but simply increment the unix 
+   timestamp by 1 until a unique key is obtained.
+
+7. who: holder of locking key; defaults to user:domain for user.
+
+8. locktries: number of attempts to obtain a lock (sleep of 1s before 
+   retrying); default is 3.
+
+9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
+
+Returns:
+
+1. suffix obtained (numeric)
+
+2. result of deleting locking key (ok if deleted, or lock never obtained)
+
+3. error: contains (localized) error message if an error occurred.
+
 
 =back
 


More information about the LON-CAPA-cvs mailing list