File:  [LON-CAPA] / loncom / interface / loncoursegroups.pm
Revision 1.47: download - view: text, annotated - select for diffs
Fri Jul 7 21:04:57 2006 UTC (17 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Changing sequence names for all groups folder, individual group folder, and group's discussion board folder when creating group.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: loncoursegroups.pm,v 1.47 2006/07/07 21:04:57 raeburn Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: package Apache::loncoursegroups;
   29: 
   30: use strict;
   31: use Apache::lonnet;
   32: use Apache::loncommon;
   33: use Apache::lonhtmlcommon;
   34: use Apache::lonlocal;
   35: use Apache::lonnavmaps;
   36: use Apache::longroup;
   37: use Apache::portfolio;
   38: use Apache::Constants qw(:common :http);
   39: use lib '/home/httpd/lib/perl/';
   40: use LONCAPA;
   41: 
   42: sub handler {
   43:     my ($r) = @_;
   44: 
   45:     &Apache::loncommon::content_type($r,'text/html');
   46:     $r->send_http_header;
   47: 
   48:     if ($r->header_only) {
   49:         return OK;
   50:     }
   51: 
   52:     #  Needs to be in a course
   53:     if (! ($env{'request.course.fn'})) {
   54:         # Not in a course
   55:         $env{'user.error.msg'}=
   56:      "/adm/coursegroups:mdg:0:0:Cannot edit or view course groups";
   57:         return HTTP_NOT_ACCEPTABLE;
   58:     }
   59: 
   60:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   61:                         ['action','refpage','state','groupname','branch']);
   62:     my $function = &Apache::loncommon::get_users_function();
   63:     my $tabcol = &Apache::loncommon::designparm($function.'.tabbg');
   64:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   65:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   66: 
   67:     my $view_permission =
   68:           &Apache::lonnet::allowed('vcg',$env{'request.course.id'});
   69:     my $manage_permission =
   70:           &Apache::lonnet::allowed('mdg',$env{'request.course.id'});
   71:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   72: 
   73:     my $gpterm = &Apache::loncommon::group_term();
   74:     my $ucgpterm = $gpterm;
   75:     $ucgpterm =~ s/^(\w)/uc($1)/e;
   76:     my $crstype = &Apache::loncommon::course_type();
   77: 
   78:     my %functions = (
   79:                       email => 'E-mail',
   80:                       discussion => 'Discussion boards',
   81:                       chat => 'Chat',
   82:                       files => 'File repository',
   83:                       roster => 'Membership roster',
   84:                       homepage => $ucgpterm.' home page',
   85:                     );
   86: 
   87:     my %idx = ();
   88:     $idx{id} = &Apache::loncoursedata::CL_ID();
   89:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
   90:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
   91:     $idx{uname} = &Apache::loncoursedata::CL_SNAME();
   92:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
   93: 
   94:     my $rowColor1 = "#dddddd";
   95:     my $rowColor2 = "#eeeeee";
   96: 
   97:     my $action = $env{'form.action'};
   98:     my $state = $env{'form.state'};
   99:     if ((!defined($action)) || ($action eq 'view')) {
  100:         if (!defined($state)) {
  101:             $state = 'view';
  102:         }
  103:     }
  104:     if ($action eq 'create' || $action eq 'modify' || $action eq 'view') { 
  105:         if ($view_permission || $manage_permission) {
  106:             &group_administration($r,$action,$state,$cdom,$cnum,
  107:                                   $tabcol,\%functions,\%idx,$view_permission,
  108:                                   $manage_permission,$rowColor1,$rowColor2,
  109:                                   $gpterm,$ucgpterm,$crstype);
  110:         } else {
  111:             $r->print(&mt('You do not have [_1] administration '.
  112:                           'privileges in this [_2]',$gpterm,lc($crstype)));
  113:         }
  114:     } else {
  115:         &print_main_menu($r,$cdom,$cnum,\%functions,\%idx,$view_permission,
  116: 			 $manage_permission,$action,$state,$gpterm,$ucgpterm,
  117: 			 $crstype);
  118:     }
  119:     return OK;
  120: }
  121: 
  122: sub print_main_menu {
  123:     my ($r,$cdom,$cnum,$functions,$idx,$view_permission,$manage_permission,
  124: 	$action,$state,$gpterm,$ucgpterm,$crstype) = @_;
  125:     my $pagename = "$crstype $ucgpterm".'s';
  126:     my $jscript = qq|
  127: function changeSort(caller) {
  128:     document.$state.sortby.value = caller;
  129:     document.$state.submit();
  130: }\n|;
  131:     $r->print(&header($pagename,$jscript,$action,$state));
  132:     &Apache::lonhtmlcommon::add_breadcrumb
  133:         ({href=>"/adm/coursegroups",
  134:           text=>"$pagename"});
  135:     $r->print(&Apache::lonhtmlcommon::breadcrumbs($pagename));
  136:     &display_groups($r,$cdom,$cnum,$functions,$idx,$view_permission,
  137: 		    $manage_permission,$action,$state,$gpterm,$ucgpterm,
  138: 		    $crstype);
  139:     $r->print(&footer());
  140:     return;
  141: }
  142: 
  143: sub display_groups {
  144:     my ($r,$cdom,$cnum,$functions,$idx,$view_permission,
  145:         $manage_permission,$action,$state,$gpterm,$ucgpterm,$crstype) = @_;
  146:     my %curr_groups = ();
  147:     my %grp_info = ();
  148:     my %actionlinks = (
  149:       modify => '<a href="/adm/coursegroups?action=modify&refpage='.
  150:                          $env{'form.refpage'}.'&groupname=',
  151:       view => '<a href="/adm/'.$cdom.'/'.$cnum.'/',
  152:       delete => '<a href="/adm/coursegroups?action=delete&refpage='.
  153:                          $env{'form.refpage'}.'&groupname=',
  154:     );
  155:     my %lt = &Apache::lonlocal::texthash( 
  156:                           modify => 'Modify',
  157:                           view   => 'View',
  158:                           delete => 'Delete',
  159:                           act    => 'Action',
  160:                           gname  => "$ucgpterm Name",
  161:                           desc   => 'Description',
  162:                           crea   => 'Creator',
  163:                           crtd   => 'Created',
  164:                           last   => 'Last Modified',
  165:                           func   => 'Functionality',
  166:                           quot   => 'Quota (Mb)',
  167:                           memb   => 'Members',
  168:                           file   => 'Files',
  169:                           dibd   => 'Discussion Boards',
  170:                           dius   => 'Disk Use (%)',
  171:                           nogr   => 'No '.$gpterm.'s exist.',
  172:                           crng   => 'Create a new '.$gpterm,
  173:                           alth   => 'Although your current role has privileges'.
  174:                                     ' to view any existing '.$gpterm.'s in this'.
  175:                                     lc($crstype).', you do not have privileges'.
  176:                                     'to create new '.$gpterm.'s.',
  177:                      );
  178:     if ($view_permission) {
  179:         if (!defined($action)) {
  180:             $action = 'view';
  181:         }
  182:         my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
  183:         if (%curr_groups) {
  184:             if ($manage_permission) {
  185:                 $r->print('<br /><a href="/adm/coursegroups?action=create&refpage='.$env{'form.refpage'}.'">'.$lt{'crng'}.'</a>');
  186:             }
  187:             $r->print('<br /><br />');
  188: 	    $r->print(&Apache::loncommon::start_data_table().
  189: 		      &Apache::loncommon::start_data_table_header_row());
  190: 		      
  191:             $r->print(<<"END");
  192:         <th>$lt{'act'}</th>
  193:         <th><a href="javascript:changeSort('groupname')">$lt{'gname'}</a></th>
  194:         <th><a href="javascript:changeSort('description')">$lt{'desc'}</a></th>
  195:         <th><a href="javascript:changeSort('creator')">$lt{'crea'}</a></th>
  196:         <th><a href="javascript:changeSort('creation')">$lt{'crtd'}</a></th>
  197:         <th><a href="javascript:changeSort('modified')">$lt{'last'}</a></th>
  198:         <th>$lt{'func'}</b></td>
  199:         <th><a href="javascript:changeSort('quota')">$lt{'quot'}</a></th>
  200:         <th><a href="javascript:changeSort('totalmembers')">$lt{'memb'}</a></th>
  201:         <th><a href="javascript:changeSort('totalfiles')">$lt{'file'}</a></th>
  202:         <th><a href="javascript:changeSort('boards')">$lt{'dibd'}</a></th>
  203:         <th><a href="javascript:changeSort('diskuse')">$lt{'dius'}</a></th>
  204: END
  205: 	    $r->print(&Apache::loncommon::end_data_table_header_row());
  206:             my %Sortby = ();
  207:             foreach my $group (sort(keys(%curr_groups))) {
  208:                 %{$grp_info{$group}} = 
  209:                                   &Apache::longroup::get_group_settings(
  210:                                                          $curr_groups{$group});
  211:                 my $members_result = &group_members($cdom,$cnum,$group,
  212:                                                     \%grp_info);
  213:                 my $port_path = '/userfiles/groups/'.$group.'/portfolio';
  214:                 my $port_dir = &Apache::loncommon::propath($cdom,$cnum).$port_path;
  215:                 my $totaldirs = 0;
  216:                 my $totalfiles = 0;
  217:                 &group_files($group,$port_dir,\$totalfiles,\$totaldirs);
  218:                 $grp_info{$group}{'totalfiles'} = $totalfiles;
  219:                 $grp_info{$group}{'totaldirs'} = $totaldirs;
  220:                 my $diskuse = &Apache::lonnet::diskusage($cdom,$cnum,$port_dir);
  221:                 if ($grp_info{$group}{'quota'} > 0) {
  222:                     my $pct_use = 0.1 * $diskuse/$grp_info{$group}{'quota'};
  223:                     $grp_info{$group}{'diskuse'} = sprintf("%.0f",$pct_use);
  224:                 } else {
  225:                     $grp_info{$group}{'diskuse'} = 'N/A';
  226:                 }
  227:                 my ($groupboards,$boardshash)=&Apache::longroup::get_group_bbinfo(
  228:                                                                $cdom,$cnum,$group);
  229:                 $grp_info{$group}{'boards'} = scalar(@{$groupboards});
  230:                 if ($env{'form.sortby'} eq 'groupname') {
  231:                     push(@{$Sortby{$group}},$group);
  232:                 } elsif ($env{'form.sortby'} eq 'description') {
  233:                     push(@{$Sortby{$grp_info{$group}{'description'}}},$group);
  234:                 } elsif ($env{'form.sortby'} eq 'creator') {
  235:                     push(@{$Sortby{$grp_info{$group}{'creator'}}},$group);
  236:                 } elsif ($env{'form.sortby'} eq 'creation') {
  237:                     push(@{$Sortby{$grp_info{$group}{'creation'}}},$group);
  238:                 } elsif ($env{'form.sortby'} eq 'modified') {
  239:                     push(@{$Sortby{$grp_info{$group}{'modified'}}},$group);
  240:                 } elsif ($env{'form.sortby'} eq 'quota') {
  241:                     push(@{$Sortby{$grp_info{$group}{'quota'}}},$group);
  242:                 } elsif ($env{'form.sortby'} eq 'totalmembers') {
  243:                     push(@{$Sortby{$grp_info{$group}{'totalmembers'}}},
  244:                                                                        $group);
  245:                 } elsif ($env{'form.sortby'} eq 'totalfiles') {
  246:                     push(@{$Sortby{$grp_info{$group}{'totalfiles'}}},$group);
  247:                 } elsif ($env{'form.sortby'} eq 'boards') {
  248:                     push(@{$Sortby{$grp_info{$group}{'boards'}}},$group);
  249:                 } elsif ($env{'form.sortby'} eq 'diskuse') {
  250:                     push(@{$Sortby{$grp_info{$group}{'diskuse'}}},$group);
  251:                 } else {
  252:                     push(@{$Sortby{$group}},$group);
  253:                 }
  254:             }
  255:             foreach my $key (sort(keys(%Sortby))) {
  256:                 foreach my $group (@{$Sortby{$key}}) {
  257:                     my $description = 
  258: 			&unescape($grp_info{$group}{'description'});
  259:                     my $creator = $grp_info{$group}{'creator'};
  260:                     my $creation = $grp_info{$group}{'creation'};
  261:                     my $modified = $grp_info{$group}{'modified'}; 
  262:                     my $quota = $grp_info{$group}{'quota'};
  263:                     my $totalmembers = $grp_info{$group}{'totalmembers'};
  264:                     my $totalfiles = $grp_info{$group}{'totalfiles'};
  265:                     my $totaldirs = $grp_info{$group}{'totaldirs'};
  266:                     my $boards = $grp_info{$group}{'boards'};
  267:                     my $diskuse = $grp_info{$group}{'diskuse'};
  268:                     my $functionality;
  269:                     foreach my $tool (sort(keys(%{$functions}))) {
  270:                         if ($grp_info{$group}{functions}{$tool} eq 'on') {
  271:                             $functionality .= ' '.$tool;
  272:                         }
  273:                     }
  274:                     if (!$functionality) {
  275:                         $functionality = &mt('None available');
  276:                     }
  277:                     my $link = $actionlinks{$action};
  278:                     if ($action eq 'modify' || $action eq 'delete') {
  279:                         $link .= $group;
  280:                     } else {
  281:                         $link .= $group.'/grppg';
  282:                     }
  283:                     $link .= '">'.$lt{$action}.'</a>';
  284:                     if ($action eq 'view') { 
  285:                         if (($manage_permission) && 
  286:                             ($env{'form.refpage'} ne 'enrl')) {
  287:                             $link .= '&nbsp;&nbsp;'.$actionlinks{'modify'}.
  288:                                       $group.'">'.$lt{'modify'}.'</a>';
  289:                         }
  290:                     }
  291:                     $r->print(&Apache::loncommon::start_data_table_row('LC_data_table_dense').
  292: 			      '<td>'.$link.'</td>'.
  293: 			      '<td>'.$group.'</td>'.
  294: 			      '<td>'.$description.'</td>'.
  295: 			      '<td>'.$creator.'</td>'.
  296: 			      '<td>'. &Apache::lonnavmaps::timeToHumanString($creation).'</td>'.
  297: 			      '<td>'. &Apache::lonnavmaps::timeToHumanString($modified).'</td>'.
  298: 			      '<td>'.$functionality.'</td>'.
  299: 			      '<td align="right">'.$quota.'</td>'.
  300: 			      '<td align="right">'.$totalmembers.'</td>'.
  301: 			      '<td align="right"><nobr>'.&mt('Files: ').$totalfiles.'</nobr><br /><nobr>'.&mt('Folders: ').$totaldirs.'</nobr></td>'.
  302: 			      '<td align="right">'.$boards.'</td>'.
  303: 			      '<td align="right">'.$diskuse.'</td>'.
  304: 			      &Apache::loncommon::end_data_table_row());
  305:                 }
  306:             }
  307:             $r->print(&Apache::loncommon::end_data_table());
  308:             $r->print('<input type="hidden" name="refpage" '.
  309:                       'value="'.$env{'form.refpage'}.'" />');
  310:             if ($action eq 'view') {
  311:                 if (!defined($state)) {
  312:                     $state = 'view';
  313:                 }
  314:                 $r->print('<input type="hidden" name="state" value="'.
  315:                       $state.'" />');
  316:             }
  317:         } else {
  318:             $r->print($lt{'nogr'});
  319:             if ($manage_permission) {
  320:                 $r->print('<br /><br /><a href="/adm/coursegroups?action=create&refpage='.$env{'form.refpage'}.'">'.$lt{'crng'}.'</a>');
  321:             } else {
  322:                 $r->print('<br /><br />'.$lt{'alth'});
  323: 
  324:             }
  325:         }
  326:     } else {
  327:         my @coursegroups = split(/:/,$env{'request.course.groups'});
  328:         if (@coursegroups > 0) {
  329:             $r->print('<br /><br />');
  330:             my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
  331:             if (%curr_groups) {
  332:                 foreach my $group (@coursegroups) {
  333:                     my %group_info =  &Apache::longroup::get_group_settings(
  334:                                         $curr_groups{$group});
  335:                     my $description = &unescape(
  336:                                         $group_info{description});
  337:                     my ($uname,$udom) = split(/:/,$group_info{creator});
  338:                     $r->print('<font size="+1"><a href="/adm/'.$udom.'/'.$uname.'/'.$group.'/grppg">'.$group,'</a><font><br /><small>'.$description.'</small><br /><br />');
  339:                 }
  340:             }
  341:         } else {
  342:             $r->print(&mt('You are not currently a member of any '.
  343:                           'active [_1]s in this [_2]',$gpterm,
  344:                           lc($crstype)));
  345:         }
  346:     }
  347:     return;
  348: }
  349: 
  350: sub group_administration {
  351:     my ($r,$action,$state,$cdom,$cnum,$tabcol,$functions,$idx,
  352:         $view_permission,$manage_permission,$rowColor1,$rowColor2,$gpterm,
  353:         $ucgpterm,$crstype) = @_;
  354:     my %sectioncount = ();
  355:     my @tools = ();
  356:     my @types = ();
  357:     my @roles = ();
  358:     my @sections = ();
  359:     my %users = ();
  360:     my %userdata = ();
  361:     my @members = ();
  362:     my %usertools = ();
  363:     my %stored = ();
  364:     my %memchg;
  365:     my @member_changes = ('deletion','expire','activate','reenable',
  366:                           'changefunc','changepriv');
  367:     my ($groupname,$description,$startdate,$enddate,$granularity,$specificity,
  368:         $quota,$validate_script);
  369: 
  370:     if (defined($env{'form.groupname'})) {
  371:         $groupname = $env{'form.groupname'};
  372:     }
  373: 
  374:     if (($action eq 'create') && ($state eq '')) {
  375:         $state = 'pick_name';
  376:     }
  377:     if (($action eq 'create') || 
  378:         (($action eq 'modify') && ($state eq 'chgresult'))) { 
  379:         ($startdate,$enddate) = &get_dates_from_form();
  380:         if (defined($env{'form.description'})) {
  381:             $description = $env{'form.description'};
  382:         }
  383:         if (defined($env{'form.tool'})) {
  384:             @tools=&Apache::loncommon::get_env_multiple('form.tool');
  385:         }
  386:         if (defined($env{'form.granularity'})) {
  387:             $granularity=$env{'form.granularity'};
  388:         }
  389:         if (defined($env{'form.specificity'})) {
  390:             $specificity=$env{'form.specificity'};
  391:         }
  392:         if (defined($env{'form.quota'})) {
  393:             $quota=$env{'form.quota'};
  394:         }
  395:     }
  396:     if (($action eq 'create') || (($action eq 'modify') 
  397:         && (($state eq 'pick_privs') || ($state eq 'addresult')))) {
  398:         if (defined($env{'form.member'})) {
  399:             @members = &Apache::loncommon::get_env_multiple('form.member');
  400:             foreach my $user (@members) {
  401:                 %{$usertools{$user}} = ();
  402:             }
  403:         }
  404:     }
  405: 
  406:     if ($action eq 'modify') {
  407:         if ($state eq '') {
  408:             if (defined($env{'form.groupname'})) {
  409:                 $state = 'pick_task';
  410:             } else {
  411:                 $state = 'pick_group';
  412:             }
  413:         } else {
  414:             %stored = &retrieve_settings($cdom,$cnum,$groupname);
  415:             if (ref($stored{'types'}) eq 'ARRAY') {
  416:                 @types = @{$stored{'types'}};
  417:             }
  418:             if (ref($stored{'roles'}) eq 'ARRAY') {
  419:                 @roles = @{$stored{'roles'}};
  420:             }
  421:             if (ref($stored{'sectionpick'}) eq 'ARRAY') {
  422:                 @sections = @{$stored{'sectionpick'}};
  423:             }
  424:             unless ($state eq 'chgresult') {
  425:                 if (ref($stored{'tool'}) eq 'ARRAY') { 
  426:                     @tools = @{$stored{'tool'}};
  427:                 }
  428:                 $startdate = $stored{'startdate'};
  429:                 $enddate = $stored{'enddate'};
  430:                 $description = $stored{'description'};
  431:                 $granularity = $stored{'granularity'};
  432:                 $specificity =  $stored{'specificity'};
  433:                 $quota = $stored{'quota'};
  434:             }
  435:         }
  436:     }
  437: 
  438:     my $toolprivs = &Apache::longroup::get_tool_privs($gpterm);
  439: 
  440:     my $fixedprivs = &Apache::longroup::get_fixed_privs();
  441: 
  442:     my %elements = 
  443: 	(
  444: 	 create => {
  445: 	     pick_name => {
  446: 		 startdate_month  => 'selectbox',
  447: 		 startdate_hour   => 'selectbox',
  448: 		 enddate_month    => 'selectbox',
  449: 		 enddate_hour     => 'selectbox',
  450: 		 startdate_day    => 'text',
  451: 		 startdate_year   => 'text',
  452: 		 startdate_minute => 'text',
  453: 		 startdate_second => 'text',
  454: 		 enddate_day      => 'text',
  455: 		 enddate_year     => 'text',
  456: 		 enddate_minute   => 'text',
  457: 		 enddate_second   => 'text',
  458: 		 groupname        => 'text',
  459: 		 description      => 'text',
  460:                  quota            => 'text',
  461: 		 tool             => 'checkbox',
  462: 		 granularity      => 'radio',
  463: 		 no_end_date      => 'checkbox',
  464: 	     },
  465: 	     pick_members => {
  466: 		 member          => 'checkbox',
  467: 		 defpriv         => 'checkbox',
  468: 	     },
  469: 	 },
  470: 	 );
  471:     
  472:     $elements{'modify'} = {
  473: 	change_settings => {
  474: 	    %{$elements{'create'}{'pick_name'}},
  475: 	    specificity => 'radio',
  476: 	    defpriv     => 'checkbox',
  477: 	    autorole    => 'checkbox',
  478: 	    autoadd     => 'radio',
  479: 	    autodrop    => 'radio',
  480: 	},
  481: 	add_members => {
  482: 	    types       => 'selectbox',
  483: 	    roles       => 'selectbox',
  484: 	},
  485:     };
  486: 
  487:     if (ref($stored{'autorole'}) eq 'ARRAY') {
  488:         foreach my $role (@{$stored{'autorole'}}) {
  489:             unless ($role eq 'cc') {
  490:                 $elements{'modify'}{'change_settings'}{'sec_'.$role} = 
  491:                                                                    'selectbox';
  492:             }
  493:         }
  494:     }
  495: 
  496:     if (($action eq 'create') && ($state eq 'pick_name')) {
  497:         $elements{'create'}{'pick_name'}{'types'} = 'selectbox';
  498:         $elements{'create'}{'pick_name'}{'roles'} = 'selectbox';
  499:     }
  500:     if ((($action eq 'create') &&  
  501:         (($state eq 'pick_name') || ($state eq 'pick_privs'))) ||
  502:        (($action eq 'modify') && (($state eq 'change_settings') ||
  503:                                   ($state eq 'add_members')))) {
  504:         %sectioncount = &Apache::loncommon::get_sections($cdom,$cnum);
  505:         if (%sectioncount) {
  506:             $elements{'create'}{'pick_name'}{'sectionpick'} = 'selectbox';
  507:             $elements{'modify'}{'change_mapping'}{'sectionpick'} = 'selectbox';
  508:             $elements{'modify'}{'add_members'}{'sectionpick'} = 'selectbox';
  509:         }
  510:     }
  511: 
  512:     if (($action eq 'create') || 
  513:         ($action eq 'modify' && $state eq 'pick_members')) {
  514:         if (defined($env{'form.types'})) {
  515:             @types=&Apache::loncommon::get_env_multiple('form.types');
  516:         }
  517:         if (defined($env{'form.roles'})) {
  518:             @roles=&Apache::loncommon::get_env_multiple('form.roles');
  519:         }
  520:         if (defined($env{'form.sectionpick'})) {
  521:             @sections=&Apache::loncommon::get_env_multiple('form.sectionpick');
  522:             if (grep/^all$/,@sections) {
  523:                 @sections = sort {$a cmp $b} keys(%sectioncount);
  524:             }
  525:         }
  526:     }
  527: 
  528:     if (($state eq 'pick_members') || ($state eq 'pick_privs') || ($state eq 'change_privs')) {
  529:         &build_members_list($cdom,$cnum,\@types,\@roles,\@sections,\%users,
  530:                             \%userdata);
  531:     }
  532:     if ($state eq 'pick_members') {
  533:         if ((keys(%users) > 0) && (@tools > 0)) {
  534:             if ($granularity eq 'Yes') {
  535:                 $elements{$action}{'pick_members'}{'togglefunc'} = 'checkbox';
  536:             }
  537:             foreach my $tool (@tools) {
  538:                 if ($granularity eq 'Yes') {
  539:                     $elements{$action}{'pick_members'}{'user_'.$tool} = 'checkbox';
  540:                 }
  541:             }
  542:             $elements{$action}{'pick_members'}{'specificity'} = 'radio';
  543:         }
  544:     }
  545:     if ($state eq 'change_members') {
  546:         my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
  547:                                                                $groupname);
  548:         my $now = time;
  549:         my $num_expire = 0;
  550:         my $num_activate = 0;
  551:         my $num_reenable = 0;
  552:         my $num_deletion = 0;
  553:         my $numusers = 0;
  554:         foreach my $key (sort(keys(%membership))) {
  555:             if ($key =~ /^\Q$groupname\E:([^:]+:[^:]+)$/) {
  556:                 my $user = $1;
  557:                 my($end,$start,@userprivs) = split(/:/,$membership{$key});
  558:                 unless ($start == -1) {
  559:                     $numusers ++;
  560:                     $num_deletion ++;
  561:                     if (($end > 0) && ($end < $now)) {
  562:                         $num_reenable ++;
  563:                         next;
  564:                     } elsif (($start > $now)) {
  565:                         $num_activate = 1;
  566:                         next;
  567:                     } else {
  568:                         $num_expire ++;
  569:                         next;
  570:                     }
  571:                     next;
  572:                 }
  573:                 if ($num_reenable && $num_activate && $num_expire) {
  574:                     last;
  575:                 }
  576:             }
  577:         }
  578:         if ($num_deletion) {
  579:             $elements{$action}{'change_members'}{'deletion'} = 'checkbox';
  580:         }
  581:         if ($num_expire) {
  582:             $elements{$action}{'change_members'}{'expire'} = 'checkbox';
  583:         }
  584:         if ($num_activate) {
  585:             $elements{$action}{'change_members'}{'activate'} = 'checkbox';
  586:         }
  587:         if ($num_reenable) {
  588:             $elements{$action}{'change_members'}{'reenable'} = 'checkbox';
  589:         }
  590:         if ($numusers) {
  591:             if ($granularity eq 'Yes') {
  592:                 $elements{$action}{'change_members'}{'togglefunc'} = 'checkbox';
  593:             }
  594:             foreach my $tool (@tools) {
  595:                 if ($granularity eq 'Yes') {
  596:                     $elements{$action}{'change_members'}{'user_'.$tool} = 'checkbox';
  597:                 }
  598:             }
  599:             if ($specificity eq 'Yes') {
  600:                 $elements{$action}{'change_members'}{'changepriv'} = 'checkbox';
  601:             }
  602:         }
  603:     }
  604: 
  605:     if (($state eq 'pick_privs') || ($state eq 'change_privs') ||
  606:          (($specificity eq 'No') && 
  607:           ($state eq 'memresult' || $state eq 'result' || $state eq 'addresult'))) { 
  608:         foreach my $tool (@tools) {
  609:             my @values = &Apache::loncommon::get_env_multiple('form.user_'.$tool);
  610:             foreach my $user (@values) {
  611:                 if ($state eq 'pick_privs' || $state eq 'result' 
  612:                     || $state eq 'addresult') {
  613:                     if (!grep(/^\Q$user\E$/,@members)) {
  614:                         next;
  615:                     }
  616:                 }
  617:                 unless(exists($usertools{$user}{$tool})) {
  618:                     $usertools{$user}{$tool} = 1;
  619:                 }
  620:             }
  621:         }
  622:     }
  623: 
  624:     if (($action eq 'modify') && (($state eq 'change_privs') || ($state eq 'memresult'))) {
  625:         foreach my $chg (@member_changes) {
  626:             if (defined($env{'form.'.$chg})) {
  627:                 @{$memchg{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
  628:             }
  629:         }
  630:                                                                               
  631:         if ($state eq 'change_privs') {
  632:             my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
  633:                                                                    $groupname);
  634:             my $now = time;
  635:             foreach my $key (sort(keys(%membership))) {
  636:                 if ($key =~ /^\Q$groupname\E:([^:]+:[^:]+)$/) {
  637:                     my $user = $1;
  638:                     my $changefunc = 0;
  639:                     my ($end,$start,@userprivs) = split(/:/,$membership{$key});
  640:                     unless ($start == -1) {
  641:                         if (($end > 0) && ($end < $now)) {
  642:                             unless (grep/^$user$/,$memchg{'reenable'}) {
  643:                                 next;
  644:                             }
  645:                         }
  646:                         my @currtools = ();
  647:                         if (@userprivs > 0) {
  648:                             foreach my $tool (sort(keys(%{$fixedprivs}))) {
  649:                                 foreach my $priv (keys(%{$$fixedprivs{$tool}})) {
  650:                                     if (grep/^$priv$/,@userprivs) {
  651:                                         push(@currtools,$tool);
  652:                                         last;
  653:                                     }
  654:                                 }
  655:                             }
  656:                         }
  657:                         foreach my $tool (@currtools) {
  658:                             if (keys(%{$usertools{$user}}) > 0) {
  659:                                 if (!$usertools{$user}{$tool}) {
  660:                                     push(@{$memchg{'changefunc'}},$user);
  661:                                     $changefunc = 1;
  662:                                     last;
  663:                                 }
  664:                             } else {
  665:                                 push(@{$memchg{'changefunc'}},$user);
  666:                                 $changefunc = 1;
  667:                             }
  668:                         }
  669:                         if ($changefunc) {
  670:                             next;
  671:                         }
  672:                         if (keys(%{$usertools{$user}}) > 0) {
  673:                             foreach my $tool (keys(%{$usertools{$user}})) {
  674:                                 if (!grep/^$tool$/,@currtools) {
  675:                                     push(@{$memchg{'changefunc'}},$user);
  676:                                     $changefunc = 1;
  677:                                     last;
  678:                                 }
  679:                             }
  680:                         }
  681:                     }
  682:                 }
  683:             }
  684:             &check_changes(\@member_changes,\%memchg);
  685:             my %temptools;
  686:             foreach my $change (@member_changes) {
  687:                 if (($change eq 'deletion') || ($change eq 'expire')) {
  688:                     next;
  689:                 }
  690:                 foreach my $user (@{$memchg{$change}}) {
  691:                     unless (exists($usertools{$user})) {
  692:                         %{$usertools{$user}} = ();
  693:                     }
  694:                     %{$temptools{$user}} = %{$usertools{$user}}; 
  695:                 }
  696:             }
  697:             %usertools = %temptools;
  698:         } elsif ($state eq 'memresult') {
  699:             foreach my $change (@member_changes) {
  700:                 if ($change eq 'expire' || $change eq 'deletion') {
  701:                     next;
  702:                 }
  703:                 if (ref($memchg{$change}) eq 'ARRAY') { 
  704:                     my @users = @{$memchg{$change}};
  705:                     foreach my $user (@users) {
  706:                         unless (exists($usertools{$user})) {
  707:                             %{$usertools{$user}} = ();
  708:                         }
  709:                     }
  710:                 }
  711:             }
  712:         }
  713:     }
  714: 
  715:     if ((($state eq 'pick_privs') || ($state eq 'change_privs'))
  716:         && ($specificity eq 'Yes')) {
  717:         foreach my $user (sort(keys(%usertools))) {
  718:             foreach my $tool (keys(%{$usertools{$user}})) {
  719:                 foreach my $priv (keys(%{$$toolprivs{$tool}})) {
  720:                     unless (exists($$fixedprivs{$tool}{$priv})) {
  721:                         $elements{$action}{$state}{'userpriv_'.$priv} = 'checkbox';
  722:                     }
  723:                 }
  724:             }
  725:         }
  726:     }
  727: 
  728:     if (($action eq 'create' && $state eq 'pick_name') || 
  729:         ($action eq 'modify' && $state eq 'change_settings')) {
  730:         my ($crsquota,$freespace,$maxposs) = &get_quota_constraints($action,\%stored);
  731:         my $space_trim = '/^\s*|\s*\$/g,""';
  732:         my $float_check = '/^([0-9]*\.?[0-9]*)$/';
  733:         $validate_script = '
  734:     var newquota = document.'.$state.'.quota.value;
  735:     newquota.replace('.$space_trim.');
  736:     if (newquota == "" ) {
  737:         document.'.$state.'.quota.value = 0;
  738:         newquota = 0;       
  739:     }
  740:     var maxposs = '.$maxposs.';
  741:     if (newquota > maxposs) {
  742:         alert("The file repository quota you entered for this group ("+newquota+" Mb) exceeds the maximum possible ("+maxposs+" Mb). Please enter a smaller number.");
  743:         return;
  744:     }
  745:     var re_quota = '.$float_check.';
  746:     var check_quota = newquota.match(re_quota);
  747:     if (check_quota == null) {
  748:         alert("The quota you entered contains invalid characters, the quota should only include numbers, with or without a decimal point.");
  749:         return;
  750:     }
  751:     if (newquota == 0) {
  752:         var warn_zero = 0;
  753:         for (var i=0; i<document.'.$state.'.tool.length; i++) {
  754:             if (document.'.$state.'.tool[i].value == "files") {
  755:                 if (document.'.$state.'.tool[i].checked) {
  756:                     warn_zero = 1;
  757:                 }
  758:             }
  759:         }
  760:         if (warn_zero == 1) {
  761:             alert("You have indicated that the file repository should be enabled, but you have set the respository quota to 0 Mb.\nThis will prevent any upload of files.\nPlease set a value or disable the repository feature.");
  762:             return;
  763:         }
  764:     } 
  765: ';
  766:     }
  767:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
  768:     $jscript .= qq|
  769: function nextPage(formname,nextstate) {
  770:     formname.state.value= nextstate;
  771:     $validate_script
  772:     formname.submit();
  773: }
  774: function backPage(formname,prevstate) {
  775:     formname.state.value = prevstate;
  776:     formname.submit();
  777: }
  778: function changeSort(caller) {
  779:     document.$state.state.value = '$state';
  780:     document.$state.sortby.value = caller;
  781:     document.$state.submit();
  782: } 
  783: 
  784: |;
  785:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(
  786:                            \%{$elements{$action}{$state}},\%stored);
  787:     my $page = 0;
  788:     my %states = ();
  789:     my %branchstates = ();
  790:     @{$states{'create'}} = ('pick_name','pick_members','pick_privs','result');
  791:     @{$states{'modify'}} = ('pick_group','pick_task');
  792:     @{$branchstates{'noprivs'}} = ('result');
  793:     @{$branchstates{'settings'}} = ('change_settings','chgresult');
  794:     @{$branchstates{'members'}} = ('change_members','change_privs','memresult');
  795:     @{$branchstates{'adds'}} = ('add_members','pick_members','pick_privs',
  796:                                 'addresult');
  797: 
  798:     if (defined($env{'form.branch'})) {
  799:         push (@{$states{$action}},@{$branchstates{$env{'form.branch'}}});
  800:     }
  801: 
  802:     if (($action eq 'create') || ($action eq 'modify')) {
  803:         my $done = 0;
  804:         my $i=0;
  805:         while ($i<@{$states{$action}} && !$done) {
  806:             if ($states{$action}[$i] eq $state) {
  807:                 $page = $i;
  808:                 $done = 1;
  809:             }
  810:             $i++;
  811:         }
  812:     }
  813: 
  814:     my $loaditems =  &onload_action($action,$state);
  815:     my $crumbtitle = "$crstype $ucgpterm".'s'; 
  816:     $r->print(&header("$crumbtitle Manager",
  817: 		      $jscript,$action,$state,$page,$loaditems));
  818: 
  819:     if ($env{'form.refpage'} eq 'enrl') {
  820:         &Apache::lonhtmlcommon::add_breadcrumb
  821:         ({href=>"/adm/dropadd",
  822:           text=>"Enrollment Manager",
  823:           faq=>9,bug=>'Instructor Interface',});
  824:     } else {
  825:         &Apache::lonhtmlcommon::add_breadcrumb
  826:        ({href=>"/adm/coursegroups",
  827:           text=>"$crumbtitle",
  828:           faq=>9,bug=>'Instructor Interface',});
  829:     }
  830: 
  831:     my %trail = ();
  832:     %{$trail{'create'}} = &Apache::lonlocal::texthash (
  833:                             pick_name => $ucgpterm.' Settings',
  834:                             pick_members => 'Select Members',
  835:                             pick_privs => 'Choose Privileges',
  836:                             result => 'Creation Complete',
  837:                           );
  838:     %{$trail{'modify'}} = &Apache::lonlocal::texthash(
  839:                             pick_group => $ucgpterm.'s',
  840:                             pick_task => 'Choose Task',
  841:                             change_settings => "$ucgpterm Settings",
  842:                             change_members => 'Modify/Delete Members',
  843:                             change_privs => 'Change Privileges',
  844:                             change_mapping => 'Membership Mapping',
  845:                             add_members => 'Add Members',
  846:                             pick_members => 'Select Members',
  847:                             pick_privs => 'Choose Privileges',
  848:                             chgresult => 'Setting Changes Complete',
  849:                             memresult => 'Modifications Complete',
  850:                             addresult => 'Additions Complete',
  851:                           );
  852:     my %navbuttons = &Apache::lonlocal::texthash(
  853:                              gtns => 'Go to next step',
  854:                              gtps => 'Go to previous step',
  855:                              crgr => 'Create '.$gpterm,
  856:                              mose => 'Modify settings',
  857:                              gtpp => 'Go to previous page',
  858:                              adme => 'Add members',
  859:     );
  860:     if ((($action eq 'create') || ($action eq 'modify')) &&
  861:               ($manage_permission)) {
  862:         for (my $i=0; $i<@{$states{$action}}; $i++) {
  863:             if ($state eq $states{$action}[$i]) {
  864:                 &Apache::lonhtmlcommon::add_breadcrumb(
  865:                    {text=>"$trail{$action}{$state}"});
  866:                 $r->print(&Apache::lonhtmlcommon::breadcrumbs
  867: 			  ("$crumbtitle Manager"));
  868:                 &display_control($r,$cdom,$cnum,$tabcol,$action,$state,$page,
  869:                        \%sectioncount,$groupname,$description,$functions,
  870:                        \@tools,$toolprivs,$fixedprivs,$startdate,$enddate,
  871:                        \%users,\%userdata,$idx,\%memchg,\%usertools,
  872:                        $view_permission,$manage_permission,
  873:                        \%stored,$granularity,$quota,$specificity,\@types,\@roles,
  874:                        \@sections,\%states,\%navbuttons,$rowColor1,$rowColor2,
  875:                        $gpterm,$ucgpterm,$crstype);
  876:                 last;
  877:             } else {
  878:                 if (($state eq 'result') && ($i > 0)) {
  879:                     &Apache::lonhtmlcommon::add_breadcrumb(
  880:     {href=>"javascript:backPage(document.$state,'$states{$action}[0]')",
  881:       text=>"$trail{$action}{$states{$action}[$i]}"});
  882:                 } else { 
  883:                     &Apache::lonhtmlcommon::add_breadcrumb(
  884:      {href=>"javascript:backPage(document.$state,'$states{$action}[$i]')",
  885:       text=>"$trail{$action}{$states{$action}[$i]}"});
  886:                 }
  887:             }
  888:         }
  889:     } elsif (($action eq 'view') && ($view_permission)) {
  890:                         &Apache::lonhtmlcommon::add_breadcrumb(
  891:                    {text=>"View $gpterm".'s'});
  892:         my $crumbtitle = "$crstype $ucgpterm".'s Manager';
  893:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
  894: 		  (&mt($crumbtitle)));
  895:         &display_groups($r,$cdom,$cnum,$functions,$idx,$view_permission,
  896: 			$manage_permission,$action,$state,$gpterm,$ucgpterm,
  897: 			$crstype);
  898: 
  899:     }
  900:     $r->print(&footer());
  901:     return;
  902: }
  903: 
  904: sub retrieve_settings {
  905:     my ($cdom,$cnum,$groupname) = @_;
  906:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$groupname);
  907: 
  908:     return if (!%curr_groups);
  909: 
  910:     my %groupinfo = 
  911: 	&Apache::longroup::get_group_settings($curr_groups{$groupname});
  912: 
  913:     my %stored;
  914: 
  915:     $stored{'description'} = 
  916: 	&unescape($groupinfo{'description'});
  917:     $stored{'startdate'} = $groupinfo{'startdate'};
  918:     $stored{'enddate'} = $groupinfo{'enddate'};
  919:     if ($stored{'enddate'} == 0) {
  920: 	$stored{'no_end_date'} = 1;
  921:     }
  922:     $stored{'granularity'} = $groupinfo{'granularity'};
  923:     $stored{'specificity'} = $groupinfo{'specificity'};
  924:     $stored{'creation'} = $groupinfo{'creation'};
  925:     $stored{'creator'} = $groupinfo{'creator'};
  926:     $stored{'quota'} = $groupinfo{'quota'};
  927: 
  928:     foreach my $tool (sort(keys(%{$groupinfo{'functions'}}))) {
  929: 	if ($groupinfo{functions}{$tool} eq 'on') {
  930: 	    push(@{$stored{tool}},$tool);
  931: 	}
  932:     }
  933:     foreach my $role (@{$groupinfo{'roles'}}) {
  934: 	push(@{$stored{roles}},$role);
  935:     }
  936:     foreach my $type (@{$groupinfo{'types'}}) {
  937: 	push(@{$stored{types}},$type);
  938:     }
  939:     foreach my $section (@{$groupinfo{'sectionpick'}}) {
  940: 	push(@{$stored{sectionpick}},$section);
  941:     }
  942:     foreach my $defpriv (@{$groupinfo{'defpriv'}}) {
  943: 	push(@{$stored{defpriv}},$defpriv);
  944:     }
  945:     $stored{'autoadd'} = $groupinfo{'autoadd'};
  946:     $stored{'autodrop'} = $groupinfo{'autodrop'};
  947:     if (exists($groupinfo{'autosec'})) {
  948: 	foreach my $role (sort(keys(%{$groupinfo{'autosec'}}))) {
  949:             if (ref($groupinfo{'autosec'}{$role}) eq 'ARRAY') {
  950: 	        foreach my $section (@{$groupinfo{'autosec'}{$role}}) {
  951: 	            push (@{$stored{'sec_'.$role}},$section);
  952: 	        }
  953: 	        if (@{$groupinfo{'autosec'}{$role}} > 0) {
  954: 		    push(@{$stored{'autorole'}},$role);
  955: 	        }
  956:             }
  957: 	}
  958:     }
  959:     return %stored;
  960: }
  961: 
  962: sub display_control {
  963:     my ($r,$cdom,$cnum,$tabcol,$action,$state,$page,$sectioncount,$groupname,
  964:         $description,$functions,$tools,$toolprivs,$fixedprivs,$startdate,
  965:         $enddate,$users,$userdata,$idx,$memchg,$usertools,
  966:         $view_permission,$manage_permission,$stored,$granularity,$quota,
  967:         $specificity,$types,$roles,$sections,$states,$navbuttons,$rowColor1,
  968:         $rowColor2,$gpterm,$ucgpterm,$crstype) = @_;
  969:     if ($action eq 'create') {
  970:         if ($state eq 'pick_name') {
  971:             &general_settings_form($r,$cdom,$cnum,$action,$state,$page,
  972:                                    $functions,$tools,$toolprivs,$fixedprivs,
  973:                                    $sectioncount,$stored,$states,$navbuttons,
  974:                                    $gpterm,$ucgpterm,$crstype);
  975:         } elsif ($state eq 'pick_members') {
  976:             &choose_members_form($r,$cdom,$cnum,$action,$state,$page,
  977:                                  $groupname,$description,$granularity,$quota,
  978:                                  $startdate,$enddate,$tools,$fixedprivs,
  979:                                  $toolprivs,$functions,$users,$userdata,$idx,
  980:                                  $stored,$states,$navbuttons,$gpterm,$ucgpterm,
  981: 				 $crstype);
  982:         } elsif ($state eq 'pick_privs') {
  983:             &choose_privs_form($r,$cdom,$cnum,$action,$state,$page,
  984:                                $startdate,$enddate,$tools,$functions,
  985:                                $toolprivs,$fixedprivs,$userdata,$usertools,
  986:                                $idx,$states,$stored,$sectioncount,$navbuttons,
  987:                                $gpterm,$ucgpterm,$crstype);
  988:         } elsif ($state eq 'result') {
  989:             &process_request($r,$cdom,$cnum,$action,$state,$page,
  990:                              $groupname,$description,$specificity,$userdata,
  991:                              $startdate,$enddate,$tools,$functions,
  992:                              $toolprivs,$usertools,$idx,$types,$roles,
  993:                              $sections,$states,$navbuttons,$memchg,
  994:                              $sectioncount,$stored,$gpterm,$ucgpterm,$crstype);
  995:         }
  996:     } elsif ($action eq 'modify') {
  997:         my $groupname = $env{'form.groupname'};
  998:         if ($state eq 'pick_group') {
  999:             &display_groups($r,$cdom,$cnum,$functions,$idx,$view_permission,
 1000: 			    $manage_permission,$action,$state,$gpterm,
 1001: 			    $ucgpterm,$crstype);
 1002:         } elsif ($state eq 'pick_task') {
 1003:             &modify_menu($r,$groupname,$page,$gpterm);
 1004:         } elsif ($state eq 'change_settings') {
 1005:             &general_settings_form($r,$cdom,$cnum,$action,$state,$page,
 1006:                                    $functions,$tools,$toolprivs,$fixedprivs,
 1007:                                    $sectioncount,$stored,$states,$navbuttons,
 1008:                                    $gpterm,$ucgpterm,$crstype);
 1009:         } elsif ($state eq 'change_members') {
 1010:             &change_members_form($r,$cdom,$cnum,$tabcol,$action,$state,$page,
 1011:                                  $groupname,$description,$startdate,$enddate,
 1012:                                  $tools,$fixedprivs,$functions,$users,
 1013:                                  $userdata,$granularity,$quota,$specificity,
 1014:                                  $idx,$states,$navbuttons,$rowColor1,$rowColor2,
 1015:                                  $gpterm,$ucgpterm);
 1016:         } elsif ($state eq 'add_members') {
 1017:             &add_members_form($r,$action,$state,$page,$startdate,
 1018:                               $enddate,$groupname,$description,$granularity,
 1019:                               $quota,$sectioncount,$tools,$functions,$stored,
 1020:                               $states,$navbuttons,$gpterm,$ucgpterm);
 1021:         } elsif ($state eq 'pick_members') {
 1022:             &choose_members_form($r,$cdom,$cnum,$action,$state,$page,
 1023:                                  $groupname,$description,$granularity,$quota,
 1024:                                  $startdate,$enddate,$tools,$fixedprivs,
 1025:                                  $toolprivs,$functions,$users,$userdata,$idx,
 1026:                                  $stored,$states,$navbuttons,$gpterm,$ucgpterm,
 1027: 				 $crstype);
 1028:         } elsif ($state eq 'pick_privs') {
 1029:             &choose_privs_form($r,$cdom,$cnum,$action,$state,$page,
 1030:                                $startdate,$enddate,$tools,$functions,
 1031:                                $toolprivs,$fixedprivs,$userdata,$usertools,
 1032:                                $idx,$states,$stored,$sectioncount,$navbuttons,
 1033:                                $gpterm,$ucgpterm,$crstype);
 1034:         } elsif ($state eq 'change_privs') {
 1035:             &change_privs_form($r,$cdom,$cnum,$action,$state,$page,
 1036:                                $startdate,$enddate,$tools,$functions,
 1037:                                $toolprivs,$fixedprivs,$userdata,$usertools,
 1038:                                $memchg,$idx,$states,$stored,$sectioncount,
 1039:                                $navbuttons,$gpterm,$ucgpterm);
 1040:         } elsif ($state eq 'chgresult' || $state eq 'memresult' || 
 1041:                  $state eq 'addresult') {
 1042:             &process_request($r,$cdom,$cnum,$action,$state,$page,
 1043:                              $groupname,$description,$specificity,$userdata,
 1044:                              $startdate,$enddate,$tools,$functions,
 1045:                              $toolprivs,$usertools,$idx,$types,$roles,
 1046:                              $sections,$states,$navbuttons,$memchg,
 1047:                              $sectioncount,$stored,$gpterm,$ucgpterm,$crstype);
 1048:         }
 1049:     }
 1050: }
 1051: 
 1052: sub header {
 1053:     my ($bodytitle,$jscript,$action,$state,$page,$loaditems) = @_;
 1054:     my $start_page=
 1055: 	&Apache::loncommon::start_page($bodytitle,
 1056: 				       '<script type="text/javascript">'.
 1057: 				       $jscript.'</script>',
 1058: 				       {'add_entries' => $loaditems,});
 1059:     my $output = <<"END";
 1060: $start_page
 1061: <form method="POST" name="$state">
 1062: 
 1063: END
 1064:     if ($action eq 'create' || $action eq 'modify') {
 1065:         $output .= <<"END";
 1066:  <input type="hidden" name="action" value="$action" />
 1067:  <input type="hidden" name="state" value="" />
 1068:  <input type="hidden" name="origin" value="$state" />
 1069:  <input type="hidden" name="page" value="$page" />
 1070: END
 1071:     }
 1072:     return $output;
 1073: }
 1074: 
 1075: sub onload_action {
 1076:     my ($action,$state) = @_;
 1077:     my %loaditems;
 1078:     if ((defined($env{'form.origin'})) && ($action eq 'create') &&
 1079:                 ($state eq 'pick_name' || $state eq 'pick_members' || 
 1080:                  $state eq 'pick_privs')) {
 1081:         unless ($env{'form.origin'} eq '') {
 1082: 	    $loaditems{'onload'} = 
 1083: 		'javascript:setFormElements(document.'.$state.')';
 1084:         }
 1085:     }
 1086:     if (($action eq 'modify') &&
 1087:                 ($state eq 'change_settings' || $state eq 'change_members' ||
 1088:                  $state eq 'change_privs' || $state eq 'add_members' ||
 1089:                  $state eq 'pick_members')) {
 1090: 	$loaditems{'onload'} = 
 1091: 	    'javascript:setFormElements(document.'.$state.')';
 1092:     }
 1093:     return \%loaditems;
 1094: }
 1095: 
 1096: sub footer {
 1097:     my $end_page = &Apache::loncommon::end_page();
 1098:        return(<<ENDFOOT);
 1099:    <input type="hidden" name="sortby" value="$env{'form.sortby'}" />
 1100:   </form>
 1101: $end_page
 1102: ENDFOOT
 1103: }
 1104: 
 1105: sub build_members_list {
 1106:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata) = @_;
 1107:     my %access = ();
 1108:     foreach my $role (@{$roles}) {
 1109:         %{$$users{$role}} = ();
 1110:     }
 1111:     foreach my $type (@{$types}) {
 1112:         $access{$type} = $type;
 1113:     }
 1114:     &Apache::loncommon::get_course_users($cdom,$cnum,\%access,$roles,
 1115:                                          $sections,$users,$userdata);
 1116:     return;
 1117: }
 1118: 
 1119: sub group_files {
 1120:     my ($group,$currdir,$numfiles,$numdirs) = @_;
 1121:     my $dirptr=16384;
 1122:     my @dir_list=&Apache::portfolio::get_dir_list($currdir,$group);
 1123:     foreach my $line (@dir_list) {
 1124:         my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
 1125:         if (($filename !~ /^\.\.?$/) && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/) && ($filename ne 'no_such_dir')) { 
 1126:             if ($dirptr&$testdir) {
 1127:                 $currdir .= '/'.$filename;
 1128:                 $$numdirs ++;
 1129:                 &group_files($numfiles,$numdirs)
 1130:             } else {
 1131:                 $$numfiles ++;
 1132:             }
 1133:         }
 1134:     }
 1135:     return;
 1136: }
 1137: 
 1138: sub group_members {
 1139:     my ($cdom,$cnum,$group,$group_info) = @_;
 1140:     my %memberhash = &Apache::lonnet::get_group_membership($cdom,$cnum,$group);
 1141:     my $now = time;
 1142:     my ($tmp)=keys(%memberhash);
 1143:     if ($tmp=~/^error:/) {
 1144:         $$group_info{'totalmembers'} = 'Unknown - an error occurred';
 1145:         return $tmp;
 1146:     }
 1147:     my $totalmembers = 0;
 1148:     my $active = 0;
 1149:     my $previous = 0;
 1150:     my $future = 0;
 1151:     foreach my $member (keys %memberhash) {
 1152:         $totalmembers ++;
 1153:         my ($end,$start) = split(/:/,$memberhash{$member});
 1154:         unless ($start == -1) {
 1155:             if (($end!=0) && ($end<$now)) {
 1156:                 $previous ++;
 1157:             } elsif (($start!=0) && ($start>$now)) {
 1158:                 $future ++;
 1159:             } else {
 1160:                $active ++;
 1161:             }
 1162:         }
 1163:     }
 1164:     if ($totalmembers == 0) {
 1165:         $$group_info{$group}{'totalmembers'} = 'None';
 1166:     } else {
 1167:         $$group_info{$group}{'totalmembers'} = '<nobr>'.$active.
 1168:             '&nbsp;-&nbsp;active</nobr><br /><nobr>'.$previous.
 1169:             '&nbsp;-&nbsp;previous</nobr><br /><nobr>'.$future.
 1170:             '&nbsp;-&nbsp;future</nobr>';
 1171:     }
 1172:     return 'ok';
 1173: }
 1174: 
 1175: 
 1176: sub general_settings_form {
 1177:     my ($r,$cdom,$cnum,$action,$formname,$page,$functions,$tools,
 1178:         $toolprivs,$fixedprivs,$sectioncount,$stored,$states,$navbuttons,
 1179:         $gpterm,$ucgpterm,$crstype) = @_;
 1180:     my ($nexttext,$prevtext);
 1181:     &groupsettings_options($r,$functions,$action,$formname,$stored,1,
 1182:                            $gpterm,$ucgpterm,$crstype);
 1183:     &access_date_settings($r,$action,$formname,$stored,2,$gpterm,$ucgpterm);
 1184:     if ($action eq 'create') {
 1185:         &membership_options($r,$action,$formname,$sectioncount,3,$gpterm,
 1186: 			    $ucgpterm);
 1187:         $nexttext = $$navbuttons{'gtns'};
 1188:     } else {
 1189:         my @available = ();
 1190:         my @unavailable = ();
 1191:         &check_tools($functions,$tools,\@available,\@unavailable);
 1192:         @{$tools} = sort(keys(%{$functions}));
 1193:         &privilege_specificity($r,$action,3,$tools,$stored,$toolprivs,
 1194: 			       $fixedprivs,\@available,$formname,
 1195: 			       $gpterm,$ucgpterm);
 1196:         &mapping_options($r,$action,$formname,$page,$sectioncount,
 1197:                          $states,$stored,$navbuttons,4,5,
 1198: 			 $gpterm,$ucgpterm,$crstype);
 1199:         $nexttext = $$navbuttons{'mose'};
 1200:     }
 1201:     $prevtext = $$navbuttons{'gtpp'};
 1202:     &display_navbuttons($r,$formname,$$states{$action}[$page-1],$prevtext,
 1203:                         $$states{$action}[$page+1],$nexttext);
 1204:     return;
 1205: }
 1206: 
 1207: sub groupsettings_options {
 1208:     my ($r,$functions,$action,$formname,$stored,$image,$gpterm,
 1209:         $ucgpterm,$crstype) = @_;
 1210:     my %lt = &Apache::lonlocal::texthash(
 1211:         'gdat' => "$ucgpterm open and close dates",
 1212:         'sten' => "Set a start date/time and end date/time for the $gpterm",
 1213:         'gfun' => "$ucgpterm functionality",
 1214:         'gnde' => "$ucgpterm name, description and available functionality",
 1215:         'desc' => 'Description',
 1216:         'func' => 'Functionality',
 1217:         'gnam' => "$ucgpterm Name",
 1218:         'doyo' => "Do you want to assign different functionality ".
 1219:                   "to different $gpterm members?",
 1220:     );
 1221:     my ($crsquota,$freespace,$maxposs) = &get_quota_constraints($action,$stored);
 1222:     &topic_bar($r,$image,$lt{'gnde'});
 1223:     $r->print('
 1224:      <table class="LC_descriptive_input">
 1225:       <tr>
 1226:        <td class="LC_description">'.$lt{'gnam'}.':</td>
 1227:        <td colspan="5">
 1228: ');
 1229:     if ($action eq 'create') {
 1230:         $r->print('<input type="text" name="groupname" size="25" />');
 1231:     } else {
 1232:         $r->print('<input type="hidden" name="groupname" value="'.
 1233:                          $env{'form.groupname'}.'" />'.$env{'form.groupname'});
 1234:     }
 1235:     $r->print(<<"END");
 1236:        </td>
 1237:       <tr>
 1238:       <tr>
 1239:        <td class="LC_description">$lt{'desc'}:</td>
 1240:        <td colspan="5"><input type="text" name="description" size="40"
 1241:                                                     value="" />
 1242:        </td>
 1243:       <tr>
 1244:       <tr>
 1245:        <td class="LC_description">$lt{'func'}:</td>
 1246: END
 1247:     my $numitems = keys(%{$functions});
 1248:     my $halfnum = int($numitems/2);
 1249:     my $remnum = $numitems%2;
 1250:     if ($remnum) {
 1251:         $halfnum ++;
 1252:     }
 1253:     my @allfunctions = sort(keys (%{$functions}));
 1254:     for (my $i=0; $i<$halfnum; $i++) {
 1255:         $r->print('<td><label><input type="checkbox" name="tool" value="'.
 1256:                   $allfunctions[$i].'" />&nbsp;'.
 1257:                    $$functions{$allfunctions[$i]}.'</label></td>
 1258:                    <td>&nbsp;</td><td>&nbsp;</td>');
 1259:     }
 1260:     $r->print('<td><input type="button" value="check all" '.
 1261:               'onclick="javascript:checkAll(document.'.$formname.'.tool)" />'.
 1262:               '</td></tr><tr><td>&nbsp;</td>');
 1263:     for (my $j=$halfnum; $j<@allfunctions; $j++) {
 1264:         $r->print('<td><label><input type="checkbox" name="tool" value="'.
 1265:                   $allfunctions[$j].'" />&nbsp;'.
 1266:                   $$functions{$allfunctions[$j]}.'</label></td>
 1267:                   <td>&nbsp;</td><td>&nbsp;</td>');
 1268:     }
 1269:     if ($remnum) {
 1270:         $r->print('<td>&nbsp;</td>');
 1271:     }
 1272:     $r->print('
 1273:        <td>
 1274:         <input type="button" value="uncheck all"
 1275:           onclick="javascript:uncheckAll(document.'.$formname.'.tool)" />
 1276:        </td>
 1277:       </tr>
 1278:       <tr>
 1279:        <td class="LC_description">'.&mt('Granularity:').'</td>
 1280:        <td colspan="10">'.$lt{'doyo'}.'&nbsp;<label><input type="radio" name="granularity" value="Yes" />'.&mt('Yes').'</label>&nbsp;<label><input type="radio" name="granularity" value="No" checked="checked" />'.&mt('No').'</label>');
 1281:     if ($action eq 'modify') {
 1282:         $r->print('&nbsp;&nbsp;('.&mt('Currently set to "[_1]"',
 1283:                                       $$stored{'granularity'}).')');
 1284:     }
 1285:     $r->print('
 1286:        </td>
 1287:       </tr>
 1288:       <tr>
 1289:        <td class="LC_description">'.&mt('Disk quota: ').'</td><td colspan="10">');
 1290:     if ($action eq 'create') {
 1291:         $r->print(&mt('If you enable the file repository for the [_1], allocate a disk quota.',$gpterm));
 1292:     } else {
 1293:         $r->print(&mt('Quota allocated to file repository:'));
 1294:     } 
 1295:     $r->print('&nbsp;<input type="text" name="quota" size="4" />Mb');
 1296:     if ($action eq 'create') {
 1297:         $r->print('<br />'.
 1298:                   &mt('A total of [_1] Mb is shared between all [_2]s in the '.
 1299:                   '[_3], and [_4] Mb are currently unallocated.',$crsquota,
 1300:                   $gpterm,lc($crstype),$freespace));
 1301:     } else {
 1302:         $r->print('&nbsp;&nbsp;('.&mt('The quota is currently [_1] Mb',
 1303:                                       $$stored{'quota'}).').');
 1304: 
 1305:         $r->print('<br />'.&mt('The quota can be increased to [_1] Mb, '.
 1306:                   'by adding all unallocated space for [_2]s in the [_3].',
 1307:                   $maxposs,$gpterm,lc($crstype)));
 1308:     }
 1309:     $r->print('
 1310:        </td>
 1311:       </tr>
 1312:      </table>
 1313: ');
 1314:     return;
 1315: }
 1316: 
 1317: sub get_quota_constraints {
 1318:     my ($action,$stored) = @_;
 1319:     my ($crsquota,$freespace,$maxposs); 
 1320:     $crsquota = $env{'course.'.$env{'request.course.id'}.'.internal.coursequota'};
 1321:     if ($crsquota eq '') {
 1322:         $crsquota = 20;
 1323:     }
 1324:     $freespace = $crsquota - &Apache::longroup::sum_quotas();
 1325:     if ($action eq 'create') {
 1326:         $maxposs = $freespace;
 1327:     } else {
 1328:         $maxposs = $$stored{'quota'} + $freespace;
 1329:     }
 1330:     return ($crsquota,$freespace,$maxposs);
 1331: }
 1332: 
 1333: sub membership_options {
 1334:     my ($r,$action,$state,$sectioncount,$image,$gpterm,$ucgpterm) = @_;
 1335:     my $crstype = &Apache::loncommon::course_type();
 1336:     my %lt = &Apache::lonlocal::texthash(
 1337:                 'pipa' => 'Pick parameters to generate membership list',
 1338:                 'gmem' => "$ucgpterm membership options",
 1339:                 'picr' => 'Pick the criteria to use to build a list of '.
 1340:                           lc($crstype).' users from which you will select ',
 1341:                 'meof' => "members of the new $gpterm.",
 1342:                 'admg' => "additional members of the $gpterm.",
 1343:                 'ifno' => "If you do not wish to add members when you first ".
 1344:                           "create the $gpterm, do not make any selections.",
 1345:                 'asub' => "A subsequent step will also allow you to specify automatic adding/dropping of $gpterm members triggered by specified role and section changes.",
 1346:                 'acty' => 'Access types',
 1347:                 'coro' => $crstype.' roles',
 1348:                 'cose' => $crstype.' sections',
 1349:              );
 1350:     my %status_types = (
 1351:                    active => &mt('Currently has access'),
 1352:                    previous => &mt('Previously had access'),
 1353:                    future => &mt('Will have future access'),
 1354:                    );
 1355: 
 1356:     #FIXME need to plumb around for the various cr roles defined by the user
 1357:     my @roles = ('st','cc','in','ta','ep');
 1358: 
 1359:     my @sections = keys(%{$sectioncount});
 1360: 
 1361:     &topic_bar($r,$image,$lt{'pipa'});
 1362:     $r->print('
 1363:      <b>'.$lt{'gmem'}.'</b><br />'.$lt{'picr'});
 1364:     if ($action eq 'create') {
 1365:         $r->print($lt{'meof'}.'<br />'.$lt{'ifno'}.'<br />'.$lt{'asub'});
 1366:     } else {
 1367:         $r->print($lt{'admg'});
 1368:     }
 1369:     $r->print('
 1370:      <br />
 1371:      <br />
 1372:      <table class="LC_status_selector">
 1373:       <tr>
 1374:        <th>'.$lt{'acty'}.'</th>
 1375:        <th>'.$lt{'coro'}.'</th>');
 1376:     if (@sections >0) {
 1377:         $r->print('
 1378:        <th>'.$lt{'cose'}.'</th>');
 1379:     }
 1380:     $r->print('</tr><tr><td>');
 1381:     $r->print(&Apache::lonhtmlcommon::status_select_row(\%status_types));
 1382:     $r->print('</td><td>');
 1383:     $r->print(&Apache::lonhtmlcommon::role_select_row(\@roles));
 1384:     if (@sections > 0) {
 1385:         @sections = sort {$a cmp $b} @sections;
 1386:         unshift(@sections,'none'); # Put 'no sections' next
 1387:         unshift(@sections,'all'); # Put 'all' at the front of the list
 1388:         $r->print('</td><td>'.
 1389:         &sections_selection(\@sections,'sectionpick').'</td>');
 1390:     }
 1391:     $r->print('
 1392:       </tr>
 1393:      </table>');
 1394:     return;
 1395: }
 1396: 
 1397: sub sections_selection {
 1398:     my ($sections,$elementname) = @_;
 1399:     my $section_sel;
 1400:     my $numvisible = 4;
 1401:     if (@{$sections} < 4) {
 1402:         $numvisible = @{$sections};
 1403:     }
 1404:     foreach my $sec (@{$sections}) {
 1405:         if ($sec eq 'all') {
 1406:             $section_sel .= '  <option value="'.$sec.'" selected="selected">'.&mt('all sections').'</option>'."\n";
 1407:         } elsif ($sec eq 'none') {
 1408:             $section_sel .= '  <option value="'.$sec.'">'.&mt('no section').'</option>'."\n"; 
 1409:         } else {
 1410:             $section_sel .= '  <option value="'.$sec.'">'.$sec."</option>\n";
 1411:         }
 1412:     }
 1413:     my $output = '
 1414:         <select name="'.$elementname.'" multiple="true" size="'.$numvisible.'">
 1415:           '.$section_sel.'
 1416:         </select>';
 1417:     return $output;
 1418: }
 1419: 
 1420: sub access_date_settings {
 1421:     my ($r,$action,$formname,$stored,$image,$gpterm,$ucgpterm) = @_;
 1422:     my %lt = &Apache::lonlocal::texthash(
 1423:                 'sten' => "Default start and end dates for $gpterm access",
 1424:              );
 1425:     my $starttime = time;
 1426:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
 1427:     if ($action eq 'modify') {
 1428:         $starttime = $$stored{'startdate'};
 1429:         unless ($$stored{'enddate'} == 0) {
 1430:             $endtime = $$stored{'enddate'};
 1431:         }
 1432:     }
 1433:     my ($table) = &date_setting_table($starttime,$endtime,$formname);
 1434:     &topic_bar($r,$image,$lt{'sten'});
 1435:     $r->print('
 1436:     '.$table.'
 1437:     ');
 1438:     return;
 1439: }
 1440: 
 1441: sub choose_members_form {
 1442:     my ($r,$cdom,$cnum,$action,$formname,$page,$groupname,$description,
 1443:         $granularity,$quota,$startdate,$enddate,$tools,$fixedprivs,$toolprivs,
 1444:         $functions,$users,$userdata,$idx,$stored,$states,$navbuttons,
 1445:         $gpterm,$ucgpterm,$crstype) = @_;
 1446:     my @regexps = ('user_','userpriv_','sec_');
 1447:     my %origmembers;
 1448:     $r->print(&Apache::lonhtmlcommon::echo_form_input(
 1449:          ['origin','action','state','page','member','specificity','branch',
 1450:           'defpriv','autorole','autoadd','autodrop','sortby','togglefunc'],
 1451:          \@regexps));
 1452:     my $earlyout = &validate_groupname($groupname,$action,$cdom,$cnum,$gpterm,
 1453:                                        $ucgpterm,$crstype);
 1454:     if ($earlyout) {
 1455: 	$r->print($earlyout);
 1456:         &display_navbuttons($r,$formname,$$states{$action}[$page-1],
 1457:                            $$navbuttons{'gtps'});
 1458:         return;
 1459:     } 
 1460:     my ($specimg,$memimg);
 1461:     my @available = ();
 1462:     my @unavailable = ();
 1463:     &check_tools($functions,$tools,\@available,\@unavailable);
 1464:     if ($action eq 'create') {
 1465:         &print_current_settings($r,$action,$functions,$startdate,$enddate,
 1466: 				$groupname,$description,$granularity,$quota,
 1467: 				\@available,\@unavailable,$gpterm,$ucgpterm);
 1468:         $specimg = 4;
 1469:         $memimg = 5;
 1470:     } else {
 1471:         $specimg = 2;
 1472:         $memimg = 3;
 1473:         my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
 1474:                                                                $groupname);
 1475:         foreach my $key (sort(keys(%membership))) {
 1476:             if ($key =~ /^\Q$groupname\E:([^:]+):([^:]+)$/) {
 1477:                 my ($end,$start,@userprivs) = split(/:/,$membership{$key});
 1478:                 unless ($start == -1) {  
 1479:                     my $uname = $1;
 1480:                     my $udom = $2;
 1481:                     my $user = $uname.':'.$udom;
 1482:                     $origmembers{$user} = 1; 
 1483:                 }
 1484:             }
 1485:         }
 1486:     }
 1487:     &privilege_specificity($r,$action,$specimg,$tools,$stored,$toolprivs,
 1488:                           $fixedprivs,\@available,$formname,$gpterm,$ucgpterm);
 1489:     my $newusers = &pick_new_members($r,$action,$formname,\@available,$idx,
 1490: 				     $stored,$memimg,$users,$userdata,
 1491: 				     $granularity,\%origmembers,$gpterm,
 1492: 				     $ucgpterm);
 1493:     if ($newusers || $action eq 'create') {
 1494:         &display_navbuttons($r,$formname,$$states{$action}[$page-1],
 1495:                             $$navbuttons{'gtps'},$$states{$action}[$page+1],
 1496:                             $$navbuttons{'gtns'});
 1497:     } else {
 1498:         &display_navbuttons($r,$formname,$$states{$action}[$page-1],
 1499:                             $$navbuttons{'gtps'});
 1500:     }
 1501:     return;
 1502: }
 1503: 
 1504: sub display_navbuttons {
 1505:     my ($r,$formname,$prev,$prevtext,$next,$nexttext) = @_;
 1506:     $r->print('<div class="LC_navbuttons">');
 1507:     if ($prev) {
 1508:         $r->print('
 1509:       <input type="button" name="previous" value = "'.$prevtext.'"
 1510:     onclick="javascript:backPage(document.'.$formname.','."'".$prev."'".')"/>
 1511:    &nbsp;&nbsp;&nbsp;');
 1512:     }
 1513:     if ($next) {
 1514:         $r->print('
 1515:       <input type="button" name="next" value="'.$nexttext.'"
 1516:  onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />');
 1517:     }
 1518:     $r->print('</div>');
 1519: }
 1520: 
 1521: sub check_tools {
 1522:     my ($functions,$tools,$available,$unavailable) = @_;
 1523:     foreach my $item (sort(keys(%{$functions}))) {
 1524:         if (grep/^$item$/,@{$tools}) {
 1525:             push(@{$available},$item);
 1526:         } else {
 1527:             push(@{$unavailable},$item);
 1528:         }
 1529:     }
 1530:     return;
 1531: }
 1532: 
 1533: sub print_current_settings {
 1534:     my ($r,$action,$functions,$startdate,$enddate,$groupname,$description,
 1535: 	$granularity,$quota,$available,$unavailable,$gpterm,$ucgpterm) = @_;
 1536: 
 1537:     my %lt = &Apache::lonlocal::texthash(
 1538:         grna => "$ucgpterm Name",
 1539:         desc => 'Description',
 1540:         grfn => "$ucgpterm Functions",
 1541:         gran => 'Granularity',
 1542:         quot => 'File quota',
 1543:         dfac => 'Default access dates',
 1544:         ygrs => "Your $gpterm selections",
 1545:         tfwa => "The following settings will apply to the $gpterm:",
 1546:         difn => 'Different functionality<br />for different members:',
 1547:         stda => 'Start date',
 1548:         enda => 'End date:',
 1549:     );
 1550:     my $showstart = &Apache::lonlocal::locallocaltime($startdate);
 1551:     my $showend;
 1552:     if ($enddate == 0) {
 1553:         $showend = &mt('No end date set'); 
 1554:     } else {
 1555:         $showend = &Apache::lonlocal::locallocaltime($enddate);
 1556:     }
 1557:     if ($action eq 'create') {
 1558:         $r->print('
 1559: <div><span style="font-size: larger">'.$lt{'ygrs'}.'</span>
 1560: <br />'.$lt{'tfwa'}.'
 1561: </div>');
 1562:     }
 1563:     $r->print(&Apache::loncommon::start_data_table('LC_course_group_status').
 1564: 	      &Apache::loncommon::start_data_table_header_row());
 1565:     $r->print('
 1566:   <th>'.$lt{'grna'}.'</th>
 1567:   <th>'.$lt{'desc'}.'</th>
 1568:   <th>'.$lt{'grfn'}.'</th>
 1569:   <th>'.$lt{'gran'}.'</th>
 1570:   <th>'.$lt{'quot'}.'</th>
 1571:   <th>'.$lt{'dfac'}.'</th>
 1572: ');
 1573:     $r->print(&Apache::loncommon::end_data_table_header_row().
 1574: 	      &Apache::loncommon::start_data_table_row('LC_data_table_dense'));
 1575:     $r->print('
 1576:   <td valign="top">'.$groupname.'</td>
 1577:   <td valign="top">'.$description.'</td>
 1578:   <td>
 1579: ');
 1580:     if (@{$available} > 0) {
 1581:         $r->print('<b>Available:</b>
 1582:                     <table cellpadding="" cellspacing="1"><tr>');
 1583:         my $rowcell = int(@{$available}/2) + @{$available}%2;
 1584:         for (my $i=0; $i<@{$available}; $i++) {
 1585:             if (@{$available} > 3) {
 1586:                 if ($i==$rowcell) {
 1587:                     $r->print('</tr><tr>');
 1588:                 }
 1589:             }
 1590:             $r->print('<td>'.$$functions{$$available[$i]}.
 1591: 		      '</td><td>&nbsp;</td>');
 1592:         }
 1593:         if ((@{$available} > 3) && (@{$available}%2)) {
 1594:             $r->print('<td>&nbsp;</td><td>&nbsp;</td>');
 1595:         }
 1596:         $r->print('</tr></table><br />');
 1597:     }
 1598:     if (@{$unavailable} > 0) {
 1599:         $r->print('<b>Unavailable:</b>
 1600:                     <table cellpadding="0" cellspacing="1"  border="0"><tr>');
 1601:         my $rowcell = int(@{$unavailable}/2) + @{$unavailable}%2;
 1602:         for (my $j=0; $j<@{$unavailable}; $j++) {
 1603:             if (@{$unavailable} > 3) {
 1604:                 if ($j==$rowcell) {
 1605:                     $r->print('</tr><tr>');
 1606:                 }
 1607:             }
 1608:             $r->print('<td>'.$$functions{$$unavailable[$j]}.
 1609: 		      '</td><td>&nbsp;</td>');
 1610:         }
 1611:         if ((@{$unavailable} > 3) && (@{$unavailable}%2)) {
 1612:             $r->print('<td>&nbsp;</td><td>&nbsp;</td>');
 1613:         }
 1614:         $r->print('</tr></table>');
 1615:     }
 1616:     $r->print(<<"END");
 1617:   </td>
 1618:   <td valign="top"><b>$lt{'difn'}</b> $granularity</td>
 1619:   <td valign="top">$quota Mb</td> 
 1620:   <td valign="top"><b>$lt{'stda'}</b> $showstart<br />
 1621:       <b>$lt{'enda'}</b> $showend
 1622:   </td>
 1623: END
 1624:     $r->print(&Apache::loncommon::end_data_table_row().
 1625: 	      &Apache::loncommon::end_data_table());
 1626:     return;
 1627: }
 1628: 
 1629: sub pick_new_members {
 1630:     my ($r,$action,$formname,$available,$idx,$stored,$img,$users,$userdata,
 1631: 	$granularity,$origmembers,$gpterm,$ucgpterm) = @_;
 1632:     my %lt = &Apache::lonlocal::texthash(
 1633:           'gpme' => "$ucgpterm membership",
 1634:           'addm' => 'Add members',
 1635:           'setf' => 'Set functionality',
 1636:           'func' => 'Functionality',
 1637:           'nome' => 'No members to add at this time.',
 1638:           'nnew' => "There are no users to add as new members, as all users".
 1639:                     " matching the specified type(s), role(s), and/or ".
 1640:                     "section(s) are already affiliated with this $gpterm.",
 1641:           'yoma' =>  'You may need to use the '."'".'modify existing, past or '.
 1642:                      'future members'."'".' page if you need to re-enable '.
 1643:                      'or activate access for previous or future members.',
 1644:     );
 1645:     my %members;
 1646:     my $totalusers = 0;
 1647:     my $newusers = 0;
 1648:     foreach my $role (keys(%{$users})) {
 1649:         foreach my $user (keys(%{$$users{$role}})) {
 1650:             $totalusers ++;
 1651:             if (ref($origmembers) eq 'HASH') {
 1652:                 if (exists($$origmembers{$user})) {
 1653:                     next;
 1654:                 }
 1655:             }    
 1656:             unless (defined($members{$user})) {
 1657:                 @{$members{$user}} = @{$$userdata{$user}};
 1658:                 $newusers ++;
 1659:             }
 1660:         }
 1661:     }
 1662:     if (keys(%members) > 0) {
 1663:         if (@{$available} > 0 && $granularity eq 'Yes') {
 1664:             $r->print(&check_uncheck_tools($r,$available));
 1665:         }
 1666:     }
 1667:     &topic_bar($r,$img,$lt{'gpme'});
 1668:     if (keys(%members) > 0) {
 1669:         $r->print('
 1670:     <table>
 1671:      <tr>');
 1672:         &check_uncheck_buttons($r,$formname,'member',$lt{'addm'});
 1673:         if (@{$available} > 0 && $granularity eq 'Yes') {
 1674:             $r->print('<td><nobr>
 1675:      <fieldset><legend><b>'.$lt{'setf'}.'</b></legend>
 1676:       <input type="button" value="check all"
 1677:         onclick="javascript:checkAllTools(document.'.$formname.')" />
 1678:         &nbsp;&nbsp;
 1679:       <input type="button" value="uncheck all"
 1680:         onclick="javascript:uncheckAllTools(document.'.$formname.')" />
 1681:      </fieldset></nobr></td>');
 1682:         }
 1683:         $r->print('</tr></table>
 1684:         ');
 1685:         $r->print(&Apache::loncommon::start_data_table().
 1686: 		  &Apache::loncommon::start_data_table_header_row());
 1687:         $r->print('
 1688:      <th>'.&mt('Add?').'</b></td>
 1689:      <th><a href="javascript:changeSort('."'fullname'".')">'.&mt('Name').'</a></td>
 1690:      <th><a href="javascript:changeSort('."'username'".')">'.&mt('Username').'</a></td>
 1691:      <th><a href="javascript:changeSort('."'domain'".')">'.&mt('Domain').'</a></td>
 1692:      <th><a href="javascript:changeSort('."'id'".')">'.&mt('ID').'</a></td>
 1693:      <th><a href="javascript:changeSort('."'section'".')">'.&mt('Section').'</a></td>
 1694: ');
 1695:         if (@{$available} > 0) {
 1696:             $r->print('<th>'.$lt{'func'}.'</th>');
 1697:         }
 1698:         $r->print(&Apache::loncommon::end_data_table_header_row());
 1699:         if (@{$available} > 0) {
 1700:             if ($granularity eq 'Yes') {
 1701:                 $r->print(&Apache::loncommon::start_data_table_row('LC_data_table_dense LC_data_table_highlight').'
 1702:  <td colspan="6">&nbsp;</td>
 1703:  <td align="center"><nobr><b>'.&mt('All:').'</b>&nbsp;');
 1704:                 foreach my $tool (@{$available}) {
 1705:                     $r->print('<label><input type="checkbox" name="togglefunc" '.
 1706:    'onclick="javascript:toggleTools(document.'.$formname.'.user_'.$tool.',this);"'.
 1707:    ' value="'.$tool.'">'.'<b>'.$tool.'</b></label>&nbsp;&nbsp;&nbsp;');
 1708:                 }
 1709:                 $r->print('</nobr></td></tr>');
 1710:             }
 1711:         }
 1712:         my %Sortby = ();
 1713:         foreach my $user (sort(keys(%members))) {
 1714:             if ($env{'form.sortby'} eq 'fullname') {
 1715:                 push(@{$Sortby{$members{$user}[$$idx{fullname}]}},$user);
 1716:             } elsif ($env{'form.sortby'} eq 'username') {
 1717:                 push(@{$Sortby{$members{$user}[$$idx{uname}]}},$user);
 1718:             } elsif ($env{'form.sortby'} eq 'domain') {
 1719:                 push(@{$Sortby{$members{$user}[$$idx{udom}]}},$user);
 1720:             } elsif ($env{'form.sortby'} eq 'id') {
 1721:                 push(@{$Sortby{$members{$user}[$$idx{id}]}},$user);
 1722:             } elsif ($env{'form.sortby'} eq 'section') {
 1723:                 push(@{$Sortby{$members{$user}[$$idx{section}]}},$user);
 1724:             } else {
 1725:                 push(@{$Sortby{$members{$user}[$$idx{fullname}]}},$user);
 1726:             }
 1727:         }
 1728:         foreach my $key (sort(keys(%Sortby))) {
 1729:             foreach my $user (@{$Sortby{$key}}) {
 1730:                 my $id = $members{$user}[$$idx{id}];
 1731:                 my $fullname = $members{$user}[$$idx{fullname}];
 1732:                 my $udom = $members{$user}[$$idx{udom}];
 1733:                 my $uname = $members{$user}[$$idx{uname}];
 1734:                 my $section = $members{$user}[$$idx{section}];
 1735:                 $r->print(&Apache::loncommon::start_data_table_row('LC_data_table_dense').
 1736: 			  '<td align="right"><input type="checkbox" name="member" value="'.$user.'" /></td>'.
 1737: 			  '<td>'.$fullname.'</td>'.
 1738: 			  '<td>'.$uname.'</td>'.
 1739: 			  '<td>'.$udom.'</td>'.
 1740: 			  '<td>'.$id.'</td>'.
 1741: 			  '<td>'.$section.'</td>');
 1742:                 if (@{$available} > 0) {
 1743:                     $r->print('<td align="center"><nobr>'.
 1744:                               '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
 1745:                     foreach my $tool (@{$available}) {
 1746:                         if ($granularity eq 'Yes') {
 1747:                             $r->print('<input type="checkbox" name="user_'.
 1748:                           $tool.'" value="'.$user.'" />'.$tool.'&nbsp;&nbsp;&nbsp;');
 1749:                         } else {
 1750:                             $r->print('<input type="hidden" name="user_'.
 1751:                           $tool.'" value="'.$user.'" />'.$tool.'&nbsp;&nbsp;&nbsp;');
 1752:                         }
 1753:                     }
 1754:                     $r->print('</nobr></td>');
 1755:                 }
 1756:                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
 1757:             }
 1758:         }
 1759:         $r->print(&Apache::loncommon::end_data_table());
 1760:     } else {
 1761:         if ($totalusers > 0) {
 1762:             $r->print($lt{'nnew'}.'<br /><br />'.$lt{'yoma'});
 1763:         } else { 
 1764:             $r->print($lt{'nome'});
 1765:         }
 1766:     }
 1767:     return $newusers;
 1768: }
 1769: 
 1770: sub privilege_specificity {
 1771:     my ($r,$action,$img,$tools,$stored,$toolprivs,$fixedprivs,$available,
 1772: 	$formname,$gpterm,$ucgpterm) = @_;
 1773:     my %lt = &Apache::lonlocal::texthash (
 1774:       'uprv' => 'User privileges',
 1775:       'frty' => 'For each type of functionality you have chosen to include, '.
 1776:                 'there is a set of standard privileges which apply to all '.
 1777:                 'of those for whom the functionality is enabled.',
 1778:       'thar' => 'There are also additional privileges which can be set for '.
 1779:                 'some, or all, members. Please choose one of the following:',
 1780:       'fort' => 'For the types of functionality you have chosen to include '.
 1781:                 'there are no additional privileges which can be set for some '.
 1782:                 'or all members.',
 1783:       'eaty' => 'Each of the types of functionality includes standard '.
 1784:                 'privileges which apply to members with access to that '.
 1785:                 'functionality, and may also include additional privileges '.
 1786:                 'which can be set for specific members.',
 1787:       'cutg' => "Currently the $gpterm is configured ",
 1788:       'sdif' => "so different $gpterm members can receive different privileges.",
 1789:       'sall' => "so all $gpterm members will receive the same privileges.",
 1790:       'algm' => "All $gpterm members will receive the same privileges.",
 1791:       'smgp' => "Some $gpterm members will receive different privileges from ".
 1792:                 "others.",
 1793:       'thwi' => "These will be the privileges all $gpterm members receive, ". 
 1794:                 "if you selected the first option above.",
 1795:       'thes' => "These will be the privileges given to members assigned ".   
 1796:                 "in the future, including via automatic $gpterm assignment ".
 1797:                 "for specific sections/roles ",
 1798:       'asyo' => "As you have chosen not to include any functionality in the ".
 1799:                 "$gpterm, no default user privileges settings need to be set.",
 1800:       'plin' => 'Please indicate which <b>optional</b> privileges members '.
 1801:                 'will receive by default.',
 1802:       'oppr' => 'Optional privileges',
 1803:       'defp' => 'The default privileges new members will receive are:', 
 1804:     );
 1805:     my $totaloptionalprivs = 0;
 1806:     foreach my $tool (@{$tools}) {
 1807:         foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 1808:             if (!exists($$fixedprivs{$tool}{$priv})) {
 1809:                 $totaloptionalprivs ++;
 1810:             }
 1811:         }
 1812:     }
 1813:     &topic_bar($r,$img,$lt{'uprv'});
 1814:     if ((($action eq 'create') && (@{$available} > 0)) || 
 1815:         (($action eq 'modify') && ($formname eq 'change_settings'))) {  
 1816:         my %specific = (
 1817:                       'No'  => 'checked="checked"',
 1818:                       'Yes' => '',
 1819:                   );
 1820:         if ($action eq 'create') {
 1821:             $r->print($lt{'frty'}.'<br />');
 1822:             if ($totaloptionalprivs) {
 1823:                 $r->print($lt{'thar'});
 1824:             } else {
 1825:                 $r->print($lt{'fort'});
 1826:             }
 1827:         } else {
 1828:             $r->print($lt{'eaty'}.'&nbsp;'.$lt{cutg});
 1829:             if ($$stored{'specificity'} eq 'Yes') {
 1830:                 $r->print($lt{'sdif'});
 1831:                 $specific{'Yes'} = $specific{'No'};
 1832:                 $specific{'No'} = '';
 1833:             } else {
 1834:                 $r->print($lt{'sall'});
 1835:             }
 1836:         }
 1837:         if ($totaloptionalprivs) {
 1838:             $r->print('
 1839: <br /><br />
 1840: <label><nobr><input type="radio" name="specificity" value="No" '.$specific{'No'}.' />&nbsp;'.$lt{'algm'}.'</nobr></label><br />
 1841: <label><nobr><input type="radio" name="specificity" value="Yes" '.$specific{'Yes'}.' />&nbsp;'.$lt{'smgp'}.'</nobr></label>');
 1842:         } else {
 1843:             $r->print('<input type="hidden" name="specificity" value="No" />');
 1844:         }
 1845:         if ($totaloptionalprivs) {
 1846:             $r->print($lt{'plin'});
 1847:             if ($action eq 'create') {
 1848:                 $r->print(' '.$lt{'thwi'});
 1849:             }
 1850:             $r->print('<br />'.$lt{'thes'});
 1851:             if ($action eq 'create') {
 1852:                 $r->print('('.&mt('if enabled on the next page').').');
 1853:             } else {
 1854:                 $r->print('('.&mt('if enabled below').').');
 1855:             }
 1856:             $r->print('<br /><br />
 1857:   <table><tr>');
 1858:         &check_uncheck_buttons($r,$formname,'defpriv',$lt{'oppr'});
 1859:         $r->print('
 1860:     </tr>
 1861:    </table>
 1862:    <br />
 1863: ');
 1864:         } else {
 1865:             $r->print($lt{'algm'}.'<br /><br />');
 1866:         }
 1867:         &default_privileges($r,$action,$tools,$toolprivs,$fixedprivs,
 1868: 			    $available);
 1869:     } else {
 1870:         if ($action eq 'create') {
 1871:             $r->print($lt{'asyo'});
 1872:         } elsif ($action eq 'modify' && $formname eq 'pick_members') {
 1873:             my @defprivs;
 1874:             if (ref($$stored{'defpriv'}) eq 'ARRAY') {
 1875:                 @defprivs = @{$$stored{'defpriv'}};
 1876:             }
 1877:             $r->print($lt{'eaty'}.'&nbsp;'.$lt{cutg});
 1878:             if ($$stored{'specificity'} eq 'Yes') {
 1879:                 $r->print($lt{'sdif'});
 1880:             } else {
 1881:                 $r->print($lt{'sall'});
 1882:             }
 1883:             $r->print(' '.$lt{'defp'}.'<br /><br />');
 1884:             &display_defprivs($r,$tools,$toolprivs,\@defprivs);
 1885:         }
 1886:     }
 1887:     return;
 1888: }
 1889: 
 1890: sub default_privileges {
 1891:     my ($r,$action,$tools,$toolprivs,$fixedprivs,$available) = @_;
 1892:     my %lt = &Apache::lonlocal::texthash(
 1893:                                 'addp' => 'Additional privileges',
 1894:                                 'fixp' => 'Fixed privileges',
 1895:                                 'oppr' => 'Optional privileges',
 1896:                                 'func' => 'Function',
 1897:     );
 1898:     $r->print(&Apache::lonhtmlcommon::start_pick_box('LC_group_priv_box').
 1899: 	      &Apache::lonhtmlcommon::row_title($lt{'func'},undef,
 1900: 						'LC_groups_functionality'));
 1901:     $r->print(join('</td><td class="LC_groups_functionality">',@{$tools}));
 1902:     $r->print(&Apache::lonhtmlcommon::row_closure(1));
 1903:     my $fixed = '';
 1904:     my $dynamic = '';
 1905:     foreach my $tool (@{$tools}) {
 1906:         my $privcount = 0;
 1907: 	if ($dynamic ne '') {
 1908: 	    $dynamic .= '</td><td class="LC_groups_optional">';
 1909: 	}
 1910: 	$dynamic .= '<table class="LC_group_priv"><tr>';
 1911:         foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 1912:             if (exists($$fixedprivs{$tool}{$priv})) {
 1913: 		if ($fixed ne '') {
 1914: 		    $fixed .= '</td><td class="LC_groups_fixed">';
 1915: 		}
 1916:                 $fixed .= '<input type="hidden" name="defpriv" value="'.$priv.'" />'.$$toolprivs{$tool}{$priv}.'&nbsp;';
 1917:                 if ($action eq 'modify') {
 1918:                     if (grep(/^$tool$/,@{$available})) {
 1919:                         $fixed .= '<small>'.&mt('(on)').'<small>&nbsp;';
 1920:                     } else {
 1921:                         $fixed .= '<small>'.&mt('(off)').'<small>&nbsp;';
 1922:                     }
 1923:                 }
 1924:             } else {
 1925:                 $privcount++;
 1926:                 if ($privcount == 3) {
 1927:                     $dynamic .= '</tr>
 1928:                                  <tr>'."\n";
 1929:                 }
 1930:                 $dynamic .= '<td><label><input type="checkbox" name="defpriv" value="'.$priv.'" />'.$$toolprivs{$tool}{$priv}.'</label></td>'."\n";
 1931:             }
 1932:         }
 1933:         if ($privcount == 0) {
 1934:             $dynamic .= '<td>None</td>'."\n";
 1935:         }
 1936:         if ($privcount < 3) {
 1937:             $dynamic .= '</td>
 1938:                          <td>&nbsp;</td>'."\n";
 1939:         } elsif ($privcount%2) {
 1940:             $dynamic = '<td>&nbsp;</td>'."\n";
 1941:         }
 1942: 	$dynamic .= '</tr></table>';
 1943:     }
 1944:     $r->print(&Apache::lonhtmlcommon::row_title($lt{'fixp'},undef,
 1945: 						'LC_groups_fixed').
 1946: 	      $fixed.
 1947: 	      &Apache::lonhtmlcommon::row_closure(1));
 1948:     $r->print(&Apache::lonhtmlcommon::row_title($lt{'oppr'},undef,
 1949: 						'LC_groups_optional').
 1950: 	      $dynamic.
 1951: 	      &Apache::lonhtmlcommon::end_pick_box());
 1952:     $r->print('<br />');
 1953:     return;
 1954: 
 1955: }
 1956: 
 1957: sub display_defprivs {
 1958:     my ($r,$tools,$toolprivs,$defprivs) = @_;
 1959:     my $function = &Apache::loncommon::get_users_function();
 1960:     my $tabcol = &Apache::loncommon::designparm($function.'.tabbg');
 1961:     my $rowColor1 = "#dddddd";
 1962:     my $rowColor2 = "#eeeeee";
 1963:     my %lt = &Apache::lonlocal::texthash(
 1964:                                 'priv' => 'Privileges',
 1965:                                 'func' => 'Function',
 1966:     );
 1967:     $r->print(&Apache::lonhtmlcommon::start_pick_box());
 1968:     $r->print('<tr>');
 1969:     my $numrows = 0;
 1970:     my %currprivs;
 1971:     foreach my $tool (@{$tools}) {
 1972:         @{$currprivs{$tool}} = ();
 1973:         foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 1974:             if (ref($defprivs) eq 'ARRAY') {
 1975:                 if (grep(/^\Q$priv\E$/,@{$defprivs})) {
 1976:                     push(@{$currprivs{$tool}},$priv);
 1977:                 }
 1978:             }
 1979:         }
 1980:         my $rowcount = int(@{$currprivs{$tool}}/3);
 1981:         if (@{$currprivs{$tool}}%3 > 0) {
 1982:             $rowcount ++;
 1983:         }
 1984:         if ($rowcount > $numrows) {
 1985:             $numrows = $rowcount;
 1986:         }
 1987:     }
 1988:     my @rowCols = ($rowColor1,$rowColor2);
 1989:     foreach my $tool (@{$tools}) {
 1990:         $r->print('<td align="center" valign="top">
 1991:                     <table cellspacing="0" cellpadding="5">
 1992:                      <tr bgcolor="#cccccc">
 1993:                       <td colspan="3" align="center"><b>'.$tool.'</b></td>
 1994:                      </tr>
 1995:         ');
 1996:         my $rownum = 1;
 1997:         my $privcount = 0;
 1998:         $r->print('<tr bgcolor="'.$rowColor1.'">');
 1999:         foreach my $priv (@{$currprivs{$tool}}) {
 2000:             $privcount ++;
 2001:             if ($privcount%4 == 0) {
 2002:                 $rownum ++;
 2003:                 my $bgcol = $rownum%2; 
 2004:                 $r->print('</tr>
 2005:                              <tr bgcolor="'.$rowCols[$bgcol].'">'."\n");
 2006:             }
 2007:             $r->print('<td>'.$$toolprivs{$tool}{$priv}.'</td>'."\n");
 2008:         }
 2009:         if ($privcount%3 > 0) {
 2010:             my $emptycells = 3-($privcount%3);
 2011:             while($emptycells > 0) {
 2012:                 $r->print('<td>&nbsp;</td>'."\n");
 2013:                 $emptycells --;
 2014:             }
 2015:         }
 2016:         while ($rownum < $numrows) {
 2017:             $rownum ++;
 2018:             my $bgcol = $rownum%2;
 2019:             $r->print('<tr bgcolor="'.$rowCols[$bgcol].'"><td colspan="3">&nbsp;</td></tr>');
 2020:         }
 2021:         $r->print('</table>'."\n".'</td>');
 2022:     }
 2023:     $r->print('</tr>'."\n");
 2024:     $r->print(&Apache::lonhtmlcommon::end_pick_box());
 2025:     $r->print('<br />');
 2026:     return;
 2027: }
 2028: 
 2029: 
 2030: sub change_members_form {
 2031:     my ($r,$cdom,$cnum,$tabcol,$action,$formname,$page,$groupname,$description,
 2032:         $startdate,$enddate,$tools,$fixedprivs,$functions,$users,$userdata,
 2033:         $granularity,$quota,$specificity,$idx,$states,$navbuttons,$rowColor1,
 2034:         $rowColor2,$gpterm,$ucgpterm) = @_;
 2035:     my %lt = &Apache::lonlocal::texthash(
 2036:                                          grse => "$ucgpterm settings",
 2037:                                          mogm => "Modify $gpterm membership",
 2038:                                         );
 2039:     my @regexps = ('user_','userpriv_');
 2040:     $r->print(&Apache::lonhtmlcommon::echo_form_input(
 2041:                          ['origin','action','state','page','expire','deletion',
 2042:                           'reenable','activate','changepriv','sortby',
 2043:                           'togglefunc'],\@regexps));
 2044:     my $rowimg = 1;
 2045:     my @available = ();
 2046:     my @unavailable = ();
 2047:     &check_tools($functions,$tools,\@available,\@unavailable);
 2048:     my $nexttext = $$navbuttons{'gtns'};
 2049:     my $prevtext = $$navbuttons{'gtpp'};
 2050:     $r->print('
 2051: <br />
 2052: ');
 2053:     &topic_bar($r,1,$lt{'grse'});
 2054:     &print_current_settings($r,$action,$functions,$startdate,$enddate,
 2055: 			    $groupname,$description,$granularity,$quota,
 2056: 			    \@available,\@unavailable,$gpterm,$ucgpterm);
 2057:     &topic_bar($r,2,$lt{'mogm'});
 2058:     &current_membership($r,$cdom,$cnum,$formname,$tabcol,$rowColor1,
 2059:                         $rowColor2,$groupname,\@available,\@unavailable,
 2060:                         $fixedprivs,$granularity,$specificity);
 2061:     &display_navbuttons($r,$formname,$$states{$action}[$page-1],$prevtext,
 2062:                         $$states{$action}[$page+1],$nexttext);
 2063:     return;
 2064: }
 2065: 
 2066: sub current_membership {
 2067:     my ($r,$cdom,$cnum,$formname,$tabcol,$rowColor1,$rowColor2,$groupname,
 2068:         $available,$unavailable,$fixedprivs,$granularity,$specificity) = @_;
 2069:     my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
 2070:                                                                    $groupname);
 2071:     my %lt = &Apache::lonlocal::texthash(
 2072:                                           'actn' => 'Action?',
 2073:                                           'name' => 'Name',
 2074:                                           'usnm' => 'Username',
 2075:                                           'doma' => 'Domain',
 2076:                                           'stda' => 'Start Date',
 2077:                                           'enda' => 'End Date',
 2078:                                           'expi' => 'Expire',
 2079:                                           'reen' => 'Re-enable',
 2080:                                           'acti' => 'Activate',
 2081:                                           'dele' => 'Delete',
 2082:                                           'curf' => 'Current Functionality',
 2083:                                           'chpr' => 'Change Privileges' 
 2084:                                         );
 2085:     my ($current,$hastools,$addtools,$num_reenable,$num_activate,$num_expire) =
 2086:         &Apache::longroup::group_memberlist($cdom,$cnum,$groupname,$fixedprivs,
 2087:                                             $available);
 2088:     if (keys(%{$current}) > 0) {
 2089:         $r->print('
 2090:  <tr>
 2091:   <td>&nbsp;</td>
 2092:   <td colspan="2">
 2093:    <table>
 2094:     <tr>');
 2095:         if ($num_expire) {
 2096:             &check_uncheck_buttons($r,$formname,'expire',$lt{'expi'});
 2097:         }
 2098:         if ($num_reenable) {
 2099:             &check_uncheck_buttons($r,$formname,'reenable',$lt{'reen'});
 2100:         }
 2101:         if ($num_activate) {
 2102:             &check_uncheck_buttons($r,$formname,'activate',$lt{'acti'});
 2103:         }
 2104:         &check_uncheck_buttons($r,$formname,'deletion',$lt{'dele'});
 2105:         if (@{$available} > 0) {
 2106:             if ($specificity eq 'Yes') {
 2107:                 &check_uncheck_buttons($r,$formname,'changepriv',$lt{'chpr'});
 2108:             }
 2109:             if ($granularity eq 'Yes') {
 2110:                 $r->print(&check_uncheck_tools($r,$available));
 2111:                 $r->print('
 2112:      <td>
 2113:       <nobr>
 2114:        <fieldset><legend><b>'.$lt{'curf'}.'</b></legend>
 2115:        <input type="button" value="check all"
 2116:        onclick="javascript:checkAllTools(document.'.$formname.')" />
 2117:        &nbsp;&nbsp;
 2118:        <input type="button" value="uncheck all"
 2119:         onclick="javascript:uncheckAllTools(document.'.$formname.')" />
 2120:       </fieldset>
 2121:      </nobr>
 2122:     </td>
 2123: ');
 2124:             }
 2125:         }
 2126:         $r->print(<<"END");
 2127:    </tr>
 2128:   </table>
 2129:   </td>
 2130:   <td width="100%">&nbsp;</td>
 2131:  </tr>
 2132:  <tr>
 2133:   <td colspan="4">&nbsp;</td>
 2134:  </tr>
 2135:  <tr>
 2136:   <td>&nbsp;</td>
 2137:   <td colspan="3">
 2138: END
 2139:         $r->print(&Apache::lonhtmlcommon::start_pick_box());
 2140:         $r->print(<<"END");
 2141:    <table border="0" cellpadding="4" cellspacing="1">
 2142:     <tr bgcolor="$tabcol" align="center">
 2143:      <td><b>$lt{'actn'}</b></td>
 2144:      <td><b><a href="javascript:changeSort('fullname')">$lt{'name'}</a></b></td>
 2145:      <td><b><a href="javascript:changeSort('username')">$lt{'usnm'}</a></b>
 2146:      </td>
 2147:      <td><b><a href="javascript:changeSort('domain')">$lt{'doma'}</a></b></td>
 2148:      <td><b><a href="javascript:changeSort('id')">ID</a></b></td>
 2149:      <td><b><a href="javascript:changeSort('start')">$lt{'stda'}</a></b></td>
 2150:      <td><b><a href="javascript:changeSort('end')">$lt{'enda'}</a></b></td>
 2151: END
 2152:         my $colspan = 0;
 2153:         if ($hastools) {
 2154:             $r->print('<td><b>'.$lt{'curf'}.'</b></td>');
 2155:             $colspan ++;  
 2156:         }
 2157:         if ($addtools) {
 2158:             $r->print('<td><b>Additional Functionality</b></td>');
 2159:             $colspan ++;
 2160:         }
 2161:         $r->print('</tr>');
 2162:         if ($colspan) {
 2163:             if ($granularity eq 'Yes') {
 2164:                 $r->print('<tr bgcolor="#cccccc">
 2165:  <td colspan="7">&nbsp;</td>
 2166:  <td colspan="'.$colspan.'" align="center"><small><nobr><b>'.&mt('All:').
 2167:   '</b>&nbsp;');
 2168:                 foreach my $tool (@{$available}) {
 2169:                     $r->print('<label><input type="checkbox" name="togglefunc"'.
 2170:    ' onclick="javascript:toggleTools(document.'.$formname.'.user_'.$tool.',this);"'.
 2171:    ' value="'.$tool.'">'.'<b>'.$tool.'</b></label>&nbsp;&nbsp;&nbsp;');
 2172:                 }
 2173:                 $r->print('</nobr></small></td></tr>');
 2174:             }
 2175:         }
 2176:         my %Sortby = ();
 2177:         foreach my $user (sort(keys(%{$current}))) {
 2178:             if ($env{'form.sortby'} eq 'fullname') {
 2179:                 push(@{$Sortby{$$current{$user}{fullname}}},$user);
 2180:             } elsif ($env{'form.sortby'} eq 'username') {
 2181:                 push(@{$Sortby{$$current{$user}{uname}}},$user);
 2182:             } elsif ($env{'form.sortby'} eq 'domain') {
 2183:                 push(@{$Sortby{$$current{$user}{udom}}},$user);
 2184:             } elsif ($env{'form.sortby'} eq 'id') {
 2185:                 push(@{$Sortby{$$current{$user}{id}}},$user);
 2186:             } else {
 2187:                 push(@{$Sortby{$$current{$user}{fullname}}},$user);
 2188:             }
 2189:         }
 2190:         my $rowNum = 0;
 2191:         my $rowColor;
 2192:         foreach my $key (sort(keys(%Sortby))) {
 2193:             foreach my $user (@{$Sortby{$key}}) {
 2194:                 if ($rowNum %2 == 1) {
 2195:                     $rowColor = $rowColor1;
 2196:                 } else {
 2197:                     $rowColor = $rowColor2;
 2198:                 }
 2199:                 my $id = $$current{$user}{id};
 2200:                 my $fullname = $$current{$user}{fullname};
 2201:                 my $udom = $$current{$user}{udom};
 2202:                 my $uname = $$current{$user}{uname};
 2203:                 my $start = $$current{$user}{start};
 2204:                 my $end = $$current{$user}{end};
 2205:                 $r->print('<tr bgcolor="'.$rowColor.'">
 2206:                             <td><small>');
 2207:                 if ($$current{$user}{changestate} eq 'reenable') {
 2208:                     $r->print('<nobr><label>'. 
 2209:    '<input type="checkbox" name="reenable" value="'.$user.'" />'.
 2210:    $lt{'reen'}.'</label></nobr><br />');
 2211:                 } elsif ($$current{$user}{changestate} eq 'expire') {
 2212:                     $r->print('<nobr><label>'.
 2213:    '<input type="checkbox" name="expire" value="'.$user.'" />'.
 2214:    $lt{'expi'}.'</label></nobr><br />');
 2215:                 } elsif ($$current{$user}{changestate} eq 'activate') {
 2216:                     $r->print('<nobr><label>'.
 2217:    '<input type="checkbox" name="activate" value="'.$user.'" />'.
 2218:    $lt{'acti'}.'</label></nobr><br />');
 2219:                 }
 2220:                 $r->print('<nobr><label>'.
 2221:    '<input type="checkbox" name="deletion" value="'.$user.'" />'.
 2222:    $lt{'dele'}.'</label></nobr>');
 2223:                 if ($specificity eq 'Yes') {
 2224:                     $r->print('<br /><nobr><label>'.
 2225:    '<input type="checkbox" name="changepriv" value="'.$user.'" />'.$lt{'chpr'}.
 2226:    '</label></nobr>');
 2227:                 }
 2228:                 $r->print('
 2229:    </td>
 2230:    <td><small>'.
 2231:     $fullname.'</small></td><td><small>'.$uname.'</small></td><td><small>'.
 2232:     $udom.'</small></td><td><small>'.$id.'</small></td><td><small>'.$start.
 2233:     '</small></td><td><small>'.$end.'</small></td>');
 2234:                 if ($hastools) {
 2235:                     $r->print('<td align="left"><small><nobr>'.
 2236:                                   '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
 2237:                     foreach my $tool (@{$$current{$user}{currtools}}) {
 2238:                         if ($granularity eq 'Yes') {
 2239:                             $r->print('<label><input type="checkbox" '. 
 2240:                                        'checked="checked" '. 
 2241:                                        'name="user_'.$tool.'" value="'.
 2242:                                        $user.'" />'.$tool.'</label>');
 2243:                          } else {
 2244:                              $r->print('<input type="hidden" '.
 2245:                                        'checked="checked" '.
 2246:                                        'name="user_'.$tool.'" value="'.
 2247:                                        $user.'" />'.$tool);
 2248:                          }
 2249:                          $r->print('&nbsp;&nbsp;&nbsp;');
 2250:                     }
 2251:                     $r->print('</nobr></small></td>');
 2252:                 }
 2253:                 if ($addtools) {
 2254:                     $r->print('<td align="left"><small>');
 2255:                     if ($granularity eq 'Yes') {
 2256:                         foreach my $tool (@{$$current{$user}{newtools}}) {
 2257:                             $r->print('<nobr><label><input type="checkbox"
 2258:                                           name="user_'.$tool.'" value="'.
 2259:                                           $user.'" />'.$tool.
 2260:                                           '</label></nobr>&nbsp;&nbsp;&nbsp;');
 2261:                         }
 2262:                     } else {
 2263:                         foreach my $tool (@{$$current{$user}{newtools}}) {
 2264:                             $r->print('<nobr><input type="hidden" 
 2265:                                           name="user_'. $tool.'" value="'.
 2266:                                           $user.'" />'.$tool.
 2267:                                           '</nobr>&nbsp;&nbsp;&nbsp;');
 2268:                         }
 2269:                     }
 2270:                     $r->print('</small></td>');
 2271:                 }
 2272:                 $r->print('</tr>'."\n");
 2273:                 $rowNum ++;
 2274:             }
 2275:         }
 2276:         $r->print(&Apache::lonhtmlcommon::end_pick_box());
 2277:         $r->print('
 2278:   </td>
 2279:  </tr>');
 2280:     }
 2281:     return;
 2282: }
 2283: 
 2284: sub check_uncheck_buttons {
 2285:     my ($r,$formname,$field,$title,$colspan) = @_;
 2286:     $r->print('
 2287:      <td '.$colspan.'>
 2288:       <nobr>
 2289:        <fieldset>
 2290:        <legend><b>'.$title.'</b></legend>
 2291:        <input type="button" value="check all"
 2292:        onclick="javascript:checkAll(document.'.$formname.'.'.$field.')" />
 2293:        &nbsp;&nbsp;
 2294:        <input type="button" value="uncheck all"
 2295:        onclick="javascript:uncheckAll(document.'.$formname.'.'.$field.')" />
 2296:        </fieldset>
 2297:       </nobr>
 2298:      </td>
 2299: ');
 2300: }
 2301: 
 2302: 
 2303: sub change_privs_form {
 2304:     my ($r,$cdom,$cnum,$action,$formname,$page,$startdate,$enddate,
 2305: 	$tools,$functions,$toolprivs,$fixedprivs,$userdata,$usertools,
 2306: 	$memchg,$idx,$states,$stored,$sectioncount,$navbuttons,$gpterm,
 2307: 	$ucgpterm) = @_;
 2308:     my @regexps = ('userpriv_');
 2309:     my $nexttext;
 2310:     my %lt = &Apache::lonlocal::texthash(
 2311:                'tode' => 'To be deleted',
 2312:                'toex' => 'To be expired',
 2313:                'nome' => "No members to be deleted or expired from the $gpterm.",
 2314:     );
 2315:     $r->print(&Apache::lonhtmlcommon::echo_form_input(
 2316:          ['origin','action','state','page','sortby'],\@regexps));
 2317:     if ($env{'form.branch'} eq 'adds') {
 2318:         $nexttext = $$navbuttons{'adme'};
 2319:     } else {
 2320:         $nexttext = $$navbuttons{'mose'};
 2321:     }
 2322:     &topic_bar($r,3,&mt('Members to delete or expire'));
 2323:     my $exp_or_del = 0;
 2324:     if (ref($$memchg{'deletion'}) eq 'ARRAY') {
 2325:         if (@{$$memchg{'deletion'}} > 0) {
 2326:             $r->print('<tr><td>&nbsp;</td><td colspan="3"><b>'.$lt{'tode'}.':</b><br /><ul>');
 2327:             foreach my $user (@{$$memchg{'deletion'}}) {
 2328:                 $r->print('<li>'.$$userdata{$user}[$$idx{fullname}].
 2329:                           '&nbsp;('.$user.')</li>');
 2330:             }
 2331:             $r->print('</ul>');
 2332:             $exp_or_del += @{$$memchg{'deletion'}};
 2333:         }
 2334:     }
 2335:     if (ref($$memchg{'expire'}) eq 'ARRAY') {
 2336:         if (@{$$memchg{'expire'}} > 0) {
 2337:             $r->print('<tr><td>&nbsp;</td><td colspan="3"><b>'.$lt{'toex'}.':</b><br /><ul>');
 2338:             foreach my $user (@{$$memchg{'expire'}}) {
 2339:                 $r->print('<li>'.$$userdata{$user}[$$idx{fullname}].
 2340:                           '&nbsp;('.$user.')</li>');
 2341:             }
 2342:             $r->print('</ul>');
 2343:             $exp_or_del += @{$$memchg{'expire'}};
 2344:         }
 2345:     }
 2346:     if (!$exp_or_del) {
 2347:         $r->print($lt{'nome'}.'<br />');
 2348:     }
 2349:     
 2350:     &topic_bar($r,4,&mt('[_1] member privileges',$ucgpterm));
 2351: 
 2352:     my $numchgs = &member_privileges_form($r,$action,$formname,$tools,
 2353:                                           $toolprivs,$fixedprivs,$userdata,
 2354:                                           $usertools,$idx,$memchg,$states,
 2355:                                           $stored,$gpterm);
 2356:     my $prevtext = $$navbuttons{'gtps'};
 2357:     if ($numchgs || $exp_or_del) {
 2358:         &display_navbuttons($r,$formname,$$states{$action}[$page-1],$prevtext,
 2359:                             $$states{$action}[$page+1],$nexttext);
 2360:     } else {
 2361:         &display_navbuttons($r,$formname,$$states{$action}[$page-1],$prevtext);
 2362:     }
 2363:     return;
 2364: }
 2365: 
 2366: sub add_members_form {
 2367:     my ($r,$action,$formname,$page,$startdate,$enddate,$groupname,
 2368:         $description,$granularity,$quota,$sectioncount,$tools,$functions,
 2369:         $stored,$states,$navbuttons,$gpterm,$ucgpterm)=@_; 
 2370:     $r->print(' <br />');
 2371:     my @available = ();
 2372:     my @unavailable = ();
 2373:     &check_tools($functions,$tools,\@available,\@unavailable);
 2374:     &print_current_settings($r,$action,$functions,$startdate,$enddate,
 2375: 			    $groupname,$description,$granularity,$quota,
 2376: 			    \@available,\@unavailable,$gpterm,$ucgpterm);
 2377:     &membership_options($r,$action,$formname,$sectioncount,1,$gpterm,$ucgpterm);
 2378:     my $nexttext = $$navbuttons{'gtns'};
 2379:     my $prevtext = $$navbuttons{'gtpp'};
 2380:     &display_navbuttons($r,$formname,$$states{$action}[$page-1],$prevtext,
 2381:                         $$states{$action}[$page+1],$nexttext);
 2382:     return;
 2383: }
 2384: 
 2385: sub choose_privs_form {
 2386:     my ($r,$cdom,$cnum,$action,$formname,$page,$startdate,$enddate,
 2387: 	$tools,$functions,$toolprivs,$fixedprivs,$userdata,$usertools,$idx,
 2388: 	$states,$stored,$sectioncount,$navbuttons,$gpterm,$ucgpterm,
 2389: 	$crstype) = @_;
 2390: 
 2391:     my @regexps = ('userpriv_');
 2392:     my $nexttext;
 2393:     
 2394:     if ($action eq 'create') {
 2395:         push(@regexps,'sec_');
 2396:         $r->print(&Apache::lonhtmlcommon::echo_form_input(
 2397:          ['origin','action','state','page','sortby','autoadd','autodrop'],
 2398:          \@regexps));
 2399:         $nexttext = $$navbuttons{'crgr'};
 2400:     } else {
 2401:         $r->print(&Apache::lonhtmlcommon::echo_form_input(
 2402:          ['origin','action','state','page','sortby'],\@regexps));
 2403:         $nexttext = $$navbuttons{'adme'};
 2404:     }
 2405: 
 2406:     &topic_bar($r,6,&mt('[_1] member privileges',$ucgpterm));
 2407: 
 2408:     &member_privileges_form($r,$action,$formname,$tools,$toolprivs,
 2409:                             $fixedprivs,$userdata,$usertools,$idx,undef,
 2410:                             $states,$stored,$gpterm);
 2411: 
 2412:     if ($action eq 'create') {
 2413:         if (keys(%{$sectioncount}) > 0) {
 2414:             my $img1 = 7;
 2415:             my $img2 = 8;
 2416:             &mapping_options($r,$action,$formname,$page,$sectioncount,
 2417:                              $states,$stored,$navbuttons,$img1,$img2,
 2418:                              $gpterm,$ucgpterm,$crstype);
 2419:         }
 2420:     }
 2421:     my $prevtext = $$navbuttons{'gtps'};
 2422:     &display_navbuttons($r,$formname,$$states{$action}[$page-1],$prevtext,
 2423:                         $$states{$action}[$page+1],$nexttext);
 2424:     return;
 2425: }
 2426: 
 2427: sub build_boxes {
 2428:     my ($r,$tools,$usertools,$fixedprivs,$toolprivs,$showtools,
 2429:         $showboxes,$prefix,$specificity,$excluded) = @_;
 2430:     my $totalboxes = 0;
 2431:     if (@{$tools} > 0) {
 2432:         if ($specificity eq 'Yes') {
 2433:             foreach my $tool (@{$tools}) {
 2434:                 @{$$showboxes{$tool}} = ();
 2435:                 foreach my $user (sort(keys(%{$usertools}))) {
 2436:                     if (ref($excluded) eq 'ARRAY') {
 2437:                         if (grep/^$user$/,@{$excluded}) {
 2438:                             next;
 2439:                         }
 2440:                     }
 2441:                     if ($$usertools{$user}{$tool}) {
 2442:                         unless (grep/^$tool$/,@{$showtools}) {
 2443:                             push(@{$showtools},$tool);
 2444:                         }
 2445:                         foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 2446:                             unless (exists($$fixedprivs{$tool}{$priv})) {
 2447:                                 unless(grep(/^$priv$/,@{$$showboxes{$tool}})) {
 2448:                                     push(@{$$showboxes{$tool}},$priv);
 2449:                                     $totalboxes ++;
 2450:                                 }
 2451:                             }
 2452:                         }
 2453:                     }
 2454:                 }
 2455:             }
 2456:             if ($totalboxes > 0) {
 2457:                 $r->print('
 2458: <script type="text/javascript">
 2459: function checkAllTools(formname) {
 2460: ');
 2461:                 foreach my $tool (sort(keys(%{$showboxes}))) {
 2462:                     foreach my $priv (@{$$showboxes{$tool}}) {
 2463:                         $r->print('  checkAll(formname.'.$prefix.$priv.');'."\n");
 2464:                     }
 2465:                 }
 2466:                 $r->print('
 2467: }
 2468: function uncheckAllTools(formname) {
 2469: ');
 2470:                 foreach my $tool (sort(keys(%{$showboxes}))) {
 2471:                     foreach my $priv (@{$$showboxes{$tool}}) {
 2472:                         $r->print('  uncheckAll(formname'.$prefix.$priv.');'."\n");
 2473:                     }
 2474:                 }
 2475:                 $r->print('
 2476: }
 2477: </script>
 2478:                 ');
 2479:             }
 2480:         }
 2481:     }
 2482:     return $totalboxes;
 2483: }
 2484: 
 2485: sub member_privileges_form {
 2486:     my ($r,$action,$formname,$tools,$toolprivs,$fixedprivs,$userdata,
 2487:         $usertools,$idx,$memchg,$states,$stored,$gpterm) = @_;
 2488:     my %lt = &Apache::lonlocal::texthash(
 2489:             'addp' => 'Additional privileges',
 2490:             'fixp' => 'Fixed privileges',
 2491:             'oppr' => 'Optional privileges',
 2492:             'func' => 'Function',
 2493:             'forf' => 'For the functionality you have chosen to include '.
 2494:                       'there are no optional privileges to set besides '.
 2495:                       'the standard privileges.',
 2496:             'algr' => "All $gpterm members will receive the same privileges.",
 2497:             'asno' => "As no $gpterm members are being added, ".
 2498:                       "there are no specific user privileges to set.",
 2499:             'asng' => "As no $gpterm tools will be made available to users, ".
 2500:                       "there are no specific user privileges to set.",
 2501:             'nogm' => "No $gpterm member privileges to display or set, ".
 2502:                       "as you have not indicated that you will be activating,".
 2503:                       " re-enabling, changing privileges, or adding/removing ".
 2504:                       "functionality for any current members ",
 2505:             'full' => 'Fullname',
 2506:             'user' => 'Username',
 2507:             'doma' => 'Domain',
 2508:     );
 2509:     my @defprivs;
 2510:     my $specificity;
 2511:     if ($action eq 'create') {
 2512:         if (defined($env{'form.defpriv'})) {
 2513:             @defprivs = &Apache::loncommon::get_env_multiple('form.defpriv');
 2514:         }
 2515:         $specificity = $env{'form.specificity'};
 2516:     } else {
 2517:         if (defined($$stored{'defpriv'})) {
 2518:             @defprivs = @{$$stored{'defpriv'}};
 2519:         }
 2520:         $specificity = $$stored{'specificity'};
 2521:     }
 2522:     my @showtools;
 2523:     my %showboxes = ();
 2524:     my $numtools = 1 + @{$tools};
 2525: 
 2526:     my @excluded = ();
 2527:     my $numchgs = 0;
 2528:     if ($formname eq 'change_privs') {
 2529:         my @currmembers = ();
 2530:         if (ref($$memchg{'deletion'}) eq 'ARRAY') {
 2531:             push(@excluded,@{$$memchg{'deletion'}});
 2532:         }
 2533:         if (ref($$memchg{'expire'}) eq 'ARRAY') {
 2534:             push(@excluded,@{$$memchg{'expire'}});
 2535:         }
 2536:         if (@excluded > 0) {
 2537:             foreach my $user (sort(keys(%{$usertools}))) {
 2538:                 if (grep/^$user$/,@excluded) {
 2539:                     next;
 2540:                 }
 2541:                 push(@currmembers,$user);
 2542:             }
 2543:         } else {
 2544:             @currmembers = sort(keys(%{$usertools}));
 2545:         }
 2546:         $numchgs = @currmembers;
 2547:         if (!$numchgs) {
 2548:             $r->print($lt{'nogm'}); 
 2549:             return $numchgs;
 2550:         }
 2551:     }
 2552:  
 2553:     my $totalboxes = &build_boxes($r,$tools,$usertools,$fixedprivs,
 2554:                                    $toolprivs,\@showtools,\%showboxes,
 2555:                                    'userpriv_',$specificity,\@excluded);
 2556:     if (@{$tools} > 0) {
 2557:         if ($specificity eq 'Yes') {
 2558:             if ($totalboxes > 0) {
 2559:                 my $numcells = 2;
 2560:                 my $colspan = $numcells + 1;
 2561:                 my %total;
 2562:                 if (keys(%{$usertools}) > 1) {
 2563:                     $r->print('
 2564:    <table border="0" cellspacing="2" cellpadding="2">
 2565:     <tr>
 2566: ');
 2567:                     foreach my $tool (@{$tools}) {
 2568:                         if (@{$showboxes{$tool}} > 0) {
 2569:                             $r->print('<td valign="top">');
 2570:                             $r->print('<table class="thinborder"><tr>'.
 2571: 				      '<th colspan="'.$colspan.'">'.
 2572:                                       $tool.'</th></tr><tr>');
 2573:                             my $privcount = 0;
 2574:                             foreach my $priv (@{$showboxes{$tool}}) {
 2575:                                 $privcount ++;
 2576:                                 if (($privcount == @{$showboxes{$tool}}) && 
 2577:                                     ($privcount > 1)) {
 2578:                                     if ($privcount%$numcells) {
 2579:                                         $r->print('<td colspan="'.$colspan.'">');
 2580:                                     } else {
 2581:                                         $r->print('<td>');
 2582:                                     }
 2583:                                 } else {
 2584:                                     $r->print('<td>');
 2585:                                 }
 2586:                                 $r->print(qq|
 2587:        <fieldset><legend><b>$$toolprivs{$tool}{$priv}</b></legend>
 2588:        <nobr>
 2589:        <input type="button" value="check all"
 2590:          onclick="javascript:checkAll(document.$formname.userpriv_$priv)" />
 2591:        &nbsp;
 2592:        <input type="button" value="uncheck all"
 2593:         onclick="javascript:uncheckAll(document.$formname.userpriv_$priv)" />
 2594:       </nobr></fieldset><br />|);
 2595:                                 $r->print('</td>');
 2596:                                 if ($privcount < @{$showboxes{$tool}}) {
 2597:                                     if (@{$showboxes{$tool}} > 2) {
 2598:                                         if ($privcount%$numcells == 0) {
 2599:                                             $r->print('</tr><tr>');
 2600:                                         }
 2601:                                     } else {
 2602:                                         $r->print('<tr></tr>');
 2603:                                     }
 2604:                                 }
 2605:                             }
 2606:                             $r->print('</tr></table></td><td>&nbsp;</td>');
 2607:                         }
 2608:                     }
 2609:                     $r->print('</tr></table>');
 2610:                 }
 2611:                 $r->print(&Apache::loncommon::start_data_table().
 2612: 			  &Apache::loncommon::start_data_table_header_row());
 2613:                 $r->print(<<"END");
 2614:     <th>$lt{'full'}</th>
 2615:     <th>$lt{'user'}</th>
 2616:     <th>$lt{'doma'}</th>
 2617:     <th colspan="$numtools">$lt{'addp'}</th>
 2618: END
 2619:                 $r->print(&Apache::loncommon::end_data_table_header_row());
 2620:                 &member_privs_entries($r,$usertools,$toolprivs,$fixedprivs,
 2621:                                       $userdata,$idx,\@showtools,\@defprivs,
 2622:                                       \@excluded);
 2623:                 $r->print(&Apache::loncommon::end_data_table());
 2624:             } else {
 2625:                 $r->print($lt{'forf'}.'<br />');
 2626:                 &display_defprivs($r,$tools,$toolprivs,\@defprivs);
 2627:             }
 2628:         } else {
 2629:             if (keys(%{$usertools}) > 0) {
 2630:                 $r->print($lt{'algr'}.'<br /><br />');
 2631:                 &display_defprivs($r,$tools,$toolprivs,\@defprivs);
 2632:             } else {
 2633:                 $r->print($lt{'asno'}.'<br />');
 2634:             }
 2635:         }
 2636:     } else {
 2637:         $r->print($lt{'asng'});
 2638:     }
 2639:     return $numchgs;
 2640: }
 2641: 
 2642: sub process_request {
 2643:     my ($r,$cdom,$cnum,$action,$state,$page,$groupname,$description,
 2644:         $specificity,$userdata,$startdate,$enddate,$tools,$functions,$toolprivs,
 2645:         $usertools,$idx,$types,$roles,$sections,$states,$navbuttons,$memchg,
 2646:         $sectioncount,$stored,$gpterm,$ucgpterm,$crstype) = @_;
 2647: 
 2648:     $r->print(&Apache::lonhtmlcommon::echo_form_input(
 2649:                                  ['origin','action','state','page','sortby']));
 2650: 
 2651:     my $earlyout = &validate_groupname($groupname,$action,$cdom,$cnum,$gpterm,
 2652:                                        $ucgpterm,$crstype);
 2653:     if ($earlyout) {
 2654:         $r->print('
 2655: <table width="100%" cellpadding="0" cellspacing="0" border="0">
 2656:  <tr>
 2657:   <td>&nbsp;</td>
 2658:   <td colspan="3">
 2659: '.$earlyout.'</td></tr>');
 2660:         &display_navbuttons($r,$state,$$states{$action}[$page-1],
 2661:                             $$navbuttons{'gtps'});
 2662:         $r->print('</table>');
 2663:         return;
 2664:     }
 2665: 
 2666:     my @defprivs = ();
 2667:     if ($action eq 'create' || $state eq 'chgresult') { 
 2668:         if (defined($env{'form.defpriv'})) {
 2669:             @defprivs = &Apache::loncommon::get_env_multiple('form.defpriv');
 2670:         }
 2671:         if ($state eq 'chgresult') {
 2672:             my @okprivs = ();
 2673:             foreach my $tool (@{$tools}) {
 2674:                 foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 2675:                     push(@okprivs,$priv);
 2676:                 }
 2677:             }
 2678:             my @temp = ();
 2679:             foreach my $defpriv (@defprivs) {
 2680:                 if (grep/^$defpriv$/,@okprivs) {
 2681:                     push(@temp,$defpriv);
 2682:                 }
 2683:             }
 2684:             @defprivs = @temp; 
 2685:         }
 2686:     } else {
 2687:         if (defined($$stored{'defpriv'})) {
 2688:             @defprivs = @{$$stored{'defpriv'}};
 2689:         }
 2690:     }
 2691: 
 2692:     my $outcome;
 2693:     if ($action eq 'create' || $state eq 'chgresult') {
 2694:         $outcome = &write_group_data($r,$cdom,$cnum,$action,$state,$groupname,
 2695:                                      $description,$startdate,$enddate,
 2696:                                      $specificity,$functions,$tools,
 2697:                                      $sectioncount,$roles,$types,$sections,
 2698:                                      \@defprivs,$stored,$gpterm,$ucgpterm,
 2699:                                      $crstype); 
 2700:     }
 2701:     if (($action eq 'create' && $outcome eq 'ok') || (($action eq 'modify') && 
 2702:        (($state eq 'memresult') || ($state eq 'addresult')))) {
 2703:         &process_membership($r,$cdom,$cnum,$action,$state,$groupname,$tools,
 2704:                             $enddate,$startdate,$userdata,$idx,$toolprivs,
 2705:                             $usertools,$specificity,\@defprivs,$memchg,$gpterm,
 2706:                             $ucgpterm);
 2707:     }
 2708:     return;
 2709: }
 2710: 
 2711: sub write_group_data {
 2712:     my ($r,$cdom,$cnum,$action,$state,$groupname,$description,$startdate,
 2713:         $enddate,$specificity,$functions,$tools,$sectioncount,$roles,$types,
 2714:         $sections,$defprivs,$stored,$gpterm,$ucgpterm,$crstype) = @_;
 2715:     my $now = time;
 2716:     my $creation = $now;
 2717:     my $creator = $env{'user.name'}.':'.$env{'user.domain'};
 2718:     if ($state eq 'chgresult') {
 2719:         $creation = $$stored{'creation'};
 2720:         $creator = $$stored{'creator'};
 2721:     }
 2722:     my $esc_description = &escape($description);
 2723:     my @single_attributes = ('description','functions','startdate','enddate',
 2724:                              'creation','modified','creator','granularity',
 2725:                              'specificity','autoadd','autodrop','quota');
 2726:     my @mult_attributes = ('roles','types','sectionpick','defpriv');
 2727: 
 2728:     my ($crsquota,$freespace,$maxposs) = &get_quota_constraints($action,
 2729:                                                                 $stored);
 2730:     my $quota = $env{'form.quota'};
 2731:     
 2732:     $quota =~ s/^\s*([^\s]*)\s*$/$1/;
 2733:     if ($quota eq '') {
 2734:         $quota = 0;
 2735:     }
 2736:     if ($quota !~ /^\d*\.?\d*$/) {
 2737:         $quota = 0;
 2738:         $r->print(&mt('The value you entered for the quota for the file repository in this [_1] contained invalid characters, so it has been set to 0 Mb. You can change this by modifying the [_1] settings.<br />',$gpterm));
 2739:     }
 2740:     if ($quota > $maxposs) {
 2741:         $quota = $maxposs;
 2742:         $r->print(&mt('The value you entered for the quota for the file repository in this [_1] exceeded the maximum possible value, so it has been set to [_2] Mb (the maximum possible value).<br />',$gpterm,$maxposs));
 2743:     }
 2744:     my %groupinfo = (
 2745:                      description => $esc_description,
 2746:                      startdate => $startdate,
 2747:                      enddate => $enddate,
 2748:                      creation => $creation,
 2749:                      modified => $now,
 2750:                      creator => $creator,
 2751:                      granularity => $env{'form.granularity'},
 2752:                      specificity => $specificity,
 2753:                      autoadd => $env{'form.autoadd'},
 2754:                      autodrop => $env{'form.autodrop'},
 2755:                      quota => $quota,
 2756:                    );
 2757: 
 2758:     foreach my $func (keys(%{$functions})) {
 2759:         my $status;
 2760:         if (grep(/^$func$/,@{$tools})) {
 2761:             $status = 'on';
 2762:         } else {
 2763:             $status = 'off';
 2764:         }
 2765:         $groupinfo{'functions'} .=  qq|<name id="$func">$status</name>|;
 2766:     }
 2767: 
 2768:     $groupinfo{'roles'} = $roles;
 2769:     $groupinfo{'types'} = $types;
 2770:     $groupinfo{'sectionpick'} = $sections;
 2771:     $groupinfo{'defpriv'} = $defprivs;
 2772: 
 2773:     my %groupsettings = ();
 2774:     foreach my $item (@single_attributes) {
 2775:         $groupsettings{$groupname} .= qq|<$item>$groupinfo{$item}</$item>|;
 2776:     }
 2777:     foreach my $item (@mult_attributes) {
 2778:         foreach my $entry (@{$groupinfo{$item}}) {
 2779:             $groupsettings{$groupname} .= qq|<$item>$entry</$item>|;
 2780:         }
 2781:     }
 2782:     my $autosec;
 2783:     my @autorole = &Apache::loncommon::get_env_multiple('form.autorole');
 2784: 
 2785:     foreach my $role (@autorole) {
 2786:         if (defined($env{'form.sec_'.$role})) {
 2787:             my @autosections=&Apache::loncommon::get_env_multiple('form.sec_'.
 2788:                                                                   $role);
 2789:             $autosec .= '<role id="'.$role.'">';
 2790:             foreach my $sec (@autosections) {
 2791:                 $autosec .= '<section>'.$sec.'</section>';
 2792:             }
 2793:             $autosec .= '</role>';
 2794:         }
 2795:     }
 2796:     if ($autosec) {
 2797:         $groupsettings{$groupname} .= qq|<autosec>$autosec</autosec>|;
 2798:     }
 2799:     my $result = &Apache::lonnet::modify_coursegroup($cdom,$cnum,
 2800:                                                      \%groupsettings);
 2801: 
 2802:     if ($result eq 'ok') {
 2803:         if ($action eq 'create') {
 2804:             my $result = &add_group_folder($cdom,$cnum,$now,$groupname,$action,
 2805:                                            $description,$tools,\%groupinfo,
 2806:                                            $gpterm,$ucgpterm,$crstype);
 2807:             if ($result ne 'ok') {
 2808:                 $r->print(&mt('A problem occurred when creating folders for the new [_1]. [_2].<br />',$gpterm,$result));
 2809:             }
 2810:             $r->print(&mt('[_1] [_2] was created.<br />',$ucgpterm,$groupname));
 2811:         } else {
 2812:             $r->print(&mt('[_1] [_2] was updated.<br />',$ucgpterm,$groupname));
 2813:         }
 2814:     } else {
 2815:         my %actiontype = (
 2816:                           'create' => 'creating',
 2817:                           'modify' => 'modifying',
 2818:                          );
 2819:         &Apache::lonnet::logthis("Failed to store $gpterm $groupname ".
 2820:                                  'in '.lc($crstype).': '.$cnum.
 2821:                                  ' in domain: '.$cdom);
 2822:         $r->print(&mt('An error occurred when [_1] the [_2]. '.
 2823:                       'Please try again.',$actiontype{$action},$gpterm));
 2824:     }
 2825:     return $result;
 2826: }
 2827: 
 2828: sub process_membership {
 2829:     my ($r,$cdom,$cnum,$action,$state,$groupname,$tools,$enddate,$startdate,
 2830:         $userdata,$idx,$toolprivs,$usertools,$specificity,$defprivs,$memchg,
 2831:         $gpterm,$ucgpterm)=@_;
 2832:     my %usersettings = ();
 2833:     my %added= ();
 2834:     my %failed = ();
 2835:     my $num_ok = 0;
 2836:     my $num_fail = 0;
 2837:     my %group_privs = ();
 2838:     my %curr_privs = ();
 2839:     my %curr_start = ();
 2840:     my %curr_end = ();
 2841:     my %tooltype = ();
 2842: 
 2843:     foreach my $tool (@{$tools}) {
 2844:         foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 2845:             $tooltype{$priv} = $tool;
 2846:             if ($specificity eq 'Yes') {
 2847:                 my @users =
 2848:                   &Apache::loncommon::get_env_multiple('form.userpriv_'.$priv);
 2849:                 foreach my $user (@users) {
 2850:                     $group_privs{$user} .= $priv.':';
 2851:                     if ($state eq 'memresult') { 
 2852:                         unless (exists($$usertools{$user}{$tool})) {
 2853:                             $$usertools{$user}{$tool} = 1;
 2854:                         }
 2855:                     }
 2856:                 }
 2857:             } else {
 2858:                 if (@{$defprivs} > 0) {
 2859:                     if (grep/^\Q$priv\E$/,@{$defprivs}) {
 2860:                         foreach my $user (sort(keys(%{$usertools}))) {
 2861:                             if ($$usertools{$user}{$tool}) {
 2862:                                 $group_privs{$user} .= $priv.':';
 2863:                             }
 2864:                         }
 2865:                     }
 2866:                 }
 2867:             }
 2868:         }
 2869:     }
 2870:     foreach my $user (keys(%group_privs)) {
 2871:         $group_privs{$user} =~ s/:$//;
 2872:     }
 2873: 
 2874:     my $now = time;
 2875:     my @activate = ();
 2876:     my @expire = ();
 2877:     my @deletion = ();
 2878:     my @reenable = ();
 2879:     my @unchanged = ();
 2880:     if ($state eq 'memresult') {
 2881:         if (ref($$memchg{'activate'}) eq 'ARRAY') {
 2882:             @activate = @{$$memchg{'activate'}};
 2883:         }
 2884:         if (ref($$memchg{'expire'}) eq 'ARRAY') {
 2885:             @expire = @{$$memchg{'expire'}};
 2886:         }
 2887:         if (ref($$memchg{'deletion'}) eq 'ARRAY') {
 2888:             @deletion = @{$$memchg{'deletion'}};
 2889:         }
 2890:         if (ref($$memchg{'reenable'}) eq 'ARRAY') {
 2891:             @reenable = @{$$memchg{'reenable'}};
 2892:         }
 2893:         my %membership = &Apache::lonnet::get_group_membership($cdom,$cnum,
 2894:                                                                  $groupname);
 2895:         foreach my $key (sort(keys(%membership))) {
 2896:             if ($key =~ /^\Q$groupname\E:([^:]+:[^:]+)$/) {
 2897:                 ($curr_end{$1},$curr_start{$1},$curr_privs{$1}) = 
 2898:                                                 split(/:/,$membership{$key},3);
 2899:             }
 2900:         }
 2901:         if (@expire + @deletion > 0) {
 2902:             foreach my $user (@expire) {
 2903:                 my $savestart = $curr_start{$user};
 2904:                 if ($savestart > $now) {
 2905:                     $savestart = $now;
 2906:                 }
 2907:                 $usersettings{$groupname.':'.$user} = $now.':'.$savestart.':'.
 2908:                                                       $curr_privs{$user};
 2909:                 if (&Apache::lonnet::modify_group_roles($cdom,$cnum,$groupname,
 2910:                                                        $user,$now,$savestart,
 2911:                                                        $curr_privs{$user}) eq 'ok') {
 2912:                     push(@{$added{'expired'}},$user);
 2913:                     $num_ok ++;
 2914:                 } else {
 2915:                     push(@{$failed{'expired'}},$user);
 2916:                     $num_fail ++;
 2917:                 }
 2918:             }
 2919:             foreach my $user (@deletion) {
 2920:                 $usersettings{$groupname.':'.$user} = $now.':-1:';
 2921:                 if (&Apache::lonnet::modify_group_roles($cdom,$cnum,$groupname,
 2922:                                                        $user,$now,'-1','')
 2923:                                                          eq 'ok') {
 2924:                     push(@{$added{'deleted'}},$user);
 2925:                     $num_ok ++;
 2926:                 } else {
 2927:                     push(@{$failed{'deleted'}},$user);
 2928:                     $num_fail ++;
 2929:                 }
 2930:             }
 2931:         }
 2932:     }
 2933: 
 2934:     foreach my $user (sort(keys(%{$usertools}))) {
 2935:         if ((grep(/^$user$/,@expire)) || (grep(/^$user$/,@deletion))) {
 2936:             next;
 2937:         }
 2938:         my $type;
 2939:         my $start = $startdate;
 2940:         my $end = $enddate;
 2941:         if ($state eq 'memresult') {
 2942:             if (@activate > 0) {
 2943:                 if (grep/^$user$/,@activate) {
 2944:                     $start = $now;
 2945:                     $end = $enddate;
 2946:                     $type = 'activated';
 2947:                 }
 2948:             }
 2949:             if (@reenable > 0) {
 2950:                 if (grep/^$user$/,@reenable) {
 2951:                     $start = $startdate;
 2952:                     $end = $enddate;
 2953:                     $type = 'reenabled';
 2954:                 }
 2955:             }
 2956:             if ($type eq '') {
 2957:                 if ($curr_privs{$user} eq $group_privs{$user}) {
 2958:                     push(@unchanged,$user);
 2959:                     next;
 2960:                 }
 2961:                 if (exists($curr_start{$user})) {
 2962:                     $start = $curr_start{$user};
 2963:                 }
 2964:                 if (exists($curr_end{$user})) {
 2965:                     $end = $curr_end{$user};
 2966:                 }
 2967:                 $type = 'modified';
 2968:             }
 2969:         } else {
 2970:             $type = 'added';
 2971:         }
 2972:         $usersettings{$groupname.':'.$user} = $end.':'.$start.':'.
 2973:                                               $group_privs{$user};
 2974:         if (&Apache::lonnet::modify_group_roles($cdom,$cnum,$groupname,
 2975:                                                 $user,$end,$start,
 2976:                                                 $group_privs{$user}) eq 'ok') {
 2977:             push(@{$added{$type}},$user);
 2978:             $num_ok ++;
 2979:         } else {
 2980:             push(@{$failed{$type}},$user);
 2981:             $num_fail ++;
 2982:         }
 2983:     }
 2984:     my $roster_result = &Apache::lonnet::modify_coursegroup_membership($cdom,
 2985:                                                        $cnum,\%usersettings);
 2986:     if ($num_ok) {
 2987:         foreach my $type (sort(keys(%added))) { 
 2988:             $r->print(&mt('The following users were successfully [_1]',$type));
 2989:             if (!($type eq 'deleted' || $type eq 'expired')) {   
 2990:                 $r->print(&mt(' with the following privileges'));
 2991:             }
 2992:             $r->print(':<br />');
 2993:             foreach my $user (@{$added{$type}}) {
 2994:                 my $privlist = '';
 2995:                 if (!($type eq 'deleted' ||  $type eq 'expired')) {
 2996:                     $privlist = ': ';
 2997:                     my @privs = split(/:/,$group_privs{$user});
 2998:                     my $curr_tool = '';
 2999:                     foreach my $priv (@privs) {
 3000:                         unless ($curr_tool eq $tooltype{$priv}) {
 3001:                             $curr_tool = $tooltype{$priv};
 3002:                             $privlist .= '<b>'.$curr_tool.'</b>: ';
 3003:                         }
 3004:                         $privlist .= $$toolprivs{$curr_tool}{$priv}.', ';
 3005:                     }
 3006:                     $privlist =~ s/, $//;
 3007:                 }
 3008:                 $r->print($$userdata{$user}[$$idx{fullname}].'&nbsp;-&nbsp;'.$user.$privlist.'<br />');
 3009:             }
 3010:             $r->print('<br />');
 3011:         }
 3012:     }
 3013:     if ($num_fail) {
 3014:         foreach my $type (sort(keys(%failed))) {
 3015:             $r->print(&mt('The following users could not be [_1], because an error occurred:<br />',$type));
 3016:             foreach my $user (@{$failed{$type}}) {
 3017:                 $r->print($$userdata{$user}[$$idx{fullname}].' - '.$user.'<br />');
 3018:             }
 3019:         }
 3020:         $r->print('<br />');
 3021:     }
 3022:     if (@unchanged > 0) {
 3023:         $r->print(&mt('No change occurred for the following users:<br />'));
 3024:         foreach my $user (sort(@unchanged)) {
 3025:             $r->print($$userdata{$user}[$$idx{fullname}].' - '.$user.'<br />');
 3026:         }
 3027:         $r->print('<br />');
 3028:     }
 3029:     if ($roster_result eq 'ok') {
 3030:         $r->print('<br />'.&mt('[_1] membership list updated.',$ucgpterm));
 3031: 	$r->print('<p>'.&mt("For full access to all of [_1]'s privileges, users will need to log out and log back in.",$groupname).'</p>');
 3032:     } else {
 3033:         $r->print('<br />'.&mt('An error occurred while updating the [_1] membership list -',$gpterm).$roster_result.'<br />');
 3034:     }
 3035:     return;
 3036: }
 3037: 
 3038: sub mapping_options {
 3039:     my ($r,$action,$formname,$page,$sectioncount,$states,$stored,
 3040:         $navbuttons,$img1,$img2,$gpterm,$ucgpterm,$crstype) = @_;
 3041:     my %lt = &Apache::lonlocal::texthash(
 3042:         'auto' => "Settings for automatic $gpterm enrollment",
 3043:         'gmma' => "$ucgpterm membership mapping to specific sections/roles",
 3044:         'endi' => "Enable/disable automatic $gpterm enrollment for ".
 3045:                           "users in specified roles and sections",
 3046:         'adds'  => "If automatic $gpterm enrollment is enabled, when a user is assigned a ".lc($crstype)."-wide or section-specific role, he/she will automatically be added as a member of the $gpterm, with start and end access dates defined by the default dates set for the $gpterm, unless he/she is already a $gpterm member, with access dates that permit either current or future $gpterm access.",
 3047:         'drops'  => "If automatic $gpterm disenrollment is enabled, when a user's role is expired, access to the $gpterm will be terminated unless the user continues to have other ".lc($crstype)."-wide or section-specific active or future roles which receive automatic membership in the $gpterm.",
 3048:         'pirs' => "Pick roles and sections for automatic $gpterm enrollment",
 3049:         'curr' => 'Currently set to',
 3050:         'on' => 'on',
 3051:         'off' => 'off',
 3052:         'auad' => "Automatically enable $gpterm membership when roles are added?",
 3053:         'auex' => "Automatically expire $gpterm membership when roles are removed?",
 3054:         'mapr' => "Mapping of roles and sections affected by automatic $gpterm enrollment/disenrollment follows scheme chosen below.",
 3055:     );
 3056:     &automapping($r,$action,$stored,\%lt,$img1);
 3057:     &mapping_settings($r,$sectioncount,\%lt,$stored,$img2,$crstype);
 3058:     return;
 3059: }
 3060: 
 3061: sub automapping {
 3062:     my ($r,$action,$stored,$lt,$image) = @_;
 3063:     my $add = 'off';
 3064:     my $drop = 'off';
 3065:     if (exists($$stored{'autoadd'})) {
 3066:         $add = $$stored{'autoadd'};
 3067:     }
 3068:     if (exists($$stored{'autodrop'})) {
 3069:         $drop = $$stored{'autodrop'};
 3070:     }
 3071:     &topic_bar($r,$image,$$lt{'endi'});
 3072:     $r->print('
 3073:     <b>'.$$lt{'gmma'}.':</b><br />'.$$lt{'adds'}.'<br />'.$$lt{'drops'}.'<br /><br />
 3074:    <nobr>'.$$lt{'auad'}.':&nbsp;
 3075:     <label><input type="radio" name="autoadd" value="on" />on&nbsp;&nbsp;</label><label><input type="radio" name="autoadd" value="off" checked="checked" />off</label>');
 3076:     if ($action eq 'modify') {
 3077:         $r->print('&nbsp;&nbsp;&nbsp;&nbsp;('.$$lt{'curr'}.' <b>'.$$lt{$add}.'</b>)');
 3078:     }
 3079:     $r->print('
 3080:     </nobr><br />
 3081:     <nobr>'.$$lt{'auex'}.':&nbsp;
 3082:     <label><input type="radio" name="autodrop" value="on" />on&nbsp;&nbsp;</label><label><input type="radio" name="autodrop" value="off" checked="checked" />off</label>');
 3083:     if ($action eq 'modify') {
 3084:         $r->print('&nbsp;&nbsp;&nbsp;&nbsp;('.$$lt{'curr'}.' <b>'.$$lt{$drop}.'</b>)');
 3085:     }
 3086:     $r->print('</nobr><br /><br />'.$$lt{'mapr'});
 3087: }
 3088: 
 3089: sub mapping_settings {
 3090:     my ($r,$sectioncount,$lt,$stored,$image,$crstype) = @_;
 3091:     my @sections = keys(%{$sectioncount});
 3092:     if (@sections > 0) {
 3093:         @sections = sort {$a cmp $b} @sections;
 3094:         unshift(@sections,'none'); # Put 'no sections' next
 3095:         unshift(@sections,'all'); # Put 'all' at the front of the list
 3096:     }
 3097:     &topic_bar($r,$image,$$lt{'pirs'});
 3098:     my @roles = &standard_roles();
 3099:     my %customroles = &my_custom_roles();
 3100:     $r->print(&Apache::loncommon::start_data_table().
 3101: 	      &Apache::loncommon::start_data_table_header_row());
 3102:     $r->print('
 3103:                  <th>'.&mt('Active?').'</th>
 3104:                  <th>'.&mt('Role').'</th>');
 3105:     if (@sections > 0) {
 3106:         $r->print('<th>'.&mt('Sections').'</th>');
 3107:     }
 3108:     $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
 3109:     foreach my $role (@roles) {
 3110:         my $plrole=&Apache::lonnet::plaintext($role,$crstype);
 3111:         my $sections_sel;
 3112:         if (@sections > 0) {
 3113:             if ($role eq 'cc') {
 3114:                 $sections_sel = '<td align="right">'.
 3115:                                 &mt('all sections').'<input type="hidden" '. 
 3116:                                 'name="sec_cc" value="all" /></td>';
 3117:             } else { 
 3118:                 $sections_sel='<td align="right">'.
 3119:                               &sections_selection(\@sections,'sec_'.$role).
 3120:                               '</td>';
 3121:             }
 3122:         }
 3123:         $r->print(&Apache::loncommon::start_data_table_row().
 3124: 		  '<td><input type="checkbox" '.
 3125:                   'name="autorole" value="'.$role.'" /></td><td>'.$plrole.
 3126:                   '</td>'.$sections_sel.
 3127: 		  &Apache::loncommon::end_data_table_row());
 3128:     }
 3129:     foreach my $role (sort(keys(%customroles))) {
 3130:         my $sections_sel;
 3131:         if (@sections > 0) {
 3132:             $sections_sel = 
 3133: 		'<td>'.&sections_selection(\@sections,'sec_'.$role).'</td>';
 3134:         }
 3135:         $r->print(&Apache::loncommon::start_data_table_row().
 3136: 		  '<td><input type="checkbox" '.
 3137:                   'value="'.$role.'" /></td><td>'.$role.'</td>'.
 3138:                   $sections_sel.
 3139: 		  &Apache::loncommon::end_data_table_row());
 3140:     }
 3141:     $r->print(&Apache::loncommon::end_data_table());
 3142:     return;
 3143: }
 3144: 
 3145: sub standard_roles {
 3146:     my @roles = ('cc','in','ta','ep','st');
 3147:     return @roles;
 3148: }
 3149: 
 3150: sub my_custom_roles {
 3151:     my %returnhash=();
 3152:     my %rolehash=&Apache::lonnet::dump('roles');
 3153:     foreach (keys %rolehash) {
 3154:         if ($_=~/^rolesdef\_(\w+)$/) {
 3155:             $returnhash{$1}=$1;
 3156:         }
 3157:     }
 3158:     return %returnhash;
 3159: }
 3160: 
 3161: sub modify_menu {
 3162:     my ($r,$groupname,$page,$gpterm) = @_;
 3163:     my @menu =
 3164:         (
 3165:           { text => "Modify default $gpterm settings",
 3166:             help => 'Course_Modify_Group',
 3167:             state => 'change_settings',
 3168:             branch => 'settings',
 3169:             },
 3170:           { text => 'Modify access, tools and/or privileges for previous, '.
 3171:                     'future, or current members',
 3172:             help => 'Course_Modify_Group_Membership',
 3173:             state => 'change_members',
 3174:             branch => 'members',
 3175:             },
 3176:           { text => "Add member(s) to the $gpterm",
 3177:             help => 'Course_Group_Add_Members',
 3178:             state => 'add_members',
 3179:             branch => 'adds',
 3180:             },
 3181:           );
 3182:     my $menu_html = '';
 3183:     foreach my $menu_item (@menu) {
 3184:         $menu_html .=
 3185:         '<p><font size="+1"><a href="/adm/coursegroups?action=modify&refpage='.$env{'form.refpage'}.'&groupname='.$groupname.'&state='.$menu_item->{'state'}.'&branch='.$menu_item->{'branch'}.'">';
 3186:         $menu_html.= &mt($menu_item->{'text'}).'</a></font>';
 3187:         if (exists($menu_item->{'help'})) {
 3188:             $menu_html.=
 3189:                 &Apache::loncommon::help_open_topic($menu_item->{'help'});
 3190:         }
 3191:         $menu_html.='</p>'.$/;
 3192:     }
 3193:     $r->print($menu_html);
 3194:     return;
 3195: }
 3196: 
 3197: sub member_privs_entries {
 3198:     my ($r,$usertools,$toolprivs,$fixedprivs,$userdata,$idx,$showtools,
 3199: 	$defprivs,$excluded) = @_;
 3200:     foreach my $user (sort(keys(%{$usertools}))) {
 3201:         if (defined($excluded)) {
 3202:             if (ref($excluded) eq 'ARRAY') {
 3203:                 if (grep/^$user$/,@{$excluded}) {
 3204:                     next;
 3205:                 }
 3206:             }
 3207:         }
 3208:         my ($uname,$udom) = split(/:/,$user);
 3209:         $r->print(&Apache::loncommon::start_data_table_row().'
 3210:                 <td>'.$$userdata{$user}[$$idx{fullname}].'</td>
 3211:                 <td>'.$uname.'</td>
 3212:                 <td>'.$udom.'</td>
 3213:                 <td valign="top"><table><tr><td><b>Function</b></td></tr><tr><td><b>Fixed</b></td></tr><tr><td><b>Optional</b></td></tr></table></td>');
 3214:         foreach my $tool (@{$showtools}) {
 3215:             if (exists($$usertools{$user}{$tool})) {
 3216:                 $r->print('<td valign="top"><table><tr><th colspan="2">'.$tool.'</th></tr>');
 3217:                 my $privcount = 0;
 3218:                 my $fixed = '';
 3219:                 my $dynamic = '';
 3220:                 foreach my $priv (sort(keys(%{$$toolprivs{$tool}}))) {
 3221:                     if (exists($$fixedprivs{$tool}{$priv})) {
 3222:                         $fixed .= '<input type="hidden" name="userpriv_'.$priv.'" value="'.$user.'" />'.$$toolprivs{$tool}{$priv}.'&nbsp;';
 3223:                     } else {
 3224:                         $privcount ++;
 3225:                         if ($privcount == 3) {
 3226:                             $dynamic .= '</tr><tr>';
 3227:                         }
 3228:                         $dynamic .='<td><nobr><label><input type="checkbox" '.
 3229:                                'name="userpriv_'.$priv.'" value="'.$user.'"';
 3230:                         if (grep/^\Q$priv\E$/,@{$defprivs}) {
 3231:                             $dynamic .= ' checked="checked" ';
 3232:                         }
 3233:                         $dynamic .= ' />'.$$toolprivs{$tool}{$priv}.
 3234:                                     '</label></nobr></td>';
 3235:                     }
 3236:                 }
 3237:                 $r->print('<tr><td colspan="2"><nobr>'.$fixed.'</nobr></td></tr><tr>'.$dynamic.'</tr></table></td>');
 3238:             } else {
 3239:                 $r->print('<td valign="top"><table width="100%"><tr><th colspan="2">'.$tool.'</th></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></table></td>');
 3240:             }
 3241:         }
 3242:         $r->print(&Apache::loncommon::end_data_table_row());
 3243:     }
 3244: }
 3245: 
 3246: sub get_dates_from_form {
 3247:     my $startdate;
 3248:     my $enddate;
 3249:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
 3250:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate');
 3251:     if ( exists ($env{'form.no_end_date'}) ) {
 3252:         $enddate = 0;
 3253:     }
 3254:     return ($startdate,$enddate);
 3255: }
 3256: 
 3257: sub date_setting_table {
 3258:     my ($starttime,$endtime,$formname) = @_;
 3259:     my $startform = &Apache::lonhtmlcommon::date_setter($formname,
 3260:                                                       'startdate',$starttime);
 3261:     my $endform = &Apache::lonhtmlcommon::date_setter($formname,
 3262:                                                       'enddate',$endtime);
 3263:     my $perpetual = 
 3264: 	'<nobr><label><input type="checkbox" name="no_end_date" />'.
 3265: 	&mt('None').'</label></nobr>';
 3266:     my $table = "<table class=\"LC_descriptive_input\">\n".
 3267: 	'<tr><td class="LC_description">'.&mt('Start:').'</td>'.
 3268:         '<td>'.$startform.'</td>'.
 3269:         '<td>&nbsp;</td>'."</tr>\n".
 3270: 	'<tr><td class="LC_description">'.&mt('End:').'</td>'.
 3271:         '<td>'.$endform.'</td>'.
 3272:         '<td>'.$perpetual.'</td>'."</tr>\n".
 3273: 	"</table>\n";
 3274:     return $table;
 3275: }
 3276: 
 3277: sub add_group_folder {
 3278:     my ($cdom,$cnum,$now,$groupname,$action,$description,$tools,$groupinfo,
 3279:         $gpterm,$ucgpterm,$crstype) = @_;
 3280:     if ($cdom eq '' || $cnum eq '') {
 3281:         return &mt('Error: invalid course domain or number - group folder creation failed');  
 3282:     }
 3283:     my ($outcome,$allgrpsmap,$grpmap,$boardsmap,$grppage);
 3284:     my $navmap = Apache::lonnavmaps::navmap->new();
 3285:     my $crspath = '/uploaded/'.$cdom.'/'.$cnum.'/';
 3286:     $allgrpsmap = $crspath.'group_allfolders.sequence';
 3287:     my $topmap = $navmap->getResourceByUrl($allgrpsmap);
 3288:     undef($navmap);
 3289:     if ($action eq 'create') {
 3290:     # check if group_allfolders.sequence exists.
 3291:         if (!$topmap) {
 3292:             my $grpstitle = &mt('[_1] [_2]',$crstype,$ucgpterm);
 3293:             my $topmap_url = '/'.$env{'course.'.$env{'request.course.id'}.'.url'};
 3294:             $topmap_url =~ s|/+|/|g;
 3295:             if ($topmap_url =~ m|^/uploaded|) {
 3296:                 $outcome = &map_updater($cdom,$cnum,'group_allfolders.sequence',
 3297:                                         'toplevelgroup',$grpstitle,$topmap_url);
 3298:                 if ($outcome ne 'ok') {
 3299:                     return $outcome;
 3300:                 }
 3301:             } else {
 3302:                 $outcome = &mt('Non-standard course - folder for all groups not added.');
 3303:                 return $outcome;
 3304:             }
 3305:         }
 3306:         my $grpfolder = &mt('[_1] Folder -',$ucgpterm,).$description;
 3307:         $grppage='/adm/'.$cdom.'/'.$cnum.'/'.$groupname.'/grppg';
 3308:         my $grptitle = &mt('Group homepage').' - '.$description;
 3309:         my ($seqid,$discussions,$disctitle);
 3310:         my $outcome = &map_updater($cdom,$cnum,'group_folder_'.$groupname.'.sequence',
 3311:                                    'grpseq',$grpfolder,$allgrpsmap,$grppage,
 3312:                                    $grptitle);
 3313:         if ($outcome ne 'ok') {
 3314:             return $outcome;
 3315:         }
 3316:         my $pageout = &create_homepage($cdom,$cnum,$groupname,$groupinfo,
 3317:                                        $tools,$gpterm,$ucgpterm,$now);
 3318:         # Link to folder for bulletin boards
 3319:         $grpmap = $crspath.'group_folder_'.$groupname.'.sequence';
 3320:         if (grep/^discussion$/,@{$tools}) {
 3321:             $seqid = $now + 1;
 3322:             $disctitle = &mt('Discussion Boards');
 3323:             my $outcome = &map_updater($cdom,$cnum,'group_boards_'.$groupname.
 3324:                                        '.sequence','bbseq',$disctitle,$grpmap);
 3325:             if ($outcome ne 'ok') {
 3326:                 return $outcome;
 3327:             }
 3328:             $boardsmap = $crspath.'group_boards_'.$groupname.'.sequence';
 3329:         }
 3330:     } else {
 3331:         #modify group folder if status of discussions tools is changed
 3332:     }
 3333:     my ($furl,$ferr)= &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
 3334:     $navmap = Apache::lonnavmaps::navmap->new();
 3335:     # modify parameters
 3336:     my $parm_result;
 3337:     if ($action eq 'create') {
 3338:         if ($allgrpsmap) { 
 3339:             $parm_result .= &parm_setter($navmap,$cdom,$allgrpsmap,$groupname);
 3340:         }
 3341:         if ($grpmap) {
 3342:             $parm_result .= &parm_setter($navmap,$cdom,$grpmap,$groupname);
 3343:         }
 3344:         if ($grppage) {
 3345:             $parm_result .= &parm_setter($navmap,$cdom,$grppage,$groupname);
 3346:         }
 3347:         if ($boardsmap) {
 3348:             $parm_result .= &parm_setter($navmap,$cdom,$boardsmap,$groupname);
 3349:         }
 3350:     }
 3351:     if ($parm_result) {
 3352:         return $parm_result;
 3353:     } else {
 3354:         return 'ok';
 3355:     }
 3356: }
 3357: 
 3358: sub map_updater {
 3359:     my ($cdom,$cnum,$newfile,$itemname,$itemtitle,$parentmap,$startsrc,
 3360:         $starttitle,$endsrc,$endtitle) = @_;
 3361:     my $outcome;
 3362:     $env{'form.'.$itemname} = &new_map($startsrc,$starttitle,$endsrc,
 3363:                                        $endtitle);
 3364:     my $newmapurl=&Apache::lonnet::finishuserfileupload($cnum,$cdom,$itemname,
 3365:                                                         $newfile);
 3366:     if ($newmapurl !~ m|^/uploaded|) {
 3367:         $outcome = &mt('Error uploading new folder.')." ($newfile): $newmapurl".'<br />';
 3368:         return $outcome;
 3369:     } 
 3370:     my ($errtext,$fatal)=&Apache::lonratedt::mapread($parentmap);
 3371:     if ($fatal) {
 3372:         $outcome = &mt('Error reading contents of parent folder')." ($parentmap): $errtext".'<br />';
 3373:         return $outcome;
 3374:     } else {
 3375:         my $newidx=&Apache::lonratedt::getresidx($newmapurl);
 3376:         $Apache::lonratedt::resources[$newidx] = $itemtitle.':'.$newmapurl.
 3377:                                                  ':false:normal:res';
 3378:         $Apache::lonratedt::order[1+$#Apache::lonratedt::order]=$newidx;
 3379:         my ($outtext,$errtext) = &Apache::lonratedt::storemap($parentmap,1);
 3380:         if ($errtext) {
 3381:             $outcome = &mt('Error storing updated parent folder')." ($parentmap):  $errtext".'<br />';
 3382:             return $outcome;
 3383:         }
 3384:     }
 3385:     return 'ok';
 3386: }
 3387: 
 3388: sub new_map {
 3389:     my ($startsrc,$starttitle,$endsrc,$endtitle) = @_;
 3390:     my $newmapstr = '
 3391: <map>
 3392:  <resource id="1" src="'.$startsrc.'" type="start" title="'.$starttitle.'"></resource>
 3393:  <link from="1" to="2" index="1"></link>
 3394:  <resource id="2" src="'.$endsrc.'" type="finish" title="'.$endtitle.'"></resource>
 3395: </map>
 3396: ';
 3397:     return $newmapstr;
 3398: }
 3399: 
 3400: sub parm_setter {
 3401:     my ($navmap,$cdom,$url,$groupname) = @_;
 3402:     my $allresults;
 3403:     my %hide_settings = (
 3404:                            'course' =>  {
 3405:                                           'num' => 13,
 3406:                                           'set' => 'yes',
 3407:                                         },
 3408:                             'group' =>  {
 3409:                                           'num' => 5,
 3410:                                           'set' => 'no',
 3411:                                           'extra' => $groupname,
 3412:                                         },
 3413:                         );
 3414:     my $res = $navmap->getResourceByUrl($url);
 3415:     my $symb = $res->symb();
 3416:     foreach my $level (keys(%hide_settings)) {
 3417:         my $parmresult =  &Apache::lonparmset::storeparm_by_symb($symb,
 3418:                                                  '0_hiddenresource',
 3419:                                                  $hide_settings{$level}{'num'},
 3420:                                                  $hide_settings{$level}{'set'},
 3421:                                                  'string_yesno',undef,$cdom,
 3422:                                                  undef,undef,
 3423:                                                  $hide_settings{$level}{'extra'});
 3424:         if ($parmresult) {
 3425:             $allresults .= $level.': '.$parmresult;
 3426:         }
 3427:     }
 3428:     return $allresults;
 3429: }
 3430: 
 3431: sub create_homepage {
 3432:     my ($cdom,$cnum,$name,$groupinfo,$tools,$gpterm,$ucgpterm,$now) = @_;
 3433:     my $functionality = join(',',@{$tools});
 3434:     my $content = &unescape($$groupinfo{description});
 3435:     $content=~s/\s+$//s;
 3436:     $content=~s/^\s+//s;
 3437:     $content=~s/\<br\s*\/*\>$//s;
 3438:     $content=&Apache::lonfeedback::clear_out_html($content,1);
 3439: 
 3440:     my %pageinfo = (
 3441:                      'aaa_title' => "$ucgpterm: $name",
 3442:                      'abb_links' => $functionality,
 3443:                      'bbb_content' => $content,
 3444:                      'ccc_webreferences' => '',
 3445:                      'uploaded.lastmodified' => $now,
 3446:                    );
 3447:    my $putresult = &Apache::lonnet::put('grppage_'.$name,\%pageinfo,$cdom,$cnum);
 3448:    return $putresult;
 3449: }
 3450: 
 3451: sub check_uncheck_tools {
 3452:     my ($r,$available) = @_;
 3453:     if (ref($available) eq 'ARRAY') { 
 3454:         $r->print('
 3455: <script type="text/javascript">
 3456: function checkAllTools(formname) {
 3457: ');
 3458:         foreach my $tool (@{$available}) {
 3459:             $r->print('  checkAll(formname.user_'.$tool.');'."\n");
 3460:         }
 3461:         $r->print(' checkAll(formname.togglefunc);'."\n");
 3462:         $r->print('
 3463: }
 3464: function uncheckAllTools(formname) {
 3465: ');
 3466:         foreach my $tool (@{$available}) {
 3467:             $r->print('  uncheckAll(formname.user_'.$tool.');'."\n");
 3468:         }
 3469:         $r->print(' uncheckAll(formname.togglefunc);'."\n");
 3470:         $r->print('
 3471: }
 3472: function toggleTools(field,caller) {
 3473:      if (caller.checked) {
 3474:          checkAll(field);
 3475:      } else {
 3476:          uncheckAll(field);
 3477:      }
 3478:      return;   
 3479: }
 3480: </script>
 3481: ');
 3482:     }
 3483:     return;
 3484: }
 3485: 
 3486: sub validate_groupname {
 3487:     my ($groupname,$action,$cdom,$cnum,$gpterm,$ucgpterm,$crstype) = @_;
 3488:     my %sectioncount = &Apache::loncommon::get_sections($cdom,$cnum);
 3489:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
 3490: 
 3491:     my %lt = &Apache::lonlocal::texthash (
 3492:                       igna => "Invalid $gpterm name",
 3493:                       tgne => "The $gpterm name entered ",
 3494:                       grna => "$ucgpterm names and section names used in a ".
 3495:                                "$crstype must be unique.",
 3496:                       isno => "is not a valid name.",
 3497:                       gnmo => "$ucgpterm names may only contain letters, ". 
 3498:                               "numbers or underscores.",
 3499:                       cnnb => "can not be used as it is the name of ",
 3500:                       inth => " in this $crstype", 
 3501:                       thgr => "- does not correspond to the name of an ".
 3502:                               "existing $gpterm",    
 3503:     );
 3504: 
 3505:     my $exitmsg = '<span class="LC_error">'.$lt{'igna'}.'</span><br /><br />'.
 3506: 	$lt{'tgne'}.' "'.$groupname.'" ';
 3507:     my $dupmsg = $lt{'grna'};
 3508:     my $earlyout;
 3509:     if (($groupname eq '') || ($groupname =~ /\W/)) {
 3510:         $earlyout = $exitmsg.$lt{'isno'}.'<br />'.$lt{'gnmo'};
 3511:         return $earlyout;
 3512:     }
 3513:     if (exists($sectioncount{$groupname})) {
 3514: 	return $exitmsg.$lt{'cnnb'}.&mt('a section').$lt{'inth'}.
 3515: 	    '<br />'.$lt{'grna'};
 3516:     }
 3517:     if ($action eq 'create' 
 3518: 	&& exists($curr_groups{$groupname})) {
 3519: 
 3520: 	return $exitmsg.$lt{'cnnb'}.&mt('an existing [_1]',$gpterm).
 3521: 	    $lt{'inth'}.'<br />'.$lt{'grna'};
 3522: 
 3523:     } elsif ($action eq 'modify') {
 3524:         unless(exists($curr_groups{$groupname})) {
 3525:             $earlyout = &mt('[_1] name:',$ucgpterm).' '.$groupname.$lt{'thgr'}.
 3526:                         $lt{'inth'};
 3527:             return $earlyout;
 3528:         }
 3529:     }
 3530:     return;
 3531: }
 3532: 
 3533: sub topic_bar {
 3534:     my ($r,$imgnum,$title) = @_;
 3535:     $r->print('
 3536: <div class="LC_topic_bar">
 3537:     <img alt="'.&mt('Step [_1]',$imgnum).
 3538: 	      '"src="/res/adm/pages/bl_step'.$imgnum.'.gif" />&nbsp;
 3539:     <span>'.$title.'</span>
 3540: </div>
 3541: ');
 3542:     return;
 3543: }
 3544: 
 3545: sub check_changes {
 3546:     my ($member_changes,$memchg) = @_;
 3547:     my %exclusions;
 3548:     @{$exclusions{'changefunc'}} = ('expire');
 3549:     @{$exclusions{'changepriv'}} = ('expire','changefunc');
 3550: 
 3551:     foreach my $change (@{$member_changes}) {
 3552:         if ($change eq 'deletion') {
 3553:             next;
 3554:         }
 3555:         my @checks = ('deletion');
 3556:         if (exists($exclusions{$change})) {
 3557:             push(@checks,@{$exclusions{$change}});
 3558:         }
 3559:         my @temp = ();
 3560:         foreach my $item (@{$$memchg{$change}}) {
 3561:             my $match = 0;
 3562:             foreach my $check (@checks) {
 3563:                 if (defined($$memchg{$check})) { 
 3564:                     if (ref(@{$$memchg{$check}}) eq 'ARRAY') {
 3565:                         if (@{$$memchg{$check}} > 0) {
 3566:                             if (grep/^$item$/,@{$$memchg{$check}}) {
 3567:                                 $match = 1;
 3568:                                 last;
 3569:                             }
 3570:                         }
 3571:                     }
 3572:                 }
 3573:             }
 3574:             if ($match) {
 3575:                 next;
 3576:             }
 3577:             push(@temp,$item);
 3578:         }
 3579:         @{$$memchg{$change}} = @temp;
 3580:     }
 3581: }
 3582: 
 3583: 1;

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