Annotation of loncom/interface/londropadd.pm, revision 1.88

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to drop and add students in courses 
                      3: #
1.88    ! matthew     4: # $Id: londropadd.pm,v 1.87 2003/11/05 20:55:59 matthew Exp $
1.17      albertel    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: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       www        28: # (Handler to set parameters for assessments
                     29: #
                     30: # (Handler to resolve ambiguous file locations
                     31: #
                     32: # (TeX Content Handler
                     33: #
1.50      matthew    34: ###############################################################
1.82      www        35: ##############################################################
1.1       www        36: 
                     37: package Apache::londropadd;
                     38: 
                     39: use strict;
1.24      albertel   40: use Apache::lonnet();
                     41: use Apache::loncommon();
1.50      matthew    42: use Apache::lonhtmlcommon();
1.1       www        43: use Apache::Constants qw(:common :http REDIRECT);
1.60      matthew    44: use Spreadsheet::WriteExcel;
1.86      www        45: use Apache::lonlocal;
1.1       www        46: 
1.50      matthew    47: ###############################################################
                     48: ###############################################################
1.10      www        49: sub header {
1.46      www        50:     my $bodytag=&Apache::loncommon::bodytag('Enrollment Manager');
1.88    ! matthew    51:     my $title = &mt('LON-CAPA Enrollment Manager');
1.27      matthew    52:     return(<<ENDHEAD);
1.1       www        53: <html>
                     54: <head>
1.88    ! matthew    55: <title>$title</title>
1.1       www        56: </head>
1.46      www        57: $bodytag
1.40      matthew    58: <form method="post" enctype="multipart/form-data"  
                     59:       action="/adm/dropadd" name="studentform">
1.1       www        60: ENDHEAD
1.10      www        61: }
                     62: 
1.50      matthew    63: ###############################################################
                     64: ###############################################################
                     65: # Drop student from all sections of a course, except optional $csec
1.26      matthew    66: sub modifystudent {
1.33      matthew    67:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
1.26      matthew    68:     # if $csec is undefined, drop the student from all the courses matching
                     69:     # this one.  If $csec is defined, drop them from all other sections of 
                     70:     # this course and add them to section $csec
1.25      matthew    71:     $courseid=~s/\_/\//g;
                     72:     $courseid=~s/^(\w)/\/$1/;
1.26      matthew    73:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
                     74:     my ($tmp) = keys(%roles);
                     75:     # Bail out if we were unable to get the students roles
1.35      matthew    76:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
1.26      matthew    77:     # Go through the roles looking for enrollment in this course
1.35      matthew    78:     my $result = '';
1.26      matthew    79:     foreach my $course (keys(%roles)) {
1.35      matthew    80:         if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
1.26      matthew    81:             # We are in this course
1.25      matthew    82:             my $section=$1;
1.26      matthew    83:             $section='' if ($course eq $courseid.'_st');
1.87      matthew    84:             if (defined($csec) && $section eq $csec) {
1.71      matthew    85:                 $result .= 'ok:';
                     86:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
1.27      matthew    87:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
1.25      matthew    88:                 my $now=time;
1.50      matthew    89:                 # if this is an active role 
1.27      matthew    90:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
1.25      matthew    91:                     my $reply=&Apache::lonnet::modifystudent
1.70      matthew    92:                         # dom  name  id mode pass     f     m     l     g
                     93:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.33      matthew    94:                          $section,time,undef,undef,$desiredhost);
1.35      matthew    95:                     $result .= $reply.':';
1.25      matthew    96:                 }
1.10      www        97:             }
                     98:         }
1.20      harris41   99:     }
1.35      matthew   100:     if ($result eq '') {
1.62      matthew   101:         $result = 'Unable to find section for this student';
1.37      matthew   102:     } else {
                    103:         $result =~ s/(ok:)+/ok/g;
1.35      matthew   104:     }
                    105:     return $result;
1.10      www       106: }
                    107: 
1.50      matthew   108: ###############################################################
                    109: ###############################################################
                    110: # build a domain and server selection form
1.31      matthew   111: sub domain_form {
                    112:     my ($defdom) = @_;
                    113:     # Set up domain and server selection forms
                    114:     #
                    115:     # Get the domains
                    116:     my @domains = &Apache::loncommon::get_domains();
                    117:     # build up the menu information to be passed to 
                    118:     # &Apache::loncommon::linked_select_forms
                    119:     my %select_menus;
                    120:     foreach my $dom (@domains) {
                    121:         # set up the text for this domain
                    122:         $select_menus{$dom}->{'text'}= $dom;
                    123:         # we want a choice of 'default' as the default in the second menu
                    124:         $select_menus{$dom}->{'default'}= 'default';
                    125:         $select_menus{$dom}->{'select2'}->{'default'} = 'default';
                    126:         # Now build up the other items in the second menu
1.45      matthew   127:         my %servers = &Apache::loncommon::get_library_servers($dom);
1.31      matthew   128:         foreach my $server (keys(%servers)) {
                    129:             $select_menus{$dom}->{'select2'}->{$server} 
                    130:                                             = "$server $servers{$server}";
                    131:         }
                    132:     }
                    133:     my $result  = &Apache::loncommon::linked_select_forms
                    134:         ('studentform',' with home server ',$defdom,
                    135:          'lcdomain','lcserver',\%select_menus);
                    136:     return $result;
                    137: }
                    138: 
1.50      matthew   139: ###############################################################
                    140: ###############################################################
                    141: #  Menu Phase One
                    142: sub print_main_menu {
1.10      www       143:     my $r=shift;
1.88    ! matthew   144:     my %Text = &Apache::lonlocal::texthash
        !           145:         ('upload'    => 'Upload a class list',
        !           146:          'enrollone' => 'Enroll a single student',
        !           147:          'modify'    => 'Modify student data',
        !           148:          'view'      => 'View Class List',
        !           149:          'drop'      => 'Drop Students');
        !           150: 
1.48      matthew   151:     $r->print(<<END);
                    152: <p>
1.50      matthew   153: <font size="+1">
1.88    ! matthew   154:     <a href="/adm/dropadd?action=upload">$Text{'upload'}</a>
1.50      matthew   155: </font>
                    156: </p><p>
                    157: <font size="+1">
1.88    ! matthew   158:     <a href="/adm/dropadd?action=enrollstudent">$Text{'enrollone'}</a>
1.50      matthew   159: </font>
1.48      matthew   160: </p><p>
1.50      matthew   161: <font size="+1">
1.88    ! matthew   162:     <a href="/adm/dropadd?action=modifystudent">$Text{'modify'}</a>
1.50      matthew   163: </font>
1.48      matthew   164: </p><p>
1.50      matthew   165: <font size="+1">
1.88    ! matthew   166:     <a href="/adm/dropadd?action=classlist">$Text{'view'}</a>
1.50      matthew   167: </font>
1.48      matthew   168: </p><p>
1.50      matthew   169: <font size="+1">
1.88    ! matthew   170:     <a href="/adm/dropadd?action=drop">$Text{'drop'}</a>
1.50      matthew   171: </font>
1.48      matthew   172: </p>
                    173: END
1.10      www       174: }
                    175: 
1.50      matthew   176: ###############################################################
                    177: ###############################################################
                    178: sub print_upload_manager_header {
1.23      albertel  179:     my ($r,$datatoken,$distotal,$krbdefdom)=@_;
1.24      albertel  180:     my $javascript;
1.50      matthew   181:     if (! exists($ENV{'form.upfile_associate'})) {
                    182:         $ENV{'form.upfile_associate'} = 'forward';
                    183:     }
                    184:     if ($ENV{'form.associate'} eq 'Reverse Association') {
                    185:         if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                    186:             $ENV{'form.upfile_associate'} = 'reverse';
                    187:         } else {
                    188:             $ENV{'form.upfile_associate'} = 'forward';
                    189:         }
                    190:     }
1.24      albertel  191:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
1.50      matthew   192: 	$javascript=&upload_manager_javascript_reverse_associate();
1.24      albertel  193:     } else {
1.50      matthew   194: 	$javascript=&upload_manager_javascript_forward_associate();
1.24      albertel  195:     }
                    196:     my $javascript_validations=&javascript_validations($krbdefdom);
1.82      www       197:     my $checked=(($ENV{'form.noFirstLine'})?' checked="1"':'');
1.88    ! matthew   198:     $r->print('<h3>'.&mt('Uploading Class List')."</h3>\n".
        !           199:               "<hr>\n".
        !           200:               '<h3>'.&mt('Identify fields')."</h3>\n");
        !           201:     $r->print("<p>\n".
        !           202:               &mt('Total number of records found in file: [_1].',$distotal).
        !           203:               "\n".
        !           204:               "</p><hr>\n");
1.10      www       205:     $r->print(<<ENDPICK);
1.22      albertel  206: Enter as many fields as you can. The system will inform you and bring you back
                    207: to this page if the data selected is insufficient to run your class.<hr />
1.36      albertel  208: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1.50      matthew   209: <input type="hidden" name="action"     value="upload" />
                    210: <input type="hidden" name="state"      value="got_file" />
1.36      albertel  211: <input type="hidden" name="associate"  value="" />
1.26      matthew   212: <input type="hidden" name="datatoken"  value="$datatoken" />
1.24      albertel  213: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
1.82      www       214: <input type="checkbox" name="noFirstLine" $checked />Ignore First Line
1.24      albertel  215: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
1.26      matthew   216: <input type="hidden" name="upfile_associate" 
                    217:                                        value="$ENV{'form.upfile_associate'}" />
1.24      albertel  218: <hr />
1.28      matthew   219: <script type="text/javascript" language="Javascript">
1.24      albertel  220: $javascript
                    221: $javascript_validations
                    222: </script>
                    223: ENDPICK
                    224: }
                    225: 
1.50      matthew   226: ###############################################################
                    227: ###############################################################
1.24      albertel  228: sub javascript_validations {
                    229:     my ($krbdefdom)=@_;
1.28      matthew   230:     my %param = ( formname => 'studentform',
                    231:                   kerb_def_dom => $krbdefdom );
                    232:     my $authheader = &Apache::loncommon::authform_header(%param);
1.65      matthew   233:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.24      albertel  234:     return (<<ENDPICK);
1.73      www       235: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
1.3       www       236:     var foundatype=0;
                    237:     var message='';
                    238:     if (founduname==0) {
1.28      matthew   239: 	alert('You need to specify the username field');
1.3       www       240:         return;
                    241:     }
1.61      matthew   242:     // alert('current.radiovalue = '+current.radiovalue);
1.29      matthew   243:     if (current.radiovalue == null || current.radiovalue == 'nochange') {
1.28      matthew   244:         // They did not check any of the login radiobuttons.
                    245:         alert('You must choose an authentication type');
                    246:         return;
                    247:     }
                    248:     foundatype=1;
1.29      matthew   249:     if (current.argfield == null || current.argfield == '') {
1.28      matthew   250:         var alertmsg = '';
1.29      matthew   251:         switch (current.value) {
1.28      matthew   252:             case 'krb': 
                    253:                 alertmsg = 'You need to specify the Kerberos domain';
                    254:                 break;
                    255:             case 'loc':
                    256:             case 'fsys':
                    257:                 alertmsg = 'You need to specify the initial password';
                    258:                 break;
                    259:             case 'fsys':
                    260:                 alertmsg = '';
                    261:                 break;
                    262:             default: 
                    263:                 alertmsg = '';
1.3       www       264:         }
1.28      matthew   265:         if (alertmsg != '') {
                    266:             alert(alertmsg);
1.3       www       267:             return;
                    268:         }
                    269:     }
1.28      matthew   270: 
1.73      www       271:     if (foundname==0) { message='name fields'; }
                    272:     if (foundid==0) { if (message!='') { message+=', '; } message+='student number field'; }
                    273:     if (foundsec==0) {  if (message!='') { message+=', '; } message+='section or group field'; }
                    274:     if (foundemail==0) {  if (message!='') { message+=', '; } message+='email address field'; }
1.4       www       275:     if (message!='') {
1.73      www       276:        message='Not specified (optional): '+message+'.  Continue enrollment?';
1.4       www       277:        if (confirm(message)) {
1.50      matthew   278:           vf.state.value='enrolling';
1.4       www       279: 	  vf.submit();
                    280:        }
                    281:     } else {
1.50      matthew   282:       vf.state.value='enrolling';
1.4       www       283:       vf.submit();
1.24      albertel  284:     }
1.3       www       285: }
                    286: 
1.28      matthew   287: $authheader
1.24      albertel  288: ENDPICK
1.28      matthew   289: 
1.24      albertel  290: }
                    291: 
1.74      matthew   292: sub javascript_validations_without_auth {
                    293:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
                    294:     return (<<ENDPICK);
                    295: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
                    296:     var foundatype=0;
                    297:     var message='';
                    298:     if (founduname==0) {
                    299: 	alert('You need to specify the username field');
                    300:         return;
                    301:     }
                    302:     if (foundname==0) { message='name fields'; }
                    303:     if (foundid==0) { if (message!='') { message+=', '; } message+='student number field'; }
                    304:     if (foundsec==0) {  if (message!='') { message+=', '; } message+='section or group field'; }
                    305:     if (foundemail==0) {  if (message!='') { message+=', '; } message+='email address field'; }
                    306:     if (message!='') {
                    307:        message='Not specified (optional): '+message+'.  Continue enrollment?';
                    308:        if (confirm(message)) {
                    309:           vf.state.value='enrolling';
                    310: 	  vf.submit();
                    311:        }
                    312:     } else {
                    313:       vf.state.value='enrolling';
                    314:       vf.submit();
                    315:     }
                    316: }
                    317: 
                    318: ENDPICK
                    319: 
                    320: }
                    321: 
1.50      matthew   322: ###############################################################
                    323: ###############################################################
                    324: sub upload_manager_javascript_forward_associate {
1.24      albertel  325:     return(<<ENDPICK);
                    326: function verify(vf) {
                    327:     var founduname=0;
                    328:     var foundpwd=0;
                    329:     var foundname=0;
                    330:     var foundid=0;
                    331:     var foundsec=0;
1.73      www       332:     var foundemail=0;
1.24      albertel  333:     var tw;
                    334:     for (i=0;i<=vf.nfields.value;i++) {
                    335:         tw=eval('vf.f'+i+'.selectedIndex');
                    336:         if (tw==1) { founduname=1; }
                    337:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    338:         if (tw==7) { foundid=1; }
                    339:         if (tw==8) { foundsec=1; }
                    340:         if (tw==9) { foundpwd=1; }
1.73      www       341:         if (tw==10) { foundemail=1; }
1.24      albertel  342:     }
1.73      www       343:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail);
1.24      albertel  344: }
                    345: 
1.49      matthew   346: //
                    347: // vf = this.form
                    348: // tf = column number
                    349: //
                    350: // values of nw
                    351: //
                    352: // 0 = none
                    353: // 1 = username
                    354: // 2 = names (lastname, firstnames)
                    355: // 3 = fname (firstname)
                    356: // 4 = mname (middlename)
                    357: // 5 = lname (lastname)
                    358: // 6 = gen   (generation)
                    359: // 7 = id
                    360: // 8 = section
                    361: // 9 = ipwd  (password)
1.73      www       362: // 10 = email address
                    363: 
1.24      albertel  364: function flip(vf,tf) {
                    365:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    366:    var i;
1.49      matthew   367:    // make sure no other columns are labeled the same as this one
1.24      albertel  368:    for (i=0;i<=vf.nfields.value;i++) {
                    369:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    370:           eval('vf.f'+i+'.selectedIndex=0;')
                    371:       }
                    372:    }
1.49      matthew   373:    // If we set this to 'lastname, firstnames', clear out all the ones
                    374:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
1.24      albertel  375:    if (nw==2) {
                    376:       for (i=0;i<=vf.nfields.value;i++) {
                    377:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    378:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    379:              eval('vf.f'+i+'.selectedIndex=0;')
                    380:          }
                    381:       }
                    382:    }
1.49      matthew   383:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    384:    // clear out any that are set to 'lastname, firstnames' (2)
1.24      albertel  385:    if ((nw>=3) && (nw<=6)) {
                    386:       for (i=0;i<=vf.nfields.value;i++) {
                    387:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    388:              eval('vf.f'+i+'.selectedIndex=0;')
                    389:          }
                    390:       }
                    391:    }
1.49      matthew   392:    // If we set the password, make the password form below correspond to 
                    393:    // the new value.
1.24      albertel  394:    if (nw==9) {
1.28      matthew   395:        changed_radio('int',document.studentform);
                    396:        set_auth_radio_buttons('int',document.studentform);
                    397:        vf.intarg.value='';
                    398:        vf.krbarg.value='';
1.24      albertel  399:        vf.locarg.value='';
                    400:    }
                    401: }
                    402: 
                    403: function clearpwd(vf) {
                    404:     var i;
                    405:     for (i=0;i<=vf.nfields.value;i++) {
                    406:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    407:             eval('vf.f'+i+'.selectedIndex=0;')
                    408:         }
                    409:     }
                    410: }
                    411: 
                    412: ENDPICK
                    413: }
                    414: 
1.50      matthew   415: ###############################################################
                    416: ###############################################################
                    417: sub upload_manager_javascript_reverse_associate {
1.24      albertel  418:     return(<<ENDPICK);
                    419: function verify(vf) {
                    420:     var founduname=0;
                    421:     var foundpwd=0;
                    422:     var foundname=0;
                    423:     var foundid=0;
                    424:     var foundsec=0;
                    425:     var tw;
                    426:     for (i=0;i<=vf.nfields.value;i++) {
                    427:         tw=eval('vf.f'+i+'.selectedIndex');
                    428:         if (i==0 && tw!=0) { founduname=1; }
                    429:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    430:         if (i==6 && tw!=0) { foundid=1; }
                    431:         if (i==7 && tw!=0) { foundsec=1; }
                    432:         if (i==8 && tw!=0) { foundpwd=1; }
                    433:     }
                    434:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
                    435: }
                    436: 
                    437: function flip(vf,tf) {
                    438:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    439:    var i;
                    440:    // picked the all one one name field, reset the other name ones to blank
                    441:    if (tf==1 && nw!=0) {
                    442:       for (i=2;i<=5;i++) {
                    443:          eval('vf.f'+i+'.selectedIndex=0;')
                    444:       }
                    445:    }
                    446:    //picked one of the piecewise name fields, reset the all in
                    447:    //one field to blank
                    448:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    449:       eval('vf.f1.selectedIndex=0;')
                    450:    }
                    451:    // intial password specified, pick internal authentication
                    452:    if (tf==8 && nw!=0) {
1.28      matthew   453:        changed_radio('int',document.studentform);
                    454:        set_auth_radio_buttons('int',document.studentform);
                    455:        vf.krbarg.value='';
                    456:        vf.intarg.value='';
1.24      albertel  457:        vf.locarg.value='';
                    458:    }
                    459: }
                    460: 
                    461: function clearpwd(vf) {
                    462:     var i;
                    463:     if (eval('vf.f8.selectedIndex')!=0) {
                    464:         eval('vf.f8.selectedIndex=0;')
                    465:     }
                    466: }
1.2       www       467: ENDPICK
1.23      albertel  468: }
1.10      www       469: 
1.50      matthew   470: ###############################################################
                    471: ###############################################################
                    472: sub print_upload_manager_footer {
1.23      albertel  473:     my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
1.64      albertel  474: 
                    475:     my ($krbdef,$krbdefdom) =
                    476:         &Apache::loncommon::get_kerberos_defaults($defdom);
                    477:     my %param = ( formname => 'document.studentform',
                    478:                   kerb_def_dom => $krbdefdom,
                    479:                   kerb_def_auth => $krbdef
                    480:                   );
1.28      matthew   481:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    482:     my $intform = &Apache::loncommon::authform_internal(%param);
                    483:     my $locform = &Apache::loncommon::authform_local(%param);
1.31      matthew   484:     my $domform = &domain_form($defdom);
1.68      matthew   485:     my $date_table = &date_setting_table();
1.23      albertel  486:     $r->print(<<ENDPICK);
1.3       www       487: </table>
1.88    ! matthew   488: <input type="hidden" name="nfields" value="$i">
        !           489: <input type="hidden" name="keyfields" value="$keyfields">
1.3       www       490: <h3>Login Type</h3>
1.15      albertel  491: <p>Note: this will not take effect if the user already exists</p>
                    492: <p>
1.28      matthew   493: $krbform
1.15      albertel  494: </p>
                    495: <p>
1.28      matthew   496: $intform
1.15      albertel  497: </p>
                    498: <p>
1.28      matthew   499: $locform
1.15      albertel  500: </p>
1.5       www       501: <h3>LON-CAPA Domain for Students</h3>
1.29      matthew   502: LON-CAPA domain: $domform <p>
1.5       www       503: <h3>Starting and Ending Dates</h3>
1.68      matthew   504: <p>
                    505: $date_table
                    506: </p>
1.5       www       507: <h3>Full Update</h3>
                    508: <input type=checkbox name=fullup value=yes> Full update 
1.11      www       509: (also print list of users not enrolled anymore)<p>
1.18      www       510: <h3>ID/Student Number</h3>
                    511: <input type=checkbox name=forceid value=yes> 
                    512: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.19      www       513: (only do if you know what you are doing)<p>
1.36      albertel  514: <input type="button" onClick="javascript:verify(this.form)" value="Update Courselist" /><br />
1.50      matthew   515: Note: for large courses, this operation may be time consuming.
1.3       www       516: ENDPICK
1.23      albertel  517: }
1.24      albertel  518: 
1.23      albertel  519: # ======================================================= Menu Phase Two Upload
1.50      matthew   520: sub print_upload_manager_form {
1.23      albertel  521:     my $r=shift;
1.82      www       522:     my $firstLine;
1.24      albertel  523:     my $datatoken;
                    524:     if (!$ENV{'form.datatoken'}) {
1.26      matthew   525:       $datatoken=&Apache::loncommon::upfile_store($r);
1.24      albertel  526:     } else {
1.26      matthew   527:       $datatoken=$ENV{'form.datatoken'};
                    528:       &Apache::loncommon::load_tmp_file($r);
1.24      albertel  529:     }
                    530:     my @records=&Apache::loncommon::upfile_record_sep();
1.82      www       531:     if($ENV{'form.noFirstLine'}){$firstLine=shift(@records);}
1.23      albertel  532:     my $total=$#records;
                    533:     my $distotal=$total+1;
                    534:     my $today=time;
                    535:     my $halfyear=$today+15552000;
1.85      matthew   536:     my $defdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1.64      albertel  537:     my ($krbdef,$krbdefdom) =
                    538:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.50      matthew   539:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
1.24      albertel  540:     my $i;
                    541:     my $keyfields;
1.23      albertel  542:     if ($total>=0) {
1.50      matthew   543: 	my @d=(['username','Username'],
                    544:                ['names','Last Name, First Names'],
                    545: 	       ['fname','First Name'],
                    546:                ['mname','Middle Names/Initials'],
                    547: 	       ['lname','Last Name'],
                    548:                ['gen','Generation'],
                    549: 	       ['id','ID/Student Number'],
                    550:                ['sec','Group/Section'],
1.73      www       551: 	       ['ipwd','Initial Password'],
                    552:                ['email','EMail Address']);
1.24      albertel  553: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                    554: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                    555: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
                    556: 	    foreach (@d) { $keyfields.=$_->[0].','; }
                    557: 	    chop($keyfields);
                    558: 	} else {
                    559: 	    unshift(@d,['none','']);
                    560: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
                    561: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                    562: 	    $keyfields=join(',',sort(keys(%sone)));
1.23      albertel  563: 	}
                    564:     }
1.50      matthew   565:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
1.10      www       566: }
                    567: 
1.12      www       568: # ======================================================= Enroll single student
                    569: sub enroll_single_student {
                    570:     my $r=shift;
1.80      matthew   571:     # Remove non alphanumeric values from section
                    572:     $ENV{'form.csec'}=~s/\W//g;
1.68      matthew   573:     #
                    574:     # We do the dates first because the action of making them the defaul
                    575:     # in the course is entirely seperate from the action of enrolling the
                    576:     # student.  Also, a failure in setting the dates as default is not fatal
                    577:     # to the process of enrolling / modifying a student.
                    578:     my ($startdate,$enddate) = &get_dates_from_form();
                    579:     if ($ENV{'form.makedatesdefault'}) {
                    580:         $r->print(&make_dates_default($startdate,$enddate));
                    581:     }
                    582: 
1.12      www       583:     $r->print('<h3>Enrolling Student</h3>');
1.54      matthew   584:     $r->print('<p>Enrolling '.$ENV{'form.cuname'}." \@ ".
1.34      matthew   585:               $ENV{'form.lcdomain'}.'</p>');
1.12      www       586:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
1.31      matthew   587:         ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
                    588:         # Deal with home server selection
                    589:         my $domain=$ENV{'form.lcdomain'};
                    590:         my $desiredhost = $ENV{'form.lcserver'};
                    591:         if (lc($desiredhost) eq 'default') {
                    592:             $desiredhost = undef;
                    593:         } else {
1.45      matthew   594:             my %home_servers =&Apache::loncommon::get_library_servers($domain);
1.31      matthew   595:             if (! exists($home_servers{$desiredhost})) {
                    596:                 $r->print('<font color="#ff0000">Error:</font>'.
                    597:                           'Invalid home server specified');
                    598:                 return;
                    599:             }
                    600:         }
1.34      matthew   601:         $r->print(" with server $desiredhost :") if (defined($desiredhost));
1.31      matthew   602:         # End of home server selection logic
1.12      www       603: 	my $amode='';
                    604:         my $genpwd='';
                    605:         if ($ENV{'form.login'} eq 'krb') {
1.47      albertel  606:            $amode='krb';
                    607: 	   $amode.=$ENV{'form.krbver'};
1.28      matthew   608:            $genpwd=$ENV{'form.krbarg'};
1.12      www       609:         } elsif ($ENV{'form.login'} eq 'int') {
1.26      matthew   610:            $amode='internal';
1.28      matthew   611:            $genpwd=$ENV{'form.intarg'};
1.15      albertel  612:         }  elsif ($ENV{'form.login'} eq 'loc') {
                    613: 	    $amode='localauth';
                    614: 	    $genpwd=$ENV{'form.locarg'};
                    615: 	    if (!$genpwd) { $genpwd=" "; }
                    616: 	}
1.34      matthew   617:         my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
                    618:                                                    $ENV{'form.lcdomain'});
                    619:         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
1.55      matthew   620:             # Clean out any old roles the student has in this class.
1.33      matthew   621:             &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                    622:                            $ENV{'request.course.id'},$ENV{'form.csec'},
                    623:                             $desiredhost);
1.55      matthew   624:             my $login_result = &Apache::lonnet::modifystudent
                    625:                 ($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                    626:                  $ENV{'form.cstid'},$amode,$genpwd,
                    627:                  $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
                    628:                  $ENV{'form.clast'},$ENV{'form.cgen'},
1.67      matthew   629:                  $ENV{'form.csec'},$enddate,
                    630:                  $startdate,$ENV{'form.forceid'},
1.55      matthew   631:                  $desiredhost);
                    632:             if ($login_result =~ /^ok/) {
                    633:                 $r->print($login_result);
                    634:                 $r->print("<p> If active, the new role will be available ".
                    635:                           "when the student next logs in to LON-CAPA.</p>");
                    636:             } else {
                    637:                 $r->print("unable to enroll: ".$login_result);
                    638:             }
1.12      www       639: 	} else {
1.79      matthew   640:             $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;');
                    641:             if ($amode =~ /^krb/) {
                    642:                 $r->print('Missing Kerberos domain information.  ');
                    643:             } else {
                    644:                 $r->print('Invalid login mode or password.  ');
                    645:             }
                    646:             $r->print('<b>Unable to enroll '.$ENV{'form.cuname'}.'.</b></p>');
                    647:         }
1.12      www       648:     } else {
                    649:         $r->print('Invalid username or domain');
1.26      matthew   650:     }    
1.12      www       651: }
                    652: 
1.68      matthew   653: sub setup_date_selectors {
                    654:     my ($starttime,$endtime) = @_;
                    655:     if (! defined($starttime)) {
                    656:         $starttime = time;
                    657:         if (exists($ENV{'course.'.$ENV{'request.course.id'}.
                    658:                             '.default_enrollment_start_date'})) {
                    659:             $starttime = $ENV{'course.'.$ENV{'request.course.id'}.
                    660:                                   '.default_enrollment_start_date'};
                    661:         }
                    662:     }
                    663:     if (! defined($endtime)) {
                    664:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                    665:         if (exists($ENV{'course.'.$ENV{'request.course.id'}.
                    666:                             '.default_enrollment_end_date'})) {
                    667:             $endtime = $ENV{'course.'.$ENV{'request.course.id'}.
                    668:                                 '.default_enrollment_end_date'};
                    669:         }
                    670:     }
                    671:     my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
                    672:                                                             'startdate',
                    673:                                                             $starttime);
                    674:     my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
                    675:                                                           'enddate',
                    676:                                                           $endtime);
                    677:     return ($startdateform,$enddateform);
                    678: }
                    679: 
                    680: sub get_dates_from_form {
                    681:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
                    682:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate');
                    683:     if ($ENV{'form.no_end_date'}) {
                    684:         $enddate = 0;
                    685:     }
                    686:     return ($startdate,$enddate);
                    687: }
                    688: 
                    689: sub date_setting_table {
                    690:     my ($starttime,$endtime) = @_;
                    691:     my ($startform,$endform)=&setup_date_selectors($starttime,$endtime);
                    692:     my $dateDefault = '<nobr>'.
                    693:         '<input type="checkbox" name="makedatesdefault" />'.
                    694:         ' make these dates the default for future enrollment';
                    695:     my $perpetual = '<nobr><input type="checkbox" name="no_end_date"';
                    696:     if (defined($endtime) && $endtime == 0) {
                    697:         $perpetual .= ' checked';
                    698:     }
                    699:     $perpetual.= ' />'.' no ending date</nobr>';
                    700:     my $result = '';
                    701:     $result .= "<table>\n";
                    702:     $result .= '<tr><td align="right">Starting Date</td>'.
                    703:         '<td>'.$startform.'</td>'.
                    704:         '<td>'.$dateDefault.'</td>'."</tr>\n";
                    705:     $result .= '<tr><td align="right">Ending Date</td>'.
                    706:         '<td>'.$endform.'</td>'.
                    707:         '<td>'.$perpetual.'</td>'."</tr>\n";
                    708:     $result .= "</table>\n";
                    709:     return $result;
                    710: }
                    711: 
                    712: sub make_dates_default {
                    713:     my ($startdate,$enddate) = @_;
                    714:     my $result = '';
                    715:     my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    716:     my $crs = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    717:     my $put_result = &Apache::lonnet::put('environment',
                    718:             {'default_enrollment_start_date'=>$startdate,
                    719:              'default_enrollment_end_date'  =>$enddate},$dom,$crs);
                    720:     if ($put_result eq 'ok') {
                    721:         $result .= "Set default start and end dates for course<br />";
1.69      matthew   722:         #
                    723:         # Refresh the course environment
                    724:         &Apache::lonnet::coursedescription($ENV{'request.course.id'});
1.68      matthew   725:     } else {
                    726:         $result .= "Unable to set default dates for course:".$put_result.
                    727:             '<br />';
                    728:     }
                    729:     return $result;
                    730: }
                    731: 
1.74      matthew   732: ##
                    733: ## Single student enrollment routines (some of them)
                    734: ##
                    735: sub get_student_username_domain_form {
                    736:     my $r = shift;
                    737:     my $domform = &Apache::loncommon::select_dom_form
1.85      matthew   738:         ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'},'cudomain',0);
1.74      matthew   739:     $r->print(<<END);
                    740: <input type="hidden" name="action" value="enrollstudent" />
                    741: <input type="hidden" name="state"  value="gotusername" />
                    742: <h3>Enroll One Student</h3>
                    743: <table>
                    744: <tr><th>Username:</th>
                    745:     <td><input type="text" name="cuname"  size="15" /></td></tr>
                    746: <tr><th>Domain:</th>
                    747:     <td>$domform</td></tr>
                    748: <tr><th>&nbsp;</th>
                    749:     <td>
                    750:     <input type="submit" name="Begin Enrollment" value="Begin Enrollment" />
                    751:     </td></tr>
                    752: </table>
                    753: END
                    754:     return;
                    755: }
                    756: 
1.50      matthew   757: sub print_enroll_single_student_form {
1.10      www       758:     my $r=shift;
1.40      matthew   759:     $r->print("<h3>Enroll One Student</h3>");
1.74      matthew   760:     #
                    761:     my $username = $ENV{'form.cuname'};
                    762:     my $domain   = $ENV{'form.cudomain'};
                    763:     my $home = &Apache::lonnet::homeserver($username,$domain);
                    764:     # $new_user flags whether we are creating a new user or using an old one
                    765:     my $new_user = 1;
                    766:     if ($home ne 'no_host') {
                    767:         $new_user = 0;
                    768:     }
                    769:     #
                    770:     my $user_data_html = '';
                    771:     my $javascript_validations = '';
                    772:     if ($new_user) {
1.85      matthew   773:         my $defdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1.74      matthew   774:         # Set up authentication forms
                    775:         my ($krbdef,$krbdefdom) =
1.75      matthew   776:             &Apache::loncommon::get_kerberos_defaults($domain);
1.74      matthew   777:         $javascript_validations=&javascript_validations($krbdefdom);
                    778:         my %param = ( formname => 'document.studentform',
                    779:                       kerb_def_dom => $krbdefdom,
                    780:                       kerb_def_auth => $krbdef
                    781:                       );
                    782:         my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    783:         my $intform = &Apache::loncommon::authform_internal(%param);
                    784:         my $locform = &Apache::loncommon::authform_local(%param);
                    785:         #
                    786:         # Set up domain selection form
                    787:         my $homeserver_form = '';
                    788:         my %servers = &Apache::loncommon::get_library_servers($domain);
                    789:         $homeserver_form = '<select name="lcserver" size="1">'."\n".
                    790:             '<option value="default" selected>default</option>'."\n";
                    791:         while (my ($servername,$serverdescription) = each (%servers)) {
                    792:             $homeserver_form .= '<option value="'.$servername.'">'.
                    793:                 $serverdescription."</option>\n";
                    794:         }
                    795:         $homeserver_form .= "</select>\n";
                    796:         #
                    797:         #
                    798:         $user_data_html = <<END;
                    799: <h3>User Data for $username\@$domain</h3>
                    800: <table>
                    801: <tr><th>First Name:</th>
                    802:     <td><input type="text" name="cfirst"  size="15"></td></tr>
                    803: <tr><th>Middle Name:</th>
                    804:     <td><input type="text" name="cmiddle" size="15"></td></tr>
                    805: <tr><th>Last Name:</th>
                    806:     <td><input type="text" name="clast"   size="15"></td></tr>
                    807: <tr><th>Generation:</th>
                    808:     <td><input type="text" name="cgen"    size="5"> </td></tr>
                    809: <tr><th>Home Server:</th>
                    810:     <td>$homeserver_form</td></tr>
                    811: </table>
                    812: <h3>Password</h3>
                    813: Please select an authentication mechanism
                    814: <table>
                    815: <p>
                    816: $krbform
1.75      matthew   817: <br />
1.74      matthew   818: $intform
1.75      matthew   819: <br />
1.74      matthew   820: $locform
                    821: </p>
                    822: END
                    823:     } else {
                    824:         # User already exists.  Do not worry about authentication
                    825:         my %uenv = &Apache::lonnet::dump('environment',$domain,$username);
                    826:         $javascript_validations = &javascript_validations_without_auth();
                    827:         $user_data_html = <<END;
                    828: <h3>User Data for $username\@$domain</h3>
                    829: <input type="hidden" name="lcserver" value="default" />
                    830: <table>
                    831: <tr><th>First Name:</th>
                    832:     <td>
                    833:     <input type="text" name="cfirst" value="$uenv{'firstname'}" size="15" />
                    834:     </td></tr>
                    835: <tr><th>Middle Name:</th>
                    836:     <td>
                    837:     <input type="text" name="cmiddle" value="$uenv{'middlename'}" size="15" />
                    838:     </td></tr>
                    839: <tr><th>Last Name:</th>
                    840:     <td>
                    841:     <input type="text" name="clast"value="$uenv{'lastname'}" size="15" />
                    842:     </td></tr>
                    843: <tr><th>Generation:</th>
                    844:     <td>
                    845:     <input type="text" name="cgen" value="$uenv{'generation'}" size="5" />
                    846:     </td></tr>
                    847: </table>
                    848: END
                    849:     }
1.68      matthew   850:     my $date_table = &date_setting_table();
1.74      matthew   851:         # Print it all out
1.50      matthew   852:     $r->print(<<END);
1.74      matthew   853: <input type="hidden" name="action" value="enrollstudent" />
                    854: <input type="hidden" name="state"  value="done" />
                    855: <input type="hidden" name="cuname" value="$username" />
                    856: <input type="hidden" name="lcdomain" value="$domain" />
1.28      matthew   857: <script type="text/javascript" language="Javascript">
1.12      www       858: function verify(vf) {
                    859:     var founduname=0;
                    860:     var foundpwd=0;
                    861:     var foundname=0;
                    862:     var foundid=0;
                    863:     var foundsec=0;
                    864:     var tw;
1.26      matthew   865:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
1.31      matthew   866: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
1.12      www       867:         founduname=1;
                    868:     }
1.14      harris41  869:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
1.26      matthew   870: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
1.12      www       871:         foundname=1;
                    872:     }
1.14      harris41  873:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
1.12      www       874:         foundsec=1;
                    875:     }
1.14      harris41  876:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
1.12      www       877: 	foundid=1;
                    878:     }
                    879:     if (founduname==0) {
                    880: 	alert('You need to specify at least the username and domain fields');
                    881:         return;
                    882:     }
1.24      albertel  883:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
1.12      www       884: }
                    885: 
1.24      albertel  886: $javascript_validations
1.12      www       887: 
1.24      albertel  888: function clearpwd(vf) {
                    889:     //nothing else needs clearing
1.15      albertel  890: }
                    891: 
1.12      www       892: </script>
1.11      www       893: 
1.74      matthew   894: $user_data_html
1.50      matthew   895: 
                    896: <h3>Course Data</h3>
                    897: 
                    898: <p>Group/Section: <input type="text" name="csec" size="5" />
                    899: <p>
1.68      matthew   900: $date_table
1.50      matthew   901: </p>
                    902: <h3>ID/Student Number</h3>
                    903: <p>
                    904: ID/Student Number: <input type="text" name="cstid" size="10">
1.26      matthew   905: </p><p>
                    906: <input type="checkbox" name="forceid" value="yes"> 
1.18      www       907: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.50      matthew   908: (only do if you know what you are doing)
                    909: </p><p>
                    910: <input type="button" onClick="verify(this.form)" value="Enroll as student">
1.26      matthew   911: </p>
1.50      matthew   912: END
                    913:     return;
1.10      www       914: }
                    915: 
                    916: # ========================================================= Menu Phase Two Drop
1.51      matthew   917: sub print_drop_menu {
1.10      www       918:     my $r=shift;
1.40      matthew   919:     $r->print("<h3>Drop Students</h3>");
1.11      www       920:     my $cid=$ENV{'request.course.id'};
1.56      matthew   921:     my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
                    922:     if (! defined($classlist)) {
1.27      matthew   923:         $r->print("There are no students currently enrolled.\n");
1.51      matthew   924:         return;
1.25      matthew   925:     }
1.51      matthew   926:     # Print out the available choices
1.56      matthew   927:     &show_drop_list($r,$classlist,$keylist);
1.51      matthew   928:     return;
1.11      www       929: }
                    930: 
1.40      matthew   931: # ============================================== view classlist
1.50      matthew   932: sub print_html_classlist {
1.40      matthew   933:     my $r=shift;
1.57      matthew   934:     if (! exists($ENV{'form.sortby'})) {
                    935:         $ENV{'form.sortby'} = 'username';
                    936:     }
1.59      matthew   937:     if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
1.57      matthew   938:         $ENV{'form.Status'} = 'Active';
                    939:     }
                    940:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
                    941:         ($ENV{'form.Status'},'studentform');
1.48      matthew   942:     $r->print(<<END);
1.58      matthew   943: <input type="hidden" name="action" value="$ENV{'form.action'}" />
1.57      matthew   944: <input type="hidden" name="state"  value="" />
1.50      matthew   945: <p>
1.76      albertel  946: <font size="+1">Current Class List</font>
1.57      matthew   947: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.59      matthew   948: END
                    949:     if ($ENV{'form.action'} ne 'modifystudent') {
                    950:         $r->print(<<END);
1.57      matthew   951: <font size="+1">
                    952: <a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
1.61      matthew   953: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.60      matthew   954: <a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
1.57      matthew   955: </font>
                    956: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.61      matthew   957: Student Status:
1.48      matthew   958: END
1.59      matthew   959:     }
                    960:     $r->print($status_select."</p>\n");
1.40      matthew   961:     my $cid=$ENV{'request.course.id'};
1.56      matthew   962:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                    963:     if (! defined($classlist)) {
1.40      matthew   964:         $r->print("There are no students currently enrolled.\n");
                    965:     } else {
                    966:         # Print out the available choices
1.50      matthew   967:         if ($ENV{'form.action'} eq 'modifystudent') {
1.53      matthew   968:             &show_class_list($r,'view','modify','modifystudent',
1.57      matthew   969:                              $ENV{'form.Status'},$classlist,$keylist);
1.50      matthew   970:         } else {
1.53      matthew   971:             &show_class_list($r,'view','aboutme','classlist',
1.57      matthew   972:                              $ENV{'form.Status'},$classlist,$keylist);
1.50      matthew   973:         }
1.41      matthew   974:     }
                    975: }
                    976: 
                    977: # ============================================== view classlist
1.60      matthew   978: sub print_formatted_classlist {
1.41      matthew   979:     my $r=shift;
1.60      matthew   980:     my $mode = shift;
1.41      matthew   981:     my $cid=$ENV{'request.course.id'};
1.56      matthew   982:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                    983:     if (! defined($classlist)) {
1.41      matthew   984:         $r->print("There are no students currently enrolled.\n");
                    985:     } else {
1.60      matthew   986:         &show_class_list($r,$mode,'nolink','csv',
1.57      matthew   987:                          $ENV{'form.Status'},$classlist,$keylist);
1.40      matthew   988:     }
                    989: }
                    990: 
                    991: # =================================================== Show student list to drop
                    992: sub show_class_list {
1.56      matthew   993:     my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
1.40      matthew   994:     my $cid=$ENV{'request.course.id'};
1.60      matthew   995:     #
                    996:     # Variables for excel output
                    997:     my ($excel_workbook, $excel_sheet, $excel_filename,$row);
                    998:     #
1.53      matthew   999:     my $sortby = $ENV{'form.sortby'};
                   1000:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
                   1001:         $sortby = 'username';
                   1002:     }
1.42      matthew  1003:     # Print out header 
1.41      matthew  1004:     if ($mode eq 'view') {
1.50      matthew  1005:         if ($linkto eq 'aboutme') {
1.56      matthew  1006:             $r->print('Select a user name to view the users personal page.');
1.50      matthew  1007:         } elsif ($linkto eq 'modify') {
                   1008:             $r->print('Select a user name to modify the students information');
                   1009:         }
1.41      matthew  1010:         $r->print(<<END);
1.59      matthew  1011: 
1.53      matthew  1012: <input type="hidden" name="sortby" value="$sortby" />
1.59      matthew  1013: <input type="hidden" name="sname"  value="" />
                   1014: <input type="hidden" name="sdom"   value="" />
1.40      matthew  1015: <p>
                   1016: <table border=2>
1.53      matthew  1017: <tr><th>
1.57      matthew  1018:        <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">username</a>
1.53      matthew  1019:     </th><th>
1.57      matthew  1020:        <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">domain</a>
1.53      matthew  1021:     </th><th>
1.57      matthew  1022:        <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
1.53      matthew  1023:     </th><th>
1.57      matthew  1024:        <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">student name</a>
1.53      matthew  1025:     </th><th>
1.57      matthew  1026:        <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">section</a>
1.53      matthew  1027:     </th>
                   1028: </tr>
1.40      matthew  1029: END
1.41      matthew  1030:     } elsif ($mode eq 'csv') {
1.58      matthew  1031:         if($statusmode eq 'Expired') {
                   1032:             $r->print('"Students with expired roles"');
                   1033:         }
                   1034:         if ($statusmode eq 'Any') {
                   1035:             $r->print('"'.join('","',("username","domain","ID","student name",
                   1036:                                       "section","status")).'"'."\n");
                   1037:         } else {
                   1038:             $r->print('"'.join('","',("username","domain","ID","student name",
                   1039:                                       "section")).'"'."\n");
                   1040:         }
1.60      matthew  1041:     } elsif ($mode eq 'excel') {
                   1042:         # Create the excel spreadsheet
                   1043:         $excel_filename = '/prtspool/'.
                   1044:             $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                   1045:                 time.'_'.rand(1000000000).'.xls';
                   1046:         $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
                   1047:                                                        $excel_filename);
                   1048:         $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
                   1049:         $excel_sheet = $excel_workbook->addworksheet('classlist');
                   1050:         #
1.76      albertel 1051:         my $description = 'Class List for '.
1.60      matthew  1052:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                   1053:         $excel_sheet->write($row++,0,$description);
                   1054:         #
                   1055:         $excel_sheet->write($row++,0,["username","domain","ID",
                   1056:                                       "student name","section","status"]);
1.41      matthew  1057:     }
1.56      matthew  1058:     #
                   1059:     # Sort the students
                   1060:     my %index;
                   1061:     my $i;
                   1062:     foreach (@$keylist) {
                   1063:         $index{$_} = $i++;
                   1064:     }
                   1065:     my $index  = $index{$sortby};
                   1066:     my $second = $index{'username'};
                   1067:     my $third  = $index{'domain'};
1.53      matthew  1068:     my @Sorted_Students = sort {
1.56      matthew  1069:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   1070:             ||
                   1071:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   1072:             ||
                   1073:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
                   1074:         } (keys(%$classlist));
1.53      matthew  1075:     foreach my $student (@Sorted_Students) {
1.56      matthew  1076:         my $username = $classlist->{$student}->[$index{'username'}];
                   1077:         my $domain   = $classlist->{$student}->[$index{'domain'}];
                   1078:         my $section  = $classlist->{$student}->[$index{'section'}];
                   1079:         my $name     = $classlist->{$student}->[$index{'fullname'}];
                   1080:         my $id       = $classlist->{$student}->[$index{'id'}];
                   1081:         my $status   = $classlist->{$student}->[$index{'status'}];
1.57      matthew  1082:         next if (($statusmode ne 'Any') && ($status ne $statusmode));
1.51      matthew  1083:         if ($mode eq 'view') {
                   1084:             $r->print("<tr>\n    <td>\n        ");
                   1085:             if ($linkto eq 'nothing') {
                   1086:                 $r->print($username);
                   1087:             } elsif ($linkto eq 'aboutme') {
                   1088:                 $r->print(&Apache::loncommon::aboutmewrapper($username,
                   1089:                                                              $username,
                   1090:                                                              $domain));
                   1091:             } elsif ($linkto eq 'modify') {
1.59      matthew  1092:                 $r->print('<a href="'.
                   1093:                           "javascript:document.studentform.sname.value='".
                   1094:                           $username.
                   1095:                           "';document.studentform.sdom.value='".$domain.
                   1096:                           "';document.studentform.state.value='selected".
                   1097:                           "';document.studentform.submit();".'">'.
1.53      matthew  1098:                           $username."</a>\n");
1.50      matthew  1099:             }
1.51      matthew  1100:             $r->print(<<"END");
1.50      matthew  1101:     </td>
1.51      matthew  1102:     <td>$domain</td>
                   1103:     <td>$id</td>
                   1104:     <td>$name</td>
                   1105:     <td>$section</td>
1.40      matthew  1106: </tr>
                   1107: END
1.51      matthew  1108:         } elsif ($mode eq 'csv') {
                   1109:             # no need to bother with $linkto
                   1110:             my @line = ();
                   1111:             foreach ($username,$domain,$id,$name,$section) {
                   1112:                 push @line,&Apache::loncommon::csv_translate($_);
1.58      matthew  1113:             }
                   1114:             if ($statusmode eq 'Any') {
                   1115:                 push @line,&Apache::loncommon::csv_translate($status);
1.41      matthew  1116:             }
1.51      matthew  1117:             my $tmp = $";
                   1118:             $" = '","';
                   1119:             $r->print("\"@line\"\n");
                   1120:             $" = $tmp;
1.60      matthew  1121:         } elsif ($mode eq 'excel') {
                   1122:             $excel_sheet->write($row++,0,[$username,$domain,$id,
                   1123:                                           $name,$section,$status]);
1.40      matthew  1124:         }
                   1125:     }
1.60      matthew  1126:     if ($mode eq 'view') {
                   1127:         $r->print('</table><br>');
                   1128:     } elsif ($mode eq 'excel') {
                   1129:         $excel_workbook->close();
                   1130:         $r->print('<p><a href="'.$excel_filename.'">'.
                   1131:                   'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
                   1132:     }
1.40      matthew  1133: }
                   1134: 
1.50      matthew  1135: 
                   1136: #
                   1137: # print out form for modification of a single students data
                   1138: #
                   1139: sub print_modify_student_form {
                   1140:     my $r = shift();
                   1141:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.59      matthew  1142:                                             ['sdom','sname']);    
1.53      matthew  1143:     my $sname  = $ENV{'form.sname'};
                   1144:     my $sdom   = $ENV{'form.sdom'};
                   1145:     my $sortby = $ENV{'form.sortby'};
1.50      matthew  1146:     # determine the students name information
                   1147:     my %info=&Apache::lonnet::get('environment',
                   1148:                                   ['firstname','middlename',
1.52      matthew  1149:                                    'lastname','generation','id'],
1.50      matthew  1150:                                   $sdom, $sname);
                   1151:     my ($tmp) = keys(%info);
                   1152:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1153:         $r->print('<font color="#ff0000" size="+2">Error</font>'.
                   1154:                   '<p>'.
                   1155:                   'Unable to retrieve environment data for '.$sname.
                   1156:                   'in domain '.$sdom.'</p><p>'.
                   1157:                   'Please contact your LON-CAPA administrator '.
                   1158:                   'regarding this situation.</p></body></html>');
                   1159:         return;
                   1160:     }
                   1161:     # determine the students starting and ending times and section
                   1162:     my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
1.87      matthew  1163:     if ($starttime =~ /^error/) {
                   1164:         $r->print('<h2>Error</h2>');
                   1165:         $r->print('<p>'.$starttime.'</p>');
                   1166:         return;
                   1167:     }
1.50      matthew  1168:     # Deal with date forms
1.68      matthew  1169:     my $date_table = &date_setting_table($starttime,$endtime);
1.59      matthew  1170:     #
                   1171:     if (! exists($ENV{'form.Status'}) || 
                   1172:         $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
                   1173:         $ENV{'form.Status'} = 'crap';
                   1174:     }
1.50      matthew  1175:     # Make sure student is enrolled in course    
                   1176:     $r->print(<<END);
1.52      matthew  1177: <p>
                   1178: <font size="+1">
                   1179: Only domain coordinators can change a users password.
                   1180: </font>
                   1181: </p>
1.50      matthew  1182: <input type="hidden" name="slogin"  value="$sname"  />
                   1183: <input type="hidden" name="sdomain" value="$sdom" />
                   1184: <input type="hidden" name="action"  value="modifystudent" />
1.53      matthew  1185: <input type="hidden" name="state"   value="done" />
                   1186: <input type="hidden" name="sortby"  value="$sortby" />
1.59      matthew  1187: <input type="hidden" name="Status"  value="$ENV{'form.Status'}" />
                   1188: 
1.50      matthew  1189: <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'} 
                   1190: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
                   1191: <p>
                   1192: <b>Student Name</b>
                   1193: <table>
                   1194: <tr><th>First</th><th>Middle</th><th>Last</th><th>Generation</th></tr>
                   1195: <tr><td>
                   1196: <input type="text" name="firstname"  value="$info{'firstname'}"  /></td><td>
                   1197: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
                   1198: <input type="text" name="lastname"   value="$info{'lastname'}"   /></td><td>
                   1199: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
                   1200: </table>
                   1201: </p><p>
1.52      matthew  1202: <b>Student ID</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
                   1203: </p><p>
1.53      matthew  1204: <input type="checkbox" name="forceid" > 
                   1205: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
                   1206: (only do if you know what you are doing)
                   1207: </p><p>
1.50      matthew  1208: <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
                   1209: </p>
1.68      matthew  1210: <p>$date_table</p>
1.50      matthew  1211: <input type="submit" value="Submit Modifications" />
                   1212: </body></html>
                   1213: END
                   1214:     return;
                   1215: }
                   1216: 
                   1217: #
                   1218: # modify a single students section 
                   1219: #
                   1220: sub modify_single_student {
                   1221:     my $r = shift;
1.68      matthew  1222:     #
1.80      matthew  1223:     # Remove non alphanumeric values from the section
                   1224:     $ENV{'form.section'} =~ s/\W//g;
1.77      matthew  1225:     #
1.68      matthew  1226:     # Do the date defaults first
                   1227:     my ($starttime,$endtime) = &get_dates_from_form();
                   1228:     if ($ENV{'form.makedatesdefault'}) {
                   1229:         $r->print(&make_dates_default($starttime,$endtime));
                   1230:     }
1.59      matthew  1231:     # Get the 'sortby' and 'Status' variables so the user goes back to their
                   1232:     # previous screen
1.53      matthew  1233:     my $sortby = $ENV{'form.sortby'};
1.59      matthew  1234:     my $status = $ENV{'form.Status'};
1.53      matthew  1235:     #
                   1236:     # We always need this information
                   1237:     my $slogin     = $ENV{'form.slogin'};
                   1238:     my $sdom       = $ENV{'form.sdomain'};
                   1239:     #
                   1240:     # Get the old data
                   1241:     my %old=&Apache::lonnet::get('environment',
                   1242:                                  ['firstname','middlename',
                   1243:                                   'lastname','generation','id'],
                   1244:                                  $sdom, $slogin);
1.59      matthew  1245:     $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
                   1246:                                                   $ENV{'request.course.id'});
1.53      matthew  1247:     my ($tmp) = keys(%old);
                   1248:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1249:         $r->print("There was an error determining the environment values ".
                   1250:                   " for $slogin \@ $sdom.");
                   1251:         return;
                   1252:     }
                   1253:     undef $tmp;
                   1254:     #
                   1255:     # Get the new data
1.50      matthew  1256:     my $firstname  = $ENV{'form.firstname'};
                   1257:     my $middlename = $ENV{'form.middlename'};
                   1258:     my $lastname   = $ENV{'form.lastname'};
                   1259:     my $generation = $ENV{'form.generation'};
                   1260:     my $section    = $ENV{'form.section'};
                   1261:     my $courseid   = $ENV{'request.course.id'};
1.52      matthew  1262:     my $sid        = $ENV{'form.id'};
1.50      matthew  1263:     my $displayable_starttime = localtime($starttime);
                   1264:     my $displayable_endtime   = localtime($endtime);
1.53      matthew  1265:     # 
                   1266:     # check for forceid override
1.63      matthew  1267:     if ((defined($old{'id'})) && ($old{'id'} ne '') && 
                   1268:         ($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
1.53      matthew  1269:         $r->print("<font color=\"ff0000\">You changed the students id ".
                   1270:                   " but did not disable the ID change safeguard.".
                   1271:                   "  The students id will not be changed.</font>");
                   1272:         $sid = $old{'id'};
                   1273:     }
                   1274:     #
1.50      matthew  1275:     # talk to the user about what we are going to do
                   1276:     $r->print(<<END);
1.53      matthew  1277:     <h2>Modifying data for user $slogin \@ $sdom </h2>
1.50      matthew  1278: <h3>Student Information</h3>
1.53      matthew  1279: <table rules="rows" border="1" cellpadding="3" >
                   1280: <tr>
                   1281:     <th> Field </th>
                   1282:     <th> Old Value </th>
                   1283:     <th> New Value </th>
                   1284: </tr>
                   1285: <tr>
                   1286:     <td> <b>First name</b> </td>
                   1287:     <td> $old{'firstname'} </td>
                   1288:     <td> $firstname </td>
                   1289: </tr><tr>
                   1290:     <td> <b>Middle name</b> </td>
                   1291:     <td> $old{'middlename'} </td>
                   1292:     <td> $middlename </td>
                   1293: </tr><tr>
                   1294:     <td> <b>Last name</b> </td>
                   1295:     <td> $old{'lastname'} </td>
                   1296:     <td> $lastname </td>
                   1297: </tr><tr>
                   1298:     <td> <b>Generation</b> </td>
                   1299:     <td> $old{'generation'} </td>
                   1300:     <td> $generation </td>
                   1301: </tr><tr>
                   1302:     <td> <b>ID</b> </td>
                   1303:     <td> $old{'id'} </td>
                   1304:     <td> $sid </td>
1.59      matthew  1305: </tr><tr>
                   1306:     <td> <b>Section</b> </td>
                   1307:     <td> $old{'section'} </td>
                   1308:     <td> $section</td>
1.53      matthew  1309: </tr>
1.50      matthew  1310: </table>
                   1311: <h3>Role Information</h3>
                   1312: <table>
1.68      matthew  1313: <tr><td align="right"><b>Start Time:</b></td><td> $displayable_starttime </td></tr>
                   1314: <tr><td align="right"><b>End Time:</b></td><td> $displayable_endtime   </td></tr>
1.50      matthew  1315: </table>
1.52      matthew  1316: <p>
1.50      matthew  1317: END
1.53      matthew  1318:     #
1.63      matthew  1319:     # Send request(s) to modify data (final undef is for 'desiredhost',
                   1320:     # which is a moot point because the student already has an account.
                   1321:     my $modify_section_results = &modifystudent($sdom,$slogin,
                   1322:                                                 $ENV{'request.course.id'},
                   1323:                                                 $section,undef);
                   1324:     if ($modify_section_results !~ /^ok/) {
                   1325:         $r->print("An error occured during the attempt to change the ".
                   1326:                   "section for this student.<br />");
                   1327:     }
1.52      matthew  1328:     my $roleresults = &Apache::lonnet::modifystudent
1.53      matthew  1329:         ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
                   1330:          $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
                   1331:     if ($roleresults eq 'refused' ) {
1.50      matthew  1332:         $r->print("Your request to change the role information for this ".
1.53      matthew  1333:                   "student was refused.  You do not appear to have ".
                   1334:                   "sufficient authority to change student information.");
1.50      matthew  1335:     } elsif ($roleresults !~ /ok/) {
                   1336:         $r->print("An error occurred during the attempt to change the role".
1.52      matthew  1337:                   " information for this student.  <br />".
                   1338:                   "The error reported was ".
1.50      matthew  1339:                   $roleresults);
1.53      matthew  1340:         &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
                   1341:                                  " data for ".$slogin." \@ ".$sdom." by ".
                   1342:                                  $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
                   1343:                                  ":".$roleresults);
1.50      matthew  1344:     } else { # everything is okay!
1.52      matthew  1345:         $r->print("Student information updated successfully. <br />".
                   1346:                   "The student must log out and log in again to see ".
                   1347:                   "these changes.");
1.50      matthew  1348:     }
                   1349:     $r->print(<<END);
1.52      matthew  1350: </p><p>
1.59      matthew  1351: <input type="hidden" name="action" value="modifystudent" />
                   1352: <input type="hidden" name="sortby" value="$sortby" />
                   1353: <input type="hidden" name="Status" value="$status" />
                   1354: <a href="javascript:document.studentform.submit();">Modify another students data</a>
1.50      matthew  1355: </body></html>
                   1356: END
                   1357:     return;
                   1358: }
                   1359: 
                   1360: sub get_enrollment_data {
                   1361:     my ($sname,$sdomain) = @_;
                   1362:     my $courseid = $ENV{'request.course.id'};
                   1363:     $courseid =~ s:_:/:g;
                   1364:     my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
                   1365:     my ($tmp) = keys(%roles);
                   1366:     # Bail out if we were unable to get the students roles
1.87      matthew  1367:     return ('error'.$tmp) if ($tmp =~ /^(con_lost|error|no_such_host)/i);
1.50      matthew  1368:     # Go through the roles looking for enrollment in this course
                   1369:     my ($end,$start) = (undef,undef);
                   1370:     my $section = '';
                   1371:     my $count = scalar(keys(%roles));
                   1372:     while (my ($course,$role) = each(%roles)) {
                   1373:         if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
                   1374:             #
                   1375:             # Get active role
                   1376:             $section=$1;
                   1377:             (undef,$end,$start)=split(/\_/,$role);
                   1378:             my $now=time;
                   1379:             my $notactive=0;
                   1380:             if ($start) {
                   1381:                 if ($now<$start) { $notactive=1; }
                   1382:             }
                   1383:             if ($end) {
                   1384:                 if ($now>$end) { $notactive=1; }
                   1385:             } 
                   1386:             unless ($notactive) { return ($start,$end,$section); }
                   1387:         }
                   1388:     }
                   1389:     return ($start,$end,$section);
                   1390: }
                   1391: 
1.56      matthew  1392: #################################################
                   1393: #################################################
                   1394: 
                   1395: =pod
                   1396: 
                   1397: =item show_drop_list
                   1398: 
                   1399: Display a list of students to drop
                   1400: Inputs: 
                   1401: 
                   1402: =over 4
                   1403: 
                   1404: =item $r, Apache request
                   1405: 
                   1406: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
                   1407: 
                   1408: =item $keylist, array pointer returned from loncoursedata::get_classlist() 
                   1409: which describes the order elements are stored in the %$classlist values.
                   1410: 
                   1411: =item $nosort, if true, sorting links are omitted.
                   1412: 
                   1413: =back
                   1414: 
                   1415: =cut
                   1416: 
                   1417: #################################################
                   1418: #################################################
1.11      www      1419: sub show_drop_list {
1.56      matthew  1420:     my ($r,$classlist,$keylist,$nosort)=@_;
1.11      www      1421:     my $cid=$ENV{'request.course.id'};
1.59      matthew  1422:     if (! exists($ENV{'form.sortby'})) {
                   1423:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1424:                                                 ['sortby']);
                   1425:     }
1.54      matthew  1426:     my $sortby = $ENV{'form.sortby'};
                   1427:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
                   1428:         $sortby = 'username';
                   1429:     }
1.56      matthew  1430:     #
1.54      matthew  1431:     my $action = "drop";
                   1432:     $r->print(<<END);
                   1433: <input type="hidden" name="sortby" value="$sortby" />
                   1434: <input type="hidden" name="action" value="$action" />
1.50      matthew  1435: <input type="hidden" name="state"  value="done" />
1.32      matthew  1436: <script>
1.51      matthew  1437: function checkAll(field) {
1.32      matthew  1438:     for (i = 0; i < field.length; i++)
                   1439:         field[i].checked = true ;
                   1440: }
                   1441: 
1.51      matthew  1442: function uncheckAll(field) {
1.32      matthew  1443:     for (i = 0; i < field.length; i++)
                   1444:         field[i].checked = false ;
                   1445: }
                   1446: </script>
                   1447: <p>
1.26      matthew  1448: <input type="hidden" name="phase" value="four">
1.56      matthew  1449: END
                   1450: 
                   1451:     if ($nosort) {
                   1452:         $r->print(<<END);
                   1453: <table border=2>
                   1454: <tr>
                   1455:     <th>&nbsp;</th>
                   1456:     <th>username</th>
                   1457:     <th>domain</th>
                   1458:     <th>ID</th>
                   1459:     <th>student name</th>
                   1460:     <th>section</th>
                   1461: </tr>
                   1462: END
                   1463: 
                   1464:     } else  {
                   1465:         $r->print(<<END);
1.26      matthew  1466: <table border=2>
1.54      matthew  1467: <tr><th>&nbsp;</th>
                   1468:     <th>
                   1469:        <a href="/adm/dropadd?action=$action&sortby=username">username</a>
                   1470:     </th><th>
                   1471:        <a href="/adm/dropadd?action=$action&sortby=domain">domain</a>
                   1472:     </th><th>
                   1473:        <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
                   1474:     </th><th>
                   1475:        <a href="/adm/dropadd?action=$action&sortby=fullname">student name</a>
                   1476:     </th><th>
                   1477:        <a href="/adm/dropadd?action=$action&sortby=section">section</a>
                   1478:     </th>
                   1479: </tr>
1.26      matthew  1480: END
1.56      matthew  1481:     }
                   1482:     #
                   1483:     # Sort the students
                   1484:     my %index;
                   1485:     my $i;
                   1486:     foreach (@$keylist) {
                   1487:         $index{$_} = $i++;
                   1488:     }
                   1489:     my $index  = $index{$sortby};
                   1490:     my $second = $index{'username'};
                   1491:     my $third  = $index{'domain'};
1.54      matthew  1492:     my @Sorted_Students = sort {
1.56      matthew  1493:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   1494:             ||
                   1495:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   1496:             ||
                   1497:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
                   1498:         } (keys(%$classlist));
1.54      matthew  1499:     foreach my $student (@Sorted_Students) {
1.52      matthew  1500:         my $error;
1.56      matthew  1501:         my $username = $classlist->{$student}->[$index{'username'}];
                   1502:         my $domain   = $classlist->{$student}->[$index{'domain'}];
                   1503:         my $section  = $classlist->{$student}->[$index{'section'}];
                   1504:         my $name     = $classlist->{$student}->[$index{'fullname'}];
                   1505:         my $id       = $classlist->{$student}->[$index{'id'}];
                   1506:         my $status   = $classlist->{$student}->[$index{'status'}];
1.51      matthew  1507:         next if ($status ne 'Active');
                   1508:         #
                   1509:         $r->print(<<"END");
1.26      matthew  1510: <tr>
1.51      matthew  1511:     <td><input type="checkbox" name="droplist" value="$student"></td>
                   1512:     <td>$username</td>
                   1513:     <td>$domain</td>
                   1514:     <td>$id</td>
                   1515:     <td>$name</td>
                   1516:     <td>$section</td>
1.26      matthew  1517: </tr>
                   1518: END
1.25      matthew  1519:     }
                   1520:     $r->print('</table><br>');
1.32      matthew  1521:     $r->print(<<"END");
                   1522: </p><p>
                   1523: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
                   1524: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
                   1525: <p><input type=submit value="Drop Students"></p>
                   1526: END
1.51      matthew  1527:     return;
1.10      www      1528: }
                   1529: 
1.48      matthew  1530: #
                   1531: # Print out the initial form to get the courselist file
                   1532: #
                   1533: sub print_first_courselist_upload_form {
                   1534:     my $r=shift;
1.88    ! matthew  1535:     my $str;
        !          1536:     $str  = '<input type="hidden" name="phase" value="two">';
        !          1537:     $str .= '<input type="hidden" name="action" value="upload" />';
        !          1538:     $str .= '<input type="hidden"   name="state"  value="got_file" />';
        !          1539:     $str .= "<h3>".&mt('Upload a class list')."</h3>\n";
        !          1540:     $str .= &Apache::loncommon::upfile_select_html();
        !          1541:     $str .= "<p>\n";
        !          1542:     $str .= '<input type="submit" name="fileupload" value="'.
        !          1543:         &mt('Upload class list').'">'."\n";
        !          1544:     $str .= '<input type="checkbox" name="noFirstLine" /> '.
        !          1545:         &mt('Ignore First Line')."</p>\n";
        !          1546:     $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
        !          1547:                          "How do I create a class list from a spreadsheet").
        !          1548:                              "<br />\n";
        !          1549:     $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
        !          1550:                            "How do I create a CSV file from a spreadsheet").
        !          1551:                                "<br />\n";
        !          1552:     $str .= "</body>\n</html>\n";
        !          1553:     $r->print($str);
1.48      matthew  1554:     return;
                   1555: }
                   1556: 
1.10      www      1557: # ================================================= Drop/Add from uploaded file
                   1558: sub upfile_drop_add {
                   1559:     my $r=shift;
1.24      albertel 1560:     &Apache::loncommon::load_tmp_file($r);
                   1561:     my @studentdata=&Apache::loncommon::upfile_record_sep();
1.82      www      1562:     if($ENV{'form.noFirstLine'}){shift(@studentdata);}
1.26      matthew  1563:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   1564:     my $cid = $ENV{'request.course.id'};
1.25      matthew  1565:     my %fields=();
1.26      matthew  1566:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
1.25      matthew  1567:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1568:             if ($ENV{'form.f'.$i} ne 'none') {
                   1569:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   1570:             }
                   1571:         } else {
                   1572:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   1573:         }
                   1574:     }
1.26      matthew  1575:     #
1.68      matthew  1576:     my ($startdate,$enddate) = &get_dates_from_form();
                   1577:     if ($ENV{'form.makedatesdefault'}) {
                   1578:         $r->print(&make_dates_default($startdate,$enddate));
                   1579:     }
1.31      matthew  1580:     # Determine domain and desired host (home server)
1.25      matthew  1581:     my $domain=$ENV{'form.lcdomain'};
1.31      matthew  1582:     my $desiredhost = $ENV{'form.lcserver'};
                   1583:     if (lc($desiredhost) eq 'default') {
                   1584:         $desiredhost = undef;
                   1585:     } else {
1.45      matthew  1586:         my %home_servers = &Apache::loncommon::get_library_servers($domain);
1.31      matthew  1587:         if (! exists($home_servers{$desiredhost})) {
1.88    ! matthew  1588:             $r->print('<font color="#ff0000">'.&mt('Error').'</font>'.
        !          1589:                       &mt('Invalid home server specified'));
        !          1590:             $r->print("</body>\n</html>\n");
1.31      matthew  1591:             return;
                   1592:         }
                   1593:     }
1.26      matthew  1594:     # Determine authentication mechanism
                   1595:     my $amode  = '';
                   1596:     my $genpwd = '';
1.25      matthew  1597:     if ($ENV{'form.login'} eq 'krb') {
1.47      albertel 1598:         $amode='krb';
                   1599: 	$amode.=$ENV{'form.krbver'};
1.28      matthew  1600:         $genpwd=$ENV{'form.krbarg'};
1.25      matthew  1601:     } elsif ($ENV{'form.login'} eq 'int') {
                   1602:         $amode='internal';
1.28      matthew  1603:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
                   1604:             $genpwd=$ENV{'form.intarg'};
1.25      matthew  1605:         }
                   1606:     } elsif ($ENV{'form.login'} eq 'loc') {
                   1607:         $amode='localauth';
                   1608:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
                   1609:             $genpwd=$ENV{'form.locarg'};
1.79      matthew  1610:         }
                   1611:     }
                   1612:     if ($amode =~ /^krb/) {
                   1613:         if (! defined($genpwd) || $genpwd eq '') {
                   1614:             $r->print('<font color="red" size="+1">'.
1.88    ! matthew  1615:                       &mt('Unable to enroll students').'</font>  '.
        !          1616:                       &mt('No Kerberos domain was specified.').'</p>');
1.79      matthew  1617:             $amode = ''; # This causes the loop below to be skipped
1.25      matthew  1618:         }
                   1619:     }
                   1620:     unless (($domain=~/\W/) || ($amode eq '')) {
1.26      matthew  1621:         #######################################
                   1622:         ##         Enroll Students           ##
                   1623:         #######################################
1.88    ! matthew  1624:         $r->print('<h3>'.&mt('Enrolling Students')."</h3>\n<p>\n");
1.25      matthew  1625:         my $count=0;
                   1626:         my $flushc=0;
                   1627:         my %student=();
1.26      matthew  1628:         # Get new classlist
1.25      matthew  1629:         foreach (@studentdata) {
                   1630:             my %entries=&Apache::loncommon::record_sep($_);
1.26      matthew  1631:             # Determine student name
1.25      matthew  1632:             unless (($entries{$fields{'username'}} eq '') ||
                   1633:                     (!defined($entries{$fields{'username'}}))) {
1.26      matthew  1634:                 my ($fname, $mname, $lname,$gen) = ('','','','');
1.25      matthew  1635:                 if (defined($fields{'names'})) {
1.26      matthew  1636:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   1637:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
1.25      matthew  1638:                 } else {
                   1639:                     if (defined($fields{'fname'})) {
                   1640:                         $fname=$entries{$fields{'fname'}};
                   1641:                     }
                   1642:                     if (defined($fields{'mname'})) {
                   1643:                         $mname=$entries{$fields{'mname'}};
                   1644:                     }
                   1645:                     if (defined($fields{'lname'})) {
                   1646:                         $lname=$entries{$fields{'lname'}};
                   1647:                     }
                   1648:                     if (defined($fields{'gen'})) {
                   1649:                         $gen=$entries{$fields{'gen'}};
                   1650:                     }
                   1651:                 }
                   1652:                 if ($entries{$fields{'username'}}=~/\W/) {
1.88    ! matthew  1653:                     $r->print('<br />'.
        !          1654:       &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
        !          1655:           $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
        !          1656:                               '</b>');
1.25      matthew  1657:                 } else {
1.26      matthew  1658:                     # determine section number
1.25      matthew  1659:                     my $sec='';
                   1660:                     my $username=$entries{$fields{'username'}};
                   1661:                     if (defined($fields{'sec'})) {
                   1662:                         if (defined($entries{$fields{'sec'}})) {
                   1663:                             $sec=$entries{$fields{'sec'}};
                   1664:                         }
                   1665:                     }
1.80      matthew  1666:                     # remove non alphanumeric values from section
                   1667:                     $sec =~ s/\W//g;
1.26      matthew  1668:                     # determine student id number
1.25      matthew  1669:                     my $id='';
                   1670:                     if (defined($fields{'id'})) {
                   1671:                         if (defined($entries{$fields{'id'}})) {
                   1672:                             $id=$entries{$fields{'id'}};
                   1673:                         }
                   1674:                         $id=~tr/A-Z/a-z/;
                   1675:                     }
1.73      www      1676:                     # determine email address
                   1677:                     my $email='';
                   1678:                     if (defined($fields{'email'})) {
                   1679:                         if (defined($entries{$fields{'email'}})) {
                   1680:                             $email=$entries{$fields{'email'}};
                   1681:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   1682:                         }
                   1683:                     }
1.26      matthew  1684:                     # determine student password
1.25      matthew  1685:                     my $password='';
                   1686:                     if ($genpwd) { 
                   1687:                         $password=$genpwd; 
                   1688:                     } else {
                   1689:                         if (defined($fields{'ipwd'})) {
                   1690:                             if ($entries{$fields{'ipwd'}}) {
                   1691:                                 $password=$entries{$fields{'ipwd'}};
                   1692:                             }
                   1693:                         }
                   1694:                     }
1.56      matthew  1695:                     # Clean up whitespace
                   1696:                     foreach (\$domain,\$username,\$id,\$fname,\$mname,
                   1697:                              \$lname,\$gen,\$sec) {
                   1698:                         $$_ =~ s/(\s+$|^\s+)//g;
                   1699:                     }
1.84      albertel 1700:                     if ($password || $ENV{'form.login'} eq 'loc') {
1.33      matthew  1701:                         &modifystudent($domain,$username,$cid,$sec,
                   1702:                                        $desiredhost);
1.25      matthew  1703:                         my $reply=&Apache::lonnet::modifystudent
                   1704:                             ($domain,$username,$id,$amode,$password,
                   1705:                              $fname,$mname,$lname,$gen,$sec,$enddate,
1.73      www      1706:                              $startdate,$ENV{'form.forceid'},$desiredhost,
                   1707:                              $email);
1.26      matthew  1708:                         if ($reply ne 'ok') {
1.72      matthew  1709:                             $reply =~ s/^error://;
1.88    ! matthew  1710:                             $r->print('<br />'.
        !          1711:                 &mt('<b>[_1]</b>:  Unable to enroll: [_2]',$username,$reply));
1.10      www      1712:          		} else {
1.7       www      1713:                             $count++; $flushc++;
                   1714:                             $student{$username}=1;
1.6       www      1715:                             $r->print('. ');
1.7       www      1716:                             if ($flushc>15) {
                   1717: 				$r->rflush;
                   1718:                                 $flushc=0;
                   1719:                             }
1.6       www      1720:                         }
1.25      matthew  1721:                     } else {
1.88    ! matthew  1722:                         $r->print('<br />'.
        !          1723:       &mt('<b>[_1]</b>: Unable to enroll.  No password specified.',$username)
        !          1724:                                   );
1.25      matthew  1725:                     }
                   1726:                 }
1.26      matthew  1727:             }
                   1728:         } # end of foreach (@studentdata)
1.88    ! matthew  1729:         $r->print("</p>\n<p>\n".&mt('Processed [_1] student(s).',$count).
        !          1730:                   "</p>\n");
        !          1731:         $r->print("<p>\n".
        !          1732:                   &mt('If active, the new role will be available when the '.
        !          1733:                   'students next log in to LON-CAPA.')."</p>\n");
1.26      matthew  1734:         #####################################
                   1735:         #           Drop students           #
                   1736:         #####################################
1.25      matthew  1737:         if ($ENV{'form.fullup'} eq 'yes') {
1.88    ! matthew  1738:             $r->print('<h3>'.&mt('Dropping Students')."</h3>\n");
1.26      matthew  1739:             #  Get current classlist
1.56      matthew  1740:             my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                   1741:             if (! defined($classlist)) {
1.88    ! matthew  1742:                 $r->print(&mt('There are no students currently enrolled.').
        !          1743:                           "\n");
1.56      matthew  1744:             } else {
                   1745:                 # Remove the students we just added from the list of students.
1.25      matthew  1746:                 foreach (@studentdata) {
                   1747:                     my %entries=&Apache::loncommon::record_sep($_);
                   1748:                     unless (($entries{$fields{'username'}} eq '') ||
                   1749:                             (!defined($entries{$fields{'username'}}))) {
1.56      matthew  1750:                         delete($classlist->{$entries{$fields{'username'}}.
1.26      matthew  1751:                                                 ':'.$domain});
1.25      matthew  1752:                     }
                   1753:                 }
1.56      matthew  1754:                 # Print out list of dropped students.
                   1755:                 &show_drop_list($r,$classlist,$keylist,'nosort');
1.25      matthew  1756:             }
                   1757:         }
1.26      matthew  1758:     } # end of unless
1.10      www      1759: }
                   1760: 
1.11      www      1761: # ================================================================== Phase four
                   1762: sub drop_student_list {
                   1763:     my $r=shift;
                   1764:     my $count=0;
1.35      matthew  1765:     my @droplist;
                   1766:     if (ref($ENV{'form.droplist'})) {
                   1767:         @droplist = @{$ENV{'form.droplist'}};
                   1768:     } else {
                   1769:         @droplist = ($ENV{'form.droplist'});
                   1770:     }
                   1771:     foreach (@droplist) {
1.26      matthew  1772:         my ($uname,$udom)=split(/\:/,$_);
1.56      matthew  1773:         # drop student
1.35      matthew  1774:         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
1.37      matthew  1775:         if ($result eq 'ok' || $result eq 'ok:') {
1.88    ! matthew  1776:             $r->print(&mt('Dropped [_1]',$uname.'@'.$udom).'<br>');
1.59      matthew  1777:             $count++;
1.35      matthew  1778:         } else {
1.88    ! matthew  1779:             $r->print(
        !          1780:           &mt('Error dropping [_1]:[_2]',$uname.'@'.$udom,$result).
1.35      matthew  1781:                       '<br />');
                   1782:         }
1.20      harris41 1783:     }
1.88    ! matthew  1784:     $r->print('<p><b>'.&mt('Dropped [_1] student(s).',$count).'</b></p>');
        !          1785:     $r->print('<p>'.&mt('Re-enrollment will re-activate data.')) if ($count);
1.11      www      1786: }
                   1787: 
1.50      matthew  1788: ###################################################################
                   1789: ###################################################################
                   1790: 
                   1791: =pod
                   1792: 
                   1793: =item &handler
                   1794: 
                   1795: The typical handler you see in all these modules.  Takes $r, the
                   1796: http request, as an argument.  
                   1797: 
                   1798: The response to the request is governed by two form variables
                   1799: 
                   1800:  form.action      form.state     response
                   1801:  ---------------------------------------------------
                   1802:  undefined        undefined      print main menu
                   1803:  upload           undefined      print courselist upload menu
                   1804:  upload           got_file       deal with uploaded file,
                   1805:                                  print the upload managing menu
                   1806:  upload           enrolling      enroll students based on upload
                   1807:  drop             undefined      print the classlist ready to drop
                   1808:  drop             done           drop the selected students
1.74      matthew  1809:  enrollstudent    undefined      print student username domain form
                   1810:  enrollstudent    gotusername    print single student enroll menu
1.50      matthew  1811:  enrollstudent    enrolling      enroll student
                   1812:  classlist        undefined      print html classlist
                   1813:  classlist        csv            print csv classlist
                   1814:  modifystudent    undefined      print classlist to select student to modify
                   1815:  modifystudent    selected       print modify student menu
                   1816:  modifystudent    done           make modifications to student record
                   1817: 
                   1818: =cut
                   1819: 
                   1820: ###################################################################
                   1821: ###################################################################
1.10      www      1822: sub handler {
1.26      matthew  1823:     my $r=shift;
                   1824:     if ($r->header_only) {
1.86      www      1825:         &Apache::loncommon::content_type($r,'text/html');
1.26      matthew  1826:         $r->send_http_header;
                   1827:         return OK;
                   1828:     }
1.48      matthew  1829:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.50      matthew  1830:                                             ['action','state']);
1.26      matthew  1831:     #  Needs to be in a course
1.50      matthew  1832:     if (! (($ENV{'request.course.fn'}) &&
                   1833:           (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
                   1834:         # Not in a course, or not allowed to modify parms
                   1835:         $ENV{'user.error.msg'}=
                   1836:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
                   1837:         return HTTP_NOT_ACCEPTABLE; 
                   1838:     }
                   1839:     #
                   1840:     # Only output the header information if they did not request csv format
                   1841:     #
                   1842:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
                   1843:         $r->content_type('text/csv');
                   1844:     } else {
1.26      matthew  1845:         # Start page
1.86      www      1846:         &Apache::loncommon::content_type($r,'text/html');
1.26      matthew  1847:         $r->send_http_header;
1.50      matthew  1848:         $r->print(&header());
                   1849:     }
                   1850:     #
                   1851:     # Main switch on form.action and form.state, as appropriate
                   1852:     if (! exists($ENV{'form.action'})) {
                   1853:         &print_main_menu($r);
                   1854:     } elsif ($ENV{'form.action'} eq 'upload') {
                   1855:         if (! exists($ENV{'form.state'})) {
                   1856:             &print_first_courselist_upload_form($r);            
                   1857:         } elsif ($ENV{'form.state'} eq 'got_file') {
                   1858:             &print_upload_manager_form($r);
                   1859:         } elsif ($ENV{'form.state'} eq 'enrolling') {
1.26      matthew  1860:             if ($ENV{'form.datatoken'}) {
                   1861:                 &upfile_drop_add($r);
1.50      matthew  1862:             } else {
                   1863:                 # Hmmm, this is an error
1.26      matthew  1864:             }
1.50      matthew  1865:         } else {
                   1866:             &print_first_courselist_upload_form($r);            
1.26      matthew  1867:         }
1.50      matthew  1868:     } elsif ($ENV{'form.action'} eq 'drop') {
                   1869:         if (! exists($ENV{'form.state'})) {
1.51      matthew  1870:             &print_drop_menu($r);
1.50      matthew  1871:         } elsif ($ENV{'form.state'} eq 'done') {
1.26      matthew  1872:             &drop_student_list($r);
1.50      matthew  1873:         } else {
1.55      matthew  1874:             &print_drop_menu($r);
1.26      matthew  1875:         }
1.50      matthew  1876:     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
                   1877:         if (! exists($ENV{'form.state'})) {
1.74      matthew  1878:             &get_student_username_domain_form($r);
                   1879:         } elsif ($ENV{'form.state'} eq 'gotusername') {
1.50      matthew  1880:             &print_enroll_single_student_form($r);
                   1881:         } elsif ($ENV{'form.state'} eq 'enrolling') {
1.26      matthew  1882:             &enroll_single_student($r);
1.50      matthew  1883:         } else {
1.74      matthew  1884:             &get_student_username_domain_form($r);
1.26      matthew  1885:         }
1.50      matthew  1886:     } elsif ($ENV{'form.action'} eq 'classlist') {
                   1887:         if (! exists($ENV{'form.state'})) {
                   1888:             &print_html_classlist($r);
                   1889:         } elsif ($ENV{'form.state'} eq 'csv') {
1.60      matthew  1890:             &print_formatted_classlist($r,'csv');
                   1891:         } elsif ($ENV{'form.state'} eq 'excel') {
                   1892:             &print_formatted_classlist($r,'excel');
1.50      matthew  1893:         } else {
                   1894:             &print_html_classlist($r);
                   1895:         }
                   1896:     } elsif ($ENV{'form.action'} eq 'modifystudent') {
                   1897:         if (! exists($ENV{'form.state'})) {
                   1898:             &print_html_classlist($r);
                   1899:         } elsif ($ENV{'form.state'} eq 'selected') {
                   1900:             &print_modify_student_form($r);
                   1901:         } elsif ($ENV{'form.state'} eq 'done') {
                   1902:             &modify_single_student($r);
                   1903:         } else {
                   1904:             &print_html_classlist($r);
                   1905:         }        
                   1906:     } else {
                   1907:         # We should not end up here, but I guess it is possible
                   1908:         &Apache::lonnet::logthis("Undetermined state in londropadd.pm.  ".
                   1909:                                  "form.action = ".$ENV{'form.action'}.
                   1910:                                  "Someone should fix this.");
                   1911:         &print_main_menu($r);
                   1912:     }
                   1913:     #
                   1914:     # Finish up
                   1915:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
                   1916:         $r->print("\n");
1.26      matthew  1917:     } else {
1.50      matthew  1918:         $r->print('</form></body></html>');
1.26      matthew  1919:     }
                   1920:     return OK;
1.1       www      1921: }
                   1922: 
1.50      matthew  1923: ###################################################################
                   1924: ###################################################################
                   1925: 
1.1       www      1926: 1;
                   1927: __END__
1.50      matthew  1928: 
1.1       www      1929: 

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