Add reading speed effect

pull/2487/head
Angel Fernando Quiroz Campos 8 years ago
parent 333d15d677
commit 95046a7461
  1. 120
      main/exercise/ReadingSpeed.php
  2. 73
      main/inc/lib/exercise.lib.php
  3. 48
      main/template/default/exercise/reading_speed.tpl

@ -25,7 +25,7 @@ class ReadingSpeed extends UniqueAnswer
* window will progress from top to bottom in 6 minutes
* @var array $speeds
*/
public $speeds = [
public static $speeds = [
1 => 50,
2 => 100,
3 => 175,
@ -38,6 +38,7 @@ class ReadingSpeed extends UniqueAnswer
* @var int $wordsCount
*/
private $wordsCount = 0;
public $expectedCount = 0;
/**
* Constructor
@ -47,6 +48,7 @@ class ReadingSpeed extends UniqueAnswer
parent::__construct();
$this->type = READING_SPEED;
$this->isContent = $this->getIsContent();
$this->expectedCount = self::$speeds[$this->level];
}
/**
@ -60,10 +62,124 @@ class ReadingSpeed extends UniqueAnswer
if (empty($this->wordsCount) or empty($textHeight)) {
return 0;
}
$wordsPerMinute = $this->speeds[$this->level];
$wordsPerMinute = $this->expectedCount;
if ($wordsPerMinute == 0) {
$wordsPerMinute = 1;
}
return $this->wordsCount/$wordsPerMinute;
}
public function createAnswersForm($form)
{
$form->addTextarea('text', get_lang('Text'), ['rows' => '15']);
parent::createAnswersForm($form);
}
public function processAnswersCreation($form)
{
$text = $form->exportValue('text');
$objAnswer = new Answer($this->id);
$objAnswer->createAnswer($text, false, null, 0, 0);
$questionWeighting = $nbrGoodAnswers = 0;
$correct = $form->getSubmitValue('correct');
$nb_answers = $form->getSubmitValue('nb_answers');
for ($i = 1; $i <= $nb_answers; $i++) {
$answer = trim($form->getSubmitValue('answer[' . $i . ']'));
$comment = trim($form->getSubmitValue('comment[' . $i . ']'));
$weighting = trim($form->getSubmitValue('weighting[' . $i . ']'));
$scenario = $form->getSubmitValue('scenario');
$try = $scenario['try' . $i];
$lp = $scenario['lp' . $i];
$destination = $scenario['destination' . $i];
$url = trim($scenario['url' . $i]);
$goodAnswer = $correct == $i ? true : false;
if ($goodAnswer) {
$nbrGoodAnswers++;
$weighting = abs($weighting);
if ($weighting > 0) {
$questionWeighting += $weighting;
}
}
if (empty($try)) {
$try = 0;
}
if (empty($lp)) {
$lp = 0;
}
if (empty($destination)) {
$destination = 0;
}
if ($url == '') {
$url = 0;
}
//1@@1;2;@@2;4;4;@@http://www.chamilo.org
$dest = $try . '@@' . $lp . '@@' . $destination . '@@' . $url;
$objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i, null, null, $dest);
}
// saves the answers into the data base
$objAnswer->save();
// sets the total weighting of the question
$this->updateWeighting($questionWeighting);
$this->save();
}
private function displayReading($wordsCount, $turns, $text)
{
$view = new Template('', false, false, false, true, false, false);
$template = $view->get_template('exercise/reading_speed.tpl');
$view->assign('id', $this->id);
$view->assign('text', $text);
$view->assign('words_count', $wordsCount);
$view->assign('turns', $turns);
$view->display($template);
}
public function processText($text)
{
$words = str_word_count($text, 2, '0..9');
$indexes = array_keys($words);
$tagEnd = '</span>';
$tagStart = $tagEnd.'<span class="text-highlight">';
$turns = ceil(
count($words) / $this->expectedCount
);
$firstIndex = $indexes[0];
for ($i = 1; $i <= $turns; $i++) {
$text = substr_replace($text, $tagStart, $firstIndex, 0);
if ($i * $this->expectedCount <= count($words)) {
$nextFirstIndex = $indexes[$i * $this->expectedCount];
$firstIndex = $nextFirstIndex + (strlen($tagStart) * $i);
}
}
$pos = strpos($text, $tagEnd);
$text = substr_replace($text, '', $pos, strlen($tagEnd));
$text .= $tagEnd;
$this->displayReading(count($words), $turns, $text);
}
}

@ -271,7 +271,17 @@ class ExerciseLib
}
}
if ($answerType == READING_SPEED) {
$objQuestionTmp->processText(
$objAnswerTmp->selectAnswer(1)
);
}
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
if ($answerType == READING_SPEED && $answerId === 1) {
$answerId++;
}
$answer = $objAnswerTmp->selectAnswer($answerId);
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
$numAnswer = $objAnswerTmp->selectAutoId($answerId);
@ -284,6 +294,8 @@ class ExerciseLib
case UNIQUE_ANSWER_NO_OPTION:
//no break
case UNIQUE_ANSWER_IMAGE:
//no break
case READING_SPEED:
$input_id = 'choice-' . $questionId . '-' . $answerId;
if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
@ -1327,67 +1339,6 @@ HOTSPOT;
unset($objAnswerTmp, $objQuestionTmp);
} elseif ($answerType == READING_SPEED) {
//TODO adapt to READING_SPEED type (currently copy of annotation)
global $exe_id;
$relPath = api_get_path(WEB_CODE_PATH);
if (api_is_platform_admin() || api_is_course_admin()) {
if ($freeze) {
return 0;
}
}
if (!$only_questions) {
if ($show_title) {
TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
echo $objQuestionTmp->getTitleToDisplay($current_item);
}
echo '
<input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />
<div class="exercise_questions">
'.$objQuestionTmp->selectDescription().'
<div class="row">
<div class="col-sm-8 col-md-9">
<div id="annotation-canvas-'.$questionId.'" class="annotation-canvas center-block">
</div>
<script>
AnnotationQuestion({
questionId: '.$questionId.',
exerciseId: '.$exe_id.',
relPath: \''.$relPath.'\'
});
</script>
</div>
<div class="col-sm-4 col-md-3">
<div class="well well-sm" id="annotation-toolbar-'.$questionId.'">
<div class="btn-toolbar">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default active"
aria-label="'.get_lang('AddAnnotationPath').'">
<input type="radio" value="0" name="'.$questionId.'-options" autocomplete="off" checked>
<span class="fa fa-pencil" aria-hidden="true"></span>
</label>
<label class="btn btn-default"
aria-label="'.get_lang('AddAnnotationText').'">
<input type="radio" value="1" name="'.$questionId.'-options" autocomplete="off">
<span class="fa fa-font fa-fw" aria-hidden="true"></span>
</label>
</div>
</div>
<ul class="list-unstyled"></ul>
</div>
</div>
</div>
</div>
';
}
$objAnswerTmp = new Answer($questionId);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
unset($objAnswerTmp, $objQuestionTmp);
}
return $nbrAnswers;
}

@ -0,0 +1,48 @@
<div class="question-{{ d }}">
<div class="btn-toolbar">
{{ words_count }}
</div>
<div id="question-{{ id }}-text" class="center-block question-text">
{{ text }}
</div>
</div>
<style>
.question-text {
color: #FFF;
text-align: justify;
width: 800px;
}
.text-highlight.active {
color: rgba(0, 0, 0, 1);
}
</style>
<script>
$(document).on('ready', function () {
var index = 0,
total = $('#question-{{ id }}-text .text-highlight').length;
function updateView()
{
$('#question-{{ id }}-text .text-highlight').removeClass('active');
if (index >= total) {
window.clearInterval(timeOuId);
return;
}
var current = $('#question-{{ id }}-text .text-highlight').get(index);
$(current).addClass('active');
index++;
}
updateView();
var timeOuId = window.setInterval(updateView, 6 * 1000);
});
</script>
Loading…
Cancel
Save