MDL-31989 search: New search areas
[moodle.git] / mod / page / classes / search / activity.php
CommitLineData
e2be9961
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Search area for mod_page activities.
19 *
20 * @package mod_page
21 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace mod_page\search;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Search area for mod_page activities.
31 *
32 * @package mod_page
33 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class activity extends \core_search\area\base_activity {
37
38
39 /**
40 * Returns the document associated with this activity.
41 *
42 * Overwriting base_activity method as page contents field is required,
43 * description field is not.
44 *
45 * @param stdClass $record
46 * @return \core_search\document
47 */
48 public function get_document($record) {
49
50 try {
51 $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course);
52 $context = \context_module::instance($cm->id);
53 } catch (\dml_missing_record_exception $ex) {
54 // Notify it as we run here as admin, we should see everything.
55 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
56 $ex->getMessage(), DEBUG_DEVELOPER);
57 return false;
58 } catch (\dml_exception $ex) {
59 // Notify it as we run here as admin, we should see everything.
60 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
61 return false;
62 }
63
64 // Prepare associative array with data from DB.
65 $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
66 $doc->set('title', $record->name);
67 $doc->set('content', editor_input_to_text($record->content, $record->contentformat));
68 $doc->set('contextid', $context->id);
69 $doc->set('type', \core_search\manager::TYPE_TEXT);
70 $doc->set('courseid', $record->course);
71 $doc->set('modified', $record->timemodified);
72 $doc->set('description1', editor_input_to_text($record->intro, $record->introformat));
73
74 return $doc;
75 }
76}