diff --git a/communication/classes/api.php b/communication/classes/api.php index 8f454d6b772..93dbdff9052 100644 --- a/communication/classes/api.php +++ b/communication/classes/api.php @@ -226,9 +226,8 @@ class api { * @return bool */ public function set_avatar_from_datauri_or_filepath(?string $datauri): bool { - global $DB; - $currentfilename = $DB->get_field('communication', 'avatarfilename', ['id' => $this->communication->get_id()]); + $currentfilename = $this->communication->get_avatar_filename(); if (empty($datauri) && empty($currentfilename)) { return false; } @@ -260,7 +259,11 @@ class api { $fs->create_file_from_string($this->get_avatar_filerecord($filename), file_get_contents($datauri)); } - $DB->set_field('communication', 'avatarfilename', $filename, ['id' => $this->communication->get_id()]); + $this->communication->set_avatar_filename($filename); + + // Indicate that we need to sync the avatar when the update task is run. + $this->communication->set_avatar_synced_flag(false); + return true; } diff --git a/communication/classes/processor.php b/communication/classes/processor.php index b8d806a396f..839926c36f4 100644 --- a/communication/classes/processor.php +++ b/communication/classes/processor.php @@ -96,6 +96,7 @@ class processor { 'roomname' => $roomname, 'avatarfilename' => null, 'active' => self::PROVIDER_ACTIVE, + 'avatarsynced' => 0, ]; $record->id = $DB->insert_record('communication', $record); @@ -586,6 +587,46 @@ class processor { return $file ? $file : null; } + + /** + * Set the avatar file name. + * + * @param string|null $filename + */ + public function set_avatar_filename(?string $filename): void { + global $DB; + $DB->update_record('communication', ['id' => $this->instancedata->id, 'avatarfilename' => $filename]); + } + + /** + * Get the avatar file name. + * + * @return string|null + */ + public function get_avatar_filename(): ?string { + return $this->instancedata->avatarfilename; + } + + /** + * Check if the avatar has been synced with the provider. + * + * @return bool + */ + public function is_avatar_synced(): bool { + return (bool)$this->instancedata->avatarsynced; + } + + /** + * Indicate if the avatar has been synced with the provider. + * + * @param boolean $synced True if avatar has been synced. + */ + public function set_avatar_synced_flag(bool $synced): void { + global $DB; + $DB->update_record('communication', ['id' => $this->instancedata->id, 'avatarsynced' => (int)$synced]); + $this->instancedata->avatarsynced = (int)$synced; + } + /** * Get a room url. * diff --git a/communication/tests/processor_test.php b/communication/tests/processor_test.php index 9c88a6bb38b..73438e47da0 100644 --- a/communication/tests/processor_test.php +++ b/communication/tests/processor_test.php @@ -361,6 +361,9 @@ class processor_test extends \advanced_testcase { * * @covers ::get_avatar * @covers ::load_by_instance + * @covers ::get_avatar_filename + * @covers ::set_avatar_filename + * @covers ::set_avatar_synced_flag */ public function test_get_avatar(): void { $this->resetAfterTest(); @@ -394,6 +397,17 @@ class processor_test extends \advanced_testcase { $this->assertEquals($avatar->get_itemid(), $communicationprocessor->get_id()); $this->assertEquals($avatar->get_filepath(), '/'); $this->assertEquals($avatar->get_filearea(), 'avatar'); + $this->assertEquals($avatar->get_filename(), $communicationprocessor->get_avatar_filename()); + + // Change the avatar file name to something else and check it was set. + $communicationprocessor->set_avatar_filename('newname.svg'); + + $communicationprocessor = processor::load_by_instance( + 'core_course', + 'coursecommunication', + $course->id + ); + $this->assertEquals($communicationprocessor->get_avatar_filename(), 'newname.svg'); } /** diff --git a/lib/db/install.xml b/lib/db/install.xml index bebdd926af4..e18b9a07e03 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -228,6 +228,7 @@ + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 518567c1418..f8efc889580 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3333,5 +3333,20 @@ privatefiles,moodle|/user/files.php'; upgrade_main_savepoint(true, 2023062700.01); } + if ($oldversion < 2023062900.01) { + + // Define field avatarsynced to be added to communication. + $table = new xmldb_table('communication'); + $field = new xmldb_field('avatarsynced', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'active'); + + // Conditionally launch add field avatarsynced. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2023062900.01); + } + return true; } diff --git a/version.php b/version.php index b7bc78e76f8..b7944e76469 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2023062900.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2023062900.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.3dev (Build: 20230629)'; // Human-friendly version name