forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion.php
More file actions
156 lines (140 loc) · 6.16 KB
/
question.php
File metadata and controls
156 lines (140 loc) · 6.16 KB
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_questionnaire\search;
/**
* Contains the question class definition for search.
*
* Search area for mod_questionnaire questions. Separated from the activity search so that admins can choose whether or not they
* want this part enabled.
*
* @package mod_questionnaire
* @author Mike Churchward
* @copyright 2016 Mike Churchward (mike.churchward@poetgroup.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question extends \core_search\base_mod {
/**
* Returns recordset containing required data for indexing activities.
*
* @param int $modifiedfrom timestamp
* @return \moodle_recordset
*/
public function get_recordset_by_timestamp($modifiedfrom = 0) {
global $DB;
// Join the survey record to ensure only questionnaires with questions are returned.
$sql = 'SELECT q.* ' .
'FROM {questionnaire} q ' .
'INNER JOIN {questionnaire_survey} s ON q.sid = s.id ' .
'WHERE q.timemodified >= ? ' .
'ORDER BY q.timemodified ASC';
return $DB->get_recordset_sql($sql, [$modifiedfrom]);
}
/**
* Returns the document for a particular question. In this case, the document created contains the question content for all
* questions associated with a questionnaire.
*
* @param \stdClass $record
* @param array $options
* @return \core_search\document
*/
public function get_document($record, $options = []) {
global $DB;
try {
$cm = $this->get_cm('questionnaire', $record->id, $record->course);
$context = \context_module::instance($cm->id);
} catch (\dml_missing_record_exception $ex) {
// Notify it as we run here as admin, we should see everything.
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
$ex->getMessage(), DEBUG_DEVELOPER);
return false;
} catch (\dml_exception $ex) {
// Notify it as we run here as admin, we should see everything.
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
return false;
}
// Because there is no database agnostic way to combine all of the possible question content data into one record in
// get_recordset_by_timestamp, I need to grab it all now and add it to the document.
$recordset = $DB->get_recordset_select('questionnaire_question',
'surveyid = ? AND deleted IS NULL', [$record->sid], 'id', 'id,content');
// If no question data, don't index this document.
if (empty($recordset)) {
return false;
}
$qcontent = '';
foreach ($recordset as $question) {
$qcontent .= $question->content . "\n";
}
$recordset->close();
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', content_to_text($record->name, false));
$doc->set('content', content_to_text($qcontent, $record->introformat));
$doc->set('contextid', $context->id);
$doc->set('courseid', $record->course);
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
$doc->set('modified', $record->timemodified);
return $doc;
}
/**
* Can the current user edit questions in the document.
*
* @param int $id The internal search area entity id.
* @return bool True if the user can see it, false otherwise
*/
public function check_access($id) {
global $DB;
try {
$activity = $DB->get_record('questionnaire', ['id' => $id], '*', MUST_EXIST);
$cminfo = $this->get_cm('questionnaire', $activity->id, $activity->course);
$cminfo->get_course_module_record();
} catch (\dml_missing_record_exception $ex) {
return \core_search\manager::ACCESS_DELETED;
} catch (\dml_exception $ex) {
return \core_search\manager::ACCESS_DENIED;
}
// Recheck uservisible although it should have already been checked in core_search.
if ($cminfo->uservisible === false) {
return \core_search\manager::ACCESS_DENIED;
}
// If the user has the ability to see questions beyond completing a questionnaire, grant access.
$context = \context_module::instance($cminfo->id);
if (!(has_capability('mod/questionnaire:readallresponses', $context) ||
has_capability('mod/questionnaire:readallresponseanytime', $context) ||
has_capability('mod/questionnaire:editquestions', $context))) {
return \core_search\manager::ACCESS_DENIED;
}
return \core_search\manager::ACCESS_GRANTED;
}
/**
* Link to the module instance.
*
* @param \core_search\document $doc
* @return \moodle_url
*/
public function get_doc_url(\core_search\document $doc) {
return $this->get_context_url($doc);
}
/**
* Link to the module instance.
*
* @param \core_search\document $doc
* @return \moodle_url
*/
public function get_context_url(\core_search\document $doc) {
$context = \context::instance_by_id($doc->get('contextid'));
return new \moodle_url('/mod/questionnaire/view.php', ['id' => $context->instanceid]);
}
}