Use WamiRecorder instead of Nanogong - refs #10793

ofaj
Angel Fernando Quiroz Campos 9 years ago
parent edce932baa
commit cdd7fb2cf5
  1. 13
      main/exercice/exercise.class.php
  2. BIN
      main/inc/ajax/Wami.swf
  3. 28
      main/inc/ajax/wamirecorder.ajax.php
  4. 386
      main/inc/lib/WamiRecorder.php
  5. 26
      main/inc/lib/exercise.lib.php
  6. 2
      main/inc/lib/exercise_show_functions.lib.php

@ -3186,6 +3186,7 @@ class Exercise
}
$nano = null;
$wamiRecorder = null;
if ($answerType == ORAL_EXPRESSION) {
$exe_info = Event::get_exercise_results_by_attempt($exeId);
@ -3200,6 +3201,14 @@ class Exercise
$params['exe_id'] = isset($exe_info['exe_id']) ? $exe_info['exe_id'] : $exeId;
$nano = new Nanogong($params);
$wamiRecorder = new WamiRecorder(
api_get_course_int_id(),
api_get_session_id(),
isset($exe_info['exe_user_id']) ? $exe_info['exe_user_id'] : api_get_user_id(),
isset($exe_info['exe_exo_id']) ? $exe_info['exe_exo_id'] : $this->id,
$questionId,
isset($exe_info['exe_id']) ? $exe_info['exe_id'] : $exeId
);
//probably this attempt came in an exercise all question by page
if ($feedback_type == 0) {
@ -4186,7 +4195,7 @@ class Exercise
$choice,
0,
0,
$nano);
$wamiRecorder);
//}
} elseif ($answerType == HOT_SPOT) {
//if ($origin != 'learnpath') {
@ -4518,7 +4527,7 @@ class Exercise
$choice,
$exeId,
$questionId,
$nano
$wamiRecorder
) . '</td>
</tr>
</table>';

Binary file not shown.

@ -0,0 +1,28 @@
<?php
/* For license terms, see /license.txt */
/**
* AJAX Call for wamirecorder
*/
require_once '../global.inc.php';
$action = $_REQUEST['a'];
//$js_path = api_get_path(WEB_LIBRARY_PATH) . 'javascript/';
$courseId = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : 0;
$sessionId = isset($_REQUEST['session_id']) ? $_REQUEST['session_id'] : 0;
$userId = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : 0;
$exerciseId = isset($_REQUEST['exercise_id']) ? $_REQUEST['exercise_id'] : 0;
$questionId = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : 0;
$exeId = isset($_REQUEST['exe_id']) ? $_REQUEST['exe_id'] : 0;
$wamiRecorder = new WamiRecorder($courseId, $sessionId, $userId, $exerciseId, $questionId, $exeId);
switch ($action) {
case 'show_form':
api_protect_course_script(true);
Display::display_reduced_header();
$wamiRecorder->showJS();
$wamiRecorder->showForm();
break;
}

@ -0,0 +1,386 @@
<?php
/* For license terms, see /license.txt */
class WamiRecorder
{
public $courseInfo;
public $userId;
public $exeId;
public $exerciseId;
public $questionId;
public $courseId;
public $sessionId;
public $storePath;
public $fileName;
public $filePath;
public $canEdit;
/**
* WamiRecorder constructor.
* @param int $courseId
* @param int $sessionId
* @param int $userId
* @param int $exerciseId
* @param int $questionId
* @param int $exeId
*/
public function __construct($courseId = 0, $sessionId = 0, $userId = 0, $exerciseId = 0, $questionId = 0, $exeId = 0)
{
if (!empty($courseId)) {
$this->courseId = intval($courseId);
} else {
$this->courseId = api_get_course_int_id();
}
$this->courseInfo = api_get_course_info_by_id($this->courseId);
if (!empty($sessionId)) {
$this->sessionId = intval($sessionId);
} else {
$this->sessionId = api_get_session_id();
}
if (!empty($userId)) {
$this->userId = intval($userId);
} else {
$this->userId = api_get_user_id();
}
$this->exerciseId = 0;
if (!empty($exerciseId)) {
$this->exerciseId = intval($exerciseId);
}
$this->questionId = 0;
if (!empty($questionId)) {
$this->questionId = intval($questionId);
}
$this->canEdit = false;
if (api_is_allowed_to_edit()) {
$this->canEdit = true;
} else {
if ($this->userId == api_get_user_id()) {
$this->canEdit = true;
}
}
$this->exeId = intval($exeId);
$this->storePath = $this->generateDirectory();
$this->fileName = $this->generateFileName();
$this->filePath = $this->storePath . $this->fileName;
}
/**
* Generate the file name
* @return string
*/
private function generateFileName()
{
return implode(
'-',
array(
$this->courseId,
$this->sessionId,
$this->userId,
$this->exerciseId,
$this->questionId,
$this->exeId
)
);
}
/**
* Generate the necessary directory for audios. If them not exists, are created
* @return string
*/
private function generateDirectory()
{
$this->storePath = api_get_path(SYS_COURSE_PATH) . $this->courseInfo['path'] . '/exercises/';
if (!is_dir($this->storePath)) {
mkdir($this->storePath);
}
if (!is_dir($this->storePath . $this->sessionId)) {
mkdir($this->storePath . $this->sessionId);
}
if (!empty($this->exerciseId) && !is_dir($this->storePath . $this->sessionId . '/' . $this->exerciseId)) {
mkdir($this->storePath . $this->sessionId . '/' . $this->exerciseId);
}
if (!empty($this->questionId) && !is_dir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->questionId)) {
mkdir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->questionId);
}
if (!empty($this->userId) && !is_dir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->questionId . '/' . $this->userId)) {
mkdir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->questionId . '/' . $this->userId);
}
return $this->storePath .= implode(
'/',
array(
$this->sessionId,
$this->exerciseId,
$this->questionId,
$this->userId
)
) . '/';
}
/**
* Generate a relative directory path
* @return string
*/
private function generateRelativeDirectory()
{
return implode(
'/',
array(
$this->sessionId,
$this->exerciseId,
$this->questionId,
$this->userId
)
);
}
/**
* Get necessary params for Flash
* @return string
*/
private function getParams()
{
return http_build_query(array(
'course_id' => $this->courseId,
'session_id' => $this->sessionId,
'exercise_id' => $this->exerciseId,
'exe_id' => $this->exeId,
'question_id' => $this->questionId,
'user_id' => $this->userId
));
}
/**
* Print the JavaScript for Wami
*/
public function showJS()
{
$wamidir = '/../exercises/' . $this->generateRelativeDirectory() . '/';
$wamiurlplay = api_get_path(WEB_COURSE_PATH) . api_get_course_path($this->courseInfo['code']) . '/exercises/' . $this->generateRelativeDirectory();
$wamiuserid = $this->userId;
echo '
<!-- swfobject is a commonly used library to embed Flash content https://ajax.googleapis.com/ajax/libs/swfobject/2.2/ -->
<script type="text/javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'swfobject/swfobject.js"></script>
<!-- Setup the recorder interface -->
<script type="text/javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'wami-recorder/recorder.js"></script>
<!-- GUI code... take it or leave it -->
<script type="text/javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'wami-recorder/gui.js"></script>
<script type="text/javascript">
function newNameRecord() {
location.reload(true)
}
function setupRecorder() {
document.getElementById(\'audio_button\').style.display = \'none\';
Wami.setup({
id : \'wami\',
onReady : setupGUI
});
}
function setupGUI() {
var waminame = \'' . $this->fileName . '.wav\';
var waminame_play=waminame;
var gui = new Wami.GUI({
id : \'wami\',
singleButton : true,
recordUrl: \'' . api_get_path(WEB_LIBRARY_PATH) . 'wami-recorder/record_document.php?waminame=\' + waminame + \'&wamidir=' . $wamidir . '&wamiuserid=' . $wamiuserid . '\',
playUrl: \'' . $wamiurlplay . '\' + waminame_play,
buttonUrl: \'' . api_get_path(WEB_LIBRARY_PATH) . 'wami-recorder/buttons.png\',
swfUrl: \'' . api_get_path(WEB_LIBRARY_PATH) . 'wami-recorder/Wami.swf\'
});
gui.setPlayEnabled(false);
}
</script>
';
}
/**
* Show the form controls to Wami
*/
public function showForm()
{
echo '
<div id="wami" style="padding-top:10px;"></div>
<div align="center" style="padding-top:150px;">
<form name="form_wami_recorder">
<button type="button" value="" onclick="setupRecorder()" id="audio_button" />' . get_lang('Activate') . '</button>
<button type="button" value="" onclick="newNameRecord()" id="new_name" />' . get_lang('Reload') . '</button>
</form>
</div>
';
}
/**
* Get a HTML button for record answer
* @return string
*/
public function getButton()
{
$params_strings = $this->getParams();
$params_strings .= '&a=show_form';
$html = "
<script>
$(document).on('ready', function () {
$('#btn-record_answer').on('click', function (e) {
e.preventDefault();
var url = '" . api_get_path(WEB_AJAX_PATH) . "wamirecorder.ajax.php?" . $params_strings . "',
iframe = $('<iframe>').attr({
hspace: 0,
src: url,
frameborder: 0,
width: '100%',
height: 400
});
var modalDialog = $('#global-modal').find('.modal-dialog'),
modalTitle = '" . get_lang('RecordAnswer') ."';
modalDialog
.removeClass('modal-lg modal-sm')
.css('width', '500px')
$('#global-modal').find('.modal-title').text(modalTitle);
$('#global-modal').find('.modal-body').html(iframe);
$('#global-modal').modal('show');
});
});
</script>
";
$html .= '<br />'
. Display::toolbarButton(
get_lang('RecordAnswer'),
api_get_path(WEB_AJAX_PATH) . 'wamirecorder.ajax.php?' . $params_strings . http_build_query([
'a' => 'show_form',
'TB_iframe' => 'true',
'data-height' => 400,
'data-width' => 500
]),
'microphone',
'primary',
['id' => 'btn-record_answer']
)
. '<br /><br />' . Display::return_message(get_lang('UseTheMessageBelowToAddSomeComments'));
return $html;
}
/**
* Show the audio file
* @return null|string
*/
public function showAudioFile()
{
$filePath = $this->loadFileIfExists();
if (empty($filePath)) {
return null;
}
$url = $this->getPublicURL();
$params = array(
'url' => $url,
'extension' => 'wav',
'count' => 1
);
$jquery = DocumentManager::generate_jplayer_jquery($params);
$jsPath = api_get_path(WEB_LIBRARY_PATH) . 'javascript/';
$actions = Display::url(Display::return_icon('save.png', get_lang('Download'), array(), ICON_SIZE_SMALL), $url, array('target' => '_blank'));
$html = '<link rel="stylesheet" href="' . $jsPath . 'jquery-jplayer/skins/blue/jplayer.blue.monday.css" type="text/css">';
//$html .= '<link rel="stylesheet" href="' . $jsPath . 'jquery-jplayer/skins/chamilo/jplayer.blue.monday.css" type="text/css">';
$html .= '<script type="text/javascript" src="' . $jsPath . 'jquery-jplayer/jquery.jplayer.min.js"></script>';
$html .= '<div class="nanogong_player"></div>';
$html .= '<br /><div class="action_player">' . $actions . '</div><br /><br /><br />';
$html .= '<script>
$(document).ready( function() {
//Experimental changes to preview mp3, ogg files
' . $jquery . '
});
</script>';
$html .= DocumentManager::generate_media_preview(1, 'advanced');
return $html;
}
/**
* Get the audio file
* @param bool $loadFromDatabase
* @return null|string
*/
public function loadFileIfExists($loadFromDatabase = false)
{
$fileName = $this->fileName . '.wav';
//temp_exe
if ($loadFromDatabase) {
//Load the real filename just if exists
if (isset($this->exeId) && isset($this->userId) && isset($this->questionId) && isset($this->sessionId) && isset($this->courseId)) {
$attempt_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$sql = "SELECT filename FROM $attempt_table
WHERE exe_id = " . $this->exeId . " AND
user_id = " . $this->userId . " AND
question_id = " . $this->questionId . " AND
session_id = " . $this->sessionId . " AND
course_code = '" . $this->courseInfo['code'] . "' LIMIT 1";
$result = Database::query($sql);
$result = Database::fetch_row($result, 'ASSOC');
if (isset($result) && isset($result[0]) && !empty($result[0])) {
$fileName = $result[0];
}
}
}
if (is_file($this->storePath . $fileName)) {
return $this->storePath . $fileName;
}
return null;
}
/**
* Get a public URL
* @return string
*/
public function getPublicURL()
{
$url = api_get_path(WEB_COURSE_PATH) . $this->courseInfo['path'] . '/exercises/';
$url .= $this->generateRelativeDirectory() . '/';
$url .= $this->fileName . '.wav';
return $url;
}
}

@ -171,20 +171,26 @@ class ExerciseLib
global $exercise_stat_info, $exerciseId, $exe_id;
if (!empty($exercise_stat_info)) {
$params = array(
'exercise_id' => $exercise_stat_info['exe_exo_id'],
'exe_id' => $exercise_stat_info['exe_id'],
'question_id' => $questionId
$wamiRecorder = new WamiRecorder(
0,
0,
0,
$exercise_stat_info['exe_exo_id'],
$questionId,
$exercise_stat_info['exe_id']
);
} else {
$params = array(
'exercise_id' => $exerciseId,
'exe_id' => 'temp_exe',
'question_id' => $questionId
$wamiRecorder = new WamiRecorder(
0,
0,
0,
$exerciseId,
$questionId,
'temp_exe'
);
}
$nano = new Nanogong($params);
echo $nano->show_button();
echo $wamiRecorder->getButton();
}
$form = new FormValidator('free_choice_'.$questionId);

@ -120,7 +120,7 @@ class ExerciseShowFunctions
public static function display_oral_expression_answer($feedback_type, $answer, $id, $questionId, $nano = null)
{
if (isset($nano)) {
echo $nano->show_audio_file();
echo $nano->showAudioFile();
}
if (empty($id)) {

Loading…
Cancel
Save