openmeetings.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * Chamilo-OpenMeetings integration plugin library, defining methods to connect
  4. * to OpenMeetings from Chamilo by calling its web services.
  5. *
  6. * @package chamilo.plugin.openmeetings
  7. */
  8. namespace Chamilo\Plugin\OpenMeetings;
  9. include_once __DIR__.'/session.class.php';
  10. include_once __DIR__.'/room.class.php';
  11. include_once __DIR__.'/user.class.php';
  12. /**
  13. * Open Meetings-Chamilo connector class.
  14. */
  15. class OpenMeetings
  16. {
  17. public $url;
  18. public $user;
  19. public $pass;
  20. public $api;
  21. public $user_complete_name = null;
  22. public $protocol = 'http://';
  23. public $debug = false;
  24. public $logout_url = null;
  25. public $plugin_enabled = false;
  26. public $sessionId = "";
  27. public $roomName = '';
  28. public $chamiloCourseId;
  29. public $chamiloSessionId;
  30. public $externalType;
  31. /**
  32. * Constructor (generates a connection to the API and the Chamilo settings
  33. * required for the connection to the video conference server).
  34. */
  35. public function __construct()
  36. {
  37. global $_configuration;
  38. // initialize video server settings from global settings
  39. $plugin = \OpenMeetingsPlugin::create();
  40. $om_plugin = (bool) $plugin->get('tool_enable');
  41. $om_host = $plugin->get('host');
  42. $om_user = $plugin->get('user');
  43. $om_pass = $plugin->get('pass');
  44. $accessUrl = api_get_access_url($_configuration['access_url']);
  45. $this->externalType = substr($accessUrl['url'], strpos($accessUrl['url'], '://') + 3, -1);
  46. if (strcmp($this->externalType, 'localhost') == 0) {
  47. $this->externalType = substr(api_get_path(WEB_PATH), strpos(api_get_path(WEB_PATH), '://') + 3, -1);
  48. }
  49. $this->externalType = 'chamilolms.'.$this->externalType;
  50. $this->table = \Database::get_main_table('plugin_openmeetings');
  51. if ($om_plugin) {
  52. $user_info = api_get_user_info();
  53. $this->user_complete_name = $user_info['complete_name'];
  54. $this->user = $om_user;
  55. $this->pass = $om_pass;
  56. $this->url = $om_host;
  57. // Setting OM api
  58. define('CONFIG_OPENMEETINGS_USER', $this->user);
  59. define('CONFIG_OPENMEETINGS_PASS', $this->pass);
  60. define('CONFIG_OPENMEETINGS_SERVER_URL', $this->url);
  61. $this->gateway = new \OpenMeetingsGateway($this->url, $this->user, $this->pass);
  62. $this->plugin_enabled = $om_plugin;
  63. // The room has a name composed of C + course ID + '-' + session ID
  64. $this->chamiloCourseId = api_get_course_int_id();
  65. $this->chamiloSessionId = api_get_session_id();
  66. $this->roomName = 'C'.$this->chamiloCourseId.'-'.$this->chamiloSessionId;
  67. $return = $this->gateway->loginUser();
  68. if ($return == 0) {
  69. $msg = 'Could not initiate session with server through OpenMeetingsGateway::loginUser()';
  70. error_log(__FILE__.'+'.__LINE__.': '.$msg);
  71. die($msg);
  72. }
  73. $this->sessionId = $this->gateway->sessionId;
  74. }
  75. }
  76. /**
  77. * Checks whether a user is teacher in the current course.
  78. *
  79. * @return bool True if the user can be considered a teacher in this course, false otherwise
  80. */
  81. public function isTeacher()
  82. {
  83. return api_is_course_admin() || api_is_coach() || api_is_platform_admin();
  84. }
  85. /*
  86. * Creating a Room for the meeting
  87. * @return bool True if the user is correct and false when is incorrect
  88. */
  89. public function createMeeting($params)
  90. {
  91. global $_configuration;
  92. // First, try to see if there is an active room for this course and session.
  93. $roomId = null;
  94. $meetingData = \Database::select(
  95. '*',
  96. $this->table,
  97. [
  98. 'where' => [
  99. 'c_id = ?' => $this->chamiloCourseId,
  100. ' AND session_id = ? ' => $this->chamiloSessionId,
  101. ' AND status <> ? ' => 2,
  102. ],
  103. ],
  104. 'first'
  105. );
  106. if ($meetingData != false && count($meetingData) > 0) {
  107. // There has been a room in the past for this course. It should
  108. // still be on the server, so update (instead of creating a new one)
  109. // This fills the following attributes: status, name, comment, chamiloCourseId, chamiloSessionId
  110. $room = new Room();
  111. $room->loadRoomId($meetingData['room_id']);
  112. $roomArray = (array) $room;
  113. $roomArray['SID'] = $this->sessionId;
  114. $roomId = $this->gateway->updateRoomWithModeration($room);
  115. if ($roomId != $meetingData['room_id']) {
  116. $msg = 'Something went wrong: the updated room ID ('.$roomId.') is not the same as the one we had ('.$meetingData['room_id'].')';
  117. die($msg);
  118. }
  119. } else {
  120. $room = new Room();
  121. $room->SID = $this->sessionId;
  122. $room->name = $this->roomName;
  123. //$room->roomtypes_id = $room->roomtypes_id;
  124. $room->comment = urlencode(get_lang('Course').': '.$params['meeting_name'].' - '.$_configuration['software_name']);
  125. //$room->numberOfPartizipants = $room->numberOfPartizipants;
  126. $room->ispublic = boolval($room->getString('isPublic', 'false'));
  127. //$room->appointment = $room->getString('appointment');
  128. //$room->isDemoRoom = $room->getString('isDemoRoom');
  129. //$room->demoTime = $room->demoTime;
  130. //$room->isModeratedRoom = $room->getString('isModeratedRoom');
  131. $roomId = $this->gateway->createRoomWithModAndType($room);
  132. }
  133. if (!empty($roomId)) {
  134. /*
  135. // Find the biggest room_id so far, and create a new one
  136. if (empty($roomId)) {
  137. $roomData = \Database::select('MAX(room_id) as room_id', $this->table, array(), 'first');
  138. $roomId = $roomData['room_id'] + 1;
  139. }*/
  140. $params['status'] = '1';
  141. $params['meeting_name'] = $room->name;
  142. $params['created_at'] = api_get_utc_datetime();
  143. $params['room_id'] = $roomId;
  144. $params['c_id'] = api_get_course_int_id();
  145. $params['session_id'] = api_get_session_id();
  146. $params['record'] = ($room->allowRecording ? 1 : 0);
  147. $id = \Database::insert($this->table, $params);
  148. $this->joinMeeting($id);
  149. } else {
  150. return -1;
  151. }
  152. }
  153. /**
  154. * Returns a meeting "join" URL.
  155. *
  156. * @param string The name of the meeting (usually the course code)
  157. *
  158. * @return mixed The URL to join the meeting, or false on error
  159. *
  160. * @todo implement moderator pass
  161. * @assert ('') === false
  162. * @assert ('abcdefghijklmnopqrstuvwxyzabcdefghijklmno') === false
  163. */
  164. public function joinMeeting($meetingId)
  165. {
  166. if (empty($meetingId)) {
  167. return false;
  168. }
  169. $meetingData = \Database::select(
  170. '*',
  171. $this->table,
  172. ['where' => ['id = ? AND status = 1 ' => $meetingId]],
  173. 'first'
  174. );
  175. if (empty($meetingData)) {
  176. if ($this->debug) {
  177. error_log("meeting does not exist: $meetingId ");
  178. }
  179. return false;
  180. }
  181. $params = ['room_id' => $meetingData['room_id']];
  182. $returnVal = $this->setUserObjectAndGenerateRoomHashByURLAndRecFlag($params);
  183. $iframe = $this->url."/?"."secureHash=".$returnVal;
  184. printf("<iframe src='%s' width='%s' height = '%s' />", $iframe, "100%", 640);
  185. }
  186. /**
  187. * Checks if the videoconference server is running.
  188. * Function currently disabled (always returns 1).
  189. *
  190. * @return bool True if server is running, false otherwise
  191. * @assert () === false
  192. */
  193. public function isServerRunning()
  194. {
  195. // Always return true for now as this requires the openmeetings object
  196. // to have been instanciated and this includes a loginUser() which
  197. // connects to the server
  198. return true;
  199. }
  200. /**
  201. * Gets the password for a specific meeting for the current user.
  202. *
  203. * @return string A moderator password if user is teacher, or the course code otherwise
  204. */
  205. public function getMeetingUserPassword()
  206. {
  207. if ($this->isTeacher()) {
  208. return $this->getMeetingModerationPassword();
  209. } else {
  210. return api_get_course_id();
  211. }
  212. }
  213. /**
  214. * Generated a moderator password for the meeting.
  215. *
  216. * @return string A password for the moderation of the video conference
  217. */
  218. public function getMeetingModerationPassword()
  219. {
  220. return api_get_course_id().'mod';
  221. }
  222. /**
  223. * Get information about the given meeting.
  224. *
  225. * @param array ...?
  226. *
  227. * @return mixed Array of information on success, false on error
  228. * @assert (array()) === false
  229. */
  230. public function getMeetingInfo($params)
  231. {
  232. try {
  233. $result = $this->api->getMeetingInfoArray($params);
  234. if ($result == null) {
  235. if ($this->debug) {
  236. error_log(__FILE__.'+'.__LINE__." Failed to get any response. Maybe we can't contact the OpenMeetings server.");
  237. }
  238. } else {
  239. return $result;
  240. }
  241. } catch (Exception $e) {
  242. if ($this->debug) {
  243. error_log(__FILE__.'+'.__LINE__.' Caught exception: ', $e->getMessage(), "\n");
  244. }
  245. }
  246. return false;
  247. }
  248. /**
  249. * @param array $params Array of parameters
  250. *
  251. * @return mixed
  252. */
  253. public function setUserObjectAndGenerateRecordingHashByURL($params)
  254. {
  255. $username = $_SESSION['_user']['username'];
  256. $firstname = $_SESSION['_user']['firstname'];
  257. $lastname = $_SESSION['_user']['lastname'];
  258. $userId = $_SESSION['_user']['user_id'];
  259. $systemType = 'chamilo';
  260. $room_id = $params['room_id'];
  261. $urlWsdl = $this->url."/services/UserService?wsdl";
  262. $omServices = new \SoapClient($urlWsdl);
  263. $objRec = new User();
  264. $objRec->SID = $this->sessionId;
  265. $objRec->username = $username;
  266. $objRec->firstname = $firstname;
  267. $objRec->lastname = $lastname;
  268. $objRec->externalUserId = $userId;
  269. $objRec->externalUserType = $systemType;
  270. $objRec->recording_id = $recording_id;
  271. $orFn = $omServices->setUserObjectAndGenerateRecordingHashByURL($objRec);
  272. return $orFn->return;
  273. }
  274. /**
  275. * @param array $params Array of parameters
  276. *
  277. * @return mixed
  278. */
  279. public function setUserObjectAndGenerateRoomHashByURLAndRecFlag($params)
  280. {
  281. $username = $_SESSION['_user']['username'];
  282. $firstname = $_SESSION['_user']['firstname'];
  283. $lastname = $_SESSION['_user']['lastname'];
  284. $profilePictureUrl = $_SESSION['_user']['avatar'];
  285. $email = $_SESSION['_user']['mail'];
  286. $userId = $_SESSION['_user']['user_id'];
  287. $systemType = 'Chamilo';
  288. $room_id = $params['room_id'];
  289. $becomeModerator = ($this->isTeacher() ? 1 : 0);
  290. $allowRecording = 1; //Provisional
  291. $urlWsdl = $this->url."/services/UserService?wsdl";
  292. $omServices = new \SoapClient($urlWsdl);
  293. $objRec = new User();
  294. $objRec->SID = $this->sessionId;
  295. $objRec->username = $username;
  296. $objRec->firstname = $firstname;
  297. $objRec->lastname = $lastname;
  298. $objRec->profilePictureUrl = $profilePictureUrl;
  299. $objRec->email = $email;
  300. $objRec->externalUserId = $userId;
  301. $objRec->externalUserType = $systemType;
  302. $objRec->room_id = $room_id;
  303. $objRec->becomeModeratorAsInt = $becomeModerator;
  304. $objRec->showAudioVideoTestAsInt = 1;
  305. $objRec->allowRecording = $allowRecording;
  306. $rcFn = $omServices->setUserObjectAndGenerateRoomHashByURLAndRecFlag($objRec);
  307. return $rcFn->return;
  308. }
  309. /**
  310. * Gets all the course meetings saved in the plugin_openmeetings table.
  311. *
  312. * @return array Array of current open meeting rooms
  313. */
  314. public function getCourseMeetings()
  315. {
  316. $newMeetingsList = [];
  317. $item = [];
  318. $meetingsList = \Database::select(
  319. '*',
  320. $this->table,
  321. ['where' => [
  322. 'c_id = ? ' => api_get_course_int_id(),
  323. ' AND session_id = ? ' => api_get_session_id(),
  324. ' AND status <> ? ' => 2, // status deleted
  325. ],
  326. ]
  327. );
  328. $room = new Room();
  329. $room->SID = $this->sessionId;
  330. if (!empty($meetingsList)) {
  331. foreach ($meetingsList as $meetingDb) {
  332. //$room->rooms_id = $meetingDb['room_id'];
  333. error_log(__FILE__.'+'.__LINE__.' Meetings found: '.print_r($meetingDb, 1));
  334. $remoteMeeting = [];
  335. $meetingDb['created_at'] = api_get_local_time($meetingDb['created_at']);
  336. $meetingDb['closed_at'] = (!empty($meetingDb['closed_at']) ? api_get_local_time($meetingDb['closed_at']) : '');
  337. // Fixed value for now
  338. $meetingDb['participantCount'] = 40;
  339. $rec = $this->gateway->getFlvRecordingByRoomId($meetingDb['room_id']);
  340. $links = [];
  341. // Links to videos look like these:
  342. // http://video2.openmeetings.com:5080/openmeetings/DownloadHandler?fileName=flvRecording_4.avi&moduleName=lzRecorderApp&parentPath=&room_id=&sid=dfc0cac396d384f59242aa66e5a9bbdd
  343. $link = $this->url.'/DownloadHandler?fileName=%s&moduleName=lzRecorderApp&parentPath=&room_id=%s&sid=%s';
  344. if (!empty($rec)) {
  345. $link1 = sprintf($link, $rec['fileHash'], $meetingDb['room_id'], $this->sessionId);
  346. $link2 = sprintf($link, $rec['alternateDownload'], $meetingDb['room_id'], $this->sessionId);
  347. $links[] = $rec['fileName'].' '.
  348. \Display::url('[.flv]', $link1, ['target' => '_blank']).' '.
  349. \Display::url('[.avi]', $link2, ['target' => '_blank']);
  350. }
  351. $item['show_links'] = implode('<br />', $links);
  352. // The following code is currently commented because the web service
  353. // says this is not allowed by the SOAP user.
  354. /*
  355. try {
  356. // Get the conference room object from OpenMeetings server - requires SID and rooms_id to be defined
  357. $objRoomId = $this->gateway->getRoomById($meetingDb['room_id']);
  358. if (empty($objRoomId->return)) {
  359. error_log(__FILE__.'+'.__LINE__.' Emptyyyyy ');
  360. //\Database::delete($this->table, "id = {$meetingDb['id']}");
  361. // Don't delete expired rooms, just mark as closed
  362. \Database::update($this->table, array('status' => 0, 'closed_at' => api_get_utc_datetime()), array('id = ? ' => $meetingDb['id']));
  363. continue;
  364. }
  365. //$objCurUs = $omServices->getRoomWithCurrentUsersById($objCurrentUsers);
  366. } catch (SoapFault $e) {
  367. error_log(__FILE__.'+'.__LINE__.' '.$e->faultstring);
  368. exit;
  369. }
  370. //if( empty($objCurUs->returnMeetingID) ) continue;
  371. $current_room = array(
  372. 'roomtype' => $objRoomId->return->roomtype->roomtypes_id,
  373. 'meetingName' => $objRoomId->return->name,
  374. 'meetingId' => $objRoomId->return->meetingID,
  375. 'createTime' => $objRoomId->return->rooms_id,
  376. 'showMicrophoneStatus' => $objRoomId->return->showMicrophoneStatus,
  377. 'attendeePw' => $objRoomId->return->attendeePW,
  378. 'moderatorPw' => $objRoomId->return->moderators,
  379. 'isClosed' => $objRoomId->return->isClosed,
  380. 'allowRecording' => $objRoomId->return->allowRecording,
  381. 'startTime' => $objRoomId->return->startTime,
  382. 'endTime' => $objRoomId->return->updatetime,
  383. 'participantCount' => count($objRoomId->return->currentusers),
  384. 'maxUsers' => $objRoomId->return->numberOfPartizipants,
  385. 'moderatorCount' => count($objRoomId->return->moderators)
  386. );
  387. // Then interate through attendee results and return them as part of the array:
  388. if (!empty($objRoomId->return->currentusers)) {
  389. foreach ($objRoomId->return->currentusers as $a)
  390. $current_room[] = array(
  391. 'userId' => $a->username,
  392. 'fullName' => $a->firstname . " " . $a->lastname,
  393. 'isMod' => $a->isMod
  394. );
  395. }
  396. $remoteMeeting = $current_room;
  397. */
  398. if (empty($remoteMeeting)) {
  399. /*
  400. error_log(__FILE__.'+'.__LINE__.' Empty remote Meeting for now');
  401. if ($meetingDb['status'] == 1 && $this->isTeacher()) {
  402. $this->endMeeting($meetingDb['id']);
  403. }
  404. */
  405. } else {
  406. $remoteMeeting['add_to_calendar_url'] = api_get_self().'?action=add_to_calendar&id='.$meetingDb['id'].'&start='.api_strtotime($meetingDb['startTime']);
  407. }
  408. $remoteMeeting['end_url'] = api_get_self().'?action=end&id='.$meetingDb['id'];
  409. $remoteMeeting['delete_url'] = api_get_self().'?action=delete&id='.$meetingDb['id'];
  410. //$record_array = array();
  411. // if ($meetingDb['record'] == 1) {
  412. // $recordingParams = array(
  413. // 'meetingId' => $meetingDb['id'], //-- OPTIONAL - comma separate if multiple ids
  414. // );
  415. //
  416. // $records = $this->api->getRecordingsWithXmlResponseArray($recordingParams);
  417. // if (!empty($records)) {
  418. // $count = 1;
  419. // if (isset($records['message']) && !empty($records['message'])) {
  420. // if ($records['messageKey'] == 'noRecordings') {
  421. // $record_array[] = get_lang('NoRecording');
  422. // } else {
  423. // //$record_array[] = $records['message'];
  424. // }
  425. // } else {
  426. // foreach ($records as $record) {
  427. // if (is_array($record) && isset($record['recordId'])) {
  428. // $url = Display::url(get_lang('ViewRecord'), $record['playbackFormatUrl'], array('target' => '_blank'));
  429. // if ($this->is_teacher()) {
  430. // $url .= Display::url(Display::return_icon('link.gif',get_lang('CopyToLinkTool')), api_get_self().'?action=copy_record_to_link_tool&id='.$meetingDb['id'].'&record_id='.$record['recordId']);
  431. // $url .= Display::url(Display::return_icon('agenda.png',get_lang('AddToCalendar')), api_get_self().'?action=add_to_calendar&id='.$meetingDb['id'].'&start='.api_strtotime($meetingDb['created_at']).'&url='.$record['playbackFormatUrl']);
  432. // $url .= Display::url(Display::return_icon('delete.png',get_lang('Delete')), api_get_self().'?action=delete_record&id='.$record['recordId']);
  433. // }
  434. // //$url .= api_get_self().'?action=publish&id='.$record['recordID'];
  435. // $count++;
  436. // $record_array[] = $url;
  437. // } else {
  438. //
  439. // }
  440. // }
  441. // }
  442. // }
  443. // //var_dump($record_array);
  444. // $item['show_links'] = implode('<br />', $record_array);
  445. //
  446. // }
  447. //
  448. //$item['created_at'] = api_convert_and_format_date($meetingDb['created_at']);
  449. // //created_at
  450. //
  451. // $item['publish_url'] = api_get_self().'?action=publish&id='.$meetingDb['id'];
  452. // $item['unpublish_url'] = api_get_self().'?action=unpublish&id='.$meetingDb['id'];
  453. //
  454. //if ($meetingDb['status'] == 1) {
  455. // $joinParams = array(
  456. // 'meetingId' => $meetingDb['id'], //-- REQUIRED - A unique id for the meeting
  457. // 'username' => $this->user_complete_name, //-- REQUIRED - The name that will display for the user in the meeting
  458. // 'password' => $pass, //-- REQUIRED - The attendee or moderator password, depending on what's passed here
  459. // 'createTime' => '', //-- OPTIONAL - string. Leave blank ('') unless you set this correctly.
  460. // 'userID' => '', // -- OPTIONAL - string
  461. // 'webVoiceConf' => '' // -- OPTIONAL - string
  462. // );
  463. // $returnVal = $this->setUserObjectAndGenerateRoomHashByURLAndRecFlag( array('room_id' => $meetingDb['id']) );
  464. // $joinUrl = CONFIG_OPENMEETINGS_SERVER_URL . "?" .
  465. // "secureHash=" . $returnVal;
  466. //
  467. // $item['go_url'] = $joinUrl;
  468. //}
  469. $item = array_merge($item, $meetingDb, $remoteMeeting);
  470. //error_log(__FILE__.'+'.__LINE__.' Item: '.print_r($item,1));
  471. $newMeetingsList[] = $item;
  472. } //end foreach $meetingsList
  473. }
  474. return $newMeetingsList;
  475. }
  476. /**
  477. * Send a command to the OpenMeetings server to close the meeting.
  478. *
  479. * @param int $meetingId
  480. *
  481. * @return int
  482. */
  483. public function endMeeting($meetingId)
  484. {
  485. try {
  486. $room = new Room($meetingId);
  487. $room->SID = $this->sessionId;
  488. $room->room_id = intval($meetingId);
  489. $room->status = false;
  490. $urlWsdl = $this->url."/services/RoomService?wsdl";
  491. $ws = new \SoapClient($urlWsdl);
  492. $roomClosed = $ws->closeRoom($room);
  493. if ($roomClosed > 0) {
  494. \Database::update(
  495. $this->table,
  496. [
  497. 'status' => 0,
  498. 'closed_at' => api_get_utc_datetime(),
  499. ],
  500. ['id = ? ' => $meetingId]
  501. );
  502. }
  503. } catch (SoapFault $e) {
  504. error_log(__FILE__.'+'.__LINE__.' Warning: We have detected some problems: Fault: '.$e->faultstring);
  505. exit;
  506. return -1;
  507. }
  508. }
  509. /**
  510. * @param int $id
  511. *
  512. * @return int
  513. */
  514. public function deleteMeeting($id)
  515. {
  516. try {
  517. $room = new Room();
  518. $room->loadRoomId($id);
  519. $this->gateway->deleteRoom($room);
  520. \Database::update(
  521. $this->table,
  522. [
  523. 'status' => 2,
  524. ],
  525. ['id = ? ' => $id]
  526. );
  527. return $id;
  528. } catch (SoapFault $e) {
  529. error_log(__FILE__.'+'.__LINE__.' Warning: We have detected some problems: Fault: '.$e->faultstring);
  530. exit;
  531. return -1;
  532. }
  533. }
  534. }