Test Setup Failed
Push — master ( f71949...6c6bd7 )
by Julito
55:21
created

ReadingComprehension::get_default_levels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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