oral_expression.class.php 9.0 KB

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