Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
/**
4
 * This is a data layer which uses the file system so it isn't specific to any framework.
5
 */
6
class H5PDevelopment {
7
 
8
  const MODE_NONE = 0;
9
  const MODE_CONTENT = 1;
10
  const MODE_LIBRARY = 2;
11
 
12
  private $h5pF, $libraries, $language, $filesPath;
13
 
14
  /**
15
   * Constructor.
16
   *
17
   * @param H5PFrameworkInterface|object $H5PFramework
18
   *  The frameworks implementation of the H5PFrameworkInterface
19
   * @param string $filesPath
20
   *  Path to where H5P should store its files
21
   * @param $language
22
   * @param array $libraries Optional cache input.
23
   */
24
  public function __construct(H5PFrameworkInterface $H5PFramework, $filesPath, $language, $libraries = NULL) {
25
    $this->h5pF = $H5PFramework;
26
    $this->language = $language;
27
    $this->filesPath = $filesPath;
28
    if ($libraries !== NULL) {
29
      $this->libraries = $libraries;
30
    }
31
    else {
32
      $this->findLibraries($filesPath . '/development');
33
    }
34
  }
35
 
36
  /**
37
   * Get contents of file.
38
   *
39
   * @param string $file File path.
40
   * @return mixed String on success or NULL on failure.
41
   */
42
  private function getFileContents($file) {
43
    if (file_exists($file) === FALSE) {
44
      return NULL;
45
    }
46
 
47
    $contents = file_get_contents($file);
48
    if ($contents === FALSE) {
49
      return NULL;
50
    }
51
 
52
    return $contents;
53
  }
54
 
55
  /**
56
   * Scans development directory and find all libraries.
57
   *
58
   * @param string $path Libraries development folder
59
   */
60
  private function findLibraries($path) {
61
    $this->libraries = array();
62
 
63
    if (is_dir($path) === FALSE) {
64
      return;
65
    }
66
 
67
    $contents = scandir($path);
68
 
69
    for ($i = 0, $s = count($contents); $i < $s; $i++) {
70
      if ($contents[$i][0] === '.') {
71
        continue; // Skip hidden stuff.
72
      }
73
 
74
      $libraryPath = $path . '/' . $contents[$i];
75
      $libraryJSON = $this->getFileContents($libraryPath . '/library.json');
76
      if ($libraryJSON === NULL) {
77
        continue; // No JSON file, skip.
78
      }
79
 
80
      $library = json_decode($libraryJSON, TRUE);
81
      if ($library === NULL) {
82
        continue; // Invalid JSON.
83
      }
84
 
85
      // TODO: Validate props? Not really needed, is it? this is a dev site.
86
 
87
      $library['libraryId'] = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']);
88
 
89
      // Convert metadataSettings values to boolean & json_encode it before saving
90
      $library['metadataSettings'] = isset($library['metadataSettings']) ?
91
        H5PMetadata::boolifyAndEncodeSettings($library['metadataSettings']) :
92
        NULL;
93
 
94
      // Save/update library.
95
      $this->h5pF->saveLibraryData($library, $library['libraryId'] === FALSE);
96
 
97
      // Need to decode it again, since it is served from here.
98
      $library['metadataSettings'] = isset($library['metadataSettings'])
99
        ? json_decode($library['metadataSettings'])
100
        : NULL;
101
 
102
      $library['path'] = 'development/' . $contents[$i];
103
      $this->libraries[H5PDevelopment::libraryToString($library['machineName'], $library['majorVersion'], $library['minorVersion'])] = $library;
104
    }
105
 
106
    // TODO: Should we remove libraries without files? Not really needed, but must be cleaned up some time, right?
107
 
108
    // Go trough libraries and insert dependencies. Missing deps. will just be ignored and not available. (I guess?!)
109
    $this->h5pF->lockDependencyStorage();
110
    foreach ($this->libraries as $library) {
111
      $this->h5pF->deleteLibraryDependencies($library['libraryId']);
112
      // This isn't optimal, but without it we would get duplicate warnings.
113
      // TODO: You might get PDOExceptions if two or more requests does this at the same time!!
114
      $types = array('preloaded', 'dynamic', 'editor');
115
      foreach ($types as $type) {
116
        if (isset($library[$type . 'Dependencies'])) {
117
          $this->h5pF->saveLibraryDependencies($library['libraryId'], $library[$type . 'Dependencies'], $type);
118
        }
119
      }
120
    }
121
    $this->h5pF->unlockDependencyStorage();
122
    // TODO: Deps must be inserted into h5p_nodes_libraries as well... ? But only if they are used?!
123
  }
124
 
125
  /**
126
   * @return array Libraries in development folder.
127
   */
128
  public function getLibraries() {
129
    return $this->libraries;
130
  }
131
 
132
  /**
133
   * Get library
134
   *
135
   * @param string $name of the library.
136
   * @param int $majorVersion of the library.
137
   * @param int $minorVersion of the library.
138
   * @return array library.
139
   */
140
  public function getLibrary($name, $majorVersion, $minorVersion) {
141
    $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion);
142
    return isset($this->libraries[$library]) === TRUE ? $this->libraries[$library] : NULL;
143
  }
144
 
145
  /**
146
   * Get semantics for the given library.
147
   *
148
   * @param string $name of the library.
149
   * @param int $majorVersion of the library.
150
   * @param int $minorVersion of the library.
151
   * @return string Semantics
152
   */
153
  public function getSemantics($name, $majorVersion, $minorVersion) {
154
    $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion);
155
    if (isset($this->libraries[$library]) === FALSE) {
156
      return NULL;
157
    }
158
    return $this->getFileContents($this->filesPath . $this->libraries[$library]['path'] . '/semantics.json');
159
  }
160
 
161
  /**
162
   * Get translations for the given library.
163
   *
164
   * @param string $name of the library.
165
   * @param int $majorVersion of the library.
166
   * @param int $minorVersion of the library.
167
   * @param $language
168
   * @return string Translation
169
   */
170
  public function getLanguage($name, $majorVersion, $minorVersion, $language) {
171
    $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion);
172
 
173
    if (isset($this->libraries[$library]) === FALSE) {
174
      return NULL;
175
    }
176
 
177
    return $this->getFileContents($this->filesPath . $this->libraries[$library]['path'] . '/language/' . $language . '.json');
178
  }
179
 
180
  /**
181
   * Writes library as string on the form "name majorVersion.minorVersion"
182
   *
183
   * @param string $name Machine readable library name
184
   * @param integer $majorVersion
185
   * @param $minorVersion
186
   * @return string Library identifier.
187
   */
188
  public static function libraryToString($name, $majorVersion, $minorVersion) {
189
    return $name . ' ' . $majorVersion . '.' . $minorVersion;
190
  }
191
}