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

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::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));
1.2       albertel   76:             my ($outcome,$symb,$newurl,$bbtitle) = 
                     77: 		&create_board($cdom,$cnum,$group,$env{'form.newbul'});
1.1       raeburn    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>'.
1.3     ! raeburn    82:                           '<a href="'.$newurl.'?group='.$group.'">'.
1.1       raeburn    83:                           &mt('Edit [_1] board',$bbtitle).'</a></td>'.
                     84:                            '<td>&nbsp;&nbsp;</td><td>'.
                     85:                           '<a href="/adm/groupboards?group='.$group.'">'.
                     86:                           &mt('View all group discussion boards').
                     87:                           '</a></td></tr></table>');
                     88:             } else {
1.2       albertel   89:                 $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    90:                          &mt('Return to discussion boards').'</a>');
                     91:             }
                     92:             $r->print(&Apache::loncommon::end_page());
                     93:             return OK;
                     94:         }
                     95:     }
                     96:     my $jscript;
                     97:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
                     98:         $jscript = qq|
                     99: function makebulboard() {
                    100:    var title=prompt('Discussion Board Title');
                    101:    if (title) {
                    102:     this.document.forms.newbb.newbul.value=
                    103:         title+'=/adm/$cdom/$cnum/$now/bulletinboard';
                    104:     this.document.forms.newbb.submit();
                    105:    }
                    106: }
                    107: |;
                    108:     }
                    109:     $r->print(&Apache::loncommon::start_page($bodytitle,
                    110:                                        '<script type="text/javascript">'.
                    111:                                        $jscript.'</script>'));
                    112:     if (!$can_view) {
                    113:         $r->print(&mt('You do not have privileges to view discussion boards in this [_1]',$crstype));
                    114:         return OK;
                    115:     }
                    116:     my $navmap = Apache::lonnavmaps::navmap->new();
                    117:     my @groupboards;
                    118:     my %boards;
                    119:     my $grpbbmap = &get_bbfolder_url($cdom,$cnum,$group);
                    120:     if ($grpbbmap) {
                    121:         my $bbfolderres = $navmap->getResourceByUrl($grpbbmap);
                    122:         if ($bbfolderres) {
                    123:             my @boards = $navmap->retrieveResources($bbfolderres,undef,0,0);
                    124:             foreach my $res (@boards) {
                    125:                 my $url = $res->src();
                    126:                 if ($url =~ m|^/adm/\Q$cdom\E/\Q$cnum\E/\d+/bulletinboard|) {
1.2       albertel  127:                     push(@groupboards,$res->symb());
                    128:                     $boards{$res->symb()} = {
                    129:                                         title => $res->title(),
                    130:                                         url   => $res->src(),
                    131: 				    };
1.1       raeburn   132:                 }
                    133:             }
                    134:         }
                    135:     }
                    136:     if (($can_create) || (&Apache::lonnet::allowed('mdg',$env{'request.course.id'}))) {
                    137:         $r->print('<form method="post" name="newbb" action="/adm/groupboards">'.
                    138:                   "\n".'<input type="button" name="bbbutton" value="'.
                    139:                   &mt('New Discussion Board').
                    140:                   '" onClick="javascript:makebulboard();" />'."\n".
                    141:                   '  <input type="hidden" name="newbul" />'."\n".
                    142:                   '  <input type="hidden" name="group" value="'.$group.'" />'.
                    143:                   "\n".'</form><br />');
                    144:     }
                    145:     if (@groupboards) {
                    146:         foreach my $board (@groupboards) {
1.3     ! raeburn   147:             $r->print('<a href="'.$boards{$board}{'url'}.'?group='.$group.'">'.$boards{$board}{'title'}.'</a><br />');
1.1       raeburn   148:         }
                    149:     } else {
                    150:         $r->print(&mt('There are currently no discussion boards in this [_1].',
                    151:                       $gpterm));
                    152:     }
                    153:     $r->print(&Apache::loncommon::end_page());
                    154:     return OK;
                    155: }
                    156: 
                    157: sub create_board {
                    158:     my ($cdom,$cnum,$group,$newboard,$symb) = @_;
                    159:     my ($bbtitle,$newurl)=split(/\=/,$newboard);
                    160:     $bbtitle=&unescape($bbtitle);
                    161:     $newurl=&unescape($newurl);
                    162:     my $allbbsmap = &get_bbfolder_url($cdom,$cnum,$group);
                    163:     my ($outcome,$symb);
                    164:     if ($allbbsmap =~ m|^/uploaded|) {
                    165:         my ($errtext,$fatal)=&Apache::lonratedt::mapread($allbbsmap);
                    166:         if (!$fatal) {
                    167:             my $newidx=&Apache::lonratedt::getresidx($newurl);
                    168:             $Apache::lonratedt::resources[$newidx]=$bbtitle.':'.$newurl.
                    169:                                                    ':false:normal:res';
1.2       albertel  170:             push(@Apache::lonratedt::order,$newidx);
1.1       raeburn   171:             my ($errtext,$fatal)=&Apache::lonratedt::storemap($allbbsmap,1);
                    172:             if ($fatal) {
1.2       albertel  173:                 $outcome = "error: failed to store discussion boards map - $errtext\n";
1.1       raeburn   174:             } else {
                    175:                 $outcome = 'ok';
                    176:                 $symb = &Apache::lonnet::encode_symb($allbbsmap,$newidx,$newurl);
                    177:             }
                    178:         } else {
1.2       albertel  179:             $outcome = "error: failed to read all discussion boards map - $errtext\n";
1.1       raeburn   180:         }
                    181:     } else {
1.2       albertel  182:         $outcome = 'error: discussion boards folder absent, '.
1.1       raeburn   183:                    'or in unexpected location - '.$allbbsmap."\n";
                    184:     }
                    185:     return ($outcome,$symb,$newurl,$bbtitle);
                    186: }
                    187: 
                    188: sub get_bbfolder_url {
                    189:     my ($cdom,$cnum,$group) = @_;
                    190:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                    191:     my $grpbbmap;
                    192:     if (%curr_groups) {
                    193:         my %group_info =  &Apache::longroup::get_group_settings(
                    194:                                                     $curr_groups{$group});
                    195:         my $creation = $group_info{'creation'};
                    196:         my $bbfolder = $creation + 1;
                    197:         my $crspath = '/uploaded/'.$cdom.'/'.$cnum.'/';
                    198:         $grpbbmap = $crspath.'default_'.$bbfolder.'.sequence';
                    199:     }
                    200:     return $grpbbmap;
                    201: }
                    202: 
                    203: 1;

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