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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Group Bulletin Boards Manager
                      3: #
1.14    ! bisitz      4: # $Id: groupboards.pm,v 1.14 2009/02/13 16:45:00 bisitz Exp $
        !             5: #
1.1       raeburn     6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::groupboards;
1.2       albertel   30: 
1.1       raeburn    31: use strict;
                     32: use Apache::Constants qw(:common :http);
1.10      albertel   33: use Apache::loncommon();
1.1       raeburn    34: use Apache::lonnet;
1.10      albertel   35: use Apache::lonuserstate();
                     36: use LONCAPA::map();
1.1       raeburn    37: use Apache::lonlocal;
                     38: use LONCAPA;
                     39: 
                     40: sub handler {
                     41:     my ($r) = @_;
                     42:     &Apache::loncommon::content_type($r,'text/html');
                     43:     $r->send_http_header;
                     44:     return OK if $r->header_only;
                     45: 
                     46:     #  Needs to be in a course
                     47:     if (! ($env{'request.course.fn'})) {
                     48:         # Not in a course
                     49:         $env{'user.error.msg'}=
                     50:      "/adm/groupboards:mdg:0:0:Cannot edit or view course groups";
                     51:         return HTTP_NOT_ACCEPTABLE;
                     52:     }
                     53: 
                     54:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.7       raeburn    55:                         ['group','ref']);
1.1       raeburn    56: 
                     57:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                     58:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                     59:     my $now = time;
                     60:     my $crstype = &Apache::loncommon::course_type();
                     61:     my $gpterm =  &Apache::loncommon::group_term();
1.5       raeburn    62:     my $ucgpterm = $gpterm;
                     63:     $ucgpterm =~ s/^(\w)/uc($1)/e;
1.13      schafran   64:     my $bodytitle = $ucgpterm.' Discussion Boards';
1.1       raeburn    65:     my $group = $env{'form.group'};
1.5       raeburn    66:     $group =~ s/\W//g;
1.7       raeburn    67:     my ($description,$earlyout,$refarg);
                     68: 
                     69:     if (exists($env{'form.ref'})) {
                     70:         $refarg = 'ref='.$env{'form.ref'};
                     71:     }
1.5       raeburn    72: 
                     73:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.1       raeburn    74:     if (!defined($group)) {
1.5       raeburn    75:         $earlyout = &mt('No [_1] defined, so there are no [_1] discussion boards to display',$gpterm);
                     76:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn    77:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
1.5       raeburn    78:         return OK;
                     79:     }
                     80:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                     81:     if (!defined($curr_groups{$group})) {
1.8       raeburn    82:         $earlyout = &mt('Invalid group');
1.5       raeburn    83:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn    84:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
1.1       raeburn    85:         return OK;
                     86:     }
1.5       raeburn    87:     my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
1.6       albertel   88:     $description = &unescape($content{'description'});
1.5       raeburn    89: 
1.1       raeburn    90:     my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
                     91:                                             '/'.$group);
1.7       raeburn    92:     my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
1.5       raeburn    93: 
                     94:     if (!$can_view) {
1.7       raeburn    95:         $can_view = &Apache::lonnet::allowed('vgb',$env{'request.course.id'}.
1.1       raeburn    96:                                           '/'.$group);
1.5       raeburn    97:     }
                     98:     if (!$can_view) {
                     99:         $earlyout=&mt('You do not have privileges to view discussion boards in this [_1]',$gpterm);
                    100:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn   101:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
1.5       raeburn   102:         return OK;
                    103:     }
1.1       raeburn   104:     if (defined($env{'form.newbul'})) {
                    105:         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
1.5       raeburn   106:             $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn   107:                                      $ucgpterm,$bodytitle,$refarg));
1.8       raeburn   108:             my ($outcome,$newurl,$bbtitle,$dellockoutcome) = 
1.2       albertel  109: 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
1.1       raeburn   110:             if ($outcome eq 'ok') {
                    111:                 my ($furl,$ferr)= &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
                    112:                 $r->print(&mt('The new discussion board was added successfully.<br />'));
                    113:                 $r->print('<table border="0"><tr><td>'.
1.7       raeburn   114:                           '<a href="'.$newurl.'?group='.$group.'&amp;'.$refarg.
                    115:                           '">'.&mt('Edit [_1] board',$bbtitle).'</a></td>'.
1.1       raeburn   116:                            '<td>&nbsp;&nbsp;</td><td>'.
1.7       raeburn   117:                           '<a href="/adm/groupboards?group='.$group.'&amp;'.
                    118:                           $refarg.'">'.&mt('View all group discussion boards').
1.1       raeburn   119:                           '</a></td></tr></table>');
1.8       raeburn   120:                 if ($dellockoutcome ne 'ok') {
                    121:                     $r->print(&mt('There was a problem removing a lockfile for the group ([_1]). This may prevent creation of additional bulletin boards in this group.  Please contact the system administrator for your LON-CAPA domain.'));
                    122:                 }
1.1       raeburn   123:             } else {
1.2       albertel  124:                 $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   125:                          &mt('Return to discussion boards').'</a>');
                    126:             }
                    127:             $r->print(&Apache::loncommon::end_page());
                    128:             return OK;
                    129:         }
                    130:     }
                    131:     my $jscript;
                    132:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
                    133:         $jscript = qq|
1.5       raeburn   134: <script type="text/javascript">
1.1       raeburn   135: function makebulboard() {
                    136:    var title=prompt('Discussion Board Title');
                    137:    if (title) {
1.8       raeburn   138:     this.document.forms.newbb.newbul.value=title;
1.1       raeburn   139:     this.document.forms.newbb.submit();
                    140:    }
                    141: }
1.5       raeburn   142: </script>
1.1       raeburn   143: |;
                    144:     }
1.5       raeburn   145:     $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,
1.7       raeburn   146:                              $bodytitle,$refarg,$jscript));
1.4       raeburn   147:     my ($groupboards,$boards) = &Apache::longroup::get_group_bbinfo($cdom,$cnum,
                    148:                                                                     $group);
1.1       raeburn   149:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
1.5       raeburn   150:         $r->print('<br /><form method="post" name="newbb" action="/adm/groupboards">'.
1.1       raeburn   151:                   "\n".'<input type="button" name="bbbutton" value="'.
                    152:                   &mt('New Discussion Board').
                    153:                   '" onClick="javascript:makebulboard();" />'."\n".
1.7       raeburn   154:                   '<input type="hidden" name="newbul" />'."\n".
                    155:                   '<input type="hidden" name="group" value="'.$group.'" />'.
                    156:                   '<input type="hidden" name="ref" value="'.$env{'form.ref'}.'" />'.
1.1       raeburn   157:                   "\n".'</form><br />');
                    158:     }
1.4       raeburn   159:     if (@{$groupboards} > 0) {
1.12      bisitz    160:         $r->print('<br />'
                    161:                  .&Apache::loncommon::start_data_table()
                    162:                  .&Apache::loncommon::start_data_table_header_row()
                    163:                  .'<th>'.&mt('Discussion Boards').'</th>'
                    164:                  .&Apache::loncommon::end_data_table_header_row()
                    165:         );
1.4       raeburn   166:         foreach my $board (@{$groupboards}) {
1.5       raeburn   167:             my $board_url = $$boards{$board}{'url'};
                    168:             if ($board_url =~ /\?/) {
                    169:                 $board_url .= '&amp;group='.$group;
                    170:             } else {
                    171:                 $board_url .= '?group='.$group;
                    172:             }
1.12      bisitz    173:             $r->print(&Apache::loncommon::start_data_table_row()
                    174:                      .'<td>'
                    175:                      .'<a href="'.$board_url.'&amp;'.$refarg.'">'
                    176:                      .$$boards{$board}{'title'}.'</a>'
                    177:                      .'</td>'
                    178:                      .&Apache::loncommon::end_data_table_row()
                    179:             );
1.1       raeburn   180:         }
1.12      bisitz    181:         $r->print(&Apache::loncommon::end_data_table());
1.1       raeburn   182:     } else {
1.12      bisitz    183:         $r->print('<p class="LC_info">'
                    184:                  .&mt('There are currently no discussion boards in this '.$gpterm.'.',)
                    185:                  .'</p>'
                    186:         );
1.1       raeburn   187:     }
                    188:     $r->print(&Apache::loncommon::end_page());
                    189:     return OK;
                    190: }
                    191: 
                    192: sub create_board {
1.8       raeburn   193:     my ($cdom,$cnum,$group,$bbtitle) = @_;
                    194:     my ($outcome,$boardid,$newurl);
                    195:     $bbtitle=&unescape($bbtitle);
                    196:     # get lock on nohist_groupboards file
                    197:     my $lockhash = {
                    198:                   $group."\0".'locked_boardids' => $env{'user.name'}.
                    199:                                                        ':'.$env{'user.domain'},
                    200:                    };
                    201:     my $tries = 0;
                    202:     my $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
                    203:     my $dellockoutcome;
                    204:     while (($gotlock ne 'ok') && $tries <3) {
                    205:         $tries ++;
                    206:         sleep 1;
                    207:         $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
                    208:     }
                    209:     if ($gotlock eq 'ok') {
                    210:        my %curr_boards = &Apache::lonnet::dump('nohist_groupboards',$cdom,$cnum,$group);
                    211:        $boardid = time;
                    212:        my $idtries = 0;
                    213:        while(exists($curr_boards{$group."\0".$boardid}) && $idtries < 20) {
                    214:            $boardid ++;
                    215:            $idtries ++;
                    216:        }
                    217:        if (!exists($curr_boards{$group."\0".$boardid})) {
                    218:            my %new_board = (
                    219:                           $group."\0".$boardid => $env{'user.name'}.':'.
                    220:                                                   $env{'user.domain'},
                    221:            );
                    222:            my $putresult = &Apache::lonnet::put('nohist_groupboards',\%new_board,
                    223:                                                 $cdom,$cnum);
                    224:            if ($putresult ne 'ok') {
1.11      albertel  225:                $outcome = 'error saving new board: '.$putresult;
1.8       raeburn   226:            } else {
                    227:                $newurl = '/adm/'.$cdom.'/'.$cnum.'/'.$boardid.
                    228:                          '/bulletinboard';
                    229:            }
                    230:        } else {
                    231:             $outcome = ('error: no unique ID for the new board available.');
                    232:        }
                    233:        #  remove lock
                    234:        my @del_lock = ($group."\0".'locked_boardids');
                    235:        $dellockoutcome = &Apache::lonnet::del('nohist_groupboards',\@del_lock,$cdom,$cnum);
                    236:     } else {
                    237:         $outcome = "error: could not obtain lockfile\n";
                    238:         $dellockoutcome = 'ok';
                    239:     }
                    240:     if (!$newurl) {
                    241:         return ($outcome,$newurl,$bbtitle,$dellockoutcome);
1.5       raeburn   242:     }
1.1       raeburn   243:     $newurl=&unescape($newurl);
1.8       raeburn   244:     # need to check here if group_boards_$group.sequence is in the course
                    245:     # if not - add it as an item in group_folder_$group.sequence 
1.4       raeburn   246:     my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
1.1       raeburn   247:     if ($allbbsmap =~ m|^/uploaded|) {
1.10      albertel  248:         my ($errtext,$fatal)=&LONCAPA::map::mapread($allbbsmap);
1.1       raeburn   249:         if (!$fatal) {
1.10      albertel  250:             my $newidx=&LONCAPA::map::getresidx($newurl);
                    251:             $LONCAPA::map::resources[$newidx]=$bbtitle.':'.$newurl.
1.1       raeburn   252:                                                    ':false:normal:res';
1.10      albertel  253:             push(@LONCAPA::map::order,$newidx);
                    254:             my ($errtext,$fatal)=&LONCAPA::map::storemap($allbbsmap,1);
1.1       raeburn   255:             if ($fatal) {
1.11      albertel  256:                 $outcome = "error: failed to save discussion boards map - $errtext\n";
1.1       raeburn   257:             } else {
1.5       raeburn   258:                 my %boardinfo = (
                    259:                   'group' => $group,
                    260:                 );
                    261:                 $outcome = &Apache::lonnet::put('bulletinpage_'.$boardid,
                    262:                                                 \%boardinfo,$cdom,$cnum);
1.1       raeburn   263:             }
                    264:         } else {
1.2       albertel  265:             $outcome = "error: failed to read all discussion boards map - $errtext\n";
1.1       raeburn   266:         }
                    267:     } else {
1.2       albertel  268:         $outcome = 'error: discussion boards folder absent, '.
1.1       raeburn   269:                    'or in unexpected location - '.$allbbsmap."\n";
                    270:     }
1.8       raeburn   271:     return ($outcome,$newurl,$bbtitle,$dellockoutcome);
1.1       raeburn   272: }
                    273: 
1.5       raeburn   274: sub display_error {
1.7       raeburn   275:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout,
                    276:         $refarg)=@_;
1.5       raeburn   277:     my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn   278:                                 $ucgpterm,$bodytitle,$refarg);
1.5       raeburn   279:     $output .= $earlyout;
                    280:     $output .= &Apache::loncommon::end_page();
                    281:     return $output;
                    282: }
                    283: 
                    284: sub boards_header {
1.7       raeburn   285:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$refarg,
                    286:         $jscript)=@_;
1.5       raeburn   287:     my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
1.7       raeburn   288:     if ($refarg) {
                    289:         &Apache::lonhtmlcommon::add_breadcrumb
                    290:         ({href=>"/adm/coursegroups",
                    291:           text=>"Groups",
                    292:           title=>"View course groups"},);
                    293:     }
1.5       raeburn   294:     &Apache::lonhtmlcommon::add_breadcrumb
1.7       raeburn   295:         ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
1.5       raeburn   296:           text=>"$ucgpterm: $description",
                    297:           title=>"Go to group's home page"},
1.7       raeburn   298:          {href=>"/adm/groupboards?group=$group&amp;$refarg",
1.5       raeburn   299:           text=>"Discussion Boards",
                    300:           title=>"Display group discussion boards"},);
                    301:     $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description));
                    302:     return $output;
                    303: }
                    304: 
1.1       raeburn   305: 1;

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