MDL-65032 mod_forum: Pass the the entity factories into the vaults

This commit is contained in:
Peter 2019-04-08 10:18:28 +08:00
parent f5b4320ec5
commit c475fe410f
9 changed files with 65 additions and 27 deletions

View File

@ -14,7 +14,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Forum repository class to encapsulate all of the AJAX requests thatsubscribe or unsubscribe
* Forum repository class to encapsulate all of the AJAX requests that subscribe or unsubscribe
* can be sent for forum.
*
* @module mod_forum/repository

View File

@ -98,7 +98,8 @@ class container {
return new vault_factory(
$DB,
self::get_entity_factory(),
get_file_storage()
get_file_storage(),
self::get_legacy_data_mapper_factory()
);
}

View File

@ -30,6 +30,7 @@ use mod_forum\local\data_mappers\legacy\author as author_data_mapper;
use mod_forum\local\data_mappers\legacy\discussion as discussion_data_mapper;
use mod_forum\local\data_mappers\legacy\forum as forum_data_mapper;
use mod_forum\local\data_mappers\legacy\post as post_data_mapper;
use mod_forum\local\entities\forum;
/**
* Legacy data mapper factory.
@ -76,4 +77,23 @@ class legacy_data_mapper {
public function get_author_data_mapper() : author_data_mapper {
return new author_data_mapper();
}
/**
* Get the corresponding entity based on the supplied value
*
* @param string $entity
* @return author_data_mapper|discussion_data_mapper|forum_data_mapper|post_data_mapper
*/
public function get_legacy_data_mapper_for_vault($entity) {
switch($entity) {
case 'forum':
return $this->get_forum_data_mapper();
case 'discussion':
return $this->get_discussion_data_mapper();
case 'post':
return $this->get_post_data_mapper();
case 'author':
return $this->get_author_data_mapper();
}
}
}

View File

@ -49,6 +49,8 @@ use moodle_database;
class vault {
/** @var entity_factory $entityfactory Entity factory */
private $entityfactory;
/** @var legacy_data_mapper $legacymapper Entity factory */
private $legacymapper;
/** @var moodle_database $db A moodle database */
private $db;
/** @var file_storage $filestorage A file storage instance */
@ -60,11 +62,14 @@ class vault {
* @param moodle_database $db A moodle database
* @param entity_factory $entityfactory Entity factory
* @param file_storage $filestorage A file storage instance
* @param legacy_data_mapper $legacyfactory Datamapper
*/
public function __construct(moodle_database $db, entity_factory $entityfactory, file_storage $filestorage) {
public function __construct(moodle_database $db, entity_factory $entityfactory,
file_storage $filestorage, legacy_data_mapper $legacyfactory) {
$this->db = $db;
$this->entityfactory = $entityfactory;
$this->filestorage = $filestorage;
$this->legacymapper = $legacyfactory;
}
/**
@ -75,7 +80,8 @@ class vault {
public function get_forum_vault() : forum_vault {
return new forum_vault(
$this->db,
$this->entityfactory
$this->entityfactory,
$this->legacymapper->get_legacy_data_mapper_for_vault('forum')
);
}
@ -87,7 +93,8 @@ class vault {
public function get_discussion_vault() : discussion_vault {
return new discussion_vault(
$this->db,
$this->entityfactory
$this->entityfactory,
$this->legacymapper->get_legacy_data_mapper_for_vault('discussion')
);
}
@ -99,7 +106,8 @@ class vault {
public function get_discussions_in_forum_vault() : discussion_list_vault {
return new discussion_list_vault(
$this->db,
$this->entityfactory
$this->entityfactory,
$this->legacymapper->get_legacy_data_mapper_for_vault('discussion')
);
}
@ -111,7 +119,8 @@ class vault {
public function get_post_vault() : post_vault {
return new post_vault(
$this->db,
$this->entityfactory
$this->entityfactory,
$this->legacymapper->get_legacy_data_mapper_for_vault('post')
);
}
@ -123,7 +132,8 @@ class vault {
public function get_author_vault() : author_vault {
return new author_vault(
$this->db,
$this->entityfactory
$this->entityfactory,
$this->legacymapper->get_legacy_data_mapper_for_vault('author')
);
}
@ -135,7 +145,8 @@ class vault {
public function get_post_read_receipt_collection_vault() : post_read_receipt_collection_vault {
return new post_read_receipt_collection_vault(
$this->db,
$this->entityfactory
$this->entityfactory,
$this->legacymapper->get_legacy_data_mapper_for_vault('post')
);
}

View File

@ -40,19 +40,24 @@ abstract class db_table_vault {
private $db;
/** @var entity_factory $entityfactory Entity factory */
private $entityfactory;
/** @var legacy_factor $legacyfactory Entity->legacy factory */
private $legacyfactory;
/**
* Constructor.
*
* @param moodle_database $db A moodle database
* @param entity_factory $entityfactory Entity factory
* @param object $legacyfactory Legacy factory
*/
public function __construct(
moodle_database $db,
entity_factory $entityfactory
entity_factory $entityfactory,
$legacyfactory
) {
$this->db = $db;
$this->entityfactory = $entityfactory;
$this->legacyfactory = $legacyfactory;
}
/**
@ -116,6 +121,15 @@ abstract class db_table_vault {
return $this->entityfactory;
}
/**
* Get the legacy factory
*
* @return object
*/
protected function get_legacy_factory() {
return $this->legacyfactory;
}
/**
* Execute the defined preprocessors on the DB record results and then convert
* them into entities.

View File

@ -132,9 +132,10 @@ class discussion extends db_table_vault {
* @param discussion_entity $discussion
* @return discussion_entity|null
*/
public function update_discussion($discussion) : ?discussion_entity {
if ($this->get_db()->update_record('forum_discussions', $discussion)) {
$records = $this->transform_db_records_to_entities([$discussion]);
public function update_discussion(discussion_entity $discussion) : ?discussion_entity {
$discussionrecord = $this->get_legacy_factory()->to_legacy_object($discussion);
if ($this->get_db()->update_record('forum_discussions', $discussionrecord)) {
$records = $this->transform_db_records_to_entities([$discussionrecord]);
return count($records) ? array_shift($records) : null;
}

View File

@ -1512,7 +1512,6 @@ class mod_forum_external extends external_api {
return \mod_forum\local\exporters\discussion::get_read_structure();
}
/**
* Set the lock state.
*
@ -1541,16 +1540,12 @@ class mod_forum_external extends external_api {
$capabilitymanager = $managerfactory->get_capability_manager($forum);
$discussionvault = $vaultfactory->get_discussion_vault();
$discussion = $discussionvault->get_from_id($params['discussionid']);
$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
$forumrecord = $legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($forum);
// If the current state doesn't equal the desired state then update the current
// If the current state doesn't equal the desired state then update the current.
// state to the desired state.
if ($capabilitymanager->can_manage_forum($USER)) {
$discussion->toggle_locked_state($targetstate);
$discussionrecord = $legacydatamapperfactory->get_discussion_data_mapper()->to_legacy_object($discussion);
$response = $discussionvault->update_discussion($discussionrecord);
$response = $discussionvault->update_discussion($discussion);
$discussion = !$response ? $response : $discussion;
}

View File

@ -2600,7 +2600,6 @@ function forum_get_lock_discussion_icon($forum, $discussion, $returnurl = null,
$returnurl = $PAGE->url->out();
}
$o = '';
$discussionid = $discussion->id;
$lockstatus = forum_discussion_is_locked($forum, $discussion);
$subscriptionlink = new moodle_url('/mod/forum/lockdiscussion.php', array(
@ -2610,10 +2609,6 @@ function forum_get_lock_discussion_icon($forum, $discussion, $returnurl = null,
'returnurl' => $returnurl,
));
if ($includetext) {
$o .= $lockstatus ? get_string('locked', 'mod_forum') : get_string('notlocked', 'mod_forum');
}
if ($lockstatus) {
$output = $OUTPUT->pix_icon('t/unlock', get_string('clicktounlockdiscussion', 'forum'), 'core');
if ($includetext) {

View File

@ -26,7 +26,7 @@
* none
Context variables required for this template:
* TODO
* none
Example context (json):
{
@ -39,7 +39,8 @@
data-action="toggle"
data-discussionid="{{id}}"
data-forumid="{{forumid}}"
data-state="{{times.locked}}" href="#"
data-state="{{times.locked}}"
tabindex="-1"
{{#userstate.locked}}
title="{{#str}}locked, forum{{/str}}"
{{/userstate.locked}}