Annotation of loncom/interface/lonuserutils.pm, revision 1.133

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Utility functions for managing LON-CAPA user accounts
                      3: #
1.133   ! raeburn     4: # $Id: lonuserutils.pm,v 1.132 2010/11/14 20:34:47 raeburn Exp $
1.1       raeburn     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: #
                     28: ###############################################################
                     29: ###############################################################
                     30: 
                     31: package Apache::lonuserutils;
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon;
                     37: use Apache::lonlocal;
1.8       raeburn    38: use Apache::longroup;
                     39: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn    40: 
                     41: ###############################################################
                     42: ###############################################################
                     43: # Drop student from all sections of a course, except optional $csec
                     44: sub modifystudent {
1.52      raeburn    45:     my ($udom,$unam,$courseid,$csec,$desiredhost,$context)=@_;
1.1       raeburn    46:     # if $csec is undefined, drop the student from all the courses matching
                     47:     # this one.  If $csec is defined, drop them from all other sections of
                     48:     # this course and add them to section $csec
1.17      raeburn    49:     my ($cnum,$cdom) = &get_course_identity($courseid);
1.127     raeburn    50:     my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
                     51:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam,'.',undef,$extra);
1.1       raeburn    52:     my ($tmp) = keys(%roles);
                     53:     # Bail out if we were unable to get the students roles
                     54:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                     55:     # Go through the roles looking for enrollment in this course
                     56:     my $result = '';
                     57:     foreach my $course (keys(%roles)) {
                     58:         if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
                     59:             # We are in this course
                     60:             my $section=$1;
                     61:             $section='' if ($course eq "/$cdom/$cnum".'_st');
                     62:             if (defined($csec) && $section eq $csec) {
                     63:                 $result .= 'ok:';
                     64:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
                     65:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
                     66:                 my $now=time;
                     67:                 # if this is an active role
                     68:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
                     69:                     my $reply=&Apache::lonnet::modifystudent
                     70:                         # dom  name  id mode pass     f     m     l     g
                     71:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.22      raeburn    72:                          $section,time,undef,undef,$desiredhost,'','manual',
1.52      raeburn    73:                          '',$courseid,'',$context);
1.1       raeburn    74:                     $result .= $reply.':';
                     75:                 }
                     76:             }
                     77:         }
                     78:     }
                     79:     if ($result eq '') {
1.37      raeburn    80:         $result = &mt('Unable to find section for this student');
1.1       raeburn    81:     } else {
                     82:         $result =~ s/(ok:)+/ok/g;
                     83:     }
                     84:     return $result;
                     85: }
                     86: 
                     87: sub modifyuserrole {
                     88:     my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
                     89:         $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
1.84      raeburn    90:         $end,$start,$checkid,$inststatus) = @_;
1.5       raeburn    91:     my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1       raeburn    92:     if ($setting eq 'course' || $context eq 'course') {
                     93:         $scope = '/'.$cid;
                     94:         $scope =~ s/\_/\//g;
1.103     raeburn    95:         if (($role ne 'cc') && ($role ne 'co') && ($sec ne '')) {
1.1       raeburn    96:             $scope .='/'.$sec;
                     97:         }
1.5       raeburn    98:     } elsif ($context eq 'domain') {
1.1       raeburn    99:         $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn   100:     } elsif ($context eq 'author') {
1.1       raeburn   101:         $scope =  '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                    102:     }
                    103:     if ($context eq 'domain') {
                    104:         my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    105:         if ($uhome ne 'no_host') {
1.5       raeburn   106:             if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1       raeburn   107:                 if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
                    108:                     ($umode eq 'localauth')) {
                    109:                     $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
                    110:                 }
                    111:             }
1.5       raeburn   112:             if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
                    113:                 ($env{'form.recurseid'}) && ($checkid)) {
                    114:                 my %userupdate = (
                    115:                                   lastname   => $last,
                    116:                                   middlename => $middle,
                    117:                                   firstname  => $first,
                    118:                                   generation => $gene,
                    119:                                   id         => $uid,
                    120:                                  );
                    121:                 $idresult = &propagate_id_change($uname,$udom,\%userupdate);
                    122:             }
1.1       raeburn   123:         }
                    124:     }
                    125:     $userresult =
                    126:         &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
                    127:                                     $middle,$last,$gene,$forceid,$desiredhome,
1.84      raeburn   128:                                     $email,$inststatus);
1.1       raeburn   129:     if ($userresult eq 'ok') {
1.5       raeburn   130:         if ($role ne '') {
1.22      raeburn   131:             $role =~ s/_/\//g;
1.1       raeburn   132:             $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
1.52      raeburn   133:                                                       $role,$end,$start,'',
                    134:                                                       '',$context);
1.1       raeburn   135:         }
                    136:     }
1.5       raeburn   137:     return ($userresult,$authresult,$roleresult,$idresult);
1.1       raeburn   138: }
                    139: 
1.5       raeburn   140: sub propagate_id_change {
                    141:     my ($uname,$udom,$user) = @_;
1.12      raeburn   142:     my (@types,@roles);
1.5       raeburn   143:     @types = ('active','future');
                    144:     @roles = ('st');
                    145:     my $idresult;
                    146:     my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12      raeburn   147:                         $udom,'userroles',\@types,\@roles);
                    148:     my %args = (
                    149:                 one_time => 1,
                    150:                );
1.5       raeburn   151:     foreach my $item (keys(%roleshash)) {
1.22      raeburn   152:         my ($cnum,$cdom,$role) = split(/:/,$item,-1);
1.5       raeburn   153:         my ($start,$end) = split(/:/,$roleshash{$item});
                    154:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   155:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
                    156:             my %coursehash = 
                    157:                 &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
                    158:             my $cdesc = $coursehash{'description'};
                    159:             if ($cdesc eq '') { 
                    160:                 $cdesc = $cdom.'_'.$cnum;
                    161:             }
1.5       raeburn   162:             if ($result eq 'ok') {
1.12      raeburn   163:                 $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   164:             } else {
1.12      raeburn   165:                 $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   166:             }
                    167:         }
                    168:     }
                    169:     return $idresult;
                    170: }
                    171: 
                    172: sub update_classlist {
1.63      raeburn   173:     my ($cdom,$cnum,$udom,$uname,$user,$newend) = @_;
1.6       albertel  174:     my ($uid,$classlistentry);
1.5       raeburn   175:     my $fullname =
                    176:         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
                    177:                                      $user->{'lastname'},$user->{'generation'},
                    178:                                      'lastname');
                    179:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
                    180:                                          $cdom,$cnum);
                    181:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
                    182:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
                    183:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
1.63      raeburn   184:     my $endidx = &Apache::loncoursedata::CL_END() - 2;
                    185:     my $startidx = &Apache::loncoursedata::CL_START() - 2;
1.5       raeburn   186:     for (my $i=0; $i<@classinfo; $i++) {
1.63      raeburn   187:         if ($i == $endidx) {
                    188:             if ($newend ne '') {
                    189:                 $classlistentry .= $newend.':';
                    190:             } else {
                    191:                 $classlistentry .= $classinfo[$i].':';
                    192:             }
                    193:         } elsif ($i == $startidx) {
                    194:             if ($newend ne '') {
                    195:                 if ($classinfo[$i] > $newend) {
                    196:                     $classlistentry .= $newend.':';
                    197:                 } else {
                    198:                     $classlistentry .= $classinfo[$i].':';
                    199:                 }
                    200:             } else {
                    201:                 $classlistentry .= $classinfo[$i].':';
                    202:             }
                    203:         } elsif ($i == $ididx) {
1.5       raeburn   204:             if (defined($user->{'id'})) {
                    205:                 $classlistentry .= $user->{'id'}.':';
                    206:             } else {
                    207:                 $classlistentry .= $classinfo[$i].':';
                    208:             }
                    209:         } elsif ($i == $nameidx) {
1.63      raeburn   210:             if (defined($user->{'lastname'})) {
                    211:                 $classlistentry .= $fullname.':';
                    212:             } else {
                    213:                 $classlistentry .= $classinfo[$i].':';
                    214:             }
1.5       raeburn   215:         } else {
                    216:             $classlistentry .= $classinfo[$i].':';
                    217:         }
                    218:     }
                    219:     $classlistentry =~ s/:$//;
                    220:     my $reply=&Apache::lonnet::cput('classlist',
                    221:                                     {"$uname:$udom" => $classlistentry},
                    222:                                     $cdom,$cnum);
                    223:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                    224:         return 'ok';
                    225:     } else {
                    226:         return 'error: '.$reply;
                    227:     }
                    228: }
                    229: 
                    230: 
1.1       raeburn   231: ###############################################################
                    232: ###############################################################
1.2       raeburn   233: # build a role type and role selection form
                    234: sub domain_roles_select {
                    235:     # Set up the role type and role selection boxes when in 
                    236:     # domain context   
                    237:     #
                    238:     # Role types
1.101     raeburn   239:     my @roletypes = ('domain','author','course','community');
1.2       raeburn   240:     my %lt = &role_type_names();
1.1       raeburn   241:     #
                    242:     # build up the menu information to be passed to
                    243:     # &Apache::loncommon::linked_select_forms
                    244:     my %select_menus;
1.2       raeburn   245:     if ($env{'form.roletype'} eq '') {
                    246:         $env{'form.roletype'} = 'domain';
                    247:     }
                    248:     foreach my $roletype (@roletypes) {
1.1       raeburn   249:         # set up the text for this domain
1.2       raeburn   250:         $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.102     raeburn   251:         my $crstype;
                    252:         if ($roletype eq 'community') {
                    253:             $crstype = 'Community';
                    254:         }
1.1       raeburn   255:         # we want a choice of 'default' as the default in the second menu
1.2       raeburn   256:         if ($env{'form.roletype'} ne '') {
                    257:             $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
                    258:         } else { 
                    259:             $select_menus{$roletype}->{'default'} = 'Any';
                    260:         }
1.1       raeburn   261:         # Now build up the other items in the second menu
1.2       raeburn   262:         my @roles;
                    263:         if ($roletype eq 'domain') {
                    264:             @roles = &domain_roles();
1.13      raeburn   265:         } elsif ($roletype eq 'author') {
1.2       raeburn   266:             @roles = &construction_space_roles();
                    267:         } else {
1.17      raeburn   268:             my $custom = 1;
1.101     raeburn   269:             @roles = &course_roles('domain',undef,$custom,$roletype);
1.1       raeburn   270:         }
1.2       raeburn   271:         my $order = ['Any',@roles];
                    272:         $select_menus{$roletype}->{'order'} = $order; 
                    273:         foreach my $role (@roles) {
1.5       raeburn   274:             if ($role eq 'cr') {
                    275:                 $select_menus{$roletype}->{'select2'}->{$role} =
                    276:                               &mt('Custom role');
                    277:             } else {
                    278:                 $select_menus{$roletype}->{'select2'}->{$role} = 
1.102     raeburn   279:                               &Apache::lonnet::plaintext($role,$crstype);
1.5       raeburn   280:             }
1.2       raeburn   281:         }
                    282:         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1       raeburn   283:     }
1.2       raeburn   284:     my $result = &Apache::loncommon::linked_select_forms
                    285:         ('studentform',('&nbsp;'x3).&mt('Role: '),$env{'form.roletype'},
1.101     raeburn   286:          'roletype','showrole',\%select_menus,
                    287:          ['domain','author','course','community']);
1.1       raeburn   288:     return $result;
                    289: }
                    290: 
                    291: ###############################################################
                    292: ###############################################################
                    293: sub hidden_input {
                    294:     my ($name,$value) = @_;
                    295:     return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
                    296: }
                    297: 
                    298: sub print_upload_manager_header {
1.123     raeburn   299:     my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype,
                    300:         $can_assign)=@_;
1.1       raeburn   301:     my $javascript;
                    302:     #
                    303:     if (! exists($env{'form.upfile_associate'})) {
                    304:         $env{'form.upfile_associate'} = 'forward';
                    305:     }
                    306:     if ($env{'form.associate'} eq 'Reverse Association') {
                    307:         if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                    308:             $env{'form.upfile_associate'} = 'reverse';
                    309:         } else {
                    310:             $env{'form.upfile_associate'} = 'forward';
                    311:         }
                    312:     }
                    313:     if ($env{'form.upfile_associate'} eq 'reverse') {
1.123     raeburn   314:         $javascript=&upload_manager_javascript_reverse_associate($can_assign);
1.1       raeburn   315:     } else {
1.123     raeburn   316:         $javascript=&upload_manager_javascript_forward_associate($can_assign);
1.1       raeburn   317:     }
                    318:     #
                    319:     # Deal with restored settings
                    320:     my $password_choice = '';
                    321:     if (exists($env{'form.ipwd_choice'}) &&
                    322:         $env{'form.ipwd_choice'} ne '') {
                    323:         # If a column was specified for password, assume it is for an
                    324:         # internal password.  This is a bug waiting to be filed (could be
                    325:         # local or krb auth instead of internal) but I do not have the
                    326:         # time to mess around with this now.
                    327:         $password_choice = 'int';
                    328:     }
                    329:     #
1.22      raeburn   330:     my $groupslist;
                    331:     if ($context eq 'course') {
                    332:         $groupslist = &get_groupslist();
                    333:     }
1.1       raeburn   334:     my $javascript_validations =
1.22      raeburn   335:         &javascript_validations('upload',$krbdefdom,$password_choice,undef,
                    336:                                 $env{'request.role.domain'},$context,
1.103     raeburn   337:                                 $groupslist,$crstype);
1.91      bisitz    338:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1.95      bisitz    339:     $r->print('<p>'
                    340:              .&mt('Total number of records found in file: [_1]'
                    341:                  ,'<b>'.$distotal.'</b>')
                    342:              ."</p>\n");
1.1       raeburn   343:     $r->print('<div class="LC_left_float"><h3>'.
                    344:               &mt('Identify fields in uploaded list')."</h3>\n");
                    345:     $r->print(&mt('Enter as many fields as you can.<br /> The system will inform you and bring you back to this page, <br /> if the data selected are insufficient to add users.')."<br />\n");
                    346:     $r->print(&hidden_input('action','upload').
                    347:               &hidden_input('state','got_file').
                    348:               &hidden_input('associate','').
                    349:               &hidden_input('datatoken',$datatoken).
                    350:               &hidden_input('fileupload',$env{'form.fileupload'}).
                    351:               &hidden_input('upfiletype',$env{'form.upfiletype'}).
                    352:               &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
1.86      bisitz    353:     $r->print('<br /><label><input type="checkbox" name="noFirstLine"'.$checked.' />'.
1.73      bisitz    354:               &mt('Ignore First Line').'</label><br />');
1.59      bisitz    355:     $r->print('<br /><input type="button" value="'.&mt('Reverse Association').'" '.
                    356:               'name="Reverse Association" '.
1.96      bisitz    357:               'onclick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
1.1       raeburn   358:     $r->print("<br /><br />\n".
                    359:               '<script type="text/javascript" language="Javascript">'."\n".
1.96      bisitz    360:               '// <![CDATA['."\n".
                    361:               $javascript."\n".$javascript_validations."\n".
                    362:               '// ]]>'."\n".
                    363:               '</script>');
1.1       raeburn   364: }
                    365: 
                    366: ###############################################################
                    367: ###############################################################
                    368: sub javascript_validations {
1.22      raeburn   369:     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
1.103     raeburn   370:         $context,$groupslist,$crstype)=@_;
1.22      raeburn   371:     my %param = (
                    372:                   kerb_def_dom => $krbdefdom,
                    373:                   curr_authtype => $curr_authtype,
                    374:                 );
1.37      raeburn   375:     if ($mode eq 'upload') {
1.22      raeburn   376:         $param{'formname'} = 'studentform';
1.1       raeburn   377:     } elsif ($mode eq 'createcourse') {
1.22      raeburn   378:         $param{'formname'} = 'ccrs';
1.1       raeburn   379:     } elsif ($mode eq 'modifycourse') {
1.22      raeburn   380:         $param{'formname'} = 'cmod';
                    381:         $param{'mode'} = 'modifycourse',
                    382:         $param{'curr_autharg'} = $curr_authfield;
                    383:     }
                    384: 
                    385:     my ($setsection_call,$setsections_js);
                    386:     my $finish = "  vf.submit();\n";
                    387:     if ($mode eq 'upload') {
                    388:         if (($context eq 'course') || ($context eq 'domain')) {
                    389:             if ($context eq 'course') {
                    390:                 if ($env{'request.course.sec'} eq '') {
1.109     raeburn   391:                     $setsection_call = 'setSections(document.'.$param{'formname'}.",'$crstype'".');';
1.22      raeburn   392:                     $setsections_js =
                    393:                         &setsections_javascript($param{'formname'},$groupslist,
1.103     raeburn   394:                                                 $mode,'',$crstype);
1.22      raeburn   395:                 } else {
                    396:                     $setsection_call = "'ok'";
                    397:                 }
                    398:             } elsif ($context eq 'domain') {
                    399:                 $setsection_call = 'setCourse()';
1.37      raeburn   400:                 $setsections_js = &dc_setcourse_js($param{'formname'},$mode,$context);
1.22      raeburn   401:             }
                    402:             $finish = "  var checkSec = $setsection_call\n".
                    403:                       "  if (checkSec == 'ok') {\n".
                    404:                       "      vf.submit();\n".
                    405:                       "   }\n";
                    406:         }
1.1       raeburn   407:     }
1.22      raeburn   408:     my $authheader = &Apache::loncommon::authform_header(%param);
1.1       raeburn   409: 
                    410:     my %alert = &Apache::lonlocal::texthash
                    411:         (username => 'You need to specify the username field.',
                    412:          authen   => 'You must choose an authentication type.',
                    413:          krb      => 'You need to specify the Kerberos domain.',
                    414:          ipass    => 'You need to specify the initial password.',
                    415:          name     => 'The optional name field was not specified.',
1.93      bisitz    416:          snum     => 'The optional student/employee ID field was not specified.',
1.1       raeburn   417:          section  => 'The optional section field was not specified.',
1.75      schafran  418:          email    => 'The optional e-mail address field was not specified.',
1.1       raeburn   419:          role     => 'The optional role field was not specified.',
1.57      raeburn   420:          domain   => 'The optional domain field was not specified.',
1.1       raeburn   421:          continue => 'Continue adding users?',
                    422:          );
1.84      raeburn   423:     if (($mode eq 'upload') && ($context eq 'domain')) {
                    424:         $alert{'inststatus'} = &mt('The optional affiliation field was not specified'); 
                    425:     }
1.37      raeburn   426:     my $function_name = <<"END";
1.22      raeburn   427: $setsections_js
                    428: 
1.84      raeburn   429: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus) {
1.1       raeburn   430: END
                    431:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($domain);
                    432:     my $auth_checks;
                    433:     if ($mode eq 'createcourse') {
                    434:         $auth_checks .= (<<END);
                    435:     if (vf.autoadds[0].checked == true) {
                    436:         if (current.radiovalue == null || current.radiovalue == 'nochange') {
                    437:             alert('$alert{'authen'}');
                    438:             return;
                    439:         }
                    440:     }
                    441: END
                    442:     } else {
                    443:         $auth_checks .= (<<END);
                    444:     var foundatype=0;
                    445:     if (founduname==0) {
                    446:         alert('$alert{'username'}');
                    447:         return;
                    448:     }
                    449: 
                    450: END
                    451:         if ($authnum > 1) {
                    452:             $auth_checks .= (<<END);
                    453:     if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
                    454:         // They did not check any of the login radiobuttons.
                    455:         alert('$alert{'authen'}');
                    456:         return;
                    457:     }
                    458: END
                    459:         }
                    460:     }
                    461:     if ($mode eq 'createcourse') {
                    462:         $auth_checks .= "
                    463:     if ( (vf.autoadds[0].checked == true) &&
                    464:          (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
                    465: ";
                    466:     } elsif ($mode eq 'modifycourse') {
                    467:         $auth_checks .= "
                    468:     if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
                    469: ";
                    470:     }
                    471:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    472:         $auth_checks .= (<<END);
                    473:         var alertmsg = '';
                    474:         switch (current.radiovalue) {
                    475:             case 'krb':
                    476:                 alertmsg = '$alert{'krb'}';
                    477:                 break;
                    478:             default:
                    479:                 alertmsg = '';
                    480:         }
                    481:         if (alertmsg != '') {
                    482:             alert(alertmsg);
                    483:             return;
                    484:         }
                    485:     }
                    486: END
                    487:     } else {
                    488:         $auth_checks .= (<<END);
                    489:     foundatype=1;
                    490:     if (current.argfield == null || current.argfield == '') {
                    491:         var alertmsg = '';
1.38      raeburn   492:         switch (current.radiovalue) {
1.1       raeburn   493:             case 'krb':
                    494:                 alertmsg = '$alert{'krb'}';
                    495:                 break;
                    496:             case 'loc':
                    497:             case 'fsys':
                    498:                 alertmsg = '$alert{'ipass'}';
                    499:                 break;
                    500:             case 'fsys':
                    501:                 alertmsg = '';
                    502:                 break;
                    503:             default:
                    504:                 alertmsg = '';
                    505:         }
                    506:         if (alertmsg != '') {
                    507:             alert(alertmsg);
                    508:             return;
                    509:         }
                    510:     }
                    511: END
                    512:     }
                    513:     my $section_checks;
                    514:     my $optional_checks = '';
                    515:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    516:         $optional_checks = (<<END);
                    517:     vf.submit();
                    518: }
                    519: END
                    520:     } else {
                    521:         $section_checks = &section_check_js();
                    522:         $optional_checks = (<<END);
                    523:     var message='';
                    524:     if (foundname==0) {
                    525:         message='$alert{'name'}';
                    526:     }
                    527:     if (foundid==0) {
                    528:         if (message!='') {
                    529:             message+='\\n';
                    530:         }
                    531:         message+='$alert{'snum'}';
                    532:     }
                    533:     if (foundsec==0) {
                    534:         if (message!='') {
                    535:             message+='\\n';
                    536:         }
1.130     raeburn   537:         message+='$alert{'section'}';
1.1       raeburn   538:     }
                    539:     if (foundemail==0) {
                    540:         if (message!='') {
                    541:             message+='\\n';
                    542:         }
                    543:         message+='$alert{'email'}';
                    544:     }
1.57      raeburn   545:     if (foundrole==0) {
                    546:         if (message!='') {
                    547:             message+='\\n';
                    548:         }
                    549:         message+='$alert{'role'}';
                    550:     }
                    551:     if (founddomain==0) {
                    552:         if (message!='') {
                    553:             message+='\\n';
                    554:         }
                    555:         message+='$alert{'domain'}';
                    556:     }
1.84      raeburn   557: END
                    558:         if (($mode eq 'upload') && ($context eq 'domain')) {
                    559:             $optional_checks .= (<<END);
                    560: 
                    561:     if (foundinststatus==0) {
                    562:         if (message!='') {
                    563:             message+='\\n';
                    564:         }
                    565:         message+='$alert{'inststatus'}';
                    566:     }
                    567: END
                    568:         }
                    569:         $optional_checks .= (<<END);
                    570: 
1.1       raeburn   571:     if (message!='') {
                    572:         message+= '\\n$alert{'continue'}';
                    573:         if (confirm(message)) {
                    574:             vf.state.value='enrolling';
1.22      raeburn   575:             $finish
1.1       raeburn   576:         }
                    577:     } else {
                    578:         vf.state.value='enrolling';
1.22      raeburn   579:         $finish
1.1       raeburn   580:     }
                    581: }
                    582: END
                    583:     }
1.37      raeburn   584:     my $result = $function_name.$auth_checks.$optional_checks."\n".
                    585:                  $section_checks.$authheader;
1.1       raeburn   586:     return $result;
                    587: }
                    588: ###############################################################
                    589: ###############################################################
                    590: sub upload_manager_javascript_forward_associate {
1.123     raeburn   591:     my ($can_assign) = @_;
1.132     raeburn   592:     my ($auth_update,$numbuttons,$argreset);
1.123     raeburn   593:     if (ref($can_assign) eq 'HASH') {
1.132     raeburn   594:         if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
                    595:             $argreset .= "      vf.krbarg.value='';\n";
                    596:             $numbuttons ++ ;
                    597:         }
                    598:         if ($can_assign->{'int'}) {
                    599:             $argreset .= "      vf.intarg.value='';\n";
                    600:             $numbuttons ++;
                    601:         }
                    602:         if ($can_assign->{'loc'}) {
                    603:             $argreset .= "      vf.locarg.value='';\n";
                    604:             $numbuttons ++;
                    605:         }
                    606:         if (!$can_assign->{'int'}) {
                    607:             my $warning = &mt('You may not specify an initial password for each user, as this is only available when new users use LON-CAPA internal authentication.\n').
                    608:                           &mt('Your current role does not have rights to create users with that authentication type.');
                    609:             $auth_update = <<"END";
                    610:    // Currently the initial password field is only supported for internal auth
                    611:    // (see bug 6368).
                    612:    if (nw==9) {
                    613:        eval('vf.f'+tf+'.selectedIndex=0;')
                    614:        alert('$warning');
                    615:    }
                    616: END
                    617:         } elsif ($numbuttons > 1) {
1.123     raeburn   618:             $auth_update = <<"END";
                    619:    // If we set the password, make the password form below correspond to
                    620:    // the new value.
                    621:    if (nw==9) {
                    622:       changed_radio('int',document.studentform);
                    623:       set_auth_radio_buttons('int',document.studentform);
1.132     raeburn   624: $argreset
                    625:    }
                    626: 
1.123     raeburn   627: END
                    628:         }
                    629:     }
                    630: 
1.1       raeburn   631:     return(<<ENDPICK);
                    632: function verify(vf,sec_caller) {
                    633:     var founduname=0;
                    634:     var foundpwd=0;
                    635:     var foundname=0;
                    636:     var foundid=0;
                    637:     var foundsec=0;
                    638:     var foundemail=0;
                    639:     var foundrole=0;
1.57      raeburn   640:     var founddomain=0;
1.84      raeburn   641:     var foundinststatus=0;
1.1       raeburn   642:     var tw;
                    643:     for (i=0;i<=vf.nfields.value;i++) {
                    644:         tw=eval('vf.f'+i+'.selectedIndex');
                    645:         if (tw==1) { founduname=1; }
                    646:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    647:         if (tw==7) { foundid=1; }
                    648:         if (tw==8) { foundsec=1; }
                    649:         if (tw==9) { foundpwd=1; }
                    650:         if (tw==10) { foundemail=1; }
                    651:         if (tw==11) { foundrole=1; }
1.57      raeburn   652:         if (tw==12) { founddomain=1; }
1.84      raeburn   653:         if (tw==13) { foundinststatus=1; }
1.1       raeburn   654:     }
1.84      raeburn   655:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
1.1       raeburn   656: }
                    657: 
                    658: //
                    659: // vf = this.form
                    660: // tf = column number
                    661: //
                    662: // values of nw
                    663: //
                    664: // 0 = none
                    665: // 1 = username
                    666: // 2 = names (lastname, firstnames)
                    667: // 3 = fname (firstname)
                    668: // 4 = mname (middlename)
                    669: // 5 = lname (lastname)
                    670: // 6 = gen   (generation)
                    671: // 7 = id
                    672: // 8 = section
                    673: // 9 = ipwd  (password)
                    674: // 10 = email address
                    675: // 11 = role
1.57      raeburn   676: // 12 = domain
1.84      raeburn   677: // 13 = inststatus
1.1       raeburn   678: 
                    679: function flip(vf,tf) {
                    680:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    681:    var i;
                    682:    // make sure no other columns are labeled the same as this one
                    683:    for (i=0;i<=vf.nfields.value;i++) {
                    684:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    685:           eval('vf.f'+i+'.selectedIndex=0;')
                    686:       }
                    687:    }
                    688:    // If we set this to 'lastname, firstnames', clear out all the ones
                    689:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
                    690:    if (nw==2) {
                    691:       for (i=0;i<=vf.nfields.value;i++) {
                    692:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    693:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    694:              eval('vf.f'+i+'.selectedIndex=0;')
                    695:          }
                    696:       }
                    697:    }
                    698:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    699:    // clear out any that are set to 'lastname, firstnames' (2)
                    700:    if ((nw>=3) && (nw<=6)) {
                    701:       for (i=0;i<=vf.nfields.value;i++) {
                    702:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    703:              eval('vf.f'+i+'.selectedIndex=0;')
                    704:          }
                    705:       }
                    706:    }
1.123     raeburn   707:    $auth_update
1.1       raeburn   708: }
                    709: 
                    710: function clearpwd(vf) {
                    711:     var i;
                    712:     for (i=0;i<=vf.nfields.value;i++) {
                    713:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    714:             eval('vf.f'+i+'.selectedIndex=0;')
                    715:         }
                    716:     }
                    717: }
                    718: 
                    719: ENDPICK
                    720: }
                    721: 
                    722: ###############################################################
                    723: ###############################################################
                    724: sub upload_manager_javascript_reverse_associate {
1.123     raeburn   725:     my ($can_assign) = @_;
1.132     raeburn   726:     my ($auth_update,$numbuttons,$argreset);
1.123     raeburn   727:     if (ref($can_assign) eq 'HASH') {
1.132     raeburn   728:         if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
                    729:             $argreset .= "      vf.krbarg.value='';\n";
                    730:             $numbuttons ++ ;
                    731:         }
                    732:         if ($can_assign->{'int'}) {
                    733:             $argreset .= "      vf.intarg.value='';\n";
                    734:             $numbuttons ++;
                    735:         }
                    736:         if ($can_assign->{'loc'}) {
                    737:             $argreset .= "      vf.locarg.value='';\n";
                    738:             $numbuttons ++;
                    739:         }
                    740:         if (!$can_assign->{'int'}) {
                    741:             my $warning = &mt('You may not specify an initial password, as this is only available when new users use LON-CAPA internal authentication.\n').
                    742:                           &mt('Your current role does not have rights to create users with that authentication type.');
                    743:             $auth_update = <<"END";
                    744:    // Currently the initial password field is only supported for internal auth
                    745:    // (see bug 6368).
                    746:    if (tf==8 && nw!=0) {
                    747:        eval('vf.f'+tf+'.selectedIndex=0;')
                    748:        alert('$warning');
                    749:    }
                    750: END
                    751:         } elsif ($numbuttons > 1) {
1.123     raeburn   752:             $auth_update = <<"END";
                    753:    // initial password specified, pick internal authentication
                    754:    if (tf==8 && nw!=0) {
                    755:       changed_radio('int',document.studentform);
                    756:       set_auth_radio_buttons('int',document.studentform);
1.132     raeburn   757: $argreset
                    758:    }
                    759: 
1.123     raeburn   760: END
                    761:         }
                    762:     }
1.132     raeburn   763: 
1.1       raeburn   764:     return(<<ENDPICK);
                    765: function verify(vf,sec_caller) {
                    766:     var founduname=0;
                    767:     var foundpwd=0;
                    768:     var foundname=0;
                    769:     var foundid=0;
                    770:     var foundsec=0;
1.131     raeburn   771:     var foundemail=0;
1.1       raeburn   772:     var foundrole=0;
1.57      raeburn   773:     var founddomain=0;
1.84      raeburn   774:     var foundinststatus=0;
1.1       raeburn   775:     var tw;
                    776:     for (i=0;i<=vf.nfields.value;i++) {
                    777:         tw=eval('vf.f'+i+'.selectedIndex');
                    778:         if (i==0 && tw!=0) { founduname=1; }
                    779:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    780:         if (i==6 && tw!=0) { foundid=1; }
                    781:         if (i==7 && tw!=0) { foundsec=1; }
                    782:         if (i==8 && tw!=0) { foundpwd=1; }
1.130     raeburn   783:         if (i==9 && tw!=0) { foundemail=1; }
                    784:         if (i==10 && tw!=0) { foundrole=1; }
                    785:         if (i==11 && tw!=0) { founddomain=1; }
                    786:         if (i==12 && tw!=0) { foundinstatus=1; }
1.1       raeburn   787:     }
1.130     raeburn   788:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
1.1       raeburn   789: }
                    790: 
                    791: function flip(vf,tf) {
                    792:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    793:    var i;
                    794:    // picked the all one name field, reset the other name ones to blank
                    795:    if (tf==1 && nw!=0) {
                    796:       for (i=2;i<=5;i++) {
                    797:          eval('vf.f'+i+'.selectedIndex=0;')
                    798:       }
                    799:    }
                    800:    //picked one of the piecewise name fields, reset the all in
                    801:    //one field to blank
                    802:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    803:       eval('vf.f1.selectedIndex=0;')
                    804:    }
1.123     raeburn   805:    $auth_update
1.1       raeburn   806: }
                    807: 
                    808: function clearpwd(vf) {
                    809:     var i;
                    810:     if (eval('vf.f8.selectedIndex')!=0) {
                    811:         eval('vf.f8.selectedIndex=0;')
                    812:     }
                    813: }
                    814: ENDPICK
                    815: }
                    816: 
                    817: ###############################################################
                    818: ###############################################################
                    819: sub print_upload_manager_footer {
1.101     raeburn   820:     my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission,$crstype) = @_;
1.22      raeburn   821:     my $form = 'document.studentform';
                    822:     my $formname = 'studentform';
1.1       raeburn   823:     my ($krbdef,$krbdefdom) =
                    824:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.22      raeburn   825:     my %param = ( formname => $form,
1.1       raeburn   826:                   kerb_def_dom => $krbdefdom,
                    827:                   kerb_def_auth => $krbdef
                    828:                   );
                    829:     if (exists($env{'form.ipwd_choice'}) &&
                    830:         defined($env{'form.ipwd_choice'}) &&
                    831:         $env{'form.ipwd_choice'} ne '') {
                    832:         $param{'curr_authtype'} = 'int';
                    833:     }
                    834:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    835:     my $intform = &Apache::loncommon::authform_internal(%param);
                    836:     my $locform = &Apache::loncommon::authform_local(%param);
1.22      raeburn   837:     my $date_table = &date_setting_table(undef,undef,$context,undef,
1.101     raeburn   838:                                          $formname,$permission,$crstype);
1.95      bisitz    839: 
1.1       raeburn   840:     my $Str = "\n".'<div class="LC_left_float">';
                    841:     $Str .= &hidden_input('nfields',$i);
                    842:     $Str .= &hidden_input('keyfields',$keyfields);
1.95      bisitz    843: 
                    844:     $Str .= '<h3>'.&mt('Options').'</h3>'
                    845:            .&Apache::lonhtmlcommon::start_pick_box();
                    846: 
                    847:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Login Type'));
1.1       raeburn   848:     if ($context eq 'domain') {
1.95      bisitz    849:         $Str .= '<p>'
                    850:                .&mt('Change authentication for existing users in domain "[_1]" to these settings?'
                    851:                    ,$defdom)
                    852:                .'&nbsp;<span class="LC_nobreak"><label>'
                    853:                .'<input type="radio" name="changeauth" value="No" checked="checked" />'
                    854:                .&mt('No').'</label>'
                    855:                .'&nbsp;&nbsp;<label>'
                    856:                .'<input type="radio" name="changeauth" value="Yes" />'
                    857:                .&mt('Yes').'</label>'
                    858:                .'</span></p>'; 
1.1       raeburn   859:     } else {
1.95      bisitz    860:         $Str .= '<p class="LC_info">'."\n".
                    861:             &mt('This will not take effect if the user already exists.').
1.1       raeburn   862:             &Apache::loncommon::help_open_topic('Auth_Options').
                    863:             "</p>\n";
                    864:     }
1.97      raeburn   865:     $Str .= &set_login($defdom,$krbform,$intform,$locform);
1.95      bisitz    866: 
1.1       raeburn   867:     my ($home_server_pick,$numlib) =
                    868:         &Apache::loncommon::home_server_form_item($defdom,'lcserver',
                    869:                                                   'default','hide');
                    870:     if ($numlib > 1) {
1.97      raeburn   871:         $Str .= &Apache::lonhtmlcommon::row_closure()
                    872:                .&Apache::lonhtmlcommon::row_title(
1.95      bisitz    873:                     &mt('LON-CAPA Home Server for New Users'))
                    874:                .&mt('LON-CAPA domain: [_1] with home server:','"'.$defdom.'"')
                    875:                .$home_server_pick
                    876:                .&Apache::lonhtmlcommon::row_closure();
                    877:     } else {
1.97      raeburn   878:         $Str .= $home_server_pick.
                    879:                 &Apache::lonhtmlcommon::row_closure();
1.95      bisitz    880:     }
                    881: 
                    882:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default domain'))
                    883:            .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1)
                    884:            .&Apache::lonhtmlcommon::row_closure();
                    885: 
                    886:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Starting and Ending Dates'))
                    887:            ."<p>\n".$date_table."</p>\n"
                    888:            .&Apache::lonhtmlcommon::row_closure();
                    889: 
1.1       raeburn   890:     if ($context eq 'domain') {
1.95      bisitz    891:         $Str .= &Apache::lonhtmlcommon::row_title(
                    892:                     &mt('Settings for assigning roles'))
                    893:                .&mt('Pick the action to take on roles for these users:').'<br />'
                    894:                .'<span class="LC_nobreak"><label>'
                    895:                .'<input type="radio" name="roleaction" value="norole" checked="checked" />'
                    896:                .'&nbsp;'.&mt('No role changes').'</label>'
                    897:                .'&nbsp;&nbsp;&nbsp;<label>'
                    898:                .'<input type="radio" name="roleaction" value="domain" />'
                    899:                .'&nbsp;'.&mt('Add a domain role').'</label>'
                    900:                .'&nbsp;&nbsp;&nbsp;<label>'
                    901:                .'<input type="radio" name="roleaction" value="course" />'
1.103     raeburn   902:                .'&nbsp;'.&mt('Add a course/community role').'</label>'
1.95      bisitz    903:                .'</span>';
                    904:     } elsif ($context eq 'author') {
                    905:         $Str .= &Apache::lonhtmlcommon::row_title(
                    906:                     &mt('Default role'))
                    907:                .&mt('Choose the role to assign to users without a value specified in the uploaded file.')
1.1       raeburn   908:     } elsif ($context eq 'course') {
1.95      bisitz    909:         $Str .= &Apache::lonhtmlcommon::row_title(
                    910:                     &mt('Default role and section'))
                    911:                .&mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file.');
                    912:     } else {
                    913:         $Str .= &Apache::lonhtmlcommon::row_title(
                    914:                     &mt('Default role and/or section(s)'))
                    915:                .&mt('Role and/or section(s) for users without values specified in the uploaded file.');
1.1       raeburn   916:     }
1.22      raeburn   917:     if (($context eq 'domain') || ($context eq 'author')) {
1.95      bisitz    918:         $Str .= '<br />';
1.22      raeburn   919:         my ($options,$cb_script,$coursepick) = &default_role_selector($context,1);
                    920:         if ($context eq 'domain') {
1.95      bisitz    921:             $Str .= '<p>'
                    922:                    .'<b>'.&mt('Domain Level').'</b><br />'
                    923:                    .$options
                    924:                    .'</p><p>'
                    925:                    .'<b>'.&mt('Course Level').'</b>'
                    926:                    .'</p>'
                    927:                    .$cb_script.$coursepick
                    928:                    .&Apache::lonhtmlcommon::row_closure();
1.22      raeburn   929:         } elsif ($context eq 'author') {
1.95      bisitz    930:             $Str .= $options
                    931:                    .&Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.22      raeburn   932:         }
1.1       raeburn   933:     } else {
1.22      raeburn   934:         my ($cnum,$cdom) = &get_course_identity();
                    935:         my $rowtitle = &mt('section');
                    936:         my $secbox = &section_picker($cdom,$cnum,'Any',$rowtitle,
1.101     raeburn   937:                                      $permission,$context,'upload',$crstype);
1.95      bisitz    938:         $Str .= $secbox
                    939:                .&Apache::lonhtmlcommon::row_closure();
1.101     raeburn   940:         my %lt;
                    941:         if ($crstype eq 'Community') {
                    942:             %lt = &Apache::lonlocal::texthash (
                    943:                     disp => 'Display members with current/future access who are not in the uploaded file',
                    944:                     stus => 'Members selected from this list can be dropped.'
                    945:             );
                    946:         } else {
                    947:             %lt = &Apache::lonlocal::texthash (
                    948:                     disp => 'Display students with current/future access who are not in the uploaded file',
                    949:                     stus => 'Students selected from this list can be dropped.'
                    950:             );
                    951:         }
1.95      bisitz    952:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Full Update'))
1.101     raeburn   953:                .'<label><input type="checkbox" name="fullup" value="yes" />'
                    954:                .' '.$lt{'disp'}
1.95      bisitz    955:                .'</label><br />'
1.101     raeburn   956:                .$lt{'stus'}
1.95      bisitz    957:                .&Apache::lonhtmlcommon::row_closure();
1.1       raeburn   958:     }
1.5       raeburn   959:     if ($context eq 'course' || $context eq 'domain') {
                    960:         $Str .= &forceid_change($context);
                    961:     }
1.95      bisitz    962: 
                    963:     $Str .= &Apache::lonhtmlcommon::end_pick_box();
1.73      bisitz    964:     $Str .= '</div>';
1.95      bisitz    965: 
                    966:     # Footer
                    967:     $Str .= '<div class="LC_clear_float_footer">'
                    968:            .'<hr />';
1.1       raeburn   969:     if ($context eq 'course') {
1.95      bisitz    970:         $Str .= '<p class="LC_info">'
1.103     raeburn   971:                .&mt('Note: This operation may be time consuming when adding several users.')
1.95      bisitz    972:                .'</p>';
1.73      bisitz    973:     }
1.95      bisitz    974:     $Str .= '<p><input type="button"'
1.96      bisitz    975:            .' onclick="javascript:verify(this.form,this.form.csec)"'
                    976:            .' value="'.&mt('Update Users').'" />'
1.95      bisitz    977:            .'</p>'."\n"
1.73      bisitz    978:            .'</div>';
1.1       raeburn   979:     $r->print($Str);
                    980:     return;
                    981: }
                    982: 
1.5       raeburn   983: sub forceid_change {
                    984:     my ($context) = @_;
                    985:     my $output = 
1.95      bisitz    986:         &Apache::lonhtmlcommon::row_title(&mt('Student/Employee ID'))
                    987:        .'<label><input type="checkbox" name="forceid" value="yes" />'
                    988:        .&mt('Disable Student/Employee ID Safeguard and force change of conflicting IDs')
                    989:        .'</label><br />'."\n"
                    990:        .&mt('(only do if you know what you are doing.)')."\n";
1.5       raeburn   991:     if ($context eq 'domain') {
1.25      raeburn   992:         $output .= '<br /><label><input type="checkbox" name="recurseid"'.
1.86      bisitz    993:                    ' value="yes" />'. 
1.93      bisitz    994:   &mt('Update student/employee ID in courses in which user is active/future student,[_1](if forcing change).','<br />').
1.25      raeburn   995:                    '</label>'."\n";
1.5       raeburn   996:     }
1.95      bisitz    997:     $output .= &Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.5       raeburn   998:     return $output;
                    999: }
                   1000: 
1.1       raeburn  1001: ###############################################################
                   1002: ###############################################################
                   1003: sub print_upload_manager_form {
1.101     raeburn  1004:     my ($r,$context,$permission,$crstype) = @_;
1.1       raeburn  1005:     my $firstLine;
                   1006:     my $datatoken;
                   1007:     if (!$env{'form.datatoken'}) {
                   1008:         $datatoken=&Apache::loncommon::upfile_store($r);
                   1009:     } else {
                   1010:         $datatoken=$env{'form.datatoken'};
                   1011:         &Apache::loncommon::load_tmp_file($r);
                   1012:     }
                   1013:     my @records=&Apache::loncommon::upfile_record_sep();
                   1014:     if($env{'form.noFirstLine'}){
                   1015:         $firstLine=shift(@records);
                   1016:     }
                   1017:     my $total=$#records;
                   1018:     my $distotal=$total+1;
                   1019:     my $today=time;
                   1020:     my $halfyear=$today+15552000;
                   1021:     #
                   1022:     # Restore memorized settings
                   1023:     my $col_setting_names =  { 'username_choice' => 'scalar', # column settings
                   1024:                                'names_choice' => 'scalar',
                   1025:                                'fname_choice' => 'scalar',
                   1026:                                'mname_choice' => 'scalar',
                   1027:                                'lname_choice' => 'scalar',
                   1028:                                'gen_choice' => 'scalar',
                   1029:                                'id_choice' => 'scalar',
                   1030:                                'sec_choice' => 'scalar',
                   1031:                                'ipwd_choice' => 'scalar',
                   1032:                                'email_choice' => 'scalar',
                   1033:                                'role_choice' => 'scalar',
1.57      raeburn  1034:                                'domain_choice' => 'scalar',
1.84      raeburn  1035:                                'inststatus_choice' => 'scalar',
1.1       raeburn  1036:                              };
                   1037:     my $defdom = $env{'request.role.domain'};
                   1038:     if ($context eq 'course') {
                   1039:         &Apache::loncommon::restore_course_settings('enrollment_upload',
                   1040:                                                     $col_setting_names);
                   1041:     } else {
                   1042:         &Apache::loncommon::restore_settings($context,'user_upload',
                   1043:                                              $col_setting_names);
                   1044:     }
                   1045:     #
                   1046:     # Determine kerberos parameters as appropriate
                   1047:     my ($krbdef,$krbdefdom) =
                   1048:         &Apache::loncommon::get_kerberos_defaults($defdom);
                   1049:     #
1.123     raeburn  1050:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($defdom);
1.22      raeburn  1051:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
1.123     raeburn  1052:                                  $permission,$crstype,\%can_assign);
1.1       raeburn  1053:     my $i;
                   1054:     my $keyfields;
                   1055:     if ($total>=0) {
                   1056:         my @field=
                   1057:             (['username',&mt('Username'),     $env{'form.username_choice'}],
                   1058:              ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
                   1059:              ['fname',&mt('First Name'),      $env{'form.fname_choice'}],
                   1060:              ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
                   1061:              ['lname',&mt('Last Name'),       $env{'form.lname_choice'}],
                   1062:              ['gen',  &mt('Generation'),      $env{'form.gen_choice'}],
1.61      bisitz   1063:              ['id',   &mt('Student/Employee ID'),$env{'form.id_choice'}],
1.1       raeburn  1064:              ['sec',  &mt('Section'),          $env{'form.sec_choice'}],
                   1065:              ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
                   1066:              ['email',&mt('E-mail Address'),   $env{'form.email_choice'}],
1.57      raeburn  1067:              ['role',&mt('Role'),             $env{'form.role_choice'}],
1.84      raeburn  1068:              ['domain',&mt('Domain'),         $env{'form.domain_choice'}],
                   1069:              ['inststatus',&mt('Affiliation'), $env{'form.inststatus_choice'}]);
1.1       raeburn  1070:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   1071:             &Apache::loncommon::csv_print_samples($r,\@records);
                   1072:             $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1073:                                                           \@field);
                   1074:             foreach (@field) {
                   1075:                 $keyfields.=$_->[0].',';
                   1076:             }
                   1077:             chop($keyfields);
                   1078:         } else {
                   1079:             unshift(@field,['none','']);
                   1080:             $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1081:                                                             \@field);
                   1082:             my %sone=&Apache::loncommon::record_sep($records[0]);
                   1083:             $keyfields=join(',',sort(keys(%sone)));
                   1084:         }
                   1085:     }
                   1086:     $r->print('</div>');
                   1087:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1.101     raeburn  1088:                                  $context,$permission,$crstype);
1.1       raeburn  1089: }
                   1090: 
                   1091: sub setup_date_selectors {
1.22      raeburn  1092:     my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
                   1093:     if ($formname eq '') {
                   1094:         $formname = 'studentform';
                   1095:     }
1.1       raeburn  1096:     if (! defined($starttime)) {
                   1097:         $starttime = time;
                   1098:         unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1099:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1100:                             '.default_enrollment_start_date'})) {
                   1101:                 $starttime = $env{'course.'.$env{'request.course.id'}.
                   1102:                                   '.default_enrollment_start_date'};
                   1103:             }
                   1104:         }
                   1105:     }
                   1106:     if (! defined($endtime)) {
                   1107:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                   1108:         unless ($mode eq 'createcourse') {
                   1109:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1110:                             '.default_enrollment_end_date'})) {
                   1111:                 $endtime = $env{'course.'.$env{'request.course.id'}.
                   1112:                                 '.default_enrollment_end_date'};
                   1113:             }
                   1114:         }
                   1115:     }
1.11      raeburn  1116: 
                   1117:     my $startdateform = 
1.22      raeburn  1118:         &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11      raeburn  1119:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1120: 
                   1121:     my $enddateform = 
1.22      raeburn  1122:         &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11      raeburn  1123:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1124: 
1.1       raeburn  1125:     if ($mode eq 'create_enrolldates') {
                   1126:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1127:                                                             'startenroll',
                   1128:                                                             $starttime);
                   1129:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1130:                                                           'endenroll',
                   1131:                                                           $endtime);
                   1132:     }
                   1133:     if ($mode eq 'create_defaultdates') {
                   1134:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1135:                                                             'startaccess',
                   1136:                                                             $starttime);
                   1137:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1138:                                                           'endaccess',
                   1139:                                                           $endtime);
                   1140:     }
                   1141:     return ($startdateform,$enddateform);
                   1142: }
                   1143: 
                   1144: 
                   1145: sub get_dates_from_form {
1.54      raeburn  1146:     my ($startname,$endname) = @_;
                   1147:     if ($startname eq '') {
                   1148:         $startname = 'startdate';
                   1149:     }
                   1150:     if ($endname eq '') {
                   1151:         $endname = 'enddate';
                   1152:     }
                   1153:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   1154:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.1       raeburn  1155:     if ($env{'form.no_end_date'}) {
                   1156:         $enddate = 0;
                   1157:     }
                   1158:     return ($startdate,$enddate);
                   1159: }
                   1160: 
                   1161: sub date_setting_table {
1.101     raeburn  1162:     my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission,$crstype) = @_;
1.11      raeburn  1163:     my $nolink;
                   1164:     if ($bulkaction) {
                   1165:         $nolink = 1;
                   1166:     }
                   1167:     my ($startform,$endform) = 
1.22      raeburn  1168:         &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1       raeburn  1169:     my $dateDefault;
                   1170:     if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1171:         $dateDefault = '&nbsp;';
1.13      raeburn  1172:     } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11      raeburn  1173:         if (($bulkaction eq 'reenable') || 
                   1174:             ($bulkaction eq 'activate') || 
1.22      raeburn  1175:             ($bulkaction eq 'chgdates') ||
                   1176:             ($env{'form.action'} eq 'upload')) {
                   1177:             if ($env{'request.course.sec'} eq '') {
                   1178:                 $dateDefault = '<span class="LC_nobreak">'.
1.101     raeburn  1179:                     '<label><input type="checkbox" name="makedatesdefault" value="1" /> ';
                   1180:                 if ($crstype eq 'Community') {
                   1181:                     $dateDefault .= &mt("make these dates the default access dates for future community enrollment");
                   1182:                 } else {
                   1183:                     $dateDefault .= &mt("make these dates the default access dates for future course enrollment");
                   1184:                 }
                   1185:                 $dateDefault .= '</label></span>';
1.22      raeburn  1186:             }
1.11      raeburn  1187:         }
1.1       raeburn  1188:     }
1.11      raeburn  1189:     my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1       raeburn  1190:     if (defined($endtime) && $endtime == 0) {
1.70      bisitz   1191:         $perpetual .= ' checked="checked"';
1.1       raeburn  1192:     }
1.11      raeburn  1193:     $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1       raeburn  1194:     if ($mode eq 'create_enrolldates') {
                   1195:         $perpetual = '&nbsp;';
                   1196:     }
1.11      raeburn  1197:     my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
                   1198:     $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
                   1199:                                                      'LC_oddrow_value')."\n".
                   1200:                $startform."\n".
                   1201:                &Apache::lonhtmlcommon::row_closure(1).
                   1202:                &Apache::lonhtmlcommon::row_title(&mt('Ending Date'), 
                   1203:                                                      'LC_oddrow_value')."\n".
                   1204:                $endform.'&nbsp;'.$perpetual.
                   1205:                &Apache::lonhtmlcommon::row_closure(1).
1.22      raeburn  1206:                &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn  1207:     if ($dateDefault) {
                   1208:         $result .=  $dateDefault.'<br />'."\n";
                   1209:     }
                   1210:     return $result;
                   1211: }
                   1212: 
                   1213: sub make_dates_default {
1.101     raeburn  1214:     my ($startdate,$enddate,$context,$crstype) = @_;
1.1       raeburn  1215:     my $result = '';
                   1216:     if ($context eq 'course') {
1.17      raeburn  1217:         my ($cnum,$cdom) = &get_course_identity();
1.1       raeburn  1218:         my $put_result = &Apache::lonnet::put('environment',
                   1219:                 {'default_enrollment_start_date'=>$startdate,
1.17      raeburn  1220:                  'default_enrollment_end_date'  =>$enddate},$cdom,$cnum);
1.1       raeburn  1221:         if ($put_result eq 'ok') {
1.101     raeburn  1222:             if ($crstype eq 'Community') {
                   1223:                 $result .= &mt('Set default start and end access dates for community.');
                   1224:             } else {
                   1225:                 $result .= &mt('Set default start and end access dates for course.');
                   1226:             }
                   1227:             $result .= '<br />'."\n";
1.1       raeburn  1228:             #
                   1229:             # Refresh the course environment
                   1230:             &Apache::lonnet::coursedescription($env{'request.course.id'},
                   1231:                                                {'freshen_cache' => 1});
                   1232:         } else {
1.101     raeburn  1233:             if ($crstype eq 'Community') {
                   1234:                 $result .= &mt('Unable to set default access dates for community');
                   1235:             } else {
                   1236:                 $result .= &mt('Unable to set default access dates for course');
                   1237:             }
                   1238:             $result .= ':'.$put_result.'<br />';
1.1       raeburn  1239:         }
                   1240:     }
                   1241:     return $result;
                   1242: }
                   1243: 
                   1244: sub default_role_selector {
1.101     raeburn  1245:     my ($context,$checkpriv,$crstype) = @_;
1.1       raeburn  1246:     my %customroles;
                   1247:     my ($options,$coursepick,$cb_jscript);
1.13      raeburn  1248:     if ($context ne 'author') {
1.104     raeburn  1249:         %customroles = &my_custom_roles($crstype);
1.1       raeburn  1250:     }
                   1251: 
                   1252:     my %lt=&Apache::lonlocal::texthash(
                   1253:                     'rol'  => "Role",
                   1254:                     'grs'  => "Section",
                   1255:                     'exs'  => "Existing sections",
                   1256:                     'new'  => "New section",
                   1257:                   );
                   1258:     $options = '<select name="defaultrole">'."\n".
                   1259:                ' <option value="">'.&mt('Please select').'</option>'."\n"; 
                   1260:     if ($context eq 'course') {
1.101     raeburn  1261:         $options .= &default_course_roles($context,$checkpriv,$crstype,%customroles);
1.13      raeburn  1262:     } elsif ($context eq 'author') {
1.2       raeburn  1263:         my @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1264:         foreach my $role (@roles) {
                   1265:            my $plrole=&Apache::lonnet::plaintext($role);
                   1266:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1267:         }
                   1268:     } elsif ($context eq 'domain') {
1.2       raeburn  1269:         my @roles = &domain_roles($checkpriv);
1.1       raeburn  1270:         foreach my $role (@roles) {
                   1271:            my $plrole=&Apache::lonnet::plaintext($role);
                   1272:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1273:         }
                   1274:         my $courseform = &Apache::loncommon::selectcourse_link
1.103     raeburn  1275:             ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course/Community');
1.1       raeburn  1276:         $cb_jscript = 
1.103     raeburn  1277:             &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform','courserole','Course/Community');
1.1       raeburn  1278:         $coursepick = &Apache::loncommon::start_data_table().
                   1279:                       &Apache::loncommon::start_data_table_header_row().
                   1280:                       '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
                   1281:                       '<th>'.$lt{'grs'}.'</th>'.
                   1282:                       &Apache::loncommon::end_data_table_header_row().
                   1283:                       &Apache::loncommon::start_data_table_row()."\n".
1.103     raeburn  1284:                       '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc','','','','crstype'".')" /></td>'."\n".
1.1       raeburn  1285:                       '<td><select name="courserole">'."\n".
1.101     raeburn  1286:                       &default_course_roles($context,$checkpriv,'Course',%customroles)."\n".
1.1       raeburn  1287:                       '</select></td><td>'.
                   1288:                       '<table class="LC_createuser">'.
                   1289:                       '<tr class="LC_section_row"><td valign"top">'.
1.22      raeburn  1290:                       $lt{'exs'}.'<br /><select name="currsec">'.
1.1       raeburn  1291:                       ' <option value=""><--'.&mt('Pick course first').
                   1292:                       '</select></td>'.
                   1293:                       '<td>&nbsp;&nbsp;</td>'.
                   1294:                       '<td valign="top">'.$lt{'new'}.'<br />'.
                   1295:                       '<input type="text" name="newsec" value="" size="5" />'.
1.22      raeburn  1296:                       '<input type="hidden" name="groups" value="" />'.
                   1297:                       '<input type="hidden" name="sections" value="" />'.
                   1298:                       '<input type="hidden" name="origdom" value="'.
                   1299:                       $env{'request.role.domain'}.'" />'.
                   1300:                       '<input type="hidden" name="dccourse" value="" />'.
                   1301:                       '<input type="hidden" name="dcdomain" value="" />'.
1.103     raeburn  1302:                       '<input type="hidden" name="crstype" value="" />'.
1.22      raeburn  1303:                       '</td></tr></table></td>'.
1.1       raeburn  1304:                       &Apache::loncommon::end_data_table_row().
1.22      raeburn  1305:                       &Apache::loncommon::end_data_table()."\n";
1.1       raeburn  1306:     }
                   1307:     $options .= '</select>';
                   1308:     return ($options,$cb_jscript,$coursepick);
                   1309: }
                   1310: 
                   1311: sub default_course_roles {
1.101     raeburn  1312:     my ($context,$checkpriv,$crstype,%customroles) = @_;
1.1       raeburn  1313:     my $output;
1.17      raeburn  1314:     my $custom = 1;
1.101     raeburn  1315:     my @roles = &course_roles($context,$checkpriv,$custom,lc($crstype));
1.1       raeburn  1316:     foreach my $role (@roles) {
1.22      raeburn  1317:         if ($role ne 'cr') {
1.101     raeburn  1318:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.22      raeburn  1319:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1320:         }
1.1       raeburn  1321:     }
                   1322:     if (keys(%customroles) > 0) {
1.22      raeburn  1323:         if (grep(/^cr$/,@roles)) {
                   1324:             foreach my $cust (sort(keys(%customroles))) {
                   1325:                 my $custrole='cr_'.$env{'user.domain'}.
                   1326:                              '_'.$env{'user.name'}.'_'.$cust;
                   1327:                 $output .= '  <option value="'.$custrole.'">'.$cust.'</option>';
                   1328:             }
1.1       raeburn  1329:         }
                   1330:     }
                   1331:     return $output;
                   1332: }
                   1333: 
                   1334: sub construction_space_roles {
1.2       raeburn  1335:     my ($checkpriv) = @_;
1.17      raeburn  1336:     my @allroles = &roles_by_context('author');
1.1       raeburn  1337:     my @roles;
1.2       raeburn  1338:     if ($checkpriv) {
                   1339:         foreach my $role (@allroles) {
                   1340:             if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) { 
                   1341:                 push(@roles,$role); 
                   1342:             }
1.1       raeburn  1343:         }
1.2       raeburn  1344:         return @roles;
                   1345:     } else {
                   1346:         return @allroles;
1.1       raeburn  1347:     }
                   1348: }
                   1349: 
                   1350: sub domain_roles {
1.2       raeburn  1351:     my ($checkpriv) = @_;
1.17      raeburn  1352:     my @allroles = &roles_by_context('domain');
1.1       raeburn  1353:     my @roles;
1.2       raeburn  1354:     if ($checkpriv) {
                   1355:         foreach my $role (@allroles) {
                   1356:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   1357:                 push(@roles,$role);
                   1358:             }
1.1       raeburn  1359:         }
1.2       raeburn  1360:         return @roles;
                   1361:     } else {
                   1362:         return @allroles;
1.1       raeburn  1363:     }
                   1364: }
                   1365: 
                   1366: sub course_roles {
1.101     raeburn  1367:     my ($context,$checkpriv,$custom,$roletype) = @_;
1.102     raeburn  1368:     my $crstype;
                   1369:     if ($roletype eq 'community') {
                   1370:         $crstype = 'Community' ;
                   1371:     } else {
                   1372:         $crstype = 'Course';
                   1373:     }
                   1374:     my @allroles = &roles_by_context('course',$custom,$crstype);
1.1       raeburn  1375:     my @roles;
                   1376:     if ($context eq 'domain') {
                   1377:         @roles = @allroles;
                   1378:     } elsif ($context eq 'course') {
                   1379:         if ($env{'request.course.id'}) {
1.2       raeburn  1380:             if ($checkpriv) { 
                   1381:                 foreach my $role (@allroles) {
                   1382:                     if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   1383:                         push(@roles,$role);
                   1384:                     } else {
1.101     raeburn  1385:                         if ((($role ne 'cc') && ($role ne 'co')) && ($env{'request.course.sec'} ne '')) {
1.22      raeburn  1386:                             if (&Apache::lonnet::allowed('c'.$role,
1.2       raeburn  1387:                                              $env{'request.course.id'}.'/'.
1.22      raeburn  1388:                                              $env{'request.course.sec'})) {
1.2       raeburn  1389:                                 push(@roles,$role);
                   1390:                             }
1.1       raeburn  1391:                         }
                   1392:                     }
                   1393:                 }
1.2       raeburn  1394:             } else {
                   1395:                 @roles = @allroles;
1.1       raeburn  1396:             }
                   1397:         }
                   1398:     }
                   1399:     return @roles;
                   1400: }
                   1401: 
                   1402: sub curr_role_permissions {
1.101     raeburn  1403:     my ($context,$setting,$checkpriv,$type) = @_; 
1.17      raeburn  1404:     my $custom = 1;
1.1       raeburn  1405:     my @roles;
1.13      raeburn  1406:     if ($context eq 'author') {
1.2       raeburn  1407:         @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1408:     } elsif ($context eq 'domain') {
                   1409:         if ($setting eq 'course') {
1.101     raeburn  1410:             @roles = &course_roles($context,$checkpriv,$custom,$type); 
1.1       raeburn  1411:         } else {
1.2       raeburn  1412:             @roles = &domain_roles($checkpriv);
1.1       raeburn  1413:         }
                   1414:     } elsif ($context eq 'course') {
1.101     raeburn  1415:         @roles = &course_roles($context,$checkpriv,$custom,$type);
1.1       raeburn  1416:     }
                   1417:     return @roles;
                   1418: }
                   1419: 
                   1420: # ======================================================= Existing Custom Roles
                   1421: 
                   1422: sub my_custom_roles {
1.104     raeburn  1423:     my ($crstype) = @_;
1.1       raeburn  1424:     my %returnhash=();
                   1425:     my %rolehash=&Apache::lonnet::dump('roles');
1.104     raeburn  1426:     foreach my $key (keys(%rolehash)) {
1.1       raeburn  1427:         if ($key=~/^rolesdef\_(\w+)$/) {
1.104     raeburn  1428:             if ($crstype eq 'Community') {
                   1429:                 next if ($rolehash{$key} =~ /bre\&S/); 
                   1430:             }
1.1       raeburn  1431:             $returnhash{$1}=$1;
                   1432:         }
                   1433:     }
                   1434:     return %returnhash;
                   1435: }
                   1436: 
1.2       raeburn  1437: sub print_userlist {
                   1438:     my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
                   1439:         $idlist,$idlist_titles) = @_;
                   1440:     my $format = $env{'form.output'};
1.1       raeburn  1441:     if (! exists($env{'form.sortby'})) {
                   1442:         $env{'form.sortby'} = 'username';
                   1443:     }
1.2       raeburn  1444:     if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
                   1445:         $env{'form.Status'} = 'Active';
1.1       raeburn  1446:     }
                   1447:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2       raeburn  1448:         ($env{'form.Status'});
1.1       raeburn  1449: 
1.2       raeburn  1450:     if ($env{'form.showrole'} eq '') {
1.13      raeburn  1451:         if ($context eq 'course') {
                   1452:             $env{'form.showrole'} = 'st';
                   1453:         } else {
                   1454:             $env{'form.showrole'} = 'Any';            
                   1455:         }
1.2       raeburn  1456:     }
1.1       raeburn  1457:     if (! defined($env{'form.output'}) ||
                   1458:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
                   1459:         $env{'form.output'} = 'html';
                   1460:     }
                   1461: 
1.2       raeburn  1462:     my @statuses;
                   1463:     if ($env{'form.Status'} eq 'Any') {
                   1464:         @statuses = ('previous','active','future');
                   1465:     } elsif ($env{'form.Status'} eq 'Expired') {
                   1466:         @statuses = ('previous');
                   1467:     } elsif ($env{'form.Status'} eq 'Active') {
                   1468:         @statuses = ('active');
                   1469:     } elsif ($env{'form.Status'} eq 'Future') {
                   1470:         @statuses = ('future');
                   1471:     }
1.1       raeburn  1472: 
1.2       raeburn  1473: #    if ($context eq 'course') { 
                   1474: #        $r->print(&display_adv_courseroles());
                   1475: #    }
1.1       raeburn  1476:     #
                   1477:     # Interface output
1.2       raeburn  1478:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   1479:               '<input type="hidden" name="action" value="'.
1.1       raeburn  1480:               $env{'form.action'}.'" />');
                   1481:     $r->print("<p>\n");
                   1482:     if ($env{'form.action'} ne 'modifystudent') {
                   1483:         my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
                   1484:                                            'excel' => "Excel",
                   1485:                                            'html'  => 'HTML');
                   1486:         my $output_selector = '<select size="1" name="output" >';
                   1487:         foreach my $outputformat ('html','csv','excel') {
1.96      bisitz   1488:             my $option = '<option value="'.$outputformat.'"';
1.1       raeburn  1489:             if ($outputformat eq $env{'form.output'}) {
1.96      bisitz   1490:                 $option .= ' selected="selected"';
1.1       raeburn  1491:             }
                   1492:             $option .='>'.$lt{$outputformat}.'</option>';
                   1493:             $output_selector .= "\n".$option;
                   1494:         }
                   1495:         $output_selector .= '</select>';
1.70      bisitz   1496:         $r->print('<label><span class="LC_nobreak">'
                   1497:                  .&mt('Output Format: [_1]',$output_selector)
                   1498:                  .'</span></label>'.('&nbsp;'x3));
                   1499:     }
                   1500:     $r->print('<label><span class="LC_nobreak">'
                   1501:              .&mt('User Status: [_1]',$status_select)
                   1502:              .'</span></label>'.('&nbsp;'x3)."\n");
1.2       raeburn  1503:     my $roleselected = '';
                   1504:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1505:        $roleselected = ' selected="selected"'; 
1.2       raeburn  1506:     }
1.53      raeburn  1507:     my ($cnum,$cdom);
                   1508:     $r->print(&role_filter($context));
                   1509:     if ($context eq 'course') {
                   1510:         ($cnum,$cdom) = &get_course_identity();
                   1511:         $r->print(&section_group_filter($cnum,$cdom));
1.2       raeburn  1512:     }
1.78      raeburn  1513:     if ($env{'form.phase'} eq '') {
                   1514:         $r->print('<br /><br />'.&list_submit_button(&mt('Display List of Users')).
                   1515:                   "\n</p>\n".
                   1516:                   '<input type="hidden" name="phase" value="" /></form>');
                   1517:         return;
                   1518:     }
1.106     raeburn  1519:     if (!(($context eq 'domain') && 
                   1520:           (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
1.111     bisitz   1521:         $r->print(
                   1522:             "\n</p>\n"
                   1523:            .'<p>'
                   1524:            .&list_submit_button(&mt('Update Display'))
                   1525:            ."</p>\n"
                   1526:         );
1.2       raeburn  1527:     }
                   1528:     my ($indexhash,$keylist) = &make_keylist_array();
1.106     raeburn  1529:     my (%userlist,%userinfo,$clearcoursepick);
1.102     raeburn  1530:     if (($context eq 'domain') && 
                   1531:         ($env{'form.roletype'} eq 'course') || 
                   1532:         ($env{'form.roletype'} eq 'community')) {
                   1533:         my ($crstype,$numcodes,$title,$warning);
                   1534:         if ($env{'form.roletype'} eq 'course') {
                   1535:             $crstype = 'Course';
                   1536:             $numcodes = $totcodes;
                   1537:             $title = &mt('Select Courses');
                   1538:             $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
                   1539:         } elsif ($env{'form.roletype'} eq 'community') {
                   1540:             $crstype = 'Community';
                   1541:             $numcodes = 0;
                   1542:             $title = &mt('Select Communities');
                   1543:             $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
                   1544:         }
1.120     raeburn  1545:         my @standardnames = &Apache::loncommon::get_standard_codeitems();
1.3       raeburn  1546:         my $courseform =
1.102     raeburn  1547:             &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
1.120     raeburn  1548:                             $codetitles,$idlist,$idlist_titles,$crstype,
                   1549:                             \@standardnames);
1.3       raeburn  1550:         $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
                   1551:                   &Apache::lonhtmlcommon::start_pick_box()."\n".
1.102     raeburn  1552:                   &Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
1.3       raeburn  1553:                   $courseform."\n".
                   1554:                   &Apache::lonhtmlcommon::row_closure(1).
                   1555:                   &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1.106     raeburn  1556:                   '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
                   1557:                   &list_submit_button(&mt('Update Display')).
1.102     raeburn  1558:                   "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
1.106     raeburn  1559:         $clearcoursepick = 0;
                   1560:         if (($env{'form.origroletype'} ne '') &&
                   1561:             ($env{'form.origroletype'} ne $env{'form.roletype'})) {
                   1562:             $clearcoursepick = 1;
                   1563:         }
                   1564:         if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.11      raeburn  1565:             $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
                   1566:         }
                   1567:     } else {
                   1568:         $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
1.3       raeburn  1569:     }
                   1570:     $r->rflush();
1.1       raeburn  1571:     if ($context eq 'course') {
1.46      raeburn  1572:         if (($env{'form.showrole'} eq 'st') || ($env{'form.showrole'} eq 'Any')) { 
1.45      raeburn  1573:             my $classlist = &Apache::loncoursedata::get_classlist();
1.66      raeburn  1574:             if (ref($classlist) eq 'HASH') {
                   1575:                 %userlist = %{$classlist};
                   1576:             }
1.45      raeburn  1577:         }
1.43      raeburn  1578:         if ($env{'form.showrole'} ne 'st') {
                   1579:             my $showroles;
                   1580:             if ($env{'form.showrole'} ne 'Any') {
                   1581:                 $showroles = [$env{'form.showrole'}];
1.3       raeburn  1582:             } else {
1.43      raeburn  1583:                 $showroles = undef;
1.1       raeburn  1584:             }
1.43      raeburn  1585:             my $withsec = 1;
                   1586:             my $hidepriv = 1;
                   1587:             my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   1588:                               \@statuses,$showroles,undef,$withsec,$hidepriv);
                   1589:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                   1590:                              \%advrolehash,$permission);
1.1       raeburn  1591:         }
1.2       raeburn  1592:     } else {
                   1593:         my (%cstr_roles,%dom_roles);
1.13      raeburn  1594:         if ($context eq 'author') {
1.2       raeburn  1595:             # List co-authors and assistant co-authors
1.17      raeburn  1596:             my @possroles = &roles_by_context($context);
1.2       raeburn  1597:             %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   1598:                                               \@statuses,\@possroles);
                   1599:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11      raeburn  1600:                              \%cstr_roles,$permission);
1.2       raeburn  1601:         } elsif ($context eq 'domain') {
                   1602:             if ($env{'form.roletype'} eq 'domain') {
                   1603:                 %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
                   1604:                 foreach my $key (keys(%dom_roles)) {
                   1605:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1606:                         &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1607:                                          \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1608:                     }
                   1609:                 }
1.13      raeburn  1610:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1611:                 my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
                   1612:                 my %coauthors;
                   1613:                 foreach my $key (keys(%dom_roles)) {
                   1614:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1615:                         if ($env{'form.showrole'} eq 'au') {
                   1616:                             &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1617:                                              \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1618:                         } else {
                   1619:                             my @possroles;
                   1620:                             if ($env{'form.showrole'} eq 'Any') {
1.22      raeburn  1621:                                 @possroles = &roles_by_context('author');
1.2       raeburn  1622:                             } else {
                   1623:                                 @possroles = ($env{'form.showrole'}); 
                   1624:                             }
                   1625:                             foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22      raeburn  1626:                                 my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2       raeburn  1627:                                 my $extent = '/'.$authordom.'/'.$authorname;
                   1628:                                 %{$coauthors{$extent}} =
                   1629:                                     &Apache::lonnet::get_my_roles($authorname,
                   1630:                                        $authordom,undef,\@statuses,\@possroles);
                   1631:                             }
                   1632:                             &gather_userinfo($context,$format,\%userlist,
1.11      raeburn  1633:                                      $indexhash,\%userinfo,\%coauthors,$permission);
1.2       raeburn  1634:                         }
                   1635:                     }
                   1636:                 }
1.101     raeburn  1637:             } elsif (($env{'form.roletype'} eq 'course') ||
                   1638:                      ($env{'form.roletype'} eq 'community')) {
1.106     raeburn  1639:                 if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.2       raeburn  1640:                     my %courses = &process_coursepick();
1.39      raeburn  1641:                     my %allusers;
                   1642:                     my $hidepriv = 1;
1.2       raeburn  1643:                     foreach my $cid (keys(%courses)) {
1.17      raeburn  1644:                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11      raeburn  1645:                         next if ($cnum eq '' || $cdom eq '');
1.17      raeburn  1646:                         my $custom = 1;
1.2       raeburn  1647:                         my (@roles,@sections,%access,%users,%userdata,
1.6       albertel 1648:                             %statushash);
1.2       raeburn  1649:                         if ($env{'form.showrole'} eq 'Any') {
1.101     raeburn  1650:                             @roles = &course_roles($context,undef,$custom,
                   1651:                                                    $env{'form.roletype'});
1.2       raeburn  1652:                         } else {
                   1653:                             @roles = ($env{'form.showrole'});
                   1654:                         }
                   1655:                         foreach my $role (@roles) {
                   1656:                             %{$users{$role}} = ();
                   1657:                         }
                   1658:                         foreach my $type (@statuses) {
                   1659:                             $access{$type} = $type;
                   1660:                         }
1.39      raeburn  1661:                         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
1.2       raeburn  1662:                         foreach my $user (keys(%userdata)) {
                   1663:                             next if (ref($userinfo{$user}) eq 'HASH');
                   1664:                             foreach my $item ('fullname','id') {
                   1665:                                 $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
                   1666:                             }
                   1667:                         }
                   1668:                         foreach my $role (keys(%users)) {
                   1669:                             foreach my $user (keys(%{$users{$role}})) {
                   1670:                                 my $uniqid = $user.':'.$role;
                   1671:                                 $allusers{$uniqid}{$cid} = { desc => $cdesc,
                   1672:                                                              secs  => $statushash{$user}{$role},
                   1673:                                                            };
                   1674:                             }
                   1675:                         }
                   1676:                     }
                   1677:                     &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1678:                                      \%userinfo,\%allusers,$permission);
1.2       raeburn  1679:                 } else {
1.10      raeburn  1680:                     $r->print('<input type="hidden" name="phase" value="'.
                   1681:                               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1682:                     return;
                   1683:                 }
1.1       raeburn  1684:             }
                   1685:         }
1.3       raeburn  1686:     }
                   1687:     if (keys(%userlist) == 0) {
1.13      raeburn  1688:         if ($context eq 'author') {
1.3       raeburn  1689:             $r->print(&mt('There are no co-authors to display.')."\n");
                   1690:         } elsif ($context eq 'domain') {
                   1691:             if ($env{'form.roletype'} eq 'domain') {
                   1692:                 $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13      raeburn  1693:             } elsif ($env{'form.roletype'} eq 'author') {
1.3       raeburn  1694:                 $r->print(&mt('There are no authors or co-authors to display.')."\n");
                   1695:             } elsif ($env{'form.roletype'} eq 'course') {
                   1696:                 $r->print(&mt('There are no course users to display')."\n"); 
1.101     raeburn  1697:             } elsif ($env{'form.roletype'} eq 'community') {
                   1698:                 $r->print(&mt('There are no community users to display')."\n");
1.2       raeburn  1699:             }
1.3       raeburn  1700:         } elsif ($context eq 'course') {
                   1701:             $r->print(&mt('There are no course users to display.')."\n");
                   1702:         }
                   1703:     } else {
                   1704:         # Print out the available choices
1.4       raeburn  1705:         my $usercount;
1.3       raeburn  1706:         if ($env{'form.action'} eq 'modifystudent') {
1.10      raeburn  1707:             ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4       raeburn  1708:                                  $env{'form.Status'},\%userlist,$keylist);
1.1       raeburn  1709:         } else {
1.4       raeburn  1710:             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10      raeburn  1711:                                $permission,$env{'form.Status'},\%userlist,$keylist);
1.4       raeburn  1712:         }
                   1713:         if (!$usercount) {
1.72      bisitz   1714:             $r->print('<br /><span class="LC_warning">'
                   1715:                      .&mt('There are no users matching the search criteria.')
                   1716:                      .'</span>'
                   1717:             ); 
1.2       raeburn  1718:         }
                   1719:     }
1.10      raeburn  1720:     $r->print('<input type="hidden" name="phase" value="'.
                   1721:               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1722: }
                   1723: 
1.53      raeburn  1724: sub role_filter {
                   1725:     my ($context) = @_;
                   1726:     my $output;
                   1727:     my $roleselected = '';
                   1728:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1729:        $roleselected = ' selected="selected"';
1.53      raeburn  1730:     }
                   1731:     my ($role_select);
                   1732:     if ($context eq 'domain') {
                   1733:         $role_select = &domain_roles_select();
1.70      bisitz   1734:         $output = '<label><span class="LC_nobreak">'
                   1735:                  .&mt('Role Type: [_1]',$role_select)
                   1736:                  .'</span></label>';
1.53      raeburn  1737:     } else {
                   1738:         $role_select = '<select name="showrole">'."\n".
                   1739:                        '<option value="Any" '.$roleselected.'>'.
                   1740:                        &mt('Any role').'</option>';
1.101     raeburn  1741:         my ($roletype,$crstype);
                   1742:         if ($context eq 'course') {
                   1743:             $crstype = &Apache::loncommon::course_type();
                   1744:             if ($crstype eq 'Community') {
                   1745:                 $roletype = 'community';
                   1746:             } else {
                   1747:                 $roletype = 'course';
                   1748:             } 
                   1749:         }
                   1750:         my @poss_roles = &curr_role_permissions($context,'','',$roletype);
1.53      raeburn  1751:         foreach my $role (@poss_roles) {
                   1752:             $roleselected = '';
                   1753:             if ($role eq $env{'form.showrole'}) {
1.91      bisitz   1754:                 $roleselected = ' selected="selected"';
1.53      raeburn  1755:             }
                   1756:             my $plrole;
                   1757:             if ($role eq 'cr') {
                   1758:                 $plrole = &mt('Custom role');
                   1759:             } else {
1.101     raeburn  1760:                 $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.53      raeburn  1761:             }
                   1762:             $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
                   1763:         }
                   1764:         $role_select .= '</select>';
1.70      bisitz   1765:         $output = '<label><span class="LC_nobreak">'
                   1766:                  .&mt('Role: [_1]',$role_select)
1.111     bisitz   1767:                  .'</span></label> ';
1.53      raeburn  1768:     }
                   1769:     return $output;
                   1770: }
                   1771: 
1.33      raeburn  1772: sub section_group_filter {
                   1773:     my ($cnum,$cdom) = @_;
                   1774:     my @filters;
                   1775:     if ($env{'request.course.sec'} eq '') {
                   1776:         @filters = ('sec');
                   1777:     }
                   1778:     push(@filters,'grp');
                   1779:     my %name = (
                   1780:                  sec => 'secfilter',
                   1781:                  grp => 'grpfilter',
                   1782:                );
                   1783:     my %title = &Apache::lonlocal::texthash (
                   1784:                                               sec  => 'Section(s)',
                   1785:                                               grp  => 'Group(s)',
                   1786:                                               all  => 'all',
                   1787:                                               none => 'none',
                   1788:                                             );
1.47      raeburn  1789:     my $output;
1.33      raeburn  1790:     foreach my $item (@filters) {
1.47      raeburn  1791:         my ($markup,@options); 
1.33      raeburn  1792:         if ($env{'form.'.$name{$item}} eq '') {
                   1793:             $env{'form.'.$name{$item}} = 'all';
                   1794:         }
                   1795:         if ($item eq 'sec') {
1.103     raeburn  1796:             if (($env{'form.showrole'} eq 'cc') || ($env{'form.showrole'} eq 'co')) {
1.33      raeburn  1797:                 $env{'form.'.$name{$item}} = 'none';
                   1798:             }
                   1799:             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   1800:             @options = sort(keys(%sections_count));
                   1801:         } elsif ($item eq 'grp') {
                   1802:             my %curr_groups = &Apache::longroup::coursegroups();
                   1803:             @options = sort(keys(%curr_groups));
                   1804:         }
                   1805:         if (@options > 0) {
                   1806:             my $currsel;
1.112     bisitz   1807:             $markup = '<select name="'.$name{$item}.'">'."\n";
1.33      raeburn  1808:             foreach my $option ('all','none',@options) { 
                   1809:                 $currsel = '';
                   1810:                 if ($env{'form.'.$name{$item}} eq $option) {
1.96      bisitz   1811:                     $currsel = ' selected="selected"';
1.33      raeburn  1812:                 }
                   1813:                 $markup .= ' <option value="'.$option.'"'.$currsel.'>';
                   1814:                 if (($option eq 'all') || ($option eq 'none')) {
                   1815:                     $markup .= $title{$option};
                   1816:                 } else {
                   1817:                     $markup .= $option;
                   1818:                 }   
                   1819:                 $markup .= '</option>'."\n";
                   1820:             }
                   1821:             $markup .= '</select>'."\n";
1.111     bisitz   1822:             $output .= ('&nbsp;'x3).'<span class="LC_nobreak">'
                   1823:                       .'<label>'.$title{$item}.': '.$markup.'</label>'
                   1824:                       .'</span> ';
1.33      raeburn  1825:         }
                   1826:     }
                   1827:     return $output;
                   1828: }
                   1829: 
1.2       raeburn  1830: sub list_submit_button {
                   1831:     my ($text) = @_;
1.11      raeburn  1832:     return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2       raeburn  1833: }
                   1834: 
                   1835: sub gather_userinfo {
1.11      raeburn  1836:     my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.52      raeburn  1837:     my $viewablesec;
                   1838:     if ($context eq 'course') {
                   1839:         $viewablesec = &viewable_section($permission);
                   1840:     }
1.2       raeburn  1841:     foreach my $item (keys(%{$rolehash})) {
                   1842:         my %userdata;
1.22      raeburn  1843:         if ($context eq 'author') { 
1.2       raeburn  1844:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1845:                 split(/:/,$item);
                   1846:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1847:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1848:                                $item,$userlist);
1.22      raeburn  1849:         } elsif ($context eq 'course') {
                   1850:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   1851:              $userdata{'section'}) = split(/:/,$item,-1);
                   1852:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   1853:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   1854:                 next if ($viewablesec ne $userdata{'section'});
                   1855:             }
1.24      raeburn  1856:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1857:                                $item,$userlist);
1.2       raeburn  1858:         } elsif ($context eq 'domain') {
                   1859:             if ($env{'form.roletype'} eq 'domain') {
                   1860:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   1861:                     split(/:/,$item);
                   1862:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1863:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1864:                                    $item,$userlist);
1.13      raeburn  1865:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1866:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   1867:                     $userdata{'extent'} = $item;
                   1868:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   1869:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   1870:                         ($userdata{'start'},$userdata{'end'}) = 
                   1871:                             split(/:/,$rolehash->{$item}{$key});
                   1872:                         my $uniqid = $key.':'.$item;
1.25      raeburn  1873:                         &build_user_record($context,\%userdata,$userinfo,
                   1874:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  1875:                     }
                   1876:                 }
1.102     raeburn  1877:             } elsif (($env{'form.roletype'} eq 'course') || 
                   1878:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  1879:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1880:                     split(/:/,$item);
                   1881:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  1882:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  1883:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   1884:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   1885:                             my $spanstart = '';
                   1886:                             my $spanend = '; ';
                   1887:                             my $space = ', ';
                   1888:                             if ($format eq 'html' || $format eq 'view') {
                   1889:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  1890:                                 # FIXME: actions on courses disabled for now
                   1891: #                                if ($permission->{'cusr'}) {
                   1892: #                                    if ($numcids > 1) {
1.25      raeburn  1893: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1894: #                                    } else {
1.25      raeburn  1895: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1896: #                                    }
                   1897: #                                }
1.2       raeburn  1898:                                 $spanend = '</span><br />';
                   1899:                                 $space = ',&nbsp;';
                   1900:                             }
                   1901:                             $userdata{'extent'} .= $spanstart.
                   1902:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   1903:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   1904:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  1905:                                     if (($env{'form.Status'} eq 'Any') ||
                   1906:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   1907:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   1908:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   1909:                                     }
1.2       raeburn  1910:                                 }
                   1911:                             }
                   1912:                         }
                   1913:                     }
                   1914:                 }
1.25      raeburn  1915:                 if ($userdata{'status'} ne '') {
                   1916:                     &build_user_record($context,\%userdata,$userinfo,
                   1917:                                        $indexhash,$item,$userlist);
                   1918:                 }
1.2       raeburn  1919:             }
                   1920:         }
                   1921:     }
                   1922:     return;
                   1923: }
                   1924: 
                   1925: sub build_user_record {
1.24      raeburn  1926:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  1927:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.102     raeburn  1928:     if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
                   1929:                              && ($env{'form.roletype'} eq 'community')))) {
1.24      raeburn  1930:         &process_date_info($userdata);
                   1931:     }
1.2       raeburn  1932:     my $username = $userdata->{'username'};
                   1933:     my $domain = $userdata->{'domain'};
                   1934:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  1935:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  1936:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1937:     } else {
                   1938:         &aggregate_user_info($domain,$username,$userinfo);
                   1939:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   1940:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1941:     }
                   1942:     foreach my $key (keys(%{$indexhash})) {
                   1943:         if (defined($userdata->{$key})) {
                   1944:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   1945:         }
                   1946:     }
                   1947:     return;
                   1948: }
                   1949: 
                   1950: sub courses_selector {
                   1951:     my ($cdom,$formname) = @_;
                   1952:     my %coursecodes = ();
                   1953:     my %codes = ();
                   1954:     my @codetitles = ();
                   1955:     my %cat_titles = ();
                   1956:     my %cat_order = ();
                   1957:     my %idlist = ();
                   1958:     my %idnums = ();
                   1959:     my %idlist_titles = ();
                   1960:     my $caller = 'global';
                   1961:     my $format_reply;
                   1962:     my $jscript = '';
                   1963: 
1.7       albertel 1964:     my $totcodes = 0;
                   1965:     $totcodes =
1.2       raeburn  1966:         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                   1967:                                                       $cdom,$totcodes);
                   1968:     if ($totcodes > 0) {
                   1969:         $format_reply =
                   1970:              &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
                   1971:                                 \%codes,\@codetitles,\%cat_titles,\%cat_order);
                   1972:         if ($format_reply eq 'ok') {
                   1973:             my $numtypes = @codetitles;
                   1974:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   1975:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   1976:             my $longtitles_str = join('","',@{$longtitles});
                   1977:             my $allidlist = $idlist{$codetitles[0]};
                   1978:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   1979:             $jscript .= $scripttext;
                   1980:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
                   1981:         }
                   1982:     }
                   1983:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   1984: 
                   1985:     my %elements = (
                   1986:                      Year => 'selectbox',
                   1987:                      coursepick => 'radio',
                   1988:                      coursetotal => 'text',
                   1989:                      courselist => 'text',
                   1990:                    );
                   1991:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   1992:     if ($env{'form.coursepick'} eq 'category') {
                   1993:         $jscript .= qq|
                   1994: function setCourseCat(formname) {
                   1995:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   1996:         return;
                   1997:     }
1.120     raeburn  1998:     courseSet('$codetitles[0]');
1.2       raeburn  1999:     for (var j=0; j<formname.Semester.length; j++) {
                   2000:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   2001:             formname.Semester.options[j].selected = true;
                   2002:         }
                   2003:     }
                   2004:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   2005:         return;
                   2006:     }
1.120     raeburn  2007:     courseSet('$codetitles[1]');
1.2       raeburn  2008:     for (var j=0; j<formname.Department.length; j++) {
                   2009:         if (formname.Department.options[j].value == "$env{'form.Department'}") {            formname.Department.options[j].selected = true;
                   2010:         }
                   2011:     }
                   2012:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   2013:         return;
                   2014:     }
1.120     raeburn  2015:     courseSet('$codetitles[2]');
1.2       raeburn  2016:     for (var j=0; j<formname.Number.length; j++) {
                   2017:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   2018:             formname.Number.options[j].selected = true;
                   2019:         }
                   2020:     }
                   2021: }
                   2022: |;
                   2023:     }
                   2024:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   2025:             \%idlist_titles);
                   2026: }
                   2027: 
                   2028: sub course_selector_loadcode {
                   2029:     my ($formname) = @_;
                   2030:     my $loadcode;
                   2031:     if ($env{'form.coursepick'} ne '') {
                   2032:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   2033:         if ($env{'form.coursepick'} eq 'category') {
                   2034:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   2035:         }
                   2036:     }
                   2037:     return $loadcode;
                   2038: }
                   2039: 
                   2040: sub process_coursepick {
                   2041:     my $coursefilter = $env{'form.coursepick'};
                   2042:     my $cdom = $env{'request.role.domain'};
                   2043:     my %courses;
1.105     raeburn  2044:     my $crssrch = 'Course';
                   2045:     if ($env{'form.roletype'} eq 'community') {
                   2046:         $crssrch = 'Community';
                   2047:     }
1.2       raeburn  2048:     if ($coursefilter eq 'all') {
                   2049:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1.105     raeburn  2050:                                                  undef,undef,$crssrch);
1.2       raeburn  2051:     } elsif ($coursefilter eq 'category') {
                   2052:         my $instcode = &instcode_from_coursefilter();
                   2053:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1.105     raeburn  2054:                                                  undef,undef,$crssrch);
1.2       raeburn  2055:     } elsif ($coursefilter eq 'specific') {
                   2056:         if ($env{'form.coursetotal'} > 1) {
                   2057:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   2058:             foreach my $cid (@course_ids) {
                   2059:                 $courses{$cid} = '';
1.1       raeburn  2060:             }
1.2       raeburn  2061:         } else {
                   2062:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  2063:         }
1.2       raeburn  2064:     }
                   2065:     return %courses;
                   2066: }
                   2067: 
                   2068: sub instcode_from_coursefilter {
                   2069:     my $instcode = '';
                   2070:     my @cats = ('Semester','Year','Department','Number');
                   2071:     foreach my $category (@cats) {
                   2072:         if (defined($env{'form.'.$category})) {
                   2073:             unless ($env{'form.'.$category} eq '-1') {
                   2074:                 $instcode .= $env{'form.'.$category};
                   2075:            }
                   2076:         }
                   2077:     }
                   2078:     if ($instcode eq '') {
                   2079:         $instcode = '.';
                   2080:     }
                   2081:     return $instcode;
                   2082: }
                   2083: 
                   2084: sub display_adv_courseroles {
                   2085:     my $output;
                   2086:     #
                   2087:     # List course personnel
                   2088:     my %coursepersonnel = 
                   2089:        &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
                   2090:     #
                   2091:     $output = '<br />'.&Apache::loncommon::start_data_table();
                   2092:     foreach my $role (sort(keys(%coursepersonnel))) {
                   2093:         next if ($role =~ /^\s*$/);
                   2094:         $output .= &Apache::loncommon::start_data_table_row().
                   2095:                   '<td>'.$role.'</td><td>';
                   2096:         foreach my $user (split(',',$coursepersonnel{$role})) {
                   2097:             my ($puname,$pudom)=split(':',$user);
                   2098:             $output .= ' '.&Apache::loncommon::aboutmewrapper(
                   2099:                        &Apache::loncommon::plainname($puname,$pudom),
                   2100:                        $puname,$pudom);
                   2101:         }
                   2102:         $output .= '</td>'.&Apache::loncommon::end_data_table_row();
                   2103:     }
                   2104:     $output .= &Apache::loncommon::end_data_table();
                   2105: }
                   2106: 
                   2107: sub make_keylist_array {
                   2108:     my ($index,$keylist);
                   2109:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   2110:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   2111:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   2112:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   2113:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   2114:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   2115:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   2116:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2117:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2118:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   2119:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   2120:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   2121:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   2122:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1.44      raeburn  2123:     $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
1.47      raeburn  2124:     $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
1.2       raeburn  2125:     foreach my $key (keys(%{$index})) {
                   2126:         $keylist->[$index->{$key}] = $key;
                   2127:     }
                   2128:     return ($index,$keylist);
                   2129: }
                   2130: 
                   2131: sub aggregate_user_info {
                   2132:     my ($udom,$uname,$userinfo) = @_;
                   2133:     my %info=&Apache::lonnet::get('environment',
                   2134:                                   ['firstname','middlename',
                   2135:                                    'lastname','generation','id'],
                   2136:                                    $udom,$uname);
                   2137:     my ($tmp) = keys(%info);
                   2138:     my ($fullname,$id);
                   2139:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   2140:         $fullname = 'not available';
                   2141:         $id = 'not available';
                   2142:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   2143:                                  'for '.$uname.':'.$udom);
1.1       raeburn  2144:     } else {
1.2       raeburn  2145:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   2146:         $id = $info{'id'};
                   2147:     }
                   2148:     $userinfo->{$uname.':'.$udom} = { 
                   2149:                                       fullname => $fullname,
                   2150:                                       id       => $id,
                   2151:                                     };
                   2152:     return;
                   2153: }
1.1       raeburn  2154: 
1.2       raeburn  2155: sub process_date_info {
                   2156:     my ($userdata) = @_;
                   2157:     my $now = time;
1.83      raeburn  2158:     $userdata->{'status'} = 'Active';
1.2       raeburn  2159:     if ($userdata->{'start'} > 0) {
                   2160:         if ($now < $userdata->{'start'}) {
1.83      raeburn  2161:             $userdata->{'status'} = 'Future';
1.2       raeburn  2162:         }
1.1       raeburn  2163:     }
1.2       raeburn  2164:     if ($userdata->{'end'} > 0) {
                   2165:         if ($now > $userdata->{'end'}) {
1.83      raeburn  2166:             $userdata->{'status'} = 'Expired';
1.2       raeburn  2167:         }
                   2168:     }
                   2169:     return;
1.1       raeburn  2170: }
                   2171: 
                   2172: sub show_users_list {
1.55      raeburn  2173:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname)=@_;
                   2174:     if ($formname eq '') {
                   2175:         $formname = 'studentform';
                   2176:     }
1.1       raeburn  2177:     #
                   2178:     # Variables for excel output
                   2179:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   2180:     #
                   2181:     # Variables for csv output
                   2182:     my ($CSVfile,$CSVfilename);
                   2183:     #
                   2184:     my $sortby = $env{'form.sortby'};
1.3       raeburn  2185:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  2186:     if ($context eq 'course') {
1.3       raeburn  2187:         push(@sortable,('section','groups','type'));
1.2       raeburn  2188:     } else {
1.3       raeburn  2189:         push(@sortable,'extent');
                   2190:     }
1.55      raeburn  2191:     if ($mode eq 'pickauthor') {
                   2192:         @sortable = ('username','fullname','email','status');
                   2193:     }
1.3       raeburn  2194:     if (!grep(/^\Q$sortby\E$/,@sortable)) {
                   2195:         $sortby = 'username';
1.1       raeburn  2196:     }
1.22      raeburn  2197:     my $setting = $env{'form.roletype'};
1.101     raeburn  2198:     my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers,$crstype);
1.1       raeburn  2199:     if ($context eq 'course') {
1.22      raeburn  2200:         $cid = $env{'request.course.id'};
1.101     raeburn  2201:         $crstype = &Apache::loncommon::course_type();
1.17      raeburn  2202:         ($cnum,$cdom) = &get_course_identity($cid);
1.2       raeburn  2203:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   2204:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  2205:         if ($mode eq 'autoenroll') {
                   2206:             $env{'form.showrole'} = 'st';
                   2207:         } else {
                   2208:             if (! exists($env{'form.displayphotos'})) {
                   2209:                 $env{'form.displayphotos'} = 'off';
                   2210:             }
                   2211:             $displayphotos = $env{'form.displayphotos'};
                   2212:             if (! exists($env{'form.displayclickers'})) {
                   2213:                 $env{'form.displayclickers'} = 'off';
                   2214:             }
                   2215:             $displayclickers = $env{'form.displayclickers'};
                   2216:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   2217:                 $r->print('
1.1       raeburn  2218: <script type="text/javascript">
1.96      bisitz   2219: // <![CDATA[
1.1       raeburn  2220: function photowindow(photolink) {
                   2221:     var title = "Photo_Viewer";
                   2222:     var options = "scrollbars=1,resizable=1,menubar=0";
                   2223:     options += ",width=240,height=240";
                   2224:     stdeditbrowser = open(photolink,title,options,"1");
                   2225:     stdeditbrowser.focus();
                   2226: }
1.96      bisitz   2227: // ]]>
1.1       raeburn  2228: </script>
1.16      raeburn  2229:                ');
                   2230:             }
                   2231:             $r->print(<<END);
1.1       raeburn  2232: <input type="hidden" name="displayphotos" value="$displayphotos" />
                   2233: <input type="hidden" name="displayclickers" value="$displayclickers" />
                   2234: END
1.16      raeburn  2235:         }
1.102     raeburn  2236:     } elsif ($context eq 'domain') {
                   2237:         if ($setting eq 'community') {
                   2238:             $crstype = 'Community';
1.105     raeburn  2239:         } elsif ($setting eq 'course') {
1.102     raeburn  2240:             $crstype = 'Course';
                   2241:         }
1.1       raeburn  2242:     }
1.55      raeburn  2243:     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.11      raeburn  2244:         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.40      raeburn  2245:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.56      raeburn  2246:         my $verify_action_js = &bulkaction_javascript($formname);
1.1       raeburn  2247:         $r->print(<<END);
1.10      raeburn  2248: 
                   2249: <script type="text/javascript" language="Javascript">
1.96      bisitz   2250: // <![CDATA[
1.11      raeburn  2251: $check_uncheck_js
                   2252: 
1.56      raeburn  2253: $verify_action_js
1.10      raeburn  2254: 
                   2255: function username_display_launch(username,domain) {
                   2256:     var target;
1.55      raeburn  2257:     for (var i=0; i<document.$formname.usernamelink.length; i++) {
                   2258:         if (document.$formname.usernamelink[i].checked) {
                   2259:             target = document.$formname.usernamelink[i].value;
1.10      raeburn  2260:         }
                   2261:     }
                   2262:     if (target == 'modify') {
1.55      raeburn  2263:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2264:             var url = '/adm/createuser?srchterm='+username+'&srchdomain='+domain+'&phase=get_user_info&action=singleuser&srchin=dom&srchby=uname&srchtype=exact&popup=1';
                   2265:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2266:             modifywin = window.open(url,'',options,1);
                   2267:             modifywin.focus();
                   2268:             return;
                   2269:         } else {
1.55      raeburn  2270:             document.$formname.srchterm.value=username;
                   2271:             document.$formname.srchdomain.value=domain;
                   2272:             document.$formname.phase.value='get_user_info';
                   2273:             document.$formname.action.value = 'singleuser';
                   2274:             document.$formname.submit();
1.50      raeburn  2275:         }
1.10      raeburn  2276:     }
1.48      raeburn  2277:     if (target == 'aboutme') {
1.55      raeburn  2278:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2279:             var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
                   2280:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2281:             aboutmewin = window.open(url,'',options,1);
                   2282:             aboutmewin.focus();
                   2283:             return;
                   2284:         } else {
                   2285:             document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   2286:         }
1.48      raeburn  2287:     }
1.98      raeburn  2288:     if (target == 'track') {
                   2289:         if (document.$formname.userwin.checked == true) {
                   2290:             var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
                   2291:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2292:             var trackwin = window.open(url,'',options,1);
                   2293:             trackwin.focus();
                   2294:             return;
                   2295:         } else {
                   2296:             document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
                   2297:         }
                   2298:     }
1.10      raeburn  2299: }
1.96      bisitz   2300: // ]]>
1.10      raeburn  2301: </script>
1.11      raeburn  2302: $date_sec_selector
1.1       raeburn  2303: <input type="hidden" name="state" value="$env{'form.state'}" />
                   2304: END
                   2305:     }
                   2306:     $r->print(<<END);
                   2307: <input type="hidden" name="sortby" value="$sortby" />
                   2308: END
                   2309: 
                   2310:     my %lt=&Apache::lonlocal::texthash(
                   2311:                        'username'   => "username",
                   2312:                        'domain'     => "domain",
                   2313:                        'id'         => 'ID',
                   2314:                        'fullname'   => "name",
                   2315:                        'section'    => "section",
                   2316:                        'groups'     => "active groups",
                   2317:                        'start'      => "start date",
                   2318:                        'end'        => "end date",
                   2319:                        'status'     => "status",
1.2       raeburn  2320:                        'role'       => "role",
1.1       raeburn  2321:                        'type'       => "enroll type/action",
1.79      schafran 2322:                        'email'      => "e-mail address",
1.1       raeburn  2323:                        'photo'      => "photo",
1.2       raeburn  2324:                        'extent'     => "extent",
1.11      raeburn  2325:                        'pr'         => "Proceed",
                   2326:                        'ca'         => "check all",
                   2327:                        'ua'         => "uncheck all",
                   2328:                        'ac'         => "Action to take for selected users",
1.56      raeburn  2329:                        'link'       => "Behavior of clickable username link for each user",
1.82      weissno  2330:                        'aboutme'    => "Display a user's personal information page",
1.50      raeburn  2331:                        'owin'       => "Open in a new window",
1.10      raeburn  2332:                        'modify'     => "Modify a user's information",
1.98      raeburn  2333:                        'track'      => "View a user's recent activity",
1.67      droeschl 2334:                        'clicker'    => "Clicker-ID",
1.1       raeburn  2335:                       );
1.2       raeburn  2336:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   2337:         $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.102     raeburn  2338:     } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
                   2339:         $lt{'extent'} = &mt('Communities: description, section(s), status');
1.13      raeburn  2340:     } elsif ($context eq 'author') {
1.2       raeburn  2341:         $lt{'extent'} = &mt('Author'); 
                   2342:     }
1.55      raeburn  2343:     my @cols;
                   2344:     if ($mode eq 'pickauthor') {
                   2345:         @cols = ('username','fullname','status','email');
                   2346:     } else {
                   2347:         @cols = ('username','domain','id','fullname');
                   2348:         if ($context eq 'course') {
                   2349:             push(@cols,'section');
                   2350:         }
1.102     raeburn  2351:         if (!($context eq 'domain' && ($env{'form.roletype'} eq 'course')
                   2352:                               && ($env{'form.roletype'} eq 'community'))) { 
1.55      raeburn  2353:             push(@cols,('start','end'));
                   2354:         }
                   2355:         if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
                   2356:             push(@cols,'role');
                   2357:         }
                   2358:         if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.102     raeburn  2359:                                     $env{'form.roletype'} eq 'course' ||
                   2360:                                     $env{'form.roletype'} eq 'community')) {
1.55      raeburn  2361:             push (@cols,'extent');
                   2362:         }
                   2363:         if (($statusmode eq 'Any') && 
1.102     raeburn  2364:             (!($context eq 'domain' && (($env{'form.roletype'} eq 'course')
                   2365:              || ($env{'form.roletype'} eq 'community'))))) {
1.55      raeburn  2366:             push(@cols,'status');
                   2367:         }
                   2368:         if ($context eq 'course') {
                   2369:             push(@cols,'groups');
                   2370:         }
                   2371:         push(@cols,'email');
1.2       raeburn  2372:     }
1.1       raeburn  2373: 
1.4       raeburn  2374:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2375:     if ($env{'form.showrole'} eq 'cr') {
                   2376:         $rolefilter = &mt('custom');  
                   2377:     } elsif ($env{'form.showrole'} ne 'Any') {
1.101     raeburn  2378:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
1.2       raeburn  2379:     }
1.16      raeburn  2380:     my $results_description;
                   2381:     if ($mode ne 'autoenroll') {
                   2382:         $results_description = &results_header_row($rolefilter,$statusmode,
1.102     raeburn  2383:                                                    $context,$permission,$mode,$crstype);
1.56      raeburn  2384:         $r->print('<b>'.$results_description.'</b><br /><br />');
1.16      raeburn  2385:     }
1.26      raeburn  2386:     my ($output,$actionselect,%canchange,%canchangesec);
1.55      raeburn  2387:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
                   2388:         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.16      raeburn  2389:             if ($permission->{'cusr'}) {
1.105     raeburn  2390:                 unless (($context eq 'domain') && 
                   2391:                         (($setting eq 'course') || ($setting eq 'community'))) {
                   2392:                     $actionselect = 
                   2393:                         &select_actions($context,$setting,$statusmode,$formname);
                   2394:                 }
1.16      raeburn  2395:             }
                   2396:             $r->print(<<END);
1.10      raeburn  2397: <input type="hidden" name="srchby"  value="uname" />
                   2398: <input type="hidden" name="srchin"   value="dom" />
                   2399: <input type="hidden" name="srchtype" value="exact" />
                   2400: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2401: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2402: END
1.16      raeburn  2403:             if ($actionselect) {
1.41      raeburn  2404:                 $output .= <<"END";
1.94      bisitz   2405: <div class="LC_left_float"><fieldset><legend>$lt{'ac'}</legend>
1.56      raeburn  2406: $actionselect
                   2407: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" /> &nbsp;
                   2408: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.$formname.actionlist)" /><br /><input type="button" value="$lt{'pr'}" onclick="javascript:verify_action('actionlist')" /></fieldset></div>
1.11      raeburn  2409: END
1.26      raeburn  2410:                 my @allroles;
                   2411:                 if ($env{'form.showrole'} eq 'Any') {
                   2412:                     my $custom = 1;
                   2413:                     if ($context eq 'domain') {
1.101     raeburn  2414:                         @allroles = &roles_by_context($setting,$custom,$crstype);
1.26      raeburn  2415:                     } else {
1.101     raeburn  2416:                         @allroles = &roles_by_context($context,$custom,$crstype);
1.26      raeburn  2417:                     }
                   2418:                 } else {
                   2419:                     @allroles = ($env{'form.showrole'});
                   2420:                 }
                   2421:                 foreach my $role (@allroles) {
                   2422:                     if ($context eq 'domain') {
                   2423:                         if ($setting eq 'domain') {
                   2424:                             if (&Apache::lonnet::allowed('c'.$role,
                   2425:                                     $env{'request.role.domain'})) {
                   2426:                                 $canchange{$role} = 1;
                   2427:                             }
1.31      raeburn  2428:                         } elsif ($setting eq 'author') {
                   2429:                             if (&Apache::lonnet::allowed('c'.$role,
                   2430:                                     $env{'request.role.domain'})) {
                   2431:                                 $canchange{$role} = 1;
                   2432:                             }
1.26      raeburn  2433:                         }
                   2434:                     } elsif ($context eq 'author') {
                   2435:                         if (&Apache::lonnet::allowed('c'.$role,
                   2436:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2437:                             $canchange{$role} = 1;
                   2438:                         }
                   2439:                     } elsif ($context eq 'course') {
                   2440:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2441:                             $canchange{$role} = 1;
                   2442:                         } elsif ($env{'request.course.sec'} ne '') {
                   2443:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2444:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2445:                             }
                   2446:                         }
                   2447:                     }
                   2448:                 }
1.16      raeburn  2449:             }
1.94      bisitz   2450:             $output .= '<div class="LC_left_float"><fieldset><legend>'.$lt{'link'}.'</legend>'.
1.56      raeburn  2451:                        '<table><tr>';
                   2452:             my @linkdests = ('aboutme');
                   2453:             if ($permission->{'cusr'}) {
                   2454:                 unshift (@linkdests,'modify');
                   2455:             }
1.98      raeburn  2456:             if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
                   2457:                 &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
                   2458:                                          $env{'request.course.sec'})) {
                   2459:                 push(@linkdests,'track');
                   2460:             }
                   2461: 
1.56      raeburn  2462:             $output .= '<td>';
                   2463:             my $usernamelink = $env{'form.usernamelink'};
                   2464:             if ($usernamelink eq '') {
                   2465:                 $usernamelink = 'aboutme';
                   2466:             }
                   2467:             foreach my $item (@linkdests) {
                   2468:                 my $checkedstr = '';
                   2469:                 if ($item eq $usernamelink) {
1.86      bisitz   2470:                     $checkedstr = ' checked="checked"';
1.56      raeburn  2471:                 }
1.86      bisitz   2472:                 $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' />&nbsp;'.$lt{$item}.'</label></span><br />';
1.56      raeburn  2473:             }
                   2474:             my $checkwin;
                   2475:             if ($env{'form.userwin'}) {
1.86      bisitz   2476:                 $checkwin = ' checked="checked"';
1.56      raeburn  2477:             }
1.110     bisitz   2478:             $output .= '</td><td valign="top"  style="border-left: 1px solid;"><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
1.4       raeburn  2479:         }
1.56      raeburn  2480:         $output .= "\n".'<div class="LC_clear_float_footer">&nbsp;</div>'."\n".
1.1       raeburn  2481:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2482:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2483:         if ($mode eq 'autoenroll') {
1.4       raeburn  2484:             $output .= "
1.55      raeburn  2485:  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2486:             ";
1.1       raeburn  2487:         } else {
1.105     raeburn  2488:             $output .= "\n".'<th>&nbsp;</th>'."\n";
1.11      raeburn  2489:             if ($actionselect) {
                   2490:                 $output .= '<th>'.&mt('Select').'</th>'."\n";
                   2491:             }
1.1       raeburn  2492:         }
                   2493:         foreach my $item (@cols) {
1.55      raeburn  2494:             $output .= "<th><a href=\"javascript:document.$formname.sortby.value='$item';document.$formname.submit();\">$lt{$item}</a></th>\n";
1.1       raeburn  2495:         }
1.2       raeburn  2496:         my %role_types = &role_type_names();
1.16      raeburn  2497:         if ($context eq 'course' && $mode ne 'autoenroll') {
1.4       raeburn  2498:             if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2499:                 # Clicker display on or off?
1.58      bisitz   2500:                 my %clicker_options = (
                   2501:                                         'on' => 'Show',
                   2502:                                         'off' => 'Hide',
                   2503:                                       );
1.4       raeburn  2504:                 my $clickerchg = 'on';
                   2505:                 if ($displayclickers eq 'on') {
                   2506:                     $clickerchg = 'off';
                   2507:                 }
1.58      bisitz   2508:                 $output .= '    <th>'."\n".'     '
                   2509:                         .&mt('[_1]'.$clicker_options{$clickerchg}.'[_2] clicker id'
                   2510:                             ,'<a href="javascript:document.'.$formname.'.displayclickers.value='
                   2511:                              ."'".$clickerchg."'".';document.'.$formname.'.submit();">'
                   2512:                             ,'</a>')
                   2513:                         ."\n".'    </th>'."\n";
1.1       raeburn  2514: 
1.4       raeburn  2515:                 # Photo display on or off?
                   2516:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2517:                     my %photo_options = &Apache::lonlocal::texthash(
                   2518:                                                             'on' => 'Show',
                   2519:                                                             'off' => 'Hide',
                   2520:                                                                 );
                   2521:                     my $photochg = 'on';
                   2522:                     if ($displayphotos eq 'on') {
                   2523:                         $photochg = 'off';
                   2524:                     }
                   2525:                     $output .= '    <th>'."\n".'     '.
1.55      raeburn  2526:                 '<a href="javascript:document.'.$formname.'.displayphotos.value='.
                   2527:                       "'".$photochg."'".';document.'.$formname.'.submit();">'.
1.1       raeburn  2528:                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
1.4       raeburn  2529:                       '    </th>'."\n";
                   2530:                 }
1.1       raeburn  2531:             }
1.4       raeburn  2532:         }
1.16      raeburn  2533:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2534: # Done with the HTML header line
                   2535:     } elsif ($mode eq 'csv') {
                   2536:         #
                   2537:         # Open a file
                   2538:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2539:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2540:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2541:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2542:             $r->log_error("Couldn't open $CSVfilename for output $!");
1.108     bisitz   2543:             $r->print(
                   2544:                 '<p class="LC_error">'
                   2545:                .&mt('Problems occurred in writing the CSV file.')
                   2546:                .' '.&mt('This error has been logged.')
                   2547:                .' '.&mt('Please alert your LON-CAPA administrator.')
                   2548:                .'</p>'
                   2549:             );
1.1       raeburn  2550:             $CSVfile = undef;
                   2551:         }
                   2552:         #
1.67      droeschl 2553:         push @cols,'clicker';
1.1       raeburn  2554:         # Write headers and data to file
1.2       raeburn  2555:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2556:         print $CSVfile '"'.join('","',map {
                   2557:             &Apache::loncommon::csv_translate($lt{$_})
1.67      droeschl 2558:             } (@cols))."\"\n";
1.1       raeburn  2559:     } elsif ($mode eq 'excel') {
1.67      droeschl 2560:         push @cols,'clicker';
1.1       raeburn  2561:         # Create the excel spreadsheet
                   2562:         ($excel_workbook,$excel_filename,$format) =
                   2563:             &Apache::loncommon::create_workbook($r);
                   2564:         return if (! defined($excel_workbook));
                   2565:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2566:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2567:         #
                   2568:         my @colnames = map {$lt{$_}} (@cols);
1.67      droeschl 2569: 
1.1       raeburn  2570:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2571:     }
                   2572: 
                   2573: # Done with header lines in all formats
                   2574:     my %index;
                   2575:     my $i;
1.2       raeburn  2576:     foreach my $idx (@$keylist) {
                   2577:         $index{$idx} = $i++;
                   2578:     }
1.4       raeburn  2579:     my $usercount = 0;
1.33      raeburn  2580:     my ($secfilter,$grpfilter);
                   2581:     if ($context eq 'course') {
                   2582:         $secfilter = $env{'form.secfilter'};
                   2583:         $grpfilter = $env{'form.grpfilter'};
                   2584:         if ($secfilter eq '') {
                   2585:             $secfilter = 'all';
                   2586:         }
                   2587:         if ($grpfilter eq '') {
                   2588:             $grpfilter = 'all';
                   2589:         }
                   2590:     }
1.83      raeburn  2591:     my %ltstatus = &Apache::lonlocal::texthash(
                   2592:                                                 Active  => 'Active',
                   2593:                                                 Future  => 'Future',
                   2594:                                                 Expired => 'Expired',
                   2595:                                                );
1.2       raeburn  2596:     # Get groups, role, permanent e-mail so we can sort on them if
                   2597:     # necessary.
                   2598:     foreach my $user (keys(%{$userlist})) {
1.43      raeburn  2599:         if ($user eq '' ) {
                   2600:             delete($userlist->{$user});
                   2601:             next;
                   2602:         }
1.11      raeburn  2603:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2604:             delete($userlist->{$user});
                   2605:             next;
                   2606:         }
1.2       raeburn  2607:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2608:         if (($statusmode ne 'Any') && 
                   2609:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2610:             delete($userlist->{$user});
                   2611:             next;
                   2612:         }
1.2       raeburn  2613:         if ($context eq 'domain') {
                   2614:             if ($env{'form.roletype'} eq 'domain') {
                   2615:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2616:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2617:                     ($udom eq $env{'request.role.domain'})) {
                   2618:                     delete($userlist->{$user});
                   2619:                     next;
                   2620:                 }
1.13      raeburn  2621:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2622:                 ($uname,$udom,$role) = split(/:/,$user,-1);
1.102     raeburn  2623:             } elsif (($env{'form.roletype'} eq 'course') || 
                   2624:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2625:                 ($uname,$udom,$role) = split(/:/,$user);
                   2626:             }
                   2627:         } else {
                   2628:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   2629:             if (($context eq 'course') && $role eq '') {
                   2630:                 $role = 'st';
                   2631:             }
                   2632:         }
                   2633:         $userlist->{$user}->[$index{'role'}] = $role;
                   2634:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   2635:             delete($userlist->{$user});
                   2636:             next;
                   2637:         }
1.33      raeburn  2638:         if ($context eq 'course') {
                   2639:             my @ac_groups;
                   2640:             if (ref($classgroups) eq 'HASH') {
                   2641:                 $groups = $classgroups->{$user};
                   2642:             }
                   2643:             if (ref($groups->{'active'}) eq 'HASH') {
                   2644:                 @ac_groups = keys(%{$groups->{'active'}});
                   2645:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   2646:             }
                   2647:             if ($mode ne 'autoenroll') {
                   2648:                 my $section = $userlist->{$user}->[$index{'section'}];
1.43      raeburn  2649:                 if (($env{'request.course.sec'} ne '') && 
                   2650:                     ($section ne $env{'request.course.sec'})) {
                   2651:                     if ($role eq 'st') {
                   2652:                         delete($userlist->{$user});
                   2653:                         next;
                   2654:                     }
                   2655:                 }
1.33      raeburn  2656:                 if ($secfilter eq 'none') {
                   2657:                     if ($section ne '') {
                   2658:                         delete($userlist->{$user});
                   2659:                         next;
                   2660:                     }
                   2661:                 } elsif ($secfilter ne 'all') {
                   2662:                     if ($section ne $secfilter) {
                   2663:                         delete($userlist->{$user});
                   2664:                         next;
                   2665:                     }
                   2666:                 }
                   2667:                 if ($grpfilter eq 'none') {
                   2668:                     if (@ac_groups > 0) {
                   2669:                         delete($userlist->{$user});
                   2670:                         next;
                   2671:                     }
                   2672:                 } elsif ($grpfilter ne 'all') {
                   2673:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   2674:                         delete($userlist->{$user});
                   2675:                         next;
                   2676:                     }
                   2677:                 }
1.44      raeburn  2678:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2679:                     if (($displayphotos eq 'on') && ($role eq 'st')) {
                   2680:                         $userlist->{$user}->[$index{'photo'}] =
1.47      raeburn  2681:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
                   2682:                         $userlist->{$user}->[$index{'thumbnail'}] =
1.44      raeburn  2683:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,
                   2684:                                                                 'gif','thumbnail');
                   2685:                     }
                   2686:                 }
1.33      raeburn  2687:             }
1.2       raeburn  2688:         }
                   2689:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   2690:         if ($emails{'permanentemail'} =~ /\S/) {
                   2691:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   2692:         }
1.4       raeburn  2693:         $usercount ++;
                   2694:     }
                   2695:     my $autocount = 0;
                   2696:     my $manualcount = 0;
                   2697:     my $lockcount = 0;
                   2698:     my $unlockcount = 0;
                   2699:     if ($usercount) {
                   2700:         $r->print($output);
                   2701:     } else {
                   2702:         if ($mode eq 'autoenroll') {
                   2703:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   2704:         } else {
                   2705:             return;
                   2706:         }
1.1       raeburn  2707:     }
1.2       raeburn  2708:     #
                   2709:     # Sort the users
1.1       raeburn  2710:     my $index  = $index{$sortby};
                   2711:     my $second = $index{'username'};
                   2712:     my $third  = $index{'domain'};
1.2       raeburn  2713:     my @sorted_users = sort {
                   2714:         lc($userlist->{$a}->[$index])  cmp lc($userlist->{$b}->[$index])
1.1       raeburn  2715:             ||
1.2       raeburn  2716:         lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second])            ||
                   2717:         lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   2718:         } (keys(%$userlist));
1.4       raeburn  2719:     my $rowcount = 0;
1.2       raeburn  2720:     foreach my $user (@sorted_users) {
1.4       raeburn  2721:         my %in;
1.2       raeburn  2722:         my $sdata = $userlist->{$user};
1.4       raeburn  2723:         $rowcount ++; 
1.2       raeburn  2724:         foreach my $item (@{$keylist}) {
                   2725:             $in{$item} = $sdata->[$index{$item}];
                   2726:         }
1.67      droeschl 2727:         my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   2728:         if ($clickers!~/\w/) { $clickers='-'; }
                   2729:         $in{'clicker'} = $clickers; 
                   2730: 	my $role = $in{'role'};
1.102     raeburn  2731:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
1.2       raeburn  2732:         if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2733:             $in{'start'} = &mt('none');
                   2734:         } else {
                   2735:             $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
1.1       raeburn  2736:         }
1.2       raeburn  2737:         if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2738:             $in{'end'} = &mt('none');
1.1       raeburn  2739:         } else {
1.2       raeburn  2740:             $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1       raeburn  2741:         }
1.55      raeburn  2742:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2743:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  2744:             my $checkval;
1.16      raeburn  2745:             if ($mode eq 'autoenroll') {
                   2746:                 my $cellentry;
                   2747:                 if ($in{'type'} eq 'auto') {
                   2748:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label>';
                   2749:                     $autocount ++;
                   2750:                 } else {
1.76      bisitz   2751:                     $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><span class="LC_nobreak"><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label></span></td></tr><tr><td><span class="LC_nobreak">';
1.16      raeburn  2752:                     $manualcount ++;
                   2753:                     if ($in{'lockedtype'}) {
                   2754:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Unlock').'</label>';
                   2755:                         $unlockcount ++;
                   2756:                     } else {
                   2757:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Lock').'</label>';
                   2758:                         $lockcount ++;
1.11      raeburn  2759:                     }
1.76      bisitz   2760:                     $cellentry .= '</span></td></tr></table>';
1.16      raeburn  2761:                 }
                   2762:                 $r->print("<td>$cellentry</td>\n");
                   2763:             } else {
1.55      raeburn  2764:                 if ($mode ne 'pickauthor') {  
                   2765:                     $r->print("<td>$rowcount</td>\n");
                   2766:                 }
1.16      raeburn  2767:                 if ($actionselect) {
1.26      raeburn  2768:                     my $showcheckbox;
                   2769:                     if ($role =~ /^cr\//) {
                   2770:                         $showcheckbox = $canchange{'cr'};
                   2771:                     } else {
                   2772:                         $showcheckbox = $canchange{$role};
                   2773:                     }
                   2774:                     if (!$showcheckbox) {
                   2775:                         if ($context eq 'course') {
                   2776:                             if ($canchangesec{$role} ne '') {
                   2777:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   2778:                                     $showcheckbox = 1;
                   2779:                                 }
                   2780:                             }
1.16      raeburn  2781:                         }
1.26      raeburn  2782:                     }
                   2783:                     if ($showcheckbox) {
                   2784:                         $checkval = $user; 
                   2785:                         if ($context eq 'course') {
                   2786:                             if ($role eq 'st') {
                   2787:                                 $checkval .= ':st';
                   2788:                             }
                   2789:                             $checkval .= ':'.$in{'section'};
                   2790:                             if ($role eq 'st') {
                   2791:                                 $checkval .= ':'.$in{'type'}.':'.
                   2792:                                              $in{'lockedtype'};
                   2793:                             }
1.16      raeburn  2794:                         }
1.26      raeburn  2795:                         $r->print('<td><input type="checkbox" name="'.
1.86      bisitz   2796:                                   'actionlist" value="'.$checkval.'" /></td>');
1.26      raeburn  2797:                     } else {
                   2798:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  2799:                     }
1.55      raeburn  2800:                 } elsif ($mode eq 'pickauthor') {
                   2801:                         $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
1.11      raeburn  2802:                 }
                   2803:             }
1.2       raeburn  2804:             foreach my $item (@cols) {
1.10      raeburn  2805:                 if ($item eq 'username') {
1.48      raeburn  2806:                     $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
1.16      raeburn  2807:                 } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11      raeburn  2808:                     $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.83      raeburn  2809:                 } elsif ($item eq 'status') {
                   2810:                     my $showitem = $in{$item};
                   2811:                     if (defined($ltstatus{$in{$item}})) {
                   2812:                         $showitem = $ltstatus{$in{$item}};
                   2813:                     }
                   2814:                     $r->print('<td>'.$showitem.'</td>'."\n");
1.10      raeburn  2815:                 } else {
                   2816:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   2817:                 }
1.2       raeburn  2818:             }
1.16      raeburn  2819:             if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4       raeburn  2820:                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2821:                     if ($displayclickers eq 'on') {
                   2822:                         my $clickers =
1.2       raeburn  2823:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4       raeburn  2824:                         if ($clickers!~/\w/) { $clickers='-'; }
                   2825:                         $r->print('<td>'.$clickers.'</td>');
1.2       raeburn  2826:                     } else {
                   2827:                         $r->print('    <td>&nbsp;</td>  ');
                   2828:                     }
1.4       raeburn  2829:                     if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.44      raeburn  2830:                         if ($displayphotos eq 'on' && $role eq 'st' && $in{'photo'} ne '') {
1.90      bisitz   2831:                             $r->print('    <td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
1.4       raeburn  2832:                         } else {
                   2833:                             $r->print('    <td>&nbsp;</td>  ');
                   2834:                         }
                   2835:                     }
1.2       raeburn  2836:                 }
                   2837:             }
                   2838:             $r->print(&Apache::loncommon::end_data_table_row());
                   2839:         } elsif ($mode eq 'csv') {
                   2840:             next if (! defined($CSVfile));
                   2841:             # no need to bother with $linkto
                   2842:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2843:                 $in{'start'} = &mt('none');
                   2844:             } else {
                   2845:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   2846:             }
                   2847:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2848:                 $in{'end'} = &mt('none');
                   2849:             } else {
                   2850:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   2851:             }
                   2852:             my @line = ();
                   2853:             foreach my $item (@cols) {
                   2854:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   2855:             }
1.67      droeschl 2856:             print $CSVfile '"'.join('","',@line)."\"\n";
1.2       raeburn  2857:         } elsif ($mode eq 'excel') {
                   2858:             my $col = 0;
                   2859:             foreach my $item (@cols) {
                   2860:                 if ($item eq 'start' || $item eq 'end') {
                   2861:                     if (defined($item) && $item != 0) {
                   2862:                         $excel_sheet->write($row,$col++,
                   2863:                             &Apache::lonstathelpers::calc_serial($in{item}),
                   2864:                                     $format->{'date'});
                   2865:                     } else {
                   2866:                         $excel_sheet->write($row,$col++,'none');
                   2867:                     }
                   2868:                 } else {
                   2869:                     $excel_sheet->write($row,$col++,$in{$item});
                   2870:                 }
                   2871:             }
                   2872:             $row++;
1.1       raeburn  2873:         }
                   2874:     }
1.55      raeburn  2875:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2876:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   2877:     } elsif ($mode eq 'excel') {
                   2878:         $excel_workbook->close();
1.68      droeschl 2879: 	$r->print(&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<p><a href="'.$excel_filename.'">','</a>')."</p>\n");
1.2       raeburn  2880:     } elsif ($mode eq 'csv') {
                   2881:         close($CSVfile);
1.68      droeschl 2882: 	$r->print(&mt('[_1]Your CSV file[_2] is ready for download.', '<p><a href="'.$CSVfilename.'">','</a>')."</p>\n");
1.2       raeburn  2883:         $r->rflush();
                   2884:     }
                   2885:     if ($mode eq 'autoenroll') {
                   2886:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  2887:     } else {
                   2888:         return ($usercount);
1.2       raeburn  2889:     }
1.1       raeburn  2890: }
                   2891: 
1.56      raeburn  2892: sub bulkaction_javascript {
                   2893:     my ($formname,$caller) = @_;
                   2894:     my $docstart = 'document';
                   2895:     if ($caller eq 'popup') {
                   2896:         $docstart = 'opener.document';
                   2897:     }
                   2898:     my %lt = &Apache::lonlocal::texthash(
                   2899:               acwi => 'Access will be set to start immediately',
                   2900:               asyo => 'as you did not select an end date in the pop-up window',
                   2901:               accw => 'Access will be set to continue indefinitely',
                   2902:               asyd => 'as you did not select an end date in the pop-up window',
                   2903:               sewi => "Sections will be switched to 'No section'",
                   2904:               ayes => "as you either selected the 'No section' option",
                   2905:               oryo => 'or you did not select a section in the pop-up window',
                   2906:               arol => 'A role with no section will be added',
                   2907:               swbs => 'Sections will be switched to:',
                   2908:               rwba => 'Roles will be added for section(s):',
                   2909:             );
                   2910:     my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
                   2911:     my $noaction = &mt("You need to select an action to take for the user(s) you have selected"); 
                   2912:     my $singconfirm = &mt(' for a single user?');
                   2913:     my $multconfirm = &mt(' for multiple users?');
                   2914:     my $output = <<"ENDJS";
                   2915: function verify_action (field) {
                   2916:     var numchecked = 0;
                   2917:     var singconf = '$singconfirm';
                   2918:     var multconf = '$multconfirm';
                   2919:     if ($docstart.$formname.elements[field].length > 0) {
                   2920:         for (i=0; i<$docstart.$formname.elements[field].length; i++) {
                   2921:             if ($docstart.$formname.elements[field][i].checked == true) {
                   2922:                numchecked ++;
                   2923:             }
                   2924:         }
                   2925:     } else {
                   2926:         if ($docstart.$formname.elements[field].checked == true) {
                   2927:             numchecked ++;
                   2928:         }
                   2929:     }
                   2930:     if (numchecked == 0) {
                   2931:         alert("$alert");
                   2932:         return;
                   2933:     } else {
                   2934:         var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
                   2935:         var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
                   2936:         if (choice == '') {
                   2937:             alert("$noaction");
                   2938:             return;
                   2939:         } else {
                   2940:             if (numchecked == 1) {
                   2941:                 message += singconf;
                   2942:             } else {
                   2943:                 message += multconf;
                   2944:             }
                   2945: ENDJS
                   2946:     if ($caller ne 'popup') {
                   2947:         $output .= <<"NEWWIN";
                   2948:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
                   2949:                 opendatebrowser(document.$formname,'$formname','go');
                   2950:                 return;
                   2951: 
                   2952:             } else {
                   2953:                 if (confirm(message)) {
                   2954:                     document.$formname.phase.value = 'bulkchange';
                   2955:                     document.$formname.submit();
                   2956:                     return;
                   2957:                 }
                   2958:             }
                   2959: NEWWIN
                   2960:     } else {
                   2961:         $output .= <<"POPUP";
                   2962:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
                   2963:                 var datemsg = '';
                   2964:                 if (($docstart.$formname.startdate_month.value == '') &&
                   2965:                     ($docstart.$formname.startdate_day.value  == '') &&
                   2966:                     ($docstart.$formname.startdate_year.value == '')) {
                   2967:                     datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
                   2968:                 }
                   2969:                 if (($docstart.$formname.enddate_month.value == '') &&
                   2970:                     ($docstart.$formname.enddate_day.value  == '') &&
                   2971:                     ($docstart.$formname.enddate_year.value == '')) {
                   2972:                     datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
                   2973:                 }
                   2974:                 if (datemsg != '') {
                   2975:                     message += "\\n"+datemsg;
                   2976:                 }
                   2977:             }
                   2978:             if (choice == 'chgsec') {
                   2979:                 var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
                   2980:                 var retained =  $docstart.$formname.retainsec.value;
                   2981:                 var secshow = $docstart.$formname.newsecs.value;
                   2982:                 if (secshow == '') {
                   2983:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   2984:                         message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   2985:                     } else {
                   2986:                         message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   2987:                     }
                   2988:                 } else {
                   2989:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   2990:                         message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
                   2991:                     } else {
                   2992:                         message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
                   2993:                     }
                   2994:                 }
                   2995:             }
                   2996:             if (confirm(message)) {
                   2997:                 $docstart.$formname.phase.value = 'bulkchange';
                   2998:                 $docstart.$formname.submit();
                   2999:                 window.close();
                   3000:             }
                   3001: POPUP
                   3002:     }
                   3003:     $output .= '
                   3004:         }
                   3005:     }
                   3006: }
                   3007: ';
                   3008:     return $output;
                   3009: }
                   3010: 
1.10      raeburn  3011: sub print_username_link {
1.48      raeburn  3012:     my ($mode,$in) = @_;
1.10      raeburn  3013:     my $output;
1.16      raeburn  3014:     if ($mode eq 'autoenroll') {
                   3015:         $output = $in->{'username'};
1.10      raeburn  3016:     } else {
                   3017:         $output = '<a href="javascript:username_display_launch('.
1.112     bisitz   3018:                   "'$in->{'username'}','$in->{'domain'}'".')">'.
1.10      raeburn  3019:                   $in->{'username'}.'</a>';
                   3020:     }
                   3021:     return $output;
                   3022: }
                   3023: 
1.2       raeburn  3024: sub role_type_names {
                   3025:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  3026:                          'domain' => 'Domain Roles',
                   3027:                          'author' => 'Co-Author Roles',
                   3028:                          'course' => 'Course Roles',
1.101     raeburn  3029:                          'community' => 'Community Roles',
1.2       raeburn  3030:              );
                   3031:     return %lt;
                   3032: }
                   3033: 
1.11      raeburn  3034: sub select_actions {
1.55      raeburn  3035:     my ($context,$setting,$statusmode,$formname) = @_;
1.11      raeburn  3036:     my %lt = &Apache::lonlocal::texthash(
                   3037:                 revoke   => "Revoke user roles",
                   3038:                 delete   => "Delete user roles",
                   3039:                 reenable => "Re-enable expired user roles",
                   3040:                 activate => "Make future user roles active now",
                   3041:                 chgdates  => "Change starting/ending dates",
                   3042:                 chgsec   => "Change section associated with user roles",
                   3043:     );
                   3044:     my ($output,$options,%choices);
1.23      raeburn  3045:     # FIXME Disable actions for now for roletype=course in domain context
                   3046:     if ($context eq 'domain' && $setting eq 'course') {
                   3047:         return;
                   3048:     }
1.26      raeburn  3049:     if ($context eq 'course') {
                   3050:         if ($env{'form.showrole'} ne 'Any') {
                   3051:              if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   3052:                                            $env{'request.course.id'})) {
                   3053:                  if ($env{'request.course.sec'} eq '') {
                   3054:                      return;
                   3055:                  } else {
                   3056:                      if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   3057:                          return;
                   3058:                      }
                   3059:                  }
                   3060:             }
                   3061:         }
                   3062:     }
1.11      raeburn  3063:     if ($statusmode eq 'Any') {
                   3064:         $options .= '
                   3065: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   3066:         $choices{'dates'} = 1;
                   3067:     } else {
                   3068:         if ($statusmode eq 'Future') {
                   3069:             $options .= '
                   3070: <option value="activate">'.$lt{'activate'}.'</option>';
                   3071:             $choices{'dates'} = 1;
                   3072:         } elsif ($statusmode eq 'Expired') {
                   3073:             $options .= '
                   3074: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   3075:             $choices{'dates'} = 1;
                   3076:         }
1.13      raeburn  3077:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   3078:             $options .= '
                   3079: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   3080: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   3081:             $choices{'dates'} = 1;
                   3082:         }
1.11      raeburn  3083:     }
                   3084:     if ($context eq 'domain') {
                   3085:         $options .= '
                   3086: <option value="delete">'.$lt{'delete'}.'</option>';
                   3087:     }
                   3088:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  3089:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  3090:             $options .= '
                   3091: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   3092:             $choices{'sections'} = 1;
                   3093:         }
                   3094:     }
                   3095:     if ($options) {
1.56      raeburn  3096:         $output = '<select name="bulkaction">'."\n".
1.11      raeburn  3097:                   '<option value="" selected="selected">'.
                   3098:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   3099:         if ($choices{'dates'}) {
                   3100:             $output .= 
                   3101:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   3102:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   3103:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   3104:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   3105:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   3106:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   3107:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   3108:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   3109:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   3110:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   3111:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
1.56      raeburn  3112:                 '<input type="hidden" name="enddate_second" value="" />'."\n".
                   3113:                 '<input type="hidden" name="no_end_date" value="" />'."\n";
1.11      raeburn  3114:             if ($context eq 'course') {
                   3115:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   3116:             }
                   3117:         }
                   3118:         if ($choices{'sections'}) {
1.91      bisitz   3119:             $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
                   3120:                        '<input type="hidden" name="newsecs" value="" />'."\n";
1.11      raeburn  3121:         }
                   3122:     }
                   3123:     return $output;
                   3124: }
                   3125: 
                   3126: sub date_section_javascript {
                   3127:     my ($context,$setting) = @_;
1.49      raeburn  3128:     my $title = 'Date_And_Section_Selector';
1.41      raeburn  3129:     my %nopopup = &Apache::lonlocal::texthash (
                   3130:         revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
                   3131:         delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
                   3132:         none   => "Choose an action to take for selected users",
                   3133:     );  
1.96      bisitz   3134:     my $output = <<"ENDONE";
                   3135: <script type="text/javascript">
                   3136: // <![CDATA[
1.41      raeburn  3137:     function opendatebrowser(callingform,formname,calledby) {
1.11      raeburn  3138:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   3139:         var url = '/adm/createuser?';
                   3140:         var type = '';
                   3141:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   3142: ENDONE
                   3143:     if ($context eq 'domain') {
                   3144:         $output .= '
                   3145:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   3146: ';
                   3147:     }
                   3148:     my $width= '700';
                   3149:     my $height = '400';
                   3150:     $output .= <<"ENDTWO";
                   3151:         url += 'action=dateselect&callingform=' + formname + 
                   3152:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   3153:         var title = '$title';
                   3154:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   3155:         options += ',width=$width,height=$height';
                   3156:         stdeditbrowser = open(url,title,options,'1');
                   3157:         stdeditbrowser.focus();
                   3158:     }
1.96      bisitz   3159: // ]]>
1.11      raeburn  3160: </script>
                   3161: ENDTWO
                   3162:     return $output;
                   3163: }
                   3164: 
                   3165: sub date_section_selector {
1.101     raeburn  3166:     my ($context,$permission,$crstype) = @_;
1.11      raeburn  3167:     my $callingform = $env{'form.callingform'};
                   3168:     my $formname = 'dateselect';  
                   3169:     my $groupslist = &get_groupslist();
                   3170:     my $sec_js = &setsections_javascript($formname,$groupslist);
                   3171:     my $output = <<"END";
                   3172: <script type="text/javascript">
1.96      bisitz   3173: // <![CDATA[
1.11      raeburn  3174: 
                   3175: $sec_js
                   3176: 
                   3177: function saveselections(formname) {
                   3178: 
                   3179: END
                   3180:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3181:         $output .= <<"END";
1.40      raeburn  3182:         if (formname.retainsec.length > 1) {  
                   3183:             for (var i=0; i<formname.retainsec.length; i++) {
                   3184:                 if (formname.retainsec[i].checked == true) {
                   3185:                     opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
                   3186:                 }
                   3187:             }
                   3188:         } else {
                   3189:             opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   3190:         }
1.103     raeburn  3191:         setSections(formname,'$crstype');
1.11      raeburn  3192:         if (seccheck == 'ok') {
                   3193:             opener.document.$callingform.newsecs.value = formname.sections.value;
                   3194:         }
                   3195: END
                   3196:     } else {
                   3197:         if ($context eq 'course') {
                   3198:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   3199:                 ($env{'form.bulkaction'} eq 'activate') || 
                   3200:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  3201:                 if ($env{'request.course.sec'} eq '') {
                   3202:                     $output .= <<"END";
1.11      raeburn  3203:  
                   3204:         if (formname.makedatesdefault.checked == true) {
                   3205:             opener.document.$callingform.makedatesdefault.value = 1;
                   3206:         }
                   3207:         else {
                   3208:             opener.document.$callingform.makedatesdefault.value = 0;
                   3209:         }
                   3210: 
                   3211: END
1.26      raeburn  3212:                 }
1.11      raeburn  3213:             }
                   3214:         }
                   3215:         $output .= <<"END";
                   3216:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   3217:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   3218:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   3219:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   3220:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   3221:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   3222:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   3223:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   3224:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   3225:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   3226:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   3227:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
1.56      raeburn  3228:     if (formname.no_end_date.checked) {
                   3229:         opener.document.$callingform.no_end_date.value = '1';
                   3230:     } else {
                   3231:         opener.document.$callingform.no_end_date.value = '0';
                   3232:     }
1.11      raeburn  3233: END
                   3234:     }
1.56      raeburn  3235:     my $verify_action_js = &bulkaction_javascript($callingform,'popup');
                   3236:     $output .= <<"ENDJS";
                   3237:     verify_action('actionlist');
1.11      raeburn  3238: }
1.56      raeburn  3239: 
                   3240: $verify_action_js
                   3241: 
1.96      bisitz   3242: // ]]>
1.11      raeburn  3243: </script>
1.56      raeburn  3244: ENDJS
1.11      raeburn  3245:     my %lt = &Apache::lonlocal::texthash (
                   3246:                  chac => 'Access dates to apply for selected users',
                   3247:                  chse => 'Changes in section affiliation to apply to selected users',
1.118     raeburn  3248:                  fors => 'For student roles, changing the section will result in a section switch as students may only be in one section of a course at a time.',
                   3249:                  forn => 'For a course role that is not "student", users may have roles in more than one section at a time.',
                   3250:                  reta => "Retain each user's current section affiliations?",
1.103     raeburn  3251:                  dnap => '(Does not apply to student roles).',
1.11      raeburn  3252:             );
                   3253:     my ($date_items,$headertext);
                   3254:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3255:         $headertext = $lt{'chse'};
                   3256:     } else {
                   3257:         $headertext = $lt{'chac'};
                   3258:         my $starttime;
                   3259:         if (($env{'form.bulkaction'} eq 'activate') || 
                   3260:             ($env{'form.bulkaction'} eq 'reenable')) {
                   3261:             $starttime = time;
                   3262:         }
                   3263:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  3264:                                           $env{'form.bulkaction'},$formname,
1.101     raeburn  3265:                                           $permission,$crstype);
1.11      raeburn  3266:     }
                   3267:     $output .= '<h3>'.$headertext.'</h3>'.
1.118     raeburn  3268:                '<form name="'.$formname.'" method="post" action="">'."\n".
1.11      raeburn  3269:                 $date_items;
                   3270:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  3271:         my ($cnum,$cdom) = &get_course_identity();
1.103     raeburn  3272:         if ($crstype eq 'Community') {
1.118     raeburn  3273:             $lt{'fors'} = &mt('For member roles, changing the section will result in a section switch, as members may only be in one section of a community at a time.');
                   3274:             $lt{'forn'} = &mt('For a community role that is not "member", users may have roles in more than one section at a time.');
1.103     raeburn  3275:             $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
                   3276:         }
1.11      raeburn  3277:         my $info;
                   3278:         if ($env{'form.showrole'} eq 'st') {
                   3279:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  3280:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  3281:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   3282:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   3283:             $info = $lt{'reta'};
                   3284:         } else {
                   3285:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   3286:             $info = $lt{'reta'};
                   3287:         }
                   3288:         if ($info) {
                   3289:             $info .= '<span class="LC_nobreak">'.
                   3290:                      '<label><input type="radio" name="retainsec" value="1" '.
                   3291:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   3292:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   3293:                      &mt('No').'</label></span>';
                   3294:             if ($env{'form.showrole'} eq 'Any') {
                   3295:                 $info .= '<br />'.$lt{'dnap'};
                   3296:             }
                   3297:             $info .= '</p>';
                   3298:         } else {
                   3299:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   3300:         }
1.21      raeburn  3301:         my $rowtitle = &mt('New section to assign');
1.101     raeburn  3302:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context,'',$crstype);
1.11      raeburn  3303:         $output .= $info.$secbox;
                   3304:     }
                   3305:     $output .= '<p>'.
1.81      schafran 3306: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
1.11      raeburn  3307: '</form>';
                   3308:     return $output;
                   3309: }
                   3310: 
1.17      raeburn  3311: sub section_picker {
1.101     raeburn  3312:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype) = @_;
1.17      raeburn  3313:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3314:     my $sections_select .= &course_sections(\%sections_count,$role);
1.118     raeburn  3315:     my $secbox = '<div>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
1.17      raeburn  3316:     if ($mode eq 'upload') {
                   3317:         my ($options,$cb_script,$coursepick) =
1.101     raeburn  3318:             &default_role_selector($context,1,$crstype);
1.59      bisitz   3319:         $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
1.17      raeburn  3320:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3321:     }
                   3322:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
                   3323:     if ($env{'request.course.sec'} eq '') {
                   3324:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   3325:                    '<td align="center">'.&mt('Existing sections')."\n".
                   3326:                    '<br />'.$sections_select.'</td><td align="center">'.
                   3327:                    &mt('New section').'<br />'."\n".
1.118     raeburn  3328:                    '<input type="text" name="newsec" size="15" value="" />'."\n".
1.17      raeburn  3329:                    '<input type="hidden" name="sections" value="" />'."\n".
                   3330:                    '</td></tr></table>'."\n";
                   3331:     } else {
                   3332:        $secbox .= '<input type="hidden" name="sections" value="'.
                   3333:                    $env{'request.course.sec'}.'" />'.
                   3334:                    $env{'request.course.sec'};
                   3335:     }
                   3336:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
1.118     raeburn  3337:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
1.17      raeburn  3338:     return $secbox;
                   3339: }
                   3340: 
1.2       raeburn  3341: sub results_header_row {
1.102     raeburn  3342:     my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
1.5       raeburn  3343:     my ($description,$showfilter);
                   3344:     if ($rolefilter ne 'Any') {
                   3345:         $showfilter = $rolefilter;
                   3346:     }
1.2       raeburn  3347:     if ($context eq 'course') {
1.24      raeburn  3348:         if ($mode eq 'csv' || $mode eq 'excel') {
1.102     raeburn  3349:             if ($crstype eq 'Community') {
                   3350:                 $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3351:             } else {
                   3352:                 $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3353:             }
1.24      raeburn  3354:         }
1.2       raeburn  3355:         if ($statusmode eq 'Expired') {
1.102     raeburn  3356:             if ($crstype eq 'Community') {
                   3357:                 $description .= &mt('Users in community with expired [_1] roles',$showfilter);
                   3358:             } else {
                   3359:                 $description .= &mt('Users in course with expired [_1] roles',$showfilter);
                   3360:             }
1.11      raeburn  3361:         } elsif ($statusmode eq 'Future') {
1.102     raeburn  3362:             if ($crstype eq 'Community') {
                   3363:                 $description .= &mt('Users in community with future [_1] roles',$showfilter);
                   3364:             } else {
                   3365:                 $description .= &mt('Users in course with future [_1] roles',$showfilter);
                   3366:             }
1.2       raeburn  3367:         } elsif ($statusmode eq 'Active') {
1.102     raeburn  3368:             if ($crstype eq 'Community') {
                   3369:                 $description .= &mt('Users in community with active [_1] roles',$showfilter);
                   3370:             } else {
                   3371:                 $description .= &mt('Users in course with active [_1] roles',$showfilter);
                   3372:             }
1.2       raeburn  3373:         } else {
                   3374:             if ($rolefilter eq 'Any') {
1.102     raeburn  3375:                 if ($crstype eq 'Community') {
                   3376:                     $description .= &mt('All users in community');
                   3377:                 } else {
                   3378:                     $description .= &mt('All users in course');
                   3379:                 }
1.2       raeburn  3380:             } else {
1.102     raeburn  3381:                 if ($crstype eq 'Community') {
                   3382:                     $description .= &mt('All users in community with [_1] roles',$rolefilter);
                   3383:                 } else {
                   3384:                     $description .= &mt('All users in course with [_1] roles',$rolefilter);
                   3385:                 }
1.2       raeburn  3386:             }
                   3387:         }
1.33      raeburn  3388:         my $constraint;
1.26      raeburn  3389:         my $viewablesec = &viewable_section($permission);
                   3390:         if ($viewablesec ne '') {
1.15      raeburn  3391:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  3392:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.103     raeburn  3393:             } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
1.33      raeburn  3394:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   3395:             }
                   3396:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3397:                 if ($env{'form.grpfilter'} eq 'none') {
                   3398:                     $constraint .= &mt(' and not in any group');
                   3399:                 } else {
                   3400:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   3401:                 }
                   3402:             }
                   3403:         } else {
                   3404:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   3405:                 if ($env{'form.secfilter'} eq 'none') {
                   3406:                     $constraint = &mt('only users affiliated with no section');
                   3407:                 } else {
                   3408:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   3409:                 }
                   3410:             }
                   3411:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3412:                 if ($env{'form.grpfilter'} eq 'none') {
                   3413:                     if ($constraint eq '') {
                   3414:                         $constraint = &mt('only users not in any group');
                   3415:                     } else {
                   3416:                         $constraint .= &mt(' and also not in any group'); 
                   3417:                     }
                   3418:                 } else {
                   3419:                     if ($constraint eq '') {
                   3420:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   3421:                     } else {
                   3422:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   3423:                     }
                   3424:                 }
1.15      raeburn  3425:             }
                   3426:         }
1.33      raeburn  3427:         if ($constraint ne '') {
                   3428:             $description .= ' ('.$constraint.')';
                   3429:         } 
1.13      raeburn  3430:     } elsif ($context eq 'author') {
1.14      raeburn  3431:         $description = 
1.73      bisitz   3432:             &mt('Author space for [_1]'
                   3433:                 ,'<span class="LC_cusr_emph">'
                   3434:                 .&Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})
                   3435:                 .'</span>')
                   3436:             .':&nbsp;&nbsp;';
1.2       raeburn  3437:         if ($statusmode eq 'Expired') {
1.5       raeburn  3438:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  3439:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  3440:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  3441:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  3442:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  3443:         } else {
                   3444:             if ($rolefilter eq 'Any') {
1.5       raeburn  3445:                 $description .= &mt('All co-authors');
1.2       raeburn  3446:             } else {
                   3447:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   3448:             }
                   3449:         }
                   3450:     } elsif ($context eq 'domain') {
                   3451:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.73      bisitz   3452:         $description = &mt('Domain - [_1]:',$domdesc).' ';
1.2       raeburn  3453:         if ($env{'form.roletype'} eq 'domain') {
                   3454:             if ($statusmode eq 'Expired') {
1.5       raeburn  3455:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3456:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3457:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  3458:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3459:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  3460:             } else {
                   3461:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3462:                     $description .= &mt('All users in domain');
1.2       raeburn  3463:                 } else {
                   3464:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   3465:                 }
                   3466:             }
1.13      raeburn  3467:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  3468:             if ($statusmode eq 'Expired') {
1.5       raeburn  3469:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3470:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3471:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  3472:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3473:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  3474:             } else {
                   3475:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3476:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  3477:                 } else {
1.116     bisitz   3478:                     $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
1.2       raeburn  3479:                 }
                   3480:             }
1.102     raeburn  3481:         } elsif (($env{'form.roletype'} eq 'course') || 
                   3482:                  ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  3483:             my $coursefilter = $env{'form.coursepick'};
1.102     raeburn  3484:             if ($env{'form.roletype'} eq 'course') {
                   3485:                 if ($coursefilter eq 'category') {
                   3486:                     my $instcode = &instcode_from_coursefilter();
                   3487:                     if ($instcode eq '.') {
                   3488:                         $description .= &mt('All courses in domain').' - ';
                   3489:                     } else {
                   3490:                         $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                   3491:                     }
                   3492:                 } elsif ($coursefilter eq 'selected') {
                   3493:                     $description .= &mt('Selected courses in domain').' - ';
                   3494:                 } elsif ($coursefilter eq 'all') {
1.2       raeburn  3495:                     $description .= &mt('All courses in domain').' - ';
                   3496:                 }
1.102     raeburn  3497:             } elsif ($env{'form.roletype'} eq 'community') {
                   3498:                 if ($coursefilter eq 'selected') {
                   3499:                     $description .= &mt('Selected communities in domain').' - ';
                   3500:                 } elsif ($coursefilter eq 'all') {
                   3501:                     $description .= &mt('All communities in domain').' - ';
                   3502:                 }
1.2       raeburn  3503:             }
                   3504:             if ($statusmode eq 'Expired') {
1.5       raeburn  3505:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  3506:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3507:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  3508:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3509:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  3510:             } else {
                   3511:                 if ($rolefilter eq 'Any') {
                   3512:                     $description .= &mt('all users');
                   3513:                 } else {
                   3514:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   3515:                 }
                   3516:             }
                   3517:         }
                   3518:     }
                   3519:     return $description;
                   3520: }
1.22      raeburn  3521: 
                   3522: sub viewable_section {
                   3523:     my ($permission) = @_;
                   3524:     my $viewablesec;
                   3525:     if (ref($permission) eq 'HASH') {
                   3526:         if (exists($permission->{'view_section'})) {
                   3527:             $viewablesec = $permission->{'view_section'};
                   3528:         } elsif (exists($permission->{'cusr_section'})) {
                   3529:             $viewablesec = $permission->{'cusr_section'};
                   3530:         }
                   3531:     }
                   3532:     return $viewablesec;
                   3533: }
                   3534: 
1.2       raeburn  3535:     
1.1       raeburn  3536: #################################################
                   3537: #################################################
                   3538: sub show_drop_list {
1.101     raeburn  3539:     my ($r,$classlist,$nosort,$permission,$crstype) = @_;
1.29      raeburn  3540:     my $cid = $env{'request.course.id'};
1.17      raeburn  3541:     my ($cnum,$cdom) = &get_course_identity($cid);
1.1       raeburn  3542:     if (! exists($env{'form.sortby'})) {
                   3543:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   3544:                                                 ['sortby']);
                   3545:     }
                   3546:     my $sortby = $env{'form.sortby'};
                   3547:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   3548:         $sortby = 'username';
                   3549:     }
                   3550:     my $action = "drop";
1.17      raeburn  3551:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  3552:     $r->print(<<END);
                   3553: <input type="hidden" name="sortby" value="$sortby" />
                   3554: <input type="hidden" name="action" value="$action" />
                   3555: <input type="hidden" name="state"  value="done" />
1.17      raeburn  3556: <script type="text/javascript" language="Javascript">
1.96      bisitz   3557: // <![CDATA[
1.17      raeburn  3558: $check_uncheck_js
1.96      bisitz   3559: // ]]>
1.1       raeburn  3560: </script>
                   3561: <p>
1.86      bisitz   3562: <input type="hidden" name="phase" value="four" />
1.1       raeburn  3563: END
1.30      raeburn  3564:     my ($indexhash,$keylist) = &make_keylist_array();
                   3565:     my $studentcount = 0;
                   3566:     if (ref($classlist) eq 'HASH') {
                   3567:         foreach my $student (keys(%{$classlist})) {
                   3568:             my $sdata = $classlist->{$student}; 
                   3569:             my $status = $sdata->[$indexhash->{'status'}];
                   3570:             my $section = $sdata->[$indexhash->{'section'}];
                   3571:             if ($status ne 'Active') {
                   3572:                 delete($classlist->{$student});
                   3573:                 next;
                   3574:             }
                   3575:             if ($env{'request.course.sec'} ne '') {
                   3576:                 if ($section ne $env{'request.course.sec'}) {
                   3577:                     delete($classlist->{$student});
                   3578:                     next;
                   3579:                 }
                   3580:             }
                   3581:             $studentcount ++;
                   3582:         }
                   3583:     }
                   3584:     if (!$studentcount) {
1.101     raeburn  3585:         if ($crstype eq 'Community') {
                   3586:             $r->print(&mt('There are no members to drop.'));
                   3587:         } else {
                   3588:             $r->print(&mt('There are no students to drop.'));
                   3589:         }
1.30      raeburn  3590:         return;
                   3591:     }
                   3592:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   3593:                                               $classlist,$keylist,$cdom,$cnum);
                   3594:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   3595:                                        'dom'    => "domain",
                   3596:                                        'sn'     => "student name",
1.101     raeburn  3597:                                        'mn'     => "member name",
1.30      raeburn  3598:                                        'sec'    => "section",
                   3599:                                        'start'  => "start date",
                   3600:                                        'end'    => "end date",
                   3601:                                        'groups' => "active groups",
                   3602:                                       );
1.101     raeburn  3603:     my $nametitle = $lt{'sn'};
                   3604:     if ($crstype eq 'Community') {
                   3605:         $nametitle = $lt{'mn'};
                   3606:     }
1.1       raeburn  3607:     if ($nosort) {
1.17      raeburn  3608:         $r->print(&Apache::loncommon::start_data_table().
                   3609:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3610:         $r->print(<<END);
                   3611:     <th>&nbsp;</th>
                   3612:     <th>$lt{'usrn'}</th>
                   3613:     <th>$lt{'dom'}</th>
                   3614:     <th>ID</th>
1.101     raeburn  3615:     <th>$nametitle</th>
1.1       raeburn  3616:     <th>$lt{'sec'}</th>
                   3617:     <th>$lt{'start'}</th>
                   3618:     <th>$lt{'end'}</th>
                   3619:     <th>$lt{'groups'}</th>
                   3620: END
1.17      raeburn  3621:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3622:     } else  {
1.17      raeburn  3623:         $r->print(&Apache::loncommon::start_data_table().
                   3624:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3625:         $r->print(<<END);
1.17      raeburn  3626:     <th>&nbsp;</th>
1.1       raeburn  3627:     <th>
1.17      raeburn  3628:        <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1       raeburn  3629:     </th><th>
1.17      raeburn  3630:        <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1       raeburn  3631:     </th><th>
1.17      raeburn  3632:        <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1       raeburn  3633:     </th><th>
1.101     raeburn  3634:        <a href="/adm/createuser?action=$action&sortby=fullname">$nametitle</a>
1.1       raeburn  3635:     </th><th>
1.17      raeburn  3636:        <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1       raeburn  3637:     </th><th>
1.17      raeburn  3638:        <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1       raeburn  3639:     </th><th>
1.17      raeburn  3640:        <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1       raeburn  3641:     </th><th>
1.17      raeburn  3642:        <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
1.1       raeburn  3643:     </th>
                   3644: END
1.17      raeburn  3645:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3646:     }
                   3647:     #
                   3648:     # Sort the students
1.30      raeburn  3649:     my $index  = $indexhash->{$sortby};
                   3650:     my $second = $indexhash->{'username'};
                   3651:     my $third  = $indexhash->{'domain'};
1.1       raeburn  3652:     my @Sorted_Students = sort {
                   3653:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   3654:             ||
                   3655:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   3656:             ||
                   3657:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  3658:         } (keys(%{$classlist}));
1.1       raeburn  3659:     foreach my $student (@Sorted_Students) {
                   3660:         my $error;
                   3661:         my $sdata = $classlist->{$student};
1.30      raeburn  3662:         my $username = $sdata->[$indexhash->{'username'}];
                   3663:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   3664:         my $section  = $sdata->[$indexhash->{'section'}];
                   3665:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   3666:         my $id       = $sdata->[$indexhash->{'id'}];
                   3667:         my $start    = $sdata->[$indexhash->{'start'}];
                   3668:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  3669:         my $groups = $classgroups->{$student};
                   3670:         my $active_groups;
                   3671:         if (ref($groups->{active}) eq 'HASH') {
                   3672:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   3673:         }
                   3674:         if (! defined($start) || $start == 0) {
                   3675:             $start = &mt('none');
                   3676:         } else {
                   3677:             $start = &Apache::lonlocal::locallocaltime($start);
                   3678:         }
                   3679:         if (! defined($end) || $end == 0) {
                   3680:             $end = &mt('none');
                   3681:         } else {
                   3682:             $end = &Apache::lonlocal::locallocaltime($end);
                   3683:         }
1.17      raeburn  3684:         my $studentkey = $student.':'.$section;
1.30      raeburn  3685:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  3686:         #
                   3687:         $r->print(&Apache::loncommon::start_data_table_row());
                   3688:         $r->print(<<"END");
1.86      bisitz   3689:     <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
1.1       raeburn  3690:     <td>$username</td>
                   3691:     <td>$domain</td>
                   3692:     <td>$id</td>
                   3693:     <td>$name</td>
                   3694:     <td>$section</td>
1.29      raeburn  3695:     <td>$start $startitem</td>
1.1       raeburn  3696:     <td>$end</td>
                   3697:     <td>$active_groups</td>
                   3698: END
                   3699:         $r->print(&Apache::loncommon::end_data_table_row());
                   3700:     }
                   3701:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3702:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  3703:                        'dp'   => "Drop Students",
1.101     raeburn  3704:                        'dm'   => "Drop Members",
1.1       raeburn  3705:                        'ca'   => "check all",
                   3706:                        'ua'   => "uncheck all",
                   3707:                                        );
1.101     raeburn  3708:     my $btn = $lt{'dp'};
                   3709:     if ($crstype eq 'Community') {
                   3710:         $btn = $lt{'dm'}; 
                   3711:     }
1.1       raeburn  3712:     $r->print(<<"END");
1.89      bisitz   3713: </p>
                   3714: <p>
1.86      bisitz   3715: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" /> &nbsp;
                   3716: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
1.89      bisitz   3717: </p>
                   3718: <p>
1.101     raeburn  3719: <input type="submit" value="$btn" />
1.89      bisitz   3720: </p>
1.1       raeburn  3721: END
                   3722:     return;
                   3723: }
                   3724: 
                   3725: #
                   3726: # Print out the initial form to get the file containing a list of users
                   3727: #
                   3728: sub print_first_users_upload_form {
                   3729:     my ($r,$context) = @_;
                   3730:     my $str;
1.86      bisitz   3731:     $str  = '<input type="hidden" name="phase" value="two" />';
1.1       raeburn  3732:     $str .= '<input type="hidden" name="action" value="upload" />';
1.101     raeburn  3733:     $str .= '<input type="hidden" name="state"  value="got_file" />';
1.95      bisitz   3734: 
1.85      bisitz   3735:     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
1.95      bisitz   3736: 
                   3737:     # Excel and CSV Help
1.107     raeburn  3738:     $str .= '<div class="LC_left_float">'
1.95      bisitz   3739:            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   3740:                 &mt("How do I create a users list from a spreadsheet"))
1.107     raeburn  3741:            .'</div><div class="LC_left_float">'."\n"
1.95      bisitz   3742:            .&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   3743:                 &mt("How do I create a CSV file from a spreadsheet"))
1.107     raeburn  3744:            .'</div><br clear="all" />'."\n";
1.95      bisitz   3745:     $str .= &Apache::lonhtmlcommon::start_pick_box()
1.103     raeburn  3746:            .&Apache::lonhtmlcommon::row_title(&mt('File'));
                   3747:     if (&Apache::lonlocal::current_language() ne 'en') {
                   3748:         if ($context eq 'course') { 
                   3749:             $str .= '<p class="LC_info">'."\n"
                   3750:                    .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
                   3751:                    .'</p>'."\n";
                   3752:         }
                   3753:     }
                   3754:     $str .= &Apache::loncommon::upfile_select_html()
1.95      bisitz   3755:            .&Apache::lonhtmlcommon::row_closure()
                   3756:            .&Apache::lonhtmlcommon::row_title(
                   3757:                 '<label for="noFirstLine">'
                   3758:                .&mt('Ignore First Line')
                   3759:                .'</label>')
                   3760:            .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
                   3761:            .&Apache::lonhtmlcommon::row_closure(1)
                   3762:            .&Apache::lonhtmlcommon::end_pick_box();
                   3763: 
                   3764:     $str .= '<p>'
                   3765:            .'<input type="submit" name="fileupload" value="'.&mt('Next').'" />'
                   3766:            .'</p>';
                   3767: 
1.1       raeburn  3768:     $r->print($str);
                   3769:     return;
                   3770: }
                   3771: 
                   3772: # ================================================= Drop/Add from uploaded file
                   3773: sub upfile_drop_add {
1.21      raeburn  3774:     my ($r,$context,$permission) = @_;
1.1       raeburn  3775:     &Apache::loncommon::load_tmp_file($r);
                   3776:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   3777:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   3778:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   3779:     my %fields=();
                   3780:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   3781:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   3782:             if ($env{'form.f'.$i} ne 'none') {
                   3783:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   3784:             }
                   3785:         } else {
                   3786:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   3787:         }
                   3788:     }
1.29      raeburn  3789:     if ($env{'form.fullup'} ne 'yes') {
                   3790:         $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   3791:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
                   3792:     }
1.1       raeburn  3793:     #
                   3794:     # Store the field choices away
                   3795:     foreach my $field (qw/username names
1.57      raeburn  3796:                        fname mname lname gen id sec ipwd email role domain/) {
1.1       raeburn  3797:         $env{'form.'.$field.'_choice'}=$fields{$field};
                   3798:     }
                   3799:     &Apache::loncommon::store_course_settings('enrollment_upload',
                   3800:                                               { 'username_choice' => 'scalar',
                   3801:                                                 'names_choice' => 'scalar',
                   3802:                                                 'fname_choice' => 'scalar',
                   3803:                                                 'mname_choice' => 'scalar',
                   3804:                                                 'lname_choice' => 'scalar',
                   3805:                                                 'gen_choice' => 'scalar',
                   3806:                                                 'id_choice' => 'scalar',
                   3807:                                                 'sec_choice' => 'scalar',
                   3808:                                                 'ipwd_choice' => 'scalar',
                   3809:                                                 'email_choice' => 'scalar',
1.57      raeburn  3810:                                                 'role_choice'  => 'scalar',
1.84      raeburn  3811:                                                 'domain_choice' => 'scalar',
                   3812:                                                 'inststatus_choice' => 'scalar'});
1.1       raeburn  3813:     #
1.101     raeburn  3814:     my ($cid,$crstype,$setting);
                   3815:     if ($context eq 'domain') {
                   3816:         $setting = $env{'form.roleaction'};
                   3817:     }
                   3818:     if ($env{'request.course.id'} ne '') {
                   3819:         $cid = $env{'request.course.id'};
                   3820:         $crstype = &Apache::loncommon::course_type();
                   3821:     } elsif ($setting eq 'course') {
                   3822:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   3823:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
                   3824:             $crstype = &Apache::loncommon::course_type($cid);
                   3825:         }
                   3826:     }
1.1       raeburn  3827:     my ($startdate,$enddate) = &get_dates_from_form();
                   3828:     if ($env{'form.makedatesdefault'}) {
1.101     raeburn  3829:         $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  3830:     }
                   3831:     # Determine domain and desired host (home server)
1.57      raeburn  3832:     my $defdom=$env{'request.role.domain'};
                   3833:     my $domain;
                   3834:     if ($env{'form.defaultdomain'} ne '') {
                   3835:         $domain = $env{'form.defaultdomain'};
                   3836:     } else {
                   3837:         $domain = $defdom;
                   3838:     }
1.1       raeburn  3839:     my $desiredhost = $env{'form.lcserver'};
                   3840:     if (lc($desiredhost) eq 'default') {
                   3841:         $desiredhost = undef;
                   3842:     } else {
1.57      raeburn  3843:         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
1.1       raeburn  3844:         if (! exists($home_servers{$desiredhost})) {
                   3845:             $r->print('<span class="LC_error">'.&mt('Error').
                   3846:                       &mt('Invalid home server specified').'</span>');
                   3847:             $r->print(&Apache::loncommon::end_page());
                   3848:             return;
                   3849:         }
                   3850:     }
                   3851:     # Determine authentication mechanism
                   3852:     my $changeauth;
                   3853:     if ($context eq 'domain') {
                   3854:         $changeauth = $env{'form.changeauth'};
                   3855:     }
                   3856:     my $amode  = '';
                   3857:     my $genpwd = '';
                   3858:     if ($env{'form.login'} eq 'krb') {
                   3859:         $amode='krb';
                   3860:         $amode.=$env{'form.krbver'};
                   3861:         $genpwd=$env{'form.krbarg'};
                   3862:     } elsif ($env{'form.login'} eq 'int') {
                   3863:         $amode='internal';
                   3864:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   3865:             $genpwd=$env{'form.intarg'};
                   3866:         }
                   3867:     } elsif ($env{'form.login'} eq 'loc') {
                   3868:         $amode='localauth';
                   3869:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   3870:             $genpwd=$env{'form.locarg'};
                   3871:         }
                   3872:     }
                   3873:     if ($amode =~ /^krb/) {
                   3874:         if (! defined($genpwd) || $genpwd eq '') {
                   3875:             $r->print('<span class="Error">'.
                   3876:                       &mt('Unable to enroll users').' '.
                   3877:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   3878:             $amode = ''; # This causes the loop below to be skipped
                   3879:         }
                   3880:     }
1.101     raeburn  3881:     my ($defaultsec,$defaultrole);
1.1       raeburn  3882:     if ($context eq 'domain') {
                   3883:         if ($setting eq 'domain') {
                   3884:             $defaultrole = $env{'form.defaultrole'};
                   3885:         } elsif ($setting eq 'course') {
                   3886:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  3887:             $defaultsec = $env{'form.sections'};
1.1       raeburn  3888:         }  
1.13      raeburn  3889:     } elsif ($context eq 'author') {
1.1       raeburn  3890:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  3891:     } elsif ($context eq 'course') {
                   3892:         $defaultrole = $env{'form.defaultrole'};
                   3893:         $defaultsec = $env{'form.sections'};
1.1       raeburn  3894:     }
1.27      raeburn  3895:     # Check to see if user information can be changed
                   3896:     my @userinfo = ('firstname','middlename','lastname','generation',
                   3897:                     'permanentemail','id');
                   3898:     my %canmodify;
                   3899:     if (&Apache::lonnet::allowed('mau',$domain)) {
1.84      raeburn  3900:         push(@userinfo,'inststatus');
1.27      raeburn  3901:         foreach my $field (@userinfo) {
                   3902:             $canmodify{$field} = 1;
                   3903:         }
                   3904:     }
                   3905:     my (%userlist,%modifiable_fields,@poss_roles);
                   3906:     my $secidx = &Apache::loncoursedata::CL_SECTION();
1.102     raeburn  3907:     my @courseroles = &roles_by_context('course',1,$crstype);
1.27      raeburn  3908:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   3909:         if ($context eq 'course' || $context eq 'author') {
1.101     raeburn  3910:             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
1.27      raeburn  3911:             my @statuses = ('active','future');
                   3912:             my ($indexhash,$keylist) = &make_keylist_array();
                   3913:             my %info;
                   3914:             foreach my $role (@poss_roles) {
                   3915:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   3916:                                                         \@userinfo,[$role]);
                   3917:             }
                   3918:             if ($context eq 'course') {
                   3919:                 my ($cnum,$cdom) = &get_course_identity();
                   3920:                 my $roster = &Apache::loncoursedata::get_classlist();
1.66      raeburn  3921:                 if (ref($roster) eq 'HASH') {
                   3922:                     %userlist = %{$roster};
                   3923:                 }
1.27      raeburn  3924:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   3925:                                                          \@statuses,\@poss_roles);
                   3926:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3927:                                 \%advrolehash,$permission);
                   3928:             } elsif ($context eq 'author') {
                   3929:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   3930:                                                   \@statuses,\@poss_roles);
                   3931:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3932:                              \%cstr_roles,$permission);
                   3933: 
                   3934:             }
                   3935:         }
1.1       raeburn  3936:     }
                   3937:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   3938:         && ($amode ne '')) {
                   3939:         #######################################
                   3940:         ##         Add/Modify Users          ##
                   3941:         #######################################
                   3942:         if ($context eq 'course') {
                   3943:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  3944:         } elsif ($context eq 'author') {
1.1       raeburn  3945:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   3946:         } else {
                   3947:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   3948:         }
1.87      bisitz   3949:         $r->rflush;
                   3950: 
1.1       raeburn  3951:         my %counts = (
                   3952:                        user => 0,
                   3953:                        auth => 0,
                   3954:                        role => 0,
                   3955:                      );
                   3956:         my $flushc=0;
                   3957:         my %student=();
1.42      raeburn  3958:         my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
1.1       raeburn  3959:         my %userchg;
1.27      raeburn  3960:         if ($context eq 'course' || $setting eq 'course') {
                   3961:             if ($context eq 'course') {
                   3962:                 # Get information about course groups
                   3963:                 %curr_groups = &Apache::longroup::coursegroups();
                   3964:             } elsif ($setting eq 'course') {
                   3965:                 if ($cid) {
                   3966:                     %curr_groups =
                   3967:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   3968:                                                         $env{'form.dccourse'});
                   3969:                 }
                   3970:             }
                   3971:             # determine section number
                   3972:             if ($defaultsec =~ /,/) {
                   3973:                 push(@sections,split(/,/,$defaultsec));
                   3974:             } else {
                   3975:                 push(@sections,$defaultsec);
                   3976:             }
                   3977:             # remove non alphanumeric values from section
                   3978:             foreach my $item (@sections) {
                   3979:                 $item =~ s/\W//g;
                   3980:                 if ($item eq "none" || $item eq 'all') {
                   3981:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   3982:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   3983:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   3984:                 } elsif ($item ne '') {
                   3985:                     push(@cleansec,$item);
                   3986:                 }
                   3987:             }
                   3988:             if ($defaultwarn) {
                   3989:                 $r->print($defaultwarn.'<br />');
                   3990:             }
                   3991:             if ($groupwarn) {
                   3992:                 $r->print($groupwarn.'<br />');
                   3993:             }
1.1       raeburn  3994:         }
1.124     raeburn  3995:         my (%curr_rules,%got_rules,%alerts,%cancreate);
1.104     raeburn  3996:         my %customroles = &my_custom_roles($crstype);
1.101     raeburn  3997:         my @permitted_roles = 
1.124     raeburn  3998:             &roles_on_upload($context,$setting,$crstype,%customroles);
                   3999:         my %longtypes = &Apache::lonlocal::texthash(
                   4000:                             official   => 'Institutional',
                   4001:                             unofficial => 'Non-institutional',
                   4002:                         );
                   4003:         map { $cancreate{$_} = &can_create_user($domain,$context,$_); } keys(%longtypes);
1.1       raeburn  4004:         # Get new users list
1.27      raeburn  4005:         foreach my $line (@userdata) {
1.42      raeburn  4006:             my @secs;
1.27      raeburn  4007:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4008:             # Determine user name
1.128     raeburn  4009:             $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4010:             unless (($entries{$fields{'username'}} eq '') ||
                   4011:                     (!defined($entries{$fields{'username'}}))) {
                   4012:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   4013:                 if (defined($fields{'names'})) {
                   4014:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   4015:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   4016:                 } else {
                   4017:                     if (defined($fields{'fname'})) {
                   4018:                         $fname=$entries{$fields{'fname'}};
                   4019:                     }
                   4020:                     if (defined($fields{'mname'})) {
                   4021:                         $mname=$entries{$fields{'mname'}};
                   4022:                     }
                   4023:                     if (defined($fields{'lname'})) {
                   4024:                         $lname=$entries{$fields{'lname'}};
                   4025:                     }
                   4026:                     if (defined($fields{'gen'})) {
                   4027:                         $gen=$entries{$fields{'gen'}};
                   4028:                     }
                   4029:                 }
1.128     raeburn  4030: 
1.1       raeburn  4031:                 if ($entries{$fields{'username'}}
                   4032:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
1.128     raeburn  4033:                     my $nowhitespace;
                   4034:                     if ($entries{$fields{'username'}} =~ /\s/) {
                   4035:                         $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
                   4036:                     }
1.1       raeburn  4037:                     $r->print('<br />'.
1.74      bisitz   4038:       &mt('[_1]: Unacceptable username for user [_2] [_3] [_4] [_5]',
1.128     raeburn  4039:           '<b>'.$entries{$fields{'username'}}.'</b>',$fname,$mname,$lname,$gen).
                   4040:                               $nowhitespace);
1.27      raeburn  4041:                     next;
1.1       raeburn  4042:                 } else {
1.129     raeburn  4043:                     $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
1.71      droeschl 4044:                     if ($entries{$fields{'domain'}} 
1.57      raeburn  4045:                         ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
                   4046:                         $r->print('<br />'. '<b>'.$entries{$fields{'domain'}}.
1.77      raeburn  4047:                                   '</b>: '.&mt('Unacceptable domain for user [_2] [_3] [_4] [_5]',$fname,$mname,$lname,$gen));
1.57      raeburn  4048:                         next;
                   4049:                     }
1.5       raeburn  4050:                     my $username = $entries{$fields{'username'}};
1.57      raeburn  4051:                     my $userdomain = $entries{$fields{'domain'}};
                   4052:                     if ($userdomain eq '') {
                   4053:                         $userdomain = $domain;
                   4054:                     }
1.27      raeburn  4055:                     if (defined($fields{'sec'})) {
                   4056:                         if (defined($entries{$fields{'sec'}})) {
1.42      raeburn  4057:                             $entries{$fields{'sec'}} =~ s/\W//g;
1.27      raeburn  4058:                             my $item = $entries{$fields{'sec'}};
                   4059:                             if ($item eq "none" || $item eq 'all') {
1.74      bisitz   4060:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item));
1.27      raeburn  4061:                                 next;
                   4062:                             } elsif (exists($curr_groups{$item})) {
1.74      bisitz   4063:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item).' '.&mt('Section names and group names must be distinct.'));
1.27      raeburn  4064:                                 next;
                   4065:                             } else {
                   4066:                                 push(@secs,$item);
                   4067:                             }
                   4068:                         }
                   4069:                     }
                   4070:                     if ($env{'request.course.sec'} ne '') {
                   4071:                         @secs = ($env{'request.course.sec'});
1.57      raeburn  4072:                         if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
                   4073:                             my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
1.27      raeburn  4074:                             if ($currsec ne $env{'request.course.sec'}) {
1.74      bisitz   4075:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$secs[0]).'<br />');
1.27      raeburn  4076:                                 if ($currsec eq '') {
                   4077:                                     $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.'));
                   4078: 
                   4079:                                 } else {
                   4080:                                     $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec));
                   4081:                                 }
                   4082:                                 $r->print('<br />'.&mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$secs[0]).'<br />');
                   4083:                                 next;
1.1       raeburn  4084:                             }
                   4085:                         }
1.27      raeburn  4086:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   4087:                         if (@secs == 0) {
                   4088:                             @secs = @cleansec;
1.1       raeburn  4089:                         }
                   4090:                     }
                   4091:                     # determine id number
                   4092:                     my $id='';
                   4093:                     if (defined($fields{'id'})) {
                   4094:                         if (defined($entries{$fields{'id'}})) {
                   4095:                             $id=$entries{$fields{'id'}};
                   4096:                         }
                   4097:                         $id=~tr/A-Z/a-z/;
                   4098:                     }
                   4099:                     # determine email address
                   4100:                     my $email='';
                   4101:                     if (defined($fields{'email'})) {
1.129     raeburn  4102:                         $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4103:                         if (defined($entries{$fields{'email'}})) {
                   4104:                             $email=$entries{$fields{'email'}};
1.84      raeburn  4105:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   4106:                         }
                   4107:                     }
                   4108:                     # determine affiliation
                   4109:                     my $inststatus='';
                   4110:                     if (defined($fields{'inststatus'})) {
                   4111:                         if (defined($entries{$fields{'inststatus'}})) {
                   4112:                             $inststatus=$entries{$fields{'inststatus'}};
                   4113:                         }
1.1       raeburn  4114:                     }
                   4115:                     # determine user password
                   4116:                     my $password = $genpwd;
                   4117:                     if (defined($fields{'ipwd'})) {
                   4118:                         if ($entries{$fields{'ipwd'}}) {
                   4119:                             $password=$entries{$fields{'ipwd'}};
                   4120:                         }
                   4121:                     }
                   4122:                     # determine user role
                   4123:                     my $role = '';
                   4124:                     if (defined($fields{'role'})) {
                   4125:                         if ($entries{$fields{'role'}}) {
1.42      raeburn  4126:                             $entries{$fields{'role'}}  =~ s/(\s+$|^\s+)//g;
                   4127:                             if ($entries{$fields{'role'}} ne '') {
                   4128:                                 if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
                   4129:                                     $role = $entries{$fields{'role'}};
1.27      raeburn  4130:                                 }
                   4131:                             }
                   4132:                             if ($role eq '') {
                   4133:                                 my $rolestr = join(', ',@permitted_roles);
1.74      bisitz   4134:                                 $r->print('<br />'
                   4135:                                          .&mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
                   4136:                                              ,'<b>'.$entries{$fields{'username'}}.'</b>'
                   4137:                                              ,$entries{$fields{'role'}})
                   4138:                                          .'<br />'
                   4139:                                          .&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n"
                   4140:                                 );
1.1       raeburn  4141:                                 next;
                   4142:                             }
                   4143:                         }
                   4144:                     }
                   4145:                     if ($role eq '') {
                   4146:                         $role = $defaultrole;
                   4147:                     }
                   4148:                     # Clean up whitespace
1.129     raeburn  4149:                     foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
1.1       raeburn  4150:                         $$_ =~ s/(\s+$|^\s+)//g;
                   4151:                     }
1.5       raeburn  4152:                     # check against rules
                   4153:                     my $checkid = 0;
                   4154:                     my $newuser = 0;
                   4155:                     my (%rulematch,%inst_results,%idinst_results);
1.57      raeburn  4156:                     my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
1.5       raeburn  4157:                     if ($uhome eq 'no_host') {
1.124     raeburn  4158:                         if ($userdomain ne $domain) {
                   4159:                             $r->print('<br />'.
                   4160:                                 &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
                   4161:                                     '<b>'.$username.'</b>',$userdomain).'<br />'.
                   4162:                                 &mt('The user does not already exist, and you may not create a new user in a different domain.'));
                   4163:                             next;
                   4164:                         }
1.5       raeburn  4165:                         $checkid = 1;
                   4166:                         $newuser = 1;
1.124     raeburn  4167:                         my $user = $username.':'.$domain;
1.5       raeburn  4168:                         my $checkhash;
                   4169:                         my $checks = { 'username' => 1 };
                   4170:                         $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
                   4171:                         &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4172:                             \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                   4173:                             \%got_rules);
                   4174:                         if (ref($alerts{'username'}) eq 'HASH') {
                   4175:                             if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1.124     raeburn  4176:                                 if ($alerts{'username'}{$domain}{$username}) {
                   4177:                                     $r->print('<br />'.
                   4178:                                               &mt('[_1]: matches the username format at your institution, but is not known to your directory service.','<b>'.$username.'</b>').'<br />'.
                   4179:                                               &mt('Consequently, the user was not created.'));
                   4180:                                     next;
                   4181:                                 }
                   4182:                             }
                   4183:                         }
                   4184:                         my $usertype = 'unofficial';
                   4185:                         if (ref($rulematch{$user}) eq 'HASH') {
                   4186:                             if ($rulematch{$user}{'username'}) {
                   4187:                                 $usertype = 'official';
1.5       raeburn  4188:                             }
                   4189:                         }
1.124     raeburn  4190:                         unless ($cancreate{$usertype}) {
                   4191:                             my $showtype = $longtypes{$usertype};
                   4192:                             $r->print('<br />'.
1.133   ! raeburn  4193:                                       &mt('[_1]: The user does not exist, and you are not permitted to create users of type: [_2].','<b>'.$username.'</b>',$showtype));
1.124     raeburn  4194:                             next;
                   4195:                         }
1.13      raeburn  4196:                     } else {
1.27      raeburn  4197:                         if ($context eq 'course' || $context eq 'author') {
1.57      raeburn  4198:                             if ($userdomain eq $domain ) {
                   4199:                                 if ($role eq '') {
                   4200:                                     my @checkroles;
                   4201:                                     foreach my $role (@poss_roles) {
                   4202:                                         my $endkey;
                   4203:                                         if ($role ne 'st') {
                   4204:                                             $endkey = ':'.$role;
                   4205:                                         }
                   4206:                                         if (exists($userlist{$username.':'.$userdomain.$endkey})) {
                   4207:                                             if (!grep(/^\Q$role\E$/,@checkroles)) {
                   4208:                                                 push(@checkroles,$role);
                   4209:                                             }
                   4210:                                         }
1.27      raeburn  4211:                                     }
1.57      raeburn  4212:                                     if (@checkroles > 0) {
                   4213:                                         %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
1.27      raeburn  4214:                                     }
1.57      raeburn  4215:                                 } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   4216:                                     %canmodify = %{$modifiable_fields{$role}};
1.27      raeburn  4217:                                 }
                   4218:                             }
1.57      raeburn  4219:                             my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
1.84      raeburn  4220:                             for (my $i=0; $i<@newinfo; $i++) {
1.57      raeburn  4221:                                 if (${$newinfo[$i]} ne '') {
                   4222:                                     if (!$canmodify{$userinfo[$i]}) {
                   4223:                                         ${$newinfo[$i]} = '';
                   4224:                                     }
1.27      raeburn  4225:                                 }
                   4226:                             }
                   4227:                         }
1.5       raeburn  4228:                     }
                   4229:                     if ($id ne '') {
                   4230:                         if (!$newuser) {
1.57      raeburn  4231:                             my %idhash = &Apache::lonnet::idrget($userdomain,($username));
1.5       raeburn  4232:                             if ($idhash{$username} ne $id) {
                   4233:                                 $checkid = 1;
                   4234:                             }
                   4235:                         }
                   4236:                         if ($checkid) {
                   4237:                             my $checkhash;
                   4238:                             my $checks = { 'id' => 1 };
1.57      raeburn  4239:                             $checkhash->{$username.':'.$userdomain} = { 'newuser' => $newuser,
1.5       raeburn  4240:                                                                     'id'  => $id };
                   4241:                             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4242:                                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
                   4243:                                 \%got_rules);
                   4244:                             if (ref($alerts{'id'}) eq 'HASH') {
1.57      raeburn  4245:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
1.124     raeburn  4246:                                     if ($alerts{'id'}{$userdomain}{$id}) {
                   4247:                                         $r->print(&mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is found by your directory service.',
                   4248:                                                   '<b>'.$username.'</b>').'<br />'.
                   4249:                                                   &mt('Consequently, the user was not created.'));
                   4250:                                         next;
                   4251:                                     }
1.5       raeburn  4252:                                 }
                   4253:                             }
                   4254:                         }
                   4255:                     }
1.1       raeburn  4256:                     if ($password || $env{'form.login'} eq 'loc') {
1.27      raeburn  4257:                         my $multiple = 0;
                   4258:                         my ($userresult,$authresult,$roleresult,$idresult);
                   4259:                         my (%userres,%authres,%roleres,%idres);
1.42      raeburn  4260:                         my $singlesec = '';
1.1       raeburn  4261:                         if ($role eq 'st') {
1.27      raeburn  4262:                             my $sec;
1.42      raeburn  4263:                             if (@secs > 0) {
                   4264:                                 $sec = $secs[0];
1.27      raeburn  4265:                             }
1.57      raeburn  4266:                             &modifystudent($userdomain,$username,$cid,$sec,
1.52      raeburn  4267:                                            $desiredhost,$context);
1.42      raeburn  4268:                             $roleresult =
                   4269:                                 &Apache::lonnet::modifystudent
1.57      raeburn  4270:                                     ($userdomain,$username,$id,$amode,$password,
1.42      raeburn  4271:                                      $fname,$mname,$lname,$gen,$sec,$enddate,
                   4272:                                      $startdate,$env{'form.forceid'},
1.52      raeburn  4273:                                      $desiredhost,$email,'manual','',$cid,
1.84      raeburn  4274:                                      '',$context,$inststatus);
1.42      raeburn  4275:                             $userresult = $roleresult;
1.1       raeburn  4276:                         } else {
1.42      raeburn  4277:                             if ($role ne '') { 
                   4278:                                 if ($context eq 'course' || $setting eq 'course') {
                   4279:                                     if ($customroles{$role}) {
                   4280:                                         $role = 'cr_'.$env{'user.domain'}.'_'.
                   4281:                                                 $env{'user.name'}.'_'.$role;
                   4282:                                     }
1.103     raeburn  4283:                                     if (($role ne 'cc') && ($role ne 'co')) { 
1.42      raeburn  4284:                                         if (@secs > 1) {
                   4285:                                             $multiple = 1;
                   4286:                                             foreach my $sec (@secs) {
                   4287:                                                 ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   4288:                                                 &modifyuserrole($context,$setting,
1.57      raeburn  4289:                                                     $changeauth,$cid,$userdomain,$username,
1.42      raeburn  4290:                                                     $id,$amode,$password,$fname,
                   4291:                                                     $mname,$lname,$gen,$sec,
                   4292:                                                     $env{'form.forceid'},$desiredhost,
                   4293:                                                     $email,$role,$enddate,
1.84      raeburn  4294:                                                     $startdate,$checkid,$inststatus);
1.42      raeburn  4295:                                             }
                   4296:                                         } elsif (@secs > 0) {
                   4297:                                             $singlesec = $secs[0];
                   4298:                                         }
1.27      raeburn  4299:                                     }
                   4300:                                 }
                   4301:                             }
                   4302:                             if (!$multiple) {
1.28      raeburn  4303:                                 ($userresult,$authresult,$roleresult,$idresult) = 
1.27      raeburn  4304:                                     &modifyuserrole($context,$setting,
1.57      raeburn  4305:                                                     $changeauth,$cid,$userdomain,$username, 
1.42      raeburn  4306:                                                     $id,$amode,$password,$fname,
                   4307:                                                     $mname,$lname,$gen,$singlesec,
                   4308:                                                     $env{'form.forceid'},$desiredhost,
1.84      raeburn  4309:                                                     $email,$role,$enddate,$startdate,
                   4310:                                                     $checkid,$inststatus);
1.27      raeburn  4311:                             }
                   4312:                         }
                   4313:                         if ($multiple) {
                   4314:                             foreach my $sec (sort(keys(%userres))) {
1.42      raeburn  4315:                                 $flushc =
1.27      raeburn  4316:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   4317:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
1.57      raeburn  4318:                                                     $username,$userdomain,\%userchg);
1.27      raeburn  4319: 
                   4320:                             }
                   4321:                         } else {
                   4322:                             $flushc = 
                   4323:                                 &user_change_result($r,$userresult,$authresult,
1.28      raeburn  4324:                                                     $roleresult,$idresult,\%counts,$flushc,
1.57      raeburn  4325:                                                     $username,$userdomain,\%userchg);
1.1       raeburn  4326:                         }
                   4327:                     } else {
                   4328:                         if ($context eq 'course') {
                   4329:                             $r->print('<br />'. 
1.74      bisitz   4330:       &mt('[_1]: Unable to enroll. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4331:                                      );
1.13      raeburn  4332:                         } elsif ($context eq 'author') {
1.1       raeburn  4333:                             $r->print('<br />'.
1.74      bisitz   4334:       &mt('[_1]: Unable to add co-author. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4335:                                      );
                   4336:                         } else {
                   4337:                             $r->print('<br />'.
1.74      bisitz   4338:       &mt('[_1]: Unable to add user. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4339:                                      );
                   4340:                         }
                   4341:                     }
                   4342:                 }
                   4343:             }
                   4344:         } # end of foreach (@userdata)
                   4345:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  4346:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.29      raeburn  4347:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  4348:                   "</p>\n");
                   4349:         if ($counts{'role'} > 0) {
                   4350:             $r->print("<p>\n".
1.29      raeburn  4351:                       &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.&mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.')."</p>\n");
                   4352:         } else {
                   4353:             $r->print('<p>'.&mt('No roles added').'</p>');
1.1       raeburn  4354:         }
                   4355:         if ($counts{'auth'} > 0) {
                   4356:             $r->print("<p>\n".
                   4357:                       &mt('Authentication changed for [_1] existing users.',
                   4358:                           $counts{'auth'})."</p>\n");
                   4359:         }
1.13      raeburn  4360:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1       raeburn  4361:         #####################################
1.29      raeburn  4362:         # Display list of students to drop  #
1.1       raeburn  4363:         #####################################
                   4364:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  4365:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  4366:             #  Get current classlist
1.30      raeburn  4367:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4368:             if (! defined($classlist)) {
1.29      raeburn  4369:                 $r->print('<form name="studentform" method="post" action="/adm/createuser" />'.
                   4370:                           '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                   4371:                           &mt('There are no students with current/future access to the course.').
                   4372:                           '</form>'."\n");
1.66      raeburn  4373:             } elsif (ref($classlist) eq 'HASH') {
1.1       raeburn  4374:                 # Remove the students we just added from the list of students.
1.30      raeburn  4375:                 foreach my $line (@userdata) {
                   4376:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4377:                     unless (($entries{$fields{'username'}} eq '') ||
                   4378:                             (!defined($entries{$fields{'username'}}))) {
                   4379:                         delete($classlist->{$entries{$fields{'username'}}.
                   4380:                                                 ':'.$domain});
                   4381:                     }
                   4382:                 }
                   4383:                 # Print out list of dropped students.
1.30      raeburn  4384:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  4385:             }
                   4386:         }
                   4387:     } # end of unless
1.29      raeburn  4388:     if ($env{'form.fullup'} ne 'yes') {
                   4389:         $r->print('</form>');
                   4390:     }
1.1       raeburn  4391: }
                   4392: 
1.13      raeburn  4393: sub print_namespacing_alerts {
                   4394:     my ($domain,$alerts,$curr_rules) = @_;
                   4395:     my $output;
                   4396:     if (ref($alerts) eq 'HASH') {
                   4397:         if (keys(%{$alerts}) > 0) {
                   4398:             if (ref($alerts->{'username'}) eq 'HASH') {
                   4399:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   4400:                     my $count;
                   4401:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   4402:                         $count = keys(%{$alerts->{'username'}{$dom}});
                   4403:                     }
                   4404:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4405:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4406:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4407:                                         'username',$domdesc,$count,'upload');
                   4408:                     }
                   4409:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4410:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   4411:                                    'username');
                   4412:                 }
                   4413:             }
                   4414:             if (ref($alerts->{'id'}) eq 'HASH') {
                   4415:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   4416:                     my $count;
                   4417:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   4418:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   4419:                     }
                   4420:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4421:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4422:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4423:                                               'id',$domdesc,$count,'upload');
                   4424:                     }
                   4425:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4426:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   4427:                 }
                   4428:             }
                   4429:         }
                   4430:     }
                   4431: }
                   4432: 
1.1       raeburn  4433: sub user_change_result {
1.29      raeburn  4434:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
1.57      raeburn  4435:         $username,$userdomain,$userchg) = @_;
1.1       raeburn  4436:     my $okresult = 0;
                   4437:     if ($userresult ne 'ok') {
                   4438:         if ($userresult =~ /^error:(.+)$/) {
                   4439:             my $error = $1;
                   4440:             $r->print('<br />'.
1.74      bisitz   4441:                   &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4442:         }
                   4443:     } else {
                   4444:         $counts->{'user'} ++;
                   4445:         $okresult = 1;
                   4446:     }
                   4447:     if ($authresult ne 'ok') {
                   4448:         if ($authresult =~ /^error:(.+)$/) {
                   4449:             my $error = $1;
                   4450:             $r->print('<br />'.
1.74      bisitz   4451:                   &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4452:         } 
                   4453:     } else {
                   4454:         $counts->{'auth'} ++;
                   4455:         $okresult = 1;
                   4456:     }
                   4457:     if ($roleresult ne 'ok') {
                   4458:         if ($roleresult =~ /^error:(.+)$/) {
                   4459:             my $error = $1;
                   4460:             $r->print('<br />'.
1.74      bisitz   4461:                   &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4462:         }
                   4463:     } else {
                   4464:         $counts->{'role'} ++;
                   4465:         $okresult = 1;
                   4466:     }
                   4467:     if ($okresult) {
                   4468:         $flushc++;
1.57      raeburn  4469:         $userchg->{$username.':'.$userdomain}=1;
1.1       raeburn  4470:         $r->print('. ');
                   4471:         if ($flushc>15) {
                   4472:             $r->rflush;
                   4473:             $flushc=0;
                   4474:         }
                   4475:     }
1.29      raeburn  4476:     if ($idresult) {
                   4477:         $r->print($idresult);
                   4478:     }
1.1       raeburn  4479:     return $flushc;
                   4480: }
                   4481: 
                   4482: # ========================================================= Menu Phase Two Drop
1.17      raeburn  4483: sub print_drop_menu {
1.101     raeburn  4484:     my ($r,$context,$permission,$crstype) = @_;
                   4485:     my $heading;
                   4486:     if ($crstype eq 'Community') {
                   4487:         $heading = &mt("Drop Members");
                   4488:     } else {
                   4489:         $heading = &mt("Drop Students");
                   4490:     }
                   4491:     $r->print('<h3>'.$heading.'</h3>'."\n".
1.17      raeburn  4492:               '<form name="studentform" method="post">'."\n");
1.30      raeburn  4493:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4494:     if (! defined($classlist)) {
1.101     raeburn  4495:         if ($crstype eq 'Community') {
                   4496:             $r->print(&mt('There are no members currently enrolled.')."\n");
                   4497:         } else {
                   4498:             $r->print(&mt('There are no students currently enrolled.')."\n");
                   4499:         }
1.30      raeburn  4500:     } else {
1.101     raeburn  4501:         &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
1.1       raeburn  4502:     }
1.17      raeburn  4503:     $r->print('</form>'. &Apache::loncommon::end_page());
1.1       raeburn  4504:     return;
                   4505: }
                   4506: 
                   4507: # ================================================================== Phase four
                   4508: 
1.11      raeburn  4509: sub update_user_list {
1.118     raeburn  4510:     my ($r,$context,$setting,$choice,$crstype) = @_;
1.11      raeburn  4511:     my $now = time;
1.1       raeburn  4512:     my $count=0;
1.101     raeburn  4513:     if ($context eq 'course') {
                   4514:         $crstype = &Apache::loncommon::course_type();
                   4515:     }
1.11      raeburn  4516:     my @changelist;
1.29      raeburn  4517:     if ($choice eq 'drop') {
                   4518:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   4519:     } else {
1.11      raeburn  4520:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   4521:     }
                   4522:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   4523:                                    'delete'   => 'Deleted',
                   4524:                                    'reenable' => 'Re-enabled',
1.17      raeburn  4525:                                    'activate' => 'Activated',
                   4526:                                    'chgdates' => 'Changed Access Dates for',
1.118     raeburn  4527:                                    'chgsec'   => 'Changed section(s) for',
1.17      raeburn  4528:                                    'drop'     => 'Dropped',
1.11      raeburn  4529:                                  },
                   4530:                         error => {'revoke'    => 'revoking',
                   4531:                                   'delete'    => 'deleting',
                   4532:                                   'reenable'  => 're-enabling',
                   4533:                                   'activate'  => 'activating',
1.17      raeburn  4534:                                   'chgdates'  => 'changing access dates for',
                   4535:                                   'chgsec'    => 'changing section for',
                   4536:                                   'drop'      => 'dropping',
1.11      raeburn  4537:                                  },
                   4538:                       );
                   4539:     my ($startdate,$enddate);
                   4540:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   4541:         ($startdate,$enddate) = &get_dates_from_form();
                   4542:     }
                   4543:     foreach my $item (@changelist) {
1.118     raeburn  4544:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,
                   4545:             @sections,$scopestem,$singlesec,$showsecs,$warn_singlesec,
                   4546:             $nothingtodo,$keepnosection);
1.17      raeburn  4547:         if ($choice eq 'drop') {
                   4548:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   4549:             $role = 'st';
                   4550:             $cid = $env{'request.course.id'};
                   4551:             $scopestem = '/'.$cid;
                   4552:             $scopestem =~s/\_/\//g;
                   4553:             if ($sec eq '') {
                   4554:                 $scope = $scopestem;
                   4555:             } else {
                   4556:                 $scope = $scopestem.'/'.$sec;
                   4557:             }
                   4558:         } elsif ($context eq 'course') {
1.11      raeburn  4559:             ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
                   4560:             $cid = $env{'request.course.id'};
                   4561:             $scopestem = '/'.$cid;
                   4562:             $scopestem =~s/\_/\//g;
                   4563:             if ($sec eq '') {
                   4564:                 $scope = $scopestem;
                   4565:             } else {
                   4566:                 $scope = $scopestem.'/'.$sec;
                   4567:             }
1.13      raeburn  4568:         } elsif ($context eq 'author') {
1.11      raeburn  4569:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   4570:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   4571:         } elsif ($context eq 'domain') {
                   4572:             if ($setting eq 'domain') {
                   4573:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   4574:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  4575:             } elsif ($setting eq 'author') { 
1.11      raeburn  4576:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   4577:             } elsif ($setting eq 'course') {
                   4578:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype) = 
                   4579:                     split(/\:/,$item);
                   4580:                 $scope = '/'.$cid;
                   4581:                 $scope =~s/\_/\//g;
                   4582:                 if ($sec ne '') {
                   4583:                     $scope .= '/'.$sec;
                   4584:                 }
                   4585:             }
                   4586:         }
1.101     raeburn  4587:         my $plrole = &Apache::lonnet::plaintext($role,$crstype);
1.11      raeburn  4588:         my $start = $env{'form.'.$item.'_start'};
                   4589:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  4590:         if ($choice eq 'drop') {
                   4591:             # drop students
                   4592:             $end = $now;
                   4593:             $type = 'manual';
                   4594:             $result =
1.52      raeburn  4595:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.17      raeburn  4596:         } elsif ($choice eq 'revoke') {
                   4597:             # revoke or delete user role
1.11      raeburn  4598:             $end = $now; 
                   4599:             if ($role eq 'st') {
                   4600:                 $result = 
1.52      raeburn  4601:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4602:             } else {
                   4603:                 $result = 
1.52      raeburn  4604:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
                   4605:                                                 '','',$context);
1.11      raeburn  4606:             }
                   4607:         } elsif ($choice eq 'delete') {
                   4608:             if ($role eq 'st') {
1.52      raeburn  4609:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context);
1.29      raeburn  4610:             }
                   4611:             $result =
                   4612:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
1.52      raeburn  4613:                                             $start,1,'',$context);
1.11      raeburn  4614:         } else {
                   4615:             #reenable, activate, change access dates or change section
                   4616:             if ($choice ne 'chgsec') {
                   4617:                 $start = $startdate; 
                   4618:                 $end = $enddate;
                   4619:             }
                   4620:             if ($choice eq 'reenable') {
                   4621:                 if ($role eq 'st') {
1.52      raeburn  4622:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4623:                 } else {
                   4624:                     $result = 
                   4625:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4626:                                                     $now,'','',$context);
1.11      raeburn  4627:                 }
                   4628:             } elsif ($choice eq 'activate') {
                   4629:                 if ($role eq 'st') {
1.52      raeburn  4630:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4631:                 } else {
                   4632:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4633:                                             $now,'','',$context);
1.11      raeburn  4634:                 }
                   4635:             } elsif ($choice eq 'chgdates') {
                   4636:                 if ($role eq 'st') {
1.52      raeburn  4637:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4638:                 } else {
                   4639:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4640:                                                 $start,'','',$context);
1.11      raeburn  4641:                 }
                   4642:             } elsif ($choice eq 'chgsec') {
                   4643:                 my (@newsecs,$revresult,$nochg,@retained);
1.103     raeburn  4644:                 if (($role ne 'cc') && ($role ne 'co')) {
1.117     raeburn  4645:                     my @secs = sort(split(/,/,$env{'form.newsecs'}));
                   4646:                     if (@secs) {
                   4647:                         my %curr_groups = &Apache::longroup::coursegroups();
                   4648:                         foreach my $sec (@secs) {
                   4649:                             next if (($sec =~ /\W/) || ($sec eq 'none') ||
                   4650:                             (exists($curr_groups{$sec})));
                   4651:                             push(@newsecs,$sec);
                   4652:                         }
                   4653:                     }
1.11      raeburn  4654:                 }
                   4655:                 # remove existing section if not to be retained.   
1.118     raeburn  4656:                 if (!$env{'form.retainsec'} || ($role eq 'st')) {
1.11      raeburn  4657:                     if ($sec eq '') {
                   4658:                         if (@newsecs == 0) {
1.118     raeburn  4659:                             $result = 'ok';
1.11      raeburn  4660:                             $nochg = 1;
1.118     raeburn  4661:                             $nothingtodo = 1;
1.40      raeburn  4662:                         } else {
                   4663:                             $revresult =
                   4664:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4665:                                                             $scope,$role,
                   4666:                                                             '','',$context);
1.40      raeburn  4667:                         } 
1.11      raeburn  4668:                     } else {
1.28      raeburn  4669:                         if (@newsecs > 0) {
                   4670:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   4671:                                 push(@retained,$sec);
                   4672:                             } else {
                   4673:                                 $revresult =
                   4674:                                     &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4675:                                                                 $scope,$role,
                   4676:                                                                 '','',$context);
1.28      raeburn  4677:                             }
                   4678:                         } else {
1.11      raeburn  4679:                             $revresult =
1.28      raeburn  4680:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4681:                                                             $scope,$role,
                   4682:                                                             '','',$context);
1.11      raeburn  4683:                         }
                   4684:                     }
                   4685:                 } else {
1.28      raeburn  4686:                     if ($sec eq '') {
                   4687:                         $nochg = 1;
1.118     raeburn  4688:                         $keepnosection = 1;
                   4689:                     } else {
1.28      raeburn  4690:                         push(@retained,$sec);
                   4691:                     }
1.11      raeburn  4692:                 }
                   4693:                 # add new sections
1.118     raeburn  4694:                 my (@diffs,@shownew);
                   4695:                 if (@retained) {
                   4696:                     @diffs = &Apache::loncommon::compare_arrays(\@retained,\@newsecs);
                   4697:                 } else {
                   4698:                     @diffs = @newsecs;
                   4699:                 }
1.11      raeburn  4700:                 if (@newsecs == 0) {
1.118     raeburn  4701:                     if ($nochg) {
                   4702:                         $result = 'ok';
                   4703:                         $nothingtodo = 1;
                   4704:                     } else {
1.28      raeburn  4705:                         if ($role eq 'st') {
                   4706:                             $result = 
1.52      raeburn  4707:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context);
1.28      raeburn  4708:                         } else {
                   4709:                             my $newscope = $scopestem;
1.52      raeburn  4710:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
1.11      raeburn  4711:                         }
                   4712:                     }
1.118     raeburn  4713:                     $showsecs = &mt('No section');
                   4714:                 } elsif (@diffs == 0) {
                   4715:                     $result = 'ok';
                   4716:                     $nothingtodo = 1;
1.11      raeburn  4717:                 } else {
1.118     raeburn  4718:                     foreach my $newsec (@newsecs) {
1.11      raeburn  4719:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   4720:                             if ($role eq 'st') {
1.52      raeburn  4721:                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context);
1.118     raeburn  4722:                                 if (@newsecs > 1) {
                   4723:                                     my $showsingle; 
                   4724:                                     if ($newsec eq '') {
                   4725:                                         $showsingle = &mt('No section');
                   4726:                                     } else {
                   4727:                                         $showsingle = $newsec;
                   4728:                                     }
                   4729:                                     if ($crstype eq 'Community') {
                   4730:                                         $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each community member may only be in one section at a time.','<i>'.$showsingle.'</i>');
                   4731:                                     } else { 
                   4732:                                         $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each student may only be in one section of a course at a time.','<i>'.$showsingle.'</i>');
                   4733:                                     }
                   4734:                                     $showsecs = $showsingle; 
                   4735:                                     last;
                   4736:                                 } else {
                   4737:                                     if ($newsec eq '') {
                   4738:                                         $showsecs = &mt('No section');
                   4739:                                     } else {
                   4740:                                         $showsecs = $newsec;
                   4741:                                     }
                   4742:                                 }
1.11      raeburn  4743:                             } else {
                   4744:                                 my $newscope = $scopestem;
                   4745:                                 if ($newsec ne '') {
                   4746:                                    $newscope .= '/'.$newsec;
1.118     raeburn  4747:                                    push(@shownew,$newsec); 
1.11      raeburn  4748:                                 }
                   4749:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   4750:                                                         $newscope,$role,$end,$start);
1.118     raeburn  4751:                                 
1.11      raeburn  4752:                             }
                   4753:                         }
                   4754:                     }
                   4755:                 }
1.118     raeburn  4756:                 unless ($role eq 'st') {
                   4757:                     unless ($showsecs) {
                   4758:                         my @tolist = sort(@shownew,@retained);
                   4759:                         if ($keepnosection) {
                   4760:                             push(@tolist,&mt('No section'));
                   4761:                         }
                   4762:                         $showsecs = join(', ',@tolist);
                   4763:                     }
                   4764:                 }
1.11      raeburn  4765:             }
                   4766:         }
1.17      raeburn  4767:         my $extent = $scope;
                   4768:         if ($choice eq 'drop' || $context eq 'course') {
                   4769:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   4770:             if ($cdesc) {
                   4771:                 $extent = $cdesc;
                   4772:             }
                   4773:         }
1.1       raeburn  4774:         if ($result eq 'ok' || $result eq 'ok:') {
1.118     raeburn  4775:             my $dates;
                   4776:             if (($choice eq 'chgsec') || ($choice eq 'chgdates')) {
                   4777:                 $dates = &dates_feedback($start,$end,$now);
                   4778:             }
                   4779:             if ($choice eq 'chgsec') {
                   4780:                 if ($nothingtodo) {
                   4781:                     $r->print(&mt("Section assignment for role of '[_1]' in [_2] for '[_3]' unchanged.",$plrole,$extent,'<i>'.
                   4782:                           &Apache::loncommon::plainname($uname,$udom).
                   4783:                           '</i>').' ');
                   4784:                     if ($sec eq '') {
                   4785:                         $r->print(&mt('[_1]No section[_2] - [_3]','<b>','</b>',$dates));
                   4786:                     } else {
                   4787:                         $r->print(&mt('Section(s): [_1] - [_2]',
                   4788:                                       '<b>'.$showsecs.'</b>',$dates));
                   4789:                     }
                   4790:                     $r->print('<br />');
                   4791:                 } else {
                   4792:                     $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
                   4793:                         '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
                   4794:                         '<b>'.$showsecs.'</b>',$dates).'<br />');
                   4795:                    $count ++;
                   4796:                }
                   4797:                if ($warn_singlesec) {
                   4798:                    $r->print('<div class="LC_warning">'.$warn_singlesec.'</div>');
                   4799:                }
                   4800:             } elsif ($choice eq 'chgdates') {
                   4801:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent, 
1.121     raeburn  4802:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  4803:                       $dates).'<br />');
                   4804:                $count ++;
                   4805:             } else {
                   4806:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
1.121     raeburn  4807:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>').
1.118     raeburn  4808:                           '<br />');
                   4809:                 $count ++;
                   4810:             }
1.1       raeburn  4811:         } else {
                   4812:             $r->print(
1.118     raeburn  4813:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
                   4814:                     $plrole,$extent,
1.121     raeburn  4815:                     '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  4816:                     $result).'<br />');
1.11      raeburn  4817:         }
                   4818:     }
1.32      raeburn  4819:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  4820:     if ($choice eq 'drop') {
                   4821:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   4822:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   4823:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   4824:     } else {
                   4825:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   4826:             if ($env{'form.'.$item} ne '') {
                   4827:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   4828:                           '" />'."\n");
                   4829:             }
1.32      raeburn  4830:         }
                   4831:     }
1.118     raeburn  4832:     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} for [quant,_1,user role,user roles,no user roles].",$count).'</b></p>');
1.11      raeburn  4833:     if ($count > 0) {
1.17      raeburn  4834:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.74      bisitz   4835:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
1.11      raeburn  4836:         }
                   4837:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  4838:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.11      raeburn  4839:     }
                   4840:     if ($env{'form.makedatesdefault'}) {
                   4841:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.101     raeburn  4842:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  4843:         }
                   4844:     }
1.33      raeburn  4845:     my $linktext = &mt('Display User Lists');
                   4846:     if ($choice eq 'drop') {
                   4847:         $linktext = &mt('Display current class roster');
                   4848:     }
                   4849:     $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
1.1       raeburn  4850: }
                   4851: 
1.118     raeburn  4852: sub dates_feedback {
                   4853:     my ($start,$end,$now) = @_;
                   4854:     my $dates;
                   4855:     if ($start < $now) {
                   4856:         if ($end == 0) {
                   4857:             $dates .= &mt('role(s) active now; no end date');
                   4858:         } elsif ($end > $now) {
                   4859:             $dates = &mt('role(s) active now; ends [_1].',&Apache::lonlocal::locallocaltime($end));
                   4860:         } else {
                   4861:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   4862:         }
                   4863:      } else {
                   4864:         if ($end == 0 || $end > $now) {
                   4865:             $dates = &mt('future role(s); starts: [_1].',&Apache::lonlocal::locallocaltime($start));
                   4866:         } else {
                   4867:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   4868:         }
                   4869:     }
                   4870:     return $dates;
                   4871: }
                   4872: 
1.8       raeburn  4873: sub classlist_drop {
1.29      raeburn  4874:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  4875:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  4876:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.8       raeburn  4877:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
1.63      raeburn  4878:             my %user;
                   4879:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
1.8       raeburn  4880:             return &mt('Drop from classlist: [_1]',
                   4881:                        '<b>'.$result.'</b>').'<br />';
                   4882:         }
                   4883:     }
                   4884: }
                   4885: 
                   4886: sub active_student_roles {
                   4887:     my ($cnum,$cdom,$uname,$udom) = @_;
                   4888:     my %roles =
                   4889:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   4890:                                       ['future','active'],['st']);
                   4891:     return exists($roles{"$cnum:$cdom:st"});
                   4892: }
                   4893: 
1.1       raeburn  4894: sub section_check_js {
1.8       raeburn  4895:     my $groupslist= &get_groupslist();
1.1       raeburn  4896:     return <<"END";
                   4897: function validate(caller) {
1.9       raeburn  4898:     var groups = new Array($groupslist);
1.1       raeburn  4899:     var secname = caller.value;
                   4900:     if ((secname == 'all') || (secname == 'none')) {
                   4901:         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
                   4902:         return 'error';
                   4903:     }
                   4904:     if (secname != '') {
                   4905:         for (var k=0; k<groups.length; k++) {
                   4906:             if (secname == groups[k]) {
                   4907:                 alert("'"+secname+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
                   4908:                 return 'error';
                   4909:             }
                   4910:         }
                   4911:     }
                   4912:     return 'ok';
                   4913: }
                   4914: END
                   4915: }
                   4916: 
                   4917: sub set_login {
                   4918:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   4919:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4920:     my $response;
                   4921:     my ($authnum,%can_assign) =
                   4922:         &Apache::loncommon::get_assignable_auth($dom);
                   4923:     if ($authnum) {
                   4924:         $response = &Apache::loncommon::start_data_table();
                   4925:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   4926:             $response .= &Apache::loncommon::start_data_table_row().
                   4927:                          '<td>'.$authformkrb.'</td>'.
                   4928:                          &Apache::loncommon::end_data_table_row()."\n";
                   4929:         }
                   4930:         if ($can_assign{'int'}) {
                   4931:             $response .= &Apache::loncommon::start_data_table_row().
                   4932:                          '<td>'.$authformint.'</td>'.
                   4933:                          &Apache::loncommon::end_data_table_row()."\n"
                   4934:         }
                   4935:         if ($can_assign{'loc'}) {
                   4936:             $response .= &Apache::loncommon::start_data_table_row().
                   4937:                          '<td>'.$authformloc.'</td>'.
                   4938:                          &Apache::loncommon::end_data_table_row()."\n";
                   4939:         }
                   4940:         $response .= &Apache::loncommon::end_data_table();
                   4941:     }
                   4942:     return $response;
                   4943: }
                   4944: 
1.8       raeburn  4945: sub course_sections {
1.51      raeburn  4946:     my ($sections_count,$role,$current_sec) = @_;
1.8       raeburn  4947:     my $output = '';
                   4948:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
1.29      raeburn  4949:     my $numsec = scalar(@sections);
1.92      bisitz   4950:     my $is_selected = ' selected="selected"';
1.29      raeburn  4951:     if ($numsec <= 1) {
1.8       raeburn  4952:         $output = '<select name="currsec_'.$role.'" >'."\n".
1.51      raeburn  4953:                   '  <option value="">'.&mt('Select').'</option>'."\n";
                   4954:         if ($current_sec eq 'none') {
                   4955:             $output .=       
                   4956:                   '  <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
                   4957:         } else {
                   4958:             $output .=
1.29      raeburn  4959:                   '  <option value="">'.&mt('No section').'</option>'."\n";
1.51      raeburn  4960:         }
1.29      raeburn  4961:         if ($numsec == 1) {
1.51      raeburn  4962:             if ($current_sec eq $sections[0]) {
                   4963:                 $output .=
                   4964:                   '  <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
                   4965:             } else {
                   4966:                 $output .=  
1.8       raeburn  4967:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.51      raeburn  4968:             }
1.29      raeburn  4969:         }
1.8       raeburn  4970:     } else {
                   4971:         $output = '<select name="currsec_'.$role.'" ';
                   4972:         my $multiple = 4;
                   4973:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  4974:         if ($role eq 'st') {
                   4975:             $output .= '>'."\n".
1.51      raeburn  4976:                        '  <option value="">'.&mt('Select').'</option>'."\n";
                   4977:             if ($current_sec eq 'none') {
                   4978:                 $output .= 
                   4979:                        '  <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
                   4980:             } else {
                   4981:                 $output .=
1.29      raeburn  4982:                        '  <option value="">'.&mt('No section')."</option>\n";
1.51      raeburn  4983:             }
1.29      raeburn  4984:         } else {
                   4985:             $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
                   4986:         }
1.8       raeburn  4987:         foreach my $sec (@sections) {
1.51      raeburn  4988:             if ($current_sec eq $sec) {
                   4989:                 $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
                   4990:             } else {
                   4991:                 $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   4992:             }
1.8       raeburn  4993:         }
                   4994:     }
                   4995:     $output .= '</select>';
                   4996:     return $output;
                   4997: }
                   4998: 
                   4999: sub get_groupslist {
                   5000:     my $groupslist;
                   5001:     my %curr_groups = &Apache::longroup::coursegroups();
                   5002:     if (%curr_groups) {
                   5003:         $groupslist = join('","',sort(keys(%curr_groups)));
                   5004:         $groupslist = '"'.$groupslist.'"';
                   5005:     }
1.11      raeburn  5006:     return $groupslist; 
1.8       raeburn  5007: }
                   5008: 
                   5009: sub setsections_javascript {
1.103     raeburn  5010:     my ($formname,$groupslist,$mode,$checkauth,$crstype) = @_;
1.28      raeburn  5011:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   5012:     if ($mode eq 'upload') {
                   5013:         $checkincluded = 'formname.name == "'.$formname.'"';
                   5014:         $finish = "return 'ok';";
                   5015:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   5016:     } elsif ($formname eq 'cu') {
1.8       raeburn  5017:         $checkincluded = 'formname.elements[i-1].checked == true';
1.37      raeburn  5018:         if ($checkauth) {
                   5019:             $finish = "var authcheck = auth_check();\n".
                   5020:                       "   if (authcheck == 'ok') {\n".
                   5021:                       "       formname.submit();\n".
                   5022:                       "   }\n";
                   5023:         } else {
                   5024:             $finish = 'formname.submit()';
                   5025:         }
1.28      raeburn  5026:         $rolecode = "var match = str.split('_');
                   5027:                 var role = match[3];\n";
                   5028:     } elsif ($formname eq 'enrollstudent') {
                   5029:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  5030:         if ($checkauth) {
                   5031:             $finish = "var authcheck = auth_check();\n".
                   5032:                       "   if (authcheck == 'ok') {\n".
                   5033:                       "       formname.submit();\n".
                   5034:                       "   }\n";
                   5035:         } else {
                   5036:             $finish = 'formname.submit()';
                   5037:         }
1.28      raeburn  5038:         $rolecode = "var match = str.split('_');
                   5039:                 var role = match[1];\n";
1.8       raeburn  5040:     } else {
1.28      raeburn  5041:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  5042:         $finish = "seccheck = 'ok';";
1.28      raeburn  5043:         $rolecode = "var match = str.split('_');
                   5044:                 var role = match[1];\n";
1.11      raeburn  5045:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  5046:     }
                   5047:     my %alerts = &Apache::lonlocal::texthash(
                   5048:                     secd => 'Section designations do not apply to Course Coordinator roles.',
1.103     raeburn  5049:                     sedn => 'Section designations do not apply to Coordinator roles.',
1.8       raeburn  5050:                     accr => 'A course coordinator role will be added with access to all sections.',
1.103     raeburn  5051:                     acor => 'A coordinator role will be added with access to all sections',
1.8       raeburn  5052:                     inea => 'In each course, each user may only have one student role at a time.',
1.125     raeburn  5053:                     inco => 'In each community, each user may only have one member role at a time.',
1.8       raeburn  5054:                     youh => 'You had selected ',
                   5055:                     secs => 'sections.',
                   5056:                     plmo => 'Please modify your selections so they include no more than one section.',
                   5057:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   5058:                     plch => 'Please choose a different section name.',
                   5059:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   5060:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.113     raeburn  5061:                     nonw => 'Section names may only contain letters or numbers.',
1.11      raeburn  5062:                  );                
1.8       raeburn  5063:     $setsection_js .= <<"ENDSECCODE";
                   5064: 
1.103     raeburn  5065: function setSections(formname,crstype) {
1.8       raeburn  5066:     var re1 = /^currsec_/;
1.113     raeburn  5067:     var re2 =/\\W/;
1.115     raeburn  5068:     var trimleading = /^\\s+/;
                   5069:     var trimtrailing = /\\s+\$/;
1.8       raeburn  5070:     var groups = new Array($groupslist);
                   5071:     for (var i=0;i<formname.elements.length;i++) {
                   5072:         var str = formname.elements[i].name;
                   5073:         var checkcurr = str.match(re1);
                   5074:         if (checkcurr != null) {
1.115     raeburn  5075:             var num = i;
1.8       raeburn  5076:             if ($checkincluded) {
1.28      raeburn  5077:                 $rolecode
1.103     raeburn  5078:                 if (role == 'cc' || role == 'co') {
                   5079:                     if (role == 'cc') {
                   5080:                         alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   5081:                     } else {
                   5082:                         alert("$alerts{'sedn'}\\n$alerts{'acor'}");
                   5083:                     }
                   5084:                 } else {
1.8       raeburn  5085:                     var sections = '';
                   5086:                     var numsec = 0;
1.115     raeburn  5087:                     var fromexisting = new Array();
                   5088:                     for (var j=0; j<formname.elements[num].length; j++) {
                   5089:                         if (formname.elements[num].options[j].selected == true ) {
                   5090:                             var addsec = formname.elements[num].options[j].value;
1.119     raeburn  5091:                             if ((addsec != "") && (addsec != null)) {
1.115     raeburn  5092:                                 fromexisting.push(addsec);
1.8       raeburn  5093:                                 if (numsec == 0) {
1.115     raeburn  5094:                                     sections = addsec;
                   5095:                                 } else {
                   5096:                                     sections = sections + "," +  addsec;
1.8       raeburn  5097:                                 }
1.115     raeburn  5098:                                 numsec ++;
1.8       raeburn  5099:                             }
                   5100:                         }
                   5101:                     }
1.115     raeburn  5102:                     var newsecs = formname.elements[num+1].value;
1.113     raeburn  5103:                     var validsecs = new Array();
1.115     raeburn  5104:                     var validsecstr = '';
1.113     raeburn  5105:                     var badsecs = new Array();
1.8       raeburn  5106:                     if (newsecs != null && newsecs != "") {
1.115     raeburn  5107:                         var numsplit;
                   5108:                         if (newsecs.indexOf(',') == -1) {
                   5109:                             numsplit = new Array(newsecs);
                   5110:                         } else {
                   5111:                             numsplit = newsecs.split(/,/g);
                   5112:                         }
1.117     raeburn  5113:                         for (var m=0; m<numsplit.length; m++) {
                   5114:                             var newsec = numsplit[m];
1.115     raeburn  5115:                             newsec = newsec.replace(trimleading,'');
                   5116:                             newsec = newsec.replace(trimtrailing,'');
                   5117:                             if (re2.test(newsec) == true) {
                   5118:                                 badsecs.push(newsec);
1.113     raeburn  5119:                             } else {
1.115     raeburn  5120:                                 if (newsec != '') {
                   5121:                                     var isnew = 1;
                   5122:                                     if (fromexisting != null) {
1.117     raeburn  5123:                                         for (var n=0; n<fromexisting.length; n++) {
                   5124:                                             if (newsec == fromexisting[n]) {
1.115     raeburn  5125:                                                 isnew = 0;
                   5126:                                             }
                   5127:                                         }
                   5128:                                     }
                   5129:                                     if (isnew == 1) {
                   5130:                                         validsecs.push(newsec);
                   5131:                                     }
                   5132:                                 }
1.113     raeburn  5133:                             }
                   5134:                         }
                   5135:                         if (badsecs.length > 0) {
                   5136:                             alert("$alerts{'nonw'}\\n$alerts{'plch'}");
                   5137:                             return;
                   5138:                         }
                   5139:                         numsec = numsec + validsecs.length;
1.8       raeburn  5140:                     }
                   5141:                     if ((role == 'st') && (numsec > 1)) {
1.103     raeburn  5142:                         if (crstype == 'Community') {
                   5143:                             alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5144:                         } else {
                   5145:                             alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5146:                         }
1.8       raeburn  5147:                         return;
1.115     raeburn  5148:                     } else {
                   5149:                         if (validsecs != null) {
                   5150:                             for (var j=0; j<validsecs.length; j++) {
                   5151:                                 if (validsecstr == '' || validsecstr == null) {
                   5152:                                     validsecstr = validsecs[j];
                   5153:                                 } else {
                   5154:                                     validsecstr += ','+validsecs[j];
                   5155:                                 }
                   5156:                                 if ((validsecs[j] == 'all') ||
                   5157:                                     (validsecs[j] == 'none')) {
                   5158:                                     alert("'"+validsecs[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
1.8       raeburn  5159:                                     return;
                   5160:                                 }
                   5161:                                 for (var k=0; k<groups.length; k++) {
1.115     raeburn  5162:                                     if (validsecs[j] == groups[k]) {
                   5163:                                         alert("'"+validsecs[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
1.8       raeburn  5164:                                         return;
                   5165:                                     }
                   5166:                                 }
                   5167:                             }
                   5168:                         }
                   5169:                     }
1.115     raeburn  5170:                     if ((validsecstr != '') && (validsecstr != null)) {
1.117     raeburn  5171:                         if ((sections == '') || (sections == null)) {
                   5172:                             sections = validsecstr;
                   5173:                         } else {
1.115     raeburn  5174:                             sections = sections + "," + validsecstr;
                   5175:                         }
                   5176:                     }
                   5177:                     formname.elements[num+2].value = sections;
1.8       raeburn  5178:                 }
                   5179:             }
                   5180:         }
                   5181:     }
                   5182:     $finish
                   5183: }
                   5184: ENDSECCODE
1.11      raeburn  5185:     return $setsection_js; 
1.8       raeburn  5186: }
                   5187: 
1.15      raeburn  5188: sub can_create_user {
                   5189:     my ($dom,$context,$usertype) = @_;
                   5190:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5191:     my $cancreate = 1;
1.28      raeburn  5192:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   5193:         return $cancreate;
                   5194:     }
1.15      raeburn  5195:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   5196:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
1.100     raeburn  5197:             if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
1.15      raeburn  5198:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   5199:                 if ($creation eq 'none') {
                   5200:                     $cancreate = 0;
                   5201:                 } elsif ($creation ne 'any') {
                   5202:                     if (defined($usertype)) {
                   5203:                         if ($creation ne $usertype) {
                   5204:                             $cancreate = 0;
                   5205:                         }
                   5206:                     }
                   5207:                 }
                   5208:             }
                   5209:         }
                   5210:     }
                   5211:     return $cancreate;
                   5212: }
                   5213: 
1.20      raeburn  5214: sub can_modify_userinfo {
                   5215:     my ($context,$dom,$fields,$userroles) = @_;
                   5216:     my %domconfig =
                   5217:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   5218:                                 $dom);
                   5219:     my %canmodify;
                   5220:     if (ref($fields) eq 'ARRAY') {
                   5221:         foreach my $field (@{$fields}) {
                   5222:             $canmodify{$field}  = 0;
                   5223:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   5224:                 $canmodify{$field} = 1;
                   5225:             } else {
                   5226:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   5227:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   5228:                         if (ref($userroles) eq 'ARRAY') {
                   5229:                             foreach my $role (@{$userroles}) {
                   5230:                                 my $testrole;
1.60      raeburn  5231:                                 if ($context eq 'selfcreate') {
                   5232:                                     $testrole = $role;
1.20      raeburn  5233:                                 } else {
1.60      raeburn  5234:                                     if ($role =~ /^cr\//) {
                   5235:                                         $testrole = 'cr';
                   5236:                                     } else {
                   5237:                                         $testrole = $role;
                   5238:                                     }
1.20      raeburn  5239:                                 }
                   5240:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   5241:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   5242:                                         $canmodify{$field} = 1;
                   5243:                                         last;
                   5244:                                     }
                   5245:                                 }
                   5246:                             }
                   5247:                         } else {
                   5248:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   5249:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   5250:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   5251:                                         $canmodify{$field} = 1;
                   5252:                                         last;
                   5253:                                     }
                   5254:                                 }
                   5255:                             }
                   5256:                         }
                   5257:                     }
                   5258:                 } elsif ($context eq 'course') {
                   5259:                     if (ref($userroles) eq 'ARRAY') {
                   5260:                         if (grep(/^st$/,@{$userroles})) {
                   5261:                             $canmodify{$field} = 1;
                   5262:                         }
                   5263:                     } else {
                   5264:                         $canmodify{$field} = 1;
                   5265:                     }
                   5266:                 }
                   5267:             }
                   5268:         }
                   5269:     }
                   5270:     return %canmodify;
                   5271: }
                   5272: 
1.18      raeburn  5273: sub check_usertype {
                   5274:     my ($dom,$uname,$rules) = @_;
                   5275:     my $usertype;
                   5276:     if (ref($rules) eq 'HASH') {
                   5277:         my @user_rules = keys(%{$rules});
                   5278:         if (@user_rules > 0) {
                   5279:             my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   5280:             if (keys(%rule_check) > 0) {
                   5281:                 $usertype = 'unofficial';
                   5282:                 foreach my $item (keys(%rule_check)) {
                   5283:                     if ($rule_check{$item}) {
                   5284:                         $usertype = 'official';
                   5285:                         last;
                   5286:                     }
                   5287:                 }
                   5288:             }
                   5289:         }
                   5290:     }
                   5291:     return $usertype;
                   5292: }
                   5293: 
1.17      raeburn  5294: sub roles_by_context {
1.101     raeburn  5295:     my ($context,$custom,$crstype) = @_;
1.17      raeburn  5296:     my @allroles;
                   5297:     if ($context eq 'course') {
1.99      raeburn  5298:         @allroles = ('st');
                   5299:         if ($env{'request.role'} =~ m{^dc\./}) {
                   5300:             push(@allroles,'ad');
                   5301:         }
1.101     raeburn  5302:         push(@allroles,('ta','ep','in'));
                   5303:         if ($crstype eq 'Community') {
                   5304:             push(@allroles,'co');
                   5305:         } else {
                   5306:             push(@allroles,'cc');
                   5307:         }
1.17      raeburn  5308:         if ($custom) {
                   5309:             push(@allroles,'cr');
                   5310:         }
                   5311:     } elsif ($context eq 'author') {
                   5312:         @allroles = ('ca','aa');
                   5313:     } elsif ($context eq 'domain') {
1.99      raeburn  5314:         @allroles = ('li','ad','dg','sc','au','dc');
1.17      raeburn  5315:     }
                   5316:     return @allroles;
                   5317: }
                   5318: 
1.16      raeburn  5319: sub get_permission {
1.101     raeburn  5320:     my ($context,$crstype) = @_;
1.16      raeburn  5321:     my %permission;
                   5322:     if ($context eq 'course') {
1.17      raeburn  5323:         my $custom = 1;
1.101     raeburn  5324:         my @allroles = &roles_by_context($context,$custom,$crstype);
1.17      raeburn  5325:         foreach my $role (@allroles) {
                   5326:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   5327:                 $permission{'cusr'} = 1;
                   5328:                 last;
                   5329:             }
1.16      raeburn  5330:         }
                   5331:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   5332:             $permission{'custom'} = 1;
                   5333:         }
                   5334:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   5335:             $permission{'view'} = 1;
                   5336:         }
                   5337:         if (!$permission{'view'}) {
                   5338:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5339:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   5340:             if ($permission{'view'}) {
                   5341:                 $permission{'view_section'} = $env{'request.course.sec'};
                   5342:             }
                   5343:         }
1.17      raeburn  5344:         if (!$permission{'cusr'}) {
                   5345:             if ($env{'request.course.sec'} ne '') {
                   5346:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5347:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   5348:                 if ($permission{'cusr'}) {
                   5349:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   5350:                 }
                   5351:             }
                   5352:         }
1.16      raeburn  5353:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   5354:             $permission{'grp_manage'} = 1;
                   5355:         }
                   5356:     } elsif ($context eq 'author') {
                   5357:         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
                   5358:         $permission{'view'} = $permission{'cusr'};
                   5359:     } else {
1.17      raeburn  5360:         my @allroles = &roles_by_context($context);
                   5361:         foreach my $role (@allroles) {
1.28      raeburn  5362:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   5363:                 $permission{'cusr'} = 1;
1.17      raeburn  5364:                 last;
                   5365:             }
                   5366:         }
                   5367:         if (!$permission{'cusr'}) {
                   5368:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   5369:                 $permission{'cusr'} = 1;
                   5370:             }
1.16      raeburn  5371:         }
                   5372:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   5373:             $permission{'custom'} = 1;
                   5374:         }
                   5375:         $permission{'view'} = $permission{'cusr'};
                   5376:     }
                   5377:     my $allowed = 0;
                   5378:     foreach my $perm (values(%permission)) {
                   5379:         if ($perm) { $allowed=1; last; }
                   5380:     }
                   5381:     return (\%permission,$allowed);
                   5382: }
                   5383: 
                   5384: # ==================================================== Figure out author access
                   5385: 
                   5386: sub authorpriv {
                   5387:     my ($auname,$audom)=@_;
                   5388:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   5389:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   5390: }
                   5391: 
1.27      raeburn  5392: sub roles_on_upload {
1.101     raeburn  5393:     my ($context,$setting,$crstype,%customroles) = @_;
1.27      raeburn  5394:     my (@possible_roles,@permitted_roles);
1.101     raeburn  5395:     @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
1.27      raeburn  5396:     foreach my $role (@possible_roles) {
                   5397:         if ($role eq 'cr') {
                   5398:             push(@permitted_roles,keys(%customroles));
                   5399:         } else {
                   5400:             push(@permitted_roles,$role);
                   5401:         }
                   5402:     }
1.42      raeburn  5403:     return @permitted_roles;
1.27      raeburn  5404: }
                   5405: 
1.17      raeburn  5406: sub get_course_identity {
                   5407:     my ($cid) = @_;
                   5408:     my ($cnum,$cdom,$cdesc);
                   5409:     if ($cid eq '') {
                   5410:         $cid = $env{'request.course.id'}
                   5411:     }
                   5412:     if ($cid ne '') {
                   5413:         $cnum = $env{'course.'.$cid.'.num'};
                   5414:         $cdom = $env{'course.'.$cid.'.domain'};
                   5415:         $cdesc = $env{'course.'.$cid.'.description'};
                   5416:         if ($cnum eq '' || $cdom eq '') {
                   5417:             my %coursehash =
                   5418:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   5419:             $cdom = $coursehash{'domain'};
                   5420:             $cnum = $coursehash{'num'};
                   5421:             $cdesc = $coursehash{'description'};
                   5422:         }
                   5423:     }
                   5424:     return ($cnum,$cdom,$cdesc);
                   5425: }
                   5426: 
1.19      raeburn  5427: sub dc_setcourse_js {
1.37      raeburn  5428:     my ($formname,$mode,$context) = @_;
                   5429:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  5430:     my $cctext = &Apache::lonnet::plaintext('cc');
1.103     raeburn  5431:     my $cotext = &Apache::lonnet::plaintext('co');
1.19      raeburn  5432:     my %alerts = &sectioncheck_alerts();
                   5433:     my $role = 'role';
                   5434:     if ($mode eq 'upload') {
                   5435:         $role = 'courserole';
1.37      raeburn  5436:     } else {
                   5437:         $authen_check = &verify_authen($formname,$context);
1.19      raeburn  5438:     }
                   5439:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  5440: $authen_check
                   5441: 
1.19      raeburn  5442: function setCourse() {
                   5443:     var course = document.$formname.dccourse.value;
                   5444:     if (course != "") {
                   5445:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   5446:             alert("$alerts{'curd'}");
                   5447:             return;
                   5448:         }
                   5449:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   5450:         var section="";
                   5451:         var numsections = 0;
                   5452:         var newsecs = new Array();
                   5453:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   5454:             if (document.$formname.currsec.options[i].selected == true ) {
                   5455:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   5456:                     if (numsections == 0) {
                   5457:                         section = document.$formname.currsec.options[i].value
                   5458:                         numsections = 1;
                   5459:                     }
                   5460:                     else {
                   5461:                         section = section + "," +  document.$formname.currsec.options[i].value
                   5462:                         numsections ++;
                   5463:                     }
                   5464:                 }
                   5465:             }
                   5466:         }
                   5467:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   5468:             if (numsections == 0) {
                   5469:                 section = document.$formname.newsec.value
                   5470:             }
                   5471:             else {
                   5472:                 section = section + "," +  document.$formname.newsec.value
                   5473:             }
                   5474:             newsecs = document.$formname.newsec.value.split(/,/g);
                   5475:             numsections = numsections + newsecs.length;
                   5476:         }
                   5477:         if ((userrole == 'st') && (numsections > 1)) {
1.103     raeburn  5478:             if (document.$formname.crstype.value == 'Community') {
                   5479:                 alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5480:             } else {
                   5481:                 alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5482:             }
1.19      raeburn  5483:             return;
                   5484:         }
                   5485:         for (var j=0; j<newsecs.length; j++) {
                   5486:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   5487:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   5488:                 return;
                   5489:             }
                   5490:             if (document.$formname.groups.value != '') {
                   5491:                 var groups = document.$formname.groups.value.split(/,/g);
                   5492:                 for (var k=0; k<groups.length; k++) {
                   5493:                     if (newsecs[j] == groups[k]) {
1.103     raeburn  5494:                         if (document.$formname.crstype.value == 'Community') {
                   5495:                             alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5496:                         } else {
                   5497:                             alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5498:                         }
1.19      raeburn  5499:                         return;
                   5500:                     }
                   5501:                 }
                   5502:             }
                   5503:         }
                   5504:         if ((userrole == 'cc') && (numsections > 0)) {
                   5505:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5506:             section = "";
                   5507:         }
1.103     raeburn  5508:         if ((userrole == 'co') && (numsections > 0)) {
                   5509:             alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5510:             section = "";
                   5511:         }
1.19      raeburn  5512: SCRIPTTOP
                   5513:     if ($mode ne 'upload') {
                   5514:         $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5515:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   5516:         var numcourse = getIndex(document.$formname.dccourse);
                   5517:         if (numcourse == "-1") {
1.103     raeburn  5518:             if (document.$formname.type == 'Community') {
                   5519:                 alert("$alerts{'thwc'}");
                   5520:             } else {
                   5521:                 alert("$alerts{'thwa'}");
                   5522:             }
1.19      raeburn  5523:             return;
                   5524:         }
                   5525:         else {
                   5526:             document.$formname.elements[numcourse].name = "act"+coursename;
                   5527:             var numnewsec = getIndex(document.$formname.newsec);
                   5528:             if (numnewsec != "-1") {
                   5529:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   5530:                 document.$formname.elements[numnewsec].value = section;
                   5531:             }
                   5532:             var numstart = getIndex(document.$formname.start);
                   5533:             if (numstart != "-1") {
                   5534:                 document.$formname.elements[numstart].name = "start"+coursename;
                   5535:             }
                   5536:             var numend = getIndex(document.$formname.end);
                   5537:             if (numend != "-1") {
                   5538:                 document.$formname.elements[numend].name = "end"+coursename
                   5539:             }
                   5540:         }
                   5541:     }
1.37      raeburn  5542:     var authcheck = auth_check();
                   5543:     if (authcheck == 'ok') {
                   5544:         document.$formname.submit();
                   5545:     }
1.19      raeburn  5546: }
                   5547: ENDSCRIPT
                   5548:     } else {
                   5549:         $dc_setcourse_code .=  "
                   5550:         document.$formname.sections.value = section;
                   5551:     }
                   5552:     return 'ok';
                   5553: }
                   5554: ";
                   5555:     }
                   5556:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5557: 
                   5558:     function getIndex(caller) {
                   5559:         for (var i=0;i<document.$formname.elements.length;i++) {
                   5560:             if (document.$formname.elements[i] == caller) {
                   5561:                 return i;
                   5562:             }
                   5563:         }
                   5564:         return -1;
                   5565:     }
                   5566: ENDSCRIPT
1.37      raeburn  5567:     return $dc_setcourse_code;
                   5568: }
                   5569: 
                   5570: sub verify_authen {
                   5571:     my ($formname,$context) = @_;
                   5572:     my %alerts = &authcheck_alerts();
                   5573:     my $finish = "return 'ok';";
                   5574:     if ($context eq 'author') {
                   5575:         $finish = "document.$formname.submit();";
                   5576:     }
                   5577:     my $outcome = <<"ENDSCRIPT";
                   5578: 
                   5579: function auth_check() {
                   5580:     var logintype;
                   5581:     if (document.$formname.login.length) {
                   5582:         if (document.$formname.login.length > 0) {
                   5583:             var loginpicked = 0;
                   5584:             for (var i=0; i<document.$formname.login.length; i++) {
                   5585:                 if (document.$formname.login[i].checked == true) {
                   5586:                     loginpicked = 1;
                   5587:                     logintype = document.$formname.login[i].value;
                   5588:                 }
                   5589:             }
                   5590:             if (loginpicked == 0) {
                   5591:                 alert("$alerts{'authen'}");
                   5592:                 return;
                   5593:             }
                   5594:         }
                   5595:     } else {
                   5596:         logintype = document.$formname.login.value;
                   5597:     }
                   5598:     if (logintype == 'nochange') {
                   5599:         return 'ok';
                   5600:     }
                   5601:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   5602:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   5603:         var alertmsg = '';
                   5604:         switch (logintype) {
                   5605:             case 'krb':
                   5606:                 alertmsg = '$alerts{'krb'}';
                   5607:                 break;
                   5608:             case 'int':
                   5609:                 alertmsg = '$alerts{'ipass'}';
                   5610:             case 'fsys':
                   5611:                 alertmsg = '$alerts{'ipass'}';
                   5612:                 break;
                   5613:             case 'loc':
                   5614:                 alertmsg = '';
                   5615:                 break;
                   5616:             default:
                   5617:                 alertmsg = '';
                   5618:         }
                   5619:         if (alertmsg != '') {
                   5620:             alert(alertmsg);
                   5621:             return;
                   5622:         }
                   5623:     }
                   5624:     $finish
                   5625: }
                   5626: ENDSCRIPT
1.19      raeburn  5627: }
                   5628: 
                   5629: sub sectioncheck_alerts {
                   5630:     my %alerts = &Apache::lonlocal::texthash(
1.103     raeburn  5631:                     curd => 'You must select a course or community in the current domain',
1.19      raeburn  5632:                     inea => 'In each course, each user may only have one student role at a time',
1.103     raeburn  5633:                     inco => 'In each community, each user may only have one member role at a time', 
1.19      raeburn  5634:                     youh => 'You had selected',
                   5635:                     sect => 'sections',
                   5636:                     plsm => 'Please modify your selections so they include no more than one section',
                   5637:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   5638:                     plsc => 'Please choose a different section name',
                   5639:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
1.103     raeburn  5640:                     mayc => 'may not be used as the name for a section, as it is the name of a community group',
1.19      raeburn  5641:                     secn => 'Section names and group names must be distinct',
                   5642:                     secd => 'Section designations do not apply to ',
                   5643:                     role => 'roles',
                   5644:                     accr => 'role will be added with access to all sections',
1.103     raeburn  5645:                     thwa => 'There was a problem with your course selection',
                   5646:                     thwc => 'There was a problem with your community selection',
1.19      raeburn  5647:                  );
                   5648:     return %alerts;
                   5649: }
1.17      raeburn  5650: 
1.37      raeburn  5651: sub authcheck_alerts {
                   5652:     my %alerts = 
                   5653:         &Apache::lonlocal::texthash(
                   5654:                     authen => 'You must choose an authentication type.',
                   5655:                     krb    => 'You need to specify the Kerberos domain.',
                   5656:                     ipass  => 'You need to specify the initial password.',
                   5657:         );
                   5658:     return %alerts;
                   5659: }
                   5660: 
1.1       raeburn  5661: 1;
                   5662: 

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