oral_expression.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. * @inheritdoc
  34. */
  35. public function createAnswersForm($form)
  36. {
  37. $form->addText(
  38. 'weighting',
  39. get_lang('Weighting'),
  40. array('class' => 'span1')
  41. );
  42. global $text;
  43. // setting the save button here and not in the question class.php
  44. $form->addButtonSave($text, 'submitQuestion');
  45. if (!empty($this->id)) {
  46. $form -> setDefaults(array('weighting' => float_format($this->weighting, 1)));
  47. } else {
  48. if ($this->isContent == 1) {
  49. $form->setDefaults(array('weighting' => '10'));
  50. }
  51. }
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function processAnswersCreation($form, $exercise)
  57. {
  58. $this->weighting = $form->getSubmitValue('weighting');
  59. $this->save($exercise);
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function return_header($exercise, $counter = null, $score = null)
  65. {
  66. $score['revised'] = $this->isQuestionWaitingReview($score);
  67. $header = parent::return_header($exercise, $counter, $score);
  68. $header .= '<table class="'.$this->question_table_class.'">
  69. <tr>
  70. <th>'.get_lang("Answer").'</th>
  71. </tr>';
  72. return $header;
  73. }
  74. /**
  75. * initialize the attributes to generate the file path
  76. * @param $sessionId integer
  77. * @param $userId integer
  78. * @param $exerciseId integer
  79. * @param $exeId integer
  80. */
  81. public function initFile($sessionId, $userId, $exerciseId, $exeId)
  82. {
  83. $this->sessionId = intval($sessionId);
  84. $this->userId = intval($userId);
  85. $this->exerciseId = 0;
  86. $this->exeId = intval($exeId);
  87. if (!empty($exerciseId)) {
  88. $this->exerciseId = intval($exerciseId);
  89. }
  90. $this->storePath = $this->generateDirectory();
  91. $this->fileName = $this->generateFileName();
  92. $this->filePath = $this->storePath.$this->fileName;
  93. }
  94. /**
  95. * Generate the necessary directory for audios. If them not exists, are created
  96. * @return string
  97. */
  98. private function generateDirectory()
  99. {
  100. $this->storePath = api_get_path(SYS_COURSE_PATH).$this->course['path'].'/exercises/';
  101. if (!is_dir($this->storePath)) {
  102. mkdir($this->storePath);
  103. }
  104. if (!is_dir($this->storePath.$this->sessionId)) {
  105. mkdir($this->storePath.$this->sessionId);
  106. }
  107. if (!empty($this->exerciseId) && !is_dir($this->storePath.$this->sessionId.'/'.$this->exerciseId)) {
  108. mkdir($this->storePath.$this->sessionId.'/'.$this->exerciseId);
  109. }
  110. if (!empty($this->id) && !is_dir($this->storePath.$this->sessionId.'/'.$this->exerciseId.'/'.$this->id)) {
  111. mkdir($this->storePath.$this->sessionId.'/'.$this->exerciseId.'/'.$this->id);
  112. }
  113. if (!empty($this->userId) && !is_dir($this->storePath.$this->sessionId.'/'.$this->exerciseId.'/'.$this->id.'/'.$this->userId)) {
  114. mkdir($this->storePath.$this->sessionId.'/'.$this->exerciseId.'/'.$this->id.'/'.$this->userId);
  115. }
  116. $params = [
  117. $this->sessionId,
  118. $this->exerciseId,
  119. $this->id,
  120. $this->userId,
  121. ];
  122. $this->storePath .= implode('/', $params).'/';
  123. return $this->storePath;
  124. }
  125. /**
  126. * Generate the file name
  127. * @return string
  128. */
  129. private function generateFileName()
  130. {
  131. return implode(
  132. '-',
  133. [
  134. $this->course['real_id'],
  135. $this->sessionId,
  136. $this->userId,
  137. $this->exerciseId,
  138. $this->id,
  139. $this->exeId
  140. ]
  141. );
  142. }
  143. /**
  144. * Generate a relative directory path
  145. * @return string
  146. */
  147. private function generateRelativeDirectory()
  148. {
  149. $params = [
  150. $this->sessionId,
  151. $this->exerciseId,
  152. $this->id,
  153. $this->userId,
  154. ];
  155. $path = implode('/', $params);
  156. $directory = '/exercises/'.$path.'/';
  157. return $directory;
  158. }
  159. /**
  160. * Return the HTML code to show the RecordRTC/Wami recorder
  161. * @return string
  162. */
  163. public function returnRecorder()
  164. {
  165. $directory = '/..'.$this->generateRelativeDirectory();
  166. $recordAudioView = new Template(
  167. '',
  168. false,
  169. false,
  170. false,
  171. false,
  172. false,
  173. false
  174. );
  175. $recordAudioView->assign('directory', $directory);
  176. $recordAudioView->assign('user_id', $this->userId);
  177. $recordAudioView->assign('file_name', $this->fileName);
  178. $recordAudioView->assign('question_id', $this->id);
  179. $template = $recordAudioView->get_template('exercise/oral_expression.tpl');
  180. return $recordAudioView->fetch($template);
  181. }
  182. /**
  183. * Get the absolute file path. Return null if the file doesn't exists
  184. * @param bool $loadFromDatabase
  185. * @return string
  186. */
  187. public function getAbsoluteFilePath($loadFromDatabase = false)
  188. {
  189. $fileName = $this->fileName;
  190. if ($loadFromDatabase) {
  191. $em = Database::getManager();
  192. //Load the real filename just if exists
  193. if (isset($this->exeId, $this->userId, $this->id, $this->sessionId, $this->course['real_id'])) {
  194. $result = $em
  195. ->getRepository('ChamiloCoreBundle:TrackEAttempt')
  196. ->findOneBy([
  197. 'exeId' => $this->exeId,
  198. 'userId' => $this->userId,
  199. 'questionId' => $this->id,
  200. 'sessionId' => $this->sessionId,
  201. 'cId' => $this->course['real_id']
  202. ]);
  203. if (!$result) {
  204. return '';
  205. }
  206. $fileName = $result->getFilename();
  207. if (empty($fileName)) {
  208. return '';
  209. }
  210. return $this->storePath.$result->getFilename();
  211. }
  212. }
  213. foreach ($this->available_extensions as $extension) {
  214. $audioFile = $this->storePath.$fileName;
  215. $file = "$audioFile.$extension";
  216. if (is_file($file)) {
  217. return $file;
  218. }
  219. // Function handle_uploaded_document() adds the session and group id by default.
  220. $file = "$audioFile"."__".$this->sessionId."__0.$extension";
  221. if (is_file($file)) {
  222. return $file;
  223. }
  224. continue;
  225. }
  226. return '';
  227. }
  228. /**
  229. * Get the URL for the audio file. Return null if the file doesn't exists
  230. * @param bool $loadFromDatabase
  231. *
  232. * @return string
  233. */
  234. public function getFileUrl($loadFromDatabase = false)
  235. {
  236. $filePath = $this->getAbsoluteFilePath($loadFromDatabase);
  237. if (empty($filePath)) {
  238. return null;
  239. }
  240. return str_replace(
  241. api_get_path(SYS_COURSE_PATH),
  242. api_get_path(WEB_COURSE_PATH),
  243. $filePath
  244. );
  245. }
  246. /**
  247. * Tricky stuff to deal with the feedback = 0 in exercises (all question per page)
  248. * @param $exe_id integer
  249. */
  250. public function replaceWithRealExe($exe_id)
  251. {
  252. $filename = null;
  253. //ugly fix
  254. foreach ($this->available_extensions as $extension) {
  255. $items = explode('-', $this->fileName);
  256. $items[5] = 'temp_exe';
  257. $filename = implode('-', $items);
  258. if (is_file($this->storePath.$filename.'.'.$extension)) {
  259. $old_name = $this->storePath.$filename.'.'.$extension;
  260. $items = explode('-', $this->fileName);
  261. $items[5] = $exe_id;
  262. $filename = $filename = implode('-', $items);
  263. $new_name = $this->storePath.$filename.'.'.$extension;
  264. rename($old_name, $new_name);
  265. break;
  266. }
  267. }
  268. }
  269. }