Skip to content

Commit

Permalink
#1660 default recommendation at new context add
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Dec 6, 2024
1 parent c971f71 commit 05100d8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public function up(): void
->default(true)
->comment('The status which determine if will be showen in recommendation list');

$table
->boolean('removable')
->default(true)
->comment('Describe if system define and non removable');

$table->timestamps();

});
Expand Down
21 changes: 11 additions & 10 deletions classes/migration/upgrade/v3_6_0/I1660_ReviewerRecommendations.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function up(): void
{
$this->recommendationInstallMigration->up();

$this->seedNonRemovableRecommendations();
$this->seedNonRemovableRecommendations($this->systemDefineNonRemovableRecommendations());
}

/**
Expand All @@ -59,15 +59,13 @@ public function down(): void
}

// TODO : Optimize the process if possible
protected function seedNonRemovableRecommendations(): void
protected function seedNonRemovableRecommendations(array $nonRemovablerecommendations): void
{
$nonRemovablerecommendations = $this->systemDefineNonRemovableRecommendations();

if (empty($nonRemovablerecommendations)) {
return;
}

$currentLocale = Locale::getLocale();
// $currentLocale = Locale::getLocale();
$contextSupportedLocales = DB::table($this->recommendationInstallMigration->contextTable())
->select($this->recommendationInstallMigration->contextPrimaryKey())
->addSelect([
Expand All @@ -93,7 +91,6 @@ protected function seedNonRemovableRecommendations(): void
$recommendations[$recommendationValue] = [
'contextId' => null,
'value' => $recommendationValue,
'removable' => 0,
'status' => 1,
'title' => [],
];
Expand All @@ -111,14 +108,18 @@ protected function seedNonRemovableRecommendations(): void

foreach ($allContextSupportLocales as $locale) {

Locale::setLocale($locale);
// Locale::setLocale($locale);

foreach ($nonRemovablerecommendations as $recommendationValue => $translatableKey) {
$recommendations[$recommendationValue]['title'][$locale] = __($translatableKey);
$recommendations[$recommendationValue]['title'][$locale] = Locale::get(
$translatableKey,
[],
$locale
);
}
}

Locale::setLocale($currentLocale);
// Locale::setLocale($currentLocale);

$contextSupportedLocales->each(
fn (array $supportedLocales, int $contextId) => collect($recommendations)->each(
Expand All @@ -142,7 +143,7 @@ protected function seedNonRemovableRecommendations(): void
} catch (Throwable $exception) {

DB::rollBack();
Locale::setLocale($currentLocale);
// Locale::setLocale($currentLocale);
ReviewerRecommendation::reguard();

throw $exception;
Expand Down
1 change: 1 addition & 0 deletions classes/navigationMenu/NavigationMenuDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace PKP\navigationMenu;

use APP\core\Application;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use PKP\db\DAORegistry;
use PKP\db\DAOResultFactory;
Expand Down
25 changes: 24 additions & 1 deletion classes/services/PKPContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace PKP\services;

use APP\core\Application;
use Illuminate\Support\Arr;

use APP\core\Request;
use APP\facades\Repo;
use APP\file\PublicFileManager;
Expand Down Expand Up @@ -49,6 +51,7 @@
use PKP\services\interfaces\EntityWriteInterface;
use PKP\submission\GenreDAO;
use PKP\validation\ValidatorFactory;
use PKP\submission\reviewer\recommendation\ReviewerRecommendation;

abstract class PKPContextService implements EntityPropertyInterface, EntityReadInterface, EntityWriteInterface
{
Expand Down Expand Up @@ -522,6 +525,25 @@ public function add($context, $request)

$context = $this->get($context->getId());

$locales = array_merge(
Arr::wrap($context->getData('primaryLocale')),
$context->getData('supportedLocales')
);
foreach(ReviewerRecommendation::seedableRecommendations() as $recommendationValue => $translatableKey) {
ReviewerRecommendation::create([
'contextId' => $context->getId(),
'value' => $recommendationValue,
'status' => 1,
'title' => collect($locales)
->mapWithKeys(
fn (string $locale): array => [
$locale => Locale::get($translatableKey, [], $locale)
]
)
->toArray(),
]);
}

// Move uploaded files into place and update the settings
$supportedLocales = $context->getSupportedFormLocales();
$fileUploadProps = ['favicon', 'homepageImage', 'pageHeaderLogoImage'];
Expand Down Expand Up @@ -629,7 +651,8 @@ public function delete($context)
$genreDao->deleteByContextId($context->getId());

// TODO is it OK to delete without listening Model's delete-associated events (not loading each Model)?
Announcement::withContextIds([$context->getId])->delete();
Announcement::withContextIds([$context->getId()])->delete();
ReviewerRecommendation::query()->withContextId($context->getId())->delete();

Repo::highlight()
->getCollector()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ReviewerRecommendation extends Model
/**
* @copydoc \Illuminate\Database\Eloquent\Concerns\GuardsAttributes::$guarded
*/
// TODO : add `recommendation_id` and `removable` as guarded column once pkp/pkp-lib#10292 and pkp/pkp-lib#10562 merged
// TODO : add `recommendation_id` as guarded column once pkp/pkp-lib#10292 and pkp/pkp-lib#10562 merged
protected $guarded = [];

/**
Expand All @@ -39,7 +39,6 @@ protected function casts(): array
'value' => 'integer',
'context_id' => 'integer',
'status' => 'integer', // We cast the boolean to corresponding int e.g. true/false to 1/0
'removable' => 'boolean',
];
}

Expand All @@ -48,9 +47,9 @@ protected function casts(): array
*/
protected static function booted(): void
{
static::creating(function (Model $recommendation) {
$recommendation->value = $recommendation->value;
});
static::creating(
fn (self $recommendation) => $recommendation->value = $recommendation->value
);
}

/**
Expand All @@ -61,6 +60,21 @@ public function getSettingsTable(): string
return 'reviewer_recommendation_settings';
}

/**
* Get default recommendation seed data
*/
public static function seedableRecommendations(): array
{
return [
1 => 'reviewer.article.decision.accept', // SUBMISSION_REVIEWER_RECOMMENDATION_ACCEPT
2 => 'reviewer.article.decision.pendingRevisions', // SUBMISSION_REVIEWER_RECOMMENDATION_PENDING_REVISIONS
3 => 'reviewer.article.decision.resubmitHere', // SUBMISSION_REVIEWER_RECOMMENDATION_RESUBMIT_HERE
4 => 'reviewer.article.decision.resubmitElsewhere', // SUBMISSION_REVIEWER_RECOMMENDATION_RESUBMIT_ELSEWHERE
5 => 'reviewer.article.decision.decline', // SUBMISSION_REVIEWER_RECOMMENDATION_DECLINE
6 => 'reviewer.article.decision.seeComments', // SUBMISSION_REVIEWER_RECOMMENDATION_SEE_COMMENTS
];
}

/**
* @copydoc \PKP\core\traits\ModelWithSettings::getSchemaName
*/
Expand Down Expand Up @@ -101,7 +115,7 @@ protected function value(): Attribute
$existingRecommendation = static::query()
->withContextId($this->contextId)
->where('value', $value)
->first();
->exists();

if ($existingRecommendation) {
throw new Exception(
Expand Down Expand Up @@ -129,25 +143,11 @@ protected function value(): Attribute
protected function removable(): Attribute
{
return Attribute::make(
get: function () {
if (!$this->getRawOriginal('removable')) {
return false;
}

$reviewAssignmentCount = Repo::reviewAssignment()
->getCollector()
->filterByRecommenddations([$this->value])
->getCount();

return $reviewAssignmentCount === 0;
},
// TODO : MUST FIX ME !!! This cause issue at data seeding in migration process
set: function (bool $value) {
if (!is_null($this->getRawOriginal('removable'))) {
return $this->getRawOriginal('removable');
}
return $value;
}
get: fn () => Repo::reviewAssignment()
->getCollector()
->filterByRecommenddations([$this->value])
->getQueryBuilder()
->exists()
)->shouldCache();
}

Expand Down

0 comments on commit 05100d8

Please sign in to comment.