Annotation of loncom/interface/groupboards.pm, revision 1.5

1.1       raeburn     1: # The LearningOnline Network
                      2: # Group Bulletin Boards Manager
                      3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: 
                     27: package Apache::groupboards;
1.2       albertel   28: 
1.1       raeburn    29: use strict;
                     30: use Apache::Constants qw(:common :http);
                     31: use Apache::loncommon;
                     32: use Apache::lonnet;
                     33: use Apache::lonuserstate;
                     34: use Apache::lonratedt;
                     35: use Apache::lonlocal;
                     36: use LONCAPA;
                     37: 
                     38: sub handler {
                     39:     my ($r) = @_;
                     40:     &Apache::loncommon::content_type($r,'text/html');
                     41:     $r->send_http_header;
                     42:     return OK if $r->header_only;
                     43: 
                     44:     #  Needs to be in a course
                     45:     if (! ($env{'request.course.fn'})) {
                     46:         # Not in a course
                     47:         $env{'user.error.msg'}=
                     48:      "/adm/groupboards:mdg:0:0:Cannot edit or view course groups";
                     49:         return HTTP_NOT_ACCEPTABLE;
                     50:     }
                     51: 
                     52:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     53:                         ['group']);
                     54: 
                     55:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                     56:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                     57:     my $now = time;
                     58:     my $crstype = &Apache::loncommon::course_type();
                     59:     my $gpterm =  &Apache::loncommon::group_term();
1.5     ! raeburn    60:     my $ucgpterm = $gpterm;
        !            61:     $ucgpterm =~ s/^(\w)/uc($1)/e;
1.1       raeburn    62:     my $bodytitle = &mt('[_1] Discussion Boards',$crstype);
                     63:     my $group = $env{'form.group'};
1.5     ! raeburn    64:     $group =~ s/\W//g;
        !            65:     my ($description,$earlyout);
        !            66: 
        !            67:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.1       raeburn    68:     if (!defined($group)) {
1.5     ! raeburn    69:         $earlyout = &mt('No [_1] defined, so there are no [_1] discussion boards to display',$gpterm);
        !            70:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
        !            71:                                  $ucgpterm,$bodytitle,$earlyout));
        !            72:         return OK;
        !            73:     }
        !            74:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
        !            75:     if (!defined($curr_groups{$group})) {
        !            76:         $earlyout = &mt('Invalid [_1]',$gpterm);
        !            77:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
        !            78:                                  $ucgpterm,$bodytitle,$earlyout));
1.1       raeburn    79:         return OK;
                     80:     }
1.5     ! raeburn    81:     my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
        !            82:     my $description = &unescape($content{'description'});
        !            83: 
1.1       raeburn    84:     my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
                     85:                                             '/'.$group);
1.5     ! raeburn    86:     my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'});
        !            87: 
        !            88:     if (!$can_view) {
        !            89:         $can_view = &Apache::lonnet::allowed('pgd',$env{'request.course.id'}.
1.1       raeburn    90:                                           '/'.$group);
1.5     ! raeburn    91:     }
        !            92:     if (!$can_view) {
        !            93:         $earlyout=&mt('You do not have privileges to view discussion boards in this [_1]',$gpterm);
        !            94:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
        !            95:                                  $ucgpterm,$bodytitle,$earlyout));
        !            96:         return OK;
        !            97:     }
1.1       raeburn    98:     if (defined($env{'form.newbul'})) {
                     99:         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
1.5     ! raeburn   100:             $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
        !           101:                                      $ucgpterm,$bodytitle));
1.4       raeburn   102:             my ($outcome,$newurl,$bbtitle) = 
1.2       albertel  103: 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
1.1       raeburn   104:             if ($outcome eq 'ok') {
                    105:                 my ($furl,$ferr)= &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
                    106:                 $r->print(&mt('The new discussion board was added successfully.<br />'));
                    107:                 $r->print('<table border="0"><tr><td>'.
1.3       raeburn   108:                           '<a href="'.$newurl.'?group='.$group.'">'.
1.1       raeburn   109:                           &mt('Edit [_1] board',$bbtitle).'</a></td>'.
                    110:                            '<td>&nbsp;&nbsp;</td><td>'.
                    111:                           '<a href="/adm/groupboards?group='.$group.'">'.
                    112:                           &mt('View all group discussion boards').
                    113:                           '</a></td></tr></table>');
                    114:             } else {
1.2       albertel  115:                 $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.'">'.
1.1       raeburn   116:                          &mt('Return to discussion boards').'</a>');
                    117:             }
                    118:             $r->print(&Apache::loncommon::end_page());
                    119:             return OK;
                    120:         }
                    121:     }
                    122:     my $jscript;
                    123:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
                    124:         $jscript = qq|
1.5     ! raeburn   125: <script type="text/javascript">
1.1       raeburn   126: function makebulboard() {
                    127:    var title=prompt('Discussion Board Title');
                    128:    if (title) {
                    129:     this.document.forms.newbb.newbul.value=
                    130:         title+'=/adm/$cdom/$cnum/$now/bulletinboard';
                    131:     this.document.forms.newbb.submit();
                    132:    }
                    133: }
1.5     ! raeburn   134: </script>
1.1       raeburn   135: |;
                    136:     }
1.5     ! raeburn   137:     $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,
        !           138:                              $bodytitle,$jscript));
1.4       raeburn   139:     my ($groupboards,$boards) = &Apache::longroup::get_group_bbinfo($cdom,$cnum,
                    140:                                                                     $group);
1.1       raeburn   141:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
1.5     ! raeburn   142:         $r->print('<br /><form method="post" name="newbb" action="/adm/groupboards">'.
1.1       raeburn   143:                   "\n".'<input type="button" name="bbbutton" value="'.
                    144:                   &mt('New Discussion Board').
                    145:                   '" onClick="javascript:makebulboard();" />'."\n".
                    146:                   '  <input type="hidden" name="newbul" />'."\n".
                    147:                   '  <input type="hidden" name="group" value="'.$group.'" />'.
                    148:                   "\n".'</form><br />');
                    149:     }
1.4       raeburn   150:     if (@{$groupboards} > 0) {
1.5     ! raeburn   151:         $r->print('<br />');
1.4       raeburn   152:         foreach my $board (@{$groupboards}) {
1.5     ! raeburn   153:             my $board_url = $$boards{$board}{'url'};
        !           154:             if ($board_url =~ /\?/) {
        !           155:                 $board_url .= '&amp;group='.$group;
        !           156:             } else {
        !           157:                 $board_url .= '?group='.$group;
        !           158:             }
        !           159:             $r->print('<a href="'.$board_url.'">'.$$boards{$board}{'title'}.'</a><br />');
1.1       raeburn   160:         }
                    161:     } else {
1.5     ! raeburn   162:         $r->print('<br />'.&mt('There are currently no discussion boards in this [_1].',
1.1       raeburn   163:                       $gpterm));
                    164:     }
                    165:     $r->print(&Apache::loncommon::end_page());
                    166:     return OK;
                    167: }
                    168: 
                    169: sub create_board {
1.4       raeburn   170:     my ($cdom,$cnum,$group,$newboard) = @_;
1.1       raeburn   171:     my ($bbtitle,$newurl)=split(/\=/,$newboard);
1.5     ! raeburn   172:     my $outcome;
        !           173:     my ($boardid) = ($newurl =~ m-/adm/\Q$cdom\E/\Q$cnum\E/(\d+)/bulletinboard-);
        !           174:     if (!$boardid) {
        !           175:         $outcome = ('error: the URL for new board was invalid');
        !           176:         return ($outcome,$newurl,$bbtitle);
        !           177:     }
1.1       raeburn   178:     $bbtitle=&unescape($bbtitle);
                    179:     $newurl=&unescape($newurl);
1.4       raeburn   180:     my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
1.1       raeburn   181:     if ($allbbsmap =~ m|^/uploaded|) {
                    182:         my ($errtext,$fatal)=&Apache::lonratedt::mapread($allbbsmap);
                    183:         if (!$fatal) {
                    184:             my $newidx=&Apache::lonratedt::getresidx($newurl);
                    185:             $Apache::lonratedt::resources[$newidx]=$bbtitle.':'.$newurl.
                    186:                                                    ':false:normal:res';
1.2       albertel  187:             push(@Apache::lonratedt::order,$newidx);
1.1       raeburn   188:             my ($errtext,$fatal)=&Apache::lonratedt::storemap($allbbsmap,1);
                    189:             if ($fatal) {
1.2       albertel  190:                 $outcome = "error: failed to store discussion boards map - $errtext\n";
1.1       raeburn   191:             } else {
1.5     ! raeburn   192:                 my %boardinfo = (
        !           193:                   'group' => $group,
        !           194:                 );
        !           195:                 $outcome = &Apache::lonnet::put('bulletinpage_'.$boardid,
        !           196:                                                 \%boardinfo,$cdom,$cnum);
1.1       raeburn   197:             }
                    198:         } else {
1.2       albertel  199:             $outcome = "error: failed to read all discussion boards map - $errtext\n";
1.1       raeburn   200:         }
                    201:     } else {
1.2       albertel  202:         $outcome = 'error: discussion boards folder absent, '.
1.1       raeburn   203:                    'or in unexpected location - '.$allbbsmap."\n";
                    204:     }
1.4       raeburn   205:     return ($outcome,$newurl,$bbtitle);
1.1       raeburn   206: }
                    207: 
1.5     ! raeburn   208: sub display_error {
        !           209:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout)=@_;
        !           210:     my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
        !           211:                                 $ucgpterm,$bodytitle);
        !           212:     $output .= $earlyout;
        !           213:     $output .= &Apache::loncommon::end_page();
        !           214:     return $output;
        !           215: }
        !           216: 
        !           217: sub boards_header {
        !           218:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$jscript)=@_;
        !           219:     my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
        !           220:     &Apache::lonhtmlcommon::add_breadcrumb
        !           221:         ({href=>"/adm/$cdom/$cnum/$group/smppg",
        !           222:           text=>"$ucgpterm: $description",
        !           223:           title=>"Go to group's home page"},
        !           224:          {href=>"/adm/groupboards?group=$group",
        !           225:           text=>"Discussion Boards",
        !           226:           title=>"Display group discussion boards"},);
        !           227:     $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description));
        !           228:     return $output;
        !           229: }
        !           230: 
1.1       raeburn   231: 1;

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