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

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

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