Merge branch 'MDL-59467-main' of https://github.com/andrewnicols/moodle
[moodle.git] / mod / wiki / search.php
blob564b5fe1ca406dc2a80a82665d2afbc54351503e
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * @package mod_wiki
20 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once('../../config.php');
25 require_once($CFG->dirroot . '/mod/wiki/lib.php');
26 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
27 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
29 $search = optional_param('searchstring', null, PARAM_TEXT);
30 $courseid = optional_param('courseid', 0, PARAM_INT);
31 $searchcontent = optional_param('searchwikicontent', 0, PARAM_INT);
32 $cmid = optional_param('cmid', 0, PARAM_INT);
33 $subwikiid = optional_param('subwikiid', 0, PARAM_INT);
34 $userid = optional_param('uid', 0, PARAM_INT);
36 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
37 throw new \moodle_exception('invalidcourseid');
39 if (!$cm = get_coursemodule_from_id('wiki', $cmid)) {
40 throw new \moodle_exception('invalidcoursemodule');
43 require_login($course, true, $cm);
45 // Checking wiki instance
46 if (!$wiki = wiki_get_wiki($cm->instance)) {
47 throw new \moodle_exception('incorrectwikiid', 'wiki');
50 if ($subwikiid) {
51 // Subwiki id is specified.
52 $subwiki = wiki_get_subwiki($subwikiid);
53 if (!$subwiki || $subwiki->wikiid != $wiki->id) {
54 throw new \moodle_exception('incorrectsubwikiid', 'wiki');
56 } else {
57 // Getting current group id
58 $gid = groups_get_activity_group($cm);
60 // Getting current user id
61 if ($wiki->wikimode == 'individual') {
62 $userid = $userid ? $userid : $USER->id;
63 } else {
64 $userid = 0;
66 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
67 // Subwiki does not exist yet, redirect to the view page (which will redirect to create page if allowed).
68 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
69 $url = new moodle_url('/mod/wiki/view.php', $params);
70 redirect($url);
74 if ($subwiki && !wiki_user_can_view($subwiki, $wiki)) {
75 throw new \moodle_exception('cannotviewpage', 'wiki');
78 $wikipage = new page_wiki_search($wiki, $subwiki, $cm);
80 $wikipage->set_search_string($search, $searchcontent);
82 $wikipage->set_title(get_string('search'));
84 $wikipage->print_header();
86 $wikipage->print_content();
88 $wikipage->print_footer();