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

raeburn lon-capa-cvs@mail.lon-capa.org
Mon, 20 Nov 2006 23:50:52 -0000


This is a MIME encoded message

--raeburn1164066652
Content-Type: text/plain

raeburn		Mon Nov 20 18:50:52 2006 EDT

  Modified files:              
    /loncom/interface	loncoursegroups.pm 
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  Delete a group.  groupnames of deleted groups may not be used as names of new groups in the same course.  Still need to decide what action to take about access controls for group portfolio files in deleted groups.
  
  
--raeburn1164066652
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20061120185052.txt"

Index: loncom/interface/loncoursegroups.pm
diff -u loncom/interface/loncoursegroups.pm:1.63 loncom/interface/loncoursegroups.pm:1.64
--- loncom/interface/loncoursegroups.pm:1.63	Thu Nov  2 16:06:06 2006
+++ loncom/interface/loncoursegroups.pm	Mon Nov 20 18:49:49 2006
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: loncoursegroups.pm,v 1.63 2006/11/02 21:06:06 albertel Exp $
+# $Id: loncoursegroups.pm,v 1.64 2006/11/20 23:49:49 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -92,12 +92,13 @@
 
     my $action = $env{'form.action'};
     my $state = $env{'form.state'};
-    if ((!defined($action)) || ($action eq 'view') || ($action eq 'modify')) {
+    if ((!defined($action)) || ($action eq 'view') || ($action eq 'modify') || ($action eq 'delete')) {
         if (!defined($state)) {
             $state = 'view';
         }
     }
-    if ($action eq 'create' || $action eq 'modify' || $action eq 'view') { 
+    if ($action eq 'create' || $action eq 'modify' || $action eq 'view' || 
+        $action eq 'delete') { 
         if ($view_permission || $manage_permission) {
             if ($state eq 'view') {
                 &print_main_menu($r,$cdom,$cnum,\%functions,\%idx,
@@ -165,7 +166,7 @@
                          $env{'form.refpage'}.'&state=pick_task&groupname=',
       view => '<a href="/adm/'.$cdom.'/'.$cnum.'/',
       delete => '<a href="/adm/coursegroups?action=delete&refpage='.
-                         $env{'form.refpage'}.'&groupname=',
+                         $env{'form.refpage'}.'&state=verify&groupname=',
     );
     my %lt = &Apache::lonlocal::texthash( 
                           modify => 'Modify',
@@ -305,7 +306,9 @@
                         if (($manage_permission) && 
                             ($env{'form.refpage'} ne 'enrl')) {
                             $link .= '&nbsp;&nbsp;'.$actionlinks{'modify'}.
-                                      $group.'">'.$lt{'modify'}.'</a>';
+                                      $group.'">'.$lt{'modify'}.'</a>'.
+                                     '&nbsp;&nbsp;'.$actionlinks{'delete'}.
+                                      $group.'">'.$lt{'delete'}.'</a>';
                         }
                     }
                     $r->print(&Apache::loncommon::start_data_table_row('LC_data_table_dense').
@@ -808,6 +811,7 @@
     my %branchstates = ();
     @{$states{'create'}} = ('pick_name','pick_members','pick_privs','result');
     @{$states{'modify'}} = ('pick_task');
+    @{$states{'delete'}} = ('verify','result');
     @{$branchstates{'noprivs'}} = ('result');
     @{$branchstates{'settings'}} = ('change_settings','chgresult');
     @{$branchstates{'members'}} = ('change_members','change_privs','memresult');
@@ -818,7 +822,7 @@
         push (@{$states{$action}},@{$branchstates{$env{'form.branch'}}});
     }
 
-    if (($action eq 'create') || ($action eq 'modify')) {
+    if (($action eq 'create') || ($action eq 'modify') || ($action eq 'delete')) {
         my $done = 0;
         my $i=0;
         while ($i<@{$states{$action}} && !$done) {
@@ -839,9 +843,9 @@
         ({href=>"/adm/dropadd",
           text=>"Enrollment Manager",
           faq=>9,bug=>'Instructor Interface',});
-        if ($action eq 'modify') {
+        if ($action eq 'modify' || $action eq 'delete') {
             &Apache::lonhtmlcommon::add_breadcrumb
-            ({href=>"/adm/coursegroups?refpage=enrl&action=modify",
+            ({href=>"/adm/coursegroups?refpage=enrl&action=$action",
               text=>"Groups",
               faq=>9,bug=>'Instructor Interface',});
         }
@@ -877,6 +881,10 @@
                             memresult => 'Modifications Complete',
                             addresult => 'Additions Complete',
                           );
+    %{$trail{'delete'}} = &Apache::lonlocal::texthash(
+                            verify => 'Verify deletion',
+                            result => 'Deletion Complete'
+                          );
     my %navbuttons = &Apache::lonlocal::texthash(
                              gtns => 'Go to next step',
                              gtps => 'Go to previous step',
@@ -885,7 +893,7 @@
                              gtpp => 'Go to previous page',
                              adme => 'Add members',
     );
-    if ((($action eq 'create') || ($action eq 'modify')) &&
+    if ((($action eq 'create') || ($action eq 'modify') || ($action eq 'delete')) &&
               ($manage_permission)) {
         for (my $i=0; $i<@{$states{$action}}; $i++) {
             if ($state eq $states{$action}[$i]) {
@@ -903,16 +911,18 @@
 				 $crstype);
                 last;
             } else {
-                if (($state eq 'result') && ($i > 0)) {
-                    &Apache::lonhtmlcommon::add_breadcrumb(
+                if (($action eq 'create') || ($action eq 'modify')) {
+                    if (($state eq 'result') && ($i > 0)) {
+                        &Apache::lonhtmlcommon::add_breadcrumb(
     {href=>"javascript:backPage(document.$state,'$states{$action}[0]')",
       text=>"$trail{$action}{$states{$action}[$i]}"});
-                } else { 
-                    &Apache::lonhtmlcommon::add_breadcrumb(
+                    } else { 
+                        &Apache::lonhtmlcommon::add_breadcrumb(
      {href=>"javascript:backPage(document.$state,'$states{$action}[$i]')",
       text=>"$trail{$action}{$states{$action}[$i]}"});
+                    }
                 }
-            }
+            }             
         }
     } elsif (($action eq 'view') && ($view_permission)) {
                         &Apache::lonhtmlcommon::add_breadcrumb(
@@ -922,7 +932,6 @@
         &display_groups($r,$cdom,$cnum,$functions,$idx,$view_permission,
 			$manage_permission,$action,$state,$gpterm,$ucgpterm,
 			$crstype);
-
     }
     $r->print(&footer());
     return;
@@ -1069,7 +1078,138 @@
                              $sections,$states,$navbuttons,$memchg,
                              $sectioncount,$stored,$gpterm,$ucgpterm,$crstype);
         }
+    } elsif ($action eq 'delete') {
+        my %stored = &retrieve_settings($cdom,$cnum,$groupname);
+        if ($state eq 'verify') {
+            &verify_delete($r,$groupname,$state,$action,$page,$states,
+                           \%stored);
+        } elsif ($state eq 'result') {
+            &delete_group($r,$cdom,$cnum,$groupname);
+        }
+    }
+}
+
+sub verify_delete {
+    my ($r,$groupname,$formname,$action,$page,$states,$stored) = @_;
+    $r->print(&Apache::lonhtmlcommon::echo_form_input([]));
+    $r->print(&mt("You have requested deletion of the following group: ").'<i>'.
+              $stored->{'description'}.'</i>'.
+              '<br /><br />'.&mt('When a group is deleted the following occurs:').'<ul>'.
+              '<li>'.&mt('All group membership is terminated.').'</li>'.
+              '<li>'.&mt('The group ceases to be available either for viewing or for modification of group settings and membership.').'</li>'.
+              '<li>'.&mt('The group folder is removed from the folder containing it - normally this is the "Course Groups" folder which contains folders for all groups in the course.').'</li>'.
+              '</ul>'.&mt('Although a deleted group is no longer accessible, the group name used for the group will be reserved, and will not be available for assignment to a new group in the same course in the future.'));
+    my $prevtext = &mt('Go back');
+    my $nexttext = &mt('Delete group');
+    my $prev;
+    if ($env{'form.refpage'} eq 'enrl')  {
+        $prev = 'view';
+    }
+    &display_navbuttons($r,$formname,$prev,$prevtext,
+                        $$states{$action}[$page+1],$nexttext);
+    return;
+}
+
+sub delete_group {
+    my ($r,$cdom,$cnum,$groupname) = @_;
+    my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
+                                                           $groupname);
+    my $now = time;
+    my $num_users = 0;
+    my $num_fail = 0;
+    my $num_ok = 0;
+    my @deleted;
+    my @undeleted;
+    my %usersettings;
+    foreach my $key (sort(keys(%membership))) {
+        if ($key =~ /^\Q$groupname\E:([^:]+:[^:]+)$/) {
+            my $user = $1;
+            my($end,$start,@userprivs) = split(/:/,$membership{$key});
+            if ($start != -1) {
+                $num_users ++;
+                $usersettings{$groupname.':'.$user} = $now.':-1:';
+                if (&Apache::lonnet::modify_group_roles($cdom,$cnum,
+                                                        $groupname,$user,
+                                                        $now,'-1','')
+                    eq 'ok') {
+                    $num_ok ++;
+                    push(@deleted,$user);
+                } else {
+                    push(@undeleted,$user);
+                    $num_fail ++;
+                }
+            }
+        }
+    }
+    if ($num_ok > 0) {
+        my $roster_result = 
+            &Apache::lonnet::modify_coursegroup_membership($cdom,$cnum,
+                                                           \%usersettings);
+    }
+    if ($num_fail > 0) {
+        $r->print(&mt('Group deletion failed because deletion of [_1] out of [_2] members failed',$num_fail,$num_users));
+        
+    } else {
+        my ($result,$message) = 
+             &Apache::lonnet::delete_coursegroup($cdom,$cnum,$groupname);
+        if ($result eq 'ok') {
+            my $outcome = &modify_folders($cdom,$cnum,$groupname);
+            if ($outcome eq '') {
+                $r->print(&mt('Group successfully deleted'));
+            } else {
+                $r->print(&mt("Although the group was deleted, an error ([_1]) occurred when removing the group's folder from the 'Course Groups' folder.",$outcome));
+            }
+        } else {
+            $r->print(&mt('Group deletion failed'));
+        }
     }
+    return;
+}
+
+sub modify_folders {
+    my ($cdom,$cnum,$groupname) = @_;
+    my $outcome;
+    my $navmap = Apache::lonnavmaps::navmap->new();
+    my $groupmap = '/uploaded/'.$cdom.'/'.$cnum.'/'.'group_folder_'.
+                   $groupname.'.sequence';
+    my $groupmapres = $navmap->getResourceByUrl($groupmap);
+    if ($groupmapres) {
+        my ($map,$id,$src)=&Apache::lonnet::decode_symb($groupmapres->symb());
+        $map = '/'.$map;
+        my ($errtext,$fatal) = &LONCAPA::map::mapread($map);
+        if ($fatal) {
+            $outcome=&mt('Error reading contents of parent folder to group').
+                         " ($map): $errtext".'<br />';
+        } else {
+            my $idx = 0;
+            my $grpidx;
+            foreach my $item (@LONCAPA::map::order) {
+                my ($name,$url)=split(/\:/,$LONCAPA::map::resources[$item]);
+                $url=&LONCAPA::map::qtescape($url);
+                if ($url eq $groupmap) {
+                    $grpidx = $idx;
+                    last;
+                } else {
+                    $idx++;
+                }
+            }
+
+            if ($grpidx ne '') {
+                &LONCAPA::map::makezombie($LONCAPA::map::order[$grpidx]);
+                for (my $i=$grpidx;$i<$#LONCAPA::map::order;$i++) {
+                    $LONCAPA::map::order[$i] = $LONCAPA::map::order[$i+1];
+                }
+                $#LONCAPA::map::order--;
+                my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1);
+                if ($outtext) {
+                    $outcome = &mt('Error storing updated parent folder to group'). " ($map): $errtext".'<br />';
+                    return;
+                }
+            }
+        }
+    }
+    undef($navmap);
+    return $outcome;
 }
 
 sub header {
@@ -1543,6 +1683,11 @@
       <input type="button" name="previous" value = "'.$prevtext.'"
     onclick="javascript:backPage(document.'.$formname.','."'".$prev."'".')"/>
    &nbsp;&nbsp;&nbsp;');
+    } elsif ($prevtext) {
+        $r->print('
+      <input type="button" name="previous" value = "'.$prevtext.'"
+    onclick="javascript:history.back()"/>
+   &nbsp;&nbsp;&nbsp;');
     }
     if ($next) {
         $r->print('
@@ -3458,7 +3603,7 @@
                                                  ':false:normal:res';
         $LONCAPA::map::order[1+$#LONCAPA::map::order]=$newidx;
         my ($outtext,$errtext) = &LONCAPA::map::storemap($parentmap,1);
-        if ($errtext) {
+        if ($outtext) {
             $outcome = &mt('Error storing updated parent folder')." ($parentmap):  $errtext".'<br />';
             return $outcome;
         }
@@ -3568,7 +3713,11 @@
     my ($groupname,$action,$cdom,$cnum,$gpterm,$ucgpterm,$crstype) = @_;
     my %sectioncount = &Apache::loncommon::get_sections($cdom,$cnum);
     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
-
+    my %deleted_groups = &Apache::lonnet::get_deleted_groups($cdom,$cnum);
+    if (my $tmp = &Apache::lonnet::error(%deleted_groups)) {
+        undef(%deleted_groups);
+        &Apache::lonnet::logthis('Error retrieving groups: '.$tmp.' in '.$cnum.':'.$cdom);
+    }
     my %lt = &Apache::lonlocal::texthash (
                       igna => "Invalid $gpterm name",
                       tgne => "The $gpterm name entered ",
@@ -3595,12 +3744,14 @@
 	return $exitmsg.$lt{'cnnb'}.&mt('a section').$lt{'inth'}.
 	    '<br />'.$lt{'grna'};
     }
-    if ($action eq 'create' 
-	&& exists($curr_groups{$groupname})) {
-
-	return $exitmsg.$lt{'cnnb'}.&mt('an existing [_1]',$gpterm).
-	    $lt{'inth'}.'<br />'.$lt{'grna'};
-
+    if ($action eq 'create') { 
+	if (exists($curr_groups{$groupname})) {
+	    return $exitmsg.$lt{'cnnb'}.&mt('an existing [_1]',$gpterm).
+	           $lt{'inth'}.'.<br />'.$lt{'grna'};
+        } elsif (exists($deleted_groups{$groupname})) {
+            return $exitmsg.$lt{'cnnb'}.&mt('a [_1] which previously existed',$gpterm).
+                   $lt{'inth'}.'.<br />'.$lt{'grna'};
+        }
     } elsif ($action eq 'modify') {
         unless(exists($curr_groups{$groupname})) {
             $earlyout = &mt('[_1] name:',$ucgpterm).' '.$groupname.$lt{'thgr'}.
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.804 loncom/lonnet/perl/lonnet.pm:1.805
--- loncom/lonnet/perl/lonnet.pm:1.804	Wed Nov 15 09:35:10 2006
+++ loncom/lonnet/perl/lonnet.pm	Mon Nov 20 18:50:51 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.804 2006/11/15 14:35:10 raeburn Exp $
+# $Id: lonnet.pm,v 1.805 2006/11/20 23:50:51 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -4324,11 +4324,39 @@
     return(&dump('coursegroups',$cdom,$cnum,$group));
 }
 
+sub get_deleted_groups {
+    my ($cdom,$cnum,$group) = @_;
+    return(&dump('deleted_groups',$cdom,$cnum,$group));
+}
+
 sub modify_coursegroup {
     my ($cdom,$cnum,$groupsettings) = @_;
     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 }
 
+sub delete_coursegroup {
+    my ($cdom,$cnum,$group) = @_;
+    my %curr_group = &get_coursegroups($cdom,$cnum,$group);
+    if (my $tmp = &error(%curr_group)) {
+        &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
+        return ('read error',$tmp);
+    } else {
+        my %savedsettings = %curr_group; 
+        my $result = &put('deleted_groups',\%savedsettings,$cdom,$cnum);
+        my $deloutcome;
+        if ($result eq 'ok') {
+            $deloutcome = &del('coursegroups',[$group],$cdom,$cnum);
+        } else {
+            return ('write error',$result);
+        }
+        if ($deloutcome eq 'ok') {
+            return 'ok';
+        } else {
+            return ('delete error',$deloutcome);
+        }
+    }
+}
+
 sub modify_group_roles {
     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;

--raeburn1164066652--