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

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

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