-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
194 lines (149 loc) · 5.66 KB
/
Plugin.php
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
<?php
namespace ValuersManagement;
use MapasCulturais\App;
use MapasCulturais\Controllers\Opportunity as ControllersOpportunity;
use MapasCulturais\Definitions\FileGroup;
use MapasCulturais\Entities\Opportunity;
use MapasCulturais\Entities\Registration;
use PhpOffice\PhpSpreadsheet\IOFactory;
class Plugin extends \MapasCulturais\Plugin
{
function __construct($config = [])
{
$config += [];
parent::__construct($config);
}
public function _init()
{
$app = App::i();
$app->view->enqueueStyle('app-v2', 'ValuersManagement-v2', 'css/plugin-ValuersManagement.css');
$self = $this;
$app->hook("component(opportunity-phase-config-evaluation).evaluation-step-header:end", function () {
$entity = $this->controller->requestedEntity;
$this->part('evalmaster--upload', ['entity' => $entity]);
});
$app->hook('GET(opportunity.valuersmanagement)', function () use ($self, $app) {
ini_set('max_execution_time', '0');
/** @var ControllersOpportunity $this */
$this->requireAuthentication();
$opportunity = $app->repo('Opportunity')->find($this->data['entity']);
if(!$opportunity) {
$app->pass();
}
$opportunity->checkPermission('@control');
$request = $this->data;
if($self->valuersmanagement($request)) {
$this->json(true);
}
});
}
public function valuersmanagement($request)
{
$app = App::i();
if ($file = $app->repo("File")->find($request['file'])) {
$spreadsheet = IOFactory::load($file->getPath());
$data = $this->getSpreadsheetData($spreadsheet);
$this->buildList($data, $file->owner);
$file = $app->repo("File")->find($request['file']);
$file->delete(true);
}
return true;
}
function getNumber($item) {
$k = null;
foreach($item as $key => $value) {
if(in_array(mb_strtolower($key), ['inscrição', 'inscricao', 'number', 'número'])) {
$k = $key;
break;
}
}
return $item[$k];
}
function getAgent($item) {
$k = null;
foreach($item as $key => $value) {
if(in_array(mb_strtolower($key), ['agente', 'id do agente', 'id do avaliador'])) {
$k = $key;
break;
}
}
return $item[$k];
}
public function buildList($values, Opportunity $opportunity) {
$app = App::i();
$data = [];
foreach($values as $item) {
$number = $this->getNumber($item);
$data[$number] = $data[$number] ?? [];
$data[$number][] = $this->getAgent($item);
}
$_eval_users_id = [];
$n_total = count($data);
$n = 0;
foreach($data as $number => $valuers) {
$n++;
/** @var Registration $registration */
$registration = $app->repo('Registration')->findOneBy(['opportunity' => $opportunity, 'number' => $number]);
$ids = implode(', ', $valuers);
$conn = $app->em->getConnection();
$users = $conn->fetchFirstColumn("SELECT user_id FROM agent WHERE id in ($ids)");
foreach($users as $usr){
$_eval_users_id[] = $usr;
}
$registration->__skipQueuingPCacheRecreation = true;
$registration->valuersExcludeList = [];
$registration->valuersIncludeList = array_map(function($item) { return "$item"; }, $users);
$registration->save(true);
$app->log->debug("({$n}/{$n_total}) Definindo avaliadores para a inscrição $number: $ids");
$app->em->clear();
}
$_eval_users_id = array_unique($_eval_users_id);
$users = $app->repo('User')->findBy(['id' => $_eval_users_id]);
$opportunity->enqueueToPCacheRecreation($users);
}
public function getSpreadsheetData($spreadsheet)
{
$worksheet = $spreadsheet->getActiveSheet();
$header = [];
$firstRow = true;
foreach ($worksheet->getRowIterator() as $row) {
if ($firstRow) {
$header = $this->getSpreadsheetHeader($row);
$firstRow = false;
continue;
}
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$rowData = [];
$columnIndex = 0;
foreach ($cellIterator as $cell) {
$headerValue = $header[$columnIndex];
$cellValue = $cell->getValue();
if($cellValue){
$rowData[$headerValue] = $cellValue;
}
$columnIndex++;
}
if($rowData){
$data[] = $rowData;
}
}
return $data;
}
public function getSpreadsheetHeader($row)
{
$header = [];
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
$header[] = $cell->getValue();
}
return $header;
}
public function register()
{
$app = App::i();
$file_group_definition = new FileGroup('evalmaster', ['^text/csv$', '^application/vnd.ms-excel$', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 'O arquivo enviado não é válido.', true, null, true);
$app->registerFileGroup('opportunity', $file_group_definition);
}
}