ReadingComprehension.php 6.2 KB

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