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