--- loncom/interface/groupboards.pm 2006/06/30 14:08:35 1.2
+++ loncom/interface/groupboards.pm 2009/02/02 14:31:58 1.13
@@ -28,11 +28,10 @@ package Apache::groupboards;
use strict;
use Apache::Constants qw(:common :http);
-use Apache::loncommon;
+use Apache::loncommon();
use Apache::lonnet;
-use Apache::lonnavmaps;
-use Apache::lonuserstate;
-use Apache::lonratedt;
+use Apache::lonuserstate();
+use LONCAPA::map();
use Apache::lonlocal;
use LONCAPA;
@@ -51,41 +50,74 @@ 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'};
my $now = time;
my $crstype = &Apache::loncommon::course_type();
my $gpterm = &Apache::loncommon::group_term();
- my $bodytitle = &mt('[_1] Discussion Boards',$crstype);
+ my $ucgpterm = $gpterm;
+ $ucgpterm =~ s/^(\w)/uc($1)/e;
+ my $bodytitle = $ucgpterm.' Discussion Boards';
my $group = $env{'form.group'};
+ $group =~ s/\W//g;
+ my ($description,$earlyout,$refarg);
+
+ if (exists($env{'form.ref'})) {
+ $refarg = 'ref='.$env{'form.ref'};
+ }
+
+ &Apache::lonhtmlcommon::clear_breadcrumbs();
if (!defined($group)) {
- $r->print(&Apache::loncommon::start_page($bodytitle));
- $r->print(&mt('No [_1] defined, so there are no [_1] discussion boards to display',$gpterm));
- $r->print(&Apache::loncommon::end_page());
+ $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,$refarg));
+ return OK;
+ }
+ my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
+ if (!defined($curr_groups{$group})) {
+ $earlyout = &mt('Invalid group');
+ $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
+ $ucgpterm,$bodytitle,$earlyout,$refarg));
return OK;
}
+ my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
+ $description = &unescape($content{'description'});
+
my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
'/'.$group);
- my $can_view=&Apache::lonnet::allowed('pgd',$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('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,$refarg));
+ return OK;
+ }
if (defined($env{'form.newbul'})) {
if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
- $r->print(&Apache::loncommon::start_page($bodytitle));
- my ($outcome,$symb,$newurl,$bbtitle) =
+ $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
+ $ucgpterm,$bodytitle,$refarg));
+ my ($outcome,$newurl,$bbtitle,$dellockoutcome) =
&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.
'));
$r->print('
'. - ''. - &mt('Edit [_1] board',$bbtitle).' | '. + ''.&mt('Edit [_1] board',$bbtitle).''. ''. - ''. - &mt('View all group discussion boards'). + ''.&mt('View all group discussion boards'). ' |
' + .&mt('There are currently no discussion boards in this '.$gpterm.'.',) + .'
' + ); } $r->print(&Apache::loncommon::end_page()); return OK; } sub create_board { - my ($cdom,$cnum,$group,$newboard,$symb) = @_; - my ($bbtitle,$newurl)=split(/\=/,$newboard); + my ($cdom,$cnum,$group,$bbtitle) = @_; + my ($outcome,$boardid,$newurl); $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); + } else { + $outcome = "error: could not obtain lockfile\n"; + $dellockoutcome = 'ok'; + } + if (!$newurl) { + return ($outcome,$newurl,$bbtitle,$dellockoutcome); + } $newurl=&unescape($newurl); - my $allbbsmap = &get_bbfolder_url($cdom,$cnum,$group); - my ($outcome,$symb); + # 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); if ($fatal) { - $outcome = "error: failed to store discussion boards map - $errtext\n"; + $outcome = "error: failed to save discussion boards map - $errtext\n"; } else { - $outcome = 'ok'; - $symb = &Apache::lonnet::encode_symb($allbbsmap,$newidx,$newurl); + my %boardinfo = ( + 'group' => $group, + ); + $outcome = &Apache::lonnet::put('bulletinpage_'.$boardid, + \%boardinfo,$cdom,$cnum); } } else { $outcome = "error: failed to read all discussion boards map - $errtext\n"; @@ -183,22 +266,38 @@ sub create_board { $outcome = 'error: discussion boards folder absent, '. 'or in unexpected location - '.$allbbsmap."\n"; } - return ($outcome,$symb,$newurl,$bbtitle); + return ($outcome,$newurl,$bbtitle,$dellockoutcome); } -sub get_bbfolder_url { - my ($cdom,$cnum,$group) = @_; - my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group); - my $grpbbmap; - if (%curr_groups) { - my %group_info = &Apache::longroup::get_group_settings( - $curr_groups{$group}); - my $creation = $group_info{'creation'}; - my $bbfolder = $creation + 1; - my $crspath = '/uploaded/'.$cdom.'/'.$cnum.'/'; - $grpbbmap = $crspath.'default_'.$bbfolder.'.sequence'; +sub display_error { + my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout, + $refarg)=@_; + my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm, + $ucgpterm,$bodytitle,$refarg); + $output .= $earlyout; + $output .= &Apache::loncommon::end_page(); + return $output; +} + +sub boards_header { + 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"},); } - return $grpbbmap; + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg", + text=>"$ucgpterm: $description", + title=>"Go to group's home page"}, + {href=>"/adm/groupboards?group=$group&$refarg", + text=>"Discussion Boards", + title=>"Display group discussion boards"},); + $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description)); + return $output; } 1;