Skip to content

Commit

Permalink
MDL-77130 cohort: add custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Apr 4, 2023
1 parent f7a8df2 commit c11d428
Show file tree
Hide file tree
Showing 16 changed files with 1,008 additions and 35 deletions.
10 changes: 9 additions & 1 deletion admin/settings/users.php
Expand Up @@ -101,7 +101,15 @@

$ADMIN->add('accounts', new admin_externalpage('profilefields', new lang_string('profilefields','admin'), "$CFG->wwwroot/user/profile/index.php", 'moodle/site:config'));
$ADMIN->add('accounts', new admin_externalpage('cohorts', new lang_string('cohorts', 'cohort'), $CFG->wwwroot . '/cohort/index.php', array('moodle/cohort:manage', 'moodle/cohort:view')));

$ADMIN->add(
'accounts',
new admin_externalpage(
'cohort_customfield',
new lang_string('cohort_customfield', 'admin'),
$CFG->wwwroot . '/cohort/customfield.php',
['moodle/cohort:configurecustomfields']
)
);

// Stuff under the "roles" subcategory.

Expand Down
124 changes: 124 additions & 0 deletions cohort/classes/customfield/cohort_handler.php
@@ -0,0 +1,124 @@
<?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 core_cohort\customfield;

use core_customfield\handler;
use core_customfield\field_controller;

/**
* Cohort handler for custom fields.
*
* @package core_cohort
* @copyright 2023 Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_handler extends handler {

/**
* @var cohort_handler
*/
static protected $singleton;

/**
* Returns a singleton.
*
* @param int $itemid
* @return \core_customfield\handler
*/
public static function create(int $itemid = 0): handler {
if (static::$singleton === null) {
self::$singleton = new static(0);
}
return self::$singleton;
}

/**
* Run reset code after unit tests to reset the singleton usage.
*/
public static function reset_caches(): void {
if (!PHPUNIT_TEST) {
throw new \coding_exception('This feature is only intended for use in unit tests');
}

static::$singleton = null;
}

/**
* The current user can configure custom fields on this component.
*
* @return bool true if the current can configure custom fields, false otherwise
*/
public function can_configure(): bool {
return has_capability('moodle/cohort:configurecustomfields', $this->get_configuration_context());
}

/**
* The current user can edit custom fields on the given cohort.
*
* @param field_controller $field
* @param int $instanceid id of the cohort to test edit permission
* @return bool true if the current can edit custom field, false otherwise
*/
public function can_edit(field_controller $field, int $instanceid = 0): bool {
return has_capability('moodle/cohort:manage', $this->get_instance_context($instanceid));
}

/**
* The current user can view custom fields on the given cohort.
*
* @param field_controller $field
* @param int $instanceid id of the cohort to test edit permission
* @return bool true if the current can view custom field, false otherwise
*/
public function can_view(field_controller $field, int $instanceid): bool {
return has_any_capability(['moodle/cohort:manage', 'moodle/cohort:view'], $this->get_instance_context($instanceid));
}

/**
* Context that should be used for new categories created by this handler.
*
* @return \context the context for configuration
*/
public function get_configuration_context(): \context {
return \context_system::instance();
}

/**
* URL for configuration of the fields on this handler.
*
* @return \moodle_url The URL to configure custom fields for this component
*/
public function get_configuration_url(): \moodle_url {
return new \moodle_url('/cohort/customfield.php');
}

/**
* Returns the context for the data associated with the given instanceid.
*
* @param int $instanceid id of the record to get the context for
* @return \context the context for the given record
*/
public function get_instance_context(int $instanceid = 0): \context {
global $DB;
if ($instanceid > 0) {
$cohort = $DB->get_record('cohort', ['id' => $instanceid], '*', MUST_EXIST);
return \context::instance_by_id($cohort->contextid, MUST_EXIST);
} else {
return \context_system::instance();
}
}
}
40 changes: 40 additions & 0 deletions cohort/customfield.php
@@ -0,0 +1,40 @@
<?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/>.

/**
* Manage cohort custom fields
*
* @package core_cohort
* @copyright 2023 Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use core_cohort\customfield\cohort_handler;
use core_customfield\output\management;

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

admin_externalpage_setup('cohort_customfield');

$output = $PAGE->get_renderer('core_customfield');
$handler = cohort_handler::create();
$outputpage = new management($handler);

echo $output->header(),
$output->heading(new lang_string('cohort_customfield', 'admin')),
$output->render($outputpage),
$output->footer();
16 changes: 16 additions & 0 deletions cohort/edit_form.php
Expand Up @@ -69,8 +69,12 @@ public function definition() {
$mform->setType('returnurl', PARAM_LOCALURL);
}

$handler = core_cohort\customfield\cohort_handler::create();
$handler->instance_form_definition($mform, empty($cohort->id) ? 0 : $cohort->id);

$this->add_action_buttons();

$handler->instance_form_before_set_data($cohort);
$this->set_data($cohort);
}

Expand All @@ -97,6 +101,9 @@ public function validation($data, $files) {
}
}

$handler = core_cohort\customfield\cohort_handler::create();
$errors = array_merge($errors, $handler->instance_form_validation($data, $files));

return $errors;
}

Expand All @@ -118,5 +125,14 @@ protected function get_category_options($currentcontextid) {
}
return $options;
}

/**
* Apply a logic after data is set.
*/
public function definition_after_data() {
$cohortid = $this->_form->getElementValue('id');
$handler = core_cohort\customfield\cohort_handler::create();
$handler->instance_form_definition_after_data($this->_form, empty($cohortid) ? 0 : $cohortid);
}
}

0 comments on commit c11d428

Please sign in to comment.