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