MDL-59459 Golbal Search: Increase file indexing coverage
Increase the scope of the files that are detected and indexed by Moodle's Global Search. This includes intro and content areas of search areas. The use case here is in a description for an activity there is a file added that contains more information about the activity. It would an advantage to be able to search this content as well. It would also allow search engine plugins to be able to index non text files such as images.
This commit is contained in:
@@ -124,4 +124,41 @@ class mycourse extends \core_search\base {
|
||||
public function get_context_url(\core_search\document $doc) {
|
||||
return new \moodle_url('/course/view.php', array('id' => $doc->get('courseid')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* Should be overridden by each search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array(
|
||||
'overviewfiles',
|
||||
'summary'// Fileareas.
|
||||
);
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the moodle component name.
|
||||
*
|
||||
* It might be the plugin name (whole frankenstyle name) or the core subsystem name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_component_name() {
|
||||
return 'course';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ require_once($CFG->dirroot . '/mod/assign/locallib.php');
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
@@ -46,22 +47,14 @@ class activity extends \core_search\base_activity {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the attached description files.
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @param document $document The current document
|
||||
* @return null
|
||||
* @return array
|
||||
*/
|
||||
public function attach_files($document) {
|
||||
$fs = get_file_storage();
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('intro', ASSIGN_INTROATTACHMENT_FILEAREA); // Fileareas.
|
||||
|
||||
$cm = $this->get_cm($this->get_module_name(), $document->get('itemid'), $document->get('courseid'));
|
||||
$context = \context_module::instance($cm->id);
|
||||
|
||||
$files = $fs->get_area_files($context->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA, 0,
|
||||
'sortorder DESC, id ASC', false);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$document->add_stored_file($file);
|
||||
}
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,4 +158,25 @@ class chapter extends \core_search\base_mod {
|
||||
$contextmodule = \context::instance_by_id($doc->get('contextid'));
|
||||
return new \moodle_url('/mod/book/view.php', array('id' => $contextmodule->instanceid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('chapter'); // Filearea.
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,14 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
@@ -44,21 +45,14 @@ class activity extends \core_search\base_activity {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all the folder files to the index.
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @param document $document The current document
|
||||
* @return null
|
||||
* @return array
|
||||
*/
|
||||
public function attach_files($document) {
|
||||
$fs = get_file_storage();
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('intro', 'content'); // Fileareas.
|
||||
|
||||
$cm = $this->get_cm($this->get_module_name(), $document->get('itemid'), $document->get('courseid'));
|
||||
$context = \context_module::instance($cm->id);
|
||||
|
||||
$files = $fs->get_area_files($context->id, 'mod_folder', 'content', 0, 'sortorder DESC, id ASC', false);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$document->add_stored_file($file);
|
||||
}
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,21 @@ class post extends \core_search\base_mod {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array(
|
||||
'attachment',
|
||||
'post'
|
||||
);
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the forum post attachments.
|
||||
*
|
||||
@@ -142,14 +157,21 @@ class post extends \core_search\base_mod {
|
||||
// Because this is used during indexing, we don't want to cache posts. Would result in memory leak.
|
||||
unset($this->postsdata[$postid]);
|
||||
|
||||
$cm = $this->get_cm('forum', $post->forum, $document->get('courseid'));
|
||||
$cm = $this->get_cm($this->get_module_name(), $post->forum, $document->get('courseid'));
|
||||
$context = \context_module::instance($cm->id);
|
||||
$contextid = $context->id;
|
||||
|
||||
$fileareas = $this->get_search_fileareas();
|
||||
$component = $this->get_component_name();
|
||||
|
||||
// Get the files and attach them.
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($context->id, 'mod_forum', 'attachment', $postid, "filename", false);
|
||||
foreach ($files as $file) {
|
||||
$document->add_stored_file($file);
|
||||
foreach ($fileareas as $filearea) {
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($contextid, $component, $filearea, $postid, '', false);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$document->add_stored_file($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,4 +193,25 @@ class entry extends \core_search\base_mod {
|
||||
}
|
||||
return $this->entriesdata[$entryid];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('attachment', 'entry'); // Fileareas.
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,15 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwritten as labels are displayed in-course.
|
||||
*
|
||||
|
||||
@@ -34,4 +34,25 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('intro', 'page_contents'); // Fileareas.
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
|
||||
/**
|
||||
* Returns the document associated with this activity.
|
||||
*
|
||||
@@ -74,4 +73,25 @@ class activity extends \core_search\base_activity {
|
||||
|
||||
return $doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('intro', 'content'); // Fileareas.
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
@@ -44,24 +45,14 @@ class activity extends \core_search\base_activity {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the main file to the index.
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @param document $document The current document
|
||||
* @return null
|
||||
* @return array
|
||||
*/
|
||||
public function attach_files($document) {
|
||||
$fs = get_file_storage();
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('intro', 'content'); // Fileareas.
|
||||
|
||||
$cm = $this->get_cm($this->get_module_name(), $document->get('itemid'), $document->get('courseid'));
|
||||
$context = \context_module::instance($cm->id);
|
||||
|
||||
// Order by sortorder desc, the first is consided the main file.
|
||||
$files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
|
||||
|
||||
$mainfile = $files ? reset($files) : null;
|
||||
if ($mainfile && $mainfile->get_sortorder() > 0) {
|
||||
$document->add_stored_file($mainfile);
|
||||
}
|
||||
return $fileareas;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ class mod_resource_search_testcase extends advanced_testcase {
|
||||
);
|
||||
$fs->create_file_from_string($filerecord, 'Test resource file');
|
||||
|
||||
// Attach a second file that shouldn't be returned with the search doc.
|
||||
// Attach a second file.
|
||||
$filerecord['filename'] = 'extrafile';
|
||||
$filerecord['sortorder'] = 0;
|
||||
$fs->create_file_from_string($filerecord, 'Test resource file 2');
|
||||
@@ -101,10 +101,15 @@ class mod_resource_search_testcase extends advanced_testcase {
|
||||
$searcharea->attach_files($doc);
|
||||
$files = $doc->get_files();
|
||||
|
||||
// Resources should only return their main file.
|
||||
$this->assertCount(1, $files);
|
||||
$file = reset($files);
|
||||
$this->assertEquals('mainfile', $file->get_filename());
|
||||
// Resources should return all added files.
|
||||
$this->assertCount(2, $files);
|
||||
|
||||
$filenames = array();
|
||||
foreach ($files as $file) {
|
||||
$filenames[] = $file->get_filename();
|
||||
}
|
||||
$this->assertContains('mainfile', $filenames);
|
||||
$this->assertContains('extrafile', $filenames);
|
||||
|
||||
$nrecords++;
|
||||
}
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,15 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns recordset containing required data for indexing activities.
|
||||
*
|
||||
|
||||
@@ -35,6 +35,15 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the document associated with this activity.
|
||||
*
|
||||
|
||||
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,4 +175,25 @@ class collaborative_page extends \core_search\base_mod {
|
||||
$contextmodule = \context::instance_by_id($doc->get('contextid'));
|
||||
return new \moodle_url('/mod/wiki/view.php', array('id' => $contextmodule->instanceid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('attachments'); // Filearea.
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,25 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class activity extends \core_search\base_activity {
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array('intro', 'instructauthors', 'instructreviewers', 'conclusion'); // Fileareas.
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
}
|
||||
|
||||
+37
-2
@@ -274,13 +274,48 @@ abstract class base {
|
||||
abstract public function get_document($record, $options = array());
|
||||
|
||||
/**
|
||||
* Add any files to the document that should be indexed.
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* Should be onerridden by each search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array();
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Files related to the current document are attached,
|
||||
* to the document object ready for indexing by
|
||||
* Global Search.
|
||||
*
|
||||
* The default implementation retrieves all files for
|
||||
* the file areas returned by get_search_fileareas().
|
||||
* If you need to filter files to specific items per
|
||||
* file area, you will need to override this method
|
||||
* and explicitly provide the items.
|
||||
*
|
||||
* @param document $document The current document
|
||||
* @return void
|
||||
*/
|
||||
public function attach_files($document) {
|
||||
return;
|
||||
$fileareas = $this->get_search_fileareas();
|
||||
$contextid = $document->get('contextid');
|
||||
$component = $this->get_component_name();
|
||||
$itemid = $document->get('itemid');
|
||||
|
||||
foreach ($fileareas as $filearea) {
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($contextid, $component, $filearea, $itemid, '', false);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$document->add_stored_file($file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -149,7 +149,7 @@ abstract class base_activity extends base_mod {
|
||||
* @param \core_search\document $doc
|
||||
* @return \moodle_url
|
||||
*/
|
||||
public function get_doc_url(\core_search\document $doc) {;
|
||||
public function get_doc_url(\core_search\document $doc) {
|
||||
return $this->get_context_url($doc);
|
||||
}
|
||||
|
||||
@@ -164,15 +164,6 @@ abstract class base_activity extends base_mod {
|
||||
return new \moodle_url('/mod/' . $this->get_module_name() . '/view.php', array('id' => $cminfo->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the module name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_module_name() {
|
||||
return substr($this->componentname, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an activity instance. Internally uses the class component to know which activity module should be retrieved.
|
||||
*
|
||||
@@ -189,4 +180,54 @@ abstract class base_activity extends base_mod {
|
||||
return $this->activitiesdata[$this->get_module_name()][$instanceid];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* Should be onerridden by each search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array(
|
||||
'intro' // Fileareas.
|
||||
);
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Files related to the current document are attached,
|
||||
* to the document object ready for indexing by
|
||||
* Global Search.
|
||||
*
|
||||
* The default implementation retrieves all files for
|
||||
* the file areas returned by get_search_fileareas().
|
||||
* If you need to filter files to specific items per
|
||||
* file area, you will need to override this method
|
||||
* and explicitly provide the items.
|
||||
*
|
||||
* @param document $document The current document
|
||||
* @return void
|
||||
*/
|
||||
public function attach_files($document) {
|
||||
$fileareas = $this->get_search_fileareas();
|
||||
|
||||
if (!empty($fileareas)) {
|
||||
$cm = $this->get_cm($this->get_module_name(), $document->get('itemid'), $document->get('courseid'));
|
||||
|
||||
$context = \context_module::instance($cm->id);
|
||||
$contextid = $context->id;
|
||||
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($contextid, $this->get_component_name(), $fileareas, false, '', false);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$document->add_stored_file($file);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,15 @@ abstract class base_mod extends base {
|
||||
*/
|
||||
protected static $levels = [CONTEXT_MODULE];
|
||||
|
||||
/**
|
||||
* Returns the module name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_module_name() {
|
||||
return substr($this->componentname, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the course module for the required instanceid + modulename.
|
||||
*
|
||||
@@ -75,7 +84,6 @@ abstract class base_mod extends base {
|
||||
|
||||
// Nothing found.
|
||||
throw new \dml_missing_record_exception($modulename);
|
||||
|
||||
return $cm;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
<?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 engine base unit tests.
|
||||
*
|
||||
* @package core_search
|
||||
* @copyright 2017 Matt Porritt <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once(__DIR__ . '/fixtures/testable_core_search.php');
|
||||
require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
|
||||
|
||||
/**
|
||||
* Search engine base unit tests.
|
||||
*
|
||||
* @package core_search
|
||||
* @copyright 2017 Matt Porritt <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class search_base_activity_testcase extends advanced_testcase {
|
||||
/**
|
||||
* @var \core_search::manager
|
||||
*/
|
||||
protected $search = null;
|
||||
|
||||
/**
|
||||
* @var Instace of core_search_generator.
|
||||
*/
|
||||
protected $generator = null;
|
||||
|
||||
/**
|
||||
* @var Instace of testable_engine.
|
||||
*/
|
||||
protected $engine = null;
|
||||
|
||||
public function setUp() {
|
||||
$this->resetAfterTest();
|
||||
set_config('enableglobalsearch', true);
|
||||
|
||||
// Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
|
||||
$search = testable_core_search::instance();
|
||||
|
||||
$this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
|
||||
$this->generator->setup();
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
// For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
|
||||
if ($this->generator) {
|
||||
// Moodle DML freaks out if we don't teardown the temp table after each run.
|
||||
$this->generator->teardown();
|
||||
$this->generator = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test base activity get search fileareas
|
||||
*/
|
||||
public function test_get_search_fileareas_base() {
|
||||
|
||||
$builder = $this->getMockBuilder('\core_search\base_activity');
|
||||
$builder->disableOriginalConstructor();
|
||||
$stub = $builder->getMockForAbstractClass();
|
||||
|
||||
$result = $stub->get_search_fileareas();
|
||||
|
||||
$this->assertEquals(array('intro'), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test base attach files
|
||||
*/
|
||||
public function test_attach_files_base() {
|
||||
$filearea = 'intro';
|
||||
$component = 'mod_forum';
|
||||
$module = 'forum';
|
||||
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$activity = self::getDataGenerator()->create_module('forum', array('course' => $course->id));
|
||||
$context = \context_module::instance($activity->cmid);
|
||||
$contextid = $context->id;
|
||||
|
||||
// Create file to add.
|
||||
$fs = get_file_storage();
|
||||
$filerecord = array(
|
||||
'contextid' => $contextid,
|
||||
'component' => $component,
|
||||
'filearea' => $filearea,
|
||||
'itemid' => 0,
|
||||
'filepath' => '/',
|
||||
'filename' => 'testfile.txt');
|
||||
$content = 'All the news that\'s fit to print';
|
||||
$file = $fs->create_file_from_string($filerecord, $content);
|
||||
|
||||
// Construct the search document.
|
||||
$rec = new \stdClass();
|
||||
$rec->courseid = $course->id;
|
||||
$area = new core_mocksearch\search\mock_search_area();
|
||||
$record = $this->generator->create_record($rec);
|
||||
|
||||
$document = $area->get_document($record);
|
||||
$document->set('itemid', $activity->id);
|
||||
|
||||
// Create a mock from the abstract class,
|
||||
// with required methods stubbed.
|
||||
$builder = $this->getMockBuilder('\core_search\base_activity');
|
||||
$builder->disableOriginalConstructor();
|
||||
$builder->setMethods(array('get_module_name', 'get_component_name'));
|
||||
$stub = $builder->getMockForAbstractClass();
|
||||
$stub->method('get_module_name')->willReturn($module);
|
||||
$stub->method('get_component_name')->willReturn($component);
|
||||
|
||||
// Attach file to our test document.
|
||||
$stub->attach_files($document);
|
||||
|
||||
// Verify file is attached.
|
||||
$files = $document->get_files();
|
||||
$file = array_values($files)[0];
|
||||
|
||||
$this->assertEquals(1, count($files));
|
||||
$this->assertEquals($content, $file->get_content());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?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 engine base unit tests.
|
||||
*
|
||||
* @package core_search
|
||||
* @copyright 2017 Matt Porritt <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once(__DIR__ . '/fixtures/testable_core_search.php');
|
||||
require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
|
||||
|
||||
/**
|
||||
* Search engine base unit tests.
|
||||
*
|
||||
* @package core_search
|
||||
* @copyright 2017 Matt Porritt <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class search_base_testcase extends advanced_testcase {
|
||||
/**
|
||||
* @var \core_search::manager
|
||||
*/
|
||||
protected $search = null;
|
||||
|
||||
/**
|
||||
* @var Instace of core_search_generator.
|
||||
*/
|
||||
protected $generator = null;
|
||||
|
||||
/**
|
||||
* @var Instace of testable_engine.
|
||||
*/
|
||||
protected $engine = null;
|
||||
|
||||
public function setUp() {
|
||||
$this->resetAfterTest();
|
||||
set_config('enableglobalsearch', true);
|
||||
|
||||
// Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
|
||||
$search = testable_core_search::instance();
|
||||
|
||||
$this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
|
||||
$this->generator->setup();
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
// For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
|
||||
if ($this->generator) {
|
||||
// Moodle DML freaks out if we don't teardown the temp table after each run.
|
||||
$this->generator->teardown();
|
||||
$this->generator = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test base get search fileareas
|
||||
*/
|
||||
public function test_get_search_fileareas_base() {
|
||||
|
||||
$builder = $this->getMockBuilder('\core_search\base');
|
||||
$builder->disableOriginalConstructor();
|
||||
$stub = $builder->getMockForAbstractClass();
|
||||
|
||||
$result = $stub->get_search_fileareas();
|
||||
|
||||
$this->assertEquals(array(), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test base attach files
|
||||
*/
|
||||
public function test_attach_files_base() {
|
||||
$filearea = 'search';
|
||||
$component = 'mod_test';
|
||||
|
||||
// Create file to add.
|
||||
$fs = get_file_storage();
|
||||
$filerecord = array(
|
||||
'contextid' => 1,
|
||||
'component' => $component,
|
||||
'filearea' => $filearea,
|
||||
'itemid' => 1,
|
||||
'filepath' => '/',
|
||||
'filename' => 'testfile.txt');
|
||||
$content = 'All the news that\'s fit to print';
|
||||
$file = $fs->create_file_from_string($filerecord, $content);
|
||||
|
||||
// Construct the search document.
|
||||
$rec = new \stdClass();
|
||||
$area = new core_mocksearch\search\mock_search_area();
|
||||
$record = $this->generator->create_record($rec);
|
||||
$document = $area->get_document($record);
|
||||
|
||||
// Create a mock from the abstract class,
|
||||
// with required methods stubbed.
|
||||
$builder = $this->getMockBuilder('\core_search\base');
|
||||
$builder->disableOriginalConstructor();
|
||||
$builder->setMethods(array('get_search_fileareas', 'get_component_name'));
|
||||
$stub = $builder->getMockForAbstractClass();
|
||||
$stub->method('get_search_fileareas')->willReturn(array($filearea));
|
||||
$stub->method('get_component_name')->willReturn($component);
|
||||
|
||||
// Attach file to our test document.
|
||||
$stub->attach_files($document);
|
||||
|
||||
// Verify file is attached.
|
||||
$files = $document->get_files();
|
||||
$file = array_values($files)[0];
|
||||
|
||||
$this->assertEquals(1, count($files));
|
||||
$this->assertEquals($content, $file->get_content());
|
||||
}
|
||||
}
|
||||
@@ -120,4 +120,41 @@ class user extends \core_search\base {
|
||||
public function get_context_url(\core_search\document $doc) {
|
||||
return new \moodle_url('/user/profile.php', array('id' => $doc->get('itemid')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this area uses file indexing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uses_file_indexing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context info required to index files for
|
||||
* this search area.
|
||||
*
|
||||
* Should be onerridden by each search area.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_search_fileareas() {
|
||||
$fileareas = array(
|
||||
'profile' // Fileareas.
|
||||
);
|
||||
|
||||
return $fileareas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the moodle component name.
|
||||
*
|
||||
* It might be the plugin name (whole frankenstyle name) or the core subsystem name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_component_name() {
|
||||
return 'user';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user