diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index c1f44f8ab03..a6dcbdc9a92 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -652,10 +652,11 @@ abstract class moodle_database { /** * Returns the SQL WHERE conditions. + * * @param string $table The table name that these conditions will be validated against. * @param array $conditions The conditions to build the where clause. (must not contain numeric indexes) - * @throws dml_exception * @return array An array list containing sql 'where' part and 'params'. + * @throws dml_exception */ protected function where_clause($table, array $conditions=null) { // We accept nulls in conditions @@ -1761,6 +1762,20 @@ abstract class moodle_database { return reset($record); // first column } + /** + * Selects records and return values of chosen field as an array where all the given conditions met. + * + * @param string $table the table to query. + * @param string $return the field we are intered in + * @param array|null $conditions optional array $fieldname=>requestedvalue with AND in between + * @return array of values + * @throws dml_exception A DML specific exception is thrown for any errors. + */ + public function get_fieldset(string $table, string $return, ?array $conditions = null): array { + [$select, $params] = $this->where_clause($table, $conditions); + return $this->get_fieldset_select($table, $return, $select, $params); + } + /** * Selects records and return values of chosen field as an array which match a particular WHERE clause. * diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index 160c93c7680..6b18cf53114 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -42,7 +42,7 @@ defined('MOODLE_INTERNAL') || die(); * @category test * @copyright 2008 Nicolas Connault * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @coversDefaultClass \moodle_database + * @covers \moodle_database */ class dml_test extends \database_driver_testcase { @@ -2079,6 +2079,32 @@ EOD; $this->assertEquals(3, $DB->get_field_sql("SELECT course FROM {{$tablename}} WHERE id = ?", array(1))); } + public function test_get_fieldset() { + $DB = $this->tdb; + $dbman = $DB->get_manager(); + + $table = $this->get_test_table(); + $tablename = $table->getName(); + + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $dbman->create_table($table); + + $DB->insert_record($tablename, ['course' => 1]); + $DB->insert_record($tablename, ['course' => 1]); + $DB->insert_record($tablename, ['course' => 2]); + $DB->insert_record($tablename, ['course' => 1]); + + $fieldset = $DB->get_fieldset($tablename, 'id', ['course' => 1]); + $this->assertIsArray($fieldset); + + $this->assertCount(3, $fieldset); + $this->assertEquals(1, $fieldset[0]); + $this->assertEquals(2, $fieldset[1]); + $this->assertEquals(4, $fieldset[2]); + } + public function test_get_fieldset_select() { $DB = $this->tdb; $dbman = $DB->get_manager(); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index b3a38517012..6f62d0ddae3 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -25,6 +25,7 @@ information provided here is intended especially for developers. By default, tasks will be retried until they succeed, other tasks can override this method to change this behaviour. - set_attempts_available(): Used to set the number of attempts available for the task - get_attempts_available(): Used to get the number of attempts available for the task. +* There is a new DML method $DB->get_fieldset. For some reason, this did not exist even though get_fieldset_select etc. did. === 4.3 === diff --git a/question/classes/statistics/responses/analysis_for_question.php b/question/classes/statistics/responses/analysis_for_question.php index a2520f923af..b491cdc810a 100644 --- a/question/classes/statistics/responses/analysis_for_question.php +++ b/question/classes/statistics/responses/analysis_for_question.php @@ -208,14 +208,13 @@ class analysis_for_question { $transaction = $DB->start_delegated_transaction(); - $analysisids = $DB->get_fieldset_select( + $analysisids = $DB->get_fieldset( 'question_response_analysis', 'id', - 'hashcode = ? AND whichtries = ? AND questionid = ?', [ - $qubaids->get_hash_code(), - $whichtries, - $questionid, + 'hashcode' => $qubaids->get_hash_code(), + 'whichtries' => $whichtries, + 'questionid' => $questionid, ] ); if (!empty($analysisids)) {