File:  [LON-CAPA] / loncom / interface / groupboards.pm
Revision 1.2: download - view: text, annotated - select for diffs
Fri Jun 30 14:08:35 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- style

    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::lonnavmaps;
   34: use Apache::lonuserstate;
   35: use Apache::lonratedt;
   36: use Apache::lonlocal;
   37: use LONCAPA;
   38: 
   39: sub handler {
   40:     my ($r) = @_;
   41:     &Apache::loncommon::content_type($r,'text/html');
   42:     $r->send_http_header;
   43:     return OK if $r->header_only;
   44: 
   45:     #  Needs to be in a course
   46:     if (! ($env{'request.course.fn'})) {
   47:         # Not in a course
   48:         $env{'user.error.msg'}=
   49:      "/adm/groupboards:mdg:0:0:Cannot edit or view course groups";
   50:         return HTTP_NOT_ACCEPTABLE;
   51:     }
   52: 
   53:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   54:                         ['group']);
   55: 
   56:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   57:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   58:     my $now = time;
   59:     my $crstype = &Apache::loncommon::course_type();
   60:     my $gpterm =  &Apache::loncommon::group_term();
   61:     my $bodytitle = &mt('[_1] Discussion Boards',$crstype);
   62:     my $group = $env{'form.group'};
   63:     if (!defined($group)) {
   64:         $r->print(&Apache::loncommon::start_page($bodytitle));
   65:         $r->print(&mt('No [_1] defined, so there are no [_1] discussion boards to display',$gpterm));
   66:         $r->print(&Apache::loncommon::end_page());
   67:         return OK;
   68:     }
   69:     my $can_create=&Apache::lonnet::allowed('cgb',$env{'request.course.id'}.
   70:                                             '/'.$group);
   71:     my $can_view=&Apache::lonnet::allowed('pgd',$env{'request.course.id'}.
   72:                                           '/'.$group);
   73:     if (defined($env{'form.newbul'})) {
   74:         if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
   75:             $r->print(&Apache::loncommon::start_page($bodytitle));
   76:             my ($outcome,$symb,$newurl,$bbtitle) = 
   77: 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
   78:             if ($outcome eq 'ok') {
   79:                 my ($furl,$ferr)= &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
   80:                 $r->print(&mt('The new discussion board was added successfully.<br />'));
   81:                 $r->print('<table border="0"><tr><td>'.
   82:                           '<a href="'.$newurl.'?register=1&symb='.$symb.
   83:                           '&group='.$group.'">'.
   84:                           &mt('Edit [_1] board',$bbtitle).'</a></td>'.
   85:                            '<td>&nbsp;&nbsp;</td><td>'.
   86:                           '<a href="/adm/groupboards?group='.$group.'">'.
   87:                           &mt('View all group discussion boards').
   88:                           '</a></td></tr></table>');
   89:             } else {
   90:                 $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.'">'.
   91:                          &mt('Return to discussion boards').'</a>');
   92:             }
   93:             $r->print(&Apache::loncommon::end_page());
   94:             return OK;
   95:         }
   96:     }
   97:     my $jscript;
   98:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
   99:         $jscript = qq|
  100: function makebulboard() {
  101:    var title=prompt('Discussion Board Title');
  102:    if (title) {
  103:     this.document.forms.newbb.newbul.value=
  104:         title+'=/adm/$cdom/$cnum/$now/bulletinboard';
  105:     this.document.forms.newbb.submit();
  106:    }
  107: }
  108: |;
  109:     }
  110:     $r->print(&Apache::loncommon::start_page($bodytitle,
  111:                                        '<script type="text/javascript">'.
  112:                                        $jscript.'</script>'));
  113:     if (!$can_view) {
  114:         $r->print(&mt('You do not have privileges to view discussion boards in this [_1]',$crstype));
  115:         return OK;
  116:     }
  117:     my $navmap = Apache::lonnavmaps::navmap->new();
  118:     my @groupboards;
  119:     my %boards;
  120:     my $grpbbmap = &get_bbfolder_url($cdom,$cnum,$group);
  121:     if ($grpbbmap) {
  122:         my $bbfolderres = $navmap->getResourceByUrl($grpbbmap);
  123:         if ($bbfolderres) {
  124:             my @boards = $navmap->retrieveResources($bbfolderres,undef,0,0);
  125:             foreach my $res (@boards) {
  126:                 my $url = $res->src();
  127:                 if ($url =~ m|^/adm/\Q$cdom\E/\Q$cnum\E/\d+/bulletinboard|) {
  128:                     push(@groupboards,$res->symb());
  129:                     $boards{$res->symb()} = {
  130:                                         title => $res->title(),
  131:                                         url   => $res->src(),
  132: 				    };
  133:                 }
  134:             }
  135:         }
  136:     }
  137:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
  138:         $r->print('<form method="post" name="newbb" action="/adm/groupboards">'.
  139:                   "\n".'<input type="button" name="bbbutton" value="'.
  140:                   &mt('New Discussion Board').
  141:                   '" onClick="javascript:makebulboard();" />'."\n".
  142:                   '  <input type="hidden" name="newbul" />'."\n".
  143:                   '  <input type="hidden" name="group" value="'.$group.'" />'.
  144:                   "\n".'</form><br />');
  145:     }
  146:     if (@groupboards) {
  147:         foreach my $board (@groupboards) {
  148:             $r->print('<a href="'.$boards{$board}{'url'}.'?register=1&amp;symb='.$board.'&amp;group='.$group.'">'.$boards{$board}{'title'}.'</a><br />');
  149:         }
  150:     } else {
  151:         $r->print(&mt('There are currently no discussion boards in this [_1].',
  152:                       $gpterm));
  153:     }
  154:     $r->print(&Apache::loncommon::end_page());
  155:     return OK;
  156: }
  157: 
  158: sub create_board {
  159:     my ($cdom,$cnum,$group,$newboard,$symb) = @_;
  160:     my ($bbtitle,$newurl)=split(/\=/,$newboard);
  161:     $bbtitle=&unescape($bbtitle);
  162:     $newurl=&unescape($newurl);
  163:     my $allbbsmap = &get_bbfolder_url($cdom,$cnum,$group);
  164:     my ($outcome,$symb);
  165:     if ($allbbsmap =~ m|^/uploaded|) {
  166:         my ($errtext,$fatal)=&Apache::lonratedt::mapread($allbbsmap);
  167:         if (!$fatal) {
  168:             my $newidx=&Apache::lonratedt::getresidx($newurl);
  169:             $Apache::lonratedt::resources[$newidx]=$bbtitle.':'.$newurl.
  170:                                                    ':false:normal:res';
  171:             push(@Apache::lonratedt::order,$newidx);
  172:             my ($errtext,$fatal)=&Apache::lonratedt::storemap($allbbsmap,1);
  173:             if ($fatal) {
  174:                 $outcome = "error: failed to store discussion boards map - $errtext\n";
  175:             } else {
  176:                 $outcome = 'ok';
  177:                 $symb = &Apache::lonnet::encode_symb($allbbsmap,$newidx,$newurl);
  178:             }
  179:         } else {
  180:             $outcome = "error: failed to read all discussion boards map - $errtext\n";
  181:         }
  182:     } else {
  183:         $outcome = 'error: discussion boards folder absent, '.
  184:                    'or in unexpected location - '.$allbbsmap."\n";
  185:     }
  186:     return ($outcome,$symb,$newurl,$bbtitle);
  187: }
  188: 
  189: sub get_bbfolder_url {
  190:     my ($cdom,$cnum,$group) = @_;
  191:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
  192:     my $grpbbmap;
  193:     if (%curr_groups) {
  194:         my %group_info =  &Apache::longroup::get_group_settings(
  195:                                                     $curr_groups{$group});
  196:         my $creation = $group_info{'creation'};
  197:         my $bbfolder = $creation + 1;
  198:         my $crspath = '/uploaded/'.$cdom.'/'.$cnum.'/';
  199:         $grpbbmap = $crspath.'default_'.$bbfolder.'.sequence';
  200:     }
  201:     return $grpbbmap;
  202: }
  203: 
  204: 1;

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