/** * Returns the document associated with this course. * * @param stdClass $record * @param array $options * @return \core_search\document */ public function get_document($record, $options = array()) { try { $context = \context_course::instance($record->id); } catch (\moodle_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); $doc->set('title', content_to_text($record->fullname, false)); $doc->set('content', content_to_text($record->summary, $record->summaryformat)); $doc->set('contextid', $context->id); $doc->set('courseid', $record->id); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('modified', $record->timemodified); $doc->set('description1', $record->shortname); // Check if this document should be considered new. if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) { // If the document was created after the last index time, it must be new. $doc->set_is_new(true); } return $doc; }
/** * Returns the document associated with this activity. * * Overwriting base_activity method as page contents field is required, * description field is not. * * @param stdClass $record * @param array $options * @return \core_search\document */ public function get_document($record, $options = array()) { try { $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course); $context = \context_module::instance($cm->id); } catch (\dml_missing_record_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } catch (\dml_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); $doc->set('title', content_to_text($record->name, false)); $doc->set('content', content_to_text($record->content, $record->contentformat)); $doc->set('contextid', $context->id); $doc->set('courseid', $record->course); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('modified', $record->timemodified); $doc->set('description1', content_to_text($record->intro, $record->introformat)); return $doc; }
/** * Returns the documents associated with this glossary entry id. * * @param stdClass $entry glossary entry. * @param array $options * @return \core_search\document */ public function get_document($entry, $options = array()) { global $DB; $keywords = array(); if ($aliases = $DB->get_records('glossary_alias', array('entryid' => $entry->id))) { foreach ($aliases as $alias) { $keywords[] = $alias->alias; } } try { $cm = $this->get_cm('glossary', $entry->glossaryid, $entry->course); $context = \context_module::instance($cm->id); } catch (\dml_missing_record_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving mod_glossary ' . $entry->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } catch (\dml_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving mod_glossary' . $entry->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($entry->id, $this->componentname, $this->areaname); $doc->set('title', content_to_text($entry->concept, false)); $doc->set('content', content_to_text($entry->definition, $entry->definitionformat)); $doc->set('contextid', $context->id); $doc->set('courseid', $entry->course); $doc->set('userid', $entry->userid); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('modified', $entry->timemodified); // Check if this document should be considered new. if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $entry->timecreated) { // If the document was created after the last index time, it must be new. $doc->set_is_new(true); } // Adding keywords as extra info. if ($keywords) { // No need to pass through content_to_text here as this is just a list of keywords. $doc->set('description1', implode(' ', $keywords)); } return $doc; }
/** * Returns document instances for each record in the recordset. * * @param StdClass $record * @param array $options * @return core_search/document */ public function get_document($record, $options = array()) { $context = \context_system::instance(); // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); // Assigning properties to our document. $doc->set('title', content_to_text(fullname($record), false)); $doc->set('contextid', $context->id); $doc->set('courseid', SITEID); $doc->set('itemid', $record->id); $doc->set('modified', $record->timemodified); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('content', content_to_text($record->description, $record->descriptionformat)); // Check if this document should be considered new. if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) { // If the document was created after the last index time, it must be new. $doc->set_is_new(true); } return $doc; }
/** * Returns the documents associated with this glossary entry id. * * @param stdClass $entry glossary entry. * @return \core_search\document */ public function get_document($entry) { global $DB; $keywords = array(); if ($aliases = $DB->get_records('glossary_alias', array('entryid' => $entry->id))) { foreach ($aliases as $alias) { $keywords[] = $alias->alias; } } try { $cm = $this->get_cm('glossary', $entry->glossaryid, $entry->course); $context = \context_module::instance($cm->id); } catch (\dml_missing_record_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving mod_glossary ' . $entry->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } catch (\dml_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving mod_glossary' . $entry->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($entry->id, $this->componentname, $this->areaname); $doc->set('title', $entry->concept); $doc->set('content', content_to_text($entry->definition, $entry->definitionformat)); $doc->set('contextid', $context->id); $doc->set('type', \core_search\manager::TYPE_TEXT); $doc->set('courseid', $entry->course); $doc->set('userid', $entry->userid); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('modified', $entry->timemodified); // Adding keywords as extra info. if ($keywords) { $doc->set('description1', implode(' ', $keywords)); } return $doc; }
/** * Returns the document associated with this post id. * * @param stdClass $record Post info. * @return \core_search\document */ public function get_document($record) { try { $cm = $this->get_cm('forum', $record->forumid, $record->courseid); $context = \context_module::instance($cm->id); } catch (\dml_missing_record_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } catch (\dml_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); $doc->set('title', $record->subject); $doc->set('content', content_to_text($record->message, $record->messageformat)); $doc->set('contextid', $context->id); $doc->set('type', \core_search\manager::TYPE_TEXT); $doc->set('courseid', $record->courseid); $doc->set('userid', $record->userid); $doc->set('modified', $record->modified); return $doc; }
/** * Returns the presentable string value for a field content. * * The returned string should be plain text. * * @param stdClass $content * @return string */ public static function get_content_value($content) { return content_to_text($content->content, $content->content1); }
/** * Document contents. * * @return void */ public function test_message_sent_document() { // Returns the instance as long as the area is supported. $searcharea = \core_search\manager::get_search_area($this->messagesentareaid); $this->assertInstanceOf('\\core_message\\search\\message_sent', $searcharea); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $this->preventResetByRollback(); $sink = $this->redirectMessages(); $message = new \core\message\message(); $message->courseid = SITEID; $message->userfrom = $user1; $message->userto = $user2; $message->subject = "Test Subject"; $message->smallmessage = "Test small messsage"; $message->fullmessage = "Test full messsage"; $message->fullmessageformat = 0; $message->fullmessagehtml = null; $message->notification = 0; $message->component = "moodle"; $message->name = "instantmessage"; message_send($message); $messages = $sink->get_messages(); $message = $messages[0]; $doc = $searcharea->get_document($message); $this->assertInstanceOf('\\core_search\\document', $doc); $this->assertEquals($message->id, $doc->get('itemid')); $this->assertEquals($this->messagesentareaid . '-' . $message->id, $doc->get('id')); $this->assertEquals(SITEID, $doc->get('courseid')); $this->assertEquals($message->useridfrom, $doc->get('owneruserid')); $this->assertEquals($message->useridto, $doc->get('userid')); $this->assertEquals(content_to_text($message->subject, false), $doc->get('title')); $this->assertEquals(content_to_text($message->smallmessage, false), $doc->get('content')); }
/** * Returns the document for a particular page. * * @param \stdClass $record A record containing, at least, the indexed document id and a modified timestamp * @param array $options Options for document creation * @return \core_search\document */ public function get_document($record, $options = array()) { try { $cm = $this->get_cm('wiki', $record->wikiid, $record->courseid); $context = \context_module::instance($cm->id); } catch (\dml_missing_record_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } catch (\dml_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Make a page object without extra fields. $page = clone $record; unset($page->courseid); unset($page->wikiid); // Conversion based wiki_print_page_content(). // Check if we have passed the cache time. if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) { $content = wiki_refresh_cachedcontent($page); $page = $content['page']; } // Convert to text. $content = content_to_text($page->cachedcontent, FORMAT_MOODLE); // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); $doc->set('title', content_to_text($record->title, false)); $doc->set('content', $content); $doc->set('contextid', $context->id); $doc->set('courseid', $record->courseid); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('modified', $record->timemodified); // Check if this document should be considered new. if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) { // If the document was created after the last index time, it must be new. $doc->set_is_new(true); } return $doc; }
/** * Tests for content_to_text. * * @param string $content The content * @param int|false $format The content format * @param string $expected Expected value * @dataProvider provider_content_to_text */ public function test_content_to_text($content, $format, $expected) { $content = content_to_text($content, $format); $this->assertEquals($expected, $content); }
/** * Document contents. * * @return void */ public function test_users_document() { // Returns the instance as long as the area is supported. $searcharea = \core_search\manager::get_search_area($this->userareaid); $this->assertInstanceOf('\\core_user\\search\\user', $searcharea); $user = self::getDataGenerator()->create_user(); $doc = $searcharea->get_document($user); $this->assertInstanceOf('\\core_search\\document', $doc); $this->assertEquals($user->id, $doc->get('itemid')); $this->assertEquals($this->userareaid . '-' . $user->id, $doc->get('id')); $this->assertEquals(SITEID, $doc->get('courseid')); $this->assertFalse($doc->is_set('userid')); $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid')); $this->assertEquals(content_to_text(fullname($user), false), $doc->get('title')); $this->assertEquals(content_to_text($user->description, $user->descriptionformat), $doc->get('content')); }
/** * Document contents. * * @return void */ public function test_mycourses_document() { // Returns the instance as long as the area is supported. $searcharea = \core_search\manager::get_search_area($this->mycoursesareaid); $this->assertInstanceOf('\\core_course\\search\\mycourse', $searcharea); $user = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher'); $doc = $searcharea->get_document($course); $this->assertInstanceOf('\\core_search\\document', $doc); $this->assertEquals($course->id, $doc->get('itemid')); $this->assertEquals($this->mycoursesareaid . '-' . $course->id, $doc->get('id')); $this->assertEquals($course->id, $doc->get('courseid')); $this->assertFalse($doc->is_set('userid')); $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid')); $this->assertEquals($course->fullname, $doc->get('title')); // Not nice. Applying \core_search\document::set line breaks clean up. $summary = preg_replace("/\\s+/u", " ", content_to_text($course->summary, $course->summaryformat)); $this->assertEquals($summary, $doc->get('content')); $this->assertEquals($course->shortname, $doc->get('description1')); }
/** * Returns the document associated with this activity. * * This default implementation for activities sets the activity name to title and the activity intro to * content. Any activity can overwrite this function if it is interested in setting other fields than the * default ones, or to fill description optional fields with extra stuff. * * @param stdClass $record * @param array $options * @return \core_search\document */ public function get_document($record, $options = array()) { try { $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course); $context = \context_module::instance($cm->id); } catch (\dml_missing_record_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } catch (\dml_exception $ex) { // Notify it as we run here as admin, we should see everything. debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); return false; } // Prepare associative array with data from DB. $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); $doc->set('title', content_to_text($record->name, false)); $doc->set('content', content_to_text($record->intro, $record->introformat)); $doc->set('contextid', $context->id); $doc->set('courseid', $record->course); $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); $doc->set('modified', $record->{static::MODIFIED_FIELD_NAME}); // Check if this document should be considered new. if (isset($options['lastindexedtime'])) { $createdfield = static::CREATED_FIELD_NAME; if (!empty($createdfield) && $options['lastindexedtime'] < $record->{$createdfield}) { // If the document was created after the last index time, it must be new. $doc->set_is_new(true); } } return $doc; }