bbb.lib.php 40 KB

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