-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
278 lines (242 loc) · 9.23 KB
/
lib.php
File metadata and controls
278 lines (242 loc) · 9.23 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?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/>.
/**
* Library of interface functions and constants.
*
* @package mod_virtualcoach
* @copyright 2019 Salfa Meridian S.L. - Aula21
* @author Dany Daniel Roche <ddanielroche@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Return if the plugin supports $feature.
*
* @param string $feature Constant representing the feature.
* @return true | null True if the feature is supported, null otherwise.
*/
function virtualcoach_supports($feature) {
switch ($feature) {
case FEATURE_MOD_INTRO:
return true;
default:
return null;
}
}
/**
* Saves a new instance of the mod_virtualcoach into the database.
*
* Given an object containing all the necessary data, (defined by the form
* in mod_form.php) this function will create a new instance and return the id
* number of the instance.
*
* @param object $moduleinstance An object from the form.
* @param mod_virtualcoach_mod_form $mform The form.
* @return int The id of the newly inserted record.
*/
function virtualcoach_add_instance($moduleinstance, $mform = null) {
global $DB;
$moduleinstance->timecreated = time();
$id = $DB->insert_record('virtualcoach', $moduleinstance);
return $id;
}
/**
* Updates an instance of the mod_virtualcoach in the database.
*
* Given an object containing all the necessary data (defined in mod_form.php),
* this function will update an existing instance with new data.
*
* @param object $moduleinstance An object from the form in mod_form.php.
* @param mod_virtualcoach_mod_form $mform The form.
* @return bool True if successful, false otherwise.
*/
function virtualcoach_update_instance($moduleinstance, $mform = null) {
global $DB;
$moduleinstance->timemodified = time();
$moduleinstance->id = $moduleinstance->instance;
return $DB->update_record('virtualcoach', $moduleinstance);
}
/**
* Removes an instance of the mod_virtualcoach from the database.
*
* @param int $id Id of the module instance.
* @return bool True if successful, false on failure.
*/
function virtualcoach_delete_instance($id) {
global $DB;
$exists = $DB->get_record('virtualcoach', array('id' => $id));
if (!$exists) {
return false;
}
$DB->delete_records('virtualcoach', array('id' => $id));
return true;
}
/**
* Extends the global navigation tree by adding mod_virtualcoach nodes if there is a relevant content.
*
* This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
*
* @param navigation_node $virtualcoachnode An object representing the navigation tree node.
* @param stdClass $course.
* @param stdClass $module.
* @param cm_info $cm.
*/
function virtualcoach_extend_navigation($virtualcoachnode, $course, $module, $cm) {
}
/**
* Extends the settings navigation with the mod_virtualcoach settings.
*
* This function is called when the context for the page is a mod_virtualcoach module.
* This is not called by AJAX so it is safe to rely on the $PAGE.
*
* @param settings_navigation $settingsnav {@link settings_navigation}
* @param navigation_node $virtualcoachnode {@link navigation_node}
*/
function virtualcoach_extend_settings_navigation($settingsnav, $virtualcoachnode = null) {
}
/**
* Request and render event form fragment.
*
* @param array $args The fragment arguments.
* @return string The rendered mform fragment.
*/
function mod_virtualcoach_output_fragment_event_form($args) {
global $CFG, $OUTPUT, $USER;
require_once($CFG->libdir . '/grouplib.php');
$html = '';
$data = [];
$eventid = isset($args['eventid']) ? clean_param($args['eventid'], PARAM_INT) : null;
$starttime = isset($args['starttime']) ? clean_param($args['starttime'], PARAM_INT) : null;
$courseid = (isset($args['courseid']) && $args['courseid'] != SITEID) ? clean_param($args['courseid'], PARAM_INT) : null;
$categoryid = isset($args['categoryid']) ? clean_param($args['categoryid'], PARAM_INT) : null;
$location = isset($args['location']) ? clean_param($args['location'], PARAM_INT) : null;
$event = null;
$hasformdata = isset($args['formdata']) && !empty($args['formdata']);
$context = \context_user::instance($USER->id);
$editoroptions = mod_virtualcoach\event\forms\create::build_editor_options($context);
$formoptions = ['editoroptions' => $editoroptions, 'courseid' => $courseid];
$draftitemid = 0;
if ($hasformdata) {
parse_str(clean_param($args['formdata'], PARAM_TEXT), $data);
if (isset($data['description']['itemid'])) {
$draftitemid = $data['description']['itemid'];
}
}
if ($starttime) {
$formoptions['starttime'] = $starttime;
}
if (is_null($eventid)) {
if (!empty($courseid)) {
$groupcoursedata = groups_get_course_data($courseid);
$formoptions['groups'] = [];
foreach ($groupcoursedata->groups as $groupid => $groupdata) {
$formoptions['groups'][$groupid] = $groupdata->name;
}
}
$mform = new mod_virtualcoach\event\forms\create(
null,
$formoptions,
'post',
'',
null,
true,
$data
);
// Let's check first which event types user can add.
$eventtypes = calendar_get_allowed_event_types($courseid);
// If the user is on course context and is allowed to add course events set the event type default to course.
/*if (!empty($courseid) && !empty($eventtypes['course'])) {
$data['eventtype'] = 'course';
$data['courseid'] = $courseid;
$data['groupcourseid'] = $courseid;
} else if (!empty($categoryid) && !empty($eventtypes['category'])) {
$data['eventtype'] = 'category';
$data['categoryid'] = $categoryid;
} else if (!empty($groupcoursedata) && !empty($eventtypes['group'])) {
$data['groupcourseid'] = $courseid;
$data['groups'] = $groupcoursedata->groups;
}*/
$data['location'] = $location;
$data['courseid'] = $courseid;
$cm = get_coursemodule_from_id('virtualcoach', $args['moduleid'], 0, false, MUST_EXIST);
$data['instance'] = $cm->instance;
$mform->set_data($data);
} else {
$event = calendar_event::load($eventid);
if (!calendar_edit_event_allowed($event)) {
print_error('nopermissiontoupdatecalendar');
}
$mapper = new \core_calendar\local\event\mappers\create_update_form_mapper();
$eventdata = $mapper->from_legacy_event_to_data($event);
$data = array_merge((array) $eventdata, $data);
$data['duration'] = 2;
if (!$data['timedurationminutes']) {
$data['timedurationminutes'] = (int)(($event->properties()->timeduration+1)/60);
}
$event->count_repeats();
$formoptions['event'] = $event;
if (!empty($event->courseid)) {
$groupcoursedata = groups_get_course_data($event->courseid);
$formoptions['groups'] = [];
foreach ($groupcoursedata->groups as $groupid => $groupdata) {
$formoptions['groups'][$groupid] = $groupdata->name;
}
}
$data['description']['text'] = file_prepare_draft_area(
$draftitemid,
$event->context->id,
'calendar',
'event_description',
$event->id,
null,
$data['description']['text']
);
$data['description']['itemid'] = $draftitemid;
/*$mform = new \core_calendar\local\event\forms\update(
null,
$formoptions,
'post',
'',
null,
true,
$data
);*/
$mform = new mod_virtualcoach\event\forms\create(
null,
$formoptions,
'post',
'',
null,
true,
$data
);
$mform->set_data($data);
// Check to see if this event is part of a subscription or import.
// If so display a warning on edit.
if (isset($event->subscriptionid) && ($event->subscriptionid != null)) {
$renderable = new \core\output\notification(
get_string('eventsubscriptioneditwarning', 'calendar'),
\core\output\notification::NOTIFY_INFO
);
$html .= $OUTPUT->render($renderable);
}
}
if ($hasformdata) {
$mform->is_validated();
}
$html .= $mform->render();
return $html;
}