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

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);
1.10      albertel   31: use Apache::loncommon();
1.1       raeburn    32: use Apache::lonnet;
1.10      albertel   33: use Apache::lonuserstate();
                     34: use LONCAPA::map();
1.1       raeburn    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'},
1.7       raeburn    53:                         ['group','ref']);
1.1       raeburn    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.13    ! schafran   62:     my $bodytitle = $ucgpterm.' Discussion Boards';
1.1       raeburn    63:     my $group = $env{'form.group'};
1.5       raeburn    64:     $group =~ s/\W//g;
1.7       raeburn    65:     my ($description,$earlyout,$refarg);
                     66: 
                     67:     if (exists($env{'form.ref'})) {
                     68:         $refarg = 'ref='.$env{'form.ref'};
                     69:     }
1.5       raeburn    70: 
                     71:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.1       raeburn    72:     if (!defined($group)) {
1.5       raeburn    73:         $earlyout = &mt('No [_1] defined, so there are no [_1] discussion boards to display',$gpterm);
                     74:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn    75:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
1.5       raeburn    76:         return OK;
                     77:     }
                     78:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                     79:     if (!defined($curr_groups{$group})) {
1.8       raeburn    80:         $earlyout = &mt('Invalid group');
1.5       raeburn    81:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn    82:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
1.1       raeburn    83:         return OK;
                     84:     }
1.5       raeburn    85:     my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
1.6       albertel   86:     $description = &unescape($content{'description'});
1.5       raeburn    87: 
1.1       raeburn    88:     my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
                     89:                                             '/'.$group);
1.7       raeburn    90:     my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
1.5       raeburn    91: 
                     92:     if (!$can_view) {
1.7       raeburn    93:         $can_view = &Apache::lonnet::allowed('vgb',$env{'request.course.id'}.
1.1       raeburn    94:                                           '/'.$group);
1.5       raeburn    95:     }
                     96:     if (!$can_view) {
                     97:         $earlyout=&mt('You do not have privileges to view discussion boards in this [_1]',$gpterm);
                     98:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn    99:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
1.5       raeburn   100:         return OK;
                    101:     }
1.1       raeburn   102:     if (defined($env{'form.newbul'})) {
                    103:         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
1.5       raeburn   104:             $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn   105:                                      $ucgpterm,$bodytitle,$refarg));
1.8       raeburn   106:             my ($outcome,$newurl,$bbtitle,$dellockoutcome) = 
1.2       albertel  107: 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
1.1       raeburn   108:             if ($outcome eq 'ok') {
                    109:                 my ($furl,$ferr)= &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
                    110:                 $r->print(&mt('The new discussion board was added successfully.<br />'));
                    111:                 $r->print('<table border="0"><tr><td>'.
1.7       raeburn   112:                           '<a href="'.$newurl.'?group='.$group.'&amp;'.$refarg.
                    113:                           '">'.&mt('Edit [_1] board',$bbtitle).'</a></td>'.
1.1       raeburn   114:                            '<td>&nbsp;&nbsp;</td><td>'.
1.7       raeburn   115:                           '<a href="/adm/groupboards?group='.$group.'&amp;'.
                    116:                           $refarg.'">'.&mt('View all group discussion boards').
1.1       raeburn   117:                           '</a></td></tr></table>');
1.8       raeburn   118:                 if ($dellockoutcome ne 'ok') {
                    119:                     $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.'));
                    120:                 }
1.1       raeburn   121:             } else {
1.2       albertel  122:                 $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   123:                          &mt('Return to discussion boards').'</a>');
                    124:             }
                    125:             $r->print(&Apache::loncommon::end_page());
                    126:             return OK;
                    127:         }
                    128:     }
                    129:     my $jscript;
                    130:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
                    131:         $jscript = qq|
1.5       raeburn   132: <script type="text/javascript">
1.1       raeburn   133: function makebulboard() {
                    134:    var title=prompt('Discussion Board Title');
                    135:    if (title) {
1.8       raeburn   136:     this.document.forms.newbb.newbul.value=title;
1.1       raeburn   137:     this.document.forms.newbb.submit();
                    138:    }
                    139: }
1.5       raeburn   140: </script>
1.1       raeburn   141: |;
                    142:     }
1.5       raeburn   143:     $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,
1.7       raeburn   144:                              $bodytitle,$refarg,$jscript));
1.4       raeburn   145:     my ($groupboards,$boards) = &Apache::longroup::get_group_bbinfo($cdom,$cnum,
                    146:                                                                     $group);
1.1       raeburn   147:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
1.5       raeburn   148:         $r->print('<br /><form method="post" name="newbb" action="/adm/groupboards">'.
1.1       raeburn   149:                   "\n".'<input type="button" name="bbbutton" value="'.
                    150:                   &mt('New Discussion Board').
                    151:                   '" onClick="javascript:makebulboard();" />'."\n".
1.7       raeburn   152:                   '<input type="hidden" name="newbul" />'."\n".
                    153:                   '<input type="hidden" name="group" value="'.$group.'" />'.
                    154:                   '<input type="hidden" name="ref" value="'.$env{'form.ref'}.'" />'.
1.1       raeburn   155:                   "\n".'</form><br />');
                    156:     }
1.4       raeburn   157:     if (@{$groupboards} > 0) {
1.12      bisitz    158:         $r->print('<br />'
                    159:                  .&Apache::loncommon::start_data_table()
                    160:                  .&Apache::loncommon::start_data_table_header_row()
                    161:                  .'<th>'.&mt('Discussion Boards').'</th>'
                    162:                  .&Apache::loncommon::end_data_table_header_row()
                    163:         );
1.4       raeburn   164:         foreach my $board (@{$groupboards}) {
1.5       raeburn   165:             my $board_url = $$boards{$board}{'url'};
                    166:             if ($board_url =~ /\?/) {
                    167:                 $board_url .= '&amp;group='.$group;
                    168:             } else {
                    169:                 $board_url .= '?group='.$group;
                    170:             }
1.12      bisitz    171:             $r->print(&Apache::loncommon::start_data_table_row()
                    172:                      .'<td>'
                    173:                      .'<a href="'.$board_url.'&amp;'.$refarg.'">'
                    174:                      .$$boards{$board}{'title'}.'</a>'
                    175:                      .'</td>'
                    176:                      .&Apache::loncommon::end_data_table_row()
                    177:             );
1.1       raeburn   178:         }
1.12      bisitz    179:         $r->print(&Apache::loncommon::end_data_table());
1.1       raeburn   180:     } else {
1.12      bisitz    181:         $r->print('<p class="LC_info">'
                    182:                  .&mt('There are currently no discussion boards in this '.$gpterm.'.',)
                    183:                  .'</p>'
                    184:         );
1.1       raeburn   185:     }
                    186:     $r->print(&Apache::loncommon::end_page());
                    187:     return OK;
                    188: }
                    189: 
                    190: sub create_board {
1.8       raeburn   191:     my ($cdom,$cnum,$group,$bbtitle) = @_;
                    192:     my ($outcome,$boardid,$newurl);
                    193:     $bbtitle=&unescape($bbtitle);
                    194:     # get lock on nohist_groupboards file
                    195:     my $lockhash = {
                    196:                   $group."\0".'locked_boardids' => $env{'user.name'}.
                    197:                                                        ':'.$env{'user.domain'},
                    198:                    };
                    199:     my $tries = 0;
                    200:     my $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
                    201:     my $dellockoutcome;
                    202:     while (($gotlock ne 'ok') && $tries <3) {
                    203:         $tries ++;
                    204:         sleep 1;
                    205:         $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
                    206:     }
                    207:     if ($gotlock eq 'ok') {
                    208:        my %curr_boards = &Apache::lonnet::dump('nohist_groupboards',$cdom,$cnum,$group);
                    209:        $boardid = time;
                    210:        my $idtries = 0;
                    211:        while(exists($curr_boards{$group."\0".$boardid}) && $idtries < 20) {
                    212:            $boardid ++;
                    213:            $idtries ++;
                    214:        }
                    215:        if (!exists($curr_boards{$group."\0".$boardid})) {
                    216:            my %new_board = (
                    217:                           $group."\0".$boardid => $env{'user.name'}.':'.
                    218:                                                   $env{'user.domain'},
                    219:            );
                    220:            my $putresult = &Apache::lonnet::put('nohist_groupboards',\%new_board,
                    221:                                                 $cdom,$cnum);
                    222:            if ($putresult ne 'ok') {
1.11      albertel  223:                $outcome = 'error saving new board: '.$putresult;
1.8       raeburn   224:            } else {
                    225:                $newurl = '/adm/'.$cdom.'/'.$cnum.'/'.$boardid.
                    226:                          '/bulletinboard';
                    227:            }
                    228:        } else {
                    229:             $outcome = ('error: no unique ID for the new board available.');
                    230:        }
                    231:        #  remove lock
                    232:        my @del_lock = ($group."\0".'locked_boardids');
                    233:        $dellockoutcome = &Apache::lonnet::del('nohist_groupboards',\@del_lock,$cdom,$cnum);
                    234:     } else {
                    235:         $outcome = "error: could not obtain lockfile\n";
                    236:         $dellockoutcome = 'ok';
                    237:     }
                    238:     if (!$newurl) {
                    239:         return ($outcome,$newurl,$bbtitle,$dellockoutcome);
1.5       raeburn   240:     }
1.1       raeburn   241:     $newurl=&unescape($newurl);
1.8       raeburn   242:     # need to check here if group_boards_$group.sequence is in the course
                    243:     # if not - add it as an item in group_folder_$group.sequence 
1.4       raeburn   244:     my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
1.1       raeburn   245:     if ($allbbsmap =~ m|^/uploaded|) {
1.10      albertel  246:         my ($errtext,$fatal)=&LONCAPA::map::mapread($allbbsmap);
1.1       raeburn   247:         if (!$fatal) {
1.10      albertel  248:             my $newidx=&LONCAPA::map::getresidx($newurl);
                    249:             $LONCAPA::map::resources[$newidx]=$bbtitle.':'.$newurl.
1.1       raeburn   250:                                                    ':false:normal:res';
1.10      albertel  251:             push(@LONCAPA::map::order,$newidx);
                    252:             my ($errtext,$fatal)=&LONCAPA::map::storemap($allbbsmap,1);
1.1       raeburn   253:             if ($fatal) {
1.11      albertel  254:                 $outcome = "error: failed to save discussion boards map - $errtext\n";
1.1       raeburn   255:             } else {
1.5       raeburn   256:                 my %boardinfo = (
                    257:                   'group' => $group,
                    258:                 );
                    259:                 $outcome = &Apache::lonnet::put('bulletinpage_'.$boardid,
                    260:                                                 \%boardinfo,$cdom,$cnum);
1.1       raeburn   261:             }
                    262:         } else {
1.2       albertel  263:             $outcome = "error: failed to read all discussion boards map - $errtext\n";
1.1       raeburn   264:         }
                    265:     } else {
1.2       albertel  266:         $outcome = 'error: discussion boards folder absent, '.
1.1       raeburn   267:                    'or in unexpected location - '.$allbbsmap."\n";
                    268:     }
1.8       raeburn   269:     return ($outcome,$newurl,$bbtitle,$dellockoutcome);
1.1       raeburn   270: }
                    271: 
1.5       raeburn   272: sub display_error {
1.7       raeburn   273:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout,
                    274:         $refarg)=@_;
1.5       raeburn   275:     my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
1.7       raeburn   276:                                 $ucgpterm,$bodytitle,$refarg);
1.5       raeburn   277:     $output .= $earlyout;
                    278:     $output .= &Apache::loncommon::end_page();
                    279:     return $output;
                    280: }
                    281: 
                    282: sub boards_header {
1.7       raeburn   283:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$refarg,
                    284:         $jscript)=@_;
1.5       raeburn   285:     my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
1.7       raeburn   286:     if ($refarg) {
                    287:         &Apache::lonhtmlcommon::add_breadcrumb
                    288:         ({href=>"/adm/coursegroups",
                    289:           text=>"Groups",
                    290:           title=>"View course groups"},);
                    291:     }
1.5       raeburn   292:     &Apache::lonhtmlcommon::add_breadcrumb
1.7       raeburn   293:         ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
1.5       raeburn   294:           text=>"$ucgpterm: $description",
                    295:           title=>"Go to group's home page"},
1.7       raeburn   296:          {href=>"/adm/groupboards?group=$group&amp;$refarg",
1.5       raeburn   297:           text=>"Discussion Boards",
                    298:           title=>"Display group discussion boards"},);
                    299:     $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description));
                    300:     return $output;
                    301: }
                    302: 
1.1       raeburn   303: 1;

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