-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
216 lines (188 loc) · 7.51 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
210
211
212
213
214
215
216
<?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/>.
/**
* This file contains the moodle hooks for the assign module.
*
* It delegates most functions to the assignment class.
*
* @package mod_observation
* @copyright Matthew Hilton, Celine Lindeque, Jack Kepper, Jared Hungerford
* @author Matthew Hilton <[email protected]>, Celine Lindeque
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Return the preconfigured tools which are configured for inclusion in the activity picker.
*
* @param \core_course\local\entity\content_item $defaultmodulecontentitem reference to the content item for the Observation module.
* @return array the array of content items.
*/
function observation_get_course_content_items(\core_course\local\entity\content_item $defaultmodulecontentitem) {
global $OUTPUT;
$contentitem = new \core_course\local\entity\content_item(
1,
"observationActivityModule",
new core_course\local\entity\string_title("Observation"),
$defaultmodulecontentitem->get_link(),
$OUTPUT->pix_icon('icon', 'add observation', 'observation'),
$defaultmodulecontentitem->get_help(),
$defaultmodulecontentitem->get_archetype(),
$defaultmodulecontentitem->get_component_name(),
MOD_PURPOSE_ASSESSMENT
);
return [$contentitem];
}
/**
* Adds an Observation instance.
* @param object $data form data
* @return int new observation instance id
*/
function observation_add_instance($data): int {
return \mod_observation\observation_manager::modify_instance(array(
"course" => (int)$data->course,
"name" => $data->name,
"intro" => "",
"timemodified" => time(),
"observer_ins" => $data->observerins_editor['text'],
"observer_ins_f" => $data->observerins_editor['format'],
"observee_ins" => $data->observeeins_editor['text'],
"observee_ins_f" => $data->observeeins_editor['format'],
"students_self_unregister" => (int) $data->students_self_unregister
));
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $data the data that came from the form.
* @return bool true on success, false or a string error message on failure.
*/
function observation_update_instance($data): bool {
$success = \mod_observation\observation_manager::modify_instance(array(
"id" => $data->instance,
"name" => $data->name,
"timemodified" => time(),
"observer_ins" => $data->observerins_editor['text'],
"observer_ins_f" => $data->observerins_editor['format'],
"observee_ins" => $data->observeeins_editor['text'],
"observee_ins_f" => $data->observeeins_editor['format'],
"students_self_unregister" => (int) $data->students_self_unregister
));
if ($success === true) {
// Update all the calendar events to get the new data.
$timeslots = \mod_observation\timeslot_manager::get_time_slots($data->instance);
$timeslotids = array_column($timeslots, 'id');
foreach ($timeslotids as $slotid) {
\mod_observation\timeslot_manager::update_timeslot_calendar_events($data->instance, $slotid);
}
}
return $success;
}
/**
* Delete observation instance.
* @param int $id
* @return bool true
*/
function observation_delete_instance($id) {
global $DB, $USER;
if (!$observation = $DB->get_record('observation', ['id' => $id])) {
return false;
}
$timeslots = \mod_observation\timeslot_manager::get_time_slots($observation->id);
foreach ($timeslots as $timeslot) {
\mod_observation\timeslot_manager::delete_time_slot($observation->id, $timeslot->id, $USER->id);
}
$sessions = \mod_observation\session_manager::get_sessions($observation->id);
foreach ($sessions as $session) {
\mod_observation\session_manager::delete_observation_session($observation->id, $session->id);
}
$points = \mod_observation\observation_manager::get_observation_points($observation->id);
foreach ($points as $point) {
\mod_observation\observation_manager::delete_observation_point($observation->id, $point->id);
}
$DB->delete_records('observation', ['id' => $observation->id]);
return true;
}
/**
* Defines what features this activity supports.
* @param mixed $feature given feature enum
* @return mixed True is supports feature, else null.
*/
function observation_supports($feature) {
switch($feature) {
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
// Activity plugins default this to true, so explicity set to false.
// Since it is not supported by mod_observation.
case FEATURE_MOD_INTRO:
return false;
default:
return null;
}
}
/**
* Determines if a calendar event is visible.
* @param calendar_event $event event to determine visibility for.
*/
function mod_observation_core_calendar_is_event_visible(calendar_event $event) {
global $USER;
global $DB;
// For some archaic reason, $event->userid is NOT the userID of the event in the database,
// but is instead the caller of this function (i.e. the same as $USER->id).
// We have no choice but to query the $DB again.
$sql = 'SELECT *
FROM {observation_timeslots}
WHERE (observer_event_id = :observerevent AND observer_id = :observer)
OR (observee_event_id = :observeeevent AND observee_id = :observee)';
$params = [
"observerevent" => $event->id,
"observeeevent" => $event->id,
"observer" => $USER->id,
"observee" => $USER->id
];
$matchingevent = $DB->get_records_sql($sql, $params);
return !empty($matchingevent);
}
/**
* Context checks and serves file from certain areas
* @param mixed $course course
* @param mixed $cm course module
* @param context $context context
* @param string $filearea file area
* @param mixed $args args
* @param bool $forcedownload bool if download should be forced
* @param array $options an array of options
*/
function observation_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
// Ensure logged in to course.
require_course_login($course->id);
// Ensure has correct capability.
require_capability('mod/observation:viewfiles', $context);
if ($context->contextlevel != CONTEXT_MODULE) {
send_file_not_found();
}
if (strpos($filearea, 'response') !== 0 ) {
send_file_not_found();
}
$fs = get_file_storage();
$filename = array_pop($args);
$itemid = array_pop($args);
$filepath = '/';
$file = $fs->get_file($context->id, 'mod_observation', $filearea, $itemid, $filepath, $filename);
send_stored_file($file, null, 0, $forcedownload, $options);
}