[system] / trunk / webwork-modperl / bin / hash2sql Repository:
ViewVC logotype

View of /trunk/webwork-modperl/bin/hash2sql

Parent Directory Parent Directory | Revision Log Revision Log


Revision 922 - (download) (annotate)
Wed May 28 01:18:11 2003 UTC (9 years, 11 months ago) by sh002i
File size: 4965 byte(s)
added hash2sql conversion script.
-sam

    1 #!/usr/bin/env perl
    2 
    3 ################################################################################
    4 # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
    5 # $Id: hash2sql,v 1.1 2003-05-28 01:18:11 sh002i Exp $
    6 ################################################################################
    7 
    8 =head1 NAME
    9 
   10 hash2sql - copies data from a course using a hash-based database (without global tables)
   11 to a course using an sql-based database (with global tables).
   12 
   13 =cut
   14 
   15 use strict;
   16 use warnings;
   17 use FindBin;
   18 use lib "$FindBin::Bin/../lib";
   19 use WeBWorK::CourseEnvironment;
   20 use WeBWorK::DB;
   21 use WeBWorK::DB::Utils qw(findDefaults);
   22 
   23 sub main(@) {
   24 	my ($hashCourse, $sqlCourse) = @_;
   25 	
   26 	unless ($ENV{WEBWORK_ROOT}) {
   27 		die "WEBWORK_ROOT not found in environment.\n";
   28 	}
   29 	
   30 	unless ($hashCourse and $sqlCourse) {
   31 		die "usage: $0 hashCourse sqlCourse\n";
   32 	}
   33 	
   34 	my $hashCE = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "", $hashCourse);
   35 	my $hashDB = WeBWorK::DB->new($hashCE);
   36 	
   37 	my $sqlCE = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "", $sqlCourse);
   38 	my $sqlDB = WeBWorK::DB->new($sqlCE);
   39 	
   40 	# get names of global record classes
   41 	my $globalSetClass = $sqlCE->{dbLayout}->{set}->{record};
   42 	my $globalProblemClass = $sqlCE->{dbLayout}->{problem}->{record};
   43 	
   44 	# get non-key, global field names
   45 	my @setFields = getNonKeyFields($sqlCE->{dbLayout}->{set}->{record});
   46 	my @problemFields = getNonKeyFields($sqlCE->{dbLayout}->{problem}->{record});
   47 	
   48 	# hash to store all sets
   49 	my %allSets;
   50 	
   51 	# populate user, password, permission, and key tables
   52 	print "\n---------- adding users: ----------\n\n";
   53 	
   54 	foreach my $userID ($hashDB->listUsers()) {
   55 		print "adding user $userID:";
   56 		$sqlDB->addUser($hashDB->getUser($userID));
   57 		
   58 		my $Password = $hashDB->getPassword($userID);
   59 		if ($Password) {
   60 			print " +password";
   61 			$sqlDB->addPassword($Password);
   62 		} else {
   63 			print " -password";
   64 		}
   65 		
   66 		my $PermissionLevel = $hashDB->getPermissionLevel($userID);
   67 		if ($PermissionLevel) {
   68 			print " +permissionLevel";
   69 			$sqlDB->addPermissionLevel($PermissionLevel);
   70 		} else {
   71 			print " -permissionLevel";
   72 		}
   73 		
   74 		my $Key = $hashDB->getKey($userID);
   75 		if ($Key) {
   76 			print " +key";
   77 			$sqlDB->addKey($Key) if $Key;
   78 		} else {
   79 			print " -key";
   80 		}
   81 		
   82 		print "\n";
   83 		
   84 		# also get the names of all sets
   85 		foreach my $setID ($hashDB->listUserSets($userID)) {
   86 			$allSets{$setID}++;
   87 		}
   88 	}
   89 	
   90 	print "\n---------- found these sets: ----------\n\n";
   91 	
   92 	print join(" ", keys %allSets), "\n";
   93 	
   94 	print "\n---------- determining set defaults: ----------\n\n";
   95 	
   96 	foreach my $setID (keys %allSets) {
   97 		print "processing set $setID.\n";
   98 		# get a consensus view for each set
   99 		my @setUserIDs = $hashDB->listSetUsers($setID);
  100 		my @UserSets = map { $hashDB->getUserSet($_, $setID) } @setUserIDs;
  101 		my $GlobalSet = findDefaults($globalSetClass, @UserSets);
  102 		print "adding global set record.\n";
  103 		$sqlDB->addGlobalSet($GlobalSet);
  104 
  105 		# remove default values from each user set
  106 		# and store into sql db.
  107 		foreach my $UserSet (@UserSets) {
  108 			foreach my $field (@setFields) {
  109 				if ($UserSet->$field() eq $GlobalSet->$field()) {
  110 					$UserSet->$field(undef);
  111 				}
  112 			}
  113 			print "adding user set for user ", $UserSet->user_id, ".\n";
  114 			$sqlDB->addUserSet($UserSet);
  115 		}
  116 		
  117 		# get all problems in this set
  118 		my %allProblems;
  119 		foreach my $userID ($hashDB->listSetUsers($setID)) {
  120 			foreach my $problemID ($hashDB->listUserProblems($userID, $setID)) {
  121 				$allProblems{$problemID}++;
  122 			}
  123 		}
  124 		
  125 		print "\n----- found these problems in set $setID: ----\n\n";
  126 		
  127 		print join(" ", keys %allProblems), "\n";
  128 		
  129 	print "\n----- determining defaults for problems in set $setID: -----\n\n";
  130 	
  131 		# get a consensus for each problem in this set
  132 		foreach my $problemID (keys %allProblems) {
  133 			print "determining defaults for problem $problemID.\n";
  134 			my @problemUserIDs = $hashDB->listProblemUsers($setID, $problemID);
  135 			my @UserProblems;
  136 			print "getting user problem for user:";
  137 			foreach (@problemUserIDs) {
  138 				print " $_";
  139 				my $UserProblem = $hashDB->getUserProblem($_, $setID, $problemID);
  140 				unless (defined $UserProblem) {
  141 					print "(UNDEFINED!)";
  142 					next;
  143 				}
  144 				push @UserProblems, $UserProblem;
  145 			}
  146 			print "\n";
  147 			my $GlobalProblem = findDefaults($globalProblemClass, @UserProblems);
  148 			print "adding global problem record.\n";
  149 			$sqlDB->addGlobalProblem($GlobalProblem);
  150 			
  151 			# remove defaults from each user problem
  152 			foreach my $UserProblem (@UserProblems) {
  153 				foreach my $field (@problemFields) {
  154 					if ($UserProblem->$field() eq $GlobalProblem->$field()) {
  155 						$UserProblem->$field(undef);
  156 					}
  157 				}
  158 				print "adding user problem for user ", $UserProblem->user_id, ".\n";
  159 				$sqlDB->addUserProblem($UserProblem);
  160 			}
  161 		}
  162 	}
  163 }
  164 
  165 sub getNonKeyFields($) {
  166 	my ($Record) = @_;
  167 	
  168 	my %fields = map { $_ => {} } $Record->FIELDS();
  169 	delete $fields{$_} foreach $Record->KEYFIELDS();
  170 	return keys %fields;
  171 }
  172 
  173 main(@ARGV);

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9