diff --git a/main/lp/lp_add_audio.php b/main/lp/lp_add_audio.php
index 2d2c7b37827..ba58d7f73b5 100755
--- a/main/lp/lp_add_audio.php
+++ b/main/lp/lp_add_audio.php
@@ -15,9 +15,9 @@
$isStudentView = api_is_student_view_active();
$learnpath_id = (int) $_REQUEST['lp_id'];
$lp_item_id = isset($_GET['id']) ? (int) $_GET['id'] : null;
-$submit = isset($_POST['submit_button']) ? $_POST['submit_button'] : null;
-$type = isset($_GET['type']) ? $_GET['type'] : null;
-$action = isset($_GET['action']) ? $_GET['action'] : null;
+$submit = $_POST['submit_button'] ?? null;
+$type = $_GET['type'] ?? null;
+$action = $_GET['action'] ?? null;
$courseInfo = api_get_course_info();
if (!$is_allowed_to_edit || $isStudentView) {
@@ -48,7 +48,7 @@
'name' => $lp->getNameNoTags(),
];
-$audioPreview = DocumentManager::generateAudioJavascript([]);
+$audioPreview = DocumentManager::generateAudioJavascript();
$htmlHeadXtra[] = "';
-$htmlHeadXtra[] = '';
-$htmlHeadXtra[] = '';
-$htmlHeadXtra[] = '';
+$htmlHeadXtra[] = '';
+$htmlHeadXtra[] = '';
+$htmlHeadXtra[] = '';
$tpl = new Template(get_lang('Add'));
$tpl->assign('unique_file_id', api_get_unique_id());
@@ -161,7 +166,14 @@
Display::getMediaPlayer($file, ['url' => $urlFile]).
"";
$form->addElement('label', get_lang('Listen'), $audioPlayer);
- $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?lp_id='.$lp->get_id().'&action=add_audio&id='.$lp_item_id.'&delete_file=1&'.api_get_cidreq();
+ $url = $webCodePath.'lp/lp_controller.php?&'
+ .http_build_query([
+ 'lp_id' => $lp->get_id(),
+ 'action' => 'add_audio',
+ 'id' => $lp_item_id,
+ 'delete_file' => 1,
+ ])
+ .'&'.api_get_cidreq();
$form->addElement(
'label',
null,
@@ -184,7 +196,7 @@
api_get_session_id(),
false,
'',
- api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?action=add_audio&lp_id='.$lp->get_id().'&id='.$lp_item_id,
+ $webCodePath.'lp/lp_controller.php?action=add_audio&lp_id='.$lp->get_id().'&id='.$lp_item_id,
false,
true,
$audioFolderId,
@@ -196,6 +208,96 @@
$page .= $recordVoiceForm;
$page .= '
';
$page .= $form->returnForm();
+
+$text2speechPlugin = Text2SpeechPlugin::create();
+
+if ($text2speechPlugin->isEnabled(true)) {
+ $page .= '
+ +
+ + ++ +
+'.$this->get_lang('plugin_comment').'
'; + + $settings = [ + $message => 'html', + 'tool_enable' => 'boolean', + 'api_name' => [ + 'type' => 'select', + 'options' => $this->getApiList(), + ], + 'api_key' => 'text', + 'url' => 'text', + 'tool_lp_enable' => 'boolean', + ]; + + parent::__construct($version, $author, $settings); + } + + /** + * Get the list of apis availables. + * + * @return array + */ + public function getApiList() + { + return [ + self::MOZILLATTS_API => 'MozillaTTS', + ]; + } + + /** + * Get the completion text from openai. + * + * @return string + */ + public function convert(string $text) + { + $path = '/app/upload/plugins/text2speech/'; + switch ($this->get('api_name')) { + case self::MOZILLATTS_API: + require_once __DIR__.'/src/mozillatts/MozillaTTS.php'; + + $mozillaTTS = new MozillaTTS($this->get('url'), $this->get('api_key'), self::PATH_TO_SAVE_FILES); + $path .= $mozillaTTS->convert($text); + break; + } + + return $path; + } + + /** + * Get the plugin directory name. + */ + public function get_name(): string + { + return 'text2speech'; + } + + /** + * Get the class instance. + * + * @staticvar Text2SpeechPlugin $result + */ + public static function create(): Text2SpeechPlugin + { + static $result = null; + + return $result ?: $result = new self(); + } + + /** + * Install the plugin. create folder to save files. + */ + public function install() + { + if (!file_exists(self::PATH_TO_SAVE_FILES)) { + mkdir(self::PATH_TO_SAVE_FILES); + } + } + + /** + * Unistall plugin. Clear the folder. + */ + public function uninstall() + { + if (file_exists(self::PATH_TO_SAVE_FILES)) { + array_map('unlink', glob(self::PATH_TO_SAVE_FILES.'/*.*')); + rmdir(self::PATH_TO_SAVE_FILES); + } + } +} diff --git a/plugin/text2speech/convert.php b/plugin/text2speech/convert.php new file mode 100644 index 00000000000..44179d67934 --- /dev/null +++ b/plugin/text2speech/convert.php @@ -0,0 +1,70 @@ +isEnabled(true) + || !$isAllowedToEdit + ) { + throw new Exception(); + } + + $textToConvert = ''; + + if ($httpRequest->query->has('text')) { + $textToConvert = $httpRequest->query->get('text'); + } elseif ($httpRequest->query->has('item_id')) { + $itemId = $httpRequest->query->getInt('item_id'); + + $item = $em->find(CLpItem::class, $itemId); + + if (!$item) { + throw new Exception(); + } + + $course = api_get_course_entity($item->getCId()); + $documentRepo = $em->getRepository(CDocument::class); + + $document = $documentRepo->findOneBy([ + 'cId' => $course->getId(), + 'iid' => $item->getPath(), + ]); + + if (!$document) { + throw new Exception(); + } + + $textToConvert = file_get_contents( + api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/document/'.$document->getPath() + ); + $textToConvert = strip_tags($textToConvert); + } + + if (empty($textToConvert)) { + throw new Exception(); + } + + $path = $plugin->convert($textToConvert); + + $httpResponse->setContent($path); +} catch (Exception $exception) { + $httpResponse->setStatusCode(HttpResponse::HTTP_BAD_REQUEST); +} + +$httpResponse->send(); diff --git a/plugin/text2speech/install.php b/plugin/text2speech/install.php new file mode 100755 index 00000000000..408e9f86eb2 --- /dev/null +++ b/plugin/text2speech/install.php @@ -0,0 +1,16 @@ +install(); diff --git a/plugin/text2speech/lang/english.php b/plugin/text2speech/lang/english.php new file mode 100755 index 00000000000..81ac14e3bcf --- /dev/null +++ b/plugin/text2speech/lang/english.php @@ -0,0 +1,13 @@ +get_info(); diff --git a/plugin/text2speech/src/IProvider.php b/plugin/text2speech/src/IProvider.php new file mode 100644 index 00000000000..7312fbbe288 --- /dev/null +++ b/plugin/text2speech/src/IProvider.php @@ -0,0 +1,8 @@ +url = $url; + $this->apiKey = $apiKey; + $this->filePath = $filePath; + } + + public function convert(string $text): string + { + return $this->request($text); + } + + private function request(string $data): string + { + $filename = uniqid().'.wav'; + $filePath = $this->filePath.$filename; +// $resource = fopen(realpath($filePath), 'w'); + + $client = new GuzzleHttp\Client(); + $client->get($this->url.'?api_key='.urlencode($this->apiKey). + '&text='.str_replace('%0A', '+', urlencode($data)), [ + 'headers' => [ + 'Cache-Control' => 'no-cache', + 'Content-Type' => 'audio/wav', + ], + 'sink' => $filePath, + ]); + + return $filename; + } +} diff --git a/plugin/text2speech/uninstall.php b/plugin/text2speech/uninstall.php new file mode 100755 index 00000000000..df666a1d5dc --- /dev/null +++ b/plugin/text2speech/uninstall.php @@ -0,0 +1,16 @@ +uninstall();