File:  [LON-CAPA] / loncom / interface / groupboards.pm
Revision 1.12: download - view: text, annotated - select for diffs
Wed Jan 28 14:09:54 2009 UTC (15 years, 3 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
- Wrapped list of group discussion boards in data_table
- Optimized &mt() call

Additional changes in de.pm:
- Some updates related to 2.8.0 release

    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;
   28: 
   29: use strict;
   30: use Apache::Constants qw(:common :http);
   31: use Apache::loncommon();
   32: use Apache::lonnet;
   33: use Apache::lonuserstate();
   34: use LONCAPA::map();
   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','ref']);
   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();
   60:     my $ucgpterm = $gpterm;
   61:     $ucgpterm =~ s/^(\w)/uc($1)/e;
   62:     my $bodytitle = $crstype.' Discussion Boards';
   63:     my $group = $env{'form.group'};
   64:     $group =~ s/\W//g;
   65:     my ($description,$earlyout,$refarg);
   66: 
   67:     if (exists($env{'form.ref'})) {
   68:         $refarg = 'ref='.$env{'form.ref'};
   69:     }
   70: 
   71:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   72:     if (!defined($group)) {
   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,
   75:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
   76:         return OK;
   77:     }
   78:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
   79:     if (!defined($curr_groups{$group})) {
   80:         $earlyout = &mt('Invalid group');
   81:         $r->print(&display_error($cdom,$cnum,$group,$description,$gpterm,
   82:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
   83:         return OK;
   84:     }
   85:     my %content = &Apache::longroup::get_group_settings($curr_groups{$group});
   86:     $description = &unescape($content{'description'});
   87: 
   88:     my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
   89:                                             '/'.$group);
   90:     my $can_view = &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
   91: 
   92:     if (!$can_view) {
   93:         $can_view = &Apache::lonnet::allowed('vgb',$env{'request.course.id'}.
   94:                                           '/'.$group);
   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,
   99:                                  $ucgpterm,$bodytitle,$earlyout,$refarg));
  100:         return OK;
  101:     }
  102:     if (defined($env{'form.newbul'})) {
  103:         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
  104:             $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,
  105:                                      $ucgpterm,$bodytitle,$refarg));
  106:             my ($outcome,$newurl,$bbtitle,$dellockoutcome) = 
  107: 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
  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>'.
  112:                           '<a href="'.$newurl.'?group='.$group.'&amp;'.$refarg.
  113:                           '">'.&mt('Edit [_1] board',$bbtitle).'</a></td>'.
  114:                            '<td>&nbsp;&nbsp;</td><td>'.
  115:                           '<a href="/adm/groupboards?group='.$group.'&amp;'.
  116:                           $refarg.'">'.&mt('View all group discussion boards').
  117:                           '</a></td></tr></table>');
  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:                 }
  121:             } else {
  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.'">'.
  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|
  132: <script type="text/javascript">
  133: function makebulboard() {
  134:    var title=prompt('Discussion Board Title');
  135:    if (title) {
  136:     this.document.forms.newbb.newbul.value=title;
  137:     this.document.forms.newbb.submit();
  138:    }
  139: }
  140: </script>
  141: |;
  142:     }
  143:     $r->print(&boards_header($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,
  144:                              $bodytitle,$refarg,$jscript));
  145:     my ($groupboards,$boards) = &Apache::longroup::get_group_bbinfo($cdom,$cnum,
  146:                                                                     $group);
  147:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
  148:         $r->print('<br /><form method="post" name="newbb" action="/adm/groupboards">'.
  149:                   "\n".'<input type="button" name="bbbutton" value="'.
  150:                   &mt('New Discussion Board').
  151:                   '" onClick="javascript:makebulboard();" />'."\n".
  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'}.'" />'.
  155:                   "\n".'</form><br />');
  156:     }
  157:     if (@{$groupboards} > 0) {
  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:         );
  164:         foreach my $board (@{$groupboards}) {
  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:             }
  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:             );
  178:         }
  179:         $r->print(&Apache::loncommon::end_data_table());
  180:     } else {
  181:         $r->print('<p class="LC_info">'
  182:                  .&mt('There are currently no discussion boards in this '.$gpterm.'.',)
  183:                  .'</p>'
  184:         );
  185:     }
  186:     $r->print(&Apache::loncommon::end_page());
  187:     return OK;
  188: }
  189: 
  190: sub create_board {
  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') {
  223:                $outcome = 'error saving new board: '.$putresult;
  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);
  240:     }
  241:     $newurl=&unescape($newurl);
  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 
  244:     my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
  245:     if ($allbbsmap =~ m|^/uploaded|) {
  246:         my ($errtext,$fatal)=&LONCAPA::map::mapread($allbbsmap);
  247:         if (!$fatal) {
  248:             my $newidx=&LONCAPA::map::getresidx($newurl);
  249:             $LONCAPA::map::resources[$newidx]=$bbtitle.':'.$newurl.
  250:                                                    ':false:normal:res';
  251:             push(@LONCAPA::map::order,$newidx);
  252:             my ($errtext,$fatal)=&LONCAPA::map::storemap($allbbsmap,1);
  253:             if ($fatal) {
  254:                 $outcome = "error: failed to save discussion boards map - $errtext\n";
  255:             } else {
  256:                 my %boardinfo = (
  257:                   'group' => $group,
  258:                 );
  259:                 $outcome = &Apache::lonnet::put('bulletinpage_'.$boardid,
  260:                                                 \%boardinfo,$cdom,$cnum);
  261:             }
  262:         } else {
  263:             $outcome = "error: failed to read all discussion boards map - $errtext\n";
  264:         }
  265:     } else {
  266:         $outcome = 'error: discussion boards folder absent, '.
  267:                    'or in unexpected location - '.$allbbsmap."\n";
  268:     }
  269:     return ($outcome,$newurl,$bbtitle,$dellockoutcome);
  270: }
  271: 
  272: sub display_error {
  273:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout,
  274:         $refarg)=@_;
  275:     my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
  276:                                 $ucgpterm,$bodytitle,$refarg);
  277:     $output .= $earlyout;
  278:     $output .= &Apache::loncommon::end_page();
  279:     return $output;
  280: }
  281: 
  282: sub boards_header {
  283:     my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$refarg,
  284:         $jscript)=@_;
  285:     my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
  286:     if ($refarg) {
  287:         &Apache::lonhtmlcommon::add_breadcrumb
  288:         ({href=>"/adm/coursegroups",
  289:           text=>"Groups",
  290:           title=>"View course groups"},);
  291:     }
  292:     &Apache::lonhtmlcommon::add_breadcrumb
  293:         ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
  294:           text=>"$ucgpterm: $description",
  295:           title=>"Go to group's home page"},
  296:          {href=>"/adm/groupboards?group=$group&amp;$refarg",
  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: 
  303: 1;

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