MOBILE-3235 core: Add enumeration defining available context levels

main
Noel De Martin 2019-11-28 17:18:13 +01:00
parent dcf33d4fc1
commit 46cc61cc01
2 changed files with 29 additions and 7 deletions

View File

@ -16,7 +16,7 @@ import { Component, OnInit, Injector, Input } from '@angular/core';
import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate';
import { CoreBlockBaseComponent } from '@core/block/classes/base-block-component'; import { CoreBlockBaseComponent } from '@core/block/classes/base-block-component';
import { CoreConstants } from '@core/constants'; import { CoreConstants, ContextLevel } from '@core/constants';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { CoreSitesProvider } from '@providers/sites'; import { CoreSitesProvider } from '@providers/sites';
@ -29,7 +29,7 @@ import { CoreSitesProvider } from '@providers/sites';
}) })
export class AddonBlockActivityModulesComponent extends CoreBlockBaseComponent implements OnInit { export class AddonBlockActivityModulesComponent extends CoreBlockBaseComponent implements OnInit {
@Input() block: any; // The block to render. @Input() block: any; // The block to render.
@Input() contextLevel: string; // The context where the block will be used. @Input() contextLevel: ContextLevel; // The context where the block will be used.
@Input() instanceId: number; // The instance ID associated with the context level. @Input() instanceId: number; // The instance ID associated with the context level.
entries: any[] = []; entries: any[] = [];
@ -65,11 +65,7 @@ export class AddonBlockActivityModulesComponent extends CoreBlockBaseComponent i
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
protected fetchContent(): Promise<any> { protected fetchContent(): Promise<any> {
const courseId = this.contextLevel === 'course' return this.courseProvider.getSections(this.getCourseId(), false, true).then((sections) => {
? this.instanceId
: this.sitesProvider.getCurrentSiteHomeId();
return this.courseProvider.getSections(courseId, false, true).then((sections) => {
this.entries = []; this.entries = [];
const archetypes = {}, const archetypes = {},
@ -127,4 +123,18 @@ export class AddonBlockActivityModulesComponent extends CoreBlockBaseComponent i
} }
}); });
} }
/**
* Obtain the appropiate course id for the block.
*
* @return Course id.
*/
protected getCourseId(): number {
switch (this.contextLevel) {
case ContextLevel.Course:
return this.instanceId;
default:
return this.sitesProvider.getCurrentSiteHomeId();
}
}
} }

View File

@ -12,6 +12,18 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
/**
* Context levels enumeration.
*/
export const enum ContextLevel {
System = 'system',
User = 'user',
Category = 'category',
Course = 'course',
ActivityModule = 'module',
Block = 'block',
}
/** /**
* Static class to contain all the core constants. * Static class to contain all the core constants.
*/ */