/
lib.php
372 lines (331 loc) · 11.7 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
*
* @package mahara
* @subpackage interaction
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
/**
* Helper interface to hold PluginInteraction's static abstract methods
*/
interface IPluginInteraction {
/**
* override this to add extra pieform elements to the edit instance form
*/
public static function instance_config_form($group, $instance=null);
/**
* override this to save any extra fields in the instance form.
*/
public static function instance_config_save($instance, $values);
/*
* override this to perform extra validation
* public function instance_config_validate(Pieform $form, $values);
*/
}
/**
* Base interaction plugin class
* @abstract
*/
abstract class PluginInteraction extends Plugin implements IPluginInteraction {
public static function get_plugintype_name() {
return 'interaction';
}
public static function instance_config_base_form($plugin, $group, $instance=null) {
global $USER;
return array(
'id' => array(
'type' => 'hidden',
'value' => (isset($instance) ? $instance->get('id') : 0),
),
'plugin' => array(
'type' => 'hidden',
'value' => $plugin,
),
'group' => array(
'type' => 'hidden',
'value' => $group->id
),
'justcreated' => array(
'type' => 'hidden',
'value' => !isset($instance),
),
'creator' => array(
'type' => 'hidden',
'value' => (isset($instance) ? $instance->get('creator') : $USER->get('id'))
),
'title' => array(
'type' => 'text',
'title' => get_string('title', 'group'),
'defaultvalue' => (isset($instance) ? $instance->get('title') : ''),
'rules' => array(
'required' => true,
)
),
'description' => array(
'type' => 'wysiwyg',
'title' => get_string('description'),
'rows' => 10,
'cols' => 60,
'defaultvalue' => (isset($instance) ? $instance->get('description') : ''),
'rules' => array(
'required' => true,
'maxlength' => 65536,
)
),
);
}
/**
* This function returns an array of menu items
* to be displayed
*
* See the function find_menu_children() in lib/web.php
* for a description of the expected array structure.
*
* @return array
*/
public static function menu_items() {
return array();
}
/**
* This function returns an array of menu items
* to be displayed in the top right navigation menu
*
* See the function find_menu_children() in lib/web.php
* for a description of the expected array structure.
*
* @return array
*/
public static function right_nav_menu_items() {
return array();
}
/**
* This function returns an array of admin menu items
* to be displayed
*
* See the function find_menu_children() in lib/web.php
* for a description of the expected array structure.
*
* @return array
*/
public static function admin_menu_items() {
return array();
}
/**
* This function returns an array of institution menu items
* to be displayed
*
* See the function find_menu_children() in lib/web.php
* for a description of the expected array structure.
*
* @return array
*/
public static function institution_menu_items() {
return array();
}
/**
* This function returns an array of institution staff menu items
* to be displayed
*
* See the function find_menu_children() in lib/web.php
* for a description of the expected array structure.
*
* @return array
*/
public static function institution_staff_menu_items() {
return array();
}
}
/**
* Helper interface to hold InteractionInstance's abstract static methods
*/
interface IInteractionInstance {
public static function get_plugin();
}
/**
* Base class for interaction instances
*/
abstract class InteractionInstance implements IInteractionInstance {
protected $id;
protected $title;
protected $description;
protected $group;
protected $plugin; // I wanted to make this private but then get_object_vars doesn't include it.
protected $ctime;
protected $creator;
protected $dirty;
public function __construct($id=0, $data=null) {
if (!empty($id)) {
if (empty($data)) {
if (!$data = get_record('interaction_instance', 'id', $id)) {
throw new InteractionInstanceNotFoundException(get_string('interactioninstancenotfound', 'error', $id));
}
}
$this->id = $id;
}
else {
$this->dirty = true;
}
if (empty($data)) {
$data = array();
}
foreach ((array)$data as $field => $value) {
if (property_exists($this, $field)) {
$this->{$field} = $value;
}
}
if (empty($this->id)) {
$this->ctime = time();
}
$this->plugin = $this->get_plugin();
}
public function get($field) {
if (!property_exists($this, $field)) {
throw new ParamOutOfRangeException("Field $field wasn't found in class " . get_class($this));
}
return $this->{$field};
}
public function set($field, $value) {
if (property_exists($this, $field)) {
if ($this->{$field} != $value) {
// only set it to dirty if it's changed
$this->dirty = true;
$this->{$field} = $value;
}
return true;
}
throw new ParamOutOfRangeException("Field $field wasn't found in class " . get_class($this));
}
public function commit() {
if (empty($this->dirty)) {
return;
}
$fordb = new StdClass;
foreach (get_object_vars($this) as $k => $v) {
if ($k == 'ctime') {
$v = db_format_timestamp($v);
}
$fordb->{$k} = $v;
}
if (empty($this->id)) {
$this->id = insert_record('interaction_instance', $fordb, 'id', true);
}
else {
update_record('interaction_instance', $fordb, 'id');
}
// @TODO maybe handle_event here.
$this->dirty = false;
}
public function delete() {
if (empty($this->id)) {
$this->dirty = false;
return;
}
set_field('interaction_instance', 'deleted', 1, 'id', $this->id);
$this->dirty = false;
}
public abstract function interaction_remove_user($userid);
}
function interaction_check_plugin_sanity($pluginname) {
safe_require('interaction', $pluginname);
$classname = generate_interaction_instance_class_name($pluginname);
if (!class_exists($classname)) {
throw new InstallationException(get_string('classmissing', 'error', $classname, 'interaction', $pluginname));
}
}
function interaction_instance_from_id($id) {
if (!$interaction = get_record('interaction_instance', 'id', $id)) {
throw new InteractionInstanceNotFoundException(get_string('interactioninstancenotfound', 'error', $id));
}
$classname = generate_interaction_instance_class_name($interaction->plugin);
safe_require('interaction', $interaction->plugin);
return new $classname($id, $interaction);
}
function edit_interaction_validation(Pieform $form, $values) {
safe_require('interaction', $values['plugin']);
if (is_callable(array(generate_class_name('interaction', $values['plugin'])), 'instance_config_validate')) {
call_static_method(generate_class_name('interaction', $values['plugin']), 'instance_config_validate', $form, $values);
}
}
function edit_interaction_submit(Pieform $form, $values) {
require_once('embeddedimage.php');
safe_require('interaction', $values['plugin']);
$classname = generate_interaction_instance_class_name($values['plugin']);
$instance = new $classname($values['id']);
$instance->set('creator', $values['creator']);
$instance->set('title', $values['title']);
$newdescription = EmbeddedImage::prepare_embedded_images($values['description'], 'forum', $instance->get('id'), $instance->get('group'));
$instance->set('description', $newdescription);
if (empty($values['id'])) {
$instance->set('group', $values['group']);
}
$instance->commit();
call_static_method(generate_class_name('interaction', $values['plugin']), 'instance_config_save', $instance, $values);
global $SESSION;
$SESSION->add_ok_msg(get_string('interactionsaved', 'group', get_string('name', 'interaction.' . $values['plugin'])));
$returnto = param_alpha('returnto', 'view');
if ($returnto == 'index') {
redirect('/interaction/' . $values['plugin'] . '/index.php?group=' . $instance->get('group'));
}
else {
redirect('/interaction/' . $values['plugin'] . '/view.php?id=' . $instance->get('id'));
}
}
function delete_interaction_submit(Pieform $form, $values) {
require_once(get_config('docroot') . 'interaction/lib.php');
$instance = interaction_instance_from_id($values['id']);
$instance->delete();
global $SESSION;
$SESSION->add_ok_msg(get_string('interactiondeleted', 'group', get_string('name', 'interaction.' . $instance->get('plugin'))));
redirect('/interaction/' . $instance->get('plugin') . '/index.php?group=' . $instance->get('group'));
}
/**
* creates the information for the interaction_sideblock
*
* @param int $groupid the group the sideblock is for
* @param boolean (optional) $membership whether the user is a member
* @return array containing indices 'name', 'weight', 'data'
*/
function interaction_sideblock($groupid, $membership=true) {
$interactiontypes = array_flip(
array_map(
create_function('$a', 'return $a->name;'),
plugins_installed('interaction')
)
);
if (!$interactions = get_records_select_array('interaction_instance',
'"group" = ? AND deleted = ?', array($groupid, 0),
'plugin, ctime', 'id, plugin, title')) {
$interactions = array();
}
foreach ($interactions as $i) {
if (!is_array($interactiontypes[$i->plugin])) {
$interactiontypes[$i->plugin] = array();
}
$interactiontypes[$i->plugin][] = $i;
}
// Sort them according to how the plugin wants them sorted
if ($interactiontypes) {
foreach ($interactiontypes as $plugin => &$interactions) {
safe_require('interaction', $plugin);
$classname = generate_class_name('interaction', $plugin);
if (method_exists($classname, 'sideblock_sort')) {
$interactions = call_static_method($classname, 'sideblock_sort', $interactions);
}
}
}
$data = array(
'group' => $groupid,
'interactiontypes' => $interactiontypes,
'membership' => $membership,
);
// Add a sideblock for group interactions
return array(
'name' => 'groupinteractions',
'weight' => -5,
'data' => $data
);
}