-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from catalyst/issue8
- Loading branch information
Showing
7 changed files
with
923 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_dynamic_cohorts; | ||
|
||
/** | ||
* Abstract class that all conditions must extend. | ||
* | ||
* @package tool_dynamic_cohorts | ||
* @copyright 2024 Catalyst IT | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
abstract class condition_base { | ||
|
||
/** | ||
* Condition persistent object. | ||
* | ||
* @var condition $condition | ||
*/ | ||
protected $condition; | ||
|
||
/** | ||
* Protected constructor. | ||
*/ | ||
protected function __construct() { | ||
} | ||
|
||
/** | ||
* Return an instance of condition object. | ||
* | ||
* @param int $id | ||
* @param \stdClass|null $record | ||
* @return \tool_dynamic_cohorts\condition_base|null | ||
*/ | ||
final public static function get_instance(int $id = 0, ?\stdClass $record = null):? condition_base { | ||
$condition = new condition($id, $record); | ||
|
||
// In case we are getting the instance without underlying persistent data. | ||
if (!$classname = $condition->get('classname')) { | ||
$classname = get_called_class(); | ||
$condition->set('classname', $classname); | ||
} | ||
|
||
if (!class_exists($classname) || !is_subclass_of($classname, self::class)) { | ||
return null; | ||
} | ||
|
||
$instance = new $classname(); | ||
$instance->condition = $condition; | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* Gets required config data from submitted condition form data. | ||
* | ||
* @param \stdClass $formdata Form data generated via $mform->get_data() | ||
* @return array | ||
*/ | ||
public static function retrieve_config_data(\stdClass $formdata): array { | ||
$configdata = (array)$formdata; | ||
|
||
// Everything except these fields is considered as config data. | ||
unset($configdata['id']); | ||
unset($configdata['ruleid']); | ||
unset($configdata['position']); | ||
|
||
return $configdata; | ||
} | ||
|
||
/** | ||
* A config data for that condition. | ||
* | ||
* @return array | ||
*/ | ||
public function get_config_data(): array { | ||
return json_decode($this->condition->get('configdata'), true); | ||
} | ||
|
||
/** | ||
* Sets config data. | ||
* | ||
* @param array $configdata | ||
*/ | ||
public function set_config_data(array $configdata): void { | ||
$this->condition->set('configdata', json_encode($configdata)); | ||
} | ||
|
||
/** | ||
* Gets condition persistent record. | ||
* | ||
* @return condition | ||
*/ | ||
public function get_record(): condition { | ||
return $this->condition; | ||
} | ||
|
||
/** | ||
* Returns a rule record for the given condition. | ||
* | ||
* @return rule|null | ||
*/ | ||
public function get_rule(): ?rule { | ||
return rule::get_record(['id' => $this->get_record()->get('ruleid')]) ?: null; | ||
} | ||
|
||
/** | ||
* Gets a list of event classes the condition will be triggered on. | ||
* | ||
* @return array | ||
*/ | ||
public function get_events(): array { | ||
return []; | ||
} | ||
|
||
/** | ||
* Human readable description of the broken condition. | ||
* | ||
* @return string | ||
*/ | ||
public function get_broken_description(): string { | ||
return $this->get_record()->get('configdata'); | ||
} | ||
|
||
/** | ||
* Returns the name of the condition | ||
* | ||
* @return string | ||
*/ | ||
abstract public function get_name(): string; | ||
|
||
/** | ||
* Add condition config form elements. | ||
* | ||
* @param \MoodleQuickForm $mform The form to add elements to | ||
*/ | ||
abstract public function config_form_add(\MoodleQuickForm $mform): void; | ||
|
||
/** | ||
* Validates conditions form elements. | ||
* | ||
* @param array $data Form data. | ||
* @return array Errors array. | ||
*/ | ||
abstract public function config_form_validate(array $data): array; | ||
|
||
/** | ||
* Returns elements to extend SQL for filtering users. | ||
* @return condition_sql | ||
*/ | ||
abstract public function get_sql(): condition_sql; | ||
|
||
/** | ||
* Human-readable description of the configured condition. | ||
* | ||
* @return string | ||
*/ | ||
abstract public function get_config_description(): string; | ||
|
||
/** | ||
* Indicate that condition is broken. | ||
* | ||
* @return bool | ||
*/ | ||
abstract public function is_broken(): bool; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_dynamic_cohorts; | ||
|
||
use core_component; | ||
|
||
/** | ||
* Condition manager class. | ||
* | ||
* @package tool_dynamic_cohorts | ||
* @copyright 2024 Catalyst IT | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class condition_manager { | ||
|
||
/** | ||
* Get a list of all exising conditions. | ||
* | ||
* @param bool $excludebroken Do we need to exclude broken condition? | ||
* @return condition_base[] | ||
*/ | ||
public static function get_all_conditions(bool $excludebroken = true): array { | ||
$instances = []; | ||
$classes = core_component::get_component_classes_in_namespace(null, '\\local\\tool_dynamic_cohorts\\condition'); | ||
|
||
foreach (array_keys($classes) as $class) { | ||
$reflectionclass = new \ReflectionClass($class); | ||
if (!$reflectionclass->isAbstract()) { | ||
$instance = $class::get_instance(); | ||
|
||
if ($excludebroken && $instance->is_broken()) { | ||
continue; | ||
} | ||
|
||
$instances[$class] = $class::get_instance(); | ||
} | ||
} | ||
|
||
// Sort conditions by name. | ||
uasort($instances, function(condition_base $a, condition_base $b) { | ||
return ($a->get_name() <=> $b->get_name()); | ||
}); | ||
|
||
return $instances; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_dynamic_cohorts; | ||
|
||
/** | ||
* Condition SQl class for storing condition related SQl to filter out users. | ||
* | ||
* @see condition_base::get_sql | ||
* | ||
* @package tool_dynamic_cohorts | ||
* @copyright 2024 Catalyst IT | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class condition_sql { | ||
|
||
/** | ||
* Join string for SQL. | ||
* @var string | ||
*/ | ||
protected $join = ''; | ||
|
||
/** Where string for SQL. | ||
* @var string | ||
*/ | ||
protected $where = ''; | ||
|
||
/** | ||
* A list of params for SQL. | ||
* @var array | ||
*/ | ||
protected $params = []; | ||
|
||
/** | ||
* condition_sql constructor. | ||
* | ||
* @param string $join Join string for SQL. | ||
* @param string $where Where string for SQL. | ||
* @param array $params A list of params for SQL. | ||
*/ | ||
public function __construct(string $join, string $where, array $params) { | ||
$this->join = $join; | ||
$this->where = $where; | ||
$this->params = $params; | ||
} | ||
|
||
/** | ||
* Returns Join string for SQL. | ||
* @return string | ||
*/ | ||
public function get_join(): string { | ||
return $this->join; | ||
} | ||
|
||
/** | ||
* Returns Where string for SQL. | ||
* @return string | ||
*/ | ||
public function get_where(): string { | ||
return $this->where; | ||
} | ||
|
||
/** | ||
* Returns A list of params for SQL. | ||
* @return array | ||
*/ | ||
public function get_params(): array { | ||
return $this->params; | ||
} | ||
|
||
/** | ||
* Generate an alias for prepending parameters in SQL. | ||
* | ||
* @return string | ||
*/ | ||
public static function generate_param_alias(): string { | ||
static $cnt = 0; | ||
return 'tcmp' . ($cnt++); | ||
} | ||
|
||
/** | ||
* Generating an alias for tables in SQLs. | ||
* | ||
* @return string | ||
*/ | ||
public static function generate_table_alias(): string { | ||
static $cnt = 0; | ||
return 'tcmf' . ($cnt++); | ||
} | ||
} |
Oops, something went wrong.