1 |
efrain |
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 |
namespace mod_questionnaire\search;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Contains the question class definition for search.
|
|
|
21 |
*
|
|
|
22 |
* Search area for mod_questionnaire questions. Separated from the activity search so that admins can choose whether or not they
|
|
|
23 |
* want this part enabled.
|
|
|
24 |
*
|
|
|
25 |
* @package mod_questionnaire
|
|
|
26 |
* @author Mike Churchward
|
|
|
27 |
* @copyright 2016 Mike Churchward (mike.churchward@poetgroup.org)
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class question extends \core_search\base_mod {
|
|
|
31 |
/**
|
|
|
32 |
* Returns recordset containing required data for indexing activities.
|
|
|
33 |
*
|
|
|
34 |
* @param int $modifiedfrom timestamp
|
|
|
35 |
* @return \moodle_recordset
|
|
|
36 |
*/
|
|
|
37 |
public function get_recordset_by_timestamp($modifiedfrom = 0) {
|
|
|
38 |
global $DB;
|
|
|
39 |
|
|
|
40 |
// Join the survey record to ensure only questionnaires with questions are returned.
|
|
|
41 |
$sql = 'SELECT q.* ' .
|
|
|
42 |
'FROM {questionnaire} q ' .
|
|
|
43 |
'INNER JOIN {questionnaire_survey} s ON q.sid = s.id ' .
|
|
|
44 |
'WHERE q.timemodified >= ? ' .
|
|
|
45 |
'ORDER BY q.timemodified ASC';
|
|
|
46 |
|
|
|
47 |
return $DB->get_recordset_sql($sql, [$modifiedfrom]);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Returns the document for a particular question. In this case, the document created contains the question content for all
|
|
|
52 |
* questions associated with a questionnaire.
|
|
|
53 |
*
|
|
|
54 |
* @param \stdClass $record
|
|
|
55 |
* @param array $options
|
|
|
56 |
* @return \core_search\document
|
|
|
57 |
*/
|
|
|
58 |
public function get_document($record, $options = []) {
|
|
|
59 |
global $DB;
|
|
|
60 |
|
|
|
61 |
try {
|
|
|
62 |
$cm = $this->get_cm('questionnaire', $record->id, $record->course);
|
|
|
63 |
$context = \context_module::instance($cm->id);
|
|
|
64 |
} catch (\dml_missing_record_exception $ex) {
|
|
|
65 |
// Notify it as we run here as admin, we should see everything.
|
|
|
66 |
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
|
|
|
67 |
$ex->getMessage(), DEBUG_DEVELOPER);
|
|
|
68 |
return false;
|
|
|
69 |
} catch (\dml_exception $ex) {
|
|
|
70 |
// Notify it as we run here as admin, we should see everything.
|
|
|
71 |
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
|
|
|
72 |
return false;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Because there is no database agnostic way to combine all of the possible question content data into one record in
|
|
|
76 |
// get_recordset_by_timestamp, I need to grab it all now and add it to the document.
|
|
|
77 |
$recordset = $DB->get_recordset('questionnaire_question', ['surveyid' => $record->sid, 'deleted' => 'n'],
|
|
|
78 |
'id', 'id,content');
|
|
|
79 |
|
|
|
80 |
// If no question data, don't index this document.
|
|
|
81 |
if (empty($recordset)) {
|
|
|
82 |
return false;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$qcontent = '';
|
|
|
86 |
foreach ($recordset as $question) {
|
|
|
87 |
$qcontent .= $question->content . "\n";
|
|
|
88 |
}
|
|
|
89 |
$recordset->close();
|
|
|
90 |
|
|
|
91 |
// Prepare associative array with data from DB.
|
|
|
92 |
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
|
|
|
93 |
$doc->set('title', content_to_text($record->name, false));
|
|
|
94 |
$doc->set('content', content_to_text($qcontent, $record->introformat));
|
|
|
95 |
$doc->set('contextid', $context->id);
|
|
|
96 |
$doc->set('courseid', $record->course);
|
|
|
97 |
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
|
|
|
98 |
$doc->set('modified', $record->timemodified);
|
|
|
99 |
|
|
|
100 |
return $doc;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Can the current user edit questions in the document.
|
|
|
105 |
*
|
|
|
106 |
* @param int $id The internal search area entity id.
|
|
|
107 |
* @return bool True if the user can see it, false otherwise
|
|
|
108 |
*/
|
|
|
109 |
public function check_access($id) {
|
|
|
110 |
global $DB;
|
|
|
111 |
try {
|
|
|
112 |
$activity = $DB->get_record('questionnaire', ['id' => $id], '*', MUST_EXIST);
|
|
|
113 |
$cminfo = $this->get_cm('questionnaire', $activity->id, $activity->course);
|
|
|
114 |
$cminfo->get_course_module_record();
|
|
|
115 |
} catch (\dml_missing_record_exception $ex) {
|
|
|
116 |
return \core_search\manager::ACCESS_DELETED;
|
|
|
117 |
} catch (\dml_exception $ex) {
|
|
|
118 |
return \core_search\manager::ACCESS_DENIED;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Recheck uservisible although it should have already been checked in core_search.
|
|
|
122 |
if ($cminfo->uservisible === false) {
|
|
|
123 |
return \core_search\manager::ACCESS_DENIED;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
// If the user has the ability to see questions beyond completing a questionnaire, grant access.
|
|
|
127 |
$context = \context_module::instance($cminfo->id);
|
|
|
128 |
if (!(has_capability('mod/questionnaire:readallresponses', $context) ||
|
|
|
129 |
has_capability('mod/questionnaire:readallresponseanytime', $context) ||
|
|
|
130 |
has_capability('mod/questionnaire:editquestions', $context))) {
|
|
|
131 |
return \core_search\manager::ACCESS_DENIED;
|
|
|
132 |
}
|
|
|
133 |
return \core_search\manager::ACCESS_GRANTED;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Link to the module instance.
|
|
|
138 |
*
|
|
|
139 |
* @param \core_search\document $doc
|
|
|
140 |
* @return \moodle_url
|
|
|
141 |
*/
|
|
|
142 |
public function get_doc_url(\core_search\document $doc) {
|
|
|
143 |
return $this->get_context_url($doc);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Link to the module instance.
|
|
|
148 |
*
|
|
|
149 |
* @param \core_search\document $doc
|
|
|
150 |
* @return \moodle_url
|
|
|
151 |
*/
|
|
|
152 |
public function get_context_url(\core_search\document $doc) {
|
|
|
153 |
$context = \context::instance_by_id($doc->get('contextid'));
|
|
|
154 |
return new \moodle_url('/mod/questionnaire/view.php', ['id' => $context->instanceid]);
|
|
|
155 |
}
|
|
|
156 |
}
|