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

Introducing process_manager and central call to process_course

parent 6365eea8
No related branches found
No related tags found
No related merge requests found
......@@ -24,8 +24,11 @@
namespace tool_cleanupcourses;
use tool_cleanupcourses\entity\trigger_subplugin;
use tool_cleanupcourses\manager\process_manager;
use tool_cleanupcourses\manager\step_manager;
use tool_cleanupcourses\manager\trigger_manager;
use tool_cleanupcourses\manager\lib_manager;
use tool_cleanupcourses\response\step_response;
use tool_cleanupcourses\response\trigger_response;
......@@ -56,7 +59,7 @@ class cleanup_processor {
break;
}
if ($response === trigger_response::trigger()) {
$this->trigger_course($course->id, trigger_subplugin::from_record($trigger));
process_manager::create_process($course->id, trigger_subplugin::from_record($trigger));
break;
}
}
......@@ -64,12 +67,35 @@ class cleanup_processor {
}
}
/**
* Calls the process_course() method of each step submodule currently responsible for a given course.
*/
public function process_courses() {
foreach (process_manager::get_processes() as $process) {
while(true) {
$step = step_manager::get_subplugin_by_instance_id($process->stepid);
$lib = lib_manager::get_step_lib($step->subpluginname);
$course = get_course($process->courseid);
$result = $lib->process_course($course);
if ($result === step_response::waiting()) {
break;
} elseif ($result === step_response::proceed()) {
process_manager::proceed_process($process);
} elseif ($result === step_response::rollback()){
// TODO: Implement Rollback!
break;
}
}
}
}
/**
* Returns a record set with all relevant courses.
* Relevant means that there is currently no cleanup process running for this course.
* @return \moodle_recordset with relevant courses.
*/
public function get_course_recordset() {
private function get_course_recordset() {
global $DB;
$sql = 'SELECT {course}.* from {course} '.
'left join {tool_cleanupcourses_process} '.
......@@ -78,19 +104,4 @@ class cleanup_processor {
return $DB->get_recordset_sql($sql);
}
/**
* Creates a process for the course which is at the respective step the trigger is followed by.
* @param int $courseid id of the course
* @param trigger_subplugin $trigger
*/
private function trigger_course($courseid, $trigger) {
global $DB;
if ($trigger->followedby !== null) {
$record = new \stdClass();
$record->courseid = $courseid;
$record->stepid = $trigger->followedby;
$DB->insert_record('tool_cleanupcourses_process', $record);
}
}
}
<?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/>.
namespace tool_cleanupcourses\entity;
defined('MOODLE_INTERNAL') || die();
/**
* Cleanup Course Process class
*
* @package tool_cleanupcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class process {
/** int id of the process*/
public $id;
/** int id of the step instance*/
public $stepid;
/** int id of the course*/
public $courseid;
public function __construct($id, $stepid, $courseid) {
$this->id = $id;
$this->stepid = $stepid;
$this->courseid = $courseid;
}
/**
* Creates a Cleanup Course Process from a db record.
* @param $record
* @return process
*/
public static function from_record($record) {
if (!object_property_exists($record, 'id')) {
return null;
}
if (!object_property_exists($record, 'stepid')) {
return null;
}
if (!object_property_exists($record, 'courseid')) {
return null;
}
$instance = new self($record->id, $record->stepid, $record->courseid);
return $instance;
}
}
\ No newline at end of file
<?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/>.
/**
* Manager for Cleanup Course Processes
*
* @package tool_cleanupcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_cleanupcourses\manager;
use tool_cleanupcourses\entity\process;
use tool_cleanupcourses\entity\trigger_subplugin;
defined('MOODLE_INTERNAL') || die();
class process_manager {
/**
* Creates a process for the course which is at the respective step the trigger is followed by.
* @param int $courseid id of the course
* @param trigger_subplugin $trigger
*/
public static function create_process($courseid, $trigger) {
global $DB;
if ($trigger->followedby !== null) {
$record = new \stdClass();
$record->courseid = $courseid;
$record->stepid = $trigger->followedby;
$DB->insert_record('tool_cleanupcourses_process', $record);
}
}
/**
* Creates a process for the course which is at the respective step the trigger is followed by.
* @return process[]
*/
public static function get_processes() {
global $DB;
$records = $DB->get_records('tool_cleanupcourses_process');
$processes = array();
foreach ($records as $record) {
$processes []= process::from_record($record);
}
return $processes;
}
/**
* Proceeds the process to the next step.
* @param process $process
*/
public static function proceed_process(&$process) {
global $DB;
$step = step_manager::get_step_instance($process->stepid);
if ($step->followedby !== null) {
$process->stepid = $step->followedby;
$DB->update_record('tool_cleanupcourses_process', $process);
} else {
if (get_course($process->courseid)) {
debugging('Course should no longer exist!!!!');
}
$DB->delete_records('tool_cleanupcourses_process', $process);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment