MOBILE-2992 forum: Sort discussions
parent
65055653be
commit
da2a835670
|
@ -483,6 +483,12 @@
|
|||
"addon.mod_forum.addanewtopic": "forum",
|
||||
"addon.mod_forum.addtofavourites": "forum",
|
||||
"addon.mod_forum.advanced": "forum",
|
||||
"addon.mod_forum.bycreateddesc": "local_moodlemobileapp",
|
||||
"addon.mod_forum.bycreatedasc": "local_moodlemobileapp",
|
||||
"addon.mod_forum.bylastpostdesc": "local_moodlemobileapp",
|
||||
"addon.mod_forum.bylastpostasc": "local_moodlemobileapp",
|
||||
"addon.mod_forum.byrepliesdesc": "local_moodlemobileapp",
|
||||
"addon.mod_forum.byrepliesasc": "local_moodlemobileapp",
|
||||
"addon.mod_forum.cannotadddiscussion": "forum",
|
||||
"addon.mod_forum.cannotadddiscussionall": "forum",
|
||||
"addon.mod_forum.cannotcreatediscussion": "forum",
|
||||
|
@ -1734,6 +1740,7 @@
|
|||
"core.sizemb": "moodle",
|
||||
"core.sizetb": "local_moodlemobileapp",
|
||||
"core.sorry": "local_moodlemobileapp",
|
||||
"core.sort": "moodle",
|
||||
"core.sortby": "moodle",
|
||||
"core.start": "grouptool",
|
||||
"core.strftimedate": "langconfig",
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
<core-context-menu-item *ngIf="loaded && (hasOffline || hasOfflineRatings) && isOnline" [priority]="600" [content]="'core.settings.synchronizenow' | translate" (action)="doRefresh(null, $event, true)" [iconAction]="syncIcon" [closeOnClick]="false"></core-context-menu-item>
|
||||
<core-context-menu-item *ngIf="prefetchStatusIcon" [priority]="500" [content]="prefetchText" (action)="prefetch()" [iconAction]="prefetchStatusIcon" [closeOnClick]="false"></core-context-menu-item>
|
||||
<core-context-menu-item *ngIf="size" [priority]="400" [content]="size" [iconDescription]="'cube'" (action)="removeFiles()" [iconAction]="'trash'"></core-context-menu-item>
|
||||
<core-context-menu-item *ngIf="sortingAvailable" [priority]="300" [content]="'core.sort' | translate" (action)="showSortOrderSelector($event)" iconAction="fa-sort"></core-context-menu-item>
|
||||
</core-context-menu>
|
||||
</core-navbar-buttons>
|
||||
|
||||
|
@ -30,6 +31,21 @@
|
|||
<ion-icon name="information-circle"></ion-icon> {{ availabilityMessage }}
|
||||
</ion-card>
|
||||
|
||||
<core-empty-box *ngIf="forum && discussions.length == 0" icon="chatbubbles" [message]="'addon.mod_forum.forumnodiscussionsyet' | translate" class="core-empty-box-clickable">
|
||||
<div padding *ngIf="forum.cancreatediscussions">
|
||||
<button ion-button block (click)="openNewDiscussion()">
|
||||
{{ 'addon.mod_forum.addanewdiscussion' | translate }}
|
||||
</button>
|
||||
</div>
|
||||
</core-empty-box>
|
||||
|
||||
<div text-wrap *ngIf="sortingAvailable && selectedSortOrder" ion-row padding-horizontal padding-top margin-bottom>
|
||||
<button *ngIf="sortingAvailable" ion-button padding-horizontal icon-end ion-col (click)="showSortOrderSelector($event)" color="light" class="core-button-select button-no-uppercase" [attr.aria-label]="('core.sort' | translate)" aria-haspopup="true" [attr.aria-expanded]="sortOrderSelectorExpanded" aria-controls="addon-mod-forum-sort-order-selector" id="addon-mod-forum-sort-order-button">
|
||||
<span class="core-section-selector-text">{{ selectedSortOrder.label | translate }}</span>
|
||||
<ion-icon name="arrow-dropdown" ios="md-arrow-dropdown"></ion-icon>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<ng-container *ngIf="forum && discussions.length > 0">
|
||||
<ion-card *ngFor="let discussion of offlineDiscussions" (click)="openNewDiscussion(discussion.timecreated)" [class.addon-forum-discussion-selected]="discussion.timecreated == -selectedDiscussion">
|
||||
<ion-item text-wrap>
|
||||
|
@ -86,14 +102,6 @@
|
|||
</ion-card>
|
||||
</ng-container>
|
||||
|
||||
<core-empty-box *ngIf="forum && discussions.length == 0" icon="chatbubbles" [message]="'addon.mod_forum.forumnodiscussionsyet' | translate" class="core-empty-box-clickable">
|
||||
<div padding *ngIf="forum.cancreatediscussions">
|
||||
<button ion-button block (click)="openNewDiscussion()">
|
||||
{{ 'addon.mod_forum.addanewdiscussion' | translate }}
|
||||
</button>
|
||||
</div>
|
||||
</core-empty-box>
|
||||
|
||||
<core-infinite-loading [enabled]="canLoadMore" (action)="fetchMoreDiscussions($event)" [error]="loadMoreError"></core-infinite-loading>
|
||||
</core-loading>
|
||||
|
||||
|
|
|
@ -2,7 +2,15 @@ ion-app.app-root addon-mod-forum-index {
|
|||
.addon-forum-discussion-selected {
|
||||
border-top: 5px solid $core-splitview-selected;
|
||||
}
|
||||
|
||||
.addon-forum-star {
|
||||
color: $core-star-color;
|
||||
}
|
||||
|
||||
button.core-button-select .core-section-selector-text {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
line-height: 2em;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { Component, Optional, Injector, ViewChild } from '@angular/core';
|
||||
import { Content, NavController } from 'ionic-angular';
|
||||
import { Content, ModalController, NavController } from 'ionic-angular';
|
||||
import { CoreSplitViewComponent } from '@components/split-view/split-view';
|
||||
import { CoreCourseModuleMainActivityComponent } from '@core/course/classes/main-activity-component';
|
||||
import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate';
|
||||
|
@ -52,6 +52,11 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
addDiscussionText = this.translate.instant('addon.mod_forum.addanewdiscussion');
|
||||
availabilityMessage: string;
|
||||
|
||||
sortingAvailable: boolean;
|
||||
sortOrders = [];
|
||||
selectedSortOrder = null;
|
||||
sortOrderSelectorExpanded = false;
|
||||
|
||||
protected syncEventName = AddonModForumSyncProvider.AUTO_SYNCED;
|
||||
protected page = 0;
|
||||
protected trackPosts = false;
|
||||
|
@ -69,6 +74,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
constructor(injector: Injector,
|
||||
@Optional() protected content: Content,
|
||||
protected navCtrl: NavController,
|
||||
protected modalCtrl: ModalController,
|
||||
protected groupsProvider: CoreGroupsProvider,
|
||||
protected userProvider: CoreUserProvider,
|
||||
protected forumProvider: AddonModForumProvider,
|
||||
|
@ -79,6 +85,9 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
protected prefetchHandler: AddonModForumPrefetchHandler,
|
||||
protected ratingOffline: CoreRatingOfflineProvider) {
|
||||
super(injector);
|
||||
|
||||
this.sortingAvailable = this.forumProvider.isDiscussionListSortingAvailable();
|
||||
this.sortOrders = this.forumProvider.getAvailableSortOrders();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,7 +171,9 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
protected fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<any> {
|
||||
this.loadMoreError = false;
|
||||
|
||||
return this.forumProvider.getForum(this.courseId, this.module.id).then((forum) => {
|
||||
const promises = [];
|
||||
|
||||
promises.push(this.forumProvider.getForum(this.courseId, this.module.id).then((forum) => {
|
||||
this.forum = forum;
|
||||
|
||||
this.description = forum.intro || this.description;
|
||||
|
@ -212,7 +223,11 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
this.canAddDiscussion = this.forum.cancreatediscussions && !cutoffDateReached;
|
||||
}),
|
||||
]);
|
||||
}).then(() => {
|
||||
}));
|
||||
|
||||
promises.push(this.fetchSortOrderPreference());
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
return Promise.all([
|
||||
this.fetchOfflineDiscussion(),
|
||||
this.fetchDiscussions(refresh),
|
||||
|
@ -291,7 +306,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
this.page = 0;
|
||||
}
|
||||
|
||||
return this.forumProvider.getDiscussions(this.forum.id, this.page).then((response) => {
|
||||
return this.forumProvider.getDiscussions(this.forum.id, this.selectedSortOrder.value, this.page).then((response) => {
|
||||
let promise;
|
||||
if (this.usesGroups) {
|
||||
promise = this.forumProvider.formatDiscussionsGroups(this.forum.cmid, response.discussions);
|
||||
|
@ -366,6 +381,27 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to fetch the sort order preference.
|
||||
*
|
||||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
protected fetchSortOrderPreference(): Promise<any> {
|
||||
let promise;
|
||||
if (this.sortingAvailable) {
|
||||
promise = this.userProvider.getUserPreference(AddonModForumProvider.PREFERENCE_SORTORDER).then((value) => {
|
||||
return value ? parseInt(value, 10) : null;
|
||||
});
|
||||
} else {
|
||||
// Use default.
|
||||
promise = Promise.resolve(null);
|
||||
}
|
||||
|
||||
return promise.then((value) => {
|
||||
this.selectedSortOrder = this.sortOrders.find((sortOrder) => sortOrder.value === value) || this.sortOrders[0];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the invalidate content function.
|
||||
*
|
||||
|
@ -382,6 +418,10 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
promises.push(this.forumProvider.invalidateAccessInformation(this.forum.id));
|
||||
}
|
||||
|
||||
if (this.sortingAvailable) {
|
||||
promises.push(this.userProvider.invalidateUserPreference(AddonModForumProvider.PREFERENCE_SORTORDER));
|
||||
}
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
|
@ -484,6 +524,37 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
|
|||
this.selectedDiscussion = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the sort order selector modal.
|
||||
*
|
||||
* @param {MouseEvent} event Event.
|
||||
*/
|
||||
showSortOrderSelector(event: MouseEvent): void {
|
||||
if (!this.sortingAvailable) {
|
||||
return;
|
||||
}
|
||||
|
||||
const params = { sortOrders: this.sortOrders, selected: this.selectedSortOrder.value };
|
||||
const modal = this.modalCtrl.create('AddonModForumSortOrderSelectorPage', params);
|
||||
modal.onDidDismiss((sortOrder) => {
|
||||
this.sortOrderSelectorExpanded = false;
|
||||
|
||||
if (sortOrder && sortOrder.value != this.selectedSortOrder.value) {
|
||||
this.selectedSortOrder = sortOrder;
|
||||
this.page = 0;
|
||||
this.userProvider.setUserPreference(AddonModForumProvider.PREFERENCE_SORTORDER, sortOrder.value.toFixed(0))
|
||||
.then(() => {
|
||||
this.showLoadingAndFetch();
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModalDefault(error, 'Error updating preference.');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
modal.present({ev: event});
|
||||
this.sortOrderSelectorExpanded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being destroyed.
|
||||
*/
|
||||
|
|
|
@ -4,6 +4,12 @@
|
|||
"addanewtopic": "Add a new topic",
|
||||
"addtofavourites": "Star this discussion",
|
||||
"advanced": "Advanced",
|
||||
"bycreateddesc": "By creation date in descending order",
|
||||
"bycreatedasc": "By creation date in ascending order",
|
||||
"bylastpostdesc": "By last post in descending order",
|
||||
"bylastpostasc": "By last post in ascending order",
|
||||
"byrepliesdesc": "By number of replies in descending order",
|
||||
"byrepliesasc": "By number of replies in ascending order",
|
||||
"cannotadddiscussion": "Adding discussions to this forum requires group membership.",
|
||||
"cannotadddiscussionall": "You do not have permission to add a new discussion topic for all participants.",
|
||||
"cannotcreatediscussion": "Could not create new discussion",
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<ion-header>
|
||||
<ion-navbar core-back-button>
|
||||
<ion-title>{{ 'core.sort' | translate }}</ion-title>
|
||||
<ion-buttons end>
|
||||
<button ion-button icon-only (click)="closeModal()" [attr.aria-label]="'core.close' | translate">
|
||||
<ion-icon name="close"></ion-icon>
|
||||
</button>
|
||||
</ion-buttons>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
<ion-content>
|
||||
<ion-list id="addon-mod-forum-sort-selector" role="menu" aria-labelledby="addon-mod-forum-sort-order-button">
|
||||
<ng-container *ngFor="let sortOrder of sortOrders">
|
||||
<a ion-item text-wrap (click)="selectSortOrder(sortOrder)" [class.core-primary-selected-item]="selected == sortOrder.value" detail-none role="menuitem" [attr.aria-label]="sortOrder.label | translate">
|
||||
<h2><core-format-text [text]="sortOrder.label | translate"></core-format-text></h2>
|
||||
</a>
|
||||
</ng-container>
|
||||
</ion-list>
|
||||
</ion-content>
|
|
@ -0,0 +1,33 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { IonicPageModule } from 'ionic-angular';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CoreComponentsModule } from '@components/components.module';
|
||||
import { CoreDirectivesModule } from '@directives/directives.module';
|
||||
import { AddonModForumSortOrderSelectorPage } from './sort-order-selector';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AddonModForumSortOrderSelectorPage,
|
||||
],
|
||||
imports: [
|
||||
CoreComponentsModule,
|
||||
CoreDirectivesModule,
|
||||
IonicPageModule.forChild(AddonModForumSortOrderSelectorPage),
|
||||
TranslateModule.forChild()
|
||||
],
|
||||
})
|
||||
export class AddonModForumSortOrderSelectorPagePageModule {}
|
|
@ -0,0 +1,51 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Component } from '@angular/core';
|
||||
import { IonicPage, NavParams, ViewController } from 'ionic-angular';
|
||||
|
||||
/**
|
||||
* Page that displays the sort selector.
|
||||
*/
|
||||
@IonicPage({ segment: 'addon-mod-forum-sort-order-selector' })
|
||||
@Component({
|
||||
selector: 'page-addon-mod-forum-sort-order-selector',
|
||||
templateUrl: 'sort-order-selector.html',
|
||||
})
|
||||
export class AddonModForumSortOrderSelectorPage {
|
||||
|
||||
sortOrders = [];
|
||||
selected: number;
|
||||
|
||||
constructor(navParams: NavParams, private viewCtrl: ViewController) {
|
||||
this.sortOrders = navParams.get('sortOrders');
|
||||
this.selected = navParams.get('selected');
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the modal.
|
||||
*/
|
||||
closeModal(): void {
|
||||
this.viewCtrl.dismiss();
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a sort order.
|
||||
*
|
||||
* @param {any} sortOrder Selected sort order.
|
||||
*/
|
||||
selectSortOrder(sortOrder: any): void {
|
||||
this.viewCtrl.dismiss(sortOrder);
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreSite } from '@classes/site';
|
||||
import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
|
||||
import { CoreAppProvider } from '@providers/app';
|
||||
import { CoreFilepoolProvider } from '@providers/filepool';
|
||||
import { CoreGroupsProvider } from '@providers/groups';
|
||||
|
@ -38,6 +38,14 @@ export class AddonModForumProvider {
|
|||
static CHANGE_DISCUSSION_EVENT = 'addon_mod_forum_lock_discussion';
|
||||
static MARK_READ_EVENT = 'addon_mod_forum_mark_read';
|
||||
|
||||
static PREFERENCE_SORTORDER = 'forum_discussionlistsortorder';
|
||||
static SORTORDER_LASTPOST_DESC = 1;
|
||||
static SORTORDER_LASTPOST_ASC = 2;
|
||||
static SORTORDER_CREATED_DESC = 3;
|
||||
static SORTORDER_CREATED_ASC = 4;
|
||||
static SORTORDER_REPLIES_DESC = 5;
|
||||
static SORTORDER_REPLIES_ASC = 6;
|
||||
|
||||
protected ROOT_CACHE_KEY = 'mmaModForum:';
|
||||
|
||||
constructor(private appProvider: CoreAppProvider,
|
||||
|
@ -105,10 +113,17 @@ export class AddonModForumProvider {
|
|||
* Get cache key for forum discussions list WS calls.
|
||||
*
|
||||
* @param {number} forumId Forum ID.
|
||||
* @param {number} sortOrder Sort order.
|
||||
* @return {string} Cache key.
|
||||
*/
|
||||
protected getDiscussionsListCacheKey(forumId: number): string {
|
||||
return this.ROOT_CACHE_KEY + 'discussions:' + forumId;
|
||||
protected getDiscussionsListCacheKey(forumId: number, sortOrder: number): string {
|
||||
let key = this.ROOT_CACHE_KEY + 'discussions:' + forumId;
|
||||
|
||||
if (sortOrder != AddonModForumProvider.SORTORDER_LASTPOST_DESC) {
|
||||
key += ':' + sortOrder;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -452,10 +467,64 @@ export class AddonModForumProvider {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether discussion lists can be sorted.
|
||||
*
|
||||
* @param {CoreSite} [site] Site. If not defined, current site.
|
||||
* @return {boolean} True if discussion lists can be sorted.
|
||||
*/
|
||||
isDiscussionListSortingAvailable(site?: CoreSite): boolean {
|
||||
site = site || this.sitesProvider.getCurrentSite();
|
||||
|
||||
return site.isVersionGreaterEqualThan('3.7');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of available sort orders.
|
||||
*
|
||||
* @return {{label: string, value: number}[]} List of sort orders.
|
||||
*/
|
||||
getAvailableSortOrders(): {label: string, value: number}[] {
|
||||
const sortOrders = [
|
||||
{
|
||||
label: 'addon.mod_forum.bylastpostdesc',
|
||||
value: AddonModForumProvider.SORTORDER_LASTPOST_DESC
|
||||
},
|
||||
];
|
||||
|
||||
if (this.isDiscussionListSortingAvailable()) {
|
||||
sortOrders.push(
|
||||
{
|
||||
label: 'addon.mod_forum.bylastpostasc',
|
||||
value: AddonModForumProvider.SORTORDER_LASTPOST_ASC
|
||||
},
|
||||
{
|
||||
label: 'addon.mod_forum.bycreateddesc',
|
||||
value: AddonModForumProvider.SORTORDER_CREATED_DESC
|
||||
},
|
||||
{
|
||||
label: 'addon.mod_forum.bycreatedasc',
|
||||
value: AddonModForumProvider.SORTORDER_CREATED_ASC
|
||||
},
|
||||
{
|
||||
label: 'addon.mod_forum.byrepliesdesc',
|
||||
value: AddonModForumProvider.SORTORDER_REPLIES_DESC
|
||||
},
|
||||
{
|
||||
label: 'addon.mod_forum.byrepliesasc',
|
||||
value: AddonModForumProvider.SORTORDER_REPLIES_ASC
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return sortOrders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get forum discussions.
|
||||
*
|
||||
* @param {number} forumId Forum ID.
|
||||
* @param {number} [sortOrder] Sort order.
|
||||
* @param {number} [page=0] Page.
|
||||
* @param {boolean} [forceCache] True to always get the value from cache. false otherwise.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
|
@ -463,23 +532,59 @@ export class AddonModForumProvider {
|
|||
* - discussions: List of discussions.
|
||||
* - canLoadMore: True if there may be more discussions to load.
|
||||
*/
|
||||
getDiscussions(forumId: number, page: number = 0, forceCache?: boolean, siteId?: string): Promise<any> {
|
||||
getDiscussions(forumId: number, sortOrder?: number, page: number = 0, forceCache?: boolean, siteId?: string): Promise<any> {
|
||||
sortOrder = sortOrder || AddonModForumProvider.SORTORDER_LASTPOST_DESC;
|
||||
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const params = {
|
||||
let method = 'mod_forum_get_forum_discussions_paginated';
|
||||
const params: any = {
|
||||
forumid: forumId,
|
||||
sortby: 'timemodified',
|
||||
sortdirection: 'DESC',
|
||||
page: page,
|
||||
perpage: AddonModForumProvider.DISCUSSIONS_PER_PAGE
|
||||
};
|
||||
const preSets: any = {
|
||||
cacheKey: this.getDiscussionsListCacheKey(forumId)
|
||||
|
||||
if (site.wsAvailable('mod_forum_get_forum_discussions')) {
|
||||
// Since Moodle 3.7.
|
||||
method = 'mod_forum_get_forum_discussions';
|
||||
params.sortorder = sortOrder;
|
||||
} else {
|
||||
if (sortOrder == AddonModForumProvider.SORTORDER_LASTPOST_DESC) {
|
||||
params.sortby = 'timemodified';
|
||||
params.sortdirection = 'DESC';
|
||||
} else {
|
||||
// Sorting not supported with the old WS method.
|
||||
return Promise.reject(null);
|
||||
}
|
||||
}
|
||||
const preSets: CoreSiteWSPreSets = {
|
||||
cacheKey: this.getDiscussionsListCacheKey(forumId, sortOrder)
|
||||
};
|
||||
if (forceCache) {
|
||||
preSets.omitExpires = true;
|
||||
}
|
||||
|
||||
return site.read('mod_forum_get_forum_discussions_paginated', params, preSets).then((response) => {
|
||||
return site.read(method, params, preSets).catch((error) => {
|
||||
// Try to get the data from cache stored with the old WS method.
|
||||
if (!this.appProvider.isOnline() && method == 'mod_forum_get_forum_discussion' &&
|
||||
sortOrder == AddonModForumProvider.SORTORDER_LASTPOST_DESC) {
|
||||
|
||||
const params = {
|
||||
forumid: forumId,
|
||||
page: page,
|
||||
perpage: AddonModForumProvider.DISCUSSIONS_PER_PAGE,
|
||||
sortby: 'timemodified',
|
||||
sortdirection: 'DESC'
|
||||
};
|
||||
const preSets: CoreSiteWSPreSets = {
|
||||
cacheKey: this.getDiscussionsListCacheKey(forumId, sortOrder),
|
||||
omitExpires: true
|
||||
};
|
||||
|
||||
return site.read('mod_forum_get_forum_discussions_paginated', params, preSets);
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}).then((response) => {
|
||||
if (response) {
|
||||
this.storeUserData(response.discussions);
|
||||
|
||||
|
@ -499,7 +604,8 @@ export class AddonModForumProvider {
|
|||
* If a page fails, the discussions until that page will be returned along with a flag indicating an error occurred.
|
||||
*
|
||||
* @param {number} forumId Forum ID.
|
||||
* @param {boolean} forceCache True to always get the value from cache, false otherwise.
|
||||
* @param {number} [sortOrder] Sort order.
|
||||
* @param {boolean} [forceCache] True to always get the value from cache, false otherwise.
|
||||
* @param {number} [numPages] Number of pages to get. If not defined, all pages.
|
||||
* @param {number} [startPage] Page to start. If not defined, first page.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
|
@ -507,8 +613,8 @@ export class AddonModForumProvider {
|
|||
* - discussions: List of discussions.
|
||||
* - error: True if an error occurred, false otherwise.
|
||||
*/
|
||||
getDiscussionsInPages(forumId: number, forceCache?: boolean, numPages?: number, startPage?: number, siteId?: string)
|
||||
: Promise<any> {
|
||||
getDiscussionsInPages(forumId: number, sortOrder?: number, forceCache?: boolean, numPages?: number, startPage?: number,
|
||||
siteId?: string): Promise<any> {
|
||||
if (typeof numPages == 'undefined') {
|
||||
numPages = -1;
|
||||
}
|
||||
|
@ -525,7 +631,7 @@ export class AddonModForumProvider {
|
|||
|
||||
const getPage = (page: number): Promise<any> => {
|
||||
// Get page discussions.
|
||||
return this.getDiscussions(forumId, page, forceCache, siteId).then((response) => {
|
||||
return this.getDiscussions(forumId, sortOrder, page, forceCache, siteId).then((response) => {
|
||||
result.discussions = result.discussions.concat(response.discussions);
|
||||
numPages--;
|
||||
|
||||
|
@ -569,9 +675,6 @@ export class AddonModForumProvider {
|
|||
invalidateContent(moduleId: number, courseId: number): Promise<any> {
|
||||
// Get the forum first, we need the forum ID.
|
||||
return this.getForum(courseId, moduleId).then((forum) => {
|
||||
// We need to get the list of discussions to be able to invalidate their posts.
|
||||
return this.getDiscussionsInPages(forum.id, true).then((response) => {
|
||||
// Now invalidate the WS calls.
|
||||
const promises = [];
|
||||
|
||||
promises.push(this.invalidateForumData(courseId));
|
||||
|
@ -579,12 +682,25 @@ export class AddonModForumProvider {
|
|||
promises.push(this.invalidateCanAddDiscussion(forum.id));
|
||||
promises.push(this.invalidateAccessInformation(forum.id));
|
||||
|
||||
this.getAvailableSortOrders().forEach((sortOrder) => {
|
||||
// We need to get the list of discussions to be able to invalidate their posts.
|
||||
promises.push(this.getDiscussionsInPages(forum.id, sortOrder.value, true).then((response) => {
|
||||
// Now invalidate the WS calls.
|
||||
const promises = [];
|
||||
|
||||
response.discussions.forEach((discussion) => {
|
||||
promises.push(this.invalidateDiscussionPosts(discussion.discussion));
|
||||
});
|
||||
|
||||
return this.utils.allPromises(promises);
|
||||
}));
|
||||
});
|
||||
|
||||
if (this.isDiscussionListSortingAvailable()) {
|
||||
promises.push(this.userProvider.invalidateUserPreference(AddonModForumProvider.PREFERENCE_SORTORDER));
|
||||
}
|
||||
|
||||
return this.utils.allPromises(promises);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -623,7 +739,9 @@ export class AddonModForumProvider {
|
|||
*/
|
||||
invalidateDiscussionsList(forumId: number, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
return site.invalidateWsCacheForKey(this.getDiscussionsListCacheKey(forumId));
|
||||
return this.utils.allPromises(this.getAvailableSortOrders().map((sortOrder) => {
|
||||
return site.invalidateWsCacheForKey(this.getDiscussionsListCacheKey(forumId, sortOrder.value));
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -162,7 +162,7 @@ export class AddonModForumHelperProvider {
|
|||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||
|
||||
const findDiscussion = (page: number): Promise<any> => {
|
||||
return this.forumProvider.getDiscussions(forumId, page, false, siteId).then((response) => {
|
||||
return this.forumProvider.getDiscussions(forumId, undefined, page, false, siteId).then((response) => {
|
||||
if (response.discussions && response.discussions.length > 0) {
|
||||
const discussion = response.discussions.find((discussion) => discussion.id == discussionId);
|
||||
if (discussion) {
|
||||
|
|
|
@ -25,7 +25,7 @@ import { CoreGroupsProvider } from '@providers/groups';
|
|||
import { CoreUserProvider } from '@core/user/providers/user';
|
||||
import { AddonModForumProvider } from './forum';
|
||||
import { AddonModForumSyncProvider } from './sync';
|
||||
import { CoreRatingProvider } from '@core/rating/providers/rating';
|
||||
import { CoreRatingProvider, CoreRatingInfo } from '@core/rating/providers/rating';
|
||||
|
||||
/**
|
||||
* Handler to prefetch forums.
|
||||
|
@ -107,26 +107,36 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
|
|||
* @return {Promise<any[]>} Promise resolved with array of posts.
|
||||
*/
|
||||
protected getPostsForPrefetch(forum: any): Promise<any[]> {
|
||||
const posts = {};
|
||||
const ratingInfos: CoreRatingInfo[] = [];
|
||||
|
||||
const promises = this.forumProvider.getAvailableSortOrders().map((sortOrder) => {
|
||||
// Get discussions in first 2 pages.
|
||||
return this.forumProvider.getDiscussionsInPages(forum.id, false, 2).then((response) => {
|
||||
return this.forumProvider.getDiscussionsInPages(forum.id, sortOrder.value, false, 2).then((response) => {
|
||||
if (response.error) {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
let posts = [];
|
||||
|
||||
response.discussions.forEach((discussion) => {
|
||||
promises.push(this.forumProvider.getDiscussionPosts(discussion.discussion).then((response) => {
|
||||
posts = posts.concat(response.posts);
|
||||
|
||||
return this.ratingProvider.prefetchRatings('module', forum.cmid, forum.scale, forum.course,
|
||||
response.ratinginfo);
|
||||
response.posts.forEach((post) => {
|
||||
posts[post.id] = post;
|
||||
});
|
||||
ratingInfos.push(response.ratinginfo);
|
||||
}));
|
||||
});
|
||||
|
||||
return Promise.all(promises);
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
return posts;
|
||||
const ratingInfo = this.ratingProvider.mergeRatingInfos(ratingInfos);
|
||||
|
||||
return this.ratingProvider.prefetchRatings('module', forum.cmid, forum.scale, forum.course, ratingInfo).then(() => {
|
||||
return this.utils.objectToArray(posts);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -210,6 +220,11 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
|
|||
// Prefetch access information.
|
||||
promises.push(this.forumProvider.getAccessInformation(forum.id));
|
||||
|
||||
// Prefetch sort order preference.
|
||||
if (this.forumProvider.isDiscussionListSortingAvailable()) {
|
||||
promises.push(this.userProvider.getUserPreference(AddonModForumProvider.PREFERENCE_SORTORDER));
|
||||
}
|
||||
|
||||
return Promise.all(promises);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -483,6 +483,12 @@
|
|||
"addon.mod_forum.addanewtopic": "Add a new topic",
|
||||
"addon.mod_forum.addtofavourites": "Star this discussion",
|
||||
"addon.mod_forum.advanced": "Advanced",
|
||||
"addon.mod_forum.bycreatedasc": "By creation date in ascending order",
|
||||
"addon.mod_forum.bycreateddesc": "By creation date in descending order",
|
||||
"addon.mod_forum.bylastpostasc": "By last post in ascending order",
|
||||
"addon.mod_forum.bylastpostdesc": "By last post in descending order",
|
||||
"addon.mod_forum.byrepliesasc": "By number of replies in ascending order",
|
||||
"addon.mod_forum.byrepliesdesc": "By number of replies in descending order",
|
||||
"addon.mod_forum.cannotadddiscussion": "Adding discussions to this forum requires group membership.",
|
||||
"addon.mod_forum.cannotadddiscussionall": "You do not have permission to add a new discussion topic for all participants.",
|
||||
"addon.mod_forum.cannotcreatediscussion": "Could not create new discussion",
|
||||
|
@ -1734,6 +1740,7 @@
|
|||
"core.sizemb": "MB",
|
||||
"core.sizetb": "TB",
|
||||
"core.sorry": "Sorry...",
|
||||
"core.sort": "Sort",
|
||||
"core.sortby": "Sort by",
|
||||
"core.start": "Start",
|
||||
"core.strftimedate": "%d %B %Y",
|
||||
|
|
|
@ -223,6 +223,7 @@
|
|||
"sizemb": "MB",
|
||||
"sizetb": "TB",
|
||||
"sorry": "Sorry...",
|
||||
"sort": "Sort",
|
||||
"sortby": "Sort by",
|
||||
"start": "Start",
|
||||
"strftimedate": "%d %B %Y",
|
||||
|
|
Loading…
Reference in New Issue