Difference between revisions of "ClasslistGenerator"

From WeBWorK_wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
  +
<pre>
 
  +
#!/usr/bin/perl
#!/usr/bin/perl
 
  +
 
  +
#Classlist generator script
#Classlist generator script
 
  +
# This script takes in a CSV file (such as those generated by Banner)
# This script takes in a CSV file (such as those generated by Banner)
 
  +
# and creates a lst file which can be used to import students into a class.
# and creates a lst file which can be used to import students into a class.
 
  +
 
  +
#Currently the script assumes that the input file is a CSV file with the
#Currently the script assumes that the input file is a CSV file with the
 
  +
#following fields
#following fields
 
  +
## STUDENT_ID, LAST_NAME, FIRST_NAME, MI, STREET1, STREET2, CITY, STATE, ZIP, textbox15, EMAIL, PHONE_1, GENDER, ETHNICITY, PHONE, textbox23, textbox32, HONORS, TERM, SUBJECT, COURSE_NUM, SEC, DEPT, REG_STAT, TITLE, INSTRUCTOR
## STUDENT_ID, LAST_NAME, FIRST_NAME, MI, STREET1, STREET2, CITY, STATE, ZIP, textbox15, EMAIL, PHONE_1, GENDER, ETHNICITY, PHONE, textbox23, textbox32, HONORS, TERM, SUBJECT, COURSE_NUM, SEC, DEPT, REG_STAT, TITLE, INSTRUCTOR
 
  +
 
  +
#The first line of the CSV file should contain lables and data entries are
#The first line of the CSV file should contain lables and data entries are
 
  +
#only seperated by commas. Currently the script strips off the student ID, the
#only seperated by commas. Currently the script strips off the student ID, the
 
  +
#first and last names, the section number and email address and uses them to
#first and last names, the section number and email address and uses them to
+
# build the lst file. The username for the student is the email address username.
# build the lst file. The username for the student is the email address username.
+
+
#Those who are familiar with perl scripts should be able to modify this setup
#Those who are familiar with perl scripts should be able to modify this setup
+
#to deal with input files that have a different format.
#to deal with input files that have a different format.
+
+
if (! $#ARGV eq 1) {
if (! $#ARGV eq 1) {
+
print "Usage: ./classistgenerator <banner csv> <target classlist>\n";
print "Usage: ./classistgenerator <banner csv> <target classlist>\n";
+
exit;
exit;
+
}
}
+
+
#Open source and target files as provided via command line
#Open source and target files as provided via command line
+
open (SOURCE, "<", $ARGV[0]) or die "Can't open source file.\n";
open (SOURCE, "<", $ARGV[0]) or die "Can't open source file.\n";
+
+
open (TARGET, ">", $ARGV[1]) or die "Can't open target file.\n";
open (TARGET, ">", $ARGV[1]) or die "Can't open target file.\n";
+
+
#Burn first line (which has labels) and then go through the input file
#Burn first line (which has labels) and then go through the input file
+
#line by line
#line by line
+
<SOURCE>;
<SOURCE>;
+
while ($line = <SOURCE>) {
+
while ($line = <SOURCE>) {
+
#split line up on commas
+
@data = split(/,/,$line);
#split line up on commas
+
@data = split(/,/,$line);
+
#pull off username from email (assumes email is the 10th entry)
+
$username = $data[10];
#pull off username from email (assumes email is the 10th entry)
+
$username =~ s/@.*//;
$username = $data[10];
 
$username =~ s/@.*//;
 
 
 
#print new csv
+
#print new csv
#follows lst format. assumes that student id is first entry, last name
+
# follows lst format. assumes that student id is first entry of csv, last name
# is second entry etc....
+
# is second entry etc....
print TARGET "$data[0],$data[1],$data[2],c,,$data[21],,$data[10],$username\n";
+
print TARGET "$data[0],$data[1],$data[2],c,,$data[21],,$data[10],$username\n";
}
+
}
+
close TARGET or die "Can't close target file.\n";
+
close TARGET or die "Can't close target file.\n";
+
</pre>
 
   
 
[[Category:Scripts]]
 
[[Category:Scripts]]

Latest revision as of 14:38, 3 January 2013

 #!/usr/bin/perl
 
 #Classlist generator script
 # This script takes in a CSV file (such as those generated by Banner)
 # and creates a lst file which can be used to import students into a class. 
 
 #Currently the script assumes that the input file is a CSV file with the 
 #following fields
 ## STUDENT_ID, LAST_NAME, FIRST_NAME, MI, STREET1, STREET2, CITY, STATE, ZIP, textbox15, EMAIL, PHONE_1, GENDER, ETHNICITY, PHONE,  textbox23, textbox32, HONORS, TERM, SUBJECT, COURSE_NUM, SEC, DEPT, REG_STAT, TITLE, INSTRUCTOR
 
 #The first line of the CSV file should contain lables and data entries are 
 #only seperated by commas.  Currently the script strips off the student ID, the 
 #first and last names, the section number and email address and uses them to 
 # build the lst file.  The username for the student is the email address username.  
 
 #Those who are familiar with perl scripts should be able to modify this setup 
 #to deal with input files that have a different format.  
 
 if (! $#ARGV eq 1) {
     print "Usage: ./classistgenerator <banner csv> <target classlist>\n";
     exit;
 }
 
 #Open source and target files as provided via command line
 open (SOURCE, "<", $ARGV[0]) or die "Can't open source file.\n"; 
 
 open (TARGET, ">", $ARGV[1]) or die "Can't open target file.\n";
 
 #Burn first line (which has labels) and then go through the input file 
 #line by line
 <SOURCE>; 
 while ($line = <SOURCE>) {
 
     #split line up on commas
     @data = split(/,/,$line);
 
     #pull off username from email (assumes email is the 10th entry)
     $username = $data[10];
     $username =~ s/@.*//;
    
     #print new csv
     # follows lst format.  assumes that student id is first entry of csv, last name
     # is second entry etc.... 
     print TARGET "$data[0],$data[1],$data[2],c,,$data[21],,$data[10],$username\n";
 }
 
 close TARGET or die "Can't close target file.\n";