bbb.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * This script initiates a videoconference session, calling the BigBlueButton API
  4. * @package chamilo.plugin.bigbluebutton
  5. */
  6. class bbb {
  7. var $url;
  8. var $salt;
  9. var $api;
  10. var $user_complete_name = null;
  11. var $protocol = 'http://';
  12. var $debug = true;
  13. var $logout_url = null;
  14. function __construct() {
  15. // initialize video server settings from global settings
  16. $settings = api_get_settings('Extra','list',api_get_current_access_url_id());
  17. $bbb_settings = array();
  18. foreach ($settings as $setting) {
  19. if (substr($setting['variable'],0,4)==='bbb_') {
  20. $bbb_settings[$setting['variable']] = $setting['selected_value'];
  21. }
  22. }
  23. $bbb_plugin = $bbb_settings['bbb_plugin'] === 'true';
  24. $bbb_host = $bbb_settings['bbb_plugin_host'];
  25. $bbb_salt = $bbb_settings['bbb_plugin_salt'];
  26. $course_code = api_get_course_id();
  27. $this->logout_url = api_get_path(WEB_COURSE_PATH).$course_code;
  28. if ($bbb_plugin) {
  29. $user_info = api_get_user_info();
  30. $this->user_complete_name = $user_info['complete_name'];
  31. $this->salt = $bbb_salt;
  32. $this->url = $bbb_host.'/bigbluebutton/';
  33. $this->table = Database::get_main_table('plugin_bbb_meeting');
  34. return true;
  35. }
  36. return false;
  37. }
  38. function create_meeting($params) {
  39. $params['c_id'] = api_get_course_int_id();
  40. $course_code = api_get_course_id();
  41. $attende_password = $params['attendee_pw'] = isset($params['moderator_pw']) ? $params['moderator_pw'] : api_get_course_id();
  42. $moderator_password = $params['moderator_pw'] = isset($params['moderator_pw']) ? $params['moderator_pw'] : api_get_course_id().'mod';
  43. $params['record'] = api_get_course_setting('big_blue_button_record_and_store', $course_code) == 1 ? true : false;
  44. $max = api_get_course_setting('big_blue_button_max_students_allowed', $course_code);
  45. $max = isset($max) ? $max : -1;
  46. $params['status'] = 1;
  47. if ($this->debug) error_log("enter create_meeting ".print_r($params, 1));
  48. $params['created_at'] = api_get_utc_datetime();
  49. $id = Database::insert($this->table, $params);
  50. if ($id) {
  51. if ($this->debug) error_log("create_meeting $id ");
  52. $meeting_name = isset($params['meeting_name']) ? $params['meeting_name'] : api_get_course_id();
  53. $welcome_msg = isset($params['welcome_msg']) ? $params['welcome_msg'] : null;
  54. $record = isset($params['record']) && $params['record'] ? 'true' : 'false';
  55. $duration = isset($params['duration']) ? intval($params['duration']) : 0;
  56. // ??
  57. $voiceBridge = 0;
  58. $metadata = array('maxParticipants' => $max);
  59. return $this->protocol.BigBlueButtonBN::createMeetingAndGetJoinURL(
  60. $this->user_complete_name, $meeting_name, $id, $welcome_msg, $moderator_password, $attende_password,
  61. $this->salt, $this->url, $this->logout_url, $record, $duration, $voiceBridge, $metadata
  62. );
  63. }
  64. }
  65. function is_meeting_exist($meeting_name) {
  66. $course_id = api_get_course_int_id();
  67. $meeting_data = Database::select('*', $this->table, array('where' => array('c_id = ? AND meeting_name = ? AND status = 1 ' => array($course_id, $meeting_name))), 'first');
  68. if ($this->debug) error_log("is_meeting_exist ".print_r($meeting_data,1));
  69. if (empty($meeting_data)) {
  70. return false;
  71. } else {
  72. return true;
  73. }
  74. }
  75. /**
  76. * @todo implement moderator pass
  77. */
  78. function join_meeting($meeting_name) {
  79. $pass = $this->get_user_metting_password();
  80. $meeting_data = Database::select('*', $this->table, array('where' => array('meeting_name = ? AND status = 1 ' => $meeting_name)), 'first');
  81. if (empty($meeting_data)) {
  82. if ($this->debug) error_log("meeting does not exist: $meeting_name ");
  83. return false;
  84. }
  85. $meeting_is_running = BigBlueButtonBN::isMeetingRunning($meeting_data['id'], $this->url, $this->salt);
  86. $meeting_info = BigBlueButtonBN::getMeetingInfoArray($meeting['id'], $pass, $this->url, $this->salt);
  87. $meeting_info_exists = false;
  88. if ($meeting_info['returncode'] != 'FAILED') {
  89. $meeting_info_exists = true;
  90. }
  91. $url = false;
  92. if ($this->debug) error_log("meeting is running".$meeting_is_running);
  93. if (isset($meeting_is_running) && $meeting_info_exists) {
  94. $url = $this->protocol.BigBlueButtonBN::joinURL($meeting_data['id'], $this->user_complete_name, $pass, $this->salt, $this->url);
  95. }
  96. if ($this->debug) error_log("return url :".$url);
  97. return $url;
  98. }
  99. /**
  100. * Gets all the course meetings saved in the plugin_bbb_meeting table
  101. * @return string
  102. */
  103. function get_course_meetings() {
  104. $pass = $this->get_user_metting_password();
  105. $meeting_list = Database::select('*', $this->table, array('where' => array('c_id = ? ' => api_get_course_int_id())));
  106. $new_meeting_list = array();
  107. foreach ($meeting_list as $meeting) {
  108. $item_meeting = $meeting;
  109. $item_meeting['info'] = BigBlueButtonBN::getMeetingInfoArray($meeting['id'], $pass, $this->url, $this->salt);
  110. if ($meeting['info']['returncode'] == 'FAILED') {
  111. } else {
  112. $item_meeting['end_url'] = api_get_self().'?action=end&id='.$meeting['id'];
  113. }
  114. $record_array = array();
  115. if ($meeting['record'] == 1) {
  116. $records = BigBlueButtonBN::getRecordingsArray($meeting['id'], $this->url, $this->salt);
  117. //var_dump($meeting['id']);
  118. if (!empty($records)) {
  119. $count = 1;
  120. foreach ($records as $record) {
  121. if (is_array($record) && isset($record['recordID']) && isset($record['playbacks'])) {
  122. //Fix the bbb timestamp
  123. //$record['startTime'] = substr($record['startTime'], 0, strlen($record['startTime']) -3);
  124. //$record['endTime'] = substr($record['endTime'], 0, strlen($record['endTime']) -3);
  125. //.' - '.api_convert_and_format_date($record['startTime']).' - '.api_convert_and_format_date($record['endTime'])
  126. foreach ($record['playbacks'] as $item) {
  127. $url = Display::url(get_lang('ViewRecord').' #'.$count, $item['url'], array('target' => '_blank'));
  128. //$url .= Display::url(get_lang('DeleteRecord'), api_get_self().'?action=delete_record&'.$record['recordID']);
  129. $url .= Display::url(get_lang('CopyToLinkTool'), api_get_self().'?action=copy_record_to_link_tool&id='.$meeting['id'].'&record_id='.$record['recordID']);
  130. //$url .= api_get_self().'?action=publish&id='.$record['recordID'];
  131. $count++;
  132. $record_array[] = $url;
  133. }
  134. }
  135. }
  136. }
  137. $item_meeting['show_links'] = implode('<br />', $record_array);
  138. }
  139. $item_meeting['created_at'] = api_convert_and_format_date($item_meeting['created_at']);
  140. //created_at
  141. $item_meeting['publish_url'] = api_get_self().'?action=publish&id='.$meeting['id'];
  142. $item_meeting['unpublish_url'] = api_get_self().'?action=unpublish&id='.$meeting['id'];
  143. if ($meeting['status'] == 1) {
  144. $item_meeting['go_url'] = $this->protocol.BigBlueButtonBN::joinURL($meeting['id'], $this->user_complete_name, $pass, $this->salt, $this->url);
  145. }
  146. $new_meeting_list[] = $item_meeting;
  147. }
  148. return $new_meeting_list;
  149. }
  150. function publish_meeting($id) {
  151. return BigBlueButtonBN::setPublishRecordings($id, 'true', $this->url, $this->salt);
  152. }
  153. function unpublish_meeting($id) {
  154. return BigBlueButtonBN::setPublishRecordings($id, 'false', $this->url, $this->salt);
  155. }
  156. function end_meeting($id) {
  157. $pass = $this->get_user_metting_password();
  158. BigBlueButtonBN::endMeeting($id, $pass, $this->url, $this->salt);
  159. Database::update($this->table, array('status' => 0), array('id = ? ' => $id));
  160. }
  161. function get_user_metting_password() {
  162. $teacher = api_is_course_admin() || api_is_coach() || api_is_platform_admin();
  163. if ($teacher) {
  164. return api_get_course_id().'mod';
  165. } else {
  166. return api_get_course_id();
  167. }
  168. }
  169. /**
  170. * Get users online in the current course room
  171. */
  172. function get_users_online_in_current_room() {
  173. $course_id = api_get_course_int_id();
  174. $meeting_data = Database::select('*', $this->table, array('where' => array('c_id = ? AND status = 1 ' => $course_id)), 'first');
  175. if (empty($meeting_data)) {
  176. return 0;
  177. }
  178. $pass = $this->get_user_metting_password();
  179. //$meeting_is_running = BigBlueButtonBN::isMeetingRunning($meeting_data['id'], $this->url, $this->salt);
  180. $info = BigBlueButtonBN::getMeetingInfoArray($meeting_data['id'], $pass, $this->url, $this->salt);
  181. if (!empty($info) && isset($info['participantCount'])) {
  182. return $info['participantCount'];
  183. }
  184. return 0;
  185. }
  186. /**
  187. * @todo
  188. */
  189. function delete_record($id) {
  190. }
  191. function copy_record_to_link_tool($id, $record_id) {
  192. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  193. $records = BigBlueButtonBN::getRecordingsArray($id, $this->url, $this->salt);
  194. if (!empty($records)) {
  195. foreach ($records as $record) {
  196. if ($record['recordID'] == $record_id) {
  197. if (is_array($record) && isset($record['recordID']) && isset($record['playbacks'])) {
  198. foreach ($record['playbacks'] as $item) {
  199. $link = new Link();
  200. $params['url'] = $item['url'];
  201. $params['title'] = 'bbb 1';
  202. $id = $link->save($params);
  203. return $id;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. return false;
  210. }
  211. function is_server_running() {
  212. return BigBlueButtonBN::isServerRunning($this->url);
  213. }
  214. }