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

New workflow views for trigger instance management

parent 8cfa793f
No related branches found
No related tags found
No related merge requests found
......@@ -16,8 +16,10 @@
namespace tool_cleanupcourses;
use tool_cleanupcourses\entity\trigger_subplugin;
use tool_cleanupcourses\form\form_workflow_instance;
use tool_cleanupcourses\form\form_step_instance;
use tool_cleanupcourses\form\form_trigger_instance;
use tool_cleanupcourses\manager\step_manager;
use tool_cleanupcourses\manager\settings_manager;
use tool_cleanupcourses\manager\trigger_manager;
......@@ -277,14 +279,38 @@ class workflow_settings {
/**
* Write the HTML for the step instance form.
* @param $form \moodleform form to be displayed.
*/
private function view_step_instance_form($form) {
$workflow = workflow_manager::get_workflow($this->workflowid);
$this->view_instance_form($form,
get_string('subpluginssettings_edit_step_instance_heading', 'tool_cleanupcourses',
$workflow->title));
}
/**
* Write the HTML for the trigger instance form.
* @param $form \moodleform form to be displayed.
*/
private function view_trigger_instance_form($form) {
$workflow = workflow_manager::get_workflow($this->workflowid);
$this->view_instance_form($form,
get_string('subpluginssettings_edit_trigger_instance_heading', 'tool_cleanupcourses',
$workflow->title));
}
/**
* Write the HTML for subplugin instance form with specific header.
* @param $form \moodleform form to be displayed.
* @param $header string header of the form.
*/
private function view_instance_form($form, $header) {
global $OUTPUT;
// Set up the table.
$this->view_header();
echo $OUTPUT->heading(get_string('subpluginssettings_edit_instance_heading', 'tool_cleanupcourses'));
echo $OUTPUT->heading($header);
echo $form->render();
......@@ -325,8 +351,51 @@ class workflow_settings {
global $PAGE;
$this->check_permissions();
if ($action === ACTION_TRIGGER_INSTANCE_FORM) {
$subpluginname = null;
$settings = null;
if ($trigger = trigger_manager::get_trigger_for_workflow($this->workflowid)) {
$settings = settings_manager::get_settings($trigger->id, SETTINGS_TYPE_TRIGGER);
} else if (!$trigger && $name = optional_param('subpluginname', null, PARAM_ALPHA)) {
$subpluginname = $name;
}
$form = new form_trigger_instance($PAGE->url, $this->workflowid, $trigger, $subpluginname, $settings);
if ($form->is_cancelled()) {
// Skip this part and continue with requiring a trigger if still null.
} else if ($form->is_submitted() && $form->is_validated() && $data = $form->get_submitted_data()) {
if (!empty($data->id)) {
$trigger = trigger_manager::get_instance($data->id);
$trigger->instancename = $data->instancename;
} else {
$trigger = trigger_subplugin::from_record($data);
}
trigger_manager::insert_or_update($trigger);
// Save local subplugin settings.
settings_manager::save_settings($trigger->id, SETTINGS_TYPE_TRIGGER, $data->subpluginname, $data);
$this->view_plugins_table();
return;
} else {
$this->view_trigger_instance_form($form);
return;
}
}
// If trigger is not yet set, redirect to trigger form!
$trigger = trigger_manager::get_trigger_for_workflow($this->workflowid);
if ($trigger === null) {
$subpluginname = null;
if ($name = optional_param('subpluginname', null, PARAM_ALPHA)) {
$subpluginname = $name;
}
$triggerform = new form_trigger_instance($PAGE->url, $this->workflowid, null, $subpluginname);
$this->view_trigger_instance_form($triggerform);
return;
}
step_manager::handle_action($action, $subplugin);
if ($action === ACTION_STEP_INSTANCE_FORM) {
$steptomodify = null;
$subpluginname = null;
$stepsettings = null;
......@@ -337,7 +406,7 @@ class workflow_settings {
$this->view_plugins_table();
return;
}
$stepsettings = settings_manager::get_settings($stepid);
$stepsettings = settings_manager::get_settings($stepid, SETTINGS_TYPE_STEP);
} else if ($name = optional_param('subpluginname', null, PARAM_ALPHA)) {
$subpluginname = $name;
} else {
......@@ -347,10 +416,10 @@ class workflow_settings {
$form = new form_step_instance($PAGE->url, $steptomodify, $this->workflowid, $subpluginname, $stepsettings);
if ($action === ACTION_STEP_INSTANCE_FORM) {
$this->view_step_instance_form($form);
} else {
if ($form->is_submitted() && !$form->is_cancelled() && $data = $form->get_submitted_data()) {
if ($form->is_cancelled()) {
$this->view_plugins_table();
return;
} else if ($form->is_submitted() && $data = $form->get_submitted_data()) {
if (!empty($data->id)) {
$step = step_manager::get_step_instance($data->id);
$step->instancename = $data->instancename;
......@@ -359,10 +428,15 @@ class workflow_settings {
}
step_manager::insert_or_update($step);
// Save local subplugin settings.
settings_manager::save_settings($step->id, $form->subpluginname, $data);
}
settings_manager::save_settings($step->id, SETTINGS_TYPE_STEP, $form->subpluginname, $data);
$this->view_plugins_table();
return;
} else {
$this->view_step_instance_form($form);
return;
}
}
$this->view_plugins_table();
}
}
\ No newline at end of file
......@@ -99,6 +99,10 @@ class form_step_instance extends \moodleform {
$mform->addElement('hidden', 'workflowid'); // Save the record's id.
$mform->setType('workflowid', PARAM_INT);
$mform->addElement('hidden', 'action'); // Save the current action.
$mform->setType('action', PARAM_TEXT);
$mform->setDefault('action', ACTION_STEP_INSTANCE_FORM);
$mform->addElement('header', 'general_settings_header', get_string('general_settings_header', 'tool_cleanupcourses'));
$elementname = 'instancename';
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Offers the possibility to add or modify a step instance.
*
* @package tool_cleanupcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_cleanupcourses\form;
use tool_cleanupcourses\entity\trigger_subplugin;
use tool_cleanupcourses\manager\lib_manager;
use tool_cleanupcourses\manager\trigger_manager;
use tool_cleanupcourses\trigger\base;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
/**
* Provides a form to modify a step instance
*/
class form_trigger_instance extends \moodleform {
/**
* @var trigger_subplugin
*/
public $trigger;
/**
* @var string
*/
public $subpluginname;
/**
* @var array/null local settings of the trigger instance
*/
public $settings;
/**
* @var base name of the subplugin to be created
*/
public $lib;
/**
* @var int id of the workflow
*/
private $workflowid;
/**
* Constructor
* @param \moodle_url $url.
* @param int $workflowid if of the workflow.
* @param trigger_subplugin $trigger step entity.
* @param string $subpluginname name of the trigger subplugin.
* @param array $settings settings of the step.
* @throws \moodle_exception if neither step nor subpluginname are set.
*/
public function __construct($url, $workflowid, $trigger = null, $subpluginname = null, $settings = null) {
$this->trigger = $trigger;
$this->workflowid = $workflowid;
$this->settings = $settings;
if ($trigger) {
$this->subpluginname = $trigger->subpluginname;
} else if ($subpluginname) {
$this->subpluginname = $subpluginname;
} else {
$triggertypes = trigger_manager::get_trigger_types();
$this->subpluginname = array_pop($triggertypes);
}
if ($this->subpluginname) {
$this->lib = lib_manager::get_trigger_lib($this->subpluginname);
}
parent::__construct($url);
}
/**
* Defines forms elements
*/
public function definition() {
$mform = $this->_form;
$mform->addElement('hidden', 'id'); // Save the record's id.
$mform->setType('id', PARAM_TEXT);
$mform->addElement('hidden', 'workflowid'); // Save the record's id.
$mform->setType('workflowid', PARAM_INT);
$mform->addElement('hidden', 'action'); // Save the current action.
$mform->setType('action', PARAM_TEXT);
$mform->setDefault('action', ACTION_TRIGGER_INSTANCE_FORM);
$mform->addElement('header', 'general_settings_header', get_string('general_settings_header', 'tool_cleanupcourses'));
$elementname = 'instancename';
$mform->addElement('text', $elementname, get_string('trigger_instancename', 'tool_cleanupcourses'));
$mform->setType($elementname, PARAM_TEXT);
$elementname = 'subpluginname';
$mform->addElement('select', $elementname,
get_string('trigger_subpluginname', 'tool_cleanupcourses'),
trigger_manager::get_trigger_types());
$mform->setType($elementname, PARAM_TEXT);
// Insert the subplugin specific settings.
if (isset($this->lib) && !empty($this->lib->instance_settings())) {
$mform->addElement('header', 'trigger_settings_header', get_string('trigger_settings_header', 'tool_cleanupcourses'));
$this->lib->extend_add_instance_form_definition($mform);
}
$mform->addElement('submit', 'reload', 'reload');
$mform->registerNoSubmitButton('reload');
$this->add_action_buttons();
}
/**
* Defines forms elements
*/
public function definition_after_data() {
$mform = $this->_form;
$mform->setDefault('workflowid', $this->workflowid);
if ($this->trigger) {
$mform->setDefault('id', $this->trigger->id);
$mform->setDefault('instancename', $this->trigger->instancename);
}
$mform->setDefault('subpluginname', $this->subpluginname);
// Setting the default values for the local trigger settings.
if ($this->settings) {
foreach ($this->settings as $key => $value) {
$mform->setDefault($key, $value);
}
}
// Insert the subplugin specific settings.
if (isset($this->lib) && !empty($this->lib->instance_settings())) {
$this->lib->extend_add_instance_form_definition_after_data($mform, $this->settings);
}
}
public function validation($data, $files) {
$error = parent::validation($data, $files);
if (empty($data['instancename'])) {
$error['instancename'] = get_string('required');
}
$required_settings = $this->lib->instance_settings();
foreach ($required_settings as $setting) {
if (!array_key_exists($setting->name, $data) || empty($data[$setting->name])) {
$error[$setting->name] = get_string('required');
}
}
return $error;
}
}
......@@ -184,13 +184,9 @@ class trigger_manager extends subplugin_manager {
public static function insert_or_update(trigger_subplugin &$subplugin) {
global $DB;
$transaction = $DB->start_delegated_transaction();
if ($subplugin->id !== null) {
if ($subplugin->id) {
$DB->update_record('tool_cleanupcourses_trigger', $subplugin);
}
$record = array(
'subpluginname' => $subplugin->subpluginname,
);
if (!$DB->record_exists('tool_cleanupcourses_trigger', $record)) {
} else {
$subplugin->id = $DB->insert_record('tool_cleanupcourses_trigger', $subplugin);
}
$transaction->allow_commit();
......@@ -279,4 +275,17 @@ class trigger_manager extends subplugin_manager {
}
}
/**
* Gets the list of step subplugins.
* @return array of step subplugins.
*/
public static function get_trigger_types() {
$subplugins = \core_component::get_plugin_list('cleanupcoursestrigger');
$result = array();
foreach ($subplugins as $id => $plugin) {
$result[$id] = get_string('pluginname', 'cleanupcoursestrigger_' . $id);
}
return $result;
}
}
......@@ -23,7 +23,10 @@
*/
namespace tool_cleanupcourses\table;
use tool_cleanupcourses\entity\trigger_subplugin;
use tool_cleanupcourses\manager\step_manager;
use tool_cleanupcourses\manager\trigger_manager;
use tool_cleanupcourses\manager\workflow_manager;
defined('MOODLE_INTERNAL') || die;
......@@ -37,19 +40,35 @@ class step_table extends \table_sql {
public function __construct($uniqueid, $workflowid) {
parent::__construct($uniqueid);
global $PAGE;
global $PAGE, $DB;
$this->workflowid = $workflowid;
list($sqlwhere, $params) = $DB->get_in_or_equal($workflowid);
$this->set_sql("id, subpluginname, instancename, sortindex",
'{tool_cleanupcourses_step}',
"workflowid = $workflowid");
"workflowid " . $sqlwhere, $params);
$this->define_baseurl($PAGE->url);
$this->pageable(false);
$this->init();
}
public function build_table() {
$trigger = trigger_manager::get_trigger_for_workflow($this->workflowid);
$this->format_and_add_array_of_rows(array(
array(
'id' => $trigger->id,
'type' => 'trigger',
'subpluginname' => $trigger->subpluginname,
'sortindex' => null,
'instancename' => 'Test',
)
), false);
return parent::build_table();
}
public function init() {
$this->define_columns(['instancename', 'subpluginname', 'sortindex', 'edit', 'delete']);
$this->define_columns(['type', 'instancename', 'subpluginname', 'sortindex', 'edit', 'delete']);
$this->define_headers([
get_string('step_type', 'tool_cleanupcourses'),
get_string('step_instancename', 'tool_cleanupcourses'),
get_string('step_subpluginname', 'tool_cleanupcourses'),
get_string('step_sortindex', 'tool_cleanupcourses'),
......@@ -60,6 +79,18 @@ class step_table extends \table_sql {
$this->setup();
}
/**
* Render the type column. This column displays Trigger or Step, depending of the type of the subplugin.
* @param $row
* @return string type of the subplugin
*/
public function col_type($row) {
if (empty($row->type)) {
return get_string('step', 'tool_cleanupcourses');
}
return get_string('trigger', 'tool_cleanupcourses');
}
/**
* Render subpluginname column.
* @param $row
......@@ -68,8 +99,11 @@ class step_table extends \table_sql {
public function col_subpluginname($row) {
$subpluginname = $row->subpluginname;
if (empty($row->type)) {
return get_string('pluginname', 'cleanupcoursesstep_' . $subpluginname);
} else {
return get_string('pluginname', 'cleanupcoursestrigger_' . $subpluginname);
}
}
/**
......@@ -111,7 +145,11 @@ class step_table extends \table_sql {
$alt = 'edit';
$icon = 't/edit';
if (empty($row->type)) {
$action = ACTION_STEP_INSTANCE_FORM;
} else {
$action = ACTION_TRIGGER_INSTANCE_FORM;
}
return $this->format_icon_link($action, $row->id, $icon, get_string($alt));
}
......@@ -122,13 +160,15 @@ class step_table extends \table_sql {
* @return string action button for deleting the subplugin
*/
public function col_delete($row) {
if (empty($row->type)) {
$alt = 'delete';
$icon = 't/delete';
$action = ACTION_STEP_INSTANCE_DELETE;
return $this->format_icon_link($action, $row->id, $icon, get_string($alt));
}
return '';
}
/**
* Util function for writing an action icon link
......
......@@ -36,15 +36,18 @@ $string['active_workflows_heading'] = 'Active Workflows';
$string['workflow_definition_heading'] = 'Workflow Definitions';
$string['subpluginssettings_edit_workflow_definition_heading'] = 'Workflow Definition';
$string['subpluginssettings_workflow_definition_steps_heading'] = 'Workflow Steps';
$string['subpluginssettings_edit_instance_heading'] = 'Step Instance';
$string['subpluginssettings_edit_trigger_instance_heading'] = 'Trigger for workflow \'{$a}\'';
$string['subpluginssettings_edit_step_instance_heading'] = 'Step Instance for workflow \'{$a}\'';
$string['add_new_step_instance'] = 'Add New Step Instance...';
$string['step_settings_header'] = 'Specific settings of the step type';
$string['trigger_settings_header'] = 'Specific settings of the trigger type';
$string['general_settings_header'] = 'General Settings';
$string['followedby_none'] = 'None';
$string['process_cleanup'] = 'Run the cleanup courses processes';
$string['trigger_subpluginname'] = 'Subplugin Name';
$string['trigger_instancename'] = 'Instance Name';
$string['trigger_enabled'] = 'Enabled';
$string['trigger_sortindex'] = 'Up/Down';
$string['trigger_workflow'] = 'Workflow';
......@@ -60,7 +63,8 @@ $string['editworkflow'] = 'Edit Title';
$string['deleteworkflow'] = 'Delete Workflow';
$string['activateworkflow'] = 'Activate';
$string['step_subpluginname'] = 'Step Type';
$string['step_type'] = 'Type';
$string['step_subpluginname'] = 'Subplugin Name';
$string['step_instancename'] = 'Instance Name';
$string['step_sortindex'] = 'Up/Down';
$string['step_edit'] = 'Edit';
......@@ -69,6 +73,8 @@ $string['step_delete'] = 'Delete';
$string['trigger'] = 'Trigger';
$string['step'] = 'Process step';
$string['workflow_trigger'] = 'Trigger for the workflow';
$string['cleanupcoursestrigger'] = 'Trigger';
$string['cleanupcoursesstep'] = 'Process step';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment