Skip to content

Commit 3fc86a3

Browse files
committed
added: support for entity_tools
1 parent 3dd25f0 commit 3fc86a3

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace ColdTrick\Questions;
4+
5+
class Access {
6+
7+
/**
8+
* After the question is updated in the databse make sure the answers have the same access_id
9+
*
10+
* @param string $event the name of the event
11+
* @param string $type the type of the event
12+
* @param \ElggObject $entity the affected object
13+
*
14+
* @return void
15+
*/
16+
public static function updateQuestion($event, $type, $entity) {
17+
18+
if (!($entity instanceof \ElggQuestion)) {
19+
return;
20+
}
21+
22+
$org_attributes = $entity->getOriginalAttributes();
23+
if (elgg_extract('access_id', $org_attributes) === null) {
24+
// access wasn't updated
25+
return;
26+
}
27+
28+
// ignore access for this part
29+
$ia = elgg_set_ignore_access(true);
30+
31+
// get all the answers for this question
32+
$answers = $entity->getAnswers(['limit' => false]);
33+
if (empty($answers)) {
34+
// restore access
35+
elgg_set_ignore_access($ia);
36+
37+
return;
38+
}
39+
40+
/* @var $answer \ElggAnswer */
41+
foreach ($answers as $answer) {
42+
// update the access_id with the questions access_id
43+
$answer->access_id = $entity->access_id;
44+
45+
$answer->save();
46+
}
47+
48+
// restore access
49+
elgg_set_ignore_access($ia);
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace ColdTrick\Questions;
4+
5+
use ColdTrick\EntityTools\Migrate;
6+
7+
class MigrateQuestions extends Migrate {
8+
9+
/**
10+
* Add questions to the supported types for EntityTools
11+
*
12+
* @param string $hook the name of the hook
13+
* @param string $type the type of the hook
14+
* @param array $return_value current return value
15+
* @param mixed $params supplied params
16+
*
17+
* @return array
18+
*/
19+
public static function supportedSubtypes($hook, $type, $return_value, $params) {
20+
21+
$return_value[\ElggQuestion::SUBTYPE] = '\ColdTrick\Questions\MigrateQuestions';
22+
23+
return $return_value;
24+
}
25+
26+
/**
27+
* {@inheritDoc}
28+
* @see \ColdTrick\EntityTools\Migrate::setSupportedOptions()
29+
*/
30+
protected function setSupportedOptions() {
31+
$this->supported_options = [
32+
'backdate' => true,
33+
'change_owner' => true,
34+
'change_container' => true,
35+
];
36+
}
37+
}

start.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ function questions_init() {
7878
elgg_register_event_handler('create', 'object', '\ColdTrick\Questions\ContentSubscriptions::createAnswer');
7979
elgg_register_event_handler('create', 'object', '\ColdTrick\Questions\ContentSubscriptions::createCommentOnAnswer');
8080

81+
elgg_register_plugin_hook_handler('supported_types', 'entity_tools', '\ColdTrick\Questions\MigrateQuestions::supportedSubtypes');
82+
8183
// events
8284
elgg_register_event_handler('leave', 'group', 'questions_leave_group_handler');
8385
elgg_register_event_handler('delete', 'relationship', 'questions_leave_site_handler');
86+
elgg_register_event_handler('update:after', 'object', '\ColdTrick\Questions\Access::updateQuestion');
8487

8588
// actions
8689
elgg_register_action('questions/toggle_expert', dirname(__FILE__) . '/actions/toggle_expert.php');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
if (elgg_get_plugin_setting('limit_to_groups', 'questions', 'no') === 'yes') {
4+
$vars['add_users'] = false;
5+
}
6+
7+
echo elgg_view('input/entity_tools_container', $vars);

0 commit comments

Comments
 (0)