upgrade
This commit is contained in:
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
use Chamilo\CoreBundle\Entity\TrackEAttempt;
|
||||
|
||||
/**
|
||||
* Class QuestionOptionsEvaluationPlugin.
|
||||
*/
|
||||
class QuestionOptionsEvaluationPlugin extends Plugin
|
||||
{
|
||||
public const SETTING_ENABLE = 'enable';
|
||||
public const SETTING_MAX_SCORE = 'exercise_max_score';
|
||||
public const EXTRAFIELD_FORMULA = 'quiz_evaluation_formula';
|
||||
|
||||
/**
|
||||
* QuestionValuationPlugin constructor.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$version = '1.0';
|
||||
$author = 'Angel Fernando Quiroz Campos';
|
||||
|
||||
parent::__construct(
|
||||
$version,
|
||||
$author,
|
||||
[
|
||||
self::SETTING_ENABLE => 'boolean',
|
||||
self::SETTING_MAX_SCORE => 'text',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return QuestionOptionsEvaluationPlugin|null
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
static $result = null;
|
||||
|
||||
return $result ? $result : $result = new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $exerciseId
|
||||
* @param int $iconSize
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function filterModify($exerciseId, $iconSize = ICON_SIZE_SMALL)
|
||||
{
|
||||
$directory = basename(__DIR__);
|
||||
$title = get_plugin_lang('plugin_title', self::class);
|
||||
$enabled = api_get_plugin_setting('questionoptionsevaluation', 'enable');
|
||||
|
||||
if ('true' !== $enabled) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Display::url(
|
||||
Display::return_icon('options_evaluation.png', $title, [], $iconSize),
|
||||
api_get_path(WEB_PATH)."plugin/$directory/evaluation.php?exercise=$exerciseId",
|
||||
[
|
||||
'class' => 'ajax',
|
||||
'data-size' => 'md',
|
||||
'data-title' => get_plugin_lang('plugin_title', self::class),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function install()
|
||||
{
|
||||
$this->createExtraField();
|
||||
}
|
||||
|
||||
public function uninstall()
|
||||
{
|
||||
$this->removeExtraField();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Plugin
|
||||
*/
|
||||
public function performActionsAfterConfigure()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $formula
|
||||
*/
|
||||
public function saveFormulaForExercise($formula, Exercise $exercise)
|
||||
{
|
||||
$this->recalculateQuestionScore($formula, $exercise);
|
||||
|
||||
$extraFieldValue = new ExtraFieldValue('quiz');
|
||||
$extraFieldValue->save(
|
||||
[
|
||||
'item_id' => $exercise->iid,
|
||||
'variable' => self::EXTRAFIELD_FORMULA,
|
||||
'value' => $formula,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $exerciseId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFormulaForExercise($exerciseId)
|
||||
{
|
||||
$extraFieldValue = new ExtraFieldValue('quiz');
|
||||
$value = $extraFieldValue->get_values_by_handler_and_field_variable(
|
||||
$exerciseId,
|
||||
self::EXTRAFIELD_FORMULA
|
||||
);
|
||||
|
||||
if (empty($value)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) $value['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getMaxScore()
|
||||
{
|
||||
$max = $this->get(self::SETTING_MAX_SCORE);
|
||||
|
||||
if (!empty($max)) {
|
||||
return (int) $max;
|
||||
}
|
||||
|
||||
return 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $trackId
|
||||
* @param int $formula
|
||||
*
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
* @throws \Doctrine\ORM\TransactionRequiredException
|
||||
*
|
||||
* @return float|int
|
||||
*/
|
||||
public function getResultWithFormula($trackId, $formula)
|
||||
{
|
||||
$em = Database::getManager();
|
||||
|
||||
$eTrack = $em->find('ChamiloCoreBundle:TrackEExercises', $trackId);
|
||||
|
||||
$qTracks = $em
|
||||
->createQuery(
|
||||
'SELECT a FROM ChamiloCoreBundle:TrackEAttempt a
|
||||
WHERE a.exeId = :id AND a.userId = :user AND a.cId = :course AND a.sessionId = :session'
|
||||
)
|
||||
->setParameters(
|
||||
[
|
||||
'id' => $eTrack->getExeId(),
|
||||
'course' => $eTrack->getCId(),
|
||||
'session' => $eTrack->getSessionId(),
|
||||
'user' => $eTrack->getExeUserId(),
|
||||
]
|
||||
)
|
||||
->getResult();
|
||||
|
||||
$counts = ['correct' => 0, 'incorrect' => 0];
|
||||
|
||||
/** @var TrackEAttempt $qTrack */
|
||||
foreach ($qTracks as $qTrack) {
|
||||
if ($qTrack->getMarks() > 0) {
|
||||
$counts['correct']++;
|
||||
} elseif ($qTrack->getMarks() < 0) {
|
||||
$counts['incorrect']++;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($formula) {
|
||||
case 1:
|
||||
$result = $counts['correct'] - $counts['incorrect'];
|
||||
break;
|
||||
case 2:
|
||||
$result = $counts['correct'] - $counts['incorrect'] / 2;
|
||||
break;
|
||||
case 3:
|
||||
$result = $counts['correct'] - $counts['incorrect'] / 3;
|
||||
break;
|
||||
}
|
||||
|
||||
$score = ($result / count($qTracks)) * $this->getMaxScore();
|
||||
|
||||
return $score >= 0 ? $score : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $formula
|
||||
*/
|
||||
private function recalculateQuestionScore($formula, Exercise $exercise)
|
||||
{
|
||||
$tblQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
|
||||
$tblAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
|
||||
|
||||
foreach ($exercise->questionList as $questionId) {
|
||||
$question = Question::read($questionId, $exercise->course, false);
|
||||
if (!in_array($question->selectType(), [UNIQUE_ANSWER, MULTIPLE_ANSWER])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$questionAnswers = new Answer($questionId, $exercise->course_id, $exercise);
|
||||
$counts = array_count_values($questionAnswers->correct);
|
||||
|
||||
$questionPonderation = 0;
|
||||
|
||||
foreach ($questionAnswers->correct as $i => $isCorrect) {
|
||||
if (!isset($questionAnswers->iid[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$iid = $questionAnswers->iid[$i];
|
||||
|
||||
if ($question->selectType() == MULTIPLE_ANSWER || 0 === $formula) {
|
||||
$ponderation = 1 == $isCorrect ? 1 / $counts[1] : -1 / $counts[0];
|
||||
} else {
|
||||
$ponderation = 1 == $isCorrect ? 1 : -1 / $formula;
|
||||
}
|
||||
|
||||
if ($ponderation > 0) {
|
||||
$questionPonderation += $ponderation;
|
||||
}
|
||||
|
||||
Database::query("UPDATE $tblAnswer SET ponderation = $ponderation WHERE iid = $iid");
|
||||
}
|
||||
|
||||
Database::query("UPDATE $tblQuestion SET ponderation = $questionPonderation WHERE iid = {$question->iid}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an extrafield.
|
||||
*/
|
||||
private function createExtraField()
|
||||
{
|
||||
$extraField = new ExtraField('quiz');
|
||||
|
||||
if (false === $extraField->get_handler_field_info_by_field_variable(self::EXTRAFIELD_FORMULA)) {
|
||||
$extraField
|
||||
->save(
|
||||
[
|
||||
'variable' => self::EXTRAFIELD_FORMULA,
|
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT,
|
||||
'display_text' => $this->get_lang('EvaluationFormula'),
|
||||
'visible_to_self' => false,
|
||||
'changeable' => false,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the extrafield .
|
||||
*/
|
||||
private function removeExtraField()
|
||||
{
|
||||
$extraField = new ExtraField('quiz');
|
||||
$value = $extraField->get_handler_field_info_by_field_variable(self::EXTRAFIELD_FORMULA);
|
||||
|
||||
if (false !== $value) {
|
||||
$extraField->delete($value['id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
plugin/questionoptionsevaluation/README.md
Normal file
20
plugin/questionoptionsevaluation/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Questions Options Evaluation
|
||||
|
||||
Allow recalulate the options score in questions:
|
||||
|
||||
* Successes - Failures
|
||||
* Successes - Failures / 2
|
||||
* Successes - Failures / 3
|
||||
* Recalculate question scores
|
||||
|
||||
**Setup instructions**
|
||||
|
||||
- Install plugin
|
||||
- Set enabled in configuration
|
||||
- Edit `configuration.php` file
|
||||
```php
|
||||
$_configuration['exercise_additional_teacher_modify_actions'] = [
|
||||
// ...
|
||||
'questionoptionsevaluation' => ['QuestionOptionsEvaluationPlugin', 'filterModify']
|
||||
];
|
||||
```
|
||||
74
plugin/questionoptionsevaluation/evaluation.php
Normal file
74
plugin/questionoptionsevaluation/evaluation.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
|
||||
api_protect_teacher_script();
|
||||
api_protect_course_script();
|
||||
|
||||
$exerciseId = isset($_REQUEST['exercise']) ? (int) $_REQUEST['exercise'] : 0;
|
||||
|
||||
if (empty($exerciseId)) {
|
||||
echo Display::return_message(get_lang('NotAllowed'), 'error');
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
$exercise = new Exercise();
|
||||
|
||||
if (!$exercise->read($exerciseId, false)) {
|
||||
echo Display::return_message(get_lang('ExerciseNotFound'), 'error');
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
$plugin = QuestionOptionsEvaluationPlugin::create();
|
||||
|
||||
if ($plugin->get('enable') !== 'true') {
|
||||
echo Display::return_message(get_lang('NotAllowed'), 'error');
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
$formEvaluation = new FormValidator('evaluation');
|
||||
$formEvaluation
|
||||
->addRadio(
|
||||
'formula',
|
||||
$plugin->get_lang('EvaluationFormula'),
|
||||
[
|
||||
-1 => $plugin->get_lang('NoFormula'),
|
||||
0 => $plugin->get_lang('RecalculateQuestionScores'),
|
||||
1 => $plugin->get_lang('Formula1'),
|
||||
2 => $plugin->get_lang('Formula2'),
|
||||
3 => $plugin->get_lang('Formula3'),
|
||||
]
|
||||
)
|
||||
->setColumnsSize([4, 7, 1]);
|
||||
$formEvaluation->addButtonSave(get_lang('Save'))->setColumnsSize([4, 7, 1]);
|
||||
$formEvaluation->addHidden('exercise', $exerciseId);
|
||||
|
||||
if ($formEvaluation->validate()) {
|
||||
$exercise->read($exerciseId, true);
|
||||
$values = $formEvaluation->exportValues();
|
||||
$formula = isset($values['formula']) ? (int) $values['formula'] : 0;
|
||||
$plugin->saveFormulaForExercise($formula, $exercise);
|
||||
Display::addFlash(
|
||||
Display::return_message(
|
||||
sprintf($plugin->get_lang('FormulaSavedForExerciseX'), $exercise->selectTitle(true)),
|
||||
'success'
|
||||
)
|
||||
);
|
||||
|
||||
header(
|
||||
'Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq()."&exerciseId=$exerciseId"
|
||||
);
|
||||
exit;
|
||||
}
|
||||
|
||||
$formEvaluation->setDefaults(['formula' => $plugin->getFormulaForExercise($exercise->iid)]);
|
||||
|
||||
echo Display::return_message(
|
||||
$plugin->get_lang('QuizQuestionsScoreRulesTitleConfirm'),
|
||||
'warning'
|
||||
);
|
||||
$formEvaluation->display();
|
||||
4
plugin/questionoptionsevaluation/install.php
Normal file
4
plugin/questionoptionsevaluation/install.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
QuestionOptionsEvaluationPlugin::create()->install();
|
||||
19
plugin/questionoptionsevaluation/lang/english.php
Normal file
19
plugin/questionoptionsevaluation/lang/english.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = 'Question options evaluation';
|
||||
$strings['plugin_comment'] = "Allow re-calculation of the options' scores in questions";
|
||||
|
||||
$strings['enable'] = 'Enable';
|
||||
$strings['exercise_max_score'] = 'Max score in exercises';
|
||||
$strings['exercise_max_score_help'] = 'Default is 10.';
|
||||
|
||||
$strings['QuizQuestionsScoreRulesTitleConfirm'] = 'Changing the evaluation formula generates changes for each question in the exercise and prevents undoing this change below. Are you sure you want to proceed?';
|
||||
$strings['EvaluationFormula'] = 'Evaluation formula';
|
||||
$strings['NoFormula'] = 'No formula';
|
||||
$strings['Formula1'] = 'Successes - Failures';
|
||||
$strings['Formula2'] = 'Successes - Failures / 2';
|
||||
$strings['Formula3'] = 'Successes - Failures / 3';
|
||||
$strings['QuestionsEvaluated'] = 'Questions evaluated';
|
||||
$strings['RecalculateQuestionScores'] = 'Recalculate question scores';
|
||||
$strings['FormulaSavedForExerciseX'] = 'Formula saved for exercise "%s".';
|
||||
19
plugin/questionoptionsevaluation/lang/spanish.php
Normal file
19
plugin/questionoptionsevaluation/lang/spanish.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = 'Evaluación para opciones de pregunta';
|
||||
$strings['plugin_comment'] = 'Permite recalcular los puntajes de opciones en preguntas';
|
||||
|
||||
$strings['enable'] = 'Habilitar';
|
||||
$strings['exercise_max_score'] = 'Puntuación máxima por ejercicio';
|
||||
$strings['exercise_max_score_help'] = 'Por defecto es 10.';
|
||||
|
||||
$strings['QuizQuestionsScoreRulesTitleConfirm'] = 'Cambiar la fórmula de evaluación genera cambios para cada pregunta del ejercicio e impide deshacer este cambio a continuación. ¿Está seguro que desea proceder?';
|
||||
$strings['EvaluationFormula'] = 'Fórmula de evaluación';
|
||||
$strings['NoFormula'] = 'Sin formula';
|
||||
$strings['Formula1'] = 'Aciertos - Fallos';
|
||||
$strings['Formula2'] = 'Aciertos - Fallos / 2';
|
||||
$strings['Formula3'] = 'Aciertos - Fallos / 3';
|
||||
$strings['QuestionsEvaluated'] = 'Preguntas evaluadas';
|
||||
$strings['RecalculateQuestionScores'] = 'Recalcular puntuaciones de preguntas';
|
||||
$strings['FormulaSavedForExerciseX'] = 'Formula guardada para el ejercicio "%s".';
|
||||
4
plugin/questionoptionsevaluation/plugin.php
Normal file
4
plugin/questionoptionsevaluation/plugin.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$plugin_info = QuestionOptionsEvaluationPlugin::create()->get_info();
|
||||
4
plugin/questionoptionsevaluation/uninstall.php
Normal file
4
plugin/questionoptionsevaluation/uninstall.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
QuestionOptionsEvaluationPlugin::create()->uninstall();
|
||||
Reference in New Issue
Block a user