Skip to content

Commit

Permalink
fix(question,section): more resilient order change handling
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Nov 27, 2020
1 parent 1ea0c64 commit d0a4c33
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ public function duplicate($options = []) {
* @return boolean true on success, false otherwise
*/
public function moveUp() {
global $DB;

$order = $this->fields['order'];
$formId = $this->fields['plugin_formcreator_forms_id'];
$otherItem = new static();
Expand All @@ -213,15 +215,22 @@ public function moveUp() {
return false;
}
$success = true;
$DB->beginTransaction();
$success = $success && $this->update([
'id' => $this->getID(),
'order' => $otherItem->getField('order'),
'order' => $otherItem->fields['order'],
]);
$success = $success && $otherItem->update([
'id' => $otherItem->getID(),
'order' => $order,
]);

if (!$success) {
$DB->rollBack();
} else {
$DB->commit();
}

return $success;
}

Expand All @@ -230,8 +239,10 @@ public function moveUp() {
* @return boolean true on success, false otherwise
*/
public function moveDown() {
$order = $this->fields['order'];
$formId = $this->fields['plugin_formcreator_forms_id'];
global $DB;

$order = $this->fields['order'];
$formId = $this->fields['plugin_formcreator_forms_id'];
$otherItem = new static();
$otherItem->getFromDBByRequest([
'WHERE' => [
Expand All @@ -247,15 +258,22 @@ public function moveDown() {
return false;
}
$success = true;
$DB->beginTransaction();
$success = $success && $this->update([
'id' => $this->getID(),
'order' => $otherItem->getField('order'),
'order' => $otherItem->fields['order'],
]);
$success = $success && $otherItem->update([
'id' => $otherItem->getID(),
'order' => $order,
]);

if (!$success) {
$DB->rollBack();
} else {
$DB->commit();
}

return $success;
}

Expand Down

0 comments on commit d0a4c33

Please sign in to comment.