Skip to content

Commit

Permalink
EVOSTDM-3175 MDL 4.3 - Valider nos plugins - Partie 3 - Divers (parta…
Browse files Browse the repository at this point in the history
…gés)
  • Loading branch information
binhdv1983 committed Jan 25, 2024
1 parent 1fed90b commit 71313b5
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 219 deletions.
38 changes: 19 additions & 19 deletions classes/cohortmembersync.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@
class cohortmembersync {

/** @var array errors when prcessing file or updating cohorts */
protected $errors = array();
protected $errors = [];

/** @var string the file name of the file */
protected $filename = '';

/** @var array updating cohorts options */
protected $params = array();
protected $params = [];

/** @var array warnings if cohort or user are not found */
protected $warnings = array();
protected $warnings = [];

/** @var array informations about members added or deleted */
protected $infos = array('usersadded' => array(), 'usersdeleted' => array());
protected $infos = ['usersadded' => [], 'usersdeleted' => []];

/** @var progress_trace trace */
protected $trace = null;
Expand All @@ -67,7 +67,7 @@ class cohortmembersync {
* @param string $filepath the file path of the cohorts members file
* @param array $params Options for processing file
*/
public function __construct($trace, $filepath, $params = array()) {
public function __construct($trace, $filepath, $params = []) {

$this->trace = $trace;
if (!empty($filepath)) {
Expand All @@ -90,12 +90,12 @@ public function __construct($trace, $filepath, $params = array()) {
$this->errors[] = "Unknown delimiter : " . $this->params['flatfiledelimiter'];
}
// Validate useridentifier.
if (!in_array($this->params['useridentifier'], array('id', 'idnumber', 'username'))) {
if (!in_array($this->params['useridentifier'], ['id', 'idnumber', 'username'])) {
$this->errors[] = "Unknown user identifier : " . $this->params['useridentifier'];
}

// Validate cohortidentifier.
if (!in_array($this->params['cohortidentifier'], array('name', 'idnumber', 'id'))) {
if (!in_array($this->params['cohortidentifier'], ['name', 'idnumber', 'id'])) {
$this->errors[] = "Unknown cohort identifier : " . $this->params['cohortidentifier'];
}
}
Expand Down Expand Up @@ -137,12 +137,12 @@ public function update_cohortsmembers() {
* @return array params list
*/
protected function get_defaults_params() {
return array(
return [
'useridentifier' => get_config('tool_cohortsync', 'useridentifier'),
'cohortidentifier' => get_config('tool_cohortsync', 'cohortidentifier'),
'flatfiledelimiter' => get_config('tool_cohortsync', 'flatfiledelimiter'),
'flatfileencoding' => get_config('tool_cohortsync', 'flatfileencoding')
);
'flatfileencoding' => get_config('tool_cohortsync', 'flatfileencoding'),
];
}

/**
Expand Down Expand Up @@ -174,15 +174,15 @@ protected function process_file() {
return;
}

$cachedusers = array();
$cachedcohorts = array();
$cachedusers = [];
$cachedcohorts = [];

// We may need more memory here.
\core_php_time_limit::raise();
\raise_memory_limit(MEMORY_HUGE);

$this->trace->output("Processing flat file cohort members");
$data = array();
$data = [];

$content = file_get_contents($this->filename);
$delimiters = \csv_import_reader::get_delimiter_list();
Expand Down Expand Up @@ -238,10 +238,10 @@ protected function process_file() {

$select = $DB->sql_equal($this->params['useridentifier'], ':useridentifier', false)
. ' AND deleted = :deleted';
$params = array(
$params = [
'useridentifier' => $fields[2],
'deleted' => 0
);
'deleted' => 0,
];
$user = $DB->get_record_select('user', $select, $params);

if ($user) {
Expand All @@ -265,9 +265,9 @@ protected function process_file() {
$cachedcohorts[$fields[1]] = 0;

$select = $DB->sql_equal($this->params['cohortidentifier'], ':cohortidentifier', false);
$params = array(
'cohortidentifier' => $fields[1]
);
$params = [
'cohortidentifier' => $fields[1],
];
$cohort = $DB->get_record_select('cohort', $select, $params);

if ($cohort) {
Expand Down
42 changes: 21 additions & 21 deletions classes/cohortsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
class cohortsync {

/** @var array errors when prcessing csv file or updating cohorts */
protected $errors = array();
protected $errors = [];

/** @var string the file name of the csv file */
protected $filename = '';

/** @var array updating cohorts options */
protected $params = array();
protected $params = [];

/** @var array cached list of available contexts */
protected $contextlist = null;
Expand All @@ -59,10 +59,10 @@ class cohortsync {
protected $defaultcontext = null;

/** @var array warnings if cohort or user are not found */
protected $warnings = array();
protected $warnings = [];

/** @var array list of cohort data */
protected $cohorts = array();
protected $cohorts = [];

/** @var progress_trace trace */
protected $trace = null;
Expand All @@ -74,7 +74,7 @@ class cohortsync {
* @param string $filepath the file path of the csv cohorts file
* @param array $params Options for processing csv file
*/
public function __construct($trace, $filepath, $params = array()) {
public function __construct($trace, $filepath, $params = []) {

$this->trace = $trace;
if (!empty($filepath)) {
Expand Down Expand Up @@ -133,11 +133,11 @@ public function update_cohorts() {
* @return array params list
*/
protected function get_defaults_params() {
return array(
return [
'defaultcontext' => get_config('tool_cohortsync', 'defaultcontext'),
'csvdelimiter' => get_config('tool_cohortsync', 'csvdelimiter'),
'csvencoding' => get_config('tool_cohortsync', 'encoding')
);
'csvencoding' => get_config('tool_cohortsync', 'encoding'),
];
}

/**
Expand Down Expand Up @@ -166,7 +166,7 @@ public function get_warnings() {
protected function get_context_options() {

if ($this->contextlist === null) {
$this->contextlist = array();
$this->contextlist = [];
$displaylist = \core_course_category::make_categories_list('moodle/cohort:manage');
// We need to index the options array by context id instead of category id and add option for system context.
$syscontext = \context_system::instance();
Expand Down Expand Up @@ -195,7 +195,7 @@ protected function process_file() {
return;
}

$cohorts = array();
$cohorts = [];

$uploadid = \csv_import_reader::get_new_iid('cohortsync');
$cir = new \csv_import_reader($uploadid, 'cohortsync');
Expand All @@ -208,11 +208,11 @@ protected function process_file() {
$columns = $cir->get_columns();

// Check that columns include 'name' and warn about extra columns.
$allowedcolumns = array('contextid', 'name', 'idnumber', 'description', 'descriptionformat', 'visible');
$additionalcolumns = array('context', 'category', 'category_id', 'category_idnumber', 'category_path');
$displaycolumns = array();
$extracolumns = array();
$columnsmapping = array();
$allowedcolumns = ['contextid', 'name', 'idnumber', 'description', 'descriptionformat', 'visible'];
$additionalcolumns = ['context', 'category', 'category_id', 'category_idnumber', 'category_path'];
$displaycolumns = [];
$extracolumns = [];
$columnsmapping = [];
foreach ($columns as $i => $columnname) {
$columnnamelower = preg_replace('/ /', '', \core_text::strtolower($columnname));
$columnsmapping[$i] = null;
Expand Down Expand Up @@ -241,11 +241,11 @@ protected function process_file() {
// Parse data rows.
$cir->init();
$rownum = 0;
$idnumbers = array();
$idnumbers = [];
while ($row = $cir->next()) {
$rownum++;
$cohorts[$rownum] = array();
$hash = array();
$cohorts[$rownum] = [];
$hash = [];
foreach ($row as $i => $value) {
if ($columnsmapping[$i]) {
$hash[$columnsmapping[$i]] = $value;
Expand All @@ -257,7 +257,7 @@ protected function process_file() {
$this->warnings = array_merge($this->warnings, $warnings);

if (!empty($hash['idnumber'])) {
if (isset($idnumbers[$hash['idnumber']]) || $DB->record_exists('cohort', array('idnumber' => $hash['idnumber']))) {
if (isset($idnumbers[$hash['idnumber']]) || $DB->record_exists('cohort', ['idnumber' => $hash['idnumber']])) {
$this->errors[] = new \lang_string('duplicateidnumber', 'cohort');
}
$idnumbers[$hash['idnumber']] = true;
Expand Down Expand Up @@ -321,7 +321,7 @@ protected function clean_cohort_data(&$hash) {
protected function resolve_context(&$hash) {
global $DB;

$warnings = array();
$warnings = [];

if (!empty($hash['contextid'])) {
// Contextid was specified, verify we can post there.
Expand Down Expand Up @@ -388,7 +388,7 @@ protected function resolve_context(&$hash) {
if (empty($this->categoriescache[$field][$value])) {
$record = $DB->get_record_sql("SELECT c.id, ctx.id contextid
FROM {context} ctx JOIN {course_categories} c ON ctx.contextlevel = ? AND ctx.instanceid = c.id
WHERE c.$field = ?", array(CONTEXT_COURSECAT, $value));
WHERE c.$field = ?", [CONTEXT_COURSECAT, $value]);
if ($record && ($contextoptions = $this->get_context_options()) && isset($contextoptions[$record->contextid])) {
$contextid = $record->contextid;
} else {
Expand Down
14 changes: 7 additions & 7 deletions cli/cohortmembersync.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@

// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
[
'help' => false,
'filepath' => false,
'flatfiledelimiter' => false,
'flatfileencoding' => false,
'cohortidentifier' => false,
'useridentifier' => false,
'verbose' => false),
array(
'verbose' => false],
[
'h' => 'help',
'f' => 'filepath',
'd' => 'flatfiledelimiter',
'e' => 'flatfileencoding',
'c' => 'cohortidentifier',
'u' => 'useridentifier',
'v' => 'verbose')
'v' => 'verbose']
);

if ($options['help']) {
Expand Down Expand Up @@ -83,15 +83,15 @@
die;
}

$params = array(
$params = [
'help',
'verbose',
'filepath',
'flatfiledelimiter',
'flatfileencoding',
'cohortidentifier',
'useridentifier'
);
'useridentifier',
];

foreach ($params as $param) {
if ($options[$param] === false) {
Expand Down
16 changes: 8 additions & 8 deletions cli/cohortsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@

// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
[
'help' => false,
'filepath' => false,
'csvdelimiter' => false,
'csvencoding' => false,
'context' => false,
'verbose' => false),
array(
'verbose' => false],
[
'h' => 'help',
'f' => 'filepath',
'd' => 'csvdelimiter',
'e' => 'csvencoding',
'ctx' => 'context',
'v' => 'verbose')
'v' => 'verbose']
);

if ($options['help']) {
Expand Down Expand Up @@ -76,21 +76,21 @@
die;
}

$params = array(
$params = [
'help',
'verbose',
'filepath',
'csvdelimiter',
'csvencoding',
'context'
);
'context',
];
foreach ($params as $param) {
if ($options[$param] === false) {
unset($options[$param]);
}
}
// Emulate normal session.
cron_setup_user();
\core\cron::setup_user();

if (empty($options['verbose'])) {
$trace = new \null_progress_trace();
Expand Down
14 changes: 7 additions & 7 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
get_string('encoding', 'tool_cohortsync'), '', 'UTF-8', $choices));

// GEt context for cohorts.
$contextoptions = array();
$contextoptions = [];
$displaylist = core_course_category::make_categories_list('moodle/cohort:manage');
// We need to index the options array by context id instead of category id and add option for system context.
$syscontext = context_system::instance();
Expand All @@ -67,23 +67,23 @@
$settings->add(new admin_setting_heading('cohortmembersyncheader',
get_string('cohortmembersyncheader', 'tool_cohortsync'), ''));

$useridentifieroptions = array(
$useridentifieroptions = [
'id' => 'id',
'username' => 'username',
'idnumber' => 'idnumber'
);
'idnumber' => 'idnumber',
];

$settings->add(new admin_setting_configselect('tool_cohortsync/useridentifier',
get_string('useridentifier', 'tool_cohortsync'),
get_string('useridentifierdesc', 'tool_cohortsync'),
'username',
$useridentifieroptions));

$cohortidentifieroptions = array(
$cohortidentifieroptions = [
'id' => 'id',
'name' => 'name',
'idnumber' => 'idnumber'
);
'idnumber' => 'idnumber',
];

$settings->add(new admin_setting_configselect('tool_cohortsync/cohortidentifier',
new lang_string('cohortidentifier', 'tool_cohortsync'),
Expand Down
Loading

0 comments on commit 71313b5

Please sign in to comment.