Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
conference.php
1<?php
2
3
5
12
14{
15 public const MEETING_SCHEDULED_TYPE = 2;
16 public const ACTIVITY_ENTITY_TYPE = 'activity';
17 public const ZOOM_AUDIO_ONLY = 'audio_only';
18
19 public static function isAvailable(): bool
20 {
21 if (!Loader::includeModule('bitrix24'))
22 {
23 return false;
24 }
25
26 $authManager = new \CSocServAuthManager();
27 if (!$authManager->isActiveAuthService('zoom'))
28 {
29 return false;
30 }
31
32 return \Bitrix\Bitrix24\Feature::isFeatureEnabled("crm_zoom_integration");
33 }
34
35 public static function create($userId, $data): Result
36 {
37 $result = new Result();
38 $preparedDataResult = self::prepareConferenceParams($data);
39 if (!$preparedDataResult->isSuccess())
40 {
41 return $result->addErrors($preparedDataResult->getErrors());
42 }
43
44 $preparedData = $preparedDataResult->getData();
45 $zoomOAuth = new \CSocServZoom($userId);
46 $createResult = $zoomOAuth->createConference($preparedData);
47 if(!$createResult->isSuccess())
48 {
49 return $result->addErrors($createResult->getErrors());
50 }
51
52 return $createResult;
53 }
54
55 public static function update(int $userId, array $updateParams): Result
56 {
57 $result = new Result();
58 if (!Loader::includeModule('socialservices'))
59 {
60 return $result->addError(new Error('Socialservices module is not installed'));
61 }
62
63 $zoomOAuth = new \CSocServZoom($userId);
64 $updateResult = $zoomOAuth->updateConference($updateParams);
65
66 if (!$updateResult->isSuccess())
67 {
68 return $result->addErrors($updateResult->getErrors());
69 }
70 $conferenceData = $updateResult->getData();
71
72 return $result->setData($conferenceData);
73 }
74
75 public static function delete(int $conferenceId): Result
76 {
77 $result = new Result();
78
79 if ($conferenceId <= 0)
80 {
81 return $result->addError(new Error('Incorrect conference id'));
82 }
83
84 $deleteResult = ZoomMeetingTable::delete($conferenceId);
85 if (!$deleteResult->isSuccess())
86 {
87 return $result->addErrors($deleteResult->getErrors());
88 }
89
90 $deleteRecordingsResult = Recording::delete($conferenceId);
91 if (!$deleteRecordingsResult->isSuccess())
92 {
93 return $result->addErrors($deleteRecordingsResult->getErrors());
94 }
95
96 return $result;
97 }
98
99 private static function prepareConferenceParams(array $data): Result
100 {
101 $result = new Result();
102
103 if (empty($data['conferenceTitle']))
104 {
105 $result->addError(new Error('Invalid entity type'));
106 }
107
108 $timestampStart = $data['timestampStart'] / 1000;
109 if ($timestampStart < time())
110 {
111 $result->addError(new Error('Invalid date'));
112 }
113
114 $data['duration'] = (int)$data['duration'];
115 if ($data['duration'] <= 0)
116 {
117 $result->addError(new Error('Invalid duration'));
118 }
119
120 if (!$result->isSuccess())
121 {
122 return $result;
123 }
124
125 $dateTimeStart = \Bitrix\Main\Type\DateTime::createFromTimestamp($timestampStart);
126 $data['start_time'] = $dateTimeStart->setTimeZone(new \DateTimeZone('UTC'))->format(DATE_ATOM);
127
128 if ($data['durationType'] === 'h')
129 {
130 $data['duration'] *= 60;
131 }
132
133 $randomSequence = new \Bitrix\Main\Type\RandomSequence($data['conferenceTitle'].$data['start_time']);
134 $password = $randomSequence->randString(10);
135
136 $result->setData([
137 'topic' => $data['conferenceTitle'],
138 'type' => self::MEETING_SCHEDULED_TYPE,
139 'start_time' => $data['start_time'],
140 'duration' => $data['duration'],
141 'password' => $password,
142 'timezone' => 'UTC',
143 ]);
144
145 return $result;
146 }
147
148 public static function getInfo($conferenceId): Result
149 {
150 $result = new Result();
151
152 $conferenceRecord = ZoomMeetingTable::getRowByExternalId($conferenceId);
153 if (!empty($conferenceRecord))
154 {
155 $result->setData($conferenceRecord);
156 }
157 else
158 {
159 $result->addError(new Error('No conference data'));
160 }
161
162 return $result;
163 }
164
165 public static function setJoin(int $conferenceId): Result
166 {
167 $result = new Result();
168
169 $params = [
170 'JOINED' => true,
171 ];
172
173 $getListResult = ZoomMeetingTable::getList([
174 'filter' => [
175 '=CONFERENCE_EXTERNAL_ID' => $conferenceId,
176 '!=JOINED' => 'Y',
177 ],
178 'select' => ['ENTITY_ID','ENTITY_TYPE_ID','ID']
179 ]);
180
181 if ($meeting = $getListResult->fetch())
182 {
183 $updateResult = ZoomMeetingTable::update($meeting['ID'], $params);
184 if (!$updateResult->isSuccess())
185 {
186 $result->addError(new Error('Error while update join status.'));
187 }
188 if ($updateResult->isSuccess() && $updateResult->getAffectedRowsCount() === 0)
189 {
190 $result->addError(new Error('Error: status has already been updated.'));
191 }
192
193 $result->setData($meeting);
194 }
195 else
196 {
197 $result->addError(new Error('No conference to update'));
198 }
199
200
201 return $result;
202 }
203
204 public static function setEnd(int $conferenceId): Result
205 {
206 $result = new Result();
207
208 $params = [
209 'CONFERENCE_ENDED' => (new DateTime()),
210 ];
211
212 $meeting = ZoomMeetingTable::getRowByExternalId($conferenceId);
213 if (!$meeting)
214 {
215 return $result->addError(new Error("Meeting {$conferenceId} is not found"));
216 }
217 $updateResult = ZoomMeetingTable::update($meeting['ID'], $params);
218 if (!$updateResult->isSuccess())
219 {
220 $result->addError(new Error('Error while update end status.'));
221 }
222 else
223 {
224 $result->setData($meeting);
225 }
226
227 return $result;
228 }
229
230
231 public static function bindActivity(array $conferenceData, int $activityId): Result
232 {
233 $result = new Result();
234 if (!Loader::includeModule('socialservices'))
235 {
236 return $result->addError(new Error('Module socialservices is not installed.'));
237 }
238
239 $conferenceInfo = self::getInfo($conferenceData['id']);
240 if ($conferenceInfo->isSuccess())
241 {
242 $conference = $conferenceInfo->getData();
243 $params = [
244 'ENTITY_TYPE_ID' => self::ACTIVITY_ENTITY_TYPE,
245 'ENTITY_ID' => $activityId,
246 ];
247
248 $result = ZoomMeetingTable::update($conference['ID'], $params);
249 if (!$result->isSuccess())
250 {
251 $result->addError(new Error('Error while saving new zoom conference.'));
252 }
253 }
254 else
255 {
256 $result->addError(new Error('Could not get conference info'));
257 }
258
259 return $result;
260 }
261
269 public static function saveRecordings(int $conferenceId, array $recordingsData, $downloadToken): Result
270 {
271 $result = new Result();
272 if (!Loader::includeModule('socialservices'))
273 {
274 return $result->addError(new Error('Module socialservices is not installed.'));
275 }
276
277 if (!is_array($recordingsData['recording_files']))
278 {
279 return $result->addError(new Error('Error: recording_files key is not found in recording data'));
280 }
281
282 $meeting = ZoomMeetingTable::getRowByExternalId($conferenceId);
283 if (!$meeting)
284 {
285 return $result->addError(new Error("Meeting {$conferenceId} is not found"));
286 }
287 $meetingId = $meeting['ID'];
288 if ($meeting['HAS_RECORDING'] !== 'Y')
289 {
290 ZoomMeetingTable::update($meetingId, [
291 'HAS_RECODING' => 'Y'
292 ]);
293 }
294 $crmInstalled = Loader::includeModule('crm');
295
296 foreach ($recordingsData['recording_files'] as $record)
297 {
298 $startDateTimestamp = \DateTime::createFromFormat(DATE_ATOM, $record['recording_start'])->getTimestamp();
299 $endDateTimestamp = \DateTime::createFromFormat(DATE_ATOM, $record['recording_end'])->getTimestamp();
300
301 $recordFields = [
302 'EXTERNAL_ID' => $record['id'],
303 'MEETING_ID' => $meetingId, //?
304 'START_DATE' => DateTime::createFromTimestamp($startDateTimestamp),
305 'END_DATE' => DateTime::createFromTimestamp($endDateTimestamp),
306 'FILE_TYPE' => $record['file_type'],
307 'FILE_SIZE' => (int)$record['file_size'],
308 'PLAY_URL' => $record['play_url'],
309 'DOWNLOAD_URL' => $record['download_url'],
310 'RECORDING_TYPE' => $record['recording_type'],
311 'DOWNLOAD_TOKEN' => $downloadToken,
312 'PASSWORD' => $recordingsData['password']
313 ];
314
315 $addResult = ZoomMeetingRecordingTable::add($recordFields);
316 if (!$addResult->isSuccess())
317 {
318 return $result->addErrors($addResult->getErrors());
319 }
320 $recordingId = $addResult->getId();
321 if($crmInstalled && $meeting['ENTITY_TYPE_ID'] === static::ACTIVITY_ENTITY_TYPE && $meeting['ENTITY_ID'] > 0)
322 {
323 $activityId = (int)$meeting['ENTITY_ID'];
324 if ($record['recording_type'] === static::ZOOM_AUDIO_ONLY)
325 {
326 DownloadAgent::scheduleDownload($activityId, $recordingId);
327 }
328 }
329 }
330
331 return $result;
332 }
333}
static createFromTimestamp($timestamp)
Definition datetime.php:246
static saveRecordings(int $conferenceId, array $recordingsData, $downloadToken)
static update(int $userId, array $updateParams)
static bindActivity(array $conferenceData, int $activityId)