File:  [LON-CAPA] / loncom / interface / groupboards.pm
Revision 1.5: download - view: text, annotated - select for diffs
Sat Jul 8 01:07:34 2006 UTC (17 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Adding breadcrumb trail for group discussion boards display. Also putting groupID in bulletinpage_$boardid when creating group.  This is used by lonbulletin.pm to determine which group a group bulletin board belongs to for the purposes of privileges checking.  This setting is currently unchanged when a group board is moved from one group's folder to another using DOCS (could cause some confusion). Some sanity checking could be in order here (e.g., checking identity of enclosing map).  Still need to add locking device to prevent possible namespace collision when new boards are created.

# The LearningOnline Network
# Group Bulletin Boards Manager
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

package Apache::groupboards;

use strict;
use Apache::Constants qw(:common :http);
use Apache::loncommon;
use Apache::lonnet;
use Apache::lonuserstate;
use Apache::lonratedt;
use Apache::lonlocal;
use LONCAPA;

sub handler {
    my ($r) = @_;
    &Apache::loncommon::content_type($r,'text/html');
    $r->send_http_header;
    return OK if $r->header_only;

    #  Needs to be in a course
    if (! ($env{'request.course.fn'})) {
        # Not in a course
        $env{'user.error.msg'}=
     "/adm/groupboards:mdg:0:0:Cannot edit or view course groups";
        return HTTP_NOT_ACCEPTABLE;
    }

    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                        ['group']);

    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 $ucgpterm = $gpterm;
    $ucgpterm =~ s/^(\w)/uc($1)/e;
    my $bodytitle = &mt('[_1] Discussion Boards',$crstype);
    my $group = $env{'form.group'};
    $group =~ s/\W//g;
    my ($description,$earlyout);

    &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));
        return OK;
    }
    my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
    if (!defined($curr_groups{$group})) {
        $earlyout = &mt('Invalid [_1]',$gpterm);
        $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
                                 $ucgpterm,$bodytitle,$earlyout));
        return OK;
    }
    my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
    my $description = &unescape($content{'description'});

    my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
                                            '/'.$group);
    my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'});

    if (!$can_view) {
        $can_view = &Apache::lonnet::allowed('pgd',$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));
        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) = 
		&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('<table border="0"><tr><td>'.
                          '<a href="'.$newurl.'?group='.$group.'">'.
                          &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></td></tr></table>');
            } 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>');
            }
            $r->print(&Apache::loncommon::end_page());
            return OK;
        }
    }
    my $jscript;
    if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
        $jscript = qq|
<script type="text/javascript">
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.submit();
   }
}
</script>
|;
    }
    $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,
                             $bodytitle,$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.'" />'.
                  "\n".'</form><br />');
    }
    if (@{$groupboards} > 0) {
        $r->print('<br />');
        foreach my $board (@{$groupboards}) {
            my $board_url = $$boards{$board}{'url'};
            if ($board_url =~ /\?/) {
                $board_url .= '&amp;group='.$group;
            } else {
                $board_url .= '?group='.$group;
            }
            $r->print('<a href="'.$board_url.'">'.$$boards{$board}{'title'}.'</a><br />');
        }
    } else {
        $r->print('<br />'.&mt('There are currently no discussion boards in this [_1].',
                      $gpterm));
    }
    $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);
    }
    $bbtitle=&unescape($bbtitle);
    $newurl=&unescape($newurl);
    my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
    if ($allbbsmap =~ m|^/uploaded|) {
        my ($errtext,$fatal)=&Apache::lonratedt::mapread($allbbsmap);
        if (!$fatal) {
            my $newidx=&Apache::lonratedt::getresidx($newurl);
            $Apache::lonratedt::resources[$newidx]=$bbtitle.':'.$newurl.
                                                   ':false:normal:res';
            push(@Apache::lonratedt::order,$newidx);
            my ($errtext,$fatal)=&Apache::lonratedt::storemap($allbbsmap,1);
            if ($fatal) {
                $outcome = "error: failed to store discussion boards map - $errtext\n";
            } else {
                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";
        }
    } else {
        $outcome = 'error: discussion boards folder absent, '.
                   'or in unexpected location - '.$allbbsmap."\n";
    }
    return ($outcome,$newurl,$bbtitle);
}

sub display_error {
    my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout)=@_;
    my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
                                $ucgpterm,$bodytitle);
    $output .= $earlyout;
    $output .= &Apache::loncommon::end_page();
    return $output;
}

sub boards_header {
    my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$jscript)=@_;
    my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
    &Apache::lonhtmlcommon::add_breadcrumb
        ({href=>"/adm/$cdom/$cnum/$group/smppg",
          text=>"$ucgpterm: $description",
          title=>"Go to group's home page"},
         {href=>"/adm/groupboards?group=$group",
          text=>"Discussion Boards",
          title=>"Display group discussion boards"},);
    $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description));
    return $output;
}

1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>