Skip to content

Commit c92d53c

Browse files
author
rtschu
committed
integrated moodleoverflow into the global search
1 parent c7640c4 commit c92d53c

File tree

2 files changed

+177
-1
lines changed

2 files changed

+177
-1
lines changed
+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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 api for moodleoverflowposts
19+
* Copyright 2020 Robin Tschudi
20+
*/
21+
22+
namespace mod_moodleoverflow\search;
23+
use core_search\document;
24+
defined('MOODLE_INTERNAL') || die();
25+
require_once(dirname(__FILE__) . '/../../locallib.php');
26+
class moodleoverflowposts extends \core_search\base_mod {
27+
28+
/**
29+
* @var array Internal quick static cache.
30+
*/
31+
protected $postsdata = array();
32+
protected $moodleoverflows = array();
33+
protected $discussions = array();
34+
35+
protected static $levels = [CONTEXT_COURSE];
36+
37+
public function uses_file_indexing() {
38+
return false;
39+
}
40+
41+
public function get_document($record, $options = array()) {
42+
try {
43+
$cm = $this->get_cm('moodleoverflow', $record->moodleoverflowid, $record->course);
44+
$context = \context_module::instance($cm->id);
45+
} catch (\dml_missing_record_exception $ex) {
46+
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
47+
$ex->getMessage(), DEBUG_DEVELOPER);
48+
return false;
49+
} catch (\dml_exception $ex) {
50+
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
51+
return false;
52+
}
53+
54+
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
55+
56+
$doc->set('title', content_to_text($record->title, true));
57+
$doc->set('content', content_to_text($record->message, FORMAT_HTML));
58+
$doc->set('contextid', $context->id);
59+
$doc->set('type', \core_search\manager::TYPE_TEXT);
60+
$doc->set('courseid', $record->course);
61+
$doc->set('modified', $record->modified);
62+
$doc->set('userid', $record->userid);
63+
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
64+
$postdata[$record->id[0]] = array('discussionid' => $record->discussion, 'moodleoverflowid' => $record->moodleoverflowid);
65+
if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->created)) {
66+
// If the document was created after the last index time, it must be new.
67+
$doc->set_is_new(true);
68+
}
69+
70+
return $doc;
71+
}
72+
73+
private function get_discussion_from_id($discussionid) {
74+
global $DB;
75+
if (isset($this->discussions[$discussionid])) {
76+
return $this->discussions[$discussionid];
77+
} else {
78+
if (!$discussion = $DB->get_record('moodleoverflow_discussions', array('id' => $discussionid))) {
79+
return false;
80+
}
81+
$this->discussions[$discussionid] = $discussion;
82+
return $discussion;
83+
}
84+
}
85+
86+
private function get_moodleoverflow_from_id($moodleoverflowid) {
87+
global $DB;
88+
if (isset($this->moodleoverflows[$moodleoverflowid])) {
89+
return $this->moodleoverflows[$moodleoverflowid];
90+
} else {
91+
if (!$moodleoverflow = $DB->get_record('moodleoverflow', array('id' => $moodleoverflowid))) {
92+
return false;
93+
}
94+
$this->moodleoverflows[$moodleoverflowid] = $moodleoverflow;
95+
return $moodleoverflow;
96+
}
97+
}
98+
99+
public function check_access($id) {
100+
try {
101+
$post = moodleoverflow_get_post_full($id);
102+
if (!$discussion = $this->get_discussion_from_id($post->discussion)) {
103+
return \core_search\manager::ACCESS_DELETED;
104+
}
105+
if (!$moodleoverflow = $this->get_moodleoverflow_from_id($post->moodleoverflow)) {
106+
return \core_search\manager::ACCESS_DELETED;
107+
}
108+
$context = moodleoverflow_get_context($post->moodleoverflow);
109+
} catch (\dml_missing_record_exception $ex) {
110+
return \core_search\manager::ACCESS_DELETED;
111+
} catch (\dml_exception $ex) {
112+
return \core_search\manager::ACCESS_DENIED;
113+
}
114+
if (moodleoverflow_user_can_see_discussion($moodleoverflow, $discussion, $context)) {
115+
return \core_search\manager::ACCESS_GRANTED;
116+
}
117+
return \core_search\manager::ACCESS_DENIED;
118+
}
119+
120+
public function get_discussionid_for_document(document $doc) {
121+
global $DB;
122+
$postid = $doc->get('itemid');
123+
if (isset($this->postsdata[$postid]) && isset($this->postsdata[$postid]['discussionid'])) {
124+
return $this->postsdata[$postid]['discussionid'];
125+
} else {
126+
$discussionid = $DB->get_field('moodleoverflow_posts', 'discussion', array('id' => $postid));
127+
if (!isset($this->postsdata[$postid])) {
128+
$this->postsdata[$postid] = array();
129+
}
130+
$this->postsdata[$postid]['discussionid'] = $discussionid;
131+
return $discussionid;
132+
}
133+
}
134+
135+
public function get_moodleoverflowid_for_document(document $doc) {
136+
global $DB;
137+
$discussionid = $this->get_discussionid_for_document($doc);
138+
$postid = $doc->get('itemid');
139+
if (isset($this->postsdata[$postid]) && isset($this->postsdata[$postid]["moodleoverflowid"])) {
140+
return $this->postsdata[$postid]["moodleoverflowid"];
141+
} else {
142+
$moodleoverflowid = $DB->get_field('moodleoverflow_discussions', 'moodleoverflow', array('id' => $discussionid));
143+
if (!isset($this->postsdata[$postid])) {
144+
$this->postsdata[$postid] = array();
145+
}
146+
$this->postsdata[$postid]['moodleoverflowid'] = $moodleoverflowid;
147+
return $moodleoverflowid;
148+
}
149+
}
150+
151+
public function get_doc_url(document $doc) {
152+
return new \moodle_url('/mod/moodleoverflow/discussion.php', array('d' => $this->get_discussionid_for_document($doc)),
153+
"p" . $doc->get('itemid'));
154+
}
155+
156+
public function get_context_url(document $doc) {
157+
return new \moodle_url('/mod/moodleoverflow/view.php', array('m' => $this->get_moodleoverflowid_for_document($doc)));
158+
}
159+
160+
public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
161+
global $DB;
162+
list($contextjoin, $contextparams) = $this->get_context_restriction_sql($context, 'moodleoverflow', 'discussions');
163+
if ($contextjoin === null) {
164+
return null;
165+
}
166+
$sql = "SELECT md.name as title, mp.*, mo.course, mo.id as moodleoverflowid FROM {moodleoverflow_posts} mp
167+
JOIN {moodleoverflow_discussions} md ON mp.discussion = md.id
168+
JOIN {moodleoverflow} mo ON md.moodleoverflow = mo.id
169+
$contextjoin
170+
WHERE mp.modified >= ? ORDER BY mp.modified ASC";
171+
return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom]));
172+
}
173+
}

lang/en/moodleoverflow.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,7 @@
390390
$string['updategrades'] = 'Update grades';
391391
$string['gradesreport'] = 'Grades report';
392392
$string['gradesupdated'] = 'Grades updated';
393-
$string['taskupdategrades'] = 'Moodleoverflow maintenance job to update grades';
393+
$string['taskupdategrades'] = 'Moodleoverflow maintenance job to update grades';
394+
395+
// Search.
396+
$string['search:moodleoverflowposts'] = 'Moodleoverflow Posts';

0 commit comments

Comments
 (0)