Skip to content

Commit 0dc1d7b

Browse files
committed
added: question database seeder
1 parent c30b398 commit 0dc1d7b

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
<?php
2+
3+
namespace ColdTrick\Questions;
4+
5+
use Elgg\Database\Seeds\Seed;
6+
use Elgg\Exceptions\Seeding\MaxAttemptsException;
7+
use Elgg\Values;
8+
9+
/**
10+
* Questions database seeder
11+
*/
12+
class Seeder extends Seed {
13+
14+
/**
15+
* {@inheritDoc}
16+
*/
17+
public function seed() {
18+
$this->advance($this->getCount());
19+
20+
$logger = elgg()->logger;
21+
$session_manager = elgg()->session_manager;
22+
$logged_in = $session_manager->getLoggedInUser();
23+
24+
$plugin = elgg_get_plugin_from_id('questions');
25+
$groups_only = $plugin->limit_to_groups === 'yes';
26+
27+
$experts_enabled = $plugin->experts_enabled;
28+
$experts_answer = $plugin->experts_answer;
29+
$experts_mark = $plugin->experts_mark;
30+
31+
// for seeding change the settings
32+
$plugin->experts_enabled = 'no';
33+
$plugin->experts_answer = 'no';
34+
$plugin->experts_mark = 'no';
35+
36+
while ($this->getCount() < $this->limit) {
37+
$owner = $this->getRandomUser();
38+
39+
$session_manager->setLoggedInUser($owner);
40+
41+
$container_guid = $owner->guid;
42+
if ($groups_only || $this->faker()->boolean()) {
43+
$group = $this->getRandomGroup();
44+
$group->enableTool('questions');
45+
46+
$container_guid = $group->guid;
47+
}
48+
49+
try {
50+
$logger->disable();
51+
52+
/* @var $entity \ElggQuestion */
53+
$entity = $this->createObject([
54+
'subtype' => \ElggQuestion::SUBTYPE,
55+
'owner_guid' => $owner->guid,
56+
'container_guid' => $container_guid,
57+
'comments_enabled' => $this->faker()->boolean() ? 'on' : 'off',
58+
]);
59+
60+
$logger->enable();
61+
} catch (MaxAttemptsException $e) {
62+
// unable to create question with given options
63+
$logger->enable();
64+
continue;
65+
}
66+
67+
if ($entity->commentsEnabled()) {
68+
$this->createComments($entity, $this->faker()->numberBetween(0, 5));
69+
}
70+
71+
$this->createLikes($entity);
72+
$this->createAnswers($entity);
73+
74+
// add river event
75+
elgg_create_river_item([
76+
'view' => 'river/object/question/create',
77+
'action_type' => 'create',
78+
'subject_guid' => $entity->owner_guid,
79+
'object_guid' => $entity->guid,
80+
'target_guid' => $entity->container_guid,
81+
'access_id' => $entity->access_id,
82+
'posted' => $entity->time_created,
83+
]);
84+
85+
// check for a solution time limit
86+
$solution_time = questions_get_solution_time($entity->getContainerEntity());
87+
if ($solution_time) {
88+
// add x number of days when the question should be solved
89+
$entity->solution_time = Values::normalizeTimestamp("+{$solution_time} days");
90+
}
91+
92+
if ($this->faker()->boolean(20)) {
93+
$entity->status = \ElggQuestion::STATUS_CLOSED;
94+
}
95+
96+
$this->advance();
97+
}
98+
99+
// restore plugin settings
100+
$plugin->experts_enabled = $experts_enabled;
101+
$plugin->experts_answer = $experts_answer;
102+
$plugin->experts_mark = $experts_mark;
103+
104+
// restore logged in user
105+
if ($logged_in) {
106+
$session_manager->setLoggedInUser($logged_in);
107+
} else {
108+
$session_manager->removeLoggedInUser();
109+
}
110+
}
111+
112+
/**
113+
* {@inheritDoc}
114+
*/
115+
public function unseed() {
116+
/* @var $entities \ElggBatch */
117+
$entities = elgg_get_entities([
118+
'type' => 'object',
119+
'subtype' => \ElggQuestion::SUBTYPE,
120+
'metadata_name' => '__faker',
121+
'limit' => false,
122+
'batch' => true,
123+
'batch_inc_offset' => false,
124+
]);
125+
126+
/* @var $entity \ElggQuestion */
127+
foreach ($entities as $entity) {
128+
if ($entity->delete()) {
129+
$this->log("Deleted question {$entity->guid}");
130+
} else {
131+
$this->log("Failed to delete question {$entity->guid}");
132+
$entities->reportFailure();
133+
continue;
134+
}
135+
136+
$this->advance();
137+
}
138+
}
139+
140+
/**
141+
* {@inheritDoc}
142+
*/
143+
public static function getType(): string {
144+
return \ElggQuestion::SUBTYPE;
145+
}
146+
147+
/**
148+
* {@inheritDoc}
149+
*/
150+
protected function getCountOptions(): array {
151+
return [
152+
'type' => 'object',
153+
'subtype' => \ElggQuestion::SUBTYPE,
154+
];
155+
}
156+
157+
/**
158+
* Add answers to the question
159+
*
160+
* @param \ElggQuestion $entity question to create answers for
161+
*
162+
* @return int
163+
*/
164+
protected function createAnswers(\ElggQuestion $entity): int {
165+
$logger = elgg()->logger;
166+
$session_manager = elgg()->session_manager;
167+
$logged_in = $session_manager->getLoggedInUser();
168+
169+
$max_answers = $this->faker()->numberBetween(1, 10);
170+
$created = [];
171+
$owners = [];
172+
for ($i = 0; $i < $max_answers; $i++) {
173+
$owner = $this->getRandomUser($owners);
174+
175+
$session_manager->setLoggedInUser($owner);
176+
177+
try {
178+
$logger->disable();
179+
180+
/* @var $answer \ElggAnswer */
181+
$answer = $this->createObject([
182+
'subtype' => \ElggAnswer::SUBTYPE,
183+
'owner_guid' => $owner->guid,
184+
'container_guid' => $entity->guid,
185+
'access_id' => $entity->access_id,
186+
]);
187+
188+
$logger->enable();
189+
} catch (MaxAttemptsException $e) {
190+
// unable to create with the given options
191+
$logger->enable();
192+
continue;
193+
}
194+
195+
// remove some seeding data
196+
unset($answer->title);
197+
unset($answer->tags);
198+
199+
if ($entity->commentsEnabled()) {
200+
$this->createComments($answer, $this->faker()->numberBetween(0, 3));
201+
}
202+
203+
$this->createLikes($answer);
204+
205+
// create river event
206+
elgg_create_river_item([
207+
'view' => 'river/object/answer/create',
208+
'action_type' => 'create',
209+
'subject_guid' => $answer->owner_guid,
210+
'object_guid' => $answer->guid,
211+
'target_guid' => $entity->guid,
212+
'access_id' => $answer->access_id,
213+
'posted' => $answer->time_created,
214+
]);
215+
216+
$created[] = $answer;
217+
$owners[] = $owner->guid;
218+
}
219+
220+
// mark an answer as the correct answer
221+
if (!empty($created) && $this->faker()->boolean(10)) {
222+
$key = array_rand($created);
223+
224+
/* @var $correct_answer \ElggAnswer */
225+
$correct_answer = $created[$key];
226+
$correct_answer->markAsCorrect();
227+
}
228+
229+
if ($logged_in) {
230+
$session_manager->setLoggedInUser($logged_in);
231+
} else {
232+
$session_manager->removeLoggedInUser();
233+
}
234+
235+
return count($created);
236+
}
237+
}

elgg-plugin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@
144144
'\ColdTrick\Questions\Search::optionsAddAnswers' => [],
145145
],
146146
],
147+
'seeds' => [
148+
'database' => [
149+
'\ColdTrick\Questions\Seeder::register' => [],
150+
],
151+
],
147152
'supported_types' => [
148153
'entity_tools' => [
149154
'\ColdTrick\Questions\Plugins\EntityTools::registerQuestions' => [],

0 commit comments

Comments
 (0)