Adding audio recording using RTC see BT#6613

1.9.x
Julio Montoya 11 years ago
parent 594bbff9a5
commit 5f4c961ce2
  1. 62
      main/inc/ajax/lp.ajax.php
  2. 21
      main/inc/lib/display.lib.php
  3. 475
      main/inc/lib/javascript/rtc/RecordRTC.js
  4. 37
      main/newscorm/learnpath.class.php
  5. 65
      main/newscorm/lp_add_audio.php
  6. 1
      main/template/default/learnpath/lp_upload_audio.tpl
  7. 120
      main/template/default/learnpath/record_voice.tpl

@ -27,7 +27,7 @@ switch ($action) {
$sections = explode('^', $new_order);
$new_array = array();
$i = 0;
foreach ($sections as $items) {
if (!empty($items)) {
list($id, $parent_id) = explode('|', $items);
@ -54,8 +54,66 @@ switch ($action) {
}
Display::display_confirmation_message(get_lang('Saved'));
}
break;
case 'record_audio':
if (api_is_allowed_to_edit(null, true) == false) {
exit;
}
/** @var Learnpath $lp */
$lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
$course_info = api_get_course_info();
$lpPathInfo = $lp->generate_lp_folder($course_info);
if (empty($lpPathInfo)) {
exit;
}
require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
require_once api_get_path(LIBRARY_PATH).'document.lib.php';
require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
foreach (array('video', 'audio') as $type) {
if (isset($_FILES["${type}-blob"])) {
$fileName = $_POST["${type}-filename"];
//$file = $_FILES["${type}-blob"]["tmp_name"];
$file = $_FILES["${type}-blob"];
$fileInfo = pathinfo($fileName);
$file['name'] = 'rec_'.date('Y-m-d H:i:s').'_'.uniqid().'.'.$fileInfo['extension'];
$file['file'] = $file;
$lpPathInfo['dir'] = api_remove_trailing_slash($lpPathInfo['dir']);
$result = DocumentManager::upload_document(
$file,
$lpPathInfo['dir'],
$file['name'],
null,
0,
'overwrite',
false,
false
);
if (!empty($result) && is_array($result)) {
$newDocId = $result['id'];
$courseId = $result['c_id'];
$lp->set_modified_on();
$lpItem = new learnpathItem($_REQUEST['lp_item_id']);
$lpItem->add_audio_from_documents($newDocId);
$data = DocumentManager::get_document_data_by_id($newDocId, $course_info['code']);
echo $data['document_url'];
exit;
}
}
}
break;
default:
echo '';
}
exit;
exit;

@ -1550,4 +1550,25 @@ class Display {
</div> </div>';
return $html;
}
public static function getMediaPlayer($file, $params = array())
{
$fileInfo = pathinfo($file);
switch ($fileInfo['extension']) {
case 'mp3':
case 'webm':
$autoplay = null;
if (isset($params['autoplay']) && $params['autoplay'] == 'true') {
$autoplay = 'autoplay';
}
$width = isset($params['width']) ? 'width="'.$params['width'].'"' : null;
$html = '<audio id="'.$fileInfo['basename'].'" controls '.$autoplay.' '.$width.' src="'.$file.'" />';
return $html;
break;
}
return null;
}
} //end class Display

File diff suppressed because one or more lines are too long

@ -1995,15 +1995,16 @@ class learnpath {
* Returns the HTML necessary to print a mediaplayer block inside a page
* @return string The mediaplayer HTML
*/
public function get_mediaplayer($autostart='true') {
public function get_mediaplayer($autostart = 'true')
{
$course_id = api_get_course_int_id();
global $_course;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
// Getting all the information about the item.
$sql = "SELECT * FROM " . $tbl_lp_item . " as lp INNER JOIN " . $tbl_lp_item_view . " as lp_view on lp.id = lp_view.lp_item_id " .
"WHERE lp.id = '" . $_SESSION['oLP']->current . "' AND
$sql = "SELECT * FROM ".$tbl_lp_item." as lp INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id ".
"WHERE lp.id = '".$_SESSION['oLP']->current."' AND
lp.c_id = $course_id AND
lp_view.c_id = $course_id";
$result = Database::query($sql);
@ -2031,20 +2032,27 @@ class learnpath {
$autostart_audio = 'true';
}
$courseInfo = api_get_course_info();
$audio = $row['audio'];
$file = '../../courses/'.$courseInfo['path'].'/document/audio/'.$audio;
if (!file_exists($file)) {
$lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
$file = '../../courses/'.$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
}
$player = Display::getMediaPlayer($file, array('autoplay' => $autostart_audio, 'width' => '90%'));
// The mp3 player.
$output = '<div id="container">';
$output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
$output .= '<script type="text/javascript">
var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=' . $autostart_audio.'");
s1.write("container");
</script>
</div>';
$output .= $player;
$output .= '</div>';
}
return $output;
}
/**
* This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
* @param int Learnpath id
@ -5304,7 +5312,8 @@ class learnpath {
* This function builds the action menu
* @return void
*/
public function build_action_menu() {
public function build_action_menu($returnContent = false)
{
$is_allowed_to_edit = api_is_allowed_to_edit(null,true);
$gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
@ -5334,6 +5343,10 @@ class learnpath {
);
$return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
$return .= '</div>';
if ($returnContent) {
return $return;
}
echo $return;
}

@ -13,8 +13,9 @@ $this_section = SECTION_COURSES;
api_protect_course_script();
include 'learnpath_functions.inc.php';
include 'resourcelinker.inc.php';
require_once 'learnpath_functions.inc.php';
require_once 'resourcelinker.inc.php';
$is_allowed_to_edit = api_is_allowed_to_edit(null, true);
@ -45,9 +46,9 @@ if (isset($_SESSION['gradebook'])) {
if (!empty($gradebook) && $gradebook == 'view') {
$interbreadcrumb[] = array (
'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
'name' => get_lang('ToolGradebook')
);
'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
'name' => get_lang('ToolGradebook')
);
}
$interbreadcrumb[] = array('url' => 'lp_controller.php?action=list', 'name' => get_lang('LearningPaths'));
@ -66,7 +67,7 @@ switch ($type) {
break;
}
if ($action == 'add_item' && $type == 'document' ) {
if ($action == 'add_item' && $type == 'document') {
$interbreadcrumb[]= array ('url' => '#', 'name' => get_lang('NewDocumentCreated'));
}
@ -77,21 +78,21 @@ if (empty($lp_item_id)) {
api_not_allowed();
}
Display::display_header(null, 'Path');
$suredel = trim(get_lang('AreYouSureToDelete'));
/* DISPLAY SECTION */
echo $_SESSION['oLP']->build_action_menu();
$tpl = new Template($tool_name);
$page = $_SESSION['oLP']->build_action_menu(true);
echo '<div class="row-fluid" style="overflow:hidden">';
echo '<div id="lp_sidebar" class="span4">';
echo $_SESSION['oLP']->return_new_tree(null, true);
$page .= '<div class="row-fluid" style="overflow:hidden">';
$page .= '<div id="lp_sidebar" class="span4">';
$page .= $_SESSION['oLP']->return_new_tree(null, true);
// Show the template list.
echo '</div>';
$page .= '</div>';
echo '<div id="doc_form" class="span8">';
$page .= '<div id="doc_form" class="span8">';
$lp_item = new learnpathItem($lp_item_id);
@ -99,7 +100,10 @@ $form = new FormValidator('add_audio', 'post', api_get_self().'?action=add_audio
$form->addElement('header', get_lang('RecordYourVoice'));
$tpl->assign('lp_item_id', $lp_item_id);
$voiceContent = $tpl->fetch('default/learnpath/record_voice.tpl');
$form->addElement('html', $voiceContent);
$form->addElement('header', get_lang('UplUpload'));
$form->addElement('html', $lp_item->get_title());
@ -108,28 +112,29 @@ $form->addElement('hidden', 'id', $lp_item_id);
if (isset($lp_item->audio) && !empty($lp_item->audio)) {
$form->addElement('checkbox', 'delete_file', null, get_lang('RemoveAudio'));
$player = '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
$player .= '<div id="preview"></div>
<script>
var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $lp_item->audio . '");
s1.write("preview");
</script>';
$form->addElement('label', get_lang('Preview'), $player);
$file = '../../courses/'.$_course['path'].'/document/audio/'.$lp_item->audio;
if (!file_exists($file)) {
$lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
$file = '../../courses/'.$_course['path'].'/document'.$lpPathInfo['dir'].$lp_item->audio;
}
$audioPlayer = '<div id="preview">'.Display::getMediaPlayer($file)."</div>";
$form->addElement('label', get_lang('Preview'), $audioPlayer);
}
$form->addElement('button', 'submit', get_lang('Edit'));
$course_info = api_get_course_info();
$document_tree = DocumentManager::get_document_preview($course_info, null, null, 0, false, '/audio', 'lp_controller.php?action=add_audio&id='.$lp_item_id);
$form->display();
$page .= $form->return_form();
$page .= '<legend>'.get_lang('SelectAnAudioFileFromDocuments').'</legend>';
$page .= $document_tree;
$page .= '</div>';
echo '<legend>'.get_lang('SelectAnAudioFileFromDocuments').'</legend>';
echo $document_tree;
echo '</div>';
$page .= '</div>';
echo '</div>';
Display::display_footer();
$tpl->assign('content', $page);
$content = $tpl->fetch('default/learnpath/lp_upload_audio.tpl');
$tpl->display_one_col_template();

@ -0,0 +1,120 @@
<script type="text/javascript" src="{{ _p.web_lib }}javascript/rtc/RecordRTC.js"></script>
<audio id="audio" autoplay="" loop="" controls=""></audio>
<span id="progress-info"></span>
<br />
<button id="record" class="btn btn-danger" >Record Audio</button>
<button id="stop" class="btn" disabled="">Stop Recording Audio</button>
<script>
$(document).ready(function() {
var format = 'webm'; // or wav
var record = document.getElementById('record');
var stop = document.getElementById('stop');
var preview = document.getElementById('audio');
var progressInfo = document.getElementById('progress-info');
var previewBlock = document.getElementById('preview');
function xhr(url, data, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
callback(location.href + request.responseText);
}
};
request.open('POST', url);
request.send(data);
}
function postBlob(blob, fileType, fileName) {
// FormData
var formData = new FormData();
formData.append(fileType + '-filename', fileName);
formData.append(fileType + '-blob', blob);
// progress-bar
var hr = document.createElement('hr');
progressInfo.appendChild(hr);
var strong = document.createElement('strong');
strong.innerHTML = fileType + ' upload progress: ';
progressInfo.appendChild(strong);
var progress = document.createElement('progress');
progressInfo.appendChild(progress);
// POST the Blob
$.ajax({
url:'{{ _p.web_ajax }}lp.ajax.php?a=record_audio&lp_item_id={{ lp_item_id }}',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success:function(fileURL) {
progressInfo.appendChild(document.createElement('hr'));
var mediaElement = document.createElement(fileType);
mediaElement.src = fileURL;
mediaElement.controls = true;
var uniq = 'id' + (new Date()).getTime();
mediaElement.id = uniq;
$(previewBlock).html('');
previewBlock.appendChild(mediaElement);
mediaElement.play();
$(progressInfo).html('');
}
});
}
var recordAudio, recordVideo;
record.onclick = function() {
record.disabled = true;
var video_constraints = {
mandatory: { },
optional: []
};
navigator.webkitGetUserMedia({
audio: true,
video: video_constraints
}, function(stream) {
preview.src = (window.webkitURL || window.URL).createObjectURL(stream);
preview.play();
recordAudio = RecordRTC(stream);
recordAudio.startRecording();
/*recordVideo = RecordRTC(stream, {
type: 'video'
});*/
//recordVideo.startRecording();
stop.disabled = false;
});
};
stop.onclick = function() {
record.disabled = false;
stop.disabled = true;
var fileName = Math.round(Math.random() * 99999999) + 99999999;
recordAudio.stopRecording();
postBlob(recordAudio.getBlob(), 'audio', fileName + '.' + format);
//recordVideo.stopRecording();
//PostBlob(recordVideo.getBlob(), 'video', fileName + '.webm');
preview.src = '';
};
});
</script>
Loading…
Cancel
Save