Skip to content

Commit

Permalink
manually corrects PHP code linter errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
stopfstedt committed Jun 4, 2024
1 parent 1efafa0 commit 36614be
Show file tree
Hide file tree
Showing 14 changed files with 493 additions and 235 deletions.
44 changes: 31 additions & 13 deletions classes/form/create_sync_job.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,57 @@
<?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/>.

/**
* Form for creating a new sync job.
*
* @package tool_ilioscategoryassignment
* @category form
* @package tool_ilioscategoryassignment
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_ilioscategoryassignment\form;

defined('MOODLE_INTERNAL') || die();

use core_course_category;
use Exception;
use moodle_page;
use moodleform;
use tool_ilioscategoryassignment\manager;
use tool_ilioscategoryassignment\output\renderer;

/* @global $CFG */
require_once($CFG->libdir . '/accesslib.php');
require_once($CFG->libdir . '/formslib.php');

/**
* Form for creating a new sync job.
* New sync job form class.
*
* @package tool_ilioscategoryassignment
* @category form
* @package tool_ilioscategoryassignment
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create_sync_job extends moodleform {

/**
* @inheritdoc
* Form definition.
*/
public function definition() {
/* @var \moodle_page $PAGE */
public function definition(): void {
/* @var moodle_page $PAGE The current page. */
global $PAGE;

/* @var renderer $renderer */
/* @var renderer $renderer The plugin renderer. */
$renderer = $PAGE->get_renderer('tool_ilioscategoryassignment');

$mform = $this->_form;
Expand Down Expand Up @@ -65,13 +82,14 @@ public function definition() {
if (!empty($iliosschools)) {
$iliosschools = array_column($iliosschools, 'title', 'id');
}
} catch (\Exception $e) {
} catch (Exception $e) {
$warning = $renderer->notify_error(get_string('ilioserror', 'tool_ilioscategoryassignment'));
$mform->addElement('html', $warning);
return;
}

$mform->addElement('text', 'title', get_string('title', 'tool_ilioscategoryassignment')); // Add elements to your form
// Add elements to your form.
$mform->addElement('text', 'title', get_string('title', 'tool_ilioscategoryassignment'));
$mform->setType('title', PARAM_NOTAGS);
$mform->addRule('title', null, 'required', null, 'client');
$mform->addRule('title', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
Expand Down
138 changes: 83 additions & 55 deletions classes/manager.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,70 @@
<?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/>.

/**
* Handles sync job, configuration and Ilios client management.
*
* @package tool_ilioscategoryassignment
* @package tool_ilioscategoryassignment
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_ilioscategoryassignment;

/* @global $CFG */
use coding_exception;
use core\event\course_category_deleted;
use core_course_category;
use curl;
use dml_exception;
use local_iliosapiclient\ilios_client;
use moodle_exception;
use stdClass;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/accesslib.php');

/**
* Handles sync job, configuration and Ilios client management.
* Handler class for sync job, configuration and Ilios client management.
*
* In other words, this is kitchen sink.
* In other words, this is the kitchen sink.
* This is obviously less than ideal, but still better than polluting the global namespace with functions in locallib.php.
* [ST 2017/07/24]
*
* @package tool_ilioscategoryassignment
* @package tool_ilioscategoryassignment
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manager {

const PLUGIN_NAME = 'tool_ilioscategoryassignment';

/**
* Instantiates and returns an Ilios API client.
*
* @return ilios_client
* @throws \moodle_exception
* @throws moodle_exception
*/
public static function instantiate_ilios_client(): ilios_client {
return new ilios_client(self::get_config('host_url', ''), new curl());
}

/**
* @param $id
* Retrieves a sync job by its given ID.
*
* @return sync_job|null
* @throws \dml_exception
* @param int $id The sync job ID.
* @return sync_job|null The sync job, or NULL if none were found.
* @throws dml_exception
*/
public static function get_sync_job($id) {
$jobs = self::get_sync_jobs(['id' => $id]);
Expand All @@ -53,26 +74,25 @@ public static function get_sync_job($id) {
/**
* Loads, caches and returns the configuration for this plugin.
*
* @return \stdClass
* @return stdClass The plugin configuration object.
* @see get_config()
* @throws \dml_exception
* @throws dml_exception
*/
public static function get_plugin_config() {
static $config = null;
if (!isset($config)) {
$config = get_config(self::PLUGIN_NAME);
$config = get_config('tool_ilioscategoryassignment');
}
return $config;
}

/**
* Returns a configuration value by its given name or a given default value.
*
* @param string $name
* @param string $default value if config does not exist yet
* Returns a configuration item by its given name or a given default value.
*
* @return string value or default
* @throws \dml_exception
* @param string $name The config item name.
* @param string $default A default value if the config item does not exist.
* @return mixed The config value or the given default value.
* @throws dml_exception
*/
public static function get_config($name, $default = null) {
$config = self::get_plugin_config();
Expand All @@ -82,28 +102,29 @@ public static function get_config($name, $default = null) {
/**
* Sets and stores a given config value.
*
* @param string $name name of config
* @param string $value string config value, null means delete
*
* @throws \dml_exception
* @param string $name The config item name.
* @param string $value string The config item's value, NULL means unset the config item.
* @return void
* @throws dml_exception
*/
public static function set_config($name, $value) {
public static function set_config($name, $value): void {
$config = self::get_plugin_config();
if ($value === null) {
unset($config->$name);
} else {
$config->$name = $value;
}
set_config($name, $value, self::PLUGIN_NAME);
set_config($name, $value, 'tool_ilioscategoryassignment');
}

/**
* @param array $filters
* Retrieves all sync jobs matching the given filters.
*
* @return sync_job[]
* @throws \dml_exception
* @param array $filters The filter criteria.
* @return array A list of sync jobs.
* @throws dml_exception
*/
public static function get_sync_jobs(array $filters = []) {
public static function get_sync_jobs(array $filters = []): array {
global $DB;
$jobs = [];
$jobrecs = $DB->get_records('tool_ilioscatassignment', $filters);
Expand All @@ -116,11 +137,13 @@ public static function get_sync_jobs(array $filters = []) {
}

/**
* @param int $job_id
* Disables a given sync job.
*
* @throws \dml_exception
* @param int $jobid The job ID.
* @return void
* @throws dml_exception
*/
public static function disable_job($jobid) {
public static function disable_job($jobid): void {
global $DB;
$tablename = 'tool_ilioscatassignment';
$job = $DB->get_record($tablename, ['id' => $jobid]);
Expand All @@ -131,11 +154,13 @@ public static function disable_job($jobid) {
}

/**
* @param int $job_id
* Enables a given sync job.
*
* @throws \dml_exception
* @param int $jobid The job ID.
* @return void
* @throws dml_exception
*/
public static function enable_job($jobid) {
public static function enable_job($jobid): void {
global $DB;
$tablename = 'tool_ilioscatassignment';
$job = $DB->get_record($tablename, ['id' => $jobid]);
Expand All @@ -146,14 +171,15 @@ public static function enable_job($jobid) {
}

/**
* @param sync_job $job
*
* @return sync_job
* @throws \dml_exception
* Creates and returns a new sync job.
* @param sync_job $job The new sync job to create.
* @return sync_job The created sync job.
* @throws dml_exception
*/
public static function create_job(sync_job $job) {
global $DB;
$dto = new \stdClass();

$dto = new stdClass();
$dto->title = $job->get_title();
$dto->roleid = $job->get_role_id();
$dto->coursecatid = $job->get_course_category_id();
Expand All @@ -165,23 +191,25 @@ public static function create_job(sync_job $job) {
}

/**
* @param int $job_id
* Deletes a given sync job.
*
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
* @param int $jobid The sync job ID.
* @return void
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public static function delete_job($jobid) {
public static function delete_job($jobid): void {
global $DB;
$job = self::get_sync_job($jobid);
if (empty($job)) {
return;
}

// delete the given job
// Delete the given job.
$DB->delete_records('tool_ilioscatassignment', ['id' => $jobid]);

// remove any course category role assignments that were managed by the given job
// Remove any course category role assignments that were managed by the given job.
$category = core_course_category::get($job->get_course_category_id(), IGNORE_MISSING);
if (empty($category)) {
return;
Expand All @@ -195,13 +223,13 @@ public static function delete_job($jobid) {
* Event observer for the "course category deleted" event.
* Removes any sync jobs and role assignments associated with that category.
*
* @param \core\event\course_category_deleted $event
*
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
* @param course_category_deleted $event
* @return void
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public static function course_category_deleted(course_category_deleted $event) {
public static function course_category_deleted(course_category_deleted $event): void {
$category = $event->get_coursecat();
$jobs = self::get_sync_jobs(['coursecatid' => $category->id]);
foreach ($jobs as $job) {
Expand Down
Loading

0 comments on commit 36614be

Please sign in to comment.