Update code style

This commit is contained in:
Luuk Verhoeven
2019-05-14 20:16:54 +02:00
parent d7039dac8c
commit 515a6954e5
4 changed files with 24 additions and 34 deletions

View File

@@ -42,7 +42,7 @@ class condition extends \core_availability\condition {
/** /**
* @var string * @var string
*/ */
protected $ip_addresses = ''; protected $ipaddresses = '';
/** /**
* condition constructor. * condition constructor.
@@ -53,11 +53,11 @@ class condition extends \core_availability\condition {
*/ */
public function __construct($structure) { public function __construct($structure) {
if (isset($structure->ip_addresses)) { if (isset($structure->ipaddresses)) {
// set a number // set a number
$this->ip_addresses = $structure->ip_addresses; $this->ipaddresses = $structure->ipaddresses;
} else { } else {
throw new \coding_exception('Missing or invalid ->ip_addresses for ipaddress condition'); throw new \coding_exception('Missing or invalid ->ipaddresses for ipaddress condition');
} }
} }
@@ -87,12 +87,12 @@ class condition extends \core_availability\condition {
*/ */
public function is_available($not, info $info, $grabthelot, $userid) { public function is_available($not, info $info, $grabthelot, $userid) {
if (empty($this->ip_addresses)) { if (empty($this->ipaddresses)) {
return true; return true;
} }
// Check if ip-address matches // Check if ip-address matches
if (address_in_subnet(getremoteaddr(), trim($this->ip_addresses))) { if (address_in_subnet(getremoteaddr(), trim($this->ipaddresses))) {
return true; return true;
} }
@@ -135,7 +135,7 @@ class condition extends \core_availability\condition {
* @return string Text representation of parameters * @return string Text representation of parameters
*/ */
protected function get_debug_string() { protected function get_debug_string() {
return !empty($this->ip_addresses) ? 'ip_addresses ON' : 'ip_addresses OFF'; return !empty($this->ipaddresses) ? 'ipaddresses ON' : 'ipaddresses OFF';
} }
/** /**
@@ -144,27 +144,27 @@ class condition extends \core_availability\condition {
* Intended for unit testing, as normally the JSON values are constructed * Intended for unit testing, as normally the JSON values are constructed
* by JavaScript code. * by JavaScript code.
* *
* @param string $ip_addresses * @param string $ipaddresses
* *
* @return \stdClass Object representing condition * @return \stdClass Object representing condition
*/ */
public static function get_json($ip_addresses) { public static function get_json($ipaddresses) {
return (object)[ return (object)[
'type' => 'ipaddress', 'type' => 'ipaddress',
'ip_addresses' => $ip_addresses, 'ipaddresses' => $ipaddresses,
]; ];
} }
/** /**
* Check if ip-address is valid * Check if ip-address is valid
* *
* @param $ip_addresses * @param $ipaddresses
* *
* @return bool * @return bool
*/ */
public static function is_valid_ip_addresses($ip_addresses) { public static function is_valid_ipaddresses($ipaddresses) {
$ip_addresses = implode(',', $ip_addresses); $ipaddresses = implode(',', $ipaddresses);
foreach ($ip_addresses as $ip_address) { foreach ($ipaddresses as $ip_address) {
if (!filter_var($ip_address, FILTER_VALIDATE_IP)) { if (!filter_var($ip_address, FILTER_VALIDATE_IP)) {
return false; return false;
} }
@@ -180,11 +180,9 @@ class condition extends \core_availability\condition {
* @return \stdClass Structure object (ready to be made into JSON format) * @return \stdClass Structure object (ready to be made into JSON format)
*/ */
public function save() { public function save() {
return (object)[ return (object)[
'type' => 'ipaddress', 'type' => 'ipaddress',
'ip_addresses' => $this->ip_addresses, 'ipaddresses' => $this->ipaddresses,
]; ];
} }
} }

View File

@@ -42,9 +42,6 @@ class frontend extends \core_availability\frontend {
protected function get_javascript_strings() { protected function get_javascript_strings() {
return [ return [
'js:ipaddress', 'js:ipaddress',
'js:turn-on-timestamps',
'js:turn-off-timestamps',
'js:enabled',
'error_ipaddress', 'error_ipaddress',
]; ];
} }

View File

@@ -30,11 +30,6 @@ $string['require_condition'] = 'Matching ip-address/subnet';
// Javascript strings. // Javascript strings.
$string['js:ipaddress'] = 'Require network address'; $string['js:ipaddress'] = 'Require network address';
$string['js:turn-on-timestamps'] = '';
$string['js:turn-off-timestamps'] = '';
$string['js:enabled'] = '';
// Errors. // Errors.
$string['error_ipaddress'] = 'Incorrect ip-address/subnet format'; $string['error_ipaddress'] = 'Incorrect ip-address/subnet format';
$string['requiresubnet_help'] = 'Access may be restricted to particular subnets on the LAN or Internet by specifying a comma-separated list of partial or full IP address numbers. This can be useful for an invigilated (proctored) quiz, to ensure that only people in a certain location can access the quiz.';

View File

@@ -81,19 +81,19 @@ M.availability_ipaddress.form.getNode = function(json) {
var html, node, root, id; var html, node, root, id;
// Make sure we work with unique id. // Make sure we work with unique id.
id = 'ip_addresses' + M.availability_ipaddress.form.instId; id = 'ipaddresses' + M.availability_ipaddress.form.instId;
M.availability_ipaddress.form.instId += 1; M.availability_ipaddress.form.instId += 1;
// Create HTML structure. // Create HTML structure.
html = ''; html = '';
html += '<span class="availability-group"><label for="' + id + '"><span class="p-r-1">' + html += '<span class="availability-group"><label for="' + id + '"><span class="p-r-1">' +
M.util.get_string('title', 'availability_ipaddress') + ' </span></label>'; M.util.get_string('title', 'availability_ipaddress') + ' </span></label>';
html += '<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ip_addresses" id="' + id + '">'; html += '<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="' + id + '">';
node = Y.Node.create('<span class="form-inline">' + html + '</span>'); node = Y.Node.create('<span class="form-inline">' + html + '</span>');
// Set initial values, if specified. // Set initial values, if specified.
if (json.ip_addresses !== undefined) { if (json.ipaddresses !== undefined) {
node.one('input[name=ip_addresses]').set('value', json.ip_addresses); node.one('input[name=ipaddresses]').set('value', json.ipaddresses);
} }
// Add event handlers (first time only). // Add event handlers (first time only).
@@ -103,7 +103,7 @@ M.availability_ipaddress.form.getNode = function(json) {
root.delegate('valuechange', function() { root.delegate('valuechange', function() {
// Trigger the updating of the hidden availability data whenever the ipaddress field changes. // Trigger the updating of the hidden availability data whenever the ipaddress field changes.
M.core_availability.form.update(); M.core_availability.form.update();
}, '.availability_ipaddress input[name=ip_addresses]'); }, '.availability_ipaddress input[name=ipaddresses]');
} }
return node; return node;
@@ -148,7 +148,7 @@ M.availability_ipaddress.form.fillValue = function(value, node) {
// to use within the JSON data in the form. Should be compatible // to use within the JSON data in the form. Should be compatible
// with the structure used in the __construct and save functions // with the structure used in the __construct and save functions
// within condition.php. // within condition.php.
value.ip_addresses = this.getValue('ip_addresses', node); value.ipaddresses = this.getValue('ipaddresses', node);
}; };
M.availability_ipaddress.form.fillErrors = function(errors, node) { M.availability_ipaddress.form.fillErrors = function(errors, node) {
@@ -157,8 +157,8 @@ M.availability_ipaddress.form.fillErrors = function(errors, node) {
this.fillValue(value, node); this.fillValue(value, node);
console.log('ip_address:', value); console.log('ip_address:', value);
// Basic ip_addresses checks. // Basic ipaddresses checks.
if (M.availability_ipaddress.validate_ipaddress(value.ip_addresses) === false) { if (M.availability_ipaddress.validate_ipaddress(value.ipaddresses) === false) {
errors.push('availability_ipaddress:error_ipaddress'); errors.push('availability_ipaddress:error_ipaddress');
} }
}; };