--- loncom/lonnet/perl/lonnet.pm	2012/11/09 17:27:18	1.1195
+++ loncom/lonnet/perl/lonnet.pm	2012/11/27 23:45:22	1.1199
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1195 2012/11/09 17:27:18 raeburn Exp $
+# $Id: lonnet.pm,v 1.1199 2012/11/27 23:45:22 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2653,13 +2653,16 @@ sub allowuploaded {
 # (b) When displaying folder contents in course editor, used to determine if
 #     "Edit" link will be displayed alongside resource.
 #
-#  input: 3 args -- filename (decluttered), course number and course domain.
-#  output: array of four scalars -- 
+#  input: six args -- filename (decluttered), course number, course domain,
+#                   url, symb (if registered) and group (if this is a group
+#                   item -- e.g., bulletin board, group page etc.).
+#  output: array of five scalars -- 
 #          $cfile -- url for file editing if editable on current server
 #          $home -- homeserver of resource (i.e., for author if published,
 #                                           or course if uploaded.).
 #          $switchserver --  1 if server switch will be needed.
-#          $uploaded -- 1 if resource is a file uploaded to a course.
+#          $forceedit -- 1 if icon/link should be to go to edit mode 
+#          $forceview -- 1 if icon/link should be to go to view mode
 #
 
 sub can_edit_resource {
@@ -2668,7 +2671,7 @@ sub can_edit_resource {
 #
 # For aboutme pages user can only edit his/her own.
 #
-    if ($resurl =~ m{^/adm/($match_domain)/($match_username)/aboutme$}) {
+    if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
         my ($sdom,$sname) = ($1,$2);
         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
             $home = $env{'user.home'};
@@ -2689,15 +2692,15 @@ sub can_edit_resource {
         if ($group ne '') {
 # if this is a group homepage or group bulletin board, check group privs
             my $allowed = 0;
-            if ($resurl =~ m{^/adm/$cdom/$cnum/$group/smppg$}) {
-                if ((&Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
-                            ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
+            if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
+                if ((&allowed('mdg',$env{'request.course.id'}.
+                              ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                     $allowed = 1;
                 }
-            } elsif ($resurl =~ m{^/adm/$cdom/$cnum/\d+/bulletinboard$}) {
-                unless ((&allowed(&Apache::lonnet::allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) ||
-                        (&allowed('cgb',$env{'request.course.id'}.$group)) || $crsedit) {
+            } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
+                if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
+                        (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                     $allowed = 1;
                 }
             }
@@ -2748,12 +2751,17 @@ sub can_edit_resource {
                         $forceedit = 1;
                     }
                     $cfile = $resurl;
-                } elsif (($resurl eq '/res/lib/templates/simpleproblem.problem')) {
+                } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
                     $incourse = 1;
                     $cfile = $resurl.'/smpedit';
-                } elsif ($resurl =~ /ext/) {
+                } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
                     $incourse = 1;
-                    # is external
+                    if ($env{'form.forceedit'}) {
+                        $forceview = 1;
+                    } else {
+                        $forceedit = 1;
+                    }
+                    $cfile = $resurl;
                 }
             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                 my $template = '/res/lib/templates/simpleproblem.problem';
@@ -2762,6 +2770,26 @@ sub can_edit_resource {
                     $forceview = 1;
                     $cfile = $template;
                 }
+            } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
+                    $incourse = 1;
+                    if ($env{'form.forceedit'}) {
+                        $forceview = 1;
+                    } else {
+                        $forceedit = 1;
+                    }
+                    $cfile = $resurl;
+            } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
+                $incourse = 1;
+                $forceview = 1;
+                if ($symb) {
+                    my ($map,$id,$res)=&decode_symb($symb);
+                    $env{'request.symb'} = $symb;
+                    $cfile = &clutter($res);
+                } else {
+                    $cfile = $env{'form.suppurl'};
+                    $cfile =~ s{^http://}{};
+                    $cfile = '/adm/wrapper/ext/'.$cfile;
+                }
             }
         }
         if ($uploaded || $incourse) {
@@ -5688,6 +5716,88 @@ sub tmpdel {
     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 {
@@ -10177,6 +10287,72 @@ sub gettitle {
     return $title;
 }
 
+sub getdocspath {
+    my ($symb) = @_;
+    my $path;
+    if ($symb) {
+        my ($mapurl,$id,$resurl) = &decode_symb($symb);
+        if ($resurl=~/\.(sequence|page)$/) {
+            $mapurl=$resurl;
+        } elsif ($resurl eq 'adm/navmaps') {
+            $mapurl=$env{'course.'.$env{'request.course.id'}.'.url'};
+        }
+        my $mapresobj;
+        my $navmap = Apache::lonnavmaps::navmap->new();
+        if (ref($navmap)) {
+            $mapresobj = $navmap->getResourceByUrl($mapurl);
+        }
+        $mapurl=~s{^.*/([^/]+)\.(\w+)$}{$1};
+        my $type=$2;
+        if (ref($mapresobj)) {
+            my $pcslist = $mapresobj->map_hierarchy();
+            if ($pcslist ne '') {
+                foreach my $pc (split(/,/,$pcslist)) {
+                    next if ($pc <= 1);
+                    my $res = $navmap->getByMapPc($pc);
+                    if (ref($res)) {
+                        my $thisurl = $res->src();
+                        $thisurl=~s{^.*/([^/]+)\.\w+$}{$1};
+                        my $thistitle = $res->title();
+                        $path .= '&'.
+                                 &Apache::lonhtmlcommon::entity_encode($thisurl).'&'.
+                                 &Apache::lonhtmlcommon::entity_encode($thistitle).
+                                 ':'.$res->randompick().
+                                 ':'.$res->randomout().
+                                 ':'.$res->encrypted().
+                                 ':'.$res->randomorder();
+                    }
+                }
+            }
+            $path =~ s/^\&//;
+            my $maptitle = $mapresobj->title();
+            if ($mapurl eq 'default') {
+                $maptitle = 'Main Course Documents';
+            }
+            $path .= ($path ne '')? '&' : ''.
+                    &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
+                    &Apache::lonhtmlcommon::entity_encode($maptitle).
+                    ':'.$mapresobj->randompick().
+                    ':'.$mapresobj->randomout().
+                    ':'.$mapresobj->encrypted().
+                    ':'.$mapresobj->randomorder();
+        } else {
+            my $maptitle = &gettitle($mapurl);
+            if ($mapurl eq 'default') {
+                $maptitle = 'Main Course Documents';
+            }
+            $path = &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
+                    &Apache::lonhtmlcommon::entity_encode($maptitle).'::::';
+        }
+        unless ($mapurl eq 'default') {
+            $path = 'default&'.
+                    &Apache::lonhtmlcommon::entity_encode('Main Course Documents').
+                    '::::&'.$path;
+        }
+    }
+    return $path;
+}
+
 sub get_slot {
     my ($which,$cnum,$cdom)=@_;
     if (!$cnum || !$cdom) {
@@ -12260,9 +12436,10 @@ provided for types, will default to retu
 =item *
 
 in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
-user: $uname:$udom has a role in the course: $cdom_$cnum. Additional
-optional arguments are: $type (if role checking is to be restricted to
-certain user status types -- previous (expired roles), active (currently
+user: $uname:$udom has a role in the course: $cdom_$cnum. 
+
+Additional optional arguments are: $type (if role checking is to be restricted 
+to certain user status types -- previous (expired roles), active (currently
 available roles) or future (roles available in the future), and
 $hideprivileged -- if true will not report course roles for users who
 have active Domain Coordinator or Super User roles.
@@ -12631,22 +12808,20 @@ forcing spreadsheet to reevaluate the re
 
 =item * 
 
-can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group)
-
-Determine if the current user should be able to edit a particular resource,
+can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
 when viewing in course context.
-(a) When viewing resource used to determine if "Edit" item is included in
-     Functions.
-(b) When displaying folder contents in course editor, used to determine if
-    "Edit" link will be displayed alongside resource.
 
- input: 3 args -- filename (decluttered), course number and course domain.
- output: array of four scalars --
+ input: six args -- filename (decluttered), course number, course domain,
+                    url, symb (if registered) and group (if this is a 
+                    group item -- e.g., bulletin board, group page etc.).
+
+ output: array of five scalars --
          $cfile -- url for file editing if editable on current server
          $home -- homeserver of resource (i.e., for author if published,
                                           or course if uploaded.).
          $switchserver --  1 if server switch will be needed.
-         $uploaded -- 1 if resource is a file uploaded to a course.
+         $forceedit -- 1 if icon/link should be to go to edit mode 
+         $forceview -- 1 if icon/link should be to go to view mode
 
 =item *
 
@@ -13214,6 +13389,8 @@ Internal notes:
  
  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
@@ -13231,7 +13408,51 @@ Returns:
 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