forked from oohoo/moodle-block_course_search
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
209 lines (183 loc) · 7.53 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* *************************************************************************
* * OOHOO - Course Search **
* *************************************************************************
* @package block **
* @subpackage coursesearch **
* @name Course Search **
* @copyright oohoo.biz **
* @link http://oohoo.biz **
* @author Patrick Thibaudeau **
* @author Nicolas Bretin **
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later **
* *************************************************************************
* ************************************************************************ */
defined('MOODLE_INTERNAL') || die();
/**
* Search in a module content in the common fields (name, intro, content)
* @global stdClass $CFG
* @global moodle_database $DB
* @global core_renderer $OUTPUT
* @param int $courseid The course ID
* @param stdClass $module The module object
* @param string $q The string searched
* @return string Return the result in HTML
*/
function block_course_search_search_module($courseid, $module, $q)
{
global $CFG, $DB, $OUTPUT;
$ret = '';
$sqlWere = 'course=? AND (false';
$sqlParams = array($courseid);
//At least one search field is needed
$onefield = false;
//The DBman will be use to check if table and field exists
$dbman = $DB->get_manager();
//Check if the table exists
if ($dbman->table_exists($module->name))
{
//Check if the fields exists
if ($dbman->field_exists($module->name, 'name'))
{
$sqlWere .= " OR name LIKE ?";
$sqlParams[] = "%$q%";
$onefield = true;
}
if ($dbman->field_exists($module->name, 'intro'))
{
$sqlWere .= " OR intro LIKE ?";
$sqlParams[] = "%$q%";
$onefield = true;
}
if ($dbman->field_exists($module->name, 'content'))
{
$sqlWere .= " OR content LIKE ?";
$sqlParams[] = "%$q%";
$onefield = true;
}
//Do the search
if ($onefield)
{
$sql = "SELECT * FROM {" . $module->name . "} WHERE $sqlWere)";
//get sql
$results = $DB->get_records_sql($sql, $sqlParams);
//To create the link we need more info
//find modid
$modid = $DB->get_record('modules', array('name' => $module->name));
foreach ($results as $result)
{
$this_course_mod = $DB->get_record('course_modules', array('course' => $courseid, 'module' => $modid->id, 'instance' => $result->id));
$ret .= "<li><a href='$CFG->wwwroot/mod/$module->name/view.php?id=$this_course_mod->id'><img src='" . $OUTPUT->pix_url('icon', $module->name) . "' alt='$module->name -'/> $result->name</a></li>";
}
}
}
return $ret;
}
/**
* Search in a module content in the common fields (name, intro, content)
* @global stdClass $CFG
* @global moodle_database $DB
* @global core_renderer $OUTPUT
* @param int $courseid The course ID
* @param string $q The string searched
* @return string Return the result in HTML
*/
function block_course_search_search_section($courseid, $q)
{
global $CFG, $DB, $OUTPUT;
$ret = '';
$sqlParams = array($courseid, "%$q%", "%$q%");
$sql = "SELECT * FROM {course_sections} WHERE course=? AND (summary LIKE ? OR name LIKE ?)";
//get sql
$results = $DB->get_records_sql($sql, $sqlParams);
foreach ($results as $result)
{
$link = "<li><a href='$CFG->wwwroot/course/view.php?id=$courseid#section-$result->section'><img src='" . $OUTPUT->pix_url('icon', 'label') . "' alt='section - '/> $result->name</a></li>";
$ret .= $link;
}
return $ret;
}
/**
* Search in a module content in the common fields (name, intro, content)
* @global stdClass $CFG
* @global moodle_database $DB
* @global core_renderer $OUTPUT
* @param int $courseid The course ID
* @param stdClass $module The module object
* @param string $q The string searched
* @param stdClass $modinfo The modinfo object
* @return string Return the result in HTML
*/
function block_course_search_search_module_label($courseid, $module, $q, $modinfo)
{
global $CFG, $DB, $OUTPUT;
$ret = '';
$sqlParams = array($courseid, "%$q%", "%$q%");
$sql = "SELECT * FROM {label} WHERE course=? AND (intro LIKE ? OR name LIKE ?)";
//get sql
$results = $DB->get_records_sql($sql, $sqlParams);
//To create the link we need more info
//find modid
$modid = $DB->get_record('modules', array('name' => 'label'));
//Get All sections
$sections = $modinfo->get_sections();
foreach ($results as $result)
{
$sectionfounded = null;
$this_course_mod = $DB->get_record('course_modules', array('course' => $courseid, 'module' => $modid->id, 'instance' => $result->id));
foreach ($sections as $sectionnum => $section)
{
foreach ($section as $mod)
{
//If mod id == the course mod id
if ($mod == $this_course_mod->id)
{
//now find the name of the section
$sectionfounded = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $sectionnum));
break 2;
}
}
}
if ($sectionfounded != null)
{
$ret .= "<li><a href='$CFG->wwwroot/course/view.php?id=$courseid#section-$sectionfounded->section'><img src='" . $OUTPUT->pix_url('icon', 'label') . "' alt='label - '/> $result->name</a></li>";
}
}
return $ret;
}
/**
* Search in a module tab content
* @global stdClass $CFG
* @global moodle_database $DB
* @global core_renderer $OUTPUT
* @param int $courseid The course ID
* @param stdClass $module The module object
* @param string $q The string searched
* @param stdClass $modinfo The modinfo object
* @return string Return the result in HTML
*/
function block_course_search_search_module_tab($courseid, $module, $q, $modinfo)
{
global $CFG, $DB, $OUTPUT;
$ret = '';
$sqlParams = array($courseid, "%$q%", "%$q%", "%$q%", "%$q%");
$sql = "SELECT {tab_content}.id as tabcontentid, {tab}.id as id,{tab}.name, {tab}.intro, {tab}.course, {tab_content}.tabname, {tab_content}.tabcontent
FROM {tab_content}
INNER JOIN {tab} ON {tab_content}.tabid = {tab}.id AND {tab}.course = ?
WHERE {tab}.name LIKE ? OR {tab}.intro LIKE ?
OR {tab_content}.tabname LIKE ? OR {tab_content}.tabcontent LIKE ?";
//get sql
$results = $DB->get_records_sql($sql, $sqlParams);
//To create the link we need more info
//find modid
$modid = $DB->get_record('modules', array('name' => 'tab'));
$c = 1;
foreach ($results as $result)
{
$this_course_mod = $DB->get_record('course_modules', array('course' => $courseid, 'module' => $modid->id, 'instance' => $result->id));
$ret .= "<li><a href='$CFG->wwwroot/mod/tab/view.php?id=$this_course_mod->id'><img src='" . $OUTPUT->pix_url('icon', 'tab') . "' alt=''/> $result->name</a></li>";
$c++;
}
return $ret;
}