bbb.lib.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class bbb
  5. * This script initiates a video conference session, calling the BigBlueButton
  6. * API
  7. * @package chamilo.plugin.bigbluebutton
  8. *
  9. * BigBlueButton-Chamilo connector class
  10. */
  11. //namespace Chamilo\Plugin\BBB;
  12. /**
  13. * Class bbb
  14. * @package Chamilo\Plugin\BBB
  15. */
  16. class bbb
  17. {
  18. public $url;
  19. public $salt;
  20. public $api;
  21. public $userCompleteName = '';
  22. public $protocol = 'http://';
  23. public $debug = false;
  24. public $logoutUrl = '';
  25. public $pluginEnabled = false;
  26. public $enableGlobalConference = false;
  27. public $enableGlobalConferencePerUser = false;
  28. public $isGlobalConference = false;
  29. public $groupSupport = false;
  30. public $userSupport = false;
  31. public $accessUrl = 1;
  32. public $userId = 0;
  33. public $plugin;
  34. /**
  35. * Constructor (generates a connection to the API and the Chamilo settings
  36. * required for the connection to the video conference server)
  37. * @param string $host
  38. * @param string $salt
  39. * @param bool $isGlobalConference
  40. * @param int $isGlobalPerUser
  41. */
  42. public function __construct($host = '', $salt = '', $isGlobalConference = false, $isGlobalPerUser = 0)
  43. {
  44. // Initialize video server settings from global settings
  45. $this->plugin = BBBPlugin::create();
  46. $bbbPluginEnabled = $this->plugin->get('tool_enable');
  47. $bbb_host = !empty($host) ? $host : $this->plugin->get('host');
  48. $bbb_salt = !empty($salt) ? $salt : $this->plugin->get('salt');
  49. $this->logoutUrl = $this->getListingUrl();
  50. $this->table = Database::get_main_table('plugin_bbb_meeting');
  51. $this->enableGlobalConference = (bool) $this->plugin->get('enable_global_conference');
  52. $this->isGlobalConference = (bool) $isGlobalConference;
  53. $columns = Database::listTableColumns($this->table);
  54. $this->groupSupport = isset($columns['group_id']) ? true : false;
  55. $this->userSupport = isset($columns['user_id']) ? true : false;
  56. $this->accessUrl = api_get_current_access_url_id();
  57. if ($this->userSupport && !empty($isGlobalPerUser)) {
  58. $this->enableGlobalConferencePerUser = (bool) $this->plugin->get('enable_global_conference_per_user');
  59. $this->userId = $isGlobalPerUser;
  60. } else {
  61. $this->enableGlobalConferencePerUser = false;
  62. }
  63. if ($this->groupSupport) {
  64. // Plugin check
  65. $this->groupSupport = (bool) $this->plugin->get('enable_conference_in_course_groups');
  66. if ($this->groupSupport) {
  67. // Platform check
  68. $bbbSetting = api_get_setting('bbb_enable_conference_in_course_groups');
  69. $bbbSetting = isset($bbbSetting['bbb']) ? $bbbSetting['bbb'] === 'true' : false;
  70. if ($bbbSetting) {
  71. // Course check
  72. $courseInfo = api_get_course_info();
  73. if ($courseInfo) {
  74. $this->groupSupport = api_get_course_setting('bbb_enable_conference_in_groups', $courseInfo['code']) === '1';
  75. }
  76. }
  77. }
  78. }
  79. if ($bbbPluginEnabled === 'true') {
  80. $userInfo = api_get_user_info();
  81. $this->userCompleteName = $userInfo['complete_name'];
  82. $this->salt = $bbb_salt;
  83. $info = parse_url($bbb_host);
  84. $this->url = $bbb_host.'/bigbluebutton/';
  85. if (isset($info['scheme'])) {
  86. $this->protocol = $info['scheme'].'://';
  87. $this->url = str_replace($this->protocol, '', $this->url);
  88. }
  89. // Setting BBB api
  90. define('CONFIG_SECURITY_SALT', $this->salt);
  91. define('CONFIG_SERVER_BASE_URL', $this->url);
  92. $this->api = new BigBlueButtonBN();
  93. $this->pluginEnabled = true;
  94. }
  95. }
  96. /**
  97. * @return bool
  98. */
  99. public function isGlobalConferenceEnabled()
  100. {
  101. return $this->enableGlobalConference;
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function isGlobalConferencePerUserEnabled()
  107. {
  108. return $this->enableGlobalConferencePerUser;
  109. }
  110. /**
  111. * @return bool
  112. */
  113. public function isGlobalConference()
  114. {
  115. if ($this->isGlobalConferenceEnabled() === false) {
  116. return false;
  117. }
  118. return (bool) $this->isGlobalConference;
  119. }
  120. /**
  121. * @return bool
  122. */
  123. public function hasGroupSupport()
  124. {
  125. return $this->groupSupport;
  126. }
  127. /**
  128. * Checks whether a user is teacher in the current course
  129. * @return bool True if the user can be considered a teacher in this course, false otherwise
  130. */
  131. public function isConferenceManager()
  132. {
  133. return api_is_course_admin() || api_is_coach() || api_is_platform_admin();
  134. }
  135. /**
  136. * See this file in you BBB to set up default values
  137. * @param array $params Array of parameters that will be completed if not containing all expected variables
  138. /var/lib/tomcat6/webapps/bigbluebutton/WEB-INF/classes/bigbluebutton.properties
  139. *
  140. More record information:
  141. http://code.google.com/p/bigbluebutton/wiki/RecordPlaybackSpecification
  142. # Default maximum number of users a meeting can have.
  143. # Doesn't get enforced yet but is the default value when the create
  144. # API doesn't pass a value.
  145. defaultMaxUsers=20
  146. # Default duration of the meeting in minutes.
  147. # Current default is 0 (meeting doesn't end).
  148. defaultMeetingDuration=0
  149. # Remove the meeting from memory when the end API is called.
  150. # This allows 3rd-party apps to recycle the meeting right-away
  151. # instead of waiting for the meeting to expire (see below).
  152. removeMeetingWhenEnded=false
  153. # The number of minutes before the system removes the meeting from memory.
  154. defaultMeetingExpireDuration=1
  155. # The number of minutes the system waits when a meeting is created and when
  156. # a user joins. If after this period, a user hasn't joined, the meeting is
  157. # removed from memory.
  158. defaultMeetingCreateJoinDuration=5
  159. *
  160. * @return mixed
  161. */
  162. public function createMeeting($params)
  163. {
  164. $courseCode = api_get_course_id();
  165. $params['c_id'] = api_get_course_int_id();
  166. $params['session_id'] = api_get_session_id();
  167. if ($this->hasGroupSupport()) {
  168. $params['group_id'] = api_get_group_id();
  169. }
  170. if ($this->isGlobalConferencePerUserEnabled()) {
  171. $currentUserId = api_get_user_id();
  172. if ($this->userId === $currentUserId) {
  173. $params['user_id'] = $this->userId;
  174. }
  175. }
  176. $params['attendee_pw'] = isset($params['moderator_pw']) ? $params['moderator_pw'] : $this->getUserMeetingPassword();
  177. $attendeePassword = $params['attendee_pw'];
  178. $params['moderator_pw'] = isset($params['moderator_pw']) ? $params['moderator_pw'] : $this->getModMeetingPassword();
  179. $moderatorPassword = $params['moderator_pw'];
  180. $params['record'] = api_get_course_setting('big_blue_button_record_and_store', $courseCode) == 1 ? true : false;
  181. $max = api_get_course_setting('big_blue_button_max_students_allowed', $courseCode);
  182. $max = isset($max) ? $max : -1;
  183. $params['status'] = 1;
  184. // Generate a pseudo-global-unique-id to avoid clash of conferences on
  185. // the same BBB server with several Chamilo portals
  186. $params['remote_id'] = uniqid(true, true);
  187. // Each simultaneous conference room needs to have a different
  188. // voice_bridge composed of a 5 digits number, so generating a random one
  189. $params['voice_bridge'] = rand(10000, 99999);
  190. if ($this->debug) {
  191. error_log("enter create_meeting ".print_r($params, 1));
  192. }
  193. $params['created_at'] = api_get_utc_datetime();
  194. $params['access_url'] = $this->accessUrl;
  195. $id = Database::insert($this->table, $params);
  196. if ($id) {
  197. if ($this->debug) {
  198. error_log("create_meeting: $id ");
  199. }
  200. $meetingName = isset($params['meeting_name']) ? $params['meeting_name'] : $this->getCurrentVideoConferenceName();
  201. $welcomeMessage = isset($params['welcome_msg']) ? $params['welcome_msg'] : null;
  202. $record = isset($params['record']) && $params['record'] ? 'true' : 'false';
  203. $duration = isset($params['duration']) ? intval($params['duration']) : 0;
  204. // This setting currently limits the maximum conference duration,
  205. // to avoid lingering sessions on the video-conference server #6261
  206. $duration = 300;
  207. $bbbParams = array(
  208. 'meetingId' => $params['remote_id'], // REQUIRED
  209. 'meetingName' => $meetingName, // REQUIRED
  210. 'attendeePw' => $attendeePassword, // Match this value in getJoinMeetingURL() to join as attendee.
  211. 'moderatorPw' => $moderatorPassword, // Match this value in getJoinMeetingURL() to join as moderator.
  212. 'welcomeMsg' => $welcomeMessage, // ''= use default. Change to customize.
  213. 'dialNumber' => '', // The main number to call into. Optional.
  214. 'voiceBridge' => $params['voice_bridge'], // PIN to join voice. Required.
  215. 'webVoice' => '', // Alphanumeric to join voice. Optional.
  216. 'logoutUrl' => $this->logoutUrl,
  217. 'maxParticipants' => $max, // Optional. -1 = unlimitted. Not supported in BBB. [number]
  218. 'record' => $record, // New. 'true' will tell BBB to record the meeting.
  219. 'duration' => $duration, // Default = 0 which means no set duration in minutes. [number]
  220. //'meta_category' => '', // Use to pass additional info to BBB server. See API docs.
  221. );
  222. if ($this->debug) {
  223. error_log("create_meeting params: ".print_r($bbbParams,1));
  224. }
  225. $status = false;
  226. $meeting = null;
  227. while ($status === false) {
  228. $result = $this->api->createMeetingWithXmlResponseArray(
  229. $bbbParams
  230. );
  231. if (isset($result) && strval($result['returncode']) == 'SUCCESS') {
  232. if ($this->debug) {
  233. error_log(
  234. "create_meeting result: " . print_r($result, 1)
  235. );
  236. }
  237. $meeting = $this->joinMeeting($meetingName, true);
  238. return $meeting;
  239. }
  240. }
  241. return $this->logoutUrl;
  242. }
  243. }
  244. /**
  245. * Tells whether the given meeting exists and is running
  246. * (using course code as name)
  247. * @param string $meetingName Meeting name (usually the course code)
  248. *
  249. * @return bool True if meeting exists, false otherwise
  250. * @assert ('') === false
  251. * @assert ('abcdefghijklmnopqrstuvwxyzabcdefghijklmno') === false
  252. */
  253. public function meetingExists($meetingName)
  254. {
  255. if (empty($meetingName)) {
  256. return false;
  257. }
  258. $courseId = api_get_course_int_id();
  259. $sessionId = api_get_session_id();
  260. $conditions = array(
  261. 'where' => array(
  262. 'c_id = ? AND session_id = ? AND meeting_name = ? AND status = 1 AND access_url = ?' =>
  263. array($courseId, $sessionId, $meetingName, $this->accessUrl)
  264. )
  265. );
  266. if ($this->hasGroupSupport()) {
  267. $groupId = api_get_group_id();
  268. $conditions = array(
  269. 'where' => array(
  270. 'c_id = ? AND session_id = ? AND meeting_name = ? AND group_id = ? AND status = 1 AND access_url = ?' =>
  271. array($courseId, $sessionId, $meetingName, $groupId, $this->accessUrl)
  272. )
  273. );
  274. }
  275. $meetingData = Database::select(
  276. '*',
  277. $this->table,
  278. $conditions,
  279. 'first'
  280. );
  281. if ($this->debug) {
  282. error_log("meeting_exists ".print_r($meetingData, 1));
  283. }
  284. if (empty($meetingData)) {
  285. return false;
  286. } else {
  287. return true;
  288. }
  289. }
  290. /**
  291. * Returns a meeting "join" URL
  292. * @param string The name of the meeting (usually the course code)
  293. * @return mixed The URL to join the meeting, or false on error
  294. * @todo implement moderator pass
  295. * @assert ('') === false
  296. * @assert ('abcdefghijklmnopqrstuvwxyzabcdefghijklmno') === false
  297. */
  298. public function joinMeeting($meetingName, $loop = false)
  299. {
  300. if (empty($meetingName)) {
  301. return false;
  302. }
  303. $pass = $this->getUserMeetingPassword();
  304. $meetingData = Database::select(
  305. '*',
  306. $this->table,
  307. array('where' => array('meeting_name = ? AND status = 1 AND access_url = ?' => array($meetingName, $this->accessUrl))),
  308. 'first'
  309. );
  310. if (empty($meetingData) || !is_array($meetingData)) {
  311. if ($this->debug) {
  312. error_log("meeting does not exist: $meetingName");
  313. }
  314. return false;
  315. }
  316. $params = array(
  317. 'meetingId' => $meetingData['remote_id'],
  318. // -- REQUIRED - The unique id for the meeting
  319. 'password' => $this->getModMeetingPassword()
  320. // -- REQUIRED - The moderator password for the meeting
  321. );
  322. $status = false;
  323. $meetingInfoExists = false;
  324. while ($status === false) {
  325. $meetingIsRunningInfo = $this->getMeetingInfo($params);
  326. if ($meetingIsRunningInfo === false) {
  327. //checking with the remote_id didn't work, so just in case and
  328. // to provide backwards support, check with the id
  329. $params = array(
  330. 'meetingId' => $meetingData['id'],
  331. // -- REQUIRED - The unique id for the meeting
  332. 'password' => $this->getModMeetingPassword()
  333. // -- REQUIRED - The moderator password for the meeting
  334. );
  335. $meetingIsRunningInfo = $this->getMeetingInfo($params);
  336. }
  337. if ($this->debug) {
  338. error_log(print_r($meetingIsRunningInfo, 1));
  339. }
  340. if (strval($meetingIsRunningInfo['returncode']) == 'SUCCESS' &&
  341. isset($meetingIsRunningInfo['meetingName']) &&
  342. !empty($meetingIsRunningInfo['meetingName'])
  343. //strval($meetingIsRunningInfo['running']) == 'true'
  344. ) {
  345. $meetingInfoExists = true;
  346. }
  347. if ($this->debug) {
  348. error_log(
  349. "meeting is running: " . intval($meetingInfoExists)
  350. );
  351. }
  352. if ($meetingInfoExists) {
  353. $status = true;
  354. }
  355. if ($loop) {
  356. continue;
  357. } else {
  358. break;
  359. }
  360. }
  361. if ($meetingInfoExists) {
  362. $joinParams = array(
  363. 'meetingId' => $meetingData['remote_id'], // -- REQUIRED - A unique id for the meeting
  364. 'username' => $this->userCompleteName, //-- REQUIRED - The name that will display for the user in the meeting
  365. 'password' => $pass, //-- REQUIRED - The attendee or moderator password, depending on what's passed here
  366. //'createTime' => api_get_utc_datetime(), //-- OPTIONAL - string. Leave blank ('') unless you set this correctly.
  367. 'userID' => api_get_user_id(), //-- OPTIONAL - string
  368. 'webVoiceConf' => '' // -- OPTIONAL - string
  369. );
  370. $url = $this->api->getJoinMeetingURL($joinParams);
  371. $url = $this->protocol.$url;
  372. } else {
  373. $url = $this->logoutUrl;
  374. }
  375. if ($this->debug) {
  376. error_log("return url :" . $url);
  377. }
  378. return $url;
  379. }
  380. /**
  381. * Get information about the given meeting
  382. * @param array ...?
  383. * @return mixed Array of information on success, false on error
  384. * @assert (array()) === false
  385. */
  386. public function getMeetingInfo($params)
  387. {
  388. try {
  389. $result = $this->api->getMeetingInfoWithXmlResponseArray($params);
  390. if ($result == null) {
  391. if ($this->debug) {
  392. error_log("Failed to get any response. Maybe we can't contact the BBB server.");
  393. }
  394. } else {
  395. return $result;
  396. }
  397. } catch (Exception $e) {
  398. if ($this->debug) {
  399. error_log('Caught exception: ', $e->getMessage(), "\n");
  400. }
  401. }
  402. return false;
  403. }
  404. /**
  405. * @param int $courseId
  406. * @param int $sessionId
  407. * @param int $status
  408. *
  409. * @return array
  410. */
  411. public function getAllMeetingsInCourse($courseId, $sessionId, $status)
  412. {
  413. $conditions = array(
  414. 'where' => array(
  415. 'status = ? AND c_id = ? AND session_id = ? ' => array(
  416. $status,
  417. $courseId,
  418. $sessionId,
  419. ),
  420. ),
  421. );
  422. $meetingList = Database::select(
  423. '*',
  424. $this->table,
  425. $conditions
  426. );
  427. return $meetingList;
  428. }
  429. /**
  430. * Gets all the course meetings saved in the plugin_bbb_meeting table
  431. * @return array Array of current open meeting rooms
  432. */
  433. public function getMeetings()
  434. {
  435. $pass = $this->getUserMeetingPassword();
  436. $isGlobal = $this->isGlobalConference();
  437. $isGlobalPerUser = $this->isGlobalConferencePerUserEnabled();
  438. $courseId = api_get_course_int_id();
  439. $sessionId = api_get_session_id();
  440. $conditions = array(
  441. 'where' => array(
  442. 'c_id = ? AND session_id = ? AND access_url = ?' => array(
  443. $courseId,
  444. $sessionId,
  445. $this->accessUrl
  446. ),
  447. ),
  448. );
  449. if ($this->hasGroupSupport()) {
  450. $groupId = api_get_group_id();
  451. $conditions = array(
  452. 'where' => array(
  453. 'c_id = ? AND session_id = ? AND group_id = ? AND access_url = ?' =>
  454. array($courseId, $sessionId, $groupId, $this->accessUrl)
  455. )
  456. );
  457. }
  458. if ($isGlobalPerUser) {
  459. $conditions = array(
  460. 'where' => array(
  461. 'user_id = ? AND access_url = ?' =>
  462. array($this->userId, $this->accessUrl)
  463. )
  464. );
  465. }
  466. $meetingList = Database::select(
  467. '*',
  468. $this->table,
  469. $conditions
  470. );
  471. $isGlobal = $this->isGlobalConference();
  472. $newMeetingList = array();
  473. $item = array();
  474. foreach ($meetingList as $meetingDB) {
  475. $meetingBBB = $this->getMeetingInfo(['meetingId' => $meetingDB['remote_id'], 'password' => $pass]);
  476. if ($meetingBBB === false) {
  477. //checking with the remote_id didn't work, so just in case and
  478. // to provide backwards support, check with the id
  479. $params = array(
  480. 'meetingId' => $meetingDB['id'],
  481. // -- REQUIRED - The unique id for the meeting
  482. 'password' => $pass
  483. // -- REQUIRED - The moderator password for the meeting
  484. );
  485. $meetingBBB = $this->getMeetingInfo($params);
  486. }
  487. if ($meetingDB['visibility'] == 0 && $this->isConferenceManager() === false) {
  488. continue;
  489. }
  490. $meetingBBB['end_url'] = $this->endUrl($meetingDB);
  491. if (isset($meetingBBB['returncode']) && (string)$meetingBBB['returncode'] == 'FAILED') {
  492. if ($meetingDB['status'] == 1 && $this->isConferenceManager()) {
  493. $this->endMeeting($meetingDB['id']);
  494. }
  495. } else {
  496. $meetingBBB['add_to_calendar_url'] = $this->addToCalendarUrl($meetingDB);
  497. }
  498. $recordArray = array();
  499. $actionLinksArray = array();
  500. if ($meetingDB['record'] == 1) {
  501. // backwards compatibility (when there was no remote ID)
  502. $mId = $meetingDB['remote_id'];
  503. if (empty($mId)) {
  504. $mId = $meetingDB['id'];
  505. }
  506. if (empty($mId)) {
  507. // if the id is still empty (should *never* occur as 'id' is
  508. // the table's primary key), skip this conference
  509. continue;
  510. }
  511. $recordingParams = array(
  512. 'meetingId' => $mId, //-- OPTIONAL - comma separate if multiple ids
  513. );
  514. //To see the recording list in your BBB server do: bbb-record --list
  515. $records = $this->api->getRecordingsWithXmlResponseArray($recordingParams);
  516. if (!empty($records)) {
  517. $count = 1;
  518. if (isset($records['message']) && !empty($records['message'])) {
  519. if ($records['messageKey'] == 'noRecordings') {
  520. $recordArray[] = $this->plugin->get_lang('NoRecording');
  521. if ($meetingDB['visibility'] == 0) {
  522. $actionLinksArray[] = Display::url(
  523. Display::return_icon(
  524. 'invisible.png',
  525. $this->plugin->get_lang('MakeVisible'),
  526. array(),
  527. ICON_SIZE_MEDIUM
  528. ),
  529. $this->publishUrl($meetingDB)
  530. );
  531. } else {
  532. $actionLinksArray[] = Display::url(
  533. Display::return_icon(
  534. 'visible.png',
  535. $this->plugin->get_lang('MakeInvisible'),
  536. array(),
  537. ICON_SIZE_MEDIUM
  538. ),
  539. $this->unPublishUrl($meetingDB)
  540. );
  541. }
  542. }
  543. } else {
  544. foreach ($records as $record) {
  545. //if you get several recordings here and you used a
  546. // previous version of Chamilo, you might want to
  547. // only keep the last result for each chamilo conf
  548. // (see show_links after the end of this loop)
  549. if (is_array($record) && isset($record['recordId'])) {
  550. $url = Display::url(
  551. $this->plugin->get_lang('ViewRecord')." [~".$record['playbackFormatLength']."']",
  552. $record['playbackFormatUrl'],
  553. array('target' => '_blank')
  554. );
  555. $actionLinks = '';
  556. if ($this->isConferenceManager()) {
  557. if ($isGlobal === false) {
  558. $actionLinks .= Display::url(
  559. Display::return_icon(
  560. 'link.gif',
  561. $this->plugin->get_lang('CopyToLinkTool')
  562. ),
  563. $this->copyToRecordToLinkTool($meetingDB)
  564. );
  565. $actionLinks .= Display::url(
  566. Display::return_icon(
  567. 'agenda.png',
  568. $this->plugin->get_lang('AddToCalendar')
  569. ),
  570. $this->addToCalendarUrl($meetingDB, $record)
  571. );
  572. }
  573. $actionLinks .= Display::url(
  574. Display::return_icon(
  575. 'delete.png',
  576. $this->plugin->get_lang('Delete')
  577. ),
  578. $this->deleteRecordUrl($meetingDB)
  579. );
  580. if ($meetingDB['visibility'] == 0) {
  581. $actionLinks .= Display::url(
  582. Display::return_icon(
  583. 'invisible.png',
  584. $this->plugin->get_lang('MakeVisible'),
  585. array(),
  586. ICON_SIZE_MEDIUM
  587. ),
  588. $this->publishUrl($meetingDB)
  589. );
  590. } else {
  591. $actionLinks .= Display::url(
  592. Display::return_icon(
  593. 'visible.png',
  594. $this->plugin->get_lang('MakeInvisible'),
  595. array(),
  596. ICON_SIZE_MEDIUM
  597. ),
  598. $this->unPublishUrl($meetingDB)
  599. );
  600. }
  601. }
  602. $count++;
  603. $recordArray[] = $url;
  604. $actionLinksArray[] = $actionLinks;
  605. } else {
  606. /*if (is_array($record) && isset($record['recordID']) && isset($record['playbacks'])) {
  607. //Fix the bbb timestamp
  608. //$record['startTime'] = substr($record['startTime'], 0, strlen($record['startTime']) -3);
  609. //$record['endTime'] = substr($record['endTime'], 0, strlen($record['endTime']) -3);
  610. //.' - '.api_convert_and_format_date($record['startTime']).' - '.api_convert_and_format_date($record['endTime'])
  611. foreach($record['playbacks'] as $item) {
  612. $url = Display::url(get_lang('ViewRecord'), $item['url'], array('target' => '_blank'));
  613. //$url .= Display::url(get_lang('DeleteRecord'), api_get_self().'?action=delete_record&'.$record['recordID']);
  614. if ($this->isConferenceManager()) {
  615. $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']);
  616. $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='.$item['url']);
  617. $url .= Display::url(Display::return_icon('delete.png',get_lang('Delete')), api_get_self().'?action=delete_record&id='.$record['recordID']);
  618. }
  619. //$url .= api_get_self().'?action=publish&id='.$record['recordID'];
  620. $count++;
  621. $recordArray[] = $url;
  622. }
  623. }*/
  624. }
  625. }
  626. }
  627. } else {
  628. $actionLinks = '';
  629. if ($this->isConferenceManager()) {
  630. if ($meetingDB['visibility'] == 0) {
  631. $actionLinks .= Display::url(
  632. Display::return_icon(
  633. 'invisible.png',
  634. $this->plugin->get_lang('MakeVisible'),
  635. array(),
  636. ICON_SIZE_MEDIUM
  637. ),
  638. $this->publishUrl($meetingDB)
  639. );
  640. } else {
  641. $actionLinks .= Display::url(
  642. Display::return_icon(
  643. 'visible.png',
  644. $this->plugin->get_lang('MakeInvisible'),
  645. array(),
  646. ICON_SIZE_MEDIUM
  647. ),
  648. $this->unPublishUrl($meetingDB)
  649. );
  650. }
  651. }
  652. $actionLinksArray[] = $actionLinks;
  653. $item['action_links'] = implode('<br />', $actionLinksArray);
  654. }
  655. $item['show_links'] = implode('<br />', $recordArray);
  656. $item['action_links'] = implode('<br />', $actionLinksArray);
  657. }
  658. $item['created_at'] = api_convert_and_format_date($meetingDB['created_at']);
  659. //created_at
  660. $meetingDB['created_at'] = $item['created_at']; //avoid overwrite in array_merge() below
  661. $item['publish_url'] = $this->publishUrl($meetingDB);
  662. $item['unpublish_url'] = $this->unPublishUrl($meetingBBB);
  663. if ($meetingDB['status'] == 1) {
  664. $joinParams = array(
  665. 'meetingId' => $meetingDB['remote_id'], //-- REQUIRED - A unique id for the meeting
  666. 'username' => $this->userCompleteName, //-- REQUIRED - The name that will display for the user in the meeting
  667. 'password' => $pass, //-- REQUIRED - The attendee or moderator password, depending on what's passed here
  668. 'createTime' => '', //-- OPTIONAL - string. Leave blank ('') unless you set this correctly.
  669. 'userID' => '', // -- OPTIONAL - string
  670. 'webVoiceConf' => '' // -- OPTIONAL - string
  671. );
  672. $item['go_url'] = $this->protocol.$this->api->getJoinMeetingURL($joinParams);
  673. }
  674. $item = array_merge($item, $meetingDB, $meetingBBB);
  675. $newMeetingList[] = $item;
  676. }
  677. return $newMeetingList;
  678. }
  679. /**
  680. * Function disabled
  681. */
  682. public function publishMeeting($id)
  683. {
  684. //return BigBlueButtonBN::setPublishRecordings($id, 'true', $this->url, $this->salt);
  685. if (empty($id)) {
  686. return false;
  687. }
  688. $id = intval($id);
  689. Database::update($this->table, array('visibility' => 1), array('id = ? ' => $id));
  690. return true;
  691. }
  692. /**
  693. * Function disabled
  694. */
  695. public function unpublishMeeting($id)
  696. {
  697. //return BigBlueButtonBN::setPublishRecordings($id, 'false', $this->url, $this->salt);
  698. if (empty($id)) {
  699. return false;
  700. }
  701. $id = intval($id);
  702. Database::update($this->table, array('visibility' => 0), array('id = ?' => $id));
  703. return true;
  704. }
  705. /**
  706. * Closes a meeting (usually when the user click on the close button from
  707. * the conferences listing.
  708. * @param string The internal ID of the meeting (id field for this meeting)
  709. * @return void
  710. * @assert (0) === false
  711. */
  712. public function endMeeting($id)
  713. {
  714. if (empty($id)) {
  715. return false;
  716. }
  717. $meetingData = Database::select('*', $this->table, array('where' => array('id = ?' => array($id))), 'first');
  718. $pass = $this->getUserMeetingPassword();
  719. $endParams = array(
  720. 'meetingId' => $meetingData['remote_id'], // REQUIRED - We have to know which meeting to end.
  721. 'password' => $pass, // REQUIRED - Must match moderator pass for meeting.
  722. );
  723. $this->api->endMeetingWithXmlResponseArray($endParams);
  724. Database::update(
  725. $this->table,
  726. array('status' => 0, 'closed_at' => api_get_utc_datetime()),
  727. array('id = ? ' => $id)
  728. );
  729. }
  730. /**
  731. * Gets the password for a specific meeting for the current user
  732. * @return string A moderator password if user is teacher, or the course code otherwise
  733. */
  734. public function getUserMeetingPassword()
  735. {
  736. if ($this->isConferenceManager()) {
  737. return $this->getModMeetingPassword();
  738. } else {
  739. if ($this->isGlobalConferencePerUserEnabled()) {
  740. return 'url_'.$this->userId.'_'.api_get_current_access_url_id();
  741. }
  742. if ($this->isGlobalConference()) {
  743. return 'url_'.api_get_current_access_url_id();
  744. }
  745. return api_get_course_id();
  746. }
  747. }
  748. /**
  749. * Generated a moderator password for the meeting
  750. * @return string A password for the moderation of the videoconference
  751. */
  752. public function getModMeetingPassword()
  753. {
  754. if ($this->isGlobalConferencePerUserEnabled()) {
  755. return 'url_'.$this->userId.'_'.api_get_current_access_url_id().'_mod';
  756. }
  757. if ($this->isGlobalConference()) {
  758. return 'url_'.api_get_current_access_url_id().'_mod';
  759. }
  760. return api_get_course_id().'mod';
  761. }
  762. /**
  763. * Get users online in the current course room
  764. * @return int The number of users currently connected to the videoconference
  765. * @assert () > -1
  766. */
  767. public function getUsersOnlineInCurrentRoom()
  768. {
  769. $courseId = api_get_course_int_id();
  770. $sessionId = api_get_session_id();
  771. $conditions = array(
  772. 'where' => array(
  773. 'c_id = ? AND session_id = ? AND status = 1 AND access_url = ?' => array(
  774. $courseId,
  775. $sessionId,
  776. $this->accessUrl
  777. ),
  778. ),
  779. );
  780. if ($this->hasGroupSupport()) {
  781. $groupId = api_get_group_id();
  782. $conditions = array(
  783. 'where' => array(
  784. 'c_id = ? AND session_id = ? AND group_id = ? AND status = 1 AND access_url = ?' => array(
  785. $courseId,
  786. $sessionId,
  787. $groupId,
  788. $this->accessUrl
  789. ),
  790. ),
  791. );
  792. }
  793. if ($this->isGlobalConferencePerUserEnabled()) {
  794. $conditions = array(
  795. 'where' => array(
  796. 'user_id = ? AND status = 1 AND access_url = ?' => array(
  797. $this->userId,
  798. $this->accessUrl
  799. ),
  800. ),
  801. );
  802. }
  803. $meetingData = Database::select(
  804. '*',
  805. $this->table,
  806. $conditions,
  807. 'first'
  808. );
  809. if (empty($meetingData)) {
  810. return 0;
  811. }
  812. $pass = $this->getModMeetingPassword();
  813. $info = $this->getMeetingInfo(array('meetingId' => $meetingData['remote_id'], 'password' => $pass));
  814. if ($info === false) {
  815. //checking with the remote_id didn't work, so just in case and
  816. // to provide backwards support, check with the id
  817. $params = array(
  818. 'meetingId' => $meetingData['id'],
  819. // -- REQUIRED - The unique id for the meeting
  820. 'password' => $pass
  821. // -- REQUIRED - The moderator password for the meeting
  822. );
  823. $info = $this->getMeetingInfo($params);
  824. }
  825. if (!empty($info) && isset($info['participantCount'])) {
  826. return $info['participantCount'];
  827. }
  828. return 0;
  829. }
  830. /**
  831. * Deletes a previous recording of a meeting
  832. * @param int $id ID of the recording
  833. * @return array ?
  834. * @assert () === false
  835. * @todo Also delete links and agenda items created from this recording
  836. */
  837. public function deleteRecord($id)
  838. {
  839. if (empty($id)) {
  840. return false;
  841. }
  842. $meetingData = Database::select(
  843. '*',
  844. $this->table,
  845. array('where' => array('id = ?' => array($id))),
  846. 'first'
  847. );
  848. $recordingParams = array(
  849. /*
  850. * NOTE: Set the recordId below to a valid id after you have
  851. * created a recorded meeting, and received a real recordID
  852. * back from your BBB server using the
  853. * getRecordingsWithXmlResponseArray method.
  854. */
  855. // REQUIRED - We have to know which recording:
  856. 'recordId' => $meetingData['remote_id'],
  857. );
  858. $result = $this->api->deleteRecordingsWithXmlResponseArray($recordingParams);
  859. if (!empty($result) && isset($result['deleted']) && $result['deleted'] === 'true') {
  860. Database::delete(
  861. $this->table,
  862. array('id = ?' => array($id))
  863. );
  864. }
  865. return $result;
  866. }
  867. /**
  868. * Creates a link in the links tool from the given videoconference recording
  869. * @param int $id ID of the item in the plugin_bbb_meeting table
  870. * @param string Hash identifying the recording, as provided by the API
  871. * @return mixed ID of the newly created link, or false on error
  872. * @assert (null, null) === false
  873. * @assert (1, null) === false
  874. * @assert (null, 'abcdefabcdefabcdefabcdef') === false
  875. */
  876. public function copyRecordToLinkTool($id)
  877. {
  878. if (empty($id)) {
  879. return false;
  880. }
  881. //$records = BigBlueButtonBN::getRecordingsUrl($id);
  882. $meetingData = Database::select('*', $this->table, array('where' => array('id = ?' => array($id))), 'first');
  883. $records = $this->api->getRecordingsWithXmlResponseArray(array('meetingId' => $meetingData['remote_id']));
  884. if (!empty($records)) {
  885. if (isset($records['message']) && !empty($records['message'])) {
  886. if ($records['messageKey'] == 'noRecordings') {
  887. $recordArray[] = $this->plugin->get_lang('NoRecording');
  888. } else {
  889. //$recordArray[] = $records['message'];
  890. }
  891. return false;
  892. } else {
  893. $record = $records[0];
  894. if (is_array($record) && isset($record['recordId'])) {
  895. $url = $record['playbackFormatUrl'];
  896. $link = new Link();
  897. $params['url'] = $url;
  898. $params['title'] = $meetingData['meeting_name'];
  899. $id = $link->save($params);
  900. return $id;
  901. }
  902. }
  903. }
  904. return false;
  905. }
  906. /**
  907. * Checks if the video conference server is running.
  908. * Function currently disabled (always returns 1)
  909. * @return bool True if server is running, false otherwise
  910. * @assert () === false
  911. */
  912. public function isServerRunning()
  913. {
  914. return true;
  915. //return BigBlueButtonBN::isServerRunning($this->protocol.$this->url);
  916. }
  917. /**
  918. * Get active session in the all platform
  919. */
  920. public function getActiveSessionsCount()
  921. {
  922. $meetingList = Database::select(
  923. 'count(id) as count',
  924. $this->table,
  925. array('where' => array('status = ? AND access_url = ?' => array(1, $this->accessUrl))),
  926. 'first'
  927. );
  928. return $meetingList['count'];
  929. }
  930. /**
  931. * @param string $url
  932. */
  933. public function redirectToBBB($url)
  934. {
  935. if (file_exists(__DIR__ . '/../config.vm.php')) {
  936. // Using VM
  937. echo Display::url($this->plugin->get_lang('ClickToContinue'), $url);
  938. exit;
  939. } else {
  940. // Classic
  941. header("Location: $url");
  942. exit;
  943. }
  944. }
  945. /**
  946. * @return string
  947. */
  948. public function getUrlParams()
  949. {
  950. $courseInfo = api_get_course_info();
  951. if (empty($courseInfo)) {
  952. if ($this->isGlobalConferencePerUserEnabled()) {
  953. return 'global=1&user_id='.$this->userId;
  954. }
  955. if ($this->isGlobalConference()) {
  956. return 'global=1';
  957. }
  958. return '';
  959. }
  960. return api_get_cidreq();
  961. }
  962. /**
  963. * @return string
  964. */
  965. public function getCurrentVideoConferenceName()
  966. {
  967. if ($this->isGlobalConferencePerUserEnabled()) {
  968. return 'url_'.$this->userId.'_'.api_get_current_access_url_id();
  969. }
  970. if ($this->isGlobalConference()) {
  971. return 'url_'.api_get_current_access_url_id();
  972. }
  973. if ($this->hasGroupSupport()) {
  974. return api_get_course_id().'-'.api_get_session_id().'-'.api_get_group_id();
  975. }
  976. return api_get_course_id().'-'.api_get_session_id();
  977. }
  978. /**
  979. * @return string
  980. */
  981. public function getConferenceUrl()
  982. {
  983. return api_get_path(WEB_PLUGIN_PATH).'bbb/start.php?launch=1&'.$this->getUrlParams();
  984. }
  985. /**
  986. * @return string
  987. */
  988. public function getListingUrl()
  989. {
  990. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams();
  991. }
  992. /**
  993. * @param array $meeting
  994. * @return string
  995. */
  996. public function endUrl($meeting)
  997. {
  998. if (!isset($meeting['id'])) {
  999. return '';
  1000. }
  1001. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams().'&action=end&id='.$meeting['id'];
  1002. }
  1003. /**
  1004. * @param array $meeting
  1005. * @param array $record
  1006. * @return string
  1007. */
  1008. public function addToCalendarUrl($meeting, $record = [])
  1009. {
  1010. $url = isset($record['playbackFormatUrl']) ? $record['playbackFormatUrl'] : '';
  1011. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams().'&action=add_to_calendar&id='.$meeting['id'].'&start='.api_strtotime($meeting['created_at']).'&url='.$url;
  1012. }
  1013. /**
  1014. * @param array $meeting
  1015. * @return string
  1016. */
  1017. public function publishUrl($meeting)
  1018. {
  1019. if (!isset($meeting['id'])) {
  1020. return '';
  1021. }
  1022. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams().'&action=publish&id='.$meeting['id'];
  1023. }
  1024. /**
  1025. * @param array $meeting
  1026. * @return string
  1027. */
  1028. public function unPublishUrl($meeting)
  1029. {
  1030. if (!isset($meeting['id'])) {
  1031. return '';
  1032. }
  1033. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams().'&action=unpublish&id='.$meeting['id'];
  1034. }
  1035. /**
  1036. * @param array $meeting
  1037. * @return string
  1038. */
  1039. public function deleteRecordUrl($meeting)
  1040. {
  1041. if (!isset($meeting['id'])) {
  1042. return '';
  1043. }
  1044. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams().'&action=delete_record&id='.$meeting['id'];
  1045. }
  1046. /**
  1047. * @param array $meeting
  1048. * @return string
  1049. */
  1050. public function copyToRecordToLinkTool($meeting)
  1051. {
  1052. if (!isset($meeting['id'])) {
  1053. return '';
  1054. }
  1055. return api_get_path(WEB_PLUGIN_PATH).'bbb/listing.php?'.$this->getUrlParams().'&action=copy_record_to_link_tool&id='.$meeting['id'];
  1056. }
  1057. }