diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml new file mode 100644 index 0000000..b8476ae --- /dev/null +++ b/.github/workflows/moodle-ci.yml @@ -0,0 +1,134 @@ +name: Moodle Plugin CI + +on: + push: + branches: + - '*' + pull_request: + branches: + - '*' + schedule: + - cron: '0 8 * * 4' + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-22.04 + + services: + postgres: + image: postgres:14 + env: + POSTGRES_USER: 'postgres' + POSTGRES_HOST_AUTH_METHOD: 'trust' + ports: + - 5432:5432 + options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3 + mariadb: + image: mariadb:10 + env: + MYSQL_USER: 'root' + MYSQL_ALLOW_EMPTY_PASSWORD: "true" + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3 + + strategy: + fail-fast: false + matrix: + php: ['8.0', '8.1', '8.2', '8.3'] + moodle-branch: ['MOODLE_401_STABLE', 'MOODLE_402_STABLE', 'MOODLE_403_STABLE', 'MOODLE_404_STABLE', 'MOODLE_405_STABLE', 'main'] + database: [pgsql, mariadb] + include: + - {moodle-branch: 'MOODLE_401_STABLE', php: '7.4', database: 'pgsql'} + exclude: + - {moodle-branch: 'MOODLE_401_STABLE', php: '8.2', database: 'pgsql'} + - {moodle-branch: 'MOODLE_401_STABLE', php: '8.2', database: 'mariadb'} + - {moodle-branch: 'MOODLE_401_STABLE', php: '8.3', database: 'pgsql'} + - {moodle-branch: 'MOODLE_401_STABLE', php: '8.3', database: 'mariadb'} + - {moodle-branch: 'MOODLE_402_STABLE', php: '8.3', database: 'pgsql'} + - {moodle-branch: 'MOODLE_402_STABLE', php: '8.3', database: 'mariadb'} + - {moodle-branch: 'MOODLE_403_STABLE', php: '8.3', database: 'pgsql'} + - {moodle-branch: 'MOODLE_403_STABLE', php: '8.3', database: 'mariadb'} + - {moodle-branch: 'MOODLE_404_STABLE', php: '8.0', database: 'pgsql'} + - {moodle-branch: 'MOODLE_404_STABLE', php: '8.0', database: 'mariadb'} + - {moodle-branch: 'MOODLE_405_STABLE', php: '8.0', database: 'pgsql'} + - {moodle-branch: 'MOODLE_405_STABLE', php: '8.0', database: 'mariadb'} + - {moodle-branch: 'main', php: '8.0', database: 'pgsql'} + - {moodle-branch: 'main', php: '8.0', database: 'mariadb'} + - {moodle-branch: 'main', php: '8.1', database: 'pgsql'} + - {moodle-branch: 'main', php: '8.1', database: 'mariadb'} + + steps: + - name: Check out repository code + uses: actions/checkout@v3 + with: + path: plugin + + - name: Setup PHP ${{ matrix.php }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: ${{ matrix.extensions }} + ini-values: max_input_vars=5000 + coverage: none + + - name: Initialise moodle-plugin-ci + run: | + composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3 + echo $(cd ci/bin; pwd) >> $GITHUB_PATH + echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH + sudo locale-gen en_AU.UTF-8 + echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV + - name: Install moodle-plugin-ci + run: | + moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1 + env: + DB: ${{ matrix.database }} + MOODLE_BRANCH: ${{ matrix.moodle-branch }} + + - name: PHP Lint + if: ${{ always() }} + run: moodle-plugin-ci phplint + + - name: PHP Copy/Paste Detector + continue-on-error: true # This step will show errors but will not fail + if: ${{ always() }} + run: moodle-plugin-ci phpcpd + + - name: PHP Mess Detector + continue-on-error: true # This step will show errors but will not fail + if: ${{ always() }} + run: moodle-plugin-ci phpmd + + - name: Moodle Code Checker + if: ${{ always() }} + run: moodle-plugin-ci codechecker + + - name: Moodle PHPDoc Checker + if: ${{ always() }} + run: moodle-plugin-ci phpdoc + + - name: Validating + if: ${{ always() }} + run: moodle-plugin-ci validate + + - name: Check upgrade savepoints + if: ${{ always() }} + run: moodle-plugin-ci savepoints + + - name: Mustache Lint + if: ${{ always() }} + run: moodle-plugin-ci mustache + + - name: Grunt + if: ${{ always() }} + run: moodle-plugin-ci grunt --max-lint-warnings 0 + + - name: PHPUnit tests + if: ${{ always() }} + run: moodle-plugin-ci phpunit + + - name: Behat features + if: ${{ always() }} + run: moodle-plugin-ci behat --profile chrome% \ No newline at end of file diff --git a/classes/manage.php b/classes/manage.php new file mode 100644 index 0000000..36c00f7 --- /dev/null +++ b/classes/manage.php @@ -0,0 +1,65 @@ +. + +/** + * Manage enrol_manual instances. + * + * @package local_ws_enrol_manual + * @copyright 2024 Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_ws_enrol_manual; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/enrol/manual/lib.php'); + +/** + * Manage enrol_manual instances. + * + * @package local_ws_enrol_manual + * @copyright 2024 Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class manage { + /** + * Add an enrol_manual instance to a course. + * + * @param array $params the parameters for the instance + * + * @return int the id of the new instance + */ + public static function add_instance($params) { + global $DB; + + // Normalize. + $fields = $params['params']; + + // Verify the course. + $course = $DB->get_record('course', array('id'=>$fields['courseid']), '*', MUST_EXIST); + + $plugin = enrol_get_plugin('manual'); + + // Check for existing instance. + if ($DB->record_exists('enrol', ['courseid' => $course->id, 'enrol' => 'manual'])) { + throw new \invalid_parameter_exception('enrol_manual instance exists for course ID '. $course->id); + } + + $enrolid = $plugin->add_instance($course, $fields); + return $enrolid; + } +} \ No newline at end of file diff --git a/db/services.php b/db/services.php new file mode 100644 index 0000000..c3000ad --- /dev/null +++ b/db/services.php @@ -0,0 +1,36 @@ +. + +/** + * Web service definition for enrol_manual. + * + * @package local_ws_enrol_manual + * @copyright 2024 Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$functions = [ + 'local_ws_enrol_manual_add_instance' => [ + 'classname' => 'local_ws_enrol_manual_external', + 'methodname' => 'add_instance', + 'classpath' => 'local/ws_enrol_manual/externallib.php', + 'description' => 'Add manual enrolment instance', + 'type' => 'write', + 'capabilities' => 'enrol/manual:config' + ], +]; diff --git a/externallib.php b/externallib.php new file mode 100644 index 0000000..51065bc --- /dev/null +++ b/externallib.php @@ -0,0 +1,81 @@ +. + +/** + * External functions for managing enrol_manual. + * + * @package local_ws_enrol_manual + * @copyright 2024 Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +use core_external\external_api; +use core_external\external_function_parameters; +use core_external\external_single_structure; +use core_external\external_multiple_structure; +use core_external\external_value; + +/** + * External functions for managing enrol_manual. + * + * @package local_ws_enrol_manual + * @copyright 2024 Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class local_ws_enrol_manual_external extends external_api { + + /** + * Returns description of method parameters + * + * @return external_function_parameters + */ + public static function add_instance_parameters() { + return new external_function_parameters([ + 'params' => new external_single_structure([ + 'courseid' => new external_value(PARAM_INT, 'The course id', VALUE_REQUIRED), + 'status' => new external_value(PARAM_INT, 'Enable manual enrolments', VALUE_DEFAULT, 0), + 'roleid' => new external_value(PARAM_INT, 'Default role', VALUE_REQUIRED), + 'enrolperiod' => new external_value(PARAM_INT, 'Default enrolment duration', VALUE_DEFAULT, 0), + 'expirynotify' => new external_value(PARAM_INT, 'Notify before enrolment expires', VALUE_DEFAULT, 0), + 'expirythreshold' => new external_value(PARAM_INT, 'Notification threshold', VALUE_DEFAULT, 86400), + 'customint1' => new external_value(PARAM_INT, 'Send course welcome message', VALUE_DEFAULT, 1), + 'customtext1' => new external_value(PARAM_RAW, 'Custom welcome message', VALUE_DEFAULT, null), + ]) + ]); + } + + /** + * Add a manual enrol instance to a course. + * + * @param array $params the parameters. + */ + public static function add_instance($params) { + $params = self::validate_parameters(self::add_instance_parameters(), ['params' => $params]); + return [ + 'id' => local_ws_enrol_manual\manage::add_instance($params), + ]; + } + + public static function add_instance_returns() { + return new external_single_structure( + [ + 'id' => new external_value(PARAM_INT, 'id of the created instance') + ] + ); + } +} diff --git a/lang/en/local_ws_enrol_manual.php b/lang/en/local_ws_enrol_manual.php new file mode 100644 index 0000000..b5d16dd --- /dev/null +++ b/lang/en/local_ws_enrol_manual.php @@ -0,0 +1,25 @@ +. + +/** + * Strings for component 'local_ws_enrol_manual', language 'en', branch 'main' + * + * @package local_ws_enrol_manual + * @copyright 2024 onwards Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['pluginname'] = 'Manual enrolment web service function'; diff --git a/version.php b/version.php new file mode 100644 index 0000000..fce4689 --- /dev/null +++ b/version.php @@ -0,0 +1,29 @@ +. + +/** + * Version information for local_ws_enrol_manual. + * + * @package local_ws_enrol_manual + * @copyright 2024 onwards Lafayette College ITS + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2024120601; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2023100400; // Requires this Moodle version. +$plugin->component = 'local_ws_enrol_manual'; // Full name of the plugin (used for diagnostics). \ No newline at end of file