Merge branch 'MDL-65168' of git://github.com/stronk7/moodle

This commit is contained in:
Jun Pataleta 2019-04-03 16:42:41 +08:00
commit ad4d995f19
7 changed files with 390 additions and 224 deletions

View file

@ -29,6 +29,7 @@ defined('MOODLE_INTERNAL') || die();
use mod_forum\local\vaults\preprocessors\extract_record as extract_record_preprocessor;
use mod_forum\local\vaults\preprocessors\extract_user as extract_user_preprocessor;
use mod_forum\local\renderers\discussion_list as discussion_list_renderer;
use core\dml\table as dml_table;
use stdClass;
/**
@ -89,22 +90,22 @@ class discussion_list extends db_table_vault {
// - First post
// - Author
// - Most recent editor.
$tablefields = $db->get_preload_columns(self::TABLE, $alias);
$postfields = $db->get_preload_columns('forum_posts', 'p_');
$thistable = new dml_table(self::TABLE, $alias, $alias);
$posttable = new dml_table('forum_posts', 'fp', 'p_');
$firstauthorfields = \user_picture::fields('fa', null, self::FIRST_AUTHOR_ID_ALIAS, self::FIRST_AUTHOR_ALIAS);
$latestuserfields = \user_picture::fields('la', null, self::LATEST_AUTHOR_ID_ALIAS, self::LATEST_AUTHOR_ALIAS);
$fields = implode(', ', [
$db->get_preload_columns_sql($tablefields, $alias),
$db->get_preload_columns_sql($postfields, 'fp'),
$thistable->get_field_select(),
$posttable->get_field_select(),
$firstauthorfields,
$latestuserfields,
]);
$tables = '{' . self::TABLE . '} ' . $alias;
$tables = $thistable->get_from_sql();
$tables .= ' JOIN {user} fa ON fa.id = ' . $alias . '.userid';
$tables .= ' JOIN {user} la ON la.id = ' . $alias . '.usermodified';
$tables .= ' JOIN {forum_posts} fp ON fp.id = ' . $alias . '.firstpost';
$tables .= ' JOIN ' . $posttable->get_from_sql() . ' ON fp.id = ' . $alias . '.firstpost';
$selectsql = 'SELECT ' . $fields . ' FROM ' . $tables;
$selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
@ -139,8 +140,8 @@ class discussion_list extends db_table_vault {
return array_merge(
parent::get_preprocessors(),
[
'discussion' => new extract_record_preprocessor($this->get_db(), self::TABLE, $this->get_table_alias()),
'firstpost' => new extract_record_preprocessor($this->get_db(), 'forum_posts', 'p_'),
'discussion' => new extract_record_preprocessor(self::TABLE, $this->get_table_alias()),
'firstpost' => new extract_record_preprocessor('forum_posts', 'p_'),
'firstpostauthor' => new extract_user_preprocessor(self::FIRST_AUTHOR_ID_ALIAS, self::FIRST_AUTHOR_ALIAS),
'latestpostauthor' => new extract_user_preprocessor(self::LATEST_AUTHOR_ID_ALIAS, self::LATEST_AUTHOR_ALIAS),
]

View file

@ -29,6 +29,7 @@ defined('MOODLE_INTERNAL') || die();
use mod_forum\local\entities\forum as forum_entity;
use mod_forum\local\vaults\preprocessors\extract_context as extract_context_preprocessor;
use mod_forum\local\vaults\preprocessors\extract_record as extract_record_preprocessor;
use core\dml\table as dml_table;
use context_helper;
/**
@ -65,22 +66,23 @@ class forum extends db_table_vault {
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null) : string {
$db = $this->get_db();
$alias = $this->get_table_alias();
$tablefields = $db->get_preload_columns(self::TABLE, $alias);
$coursemodulefields = $db->get_preload_columns('course_modules', 'cm_');
$coursefields = $db->get_preload_columns('course', 'c_');
$thistable = new dml_table(self::TABLE, $alias, $alias);
$cmtable = new dml_table('course_modules', 'cm', 'cm_');
$coursetable = new dml_table('course', 'c', 'c_');
$fields = implode(', ', [
$db->get_preload_columns_sql($tablefields, $alias),
$thistable->get_field_select(),
context_helper::get_preload_record_columns_sql('ctx'),
$db->get_preload_columns_sql($coursemodulefields, 'cm'),
$db->get_preload_columns_sql($coursefields, 'c'),
$cmtable->get_field_select(),
$coursetable->get_field_select(),
]);
$tables = '{' . self::TABLE . '} ' . $alias;
$tables = $thistable->get_from_sql();
$tables .= " JOIN {modules} m ON m.name = 'forum'";
$tables .= " JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = {$alias}.id";
$tables .= " JOIN " . $cmtable->get_from_sql() . " ON cm.module = m.id AND cm.instance = {$alias}.id";
$tables .= ' JOIN {context} ctx ON ctx.contextlevel = ' . CONTEXT_MODULE . ' AND ctx.instanceid = cm.id';
$tables .= " JOIN {course} c ON c.id = {$alias}.course";
$tables .= " JOIN " . $coursetable->get_from_sql() . " ON c.id = {$alias}.course";
$selectsql = 'SELECT ' . $fields . ' FROM ' . $tables;
$selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
@ -99,9 +101,9 @@ class forum extends db_table_vault {
return array_merge(
parent::get_preprocessors(),
[
'forum' => new extract_record_preprocessor($this->get_db(), self::TABLE, $this->get_table_alias()),
'course_module' => new extract_record_preprocessor($this->get_db(), 'course_modules', 'cm_'),
'course' => new extract_record_preprocessor($this->get_db(), 'course', 'c_'),
'forum' => new extract_record_preprocessor(self::TABLE, $this->get_table_alias()),
'course_module' => new extract_record_preprocessor('course_modules', 'cm_'),
'course' => new extract_record_preprocessor('course', 'c_'),
'context' => new extract_context_preprocessor(),
]
);

View file

@ -27,6 +27,7 @@ namespace mod_forum\local\vaults\preprocessors;
defined('MOODLE_INTERNAL') || die();
use moodle_database;
use core\dml\table as dml_table;
/**
* Extract record vault preprocessor.
@ -38,24 +39,17 @@ use moodle_database;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class extract_record {
/** @var moodle_database $db A moodle database */
private $db;
/** @var string $table The table name where the records were loaded from */
/** @var \core\dml\table $table The table object relating to the table that the records were loaded from */
private $table;
/** @var string $alias The table alias used as the record property prefix */
private $alias;
/**
* Constructor.
*
* @param moodle_database $db A moodle database
* @param string $table The table name where the records were loaded from
* @param string $alias The table alias used as the record property prefix
*/
public function __construct(moodle_database $db, string $table, string $alias) {
$this->db = $db;
$this->table = $table;
$this->alias = $alias;
public function __construct(string $table, string $alias) {
$this->table = new dml_table($table, $alias, $alias);
}
/**
@ -66,11 +60,8 @@ class extract_record {
* @return stdClass[] The extracted records
*/
public function execute(array $records) : array {
$db = $this->db;
$fields = $this->db->get_preload_columns($this->table, $this->alias);
return array_map(function($record) use ($db, $fields) {
return $db->extract_fields_from_object($fields, $record);
return array_map(function($record) {
return $this->table->extract_from_result($record);
}, $records);
}
}