Skip to content

Commit

Permalink
MDL-72490 availability: Show error if conflict in date restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
Hien Dinh authored and sammarshallou committed Sep 26, 2022
1 parent 6a6344e commit b215412
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 6 deletions.
2 changes: 1 addition & 1 deletion availability/condition/date/classes/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class frontend extends \core_availability\frontend {

protected function get_javascript_strings() {
return array('ajaxerror', 'direction_before', 'direction_from', 'direction_until',
'direction_label');
'direction_label', 'error_dateconflict');
}

/**
Expand Down
1 change: 1 addition & 0 deletions availability/condition/date/lang/en/availability_date.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
$string['direction_label'] = 'Direction';
$string['direction_until'] = 'until';
$string['description'] = 'Prevent access until (or from) a specified date and time.';
$string['error_dateconflict'] = 'Conflicts with other date restrictions';
$string['full_from'] = 'It is after <strong>{$a}</strong>';
$string['full_from_date'] = 'It is on or after <strong>{$a}</strong>';
$string['full_until'] = 'It is before <strong>{$a}</strong>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,84 @@ M.availability_date.form.fillValue = function(value, node) {
value.t = parseInt(node.getData('time'), 10);
};

/**
* List out Date node value in an array node.
*
* This will go through all array node and list from earlier date node to current date node.
*
* @method convertTreeDateValue
* @param {array} tree Tree node to convert
* @param {array} arrayDateNode
* @param {array} currentNode current node.
*
* @return {array} arrayDateNode
*/
M.availability_date.form.convertTreeDateValue = function(tree, arrayDateNode, currentNode) {
var shouldSkip = false;
tree.forEach(function(node) {
if (shouldSkip) {
return;
}
if (node.type === 'date') {
// We go through all tree node, if we meet the current node then return.
if (node.t === parseInt(currentNode.getData('time'), 10)
&& currentNode.one('select[name=direction]').get('value') == node.d) {
shouldSkip = true;
return;
}
arrayDateNode.push(node);
} else if (node.type === undefined) {
M.availability_date.form.convertTreeDateValue(node.c, arrayDateNode, currentNode);
}
});
return arrayDateNode;
};

/**
* Check current node.
*
* This will check current date node with all date node in tree node.
*
* @method checkConditionDate
* @param {array} currentNode The curent node.
*
* @return {boolean} error Return true if the date is conflict.
*/
M.availability_date.form.checkConditionDate = function(currentNode) {
var error = false;
if (M.core_availability.form.rootList.getValue().op === '&') {
var jsValue = M.core_availability.form.rootList.getValue().c;
var arrayDateNode = M.availability_date.form.convertTreeDateValue(jsValue, [], currentNode);
var currentNodeDirection = currentNode.one('select[name=direction]').get('value');
var currentNodeTime = parseInt(currentNode.getData('time'), 10);
arrayDateNode.forEach(function(checkNode) {
// Validate if the date is conflict.
if (checkNode.d === '<') {
if (currentNodeDirection === '>=' && currentNodeTime >= checkNode.t) {
error = true;
}
} else {
if (currentNodeDirection === '<' && currentNodeTime <= checkNode.t) {
error = true;
}
}
return error;
});
return error;
} else {
if (currentNode.one('div > .badge-warning')) {
currentNode.one('div > .badge-warning').remove();
}
return error;
}
};

M.availability_date.form.fillErrors = function(errors, node) {
var error = M.availability_date.form.checkConditionDate(node);
if (error) {
errors.push('availability_date:error_dateconflict');
}
};


}, '@VERSION@', {"requires": ["base", "node", "event", "io", "moodle-core_availability-form"]});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,84 @@ M.availability_date.form.fillValue = function(value, node) {
value.t = parseInt(node.getData('time'), 10);
};

/**
* List out Date node value in an array node.
*
* This will go through all array node and list from earlier date node to current date node.
*
* @method convertTreeDateValue
* @param {array} tree Tree node to convert
* @param {array} arrayDateNode
* @param {array} currentNode current node.
*
* @return {array} arrayDateNode
*/
M.availability_date.form.convertTreeDateValue = function(tree, arrayDateNode, currentNode) {
var shouldSkip = false;
tree.forEach(function(node) {
if (shouldSkip) {
return;
}
if (node.type === 'date') {
// We go through all tree node, if we meet the current node then return.
if (node.t === parseInt(currentNode.getData('time'), 10)
&& currentNode.one('select[name=direction]').get('value') == node.d) {
shouldSkip = true;
return;
}
arrayDateNode.push(node);
} else if (node.type === undefined) {
M.availability_date.form.convertTreeDateValue(node.c, arrayDateNode, currentNode);
}
});
return arrayDateNode;
};

/**
* Check current node.
*
* This will check current date node with all date node in tree node.
*
* @method checkConditionDate
* @param {array} currentNode The curent node.
*
* @return {boolean} error Return true if the date is conflict.
*/
M.availability_date.form.checkConditionDate = function(currentNode) {
var error = false;
if (M.core_availability.form.rootList.getValue().op === '&') {
var jsValue = M.core_availability.form.rootList.getValue().c;
var arrayDateNode = M.availability_date.form.convertTreeDateValue(jsValue, [], currentNode);
var currentNodeDirection = currentNode.one('select[name=direction]').get('value');
var currentNodeTime = parseInt(currentNode.getData('time'), 10);
arrayDateNode.forEach(function(checkNode) {
// Validate if the date is conflict.
if (checkNode.d === '<') {
if (currentNodeDirection === '>=' && currentNodeTime >= checkNode.t) {
error = true;
}
} else {
if (currentNodeDirection === '<' && currentNodeTime <= checkNode.t) {
error = true;
}
}
return error;
});
return error;
} else {
if (currentNode.one('div > .badge-warning')) {
currentNode.one('div > .badge-warning').remove();
}
return error;
}
};

M.availability_date.form.fillErrors = function(errors, node) {
var error = M.availability_date.form.checkConditionDate(node);
if (error) {
errors.push('availability_date:error_dateconflict');
}
};


}, '@VERSION@', {"requires": ["base", "node", "event", "io", "moodle-core_availability-form"]});
79 changes: 79 additions & 0 deletions availability/condition/date/yui/src/form/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,82 @@ M.availability_date.form.fillValue = function(value, node) {
value.d = node.one('select[name=direction]').get('value');
value.t = parseInt(node.getData('time'), 10);
};

/**
* List out Date node value in an array node.
*
* This will go through all array node and list from earlier date node to current date node.
*
* @method convertTreeDateValue
* @param {array} tree Tree node to convert
* @param {array} arrayDateNode
* @param {array} currentNode current node.
*
* @return {array} arrayDateNode
*/
M.availability_date.form.convertTreeDateValue = function(tree, arrayDateNode, currentNode) {
var shouldSkip = false;
tree.forEach(function(node) {
if (shouldSkip) {
return;
}
if (node.type === 'date') {
// We go through all tree node, if we meet the current node then return.
if (node.t === parseInt(currentNode.getData('time'), 10)
&& currentNode.one('select[name=direction]').get('value') == node.d) {
shouldSkip = true;
return;
}
arrayDateNode.push(node);
} else if (node.type === undefined) {
M.availability_date.form.convertTreeDateValue(node.c, arrayDateNode, currentNode);
}
});
return arrayDateNode;
};

/**
* Check current node.
*
* This will check current date node with all date node in tree node.
*
* @method checkConditionDate
* @param {array} currentNode The curent node.
*
* @return {boolean} error Return true if the date is conflict.
*/
M.availability_date.form.checkConditionDate = function(currentNode) {
var error = false;
if (M.core_availability.form.rootList.getValue().op === '&') {
var jsValue = M.core_availability.form.rootList.getValue().c;
var arrayDateNode = M.availability_date.form.convertTreeDateValue(jsValue, [], currentNode);
var currentNodeDirection = currentNode.one('select[name=direction]').get('value');
var currentNodeTime = parseInt(currentNode.getData('time'), 10);
arrayDateNode.forEach(function(checkNode) {
// Validate if the date is conflict.
if (checkNode.d === '<') {
if (currentNodeDirection === '>=' && currentNodeTime >= checkNode.t) {
error = true;
}
} else {
if (currentNodeDirection === '<' && currentNodeTime <= checkNode.t) {
error = true;
}
}
return error;
});
return error;
} else {
if (currentNode.one('div > .badge-warning')) {
currentNode.one('div > .badge-warning').remove();
}
return error;
}
};

M.availability_date.form.fillErrors = function(errors, node) {
var error = M.availability_date.form.checkConditionDate(node);
if (error) {
errors.push('availability_date:error_dateconflict');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,21 @@ M.core_availability.Item.prototype.fillErrors = function(errors) {
// If any errors were added, add the marker to this item.
var errorLabel = this.node.one('> .badge-warning');
if (errors.length !== before && !errorLabel.get('firstChild')) {
errorLabel.appendChild(document.createTextNode(M.util.get_string('invalid', 'availability')));
var errorString = '';
// Fetch the last error code from the array of errors and split using the ':' delimiter.
var langString = errors[errors.length - 1].split(':');
var component = langString[0];
var identifier = langString[1];
// If get_string can't find the string, it will return the string in this format.
var undefinedString = '[[' + identifier + ',' + component + ']]';
// Get the lang string.
errorString = M.util.get_string(identifier, component);
if (errorString === undefinedString) {
// Use a generic invalid input message when the error lang string cannot be loaded.
errorString = M.util.get_string('invalid', 'availability');
}
// Show the error string.
errorLabel.appendChild(document.createTextNode(errorString));
} else if (errors.length === before && errorLabel.get('firstChild')) {
errorLabel.get('firstChild').remove();
}
Expand Down
Loading

0 comments on commit b215412

Please sign in to comment.