Skip to content

Commit

Permalink
Merge pull request #3883 from dpalou/MOBILE-3947
Browse files Browse the repository at this point in the history
Mobile 3947
  • Loading branch information
crazyserver authored Dec 15, 2023
2 parents f100e59 + df0cf23 commit c1ddc04
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions scripts/langindex.json
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,7 @@
"core.fileuploader.uploadingperc": "local_moodlemobileapp",
"core.fileuploader.video": "local_moodlemobileapp",
"core.filter": "moodle",
"core.firstdayofweek": "langconfig",
"core.folder": "moodle",
"core.forcepasswordchangenotice": "moodle",
"core.fulllistofcourses": "moodle",
Expand Down
3 changes: 3 additions & 0 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ export class CoreConstants {
static readonly MOD_ARCHETYPE_ASSIGNMENT = 2; // Assignment module archetype.
static readonly MOD_ARCHETYPE_SYSTEM = 3; // System (not user-addable) module archetype.

// Other constants.
static readonly CALENDAR_DEFAULT_STARTING_WEEKDAY = 1;

// Config & environment constants.
static readonly CONFIG = { ...envJson.config } as unknown as EnvironmentConfig; // Data parsed from config.json files.
static readonly BUILD = envJson.build as unknown as EnvironmentBuild; // Build info.
Expand Down
62 changes: 62 additions & 0 deletions src/core/directives/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// 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 { Directive, OnInit } from '@angular/core';
import { CoreLang } from '@services/lang';
import { CoreUser } from '@features/user/services/user';
import { IonDatetime } from '@ionic/angular';

/**
* Directive to automatically add language and starting week day to ion-datetime.
*/
@Directive({
selector: 'ion-datetime',
})
export class CoreIonDatetimeDirective implements OnInit {

constructor(protected datetime: IonDatetime) {}

/**
* @inheritdoc
*/
ngOnInit(): void {
this.setLanguage();
this.setStartingWeekDay();
}

/**
* Set language to use.
*/
protected async setLanguage(): Promise<void> {
if (this.datetime.locale) {
return;
}

const language = await CoreLang.getCurrentLanguage();
this.datetime.locale = language;
}

/**
* Set starting week day.
*/
protected async setStartingWeekDay(): Promise<void> {
if (this.datetime.firstDayOfWeek || this.datetime.firstDayOfWeek === 0) {
return;
}

const startingDay = await CoreUser.getStartingWeekDay();
this.datetime.firstDayOfWeek = startingDay;
}

}
3 changes: 3 additions & 0 deletions src/core/directives/directives.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { CoreCollapsibleFooterDirective } from './collapsible-footer';
import { CoreContentDirective } from './content';
import { CoreUpdateNonReactiveAttributesDirective } from './update-non-reactive-attributes';
import { CoreUserTourDirective } from './user-tour';
import { CoreIonDatetimeDirective } from './datetime';

@NgModule({
declarations: [
Expand All @@ -57,6 +58,7 @@ import { CoreUserTourDirective } from './user-tour';
CoreContentDirective,
CoreUpdateNonReactiveAttributesDirective,
CoreUserTourDirective,
CoreIonDatetimeDirective,
],
exports: [
CoreAutoFocusDirective,
Expand All @@ -79,6 +81,7 @@ import { CoreUserTourDirective } from './user-tour';
CoreContentDirective,
CoreUpdateNonReactiveAttributesDirective,
CoreUserTourDirective,
CoreIonDatetimeDirective,
],
})
export class CoreDirectivesModule {}
19 changes: 19 additions & 0 deletions src/core/features/user/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { USERS_TABLE_NAME, CoreUserDBRecord } from './database/user';
import { CoreUserHelper } from './user-helper';
import { CoreUrlUtils } from '@services/utils/url';
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';
import { CoreConstants } from '@/core/constants';

const ROOT_CACHE_KEY = 'mmUser:';

Expand Down Expand Up @@ -285,6 +286,24 @@ export class CoreUserProvider {
}
}

/**
* Get the starting week day based on the user preference.
*
* @returns Starting week day.
*/
async getStartingWeekDay(): Promise<number> {
const preference = await CoreUtils.ignoreErrors(this.getUserPreference('calendar_startwday'));

if (preference && !isNaN(Number(preference))) {
return Number(preference);
}

const defaultValue = Number(CoreSites.getCurrentSite()?.getStoredConfig('calendar_startwday') ??
Translate.instant('core.firstdayofweek'));

return !isNaN(defaultValue) ? defaultValue % 7 : CoreConstants.CALENDAR_DEFAULT_STARTING_WEEKDAY;
}

/**
* Get cache key for a user WS call.
*
Expand Down
1 change: 1 addition & 0 deletions src/core/lang.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"filenameexist": "File name already exists: {{$a}}",
"filenotfound": "File not found, sorry.",
"filter": "Filter",
"firstdayofweek": "1",
"folder": "Folder",
"forcepasswordchangenotice": "You must change your password to proceed.",
"fulllistofcourses": "All courses",
Expand Down
6 changes: 5 additions & 1 deletion src/core/services/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ export class CoreTimeUtilsProvider {
* @returns Formatted time.
*/
toDatetimeFormat(timestamp?: number): string {
return moment(timestamp || Date.now()).toISOString();
const isoString = moment(timestamp || Date.now()).toISOString(true);

// Remove milliseconds and timezone for consistency with the values used by ion-datetime.
// ion-datetime no longer uses timezone, it always uses UTC.
return isoString.substring(0, isoString.indexOf('.'));
}

/**
Expand Down

0 comments on commit c1ddc04

Please sign in to comment.