Adding wami support if browser is not chrome. see BT#6613

1.9.x
Julio Montoya 12 years ago
parent d72b99a233
commit 68053abcb9
  1. 74
      main/inc/lib/wami-recorder/record_document.php
  2. 5
      main/newscorm/learnpath.class.php
  3. 34
      main/newscorm/lp_add_audio.php
  4. 154
      main/template/default/learnpath/record_voice.tpl

@ -26,7 +26,6 @@ if ($wamiuserid != api_get_user_id() || api_get_user_id() == 0 || $wamiuserid ==
die();
}
//clean
$waminame = Security::remove_XSS($waminame);
$waminame = Database::escape_string($waminame);
@ -34,9 +33,12 @@ $waminame = addslashes(trim($waminame));
$waminame = replace_dangerous_char($waminame, 'strict');
$waminame = disable_dangerous_file($waminame);
$wamidir = Security::remove_XSS($wamidir);
$content = file_get_contents('php://input');
if (empty($content)) {
exit;
}
//security extension
$ext = explode('.', $waminame);
$ext = strtolower($ext[sizeof($ext) - 1]);
@ -50,7 +52,7 @@ if ($ext != 'wav') {
$dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
$saveDir = $dirBaseDocuments.$wamidir;
$current_session_id = api_get_session_id();
$groupId = $_SESSION['_gid'];
$groupId = api_get_group_id();
//avoid duplicates
$waminame_to_save = $waminame;
@ -68,22 +70,58 @@ if (file_exists($saveDir.'/'.$waminame_noex.'.'.$ext)) {
$documentPath = $saveDir.'/'.$waminame_to_save;
//add to disk
// Add to disk
$fh = fopen($documentPath, 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);
//add document to database
$doc_id = add_document($_course, $wamidir.'/'.$waminame_to_save, 'file', filesize($documentPath), $title_to_save);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$doc_id,
'DocumentAdded',
$_user['user_id'],
$groupId,
null,
null,
null,
$current_session_id
);
$addToLP = false;
if (isset($_REQUEST['lp_item_id']) && !empty($_REQUEST['lp_item_id'])) {
$lpItemId = $_REQUEST['lp_item_id'];
$lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
if (!empty($lp)) {
$addToLP = true;
// Converts wav into mp3
require_once '../../../../vendor/autoload.php';
$ffmpeg = \FFMpeg\FFMpeg::create();
$oldWavFile = $documentPath;
if (file_exists($oldWavFile)) {
$video = $ffmpeg->open($documentPath);
$waminame_to_save = str_replace('wav', 'mp3', $waminame_to_save);
$documentPath = $saveDir.'/'.$waminame_to_save;
$title_to_save = $waminame_to_save;
$result = $video->save(new FFMpeg\Format\Audio\Mp3(), $documentPath);
if ($result) {
unlink($oldWavFile);
}
}
}
}
if (file_exists($documentPath)) {
// Add document to database
$newDocId = add_document($_course, $wamidir.'/'.$waminame_to_save, 'file', filesize($documentPath), $title_to_save);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$newDocId,
'DocumentAdded',
api_get_user_id(),
$groupId,
null,
null,
null,
$current_session_id
);
if ($addToLP) {
$lp->set_modified_on();
$lpItem = new learnpathItem($lpItemId);
$lpItem->add_audio_from_documents($newDocId);
}
}

@ -4922,7 +4922,8 @@ class learnpath {
public function return_new_tree($update_audio = 'false', $drop_element_here = false) {
$ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
echo '<script>
$return = '<script>
var newOrderData= "";
function processChildren(parentId) {
//Loop through the children of the UL element defined by the parentId
@ -5255,7 +5256,7 @@ class learnpath {
}
}
$return = '<div class="lp_tree well">';
$return .= '<div class="lp_tree well">';
$return .= '<ul id="lp_item_list">';
$return .='<h4>'.$this->name.'</h4><br>';

@ -78,14 +78,26 @@ if (empty($lp_item_id)) {
api_not_allowed();
}
$lp_item = new learnpathItem($lp_item_id);
$tpl = new Template($tool_name);
$form = new FormValidator('add_audio', 'post', api_get_self().'?action=add_audio&id='.$lp_item_id, null, array('enctype' => 'multipart/form-data'));
$suredel = trim(get_lang('AreYouSureToDelete'));
/* DISPLAY SECTION */
$tpl = new Template($tool_name);
$file = null;
$lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
$page = $_SESSION['oLP']->build_action_menu(true);
if (isset($lp_item->audio) && !empty($lp_item->audio)) {
$file = '../../courses/'.$_course['path'].'/document/audio/'.$lp_item->audio;
if (!file_exists($file)) {
$file = '../../courses/'.$_course['path'].'/document'.$lpPathInfo['dir'].$lp_item->audio;
}
}
$page = $_SESSION['oLP']->build_action_menu(true);
$page .= '<div class="row-fluid" style="overflow:hidden">';
$page .= '<div id="lp_sidebar" class="span4">';
$page .= $_SESSION['oLP']->return_new_tree(null, true);
@ -94,13 +106,11 @@ $page .= '</div>';
$page .= '<div id="doc_form" class="span8">';
$lp_item = new learnpathItem($lp_item_id);
$form = new FormValidator('add_audio', 'post', api_get_self().'?action=add_audio&id='.$lp_item_id, null, array('enctype' => 'multipart/form-data'));
$form->addElement('header', get_lang('RecordYourVoice'));
$tpl->assign('lp_item_id', $lp_item_id);
$tpl->assign('lp_dir', api_remove_trailing_slash($lpPathInfo['dir']));
$voiceContent = $tpl->fetch('default/learnpath/record_voice.tpl');
$form->addElement('html', $voiceContent);
@ -108,15 +118,13 @@ $form->addElement('header', get_lang('UplUpload'));
$form->addElement('html', $lp_item->get_title());
$form->addElement('file', 'file', get_lang('AudioFile'), 'style="width: 250px"');
if (!empty($file)) {
$form->addElement('checkbox', 'delete_file', null, get_lang('RemoveAudio'));
}
$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'));
$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;
}
if (!empty($file)) {
$audioPlayer = '<div id="preview">'.Display::getMediaPlayer($file)."</div>";
$form->addElement('label', get_lang('Preview'), $audioPlayer);
}

@ -1,36 +1,90 @@
<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 type="text/javascript" src="{{ _p.web_lib }}swfobject/swfobject.js"></script>
<script type="text/javascript" src="{{ _p.web_lib }}wami-recorder/recorder.js"></script>
<script type="text/javascript" src="{{ _p.web_lib }}wami-recorder/gui.js"></script>
<div id="rtc">
<audio id="audio" autoplay="" loop="" controls=""></audio>
<span id="progress-info"></span>
<br />
<a id="record" class="btn btn-danger" >{{ 'RecordAudio' | get_lang }}</a>
<button id="stop" class="btn" disabled="">{{ 'StopRecordingAudio' | get_lang }}</button>
</div>
<div id="wami" style="float:left">
<a id="record-wami" class="btn btn-info">{{ 'ActivateAudioRecorder' | get_lang }}</a>
<br />
<div id="wami-recorder">
</div>
<div id="start-recording" class="alert" style="display:none">
{{ "StartSpeaking" | get_lang }}
</div>
</div>
<script>
$(document).ready(function() {
function setupRecorder() {
Wami.setup({
id : "wami",
onReady : setupGUI,
swfUrl : "{{ _p.web_lib }}wami-recorder/Wami.swf"
});
$('#wami-recorder').css('margin-bottom', '150px');
}
function setupGUI() {
var uniq = 'rec_' + (new Date()).getTime() + ".wav";
var gui = new Wami.GUI({
id : "wami-recorder",
recordUrl : "{{ _p.web_lib }}wami-recorder/record_document.php?lp_item_id={{ lp_item_id }}&waminame="+uniq+"&wamidir={{ lp_dir }}&wamiuserid={{ _u.user_id }}",
//playUrl : "https://wami-recorder.appspot.com/audio",
buttonUrl : "{{ _p.web_lib }}wami-recorder/buttons.png",
buttonNoUrl: "{{ _p.web_img }}blank.gif",
onRecordStart : function() {
console.log('Record starts');
$('#start-recording').show();
},
onRecordFinish: function() {
$('#start-recording').hide();
window.location.reload();
},
onError : function() {
},
singleButton : true
});
gui.setPlayEnabled(false);
}
</script>
<script>
$(document).ready(function() {
var isChrome = navigator.webkitGetUserMedia;
//isChrome = false;
if (isChrome) {
$('#rtc').show();
$('#wami').hide();
} else {
$('#rtc').hide();
$('#wami').show();
var recordWami = $('#record-wami');
recordWami.on('click', function() {
setupRecorder();
return false;
});
}
var format = 'webm'; // or wav
var record = document.getElementById('record');
var record = $('#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) {
function postBlob(blob, fileType, fileName) {
// FormData
var formData = new FormData();
formData.append(fileType + '-filename', fileName);
@ -69,33 +123,46 @@
});
}
var recordAudio, recordVideo;
record.on('click', function() {
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;
var myURL = (window.URL || window.webkitURL || {});
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true,
video: false
},
function(stream) {
if (window.IsChrome) stream = new window.MediaStream(stream.getAudioTracks());
preview.src = myURL.createObjectURL(stream);
console.log(preview.src);
preview.play();
recordAudio = RecordRTC(stream, {
type: 'audio'
});
recordAudio.startRecording();
/*recordVideo = RecordRTC(stream, {
type: 'video'
});*/
//recordVideo.startRecording();
stop.disabled = false;
},
function(err) {
console.log("The following error occured: " + err);
return false;
});
};
}
});
stop.onclick = function() {
record.disabled = false;
@ -115,6 +182,3 @@
});
</script>

Loading…
Cancel
Save