MOBILE-4498 comments: Use advanced features to check if comments are on

main
Pau Ferrer Ocaña 2024-02-28 16:17:57 +01:00
parent 65ed0d2b1c
commit 6a52b02eeb
11 changed files with 81 additions and 27 deletions

View File

@ -18,6 +18,7 @@ import { CoreBlockOnlyTitleComponent } from '@features/block/components/only-tit
import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler'; import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler';
import { CoreCourseBlock } from '@features/course/services/course'; import { CoreCourseBlock } from '@features/course/services/course';
import { makeSingleton } from '@singletons'; import { makeSingleton } from '@singletons';
import { CoreComments } from '@features/comments/services/comments';
/** /**
* Block handler. * Block handler.
@ -28,6 +29,13 @@ export class AddonBlockCommentsHandlerService extends CoreBlockBaseHandler {
name = 'AddonBlockComments'; name = 'AddonBlockComments';
blockName = 'comments'; blockName = 'comments';
/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return await CoreComments.areCommentsEnabled();
}
/** /**
* Returns the data needed to render the block. * Returns the data needed to render the block.
* *

View File

@ -139,7 +139,7 @@ export class AddonBlogEntriesPage implements OnInit {
this.contextInstanceId = 0; this.contextInstanceId = 0;
} }
this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); this.commentsEnabled = CoreComments.areCommentsEnabledInSite();
this.tagsEnabled = CoreTag.areTagsAvailableInSite(); this.tagsEnabled = CoreTag.areTagsAvailableInSite();
const deepLinkManager = new CoreMainMenuDeepLinkManager(); const deepLinkManager = new CoreMainMenuDeepLinkManager();

View File

@ -33,7 +33,7 @@ export class AddonModAssignSubmissionCommentsComponent extends AddonModAssignSub
constructor() { constructor() {
super(); super();
this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); this.commentsEnabled = CoreComments.areCommentsEnabledInSite();
} }
/** /**

View File

@ -157,7 +157,7 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy {
return; return;
} }
this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); this.commentsEnabled = CoreComments.areCommentsEnabledInSite();
await this.fetchEntryData(); await this.fetchEntryData();
} }

View File

@ -230,7 +230,7 @@ export class AddonModDataPrefetchHandlerService extends CoreCourseActivityPrefet
// Prefetch the database data. // Prefetch the database data.
const database = info.database; const database = info.database;
const commentsEnabled = !CoreComments.areCommentsDisabledInSite(); const commentsEnabled = CoreComments.areCommentsEnabledInSite();
const promises: Promise<unknown>[] = []; const promises: Promise<unknown>[] = [];

View File

@ -19,8 +19,8 @@ Feature: Users can manage entries in database activities
| student1 | C1 | student | | student1 | C1 | student |
| student2 | C1 | student | | student2 | C1 | student |
And the following "activities" exist: And the following "activities" exist:
| activity | name | intro | course | idnumber | | activity | name | intro | course | idnumber | comments |
| data | Web links | Useful links | C1 | data1 | | data | Web links | Useful links | C1 | data1 | 0 |
And the following "mod_data > fields" exist: And the following "mod_data > fields" exist:
| database | type | name | description | | database | type | name | description |
| data1 | text | URL | URL link | | data1 | text | URL | URL link |
@ -38,7 +38,13 @@ Feature: Users can manage entries in database activities
And I should find "Moodle community site" in the app And I should find "Moodle community site" in the app
Scenario: Browse entry Scenario: Browse entry
Given I entered the data activity "Web links" on course "Course 1" as "student1" in the app Given the following "activities" exist:
| activity | name | intro | course | idnumber | comments |
| data | Data with comments | - | C1 | data2 | 1 |
And the following "mod_data > fields" exist:
| database | type | name | description |
| data2 | text | Description | Description |
And I entered the data activity "Web links" on course "Course 1" as "student1" in the app
# TODO Create and use a generator for database entries. # TODO Create and use a generator for database entries.
When I press "Add entries" in the app When I press "Add entries" in the app
@ -54,6 +60,7 @@ Feature: Users can manage entries in database activities
And I press "Save" near "Web links" in the app And I press "Save" near "Web links" in the app
And I press "Show more" near "Moodle community site" in the app And I press "Show more" near "Moodle community site" in the app
Then I should find "Moodle community site" in the app Then I should find "Moodle community site" in the app
And I should not find "Comments" in the app
And I should be able to press "Previous" in the app And I should be able to press "Previous" in the app
But I should not be able to press "Next" in the app But I should not be able to press "Next" in the app
@ -70,6 +77,21 @@ Feature: Users can manage entries in database activities
And I should find "Moodle community site" in the app And I should find "Moodle community site" in the app
And I should find "Moodle Cloud" in the app And I should find "Moodle Cloud" in the app
Given I entered the data activity "Data with comments" on course "Course 1" as "student1" in the app
When I press "Add entries" in the app
And I set the following fields to these values in the app:
| Description | Moodle community site |
And I press "Save" near "Data with comments" in the app
And I press "Show more" near "Moodle community site" in the app
Then I should find "Moodle community site" in the app
And I should find "Comments" in the app
Given the following config values are set as admin:
| usecomments | 0 |
And I entered the data activity "Data with comments" on course "Course 1" as "student1" in the app
When I press "Show more" near "Moodle community site" in the app
Then I should not find "Comments" in the app
Scenario: Students can not edit or delete other user's entries from list and single view in the app Scenario: Students can not edit or delete other user's entries from list and single view in the app
Given I entered the data activity "Web links" on course "Course 1" as "student1" in the app Given I entered the data activity "Web links" on course "Course 1" as "student1" in the app
And I press "Add entries" in the app And I press "Add entries" in the app

View File

@ -99,7 +99,7 @@ export class AddonModGlossaryEntryPage implements OnInit, OnDestroy {
try { try {
this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId');
this.tagsEnabled = CoreTag.areTagsAvailableInSite(); this.tagsEnabled = CoreTag.areTagsAvailableInSite();
this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); this.commentsEnabled = CoreComments.areCommentsEnabledInSite();
this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId');
const entrySlug = CoreNavigator.getRequiredRouteParam<string>('entrySlug'); const entrySlug = CoreNavigator.getRequiredRouteParam<string>('entrySlug');

View File

@ -155,7 +155,7 @@ export class AddonModGlossaryPrefetchHandlerService extends CoreCourseActivityPr
options, options,
).then((entries) => { ).then((entries) => {
const promises: Promise<unknown>[] = []; const promises: Promise<unknown>[] = [];
const commentsEnabled = !CoreComments.areCommentsDisabledInSite(); const commentsEnabled = CoreComments.areCommentsEnabledInSite();
entries.forEach((entry) => { entries.forEach((entry) => {
// Don't fetch individual entries, it's too many WS calls. // Don't fetch individual entries, it's too many WS calls.

View File

@ -45,7 +45,7 @@ export class CoreCommentsCommentsComponent implements OnInit, OnChanges, OnDestr
commentsLoaded = false; commentsLoaded = false;
commentsCount = ''; commentsCount = '';
countError = false; countError = false;
disabled = false; enabled = false;
protected updateSiteObserver?: CoreEventObserver; protected updateSiteObserver?: CoreEventObserver;
protected refreshCommentsObserver?: CoreEventObserver; protected refreshCommentsObserver?: CoreEventObserver;
@ -55,15 +55,15 @@ export class CoreCommentsCommentsComponent implements OnInit, OnChanges, OnDestr
this.onLoading = new EventEmitter<boolean>(); this.onLoading = new EventEmitter<boolean>();
this.disabled = CoreComments.areCommentsDisabledInSite(); this.enabled = CoreComments.areCommentsEnabledInSite();
// Update visibility if current site info is updated. // Update visibility if current site info is updated.
this.updateSiteObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => { this.updateSiteObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => {
const wasDisabled = this.disabled; const wasEnabled = this.enabled;
this.disabled = CoreComments.areCommentsDisabledInSite(); this.enabled = CoreComments.areCommentsEnabledInSite();
if (wasDisabled && !this.disabled) { if (!wasEnabled && this.enabled) {
this.fetchData(); this.fetchData();
} }
}, CoreSites.getCurrentSiteId()); }, CoreSites.getCurrentSiteId());
@ -123,7 +123,7 @@ export class CoreCommentsCommentsComponent implements OnInit, OnChanges, OnDestr
* Fetch comments data. * Fetch comments data.
*/ */
async fetchData(): Promise<void> { async fetchData(): Promise<void> {
if (this.disabled) { if (!this.enabled) {
return; return;
} }
@ -173,12 +173,10 @@ export class CoreCommentsCommentsComponent implements OnInit, OnChanges, OnDestr
* Opens the comments page. * Opens the comments page.
*/ */
openComments(e?: Event): void { openComments(e?: Event): void {
if (e) { e?.preventDefault();
e.preventDefault(); e?.stopPropagation();
e.stopPropagation();
}
if (this.disabled || this.countError) { if (!this.enabled || this.countError) {
return; return;
} }

View File

@ -1,4 +1,4 @@
<ng-container *ngIf="!disabled"> <ng-container *ngIf="enabled">
<core-loading *ngIf="!showItem" [hideUntil]="commentsLoaded" [fullscreen]="false"> <core-loading *ngIf="!showItem" [hideUntil]="commentsLoaded" [fullscreen]="false">
<button *ngIf="!countError" (click)="openComments($event)" class="as-link"> <button *ngIf="!countError" (click)="openComments($event)" class="as-link">
{{ 'core.comments.commentscount' | translate : {'$a': commentsCount} }} {{ 'core.comments.commentscount' | translate : {'$a': commentsCount} }}

View File

@ -181,15 +181,14 @@ export class CoreCommentsProvider {
} }
/** /**
* Check if Calendar is disabled in a certain site. * Check if comments are disabled in a certain site.
* *
* @param site Site. If not defined, use current site. * @param site Site. If not defined, use current site.
* @returns Whether it's disabled. * @returns Whether it's disabled.
* @deprecated since 4.4. Use areCommentsEnabledInSite instead.
*/ */
areCommentsDisabledInSite(site?: CoreSite): boolean { areCommentsDisabledInSite(site?: CoreSite): boolean {
site = site || CoreSites.getCurrentSite(); return !this.areCommentsEnabledInSite(site);
return !!site?.isFeatureDisabled('NoDelegate_CoreComments');
} }
/** /**
@ -197,11 +196,38 @@ export class CoreCommentsProvider {
* *
* @param siteId Site Id. If not defined, use current site. * @param siteId Site Id. If not defined, use current site.
* @returns Promise resolved with true if disabled, rejected or resolved with false otherwise. * @returns Promise resolved with true if disabled, rejected or resolved with false otherwise.
* @deprecated since 4.4. Use areCommentsEnabled instead.
*/ */
async areCommentsDisabled(siteId?: string): Promise<boolean> { async areCommentsDisabled(siteId?: string): Promise<boolean> {
return !this.areCommentsEnabled(siteId);
}
/**
* Check if comments are enabled in a certain site.
*
* @param site Site. If not defined, use current site.
* @returns Whether it's enabled.
*/
areCommentsEnabledInSite(site?: CoreSite): boolean {
site = site || CoreSites.getCurrentSite();
if (!site) {
return false;
}
return site.canUseAdvancedFeature('usecomments') && !site.isFeatureDisabled('NoDelegate_CoreComments');
}
/**
* Check if comments are enabled in a certain site.
*
* @param siteId Site Id. If not defined, use current site.
* @returns Promise resolved with true if enabled, rejected or resolved with false otherwise.
*/
async areCommentsEnabled(siteId?: string): Promise<boolean> {
const site = await CoreSites.getSite(siteId); const site = await CoreSites.getSite(siteId);
return this.areCommentsDisabledInSite(site); return this.areCommentsEnabledInSite(site);
} }
/** /**
@ -323,7 +349,7 @@ export class CoreCommentsProvider {
const site = await CoreSites.getSite(siteId); const site = await CoreSites.getSite(siteId);
// First check if it's disabled. // First check if it's disabled.
if (this.areCommentsDisabledInSite(site)) { if (!this.areCommentsEnabledInSite(site)) {
return false; return false;
} }