Annotation of loncom/interface/groupboards.pm, revision 1.9
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::lonuserstate;
34: use Apache::lonratedt;
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.9 ! raeburn 62: my $bodytitle = $crstype.' 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.'&'.$refarg.
113: '">'.&mt('Edit [_1] board',$bbtitle).'</a></td>'.
1.1 raeburn 114: '<td> </td><td>'.
1.7 raeburn 115: '<a href="/adm/groupboards?group='.$group.'&'.
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.5 raeburn 158: $r->print('<br />');
1.4 raeburn 159: foreach my $board (@{$groupboards}) {
1.5 raeburn 160: my $board_url = $$boards{$board}{'url'};
161: if ($board_url =~ /\?/) {
162: $board_url .= '&group='.$group;
163: } else {
164: $board_url .= '?group='.$group;
165: }
1.7 raeburn 166: $r->print('<a href="'.$board_url.'&'.$refarg.'">'.
167: $$boards{$board}{'title'}.'</a><br />');
1.1 raeburn 168: }
169: } else {
1.5 raeburn 170: $r->print('<br />'.&mt('There are currently no discussion boards in this [_1].',
1.1 raeburn 171: $gpterm));
172: }
173: $r->print(&Apache::loncommon::end_page());
174: return OK;
175: }
176:
177: sub create_board {
1.8 raeburn 178: my ($cdom,$cnum,$group,$bbtitle) = @_;
179: my ($outcome,$boardid,$newurl);
180: $bbtitle=&unescape($bbtitle);
181: # get lock on nohist_groupboards file
182: my $lockhash = {
183: $group."\0".'locked_boardids' => $env{'user.name'}.
184: ':'.$env{'user.domain'},
185: };
186: my $tries = 0;
187: my $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
188: my $dellockoutcome;
189: while (($gotlock ne 'ok') && $tries <3) {
190: $tries ++;
191: sleep 1;
192: $gotlock = &Apache::lonnet::newput('nohist_groupboards',$lockhash,$cdom,$cnum);
193: }
194: if ($gotlock eq 'ok') {
195: my %curr_boards = &Apache::lonnet::dump('nohist_groupboards',$cdom,$cnum,$group);
196: $boardid = time;
197: my $idtries = 0;
198: while(exists($curr_boards{$group."\0".$boardid}) && $idtries < 20) {
199: $boardid ++;
200: $idtries ++;
201: }
202: if (!exists($curr_boards{$group."\0".$boardid})) {
203: my %new_board = (
204: $group."\0".$boardid => $env{'user.name'}.':'.
205: $env{'user.domain'},
206: );
207: my $putresult = &Apache::lonnet::put('nohist_groupboards',\%new_board,
208: $cdom,$cnum);
209: if ($putresult ne 'ok') {
210: $outcome = 'error storing new board: '.$putresult;
211: } else {
212: $newurl = '/adm/'.$cdom.'/'.$cnum.'/'.$boardid.
213: '/bulletinboard';
214: }
215: } else {
216: $outcome = ('error: no unique ID for the new board available.');
217: }
218: # remove lock
219: my @del_lock = ($group."\0".'locked_boardids');
220: $dellockoutcome = &Apache::lonnet::del('nohist_groupboards',\@del_lock,$cdom,$cnum);
221: } else {
222: $outcome = "error: could not obtain lockfile\n";
223: $dellockoutcome = 'ok';
224: }
225: if (!$newurl) {
226: return ($outcome,$newurl,$bbtitle,$dellockoutcome);
1.5 raeburn 227: }
1.1 raeburn 228: $newurl=&unescape($newurl);
1.8 raeburn 229: # need to check here if group_boards_$group.sequence is in the course
230: # if not - add it as an item in group_folder_$group.sequence
1.4 raeburn 231: my $allbbsmap = &Apache::longroup::get_bbfolder_url($cdom,$cnum,$group);
1.1 raeburn 232: if ($allbbsmap =~ m|^/uploaded|) {
233: my ($errtext,$fatal)=&Apache::lonratedt::mapread($allbbsmap);
234: if (!$fatal) {
235: my $newidx=&Apache::lonratedt::getresidx($newurl);
236: $Apache::lonratedt::resources[$newidx]=$bbtitle.':'.$newurl.
237: ':false:normal:res';
1.2 albertel 238: push(@Apache::lonratedt::order,$newidx);
1.1 raeburn 239: my ($errtext,$fatal)=&Apache::lonratedt::storemap($allbbsmap,1);
240: if ($fatal) {
1.2 albertel 241: $outcome = "error: failed to store discussion boards map - $errtext\n";
1.1 raeburn 242: } else {
1.5 raeburn 243: my %boardinfo = (
244: 'group' => $group,
245: );
246: $outcome = &Apache::lonnet::put('bulletinpage_'.$boardid,
247: \%boardinfo,$cdom,$cnum);
1.1 raeburn 248: }
249: } else {
1.2 albertel 250: $outcome = "error: failed to read all discussion boards map - $errtext\n";
1.1 raeburn 251: }
252: } else {
1.2 albertel 253: $outcome = 'error: discussion boards folder absent, '.
1.1 raeburn 254: 'or in unexpected location - '.$allbbsmap."\n";
255: }
1.8 raeburn 256: return ($outcome,$newurl,$bbtitle,$dellockoutcome);
1.1 raeburn 257: }
258:
1.5 raeburn 259: sub display_error {
1.7 raeburn 260: my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$earlyout,
261: $refarg)=@_;
1.5 raeburn 262: my $output = &boards_header($cdom,$cnum,$group,$description,$gpterm,
1.7 raeburn 263: $ucgpterm,$bodytitle,$refarg);
1.5 raeburn 264: $output .= $earlyout;
265: $output .= &Apache::loncommon::end_page();
266: return $output;
267: }
268:
269: sub boards_header {
1.7 raeburn 270: my ($cdom,$cnum,$group,$description,$gpterm,$ucgpterm,$bodytitle,$refarg,
271: $jscript)=@_;
1.5 raeburn 272: my $output = &Apache::loncommon::start_page($bodytitle,$jscript);
1.7 raeburn 273: if ($refarg) {
274: &Apache::lonhtmlcommon::add_breadcrumb
275: ({href=>"/adm/coursegroups",
276: text=>"Groups",
277: title=>"View course groups"},);
278: }
1.5 raeburn 279: &Apache::lonhtmlcommon::add_breadcrumb
1.7 raeburn 280: ({href=>"/adm/$cdom/$cnum/$group/smppg?$refarg",
1.5 raeburn 281: text=>"$ucgpterm: $description",
282: title=>"Go to group's home page"},
1.7 raeburn 283: {href=>"/adm/groupboards?group=$group&$refarg",
1.5 raeburn 284: text=>"Discussion Boards",
285: title=>"Display group discussion boards"},);
286: $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('[_1] discussion boards - [_2]',$gpterm,$description));
287: return $output;
288: }
289:
1.1 raeburn 290: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>