|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace smtech\StMarksSearch\Canvas\Courses\Announcements; |
|
4
|
|
|
|
|
5
|
|
|
use smtech\CanvasPest\CanvasArray; |
|
6
|
|
|
use smtech\CanvasPest\CanvasObject; |
|
7
|
|
|
use smtech\StMarksSearch\Relevance; |
|
8
|
|
|
use smtech\StMarksSearch\SearchResult; |
|
9
|
|
|
use smtech\StMarksSearch\SearchSource; |
|
10
|
|
|
use smtech\StMarksSearch\Canvas\Courses\AbstractCourseSearchDomain; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Search a Canvas course's announcements |
|
14
|
|
|
* |
|
15
|
|
|
* @author Seth Battis <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class AnnouncementsSearch extends AbstractCourseSearchDomain |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Search for `$query` among the announcements |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $query |
|
23
|
|
|
* @return SearchResult[] Ordered by descending relevance |
|
24
|
|
|
*/ |
|
25
|
|
|
public function search($query) |
|
26
|
|
|
{ |
|
27
|
|
|
/* Canvas doesn't accept search queries shorter than 3 characters */ |
|
28
|
|
|
if (strlen(trim($query)) < 3) { |
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$source = new SearchSource($this); |
|
33
|
|
|
$results = []; |
|
34
|
|
|
|
|
35
|
|
|
$response = $this->getApi()->get( |
|
36
|
|
|
'courses/' . $this->getId() . '/discussion_topics', |
|
37
|
|
|
[ |
|
|
|
|
|
|
38
|
|
|
'search_term' => $query, |
|
39
|
|
|
'only_announcements' => true |
|
40
|
|
|
] |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
if (is_a($response, CanvasArray::class)) { |
|
|
|
|
|
|
44
|
|
|
foreach ($response as $announcement) { |
|
|
|
|
|
|
45
|
|
|
$results[] = new SearchResult( |
|
46
|
|
|
$announcement['html_url'], |
|
47
|
|
|
$this->relevance($announcement, $query), |
|
48
|
|
|
$announcement['title'], |
|
49
|
|
|
(empty($announcement['message']) ? '' : substr(str_replace(PHP_EOL, ' ', strip_tags($announcement['message'])), 0, 255) . '…'), |
|
50
|
|
|
$source |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->sortByRelevance($results); |
|
56
|
|
|
return $results; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Calculate the relevance of a particular announcement |
|
61
|
|
|
* |
|
62
|
|
|
* @param CanvasObject $announcement |
|
63
|
|
|
* @param string $query |
|
64
|
|
|
* @return Relevance |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
protected function relevance($announcement, $query) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$relevance = new Relevance(); |
|
69
|
|
|
|
|
70
|
|
|
$relevance->add(Relevance::stringProportion($announcement['title'], $query), 'title match'); |
|
71
|
|
|
|
|
72
|
|
|
if (($count = substr_count(strtolower($announcement['message']), strtolower($query))) > 0) { |
|
73
|
|
|
$relevance->add($count * 0.01, "$count occurrences in body"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $relevance; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: