bbb.lib.php 45 KB

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