Skip to content
Permalink
Browse files

Add questionoptionsevaluation plugin - refs BT#15265

Allow recalulate the options score in questions
  • Loading branch information...
AngelFQC committed Mar 11, 2019
1 parent 0de2668 commit 399d7ce660f5334b3743a819148a837cb519c39d
@@ -0,0 +1,104 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Class QuestionOptionsEvaluationPlugin.
*/
class QuestionOptionsEvaluationPlugin extends Plugin
{
const EXTRAFIELD_FORMULA = 'question_valuation_formula';
/**
* QuestionValuationPlugin constructor.
*/
protected function __construct()
{
$version = '1.0';
$author = 'Angel Fernando Quiroz Campos';
parent::__construct($version, $author, ['enable' => 'boolean']);
}
/**
* @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('reload.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();
}
private function createExtraField()
{
$qEf = new ExtraField('question');

This comment has been minimized.

Copy link
@jmontoyaa

jmontoyaa Mar 15, 2019

Member

Favor, para la próxima usar nombres de variables sencillos y legibles. Ejemplo:

$extraField

This comment has been minimized.

Copy link
@AngelFQC

AngelFQC Mar 15, 2019

Author Member

Entendido :)

if (false === $qEf->get_handler_field_info_by_field_variable(self::EXTRAFIELD_FORMULA)) {
$qEf
->save(
[
'variable' => self::EXTRAFIELD_FORMULA,
'field_type' => ExtraField::FIELD_TYPE_TEXT,
'display_text' => $this->get_lang('EvaluationFormula'),
'visible_to_self' => false,
'changeable' => false,
]
);
}
}
public function uninstall()
{
$this->removeExtraField();
}
private function removeExtraField()
{
$extraField = new ExtraField('question');
$qEf = $extraField->get_handler_field_info_by_field_variable(self::EXTRAFIELD_FORMULA);
if (false !== $qEf) {
$extraField->delete($qEf['id']);
}
}
/**
* @return Plugin
*/
public function performActionsAfterConfigure()
{
return $this;
}
}
@@ -0,0 +1,139 @@
<?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)) {
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;
}
$form = new FormValidator('evaluation');
$form->addRadio(
'formula',
$plugin->get_lang('EvaluationFormula'),
[
$plugin->get_lang('NoFormula'),
$plugin->get_lang('Formula1'),
$plugin->get_lang('Formula2'),
$plugin->get_lang('Formula3'),
]
)->setColumnsSize([4, 7, 1]);
$form->addButtonSave(get_lang('Save'))->setColumnsSize([4, 7, 1]);
$form->addHidden('exercise', $exerciseId);
if ($form->validate()) {
$values = $form->exportValues();
$formula = isset($values['formula']) ? (int) $values['formula'] : 0;
$nbrQuestions = count($exercise->questionList);
foreach ($exercise->questionList as $questionId) {
$question = Question::read($questionId);
if (!in_array($question->selectType(), [UNIQUE_ANSWER, MULTIPLE_ANSWER])) {
continue;
}
$questionAnswers = new Answer($questionId, 0,$exercise);
$counts = array_count_values($questionAnswers->correct);
$weighting = [];
foreach ($questionAnswers->correct as $i => $correct) {
// successes
if (1 == $correct) {
$weighting[$i] = 10 / $counts[1] / $nbrQuestions;
continue;
}
// failures
switch ($formula) {
case 0:
default:
$weighting[$i] = isset($questionAnswers->weighting[$i]) ? $questionAnswers->weighting[$i] : 0;
break;
case 1:
$weighting[$i] = -10 / $nbrQuestions;
break;
case 2:
$weighting[$i] = -10 / 2 / $nbrQuestions;
break;
case 3:
$weighting[$i] = -10 / 3 / $nbrQuestions;
break;
}
}
$weighting = array_map(
function ($weight) {
return float_format($weight);
},
$weighting
);
$questionAnswers->new_nbrAnswers = $questionAnswers->nbrAnswers;
$questionAnswers->new_answer = $questionAnswers->answer;
$questionAnswers->new_comment = $questionAnswers->comment;
$questionAnswers->new_correct = $questionAnswers->correct;
$questionAnswers->new_weighting = $weighting;
$questionAnswers->new_position = $questionAnswers->position;
$questionAnswers->new_destination = $questionAnswers->destination;
$questionAnswers->new_hotspot_coordinates = $questionAnswers->hotspot_coordinates;
$questionAnswers->new_hotspot_type = $questionAnswers->hotspot_type;
$allowedWeights = array_filter(
$weighting,
function ($weight) {
return $weight > 0;
}
);
$questionAnswers->save();
$question->updateWeighting(
array_sum($allowedWeights)
);
$question->save($exercise);
}
Display::addFlash(
Display::return_message($plugin->get_lang('QuestionsEvaluated'))
);
header(
'Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq()."&exerciseId=$exerciseId"
);
exit;
}
echo Display::return_message(
$plugin->get_lang('QuizQuestionsScoreRulesTitleConfirm'),
'warning'
);
$form->display();
@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
QuestionOptionsEvaluationPlugin::create()->install();
@@ -0,0 +1,15 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = 'Question options evaluation';
$strings['plugin_comment'] = 'Allow recalulate the options score in questions';
$strings['enable'] = 'Enable';
$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';
@@ -0,0 +1,15 @@
<?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['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 evaluados';
@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
$plugin_info = QuestionOptionsEvaluationPlugin::create()->get_info();
@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
QuestionOptionsEvaluationPlugin::create()->uninstall();

0 comments on commit 399d7ce

Please sign in to comment.
You can’t perform that action at this time.