|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Admin\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Eloquent\Problem as EloquentProblemModel; |
|
6
|
|
|
use App\Models\Eloquent\OJ as EloquentOJModel; |
|
7
|
|
|
use App\Http\Controllers\Controller; |
|
8
|
|
|
use App\Admin\Forms\ImportPOEM; |
|
9
|
|
|
use Encore\Admin\Controllers\HasResourceActions; |
|
10
|
|
|
use Encore\Admin\Form; |
|
11
|
|
|
use Encore\Admin\Grid; |
|
12
|
|
|
use Encore\Admin\Layout\Content; |
|
13
|
|
|
use Encore\Admin\Show; |
|
14
|
|
|
use Illuminate\Support\MessageBag; |
|
15
|
|
|
use ZipArchive; |
|
16
|
|
|
use Illuminate\Support\Facades\Storage; |
|
17
|
|
|
|
|
18
|
|
|
class ProblemController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
use HasResourceActions; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Index interface. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Content $content |
|
26
|
|
|
* @return Content |
|
27
|
|
|
*/ |
|
28
|
|
|
public function index(Content $content) |
|
29
|
|
|
{ |
|
30
|
|
|
return $content |
|
31
|
|
|
->header('Problems') |
|
32
|
|
|
->description('all problems') |
|
33
|
|
|
->body($this->grid()->render()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Show interface. |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $id |
|
40
|
|
|
* @param Content $content |
|
41
|
|
|
* @return Content |
|
42
|
|
|
*/ |
|
43
|
|
|
public function show($id, Content $content) |
|
44
|
|
|
{ |
|
45
|
|
|
return $content |
|
46
|
|
|
->header('Problem Detail') |
|
47
|
|
|
->description('the detail of problems') |
|
48
|
|
|
->body($this->detail($id)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Edit interface. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $id |
|
55
|
|
|
* @param Content $content |
|
56
|
|
|
* @return Content |
|
57
|
|
|
*/ |
|
58
|
|
|
public function edit($id, Content $content) |
|
59
|
|
|
{ |
|
60
|
|
|
return $content |
|
61
|
|
|
->header('Edit Problem') |
|
62
|
|
|
->description('edit the detail of problems') |
|
63
|
|
|
->body($this->form()->edit($id)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Import interface. |
|
68
|
|
|
* |
|
69
|
|
|
* @param Content $content |
|
70
|
|
|
* @return Content |
|
71
|
|
|
*/ |
|
72
|
|
|
public function import(Content $content) |
|
73
|
|
|
{ |
|
74
|
|
|
return $content |
|
75
|
|
|
->header('Import New Problem') |
|
76
|
|
|
->description('import a new problem from POEM or POETRY') |
|
77
|
|
|
->body(new ImportPOEM()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create interface. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Content $content |
|
84
|
|
|
* @return Content |
|
85
|
|
|
*/ |
|
86
|
|
|
public function create(Content $content) |
|
87
|
|
|
{ |
|
88
|
|
|
return $content |
|
89
|
|
|
->header('Create New Problem') |
|
90
|
|
|
->description('create a new problem') |
|
91
|
|
|
->body($this->form(true)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Make a grid builder. |
|
96
|
|
|
* |
|
97
|
|
|
* @return Grid |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function grid() |
|
100
|
|
|
{ |
|
101
|
|
|
$grid=new Grid(new EloquentProblemModel); |
|
102
|
|
|
$grid->column('pid', "ID")->sortable(); |
|
103
|
|
|
$grid->column('pcode', "PCode")->editable(); |
|
104
|
|
|
$grid->title("Title")->editable(); |
|
105
|
|
|
$grid->solved_count(); |
|
106
|
|
|
$grid->time_limit("Time/ms")->editable(); |
|
107
|
|
|
$grid->memory_limit("Memory/kb")->editable(); |
|
108
|
|
|
$grid->OJ(); |
|
109
|
|
|
$grid->update_date(); |
|
110
|
|
|
$grid->tot_score("Score"); |
|
111
|
|
|
$grid->partial("Partial")->display(function($partial) { |
|
112
|
|
|
return $partial ? 'Yes' : 'No'; |
|
113
|
|
|
}); |
|
114
|
|
|
$grid->markdown("Markdown")->display(function($markdown) { |
|
115
|
|
|
return $markdown ? 'Yes' : 'No'; |
|
116
|
|
|
}); |
|
117
|
|
|
$grid->order_index("order")->sortable(); |
|
118
|
|
|
$grid->filter(function(Grid\Filter $filter) { |
|
119
|
|
|
$filter->disableIdFilter(); |
|
120
|
|
|
$filter->like('pcode'); |
|
121
|
|
|
$filter->like('title'); |
|
122
|
|
|
}); |
|
123
|
|
|
|
|
124
|
|
|
// $grid->disableCreateButton(); |
|
125
|
|
|
|
|
126
|
|
|
return $grid; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Make a show builder. |
|
131
|
|
|
* |
|
132
|
|
|
* @param mixed $id |
|
133
|
|
|
* @return Show |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function detail($id) |
|
136
|
|
|
{ |
|
137
|
|
|
$show=new Show(EloquentProblemModel::findOrFail($id)); |
|
138
|
|
|
return $show; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Make a form builder for create view and edit. |
|
143
|
|
|
* |
|
144
|
|
|
* @return Form |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function form($create = false) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$form=new Form(new EloquentProblemModel); |
|
149
|
|
|
$form->model()->makeVisible('password'); |
|
150
|
|
|
$form->tab('Basic', function(Form $form){ |
|
151
|
|
|
$form->text('pid')->readonly(); |
|
152
|
|
|
$form->text('pcode')->rules('required'); |
|
153
|
|
|
$form->text('title')->rules('required'); |
|
154
|
|
|
$form->text('time_limit')->rules('required'); |
|
155
|
|
|
$form->text('memory_limit')->rules('required'); |
|
156
|
|
|
$form->simplemde('description')->rules('required'); |
|
157
|
|
|
$form->simplemde('input'); |
|
158
|
|
|
$form->simplemde('output'); |
|
159
|
|
|
$form->simplemde('note'); |
|
160
|
|
|
$form->hasMany('problemSamples', 'samples', function (Form\NestedForm $form) { |
|
161
|
|
|
$form->textarea('sample_input', 'sample input')->rows(3); |
|
162
|
|
|
$form->textarea('sample_output', 'sample output')->rows(3); |
|
163
|
|
|
$form->textarea('sample_note', 'sample note')->rows(3); |
|
164
|
|
|
}); |
|
165
|
|
|
/* $form->table('samples', function ($table) { |
|
166
|
|
|
$table->textarea('sample_input', 'sample input'); |
|
167
|
|
|
$table->textarea('sample_output', 'sample output'); |
|
168
|
|
|
$table->textarea('sample_note', 'sample note'); |
|
169
|
|
|
}); */ |
|
170
|
|
|
$ojs_temp = EloquentOJModel::select('oid', 'name')->get()->all(); |
|
171
|
|
|
$ojs = []; |
|
172
|
|
|
foreach($ojs_temp as $v){ |
|
173
|
|
|
$ojs[$v->oid] = $v->name; |
|
174
|
|
|
} |
|
175
|
|
|
$form->select('oj', 'OJ')->options($ojs)->default(1)->rules('required'); |
|
176
|
|
|
$form->select('Hide')->options([ |
|
177
|
|
|
1 => 'yes', |
|
178
|
|
|
0 => 'no' |
|
179
|
|
|
])->default(0)->rules('required'); |
|
180
|
|
|
/* $form->display('update_date'); */ |
|
181
|
|
|
/* $form->text('tot_score')->rules('required'); |
|
182
|
|
|
$form->select('partial', 'Partial Score')->options([ |
|
183
|
|
|
0 => "No", |
|
184
|
|
|
1 => "Yes" |
|
185
|
|
|
])->rules('required'); */ |
|
186
|
|
|
$form->hidden('markdown'); |
|
187
|
|
|
$form->hidden('input_type'); |
|
188
|
|
|
$form->hidden('output_type'); |
|
189
|
|
|
$form->hidden('solved_count'); |
|
190
|
|
|
$form->hidden('difficulty'); |
|
191
|
|
|
$form->hidden('file'); |
|
192
|
|
|
|
|
193
|
|
|
$form->radio('spj', 'Use SPJ') |
|
194
|
|
|
->options([ |
|
195
|
|
|
0 => 'NO', |
|
196
|
|
|
1 => 'YES', |
|
197
|
|
|
])->when(0, function (Form $form) { |
|
|
|
|
|
|
198
|
|
|
})->when(1, function (Form $form) { |
|
|
|
|
|
|
199
|
|
|
// $form->clang('spj_src','SPJ Source Code')->rules('required'); |
|
200
|
|
|
// Admin::script("CodeMirror.fromTextArea(document.getElementById(\"spj_src\"), {'mode':'text/x-csrc','lineNumbers':true,'matchBrackets':true});"); |
|
201
|
|
|
})->rules('required'); |
|
202
|
|
|
$form->clang('spj_src','SPJ Source Code'); |
|
203
|
|
|
$form->file('test_case')->rules('required'); |
|
204
|
|
|
$form->ignore(['test_case']); |
|
205
|
|
|
}); |
|
206
|
|
|
/* if($create){ |
|
207
|
|
|
$form->tools(function (Form\Tools $tools) { |
|
208
|
|
|
$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> Import from file</a>'); |
|
209
|
|
|
}); |
|
210
|
|
|
} */ |
|
211
|
|
|
$form->saving(function (Form $form){ |
|
212
|
|
|
$err = function ($msg) { |
|
213
|
|
|
$error = new MessageBag([ |
|
214
|
|
|
'title' => 'Test case file parse faild.', |
|
215
|
|
|
'message' => $msg, |
|
216
|
|
|
]); |
|
217
|
|
|
return back()->with(compact('error')); |
|
218
|
|
|
}; |
|
219
|
|
|
$pcode = $form->pcode; |
|
220
|
|
|
$p = EloquentProblemModel::where('pcode',$pcode)->first(); |
|
221
|
|
|
$pid = $form->pid ?? null; |
|
222
|
|
|
if(!empty($p) && $p->pid != $pid){ |
|
223
|
|
|
$error = new MessageBag([ |
|
224
|
|
|
'title' => 'Error occur.', |
|
225
|
|
|
'message' => 'Pcode has been token', |
|
226
|
|
|
]); |
|
227
|
|
|
return back()->with(compact('error')); |
|
228
|
|
|
} |
|
229
|
|
|
$test_case = \request()->file('test_case'); |
|
230
|
|
|
if(!empty($test_case)){ |
|
231
|
|
|
if($test_case->extension() != 'zip'){ |
|
232
|
|
|
$err('You must upload a zip file iuclude test case info and content.'); |
|
233
|
|
|
} |
|
234
|
|
|
$path = $test_case->path(); |
|
235
|
|
|
$zip = new ZipArchive; |
|
236
|
|
|
if($zip->open($path) !== true) { |
|
237
|
|
|
$err('You must upload a zip file without encrypt and can open successfully.'); |
|
238
|
|
|
}; |
|
239
|
|
|
$info_content = []; |
|
240
|
|
|
if(($zip->getFromName('info')) === false){ |
|
241
|
|
|
$info_content = [ |
|
242
|
|
|
'spj' => false, |
|
243
|
|
|
'test_cases' => [] |
|
244
|
|
|
]; |
|
245
|
|
|
$files = []; |
|
246
|
|
|
for ($i = 0; $i < $zip->numFiles; $i++) { |
|
247
|
|
|
$filename = $zip->getNameIndex($i); |
|
248
|
|
|
$files[] = $filename; |
|
249
|
|
|
} |
|
250
|
|
|
$files_in = array_filter($files, function ($filename) { |
|
251
|
|
|
return strpos('.in', $filename) != -1; |
|
252
|
|
|
}); |
|
253
|
|
|
sort($files_in); |
|
254
|
|
|
$testcase_index = 1; |
|
255
|
|
|
foreach($files_in as $filename_in){ |
|
256
|
|
|
$filename = basename($filename_in, '.in'); |
|
257
|
|
|
$filename_out = $filename.'.out'; |
|
258
|
|
|
if(($zip->getFromName($filename_out)) === false) { |
|
259
|
|
|
continue; |
|
260
|
|
|
} |
|
261
|
|
|
$test_case_in = $zip->getFromName($filename_in); |
|
262
|
|
|
$test_case_out = $zip->getFromName($filename_out); |
|
263
|
|
|
$info_content['test_cases']["{$testcase_index}"] = [ |
|
264
|
|
|
'input_size' => strlen($test_case_in), |
|
265
|
|
|
'input_name' => $filename_in, |
|
266
|
|
|
'output_size' => strlen($test_case_out), |
|
267
|
|
|
'output_name' => $filename_out, |
|
268
|
|
|
'stripped_output_md5' => md5(utf8_encode(rtrim($test_case_out))) |
|
269
|
|
|
]; |
|
270
|
|
|
$testcase_index += 1; |
|
271
|
|
|
} |
|
272
|
|
|
$zip->addFromString('info', json_encode($info_content)); |
|
273
|
|
|
$zip->close(); |
|
274
|
|
|
//$err('The zip files must include a file named info including info of test cases, and the format can see ZsgsDesign/NOJ wiki.'); |
|
275
|
|
|
}else{ |
|
276
|
|
|
$info_content = json_decode($zip->getFromName('info'),true); |
|
277
|
|
|
}; |
|
278
|
|
|
$zip->open($path); |
|
279
|
|
|
$test_cases = $info_content['test_cases']; |
|
280
|
|
|
foreach($test_cases as $index => $case) { |
|
281
|
|
|
if(!isset($case['input_name']) || !isset($case['output_name'])) { |
|
282
|
|
|
$err("Test case index {$index}: configuration missing input/output files name."); |
|
283
|
|
|
} |
|
284
|
|
|
if($zip->getFromName($case['input_name']) === false || $zip->getFromName($case['output_name']) === false ) { |
|
285
|
|
|
$err("Test case index {$index}: missing input/output files that record in the configuration."); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
if(!empty($form->pid)){ |
|
289
|
|
|
$problem = EloquentProblemModel::find($form->pid); |
|
290
|
|
|
if(!empty($problem)){ |
|
291
|
|
|
$pcode = $problem->pcode; |
|
292
|
|
|
}else{ |
|
293
|
|
|
$pcode = $form->pcode; |
|
294
|
|
|
} |
|
295
|
|
|
}else{ |
|
296
|
|
|
$pcode = $form->pcode; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
if(Storage::exists(base_path().'/storage/test_case/'.$pcode)){ |
|
|
|
|
|
|
300
|
|
|
Storage::deleteDirectory(base_path().'/storage/test_case/'.$pcode); |
|
301
|
|
|
} |
|
302
|
|
|
Storage::makeDirectory(base_path().'/storage/test_case/'.$pcode); |
|
303
|
|
|
$zip->extractTo(base_path().'/storage/test_case/'.$pcode.'/'); |
|
304
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
$form->markdown = true; |
|
307
|
|
|
$form->input_type = 'standard input'; |
|
308
|
|
|
$form->output_type = 'standard output'; |
|
309
|
|
|
$form->solved_count = 0; |
|
310
|
|
|
$form->difficulty = -1; |
|
311
|
|
|
$form->file = 0; |
|
312
|
|
|
}); |
|
313
|
|
|
return $form; |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.