Skip to content

Commit

Permalink
Merge pull request #3969 from crazyserver/MOBILE-4530
Browse files Browse the repository at this point in the history
Mobile 4530
  • Loading branch information
NoelDeMartin authored Mar 12, 2024
2 parents f7248f4 + 7252e70 commit 44b9f49
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { CoreSites } from '@services/sites';
import { CoreBlockBaseComponent } from '@features/block/classes/base-block-component';
import {
AddonBlockRecentlyAccessedItems,
AddonBlockRecentlyAccessedItemsItem,
AddonBlockRecentlyAccessedItemsItemCalculatedData,
} from '../../services/recentlyaccesseditems';
import { CoreTextUtils } from '@services/utils/text';
import { CoreDomUtils } from '@services/utils/dom';
Expand All @@ -33,7 +33,7 @@ import { CoreUtils } from '@services/utils/utils';
})
export class AddonBlockRecentlyAccessedItemsComponent extends CoreBlockBaseComponent implements OnInit {

items: AddonBlockRecentlyAccessedItemsItem[] = [];
items: AddonBlockRecentlyAccessedItemsItemCalculatedData[] = [];
scrollElementId!: string;

protected fetchContentDefaultError = 'Error getting recently accessed items data.';
Expand Down Expand Up @@ -79,7 +79,7 @@ export class AddonBlockRecentlyAccessedItemsComponent extends CoreBlockBaseCompo
* @param item Activity item info.
* @returns Promise resolved when done.
*/
async action(e: Event, item: AddonBlockRecentlyAccessedItemsItem): Promise<void> {
async action(e: Event, item: AddonBlockRecentlyAccessedItemsItemCalculatedData): Promise<void> {
e.preventDefault();
e.stopPropagation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,35 @@ export class AddonBlockRecentlyAccessedItemsProvider {
}

/**
* Get last accessed items.
* Get last accessed items from WS.
*
* @param siteId Site ID. If not defined, use current site.
* @returns Promise resolved when the info is retrieved.
*/
async getRecentItems(siteId?: string): Promise<AddonBlockRecentlyAccessedItemsItem[]> {
protected async getRecentItemsWS(siteId?: string): Promise<AddonBlockRecentlyaccesseditemsGetRecentItemsWSResponse[]> {
const site = await CoreSites.getSite(siteId);

const preSets: CoreSiteWSPreSets = {
cacheKey: this.getRecentItemsCacheKey(),
};

let items: AddonBlockRecentlyAccessedItemsItem[] =
await site.read('block_recentlyaccesseditems_get_recent_items', undefined, preSets);
return await site.read('block_recentlyaccesseditems_get_recent_items', undefined, preSets);
}

/**
* Get last accessed items.
*
* @param siteId Site ID. If not defined, use current site.
* @returns Promise resolved when the info is retrieved with some calculated data.
*/
async getRecentItems(siteId?: string): Promise<AddonBlockRecentlyAccessedItemsItemCalculatedData[]> {
const site = await CoreSites.getSite(siteId);

const items = await this.getRecentItemsWS(site.getId());

const cmIds: number[] = [];

items = await Promise.all(items.map(async (item) => {
const itemsToDisplay = await Promise.all(items.map(async (item: AddonBlockRecentlyAccessedItemsItemCalculatedData) => {
const modicon = item.icon && CoreDomUtils.getHTMLElementAttribute(item.icon, 'src');

item.iconUrl = await CoreCourseModuleDelegate.getModuleIconSrc(item.modname, modicon || undefined);
Expand All @@ -68,7 +79,7 @@ export class AddonBlockRecentlyAccessedItemsProvider {
// Check if the viewed module should be updated for each activity.
const lastViewedMap = await CoreCourse.getCertainModulesViewed(cmIds, site.getId());

items.forEach((recentItem) => {
itemsToDisplay.forEach((recentItem) => {
const timeAccess = recentItem.timeaccess * 1000;
const lastViewed = lastViewedMap[recentItem.cmid];

Expand All @@ -84,7 +95,7 @@ export class AddonBlockRecentlyAccessedItemsProvider {
});
});

return items;
return itemsToDisplay;
}

/**
Expand All @@ -103,11 +114,11 @@ export class AddonBlockRecentlyAccessedItemsProvider {
export const AddonBlockRecentlyAccessedItems = makeSingleton(AddonBlockRecentlyAccessedItemsProvider);

/**
* Result of WS block_recentlyaccesseditems_get_recent_items.
* Data returned by block_recentlyaccesseditems_get_recent_items WS.
*
* The most recently accessed activities/resources by the logged user.
*/
export type AddonBlockRecentlyAccessedItemsItem = {
type AddonBlockRecentlyaccesseditemsGetRecentItemsWSResponse = {
id: number; // Id.
courseid: number; // Courseid.
cmid: number; // Cmid.
Expand All @@ -120,12 +131,13 @@ export type AddonBlockRecentlyAccessedItemsItem = {
courseviewurl: string; // Courseviewurl.
icon: string; // Icon.
purpose?: string; // Purpose. @since 4.0
} & AddonBlockRecentlyAccessedItemsItemCalculatedData;
branded?: boolean; // Branded. @since 4.4
};

/**
* Calculated data for recently accessed item.
*/
export type AddonBlockRecentlyAccessedItemsItemCalculatedData = {
export type AddonBlockRecentlyAccessedItemsItemCalculatedData = AddonBlockRecentlyaccesseditemsGetRecentItemsWSResponse & {
iconUrl: string; // Icon URL. Calculated by the app.
iconTitle?: string | null; // Icon title.
};
103 changes: 86 additions & 17 deletions src/addons/block/timeline/services/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@
import { Injectable } from '@angular/core';
import { CoreSites } from '@services/sites';
import {
AddonCalendarEvents,
AddonCalendarEventsGroupedByCourse,
AddonCalendarEvent,
AddonCalendarGetActionEventsByCourseWSParams,
AddonCalendarGetActionEventsByTimesortWSParams,
AddonCalendarGetActionEventsByCoursesWSParams,
} from '@addons/calendar/services/calendar';
import moment from 'moment-timezone';
import { makeSingleton } from '@singletons';
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';

// Cache key was maintained from block myoverview when blocks were splitted.
const ROOT_CACHE_KEY = 'myoverview:';

/**
* Service that provides some features regarding course overview.
*/
@Injectable({ providedIn: 'root' })
export class AddonBlockTimelineProvider {

// Cache key was maintained from block myoverview when blocks were splitted.
protected static readonly ROOT_CACHE_KEY = 'myoverview:';

static readonly EVENTS_LIMIT = 20;
static readonly EVENTS_LIMIT_PER_COURSE = 10;

Expand All @@ -57,7 +52,7 @@ export class AddonBlockTimelineProvider {

const time = this.getDayStart(-14); // Check two weeks ago.

const data: AddonCalendarGetActionEventsByCourseWSParams = {
const data: AddonBlockTimelineGetActionEventsByCourseWSParams = {
timesortfrom: time,
courseid: courseId,
limitnum: AddonBlockTimelineProvider.EVENTS_LIMIT_PER_COURSE,
Expand All @@ -75,7 +70,7 @@ export class AddonBlockTimelineProvider {
preSets.getFromCache = false;
}

const courseEvents = await site.read<AddonCalendarEvents>(
const courseEvents = await site.read<AddonBlockTimelineGetActionEventsByCourseWSResponse>(
'core_calendar_get_action_events_by_course',
data,
preSets,
Expand Down Expand Up @@ -115,7 +110,7 @@ export class AddonBlockTimelineProvider {

const time = this.getDayStart(-14); // Check two weeks ago.

const data: AddonCalendarGetActionEventsByCoursesWSParams = {
const data: AddonBlockTimelineGetActionEventsByCoursesWSParams = {
timesortfrom: time,
courseids: courseIds,
limitnum: AddonBlockTimelineProvider.EVENTS_LIMIT_PER_COURSE,
Expand All @@ -129,7 +124,7 @@ export class AddonBlockTimelineProvider {
preSets.getFromCache = false;
}

const events = await site.read<AddonCalendarEventsGroupedByCourse>(
const events = await site.read<AddonBlockTimelineGetActionEventsByCoursesWSResponse>(
'core_calendar_get_action_events_by_courses',
data,
preSets,
Expand All @@ -150,7 +145,7 @@ export class AddonBlockTimelineProvider {
* @returns Cache key.
*/
protected getActionEventsByCoursesCacheKey(): string {
return ROOT_CACHE_KEY + 'bycourse';
return AddonBlockTimelineProvider.ROOT_CACHE_KEY + 'bycourse';
}

/**
Expand All @@ -171,7 +166,7 @@ export class AddonBlockTimelineProvider {
const timesortfrom = this.getDayStart(-14); // Check two weeks ago.
const limitnum = AddonBlockTimelineProvider.EVENTS_LIMIT;

const data: AddonCalendarGetActionEventsByTimesortWSParams = {
const data: AddonBlockTimelineGetActionEventsByTimesortWSParams = {
timesortfrom,
limitnum,
limittononsuspendedevents: true,
Expand All @@ -192,7 +187,7 @@ export class AddonBlockTimelineProvider {
preSets.cacheKey += ':' + searchValue;
}

const result = await site.read<AddonCalendarEvents>(
const result = await site.read<AddonBlockTimelineGetActionEventsByTimesortWSResponse>(
'core_calendar_get_action_events_by_timesort',
data,
preSets,
Expand All @@ -215,7 +210,7 @@ export class AddonBlockTimelineProvider {
* @returns Cache key.
*/
protected getActionEventsByTimesortPrefixCacheKey(): string {
return ROOT_CACHE_KEY + 'bytimesort:';
return AddonBlockTimelineProvider.ROOT_CACHE_KEY + 'bytimesort:';
}

/**
Expand Down Expand Up @@ -264,7 +259,7 @@ export class AddonBlockTimelineProvider {
* @returns Object with course events and last loaded event id if more can be loaded.
*/
protected treatCourseEvents(
course: AddonCalendarEvents,
course: AddonBlockTimelineEvents,
timeFrom: number,
): { events: AddonCalendarEvent[]; canLoadMore?: number } {

Expand Down Expand Up @@ -293,3 +288,77 @@ export class AddonBlockTimelineProvider {
}

export const AddonBlockTimeline = makeSingleton(AddonBlockTimelineProvider);

/**
* Params of core_calendar_get_action_events_by_timesort WS.
*/
type AddonBlockTimelineGetActionEventsByTimesortWSParams = {
timesortfrom?: number; // Time sort from.
timesortto?: number; // Time sort to.
aftereventid?: number; // The last seen event id.
limitnum?: number; // Limit number.
limittononsuspendedevents?: boolean; // Limit the events to courses the user is not suspended in.
userid?: number; // The user id.
searchvalue?: string; // The value a user wishes to search against.
};

/**
* Data returned by core_calendar_get_action_events_by_timesort WS.
*
* WS Description: Get calendar action events by tiemsort
*/
type AddonBlockTimelineGetActionEventsByTimesortWSResponse = AddonBlockTimelineEvents;

/**
* Params of core_calendar_get_action_events_by_course WS.
*/
type AddonBlockTimelineGetActionEventsByCourseWSParams = {
courseid: number; // Course id.
timesortfrom?: number; // Time sort from.
timesortto?: number; // Time sort to.
aftereventid?: number; // The last seen event id.
limitnum?: number; // Limit number.
searchvalue?: string; // The value a user wishes to search against.
};

/**
* Params of core_calendar_get_action_events_by_courses WS.
*/
type AddonBlockTimelineGetActionEventsByCoursesWSParams = {
courseids: number[];
timesortfrom?: number; // Time sort from.
timesortto?: number; // Time sort to.
limitnum?: number; // Limit number.
searchvalue?: string; // The value a user wishes to search against.
};

/**
* Data returned by calendar's events_grouped_by_course_exporter.
* Data returned by core_calendar_get_action_events_by_courses WS.
*/
type AddonBlockTimelineGetActionEventsByCoursesWSResponse = {
groupedbycourse: AddonBlockTimelineEventsSameCourse[]; // Groupped by course.
};

/**
* Data returned by calendar's events_same_course_exporter.
*/
type AddonBlockTimelineEventsSameCourse = AddonBlockTimelineEvents & {
courseid: number; // Courseid.
};

/**
* Data returned by core_calendar_get_action_events_by_course WS.
*
* WS Description: Get calendar action events by course
*/
type AddonBlockTimelineGetActionEventsByCourseWSResponse = AddonBlockTimelineEvents;

/**
* Data returned by calendar's events_exporter.
*/
export type AddonBlockTimelineEvents = {
events: AddonCalendarEvent[]; // Events.
firstid: number; // Firstid.
lastid: number; // Lastid.
};
3 changes: 1 addition & 2 deletions src/addons/calendar/pages/event/event.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ <h1 *ngIf="event">
<ion-list *ngIf="event">
<ion-item class="ion-text-wrap addon-calendar-event" collapsible [ngClass]="['addon-calendar-eventtype-'+event.eventtype]">
<core-mod-icon *ngIf="event.moduleIcon" [modicon]="event.moduleIcon" [showAlt]="false" [modname]="event.modulename"
[componentId]="event.instance" slot="start" [purpose]="event.purpose" />
<!-- TODO MOBILE-4530 Add isBranded when available -->
[componentId]="event.instance" slot="start" [purpose]="event.purpose" [isBranded]="event.branded" />
<ion-icon *ngIf=" event.eventIcon && !event.moduleIcon" [name]="event.eventIcon" aria-hidden="true" slot="start" />
<ion-label>
<!-- Add the icon title so accessibility tools read it. -->
Expand Down
Loading

0 comments on commit 44b9f49

Please sign in to comment.