ReadingComprehension.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ReadingComprehension.
  5. *
  6. * This class allows to instantiate an object of type READING_COMPREHENSION
  7. * extending the class question
  8. *
  9. * @package chamilo.exercise
  10. */
  11. class ReadingComprehension extends UniqueAnswer
  12. {
  13. public static $typePicture = 'reading-comprehension.png';
  14. public static $explanationLangVar = 'ReadingComprehension';
  15. /**
  16. * Defines the different speeds of scrolling for the reading window,
  17. * in words per minute. If 300 words text in 50w/m, then the moving
  18. * window will progress from top to bottom in 6 minutes.
  19. *
  20. * @var array
  21. */
  22. public static $speeds = [
  23. 1 => 50,
  24. 2 => 100,
  25. 3 => 175,
  26. 4 => 300,
  27. 5 => 600,
  28. ];
  29. /**
  30. * The number of words in the question description (which serves as the
  31. * text to read).
  32. *
  33. * @var int
  34. */
  35. public $wordsCount = 0;
  36. /**
  37. * Number of words expected to show per refresh.
  38. *
  39. * @var int
  40. */
  41. public $expectedWordsPerRefresh = 0;
  42. /**
  43. * Refresh delay in seconds.
  44. *
  45. * @var int
  46. */
  47. public $refreshTime = 3;
  48. /**
  49. * Indicates how show the question list.
  50. * 1 = all in one page; 2 = one per page (default).
  51. *
  52. * @var int
  53. */
  54. private $exerciseType = 2;
  55. /**
  56. * Constructor.
  57. */
  58. public function __construct()
  59. {
  60. parent::__construct();
  61. $this->type = READING_COMPREHENSION;
  62. $this->isContent = $this->getIsContent();
  63. }
  64. public function processText($text)
  65. {
  66. // Refresh is set to 5s, but speed is in words per minute
  67. $wordsPerSecond = self::$speeds[$this->level] / 60;
  68. $this->expectedWordsPerRefresh = intval($wordsPerSecond * $this->refreshTime);
  69. if (empty($text)) {
  70. // We have an issue here... how do we treat this case?
  71. // For now, let's define a default case
  72. $text = get_lang('NoExercise');
  73. }
  74. $words = str_word_count($text, 2, '0..9');
  75. $indexes = array_keys($words);
  76. $tagEnd = '</span>';
  77. $tagStart = $tagEnd.'<span class="text-highlight">';
  78. $this->wordsCount = count($words);
  79. $turns = ceil(
  80. $this->wordsCount / $this->expectedWordsPerRefresh
  81. );
  82. $firstIndex = $indexes[0];
  83. for ($i = 1; $i <= $turns; $i++) {
  84. $text = substr_replace($text, $tagStart, $firstIndex, 0);
  85. if ($i * $this->expectedWordsPerRefresh <= count($words)) {
  86. $newIndex = $i * $this->expectedWordsPerRefresh;
  87. if (isset($indexes[$newIndex])) {
  88. $nextFirstIndex = $indexes[$newIndex];
  89. $firstIndex = $nextFirstIndex + (strlen($tagStart) * $i);
  90. }
  91. }
  92. }
  93. $pos = strpos($text, $tagEnd);
  94. $text = substr_replace($text, '', $pos, strlen($tagEnd));
  95. $text .= $tagEnd;
  96. $this->displayReading($this->wordsCount, $turns, $text);
  97. }
  98. /**
  99. * Returns total count of words of the text to read.
  100. *
  101. * @return int
  102. */
  103. public function getWordsCount()
  104. {
  105. $words = str_word_count($this->selectDescription(), 2, '0..9');
  106. $this->wordsCount = count($words);
  107. return $this->wordsCount;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function createForm(&$form, $exercise)
  113. {
  114. // Categories
  115. $tabCat = TestCategory::getCategoriesIdAndName();
  116. $form->addSelect('questionCategory', get_lang('Category'), $tabCat);
  117. // Advanced parameters
  118. $levels = self::get_default_levels();
  119. $form->addSelect('questionLevel', get_lang('Difficulty'), $levels);
  120. $form->addElement('hidden', 'answerType', READING_COMPREHENSION);
  121. $form->addTextarea('questionDescription', get_lang('Text'), ['rows' => 20]);
  122. // question name
  123. if (api_get_configuration_value('save_titles_as_html')) {
  124. $editorConfig = ['ToolbarSet' => 'Minimal'];
  125. $form->addHtmlEditor(
  126. 'questionName',
  127. get_lang('Question'),
  128. false,
  129. false,
  130. $editorConfig,
  131. true
  132. );
  133. } else {
  134. $form->addText('questionName', get_lang('Question'), false);
  135. }
  136. // hidden values
  137. $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
  138. $isContent = isset($_REQUEST['isContent']) ? (int) $_REQUEST['isContent'] : null;
  139. // default values
  140. $defaults = [];
  141. $defaults['questionName'] = $this->question;
  142. $defaults['questionDescription'] = $this->description;
  143. $defaults['questionLevel'] = $this->level;
  144. $defaults['questionCategory'] = $this->category;
  145. // Came from he question pool
  146. if (isset($_GET['fromExercise'])) {
  147. $form->setDefaults($defaults);
  148. }
  149. if (!isset($_GET['newQuestion']) || $isContent) {
  150. $form->setDefaults($defaults);
  151. }
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public static function get_default_levels()
  157. {
  158. $select_level = [
  159. 1 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[1]),
  160. 2 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[2]),
  161. 3 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[3]),
  162. 4 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[4]),
  163. 5 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[5]),
  164. ];
  165. return $select_level;
  166. }
  167. /**
  168. * @param int $type
  169. *
  170. * @return ReadingComprehension
  171. */
  172. public function setExerciseType($type)
  173. {
  174. $this->exerciseType = (int) $type;
  175. return $this;
  176. }
  177. /**
  178. * @param $wordsCount
  179. * @param $turns
  180. * @param $text
  181. */
  182. private function displayReading($wordsCount, $turns, $text)
  183. {
  184. $view = new Template('', false, false, false, true, false, false);
  185. $template = $view->get_template('exercise/reading_comprehension.tpl');
  186. $view->assign('id', $this->id);
  187. $view->assign('text', nl2br($text));
  188. $view->assign('words_count', $wordsCount);
  189. $view->assign('turns', $turns);
  190. $view->assign('refresh_time', $this->refreshTime);
  191. $view->assign('exercise_type', $this->exerciseType);
  192. $view->display($template);
  193. }
  194. }