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