oral_expression.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class OralExpression
  5. * This class allows to instantiate an object of type FREE_ANSWER,
  6. * extending the class question
  7. * @author Eric Marguin
  8. *
  9. * @package chamilo.exercise
  10. */
  11. class OralExpression extends Question
  12. {
  13. public static $typePicture = 'audio_question.png';
  14. public static $explanationLangVar = 'OralExpression';
  15. private $sessionId;
  16. private $userId;
  17. private $exerciseId;
  18. private $exeId;
  19. private $storePath;
  20. private $fileName;
  21. private $filePath;
  22. public $available_extensions = array('wav', 'ogg');
  23. /**
  24. * Constructor
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->type = ORAL_EXPRESSION;
  30. $this->isContent = $this->getIsContent();
  31. }
  32. /**
  33. * function which redefine Question::createAnswersForm
  34. * @param FormValidator $form
  35. */
  36. function createAnswersForm($form)
  37. {
  38. $form -> addElement('text','weighting', get_lang('Weighting'), array('class' => 'span1'));
  39. global $text, $class;
  40. // setting the save button here and not in the question class.php
  41. $form->addButtonSave($text, 'submitQuestion');
  42. if (!empty($this->id)) {
  43. $form -> setDefaults(array('weighting' => float_format($this->weighting, 1)));
  44. } else {
  45. if ($this -> isContent == 1) {
  46. $form -> setDefaults(array('weighting' => '10'));
  47. }
  48. }
  49. }
  50. /**
  51. * abstract function which creates the form to create / edit the answers of the question
  52. * @param the FormValidator $form
  53. */
  54. function processAnswersCreation($form)
  55. {
  56. $this->weighting = $form->getSubmitValue('weighting');
  57. $this->save();
  58. }
  59. /**
  60. * @param null $feedback_type
  61. * @param null $counter
  62. * @param null $score
  63. * @return null|string
  64. */
  65. function return_header($feedback_type = null, $counter = null, $score = null)
  66. {
  67. $header = parent::return_header($feedback_type, $counter, $score);
  68. $header .= '<table class="'.$this->question_table_class.'">
  69. <tr>
  70. <th>&nbsp;</th>
  71. </tr>
  72. <tr>
  73. <th>'.get_lang("Answer").'</th>
  74. </tr>
  75. <tr>
  76. <th>&nbsp;</th>
  77. </tr>';
  78. return $header;
  79. }
  80. /**
  81. * initialize the attributes to generate the file path
  82. * @param $sessionId integer
  83. * @param $userId integer
  84. * @param $exerciseId integer
  85. * @param $exeId integer
  86. */
  87. public function initFile($sessionId, $userId, $exerciseId, $exeId)
  88. {
  89. $this->sessionId = intval($sessionId);
  90. $this->userId = intval($userId);
  91. $this->exerciseId = 0;
  92. $this->exeId = intval($exeId);
  93. if (!empty($exerciseId)) {
  94. $this->exerciseId = intval($exerciseId);
  95. }
  96. $this->storePath = $this->generateDirectory();
  97. $this->fileName = $this->generateFileName();
  98. $this->filePath = $this->storePath . $this->fileName;
  99. }
  100. /**
  101. * Generate the necessary directory for audios. If them not exists, are created
  102. * @return string
  103. */
  104. private function generateDirectory()
  105. {
  106. $this->storePath = api_get_path(SYS_COURSE_PATH) . $this->course['path'] . '/exercises/';
  107. if (!is_dir($this->storePath)) {
  108. mkdir($this->storePath);
  109. }
  110. if (!is_dir($this->storePath . $this->sessionId)) {
  111. mkdir($this->storePath . $this->sessionId);
  112. }
  113. if (!empty($this->exerciseId) && !is_dir($this->storePath . $this->sessionId . '/' . $this->exerciseId)) {
  114. mkdir($this->storePath . $this->sessionId . '/' . $this->exerciseId);
  115. }
  116. if (!empty($this->id) && !is_dir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->id)) {
  117. mkdir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->id);
  118. }
  119. if (!empty($this->userId) && !is_dir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->id . '/' . $this->userId)) {
  120. mkdir($this->storePath . $this->sessionId . '/' . $this->exerciseId . '/' . $this->id . '/' . $this->userId);
  121. }
  122. $params = [
  123. $this->sessionId,
  124. $this->exerciseId,
  125. $this->id,
  126. $this->userId,
  127. ];
  128. $this->storePath .= implode('/', $params).'/';
  129. return $this->storePath;
  130. }
  131. /**
  132. * Generate the file name
  133. * @return string
  134. */
  135. private function generateFileName()
  136. {
  137. return implode(
  138. '-',
  139. [
  140. $this->course['real_id'],
  141. $this->sessionId,
  142. $this->userId,
  143. $this->exerciseId,
  144. $this->id,
  145. $this->exeId
  146. ]
  147. );
  148. }
  149. /**
  150. * Generate a relative directory path
  151. * @return string
  152. */
  153. private function generateRelativeDirectory()
  154. {
  155. $params = [
  156. $this->sessionId,
  157. $this->exerciseId,
  158. $this->id,
  159. $this->userId,
  160. ];
  161. $path = implode('/', $params);
  162. $directory = '/exercises/'.$path.'/';
  163. return $directory;
  164. }
  165. /**
  166. * Return the HTML code to show the RecordRTC/Wami recorder
  167. * @return string
  168. */
  169. public function returnRecorder()
  170. {
  171. $directory = '/..' . $this->generateRelativeDirectory();
  172. $recordAudioView = new Template('', false, false,false, false, false, false);
  173. $recordAudioView->assign('directory', $directory);
  174. $recordAudioView->assign('user_id', $this->userId);
  175. $recordAudioView->assign('file_name', $this->fileName);
  176. $template = $recordAudioView->get_template('exercise/oral_expression.tpl');
  177. return $recordAudioView->fetch($template);
  178. }
  179. /**
  180. * Get the absolute file path. Return null if the file doesn't exists
  181. * @param bool $loadFromDatabase
  182. * @return string
  183. */
  184. public function getAbsoluteFilePath($loadFromDatabase = false)
  185. {
  186. $fileName = $this->fileName;
  187. if ($loadFromDatabase) {
  188. $em = Database::getManager();
  189. //Load the real filename just if exists
  190. if (isset($this->exeId, $this->userId, $this->id, $this->sessionId, $this->course['real_id'])) {
  191. $result = $em
  192. ->getRepository('ChamiloCoreBundle:TrackEAttempt')
  193. ->findOneBy([
  194. 'exeId' => $this->exeId,
  195. 'userId' => $this->userId,
  196. 'questionId' => $this->id,
  197. 'sessionId' => $this->sessionId,
  198. 'cId' => $this->course['real_id']
  199. ]);
  200. if (!$result) {
  201. return '';
  202. }
  203. $fileName = $result->getFilename();
  204. if (empty($fileName)) {
  205. return '';
  206. }
  207. return $this->storePath . $result->getFilename();
  208. }
  209. }
  210. foreach ($this->available_extensions as $extension) {
  211. $file = "{$this->storePath}$fileName.$extension";
  212. if (!is_file($file)) {
  213. continue;
  214. }
  215. return $file;
  216. }
  217. return '';
  218. }
  219. /**
  220. * Get the URL for the audio file. Return null if the file doesn't exists
  221. * @param bool $loadFromDatabase
  222. *
  223. * @return string
  224. */
  225. public function getFileUrl($loadFromDatabase = false)
  226. {
  227. $filePath = $this->getAbsoluteFilePath($loadFromDatabase);
  228. if (empty($filePath)) {
  229. return null;
  230. }
  231. return str_replace(
  232. api_get_path(SYS_COURSE_PATH),
  233. api_get_path(WEB_COURSE_PATH),
  234. $filePath
  235. );
  236. }
  237. /**
  238. * Tricky stuff to deal with the feedback = 0 in exercises (all question per page)
  239. * @param $exe_id integer
  240. */
  241. public function replaceWithRealExe($exe_id)
  242. {
  243. $filename = null;
  244. //ugly fix
  245. foreach ($this->available_extensions as $extension) {
  246. $items = explode('-', $this->fileName);
  247. $items[5] = 'temp_exe';
  248. $filename = implode('-', $items);
  249. if (is_file($this->storePath . $filename . '.' . $extension)) {
  250. $old_name = $this->storePath . $filename . '.' . $extension;
  251. $items = explode('-', $this->fileName);
  252. $items[5] = $exe_id;
  253. $filename = $filename = implode('-', $items);
  254. $new_name = $this->storePath . $filename . '.' . $extension;
  255. rename($old_name, $new_name);
  256. break;
  257. }
  258. }
  259. }
  260. }