Skip to content
Snippets Groups Projects
Commit 1a0bd8ab authored by Tobias Reischmann's avatar Tobias Reischmann
Browse files

Adjusted step_manager to new workflow model

Removed followedby functions
Added functions for changing sortindex
parent f5cd3095
Branches
Tags
No related merge requests found
...@@ -56,9 +56,8 @@ class step_manager extends subplugin_manager { ...@@ -56,9 +56,8 @@ class step_manager extends subplugin_manager {
if ($subplugin->id) { if ($subplugin->id) {
$DB->update_record('tool_cleanupcourses_step', $subplugin); $DB->update_record('tool_cleanupcourses_step', $subplugin);
} else { } else {
$subplugin->sortindex = self::count_steps_of_workflow($subplugin->worflowid) + 1;
$subplugin->id = $DB->insert_record('tool_cleanupcourses_step', $subplugin); $subplugin->id = $DB->insert_record('tool_cleanupcourses_step', $subplugin);
$record = $DB->get_record('tool_cleanupcourses_step', array('id' => $subplugin->id));
$subplugin = step_subplugin::from_record($record);
} }
$transaction->allow_commit(); $transaction->allow_commit();
} }
...@@ -71,22 +70,71 @@ class step_manager extends subplugin_manager { ...@@ -71,22 +70,71 @@ class step_manager extends subplugin_manager {
global $DB; global $DB;
$transaction = $DB->start_delegated_transaction(); $transaction = $DB->start_delegated_transaction();
if ($record = $DB->get_record('tool_cleanupcourses_step', array('id' => $stepinstanceid))) { if ($record = $DB->get_record('tool_cleanupcourses_step', array('id' => $stepinstanceid))) {
$step = step_subplugin::from_record($record);
self::remove_from_sortindex($step);
$DB->delete_records('tool_cleanupcourses_step', (array) $step);
}
$transaction->allow_commit();
}
$othersteps = $DB->get_records('tool_cleanupcourses_step', array('followedby' => $stepinstanceid)); /**
foreach ($othersteps as $steprecord) { * Removes a subplugin from the sortindex of a workflow and adjusts all other indizes.
$step = step_subplugin::from_record($steprecord); * @param step_subplugin $toberemoved
$step->followedby = null; */
self::insert_or_update($step); private static function remove_from_sortindex(&$toberemoved) {
global $DB;
$transaction = $DB->start_delegated_transaction();
if (isset($toberemoved->sortindex)) {
$subplugins = $DB->get_records_select('tool_cleanupcourses_step',
"sortindex > $toberemoved->sortindex",
array('workflowid' => $toberemoved->worflowid));
foreach ($subplugins as $record) {
$subplugin = step_subplugin::from_record($record);
$subplugin->sortindex--;
self::insert_or_update($subplugin);
}
$toberemoved->sortindex = null;
self::insert_or_update($toberemoved);
}
$transaction->allow_commit();
} }
$othertrigger = $DB->get_records('tool_cleanupcourses_trigger', array('followedby' => $stepinstanceid)); /**
foreach ($othertrigger as $triggerrecord) { * Changes the sortindex of a step by swapping it with another.
$trigger = trigger_subplugin::from_record($triggerrecord); * @param int $stepid id of the step
$trigger->followedby = null; * @param bool $up tells if the step should be set up or down
trigger_manager::insert_or_update($trigger); */
public static function change_sortindex($stepid, $up) {
global $DB;
$step = self::get_step_instance($stepid);
// Prevent first entry to be put up even more.
if ($step->sortindex == 1 && $up) {
return;
}
// Prevent last entry to be put down even more.
if ($step->sortindex == self::count_steps_of_workflow($step->worflowid) && !$up) {
return;
} }
$DB->delete_records('tool_cleanupcourses_step', (array) $record); $index = $step->sortindex;
if ($up) {
$otherindex = $index - 1;
} else {
$otherindex = $index + 1;
} }
$transaction = $DB->start_delegated_transaction();
$otherrecord = $DB->get_record('tool_cleanupcourses_step',
array(
'sortindex' => $otherindex,
'workflowid' => $step->worflowid)
);
$otherstep = step_subplugin::from_record($otherrecord);
$step->sortindex = $otherindex;
$otherstep->sortindex = $index;
self::insert_or_update($step);
self::insert_or_update($otherstep);
$transaction->allow_commit(); $transaction->allow_commit();
} }
...@@ -131,44 +179,20 @@ class step_manager extends subplugin_manager { ...@@ -131,44 +179,20 @@ class step_manager extends subplugin_manager {
return $result; return $result;
} }
/**
* Changes the followedby of a trigger.
* @param int $subpluginid id of the trigger
* @param int $followedby id of the step
*/
public static function change_followedby($subpluginid, $followedby) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$step = self::get_step_instance($subpluginid);
if (!$step) {
return; // TODO: Throw error.
}
$followedby = self::get_step_instance($followedby);
// If step is not defined clear followedby.
if ($followedby) {
$step->followedby = $followedby->id;
} else {
$step->followedby = null;
}
self::insert_or_update($step);
$transaction->allow_commit();
}
/** /**
* Handles an action of the subplugin_settings. * Handles an action of the subplugin_settings.
* @param string $action action to be executed * @param string $action action to be executed
* @param int $subplugin id of the subplugin * @param int $subpluginid id of the subplugin
*/ */
public static function handle_action($action, $subplugin) { public static function handle_action($action, $subpluginid) {
if ($action === ACTION_FOLLOWEDBY_STEP) { if ($action === ACTION_UP_STEP) {
self::change_followedby($subplugin, optional_param('followedby', null, PARAM_INT)); self::change_sortindex($subpluginid, true);
}
if ($action === ACTION_DOWN_STEP) {
self::change_sortindex($subpluginid, false);
} }
if ($action === ACTION_STEP_INSTANCE_DELETE) { if ($action === ACTION_STEP_INSTANCE_DELETE) {
self::remove($subplugin); self::remove($subpluginid);
} }
} }
......
...@@ -24,9 +24,10 @@ defined('MOODLE_INTERNAL') || die(); ...@@ -24,9 +24,10 @@ defined('MOODLE_INTERNAL') || die();
define('ACTION_ENABLE_TRIGGER', 'enable'); define('ACTION_ENABLE_TRIGGER', 'enable');
define('ACTION_DISABLE_TRIGGER', 'disable'); define('ACTION_DISABLE_TRIGGER', 'disable');
define('ACTION_UP_TRIGGER', 'up'); define('ACTION_UP_TRIGGER', 'up_trigger');
define('ACTION_DOWN_TRIGGER', 'down'); define('ACTION_DOWN_TRIGGER', 'down_trigger');
define('ACTION_WORKFLOW_TRIGGER', 'workflow_trigger'); define('ACTION_WORKFLOW_TRIGGER', 'workflow_trigger');
define('ACTION_FOLLOWEDBY_STEP', 'followedby_step'); define('ACTION_UP_STEP', 'up_step');
define('ACTION_DOWN_STEP', 'down_step');
define('ACTION_STEP_INSTANCE_FORM', 'step_instance_form'); define('ACTION_STEP_INSTANCE_FORM', 'step_instance_form');
define('ACTION_STEP_INSTANCE_DELETE', 'step_instance_delete'); define('ACTION_STEP_INSTANCE_DELETE', 'step_instance_delete');
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment