mod/workshop/amd/src/modform.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Additional javascript for the Workshop module form.
  17. *
  18. * @module mod_workshop/modform
  19. * @copyright The Open University 2018
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery'], function($) {
  23. var submissionTypes = {
  24. text: {
  25. available: null,
  26. required: null,
  27. requiredHidden: null
  28. },
  29. file: {
  30. available: null,
  31. required: null,
  32. requiredHidden: null
  33. }
  34. };
  35. /**
  36. * Determine whether one of the submission types has been marked as not available.
  37. *
  38. * If it has been marked not available, clear and disable its required checkbox. Then determine if the other submission
  39. * type is available, and if it is, check and disable its required checkbox.
  40. *
  41. * @param {Object} checkUnavailable
  42. * @param {Object} checkAvailable
  43. */
  44. function checkAvailability(checkUnavailable, checkAvailable) {
  45. if (!checkUnavailable.available.prop('checked')) {
  46. checkUnavailable.required.prop('disabled', true);
  47. checkUnavailable.required.prop('checked', false);
  48. if (checkAvailable.available.prop('checked')) {
  49. checkAvailable.required.prop('disabled', true);
  50. checkAvailable.required.prop('checked', true);
  51. // Also set the checkbox's hidden field to 1 so a 'required' value is submitted for the submission type.
  52. checkAvailable.requiredHidden.val(1);
  53. }
  54. }
  55. }
  56. /**
  57. * Enable the submission type's required checkbox and uncheck it.
  58. *
  59. * @param {Object} submissionType
  60. */
  61. function enableRequired(submissionType) {
  62. submissionType.required.prop('disabled', false);
  63. submissionType.required.prop('checked', false);
  64. submissionType.requiredHidden.val(0);
  65. }
  66. /**
  67. * Check which submission types have been marked as available, and disable required checkboxes as necessary.
  68. */
  69. function submissionTypeChanged() {
  70. checkAvailability(submissionTypes.file, submissionTypes.text);
  71. checkAvailability(submissionTypes.text, submissionTypes.file);
  72. if (submissionTypes.text.available.prop('checked') && submissionTypes.file.available.prop('checked')) {
  73. enableRequired(submissionTypes.text);
  74. enableRequired(submissionTypes.file);
  75. }
  76. }
  77. return /** @alias module:mod_workshop/modform */ {
  78. /**
  79. * Find all the required fields, set up event listeners, and set the initial state of required checkboxes.
  80. */
  81. init: function() {
  82. submissionTypes.text.available = $('#id_submissiontypetextavailable');
  83. submissionTypes.text.required = $('#id_submissiontypetextrequired');
  84. submissionTypes.text.requiredHidden = $('input[name="submissiontypetextrequired"][type="hidden"]');
  85. submissionTypes.file.available = $('#id_submissiontypefileavailable');
  86. submissionTypes.file.required = $('#id_submissiontypefilerequired');
  87. submissionTypes.file.requiredHidden = $('input[name="submissiontypefilerequired"][type="hidden"]');
  88. submissionTypes.text.available.on('change', submissionTypeChanged);
  89. submissionTypes.file.available.on('change', submissionTypeChanged);
  90. submissionTypeChanged();
  91. }
  92. };
  93. });