WhispeakAuthRequest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. /**
  5. * Class WhispeakRequest.
  6. */
  7. class WhispeakAuthRequest
  8. {
  9. const API_URL = 'http://api.whispeak.io:8080/v1.1/';
  10. /**
  11. * @param WhispeakAuthPlugin $plugin
  12. *
  13. * @throws Exception
  14. *
  15. * @return string
  16. */
  17. public static function activityId(WhispeakAuthPlugin $plugin)
  18. {
  19. $headers = [
  20. "Authorization: Bearer {$plugin->getAccessToken()}",
  21. ];
  22. $result = self::doGet('activityid', $headers);
  23. if (empty($result['activity_id'])) {
  24. throw new Exception(get_lang('BadFormData'));
  25. }
  26. return $result['activity_id'];
  27. }
  28. /**
  29. * @param WhispeakAuthPlugin $plugin
  30. *
  31. * @throws Exception
  32. *
  33. * @return string
  34. */
  35. public static function whispeakId(WhispeakAuthPlugin $plugin)
  36. {
  37. $headers = [
  38. "Authorization: Bearer {$plugin->getAccessToken()}",
  39. ];
  40. $result = self::doGet('whispeakid', $headers);
  41. if (empty($result['wsid'])) {
  42. throw new Exception(get_lang('BadFormData'));
  43. }
  44. return $result['wsid'];
  45. }
  46. /**
  47. * @param WhispeakAuthPlugin $plugin Plugin instance.
  48. * @param string $wsId User's Whispeak ID.
  49. * @param bool $grantAurp Whether user Allow to Use for Research Purpose.
  50. *
  51. * @throws Exception
  52. *
  53. * @return string
  54. */
  55. public static function license(WhispeakAuthPlugin $plugin, $wsId, $grantAurp)
  56. {
  57. $headers = [
  58. 'Content-Type: application/json',
  59. "Authorization: Bearer ".$plugin->getAccessToken(),
  60. ];
  61. $body = [
  62. 'wsid' => $wsId,
  63. 'GTU' => 'lorem ipsum',
  64. 'AURP' => $grantAurp,
  65. ];
  66. $result = self::doPost('license', $headers, json_encode($body));
  67. if (empty($result['wsid'])) {
  68. throw new Exception(get_lang('BadFormData'));
  69. }
  70. return $result['wsid'];
  71. }
  72. /**
  73. * @param WhispeakAuthPlugin $plugin
  74. *
  75. * @throws Exception
  76. *
  77. * @return string
  78. */
  79. public static function enrollmentSentence(WhispeakAuthPlugin $plugin)
  80. {
  81. $headers = [
  82. "Authorization: Bearer ".$plugin->getAccessToken(),
  83. ];
  84. $result = self::doGet('enrollmentsentence', $headers);
  85. if (empty($result['text'])) {
  86. throw new Exception(get_lang('BadFormData'));
  87. }
  88. return $result['text'];
  89. }
  90. /**
  91. * @param WhispeakAuthPlugin $plugin
  92. * @param User $user
  93. * @param string $wsId
  94. * @param string $text Sentence text used to create the voice file.
  95. * @param string $filePath
  96. *
  97. * @throws Exception
  98. *
  99. * @return array
  100. */
  101. public static function enrollment(WhispeakAuthPlugin $plugin, User $user, $wsId, $text, $filePath)
  102. {
  103. $headers = [
  104. "Authorization: Bearer ".$plugin->getAccessToken(),
  105. ];
  106. $body = [
  107. 'wsid' => $wsId,
  108. 'audioType' => 'pcm',
  109. 'spokenTongue' => WhispeakAuthPlugin::getLanguageIsoCode($user->getLanguage()),
  110. 'text' => $text,
  111. 'voice' => new CURLFile($filePath),
  112. ];
  113. $result = self::doPost('enrollment', $headers, $body);
  114. if (empty($result)) {
  115. throw new Exception(get_lang('BadFormData'));
  116. }
  117. return $result;
  118. }
  119. /**
  120. * @param WhispeakAuthPlugin $plugin
  121. *
  122. * @throws Exception
  123. *
  124. * @return string
  125. */
  126. public static function authenticateSentence(WhispeakAuthPlugin $plugin)
  127. {
  128. $headers = [
  129. "Authorization: Bearer ".$plugin->getAccessToken(),
  130. ];
  131. $result = self::doGet('authenticatesentence', $headers);
  132. if (empty($result['text'])) {
  133. throw new Exception(get_lang('BadFormData'));
  134. }
  135. return $result['text'];
  136. }
  137. /**
  138. * @param WhispeakAuthPlugin $plugin
  139. * @param string $wsId
  140. * @param string $text Sentence text used to create the voice file.
  141. * @param string $filePath
  142. *
  143. * @throws Exception
  144. *
  145. * @return array
  146. */
  147. public static function authentify(WhispeakAuthPlugin $plugin, $wsId, $text, $filePath)
  148. {
  149. $headers = [
  150. "Authorization: Bearer ".$plugin->getAccessToken(),
  151. ];
  152. if (empty($text)) {
  153. $text = '';
  154. }
  155. $body = [
  156. 'wsid' => $wsId,
  157. 'activityId' => self::activityId($plugin),
  158. 'audioType' => 'pcm',
  159. 'text' => $text,
  160. 'voice' => new CURLFile($filePath),
  161. ];
  162. $result = self::doPost('authentify', $headers, $body);
  163. if (empty($result)) {
  164. throw new Exception(get_lang('BadFormData'));
  165. }
  166. return $result;
  167. }
  168. /**
  169. * @param WhispeakAuthPlugin $plugin
  170. * @param array $wsIds
  171. * @param array $activityIds
  172. * @param DateTime $date
  173. *
  174. * @throws Exception
  175. *
  176. * @return array
  177. */
  178. public static function getUsersInfos(
  179. WhispeakAuthPlugin $plugin,
  180. array $wsIds,
  181. array $activityIds = [],
  182. DateTime $date = null
  183. ) {
  184. $headers = [
  185. 'Content-Type: application/json',
  186. "Authorization: Bearer ".$plugin->getAccessToken(),
  187. ];
  188. $metadata = ['wsids' => $wsIds];
  189. if ($activityIds) {
  190. $metadata['activityids'] = $activityIds;
  191. }
  192. if ($date) {
  193. $metadata['date'] = [
  194. 'year' => (int) $date->format('Y'),
  195. 'month' => (int) $date->format('m'),
  196. 'day' => (int) $date->format('d'),
  197. ];
  198. }
  199. $result = self::doPost('getusersinfos', $headers, json_encode($metadata));
  200. return $result;
  201. }
  202. /**
  203. * @param string $uri
  204. * @param array $headers
  205. * @param array|string $body
  206. *
  207. * @throws Exception
  208. *
  209. * @return array
  210. */
  211. private static function doPost($uri, array $headers = [], $body = null)
  212. {
  213. $ch = curl_init(self::API_URL.$uri);
  214. if ($headers) {
  215. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  216. }
  217. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  218. curl_setopt($ch, CURLOPT_POST, true);
  219. if ($body) {
  220. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  221. }
  222. $result = curl_exec($ch);
  223. $error = curl_error($ch);
  224. curl_close($ch);
  225. if (!empty($error)) {
  226. throw new Exception($error);
  227. }
  228. $result = json_decode($result, true);
  229. if (!empty($result['error'])) {
  230. throw new Exception($result['error']);
  231. }
  232. return $result;
  233. }
  234. /**
  235. * @param string $uri
  236. * @param array $headers
  237. *
  238. * @throws Exception
  239. *
  240. * @return array
  241. */
  242. private static function doGet($uri, array $headers = [])
  243. {
  244. $ch = curl_init(self::API_URL.$uri);
  245. if ($headers) {
  246. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  247. }
  248. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  249. $result = curl_exec($ch);
  250. $error = curl_error($ch);
  251. curl_close($ch);
  252. if (!empty($error)) {
  253. throw new Exception($error);
  254. }
  255. $result = json_decode($result, true);
  256. if (!empty($result['error'])) {
  257. throw new Exception($result['error']);
  258. }
  259. return $result;
  260. }
  261. }