This commit is contained in:
Jun Pataleta 2024-01-11 10:44:41 +08:00
commit c67b184f4c
No known key found for this signature in database
GPG Key ID: F83510526D99E2C7
4 changed files with 48 additions and 7 deletions

View File

@ -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.
*

View File

@ -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();

View File

@ -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 ===

View File

@ -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)) {