Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
var H5PEditor = H5PEditor || {};
2
var H5PPresave = H5PPresave || {};
3
 
4
H5PEditor.Presave = (function (Editor) {
5
  "use strict";
6
 
7
  /**
8
   * Presave structure
9
   *
10
   * @class
11
   */
12
 
13
  function Presave() {
14
    this.maxScore = 0;
15
  }
16
 
17
  /**
18
   * Process the given library and calculate the max score
19
   *
20
   * @public
21
   * @param {string} library
22
   * @param {object} content
23
   * @returns {H5PEditor.Presave}
24
   */
25
  Presave.prototype.process = function (library, content) {
26
    var self = this;
27
 
28
    library = Presave.sanitizeLibrary(library);
29
    if (Presave.libraryExists(library) === true) {
30
      H5PPresave[library](content, function (serverSideData) {
31
        if (typeof serverSideData !== 'object') {
32
          return;
33
        }
34
        if (serverSideData.hasOwnProperty('maxScore') && Presave.isInt(serverSideData.maxScore)) {
35
          self.maxScore += serverSideData.maxScore;
36
        }
37
      });
38
    }
39
    return this;
40
  };
41
 
42
  /**
43
   * Check if the score is valid or throw exception if not
44
   *
45
   * @static
46
   * @param score
47
   * @returns {boolean}
48
   * @throws {Presave.exceptions.InvalidMaxScoreException} If score is not valid
49
   */
50
  Presave.validateScore = function (score) {
51
    if (!Presave.isInt(score) || score < 0) {
52
      throw new this.exceptions.InvalidMaxScoreException();
53
    }
54
    return true;
55
  };
56
 
57
  /**
58
   * Check if a object has the given properties.
59
   *
60
   * @static
61
   * @param {object} content
62
   * @param {string|[]} requirements
63
   * @returns {boolean}
64
   */
65
  Presave.checkNestedRequirements = function (content, requirements) {
66
    if (typeof content === 'undefined') {
67
      return false;
68
    }
69
    if (typeof requirements === 'string') {
70
      requirements = requirements.split('.');
71
    }
72
    for (var i = 1; i < requirements.length; i++) {
73
      if (!content.hasOwnProperty(requirements[i])) {
74
        return false;
75
      }
76
      content = content[requirements[i]];
77
    }
78
    return true;
79
  };
80
 
81
  /**
82
   * Check if value is a integer
83
   *
84
   * @static
85
   * @param {*} value
86
   * @returns {boolean}
87
   */
88
  Presave.isInt = function (value) {
89
    return !isNaN(value) && (function (x) {
90
      return (x | 0) === x;
91
    })(parseFloat(value));
92
  };
93
 
94
  /**
95
   * Checks if given library exists as a presave function
96
   *
97
   * @static
98
   * @param {string} library
99
   * @returns {boolean}
100
   */
101
  Presave.libraryExists = function (library) {
102
    return typeof H5PPresave[library] !== 'undefined';
103
  };
104
 
105
  /**
106
   * Remove potential version number from library
107
   *
108
   * @param {string} library
109
   * @returns {*}
110
   */
111
  Presave.sanitizeLibrary = function (library) {
112
    return Editor.libraryFromString(library).machineName || library;
113
  };
114
 
115
  /**
116
   * Collection of common exceptions related to the logic handled in this file
117
   *
118
   * @type {{InvalidMaxScoreException: H5PEditor.Presave.exceptions.InvalidMaxScoreException, InvalidContentSemanticsException: H5PEditor.Presave.exceptions.InvalidContentSemanticsException}}
119
   */
120
  Presave.exceptions = {
121
    InvalidMaxScoreException: function (message) {
122
      this.message = typeof message === 'string' ? message : Editor.t('core', 'errorCalculatingMaxScore');
123
      this.name = 'InvalidMaxScoreError';
124
      this.code = 'H5P-P400';
125
    },
126
    InvalidContentSemanticsException: function (name, message) {
127
      this.message = typeof message === 'string' ? message : Editor.t('core', 'semanticsError', {':error': Editor.t('core', 'maxScoreSemanticsMissing')});
128
      this.name = typeof name === 'string' ? name : 'Invalid Content Semantics Error';
129
      this.code = 'H5P-P500';
130
    }
131
  };
132
 
133
  /**
134
   * C
135
   * @constructor
136
   * @type {Presave}
137
   */
138
  Presave.prototype.constructor = Presave;
139
  return Presave;
140
})(H5PEditor);