| … | |
… | |
| 169 | Enrolled => { allow_course_access => 1, include_in_assignment => 1, include_in_stats => 1, include_in_scoring => 1 }, |
169 | Enrolled => { allow_course_access => 1, include_in_assignment => 1, include_in_stats => 1, include_in_scoring => 1 }, |
| 170 | Audit => { allow_course_access => 1, include_in_assignment => 1, include_in_stats => 1, include_in_scoring => 0 }, |
170 | Audit => { allow_course_access => 1, include_in_assignment => 1, include_in_stats => 1, include_in_scoring => 0 }, |
| 171 | Drop => { allow_course_access => 0, include_in_assignment => 0, include_in_stats => 0, include_in_scoring => 0 }, |
171 | Drop => { allow_course_access => 0, include_in_assignment => 0, include_in_stats => 0, include_in_scoring => 0 }, |
| 172 | }; |
172 | }; |
| 173 | |
173 | |
|
|
174 | use constant DEFAULT_STATUS => "Enrolled"; |
|
|
175 | use constant DEFAULT_PERMISSION_LEVEL => "0"; |
|
|
176 | |
| 174 | our ($opt_c, $opt_r, $opt_s, $opt_u, $opt_v); |
177 | our ($opt_c, $opt_r, $opt_s, $opt_u, $opt_v); |
| 175 | getopts("crsuv"); |
178 | getopts("crsuv"); |
| 176 | |
179 | |
| 177 | sub debug { print STDERR @_ if $opt_v } |
180 | sub debug { print STDERR @_ if $opt_v } |
| 178 | sub usage { print STDERR "usage: $0 [-crsuv] course ...\n"; exit 1 } |
181 | sub usage { print STDERR "usage: $0 [-crsuv] course ...\n"; exit 1 } |
| … | |
… | |
| 186 | |
189 | |
| 187 | my $ce = WeBWorK::CourseEnvironment->new({webwork_dir => $ENV{WEBWORK_ROOT}}); |
190 | my $ce = WeBWorK::CourseEnvironment->new({webwork_dir => $ENV{WEBWORK_ROOT}}); |
| 188 | |
191 | |
| 189 | WeBWorK::DBv3::init($ce->{wwdbv3_settings}); |
192 | WeBWorK::DBv3::init($ce->{wwdbv3_settings}); |
| 190 | |
193 | |
|
|
194 | my %abbrev_to_status_id = set_up_statuses($ce->{siteDefaults}{status}); |
|
|
195 | warn "abbrev_to_status_id: ", Dumper(\%abbrev_to_status_id); |
|
|
196 | |
| 191 | my %level_to_role_id = set_up_roles($ce->{permissionLevels}); |
197 | my %level_to_role_id = set_up_roles($ce->{permissionLevels}); |
| 192 | warn Dumper(\%level_to_role_id); |
198 | warn "level_to_role_id: ", Dumper(\%level_to_role_id); |
| 193 | |
|
|
| 194 | my %abbrev_to_status_id = set_up_statuses($ce->{siteDefaults}{status}); |
|
|
| 195 | warn Dumper(\%abbrev_to_status_id); |
|
|
| 196 | |
199 | |
| 197 | foreach my $courseID (@courseIDs) { |
200 | foreach my $courseID (@courseIDs) { |
| 198 | eval { copy_course_data($courseID) }; |
201 | eval { copy_course_data($courseID, \%abbrev_to_status_id, \%level_to_role_id) }; |
| 199 | if ($@) { |
202 | if ($@) { |
| 200 | warn "An error occured while copying data from course '$courseID':\n\n$@\n\n"; |
203 | warn "An error occured while copying data from course '$courseID':\n\n$@\n\n"; |
| 201 | if ($opt_c) { |
204 | if ($opt_c) { |
| 202 | warn "Continuing with the next course...\n"; |
205 | warn "Continuing with the next course...\n"; |
| 203 | } else { |
206 | } else { |
| … | |
… | |
| 352 | } |
355 | } |
| 353 | |
356 | |
| 354 | ################################################################################ |
357 | ################################################################################ |
| 355 | |
358 | |
| 356 | sub copy_course_data { |
359 | sub copy_course_data { |
| 357 | my ($courseID) = @_; |
360 | my ($courseID, $abbrev_to_status_id, $level_to_role_id) = @_; |
| 358 | |
361 | |
| 359 | debug("Processing course '$courseID'...\n"); |
362 | debug("Processing course '$courseID'...\n"); |
| 360 | |
363 | |
| 361 | my $course_ce = WeBWorK::CourseEnvironment->new({ |
364 | my $course_ce = WeBWorK::CourseEnvironment->new({ |
| 362 | webwork_dir => $ENV{WEBWORK_ROOT}, |
365 | webwork_dir => $ENV{WEBWORK_ROOT}, |
| … | |
… | |
| 367 | |
370 | |
| 368 | # First we see if this course already exists. If it does, there's a problem |
371 | # First we see if this course already exists. If it does, there's a problem |
| 369 | # and we throw an exception. |
372 | # and we throw an exception. |
| 370 | if (WeBWorK::DBv3::Course->search(name => $courseID)) { |
373 | if (WeBWorK::DBv3::Course->search(name => $courseID)) { |
| 371 | die "Course '$courseID' exists in v3 DB"; |
374 | die "Course '$courseID' exists in v3 DB"; |
| 372 | } else { |
375 | } |
|
|
376 | |
| 373 | debug("Course '$courseID' doesn't exist in v3 DB -- adding.\n"); |
377 | debug("Course '$courseID' doesn't exist in v3 DB -- adding.\n"); |
| 374 | my $Course = WeBWorK::DBv3::Course->create({name => $courseID}); |
378 | my $v3Course = WeBWorK::DBv3::Course->create({name => $courseID}); |
| 375 | } |
|
|
| 376 | |
379 | |
| 377 | # Then, deal with users. |
380 | copy_users($course_db, $v3Course, $abbrev_to_status_id, $level_to_role_id); |
|
|
381 | |
|
|
382 | copy_abstract_data($course_db, $v3Course); |
|
|
383 | } |
|
|
384 | |
|
|
385 | sub copy_users { |
|
|
386 | my ($course_db, $v3Course, $abbrev_to_status_id, $level_to_role_id) = @_; |
|
|
387 | |
| 378 | my @userIDs = $course_db->listUsers; |
388 | my @userIDs = $course_db->listUsers; |
| 379 | my %Users; @Users{@userIDs} = $course_db->getUsers(@userIDs); |
389 | my %Users; @Users{@userIDs} = $course_db->getUsers(@userIDs); |
|
|
390 | my %Passwords; @Passwords{@userIDs} = $course_db->getPasswords(@userIDs); |
|
|
391 | my %PermissionLevels; @PermissionLevels{@userIDs} = $course_db->getPermissionLevels(@userIDs); |
| 380 | |
392 | |
| 381 | foreach my $userID (keys %Users) { |
393 | foreach my $userID (keys %Users) { |
| 382 | my $User = $Users{$userID}; |
394 | my $User = $Users{$userID}; |
|
|
395 | my $Password = $Passwords{$userID}; |
|
|
396 | my $PermissionLevel = $PermissionLevels{$userID}; |
|
|
397 | |
| 383 | unless (defined $User) { |
398 | unless (defined $User) { |
| 384 | debug("User record for user ID '$userID' not found -- skipping.\n"); |
399 | debug("User record for user ID '$userID' not found -- skipping.\n"); |
| 385 | next; |
400 | next; |
| 386 | } |
401 | } |
| 387 | |
402 | |
|
|
403 | debug("Processing user '$userID'...\n"); |
|
|
404 | |
|
|
405 | # create/update user record |
| 388 | my $v3User = WeBWorK::DBv3::User->search(login_id => $userID); |
406 | my ($v3User) = WeBWorK::DBv3::User->search(login_id => $userID); |
| 389 | if ($v3User) { |
407 | if ($v3User) { |
| 390 | debug("User with login_id '$userID' exists in v3 database -- "); |
408 | debug("A user with login_id '$userID' exists in v3 database -- "); |
| 391 | if ($opt_u) { |
409 | if ($opt_u) { |
| 392 | debug("updating.\n"); |
410 | debug("updating (as per -u switch).\n"); |
| 393 | $v3User->first_name($User->first_name) if $User->first_name ne ""; |
411 | $v3User->first_name($User->first_name) if $User->first_name ne ""; |
| 394 | $v3User->last_name($User->first_name) if $User->last_name ne ""; |
412 | $v3User->last_name($User->first_name) if $User->last_name ne ""; |
| 395 | $v3User->email_address($User->email_address) if $User->email_address ne ""; |
413 | $v3User->email_address($User->email_address) if $User->email_address ne ""; |
| 396 | $v3User->student_id($User->student_id) if $User->student_id ne ""; |
414 | $v3User->student_id($User->student_id) if $User->student_id ne ""; |
|
|
415 | $v3User->password($Password->password) if $Password->password ne ""; |
| 397 | $v3User->update; |
416 | $v3User->update; |
| 398 | } else { |
417 | } else { |
| 399 | debug("skipping.\n"); |
418 | debug("not updating (as per lack of -u switch).\n"); |
| 400 | } |
419 | } |
| 401 | } else { |
420 | } else { |
| 402 | debug("User with login_id '$userID' does not exists in v3 database -- adding.\n"); |
421 | debug("No user with login_id '$userID' exists in v3 database -- adding.\n"); |
| 403 | WeBWorK::DBv3::User->create({ |
422 | $v3User = WeBWorK::DBv3::User->create({ |
| 404 | |
423 | first_name => $User->first_name, |
|
|
424 | last_name => $User->last_name, |
|
|
425 | email_address => $User->email_address, |
|
|
426 | student_id => $User->student_id, |
|
|
427 | login_id => $User->user_id, |
|
|
428 | password => $Password->password, |
| 405 | }); |
429 | }); |
| 406 | } |
430 | } |
|
|
431 | |
|
|
432 | # get status |
|
|
433 | my $status = $User->status; |
|
|
434 | if ($status eq "") { |
|
|
435 | $status = DEFAULT_STATUS; |
|
|
436 | debug("This user has no status -- using '$status'.\n"); |
| 407 | } |
437 | } |
|
|
438 | my $v3Status_id = $abbrev_to_status_id->{$status}; |
|
|
439 | my $v3Status; |
|
|
440 | if (defined $v3Status_id) { |
|
|
441 | debug("This user has status '", $User->status, "', which maps to v3 status ID '$v3Status_id'.\n"); |
|
|
442 | $v3Status = WeBWorK::DBv3::Status->retrieve($v3Status_id); |
|
|
443 | } else { |
|
|
444 | debug("This user has status '", $User->status, "', which doesn't map to any v3 status.\n"); |
|
|
445 | } |
|
|
446 | |
|
|
447 | # get role |
|
|
448 | my $level = $PermissionLevel->permission; |
|
|
449 | if ($level eq "") { |
|
|
450 | $level = DEFAULT_PERMISSION_LEVEL; |
|
|
451 | debug("This user has no permission level -- using '$level'.\n"); |
|
|
452 | } |
|
|
453 | my $v3Role_id = $level_to_role_id->{$level}; |
|
|
454 | my $v3Role; |
|
|
455 | if (defined $v3Role_id) { |
|
|
456 | debug("This user has permission level '", $PermissionLevel->permission, "', which maps to v3 role ID '$v3Role_id'.\n"); |
|
|
457 | $v3Role = WeBWorK::DBv3::Role->retrieve($v3Role_id); |
|
|
458 | } else { |
|
|
459 | debug("This user has permission level '", $PermissionLevel->permission, "', which doesn't map to any v3 role.\n"); |
|
|
460 | } |
|
|
461 | |
|
|
462 | # find/create section record |
|
|
463 | my $section = $User->section; |
|
|
464 | my $v3Section; |
|
|
465 | if ($section ne "") { |
|
|
466 | debug("This user has section '$section'.\n"); |
|
|
467 | ($v3Section) = WeBWorK::DBv3::Section->search(course => $v3Course->id, name => $section); |
|
|
468 | if ($v3Section) { |
|
|
469 | debug("This corresponds to existing section ID $v3Section in v3 database.\n"); |
|
|
470 | } else { |
|
|
471 | debug("No corresponding section exists in v3 DB -- adding.\n"); |
|
|
472 | $v3Section = WeBWorK::DBv3::Section->create({ |
|
|
473 | course => $v3Course->id, |
|
|
474 | name => $section, |
|
|
475 | }); |
|
|
476 | debug("Added section '", $v3Section->name, "' (ID $v3Section).\n"); |
|
|
477 | } |
|
|
478 | } else { |
|
|
479 | debug("This user has no section.\n"); |
|
|
480 | } |
|
|
481 | |
|
|
482 | |
|
|
483 | # find/create recitation record |
|
|
484 | my $recitation = $User->recitation; |
|
|
485 | my $v3Recitation; |
|
|
486 | if ($recitation ne "") { |
|
|
487 | debug("This user has recitation '$recitation'.\n"); |
|
|
488 | ($v3Recitation) = WeBWorK::DBv3::Recitation->search(course => $v3Course->id, name => $User->recitation); |
|
|
489 | if ($v3Recitation) { |
|
|
490 | debug("This correponds to existing recitation ID $v3Recitation in v3 database.\n"); |
|
|
491 | } else { |
|
|
492 | debug("No corresponding recitation exists in v3 DB -- adding.\n"); |
|
|
493 | $v3Recitation = WeBWorK::DBv3::Recitation->create({ |
|
|
494 | course => $v3Course->id, |
|
|
495 | name => $User->recitation, |
|
|
496 | }); |
|
|
497 | debug("Added recitation '", $v3Recitation->name, "' (ID $v3Recitation).\n"); |
|
|
498 | } |
|
|
499 | } else { |
|
|
500 | debug("This user has no recitation.\n"); |
|
|
501 | } |
|
|
502 | |
|
|
503 | # create participant record |
|
|
504 | debug("Adding participant record for user '", $v3User->login_id, "' (ID $v3User).\n"); |
|
|
505 | my $sectionID = $v3Section->id if defined $v3Section; |
|
|
506 | my $recitationID = $v3Recitation->id if defined $v3Recitation; |
|
|
507 | my $v3Participant = WeBWorK::DBv3::Participant->create({ |
|
|
508 | course => $v3Course->id, |
|
|
509 | user => $v3User->id, |
|
|
510 | status => $v3Status->id, |
|
|
511 | role => $v3Role->id, |
|
|
512 | section => $sectionID, |
|
|
513 | recitation => $recitationID, |
|
|
514 | comment => $User->comment, |
|
|
515 | }); |
|
|
516 | } |
|
|
517 | } |
|
|
518 | |
|
|
519 | sub copy_abstract_data { |
|
|
520 | my ($course_db, $v3Course) = @_; |
|
|
521 | |
|
|
522 | |
| 408 | } |
523 | } |
| 409 | |
524 | |
| 410 | ################################################################################ |
525 | ################################################################################ |
| 411 | |
526 | |
| 412 | sub listeq { |
527 | sub listeq { |