Annotation of loncom/interface/longroup.pm, revision 1.10
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # accessor routines used to provide information about course groups
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: #
1.4 albertel 26:
1.1 raeburn 27: package Apache::longroup;
1.4 albertel 28:
1.1 raeburn 29: use strict;
30: use Apache::lonnet;
31:
32: ###############################################
33: =pod
34:
35: =item coursegroups
36:
37: Retrieve information about groups in a course,
38:
39: Input:
40: 1. Optional course domain
41: 2. Optional course number
42: 3. Optional group name
43:
44: Course domain and number will be taken from user's
45: environment if not supplied. Optional group name will
46: be passed to lonnet::get_coursegroups() as a regexp to
47: use in the call to the dump function.
1.4 albertel 48:
1.1 raeburn 49: Output
50: Returns hash of groups in the course (subject to the
51: optional group name filter). In the hash, the keys are
52: group names, and their corresponding values
53: are scalars containing group information in XML. This
54: can be sent to &get_group_settings() to be parsed.
55:
56: Side effects:
57: None.
58: =cut
59:
60: ###############################################
61:
62: sub coursegroups {
63: my ($cdom,$cnum,$group) = @_;
64: if (!defined($cdom) || !defined($cnum)) {
65: my $cid = $env{'request.course.id'};
66:
67: return if (!defined($cid));
68:
69: $cdom = $env{'course.'.$cid.'.domain'};
70: $cnum = $env{'course.'.$cid.'.num'};
71: }
72: my %curr_groups = &Apache::lonnet::get_coursegroups($cdom,$cnum,$group);
1.5 albertel 73: if (my $tmp = &Apache::lonnet::error(%curr_groups)) {
74: undef(%curr_groups);
1.3 albertel 75: &Apache::lonnet::logthis('Error retrieving groups: '.$tmp.' in '.$cnum.':'.$cdom);
1.1 raeburn 76: }
77: return %curr_groups;
78: }
79:
80: ###############################################
81:
82: =item get_group_settings
1.4 albertel 83:
1.1 raeburn 84: Uses TokeParser to extract group information from the
85: XML used to describe course groups.
1.4 albertel 86:
1.1 raeburn 87: Input:
88: Scalar containing XML - as retrieved from &coursegroups().
1.4 albertel 89:
1.1 raeburn 90: Output:
91: Hash containing group information as key=values for (a), and
92: hash of hashes for (b)
1.4 albertel 93:
1.1 raeburn 94: Keys (in two categories):
1.6 raeburn 95: (a) groupname, creator, creation, modified, startdate, enddate, quota.
1.1 raeburn 96: Corresponding values are name of the group, creator of the group
97: (username:domain), UNIX time for date group was created, and
1.6 raeburn 98: settings were last modified, file quota, and default start and end
99: access times for group members.
1.4 albertel 100:
1.1 raeburn 101: (b) functions returned in hash of hashes.
102: Outer hash key is functions.
103: Inner hash keys are chat,discussion,email,files,homepage,roster.
104: Corresponding values are either on or off, depending on
105: whether this type of functionality is available for the group.
1.4 albertel 106:
1.1 raeburn 107: =cut
108:
109: ###############################################
110:
111: sub get_group_settings {
112: my ($groupinfo)=@_;
113: my $parser=HTML::TokeParser->new(\$groupinfo);
114: my $token;
115: my $tool = '';
116: my $role = '';
117: my %content=();
118: while ($token=$parser->get_token) {
119: if ($token->[0] eq 'S') {
120: my $entry=$token->[1];
121: if ($entry eq 'functions' || $entry eq 'autosec') {
122: %{$content{$entry}} = ();
123: $tool = $entry;
124: } elsif ($entry eq 'role') {
125: if ($tool eq 'autosec') {
126: $role = $token->[2]{id};
127: @{$content{$tool}{$role}} = ();
128: }
129: } else {
130: my $value=$parser->get_text('/'.$entry);
131: if ($entry eq 'name') {
132: if ($tool eq 'functions') {
133: my $function = $token->[2]{id};
134: $content{$tool}{$function} = $value;
135: }
136: } elsif ($entry eq 'groupname') {
137: $content{$entry}=&unescape($value);
138: } elsif (($entry eq 'roles') || ($entry eq 'types') ||
139: ($entry eq 'sectionpick') || ($entry eq 'defpriv')) {
140: push(@{$content{$entry}},$value);
141: } elsif ($entry eq 'section') {
142: if ($tool eq 'autosec' && $role ne '') {
143: push(@{$content{$tool}{$role}},$value);
144: }
145: } else {
146: $content{$entry}=$value;
147: }
148: }
149: } elsif ($token->[0] eq 'E') {
150: if ($token->[1] eq 'functions' || $token->[1] eq 'autosec') {
151: $tool = '';
152: } elsif ($token->[1] eq 'role') {
153: $role = '';
154: }
155: }
156: }
157: return %content;
158: }
159:
160: ###############################################
161:
162: sub check_group_access {
163: my ($group) = @_;
164: my $access = 1;
165: my $now = time;
166: my ($start,$end) = split(/\./,$env{'user.role.gr/'.$env{'request.course,id'}.'/'.$group});
167: if (($end!=0) && ($end<$now)) { $access = 0; }
168: if (($start!=0) && ($start>$now)) { $access=0; }
169: return $access;
170: }
171:
172: ###############################################
173:
174: =pod
1.4 albertel 175:
1.1 raeburn 176: =item group_changes
177:
178: Add or drop group memberships in a course as a result of
179: changes in a user's roles/sections. Called by
180: &Apache::lonnet:assignrole()
1.4 albertel 181:
1.1 raeburn 182: Input:
183: 1. User's domain
184: 2. User's username
185: 3. Url of role
186: 4. Role
187: 5. End date of role
188: 6. Start date of role
189:
190: Checks to see if role for which assignment is being made is in a course.
191: If so, gathers information about auto-group population settings for
192: groups in the course.
193:
194: If role is being expired, will also expire any group memberships that
195: are specified for auto-group population for the specific role and
196: section (including section 'none' and 'all' sections), unless a
197: different role/section also included in auto-group population
198: for the course is included amongst the user's unexpired roles
199: and would trigger membership in teh same group(s)
200:
201: If role is being added, will add any group memberships specified
202: for auto-group population, unless use is already a group member.
203: Uses default group privileges and default start and end group access
204: times.
205:
206: Output
207: None
208:
209: Side effects:
210: May result in calls to Apache::lonnet::modify_group_roles()
211: and Apache::lonnet::modify_coursegroup_membership() to add
212: or expire group membership(s) for a user.
213:
214: =cut
215:
216: sub group_changes {
217: my ($udom,$uname,$url,$role,$origend,$origstart) = @_;
218: my $now = time;
219: my $chgtype;
220: if ($origend > 0 && $origend <= $now) {
221: $chgtype = 'drop';
222: } else {
223: $chgtype = 'add';
224: }
225: my ($cid,$cdom,$cnum,$sec);
226: if ($url =~ m-^(/[^/]+/[^/]+)/([^/]+)$-) {
227: $cid = $1;
228: $sec = $2;
229: } else {
230: $cid = $url;
231: }
232: my $courseid = $cid;
233: $courseid =~ s|^/||;
234: $courseid =~ s|/|_|;
235: my %crshash=&Apache::lonnet::coursedescription($cid);
236: $cdom = $crshash{'domain'};
237: $cnum = $crshash{'num'};
238: if (defined($cdom) && defined($cnum)) {
239: my %settings;
240: my @changegroups = ();
241: my %dropgroup = ();
242: my %dropstart = ();
243: my %addgroup = ();
244: my %curr_groups = &coursegroups($cdom,$cnum);
245: if (%curr_groups) {
246: foreach my $group (keys(%curr_groups)) {
247: %{$settings{$group}}=&get_group_settings($curr_groups{$group});
248: if ($chgtype eq 'add') {
249: if (!($settings{$group}{autoadd} eq 'on')) {
250: next;
251: }
252: } else {
253: if (!($settings{$group}{autodrop} eq 'on')) {
254: next;
255: }
256: }
257: my @autosec = ();
258: if (ref($settings{$group}{'autosec'}{$role}) eq 'ARRAY') {
259: @autosec = @{$settings{$group}{'autosec'}{$role}};
260: }
261: if ($sec eq '') {
262: $sec = 'none';
263: }
264: if ((grep(/^$sec$/,@autosec)) || (grep(/^all$/,@autosec))) {
265: push(@changegroups,$group);
266: }
267: }
268: }
269: if (@changegroups > 0) {
270: my %currpriv;
271: my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname,$cid);
1.5 albertel 272: if (my $tmp = &Apache::lonnet::error(%roleshash)) {
1.1 raeburn 273: &Apache::lonnet::logthis('Error retrieving roles: '.$tmp.
274: ' for '.$uname.':'.$udom);
275: } else {
276: my $group_privs = '';
277: foreach my $group (@changegroups) {
278: if ($chgtype eq 'add') {
279: if (ref($settings{$group}{'defpriv'}) eq 'ARRAY') {
280: $group_privs =
281: join(':',@{$settings{$group}{'defpriv'}});
282: }
283: }
284: my $key = $cid.'/'.$group.'_gr';
285: if (defined($roleshash{$key})) {
286: if ($roleshash{$key}=~ /^gr\/([^_]*)_(\d+)_([\-\d]+)$/) {
287: my $grpstart = $3;
288: my $grpend = $2;
289: $currpriv{$group} = $1;
290: if ($chgtype eq 'drop') {
291: if ($grpstart == -1) { next; } # deleted
292: if ($grpend == 0 || $grpend > $now) {
1.2 albertel 293: if (!defined($dropgroup{$group})) {
1.1 raeburn 294: $dropstart{$group} = $grpstart;
295: if ($grpstart > $now) {
296: $dropstart{$group} = $now;
297: }
298: $dropgroup{$group} = $now.':'.
299: $dropstart{$group}.
300: ':'.$currpriv{$group};
301: }
302: }
303: } elsif ($chgtype eq 'add') {
304: if (($grpstart == -1) || ($grpend > 0 &&
305: ($grpend < $settings{$group}{'enddate'} ||
306: $settings{$group}{'enddate'} == 0)) ||
307: ($grpstart > $settings{$group}{'startdate'})) {
1.2 albertel 308: if (!defined($addgroup{$group})) {
1.1 raeburn 309: $addgroup{$group} =
310: $settings{$group}{'enddate'}.':'.
311: $settings{$group}{'startdate'}.':'.
312: $group_privs;
313: }
314: }
315: }
316: }
317: } elsif ($chgtype eq 'add') {
318: $addgroup{$group} = $settings{$group}{'enddate'}.':'.
319: $settings{$group}{'startdate'}.':'.
320: $group_privs;
321: }
322: }
323: if ($chgtype eq 'add') {
324: foreach my $add (keys(%addgroup)) {
325: if (&Apache::lonnet::modify_group_roles($cdom,$cnum,
326: $add,$uname.':'.$udom,
327: $settings{$add}{'enddate'},
328: $settings{$add}{'startdate'},
329: $group_privs) eq 'ok') {
330: my %usersettings;
331: $usersettings{$add.':'.$uname.':'.$udom} =
332: $addgroup{$add};
333: my $roster_result =
334: &Apache::lonnet::modify_coursegroup_membership(
335: $cdom,$cnum,\%usersettings);
336: }
337: }
338: } elsif ($chgtype eq 'drop') {
339: foreach my $drop (keys(%dropgroup)) {
340: my $nodrop = 0;
341: if ($settings{$drop}{'autoadd'} eq 'on') {
342: foreach my $urole (keys(%{$settings{$drop}{'autosec'}})) {
343: if ($nodrop) {
344: last;
345: } else {
346: my @autosec = ();
347: if (ref($settings{$drop}{'autosec'}{$urole}) eq 'ARRAY') {
348: @autosec = @{$settings{$drop}{'autosec'}{$urole}};
349: }
350: foreach my $usec (@autosec) {
351: if ($usec eq 'all') {
352: foreach my $ukey (keys(%roleshash)) {
353: if ($ukey =~ /^\Q$cid\E(\/?\w*)_($urole)$/) {
1.2 albertel 354: if ($sec ne $1) {
1.1 raeburn 355: if ($roleshash{$ukey} =~ /_?(\d*)_?([\-\d]*)$/) {
356: my $roleend = $1;
357: if ((!$roleend) ||
358: ($roleend > $now)) {
359: $nodrop = 1;
360: last;
361: }
362: }
363: }
364: }
365: }
366: } else {
367: my $ukey = $cid.'/'.$usec.'_'.$urole;
368: if ($usec eq 'none') {
369: if ($sec eq '') {
370: next;
371: }
372: } else {
373: if ($usec eq $sec) {
374: next;
375: }
376: }
377: if (exists($roleshash{$ukey})) {
378: if ($roleshash{$ukey} =~
379: /_?(\d*)_?([\-\d]*)$/) {
380: my $roleend = $1;
381: if ((!$roleend) ||
382: ($roleend > $now)) {
383: $nodrop = 1;
384: last;
385: }
386: }
387: }
388: }
389: }
390: }
391: }
392: }
393: if (!$nodrop) {
394: if (&Apache::lonnet::modify_group_roles($cdom,
395: $cnum,$drop,
396: $uname.':'.$udom,$now,
397: $dropstart{$drop},
398: $currpriv{$drop})
399: eq 'ok') {
400: my %usersettings;
401: $usersettings{$drop.':'.$uname.':'.$udom} =
402: $dropgroup{$drop};
403: my $roster_result =
404: &Apache::lonnet::modify_coursegroup_membership(
405: $cdom,$cnum,\%usersettings);
406: }
407: }
408: }
409: }
410: }
411: }
412: }
413: return;
414: }
415:
416: ###############################################
417:
1.8 raeburn 418: sub get_fixed_privs {
419: my $fixedprivs = {
420: email => {sgm => 1},
421: discussion => {vgb => 1},
422: chat => {pgc => 1},
423: files => {rgf => 1},
424: roster => {vgm => 1},
425: homepage => {vgh => 1},
426: };
427: return $fixedprivs;
428: }
429:
430: ###############################################
431:
432: sub get_tool_privs {
433: my ($gpterm) = @_;
434: my $toolprivs = {
435: email => {
436: sgm => 'Send '.$gpterm.' mail',
437: sgb => 'Broadcast mail',
438: },
439: discussion => {
440: cgb => 'Create boards',
441: pgd => 'Post',
1.10 ! raeburn 442: egp => 'Edit own posts',
! 443: dgp => 'Hide/Delete any post',
1.8 raeburn 444: vgb => 'View boards',
445: },
446: chat => {
447: pgc => 'Chat',
448: },
449: files => {
450: rgf => 'Retrieve',
451: ugf => 'Upload',
452: mgf => 'Modify',
453: dgf => 'Delete',
454: agf => 'Control Access',
455: },
456: roster => {
1.10 ! raeburn 457: vgm => 'Basic Display',
! 458: vmd => 'Detailed Display',
1.8 raeburn 459: },
460: homepage => {
461: vgh => 'View page',
462: mgh => 'Modify page',
463: },
464: };
465: return $toolprivs;
466: }
467:
468: ###############################################
469:
470:
471: sub group_memberlist {
472: my ($cdom,$cnum,$groupname,$fixedprivs,$available) = @_;
473: my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
474: $groupname);
475: my %current = ();
476: my $hastools = 0;
477: my $addtools = 0;
1.9 raeburn 478: my %member_nums = (
479: 'previous' => 0,
480: 'future' => 0,
481: 'active' => 0,
482: );
1.8 raeburn 483: my $now = time;
484: if (keys(%membership) > 0) {
485: my %allnames = ();
486: foreach my $key (sort(keys(%membership))) {
487: if ($key =~ /^\Q$groupname\E:([^:]+):([^:]+)$/) {
488: my $uname = $1;
489: my $udom = $2;
490: my $user = $uname.':'.$udom;
491: my($end,$start,@userprivs) = split(/:/,$membership{$key});
492: unless ($start == -1) {
493: $allnames{$udom}{$uname} = 1;
494: $current{$user} = {
495: uname => $uname,
496: udom => $udom,
497: start => &Apache::lonlocal::locallocaltime($start),
498: currtools => [],
499: newtools => [],
500: privs => \@userprivs,
501: };
502:
503: if ($end == 0) {
504: $current{$user}{end} = 'No end date';
505: } else {
506: $current{$user}{end} =
507: &Apache::lonlocal::locallocaltime($end);
508: }
509: my $now = time;
510: if (($end > 0) && ($end < $now)) {
511: $current{$user}{changestate} = 'reenable';
512: $current{$user}{'status'} = 'previous';
1.9 raeburn 513: $member_nums{'previous'} ++;
1.8 raeburn 514: } elsif (($start > $now)) {
515: $current{$user}{changestate} = 'activate';
516: $current{$user}{'status'} = 'future';
1.9 raeburn 517: $member_nums{'future'} ++;
1.8 raeburn 518: } else {
519: $current{$user}{changestate} = 'expire';
520: $current{$user}{'status'} = 'active';
1.9 raeburn 521: $member_nums{'active'} ++;
1.8 raeburn 522: }
1.10 ! raeburn 523: if ((@userprivs > 0) && (ref($fixedprivs) eq 'HASH')) {
1.8 raeburn 524: foreach my $tool (sort(keys(%{$fixedprivs}))) {
525: foreach my $priv (keys(%{$$fixedprivs{$tool}})) {
526: if (grep/^$priv$/,@userprivs) {
527: push(@{$current{$user}{currtools}},$tool);
528: last;
529: }
530: }
531: }
532: $hastools = 1;
533: }
1.10 ! raeburn 534: if ((ref($available) eq 'ARRAY') && (@{$available} > 0)) {
1.8 raeburn 535: if (@{$current{$user}{currtools}} > 0) {
536: if ("@{$available}" ne "@{$current{$user}{currtools}}") {
537: foreach my $tool (@{$available}) {
538: unless (grep/^$tool$/,@{$current{$user}{currtools}}) {
539: push(@{$current{$user}{newtools}},$tool); }
540: }
541: }
542: } else {
543: @{$current{$user}{newtools}} = @{$available};
544:
545: }
546: if (@{$current{$user}{newtools}} > 0) {
547: $addtools = 1;
548: }
549: }
550: }
551: }
552: }
553: if (keys(%current) > 0) {
554: my %idhash;
555: foreach my $udom (keys(%allnames)) {
556: %{$idhash{$udom}} = &Apache::lonnet::idrget($udom,
557: keys(%{$allnames{$udom}}));
558: foreach my $uname (keys(%{$idhash{$udom}})) {
559: $current{$uname.':'.$udom}{'id'} = $idhash{$udom}{$uname};
560: }
561: foreach my $uname (keys(%{$allnames{$udom}})) {
562: $current{$uname.':'.$udom}{'fullname'} =
563: &Apache::loncommon::plainname($uname,$udom,
564: 'lastname');
565: }
566: }
567: }
568: }
1.10 ! raeburn 569: return (\%current,\%member_nums,$hastools,$addtools);
1.8 raeburn 570: }
571:
572: ###############################################
573:
1.6 raeburn 574: sub sum_quotas {
575: my ($courseid) = @_;
576: my $totalquotas = 0;
577: my ($cdom,$cnum);
578: if (!defined($courseid)) {
579: if (defined($env{'request.course.id'})) {
580: $courseid = $env{'request.course.id'};
581: $cdom = $env{'course.'.$courseid.'.domain'};
582: $cnum = $env{'course.'.$courseid.'.num'};
583: } else {
584: return '';
585: }
586: } else {
587: ($cdom,$cnum) = split(/_/,$courseid);
588: }
589: if ($cdom && $cnum) {
590: my %curr_groups = &coursegroups($cdom,$cnum);
591: if (%curr_groups) {
592: foreach my $group (keys(%curr_groups)) {
593: my %settings=&get_group_settings($curr_groups{$group});
594: my $quota = $settings{'quota'};
595: if ($quota eq '') {
596: $quota = 0;
597: }
598: $totalquotas += $quota;
599: }
600: } else {
601: return 0;
602: }
603: } else {
604: return '';
605: }
606: return $totalquotas;
607: }
608:
609: ###############################################
610:
1.7 raeburn 611: sub get_bbfolder_url {
612: my ($cdom,$cnum,$group) = @_;
613: my %curr_groups = &coursegroups($cdom,$cnum,$group);
614: my $grpbbmap;
615: if (%curr_groups) {
616: my $crspath = '/uploaded/'.$cdom.'/'.$cnum.'/';
1.9 raeburn 617: $grpbbmap = $crspath.'group_boards_'.$group.'.sequence';
1.7 raeburn 618: }
619: return $grpbbmap;
620: }
621:
622: ###############################################
623:
624: sub get_group_bbinfo {
1.10 ! raeburn 625: my ($cdom,$cnum,$group,$boardurl) = @_;
1.7 raeburn 626: my $navmap = Apache::lonnavmaps::navmap->new();
627: my @groupboards;
628: my %boardshash;
629: my $grpbbmap = &get_bbfolder_url($cdom,$cnum,$group);
630: if ($grpbbmap) {
631: my $bbfolderres = $navmap->getResourceByUrl($grpbbmap);
632: if ($bbfolderres) {
633: my @boards = $navmap->retrieveResources($bbfolderres,undef,0,0);
634: foreach my $res (@boards) {
635: my $url = $res->src();
1.10 ! raeburn 636: if ($url =~ m|^(/adm/\Q$cdom\E/\Q$cnum\E/\d+/bulletinboard)|) {
! 637: if ($boardurl) {
! 638: if ($boardurl =~ /^\Q$1\E/) {
! 639: push(@groupboards,$res->symb());
! 640: $boardshash{$res->symb()} = {
! 641: title => $res->title(),
! 642: url => $res->src(),
! 643: };
! 644: last;
! 645: }
! 646: } else {
! 647: push(@groupboards,$res->symb());
! 648: $boardshash{$res->symb()} = {
! 649: title => $res->title(),
! 650: url => $res->src(),
! 651: };
! 652: }
1.7 raeburn 653: }
654: }
655: }
656: }
657: undef($navmap);
658: return (\@groupboards,\%boardshash);
659: }
660:
661: ###############################################
662:
1.1 raeburn 663: 1;
664:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>