Skip to content

Commit

Permalink
Auto grade the correct questions with previous marks and comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna-lmsace committed Sep 13, 2023
1 parent cded597 commit 6cad375
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 13 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ Git URL: https://github.com/lmsace/moodle-hide-correct-questions
1. Download the '**hidecorrect**' from [GitHub releases](https://github.com/lmsace/moodle-hide-correct-questions/releases).
2. Log in to Moodle as a Site Administrator.
3. Go to '*Site Administration*' -> '*Plugins*' -> '*Upload Plugin*', On here upload the plugin zip '**hidecorrect.zip**'.
4. Go to Site administration’ -> ‘Notifications’ , here on Plugins check’ page you will see the '*Hide Correct Questions on New Attempt*' '*Quiz / Access rules*' plugin in listing.
5. Click the “Upgrade Moodle database now” button displayed on bottom of the page.
4. Go to ‘Site administration’ -> ‘Notifications’ , here on ‘Plugins check’ page you will see the '*Hide Correct Questions on New Attempt*' '*Quiz / Access rules*' plugin in listing.
5. Click the “Upgrade Moodle database now” button displayed on bottom of the page.
> You will get success message once the plugin installed successfully.
6. By clicking “Continue” button on success page. you will redirect to the admin notification page.
6. By clicking “Continue” button on success page. you will redirect to the admin notification page.

### Installation steps using Git.

1. Clone hidecorrect plugin Git repository into the folder '*mod / quiz / accessrule*'.
2. Rename the folder name into '**hidecorrect**'.
3. Go to Site administration’ -> ‘Notifications’ , here on Plugins check’ page you will see the '*Hide Correct Questions on New Attempt*' '*Quiz / Access rules*' plugin in listing.
4. Click the “Upgrade Moodle database now” button displayed on bottom of the page.
3. Go to ‘Site administration’ -> ‘Notifications’ , here on ‘Plugins check’ page you will see the '*Hide Correct Questions on New Attempt*' '*Quiz / Access rules*' plugin in listing.
4. Click the “Upgrade Moodle database now” button displayed on bottom of the page.
> You will get success message once the plugin installed successfully.
5. By clicking “Continue” button on success page. You will redirect to the admin notification page.
5. By clicking “Continue” button on success page. You will redirect to the admin notification page.

## Configuration
To configure the plugin settings, follow these steps:
Expand All @@ -53,7 +53,7 @@ To configure the plugin settings, follow these steps:

![hidecorrect-config](https://github.com/lmsace/moodle-hide-correct-questions/assets/57126778/4bb293e0-d5bc-4191-879c-797b214d7879)


10. Save the quiz settings to apply the changes.
With these settings enabled, when a learner attempts the quiz again, the questions will be hidden, allowing them to reflect on their previous attempt and make improvements without directly seeing the correct answers.

Expand All @@ -67,3 +67,11 @@ For support or assistance, please contact the plugin author or submit an issue t

## About
This plugin was developed by [LMSACE](https://lmsace.com/). For more information, please visit our website or contact us directly.

## Contributions

LMSACE would like to thank these main contributors for their contirbutions

1. Yuka (Shori) Kataoka, External Certified Japanese Language Instructor : Funding

2. Maemoon Naseer, Founder and CEO of Aeorax : Funding
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function define_quiz_subplugin_structure() {
$subpluginwrapper = new backup_nested_element($this->get_recommended_name());
$subplugin->add_child($subpluginwrapper);

$settings = new backup_nested_element('quizaccess_hidecorrect', null, array('hidecorrect'));
$settings = new backup_nested_element('quizaccess_hidecorrect', null, array('hidecorrect', 'autograde'));
$subpluginwrapper->add_child($settings);

$settings->set_source_table('quizaccess_hidecorrect', array('quizid' => backup::VAR_ACTIVITYID));
Expand Down
1 change: 1 addition & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="18" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="quizid" TYPE="int" LENGTH="18" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="hidecorrect" TYPE="int" LENGTH="9" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="autograde" TYPE="int" LENGTH="9" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="quizid"/>
Expand Down
50 changes: 50 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Quiz access hide correct questions - Upgrade script
*
* @package quizaccess_hidecorrect
* @subpackage hidecorrect
* @copyright 2023 LMSACE Dev Team.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Upgrade database to support autograde feature.
*
* @param int $oldversion
* @return void
*/
function xmldb_quizaccess_hidecorrect_upgrade($oldversion) {
global $DB;

$dbman = $DB->get_manager();

if ($oldversion < 2023061401) {
// Add a new column 'autograde' to the hidecorrect table.
$table = new xmldb_table('quizaccess_hidecorrect');

// Define field autograde to be added to hidecorrect.
$field = new xmldb_field('autograde', XMLDB_TYPE_INTEGER, 9, null, null, null);

// Conditionally launch add field autograde.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
upgrade_plugin_savepoint(true, 2023061401, 'quizaccess', 'hidecorrect');
}
}
4 changes: 4 additions & 0 deletions lang/en/quizaccess_hidecorrect.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
$string['hidecorrect_help'] = 'Enabled, this plugin will hide questions in new attempts that were previously answered correctly.
<b> NOTE: This only works when Each attempt builds on the last is configured yes for this quiz </b>';
$string['privacy:metadata'] = 'The Hide Correct Questions on New Attempt quiz access rule plugin does not store any personal data.';
// ... Autograde.
$string['autogradeenable'] = 'Auto grade the correct questions';
$string['autograde'] = 'Questions auto grade ';
$string['autograde_help'] = 'Auto-grading applies the grades from the last attempt to correctly answered questions in the new attempt.';
130 changes: 127 additions & 3 deletions rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* Quiz access rule Implementation - quizaccess_hidecorrect.
*
* * Included the autograde unchanged questions correctly answered in previous attempt.
*
* @package quizaccess_hidecorrect
* @copyright 2023 LMSACE Dev Team <lmsace.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand Down Expand Up @@ -87,7 +89,7 @@ public function setup_attempt_page($page) {
public function user_last_finished_attempt() {
global $USER;
// Get this user's attempts.
$attempts = quiz_get_user_attempts($this->quiz->id, $USER->id, 'finished', true);
$attempts = quiz_get_user_attempts($this->quiz->id, $USER->id, 'finished', false);
if (empty($attempts)) {
return false;
}
Expand Down Expand Up @@ -194,6 +196,13 @@ public static function add_settings_form_fields(mod_quiz_mod_form $quizform, Moo
];
$mform->addElement('select', 'hidecorrect', get_string('hidecorrect', 'quizaccess_hidecorrect'), $options);
$mform->addHelpButton('hidecorrect', 'hidecorrect', 'quizaccess_hidecorrect');

$options = [
self::DISABLE => get_string('disable'),
self::ENABLE => get_string('autogradeenable', 'quizaccess_hidecorrect'),
];
$mform->addElement('select', 'hidecorrect_autograde', get_string('autograde', 'quizaccess_hidecorrect'), $options);
$mform->addHelpButton('hidecorrect_autograde', 'autograde', 'quizaccess_hidecorrect');
}

/**
Expand All @@ -205,7 +214,7 @@ public static function add_settings_form_fields(mod_quiz_mod_form $quizform, Moo
public static function save_settings($quiz) {
global $DB;

$data = (object) ['hidecorrect' => $quiz->hidecorrect];
$data = (object) ['hidecorrect' => $quiz->hidecorrect, 'autograde' => $quiz->hidecorrect_autograde];

if ($record = $DB->get_record('quizaccess_hidecorrect', ['quizid' => $quiz->id])) {
$data->id = $record->id;
Expand Down Expand Up @@ -249,9 +258,124 @@ public static function delete_settings($quiz) {
*/
public static function get_settings_sql($quizid) {
return array(
'hidecorrect', // Select field.
'hidecorrect, autograde as hidecorrect_autograde', // Select field.
'LEFT JOIN {quizaccess_hidecorrect} hidecorrect ON hidecorrect.quizid = quiz.id', // Fetch join queyy.
[] // Paramenters.
);
}

/**
* Find the previous finished attempt of the user for this quiz.
*
* @param int $attemptid Current attempt id.
* @return stdclass|bool Return the user previous attempt object, otherwise false.
*/
public function user_previous_finished_attempt($attemptid) {
global $USER;
// Get this user's attempts.
$attempts = quiz_get_user_attempts($this->quiz->id, $USER->id, 'finished', false);
if (empty($attempts)) {
return false;
}

$attempts = array_reverse($attempts);
$attempt = next($attempts);

return $attempt;
}

/**
* This is called when the current attempt at the quiz is finished.
* Update the previous attempt grades for the hidden questions.
*
* @return void
*/
public function current_attempt_finished() {

// Verify the autograde is enabled.
if (!$this->quiz->{"hidecorrect_autograde"}) {
return true;
}

if ($attemptid = required_param('attempt', PARAM_INT)) {

$lastattempt = $this->user_previous_finished_attempt($attemptid);
if (!$lastattempt) {
return false;
}

// Make the current attempt instance.
$attemptobj = quiz_create_attempt_handling_errors($attemptid, $this->quizobj->get_cmid());
// Verify the attempt contains the manaual grading questions.
if ($attemptobj->requires_manual_grading()) {
// Load question usage instance for current attempt.
$quba = question_engine::load_questions_usage_by_activity($attemptobj->get_attempt()->uniqueid);
// Load question usage instance for previous attempt.
$prevquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);

foreach ($attemptobj->get_slots() as $slot) {
$qa = $quba->get_question_attempt($slot);
// Verify the question needs to be grade and it doesn't changed from previous attempt.
if ($qa->get_state() == question_state::$needsgrading && $qa->get_num_steps() == 2
&& $prevquba->get_question_state_string($slot, true) == 'Correct') {

$comment = '';
$prevgradeduser = '';
$commentformat = '';
$gradedmark = $prevquba->get_question_mark($slot); // Get grade of the question from previous attempt.
$prevqa = $prevquba->get_question_attempt($slot); // Question attempt instance for this quetsion.

foreach ($prevqa->get_step_iterator() as $step) {
// Find the question is graded in previous attempt.
if ($step->get_state()->is_commented()) {
$prevgradeduser = $step->get_user_id(); // Fetch the graded user.
$comment = $step->get_behaviour_var('comment'); // Fetch the comment for this question.
$commentformat = $step->get_behaviour_var('commentformat');
}
}

if ($prevgradeduser) {
// This is the qustion is graded in previous attempt.
// Then now use the same grades and comments for this attempt.
$qa->manual_grade($comment, $gradedmark, $commentformat, null, $prevgradeduser);
$quba->get_observer()->notify_attempt_modified($qa); // Create a step for manual grade.
}

}
}
// Finish the grading, update the total mark for this attempt.
$this->process_autograded_actions($quba, $attemptobj);
}
}
}

/**
* Store the final grade for this attempt and regrade the attempt.
*
* @param question_usage_by_activity $quba
* @param quiz_attempt $quizattempt
* @return void
*/
public function process_autograded_actions($quba, $quizattempt) {
global $DB;

$transaction = $DB->start_delegated_transaction();

$timestamp = time();
question_engine::save_questions_usage_by_activity($quba);

$attempt = $quizattempt->get_attempt();
$attempt->timemodified = $timestamp;
if ($attempt->state == $quizattempt::FINISHED) {
$attempt->sumgrades = $quba->get_total_mark();
}

$DB->update_record('quiz_attempts', $attempt);

if (!$quizattempt->is_preview() && $attempt->state == $quizattempt::FINISHED) {
quiz_save_best_grade($this->quiz);
}

$transaction->allow_commit();
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'quizaccess_hidecorrect';
$plugin->version = 2023061400;
$plugin->version = 2023091300;
$plugin->requires = 2021051100;
$plugin->release = 'v1.0';
$plugin->release = 'v2.0';
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 6cad375

Please sign in to comment.