[system] / trunk / webwork-modperl / lib / WeBWorK / ContentGenerator / CourseAdmin.pm Repository:
ViewVC logotype

Diff of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/CourseAdmin.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2391 Revision 2478
1################################################################################ 1################################################################################
2# WeBWorK Online Homework Delivery System 2# WeBWorK Online Homework Delivery System
3# Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ 3# Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4# $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/CourseAdmin.pm,v 1.21 2004/06/24 17:43:32 sh002i Exp $ 4# $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/CourseAdmin.pm,v 1.22 2004/06/24 20:54:52 sh002i Exp $
5# 5#
6# This program is free software; you can redistribute it and/or modify it under 6# This program is free software; you can redistribute it and/or modify it under
7# the terms of either: (a) the GNU General Public License as published by the 7# the terms of either: (a) the GNU General Public License as published by the
8# Free Software Foundation; either version 2, or (at your option) any later 8# Free Software Foundation; either version 2, or (at your option) any later
9# version, or (b) the "Artistic License" which comes with this package. 9# version, or (b) the "Artistic License" which comes with this package.
45 # check permissions 45 # check permissions
46 unless ($authz->hasPermissions($user, "create_and_delete_courses")) { 46 unless ($authz->hasPermissions($user, "create_and_delete_courses")) {
47 $self->addmessage( CGI::div({class=>'ResultsWithError'},"$user is not authorized to create or delete courses") ); 47 $self->addmessage( CGI::div({class=>'ResultsWithError'},"$user is not authorized to create or delete courses") );
48 return; 48 return;
49 } 49 }
50 50
51 ## if the user is asking for the downloaded database...
51 if (defined $r->param("download_exported_database")) { 52 #if (defined $r->param("download_exported_database")) {
53 # my $courseID = $r->param("export_courseID");
54 # my $random_chars = $r->param("download_exported_database");
55 #
56 # die "courseID not specified" unless defined $courseID;
57 # die "invalid file specification" unless $random_chars =~ m/^\w+$/;
58 #
59 # my $tempdir = $ce->{webworkDirs}->{tmp};
60 # my $export_file = "$tempdir/db_export_$random_chars";
61 #
62 # $self->reply_with_file("application/xml", $export_file, "${courseID}_database.xml", 0);
63 #
64 # return "";
65 #}
66 #
67 ## otherwise...
68
69 my @errors;
70 my $method_to_call;
71
72 my $subDisplay = $r->param("subDisplay");
73 if (defined $subDisplay) {
74
75 if ($subDisplay eq "add_course") {
76 if (defined $r->param("add_course")) {
77 @errors = $self->add_course_validate;
78 if (@errors) {
79 $method_to_call = "add_course_form";
80 } else {
81 $method_to_call = "do_add_course";
82 }
83 } else {
84 $method_to_call = "add_course_form";
85 }
86 }
87
88 elsif ($subDisplay eq "delete_course") {
89 if (defined $r->param("delete_course")) {
90 # validate or confirm
91 @errors = $self->delete_course_validate;
92 if (@errors) {
93 $method_to_call = "delete_course_form";
94 } else {
95 $method_to_call = "delete_course_confirm";
96 }
97 } elsif (defined $r->param("confirm_delete_course")) {
98 # validate and delete
99 @errors = $self->delete_course_validate;
100 if (@errors) {
101 $method_to_call = "delete_course_form";
102 } else {
103 $method_to_call = "do_delete_course";
104 }
105 } else {
106 # form only
107 $method_to_call = "delete_course_form";
108 }
109 }
110
111 elsif ($subDisplay eq "export_database") {
112 if (defined $r->param("export_database")) {
113 @errors = $self->export_database_validate;
114 if (@errors) {
115 $method_to_call = "export_database_form";
116 } else {
117 # we have to do something special here, since we're sending
118 # the database as we export it. $method_to_call still gets
119 # set here, but it gets caught by header() and content()
120 # below instead of by body().
121 $method_to_call = "do_export_database";
122 }
123 } else {
124 $method_to_call = "export_database_form";
125 }
126 }
127
128 elsif ($subDisplay eq "import_database") {
129 if (defined $r->param("import_database")) {
130 @errors = $self->import_database_validate;
131 if (@errors) {
132 $method_to_call = "import_database_form";
133 } else {
134 $method_to_call = "do_import_database";
135 }
136 } else {
137 $method_to_call = "import_database_form";
138 }
139 }
140
141 else {
142 @errors = "Unrecognized sub-display @{[ CGI::b($subDisplay) ]}.";
143 }
144
145 }
146
147 $self->{errors} = \@errors;
148 $self->{method_to_call} = $method_to_call;
149}
150
151sub header {
152 my ($self) = @_;
153 my $method_to_call = $self->{method_to_call};
154 if (defined $method_to_call and $method_to_call eq "do_export_database") {
155 my $r = $self->r;
52 my $courseID = $r->param("export_courseID"); 156 my $courseID = $r->param("export_courseID");
53 my $random_chars = $r->param("download_exported_database"); 157 $r->content_type("application/octet-stream");
54 158 $r->header_out("Content-Disposition" => "attachment; filename=\"${courseID}_database.xml\"");
55 die "courseID not specified" unless defined $courseID; 159 $r->send_http_header;
56 die "invalid file specification" unless $random_chars =~ m/^\w+$/; 160 } else {
57 161 $self->SUPER::header;
58 my $tempdir = $ce->{webworkDirs}->{tmp}; 162 }
59 my $export_file = "$tempdir/db_export_$random_chars"; 163}
60 164
61 $self->reply_with_file("application/xml", $export_file, "${courseID}_database.xml", 0); 165# sends:
166# HTTP/1.1 200 OK
167# Date: Fri, 09 Jul 2004 19:05:55 GMT
168# Server: Apache/1.3.27 (Unix) mod_perl/1.27
169# Content-Disposition: attachment; filename="mth143_database.xml"
170# Connection: close
171# Content-Type: application/octet-stream
172
173sub content {
174 my ($self) = @_;
175 my $method_to_call = $self->{method_to_call};
176 if (defined $method_to_call and $method_to_call eq "do_export_database") {
177 $self->do_export_database;
178 } else {
179 $self->SUPER::content;
62 } 180 }
63} 181}
64 182
65sub body { 183sub body {
66 my ($self) = @_; 184 my ($self) = @_;
68 my $ce = $r->ce; 186 my $ce = $r->ce;
69 my $db = $r->db; 187 my $db = $r->db;
70 my $authz = $r->authz; 188 my $authz = $r->authz;
71 my $urlpath = $r->urlpath; 189 my $urlpath = $r->urlpath;
72 190
73 my $user = $r->param('user'); 191 my $user = $r->param('user');
74 192
75 # check permissions 193 # check permissions
76 unless ($authz->hasPermissions($user, "create_and_delete_courses")) { 194 unless ($authz->hasPermissions($user, "create_and_delete_courses")) {
77 return ""; 195 return "";
78 } 196 }
79 197
80 print CGI::p({style=>"text-align: center"}, 198 print CGI::p({style=>"text-align: center"},
81 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"add_course"})}, "Add Course"), 199 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"add_course"})}, "Add Course"),
82 #" | ",
83 #CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"rename_course"})}, "Rename Course"),
84 " | ", 200 " | ",
85 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"delete_course"})}, "Delete Course"), 201 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"delete_course"})}, "Delete Course"),
86 " | ", 202 " | ",
87 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"export_database"})}, "Export Database"), 203 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"export_database"})}, "Export Database"),
88 " | ", 204 " | ",
89 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"import_database"})}, "Import Database"), 205 CGI::a({href=>$self->systemLink($urlpath, params=>{subDisplay=>"import_database"})}, "Import Database"),
90 ); 206 );
91 207
92 print CGI::hr(); 208 print CGI::hr();
93 209
94 my $subDisplay = $r->param("subDisplay"); 210 my @errors = @{$self->{errors}};
95 if (defined $subDisplay) { 211 my $method_to_call = $self->{method_to_call};
96 212
97 if ($subDisplay eq "add_course") {
98 if (defined $r->param("add_course")) {
99 my @errors = $self->add_course_validate;
100 if (@errors) { 213 if (@errors) {
101 print CGI::div({class=>"ResultsWithError"},
102 CGI::p("Please correct the following errors and try again:"),
103 CGI::ul(CGI::li(\@errors)),
104 );
105 $self->add_course_form;
106 } else {
107 $self->do_add_course;
108 }
109 } else {
110 $self->add_course_form;
111 }
112 }
113
114 elsif ($subDisplay eq "delete_course") {
115 if (defined $r->param("delete_course")) {
116 # validate or confirm
117 my @errors = $self->delete_course_validate;
118 if (@errors) {
119 print CGI::div({class=>"ResultsWithError"},
120 CGI::p("Please correct the following errors and try again:"),
121 CGI::ul(CGI::li(\@errors)),
122 );
123 $self->delete_course_form;
124 } else {
125 $self->delete_course_confirm;
126 }
127 } elsif (defined $r->param("confirm_delete_course")) {
128 # validate and delete
129 my @errors = $self->delete_course_validate;
130 if (@errors) {
131 print CGI::div({class=>"ResultsWithError"},
132 CGI::p("Please correct the following errors and try again:"),
133 CGI::ul(CGI::li(\@errors)),
134 );
135 $self->delete_course_form;
136 } else {
137 $self->do_delete_course;
138 }
139 } else {
140 # form only
141 $self->delete_course_form;
142 }
143 }
144
145 elsif ($subDisplay eq "export_database") {
146 if (defined $r->param("export_database")) {
147 my @errors = $self->export_database_validate;
148 if (@errors) {
149 print CGI::div({class=>"ResultsWithError"},
150 CGI::p("Please correct the following errors and try again:"),
151 CGI::ul(CGI::li(\@errors)),
152 );
153 $self->export_database_form;
154 } else {
155 $self->do_export_database;
156 }
157 } else {
158 $self->export_database_form;
159 }
160 }
161
162 elsif ($subDisplay eq "import_database") {
163 if (defined $r->param("import_database")) {
164 my @errors = $self->import_database_validate;
165 if (@errors) {
166 print CGI::div({class=>"ResultsWithError"},
167 CGI::p("Please correct the following errors and try again:"),
168 CGI::ul(CGI::li(\@errors)),
169 );
170 $self->import_database_form;
171 } else {
172 $self->do_import_database;
173 }
174 } else {
175 $self->import_database_form;
176 }
177 }
178
179 else {
180 print CGI::div({class=>"ResultsWithError"}, 214 print CGI::div({class=>"ResultsWithError"},
181 "Unrecognized sub-display @{[ CGI::b($subDisplay) ]}."); 215 CGI::p("Please correct the following errors and try again:"),
216 CGI::ul(CGI::li(\@errors)),
217 );
182 } 218 }
183 219
220 if (defined $method_to_call and $method_to_call ne "") {
221 $self->$method_to_call;
184 } 222 }
185 223
186 return ""; 224 return "";
187} 225}
188 226
961 $courseLabels{$courseID} = "$courseID (" . $tempCE->{dbLayoutName} . ")"; 999 $courseLabels{$courseID} = "$courseID (" . $tempCE->{dbLayoutName} . ")";
962 } 1000 }
963 1001
964 print CGI::h2("Export Database"); 1002 print CGI::h2("Export Database");
965 1003
966 print CGI::start_form("POST", $r->uri); 1004 print CGI::start_form("GET", $r->uri);
967 print $self->hidden_authen_fields; 1005 print $self->hidden_authen_fields;
968 print $self->hidden_fields("subDisplay"); 1006 print $self->hidden_fields("subDisplay");
969 1007
970 print CGI::p("Select a course to export the course's database."); 1008 print CGI::p("Select a course to export the course's database.");
971 1009
1043 $export_courseID, 1081 $export_courseID,
1044 ); 1082 );
1045 1083
1046 my $db2 = new WeBWorK::DB($ce2->{dbLayout}); 1084 my $db2 = new WeBWorK::DB($ce2->{dbLayout});
1047 1085
1048 my ($fh, $export_file) = tempfile("db_export_XXXXXX", DIR => $ce->{webworkDirs}->{tmp}); 1086 #my ($fh, $export_file) = tempfile("db_export_XXXXXX", DIR => $ce->{webworkDirs}->{tmp});
1049 my ($random_chars) = $export_file =~ m/db_export_(\w+)$/; 1087 #my ($random_chars) = $export_file =~ m/db_export_(\w+)$/;
1050 1088
1051 my @errors; 1089 my @errors;
1052 1090
1053 eval { 1091 eval {
1054 @errors = dbExport( 1092 @errors = dbExport(
1055 db => $db2, 1093 db => $db2,
1056 xml => $fh, 1094 #xml => $fh,
1095 xml => *STDOUT,
1057 tables => \@export_tables, 1096 tables => \@export_tables,
1058 ); 1097 );
1059 }; 1098 };
1060 1099
1061 push @errors, "Fatal exception: $@" if $@; 1100 #push @errors, "Fatal exception: $@" if $@;
1062 1101 #
1063 if (@errors) { 1102 #if (@errors) {
1064 print CGI::div({class=>"ResultsWithError"}, 1103 # print CGI::div({class=>"ResultsWithError"},
1065 CGI::p("An error occured while exporting the database of course $export_courseID:"), 1104 # CGI::p("An error occured while exporting the database of course $export_courseID:"),
1066 CGI::ul(CGI::li(\@errors)), 1105 # CGI::ul(CGI::li(\@errors)),
1067 ); 1106 # );
1068 } else { 1107 #} else {
1069 print CGI::div({class=>"ResultsWithoutError"}, 1108 # print CGI::div({class=>"ResultsWithoutError"},
1070 CGI::p("Export succeeded."), 1109 # CGI::p("Export succeeded."),
1071 ); 1110 # );
1072 1111 #
1073 print CGI::div({style=>"text-align: center"}, 1112 # print CGI::div({style=>"text-align: center"},
1074 CGI::a({href=>$self->systemLink($urlpath, params=>{download_exported_database=>$random_chars, export_courseID=>undef})}, "Download Exported Database"), 1113 # CGI::a({href=>$self->systemLink($urlpath, params=>{download_exported_database=>$random_chars, export_courseID=>undef})}, "Download Exported Database"),
1075 ); 1114 # );
1076 } 1115 #}
1077} 1116}
1078 1117
1079################################################################################ 1118################################################################################
1080 1119
1081sub import_database_form { 1120sub import_database_form {

Legend:
Removed from v.2391  
changed lines
  Added in v.2478

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9