ReadingComprehension.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * Constructor.
  50. */
  51. public function __construct()
  52. {
  53. parent::__construct();
  54. $this->type = READING_COMPREHENSION;
  55. $this->isContent = $this->getIsContent();
  56. }
  57. public function processText($text)
  58. {
  59. // Refresh is set to 5s, but speed is in words per minute
  60. $wordsPerSecond = self::$speeds[$this->level] / 60;
  61. $this->expectedWordsPerRefresh = intval($wordsPerSecond * $this->refreshTime);
  62. if (empty($text)) {
  63. // We have an issue here... how do we treat this case?
  64. // For now, let's define a default case
  65. $text = get_lang('NoExercise');
  66. }
  67. $words = str_word_count($text, 2, '0..9');
  68. $indexes = array_keys($words);
  69. $tagEnd = '</span>';
  70. $tagStart = $tagEnd.'<span class="text-highlight">';
  71. $this->wordsCount = count($words);
  72. $turns = ceil(
  73. $this->wordsCount / $this->expectedWordsPerRefresh
  74. );
  75. $firstIndex = $indexes[0];
  76. for ($i = 1; $i <= $turns; $i++) {
  77. $text = substr_replace($text, $tagStart, $firstIndex, 0);
  78. if ($i * $this->expectedWordsPerRefresh <= count($words)) {
  79. $newIndex = $i * $this->expectedWordsPerRefresh;
  80. if (isset($indexes[$newIndex])) {
  81. $nextFirstIndex = $indexes[$newIndex];
  82. $firstIndex = $nextFirstIndex + (strlen($tagStart) * $i);
  83. }
  84. }
  85. }
  86. $pos = strpos($text, $tagEnd);
  87. $text = substr_replace($text, '', $pos, strlen($tagEnd));
  88. $text .= $tagEnd;
  89. $this->displayReading($this->wordsCount, $turns, $text);
  90. }
  91. /**
  92. * Returns total count of words of the text to read.
  93. *
  94. * @return int
  95. */
  96. public function getWordsCount()
  97. {
  98. $words = str_word_count($this->selectDescription(), 2, '0..9');
  99. $this->wordsCount = count($words);
  100. return $this->wordsCount;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function createForm(&$form, $exercise)
  106. {
  107. // Categories
  108. $tabCat = TestCategory::getCategoriesIdAndName();
  109. $form->addSelect('questionCategory', get_lang('Category'), $tabCat);
  110. // Advanced parameters
  111. $levels = self::get_default_levels();
  112. $form->addSelect('questionLevel', get_lang('Difficulty'), $levels);
  113. $form->addElement('hidden', 'answerType', READING_COMPREHENSION);
  114. $form->addTextarea('questionDescription', get_lang('Text'), ['rows' => 20]);
  115. // question name
  116. if (api_get_configuration_value('save_titles_as_html')) {
  117. $editorConfig = ['ToolbarSet' => 'Minimal'];
  118. $form->addHtmlEditor(
  119. 'questionName',
  120. get_lang('Question'),
  121. false,
  122. false,
  123. $editorConfig,
  124. true
  125. );
  126. } else {
  127. $form->addText('questionName', get_lang('Question'), false);
  128. }
  129. // hidden values
  130. $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null;
  131. $form->addElement('hidden', 'myid', $my_id);
  132. $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
  133. $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
  134. // default values
  135. $defaults = [];
  136. $defaults['questionName'] = $this->question;
  137. $defaults['questionDescription'] = $this->description;
  138. $defaults['questionLevel'] = $this->level;
  139. $defaults['questionCategory'] = $this->category;
  140. // Came from he question pool
  141. if (isset($_GET['fromExercise'])) {
  142. $form->setDefaults($defaults);
  143. }
  144. if (!empty($_REQUEST['myid'])) {
  145. $form->setDefaults($defaults);
  146. } else {
  147. if ($isContent == 1) {
  148. $form->setDefaults($defaults);
  149. }
  150. }
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public static function get_default_levels()
  156. {
  157. $select_level = [
  158. 1 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[1]),
  159. 2 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[2]),
  160. 3 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[3]),
  161. 4 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[4]),
  162. 5 => sprintf(get_lang('ReadingComprehensionLevelX'), self::$speeds[5]),
  163. ];
  164. return $select_level;
  165. }
  166. /**
  167. * @param $wordsCount
  168. * @param $turns
  169. * @param $text
  170. */
  171. private function displayReading($wordsCount, $turns, $text)
  172. {
  173. $view = new Template('', false, false, false, true, false, false);
  174. $template = $view->get_template('exercise/reading_comprehension.tpl');
  175. $view->assign('id', $this->id);
  176. $view->assign('text', nl2br($text));
  177. $view->assign('words_count', $wordsCount);
  178. $view->assign('turns', $turns);
  179. $view->assign('refresh_time', $this->refreshTime);
  180. $view->display($template);
  181. }
  182. }