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

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Utility functions for managing LON-CAPA user accounts
                      3: #
1.137   ! raeburn     4: # $Id: lonuserutils.pm,v 1.136 2011/08/05 14:46:58 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=();
1.137   ! raeburn  1425:     my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
        !          1426:     my %rolehash=&Apache::lonnet::dump('roles',undef,undef,'.',undef,$extra);
1.104     raeburn  1427:     foreach my $key (keys(%rolehash)) {
1.1       raeburn  1428:         if ($key=~/^rolesdef\_(\w+)$/) {
1.104     raeburn  1429:             if ($crstype eq 'Community') {
                   1430:                 next if ($rolehash{$key} =~ /bre\&S/); 
                   1431:             }
1.1       raeburn  1432:             $returnhash{$1}=$1;
                   1433:         }
                   1434:     }
                   1435:     return %returnhash;
                   1436: }
                   1437: 
1.2       raeburn  1438: sub print_userlist {
                   1439:     my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
                   1440:         $idlist,$idlist_titles) = @_;
                   1441:     my $format = $env{'form.output'};
1.1       raeburn  1442:     if (! exists($env{'form.sortby'})) {
                   1443:         $env{'form.sortby'} = 'username';
                   1444:     }
1.2       raeburn  1445:     if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
                   1446:         $env{'form.Status'} = 'Active';
1.1       raeburn  1447:     }
                   1448:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2       raeburn  1449:         ($env{'form.Status'});
1.1       raeburn  1450: 
1.2       raeburn  1451:     if ($env{'form.showrole'} eq '') {
1.13      raeburn  1452:         if ($context eq 'course') {
                   1453:             $env{'form.showrole'} = 'st';
                   1454:         } else {
                   1455:             $env{'form.showrole'} = 'Any';            
                   1456:         }
1.2       raeburn  1457:     }
1.1       raeburn  1458:     if (! defined($env{'form.output'}) ||
                   1459:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
                   1460:         $env{'form.output'} = 'html';
                   1461:     }
                   1462: 
1.2       raeburn  1463:     my @statuses;
                   1464:     if ($env{'form.Status'} eq 'Any') {
                   1465:         @statuses = ('previous','active','future');
                   1466:     } elsif ($env{'form.Status'} eq 'Expired') {
                   1467:         @statuses = ('previous');
                   1468:     } elsif ($env{'form.Status'} eq 'Active') {
                   1469:         @statuses = ('active');
                   1470:     } elsif ($env{'form.Status'} eq 'Future') {
                   1471:         @statuses = ('future');
                   1472:     }
1.1       raeburn  1473: 
1.2       raeburn  1474: #    if ($context eq 'course') { 
                   1475: #        $r->print(&display_adv_courseroles());
                   1476: #    }
1.1       raeburn  1477:     #
                   1478:     # Interface output
1.2       raeburn  1479:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   1480:               '<input type="hidden" name="action" value="'.
1.1       raeburn  1481:               $env{'form.action'}.'" />');
                   1482:     $r->print("<p>\n");
                   1483:     if ($env{'form.action'} ne 'modifystudent') {
                   1484:         my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
                   1485:                                            'excel' => "Excel",
                   1486:                                            'html'  => 'HTML');
                   1487:         my $output_selector = '<select size="1" name="output" >';
                   1488:         foreach my $outputformat ('html','csv','excel') {
1.96      bisitz   1489:             my $option = '<option value="'.$outputformat.'"';
1.1       raeburn  1490:             if ($outputformat eq $env{'form.output'}) {
1.96      bisitz   1491:                 $option .= ' selected="selected"';
1.1       raeburn  1492:             }
                   1493:             $option .='>'.$lt{$outputformat}.'</option>';
                   1494:             $output_selector .= "\n".$option;
                   1495:         }
                   1496:         $output_selector .= '</select>';
1.70      bisitz   1497:         $r->print('<label><span class="LC_nobreak">'
                   1498:                  .&mt('Output Format: [_1]',$output_selector)
                   1499:                  .'</span></label>'.('&nbsp;'x3));
                   1500:     }
                   1501:     $r->print('<label><span class="LC_nobreak">'
                   1502:              .&mt('User Status: [_1]',$status_select)
                   1503:              .'</span></label>'.('&nbsp;'x3)."\n");
1.2       raeburn  1504:     my $roleselected = '';
                   1505:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1506:        $roleselected = ' selected="selected"'; 
1.2       raeburn  1507:     }
1.53      raeburn  1508:     my ($cnum,$cdom);
                   1509:     $r->print(&role_filter($context));
                   1510:     if ($context eq 'course') {
                   1511:         ($cnum,$cdom) = &get_course_identity();
                   1512:         $r->print(&section_group_filter($cnum,$cdom));
1.2       raeburn  1513:     }
1.78      raeburn  1514:     if ($env{'form.phase'} eq '') {
                   1515:         $r->print('<br /><br />'.&list_submit_button(&mt('Display List of Users')).
                   1516:                   "\n</p>\n".
                   1517:                   '<input type="hidden" name="phase" value="" /></form>');
                   1518:         return;
                   1519:     }
1.106     raeburn  1520:     if (!(($context eq 'domain') && 
                   1521:           (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
1.111     bisitz   1522:         $r->print(
                   1523:             "\n</p>\n"
                   1524:            .'<p>'
                   1525:            .&list_submit_button(&mt('Update Display'))
                   1526:            ."</p>\n"
                   1527:         );
1.2       raeburn  1528:     }
                   1529:     my ($indexhash,$keylist) = &make_keylist_array();
1.106     raeburn  1530:     my (%userlist,%userinfo,$clearcoursepick);
1.102     raeburn  1531:     if (($context eq 'domain') && 
                   1532:         ($env{'form.roletype'} eq 'course') || 
                   1533:         ($env{'form.roletype'} eq 'community')) {
                   1534:         my ($crstype,$numcodes,$title,$warning);
                   1535:         if ($env{'form.roletype'} eq 'course') {
                   1536:             $crstype = 'Course';
                   1537:             $numcodes = $totcodes;
                   1538:             $title = &mt('Select Courses');
                   1539:             $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
                   1540:         } elsif ($env{'form.roletype'} eq 'community') {
                   1541:             $crstype = 'Community';
                   1542:             $numcodes = 0;
                   1543:             $title = &mt('Select Communities');
                   1544:             $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
                   1545:         }
1.120     raeburn  1546:         my @standardnames = &Apache::loncommon::get_standard_codeitems();
1.3       raeburn  1547:         my $courseform =
1.102     raeburn  1548:             &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
1.120     raeburn  1549:                             $codetitles,$idlist,$idlist_titles,$crstype,
                   1550:                             \@standardnames);
1.3       raeburn  1551:         $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
                   1552:                   &Apache::lonhtmlcommon::start_pick_box()."\n".
1.102     raeburn  1553:                   &Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
1.3       raeburn  1554:                   $courseform."\n".
                   1555:                   &Apache::lonhtmlcommon::row_closure(1).
                   1556:                   &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1.106     raeburn  1557:                   '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
                   1558:                   &list_submit_button(&mt('Update Display')).
1.102     raeburn  1559:                   "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
1.106     raeburn  1560:         $clearcoursepick = 0;
                   1561:         if (($env{'form.origroletype'} ne '') &&
                   1562:             ($env{'form.origroletype'} ne $env{'form.roletype'})) {
                   1563:             $clearcoursepick = 1;
                   1564:         }
                   1565:         if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.11      raeburn  1566:             $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
                   1567:         }
                   1568:     } else {
                   1569:         $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
1.3       raeburn  1570:     }
                   1571:     $r->rflush();
1.1       raeburn  1572:     if ($context eq 'course') {
1.46      raeburn  1573:         if (($env{'form.showrole'} eq 'st') || ($env{'form.showrole'} eq 'Any')) { 
1.45      raeburn  1574:             my $classlist = &Apache::loncoursedata::get_classlist();
1.66      raeburn  1575:             if (ref($classlist) eq 'HASH') {
                   1576:                 %userlist = %{$classlist};
                   1577:             }
1.45      raeburn  1578:         }
1.43      raeburn  1579:         if ($env{'form.showrole'} ne 'st') {
                   1580:             my $showroles;
                   1581:             if ($env{'form.showrole'} ne 'Any') {
                   1582:                 $showroles = [$env{'form.showrole'}];
1.3       raeburn  1583:             } else {
1.43      raeburn  1584:                 $showroles = undef;
1.1       raeburn  1585:             }
1.43      raeburn  1586:             my $withsec = 1;
                   1587:             my $hidepriv = 1;
                   1588:             my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   1589:                               \@statuses,$showroles,undef,$withsec,$hidepriv);
                   1590:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                   1591:                              \%advrolehash,$permission);
1.1       raeburn  1592:         }
1.2       raeburn  1593:     } else {
                   1594:         my (%cstr_roles,%dom_roles);
1.13      raeburn  1595:         if ($context eq 'author') {
1.2       raeburn  1596:             # List co-authors and assistant co-authors
1.17      raeburn  1597:             my @possroles = &roles_by_context($context);
1.2       raeburn  1598:             %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   1599:                                               \@statuses,\@possroles);
                   1600:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11      raeburn  1601:                              \%cstr_roles,$permission);
1.2       raeburn  1602:         } elsif ($context eq 'domain') {
                   1603:             if ($env{'form.roletype'} eq 'domain') {
                   1604:                 %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
                   1605:                 foreach my $key (keys(%dom_roles)) {
                   1606:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1607:                         &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1608:                                          \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1609:                     }
                   1610:                 }
1.13      raeburn  1611:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1612:                 my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
                   1613:                 my %coauthors;
                   1614:                 foreach my $key (keys(%dom_roles)) {
                   1615:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1616:                         if ($env{'form.showrole'} eq 'au') {
                   1617:                             &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1618:                                              \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1619:                         } else {
                   1620:                             my @possroles;
                   1621:                             if ($env{'form.showrole'} eq 'Any') {
1.22      raeburn  1622:                                 @possroles = &roles_by_context('author');
1.2       raeburn  1623:                             } else {
                   1624:                                 @possroles = ($env{'form.showrole'}); 
                   1625:                             }
                   1626:                             foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22      raeburn  1627:                                 my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2       raeburn  1628:                                 my $extent = '/'.$authordom.'/'.$authorname;
                   1629:                                 %{$coauthors{$extent}} =
                   1630:                                     &Apache::lonnet::get_my_roles($authorname,
                   1631:                                        $authordom,undef,\@statuses,\@possroles);
                   1632:                             }
                   1633:                             &gather_userinfo($context,$format,\%userlist,
1.11      raeburn  1634:                                      $indexhash,\%userinfo,\%coauthors,$permission);
1.2       raeburn  1635:                         }
                   1636:                     }
                   1637:                 }
1.101     raeburn  1638:             } elsif (($env{'form.roletype'} eq 'course') ||
                   1639:                      ($env{'form.roletype'} eq 'community')) {
1.106     raeburn  1640:                 if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.2       raeburn  1641:                     my %courses = &process_coursepick();
1.39      raeburn  1642:                     my %allusers;
                   1643:                     my $hidepriv = 1;
1.2       raeburn  1644:                     foreach my $cid (keys(%courses)) {
1.17      raeburn  1645:                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11      raeburn  1646:                         next if ($cnum eq '' || $cdom eq '');
1.17      raeburn  1647:                         my $custom = 1;
1.2       raeburn  1648:                         my (@roles,@sections,%access,%users,%userdata,
1.6       albertel 1649:                             %statushash);
1.2       raeburn  1650:                         if ($env{'form.showrole'} eq 'Any') {
1.101     raeburn  1651:                             @roles = &course_roles($context,undef,$custom,
                   1652:                                                    $env{'form.roletype'});
1.2       raeburn  1653:                         } else {
                   1654:                             @roles = ($env{'form.showrole'});
                   1655:                         }
                   1656:                         foreach my $role (@roles) {
                   1657:                             %{$users{$role}} = ();
                   1658:                         }
                   1659:                         foreach my $type (@statuses) {
                   1660:                             $access{$type} = $type;
                   1661:                         }
1.39      raeburn  1662:                         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
1.2       raeburn  1663:                         foreach my $user (keys(%userdata)) {
                   1664:                             next if (ref($userinfo{$user}) eq 'HASH');
                   1665:                             foreach my $item ('fullname','id') {
                   1666:                                 $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
                   1667:                             }
                   1668:                         }
                   1669:                         foreach my $role (keys(%users)) {
                   1670:                             foreach my $user (keys(%{$users{$role}})) {
                   1671:                                 my $uniqid = $user.':'.$role;
                   1672:                                 $allusers{$uniqid}{$cid} = { desc => $cdesc,
                   1673:                                                              secs  => $statushash{$user}{$role},
                   1674:                                                            };
                   1675:                             }
                   1676:                         }
                   1677:                     }
                   1678:                     &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1679:                                      \%userinfo,\%allusers,$permission);
1.2       raeburn  1680:                 } else {
1.10      raeburn  1681:                     $r->print('<input type="hidden" name="phase" value="'.
                   1682:                               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1683:                     return;
                   1684:                 }
1.1       raeburn  1685:             }
                   1686:         }
1.3       raeburn  1687:     }
                   1688:     if (keys(%userlist) == 0) {
1.13      raeburn  1689:         if ($context eq 'author') {
1.3       raeburn  1690:             $r->print(&mt('There are no co-authors to display.')."\n");
                   1691:         } elsif ($context eq 'domain') {
                   1692:             if ($env{'form.roletype'} eq 'domain') {
                   1693:                 $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13      raeburn  1694:             } elsif ($env{'form.roletype'} eq 'author') {
1.3       raeburn  1695:                 $r->print(&mt('There are no authors or co-authors to display.')."\n");
                   1696:             } elsif ($env{'form.roletype'} eq 'course') {
                   1697:                 $r->print(&mt('There are no course users to display')."\n"); 
1.101     raeburn  1698:             } elsif ($env{'form.roletype'} eq 'community') {
                   1699:                 $r->print(&mt('There are no community users to display')."\n");
1.2       raeburn  1700:             }
1.3       raeburn  1701:         } elsif ($context eq 'course') {
                   1702:             $r->print(&mt('There are no course users to display.')."\n");
                   1703:         }
                   1704:     } else {
                   1705:         # Print out the available choices
1.4       raeburn  1706:         my $usercount;
1.3       raeburn  1707:         if ($env{'form.action'} eq 'modifystudent') {
1.10      raeburn  1708:             ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4       raeburn  1709:                                  $env{'form.Status'},\%userlist,$keylist);
1.1       raeburn  1710:         } else {
1.4       raeburn  1711:             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10      raeburn  1712:                                $permission,$env{'form.Status'},\%userlist,$keylist);
1.4       raeburn  1713:         }
                   1714:         if (!$usercount) {
1.72      bisitz   1715:             $r->print('<br /><span class="LC_warning">'
                   1716:                      .&mt('There are no users matching the search criteria.')
                   1717:                      .'</span>'
                   1718:             ); 
1.2       raeburn  1719:         }
                   1720:     }
1.10      raeburn  1721:     $r->print('<input type="hidden" name="phase" value="'.
                   1722:               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1723: }
                   1724: 
1.53      raeburn  1725: sub role_filter {
                   1726:     my ($context) = @_;
                   1727:     my $output;
                   1728:     my $roleselected = '';
                   1729:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1730:        $roleselected = ' selected="selected"';
1.53      raeburn  1731:     }
                   1732:     my ($role_select);
                   1733:     if ($context eq 'domain') {
                   1734:         $role_select = &domain_roles_select();
1.70      bisitz   1735:         $output = '<label><span class="LC_nobreak">'
                   1736:                  .&mt('Role Type: [_1]',$role_select)
                   1737:                  .'</span></label>';
1.53      raeburn  1738:     } else {
                   1739:         $role_select = '<select name="showrole">'."\n".
                   1740:                        '<option value="Any" '.$roleselected.'>'.
                   1741:                        &mt('Any role').'</option>';
1.101     raeburn  1742:         my ($roletype,$crstype);
                   1743:         if ($context eq 'course') {
                   1744:             $crstype = &Apache::loncommon::course_type();
                   1745:             if ($crstype eq 'Community') {
                   1746:                 $roletype = 'community';
                   1747:             } else {
                   1748:                 $roletype = 'course';
                   1749:             } 
                   1750:         }
                   1751:         my @poss_roles = &curr_role_permissions($context,'','',$roletype);
1.53      raeburn  1752:         foreach my $role (@poss_roles) {
                   1753:             $roleselected = '';
                   1754:             if ($role eq $env{'form.showrole'}) {
1.91      bisitz   1755:                 $roleselected = ' selected="selected"';
1.53      raeburn  1756:             }
                   1757:             my $plrole;
                   1758:             if ($role eq 'cr') {
                   1759:                 $plrole = &mt('Custom role');
                   1760:             } else {
1.101     raeburn  1761:                 $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.53      raeburn  1762:             }
                   1763:             $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
                   1764:         }
                   1765:         $role_select .= '</select>';
1.70      bisitz   1766:         $output = '<label><span class="LC_nobreak">'
                   1767:                  .&mt('Role: [_1]',$role_select)
1.111     bisitz   1768:                  .'</span></label> ';
1.53      raeburn  1769:     }
                   1770:     return $output;
                   1771: }
                   1772: 
1.33      raeburn  1773: sub section_group_filter {
                   1774:     my ($cnum,$cdom) = @_;
                   1775:     my @filters;
                   1776:     if ($env{'request.course.sec'} eq '') {
                   1777:         @filters = ('sec');
                   1778:     }
                   1779:     push(@filters,'grp');
                   1780:     my %name = (
                   1781:                  sec => 'secfilter',
                   1782:                  grp => 'grpfilter',
                   1783:                );
                   1784:     my %title = &Apache::lonlocal::texthash (
                   1785:                                               sec  => 'Section(s)',
                   1786:                                               grp  => 'Group(s)',
                   1787:                                               all  => 'all',
                   1788:                                               none => 'none',
                   1789:                                             );
1.47      raeburn  1790:     my $output;
1.33      raeburn  1791:     foreach my $item (@filters) {
1.47      raeburn  1792:         my ($markup,@options); 
1.33      raeburn  1793:         if ($env{'form.'.$name{$item}} eq '') {
                   1794:             $env{'form.'.$name{$item}} = 'all';
                   1795:         }
                   1796:         if ($item eq 'sec') {
1.103     raeburn  1797:             if (($env{'form.showrole'} eq 'cc') || ($env{'form.showrole'} eq 'co')) {
1.33      raeburn  1798:                 $env{'form.'.$name{$item}} = 'none';
                   1799:             }
                   1800:             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   1801:             @options = sort(keys(%sections_count));
                   1802:         } elsif ($item eq 'grp') {
                   1803:             my %curr_groups = &Apache::longroup::coursegroups();
                   1804:             @options = sort(keys(%curr_groups));
                   1805:         }
                   1806:         if (@options > 0) {
                   1807:             my $currsel;
1.112     bisitz   1808:             $markup = '<select name="'.$name{$item}.'">'."\n";
1.33      raeburn  1809:             foreach my $option ('all','none',@options) { 
                   1810:                 $currsel = '';
                   1811:                 if ($env{'form.'.$name{$item}} eq $option) {
1.96      bisitz   1812:                     $currsel = ' selected="selected"';
1.33      raeburn  1813:                 }
                   1814:                 $markup .= ' <option value="'.$option.'"'.$currsel.'>';
                   1815:                 if (($option eq 'all') || ($option eq 'none')) {
                   1816:                     $markup .= $title{$option};
                   1817:                 } else {
                   1818:                     $markup .= $option;
                   1819:                 }   
                   1820:                 $markup .= '</option>'."\n";
                   1821:             }
                   1822:             $markup .= '</select>'."\n";
1.111     bisitz   1823:             $output .= ('&nbsp;'x3).'<span class="LC_nobreak">'
                   1824:                       .'<label>'.$title{$item}.': '.$markup.'</label>'
                   1825:                       .'</span> ';
1.33      raeburn  1826:         }
                   1827:     }
                   1828:     return $output;
                   1829: }
                   1830: 
1.2       raeburn  1831: sub list_submit_button {
                   1832:     my ($text) = @_;
1.11      raeburn  1833:     return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2       raeburn  1834: }
                   1835: 
                   1836: sub gather_userinfo {
1.11      raeburn  1837:     my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.52      raeburn  1838:     my $viewablesec;
                   1839:     if ($context eq 'course') {
                   1840:         $viewablesec = &viewable_section($permission);
                   1841:     }
1.2       raeburn  1842:     foreach my $item (keys(%{$rolehash})) {
                   1843:         my %userdata;
1.22      raeburn  1844:         if ($context eq 'author') { 
1.2       raeburn  1845:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1846:                 split(/:/,$item);
                   1847:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1848:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1849:                                $item,$userlist);
1.22      raeburn  1850:         } elsif ($context eq 'course') {
                   1851:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   1852:              $userdata{'section'}) = split(/:/,$item,-1);
                   1853:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   1854:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   1855:                 next if ($viewablesec ne $userdata{'section'});
                   1856:             }
1.24      raeburn  1857:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1858:                                $item,$userlist);
1.2       raeburn  1859:         } elsif ($context eq 'domain') {
                   1860:             if ($env{'form.roletype'} eq 'domain') {
                   1861:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   1862:                     split(/:/,$item);
                   1863:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1864:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1865:                                    $item,$userlist);
1.13      raeburn  1866:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1867:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   1868:                     $userdata{'extent'} = $item;
                   1869:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   1870:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   1871:                         ($userdata{'start'},$userdata{'end'}) = 
                   1872:                             split(/:/,$rolehash->{$item}{$key});
                   1873:                         my $uniqid = $key.':'.$item;
1.25      raeburn  1874:                         &build_user_record($context,\%userdata,$userinfo,
                   1875:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  1876:                     }
                   1877:                 }
1.102     raeburn  1878:             } elsif (($env{'form.roletype'} eq 'course') || 
                   1879:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  1880:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1881:                     split(/:/,$item);
                   1882:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  1883:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  1884:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   1885:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   1886:                             my $spanstart = '';
                   1887:                             my $spanend = '; ';
                   1888:                             my $space = ', ';
                   1889:                             if ($format eq 'html' || $format eq 'view') {
                   1890:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  1891:                                 # FIXME: actions on courses disabled for now
                   1892: #                                if ($permission->{'cusr'}) {
                   1893: #                                    if ($numcids > 1) {
1.25      raeburn  1894: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1895: #                                    } else {
1.25      raeburn  1896: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1897: #                                    }
                   1898: #                                }
1.2       raeburn  1899:                                 $spanend = '</span><br />';
                   1900:                                 $space = ',&nbsp;';
                   1901:                             }
                   1902:                             $userdata{'extent'} .= $spanstart.
                   1903:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   1904:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   1905:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  1906:                                     if (($env{'form.Status'} eq 'Any') ||
                   1907:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   1908:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   1909:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   1910:                                     }
1.2       raeburn  1911:                                 }
                   1912:                             }
                   1913:                         }
                   1914:                     }
                   1915:                 }
1.25      raeburn  1916:                 if ($userdata{'status'} ne '') {
                   1917:                     &build_user_record($context,\%userdata,$userinfo,
                   1918:                                        $indexhash,$item,$userlist);
                   1919:                 }
1.2       raeburn  1920:             }
                   1921:         }
                   1922:     }
                   1923:     return;
                   1924: }
                   1925: 
                   1926: sub build_user_record {
1.24      raeburn  1927:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  1928:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.102     raeburn  1929:     if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
                   1930:                              && ($env{'form.roletype'} eq 'community')))) {
1.24      raeburn  1931:         &process_date_info($userdata);
                   1932:     }
1.2       raeburn  1933:     my $username = $userdata->{'username'};
                   1934:     my $domain = $userdata->{'domain'};
                   1935:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  1936:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  1937:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1938:     } else {
                   1939:         &aggregate_user_info($domain,$username,$userinfo);
                   1940:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   1941:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1942:     }
                   1943:     foreach my $key (keys(%{$indexhash})) {
                   1944:         if (defined($userdata->{$key})) {
                   1945:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   1946:         }
                   1947:     }
                   1948:     return;
                   1949: }
                   1950: 
                   1951: sub courses_selector {
                   1952:     my ($cdom,$formname) = @_;
                   1953:     my %coursecodes = ();
                   1954:     my %codes = ();
                   1955:     my @codetitles = ();
                   1956:     my %cat_titles = ();
                   1957:     my %cat_order = ();
                   1958:     my %idlist = ();
                   1959:     my %idnums = ();
                   1960:     my %idlist_titles = ();
                   1961:     my $caller = 'global';
                   1962:     my $format_reply;
                   1963:     my $jscript = '';
                   1964: 
1.7       albertel 1965:     my $totcodes = 0;
                   1966:     $totcodes =
1.2       raeburn  1967:         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                   1968:                                                       $cdom,$totcodes);
                   1969:     if ($totcodes > 0) {
                   1970:         $format_reply =
                   1971:              &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
                   1972:                                 \%codes,\@codetitles,\%cat_titles,\%cat_order);
                   1973:         if ($format_reply eq 'ok') {
                   1974:             my $numtypes = @codetitles;
                   1975:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   1976:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   1977:             my $longtitles_str = join('","',@{$longtitles});
                   1978:             my $allidlist = $idlist{$codetitles[0]};
                   1979:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   1980:             $jscript .= $scripttext;
                   1981:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
                   1982:         }
                   1983:     }
                   1984:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   1985: 
                   1986:     my %elements = (
                   1987:                      Year => 'selectbox',
                   1988:                      coursepick => 'radio',
                   1989:                      coursetotal => 'text',
                   1990:                      courselist => 'text',
                   1991:                    );
                   1992:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   1993:     if ($env{'form.coursepick'} eq 'category') {
                   1994:         $jscript .= qq|
                   1995: function setCourseCat(formname) {
                   1996:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   1997:         return;
                   1998:     }
1.120     raeburn  1999:     courseSet('$codetitles[0]');
1.2       raeburn  2000:     for (var j=0; j<formname.Semester.length; j++) {
                   2001:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   2002:             formname.Semester.options[j].selected = true;
                   2003:         }
                   2004:     }
                   2005:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   2006:         return;
                   2007:     }
1.120     raeburn  2008:     courseSet('$codetitles[1]');
1.2       raeburn  2009:     for (var j=0; j<formname.Department.length; j++) {
                   2010:         if (formname.Department.options[j].value == "$env{'form.Department'}") {            formname.Department.options[j].selected = true;
                   2011:         }
                   2012:     }
                   2013:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   2014:         return;
                   2015:     }
1.120     raeburn  2016:     courseSet('$codetitles[2]');
1.2       raeburn  2017:     for (var j=0; j<formname.Number.length; j++) {
                   2018:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   2019:             formname.Number.options[j].selected = true;
                   2020:         }
                   2021:     }
                   2022: }
                   2023: |;
                   2024:     }
                   2025:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   2026:             \%idlist_titles);
                   2027: }
                   2028: 
                   2029: sub course_selector_loadcode {
                   2030:     my ($formname) = @_;
                   2031:     my $loadcode;
                   2032:     if ($env{'form.coursepick'} ne '') {
                   2033:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   2034:         if ($env{'form.coursepick'} eq 'category') {
                   2035:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   2036:         }
                   2037:     }
                   2038:     return $loadcode;
                   2039: }
                   2040: 
                   2041: sub process_coursepick {
                   2042:     my $coursefilter = $env{'form.coursepick'};
                   2043:     my $cdom = $env{'request.role.domain'};
                   2044:     my %courses;
1.105     raeburn  2045:     my $crssrch = 'Course';
                   2046:     if ($env{'form.roletype'} eq 'community') {
                   2047:         $crssrch = 'Community';
                   2048:     }
1.2       raeburn  2049:     if ($coursefilter eq 'all') {
                   2050:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1.105     raeburn  2051:                                                  undef,undef,$crssrch);
1.2       raeburn  2052:     } elsif ($coursefilter eq 'category') {
                   2053:         my $instcode = &instcode_from_coursefilter();
                   2054:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1.105     raeburn  2055:                                                  undef,undef,$crssrch);
1.2       raeburn  2056:     } elsif ($coursefilter eq 'specific') {
                   2057:         if ($env{'form.coursetotal'} > 1) {
                   2058:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   2059:             foreach my $cid (@course_ids) {
                   2060:                 $courses{$cid} = '';
1.1       raeburn  2061:             }
1.2       raeburn  2062:         } else {
                   2063:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  2064:         }
1.2       raeburn  2065:     }
                   2066:     return %courses;
                   2067: }
                   2068: 
                   2069: sub instcode_from_coursefilter {
                   2070:     my $instcode = '';
                   2071:     my @cats = ('Semester','Year','Department','Number');
                   2072:     foreach my $category (@cats) {
                   2073:         if (defined($env{'form.'.$category})) {
                   2074:             unless ($env{'form.'.$category} eq '-1') {
                   2075:                 $instcode .= $env{'form.'.$category};
                   2076:            }
                   2077:         }
                   2078:     }
                   2079:     if ($instcode eq '') {
                   2080:         $instcode = '.';
                   2081:     }
                   2082:     return $instcode;
                   2083: }
                   2084: 
                   2085: sub display_adv_courseroles {
                   2086:     my $output;
                   2087:     #
                   2088:     # List course personnel
                   2089:     my %coursepersonnel = 
                   2090:        &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
                   2091:     #
                   2092:     $output = '<br />'.&Apache::loncommon::start_data_table();
                   2093:     foreach my $role (sort(keys(%coursepersonnel))) {
                   2094:         next if ($role =~ /^\s*$/);
                   2095:         $output .= &Apache::loncommon::start_data_table_row().
                   2096:                   '<td>'.$role.'</td><td>';
                   2097:         foreach my $user (split(',',$coursepersonnel{$role})) {
                   2098:             my ($puname,$pudom)=split(':',$user);
                   2099:             $output .= ' '.&Apache::loncommon::aboutmewrapper(
                   2100:                        &Apache::loncommon::plainname($puname,$pudom),
                   2101:                        $puname,$pudom);
                   2102:         }
                   2103:         $output .= '</td>'.&Apache::loncommon::end_data_table_row();
                   2104:     }
                   2105:     $output .= &Apache::loncommon::end_data_table();
                   2106: }
                   2107: 
                   2108: sub make_keylist_array {
                   2109:     my ($index,$keylist);
                   2110:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   2111:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   2112:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   2113:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   2114:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   2115:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   2116:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   2117:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2118:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2119:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   2120:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   2121:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   2122:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   2123:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1.44      raeburn  2124:     $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
1.47      raeburn  2125:     $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
1.2       raeburn  2126:     foreach my $key (keys(%{$index})) {
                   2127:         $keylist->[$index->{$key}] = $key;
                   2128:     }
                   2129:     return ($index,$keylist);
                   2130: }
                   2131: 
                   2132: sub aggregate_user_info {
                   2133:     my ($udom,$uname,$userinfo) = @_;
                   2134:     my %info=&Apache::lonnet::get('environment',
                   2135:                                   ['firstname','middlename',
                   2136:                                    'lastname','generation','id'],
                   2137:                                    $udom,$uname);
                   2138:     my ($tmp) = keys(%info);
                   2139:     my ($fullname,$id);
                   2140:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   2141:         $fullname = 'not available';
                   2142:         $id = 'not available';
                   2143:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   2144:                                  'for '.$uname.':'.$udom);
1.1       raeburn  2145:     } else {
1.2       raeburn  2146:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   2147:         $id = $info{'id'};
                   2148:     }
                   2149:     $userinfo->{$uname.':'.$udom} = { 
                   2150:                                       fullname => $fullname,
                   2151:                                       id       => $id,
                   2152:                                     };
                   2153:     return;
                   2154: }
1.1       raeburn  2155: 
1.2       raeburn  2156: sub process_date_info {
                   2157:     my ($userdata) = @_;
                   2158:     my $now = time;
1.83      raeburn  2159:     $userdata->{'status'} = 'Active';
1.2       raeburn  2160:     if ($userdata->{'start'} > 0) {
                   2161:         if ($now < $userdata->{'start'}) {
1.83      raeburn  2162:             $userdata->{'status'} = 'Future';
1.2       raeburn  2163:         }
1.1       raeburn  2164:     }
1.2       raeburn  2165:     if ($userdata->{'end'} > 0) {
                   2166:         if ($now > $userdata->{'end'}) {
1.83      raeburn  2167:             $userdata->{'status'} = 'Expired';
1.2       raeburn  2168:         }
                   2169:     }
                   2170:     return;
1.1       raeburn  2171: }
                   2172: 
                   2173: sub show_users_list {
1.55      raeburn  2174:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname)=@_;
                   2175:     if ($formname eq '') {
                   2176:         $formname = 'studentform';
                   2177:     }
1.1       raeburn  2178:     #
                   2179:     # Variables for excel output
                   2180:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   2181:     #
                   2182:     # Variables for csv output
                   2183:     my ($CSVfile,$CSVfilename);
                   2184:     #
                   2185:     my $sortby = $env{'form.sortby'};
1.3       raeburn  2186:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  2187:     if ($context eq 'course') {
1.3       raeburn  2188:         push(@sortable,('section','groups','type'));
1.2       raeburn  2189:     } else {
1.3       raeburn  2190:         push(@sortable,'extent');
                   2191:     }
1.55      raeburn  2192:     if ($mode eq 'pickauthor') {
                   2193:         @sortable = ('username','fullname','email','status');
                   2194:     }
1.3       raeburn  2195:     if (!grep(/^\Q$sortby\E$/,@sortable)) {
                   2196:         $sortby = 'username';
1.1       raeburn  2197:     }
1.22      raeburn  2198:     my $setting = $env{'form.roletype'};
1.101     raeburn  2199:     my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers,$crstype);
1.1       raeburn  2200:     if ($context eq 'course') {
1.22      raeburn  2201:         $cid = $env{'request.course.id'};
1.101     raeburn  2202:         $crstype = &Apache::loncommon::course_type();
1.17      raeburn  2203:         ($cnum,$cdom) = &get_course_identity($cid);
1.2       raeburn  2204:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   2205:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  2206:         if ($mode eq 'autoenroll') {
                   2207:             $env{'form.showrole'} = 'st';
                   2208:         } else {
                   2209:             if (! exists($env{'form.displayphotos'})) {
                   2210:                 $env{'form.displayphotos'} = 'off';
                   2211:             }
                   2212:             $displayphotos = $env{'form.displayphotos'};
                   2213:             if (! exists($env{'form.displayclickers'})) {
                   2214:                 $env{'form.displayclickers'} = 'off';
                   2215:             }
                   2216:             $displayclickers = $env{'form.displayclickers'};
                   2217:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   2218:                 $r->print('
1.1       raeburn  2219: <script type="text/javascript">
1.96      bisitz   2220: // <![CDATA[
1.1       raeburn  2221: function photowindow(photolink) {
                   2222:     var title = "Photo_Viewer";
                   2223:     var options = "scrollbars=1,resizable=1,menubar=0";
                   2224:     options += ",width=240,height=240";
                   2225:     stdeditbrowser = open(photolink,title,options,"1");
                   2226:     stdeditbrowser.focus();
                   2227: }
1.96      bisitz   2228: // ]]>
1.1       raeburn  2229: </script>
1.16      raeburn  2230:                ');
                   2231:             }
                   2232:             $r->print(<<END);
1.1       raeburn  2233: <input type="hidden" name="displayphotos" value="$displayphotos" />
                   2234: <input type="hidden" name="displayclickers" value="$displayclickers" />
                   2235: END
1.16      raeburn  2236:         }
1.102     raeburn  2237:     } elsif ($context eq 'domain') {
                   2238:         if ($setting eq 'community') {
                   2239:             $crstype = 'Community';
1.105     raeburn  2240:         } elsif ($setting eq 'course') {
1.102     raeburn  2241:             $crstype = 'Course';
                   2242:         }
1.1       raeburn  2243:     }
1.55      raeburn  2244:     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.11      raeburn  2245:         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.40      raeburn  2246:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.56      raeburn  2247:         my $verify_action_js = &bulkaction_javascript($formname);
1.1       raeburn  2248:         $r->print(<<END);
1.10      raeburn  2249: 
                   2250: <script type="text/javascript" language="Javascript">
1.96      bisitz   2251: // <![CDATA[
1.11      raeburn  2252: $check_uncheck_js
                   2253: 
1.56      raeburn  2254: $verify_action_js
1.10      raeburn  2255: 
                   2256: function username_display_launch(username,domain) {
                   2257:     var target;
1.55      raeburn  2258:     for (var i=0; i<document.$formname.usernamelink.length; i++) {
                   2259:         if (document.$formname.usernamelink[i].checked) {
                   2260:             target = document.$formname.usernamelink[i].value;
1.10      raeburn  2261:         }
                   2262:     }
                   2263:     if (target == 'modify') {
1.55      raeburn  2264:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2265:             var url = '/adm/createuser?srchterm='+username+'&srchdomain='+domain+'&phase=get_user_info&action=singleuser&srchin=dom&srchby=uname&srchtype=exact&popup=1';
                   2266:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2267:             modifywin = window.open(url,'',options,1);
                   2268:             modifywin.focus();
                   2269:             return;
                   2270:         } else {
1.55      raeburn  2271:             document.$formname.srchterm.value=username;
                   2272:             document.$formname.srchdomain.value=domain;
                   2273:             document.$formname.phase.value='get_user_info';
                   2274:             document.$formname.action.value = 'singleuser';
                   2275:             document.$formname.submit();
1.50      raeburn  2276:         }
1.10      raeburn  2277:     }
1.48      raeburn  2278:     if (target == 'aboutme') {
1.55      raeburn  2279:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2280:             var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
                   2281:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2282:             aboutmewin = window.open(url,'',options,1);
                   2283:             aboutmewin.focus();
                   2284:             return;
                   2285:         } else {
                   2286:             document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   2287:         }
1.48      raeburn  2288:     }
1.98      raeburn  2289:     if (target == 'track') {
                   2290:         if (document.$formname.userwin.checked == true) {
                   2291:             var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
                   2292:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2293:             var trackwin = window.open(url,'',options,1);
                   2294:             trackwin.focus();
                   2295:             return;
                   2296:         } else {
                   2297:             document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
                   2298:         }
                   2299:     }
1.10      raeburn  2300: }
1.96      bisitz   2301: // ]]>
1.10      raeburn  2302: </script>
1.11      raeburn  2303: $date_sec_selector
1.1       raeburn  2304: <input type="hidden" name="state" value="$env{'form.state'}" />
                   2305: END
                   2306:     }
                   2307:     $r->print(<<END);
                   2308: <input type="hidden" name="sortby" value="$sortby" />
                   2309: END
                   2310: 
                   2311:     my %lt=&Apache::lonlocal::texthash(
                   2312:                        'username'   => "username",
                   2313:                        'domain'     => "domain",
                   2314:                        'id'         => 'ID',
                   2315:                        'fullname'   => "name",
                   2316:                        'section'    => "section",
                   2317:                        'groups'     => "active groups",
                   2318:                        'start'      => "start date",
                   2319:                        'end'        => "end date",
                   2320:                        'status'     => "status",
1.2       raeburn  2321:                        'role'       => "role",
1.1       raeburn  2322:                        'type'       => "enroll type/action",
1.79      schafran 2323:                        'email'      => "e-mail address",
1.1       raeburn  2324:                        'photo'      => "photo",
1.2       raeburn  2325:                        'extent'     => "extent",
1.11      raeburn  2326:                        'pr'         => "Proceed",
                   2327:                        'ca'         => "check all",
                   2328:                        'ua'         => "uncheck all",
                   2329:                        'ac'         => "Action to take for selected users",
1.56      raeburn  2330:                        'link'       => "Behavior of clickable username link for each user",
1.82      weissno  2331:                        'aboutme'    => "Display a user's personal information page",
1.50      raeburn  2332:                        'owin'       => "Open in a new window",
1.10      raeburn  2333:                        'modify'     => "Modify a user's information",
1.98      raeburn  2334:                        'track'      => "View a user's recent activity",
1.67      droeschl 2335:                        'clicker'    => "Clicker-ID",
1.1       raeburn  2336:                       );
1.2       raeburn  2337:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   2338:         $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.102     raeburn  2339:     } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
                   2340:         $lt{'extent'} = &mt('Communities: description, section(s), status');
1.13      raeburn  2341:     } elsif ($context eq 'author') {
1.2       raeburn  2342:         $lt{'extent'} = &mt('Author'); 
                   2343:     }
1.55      raeburn  2344:     my @cols;
                   2345:     if ($mode eq 'pickauthor') {
                   2346:         @cols = ('username','fullname','status','email');
                   2347:     } else {
                   2348:         @cols = ('username','domain','id','fullname');
                   2349:         if ($context eq 'course') {
                   2350:             push(@cols,'section');
                   2351:         }
1.102     raeburn  2352:         if (!($context eq 'domain' && ($env{'form.roletype'} eq 'course')
                   2353:                               && ($env{'form.roletype'} eq 'community'))) { 
1.55      raeburn  2354:             push(@cols,('start','end'));
                   2355:         }
                   2356:         if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
                   2357:             push(@cols,'role');
                   2358:         }
                   2359:         if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.102     raeburn  2360:                                     $env{'form.roletype'} eq 'course' ||
                   2361:                                     $env{'form.roletype'} eq 'community')) {
1.55      raeburn  2362:             push (@cols,'extent');
                   2363:         }
                   2364:         if (($statusmode eq 'Any') && 
1.102     raeburn  2365:             (!($context eq 'domain' && (($env{'form.roletype'} eq 'course')
                   2366:              || ($env{'form.roletype'} eq 'community'))))) {
1.55      raeburn  2367:             push(@cols,'status');
                   2368:         }
                   2369:         if ($context eq 'course') {
                   2370:             push(@cols,'groups');
                   2371:         }
                   2372:         push(@cols,'email');
1.2       raeburn  2373:     }
1.1       raeburn  2374: 
1.4       raeburn  2375:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2376:     if ($env{'form.showrole'} eq 'cr') {
                   2377:         $rolefilter = &mt('custom');  
                   2378:     } elsif ($env{'form.showrole'} ne 'Any') {
1.101     raeburn  2379:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
1.2       raeburn  2380:     }
1.16      raeburn  2381:     my $results_description;
                   2382:     if ($mode ne 'autoenroll') {
                   2383:         $results_description = &results_header_row($rolefilter,$statusmode,
1.102     raeburn  2384:                                                    $context,$permission,$mode,$crstype);
1.56      raeburn  2385:         $r->print('<b>'.$results_description.'</b><br /><br />');
1.16      raeburn  2386:     }
1.26      raeburn  2387:     my ($output,$actionselect,%canchange,%canchangesec);
1.55      raeburn  2388:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
                   2389:         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.16      raeburn  2390:             if ($permission->{'cusr'}) {
1.105     raeburn  2391:                 unless (($context eq 'domain') && 
                   2392:                         (($setting eq 'course') || ($setting eq 'community'))) {
                   2393:                     $actionselect = 
                   2394:                         &select_actions($context,$setting,$statusmode,$formname);
                   2395:                 }
1.16      raeburn  2396:             }
                   2397:             $r->print(<<END);
1.10      raeburn  2398: <input type="hidden" name="srchby"  value="uname" />
                   2399: <input type="hidden" name="srchin"   value="dom" />
                   2400: <input type="hidden" name="srchtype" value="exact" />
                   2401: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2402: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2403: END
1.16      raeburn  2404:             if ($actionselect) {
1.41      raeburn  2405:                 $output .= <<"END";
1.94      bisitz   2406: <div class="LC_left_float"><fieldset><legend>$lt{'ac'}</legend>
1.56      raeburn  2407: $actionselect
                   2408: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" /> &nbsp;
                   2409: <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  2410: END
1.26      raeburn  2411:                 my @allroles;
                   2412:                 if ($env{'form.showrole'} eq 'Any') {
                   2413:                     my $custom = 1;
                   2414:                     if ($context eq 'domain') {
1.101     raeburn  2415:                         @allroles = &roles_by_context($setting,$custom,$crstype);
1.26      raeburn  2416:                     } else {
1.101     raeburn  2417:                         @allroles = &roles_by_context($context,$custom,$crstype);
1.26      raeburn  2418:                     }
                   2419:                 } else {
                   2420:                     @allroles = ($env{'form.showrole'});
                   2421:                 }
                   2422:                 foreach my $role (@allroles) {
                   2423:                     if ($context eq 'domain') {
                   2424:                         if ($setting eq 'domain') {
                   2425:                             if (&Apache::lonnet::allowed('c'.$role,
                   2426:                                     $env{'request.role.domain'})) {
                   2427:                                 $canchange{$role} = 1;
                   2428:                             }
1.31      raeburn  2429:                         } elsif ($setting eq 'author') {
                   2430:                             if (&Apache::lonnet::allowed('c'.$role,
                   2431:                                     $env{'request.role.domain'})) {
                   2432:                                 $canchange{$role} = 1;
                   2433:                             }
1.26      raeburn  2434:                         }
                   2435:                     } elsif ($context eq 'author') {
                   2436:                         if (&Apache::lonnet::allowed('c'.$role,
                   2437:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2438:                             $canchange{$role} = 1;
                   2439:                         }
                   2440:                     } elsif ($context eq 'course') {
                   2441:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2442:                             $canchange{$role} = 1;
                   2443:                         } elsif ($env{'request.course.sec'} ne '') {
                   2444:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2445:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2446:                             }
                   2447:                         }
                   2448:                     }
                   2449:                 }
1.16      raeburn  2450:             }
1.94      bisitz   2451:             $output .= '<div class="LC_left_float"><fieldset><legend>'.$lt{'link'}.'</legend>'.
1.56      raeburn  2452:                        '<table><tr>';
                   2453:             my @linkdests = ('aboutme');
                   2454:             if ($permission->{'cusr'}) {
                   2455:                 unshift (@linkdests,'modify');
                   2456:             }
1.98      raeburn  2457:             if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
                   2458:                 &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
                   2459:                                          $env{'request.course.sec'})) {
                   2460:                 push(@linkdests,'track');
                   2461:             }
                   2462: 
1.56      raeburn  2463:             $output .= '<td>';
                   2464:             my $usernamelink = $env{'form.usernamelink'};
                   2465:             if ($usernamelink eq '') {
                   2466:                 $usernamelink = 'aboutme';
                   2467:             }
                   2468:             foreach my $item (@linkdests) {
                   2469:                 my $checkedstr = '';
                   2470:                 if ($item eq $usernamelink) {
1.86      bisitz   2471:                     $checkedstr = ' checked="checked"';
1.56      raeburn  2472:                 }
1.86      bisitz   2473:                 $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' />&nbsp;'.$lt{$item}.'</label></span><br />';
1.56      raeburn  2474:             }
                   2475:             my $checkwin;
                   2476:             if ($env{'form.userwin'}) {
1.86      bisitz   2477:                 $checkwin = ' checked="checked"';
1.56      raeburn  2478:             }
1.110     bisitz   2479:             $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  2480:         }
1.56      raeburn  2481:         $output .= "\n".'<div class="LC_clear_float_footer">&nbsp;</div>'."\n".
1.1       raeburn  2482:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2483:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2484:         if ($mode eq 'autoenroll') {
1.4       raeburn  2485:             $output .= "
1.55      raeburn  2486:  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2487:             ";
1.1       raeburn  2488:         } else {
1.105     raeburn  2489:             $output .= "\n".'<th>&nbsp;</th>'."\n";
1.11      raeburn  2490:             if ($actionselect) {
                   2491:                 $output .= '<th>'.&mt('Select').'</th>'."\n";
                   2492:             }
1.1       raeburn  2493:         }
                   2494:         foreach my $item (@cols) {
1.55      raeburn  2495:             $output .= "<th><a href=\"javascript:document.$formname.sortby.value='$item';document.$formname.submit();\">$lt{$item}</a></th>\n";
1.1       raeburn  2496:         }
1.2       raeburn  2497:         my %role_types = &role_type_names();
1.16      raeburn  2498:         if ($context eq 'course' && $mode ne 'autoenroll') {
1.4       raeburn  2499:             if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2500:                 # Clicker display on or off?
1.58      bisitz   2501:                 my %clicker_options = (
                   2502:                                         'on' => 'Show',
                   2503:                                         'off' => 'Hide',
                   2504:                                       );
1.4       raeburn  2505:                 my $clickerchg = 'on';
                   2506:                 if ($displayclickers eq 'on') {
                   2507:                     $clickerchg = 'off';
                   2508:                 }
1.58      bisitz   2509:                 $output .= '    <th>'."\n".'     '
                   2510:                         .&mt('[_1]'.$clicker_options{$clickerchg}.'[_2] clicker id'
                   2511:                             ,'<a href="javascript:document.'.$formname.'.displayclickers.value='
                   2512:                              ."'".$clickerchg."'".';document.'.$formname.'.submit();">'
                   2513:                             ,'</a>')
                   2514:                         ."\n".'    </th>'."\n";
1.1       raeburn  2515: 
1.4       raeburn  2516:                 # Photo display on or off?
                   2517:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2518:                     my %photo_options = &Apache::lonlocal::texthash(
                   2519:                                                             'on' => 'Show',
                   2520:                                                             'off' => 'Hide',
                   2521:                                                                 );
                   2522:                     my $photochg = 'on';
                   2523:                     if ($displayphotos eq 'on') {
                   2524:                         $photochg = 'off';
                   2525:                     }
                   2526:                     $output .= '    <th>'."\n".'     '.
1.55      raeburn  2527:                 '<a href="javascript:document.'.$formname.'.displayphotos.value='.
                   2528:                       "'".$photochg."'".';document.'.$formname.'.submit();">'.
1.1       raeburn  2529:                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
1.4       raeburn  2530:                       '    </th>'."\n";
                   2531:                 }
1.1       raeburn  2532:             }
1.4       raeburn  2533:         }
1.16      raeburn  2534:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2535: # Done with the HTML header line
                   2536:     } elsif ($mode eq 'csv') {
                   2537:         #
                   2538:         # Open a file
                   2539:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2540:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2541:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2542:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2543:             $r->log_error("Couldn't open $CSVfilename for output $!");
1.108     bisitz   2544:             $r->print(
                   2545:                 '<p class="LC_error">'
                   2546:                .&mt('Problems occurred in writing the CSV file.')
                   2547:                .' '.&mt('This error has been logged.')
                   2548:                .' '.&mt('Please alert your LON-CAPA administrator.')
                   2549:                .'</p>'
                   2550:             );
1.1       raeburn  2551:             $CSVfile = undef;
                   2552:         }
                   2553:         #
1.67      droeschl 2554:         push @cols,'clicker';
1.1       raeburn  2555:         # Write headers and data to file
1.2       raeburn  2556:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2557:         print $CSVfile '"'.join('","',map {
                   2558:             &Apache::loncommon::csv_translate($lt{$_})
1.67      droeschl 2559:             } (@cols))."\"\n";
1.1       raeburn  2560:     } elsif ($mode eq 'excel') {
1.67      droeschl 2561:         push @cols,'clicker';
1.1       raeburn  2562:         # Create the excel spreadsheet
                   2563:         ($excel_workbook,$excel_filename,$format) =
                   2564:             &Apache::loncommon::create_workbook($r);
                   2565:         return if (! defined($excel_workbook));
                   2566:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2567:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2568:         #
                   2569:         my @colnames = map {$lt{$_}} (@cols);
1.67      droeschl 2570: 
1.1       raeburn  2571:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2572:     }
                   2573: 
                   2574: # Done with header lines in all formats
                   2575:     my %index;
                   2576:     my $i;
1.2       raeburn  2577:     foreach my $idx (@$keylist) {
                   2578:         $index{$idx} = $i++;
                   2579:     }
1.4       raeburn  2580:     my $usercount = 0;
1.33      raeburn  2581:     my ($secfilter,$grpfilter);
                   2582:     if ($context eq 'course') {
                   2583:         $secfilter = $env{'form.secfilter'};
                   2584:         $grpfilter = $env{'form.grpfilter'};
                   2585:         if ($secfilter eq '') {
                   2586:             $secfilter = 'all';
                   2587:         }
                   2588:         if ($grpfilter eq '') {
                   2589:             $grpfilter = 'all';
                   2590:         }
                   2591:     }
1.83      raeburn  2592:     my %ltstatus = &Apache::lonlocal::texthash(
                   2593:                                                 Active  => 'Active',
                   2594:                                                 Future  => 'Future',
                   2595:                                                 Expired => 'Expired',
                   2596:                                                );
1.2       raeburn  2597:     # Get groups, role, permanent e-mail so we can sort on them if
                   2598:     # necessary.
                   2599:     foreach my $user (keys(%{$userlist})) {
1.43      raeburn  2600:         if ($user eq '' ) {
                   2601:             delete($userlist->{$user});
                   2602:             next;
                   2603:         }
1.11      raeburn  2604:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2605:             delete($userlist->{$user});
                   2606:             next;
                   2607:         }
1.2       raeburn  2608:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2609:         if (($statusmode ne 'Any') && 
                   2610:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2611:             delete($userlist->{$user});
                   2612:             next;
                   2613:         }
1.2       raeburn  2614:         if ($context eq 'domain') {
                   2615:             if ($env{'form.roletype'} eq 'domain') {
                   2616:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2617:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2618:                     ($udom eq $env{'request.role.domain'})) {
                   2619:                     delete($userlist->{$user});
                   2620:                     next;
                   2621:                 }
1.13      raeburn  2622:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2623:                 ($uname,$udom,$role) = split(/:/,$user,-1);
1.102     raeburn  2624:             } elsif (($env{'form.roletype'} eq 'course') || 
                   2625:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2626:                 ($uname,$udom,$role) = split(/:/,$user);
                   2627:             }
                   2628:         } else {
                   2629:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   2630:             if (($context eq 'course') && $role eq '') {
                   2631:                 $role = 'st';
                   2632:             }
                   2633:         }
                   2634:         $userlist->{$user}->[$index{'role'}] = $role;
                   2635:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   2636:             delete($userlist->{$user});
                   2637:             next;
                   2638:         }
1.33      raeburn  2639:         if ($context eq 'course') {
                   2640:             my @ac_groups;
                   2641:             if (ref($classgroups) eq 'HASH') {
                   2642:                 $groups = $classgroups->{$user};
                   2643:             }
                   2644:             if (ref($groups->{'active'}) eq 'HASH') {
                   2645:                 @ac_groups = keys(%{$groups->{'active'}});
                   2646:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   2647:             }
                   2648:             if ($mode ne 'autoenroll') {
                   2649:                 my $section = $userlist->{$user}->[$index{'section'}];
1.43      raeburn  2650:                 if (($env{'request.course.sec'} ne '') && 
                   2651:                     ($section ne $env{'request.course.sec'})) {
                   2652:                     if ($role eq 'st') {
                   2653:                         delete($userlist->{$user});
                   2654:                         next;
                   2655:                     }
                   2656:                 }
1.33      raeburn  2657:                 if ($secfilter eq 'none') {
                   2658:                     if ($section ne '') {
                   2659:                         delete($userlist->{$user});
                   2660:                         next;
                   2661:                     }
                   2662:                 } elsif ($secfilter ne 'all') {
                   2663:                     if ($section ne $secfilter) {
                   2664:                         delete($userlist->{$user});
                   2665:                         next;
                   2666:                     }
                   2667:                 }
                   2668:                 if ($grpfilter eq 'none') {
                   2669:                     if (@ac_groups > 0) {
                   2670:                         delete($userlist->{$user});
                   2671:                         next;
                   2672:                     }
                   2673:                 } elsif ($grpfilter ne 'all') {
                   2674:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   2675:                         delete($userlist->{$user});
                   2676:                         next;
                   2677:                     }
                   2678:                 }
1.44      raeburn  2679:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2680:                     if (($displayphotos eq 'on') && ($role eq 'st')) {
                   2681:                         $userlist->{$user}->[$index{'photo'}] =
1.47      raeburn  2682:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
                   2683:                         $userlist->{$user}->[$index{'thumbnail'}] =
1.44      raeburn  2684:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,
                   2685:                                                                 'gif','thumbnail');
                   2686:                     }
                   2687:                 }
1.33      raeburn  2688:             }
1.2       raeburn  2689:         }
                   2690:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   2691:         if ($emails{'permanentemail'} =~ /\S/) {
                   2692:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   2693:         }
1.4       raeburn  2694:         $usercount ++;
                   2695:     }
                   2696:     my $autocount = 0;
                   2697:     my $manualcount = 0;
                   2698:     my $lockcount = 0;
                   2699:     my $unlockcount = 0;
                   2700:     if ($usercount) {
                   2701:         $r->print($output);
                   2702:     } else {
                   2703:         if ($mode eq 'autoenroll') {
                   2704:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   2705:         } else {
                   2706:             return;
                   2707:         }
1.1       raeburn  2708:     }
1.2       raeburn  2709:     #
                   2710:     # Sort the users
1.1       raeburn  2711:     my $index  = $index{$sortby};
                   2712:     my $second = $index{'username'};
                   2713:     my $third  = $index{'domain'};
1.2       raeburn  2714:     my @sorted_users = sort {
                   2715:         lc($userlist->{$a}->[$index])  cmp lc($userlist->{$b}->[$index])
1.1       raeburn  2716:             ||
1.2       raeburn  2717:         lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second])            ||
                   2718:         lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   2719:         } (keys(%$userlist));
1.4       raeburn  2720:     my $rowcount = 0;
1.2       raeburn  2721:     foreach my $user (@sorted_users) {
1.4       raeburn  2722:         my %in;
1.2       raeburn  2723:         my $sdata = $userlist->{$user};
1.4       raeburn  2724:         $rowcount ++; 
1.2       raeburn  2725:         foreach my $item (@{$keylist}) {
                   2726:             $in{$item} = $sdata->[$index{$item}];
                   2727:         }
1.67      droeschl 2728:         my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   2729:         if ($clickers!~/\w/) { $clickers='-'; }
                   2730:         $in{'clicker'} = $clickers; 
                   2731: 	my $role = $in{'role'};
1.102     raeburn  2732:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
1.136     raeburn  2733:         unless ($mode eq 'excel') {
                   2734:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2735:                 $in{'start'} = &mt('none');
                   2736:             } else {
                   2737:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   2738:             }
                   2739:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2740:                 $in{'end'} = &mt('none');
                   2741:             } else {
                   2742:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   2743:             }
1.1       raeburn  2744:         }
1.55      raeburn  2745:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2746:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  2747:             my $checkval;
1.16      raeburn  2748:             if ($mode eq 'autoenroll') {
                   2749:                 my $cellentry;
                   2750:                 if ($in{'type'} eq 'auto') {
                   2751:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label>';
                   2752:                     $autocount ++;
                   2753:                 } else {
1.76      bisitz   2754:                     $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  2755:                     $manualcount ++;
                   2756:                     if ($in{'lockedtype'}) {
                   2757:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Unlock').'</label>';
                   2758:                         $unlockcount ++;
                   2759:                     } else {
                   2760:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Lock').'</label>';
                   2761:                         $lockcount ++;
1.11      raeburn  2762:                     }
1.76      bisitz   2763:                     $cellentry .= '</span></td></tr></table>';
1.16      raeburn  2764:                 }
                   2765:                 $r->print("<td>$cellentry</td>\n");
                   2766:             } else {
1.55      raeburn  2767:                 if ($mode ne 'pickauthor') {  
                   2768:                     $r->print("<td>$rowcount</td>\n");
                   2769:                 }
1.16      raeburn  2770:                 if ($actionselect) {
1.26      raeburn  2771:                     my $showcheckbox;
                   2772:                     if ($role =~ /^cr\//) {
                   2773:                         $showcheckbox = $canchange{'cr'};
                   2774:                     } else {
                   2775:                         $showcheckbox = $canchange{$role};
                   2776:                     }
                   2777:                     if (!$showcheckbox) {
                   2778:                         if ($context eq 'course') {
                   2779:                             if ($canchangesec{$role} ne '') {
                   2780:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   2781:                                     $showcheckbox = 1;
                   2782:                                 }
                   2783:                             }
1.16      raeburn  2784:                         }
1.26      raeburn  2785:                     }
                   2786:                     if ($showcheckbox) {
                   2787:                         $checkval = $user; 
                   2788:                         if ($context eq 'course') {
                   2789:                             if ($role eq 'st') {
                   2790:                                 $checkval .= ':st';
                   2791:                             }
                   2792:                             $checkval .= ':'.$in{'section'};
                   2793:                             if ($role eq 'st') {
                   2794:                                 $checkval .= ':'.$in{'type'}.':'.
                   2795:                                              $in{'lockedtype'};
                   2796:                             }
1.16      raeburn  2797:                         }
1.26      raeburn  2798:                         $r->print('<td><input type="checkbox" name="'.
1.86      bisitz   2799:                                   'actionlist" value="'.$checkval.'" /></td>');
1.26      raeburn  2800:                     } else {
                   2801:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  2802:                     }
1.55      raeburn  2803:                 } elsif ($mode eq 'pickauthor') {
                   2804:                         $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
1.11      raeburn  2805:                 }
                   2806:             }
1.2       raeburn  2807:             foreach my $item (@cols) {
1.10      raeburn  2808:                 if ($item eq 'username') {
1.48      raeburn  2809:                     $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
1.16      raeburn  2810:                 } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11      raeburn  2811:                     $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.83      raeburn  2812:                 } elsif ($item eq 'status') {
                   2813:                     my $showitem = $in{$item};
                   2814:                     if (defined($ltstatus{$in{$item}})) {
                   2815:                         $showitem = $ltstatus{$in{$item}};
                   2816:                     }
                   2817:                     $r->print('<td>'.$showitem.'</td>'."\n");
1.10      raeburn  2818:                 } else {
                   2819:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   2820:                 }
1.2       raeburn  2821:             }
1.16      raeburn  2822:             if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4       raeburn  2823:                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2824:                     if ($displayclickers eq 'on') {
                   2825:                         my $clickers =
1.2       raeburn  2826:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4       raeburn  2827:                         if ($clickers!~/\w/) { $clickers='-'; }
                   2828:                         $r->print('<td>'.$clickers.'</td>');
1.2       raeburn  2829:                     } else {
                   2830:                         $r->print('    <td>&nbsp;</td>  ');
                   2831:                     }
1.4       raeburn  2832:                     if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.44      raeburn  2833:                         if ($displayphotos eq 'on' && $role eq 'st' && $in{'photo'} ne '') {
1.90      bisitz   2834:                             $r->print('    <td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
1.4       raeburn  2835:                         } else {
                   2836:                             $r->print('    <td>&nbsp;</td>  ');
                   2837:                         }
                   2838:                     }
1.2       raeburn  2839:                 }
                   2840:             }
                   2841:             $r->print(&Apache::loncommon::end_data_table_row());
                   2842:         } elsif ($mode eq 'csv') {
                   2843:             next if (! defined($CSVfile));
                   2844:             # no need to bother with $linkto
                   2845:             my @line = ();
                   2846:             foreach my $item (@cols) {
                   2847:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   2848:             }
1.67      droeschl 2849:             print $CSVfile '"'.join('","',@line)."\"\n";
1.2       raeburn  2850:         } elsif ($mode eq 'excel') {
                   2851:             my $col = 0;
                   2852:             foreach my $item (@cols) {
                   2853:                 if ($item eq 'start' || $item eq 'end') {
1.136     raeburn  2854:                     if ((defined($in{$item})) && ($in{$item} != 0)) {
1.2       raeburn  2855:                         $excel_sheet->write($row,$col++,
1.136     raeburn  2856:                             &Apache::lonstathelpers::calc_serial($in{$item}),
1.2       raeburn  2857:                                     $format->{'date'});
                   2858:                     } else {
                   2859:                         $excel_sheet->write($row,$col++,'none');
                   2860:                     }
                   2861:                 } else {
                   2862:                     $excel_sheet->write($row,$col++,$in{$item});
                   2863:                 }
                   2864:             }
                   2865:             $row++;
1.1       raeburn  2866:         }
                   2867:     }
1.55      raeburn  2868:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2869:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   2870:     } elsif ($mode eq 'excel') {
                   2871:         $excel_workbook->close();
1.68      droeschl 2872: 	$r->print(&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<p><a href="'.$excel_filename.'">','</a>')."</p>\n");
1.2       raeburn  2873:     } elsif ($mode eq 'csv') {
                   2874:         close($CSVfile);
1.68      droeschl 2875: 	$r->print(&mt('[_1]Your CSV file[_2] is ready for download.', '<p><a href="'.$CSVfilename.'">','</a>')."</p>\n");
1.2       raeburn  2876:         $r->rflush();
                   2877:     }
                   2878:     if ($mode eq 'autoenroll') {
                   2879:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  2880:     } else {
                   2881:         return ($usercount);
1.2       raeburn  2882:     }
1.1       raeburn  2883: }
                   2884: 
1.56      raeburn  2885: sub bulkaction_javascript {
                   2886:     my ($formname,$caller) = @_;
                   2887:     my $docstart = 'document';
                   2888:     if ($caller eq 'popup') {
                   2889:         $docstart = 'opener.document';
                   2890:     }
                   2891:     my %lt = &Apache::lonlocal::texthash(
                   2892:               acwi => 'Access will be set to start immediately',
                   2893:               asyo => 'as you did not select an end date in the pop-up window',
                   2894:               accw => 'Access will be set to continue indefinitely',
                   2895:               asyd => 'as you did not select an end date in the pop-up window',
                   2896:               sewi => "Sections will be switched to 'No section'",
                   2897:               ayes => "as you either selected the 'No section' option",
                   2898:               oryo => 'or you did not select a section in the pop-up window',
                   2899:               arol => 'A role with no section will be added',
                   2900:               swbs => 'Sections will be switched to:',
                   2901:               rwba => 'Roles will be added for section(s):',
                   2902:             );
                   2903:     my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
                   2904:     my $noaction = &mt("You need to select an action to take for the user(s) you have selected"); 
                   2905:     my $singconfirm = &mt(' for a single user?');
                   2906:     my $multconfirm = &mt(' for multiple users?');
                   2907:     my $output = <<"ENDJS";
                   2908: function verify_action (field) {
                   2909:     var numchecked = 0;
                   2910:     var singconf = '$singconfirm';
                   2911:     var multconf = '$multconfirm';
                   2912:     if ($docstart.$formname.elements[field].length > 0) {
                   2913:         for (i=0; i<$docstart.$formname.elements[field].length; i++) {
                   2914:             if ($docstart.$formname.elements[field][i].checked == true) {
                   2915:                numchecked ++;
                   2916:             }
                   2917:         }
                   2918:     } else {
                   2919:         if ($docstart.$formname.elements[field].checked == true) {
                   2920:             numchecked ++;
                   2921:         }
                   2922:     }
                   2923:     if (numchecked == 0) {
                   2924:         alert("$alert");
                   2925:         return;
                   2926:     } else {
                   2927:         var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
                   2928:         var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
                   2929:         if (choice == '') {
                   2930:             alert("$noaction");
                   2931:             return;
                   2932:         } else {
                   2933:             if (numchecked == 1) {
                   2934:                 message += singconf;
                   2935:             } else {
                   2936:                 message += multconf;
                   2937:             }
                   2938: ENDJS
                   2939:     if ($caller ne 'popup') {
                   2940:         $output .= <<"NEWWIN";
                   2941:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
                   2942:                 opendatebrowser(document.$formname,'$formname','go');
                   2943:                 return;
                   2944: 
                   2945:             } else {
                   2946:                 if (confirm(message)) {
                   2947:                     document.$formname.phase.value = 'bulkchange';
                   2948:                     document.$formname.submit();
                   2949:                     return;
                   2950:                 }
                   2951:             }
                   2952: NEWWIN
                   2953:     } else {
                   2954:         $output .= <<"POPUP";
                   2955:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
                   2956:                 var datemsg = '';
                   2957:                 if (($docstart.$formname.startdate_month.value == '') &&
                   2958:                     ($docstart.$formname.startdate_day.value  == '') &&
                   2959:                     ($docstart.$formname.startdate_year.value == '')) {
                   2960:                     datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
                   2961:                 }
                   2962:                 if (($docstart.$formname.enddate_month.value == '') &&
                   2963:                     ($docstart.$formname.enddate_day.value  == '') &&
                   2964:                     ($docstart.$formname.enddate_year.value == '')) {
                   2965:                     datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
                   2966:                 }
                   2967:                 if (datemsg != '') {
                   2968:                     message += "\\n"+datemsg;
                   2969:                 }
                   2970:             }
                   2971:             if (choice == 'chgsec') {
                   2972:                 var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
                   2973:                 var retained =  $docstart.$formname.retainsec.value;
                   2974:                 var secshow = $docstart.$formname.newsecs.value;
                   2975:                 if (secshow == '') {
                   2976:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   2977:                         message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   2978:                     } else {
                   2979:                         message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   2980:                     }
                   2981:                 } else {
                   2982:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   2983:                         message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
                   2984:                     } else {
                   2985:                         message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
                   2986:                     }
                   2987:                 }
                   2988:             }
                   2989:             if (confirm(message)) {
                   2990:                 $docstart.$formname.phase.value = 'bulkchange';
                   2991:                 $docstart.$formname.submit();
                   2992:                 window.close();
                   2993:             }
                   2994: POPUP
                   2995:     }
                   2996:     $output .= '
                   2997:         }
                   2998:     }
                   2999: }
                   3000: ';
                   3001:     return $output;
                   3002: }
                   3003: 
1.10      raeburn  3004: sub print_username_link {
1.48      raeburn  3005:     my ($mode,$in) = @_;
1.10      raeburn  3006:     my $output;
1.16      raeburn  3007:     if ($mode eq 'autoenroll') {
                   3008:         $output = $in->{'username'};
1.10      raeburn  3009:     } else {
                   3010:         $output = '<a href="javascript:username_display_launch('.
1.112     bisitz   3011:                   "'$in->{'username'}','$in->{'domain'}'".')">'.
1.10      raeburn  3012:                   $in->{'username'}.'</a>';
                   3013:     }
                   3014:     return $output;
                   3015: }
                   3016: 
1.2       raeburn  3017: sub role_type_names {
                   3018:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  3019:                          'domain' => 'Domain Roles',
                   3020:                          'author' => 'Co-Author Roles',
                   3021:                          'course' => 'Course Roles',
1.101     raeburn  3022:                          'community' => 'Community Roles',
1.2       raeburn  3023:              );
                   3024:     return %lt;
                   3025: }
                   3026: 
1.11      raeburn  3027: sub select_actions {
1.55      raeburn  3028:     my ($context,$setting,$statusmode,$formname) = @_;
1.11      raeburn  3029:     my %lt = &Apache::lonlocal::texthash(
                   3030:                 revoke   => "Revoke user roles",
                   3031:                 delete   => "Delete user roles",
                   3032:                 reenable => "Re-enable expired user roles",
                   3033:                 activate => "Make future user roles active now",
                   3034:                 chgdates  => "Change starting/ending dates",
                   3035:                 chgsec   => "Change section associated with user roles",
                   3036:     );
                   3037:     my ($output,$options,%choices);
1.23      raeburn  3038:     # FIXME Disable actions for now for roletype=course in domain context
                   3039:     if ($context eq 'domain' && $setting eq 'course') {
                   3040:         return;
                   3041:     }
1.26      raeburn  3042:     if ($context eq 'course') {
                   3043:         if ($env{'form.showrole'} ne 'Any') {
                   3044:              if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   3045:                                            $env{'request.course.id'})) {
                   3046:                  if ($env{'request.course.sec'} eq '') {
                   3047:                      return;
                   3048:                  } else {
                   3049:                      if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   3050:                          return;
                   3051:                      }
                   3052:                  }
                   3053:             }
                   3054:         }
                   3055:     }
1.11      raeburn  3056:     if ($statusmode eq 'Any') {
                   3057:         $options .= '
                   3058: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   3059:         $choices{'dates'} = 1;
                   3060:     } else {
                   3061:         if ($statusmode eq 'Future') {
                   3062:             $options .= '
                   3063: <option value="activate">'.$lt{'activate'}.'</option>';
                   3064:             $choices{'dates'} = 1;
                   3065:         } elsif ($statusmode eq 'Expired') {
                   3066:             $options .= '
                   3067: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   3068:             $choices{'dates'} = 1;
                   3069:         }
1.13      raeburn  3070:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   3071:             $options .= '
                   3072: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   3073: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   3074:             $choices{'dates'} = 1;
                   3075:         }
1.11      raeburn  3076:     }
                   3077:     if ($context eq 'domain') {
                   3078:         $options .= '
                   3079: <option value="delete">'.$lt{'delete'}.'</option>';
                   3080:     }
                   3081:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  3082:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  3083:             $options .= '
                   3084: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   3085:             $choices{'sections'} = 1;
                   3086:         }
                   3087:     }
                   3088:     if ($options) {
1.56      raeburn  3089:         $output = '<select name="bulkaction">'."\n".
1.11      raeburn  3090:                   '<option value="" selected="selected">'.
                   3091:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   3092:         if ($choices{'dates'}) {
                   3093:             $output .= 
                   3094:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   3095:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   3096:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   3097:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   3098:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   3099:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   3100:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   3101:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   3102:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   3103:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   3104:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
1.56      raeburn  3105:                 '<input type="hidden" name="enddate_second" value="" />'."\n".
                   3106:                 '<input type="hidden" name="no_end_date" value="" />'."\n";
1.11      raeburn  3107:             if ($context eq 'course') {
                   3108:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   3109:             }
                   3110:         }
                   3111:         if ($choices{'sections'}) {
1.91      bisitz   3112:             $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
                   3113:                        '<input type="hidden" name="newsecs" value="" />'."\n";
1.11      raeburn  3114:         }
                   3115:     }
                   3116:     return $output;
                   3117: }
                   3118: 
                   3119: sub date_section_javascript {
                   3120:     my ($context,$setting) = @_;
1.49      raeburn  3121:     my $title = 'Date_And_Section_Selector';
1.41      raeburn  3122:     my %nopopup = &Apache::lonlocal::texthash (
                   3123:         revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
                   3124:         delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
                   3125:         none   => "Choose an action to take for selected users",
                   3126:     );  
1.96      bisitz   3127:     my $output = <<"ENDONE";
                   3128: <script type="text/javascript">
                   3129: // <![CDATA[
1.41      raeburn  3130:     function opendatebrowser(callingform,formname,calledby) {
1.11      raeburn  3131:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   3132:         var url = '/adm/createuser?';
                   3133:         var type = '';
                   3134:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   3135: ENDONE
                   3136:     if ($context eq 'domain') {
                   3137:         $output .= '
                   3138:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   3139: ';
                   3140:     }
                   3141:     my $width= '700';
                   3142:     my $height = '400';
                   3143:     $output .= <<"ENDTWO";
                   3144:         url += 'action=dateselect&callingform=' + formname + 
                   3145:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   3146:         var title = '$title';
                   3147:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   3148:         options += ',width=$width,height=$height';
                   3149:         stdeditbrowser = open(url,title,options,'1');
                   3150:         stdeditbrowser.focus();
                   3151:     }
1.96      bisitz   3152: // ]]>
1.11      raeburn  3153: </script>
                   3154: ENDTWO
                   3155:     return $output;
                   3156: }
                   3157: 
                   3158: sub date_section_selector {
1.101     raeburn  3159:     my ($context,$permission,$crstype) = @_;
1.11      raeburn  3160:     my $callingform = $env{'form.callingform'};
                   3161:     my $formname = 'dateselect';  
                   3162:     my $groupslist = &get_groupslist();
                   3163:     my $sec_js = &setsections_javascript($formname,$groupslist);
                   3164:     my $output = <<"END";
                   3165: <script type="text/javascript">
1.96      bisitz   3166: // <![CDATA[
1.11      raeburn  3167: 
                   3168: $sec_js
                   3169: 
                   3170: function saveselections(formname) {
                   3171: 
                   3172: END
                   3173:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3174:         $output .= <<"END";
1.40      raeburn  3175:         if (formname.retainsec.length > 1) {  
                   3176:             for (var i=0; i<formname.retainsec.length; i++) {
                   3177:                 if (formname.retainsec[i].checked == true) {
                   3178:                     opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
                   3179:                 }
                   3180:             }
                   3181:         } else {
                   3182:             opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   3183:         }
1.103     raeburn  3184:         setSections(formname,'$crstype');
1.11      raeburn  3185:         if (seccheck == 'ok') {
                   3186:             opener.document.$callingform.newsecs.value = formname.sections.value;
                   3187:         }
                   3188: END
                   3189:     } else {
                   3190:         if ($context eq 'course') {
                   3191:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   3192:                 ($env{'form.bulkaction'} eq 'activate') || 
                   3193:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  3194:                 if ($env{'request.course.sec'} eq '') {
                   3195:                     $output .= <<"END";
1.11      raeburn  3196:  
                   3197:         if (formname.makedatesdefault.checked == true) {
                   3198:             opener.document.$callingform.makedatesdefault.value = 1;
                   3199:         }
                   3200:         else {
                   3201:             opener.document.$callingform.makedatesdefault.value = 0;
                   3202:         }
                   3203: 
                   3204: END
1.26      raeburn  3205:                 }
1.11      raeburn  3206:             }
                   3207:         }
                   3208:         $output .= <<"END";
                   3209:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   3210:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   3211:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   3212:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   3213:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   3214:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   3215:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   3216:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   3217:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   3218:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   3219:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   3220:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
1.56      raeburn  3221:     if (formname.no_end_date.checked) {
                   3222:         opener.document.$callingform.no_end_date.value = '1';
                   3223:     } else {
                   3224:         opener.document.$callingform.no_end_date.value = '0';
                   3225:     }
1.11      raeburn  3226: END
                   3227:     }
1.56      raeburn  3228:     my $verify_action_js = &bulkaction_javascript($callingform,'popup');
                   3229:     $output .= <<"ENDJS";
                   3230:     verify_action('actionlist');
1.11      raeburn  3231: }
1.56      raeburn  3232: 
                   3233: $verify_action_js
                   3234: 
1.96      bisitz   3235: // ]]>
1.11      raeburn  3236: </script>
1.56      raeburn  3237: ENDJS
1.11      raeburn  3238:     my %lt = &Apache::lonlocal::texthash (
                   3239:                  chac => 'Access dates to apply for selected users',
                   3240:                  chse => 'Changes in section affiliation to apply to selected users',
1.118     raeburn  3241:                  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.',
                   3242:                  forn => 'For a course role that is not "student", users may have roles in more than one section at a time.',
                   3243:                  reta => "Retain each user's current section affiliations?",
1.103     raeburn  3244:                  dnap => '(Does not apply to student roles).',
1.11      raeburn  3245:             );
                   3246:     my ($date_items,$headertext);
                   3247:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3248:         $headertext = $lt{'chse'};
                   3249:     } else {
                   3250:         $headertext = $lt{'chac'};
                   3251:         my $starttime;
                   3252:         if (($env{'form.bulkaction'} eq 'activate') || 
                   3253:             ($env{'form.bulkaction'} eq 'reenable')) {
                   3254:             $starttime = time;
                   3255:         }
                   3256:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  3257:                                           $env{'form.bulkaction'},$formname,
1.101     raeburn  3258:                                           $permission,$crstype);
1.11      raeburn  3259:     }
                   3260:     $output .= '<h3>'.$headertext.'</h3>'.
1.118     raeburn  3261:                '<form name="'.$formname.'" method="post" action="">'."\n".
1.11      raeburn  3262:                 $date_items;
                   3263:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  3264:         my ($cnum,$cdom) = &get_course_identity();
1.103     raeburn  3265:         if ($crstype eq 'Community') {
1.118     raeburn  3266:             $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.');
                   3267:             $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  3268:             $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
                   3269:         }
1.11      raeburn  3270:         my $info;
                   3271:         if ($env{'form.showrole'} eq 'st') {
                   3272:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  3273:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  3274:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   3275:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   3276:             $info = $lt{'reta'};
                   3277:         } else {
                   3278:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   3279:             $info = $lt{'reta'};
                   3280:         }
                   3281:         if ($info) {
                   3282:             $info .= '<span class="LC_nobreak">'.
                   3283:                      '<label><input type="radio" name="retainsec" value="1" '.
                   3284:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   3285:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   3286:                      &mt('No').'</label></span>';
                   3287:             if ($env{'form.showrole'} eq 'Any') {
                   3288:                 $info .= '<br />'.$lt{'dnap'};
                   3289:             }
                   3290:             $info .= '</p>';
                   3291:         } else {
                   3292:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   3293:         }
1.21      raeburn  3294:         my $rowtitle = &mt('New section to assign');
1.101     raeburn  3295:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context,'',$crstype);
1.11      raeburn  3296:         $output .= $info.$secbox;
                   3297:     }
                   3298:     $output .= '<p>'.
1.81      schafran 3299: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
1.11      raeburn  3300: '</form>';
                   3301:     return $output;
                   3302: }
                   3303: 
1.17      raeburn  3304: sub section_picker {
1.101     raeburn  3305:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype) = @_;
1.17      raeburn  3306:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3307:     my $sections_select .= &course_sections(\%sections_count,$role);
1.118     raeburn  3308:     my $secbox = '<div>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
1.17      raeburn  3309:     if ($mode eq 'upload') {
                   3310:         my ($options,$cb_script,$coursepick) =
1.101     raeburn  3311:             &default_role_selector($context,1,$crstype);
1.59      bisitz   3312:         $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
1.17      raeburn  3313:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3314:     }
                   3315:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
                   3316:     if ($env{'request.course.sec'} eq '') {
                   3317:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   3318:                    '<td align="center">'.&mt('Existing sections')."\n".
                   3319:                    '<br />'.$sections_select.'</td><td align="center">'.
                   3320:                    &mt('New section').'<br />'."\n".
1.118     raeburn  3321:                    '<input type="text" name="newsec" size="15" value="" />'."\n".
1.17      raeburn  3322:                    '<input type="hidden" name="sections" value="" />'."\n".
                   3323:                    '</td></tr></table>'."\n";
                   3324:     } else {
                   3325:        $secbox .= '<input type="hidden" name="sections" value="'.
                   3326:                    $env{'request.course.sec'}.'" />'.
                   3327:                    $env{'request.course.sec'};
                   3328:     }
                   3329:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
1.118     raeburn  3330:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
1.17      raeburn  3331:     return $secbox;
                   3332: }
                   3333: 
1.2       raeburn  3334: sub results_header_row {
1.102     raeburn  3335:     my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
1.5       raeburn  3336:     my ($description,$showfilter);
                   3337:     if ($rolefilter ne 'Any') {
                   3338:         $showfilter = $rolefilter;
                   3339:     }
1.2       raeburn  3340:     if ($context eq 'course') {
1.24      raeburn  3341:         if ($mode eq 'csv' || $mode eq 'excel') {
1.102     raeburn  3342:             if ($crstype eq 'Community') {
                   3343:                 $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3344:             } else {
                   3345:                 $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3346:             }
1.24      raeburn  3347:         }
1.2       raeburn  3348:         if ($statusmode eq 'Expired') {
1.102     raeburn  3349:             if ($crstype eq 'Community') {
                   3350:                 $description .= &mt('Users in community with expired [_1] roles',$showfilter);
                   3351:             } else {
                   3352:                 $description .= &mt('Users in course with expired [_1] roles',$showfilter);
                   3353:             }
1.11      raeburn  3354:         } elsif ($statusmode eq 'Future') {
1.102     raeburn  3355:             if ($crstype eq 'Community') {
                   3356:                 $description .= &mt('Users in community with future [_1] roles',$showfilter);
                   3357:             } else {
                   3358:                 $description .= &mt('Users in course with future [_1] roles',$showfilter);
                   3359:             }
1.2       raeburn  3360:         } elsif ($statusmode eq 'Active') {
1.102     raeburn  3361:             if ($crstype eq 'Community') {
                   3362:                 $description .= &mt('Users in community with active [_1] roles',$showfilter);
                   3363:             } else {
                   3364:                 $description .= &mt('Users in course with active [_1] roles',$showfilter);
                   3365:             }
1.2       raeburn  3366:         } else {
                   3367:             if ($rolefilter eq 'Any') {
1.102     raeburn  3368:                 if ($crstype eq 'Community') {
                   3369:                     $description .= &mt('All users in community');
                   3370:                 } else {
                   3371:                     $description .= &mt('All users in course');
                   3372:                 }
1.2       raeburn  3373:             } else {
1.102     raeburn  3374:                 if ($crstype eq 'Community') {
                   3375:                     $description .= &mt('All users in community with [_1] roles',$rolefilter);
                   3376:                 } else {
                   3377:                     $description .= &mt('All users in course with [_1] roles',$rolefilter);
                   3378:                 }
1.2       raeburn  3379:             }
                   3380:         }
1.33      raeburn  3381:         my $constraint;
1.26      raeburn  3382:         my $viewablesec = &viewable_section($permission);
                   3383:         if ($viewablesec ne '') {
1.15      raeburn  3384:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  3385:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.103     raeburn  3386:             } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
1.33      raeburn  3387:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   3388:             }
                   3389:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3390:                 if ($env{'form.grpfilter'} eq 'none') {
                   3391:                     $constraint .= &mt(' and not in any group');
                   3392:                 } else {
                   3393:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   3394:                 }
                   3395:             }
                   3396:         } else {
                   3397:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   3398:                 if ($env{'form.secfilter'} eq 'none') {
                   3399:                     $constraint = &mt('only users affiliated with no section');
                   3400:                 } else {
                   3401:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   3402:                 }
                   3403:             }
                   3404:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3405:                 if ($env{'form.grpfilter'} eq 'none') {
                   3406:                     if ($constraint eq '') {
                   3407:                         $constraint = &mt('only users not in any group');
                   3408:                     } else {
                   3409:                         $constraint .= &mt(' and also not in any group'); 
                   3410:                     }
                   3411:                 } else {
                   3412:                     if ($constraint eq '') {
                   3413:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   3414:                     } else {
                   3415:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   3416:                     }
                   3417:                 }
1.15      raeburn  3418:             }
                   3419:         }
1.33      raeburn  3420:         if ($constraint ne '') {
                   3421:             $description .= ' ('.$constraint.')';
                   3422:         } 
1.13      raeburn  3423:     } elsif ($context eq 'author') {
1.14      raeburn  3424:         $description = 
1.73      bisitz   3425:             &mt('Author space for [_1]'
                   3426:                 ,'<span class="LC_cusr_emph">'
                   3427:                 .&Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})
                   3428:                 .'</span>')
                   3429:             .':&nbsp;&nbsp;';
1.2       raeburn  3430:         if ($statusmode eq 'Expired') {
1.5       raeburn  3431:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  3432:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  3433:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  3434:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  3435:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  3436:         } else {
                   3437:             if ($rolefilter eq 'Any') {
1.5       raeburn  3438:                 $description .= &mt('All co-authors');
1.2       raeburn  3439:             } else {
                   3440:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   3441:             }
                   3442:         }
                   3443:     } elsif ($context eq 'domain') {
                   3444:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.73      bisitz   3445:         $description = &mt('Domain - [_1]:',$domdesc).' ';
1.2       raeburn  3446:         if ($env{'form.roletype'} eq 'domain') {
                   3447:             if ($statusmode eq 'Expired') {
1.5       raeburn  3448:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3449:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3450:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  3451:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3452:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  3453:             } else {
                   3454:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3455:                     $description .= &mt('All users in domain');
1.2       raeburn  3456:                 } else {
                   3457:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   3458:                 }
                   3459:             }
1.13      raeburn  3460:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  3461:             if ($statusmode eq 'Expired') {
1.5       raeburn  3462:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3463:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3464:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  3465:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3466:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  3467:             } else {
                   3468:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3469:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  3470:                 } else {
1.116     bisitz   3471:                     $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
1.2       raeburn  3472:                 }
                   3473:             }
1.102     raeburn  3474:         } elsif (($env{'form.roletype'} eq 'course') || 
                   3475:                  ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  3476:             my $coursefilter = $env{'form.coursepick'};
1.102     raeburn  3477:             if ($env{'form.roletype'} eq 'course') {
                   3478:                 if ($coursefilter eq 'category') {
                   3479:                     my $instcode = &instcode_from_coursefilter();
                   3480:                     if ($instcode eq '.') {
                   3481:                         $description .= &mt('All courses in domain').' - ';
                   3482:                     } else {
                   3483:                         $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                   3484:                     }
                   3485:                 } elsif ($coursefilter eq 'selected') {
                   3486:                     $description .= &mt('Selected courses in domain').' - ';
                   3487:                 } elsif ($coursefilter eq 'all') {
1.2       raeburn  3488:                     $description .= &mt('All courses in domain').' - ';
                   3489:                 }
1.102     raeburn  3490:             } elsif ($env{'form.roletype'} eq 'community') {
                   3491:                 if ($coursefilter eq 'selected') {
                   3492:                     $description .= &mt('Selected communities in domain').' - ';
                   3493:                 } elsif ($coursefilter eq 'all') {
                   3494:                     $description .= &mt('All communities in domain').' - ';
                   3495:                 }
1.2       raeburn  3496:             }
                   3497:             if ($statusmode eq 'Expired') {
1.5       raeburn  3498:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  3499:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3500:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  3501:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3502:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  3503:             } else {
                   3504:                 if ($rolefilter eq 'Any') {
                   3505:                     $description .= &mt('all users');
                   3506:                 } else {
                   3507:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   3508:                 }
                   3509:             }
                   3510:         }
                   3511:     }
                   3512:     return $description;
                   3513: }
1.22      raeburn  3514: 
                   3515: sub viewable_section {
                   3516:     my ($permission) = @_;
                   3517:     my $viewablesec;
                   3518:     if (ref($permission) eq 'HASH') {
                   3519:         if (exists($permission->{'view_section'})) {
                   3520:             $viewablesec = $permission->{'view_section'};
                   3521:         } elsif (exists($permission->{'cusr_section'})) {
                   3522:             $viewablesec = $permission->{'cusr_section'};
                   3523:         }
                   3524:     }
                   3525:     return $viewablesec;
                   3526: }
                   3527: 
1.2       raeburn  3528:     
1.1       raeburn  3529: #################################################
                   3530: #################################################
                   3531: sub show_drop_list {
1.101     raeburn  3532:     my ($r,$classlist,$nosort,$permission,$crstype) = @_;
1.29      raeburn  3533:     my $cid = $env{'request.course.id'};
1.17      raeburn  3534:     my ($cnum,$cdom) = &get_course_identity($cid);
1.1       raeburn  3535:     if (! exists($env{'form.sortby'})) {
                   3536:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   3537:                                                 ['sortby']);
                   3538:     }
                   3539:     my $sortby = $env{'form.sortby'};
                   3540:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   3541:         $sortby = 'username';
                   3542:     }
                   3543:     my $action = "drop";
1.17      raeburn  3544:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  3545:     $r->print(<<END);
                   3546: <input type="hidden" name="sortby" value="$sortby" />
                   3547: <input type="hidden" name="action" value="$action" />
                   3548: <input type="hidden" name="state"  value="done" />
1.17      raeburn  3549: <script type="text/javascript" language="Javascript">
1.96      bisitz   3550: // <![CDATA[
1.17      raeburn  3551: $check_uncheck_js
1.96      bisitz   3552: // ]]>
1.1       raeburn  3553: </script>
                   3554: <p>
1.86      bisitz   3555: <input type="hidden" name="phase" value="four" />
1.1       raeburn  3556: END
1.30      raeburn  3557:     my ($indexhash,$keylist) = &make_keylist_array();
                   3558:     my $studentcount = 0;
                   3559:     if (ref($classlist) eq 'HASH') {
                   3560:         foreach my $student (keys(%{$classlist})) {
                   3561:             my $sdata = $classlist->{$student}; 
                   3562:             my $status = $sdata->[$indexhash->{'status'}];
                   3563:             my $section = $sdata->[$indexhash->{'section'}];
                   3564:             if ($status ne 'Active') {
                   3565:                 delete($classlist->{$student});
                   3566:                 next;
                   3567:             }
                   3568:             if ($env{'request.course.sec'} ne '') {
                   3569:                 if ($section ne $env{'request.course.sec'}) {
                   3570:                     delete($classlist->{$student});
                   3571:                     next;
                   3572:                 }
                   3573:             }
                   3574:             $studentcount ++;
                   3575:         }
                   3576:     }
                   3577:     if (!$studentcount) {
1.101     raeburn  3578:         if ($crstype eq 'Community') {
                   3579:             $r->print(&mt('There are no members to drop.'));
                   3580:         } else {
                   3581:             $r->print(&mt('There are no students to drop.'));
                   3582:         }
1.30      raeburn  3583:         return;
                   3584:     }
                   3585:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   3586:                                               $classlist,$keylist,$cdom,$cnum);
                   3587:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   3588:                                        'dom'    => "domain",
                   3589:                                        'sn'     => "student name",
1.101     raeburn  3590:                                        'mn'     => "member name",
1.30      raeburn  3591:                                        'sec'    => "section",
                   3592:                                        'start'  => "start date",
                   3593:                                        'end'    => "end date",
                   3594:                                        'groups' => "active groups",
                   3595:                                       );
1.101     raeburn  3596:     my $nametitle = $lt{'sn'};
                   3597:     if ($crstype eq 'Community') {
                   3598:         $nametitle = $lt{'mn'};
                   3599:     }
1.1       raeburn  3600:     if ($nosort) {
1.17      raeburn  3601:         $r->print(&Apache::loncommon::start_data_table().
                   3602:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3603:         $r->print(<<END);
                   3604:     <th>&nbsp;</th>
                   3605:     <th>$lt{'usrn'}</th>
                   3606:     <th>$lt{'dom'}</th>
                   3607:     <th>ID</th>
1.101     raeburn  3608:     <th>$nametitle</th>
1.1       raeburn  3609:     <th>$lt{'sec'}</th>
                   3610:     <th>$lt{'start'}</th>
                   3611:     <th>$lt{'end'}</th>
                   3612:     <th>$lt{'groups'}</th>
                   3613: END
1.17      raeburn  3614:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3615:     } else  {
1.17      raeburn  3616:         $r->print(&Apache::loncommon::start_data_table().
                   3617:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3618:         $r->print(<<END);
1.17      raeburn  3619:     <th>&nbsp;</th>
1.1       raeburn  3620:     <th>
1.17      raeburn  3621:        <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1       raeburn  3622:     </th><th>
1.17      raeburn  3623:        <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1       raeburn  3624:     </th><th>
1.17      raeburn  3625:        <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1       raeburn  3626:     </th><th>
1.101     raeburn  3627:        <a href="/adm/createuser?action=$action&sortby=fullname">$nametitle</a>
1.1       raeburn  3628:     </th><th>
1.17      raeburn  3629:        <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1       raeburn  3630:     </th><th>
1.17      raeburn  3631:        <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1       raeburn  3632:     </th><th>
1.17      raeburn  3633:        <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1       raeburn  3634:     </th><th>
1.17      raeburn  3635:        <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
1.1       raeburn  3636:     </th>
                   3637: END
1.17      raeburn  3638:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3639:     }
                   3640:     #
                   3641:     # Sort the students
1.30      raeburn  3642:     my $index  = $indexhash->{$sortby};
                   3643:     my $second = $indexhash->{'username'};
                   3644:     my $third  = $indexhash->{'domain'};
1.1       raeburn  3645:     my @Sorted_Students = sort {
                   3646:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   3647:             ||
                   3648:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   3649:             ||
                   3650:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  3651:         } (keys(%{$classlist}));
1.1       raeburn  3652:     foreach my $student (@Sorted_Students) {
                   3653:         my $error;
                   3654:         my $sdata = $classlist->{$student};
1.30      raeburn  3655:         my $username = $sdata->[$indexhash->{'username'}];
                   3656:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   3657:         my $section  = $sdata->[$indexhash->{'section'}];
                   3658:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   3659:         my $id       = $sdata->[$indexhash->{'id'}];
                   3660:         my $start    = $sdata->[$indexhash->{'start'}];
                   3661:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  3662:         my $groups = $classgroups->{$student};
                   3663:         my $active_groups;
                   3664:         if (ref($groups->{active}) eq 'HASH') {
                   3665:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   3666:         }
                   3667:         if (! defined($start) || $start == 0) {
                   3668:             $start = &mt('none');
                   3669:         } else {
                   3670:             $start = &Apache::lonlocal::locallocaltime($start);
                   3671:         }
                   3672:         if (! defined($end) || $end == 0) {
                   3673:             $end = &mt('none');
                   3674:         } else {
                   3675:             $end = &Apache::lonlocal::locallocaltime($end);
                   3676:         }
1.17      raeburn  3677:         my $studentkey = $student.':'.$section;
1.30      raeburn  3678:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  3679:         #
                   3680:         $r->print(&Apache::loncommon::start_data_table_row());
                   3681:         $r->print(<<"END");
1.86      bisitz   3682:     <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
1.1       raeburn  3683:     <td>$username</td>
                   3684:     <td>$domain</td>
                   3685:     <td>$id</td>
                   3686:     <td>$name</td>
                   3687:     <td>$section</td>
1.29      raeburn  3688:     <td>$start $startitem</td>
1.1       raeburn  3689:     <td>$end</td>
                   3690:     <td>$active_groups</td>
                   3691: END
                   3692:         $r->print(&Apache::loncommon::end_data_table_row());
                   3693:     }
                   3694:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3695:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  3696:                        'dp'   => "Drop Students",
1.101     raeburn  3697:                        'dm'   => "Drop Members",
1.1       raeburn  3698:                        'ca'   => "check all",
                   3699:                        'ua'   => "uncheck all",
                   3700:                                        );
1.101     raeburn  3701:     my $btn = $lt{'dp'};
                   3702:     if ($crstype eq 'Community') {
                   3703:         $btn = $lt{'dm'}; 
                   3704:     }
1.1       raeburn  3705:     $r->print(<<"END");
1.89      bisitz   3706: </p>
                   3707: <p>
1.86      bisitz   3708: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" /> &nbsp;
                   3709: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
1.89      bisitz   3710: </p>
                   3711: <p>
1.101     raeburn  3712: <input type="submit" value="$btn" />
1.89      bisitz   3713: </p>
1.1       raeburn  3714: END
                   3715:     return;
                   3716: }
                   3717: 
                   3718: #
                   3719: # Print out the initial form to get the file containing a list of users
                   3720: #
                   3721: sub print_first_users_upload_form {
                   3722:     my ($r,$context) = @_;
                   3723:     my $str;
1.86      bisitz   3724:     $str  = '<input type="hidden" name="phase" value="two" />';
1.1       raeburn  3725:     $str .= '<input type="hidden" name="action" value="upload" />';
1.101     raeburn  3726:     $str .= '<input type="hidden" name="state"  value="got_file" />';
1.95      bisitz   3727: 
1.85      bisitz   3728:     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
1.95      bisitz   3729: 
                   3730:     # Excel and CSV Help
1.107     raeburn  3731:     $str .= '<div class="LC_left_float">'
1.95      bisitz   3732:            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   3733:                 &mt("How do I create a users list from a spreadsheet"))
1.107     raeburn  3734:            .'</div><div class="LC_left_float">'."\n"
1.95      bisitz   3735:            .&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   3736:                 &mt("How do I create a CSV file from a spreadsheet"))
1.107     raeburn  3737:            .'</div><br clear="all" />'."\n";
1.95      bisitz   3738:     $str .= &Apache::lonhtmlcommon::start_pick_box()
1.103     raeburn  3739:            .&Apache::lonhtmlcommon::row_title(&mt('File'));
                   3740:     if (&Apache::lonlocal::current_language() ne 'en') {
                   3741:         if ($context eq 'course') { 
                   3742:             $str .= '<p class="LC_info">'."\n"
                   3743:                    .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
                   3744:                    .'</p>'."\n";
                   3745:         }
                   3746:     }
                   3747:     $str .= &Apache::loncommon::upfile_select_html()
1.95      bisitz   3748:            .&Apache::lonhtmlcommon::row_closure()
                   3749:            .&Apache::lonhtmlcommon::row_title(
                   3750:                 '<label for="noFirstLine">'
                   3751:                .&mt('Ignore First Line')
                   3752:                .'</label>')
                   3753:            .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
                   3754:            .&Apache::lonhtmlcommon::row_closure(1)
                   3755:            .&Apache::lonhtmlcommon::end_pick_box();
                   3756: 
                   3757:     $str .= '<p>'
                   3758:            .'<input type="submit" name="fileupload" value="'.&mt('Next').'" />'
                   3759:            .'</p>';
                   3760: 
1.1       raeburn  3761:     $r->print($str);
                   3762:     return;
                   3763: }
                   3764: 
                   3765: # ================================================= Drop/Add from uploaded file
                   3766: sub upfile_drop_add {
1.21      raeburn  3767:     my ($r,$context,$permission) = @_;
1.1       raeburn  3768:     &Apache::loncommon::load_tmp_file($r);
                   3769:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   3770:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   3771:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   3772:     my %fields=();
                   3773:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   3774:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   3775:             if ($env{'form.f'.$i} ne 'none') {
                   3776:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   3777:             }
                   3778:         } else {
                   3779:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   3780:         }
                   3781:     }
1.29      raeburn  3782:     if ($env{'form.fullup'} ne 'yes') {
                   3783:         $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   3784:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
                   3785:     }
1.1       raeburn  3786:     #
                   3787:     # Store the field choices away
                   3788:     foreach my $field (qw/username names
1.57      raeburn  3789:                        fname mname lname gen id sec ipwd email role domain/) {
1.1       raeburn  3790:         $env{'form.'.$field.'_choice'}=$fields{$field};
                   3791:     }
                   3792:     &Apache::loncommon::store_course_settings('enrollment_upload',
                   3793:                                               { 'username_choice' => 'scalar',
                   3794:                                                 'names_choice' => 'scalar',
                   3795:                                                 'fname_choice' => 'scalar',
                   3796:                                                 'mname_choice' => 'scalar',
                   3797:                                                 'lname_choice' => 'scalar',
                   3798:                                                 'gen_choice' => 'scalar',
                   3799:                                                 'id_choice' => 'scalar',
                   3800:                                                 'sec_choice' => 'scalar',
                   3801:                                                 'ipwd_choice' => 'scalar',
                   3802:                                                 'email_choice' => 'scalar',
1.57      raeburn  3803:                                                 'role_choice'  => 'scalar',
1.84      raeburn  3804:                                                 'domain_choice' => 'scalar',
                   3805:                                                 'inststatus_choice' => 'scalar'});
1.1       raeburn  3806:     #
1.101     raeburn  3807:     my ($cid,$crstype,$setting);
                   3808:     if ($context eq 'domain') {
                   3809:         $setting = $env{'form.roleaction'};
                   3810:     }
                   3811:     if ($env{'request.course.id'} ne '') {
                   3812:         $cid = $env{'request.course.id'};
                   3813:         $crstype = &Apache::loncommon::course_type();
                   3814:     } elsif ($setting eq 'course') {
                   3815:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   3816:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
                   3817:             $crstype = &Apache::loncommon::course_type($cid);
                   3818:         }
                   3819:     }
1.1       raeburn  3820:     my ($startdate,$enddate) = &get_dates_from_form();
                   3821:     if ($env{'form.makedatesdefault'}) {
1.101     raeburn  3822:         $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  3823:     }
                   3824:     # Determine domain and desired host (home server)
1.57      raeburn  3825:     my $defdom=$env{'request.role.domain'};
                   3826:     my $domain;
                   3827:     if ($env{'form.defaultdomain'} ne '') {
                   3828:         $domain = $env{'form.defaultdomain'};
                   3829:     } else {
                   3830:         $domain = $defdom;
                   3831:     }
1.1       raeburn  3832:     my $desiredhost = $env{'form.lcserver'};
                   3833:     if (lc($desiredhost) eq 'default') {
                   3834:         $desiredhost = undef;
                   3835:     } else {
1.57      raeburn  3836:         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
1.1       raeburn  3837:         if (! exists($home_servers{$desiredhost})) {
                   3838:             $r->print('<span class="LC_error">'.&mt('Error').
                   3839:                       &mt('Invalid home server specified').'</span>');
                   3840:             $r->print(&Apache::loncommon::end_page());
                   3841:             return;
                   3842:         }
                   3843:     }
                   3844:     # Determine authentication mechanism
                   3845:     my $changeauth;
                   3846:     if ($context eq 'domain') {
                   3847:         $changeauth = $env{'form.changeauth'};
                   3848:     }
                   3849:     my $amode  = '';
                   3850:     my $genpwd = '';
                   3851:     if ($env{'form.login'} eq 'krb') {
                   3852:         $amode='krb';
                   3853:         $amode.=$env{'form.krbver'};
                   3854:         $genpwd=$env{'form.krbarg'};
                   3855:     } elsif ($env{'form.login'} eq 'int') {
                   3856:         $amode='internal';
                   3857:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   3858:             $genpwd=$env{'form.intarg'};
                   3859:         }
                   3860:     } elsif ($env{'form.login'} eq 'loc') {
                   3861:         $amode='localauth';
                   3862:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   3863:             $genpwd=$env{'form.locarg'};
                   3864:         }
                   3865:     }
                   3866:     if ($amode =~ /^krb/) {
                   3867:         if (! defined($genpwd) || $genpwd eq '') {
                   3868:             $r->print('<span class="Error">'.
                   3869:                       &mt('Unable to enroll users').' '.
                   3870:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   3871:             $amode = ''; # This causes the loop below to be skipped
                   3872:         }
                   3873:     }
1.101     raeburn  3874:     my ($defaultsec,$defaultrole);
1.1       raeburn  3875:     if ($context eq 'domain') {
                   3876:         if ($setting eq 'domain') {
                   3877:             $defaultrole = $env{'form.defaultrole'};
                   3878:         } elsif ($setting eq 'course') {
                   3879:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  3880:             $defaultsec = $env{'form.sections'};
1.1       raeburn  3881:         }  
1.13      raeburn  3882:     } elsif ($context eq 'author') {
1.1       raeburn  3883:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  3884:     } elsif ($context eq 'course') {
                   3885:         $defaultrole = $env{'form.defaultrole'};
                   3886:         $defaultsec = $env{'form.sections'};
1.1       raeburn  3887:     }
1.27      raeburn  3888:     # Check to see if user information can be changed
                   3889:     my @userinfo = ('firstname','middlename','lastname','generation',
                   3890:                     'permanentemail','id');
                   3891:     my %canmodify;
                   3892:     if (&Apache::lonnet::allowed('mau',$domain)) {
1.84      raeburn  3893:         push(@userinfo,'inststatus');
1.27      raeburn  3894:         foreach my $field (@userinfo) {
                   3895:             $canmodify{$field} = 1;
                   3896:         }
                   3897:     }
                   3898:     my (%userlist,%modifiable_fields,@poss_roles);
                   3899:     my $secidx = &Apache::loncoursedata::CL_SECTION();
1.102     raeburn  3900:     my @courseroles = &roles_by_context('course',1,$crstype);
1.27      raeburn  3901:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   3902:         if ($context eq 'course' || $context eq 'author') {
1.101     raeburn  3903:             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
1.27      raeburn  3904:             my @statuses = ('active','future');
                   3905:             my ($indexhash,$keylist) = &make_keylist_array();
                   3906:             my %info;
                   3907:             foreach my $role (@poss_roles) {
                   3908:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   3909:                                                         \@userinfo,[$role]);
                   3910:             }
                   3911:             if ($context eq 'course') {
                   3912:                 my ($cnum,$cdom) = &get_course_identity();
                   3913:                 my $roster = &Apache::loncoursedata::get_classlist();
1.66      raeburn  3914:                 if (ref($roster) eq 'HASH') {
                   3915:                     %userlist = %{$roster};
                   3916:                 }
1.27      raeburn  3917:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   3918:                                                          \@statuses,\@poss_roles);
                   3919:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3920:                                 \%advrolehash,$permission);
                   3921:             } elsif ($context eq 'author') {
                   3922:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   3923:                                                   \@statuses,\@poss_roles);
                   3924:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3925:                              \%cstr_roles,$permission);
                   3926: 
                   3927:             }
                   3928:         }
1.1       raeburn  3929:     }
                   3930:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   3931:         && ($amode ne '')) {
                   3932:         #######################################
                   3933:         ##         Add/Modify Users          ##
                   3934:         #######################################
                   3935:         if ($context eq 'course') {
                   3936:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  3937:         } elsif ($context eq 'author') {
1.1       raeburn  3938:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   3939:         } else {
                   3940:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   3941:         }
1.87      bisitz   3942:         $r->rflush;
                   3943: 
1.1       raeburn  3944:         my %counts = (
                   3945:                        user => 0,
                   3946:                        auth => 0,
                   3947:                        role => 0,
                   3948:                      );
                   3949:         my $flushc=0;
                   3950:         my %student=();
1.42      raeburn  3951:         my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
1.1       raeburn  3952:         my %userchg;
1.27      raeburn  3953:         if ($context eq 'course' || $setting eq 'course') {
                   3954:             if ($context eq 'course') {
                   3955:                 # Get information about course groups
                   3956:                 %curr_groups = &Apache::longroup::coursegroups();
                   3957:             } elsif ($setting eq 'course') {
                   3958:                 if ($cid) {
                   3959:                     %curr_groups =
                   3960:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   3961:                                                         $env{'form.dccourse'});
                   3962:                 }
                   3963:             }
                   3964:             # determine section number
                   3965:             if ($defaultsec =~ /,/) {
                   3966:                 push(@sections,split(/,/,$defaultsec));
                   3967:             } else {
                   3968:                 push(@sections,$defaultsec);
                   3969:             }
                   3970:             # remove non alphanumeric values from section
                   3971:             foreach my $item (@sections) {
                   3972:                 $item =~ s/\W//g;
                   3973:                 if ($item eq "none" || $item eq 'all') {
                   3974:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   3975:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   3976:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   3977:                 } elsif ($item ne '') {
                   3978:                     push(@cleansec,$item);
                   3979:                 }
                   3980:             }
                   3981:             if ($defaultwarn) {
                   3982:                 $r->print($defaultwarn.'<br />');
                   3983:             }
                   3984:             if ($groupwarn) {
                   3985:                 $r->print($groupwarn.'<br />');
                   3986:             }
1.1       raeburn  3987:         }
1.124     raeburn  3988:         my (%curr_rules,%got_rules,%alerts,%cancreate);
1.104     raeburn  3989:         my %customroles = &my_custom_roles($crstype);
1.101     raeburn  3990:         my @permitted_roles = 
1.124     raeburn  3991:             &roles_on_upload($context,$setting,$crstype,%customroles);
                   3992:         my %longtypes = &Apache::lonlocal::texthash(
                   3993:                             official   => 'Institutional',
                   3994:                             unofficial => 'Non-institutional',
                   3995:                         );
1.135     raeburn  3996:         my $newuserdom = $env{'request.role.domain'};
                   3997:         map { $cancreate{$_} = &can_create_user($newuserdom,$context,$_); } keys(%longtypes);
1.1       raeburn  3998:         # Get new users list
1.27      raeburn  3999:         foreach my $line (@userdata) {
1.42      raeburn  4000:             my @secs;
1.27      raeburn  4001:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4002:             # Determine user name
1.128     raeburn  4003:             $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4004:             unless (($entries{$fields{'username'}} eq '') ||
                   4005:                     (!defined($entries{$fields{'username'}}))) {
                   4006:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   4007:                 if (defined($fields{'names'})) {
                   4008:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   4009:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   4010:                 } else {
                   4011:                     if (defined($fields{'fname'})) {
                   4012:                         $fname=$entries{$fields{'fname'}};
                   4013:                     }
                   4014:                     if (defined($fields{'mname'})) {
                   4015:                         $mname=$entries{$fields{'mname'}};
                   4016:                     }
                   4017:                     if (defined($fields{'lname'})) {
                   4018:                         $lname=$entries{$fields{'lname'}};
                   4019:                     }
                   4020:                     if (defined($fields{'gen'})) {
                   4021:                         $gen=$entries{$fields{'gen'}};
                   4022:                     }
                   4023:                 }
1.128     raeburn  4024: 
1.1       raeburn  4025:                 if ($entries{$fields{'username'}}
                   4026:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
1.128     raeburn  4027:                     my $nowhitespace;
                   4028:                     if ($entries{$fields{'username'}} =~ /\s/) {
                   4029:                         $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
                   4030:                     }
1.1       raeburn  4031:                     $r->print('<br />'.
1.74      bisitz   4032:       &mt('[_1]: Unacceptable username for user [_2] [_3] [_4] [_5]',
1.128     raeburn  4033:           '<b>'.$entries{$fields{'username'}}.'</b>',$fname,$mname,$lname,$gen).
                   4034:                               $nowhitespace);
1.27      raeburn  4035:                     next;
1.1       raeburn  4036:                 } else {
1.129     raeburn  4037:                     $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
1.71      droeschl 4038:                     if ($entries{$fields{'domain'}} 
1.57      raeburn  4039:                         ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
                   4040:                         $r->print('<br />'. '<b>'.$entries{$fields{'domain'}}.
1.77      raeburn  4041:                                   '</b>: '.&mt('Unacceptable domain for user [_2] [_3] [_4] [_5]',$fname,$mname,$lname,$gen));
1.57      raeburn  4042:                         next;
                   4043:                     }
1.5       raeburn  4044:                     my $username = $entries{$fields{'username'}};
1.57      raeburn  4045:                     my $userdomain = $entries{$fields{'domain'}};
                   4046:                     if ($userdomain eq '') {
                   4047:                         $userdomain = $domain;
                   4048:                     }
1.27      raeburn  4049:                     if (defined($fields{'sec'})) {
                   4050:                         if (defined($entries{$fields{'sec'}})) {
1.42      raeburn  4051:                             $entries{$fields{'sec'}} =~ s/\W//g;
1.27      raeburn  4052:                             my $item = $entries{$fields{'sec'}};
                   4053:                             if ($item eq "none" || $item eq 'all') {
1.74      bisitz   4054:                                 $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  4055:                                 next;
                   4056:                             } elsif (exists($curr_groups{$item})) {
1.74      bisitz   4057:                                 $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  4058:                                 next;
                   4059:                             } else {
                   4060:                                 push(@secs,$item);
                   4061:                             }
                   4062:                         }
                   4063:                     }
                   4064:                     if ($env{'request.course.sec'} ne '') {
                   4065:                         @secs = ($env{'request.course.sec'});
1.57      raeburn  4066:                         if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
                   4067:                             my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
1.27      raeburn  4068:                             if ($currsec ne $env{'request.course.sec'}) {
1.74      bisitz   4069:                                 $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  4070:                                 if ($currsec eq '') {
                   4071:                                     $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.'));
                   4072: 
                   4073:                                 } else {
                   4074:                                     $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec));
                   4075:                                 }
                   4076:                                 $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 />');
                   4077:                                 next;
1.1       raeburn  4078:                             }
                   4079:                         }
1.27      raeburn  4080:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   4081:                         if (@secs == 0) {
                   4082:                             @secs = @cleansec;
1.1       raeburn  4083:                         }
                   4084:                     }
                   4085:                     # determine id number
                   4086:                     my $id='';
                   4087:                     if (defined($fields{'id'})) {
                   4088:                         if (defined($entries{$fields{'id'}})) {
                   4089:                             $id=$entries{$fields{'id'}};
                   4090:                         }
                   4091:                         $id=~tr/A-Z/a-z/;
                   4092:                     }
                   4093:                     # determine email address
                   4094:                     my $email='';
                   4095:                     if (defined($fields{'email'})) {
1.129     raeburn  4096:                         $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4097:                         if (defined($entries{$fields{'email'}})) {
                   4098:                             $email=$entries{$fields{'email'}};
1.84      raeburn  4099:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   4100:                         }
                   4101:                     }
                   4102:                     # determine affiliation
                   4103:                     my $inststatus='';
                   4104:                     if (defined($fields{'inststatus'})) {
                   4105:                         if (defined($entries{$fields{'inststatus'}})) {
                   4106:                             $inststatus=$entries{$fields{'inststatus'}};
                   4107:                         }
1.1       raeburn  4108:                     }
                   4109:                     # determine user password
                   4110:                     my $password = $genpwd;
                   4111:                     if (defined($fields{'ipwd'})) {
                   4112:                         if ($entries{$fields{'ipwd'}}) {
                   4113:                             $password=$entries{$fields{'ipwd'}};
                   4114:                         }
                   4115:                     }
                   4116:                     # determine user role
                   4117:                     my $role = '';
                   4118:                     if (defined($fields{'role'})) {
                   4119:                         if ($entries{$fields{'role'}}) {
1.42      raeburn  4120:                             $entries{$fields{'role'}}  =~ s/(\s+$|^\s+)//g;
                   4121:                             if ($entries{$fields{'role'}} ne '') {
                   4122:                                 if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
                   4123:                                     $role = $entries{$fields{'role'}};
1.27      raeburn  4124:                                 }
                   4125:                             }
                   4126:                             if ($role eq '') {
                   4127:                                 my $rolestr = join(', ',@permitted_roles);
1.74      bisitz   4128:                                 $r->print('<br />'
                   4129:                                          .&mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
                   4130:                                              ,'<b>'.$entries{$fields{'username'}}.'</b>'
                   4131:                                              ,$entries{$fields{'role'}})
                   4132:                                          .'<br />'
                   4133:                                          .&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n"
                   4134:                                 );
1.1       raeburn  4135:                                 next;
                   4136:                             }
                   4137:                         }
                   4138:                     }
                   4139:                     if ($role eq '') {
                   4140:                         $role = $defaultrole;
                   4141:                     }
                   4142:                     # Clean up whitespace
1.129     raeburn  4143:                     foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
1.1       raeburn  4144:                         $$_ =~ s/(\s+$|^\s+)//g;
                   4145:                     }
1.5       raeburn  4146:                     # check against rules
                   4147:                     my $checkid = 0;
                   4148:                     my $newuser = 0;
                   4149:                     my (%rulematch,%inst_results,%idinst_results);
1.57      raeburn  4150:                     my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
1.5       raeburn  4151:                     if ($uhome eq 'no_host') {
1.135     raeburn  4152:                         if ($userdomain ne $newuserdom) {
                   4153:                             if ($context eq 'course') {
                   4154:                                 $r->print('<br />'.
                   4155:                                           &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
                   4156:                                           '<b>'.$username.'</b>',$userdomain).'<br />');
                   4157:                             } elsif ($context eq 'author') {
                   4158:                                 $r->print(&mt('[_1]: The domain specified ([_2]) is different to that of the author.',
                   4159:                                         '<b>'.$username.'</b>',$userdomain).'<br />'); 
                   4160:                             } else {
                   4161:                                 $r->print(&mt('[_1]: The domain specified ([_2]) is different to that of your current role.',
                   4162:                                         '<b>'.$username.'</b>',$userdomain).'<br />');
                   4163:                             }
                   4164:                             $r->print(&mt('The user does not already exist, and you may not create a new user in a different domain.'));
1.124     raeburn  4165:                             next;
                   4166:                         }
1.5       raeburn  4167:                         $checkid = 1;
                   4168:                         $newuser = 1;
1.135     raeburn  4169:                         my $user = $username.':'.$newuserdom;
1.5       raeburn  4170:                         my $checkhash;
                   4171:                         my $checks = { 'username' => 1 };
1.135     raeburn  4172:                         $checkhash->{$username.':'.$newuserdom} = { 'newuser' => 1, };
1.5       raeburn  4173:                         &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4174:                             \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                   4175:                             \%got_rules);
                   4176:                         if (ref($alerts{'username'}) eq 'HASH') {
1.135     raeburn  4177:                             if (ref($alerts{'username'}{$newuserdom}) eq 'HASH') {
                   4178:                                 if ($alerts{'username'}{$newuserdom}{$username}) {
1.124     raeburn  4179:                                     $r->print('<br />'.
                   4180:                                               &mt('[_1]: matches the username format at your institution, but is not known to your directory service.','<b>'.$username.'</b>').'<br />'.
                   4181:                                               &mt('Consequently, the user was not created.'));
                   4182:                                     next;
                   4183:                                 }
                   4184:                             }
                   4185:                         }
                   4186:                         my $usertype = 'unofficial';
                   4187:                         if (ref($rulematch{$user}) eq 'HASH') {
                   4188:                             if ($rulematch{$user}{'username'}) {
                   4189:                                 $usertype = 'official';
1.5       raeburn  4190:                             }
                   4191:                         }
1.124     raeburn  4192:                         unless ($cancreate{$usertype}) {
                   4193:                             my $showtype = $longtypes{$usertype};
                   4194:                             $r->print('<br />'.
1.133     raeburn  4195:                                       &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  4196:                             next;
                   4197:                         }
1.13      raeburn  4198:                     } else {
1.27      raeburn  4199:                         if ($context eq 'course' || $context eq 'author') {
1.57      raeburn  4200:                             if ($userdomain eq $domain ) {
                   4201:                                 if ($role eq '') {
                   4202:                                     my @checkroles;
                   4203:                                     foreach my $role (@poss_roles) {
                   4204:                                         my $endkey;
                   4205:                                         if ($role ne 'st') {
                   4206:                                             $endkey = ':'.$role;
                   4207:                                         }
                   4208:                                         if (exists($userlist{$username.':'.$userdomain.$endkey})) {
                   4209:                                             if (!grep(/^\Q$role\E$/,@checkroles)) {
                   4210:                                                 push(@checkroles,$role);
                   4211:                                             }
                   4212:                                         }
1.27      raeburn  4213:                                     }
1.57      raeburn  4214:                                     if (@checkroles > 0) {
                   4215:                                         %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
1.27      raeburn  4216:                                     }
1.57      raeburn  4217:                                 } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   4218:                                     %canmodify = %{$modifiable_fields{$role}};
1.27      raeburn  4219:                                 }
                   4220:                             }
1.57      raeburn  4221:                             my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
1.84      raeburn  4222:                             for (my $i=0; $i<@newinfo; $i++) {
1.57      raeburn  4223:                                 if (${$newinfo[$i]} ne '') {
                   4224:                                     if (!$canmodify{$userinfo[$i]}) {
                   4225:                                         ${$newinfo[$i]} = '';
                   4226:                                     }
1.27      raeburn  4227:                                 }
                   4228:                             }
                   4229:                         }
1.5       raeburn  4230:                     }
                   4231:                     if ($id ne '') {
                   4232:                         if (!$newuser) {
1.57      raeburn  4233:                             my %idhash = &Apache::lonnet::idrget($userdomain,($username));
1.5       raeburn  4234:                             if ($idhash{$username} ne $id) {
                   4235:                                 $checkid = 1;
                   4236:                             }
                   4237:                         }
                   4238:                         if ($checkid) {
                   4239:                             my $checkhash;
                   4240:                             my $checks = { 'id' => 1 };
1.57      raeburn  4241:                             $checkhash->{$username.':'.$userdomain} = { 'newuser' => $newuser,
1.5       raeburn  4242:                                                                     'id'  => $id };
                   4243:                             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4244:                                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
                   4245:                                 \%got_rules);
                   4246:                             if (ref($alerts{'id'}) eq 'HASH') {
1.57      raeburn  4247:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
1.124     raeburn  4248:                                     if ($alerts{'id'}{$userdomain}{$id}) {
                   4249:                                         $r->print(&mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is found by your directory service.',
                   4250:                                                   '<b>'.$username.'</b>').'<br />'.
                   4251:                                                   &mt('Consequently, the user was not created.'));
                   4252:                                         next;
                   4253:                                     }
1.5       raeburn  4254:                                 }
                   4255:                             }
                   4256:                         }
                   4257:                     }
1.1       raeburn  4258:                     if ($password || $env{'form.login'} eq 'loc') {
1.27      raeburn  4259:                         my $multiple = 0;
                   4260:                         my ($userresult,$authresult,$roleresult,$idresult);
                   4261:                         my (%userres,%authres,%roleres,%idres);
1.42      raeburn  4262:                         my $singlesec = '';
1.1       raeburn  4263:                         if ($role eq 'st') {
1.27      raeburn  4264:                             my $sec;
1.42      raeburn  4265:                             if (@secs > 0) {
                   4266:                                 $sec = $secs[0];
1.27      raeburn  4267:                             }
1.57      raeburn  4268:                             &modifystudent($userdomain,$username,$cid,$sec,
1.52      raeburn  4269:                                            $desiredhost,$context);
1.42      raeburn  4270:                             $roleresult =
                   4271:                                 &Apache::lonnet::modifystudent
1.57      raeburn  4272:                                     ($userdomain,$username,$id,$amode,$password,
1.42      raeburn  4273:                                      $fname,$mname,$lname,$gen,$sec,$enddate,
                   4274:                                      $startdate,$env{'form.forceid'},
1.52      raeburn  4275:                                      $desiredhost,$email,'manual','',$cid,
1.84      raeburn  4276:                                      '',$context,$inststatus);
1.42      raeburn  4277:                             $userresult = $roleresult;
1.1       raeburn  4278:                         } else {
1.42      raeburn  4279:                             if ($role ne '') { 
                   4280:                                 if ($context eq 'course' || $setting eq 'course') {
                   4281:                                     if ($customroles{$role}) {
                   4282:                                         $role = 'cr_'.$env{'user.domain'}.'_'.
                   4283:                                                 $env{'user.name'}.'_'.$role;
                   4284:                                     }
1.103     raeburn  4285:                                     if (($role ne 'cc') && ($role ne 'co')) { 
1.42      raeburn  4286:                                         if (@secs > 1) {
                   4287:                                             $multiple = 1;
                   4288:                                             foreach my $sec (@secs) {
                   4289:                                                 ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   4290:                                                 &modifyuserrole($context,$setting,
1.57      raeburn  4291:                                                     $changeauth,$cid,$userdomain,$username,
1.42      raeburn  4292:                                                     $id,$amode,$password,$fname,
                   4293:                                                     $mname,$lname,$gen,$sec,
                   4294:                                                     $env{'form.forceid'},$desiredhost,
                   4295:                                                     $email,$role,$enddate,
1.84      raeburn  4296:                                                     $startdate,$checkid,$inststatus);
1.42      raeburn  4297:                                             }
                   4298:                                         } elsif (@secs > 0) {
                   4299:                                             $singlesec = $secs[0];
                   4300:                                         }
1.27      raeburn  4301:                                     }
                   4302:                                 }
                   4303:                             }
                   4304:                             if (!$multiple) {
1.28      raeburn  4305:                                 ($userresult,$authresult,$roleresult,$idresult) = 
1.27      raeburn  4306:                                     &modifyuserrole($context,$setting,
1.57      raeburn  4307:                                                     $changeauth,$cid,$userdomain,$username, 
1.42      raeburn  4308:                                                     $id,$amode,$password,$fname,
                   4309:                                                     $mname,$lname,$gen,$singlesec,
                   4310:                                                     $env{'form.forceid'},$desiredhost,
1.84      raeburn  4311:                                                     $email,$role,$enddate,$startdate,
                   4312:                                                     $checkid,$inststatus);
1.27      raeburn  4313:                             }
                   4314:                         }
                   4315:                         if ($multiple) {
                   4316:                             foreach my $sec (sort(keys(%userres))) {
1.42      raeburn  4317:                                 $flushc =
1.27      raeburn  4318:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   4319:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
1.57      raeburn  4320:                                                     $username,$userdomain,\%userchg);
1.27      raeburn  4321: 
                   4322:                             }
                   4323:                         } else {
                   4324:                             $flushc = 
                   4325:                                 &user_change_result($r,$userresult,$authresult,
1.28      raeburn  4326:                                                     $roleresult,$idresult,\%counts,$flushc,
1.57      raeburn  4327:                                                     $username,$userdomain,\%userchg);
1.1       raeburn  4328:                         }
                   4329:                     } else {
                   4330:                         if ($context eq 'course') {
                   4331:                             $r->print('<br />'. 
1.74      bisitz   4332:       &mt('[_1]: Unable to enroll. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4333:                                      );
1.13      raeburn  4334:                         } elsif ($context eq 'author') {
1.1       raeburn  4335:                             $r->print('<br />'.
1.74      bisitz   4336:       &mt('[_1]: Unable to add co-author. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4337:                                      );
                   4338:                         } else {
                   4339:                             $r->print('<br />'.
1.74      bisitz   4340:       &mt('[_1]: Unable to add user. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4341:                                      );
                   4342:                         }
                   4343:                     }
                   4344:                 }
                   4345:             }
                   4346:         } # end of foreach (@userdata)
                   4347:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  4348:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.29      raeburn  4349:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  4350:                   "</p>\n");
                   4351:         if ($counts{'role'} > 0) {
                   4352:             $r->print("<p>\n".
1.29      raeburn  4353:                       &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");
                   4354:         } else {
                   4355:             $r->print('<p>'.&mt('No roles added').'</p>');
1.1       raeburn  4356:         }
                   4357:         if ($counts{'auth'} > 0) {
                   4358:             $r->print("<p>\n".
                   4359:                       &mt('Authentication changed for [_1] existing users.',
                   4360:                           $counts{'auth'})."</p>\n");
                   4361:         }
1.13      raeburn  4362:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1       raeburn  4363:         #####################################
1.29      raeburn  4364:         # Display list of students to drop  #
1.1       raeburn  4365:         #####################################
                   4366:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  4367:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  4368:             #  Get current classlist
1.30      raeburn  4369:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4370:             if (! defined($classlist)) {
1.29      raeburn  4371:                 $r->print('<form name="studentform" method="post" action="/adm/createuser" />'.
                   4372:                           '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                   4373:                           &mt('There are no students with current/future access to the course.').
                   4374:                           '</form>'."\n");
1.66      raeburn  4375:             } elsif (ref($classlist) eq 'HASH') {
1.1       raeburn  4376:                 # Remove the students we just added from the list of students.
1.30      raeburn  4377:                 foreach my $line (@userdata) {
                   4378:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4379:                     unless (($entries{$fields{'username'}} eq '') ||
                   4380:                             (!defined($entries{$fields{'username'}}))) {
                   4381:                         delete($classlist->{$entries{$fields{'username'}}.
                   4382:                                                 ':'.$domain});
                   4383:                     }
                   4384:                 }
                   4385:                 # Print out list of dropped students.
1.30      raeburn  4386:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  4387:             }
                   4388:         }
                   4389:     } # end of unless
1.29      raeburn  4390:     if ($env{'form.fullup'} ne 'yes') {
                   4391:         $r->print('</form>');
                   4392:     }
1.1       raeburn  4393: }
                   4394: 
1.13      raeburn  4395: sub print_namespacing_alerts {
                   4396:     my ($domain,$alerts,$curr_rules) = @_;
                   4397:     my $output;
                   4398:     if (ref($alerts) eq 'HASH') {
                   4399:         if (keys(%{$alerts}) > 0) {
                   4400:             if (ref($alerts->{'username'}) eq 'HASH') {
                   4401:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   4402:                     my $count;
                   4403:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   4404:                         $count = keys(%{$alerts->{'username'}{$dom}});
                   4405:                     }
                   4406:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4407:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4408:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4409:                                         'username',$domdesc,$count,'upload');
                   4410:                     }
                   4411:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4412:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   4413:                                    'username');
                   4414:                 }
                   4415:             }
                   4416:             if (ref($alerts->{'id'}) eq 'HASH') {
                   4417:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   4418:                     my $count;
                   4419:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   4420:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   4421:                     }
                   4422:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4423:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4424:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4425:                                               'id',$domdesc,$count,'upload');
                   4426:                     }
                   4427:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4428:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   4429:                 }
                   4430:             }
                   4431:         }
                   4432:     }
                   4433: }
                   4434: 
1.1       raeburn  4435: sub user_change_result {
1.29      raeburn  4436:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
1.57      raeburn  4437:         $username,$userdomain,$userchg) = @_;
1.1       raeburn  4438:     my $okresult = 0;
                   4439:     if ($userresult ne 'ok') {
                   4440:         if ($userresult =~ /^error:(.+)$/) {
                   4441:             my $error = $1;
                   4442:             $r->print('<br />'.
1.74      bisitz   4443:                   &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4444:         }
                   4445:     } else {
                   4446:         $counts->{'user'} ++;
                   4447:         $okresult = 1;
                   4448:     }
                   4449:     if ($authresult ne 'ok') {
                   4450:         if ($authresult =~ /^error:(.+)$/) {
                   4451:             my $error = $1;
                   4452:             $r->print('<br />'.
1.74      bisitz   4453:                   &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4454:         } 
                   4455:     } else {
                   4456:         $counts->{'auth'} ++;
                   4457:         $okresult = 1;
                   4458:     }
                   4459:     if ($roleresult ne 'ok') {
                   4460:         if ($roleresult =~ /^error:(.+)$/) {
                   4461:             my $error = $1;
                   4462:             $r->print('<br />'.
1.74      bisitz   4463:                   &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4464:         }
                   4465:     } else {
                   4466:         $counts->{'role'} ++;
                   4467:         $okresult = 1;
                   4468:     }
                   4469:     if ($okresult) {
                   4470:         $flushc++;
1.57      raeburn  4471:         $userchg->{$username.':'.$userdomain}=1;
1.1       raeburn  4472:         $r->print('. ');
                   4473:         if ($flushc>15) {
                   4474:             $r->rflush;
                   4475:             $flushc=0;
                   4476:         }
                   4477:     }
1.29      raeburn  4478:     if ($idresult) {
                   4479:         $r->print($idresult);
                   4480:     }
1.1       raeburn  4481:     return $flushc;
                   4482: }
                   4483: 
                   4484: # ========================================================= Menu Phase Two Drop
1.17      raeburn  4485: sub print_drop_menu {
1.101     raeburn  4486:     my ($r,$context,$permission,$crstype) = @_;
                   4487:     my $heading;
                   4488:     if ($crstype eq 'Community') {
                   4489:         $heading = &mt("Drop Members");
                   4490:     } else {
                   4491:         $heading = &mt("Drop Students");
                   4492:     }
                   4493:     $r->print('<h3>'.$heading.'</h3>'."\n".
1.17      raeburn  4494:               '<form name="studentform" method="post">'."\n");
1.30      raeburn  4495:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4496:     if (! defined($classlist)) {
1.101     raeburn  4497:         if ($crstype eq 'Community') {
                   4498:             $r->print(&mt('There are no members currently enrolled.')."\n");
                   4499:         } else {
                   4500:             $r->print(&mt('There are no students currently enrolled.')."\n");
                   4501:         }
1.30      raeburn  4502:     } else {
1.101     raeburn  4503:         &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
1.1       raeburn  4504:     }
1.17      raeburn  4505:     $r->print('</form>'. &Apache::loncommon::end_page());
1.1       raeburn  4506:     return;
                   4507: }
                   4508: 
                   4509: # ================================================================== Phase four
                   4510: 
1.11      raeburn  4511: sub update_user_list {
1.118     raeburn  4512:     my ($r,$context,$setting,$choice,$crstype) = @_;
1.11      raeburn  4513:     my $now = time;
1.1       raeburn  4514:     my $count=0;
1.101     raeburn  4515:     if ($context eq 'course') {
                   4516:         $crstype = &Apache::loncommon::course_type();
                   4517:     }
1.11      raeburn  4518:     my @changelist;
1.29      raeburn  4519:     if ($choice eq 'drop') {
                   4520:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   4521:     } else {
1.11      raeburn  4522:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   4523:     }
                   4524:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   4525:                                    'delete'   => 'Deleted',
                   4526:                                    'reenable' => 'Re-enabled',
1.17      raeburn  4527:                                    'activate' => 'Activated',
                   4528:                                    'chgdates' => 'Changed Access Dates for',
1.118     raeburn  4529:                                    'chgsec'   => 'Changed section(s) for',
1.17      raeburn  4530:                                    'drop'     => 'Dropped',
1.11      raeburn  4531:                                  },
                   4532:                         error => {'revoke'    => 'revoking',
                   4533:                                   'delete'    => 'deleting',
                   4534:                                   'reenable'  => 're-enabling',
                   4535:                                   'activate'  => 'activating',
1.17      raeburn  4536:                                   'chgdates'  => 'changing access dates for',
                   4537:                                   'chgsec'    => 'changing section for',
                   4538:                                   'drop'      => 'dropping',
1.11      raeburn  4539:                                  },
                   4540:                       );
                   4541:     my ($startdate,$enddate);
                   4542:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   4543:         ($startdate,$enddate) = &get_dates_from_form();
                   4544:     }
                   4545:     foreach my $item (@changelist) {
1.118     raeburn  4546:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,
                   4547:             @sections,$scopestem,$singlesec,$showsecs,$warn_singlesec,
                   4548:             $nothingtodo,$keepnosection);
1.17      raeburn  4549:         if ($choice eq 'drop') {
                   4550:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   4551:             $role = 'st';
                   4552:             $cid = $env{'request.course.id'};
                   4553:             $scopestem = '/'.$cid;
                   4554:             $scopestem =~s/\_/\//g;
                   4555:             if ($sec eq '') {
                   4556:                 $scope = $scopestem;
                   4557:             } else {
                   4558:                 $scope = $scopestem.'/'.$sec;
                   4559:             }
                   4560:         } elsif ($context eq 'course') {
1.11      raeburn  4561:             ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
                   4562:             $cid = $env{'request.course.id'};
                   4563:             $scopestem = '/'.$cid;
                   4564:             $scopestem =~s/\_/\//g;
                   4565:             if ($sec eq '') {
                   4566:                 $scope = $scopestem;
                   4567:             } else {
                   4568:                 $scope = $scopestem.'/'.$sec;
                   4569:             }
1.13      raeburn  4570:         } elsif ($context eq 'author') {
1.11      raeburn  4571:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   4572:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   4573:         } elsif ($context eq 'domain') {
                   4574:             if ($setting eq 'domain') {
                   4575:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   4576:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  4577:             } elsif ($setting eq 'author') { 
1.11      raeburn  4578:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   4579:             } elsif ($setting eq 'course') {
                   4580:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype) = 
                   4581:                     split(/\:/,$item);
                   4582:                 $scope = '/'.$cid;
                   4583:                 $scope =~s/\_/\//g;
                   4584:                 if ($sec ne '') {
                   4585:                     $scope .= '/'.$sec;
                   4586:                 }
                   4587:             }
                   4588:         }
1.101     raeburn  4589:         my $plrole = &Apache::lonnet::plaintext($role,$crstype);
1.11      raeburn  4590:         my $start = $env{'form.'.$item.'_start'};
                   4591:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  4592:         if ($choice eq 'drop') {
                   4593:             # drop students
                   4594:             $end = $now;
                   4595:             $type = 'manual';
                   4596:             $result =
1.52      raeburn  4597:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.17      raeburn  4598:         } elsif ($choice eq 'revoke') {
                   4599:             # revoke or delete user role
1.11      raeburn  4600:             $end = $now; 
                   4601:             if ($role eq 'st') {
                   4602:                 $result = 
1.52      raeburn  4603:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4604:             } else {
                   4605:                 $result = 
1.52      raeburn  4606:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
                   4607:                                                 '','',$context);
1.11      raeburn  4608:             }
                   4609:         } elsif ($choice eq 'delete') {
                   4610:             if ($role eq 'st') {
1.52      raeburn  4611:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context);
1.29      raeburn  4612:             }
                   4613:             $result =
                   4614:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
1.52      raeburn  4615:                                             $start,1,'',$context);
1.11      raeburn  4616:         } else {
                   4617:             #reenable, activate, change access dates or change section
                   4618:             if ($choice ne 'chgsec') {
                   4619:                 $start = $startdate; 
                   4620:                 $end = $enddate;
                   4621:             }
                   4622:             if ($choice eq 'reenable') {
                   4623:                 if ($role eq 'st') {
1.52      raeburn  4624:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4625:                 } else {
                   4626:                     $result = 
                   4627:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4628:                                                     $now,'','',$context);
1.11      raeburn  4629:                 }
                   4630:             } elsif ($choice eq 'activate') {
                   4631:                 if ($role eq 'st') {
1.52      raeburn  4632:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4633:                 } else {
                   4634:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4635:                                             $now,'','',$context);
1.11      raeburn  4636:                 }
                   4637:             } elsif ($choice eq 'chgdates') {
                   4638:                 if ($role eq 'st') {
1.52      raeburn  4639:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4640:                 } else {
                   4641:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4642:                                                 $start,'','',$context);
1.11      raeburn  4643:                 }
                   4644:             } elsif ($choice eq 'chgsec') {
                   4645:                 my (@newsecs,$revresult,$nochg,@retained);
1.103     raeburn  4646:                 if (($role ne 'cc') && ($role ne 'co')) {
1.117     raeburn  4647:                     my @secs = sort(split(/,/,$env{'form.newsecs'}));
                   4648:                     if (@secs) {
                   4649:                         my %curr_groups = &Apache::longroup::coursegroups();
                   4650:                         foreach my $sec (@secs) {
                   4651:                             next if (($sec =~ /\W/) || ($sec eq 'none') ||
                   4652:                             (exists($curr_groups{$sec})));
                   4653:                             push(@newsecs,$sec);
                   4654:                         }
                   4655:                     }
1.11      raeburn  4656:                 }
                   4657:                 # remove existing section if not to be retained.   
1.118     raeburn  4658:                 if (!$env{'form.retainsec'} || ($role eq 'st')) {
1.11      raeburn  4659:                     if ($sec eq '') {
                   4660:                         if (@newsecs == 0) {
1.118     raeburn  4661:                             $result = 'ok';
1.11      raeburn  4662:                             $nochg = 1;
1.118     raeburn  4663:                             $nothingtodo = 1;
1.40      raeburn  4664:                         } else {
                   4665:                             $revresult =
                   4666:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4667:                                                             $scope,$role,
                   4668:                                                             '','',$context);
1.40      raeburn  4669:                         } 
1.11      raeburn  4670:                     } else {
1.28      raeburn  4671:                         if (@newsecs > 0) {
                   4672:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   4673:                                 push(@retained,$sec);
                   4674:                             } else {
                   4675:                                 $revresult =
                   4676:                                     &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4677:                                                                 $scope,$role,
                   4678:                                                                 '','',$context);
1.28      raeburn  4679:                             }
                   4680:                         } else {
1.11      raeburn  4681:                             $revresult =
1.28      raeburn  4682:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4683:                                                             $scope,$role,
                   4684:                                                             '','',$context);
1.11      raeburn  4685:                         }
                   4686:                     }
                   4687:                 } else {
1.28      raeburn  4688:                     if ($sec eq '') {
                   4689:                         $nochg = 1;
1.118     raeburn  4690:                         $keepnosection = 1;
                   4691:                     } else {
1.28      raeburn  4692:                         push(@retained,$sec);
                   4693:                     }
1.11      raeburn  4694:                 }
                   4695:                 # add new sections
1.118     raeburn  4696:                 my (@diffs,@shownew);
                   4697:                 if (@retained) {
                   4698:                     @diffs = &Apache::loncommon::compare_arrays(\@retained,\@newsecs);
                   4699:                 } else {
                   4700:                     @diffs = @newsecs;
                   4701:                 }
1.11      raeburn  4702:                 if (@newsecs == 0) {
1.118     raeburn  4703:                     if ($nochg) {
                   4704:                         $result = 'ok';
                   4705:                         $nothingtodo = 1;
                   4706:                     } else {
1.28      raeburn  4707:                         if ($role eq 'st') {
                   4708:                             $result = 
1.52      raeburn  4709:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context);
1.28      raeburn  4710:                         } else {
                   4711:                             my $newscope = $scopestem;
1.52      raeburn  4712:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
1.11      raeburn  4713:                         }
                   4714:                     }
1.118     raeburn  4715:                     $showsecs = &mt('No section');
                   4716:                 } elsif (@diffs == 0) {
                   4717:                     $result = 'ok';
                   4718:                     $nothingtodo = 1;
1.11      raeburn  4719:                 } else {
1.118     raeburn  4720:                     foreach my $newsec (@newsecs) {
1.11      raeburn  4721:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   4722:                             if ($role eq 'st') {
1.52      raeburn  4723:                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context);
1.118     raeburn  4724:                                 if (@newsecs > 1) {
                   4725:                                     my $showsingle; 
                   4726:                                     if ($newsec eq '') {
                   4727:                                         $showsingle = &mt('No section');
                   4728:                                     } else {
                   4729:                                         $showsingle = $newsec;
                   4730:                                     }
                   4731:                                     if ($crstype eq 'Community') {
                   4732:                                         $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>');
                   4733:                                     } else { 
                   4734:                                         $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>');
                   4735:                                     }
                   4736:                                     $showsecs = $showsingle; 
                   4737:                                     last;
                   4738:                                 } else {
                   4739:                                     if ($newsec eq '') {
                   4740:                                         $showsecs = &mt('No section');
                   4741:                                     } else {
                   4742:                                         $showsecs = $newsec;
                   4743:                                     }
                   4744:                                 }
1.11      raeburn  4745:                             } else {
                   4746:                                 my $newscope = $scopestem;
                   4747:                                 if ($newsec ne '') {
                   4748:                                    $newscope .= '/'.$newsec;
1.118     raeburn  4749:                                    push(@shownew,$newsec); 
1.11      raeburn  4750:                                 }
                   4751:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   4752:                                                         $newscope,$role,$end,$start);
1.118     raeburn  4753:                                 
1.11      raeburn  4754:                             }
                   4755:                         }
                   4756:                     }
                   4757:                 }
1.118     raeburn  4758:                 unless ($role eq 'st') {
                   4759:                     unless ($showsecs) {
                   4760:                         my @tolist = sort(@shownew,@retained);
                   4761:                         if ($keepnosection) {
                   4762:                             push(@tolist,&mt('No section'));
                   4763:                         }
                   4764:                         $showsecs = join(', ',@tolist);
                   4765:                     }
                   4766:                 }
1.11      raeburn  4767:             }
                   4768:         }
1.17      raeburn  4769:         my $extent = $scope;
                   4770:         if ($choice eq 'drop' || $context eq 'course') {
                   4771:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   4772:             if ($cdesc) {
                   4773:                 $extent = $cdesc;
                   4774:             }
                   4775:         }
1.1       raeburn  4776:         if ($result eq 'ok' || $result eq 'ok:') {
1.118     raeburn  4777:             my $dates;
                   4778:             if (($choice eq 'chgsec') || ($choice eq 'chgdates')) {
                   4779:                 $dates = &dates_feedback($start,$end,$now);
                   4780:             }
                   4781:             if ($choice eq 'chgsec') {
                   4782:                 if ($nothingtodo) {
                   4783:                     $r->print(&mt("Section assignment for role of '[_1]' in [_2] for '[_3]' unchanged.",$plrole,$extent,'<i>'.
                   4784:                           &Apache::loncommon::plainname($uname,$udom).
                   4785:                           '</i>').' ');
                   4786:                     if ($sec eq '') {
                   4787:                         $r->print(&mt('[_1]No section[_2] - [_3]','<b>','</b>',$dates));
                   4788:                     } else {
                   4789:                         $r->print(&mt('Section(s): [_1] - [_2]',
                   4790:                                       '<b>'.$showsecs.'</b>',$dates));
                   4791:                     }
                   4792:                     $r->print('<br />');
                   4793:                 } else {
                   4794:                     $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
                   4795:                         '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
                   4796:                         '<b>'.$showsecs.'</b>',$dates).'<br />');
                   4797:                    $count ++;
                   4798:                }
                   4799:                if ($warn_singlesec) {
                   4800:                    $r->print('<div class="LC_warning">'.$warn_singlesec.'</div>');
                   4801:                }
                   4802:             } elsif ($choice eq 'chgdates') {
                   4803:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent, 
1.121     raeburn  4804:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  4805:                       $dates).'<br />');
                   4806:                $count ++;
                   4807:             } else {
                   4808:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
1.121     raeburn  4809:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>').
1.118     raeburn  4810:                           '<br />');
                   4811:                 $count ++;
                   4812:             }
1.1       raeburn  4813:         } else {
                   4814:             $r->print(
1.118     raeburn  4815:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
                   4816:                     $plrole,$extent,
1.121     raeburn  4817:                     '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  4818:                     $result).'<br />');
1.11      raeburn  4819:         }
                   4820:     }
1.32      raeburn  4821:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  4822:     if ($choice eq 'drop') {
                   4823:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   4824:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   4825:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   4826:     } else {
                   4827:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   4828:             if ($env{'form.'.$item} ne '') {
                   4829:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   4830:                           '" />'."\n");
                   4831:             }
1.32      raeburn  4832:         }
                   4833:     }
1.118     raeburn  4834:     $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  4835:     if ($count > 0) {
1.17      raeburn  4836:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.74      bisitz   4837:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
1.11      raeburn  4838:         }
                   4839:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  4840:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.11      raeburn  4841:     }
                   4842:     if ($env{'form.makedatesdefault'}) {
                   4843:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.101     raeburn  4844:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  4845:         }
                   4846:     }
1.33      raeburn  4847:     my $linktext = &mt('Display User Lists');
                   4848:     if ($choice eq 'drop') {
                   4849:         $linktext = &mt('Display current class roster');
                   4850:     }
                   4851:     $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
1.1       raeburn  4852: }
                   4853: 
1.118     raeburn  4854: sub dates_feedback {
                   4855:     my ($start,$end,$now) = @_;
                   4856:     my $dates;
                   4857:     if ($start < $now) {
                   4858:         if ($end == 0) {
                   4859:             $dates .= &mt('role(s) active now; no end date');
                   4860:         } elsif ($end > $now) {
                   4861:             $dates = &mt('role(s) active now; ends [_1].',&Apache::lonlocal::locallocaltime($end));
                   4862:         } else {
                   4863:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   4864:         }
                   4865:      } else {
                   4866:         if ($end == 0 || $end > $now) {
                   4867:             $dates = &mt('future role(s); starts: [_1].',&Apache::lonlocal::locallocaltime($start));
                   4868:         } else {
                   4869:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   4870:         }
                   4871:     }
                   4872:     return $dates;
                   4873: }
                   4874: 
1.8       raeburn  4875: sub classlist_drop {
1.29      raeburn  4876:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  4877:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  4878:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.8       raeburn  4879:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
1.63      raeburn  4880:             my %user;
                   4881:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
1.8       raeburn  4882:             return &mt('Drop from classlist: [_1]',
                   4883:                        '<b>'.$result.'</b>').'<br />';
                   4884:         }
                   4885:     }
                   4886: }
                   4887: 
                   4888: sub active_student_roles {
                   4889:     my ($cnum,$cdom,$uname,$udom) = @_;
                   4890:     my %roles =
                   4891:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   4892:                                       ['future','active'],['st']);
                   4893:     return exists($roles{"$cnum:$cdom:st"});
                   4894: }
                   4895: 
1.1       raeburn  4896: sub section_check_js {
1.8       raeburn  4897:     my $groupslist= &get_groupslist();
1.1       raeburn  4898:     return <<"END";
                   4899: function validate(caller) {
1.9       raeburn  4900:     var groups = new Array($groupslist);
1.1       raeburn  4901:     var secname = caller.value;
                   4902:     if ((secname == 'all') || (secname == 'none')) {
                   4903:         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
                   4904:         return 'error';
                   4905:     }
                   4906:     if (secname != '') {
                   4907:         for (var k=0; k<groups.length; k++) {
                   4908:             if (secname == groups[k]) {
                   4909:                 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.");
                   4910:                 return 'error';
                   4911:             }
                   4912:         }
                   4913:     }
                   4914:     return 'ok';
                   4915: }
                   4916: END
                   4917: }
                   4918: 
                   4919: sub set_login {
                   4920:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   4921:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4922:     my $response;
                   4923:     my ($authnum,%can_assign) =
                   4924:         &Apache::loncommon::get_assignable_auth($dom);
                   4925:     if ($authnum) {
                   4926:         $response = &Apache::loncommon::start_data_table();
                   4927:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   4928:             $response .= &Apache::loncommon::start_data_table_row().
                   4929:                          '<td>'.$authformkrb.'</td>'.
                   4930:                          &Apache::loncommon::end_data_table_row()."\n";
                   4931:         }
                   4932:         if ($can_assign{'int'}) {
                   4933:             $response .= &Apache::loncommon::start_data_table_row().
                   4934:                          '<td>'.$authformint.'</td>'.
                   4935:                          &Apache::loncommon::end_data_table_row()."\n"
                   4936:         }
                   4937:         if ($can_assign{'loc'}) {
                   4938:             $response .= &Apache::loncommon::start_data_table_row().
                   4939:                          '<td>'.$authformloc.'</td>'.
                   4940:                          &Apache::loncommon::end_data_table_row()."\n";
                   4941:         }
                   4942:         $response .= &Apache::loncommon::end_data_table();
                   4943:     }
                   4944:     return $response;
                   4945: }
                   4946: 
1.8       raeburn  4947: sub course_sections {
1.51      raeburn  4948:     my ($sections_count,$role,$current_sec) = @_;
1.8       raeburn  4949:     my $output = '';
                   4950:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
1.29      raeburn  4951:     my $numsec = scalar(@sections);
1.92      bisitz   4952:     my $is_selected = ' selected="selected"';
1.29      raeburn  4953:     if ($numsec <= 1) {
1.8       raeburn  4954:         $output = '<select name="currsec_'.$role.'" >'."\n".
1.51      raeburn  4955:                   '  <option value="">'.&mt('Select').'</option>'."\n";
                   4956:         if ($current_sec eq 'none') {
                   4957:             $output .=       
                   4958:                   '  <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
                   4959:         } else {
                   4960:             $output .=
1.29      raeburn  4961:                   '  <option value="">'.&mt('No section').'</option>'."\n";
1.51      raeburn  4962:         }
1.29      raeburn  4963:         if ($numsec == 1) {
1.51      raeburn  4964:             if ($current_sec eq $sections[0]) {
                   4965:                 $output .=
                   4966:                   '  <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
                   4967:             } else {
                   4968:                 $output .=  
1.8       raeburn  4969:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.51      raeburn  4970:             }
1.29      raeburn  4971:         }
1.8       raeburn  4972:     } else {
                   4973:         $output = '<select name="currsec_'.$role.'" ';
                   4974:         my $multiple = 4;
                   4975:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  4976:         if ($role eq 'st') {
                   4977:             $output .= '>'."\n".
1.51      raeburn  4978:                        '  <option value="">'.&mt('Select').'</option>'."\n";
                   4979:             if ($current_sec eq 'none') {
                   4980:                 $output .= 
                   4981:                        '  <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
                   4982:             } else {
                   4983:                 $output .=
1.29      raeburn  4984:                        '  <option value="">'.&mt('No section')."</option>\n";
1.51      raeburn  4985:             }
1.29      raeburn  4986:         } else {
                   4987:             $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
                   4988:         }
1.8       raeburn  4989:         foreach my $sec (@sections) {
1.51      raeburn  4990:             if ($current_sec eq $sec) {
                   4991:                 $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
                   4992:             } else {
                   4993:                 $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   4994:             }
1.8       raeburn  4995:         }
                   4996:     }
                   4997:     $output .= '</select>';
                   4998:     return $output;
                   4999: }
                   5000: 
                   5001: sub get_groupslist {
                   5002:     my $groupslist;
                   5003:     my %curr_groups = &Apache::longroup::coursegroups();
                   5004:     if (%curr_groups) {
                   5005:         $groupslist = join('","',sort(keys(%curr_groups)));
                   5006:         $groupslist = '"'.$groupslist.'"';
                   5007:     }
1.11      raeburn  5008:     return $groupslist; 
1.8       raeburn  5009: }
                   5010: 
                   5011: sub setsections_javascript {
1.103     raeburn  5012:     my ($formname,$groupslist,$mode,$checkauth,$crstype) = @_;
1.28      raeburn  5013:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   5014:     if ($mode eq 'upload') {
                   5015:         $checkincluded = 'formname.name == "'.$formname.'"';
                   5016:         $finish = "return 'ok';";
                   5017:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   5018:     } elsif ($formname eq 'cu') {
1.8       raeburn  5019:         $checkincluded = 'formname.elements[i-1].checked == true';
1.37      raeburn  5020:         if ($checkauth) {
                   5021:             $finish = "var authcheck = auth_check();\n".
                   5022:                       "   if (authcheck == 'ok') {\n".
                   5023:                       "       formname.submit();\n".
                   5024:                       "   }\n";
                   5025:         } else {
                   5026:             $finish = 'formname.submit()';
                   5027:         }
1.28      raeburn  5028:         $rolecode = "var match = str.split('_');
                   5029:                 var role = match[3];\n";
                   5030:     } elsif ($formname eq 'enrollstudent') {
                   5031:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  5032:         if ($checkauth) {
                   5033:             $finish = "var authcheck = auth_check();\n".
                   5034:                       "   if (authcheck == 'ok') {\n".
                   5035:                       "       formname.submit();\n".
                   5036:                       "   }\n";
                   5037:         } else {
                   5038:             $finish = 'formname.submit()';
                   5039:         }
1.28      raeburn  5040:         $rolecode = "var match = str.split('_');
                   5041:                 var role = match[1];\n";
1.8       raeburn  5042:     } else {
1.28      raeburn  5043:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  5044:         $finish = "seccheck = 'ok';";
1.28      raeburn  5045:         $rolecode = "var match = str.split('_');
                   5046:                 var role = match[1];\n";
1.11      raeburn  5047:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  5048:     }
                   5049:     my %alerts = &Apache::lonlocal::texthash(
                   5050:                     secd => 'Section designations do not apply to Course Coordinator roles.',
1.103     raeburn  5051:                     sedn => 'Section designations do not apply to Coordinator roles.',
1.8       raeburn  5052:                     accr => 'A course coordinator role will be added with access to all sections.',
1.103     raeburn  5053:                     acor => 'A coordinator role will be added with access to all sections',
1.8       raeburn  5054:                     inea => 'In each course, each user may only have one student role at a time.',
1.125     raeburn  5055:                     inco => 'In each community, each user may only have one member role at a time.',
1.8       raeburn  5056:                     youh => 'You had selected ',
                   5057:                     secs => 'sections.',
                   5058:                     plmo => 'Please modify your selections so they include no more than one section.',
                   5059:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   5060:                     plch => 'Please choose a different section name.',
                   5061:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   5062:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.113     raeburn  5063:                     nonw => 'Section names may only contain letters or numbers.',
1.11      raeburn  5064:                  );                
1.8       raeburn  5065:     $setsection_js .= <<"ENDSECCODE";
                   5066: 
1.103     raeburn  5067: function setSections(formname,crstype) {
1.8       raeburn  5068:     var re1 = /^currsec_/;
1.113     raeburn  5069:     var re2 =/\\W/;
1.115     raeburn  5070:     var trimleading = /^\\s+/;
                   5071:     var trimtrailing = /\\s+\$/;
1.8       raeburn  5072:     var groups = new Array($groupslist);
                   5073:     for (var i=0;i<formname.elements.length;i++) {
                   5074:         var str = formname.elements[i].name;
                   5075:         var checkcurr = str.match(re1);
                   5076:         if (checkcurr != null) {
1.115     raeburn  5077:             var num = i;
1.8       raeburn  5078:             if ($checkincluded) {
1.28      raeburn  5079:                 $rolecode
1.103     raeburn  5080:                 if (role == 'cc' || role == 'co') {
                   5081:                     if (role == 'cc') {
                   5082:                         alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   5083:                     } else {
                   5084:                         alert("$alerts{'sedn'}\\n$alerts{'acor'}");
                   5085:                     }
                   5086:                 } else {
1.8       raeburn  5087:                     var sections = '';
                   5088:                     var numsec = 0;
1.115     raeburn  5089:                     var fromexisting = new Array();
                   5090:                     for (var j=0; j<formname.elements[num].length; j++) {
                   5091:                         if (formname.elements[num].options[j].selected == true ) {
                   5092:                             var addsec = formname.elements[num].options[j].value;
1.119     raeburn  5093:                             if ((addsec != "") && (addsec != null)) {
1.115     raeburn  5094:                                 fromexisting.push(addsec);
1.8       raeburn  5095:                                 if (numsec == 0) {
1.115     raeburn  5096:                                     sections = addsec;
                   5097:                                 } else {
                   5098:                                     sections = sections + "," +  addsec;
1.8       raeburn  5099:                                 }
1.115     raeburn  5100:                                 numsec ++;
1.8       raeburn  5101:                             }
                   5102:                         }
                   5103:                     }
1.115     raeburn  5104:                     var newsecs = formname.elements[num+1].value;
1.113     raeburn  5105:                     var validsecs = new Array();
1.115     raeburn  5106:                     var validsecstr = '';
1.113     raeburn  5107:                     var badsecs = new Array();
1.8       raeburn  5108:                     if (newsecs != null && newsecs != "") {
1.115     raeburn  5109:                         var numsplit;
                   5110:                         if (newsecs.indexOf(',') == -1) {
                   5111:                             numsplit = new Array(newsecs);
                   5112:                         } else {
                   5113:                             numsplit = newsecs.split(/,/g);
                   5114:                         }
1.117     raeburn  5115:                         for (var m=0; m<numsplit.length; m++) {
                   5116:                             var newsec = numsplit[m];
1.115     raeburn  5117:                             newsec = newsec.replace(trimleading,'');
                   5118:                             newsec = newsec.replace(trimtrailing,'');
                   5119:                             if (re2.test(newsec) == true) {
                   5120:                                 badsecs.push(newsec);
1.113     raeburn  5121:                             } else {
1.115     raeburn  5122:                                 if (newsec != '') {
                   5123:                                     var isnew = 1;
                   5124:                                     if (fromexisting != null) {
1.117     raeburn  5125:                                         for (var n=0; n<fromexisting.length; n++) {
                   5126:                                             if (newsec == fromexisting[n]) {
1.115     raeburn  5127:                                                 isnew = 0;
                   5128:                                             }
                   5129:                                         }
                   5130:                                     }
                   5131:                                     if (isnew == 1) {
                   5132:                                         validsecs.push(newsec);
                   5133:                                     }
                   5134:                                 }
1.113     raeburn  5135:                             }
                   5136:                         }
                   5137:                         if (badsecs.length > 0) {
                   5138:                             alert("$alerts{'nonw'}\\n$alerts{'plch'}");
                   5139:                             return;
                   5140:                         }
                   5141:                         numsec = numsec + validsecs.length;
1.8       raeburn  5142:                     }
                   5143:                     if ((role == 'st') && (numsec > 1)) {
1.103     raeburn  5144:                         if (crstype == 'Community') {
                   5145:                             alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5146:                         } else {
                   5147:                             alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5148:                         }
1.8       raeburn  5149:                         return;
1.115     raeburn  5150:                     } else {
                   5151:                         if (validsecs != null) {
                   5152:                             for (var j=0; j<validsecs.length; j++) {
                   5153:                                 if (validsecstr == '' || validsecstr == null) {
                   5154:                                     validsecstr = validsecs[j];
                   5155:                                 } else {
                   5156:                                     validsecstr += ','+validsecs[j];
                   5157:                                 }
                   5158:                                 if ((validsecs[j] == 'all') ||
                   5159:                                     (validsecs[j] == 'none')) {
                   5160:                                     alert("'"+validsecs[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
1.8       raeburn  5161:                                     return;
                   5162:                                 }
                   5163:                                 for (var k=0; k<groups.length; k++) {
1.115     raeburn  5164:                                     if (validsecs[j] == groups[k]) {
                   5165:                                         alert("'"+validsecs[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
1.8       raeburn  5166:                                         return;
                   5167:                                     }
                   5168:                                 }
                   5169:                             }
                   5170:                         }
                   5171:                     }
1.115     raeburn  5172:                     if ((validsecstr != '') && (validsecstr != null)) {
1.117     raeburn  5173:                         if ((sections == '') || (sections == null)) {
                   5174:                             sections = validsecstr;
                   5175:                         } else {
1.115     raeburn  5176:                             sections = sections + "," + validsecstr;
                   5177:                         }
                   5178:                     }
                   5179:                     formname.elements[num+2].value = sections;
1.8       raeburn  5180:                 }
                   5181:             }
                   5182:         }
                   5183:     }
                   5184:     $finish
                   5185: }
                   5186: ENDSECCODE
1.11      raeburn  5187:     return $setsection_js; 
1.8       raeburn  5188: }
                   5189: 
1.15      raeburn  5190: sub can_create_user {
                   5191:     my ($dom,$context,$usertype) = @_;
                   5192:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5193:     my $cancreate = 1;
1.28      raeburn  5194:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   5195:         return $cancreate;
                   5196:     }
1.15      raeburn  5197:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   5198:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
1.100     raeburn  5199:             if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
1.15      raeburn  5200:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   5201:                 if ($creation eq 'none') {
                   5202:                     $cancreate = 0;
                   5203:                 } elsif ($creation ne 'any') {
                   5204:                     if (defined($usertype)) {
                   5205:                         if ($creation ne $usertype) {
                   5206:                             $cancreate = 0;
                   5207:                         }
                   5208:                     }
                   5209:                 }
                   5210:             }
                   5211:         }
                   5212:     }
                   5213:     return $cancreate;
                   5214: }
                   5215: 
1.20      raeburn  5216: sub can_modify_userinfo {
                   5217:     my ($context,$dom,$fields,$userroles) = @_;
                   5218:     my %domconfig =
                   5219:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   5220:                                 $dom);
                   5221:     my %canmodify;
                   5222:     if (ref($fields) eq 'ARRAY') {
                   5223:         foreach my $field (@{$fields}) {
                   5224:             $canmodify{$field}  = 0;
                   5225:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   5226:                 $canmodify{$field} = 1;
                   5227:             } else {
                   5228:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   5229:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   5230:                         if (ref($userroles) eq 'ARRAY') {
                   5231:                             foreach my $role (@{$userroles}) {
                   5232:                                 my $testrole;
1.60      raeburn  5233:                                 if ($context eq 'selfcreate') {
                   5234:                                     $testrole = $role;
1.20      raeburn  5235:                                 } else {
1.60      raeburn  5236:                                     if ($role =~ /^cr\//) {
                   5237:                                         $testrole = 'cr';
                   5238:                                     } else {
                   5239:                                         $testrole = $role;
                   5240:                                     }
1.20      raeburn  5241:                                 }
                   5242:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   5243:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   5244:                                         $canmodify{$field} = 1;
                   5245:                                         last;
                   5246:                                     }
                   5247:                                 }
                   5248:                             }
                   5249:                         } else {
                   5250:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   5251:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   5252:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   5253:                                         $canmodify{$field} = 1;
                   5254:                                         last;
                   5255:                                     }
                   5256:                                 }
                   5257:                             }
                   5258:                         }
                   5259:                     }
                   5260:                 } elsif ($context eq 'course') {
                   5261:                     if (ref($userroles) eq 'ARRAY') {
                   5262:                         if (grep(/^st$/,@{$userroles})) {
                   5263:                             $canmodify{$field} = 1;
                   5264:                         }
                   5265:                     } else {
                   5266:                         $canmodify{$field} = 1;
                   5267:                     }
                   5268:                 }
                   5269:             }
                   5270:         }
                   5271:     }
                   5272:     return %canmodify;
                   5273: }
                   5274: 
1.18      raeburn  5275: sub check_usertype {
1.134     raeburn  5276:     my ($dom,$uname,$rules,$curr_rules,$got_rules) = @_;
1.18      raeburn  5277:     my $usertype;
1.134     raeburn  5278:     if ((ref($got_rules) eq 'HASH') && (ref($curr_rules) eq 'HASH')) {
                   5279:         if (!$got_rules->{$dom}) {
                   5280:             my %domconfig = &Apache::lonnet::get_dom('configuration',
                   5281:                                               ['usercreation'],$dom);
                   5282:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   5283:                 foreach my $item ('username','id') {
                   5284:                     if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   5285:                         $curr_rules->{$dom}{$item} =
                   5286:                                 $domconfig{'usercreation'}{$item.'_rule'};
                   5287:                     }
                   5288:                 }
                   5289:             }
                   5290:             $got_rules->{$dom} = 1;
                   5291:         }
                   5292:         if (ref($rules) eq 'HASH') {
                   5293:             my @user_rules;
                   5294:             if (ref($curr_rules->{$dom}{'username'}) eq 'ARRAY') {
                   5295:                 foreach my $rule (keys(%{$rules})) {
                   5296:                     if (grep(/^\Q$rule\E/,@{$curr_rules->{$dom}{'username'}})) {
                   5297:                         push(@user_rules,$rule);
                   5298:                     }
                   5299:                 } 
                   5300:             }
                   5301:             if (@user_rules > 0) {
                   5302:                 my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   5303:                 if (keys(%rule_check) > 0) {
                   5304:                     $usertype = 'unofficial';
                   5305:                     foreach my $item (keys(%rule_check)) {
                   5306:                         if ($rule_check{$item}) {
                   5307:                             $usertype = 'official';
                   5308:                             last;
                   5309:                         }
1.18      raeburn  5310:                     }
                   5311:                 }
                   5312:             }
                   5313:         }
                   5314:     }
                   5315:     return $usertype;
                   5316: }
                   5317: 
1.17      raeburn  5318: sub roles_by_context {
1.101     raeburn  5319:     my ($context,$custom,$crstype) = @_;
1.17      raeburn  5320:     my @allroles;
                   5321:     if ($context eq 'course') {
1.99      raeburn  5322:         @allroles = ('st');
                   5323:         if ($env{'request.role'} =~ m{^dc\./}) {
                   5324:             push(@allroles,'ad');
                   5325:         }
1.101     raeburn  5326:         push(@allroles,('ta','ep','in'));
                   5327:         if ($crstype eq 'Community') {
                   5328:             push(@allroles,'co');
                   5329:         } else {
                   5330:             push(@allroles,'cc');
                   5331:         }
1.17      raeburn  5332:         if ($custom) {
                   5333:             push(@allroles,'cr');
                   5334:         }
                   5335:     } elsif ($context eq 'author') {
                   5336:         @allroles = ('ca','aa');
                   5337:     } elsif ($context eq 'domain') {
1.99      raeburn  5338:         @allroles = ('li','ad','dg','sc','au','dc');
1.17      raeburn  5339:     }
                   5340:     return @allroles;
                   5341: }
                   5342: 
1.16      raeburn  5343: sub get_permission {
1.101     raeburn  5344:     my ($context,$crstype) = @_;
1.16      raeburn  5345:     my %permission;
                   5346:     if ($context eq 'course') {
1.17      raeburn  5347:         my $custom = 1;
1.101     raeburn  5348:         my @allroles = &roles_by_context($context,$custom,$crstype);
1.17      raeburn  5349:         foreach my $role (@allroles) {
                   5350:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   5351:                 $permission{'cusr'} = 1;
                   5352:                 last;
                   5353:             }
1.16      raeburn  5354:         }
                   5355:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   5356:             $permission{'custom'} = 1;
                   5357:         }
                   5358:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   5359:             $permission{'view'} = 1;
                   5360:         }
                   5361:         if (!$permission{'view'}) {
                   5362:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5363:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   5364:             if ($permission{'view'}) {
                   5365:                 $permission{'view_section'} = $env{'request.course.sec'};
                   5366:             }
                   5367:         }
1.17      raeburn  5368:         if (!$permission{'cusr'}) {
                   5369:             if ($env{'request.course.sec'} ne '') {
                   5370:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5371:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   5372:                 if ($permission{'cusr'}) {
                   5373:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   5374:                 }
                   5375:             }
                   5376:         }
1.16      raeburn  5377:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   5378:             $permission{'grp_manage'} = 1;
                   5379:         }
                   5380:     } elsif ($context eq 'author') {
                   5381:         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
                   5382:         $permission{'view'} = $permission{'cusr'};
                   5383:     } else {
1.17      raeburn  5384:         my @allroles = &roles_by_context($context);
                   5385:         foreach my $role (@allroles) {
1.28      raeburn  5386:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   5387:                 $permission{'cusr'} = 1;
1.17      raeburn  5388:                 last;
                   5389:             }
                   5390:         }
                   5391:         if (!$permission{'cusr'}) {
                   5392:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   5393:                 $permission{'cusr'} = 1;
                   5394:             }
1.16      raeburn  5395:         }
                   5396:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   5397:             $permission{'custom'} = 1;
                   5398:         }
                   5399:         $permission{'view'} = $permission{'cusr'};
                   5400:     }
                   5401:     my $allowed = 0;
                   5402:     foreach my $perm (values(%permission)) {
                   5403:         if ($perm) { $allowed=1; last; }
                   5404:     }
                   5405:     return (\%permission,$allowed);
                   5406: }
                   5407: 
                   5408: # ==================================================== Figure out author access
                   5409: 
                   5410: sub authorpriv {
                   5411:     my ($auname,$audom)=@_;
                   5412:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   5413:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   5414: }
                   5415: 
1.27      raeburn  5416: sub roles_on_upload {
1.101     raeburn  5417:     my ($context,$setting,$crstype,%customroles) = @_;
1.27      raeburn  5418:     my (@possible_roles,@permitted_roles);
1.101     raeburn  5419:     @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
1.27      raeburn  5420:     foreach my $role (@possible_roles) {
                   5421:         if ($role eq 'cr') {
                   5422:             push(@permitted_roles,keys(%customroles));
                   5423:         } else {
                   5424:             push(@permitted_roles,$role);
                   5425:         }
                   5426:     }
1.42      raeburn  5427:     return @permitted_roles;
1.27      raeburn  5428: }
                   5429: 
1.17      raeburn  5430: sub get_course_identity {
                   5431:     my ($cid) = @_;
                   5432:     my ($cnum,$cdom,$cdesc);
                   5433:     if ($cid eq '') {
                   5434:         $cid = $env{'request.course.id'}
                   5435:     }
                   5436:     if ($cid ne '') {
                   5437:         $cnum = $env{'course.'.$cid.'.num'};
                   5438:         $cdom = $env{'course.'.$cid.'.domain'};
                   5439:         $cdesc = $env{'course.'.$cid.'.description'};
                   5440:         if ($cnum eq '' || $cdom eq '') {
                   5441:             my %coursehash =
                   5442:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   5443:             $cdom = $coursehash{'domain'};
                   5444:             $cnum = $coursehash{'num'};
                   5445:             $cdesc = $coursehash{'description'};
                   5446:         }
                   5447:     }
                   5448:     return ($cnum,$cdom,$cdesc);
                   5449: }
                   5450: 
1.19      raeburn  5451: sub dc_setcourse_js {
1.37      raeburn  5452:     my ($formname,$mode,$context) = @_;
                   5453:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  5454:     my $cctext = &Apache::lonnet::plaintext('cc');
1.103     raeburn  5455:     my $cotext = &Apache::lonnet::plaintext('co');
1.19      raeburn  5456:     my %alerts = &sectioncheck_alerts();
                   5457:     my $role = 'role';
                   5458:     if ($mode eq 'upload') {
                   5459:         $role = 'courserole';
1.37      raeburn  5460:     } else {
                   5461:         $authen_check = &verify_authen($formname,$context);
1.19      raeburn  5462:     }
                   5463:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  5464: $authen_check
                   5465: 
1.19      raeburn  5466: function setCourse() {
                   5467:     var course = document.$formname.dccourse.value;
                   5468:     if (course != "") {
                   5469:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   5470:             alert("$alerts{'curd'}");
                   5471:             return;
                   5472:         }
                   5473:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   5474:         var section="";
                   5475:         var numsections = 0;
                   5476:         var newsecs = new Array();
                   5477:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   5478:             if (document.$formname.currsec.options[i].selected == true ) {
                   5479:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   5480:                     if (numsections == 0) {
                   5481:                         section = document.$formname.currsec.options[i].value
                   5482:                         numsections = 1;
                   5483:                     }
                   5484:                     else {
                   5485:                         section = section + "," +  document.$formname.currsec.options[i].value
                   5486:                         numsections ++;
                   5487:                     }
                   5488:                 }
                   5489:             }
                   5490:         }
                   5491:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   5492:             if (numsections == 0) {
                   5493:                 section = document.$formname.newsec.value
                   5494:             }
                   5495:             else {
                   5496:                 section = section + "," +  document.$formname.newsec.value
                   5497:             }
                   5498:             newsecs = document.$formname.newsec.value.split(/,/g);
                   5499:             numsections = numsections + newsecs.length;
                   5500:         }
                   5501:         if ((userrole == 'st') && (numsections > 1)) {
1.103     raeburn  5502:             if (document.$formname.crstype.value == 'Community') {
                   5503:                 alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5504:             } else {
                   5505:                 alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5506:             }
1.19      raeburn  5507:             return;
                   5508:         }
                   5509:         for (var j=0; j<newsecs.length; j++) {
                   5510:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   5511:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   5512:                 return;
                   5513:             }
                   5514:             if (document.$formname.groups.value != '') {
                   5515:                 var groups = document.$formname.groups.value.split(/,/g);
                   5516:                 for (var k=0; k<groups.length; k++) {
                   5517:                     if (newsecs[j] == groups[k]) {
1.103     raeburn  5518:                         if (document.$formname.crstype.value == 'Community') {
                   5519:                             alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5520:                         } else {
                   5521:                             alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5522:                         }
1.19      raeburn  5523:                         return;
                   5524:                     }
                   5525:                 }
                   5526:             }
                   5527:         }
                   5528:         if ((userrole == 'cc') && (numsections > 0)) {
                   5529:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5530:             section = "";
                   5531:         }
1.103     raeburn  5532:         if ((userrole == 'co') && (numsections > 0)) {
                   5533:             alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5534:             section = "";
                   5535:         }
1.19      raeburn  5536: SCRIPTTOP
                   5537:     if ($mode ne 'upload') {
                   5538:         $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5539:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   5540:         var numcourse = getIndex(document.$formname.dccourse);
                   5541:         if (numcourse == "-1") {
1.103     raeburn  5542:             if (document.$formname.type == 'Community') {
                   5543:                 alert("$alerts{'thwc'}");
                   5544:             } else {
                   5545:                 alert("$alerts{'thwa'}");
                   5546:             }
1.19      raeburn  5547:             return;
                   5548:         }
                   5549:         else {
                   5550:             document.$formname.elements[numcourse].name = "act"+coursename;
                   5551:             var numnewsec = getIndex(document.$formname.newsec);
                   5552:             if (numnewsec != "-1") {
                   5553:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   5554:                 document.$formname.elements[numnewsec].value = section;
                   5555:             }
                   5556:             var numstart = getIndex(document.$formname.start);
                   5557:             if (numstart != "-1") {
                   5558:                 document.$formname.elements[numstart].name = "start"+coursename;
                   5559:             }
                   5560:             var numend = getIndex(document.$formname.end);
                   5561:             if (numend != "-1") {
                   5562:                 document.$formname.elements[numend].name = "end"+coursename
                   5563:             }
                   5564:         }
                   5565:     }
1.37      raeburn  5566:     var authcheck = auth_check();
                   5567:     if (authcheck == 'ok') {
                   5568:         document.$formname.submit();
                   5569:     }
1.19      raeburn  5570: }
                   5571: ENDSCRIPT
                   5572:     } else {
                   5573:         $dc_setcourse_code .=  "
                   5574:         document.$formname.sections.value = section;
                   5575:     }
                   5576:     return 'ok';
                   5577: }
                   5578: ";
                   5579:     }
                   5580:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5581: 
                   5582:     function getIndex(caller) {
                   5583:         for (var i=0;i<document.$formname.elements.length;i++) {
                   5584:             if (document.$formname.elements[i] == caller) {
                   5585:                 return i;
                   5586:             }
                   5587:         }
                   5588:         return -1;
                   5589:     }
                   5590: ENDSCRIPT
1.37      raeburn  5591:     return $dc_setcourse_code;
                   5592: }
                   5593: 
                   5594: sub verify_authen {
                   5595:     my ($formname,$context) = @_;
                   5596:     my %alerts = &authcheck_alerts();
                   5597:     my $finish = "return 'ok';";
                   5598:     if ($context eq 'author') {
                   5599:         $finish = "document.$formname.submit();";
                   5600:     }
                   5601:     my $outcome = <<"ENDSCRIPT";
                   5602: 
                   5603: function auth_check() {
                   5604:     var logintype;
                   5605:     if (document.$formname.login.length) {
                   5606:         if (document.$formname.login.length > 0) {
                   5607:             var loginpicked = 0;
                   5608:             for (var i=0; i<document.$formname.login.length; i++) {
                   5609:                 if (document.$formname.login[i].checked == true) {
                   5610:                     loginpicked = 1;
                   5611:                     logintype = document.$formname.login[i].value;
                   5612:                 }
                   5613:             }
                   5614:             if (loginpicked == 0) {
                   5615:                 alert("$alerts{'authen'}");
                   5616:                 return;
                   5617:             }
                   5618:         }
                   5619:     } else {
                   5620:         logintype = document.$formname.login.value;
                   5621:     }
                   5622:     if (logintype == 'nochange') {
                   5623:         return 'ok';
                   5624:     }
                   5625:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   5626:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   5627:         var alertmsg = '';
                   5628:         switch (logintype) {
                   5629:             case 'krb':
                   5630:                 alertmsg = '$alerts{'krb'}';
                   5631:                 break;
                   5632:             case 'int':
                   5633:                 alertmsg = '$alerts{'ipass'}';
                   5634:             case 'fsys':
                   5635:                 alertmsg = '$alerts{'ipass'}';
                   5636:                 break;
                   5637:             case 'loc':
                   5638:                 alertmsg = '';
                   5639:                 break;
                   5640:             default:
                   5641:                 alertmsg = '';
                   5642:         }
                   5643:         if (alertmsg != '') {
                   5644:             alert(alertmsg);
                   5645:             return;
                   5646:         }
                   5647:     }
                   5648:     $finish
                   5649: }
                   5650: ENDSCRIPT
1.19      raeburn  5651: }
                   5652: 
                   5653: sub sectioncheck_alerts {
                   5654:     my %alerts = &Apache::lonlocal::texthash(
1.103     raeburn  5655:                     curd => 'You must select a course or community in the current domain',
1.19      raeburn  5656:                     inea => 'In each course, each user may only have one student role at a time',
1.103     raeburn  5657:                     inco => 'In each community, each user may only have one member role at a time', 
1.19      raeburn  5658:                     youh => 'You had selected',
                   5659:                     sect => 'sections',
                   5660:                     plsm => 'Please modify your selections so they include no more than one section',
                   5661:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   5662:                     plsc => 'Please choose a different section name',
                   5663:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
1.103     raeburn  5664:                     mayc => 'may not be used as the name for a section, as it is the name of a community group',
1.19      raeburn  5665:                     secn => 'Section names and group names must be distinct',
                   5666:                     secd => 'Section designations do not apply to ',
                   5667:                     role => 'roles',
                   5668:                     accr => 'role will be added with access to all sections',
1.103     raeburn  5669:                     thwa => 'There was a problem with your course selection',
                   5670:                     thwc => 'There was a problem with your community selection',
1.19      raeburn  5671:                  );
                   5672:     return %alerts;
                   5673: }
1.17      raeburn  5674: 
1.37      raeburn  5675: sub authcheck_alerts {
                   5676:     my %alerts = 
                   5677:         &Apache::lonlocal::texthash(
                   5678:                     authen => 'You must choose an authentication type.',
                   5679:                     krb    => 'You need to specify the Kerberos domain.',
                   5680:                     ipass  => 'You need to specify the initial password.',
                   5681:         );
                   5682:     return %alerts;
                   5683: }
                   5684: 
1.1       raeburn  5685: 1;
                   5686: 

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