-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathCourseAdmin.pm
More file actions
2902 lines (2596 loc) · 88.1 KB
/
CourseAdmin.pm
File metadata and controls
2902 lines (2596 loc) · 88.1 KB
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
package WeBWorK::ContentGenerator::CourseAdmin;
use Mojo::Base 'WeBWorK::ContentGenerator', -signatures;
=head1 NAME
WeBWorK::ContentGenerator::CourseAdmin - Add, rename, and delete courses.
=cut
use Net::IP; # needed for location management
use File::Path 'remove_tree';
use Mojo::File;
use File::stat;
use Time::localtime;
use WeBWorK::CourseEnvironment;
use WeBWorK::Debug;
use WeBWorK::Utils qw(cryptPassword trim_spaces);
use WeBWorK::Utils::CourseManagement qw(
addCourse
renameCourse
retitleCourse
deleteCourse
listCourses
archiveCourse
unarchiveCourse
initNonNativeTables
);
use WeBWorK::Utils::Logs qw(writeLog);
use WeBWorK::Utils::CourseDBIntegrityCheck;
use WeBWorK::Utils::CourseDirectoryIntegrityCheck qw(
checkCourseDirectories
checkCourseLinks
updateCourseDirectories
updateCourseLinks
);
use WeBWorK::DB;
sub pre_header_initialize ($c) {
my $ce = $c->ce;
my $authz = $c->authz;
my $user = $c->param('user');
return unless $authz->hasPermissions($user, 'create_and_delete_courses');
# Check that the non-native tables are present in the database.
# These are the tables which are not course specific.
my @table_update_messages = initNonNativeTables($ce);
$c->addgoodmessage($c->c(@table_update_messages)->join($c->tag('br'))) if @table_update_messages;
my @errors;
my $method_to_call;
my $subDisplay = $c->param('subDisplay');
if (defined $subDisplay) {
if ($subDisplay eq 'add_course') {
if (defined $c->param('add_course')) {
@errors = $c->add_course_validate;
if (@errors) {
$method_to_call = 'add_course_form';
} else {
$method_to_call = 'do_add_course';
}
} else {
$method_to_call = 'add_course_form';
}
} elsif ($subDisplay eq 'rename_course') {
if (defined $c->param('rename_course')) {
@errors = $c->rename_course_validate;
if (@errors) {
$method_to_call = 'rename_course_form';
} else {
$method_to_call = 'rename_course_confirm';
}
} elsif (defined $c->param('confirm_rename_course')) {
@errors = $c->rename_course_validate;
if (@errors) {
$method_to_call = 'rename_course_form';
} else {
$method_to_call = 'do_rename_course';
}
} elsif (defined $c->param('confirm_retitle_course')) {
$method_to_call = 'do_retitle_course';
} elsif (defined $c->param('upgrade_course_tables')) {
@errors = $c->rename_course_validate;
if (@errors) {
$method_to_call = 'rename_course_form';
} else {
$method_to_call = 'rename_course_confirm';
}
} else {
$method_to_call = 'rename_course_form';
}
} elsif ($subDisplay eq 'delete_course') {
if (defined $c->param('delete_course')) {
@errors = $c->delete_course_validate;
if (@errors) {
$method_to_call = 'delete_course_form';
} else {
$method_to_call = 'delete_course_confirm';
}
} elsif (defined $c->param('confirm_delete_course')) {
@errors = $c->delete_course_validate;
if (@errors) {
$method_to_call = 'delete_course_form';
} else {
$method_to_call = 'do_delete_course';
}
} elsif (defined($c->param('delete_course_refresh'))) {
$method_to_call = 'delete_course_form';
} else {
$method_to_call = 'delete_course_form';
}
} elsif ($subDisplay eq 'archive_course') {
if (defined $c->param('archive_course') || defined $c->param('skip_archive_course')) {
@errors = $c->archive_course_validate;
if (@errors) {
$method_to_call = 'archive_course_form';
} else {
$method_to_call = 'archive_course_confirm';
}
} elsif (defined $c->param('confirm_archive_course')) {
@errors = $c->archive_course_validate;
if (@errors) {
$method_to_call = 'archive_course_form';
} else {
$method_to_call = 'do_archive_course';
}
} elsif (defined $c->param('upgrade_course_tables')) {
@errors = $c->archive_course_validate;
if (@errors) {
$method_to_call = 'archive_course_form';
} else {
$method_to_call = 'archive_course_confirm';
}
} elsif (defined($c->param('archive_course_refresh'))) {
$method_to_call = 'archive_course_form';
} else {
$method_to_call = 'archive_course_form';
}
} elsif ($subDisplay eq 'unarchive_course') {
if (defined $c->param('unarchive_course')) {
@errors = $c->unarchive_course_validate;
if (@errors) {
$method_to_call = 'unarchive_course_form';
} else {
$method_to_call = 'unarchive_course_confirm';
}
} elsif (defined $c->param('confirm_unarchive_course')) {
@errors = $c->unarchive_course_validate;
if (@errors) {
$method_to_call = 'unarchive_course_form';
} else {
$method_to_call = 'do_unarchive_course';
}
} else {
$method_to_call = 'unarchive_course_form';
}
} elsif ($subDisplay eq 'upgrade_course') {
if (defined $c->param('upgrade_course')) {
@errors = $c->upgrade_course_validate;
if (@errors) {
$method_to_call = 'upgrade_course_form';
} else {
$method_to_call = 'upgrade_course_confirm';
}
} elsif (defined $c->param('confirm_upgrade_course')) {
@errors = $c->upgrade_course_validate;
if (@errors) {
$method_to_call = 'upgrade_course_form';
} else {
$method_to_call = 'do_upgrade_course';
}
} else {
$method_to_call = 'upgrade_course_form';
}
} elsif ($subDisplay eq 'manage_locations') {
if (defined($c->param('manage_location_action'))) {
$method_to_call = $c->param('manage_location_action');
} else {
$method_to_call = 'manage_location_form';
}
} elsif ($subDisplay eq 'hide_inactive_course') {
if (defined($c->param('hide_course'))) {
@errors = $c->hide_course_validate;
if (@errors) {
$method_to_call = 'hide_inactive_course_form';
} else {
$method_to_call = 'do_hide_inactive_course';
}
} elsif (defined($c->param('unhide_course'))) {
@errors = $c->unhide_course_validate;
if (@errors) {
$method_to_call = 'hide_inactive_course_form';
} else {
$method_to_call = 'do_unhide_inactive_course';
}
} elsif (defined($c->param('hide_course_refresh'))) {
$method_to_call = 'hide_inactive_course_form';
} else {
$method_to_call = 'hide_inactive_course_form';
}
} elsif ($subDisplay eq 'manage_lti_course_map') {
if (defined $c->param('save_lti_course_map')) {
@errors = $c->save_lti_course_map_validate;
if (@errors) {
$method_to_call = 'manage_lti_course_map_form';
} else {
$method_to_call = 'do_save_lti_course_map';
}
} else {
$method_to_call = 'manage_lti_course_map_form';
}
} elsif ($subDisplay eq 'manage_otp_secrets') {
if (defined $c->param('take_action')) {
if ($c->param('action') eq 'reset') {
$method_to_call = 'reset_otp_secrets_confirm';
} else {
$method_to_call = 'copy_otp_secrets_confirm';
}
} else {
$method_to_call = 'manage_otp_secrets_form';
}
} elsif ($subDisplay eq 'registration') {
if (defined($c->param('register_site'))) {
$method_to_call = 'do_registration';
}
} else {
@errors = "Unrecognized sub-display @{[ $c->tag('b', $subDisplay) ]}.";
}
}
$c->{errors} = \@errors;
$c->{method_to_call} = $method_to_call;
return;
}
sub add_course_form ($c) {
$c->param('number_of_additional_users', ($c->param('number_of_additional_users') // 0) + 1)
if $c->param('add_another_instructor');
return $c->include('ContentGenerator/CourseAdmin/add_course_form');
}
sub add_course_validate ($c) {
my $ce = $c->ce;
my $add_courseID = trim_spaces($c->param('new_courseID')) || '';
my $number_of_additional_users = $c->param('number_of_additional_users') || 0;
my @errors;
if ($add_courseID eq '') {
push @errors, $c->maketext('You must specify a course ID.');
}
unless ($add_courseID =~ /^[\w-]*$/) { # regex copied from CourseAdministration.pm
push @errors, $c->maketext('Course ID may only contain letters, numbers, hyphens, and underscores.');
}
if (grep { $add_courseID eq $_ } listCourses($ce)) {
push @errors, $c->maketext('A course with ID [_1] already exists.', $add_courseID);
}
if (length($add_courseID) > $ce->{maxCourseIdLength}) {
push @errors, $c->maketext('Course ID cannot exceed [_1] characters.', $ce->{maxCourseIdLength});
}
for (1 .. $number_of_additional_users) {
my $userID = trim_spaces($c->param("add_initial_userID_$_")) || '';
unless ($userID =~ /^[\w.,-@]*$/) {
push @errors,
$c->maketext(
'User ID number [_1] may only contain letters, numbers, hyphens, periods, commas, '
. 'at symbols, and underscores.',
$_
);
}
}
return @errors;
}
sub do_add_course ($c) {
my $ce = $c->ce;
my $db = $c->db;
my $authz = $c->authz;
my $add_courseID = trim_spaces($c->param('new_courseID')) // '';
my $add_courseTitle = ($c->param('add_courseTitle') // '') =~ s/^\s*|\s*$//gr;
my $add_courseInstitution = ($c->param('add_courseInstitution') // '') =~ s/^\s*|\s\*$//gr;
my $number_of_additional_users = $c->param('number_of_additional_users') || 0;
my $copy_from_course = trim_spaces($c->param('copy_from_course')) // '';
my $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $add_courseID });
my %courseOptions;
my @users;
# copy users from current (admin) course if desired
for my $userID ($c->param('add-admin-users')) {
unless ($db->existsUser($userID)) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be copied from the [_2] course as it does not exist.', $userID,
$ce->{admin_course_id}
));
next;
}
for (1 .. $number_of_additional_users) {
my $add_initial_userID = trim_spaces($c->param("add_initial_userID_$_")) // '';
if ($userID eq $add_initial_userID) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be copied from the [_2] course as it is the same as additional user '
. 'number [_3].',
$userID, $ce->{admin_course_id}, $_
));
next;
}
}
my $PermissionLevel = $db->getPermissionLevel($userID);
my $User = $db->getUser($userID);
my $Password = $db->getPassword($userID);
# Enroll student users, and make all other users observers.
$User->status($PermissionLevel->permission == $ce->{userRoles}{student} ? 'C' : 'O');
push @users, [ $User, $Password, $PermissionLevel ];
}
# add additional instructors if desired
for (1 .. $number_of_additional_users) {
my $userID = trim_spaces($c->param("add_initial_userID_$_")) // '';
my $password = trim_spaces($c->param("add_initial_password_$_")) // '';
my $firstName = trim_spaces($c->param("add_initial_firstName_$_")) // '';
my $lastName = trim_spaces($c->param("add_initial_lastName_$_")) // '';
my $email = trim_spaces($c->param("add_initial_email_$_")) // '';
my $permissionLevel = $c->param("add_initial_permission_$_");
my $add_user = $c->param("add_initial_user_$_") // 0;
if ($userID =~ /\S/) {
my $User = $db->newUser(
user_id => $userID,
first_name => $firstName,
last_name => $lastName,
email_address => $email,
status => $permissionLevel == $ce->{userRoles}{student} ? 'C' : 'O',
);
my $Password = $db->newPassword(
user_id => $userID,
password => $password ? cryptPassword($password) : '',
);
my $PermissionLevel = $db->newPermissionLevel(
user_id => $userID,
permission => $permissionLevel,
);
push @users, [ $User, $Password, $PermissionLevel ];
# Add initial user to admin course if asked.
if ($add_user) {
if ($db->existsUser($userID)) {
$c->addbadmessage($c->maketext(
'User "[_1]" will not be added to the [_2] course as it already exists.', $userID,
$ce->{admin_course_id}
));
} else {
$User->status('D'); # By default don't allow user to login.
$db->addUser($User);
$db->addPassword($Password);
$db->addPermissionLevel($PermissionLevel);
$User->status($permissionLevel == $ce->{userRoles}{student} ? 'C' : 'O');
}
}
}
}
# Include any optional arguments, including a template course and the course title and course institution.
my %optional_arguments;
if ($copy_from_course ne '') {
%optional_arguments = map { $_ => 1 } $c->param('copy_component');
$optional_arguments{copyFrom} = $copy_from_course;
$optional_arguments{copyConfig} = $c->param('copy_config_file');
}
if ($add_courseTitle ne '') {
$optional_arguments{courseTitle} = $add_courseTitle;
}
if ($add_courseInstitution ne '') {
$optional_arguments{courseInstitution} = $add_courseInstitution;
}
my $output = $c->c;
eval {
addCourse(
courseID => $add_courseID,
ce => $ce2,
courseOptions => \%courseOptions,
users => \@users,
%optional_arguments,
);
};
if ($@) {
my $error = $@;
push(
@$output,
$c->tag(
'div',
class => 'alert alert-danger p-1 mb-2',
$c->c($c->tag('p', "An error occurred while creating the course $add_courseID:"),
$c->tag('div', class => 'font-monospace', $error))->join('')
)
);
# Get rid of any partially built courses.
# FIXME: This is too fragile.
unless ($error =~ /course exists/) {
eval { deleteCourse(courseID => $add_courseID, ce => $ce2); }
}
} else {
#log the action
writeLog(
$ce,
'hosted_courses',
join("\t",
"\tAdded",
(defined $add_courseInstitution ? $add_courseInstitution : '(no institution specified)'),
(defined $add_courseTitle ? $add_courseTitle : '(no title specified)'),
$add_courseID)
);
push(
@$output,
$c->tag(
'div',
class => 'alert alert-success p-1 mb-2',
$c->maketext('Successfully created the course [_1]', $add_courseID)
)
);
push(
@$output,
$c->tag(
'div',
class => 'text-center mb-2',
$c->link_to(
$c->maketext('Log into [_1]', $add_courseID) => 'set_list' => { courseID => $add_courseID }
)
)
);
}
return $output->join('');
}
sub rename_course_form ($c) {
return $c->include('ContentGenerator/CourseAdmin/rename_course_form');
}
sub rename_course_confirm ($c) {
my $ce = $c->ce;
my $rename_oldCourseID = $c->param('rename_oldCourseID') || '';
my $rename_newCourseID = $c->param('rename_newCourseID') || '';
my $rename_newCourseTitle = $c->param('rename_newCourseTitle') || '';
my $rename_newCourseInstitution = $c->param('rename_newCourseInstitution') || '';
my $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $rename_oldCourseID });
# Create strings confirming title and institution change.
# Connect to the database to get old title and institution.
my $db = WeBWorK::DB->new($ce);
my $oldDB = WeBWorK::DB->new($ce2);
my $rename_oldCourseTitle = $oldDB->getSettingValue('courseTitle') // '';
my $rename_oldCourseInstitution = $oldDB->getSettingValue('courseInstitution') // '';
my ($change_course_title_str, $change_course_institution_str) = ('', '');
if ($c->param('rename_newCourseTitle_checkbox')) {
$change_course_title_str =
$c->maketext('Change title from [_1] to [_2]', $rename_oldCourseTitle, $rename_newCourseTitle);
}
if ($c->param('rename_newCourseInstitution_checkbox')) {
$change_course_institution_str = $c->maketext('Change course institution from [_1] to [_2]',
$rename_oldCourseInstitution, $rename_newCourseInstitution);
}
# If we are only changing the title or institution, and not the courseID, then we can cut this short.
return $c->include(
'ContentGenerator/CourseAdmin/rename_course_confirm_short',
rename_oldCourseTitle => $rename_oldCourseTitle,
change_course_title_str => $change_course_title_str,
rename_oldCourseInstitution => $rename_oldCourseInstitution,
change_course_institution_str => $change_course_institution_str,
rename_oldCourseID => $rename_oldCourseID
) unless $c->param('rename_newCourseID_checkbox');
my $CIchecker = WeBWorK::Utils::CourseDBIntegrityCheck->new($ce2);
# Check database
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($rename_oldCourseID);
# Upgrade the database if requested.
my @upgrade_report;
if ($c->param('upgrade_course_tables')) {
my @schema_table_names = keys %$dbStatus;
my @tables_to_create =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A } @schema_table_names;
my @tables_to_alter =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B }
@schema_table_names;
push(@upgrade_report, $CIchecker->updateCourseTables($rename_oldCourseID, [@tables_to_create]));
for my $table_name (@tables_to_alter) {
push(@upgrade_report, $CIchecker->updateTableFields($rename_oldCourseID, $table_name));
}
($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($rename_oldCourseID);
}
# Update and check directories and links.
my $dir_update_messages = $c->param('upgrade_course_tables') ? updateCourseDirectories($ce2) : [];
my $link_update_messages = $c->param('upgrade_course_tables') ? updateCourseLinks($ce2) : [];
my ($directories_ok, $directory_report) = checkCourseDirectories($ce2);
my ($links_ok, $link_report) = checkCourseLinks($ce2);
return $c->include(
'ContentGenerator/CourseAdmin/rename_course_confirm',
upgrade_report => \@upgrade_report,
tables_ok => $tables_ok,
dbStatus => $dbStatus,
directory_report => $directory_report,
directories_ok => $directories_ok,
dir_update_messages => $dir_update_messages,
link_report => $link_report,
links_ok => $links_ok,
link_update_messages => $link_update_messages,
rename_oldCourseTitle => $rename_oldCourseTitle,
change_course_title_str => $change_course_title_str,
rename_oldCourseInstitution => $rename_oldCourseInstitution,
change_course_institution_str => $change_course_institution_str,
rename_oldCourseID => $rename_oldCourseID,
rename_newCourseID => $rename_newCourseID
);
}
sub rename_course_validate ($c) {
my $ce = $c->ce;
my $rename_oldCourseID = $c->param('rename_oldCourseID') || '';
my $rename_newCourseID = $c->param('rename_newCourseID') || '';
my $rename_newCourseID_checkbox = $c->param('rename_newCourseID_checkbox') || '';
my $rename_newCourseTitle = $c->param('rename_newCourseTitle') || '';
my $rename_newCourseTitle_checkbox = $c->param('rename_newCourseTitle_checkbox') || '';
my $rename_newCourseInstitution = $c->param('rename_newCourseInstitution') || '';
my $rename_newCourseInstitution_checkbox = $c->param('rename_newCourseInstitution_checkbox') || '';
my @errors;
if ($rename_oldCourseID eq '') {
push @errors, $c->maketext('You must select a course to rename.');
}
if ($rename_newCourseID eq '' and $rename_newCourseID_checkbox eq 'on') {
push @errors, $c->maketext('You must specify a new name for the course.');
}
if ($rename_oldCourseID eq $rename_newCourseID and $rename_newCourseID_checkbox eq 'on') {
push @errors, $c->maketext(q{Can't rename to the same name.});
}
if ($rename_newCourseID_checkbox eq 'on' && length($rename_newCourseID) > $ce->{maxCourseIdLength}) {
push @errors, $c->maketext('Course ID cannot exceed [_1] characters.', $ce->{maxCourseIdLength});
}
unless ($rename_newCourseID =~ /^[\w-]*$/) { # regex copied from CourseAdministration.pm
push @errors, $c->maketext('Course ID may only contain letters, numbers, hyphens, and underscores.');
}
if (grep { $rename_newCourseID eq $_ } listCourses($ce)) {
push @errors, $c->maketext('A course with ID [_1] already exists.', $rename_newCourseID);
}
if ($rename_newCourseTitle eq '' and $rename_newCourseTitle_checkbox eq 'on') {
push @errors, $c->maketext('You must specify a new title for the course.');
}
if ($rename_newCourseInstitution eq '' and $rename_newCourseInstitution_checkbox eq 'on') {
push @errors, $c->maketext('You must specify a new institution for the course.');
}
unless ($rename_newCourseID
or $rename_newCourseID_checkbox
or $rename_newCourseTitle_checkbox
or $rename_newCourseInstitution_checkbox)
{
push @errors,
$c->maketext(
'No changes specified. You must mark the checkbox of the item(s) to be changed and enter the change data.'
);
}
return @errors;
}
sub do_retitle_course ($c) {
my $ce = $c->ce;
my $db = $c->db;
my $rename_oldCourseID = $c->param('rename_oldCourseID') || '';
# There is no new course, but there are new titles and institutions
my $rename_newCourseTitle = $c->param('rename_newCourseTitle') || '';
my $rename_newCourseInstitution = $c->param('rename_newCourseInstitution') || '';
my $rename_oldCourseTitle = $c->param('rename_oldCourseTitle') || '';
my $rename_oldCourseInstitution = $c->param('rename_oldCourseInstitution') || '';
my $title_checkbox = $c->param('rename_newCourseTitle_checkbox') || '';
my $institution_checkbox = $c->param('rename_newCourseInstitution_checkbox') || '';
# $rename_newCourseID = $rename_oldCourseID ; #since they are the same FIXME
# define new courseTitle and new courseInstitution
my %optional_arguments = ();
$optional_arguments{courseTitle} = $rename_newCourseTitle if $title_checkbox;
$optional_arguments{courseInstitution} = $rename_newCourseInstitution if $institution_checkbox;
my $ce2;
eval { $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $rename_oldCourseID }); };
warn "failed to create environment in do_retitle_course $@" if $@;
eval { retitleCourse(courseID => $rename_oldCourseID, ce => $ce2, %optional_arguments); };
if ($@) {
my $error = $@;
return $c->tag(
'div',
class => 'alert alert-danger p-1 mb-2',
$c->c(
$c->tag(
'p',
$c->maketext(
'An error occurred while changing the title of the course [_1].',
$rename_oldCourseID
)
),
$c->tag('div', class => 'font-monospace', $error)
)->join('')
);
} else {
writeLog(
$ce,
'hosted_courses',
join(
"\t", "\t",
$c->maketext('Retitled'),
'', '',
$c->maketext(
'[_1] title and institution changed from [_2] to [_3] and from [_4] to [_5]',
$rename_oldCourseID, $rename_oldCourseTitle, $rename_newCourseTitle,
$rename_oldCourseInstitution, $rename_newCourseInstitution
)
)
);
return $c->c(
$c->tag(
'div',
class => 'alert alert-success p-1 my-2',
$c->c(
($title_checkbox) ? $c->tag(
'div',
$c->maketext(
'The title of the course [_1] has been changed from [_2] to [_3]',
$rename_oldCourseID, $rename_oldCourseTitle, $rename_newCourseTitle
)
) : '',
($institution_checkbox) ? $c->tag(
'div',
$c->maketext(
'The institution associated with the course [_1] has been changed from [_2] to [_3]',
$rename_oldCourseID, $rename_oldCourseInstitution, $rename_newCourseInstitution
)
) : ''
)->join('')
),
$c->tag(
'div',
class => 'text-center',
$c->link_to(
$c->maketext('Log into [_1]', $rename_oldCourseID) => 'set_list' =>
{ courseID => $rename_oldCourseID }
)
)
)->join('');
}
}
sub do_rename_course ($c) {
my $rename_oldCourseID = $c->param('rename_oldCourseID') || '';
my $rename_newCourseID = $c->param('rename_newCourseID') || '';
# define new courseTitle and new courseInstitution
my %optional_arguments = ();
my ($title_message, $institution_message);
if ($c->param('rename_newCourseTitle_checkbox')) {
$optional_arguments{courseTitle} = $c->param('rename_newCourseTitle') || '';
$title_message = $c->maketext('The title of the course [_1] is now [_2]',
$rename_newCourseID, $optional_arguments{courseTitle});
}
if ($c->param('rename_newCourseInstitution_checkbox')) {
$optional_arguments{courseInstitution} = $c->param('rename_newCourseInstitution') || '';
$institution_message = $c->maketext('The institution associated with the course [_1] is now [_2]',
$rename_newCourseID, $optional_arguments{courseInstitution});
}
eval {
renameCourse(
courseID => $rename_oldCourseID,
ce => WeBWorK::CourseEnvironment->new({ courseName => $rename_oldCourseID }),
newCourseID => $rename_newCourseID,
updateLTICourseMap => 1,
%optional_arguments
);
};
if ($@) {
my $error = $@;
return $c->tag(
'div',
class => 'alert alert-danger p-1 mb-2',
$c->c(
$c->tag(
'p',
$c->maketext(
'An error occurred while renaming the course [_1] to [_2]:', $rename_oldCourseID,
$rename_newCourseID
)
),
$c->tag('div', class => 'font-monospace', $error)
)->join('')
);
} else {
writeLog($c->ce, 'hosted_courses',
join("\t", "\tRenamed", '', '', "$rename_oldCourseID to $rename_newCourseID"));
return $c->c(
$c->tag(
'div',
class => 'alert alert-success p-1 mb-2',
$c->c(
$title_message ? $c->tag('p', $title_message) : '',
$institution_message ? $c->tag('p', $institution_message) : '',
$c->tag(
'p',
class => 'mb-0',
$c->maketext(
'Successfully renamed the course [_1] to [_2]', $rename_oldCourseID,
$rename_newCourseID
)
)
)->join('')
),
$c->tag(
'div',
style => 'text-align: center',
$c->link_to(
$c->maketext('Log into [_1]', $rename_newCourseID) => 'set_list' =>
{ courseID => $rename_newCourseID }
)
)
)->join('');
}
}
sub delete_course_form ($c) {
my $ce = $c->ce;
my @courseIDs = grep { $_ ne $c->stash('courseID') } listCourses($ce);
my %courseLabels;
if (@courseIDs) {
my $coursesDir = $ce->{webworkDirs}{courses};
my $delete_listing_format = $c->param('delete_listing_format');
unless (defined $delete_listing_format) { $delete_listing_format = 'alphabetically'; } # Use the default
# Get and store last modify time for login.log for all courses. Also get visibility status.
my @noLoginLogIDs;
my @loginLogIDs;
my %coursesData;
for my $courseID (@courseIDs) {
my $loginLogFile = "$coursesDir/$courseID/logs/login.log";
if (-e $loginLogFile) {
# The login log file should always exist except for the model course.
my $epoch_modify_time = stat($loginLogFile)->mtime;
$coursesData{$courseID}{epoch_modify_time} = $epoch_modify_time;
$coursesData{$courseID}{local_modify_time} = ctime($epoch_modify_time);
push(@loginLogIDs, $courseID);
} else {
# This is for the model course.
$coursesData{$courseID}{local_modify_time} = 'no login.log';
push(@noLoginLogIDs, $courseID);
}
if (-f "$coursesDir/$courseID/hide_directory") {
$coursesData{$courseID}{status} = $c->maketext('hidden');
} else {
$coursesData{$courseID}{status} = $c->maketext('visible');
}
$courseLabels{$courseID} =
"$courseID ($coursesData{$courseID}{status} :: $coursesData{$courseID}{local_modify_time}) ";
}
if ($delete_listing_format eq 'last_login') {
# This should be an empty array except for the model course.
@noLoginLogIDs = sort { lc($a) cmp lc($b) } @noLoginLogIDs;
@loginLogIDs = sort { $coursesData{$a}{epoch_modify_time} <=> $coursesData{$b}{epoch_modify_time} }
@loginLogIDs; # oldest first
@courseIDs = (@noLoginLogIDs, @loginLogIDs);
} else {
# In this case we sort alphabetically
@courseIDs = sort { lc($a) cmp lc($b) } @courseIDs;
}
}
return $c->include(
'ContentGenerator/CourseAdmin/delete_course_form',
courseIDs => \@courseIDs,
courseLabels => \%courseLabels
);
}
sub delete_course_validate ($c) {
my @errors;
if (!$c->param('delete_courseID')) {
push @errors, $c->maketext('You must specify a course name.');
} elsif ($c->param('delete_courseID') eq $c->stash('courseID')) {
push @errors, $c->maketext('You cannot delete the course you are currently using.');
}
return @errors;
}
sub delete_course_confirm ($c) {
return $c->include('ContentGenerator/CourseAdmin/delete_course_confirm');
}
sub do_delete_course ($c) {
my $ce = $c->ce;
my $db = $c->db;
my $delete_courseID = $c->param('delete_courseID') || '';
eval {
deleteCourse(
courseID => $delete_courseID,
ce => WeBWorK::CourseEnvironment->new({ courseName => $delete_courseID }),
);
};
if ($@) {
my $error = $@;
return $c->tag(
'div',
class => 'alert alert-danger p-1 my-2',
$c->c($c->tag('p', $c->maketext('An error occurred while deleting the course [_1]:', $delete_courseID)),
$c->tag('div', class => 'font-monospace', $error))->join('')
);
} else {
writeLog($ce, 'hosted_courses', join("\t", "\tDeleted", '', '', $delete_courseID));
return $c->c(
$c->tag(
'div',
class => 'alert alert-success p-1 my-2',
$c->maketext('Successfully deleted the course [_1].', $delete_courseID),
),
$c->form_for(
$c->current_route,
method => 'POST',
$c->c(
$c->hidden_authen_fields,
$c->hidden_fields('subDisplay'),
$c->tag(
'div',
class => 'text-center',
$c->submit_button(
$c->maketext('OK'),
name => 'decline_delete_course',
class => 'btn btn-primary'
)
)
)->join('')
)
)->join('');
}
}
sub archive_course_form ($c) {
my $ce = $c->ce;
my @courseIDs = grep { $_ ne $c->stash('courseID') } listCourses($ce);
my %courseLabels;
if (@courseIDs) {
# Get and store last modify time for login.log for all courses. Also get visibility status.
my @noLoginLogIDs;
my @loginLogIDs;
my ($loginLogFile, $epoch_modify_time, $courseDir, %coursesData);
for my $courseID (@courseIDs) {
$loginLogFile = "$ce->{webworkDirs}{courses}/$courseID/logs/login.log";
if (-e $loginLogFile) {
# The login log file should always exist except for the model course.
$epoch_modify_time = stat($loginLogFile)->mtime;
$coursesData{$courseID}{epoch_modify_time} = $epoch_modify_time;
$coursesData{$courseID}{local_modify_time} = ctime($epoch_modify_time);
push(@loginLogIDs, $courseID);
} else {
# This is for the model course.
$coursesData{$courseID}{local_modify_time} = 'no login.log';
push(@noLoginLogIDs, $courseID);
}
if (-f "$ce->{webworkDirs}{courses}/$courseID/hide_directory") {
$coursesData{$courseID}{status} = $c->maketext('hidden');
} else {
$coursesData{$courseID}{status} = $c->maketext('visible');
}
$courseLabels{$courseID} =
"$courseID ($coursesData{$courseID}{status} :: $coursesData{$courseID}{local_modify_time}) ";
}
if (($c->param('archive_listing_format') // 'alphabetically') eq 'last_login') {
# This should be an empty array except for the model course
@noLoginLogIDs = sort { lc($a) cmp lc($b) } @noLoginLogIDs;
@loginLogIDs = sort { $coursesData{$a}{epoch_modify_time} <=> $coursesData{$b}{epoch_modify_time} }
@loginLogIDs; # Oldest first
@courseIDs = (@noLoginLogIDs, @loginLogIDs);
} else {
# in this case we sort alphabetically
@courseIDs = sort { lc($a) cmp lc($b) } @courseIDs;
}
}
return $c->include(
'ContentGenerator/CourseAdmin/archive_course_form',
courseIDs => \@courseIDs,
courseLabels => \%courseLabels
);
}
sub archive_course_validate ($c) {
my @archive_courseIDs = $c->param('archive_courseIDs');
my @errors;
push(@errors, $c->maketext('You must select a course to archive')) unless @archive_courseIDs;
for my $archive_courseID (@archive_courseIDs) {
if ($archive_courseID eq '') {
push @errors, $c->maketext('You must specify a course name.');
} elsif ($archive_courseID eq $c->stash('courseID')) {
push @errors, $c->maketext('You cannot archive the course you are currently using.');
}
}
return @errors;
}
sub archive_course_confirm ($c) {
my $ce = $c->ce;
my @archive_courseIDs = $c->param('archive_courseIDs');
# If we are skipping a course remove one from the list of courses
shift @archive_courseIDs if defined $c->param('skip_archive_course');
my $archive_courseID = $archive_courseIDs[0];
my $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $archive_courseID });
my $CIchecker = WeBWorK::Utils::CourseDBIntegrityCheck->new($ce2);
# Check database
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($archive_courseID);
# Upgrade the database if requested.
my @upgrade_report;
if ($c->param('upgrade_course_tables')) {
my @schema_table_names = keys %$dbStatus;
my @tables_to_create =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A } @schema_table_names;
my @tables_to_alter =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B }
@schema_table_names;
push(@upgrade_report, $CIchecker->updateCourseTables($archive_courseID, [@tables_to_create]));
for my $table_name (@tables_to_alter) {
push(@upgrade_report, $CIchecker->updateTableFields($archive_courseID, $table_name));
}
($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($archive_courseID);
}
# Update and check directories and links.
my $dir_update_messages = $c->param('upgrade_course_tables') ? updateCourseDirectories($ce2) : [];
my $link_update_messages = $c->param('upgrade_course_tables') ? updateCourseLinks($ce2) : [];
my ($directories_ok, $directory_report) = checkCourseDirectories($ce2);
my ($links_ok, $link_report) = checkCourseLinks($ce2);
return $c->include(
'ContentGenerator/CourseAdmin/archive_course_confirm',
ce2 => $ce2,
upgrade_report => \@upgrade_report,
tables_ok => $tables_ok,
dbStatus => $dbStatus,
dir_update_messages => $dir_update_messages,