--- loncom/interface/groupboards.pm	2006/07/14 17:23:49	1.6
+++ loncom/interface/groupboards.pm	2013/07/19 18:24:17	1.18
@@ -1,6 +1,8 @@
 # The LearningOnline Network
 # Group Bulletin Boards Manager
 #
+# $Id: groupboards.pm,v 1.18 2013/07/19 18:24:17 bisitz Exp $
+#
 # Copyright Michigan State University Board of Trustees
 #
 # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
@@ -28,10 +30,10 @@ package Apache::groupboards;
 
 use strict;
 use Apache::Constants qw(:common :http);
-use Apache::loncommon;
+use Apache::loncommon();
 use Apache::lonnet;
-use Apache::lonuserstate;
-use Apache::lonratedt;
+use Apache::lonuserstate();
+use LONCAPA::map();
 use Apache::lonlocal;
 use LONCAPA;
 
@@ -50,7 +52,7 @@ sub handler {
     }
 
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-                        ['group']);
+                        ['group','ref']);
 
     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
@@ -59,23 +61,27 @@ sub handler {
     my $gpterm =  &Apache::loncommon::group_term();
     my $ucgpterm = $gpterm;
     $ucgpterm =~ s/^(\w)/uc($1)/e;
-    my $bodytitle = &mt('[_1] Discussion Boards',$crstype);
+    my $bodytitle = $ucgpterm.' Discussion Boards';
     my $group = $env{'form.group'};
     $group =~ s/\W//g;
-    my ($description,$earlyout);
+    my ($description,$earlyout,$refarg);
+
+    if (exists($env{'form.ref'})) {
+        $refarg = 'ref='.$env{'form.ref'};
+    }
 
     &Apache::lonhtmlcommon::clear_breadcrumbs();
     if (!defined($group)) {
         $earlyout = &mt('No [_1] defined, so there are no [_1] discussion boards to display',$gpterm);
         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
-                                 $ucgpterm,$bodytitle,$earlyout));
+                                 $ucgpterm,$bodytitle,$earlyout,$refarg));
         return OK;
     }
     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
     if (!defined($curr_groups{$group})) {
-        $earlyout = &mt('Invalid [_1]',$gpterm);
+        $earlyout = &mt('Invalid group');
         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
-                                 $ucgpterm,$bodytitle,$earlyout));
+                                 $ucgpterm,$bodytitle,$earlyout,$refarg));
         return OK;
     }
     my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
@@ -83,34 +89,39 @@ sub handler {
 
     my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
                                             '/'.$group);
-    my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'});
+    my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
 
     if (!$can_view) {
-        $can_view = &Apache::lonnet::allowed('pgd',$env{'request.course.id'}.
+        $can_view = &Apache::lonnet::allowed('vgb',$env{'request.course.id'}.
                                           '/'.$group);
     }
     if (!$can_view) {
         $earlyout=&mt('You do not have privileges to view discussion boards in this [_1]',$gpterm);
         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
-                                 $ucgpterm,$bodytitle,$earlyout));
+                                 $ucgpterm,$bodytitle,$earlyout,$refarg));
         return OK;
     }
     if (defined($env{'form.newbul'})) {
         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
             $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
-                                     $ucgpterm,$bodytitle));
-            my ($outcome,$newurl,$bbtitle) = 
+                                     $ucgpterm,$bodytitle,$refarg));
+            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);
-                $r->print(&mt('The new discussion board was added successfully.<br />'));
+                $r->print(&mt('The new discussion board was added successfully.').'<br />');
                 $r->print('<table border="0"><tr><td>'.
-                          '<a href="'.$newurl.'?group='.$group.'">'.
-                          &mt('Edit [_1] board',$bbtitle).'</a></td>'.
+                          '<a href="'.$newurl.'?group='.$group.'&amp;'.$refarg.
+                          '">'.&mt('Edit [_1] board',$bbtitle).'</a></td>'.
                            '<td>&nbsp;&nbsp;</td><td>'.
-                          '<a href="/adm/groupboards?group='.$group.'">'.
-                          &mt('View all group discussion boards').
+                          '<a href="/adm/groupboards?group='.$group.'&amp;'.
+                          $refarg.'">'.&mt('View all group discussion boards').
                           '</a></td></tr></table>');
+                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.'">'.
                          &mt('Return to discussion boards').'</a>');
@@ -126,8 +137,7 @@ sub handler {
 function makebulboard() {
    var title=prompt('Discussion Board Title');
    if (title) {
-    this.document.forms.newbb.newbul.value=
-        title+'=/adm/$cdom/$cnum/$now/bulletinboard';
+    this.document.forms.newbb.newbul.value=title;
     this.document.forms.newbb.submit();
    }
 }
@@ -135,20 +145,26 @@ function makebulboard() {
 |;
     }
     $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,
-                             $bodytitle,$jscript));
+                             $bodytitle,$refarg,$jscript));
     my ($groupboards,$boards) = &Apache::longroup::get_group_bbinfo($cdom,$cnum,
                                                                     $group);
     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
         $r->print('<br /><form method="post" name="newbb" action="/adm/groupboards">'.
                   "\n".'<input type="button" name="bbbutton" value="'.
                   &mt('New Discussion Board').
-                  '" onClick="javascript:makebulboard();" />'."\n".
-                  '  <input type="hidden" name="newbul" />'."\n".
-                  '  <input type="hidden" name="group" value="'.$group.'" />'.
+                  '" onclick="javascript:makebulboard();" />'."\n".
+                  '<input type="hidden" name="newbul" />'."\n".
+                  '<input type="hidden" name="group" value="'.$group.'" />'.
+                  '<input type="hidden" name="ref" value="'.$env{'form.ref'}.'" />'.
                   "\n".'</form><br />');
     }
     if (@{$groupboards} > 0) {
-        $r->print('<br />');
+        $r->print('<br />'
+                 .&Apache::loncommon::start_data_table()
+                 .&Apache::loncommon::start_data_table_header_row()
+                 .'<th>'.&mt('Discussion Boards').'</th>'
+                 .&Apache::loncommon::end_data_table_header_row()
+        );
         foreach my $board (@{$groupboards}) {
             my $board_url = $$boards{$board}{'url'};
             if ($board_url =~ /\?/) {
@@ -156,38 +172,52 @@ function makebulboard() {
             } else {
                 $board_url .= '?group='.$group;
             }
-            $r->print('<a href="'.$board_url.'">'.$$boards{$board}{'title'}.'</a><br />');
+            $r->print(&Apache::loncommon::start_data_table_row()
+                     .'<td>'
+                     .'<a href="'.$board_url.'&amp;'.$refarg.'">'
+                     .$$boards{$board}{'title'}.'</a>'
+                     .'</td>'
+                     .&Apache::loncommon::end_data_table_row()
+            );
         }
+        $r->print(&Apache::loncommon::end_data_table());
     } else {
-        $r->print('<br />'.&mt('There are currently no discussion boards in this [_1].',
-                      $gpterm));
+        $r->print('<p class="LC_info">'
+                 .&mt('There are currently no discussion boards in this '.$gpterm.'.',)
+                 .'</p>'
+        );
     }
     $r->print(&Apache::loncommon::end_page());
     return OK;
 }
 
 sub create_board {
-    my ($cdom,$cnum,$group,$newboard) = @_;
-    my ($bbtitle,$newurl)=split(/\=/,$newboard);
-    my $outcome;
-    my ($boardid) = ($newurl =~ m-/adm/\Q$cdom\E/\Q$cnum\E/(\d+)/bulletinboard-);
-    if (!$boardid) {
-        $outcome = ('error: the URL for new board was invalid');
-        return ($outcome,$newurl,$bbtitle);
-    }
+    my ($cdom,$cnum,$group,$bbtitle) = @_;
+    my ($outcome,$newurl,$idtype);
     $bbtitle=&unescape($bbtitle);
-    $newurl=&unescape($newurl);
+    $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 {
+         return ($error,$newurl,$bbtitle,$dellock);
+    }
+
+    # 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);
     if ($allbbsmap =~ m|^/uploaded|) {
-        my ($errtext,$fatal)=&Apache::lonratedt::mapread($allbbsmap);
+        my ($errtext,$fatal)=&LONCAPA::map::mapread($allbbsmap);
         if (!$fatal) {
-            my $newidx=&Apache::lonratedt::getresidx($newurl);
-            $Apache::lonratedt::resources[$newidx]=$bbtitle.':'.$newurl.
+            my $newidx=&LONCAPA::map::getresidx($newurl);
+            $LONCAPA::map::resources[$newidx]=$bbtitle.':'.$newurl.
                                                    ':false:normal:res';
-            push(@Apache::lonratedt::order,$newidx);
-            my ($errtext,$fatal)=&Apache::lonratedt::storemap($allbbsmap,1);
+            push(@LONCAPA::map::order,$newidx);
+            my ($errtext,$fatal)=&LONCAPA::map::storemap($allbbsmap,1,1);
             if ($fatal) {
-                $outcome = "error: failed to store discussion boards map - $errtext\n";
+                $outcome = "error: failed to save discussion boards map - $errtext\n";
             } else {
                 my %boardinfo = (
                   'group' => $group,
@@ -202,26 +232,34 @@ sub create_board {
         $outcome = 'error: discussion boards folder absent, '.
                    'or in unexpected location - '.$allbbsmap."\n";
     }
-    return ($outcome,$newurl,$bbtitle);
+    return ($outcome,$newurl,$bbtitle,$dellock);
 }
 
 sub display_error {
-    my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout)=@_;
+    my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout,
+        $refarg)=@_;
     my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
-                                $ucgpterm,$bodytitle);
+                                $ucgpterm,$bodytitle,$refarg);
     $output .= $earlyout;
     $output .= &Apache::loncommon::end_page();
     return $output;
 }
 
 sub boards_header {
-    my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$jscript)=@_;
+    my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$refarg,
+        $jscript)=@_;
     my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
+    if ($refarg) {
+        &Apache::lonhtmlcommon::add_breadcrumb
+        ({href=>"/adm/coursegroups",
+          text=>"Groups",
+          title=>"View course groups"},);
+    }
     &Apache::lonhtmlcommon::add_breadcrumb
-        ({href=>"/adm/$cdom/$cnum/$group/smppg",
+        ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
           text=>"$ucgpterm: $description",
           title=>"Go to group's home page"},
-         {href=>"/adm/groupboards?group=$group",
+         {href=>"/adm/groupboards?group=$group&amp;$refarg",
           text=>"Discussion Boards",
           title=>"Display group discussion boards"},);
     $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description));