Passed
Branch master (28cf04)
by John
14:14
created
app/Admin/Controllers/ProblemController.php 1 patch
Spacing   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -143,11 +143,11 @@  discard block
 block discarded – undo
143 143
      *
144 144
      * @return Form
145 145
      */
146
-    protected function form($create = false)
146
+    protected function form($create=false)
147 147
     {
148 148
         $form=new Form(new EloquentProblemModel);
149 149
         $form->model()->makeVisible('password');
150
-        $form->tab('Basic', function(Form $form){
150
+        $form->tab('Basic', function(Form $form) {
151 151
             $form->text('pid')->readonly();
152 152
             $form->text('pcode')->rules('required');
153 153
             $form->text('title')->rules('required');
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
             $form->simplemde('input');
158 158
             $form->simplemde('output');
159 159
             $form->simplemde('note');
160
-            $form->hasMany('problemSamples', 'samples', function (Form\NestedForm $form) {
160
+            $form->hasMany('problemSamples', 'samples', function(Form\NestedForm $form) {
161 161
                 $form->textarea('sample_input', 'sample input')->rows(3);
162 162
                 $form->textarea('sample_output', 'sample output')->rows(3);
163 163
                 $form->textarea('sample_note', 'sample note')->rows(3);
@@ -167,10 +167,10 @@  discard block
 block discarded – undo
167 167
                 $table->textarea('sample_output', 'sample output');
168 168
                 $table->textarea('sample_note', 'sample note');
169 169
             }); */
170
-            $ojs_temp = EloquentOJModel::select('oid', 'name')->get()->all();
171
-            $ojs = [];
172
-            foreach($ojs_temp as $v){
173
-                $ojs[$v->oid] = $v->name;
170
+            $ojs_temp=EloquentOJModel::select('oid', 'name')->get()->all();
171
+            $ojs=[];
172
+            foreach ($ojs_temp as $v) {
173
+                $ojs[$v->oid]=$v->name;
174 174
             }
175 175
             $form->select('oj', 'OJ')->options($ojs)->default(1)->rules('required');
176 176
             /* $form->display('update_date'); */
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
                     0 => 'NO',
190 190
                     1 => 'YES',
191 191
                 ])->default(0)->rules('required');
192
-            $form->clang('spj_src','SPJ Source Code');
192
+            $form->clang('spj_src', 'SPJ Source Code');
193 193
             $form->file('test_case')->rules('required');
194 194
             $form->ignore(['test_case']);
195 195
 
@@ -211,125 +211,125 @@  discard block
 block discarded – undo
211 211
                 $tools->append('<a href="/'.config('admin.route.prefix').'/problems/import" class="btn btn-sm btn-success" style="margin-right:1rem"><i class="MDI file-powerpoint-box"></i>&nbsp;&nbsp;Import from file</a>');
212 212
             });
213 213
         } */
214
-        $form->saving(function (Form $form){
215
-            $err = function ($msg, $title = 'Test case file parse faild.') {
216
-                $error = new MessageBag([
214
+        $form->saving(function(Form $form) {
215
+            $err=function($msg, $title='Test case file parse faild.') {
216
+                $error=new MessageBag([
217 217
                     'title'   => $title,
218 218
                     'message' => $msg,
219 219
                 ]);
220 220
                 return back()->with(compact('error'));
221 221
             };
222
-            $pcode = $form->pcode;
223
-            $p = EloquentProblemModel::where('pcode',$pcode)->first();
222
+            $pcode=$form->pcode;
223
+            $p=EloquentProblemModel::where('pcode', $pcode)->first();
224 224
             //check pcode has been token.
225
-            $pid = $form->pid ?? null;
226
-            if(!empty($p) && $p->pid != $pid){
225
+            $pid=$form->pid ?? null;
226
+            if (!empty($p) && $p->pid!=$pid) {
227 227
                 $err('Pcode has been token', 'Error occur.');
228 228
             }
229
-            $test_case = \request()->file('test_case');
229
+            $test_case=\request()->file('test_case');
230 230
             //Make sure the user enters SPJ_SRc in spj problem.
231
-            if($form->spj && empty($form->spj_src)) {
231
+            if ($form->spj && empty($form->spj_src)) {
232 232
                 $err('The SPJ problem must provide spj_src', 'create problem error');
233 233
             }
234 234
             //check info file. Try to generate if it does not exist.
235
-            $info_content = [];
236
-            if(!empty($test_case)){
237
-                if($test_case->extension() != 'zip'){
235
+            $info_content=[];
236
+            if (!empty($test_case)) {
237
+                if ($test_case->extension()!='zip') {
238 238
                     $err('You must upload a zip file iuclude test case info and content.');
239 239
                 }
240
-                $path = $test_case->path();
241
-                $zip = new ZipArchive;
242
-                if($zip->open($path) !== true) {
240
+                $path=$test_case->path();
241
+                $zip=new ZipArchive;
242
+                if ($zip->open($path)!==true) {
243 243
                     $err('You must upload a zip file without encrypt and can open successfully.');
244 244
                 };
245
-                $info_content = [];
246
-                if(($zip->getFromName('info')) === false){
247
-                    if(!$form->spj){
248
-                        $info_content = [
245
+                $info_content=[];
246
+                if (($zip->getFromName('info'))===false) {
247
+                    if (!$form->spj) {
248
+                        $info_content=[
249 249
                             'spj' => false,
250 250
                             'test_cases' => []
251 251
                         ];
252
-                        $files = [];
253
-                        for ($i = 0; $i < $zip->numFiles; $i++) {
254
-                            $filename = $zip->getNameIndex($i);
255
-                            $files[] = $filename;
252
+                        $files=[];
253
+                        for ($i=0; $i<$zip->numFiles; $i++) {
254
+                            $filename=$zip->getNameIndex($i);
255
+                            $files[]=$filename;
256 256
                         }
257
-                        $files_in = array_filter($files, function ($filename) {
258
-                            return strpos('.in', $filename) != -1;
257
+                        $files_in=array_filter($files, function($filename) {
258
+                            return strpos('.in', $filename)!=-1;
259 259
                         });
260 260
                         sort($files_in);
261
-                        $testcase_index = 1;
262
-                        foreach($files_in as $filename_in){
263
-                            $filename = basename($filename_in, '.in');
264
-                            $filename_out = $filename.'.out';
265
-                            if(($zip->getFromName($filename_out)) === false) {
261
+                        $testcase_index=1;
262
+                        foreach ($files_in as $filename_in) {
263
+                            $filename=basename($filename_in, '.in');
264
+                            $filename_out=$filename.'.out';
265
+                            if (($zip->getFromName($filename_out))===false) {
266 266
                                 continue;
267 267
                             }
268
-                            $test_case_in = preg_replace('~(*BSR_ANYCRLF)\R~',"\n", $zip->getFromName($filename_in));
269
-                            $test_case_out = preg_replace('~(*BSR_ANYCRLF)\R~',"\n", $zip->getFromName($filename_out));
270
-                            $info_content['test_cases']["{$testcase_index}"] = [
268
+                            $test_case_in=preg_replace('~(*BSR_ANYCRLF)\R~', "\n", $zip->getFromName($filename_in));
269
+                            $test_case_out=preg_replace('~(*BSR_ANYCRLF)\R~', "\n", $zip->getFromName($filename_out));
270
+                            $info_content['test_cases']["{$testcase_index}"]=[
271 271
                                 'input_size' => strlen($test_case_in),
272 272
                                 'input_name' => $filename_in,
273 273
                                 'output_size' => strlen($test_case_out),
274 274
                                 'output_name' => $filename_out,
275 275
                                 'stripped_output_md5' => md5(utf8_encode(rtrim($test_case_out)))
276 276
                             ];
277
-                            $testcase_index += 1;
277
+                            $testcase_index+=1;
278 278
                         }
279
-                    }else{
280
-                        $info_content = [
279
+                    } else {
280
+                        $info_content=[
281 281
                             'spj' => true,
282 282
                             'test_cases' => []
283 283
                         ];
284
-                        $files = [];
285
-                        for ($i = 0; $i < $zip->numFiles; $i++) {
286
-                            $filename = $zip->getNameIndex($i);
287
-                            $files[] = $filename;
284
+                        $files=[];
285
+                        for ($i=0; $i<$zip->numFiles; $i++) {
286
+                            $filename=$zip->getNameIndex($i);
287
+                            $files[]=$filename;
288 288
                         }
289
-                        $files_in = array_filter($files, function ($filename) {
290
-                            return strpos($filename, '.in') !== false;
289
+                        $files_in=array_filter($files, function($filename) {
290
+                            return strpos($filename, '.in')!==false;
291 291
                         });
292 292
                         sort($files_in);
293
-                        $testcase_index = 1;
294
-                        foreach($files_in as $filename_in){
295
-                            $test_case_in = $zip->getFromName($filename_in);
296
-                            $info_content['test_cases']["{$testcase_index}"] = [
293
+                        $testcase_index=1;
294
+                        foreach ($files_in as $filename_in) {
295
+                            $test_case_in=$zip->getFromName($filename_in);
296
+                            $info_content['test_cases']["{$testcase_index}"]=[
297 297
                                 'input_size' => strlen($test_case_in),
298 298
                                 'input_name' => $filename_in
299 299
                             ];
300
-                            $testcase_index += 1;
300
+                            $testcase_index+=1;
301 301
                         }
302 302
                     }
303 303
                     $zip->addFromString('info', json_encode($info_content));
304 304
                     $zip->close();
305 305
                     //$err('The zip files must include a file named info including info of test cases, and the format can see ZsgsDesign/NOJ wiki.');
306
-                }else{
307
-                    $info_content = json_decode($zip->getFromName('info'),true);
306
+                } else {
307
+                    $info_content=json_decode($zip->getFromName('info'), true);
308 308
                 };
309 309
                 $zip->open($path);
310 310
                 //If there is an INFO file, check that the contents of the file match the actual situation
311
-                $test_cases = $info_content['test_cases'];
311
+                $test_cases=$info_content['test_cases'];
312 312
                 //dd($test_cases);
313
-                foreach($test_cases as $index => $case) {
314
-                    if(!isset($case['input_name']) || (!$form->spj && !isset($case['output_name']))) {
313
+                foreach ($test_cases as $index => $case) {
314
+                    if (!isset($case['input_name']) || (!$form->spj && !isset($case['output_name']))) {
315 315
                         $err("Test case index {$index}: configuration missing input/output files name.");
316 316
                     }
317
-                    if($zip->getFromName($case['input_name']) === false || (!$form->spj && $zip->getFromName($case['output_name']) === false)) {
317
+                    if ($zip->getFromName($case['input_name'])===false || (!$form->spj && $zip->getFromName($case['output_name'])===false)) {
318 318
                         $err("Test case index {$index}: missing input/output files that record in the configuration.");
319 319
                     }
320 320
                 }
321
-                if(!empty($form->pid)){
322
-                    $problem = EloquentProblemModel::find($form->pid);
323
-                    if(!empty($problem)){
324
-                        $pcode = $problem->pcode;
325
-                    }else{
326
-                        $pcode = $form->pcode;
321
+                if (!empty($form->pid)) {
322
+                    $problem=EloquentProblemModel::find($form->pid);
323
+                    if (!empty($problem)) {
324
+                        $pcode=$problem->pcode;
325
+                    } else {
326
+                        $pcode=$form->pcode;
327 327
                     }
328
-                }else{
329
-                    $pcode = $form->pcode;
328
+                } else {
329
+                    $pcode=$form->pcode;
330 330
                 }
331 331
 
332
-                if(Storage::exists(base_path().'/storage/test_case/'.$pcode)){
332
+                if (Storage::exists(base_path().'/storage/test_case/'.$pcode)) {
333 333
                     Storage::deleteDirectory(base_path().'/storage/test_case/'.$pcode);
334 334
                 }
335 335
                 Storage::makeDirectory(base_path().'/storage/test_case/'.$pcode);
@@ -337,19 +337,19 @@  discard block
 block discarded – undo
337 337
 
338 338
             }
339 339
             //Set the spj-related data
340
-            if($form->spj){
341
-                $form->spj_lang = 'c';
342
-                $form->spj_version = "{$form->pcode}#".time();
340
+            if ($form->spj) {
341
+                $form->spj_lang='c';
342
+                $form->spj_version="{$form->pcode}#".time();
343 343
             }
344 344
             //Set default data
345
-            $form->tot_score = count($info_content['test_cases']);
346
-            $form->markdown = true;
347
-            $form->input_type = 'standard input';
348
-            $form->output_type = 'standard output';
349
-            $form->solved_count = 0;
350
-            $form->difficulty = -1;
351
-            $form->partial = 1;
352
-            $form->file = 0;
345
+            $form->tot_score=count($info_content['test_cases']);
346
+            $form->markdown=true;
347
+            $form->input_type='standard input';
348
+            $form->output_type='standard output';
349
+            $form->solved_count=0;
350
+            $form->difficulty=-1;
351
+            $form->partial=1;
352
+            $form->file=0;
353 353
         });
354 354
         return $form;
355 355
     }
Please login to merge, or discard this patch.