Skip to content

Commit

Permalink
MDL-79863 qtype_ordering: Codechecker fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimonus authored and Chocolate-lightning committed Apr 4, 2024
1 parent 0d11286 commit dd3513c
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 460 deletions.
5 changes: 2 additions & 3 deletions question/type/ordering/backup/moodle1/lib.php
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -44,12 +43,12 @@ public function get_question_subpaths() {
*/
public function process_question(array $data, array $raw) {

// convert and write the answers first
// Convert and write the answers first.
if (isset($data['answers'])) {
$this->write_answers($data['answers'], $this->pluginname);
}

// convert and write the ordering extra fields
// Convert and write the ordering extra fields.
foreach ($data['ordering'] as $ordering) {
$ordering['id'] = $this->converter->get_nextid();
$this->write_xml('ordering', $ordering, array('/ordering/id'));
Expand Down
Expand Up @@ -36,34 +36,34 @@ class backup_qtype_ordering_plugin extends backup_qtype_plugin {
*/
protected function define_question_plugin_structure() {

// Define the virtual plugin element with the condition to fulfill
// Define the virtual plugin element with the condition to fulfill.
$plugin = $this->get_plugin_element(null, '../../qtype', 'ordering');

// Create one standard named plugin element (the visible container)
// Create one standard named plugin element (the visible container).
$pluginwrapper = new backup_nested_element($this->get_recommended_name());

// connect the visible container ASAP
// Connect the visible container ASAP.
$plugin->add_child($pluginwrapper);

// This qtype uses standard question_answers, add them here
// to the tree before any other information that will use them
// to the tree before any other information that will use them.
$this->add_question_question_answers($pluginwrapper);

// Now create the qtype own structures
// Now create the qtype own structures.
$fields = array('layouttype', 'selecttype', 'selectcount', 'gradingtype',
'correctfeedback', 'correctfeedbackformat',
'incorrectfeedback', 'incorrectfeedbackformat',
'partiallycorrectfeedback', 'partiallycorrectfeedbackformat');
$ordering = new backup_nested_element('ordering', array('id'), $fields);

// Now the own qtype tree
// Now the own qtype tree.
$pluginwrapper->add_child($ordering);

// set source to populate the data
// Set source to populate the data.
$params = array('questionid' => backup::VAR_PARENTID);
$ordering->set_source_table('qtype_ordering_options', $params);

// don't need to annotate ids nor files
// Don't need to annotate ids nor files.

return $plugin;
}
Expand Down
Expand Up @@ -39,15 +39,15 @@ protected function define_question_plugin_structure() {

$paths = array();

// This qtype uses question_answers, add them
// This qtype uses question_answers, add them.
$this->add_question_question_answers($paths);

// Add own qtype stuff
// Add own qtype stuff.
$elename = 'ordering';
$elepath = $this->get_pathfor('/ordering'); // we used get_recommended_name() so this works
$elepath = $this->get_pathfor('/ordering'); // We used get_recommended_name() so this works.
$paths[] = new restore_path_element($elename, $elepath);

return $paths; // And we return the interesting paths
return $paths; // And we return the interesting paths.
}

/**
Expand All @@ -60,13 +60,13 @@ public function process_ordering($data) {
$oldid = $data->id;

// Detect if the question is created or mapped
// "question" is the XML tag name, not the DB field name
// "question" is the XML tag name, not the DB field name.
$oldquestionid = $this->get_old_parentid('question');
$newquestionid = $this->get_new_parentid('question');

// If the question has been created by restore,
// we need to create a "qtype_ordering_options" record
// and create a mapping from the $oldid to the $newid
// and create a mapping from the $oldid to the $newid.
if ($this->get_mappingid('question_created', $oldquestionid)) {
$data->questionid = $newquestionid;
$newid = $DB->insert_record('qtype_ordering_options', $data);
Expand Down
85 changes: 53 additions & 32 deletions question/type/ordering/db/upgrade.php
@@ -1,7 +1,28 @@
<?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/>.

/**
* @package moodlecore
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// This file keeps track of upgrades to
// the calculated qtype plugin
// the calculated qtype plugin.
//
// Sometimes, changes between versions involve
// alterations to database structures and other
Expand All @@ -15,7 +36,7 @@
// will tell you what you need to do.
//
// The commands in here will all be database-neutral,
// using the methods of database_manager class
// using the methods of database_manager class.
//
// Please do not forget to use upgrade_set_timeout()
// before any action that may take longer time to finish.
Expand All @@ -36,18 +57,18 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
if ($questions = $DB->get_records_sql("SELECT $select FROM $from WHERE $where", $params)) {
foreach ($questions as $question) {
if ($answers = $DB->get_records('question_answers', array('question' => $question->id))) {
// add "options" for this ordering question
$question_ordering = (object)array(
// Add "options" for this ordering question.
$questionordering = (object) array(
'question' => $question->id,
'logical' => 1,
'studentsee' => min(6, count($answers)),
'correctfeedback' => '',
'partiallycorrectfeedback' => '',
'incorrectfeedback' => ''
);
$question_ordering->id = $DB->insert_record('question_ordering', $question_ordering);
$questionordering->id = $DB->insert_record('question_ordering', $questionordering);
} else {
// this is a faulty ordering question - remove it
// This is a faulty ordering question - remove it.
$DB->delete_records('question', array('id' => $question->id));
if ($dbman->table_exists('quiz_question_instances')) {
$DB->delete_records('quiz_question_instances', array('question' => $question->id));
Expand All @@ -64,7 +85,7 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
$newversion = 2015011915;
if ($oldversion < $newversion) {

// rename "ordering" table for Moodle >= 2.5
// Rename "ordering" table for Moodle >= 2.5.
$oldname = 'question_ordering';
$newname = 'qtype_ordering_options';

Expand All @@ -77,8 +98,7 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
}
}

// remove index on question(id) field
// (because we want to modify the field)
// Remove index on question(id) field (because we want to modify the field).
$table = new xmldb_table('qtype_ordering_options');
$fields = array('question', 'questionid');
foreach ($fields as $field) {
Expand All @@ -90,25 +110,26 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
}
}

// rename "question" -> "questionid"
// rename "logical" -> "selecttype"
// rename "studentsee" -> "selectcount"
// add "(xxx)feedbackformat" fields
// Rename "question" -> "questionid".
// Rename "logical" -> "selecttype".
// Rename "studentsee" -> "selectcount".
// Add "(xxx)feedbackformat" fields.
$table = new xmldb_table('qtype_ordering_options');
$fields = array(
'questionid' => new xmldb_field('question', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, '0', 'id'),
'selecttype' => new xmldb_field('logical', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'questionid'),
'selectcount' => new xmldb_field('studentsee', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'selecttype'),
'correctfeedbackformat' => new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'correctfeedback'),
'incorrectfeedbackformat' => new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'incorrectfeedback'),
'partiallycorrectfeedbackformat' => new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback')
'questionid' => new xmldb_field('question', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, '0', 'id'),
'selecttype' => new xmldb_field('logical', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'questionid'),
'selectcount' => new xmldb_field('studentsee', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'selecttype'),
'correctfeedbackformat' => new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null,
'0', 'correctfeedback'),
'incorrectfeedbackformat' => new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL,
null, '0', 'incorrectfeedback'),
'partiallycorrectfeedbackformat' => new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback')
);
foreach ($fields as $newname => $field) {
$oldexists = $dbman->field_exists($table, $field);
$newexists = $dbman->field_exists($table, $newname);
if ($field->getName()==$newname) {
// same field name
} else if ($oldexists) {
if ($field->getName() != $newname && $oldexists) {
if ($newexists) {
$dbman->drop_field($table, $field);
} else {
Expand All @@ -125,7 +146,7 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
}
}

// make sure there are no duplicate "questionid" fields in "qtype_ordering_options" table
// Make sure there are no duplicate "questionid" fields in "qtype_ordering_options" table.
$select = 'questionid, COUNT(*) AS countduplicates, MAX(id) AS maxid';
$from = '{qtype_ordering_options}';
$group = 'questionid';
Expand All @@ -139,7 +160,7 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
}
}

// restore index on questionid field
// Restore index on questionid field.
$table = new xmldb_table('qtype_ordering_options');
$index = new xmldb_index('qtypordeopti_que_uix', XMLDB_INDEX_UNIQUE, array('questionid'));
if (! $dbman->index_exists($table, $index)) {
Expand Down Expand Up @@ -177,8 +198,8 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
$dbman->change_field_type($table, $field);
} else {
$dbman->add_field($table, $field);
// when adding this field to existing records,
// the gradingtype is set to whatever the selecttype is
// When adding this field to existing records,
// the gradingtype is set to whatever the selecttype is.
$DB->execute('UPDATE {qtype_ordering_options} SET gradingtype = selecttype', array());
}
}
Expand All @@ -199,15 +220,15 @@ function xmldb_qtype_ordering_upgrade($oldversion) {
list($select, $params) = $DB->get_in_or_equal($questions);
$select = "questionid $select";
$table = 'qtype_ordering_options';
$DB->set_field_select($table, 'layouttype', 0, $select, $params); // VERTICAL
$DB->set_field_select($table, 'selecttype', 1, $select, $params); // RANDOM
$DB->set_field_select($table, 'gradingtype', 1, $select, $params); // RELATIVE
$DB->set_field_select($table, 'layouttype', 0, $select, $params); // VERTICAL.
$DB->set_field_select($table, 'selecttype', 1, $select, $params); // RANDOM.
$DB->set_field_select($table, 'gradingtype', 1, $select, $params); // RELATIVE.

// for selectcount, we only fix the value, if it is zero (=ALL)
// because Ordering questions for some low level books use 4
// For selectcount, we only fix the value, if it is zero (=ALL)
// because Ordering questions for some low level books use 4.
$select .= ' AND selectcount = ?';
$params[] = 0;
$DB->set_field_select($table, 'selectcount', 6, $select, $params); // 6
$DB->set_field_select($table, 'selectcount', 6, $select, $params); // Six.
}
}
}
Expand Down

0 comments on commit dd3513c

Please sign in to comment.