Skip to content

Commit

Permalink
MOBILE-3371 search: Implement globalsearch block
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Jul 19, 2023
1 parent e9bbf7b commit 26514ed
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/langindex.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"addon.block_calendarupcoming.pluginname": "block_calendar_upcoming",
"addon.block_comments.pluginname": "block_comments",
"addon.block_completionstatus.pluginname": "block_completionstatus",
"addon.block_globalsearch.pluginname": "block_globalsearch",
"addon.block_glossaryrandom.pluginname": "block_glossary_random",
"addon.block_learningplans.pluginname": "block_lp",
"addon.block_myoverview.all": "block_myoverview",
Expand Down
2 changes: 2 additions & 0 deletions src/addons/block/block.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { AddonBlockSiteMainMenuModule } from './sitemainmenu/sitemainmenu.module
import { AddonBlockStarredCoursesModule } from './starredcourses/starredcourses.module';
import { AddonBlockTagsModule } from './tags/tags.module';
import { AddonBlockTimelineModule } from './timeline/timeline.module';
import { AddonBlockGlobalSearchModule } from '@addons/block/globalsearch/globalsearch.module';

@NgModule({
imports: [
Expand All @@ -55,6 +56,7 @@ import { AddonBlockTimelineModule } from './timeline/timeline.module';
AddonBlockCommentsModule,
AddonBlockCompletionStatusModule,
AddonBlockCourseListModule,
AddonBlockGlobalSearchModule,
AddonBlockGlossaryRandomModule,
AddonBlockHtmlModule,
AddonBlockLearningPlansModule,
Expand Down
38 changes: 38 additions & 0 deletions src/addons/block/globalsearch/globalsearch.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// (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 { APP_INITIALIZER, NgModule } from '@angular/core';
import { IonicModule } from '@ionic/angular';
import { TranslateModule } from '@ngx-translate/core';
import { CoreBlockDelegate } from '@features/block/services/block-delegate';
import { AddonBlockGlobalSearchHandler } from './services/block-handler';
import { CoreBlockComponentsModule } from '@features/block/components/components.module';

@NgModule({
imports: [
IonicModule,
CoreBlockComponentsModule,
TranslateModule.forChild(),
],
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useValue: () => {
CoreBlockDelegate.registerHandler(AddonBlockGlobalSearchHandler.instance);
},
},
],
})
export class AddonBlockGlobalSearchModule {}
3 changes: 3 additions & 0 deletions src/addons/block/globalsearch/lang.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pluginname": "Global search"
}
61 changes: 61 additions & 0 deletions src/addons/block/globalsearch/services/block-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// (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 { Injectable } from '@angular/core';
import { CoreBlockHandlerData } from '@features/block/services/block-delegate';
import { CoreBlockOnlyTitleComponent } from '@features/block/components/only-title-block/only-title-block';
import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler';
import { makeSingleton } from '@singletons';
import { CoreCourseBlock } from '@features/course/services/course';
import { CoreSites } from '@services/sites';
import { CORE_SEARCH_PAGE_NAME } from '@features/search/services/handlers/mainmenu';

/**
* Block handler.
*/
@Injectable({ providedIn: 'root' })
export class AddonBlockGlobalSearchHandlerService extends CoreBlockBaseHandler {

name = 'AddonBlockGlobalSearch';
blockName = 'globalsearch';

/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
const site = CoreSites.getRequiredCurrentSite();

return !site.isFeatureDisabled('CoreNoDelegate_GlobalSearch')
&& site.wsAvailable('core_search_get_results')
&& site.canUseAdvancedFeature('enableglobalsearch');
}

/**
* @inheritdoc
*/
getDisplayData(block: CoreCourseBlock, contextLevel: string, instanceId: number): CoreBlockHandlerData | undefined {
return {
title: 'addon.block_globalsearch.pluginname',
class: 'addon-block-globalsearch',
component: CoreBlockOnlyTitleComponent,
link: CORE_SEARCH_PAGE_NAME,
linkParams: contextLevel === 'course'
? { courseId: instanceId }
: {},
};
}

}

export const AddonBlockGlobalSearchHandler = makeSingleton(AddonBlockGlobalSearchHandlerService);
6 changes: 6 additions & 0 deletions src/core/features/search/pages/global-search/global-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
CORE_SEARCH_GLOBAL_SEARCH_FILTERS_UPDATED,
CoreSearchGlobalSearch,
} from '@features/search/services/global-search';
import { CoreNavigator } from '@services/navigator';

@Component({
selector: 'page-core-search-global-search',
Expand All @@ -43,11 +44,16 @@ export class CoreSearchGlobalSearchPage implements OnInit, OnDestroy {
ngOnInit(): void {
const site = CoreSites.getRequiredCurrentSite();
const searchBanner = site.config?.searchbanner?.trim() ?? '';
const courseId = CoreNavigator.getRouteNumberParam('courseId');

if (CoreUtils.isTrueOrOne(site.config?.searchbannerenable) && searchBanner.length > 0) {
this.searchBanner = searchBanner;
}

if (courseId) {
this.resultsSource.setFilters({ courseIds: [courseId] });
}

this.filtersObserver = CoreEvents.on(
CORE_SEARCH_GLOBAL_SEARCH_FILTERS_UPDATED,
filters => this.resultsSource.setFilters(filters),
Expand Down
16 changes: 16 additions & 0 deletions src/core/features/search/tests/behat/global-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,19 @@ Feature: Test Global Search
When I press the more menu button in the app
And I press "Global search" in the app
Then I should find "Search indexing is under maintentance!" in the app

Scenario: Open from side block
Given global search expects the query "message" and will return:
| type | idnumber |
| activity | page01 |
And the following "blocks" exist:
| blockname | contextlevel | reference |
| globalsearch | Course | C1 |
And I entered the course "Course 1" as "student1" in the app
When I press "Open block drawer" in the app
And I press "Global search" in the app
Then I should find "What are you searching for?" in the app

When I press "Filter" in the app
Then "C1" should be selected in the app
But "C2" should not be selected in the app

0 comments on commit 26514ed

Please sign in to comment.