Skip to content

Commit

Permalink
feat(question): bring a question to 1st position
Browse files Browse the repository at this point in the history
Signed-off-by: LUIZ Manoel Resplande Oliveira
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
LUIZ Manoel Resplande Oliveira authored and btry committed Oct 9, 2019
1 parent 8d5879b commit 9ed109e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions front/question.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
if ($question->getFromDB((int) $_POST['id'])) {
if ($_POST['way'] == 'up') {
$question->moveUp();
} else if($_POST['way'] == 'top') {
$question->moveTop();
} else {
$question->moveDown();
}
Expand Down
48 changes: 47 additions & 1 deletion inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
onclick="moveSection(\'' . $token . '\', ' . $section['id'] . ', \'up\');"> ';
}
echo "</span>";

echo '</th>';
echo '</tr>';

Expand Down Expand Up @@ -226,6 +226,10 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
title='" . __('Required', 'formcreator') . "'
onclick='setRequired(\"".$token."\", ".$question['id'].", ".($question['required']?0:1).")' > ";
echo "</span>";
} else {
echo "<span class='form_control pointer'>";
echo '<div width="18px"></div>';
echo "</span>";
}

echo "<span class='form_control pointer'>";
Expand All @@ -244,6 +248,14 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
}
echo "</span>";

echo "<span class='form_control pointer'>";
if ($question['order'] != 1) {
echo '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/formcreator/pics/chevron-up.png"
title="' . __('Bring top') . '"
onclick="moveQuestion(\'' . $token . '\', ' . $question['id'] . ', \'top\');" align="absmiddle"> ';
}
echo "</span>";

echo '</td>';
echo '</tr>';
}
Expand Down Expand Up @@ -506,6 +518,40 @@ public function moveUp() {
}
}

/**
* Moves the question to top
*/
public function moveTop() {
global $DB;

$order = $this->fields['order'];
$sectionId = $this->fields['plugin_formcreator_sections_id'];
$otherItem = new static();

$result = $DB->request([
'FROM' => $otherItem->getTable(),
'WHERE' => [
'plugin_formcreator_sections_id' => $sectionId,
'order' => ['<', $order],
],
'ORDER' => 'order ASC',
]);

$this->update([
'id' => $this->getID(),
'order' => '1',
'_skip_checks' => true,
]);

foreach ($result as $value) {
$otherItem->update([
'id' => $value['id'],
'order' => $value['order'] + 1,
'_skip_checks' => true,
]);
}
}

/**
* Moves the question down in the ordered list of questions in the section
*/
Expand Down
Binary file added pics/chevron-up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/pics_chevron-up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/suite-unit/PluginFormcreatorQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,29 @@ public function testExport() {
->hasKeys($fieldsWithoutID + $extraFields + ['id'])
->hasSize(1 + count($fieldsWithoutID) + count($extraFields));
}

public function testMoveTop() {
$section = $this->getSection();
$question1 = $this->getQuestion([
'plugin_formcreator_sections_id' => $section->getID(),
]);
$question2 = $this->getQuestion([
'plugin_formcreator_sections_id' => $section->getID(),
]);
$question3 = $this->getQuestion([
'plugin_formcreator_sections_id' => $section->getID(),
]);

$this->integer((int) $question1->fields['order'])->isEqualTo(1);
$this->integer((int) $question2->fields['order'])->isEqualTo(2);
$this->integer((int) $question3->fields['order'])->isEqualTo(3);
$question3->moveTop();
// Reload questions
$question1->getFromDB($question1->getID());
$question2->getFromDB($question2->getID());
$question3->getFromDB($question3->getID());
$this->integer((int) $question3->fields['order'])->isEqualTo(1);
$this->integer((int) $question1->fields['order'])->isEqualTo(2);
$this->integer((int) $question2->fields['order'])->isEqualTo(3);
}
}

0 comments on commit 9ed109e

Please sign in to comment.