grade/grading/form/guide/amd/src/grades/grader/gradingpanel.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. * Grading panel for gradingform_guide.
  17. *
  18. * @module gradingform_guide/grades/grader/gradingpanel
  19. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {call as fetchMany} from 'core/ajax';
  23. import {normaliseResult} from 'core_grades/grades/grader/gradingpanel/normalise';
  24. import {compareData} from 'core_grades/grades/grader/gradingpanel/comparison';
  25. // Note: We use jQuery.serializer here until we can rewrite Ajax to use XHR.send()
  26. import jQuery from 'jquery';
  27. /**
  28. * For a given component, contextid, itemname & gradeduserid we can fetch the currently assigned grade.
  29. *
  30. * @param {String} component
  31. * @param {Number} contextid
  32. * @param {String} itemname
  33. * @param {Number} gradeduserid
  34. *
  35. * @returns {Promise}
  36. */
  37. export const fetchCurrentGrade = (component, contextid, itemname, gradeduserid) => {
  38. return fetchMany([{
  39. methodname: `gradingform_guide_grader_gradingpanel_fetch`,
  40. args: {
  41. component,
  42. contextid,
  43. itemname,
  44. gradeduserid,
  45. },
  46. }])[0];
  47. };
  48. /**
  49. * For a given component, contextid, itemname & gradeduserid we can store the currently assigned grade in a given form.
  50. *
  51. * @param {String} component
  52. * @param {Number} contextid
  53. * @param {String} itemname
  54. * @param {Number} gradeduserid
  55. * @param {Boolean} notifyUser
  56. * @param {HTMLElement} rootNode
  57. *
  58. * @returns {Promise}
  59. */
  60. export const storeCurrentGrade = async(component, contextid, itemname, gradeduserid, notifyUser, rootNode) => {
  61. const form = rootNode.querySelector('form');
  62. if (compareData(form) === true) {
  63. return normaliseResult(await fetchMany([{
  64. methodname: `gradingform_guide_grader_gradingpanel_store`,
  65. args: {
  66. component,
  67. contextid,
  68. itemname,
  69. gradeduserid,
  70. notifyuser: notifyUser,
  71. formdata: jQuery(form).serialize(),
  72. },
  73. }])[0]);
  74. } else {
  75. return '';
  76. }
  77. };