forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.php
2005 lines (1731 loc) · 76.4 KB
/
manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Search subsystem manager.
*
* @package core_search
* @copyright Prateek Sachan {@link http://prateeksachan.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_search;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/lib/accesslib.php');
/**
* Search subsystem manager.
*
* @package core_search
* @copyright Prateek Sachan {@link http://prateeksachan.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manager {
/**
* @var int Text contents.
*/
const TYPE_TEXT = 1;
/**
* @var int File contents.
*/
const TYPE_FILE = 2;
/**
* @var int User can not access the document.
*/
const ACCESS_DENIED = 0;
/**
* @var int User can access the document.
*/
const ACCESS_GRANTED = 1;
/**
* @var int The document was deleted.
*/
const ACCESS_DELETED = 2;
/**
* @var int Maximum number of results that will be retrieved from the search engine.
*/
const MAX_RESULTS = 100;
/**
* @var int Number of results per page.
*/
const DISPLAY_RESULTS_PER_PAGE = 10;
/**
* @var int The id to be placed in owneruserid when there is no owner.
*/
const NO_OWNER_ID = 0;
/**
* @var float If initial query takes longer than N seconds, this will be shown in cron log.
*/
const DISPLAY_LONG_QUERY_TIME = 5.0;
/**
* @var float Adds indexing progress within one search area to cron log every N seconds.
*/
const DISPLAY_INDEXING_PROGRESS_EVERY = 30.0;
/**
* @var int Context indexing: normal priority.
*/
const INDEX_PRIORITY_NORMAL = 100;
/**
* @var int Context indexing: low priority for reindexing.
*/
const INDEX_PRIORITY_REINDEXING = 50;
/**
* @var string Core search area category for all results.
*/
const SEARCH_AREA_CATEGORY_ALL = 'core-all';
/**
* @var string Core search area category for course content.
*/
const SEARCH_AREA_CATEGORY_COURSE_CONTENT = 'core-course-content';
/**
* @var string Core search area category for courses.
*/
const SEARCH_AREA_CATEGORY_COURSES = 'core-courses';
/**
* @var string Core search area category for users.
*/
const SEARCH_AREA_CATEGORY_USERS = 'core-users';
/**
* @var string Core search area category for results that do not fit into any of existing categories.
*/
const SEARCH_AREA_CATEGORY_OTHER = 'core-other';
/**
* @var \core_search\base[] Enabled search areas.
*/
protected static $enabledsearchareas = null;
/**
* @var \core_search\base[] All system search areas.
*/
protected static $allsearchareas = null;
/**
* @var \core_search\area_category[] A list of search area categories.
*/
protected static $searchareacategories = null;
/**
* @var \core_search\manager
*/
protected static $instance = null;
/**
* @var array IDs (as keys) of course deletions in progress in this requuest, if any.
*/
protected static $coursedeleting = [];
/**
* @var \core_search\engine
*/
protected $engine = null;
/**
* Note: This should be removed once possible (see MDL-60644).
*
* @var float Fake current time for use in PHPunit tests
*/
protected static $phpunitfaketime = 0;
/**
* @var int Result count when used with mock results for Behat tests.
*/
protected $behatresultcount = 0;
/**
* Constructor, use \core_search\manager::instance instead to get a class instance.
*
* @param \core_search\base The search engine to use
*/
public function __construct($engine) {
$this->engine = $engine;
}
/**
* @var int Record time of each successful schema check, but not more than once per 10 minutes.
*/
const SCHEMA_CHECK_TRACKING_DELAY = 10 * 60;
/**
* @var int Require a new schema check at least every 4 hours.
*/
const SCHEMA_CHECK_REQUIRED_EVERY = 4 * 3600;
/**
* Returns an initialised \core_search instance.
*
* While constructing the instance, checks on the search schema may be carried out. The $fast
* parameter provides a way to skip those checks on pages which are used frequently. It has
* no effect if an instance has already been constructed in this request.
*
* The $query parameter indicates that the page is used for queries rather than indexing. If
* configured, this will cause the query-only search engine to be used instead of the 'normal'
* one.
*
* @see \core_search\engine::is_installed
* @see \core_search\engine::is_server_ready
* @param bool $fast Set to true when calling on a page that requires high performance
* @param bool $query Set true on a page that is used for querying
* @throws \core_search\engine_exception
* @return \core_search\manager
*/
public static function instance(bool $fast = false, bool $query = false) {
global $CFG;
// One per request, this should be purged during testing.
if (static::$instance !== null) {
return static::$instance;
}
if (empty($CFG->searchengine)) {
throw new \core_search\engine_exception('enginenotselected', 'search');
}
if (!$engine = static::search_engine_instance($query)) {
throw new \core_search\engine_exception('enginenotfound', 'search', '', $CFG->searchengine);
}
// Get time now and at last schema check.
$now = (int)self::get_current_time();
$lastschemacheck = get_config($engine->get_plugin_name(), 'lastschemacheck');
// On pages where performance matters, tell the engine to skip schema checks.
$skipcheck = false;
if ($fast && $now < $lastschemacheck + self::SCHEMA_CHECK_REQUIRED_EVERY) {
$skipcheck = true;
$engine->skip_schema_check();
}
if (!$engine->is_installed()) {
throw new \core_search\engine_exception('enginenotinstalled', 'search', '', $CFG->searchengine);
}
$serverstatus = $engine->is_server_ready();
if ($serverstatus !== true) {
// Skip this error in Behat when faking seach results.
if (!defined('BEHAT_SITE_RUNNING') || !get_config('core_search', 'behat_fakeresult')) {
// Clear the record of successful schema checks since it might have failed.
unset_config('lastschemacheck', $engine->get_plugin_name());
// Error message with no details as this is an exception that any user may find if the server crashes.
throw new \core_search\engine_exception('engineserverstatus', 'search');
}
}
// If we did a successful schema check, record this, but not more than once per 10 minutes
// (to avoid updating the config db table/cache too often in case it gets called frequently).
if (!$skipcheck && $now >= $lastschemacheck + self::SCHEMA_CHECK_TRACKING_DELAY) {
set_config('lastschemacheck', $now, $engine->get_plugin_name());
}
static::$instance = new \core_search\manager($engine);
return static::$instance;
}
/**
* Returns whether global search is enabled or not.
*
* @return bool
*/
public static function is_global_search_enabled() {
global $CFG;
return !empty($CFG->enableglobalsearch);
}
/**
* Tests if global search is configured to be equivalent to the front page course search.
*
* @return bool
*/
public static function can_replace_course_search(): bool {
global $CFG;
// Assume we can replace front page search.
$canreplace = true;
// Global search must be enabled.
if (!static::is_global_search_enabled()) {
$canreplace = false;
}
// Users must be able to search the details of all courses that they can see,
// even if they do not have access to them.
if (empty($CFG->searchincludeallcourses)) {
$canreplace = false;
}
// Course search must be enabled.
if ($canreplace) {
$areaid = static::generate_areaid('core_course', 'course');
$enabledareas = static::get_search_areas_list(true);
$canreplace = isset($enabledareas[$areaid]);
}
return $canreplace;
}
/**
* Returns the search URL for course search
*
* @return moodle_url
*/
public static function get_course_search_url() {
if (self::can_replace_course_search()) {
$searchurl = '/search/index.php';
} else {
$searchurl = '/course/search.php';
}
return new \moodle_url($searchurl);
}
/**
* Returns whether indexing is enabled or not (you can enable indexing even when search is not
* enabled at the moment, so as to have it ready for students).
*
* @return bool True if indexing is enabled.
*/
public static function is_indexing_enabled() {
global $CFG;
return !empty($CFG->enableglobalsearch) || !empty($CFG->searchindexwhendisabled);
}
/**
* Returns an instance of the search engine.
*
* @param bool $query If true, gets the query-only search engine (where configured)
* @return \core_search\engine
*/
public static function search_engine_instance(bool $query = false) {
global $CFG;
if ($query && $CFG->searchenginequeryonly) {
return self::search_engine_instance_from_setting($CFG->searchenginequeryonly);
} else {
return self::search_engine_instance_from_setting($CFG->searchengine);
}
}
/**
* Loads a search engine based on the name given in settings, which can optionally
* include '-alternate' to indicate that an alternate version should be used.
*
* @param string $setting
* @return engine|null
*/
protected static function search_engine_instance_from_setting(string $setting): ?engine {
if (preg_match('~^(.*)-alternate$~', $setting, $matches)) {
$enginename = $matches[1];
$alternate = true;
} else {
$enginename = $setting;
$alternate = false;
}
$classname = '\\search_' . $enginename . '\\engine';
if (!class_exists($classname)) {
return null;
}
if ($alternate) {
return new $classname(true);
} else {
// Use the constructor with no parameters for compatibility.
return new $classname();
}
}
/**
* Returns the search engine.
*
* @return \core_search\engine
*/
public function get_engine() {
return $this->engine;
}
/**
* Returns a search area class name.
*
* @param string $areaid
* @return string
*/
protected static function get_area_classname($areaid) {
list($componentname, $areaname) = static::extract_areaid_parts($areaid);
return '\\' . $componentname . '\\search\\' . $areaname;
}
/**
* Returns a new area search indexer instance.
*
* @param string $areaid
* @return \core_search\base|bool False if the area is not available.
*/
public static function get_search_area($areaid) {
// We have them all here.
if (!empty(static::$allsearchareas[$areaid])) {
return static::$allsearchareas[$areaid];
}
$classname = static::get_area_classname($areaid);
if (class_exists($classname) && static::is_search_area($classname)) {
return new $classname();
}
return false;
}
/**
* Return the list of available search areas.
*
* @param bool $enabled Return only the enabled ones.
* @return \core_search\base[]
*/
public static function get_search_areas_list($enabled = false) {
// Two different arrays, we don't expect these arrays to be big.
if (static::$allsearchareas !== null) {
if (!$enabled) {
return static::$allsearchareas;
} else {
return static::$enabledsearchareas;
}
}
static::$allsearchareas = array();
static::$enabledsearchareas = array();
$searchclasses = \core_component::get_component_classes_in_namespace(null, 'search');
foreach ($searchclasses as $classname => $classpath) {
$areaname = substr(strrchr($classname, '\\'), 1);
$componentname = strstr($classname, '\\', 1);
if (!static::is_search_area($classname)) {
continue;
}
$areaid = static::generate_areaid($componentname, $areaname);
$searchclass = new $classname();
static::$allsearchareas[$areaid] = $searchclass;
if ($searchclass->is_enabled()) {
static::$enabledsearchareas[$areaid] = $searchclass;
}
}
if ($enabled) {
return static::$enabledsearchareas;
}
return static::$allsearchareas;
}
/**
* Return search area category instance by category name.
*
* @param string $name Category name. If name is not valid will return default category.
*
* @return \core_search\area_category
*/
public static function get_search_area_category_by_name($name) {
if (key_exists($name, self::get_search_area_categories())) {
return self::get_search_area_categories()[$name];
} else {
return self::get_search_area_categories()[self::get_default_area_category_name()];
}
}
/**
* Return a list of existing search area categories.
*
* @return \core_search\area_category[]
*/
public static function get_search_area_categories() {
if (!isset(static::$searchareacategories)) {
$categories = self::get_core_search_area_categories();
// Go through all existing search areas and get categories they are assigned to.
$areacategories = [];
foreach (self::get_search_areas_list() as $searcharea) {
foreach ($searcharea->get_category_names() as $categoryname) {
if (!key_exists($categoryname, $areacategories)) {
$areacategories[$categoryname] = [];
}
$areacategories[$categoryname][] = $searcharea;
}
}
// Populate core categories by areas.
foreach ($areacategories as $name => $searchareas) {
if (key_exists($name, $categories)) {
$categories[$name]->set_areas($searchareas);
} else {
throw new \coding_exception('Unknown core search area category ' . $name);
}
}
// Get additional categories.
$additionalcategories = self::get_additional_search_area_categories();
foreach ($additionalcategories as $additionalcategory) {
if (!key_exists($additionalcategory->get_name(), $categories)) {
$categories[$additionalcategory->get_name()] = $additionalcategory;
}
}
// Remove categories without areas.
foreach ($categories as $key => $category) {
if (empty($category->get_areas())) {
unset($categories[$key]);
}
}
// Sort categories by order.
uasort($categories, function($category1, $category2) {
return $category1->get_order() <=> $category2->get_order();
});
static::$searchareacategories = $categories;
}
return static::$searchareacategories;
}
/**
* Get list of core search area categories.
*
* @return \core_search\area_category[]
*/
protected static function get_core_search_area_categories() {
$categories = [];
$categories[self::SEARCH_AREA_CATEGORY_ALL] = new area_category(
self::SEARCH_AREA_CATEGORY_ALL,
get_string('core-all', 'search'),
0,
self::get_search_areas_list(true)
);
$categories[self::SEARCH_AREA_CATEGORY_COURSE_CONTENT] = new area_category(
self::SEARCH_AREA_CATEGORY_COURSE_CONTENT,
get_string('core-course-content', 'search'),
1
);
$categories[self::SEARCH_AREA_CATEGORY_COURSES] = new area_category(
self::SEARCH_AREA_CATEGORY_COURSES,
get_string('core-courses', 'search'),
2
);
$categories[self::SEARCH_AREA_CATEGORY_USERS] = new area_category(
self::SEARCH_AREA_CATEGORY_USERS,
get_string('core-users', 'search'),
3
);
$categories[self::SEARCH_AREA_CATEGORY_OTHER] = new area_category(
self::SEARCH_AREA_CATEGORY_OTHER,
get_string('core-other', 'search'),
4
);
return $categories;
}
/**
* Gets a list of additional search area categories.
*
* @return \core_search\area_category[]
*/
protected static function get_additional_search_area_categories() {
$additionalcategories = [];
// Allow plugins to add custom search area categories.
if ($pluginsfunction = get_plugins_with_function('search_area_categories')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$plugincategories = $pluginfunction();
// We're expecting a list of valid area categories.
if (is_array($plugincategories)) {
foreach ($plugincategories as $plugincategory) {
if (self::is_valid_area_category($plugincategory)) {
$additionalcategories[] = $plugincategory;
} else {
throw new \coding_exception('Invalid search area category!');
}
}
} else {
throw new \coding_exception($pluginfunction . ' should return a list of search area categories!');
}
}
}
}
return $additionalcategories;
}
/**
* Check if provided instance of area category is valid.
*
* @param mixed $areacategory Area category instance. Potentially could be anything.
*
* @return bool
*/
protected static function is_valid_area_category($areacategory) {
return $areacategory instanceof area_category;
}
/**
* Clears all static caches.
*
* @return void
*/
public static function clear_static() {
static::$enabledsearchareas = null;
static::$allsearchareas = null;
static::$instance = null;
static::$searchareacategories = null;
static::$coursedeleting = [];
static::$phpunitfaketime = null;
base_block::clear_static();
engine::clear_users_cache();
}
/**
* Generates an area id from the componentname and the area name.
*
* There should not be any naming conflict as the area name is the
* class name in component/classes/search/.
*
* @param string $componentname
* @param string $areaname
* @return void
*/
public static function generate_areaid($componentname, $areaname) {
return $componentname . '-' . $areaname;
}
/**
* Returns all areaid string components (component name and area name).
*
* @param string $areaid
* @return array Component name (Frankenstyle) and area name (search area class name)
*/
public static function extract_areaid_parts($areaid) {
return explode('-', $areaid);
}
/**
* Parse a search area id and get plugin name and config name prefix from it.
*
* @param string $areaid Search area id.
* @return array Where the first element is a plugin name and the second is config names prefix.
*/
public static function parse_areaid($areaid) {
$parts = self::extract_areaid_parts($areaid);
if (empty($parts[1])) {
throw new \coding_exception('Trying to parse invalid search area id ' . $areaid);
}
$component = $parts[0];
$area = $parts[1];
if (strpos($component, 'core') === 0) {
$plugin = 'core_search';
$configprefix = str_replace('-', '_', $areaid);
} else {
$plugin = $component;
$configprefix = 'search_' . $area;
}
return [$plugin, $configprefix];
}
/**
* Returns information about the areas which the user can access.
*
* The returned value is a stdClass object with the following fields:
* - everything (bool, true for admin only)
* - usercontexts (indexed by area identifier then context
* - separategroupscontexts (contexts within which group restrictions apply)
* - visiblegroupscontextsareas (overrides to the above when the same contexts also have
* 'visible groups' for certain search area ids - hopefully rare)
* - usergroups (groups which the current user belongs to)
*
* The areas can be limited by course id and context id. If specifying context ids, results
* are limited to the exact context ids specified and not their children (for example, giving
* the course context id would result in including search items with the course context id, and
* not anything from a context inside the course). For performance, you should also specify
* course id(s) when using context ids.
*
* @param array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
* @param array|false $limitcontextids An array of context ids to limit the search to. False for no limiting.
* @return \stdClass Object as described above
*/
protected function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
global $DB, $USER;
// All results for admins (unless they have chosen to limit results). Eventually we could
// add a new capability for managers.
if (is_siteadmin() && !$limitcourseids && !$limitcontextids) {
return (object)array('everything' => true);
}
$areasbylevel = array();
// Split areas by context level so we only iterate only once through courses and cms.
$searchareas = static::get_search_areas_list(true);
foreach ($searchareas as $areaid => $unused) {
$classname = static::get_area_classname($areaid);
$searcharea = new $classname();
foreach ($classname::get_levels() as $level) {
$areasbylevel[$level][$areaid] = $searcharea;
}
}
// This will store area - allowed contexts relations.
$areascontexts = array();
// Initialise two special-case arrays for storing other information related to the contexts.
$separategroupscontexts = array();
$visiblegroupscontextsareas = array();
$usergroups = array();
if (empty($limitcourseids) && !empty($areasbylevel[CONTEXT_SYSTEM])) {
// We add system context to all search areas working at this level. Here each area is fully responsible of
// the access control as we can not automate much, we can not even check guest access as some areas might
// want to allow guests to retrieve data from them.
$systemcontextid = \context_system::instance()->id;
if (!$limitcontextids || in_array($systemcontextid, $limitcontextids)) {
foreach ($areasbylevel[CONTEXT_SYSTEM] as $areaid => $searchclass) {
$areascontexts[$areaid][$systemcontextid] = $systemcontextid;
}
}
}
if (!empty($areasbylevel[CONTEXT_USER])) {
if ($usercontext = \context_user::instance($USER->id, IGNORE_MISSING)) {
if (!$limitcontextids || in_array($usercontext->id, $limitcontextids)) {
// Extra checking although only logged users should reach this point, guest users have a valid context id.
foreach ($areasbylevel[CONTEXT_USER] as $areaid => $searchclass) {
$areascontexts[$areaid][$usercontext->id] = $usercontext->id;
}
}
}
}
if (is_siteadmin()) {
$allcourses = $this->get_all_courses($limitcourseids);
} else {
$allcourses = $mycourses = $this->get_my_courses((bool)get_config('core', 'searchallavailablecourses'));
if (self::include_all_courses()) {
$allcourses = $this->get_all_courses($limitcourseids);
}
}
if (empty($limitcourseids) || in_array(SITEID, $limitcourseids)) {
$allcourses[SITEID] = get_course(SITEID);
if (isset($mycourses)) {
$mycourses[SITEID] = get_course(SITEID);
}
}
// Keep a list of included course context ids (needed for the block calculation below).
$coursecontextids = [];
$modulecms = [];
foreach ($allcourses as $course) {
if (!empty($limitcourseids) && !in_array($course->id, $limitcourseids)) {
// Skip non-included courses.
continue;
}
$coursecontext = \context_course::instance($course->id);
$hasgrouprestrictions = false;
if (!empty($areasbylevel[CONTEXT_COURSE]) &&
(!$limitcontextids || in_array($coursecontext->id, $limitcontextids))) {
// Add the course contexts the user can view.
foreach ($areasbylevel[CONTEXT_COURSE] as $areaid => $searchclass) {
if (!empty($mycourses[$course->id]) || \core_course_category::can_view_course_info($course)) {
$areascontexts[$areaid][$coursecontext->id] = $coursecontext->id;
}
}
}
// Skip module context if a user can't access related course.
if (isset($mycourses) && !key_exists($course->id, $mycourses)) {
continue;
}
$coursecontextids[] = $coursecontext->id;
// Info about the course modules.
$modinfo = get_fast_modinfo($course);
if (!empty($areasbylevel[CONTEXT_MODULE])) {
// Add the module contexts the user can view (cm_info->uservisible).
foreach ($areasbylevel[CONTEXT_MODULE] as $areaid => $searchclass) {
// Removing the plugintype 'mod_' prefix.
$modulename = substr($searchclass->get_component_name(), 4);
$modinstances = $modinfo->get_instances_of($modulename);
foreach ($modinstances as $modinstance) {
// Skip module context if not included in list of context ids.
if ($limitcontextids && !in_array($modinstance->context->id, $limitcontextids)) {
continue;
}
if ($modinstance->uservisible) {
$contextid = $modinstance->context->id;
$areascontexts[$areaid][$contextid] = $contextid;
$modulecms[$modinstance->id] = $modinstance;
if (!has_capability('moodle/site:accessallgroups', $modinstance->context) &&
($searchclass instanceof base_mod) &&
$searchclass->supports_group_restriction()) {
if ($searchclass->restrict_cm_access_by_group($modinstance)) {
$separategroupscontexts[$contextid] = $contextid;
$hasgrouprestrictions = true;
} else {
// Track a list of anything that has a group id (so might get
// filtered) and doesn't want to be, in this context.
if (!array_key_exists($contextid, $visiblegroupscontextsareas)) {
$visiblegroupscontextsareas[$contextid] = array();
}
$visiblegroupscontextsareas[$contextid][$areaid] = $areaid;
}
}
}
}
}
}
// Insert group information for course (unless there aren't any modules restricted by
// group for this user in this course, in which case don't bother).
if ($hasgrouprestrictions) {
$groups = groups_get_all_groups($course->id, $USER->id, 0, 'g.id');
foreach ($groups as $group) {
$usergroups[$group->id] = $group->id;
}
}
}
// Chuck away all the 'visible groups contexts' data unless there is actually something
// that does use separate groups in the same context (this data is only used as an
// 'override' in cases where the search is restricting to separate groups).
foreach ($visiblegroupscontextsareas as $contextid => $areas) {
if (!array_key_exists($contextid, $separategroupscontexts)) {
unset($visiblegroupscontextsareas[$contextid]);
}
}
// Add all supported block contexts for course contexts that user can access, in a single query for performance.
if (!empty($areasbylevel[CONTEXT_BLOCK]) && !empty($coursecontextids)) {
// Get list of all block types we care about.
$blocklist = [];
foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
$blocklist[$searchclass->get_block_name()] = true;
}
list ($blocknamesql, $blocknameparams) = $DB->get_in_or_equal(array_keys($blocklist));
// Get list of course contexts.
list ($contextsql, $contextparams) = $DB->get_in_or_equal($coursecontextids);
// Get list of block context (if limited).
$blockcontextwhere = '';
$blockcontextparams = [];
if ($limitcontextids) {
list ($blockcontextsql, $blockcontextparams) = $DB->get_in_or_equal($limitcontextids);
$blockcontextwhere = 'AND x.id ' . $blockcontextsql;
}
// Query all blocks that are within an included course, and are set to be visible, and
// in a supported page type (basically just course view). This query could be
// extended (or a second query added) to support blocks that are within a module
// context as well, and we could add more page types if required.
$blockrecs = $DB->get_records_sql("
SELECT x.*, bi.blockname AS blockname, bi.id AS blockinstanceid
FROM {block_instances} bi
JOIN {context} x ON x.instanceid = bi.id AND x.contextlevel = ?
LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
AND bp.contextid = bi.parentcontextid
AND bp.pagetype LIKE 'course-view-%'
AND bp.subpage = ''
AND bp.visible = 0
WHERE bi.parentcontextid $contextsql
$blockcontextwhere
AND bi.blockname $blocknamesql
AND bi.subpagepattern IS NULL
AND (bi.pagetypepattern = 'site-index'
OR bi.pagetypepattern LIKE 'course-view-%'
OR bi.pagetypepattern = 'course-*'
OR bi.pagetypepattern = '*')
AND bp.id IS NULL",
array_merge([CONTEXT_BLOCK], $contextparams, $blockcontextparams, $blocknameparams));
$blockcontextsbyname = [];
foreach ($blockrecs as $blockrec) {
if (empty($blockcontextsbyname[$blockrec->blockname])) {
$blockcontextsbyname[$blockrec->blockname] = [];
}
\context_helper::preload_from_record($blockrec);
$blockcontextsbyname[$blockrec->blockname][] = \context_block::instance(
$blockrec->blockinstanceid);
}
// Add the block contexts the user can view.
foreach ($areasbylevel[CONTEXT_BLOCK] as $areaid => $searchclass) {
if (empty($blockcontextsbyname[$searchclass->get_block_name()])) {
continue;
}
foreach ($blockcontextsbyname[$searchclass->get_block_name()] as $context) {
if (has_capability('moodle/block:view', $context)) {
$areascontexts[$areaid][$context->id] = $context->id;
}
}
}
}
// Return all the data.
return (object)array('everything' => false, 'usercontexts' => $areascontexts,
'separategroupscontexts' => $separategroupscontexts, 'usergroups' => $usergroups,
'visiblegroupscontextsareas' => $visiblegroupscontextsareas);
}
/**
* Returns requested page of documents plus additional information for paging.
*
* This function does not perform any kind of security checking for access, the caller code
* should check that the current user have moodle/search:query capability.
*
* If a page is requested that is beyond the last result, the last valid page is returned in
* results, and actualpage indicates which page was returned.
*
* @param stdClass $formdata
* @param int $pagenum The 0 based page number.
* @return object An object with 3 properties:
* results => An array of \core_search\documents for the actual page.
* totalcount => Number of records that are possibly available, to base paging on.
* actualpage => The actual page returned.
*/
public function paged_search(\stdClass $formdata, $pagenum) {
$out = new \stdClass();
if (self::is_search_area_categories_enabled() && !empty($formdata->cat)) {
$cat = self::get_search_area_category_by_name($formdata->cat);
if (empty($formdata->areaids)) {
$formdata->areaids = array_keys($cat->get_areas());
} else {
foreach ($formdata->areaids as $key => $areaid) {
if (!key_exists($areaid, $cat->get_areas())) {
unset($formdata->areaids[$key]);
}
}
}
}
$perpage = static::DISPLAY_RESULTS_PER_PAGE;
// Make sure we only allow request up to max page.
$pagenum = min($pagenum, (static::MAX_RESULTS / $perpage) - 1);
// Calculate the first and last document number for the current page, 1 based.
$mindoc = ($pagenum * $perpage) + 1;
$maxdoc = ($pagenum + 1) * $perpage;
// Get engine documents, up to max.
$docs = $this->search($formdata, $maxdoc);
$resultcount = count($docs);
if ($resultcount < $maxdoc) {
// This means it couldn't give us results to max, so the count must be the max.
$out->totalcount = $resultcount;
} else {
// Get the possible count reported by engine, and limit to our max.
$out->totalcount = $this->engine->get_query_total_count();
if (defined('BEHAT_SITE_RUNNING') && $this->behatresultcount) {
// Override results when using Behat mock results.
$out->totalcount = $this->behatresultcount;
}
$out->totalcount = min($out->totalcount, static::MAX_RESULTS);
}
// Determine the actual page.
if ($resultcount < $mindoc) {
// We couldn't get the min docs for this page, so determine what page we can get.
$out->actualpage = floor(($resultcount - 1) / $perpage);
} else {
$out->actualpage = $pagenum;
}
// Split the results to only return the page.
$out->results = array_slice($docs, $out->actualpage * $perpage, $perpage, true);