-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathCourseManagement.pm
1502 lines (1186 loc) · 47.7 KB
/
CourseManagement.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2024 The WeBWorK Project, https://github.com/openwebwork
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################
package WeBWorK::Utils::CourseManagement;
use base qw(Exporter);
=head1 NAME
WeBWorK::Utils::CourseManagement - create, rename, and delete courses.
=cut
use strict;
use warnings;
use Carp;
use DBI;
use String::ShellQuote;
use UUID::Tiny qw(create_uuid_as_string);
use Mojo::File qw(path);
use File::Copy::Recursive qw(dircopy);
use File::Spec;
use Archive::Tar;
use WeBWorK::Debug;
use WeBWorK::CourseEnvironment;
use WeBWorK::DB;
use WeBWorK::Utils qw(runtime_use);
use WeBWorK::Utils::Files qw(surePathToFile);
use WeBWorK::Utils::Instructor qw(assignSetsToUsers);
our @EXPORT_OK = qw(
listCourses
listArchivedCourses
addCourse
renameCourse
retitleCourse
deleteCourse
archiveCourse
unarchiveCourse
dbLayoutSQLSources
initNonNativeTables
);
use constant { # constants describing the comparison of two hashes.
ONLY_IN_A => 0,
ONLY_IN_B => 1,
DIFFER_IN_A_AND_B => 2,
SAME_IN_A_AND_B => 3
};
################################################################################
# checkCourseTables
# updateCourseTables
# checkCourseDirectories
=head1 FUNCTIONS
=over
=cut
################################################################################
=item listCourses($ce)
Lists the courses defined.
=cut
sub listCourses {
my ($ce) = @_;
my $coursesDir = $ce->{webworkDirs}->{courses};
# We connect to the database and collect table names which end in "_user":
my $dbh = DBI->connect(
$ce->{database_dsn},
$ce->{database_username},
$ce->{database_password},
{
PrintError => 0,
RaiseError => 1,
},
);
my $dbname = ${ce}->{database_name};
my $stmt_bad = 0;
my $stmt = $dbh->prepare("show tables") or ($stmt_bad = 1);
my %user_tables_seen; # Will also include problem_user, set_user, achievement_user, set_locations_user
if (!$stmt_bad) {
$stmt->execute() or ($stmt_bad = 1);
my @row;
while (@row = $stmt->fetchrow_array) {
if ($row[0] =~ /_user$/) {
$user_tables_seen{ $row[0] } = 1;
}
}
$stmt->finish();
}
$dbh->disconnect();
# Collect directories which may be course directories
my @cdirs =
@{ path($coursesDir)->list({ dir => 1 })->grep(sub { -d $_ && $_->basename ne 'modelCourse' })->map('basename')
};
if ($stmt_bad) {
# Fall back to old method listing all directories.
return @cdirs;
} else {
my @courses;
for my $cname (@cdirs) {
push(@courses, $cname) if $user_tables_seen{"${cname}_user"};
}
return @courses;
}
}
=item listArchivedCourses($ce)
Lists the courses which have been archived (end in .tar.gz).
=cut
sub listArchivedCourses {
my ($ce) = @_;
my $archivesDir = path("$ce->{webworkDirs}{courses}/$ce->{admin_course_id}/archives");
surePathToFile($ce->{webworkDirs}{courses}, "$archivesDir/test"); # Ensure archives directory exists.
return @{ $archivesDir->list->grep(qr/\.tar\.gz$/)->map('basename') };
}
################################################################################
=item addCourse(%options)
%options must contain:
courseID => course ID for the new course,
ce => a course environment for the new course,
courseOptions => hash ref explained below
users => array ref explained below
%options may contain:
copyFrom => some course ID to copy things from,
courseTitle => a title for the new course
courseInstitution => institution for the new course
copyTemplatesHtml => boolean
copySimpleConfig => boolean
copyConfig => boolean
copyNonStudents => boolean
copySets => boolean
copyAchievements => boolean
copyTitle => boolean
copyInstitution => boolean
Create a new course with ID $courseID.
$ce is a WeBWorK::CourseEnvironment object that describes the new course's
environment.
$courseOptions is a reference to a hash containing the following options:
dbLayoutName => $dbLayoutName
PRINT_FILE_NAMES_FOR => $pg{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR}
C<dbLayoutName> is required. C<PRINT_FILE_NAMES_FOR> is a reference to an array.
$users is a list of arrayrefs, each containing a User, Password, and
PermissionLevel record for a single user:
$users = [ $User, $Password, $PermissionLevel ]
These users are added to the course.
C<copyFrom> indicates the ID of a course from which various things may be
copied into the new course. Which things are copied are controlled by the
boolean options:
* copyTemplatesHtml (contents of Templates and HTML folders)
* copySimpleConfig (simple.conf file)
* copyConfig (course.conf file)
* copyNonStudents (all non-student users, their permission level, and password)
* copySets (all global sets, global set locations, and global problems)
* copyAchievements (all achievements)
* copyTitle (the course title, which will override courseTitle)
* copyInstitution (the course institution, which will override courseInstitution)
=cut
sub addCourse {
my (%options) = @_;
for my $key (keys(%options)) {
my $value = '####UNDEF###';
$value = $options{$key} if (defined($options{$key}));
debug("$key : $value");
}
my $courseID = $options{courseID};
my $sourceCourse = $options{copyFrom} // '';
my $ce = $options{ce};
my %courseOptions = %{ $options{courseOptions} };
my @users = exists $options{users} ? @{ $options{users} } : ();
debug \@users;
my @initialUsers = @users;
my %user_args = map { $_->[0]{user_id} => 1 } @users;
# get the database layout out of the options hash
my $dbLayoutName = $courseOptions{dbLayoutName};
# collect some data
my $coursesDir = $ce->{webworkDirs}->{courses};
my $courseDir = "$coursesDir/$courseID";
# fail if the course already exists
# IMPORTANT: this must be the first check! if any check other than this one
# fails, CourseAdmin deletes the course!! Oh no!!!
# DO NOT CHANGE THE DIE MESSAGE -- CourseAdmin checks it to determine whether
# a course was partially created and should be deleted!
# FIXME -- this is bad, and addCourse should deal with cleaning up partially
# created courses itself
if (-e $courseDir) {
croak "$courseID: course exists";
}
# fail if the course ID contains invalid characters
croak "Invalid characters in course ID: '$courseID' (valid characters are [-A-Za-z0-9_])"
unless $courseID =~ m/^[-A-Za-z0-9_]*$/;
# fail if requested course ID is too long
croak "Course ID cannot exceed " . $ce->{maxCourseIdLength} . " characters."
if (length($courseID) > $ce->{maxCourseIdLength});
# if we didn't get a database layout, use the default one
if (not defined $dbLayoutName) {
$dbLayoutName = $ce->{dbLayoutName};
}
# fail if the database layout is invalid
if (not exists $ce->{dbLayouts}->{$dbLayoutName}) {
croak "$dbLayoutName: not found in \%dbLayouts";
}
##### step 1: create course directory structure #####
my %courseDirs = %{ $ce->{courseDirs} };
# deal with root directory first -- if we can't create it, we have to give up.
exists $courseDirs{root}
or croak
"Can't create the course '$courseID' because no root directory is specified in the '%courseDirs' hash.";
my $root = $courseDirs{root};
delete $courseDirs{root};
{
# does the directory already exist?
-e $root and croak "Can't create the course '$courseID' because the root directory '$root' already exists.";
# is the parent directory writeable?
my @rootElements = File::Spec->splitdir($root);
pop @rootElements;
my $rootParent = File::Spec->catdir(@rootElements);
-w $rootParent
or croak
"Can't create the course '$courseID' because the courses directory '$rootParent' is not writeable.";
# try to create it
eval { path($root)->make_path };
croak "Can't create the course '$courseID' because the root directory '$root' could not be created: $@." if $@;
}
# deal with the rest of the directories
my @courseDirNames = sort { $courseDirs{$a} cmp $courseDirs{$b} } keys %courseDirs;
foreach my $courseDirName (@courseDirNames) {
my $courseDir = File::Spec->canonpath($courseDirs{$courseDirName});
# does the directory already exist?
if (-e $courseDir) {
warn "Can't create $courseDirName directory '$courseDir', "
. "since it already exists. Using existing directory.\n";
next;
}
# is the parent directory writeable?
my @courseDirElements = File::Spec->splitdir($courseDir);
pop @courseDirElements;
my $courseDirParent = File::Spec->catdir(@courseDirElements);
unless (-w $courseDirParent) {
warn "Can't create $courseDirName directory '$courseDir', since the parent directory is not writeable. "
. "You will have to create this directory manually.\n";
next;
}
# try to create it
eval { path($courseDir)->make_path };
warn "Failed to create $courseDirName directory '$courseDir': $@. "
. "You will have to create this directory manually."
if $@;
}
# hide the new course?
if (defined $ce->{new_courses_hidden_status} && $ce->{new_courses_hidden_status} eq 'hidden') {
my $hideDirFile = "$ce->{webworkDirs}{courses}/$courseID/hide_directory";
open(my $HIDEFILE, '>', $hideDirFile);
print $HIDEFILE 'Place a file named "hide_directory" in a course or other directory and it will not show up '
. 'in the courses list on the WeBWorK home page. It will still appear in the '
. 'Course Administration listing.';
close $HIDEFILE;
}
##### step 2: create course database #####
my $db = new WeBWorK::DB($ce->{dbLayouts}->{$dbLayoutName});
my $create_db_result = $db->create_tables;
die "$courseID: course database creation failed.\n" unless $create_db_result;
##### step 3: populate course database #####
# database and course environment objects for the course to copy things from.
my ($db0, $ce0);
if (
$sourceCourse ne ''
&& !(grep { $sourceCourse eq $_ } @{ $ce->{modelCoursesForCopy} })
&& ($options{copyNonStudents}
|| $options{copySets}
|| $options{copyAchievements}
|| $options{copyTitle}
|| $options{copyInstitution})
)
{
$ce0 = WeBWorK::CourseEnvironment->new({ courseName => $sourceCourse });
$db0 = WeBWorK::DB->new($ce0->{dbLayouts}{$dbLayoutName});
}
# add users (users that were directly passed to addCourse() as well as those copied from a source course)
if ($ce->{dbLayouts}{$dbLayoutName}{user}{params}{non_native}) {
debug("not adding users to the course database: 'user' table is non-native.\n");
} else {
if ($db0 && $options{copyNonStudents}) {
# If the course.conf file is being copied, then the student role from the source course needs to be used,
# as the role might be customized in that file.
my @non_student_ids =
map {@$_} ($db0->listPermissionLevelsWhere({
permission =>
{ '!=' => $options{copyConfig} ? $ce0->{userRoles}{student} : $ce->{userRoles}{student} },
user_id => { not_like => 'set_id:%' }
}));
for my $user_id (@non_student_ids) {
next if $user_args{$user_id};
my @User = $db0->getUsersWhere({ user_id => $user_id });
my @Password = $db0->getPasswordsWhere({ user_id => $user_id });
my @PermissionLevel = $db0->getPermissionLevelsWhere({ user_id => $user_id });
push @users, [ $User[0], $Password[0], $PermissionLevel[0] ];
}
}
foreach my $userTriple (@users) {
my ($User, $Password, $PermissionLevel) = @$userTriple;
eval { $db->addUser($User) };
warn $@ if $@;
eval { $db->addPassword($Password) } if $Password;
warn $@ if $@;
eval { $db->addPermissionLevel($PermissionLevel) };
warn $@ if $@;
}
}
# add sets
if ($db0 && $options{copySets}) {
my @sets = $db0->getGlobalSetsWhere;
for my $set (@sets) {
$set->lis_source_did(undef);
eval { $db->addGlobalSet($set) };
warn $@ if $@;
my @Problem = $db0->getGlobalProblemsWhere({ set_id => $set->set_id });
for my $problem (@Problem) {
eval { $db->addGlobalProblem($problem) };
warn $@ if $@;
}
my @Location = $db0->getGlobalSetLocationsWhere({ set_id => $set->set_id });
for my $location (@Location) {
eval { $db->addGlobalSetLocation($location) };
warn $@ if $@;
}
# Copy the set level proctor user for this set if there is one (despite the for loop there can only be one).
for my $setProctor ($db0->getUsersWhere({ user_id => 'set_id:' . $set->set_id })) {
eval { $db->addUser($setProctor) };
warn $@ if $@;
my $password = $db0->getPassword($setProctor->user_id);
eval { $db->addPassword($password) } if $password;
warn $@ if $@;
my $permission = $db0->getPermissionLevel($setProctor->user_id);
eval { $db->addPermissionLevel($permission) } if $permission;
warn $@ if $@;
}
}
if ($options{copyNonStudents}) {
foreach my $userTriple (@users) {
my $user_id = $userTriple->[0]{user_id};
next if $user_args{$user_id}; # Initial users will be assigned to everything below.
my @user_sets = $db0->listUserSets($user_id);
assignSetsToUsers($db, $ce, \@user_sets, [$user_id]);
}
}
assignSetsToUsers($db, $ce, [ map { $_->set_id } @sets ], [ map { $_->[0]{user_id} } @initialUsers ])
if @initialUsers;
}
# add achievements
if ($db0 && $options{copyAchievements}) {
my @achievement = $db0->getAchievementsWhere;
for my $achievement (@achievement) {
eval { $db->addAchievement($achievement) };
warn $@ if $@;
for (@initialUsers) {
my $userAchievement = $db->newUserAchievement();
$userAchievement->user_id($_->[0]{user_id});
$userAchievement->achievement_id($achievement->achievement_id);
$db->addUserAchievement($userAchievement);
}
}
if ($options{copyNonStudents}) {
foreach my $userTriple (@users) {
my $user_id = $userTriple->[0]{user_id};
next if $user_args{$user_id}; # Initial users were assigned to all achievements above.
my @user_achievements = $db0->listUserAchievements($user_id);
for my $achievement_id (@user_achievements) {
my $userAchievement = $db->newUserAchievement();
$userAchievement->user_id($user_id);
$userAchievement->achievement_id($achievement_id);
$db->addUserAchievement($userAchievement);
}
}
}
}
# copy title and/or institution if requested
for my $setting ('Title', 'Institution') {
if ($db0 && $options{"copy$setting"}) {
my $settingValue = $db0->getSettingValue("course$setting");
$db->setSettingValue("course$setting", $settingValue) if defined $settingValue && $settingValue ne '';
} else {
$db->setSettingValue("course$setting", $options{"course$setting"})
if defined $options{"course$setting"} && $options{"course$setting"} ne '';
}
}
##### step 4: write course.conf file (unless that is going to be copied from a source course) #####
unless ($sourceCourse ne '' && $options{copyConfig}) {
my $courseEnvFile = $ce->{courseFiles}{environment};
open my $fh, ">:utf8", $courseEnvFile
or die "failed to open $courseEnvFile for writing.\n";
writeCourseConf($fh, $ce, %courseOptions);
close $fh;
}
##### step 5: copy templates, html, simple.conf, course.conf if desired #####
if ($sourceCourse ne '') {
my $sourceCE = WeBWorK::CourseEnvironment->new({ get_SeedCE($ce), courseName => $sourceCourse });
if ($options{copyTemplatesHtml}) {
my $sourceDir = $sourceCE->{courseDirs}{templates};
## copy templates ##
if (-d $sourceDir) {
my $destDir = $ce->{courseDirs}{templates};
warn "Failed to copy templates from course '$sourceCourse': $! " unless dircopy("$sourceDir", $destDir);
} else {
warn "Failed to copy templates from course '$sourceCourse': "
. "templates directory '$sourceDir' does not exist.\n";
}
## copy html ##
$sourceDir = $sourceCE->{courseDirs}{html};
if (-d $sourceDir) {
warn "Failed to copy html from course '$sourceCourse': $!"
unless dircopy($sourceDir, $ce->{courseDirs}{html});
} else {
warn "Failed to copy html from course '$sourceCourse': html directory '$sourceDir' does not exist.\n";
}
}
## copy config files ##
# this copies the simple.conf file if desired
if ($options{copySimpleConfig}) {
my $sourceFile = $sourceCE->{courseFiles}{simpleConfig};
if (-e $sourceFile) {
eval { path($sourceFile)->copy_to($ce->{courseDirs}{root}) };
warn "Failed to copy simple.conf from course '$sourceCourse': $@" if $@;
}
}
# this copies the course.conf file if desired
if ($options{copyConfig}) {
my $sourceFile = $sourceCE->{courseFiles}{environment};
if (-e $sourceFile) {
eval { path($sourceFile)->copy_to($ce->{courseDirs}{root}) };
warn "Failed to copy course.conf from course '$sourceCourse': $@" if $@;
}
}
}
}
################################################################################
=item renameCourse(%options)
%options must contain:
courseID => $courseID,
ce => $ce,
newCourseID => $newCourseID,
%options may also contain:
skipDBRename => $skipDBRename,
courseTitle => $courseTitle
courseInstitution => $courseInstitution
Rename the course named $courseID to $newCourseID.
$ce is a WeBWorK::CourseEnvironment object that describes the existing course's
environment.
The name of the course's directory is changed to $newCourseID.
If the course's database layout is C<sql_single> or C<sql_moodle>, new tables
are created in the current database, course data is copied from the old tables
to the new tables, and the old tables are deleted.
If the course's database layout is something else, no database changes are made.
If $skipDBRename is true, no database changes are made. This is useful if a
course is being unarchived and no database was found, or for renaming the
modelCourse.
Any errors encountered while renaming the course are returned.
=cut
sub renameCourse {
my (%options) = @_;
# renameCourseHelper needs:
# $fromCourseID ($oldCourseID)
# $fromCE ($oldCE)
# $toCourseID ($newCourseID)
# $toCE (construct from $oldCE)
# $dbLayoutName ($oldCE->{dbLayoutName})
my $oldCourseID = $options{courseID};
my $oldCE = $options{ce};
my $newCourseID = $options{newCourseID};
my $skipDBRename = $options{skipDBRename} || 0;
# get the database layout out of the options hash
my $dbLayoutName = $oldCE->{dbLayoutName};
# collect some data
my $coursesDir = $oldCE->{webworkDirs}->{courses};
my $oldCourseDir = "$coursesDir/$oldCourseID";
my $newCourseDir = "$coursesDir/$newCourseID";
# fail if the target course already exists
if (-e $newCourseDir) {
croak "$newCourseID: course exists";
}
# fail if the target courseID is too long
croak "New course ID cannot exceed " . $oldCE->{maxCourseIdLength} . " characters."
if (length($newCourseID) > $oldCE->{maxCourseIdLength});
# fail if the source course does not exist
unless (-e $oldCourseDir) {
croak "$oldCourseID: course not found";
}
##### step 1: move course directory #####
# move top-level course directory
debug("moving course dir from $oldCourseDir to $newCourseDir");
eval { path($oldCourseDir)->move_to($newCourseDir) };
die "Failed to move course directory: $@" if ($@);
# get new course environment
my $newCE = $oldCE->new({ courseName => $newCourseID });
# find the course dirs that still exist in their original locations
# (i.e. are not subdirs of $courseDir)
my %oldCourseDirs = %{ $oldCE->{courseDirs} };
my %newCourseDirs = %{ $newCE->{courseDirs} };
my @courseDirNames = sort { $oldCourseDirs{$a} cmp $oldCourseDirs{$b} } keys %oldCourseDirs;
foreach my $courseDirName (@courseDirNames) {
my $oldDir = File::Spec->canonpath($oldCourseDirs{$courseDirName});
my $newDir = File::Spec->canonpath($newCourseDirs{$courseDirName});
if (-e $oldDir) {
debug("oldDir $oldDir still exists. might move it...\n");
# check for a few likely error conditions, since the mv error is not that helpful
# is the source really a directory
unless (-d $oldDir) {
warn "$courseDirName: Can't move '$oldDir' to '$newDir', since the source is not a directory. "
. "You will have to move this directory manually.\n";
next;
}
# does the destination already exist?
# (this should only happen on extra-coursedir directories, since we make sure the root dir doesn't exist above.)
if (-e $newDir) {
warn "$courseDirName: Can't move '$oldDir' to '$newDir', since the target already exists. "
. "You will have to move this directory manually.\n";
next;
}
# is oldDir's parent writeable
my @oldDirElements = File::Spec->splitdir($oldDir);
pop @oldDirElements;
my $oldDirParent = File::Spec->catdir(@oldDirElements);
unless (-w $oldDirParent) {
warn "$courseDirName: Can't move '$oldDir' to '$newDir', since the source parent directory is not "
. "writeable. You will have to move this directory manually.\n";
next;
}
# is newDir's parent writeable?
my @newDirElements = File::Spec->splitdir($newDir);
pop @newDirElements;
my $newDirParent = File::Spec->catdir(@newDirElements);
unless (-w $newDirParent) {
warn "$courseDirName: Can't move '$oldDir' to '$newDir', since the destination parent directory is "
. "not writeable. You will have to move this directory manually.\n";
next;
}
# try to move the directory
debug("Going to move $oldDir to $newDir...\n");
eval { path($oldDir)->move_to($newDir) };
warn "Failed to move directory from $oldDir to $newDir with error: $@" if $@;
} else {
debug("oldDir $oldDir was already moved.\n");
}
}
##### step 2: rename database #####
unless ($skipDBRename) {
my $oldDB = new WeBWorK::DB($oldCE->{dbLayouts}{$dbLayoutName});
my $rename_db_result = $oldDB->rename_tables($newCE->{dbLayouts}{$dbLayoutName});
die "$oldCourseID: course database renaming failed.\n" unless $rename_db_result;
#update title and institution
my $newDB = new WeBWorK::DB($newCE->{dbLayouts}{$dbLayoutName});
eval {
if (defined $options{courseTitle} && $options{courseTitle} ne '') {
$newDB->setSettingValue('courseTitle', $options{courseTitle});
}
if (defined $options{courseInstitution} && $options{courseInstitution} ne '') {
$newDB->setSettingValue('courseInstitution', $options{courseInstitution});
}
};
warn "Problems from resetting course title and institution = $@" if $@;
}
}
################################################################################
=item retitleCourse
Simply changes the title and institution of the course.
Options must contain:
courseID => $courseID,
ce => $ce,
Options may contain
newCourseTitle => $courseTitle,
newCourseInstitution => $courseInstitution,
=cut
sub retitleCourse {
my %options = @_;
# renameCourseHelper needs:
# $courseID ($oldCourseID)
# $ce ($oldCE)
# $dbLayoutName ($ce->{dbLayoutName})
# courseTitle
# courseInstitution
my $courseID = $options{courseID};
my $ce = $options{ce};
# get the database layout out of the options hash
my $dbLayoutName = $ce->{dbLayoutName};
my $db = new WeBWorK::DB($ce->{dbLayouts}{$dbLayoutName});
eval {
if (defined $options{courseTitle} && $options{courseTitle} ne '') {
$db->setSettingValue('courseTitle', $options{courseTitle});
}
if (defined $options{courseInstitution} && $options{courseInstitution} ne '') {
$db->setSettingValue('courseInstitution', $options{courseInstitution});
}
};
warn "Problems from resetting course title and institution = $@" if $@;
}
=item deleteCourse(%options)
Options must contain:
courseID => $courseID,
ce => $ce,
$ce is a WeBWorK::CourseEnvironment object that describes the course's
environment. It is your responsability to pass a course environment object that
describes the course to be deleted. Do not pass the course environment object
associated with the request, unless you are deleting the course you're currently
using.
Deletes the course named $courseID. The course directory is removed.
Any errors encountered while deleting the course are returned.
=cut
sub deleteCourse {
my (%options) = @_;
my $courseID = $options{courseID};
my $ce = $options{ce};
# make sure the user isn't brain damaged
die "the course environment supplied doesn't appear to describe the course $courseID. can't proceed."
unless $ce->{courseName} eq $courseID;
my %courseDirs = %{ $ce->{courseDirs} };
##### step 0: make sure course directory is deleteable #####
# deal with root directory first -- if we won't be able to delete it, we have to give up.
exists $courseDirs{root}
or croak
"Can't delete the course '$courseID' because no root directory is specified in the '%courseDirs' hash.";
my $root = $courseDirs{root};
if (-e $root) {
# is the parent directory writeable?
my @rootElements = File::Spec->splitdir($root);
pop @rootElements;
my $rootParent = File::Spec->catdir(@rootElements);
-w $rootParent
or croak
"Can't delete the course '$courseID' because the courses directory '$rootParent' is not writeable.";
} else {
warn "Warning: the course root directory '$root' does not exist. "
. "Attempting to delete the course database and other course directories...\n";
}
##### step 1: delete course database (if necessary) #####
my $dbLayoutName = $ce->{dbLayoutName};
my $db = new WeBWorK::DB($ce->{dbLayouts}->{$dbLayoutName});
my $create_db_result = $db->delete_tables;
die "$courseID: course database deletion failed.\n" unless $create_db_result;
# If this course has an entry in the LTI course map, then delete it also.
$db->deleteLTICourseMapWhere({ course_id => $courseID });
##### step 2: delete course directory structure #####
my @courseDirNames = sort { $courseDirs{$a} cmp $courseDirs{$b} } keys %courseDirs;
foreach my $courseDirName (@courseDirNames) {
my $courseDir = File::Spec->canonpath($courseDirs{$courseDirName});
if (-e $courseDir) {
debug("courseDir $courseDir still exists. might delete it...\n");
# check for a few likely error conditions, since the mv error is not that helpful
# is it really a directory
unless (-d $courseDir) {
warn "Can't delete $courseDirName directory '$courseDir', since is not a directory. "
. "If it is not wanted, you will have to delete it manually.\n";
next;
}
# is the parent writeable
my @courseDirElements = File::Spec->splitdir($courseDir);
pop @courseDirElements;
my $courseDirParent = File::Spec->catdir(@courseDirElements);
unless (-w $courseDirParent) {
warn "Can't delete $courseDirName directory '$courseDir', since its parent directory is not "
. "writeable. If it is not wanted, you will have to delete it manually.\n";
next;
}
# try to delete the directory
debug("Going to delete $courseDir...\n");
eval { path($courseDir)->remove_tree };
warn "An error occurred when deleting $courseDir" if $@;
} else {
debug("courseDir $courseDir was already deleted.\n");
}
}
}
################################################################################
=item archiveCourse(%options)
%options must contain:
courseID => $courseID,
ce => $ce,
Creates a gzipped tar archive (.tar.gz) of the course $courseID and places it in the
archives directory of the admin course or in the location given in the optional archive_path
option. Before archiving, the course database is dumped into a subdirectory of the course's
DATA directory.
Only files and directories stored directly in the course directory are archived.
The contents of linked files is not archived although the symbolic links
themselves are saved.
$courseID is the name of the course to archive.
$ce is a WeBWorK::CourseEnvironment object that describes the course's
environment. (This is used to access the course database and get path
information.)
If an error occurs, an exception is thrown.
=cut
sub archiveCourse {
my (%options) = @_;
my $courseID = $options{courseID};
my $ce = $options{ce};
# make sure the user isn't brain damaged
croak "The course environment supplied doesn't appear to match the course $courseID. Can't proceed"
unless $ce->{courseName} eq $courseID;
# grab some values we'll need
my $course_dir = $ce->{courseDirs}{root};
# tmp_archive_path is used as the target of the tar.gz operation.
# After this is done the final tar.gz file is moved either to the admin course archives directory
# course/$ce->{admin_course_id}/archives or the supplied archive_path option if it is present.
# This prevents us from tarring a directory to which we have just added a file
# see bug #2022 -- for error messages on some operating systems
my $uuidStub = create_uuid_as_string();
my $tmp_archive_path = $ce->{webworkDirs}{courses} . "/${uuidStub}_$courseID.tar.gz";
my $data_dir = $ce->{courseDirs}{DATA};
my $dump_dir = "$data_dir/mysqldump";
my $archive_path;
if (defined $options{archive_path} && $options{archive_path} =~ /\S/) {
$archive_path = $options{archive_path};
} else {
$archive_path = "$ce->{webworkDirs}{courses}/$ce->{admin_course_id}/archives/$courseID.tar.gz";
surePathToFile($ce->{webworkDirs}{courses}, $archive_path);
}
# fail if the source course does not exist
unless (-e $course_dir) {
croak "$courseID: course not found";
}
my $message = '';
# replace previous archived file if it exists.
if (-e $archive_path) {
unlink($archive_path) if (-w $archive_path);
unless (-e $archive_path) {
$message .= "The archival version of '$courseID' has been replaced'.";
} else {
croak "Unable to replace the archival version of '$courseID'";
}
}
#### step 1: dump tables #####
unless (-e $dump_dir) {
eval { path($dump_dir)->make_path };
croak "Failed to create course database dump directory '$dump_dir': $@" if $@;
}
my $db = new WeBWorK::DB($ce->{dbLayout});
my $dump_db_result = $db->dump_tables($dump_dir);
unless ($dump_db_result) {
_archiveCourse_remove_dump_dir($ce, $dump_dir);
croak "$courseID: course database dump failed.\n";
}
##### step 2: tar and gzip course directory (including dumped database) #####
my $parent_dir = $ce->{webworkDirs}{courses};
my $files = path($course_dir)->list_tree({ dir => 1, hidden => 1 })->map('to_abs');
my $tar = Archive::Tar->new;
$tar->add_files(@$files);
for ($tar->get_files) {
$tar->rename($_->full_path, $_->full_path =~ s!^$parent_dir/!!r);
}
my $ok = $tar->write($tmp_archive_path, COMPRESS_GZIP);
unless ($ok) {
_archiveCourse_remove_dump_dir($ce, $dump_dir);
croak "Failed to archive course directory '$course_dir': $!";
}
##### step 3: cleanup -- remove database dump files from course directory #####
unless (-e $archive_path) {
eval { path($tmp_archive_path)->move_to($archive_path) };
if ($@) {
eval { path($tmp_archive_path)->remove };
croak "Failed to rename archived file to '$archive_path': $@";
}
} else {
eval { path($tmp_archive_path)->remove };
croak "Failed to create archived file at '$archive_path'. File already exists.";
}
_archiveCourse_remove_dump_dir($ce, $dump_dir);
return $message;
}
sub _archiveCourse_remove_dump_dir {
my ($ce, $dump_dir) = @_;
path($dump_dir)->remove_tree({ error => \my $err });
if ($err && @$err) {
for my $diag (@$err) {
my ($file, $message) = %$diag;
warn "Failed to remove course database dump directory: $file with message: $message ";
}
}
return;
}
################################################################################
=item unarchiveCourse(%options)
%options must contain:
oldCourseID => $oldCourseID,
archivePath => $archivePath,
ce => $ce,
%options may also contain:
newCourseID => $newCourseID,
Restores course $oldCourseID from a gzipped tar archive (.tar.gz) located at
$archivePath. After unarchiving, the course database is restored from a
subdirectory of the course's DATA directory.
If $newCourseID is defined and differs from $oldCourseID, the course is renamed
after unarchiving.
$ce is a WeBWorK::CourseEnvironment object that describes the some course's
environment. (Usually this would be the admin course.) This is used to access
the course database and get path information.
If an error occurs, an exception is thrown.
=cut
sub unarchiveCourse {
my (%options) = @_;
my $newCourseID = $options{newCourseID};
my $currCourseID = $options{oldCourseID};