1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6
7class H5P_EditorTikiAjax implements H5PEditorAjaxInterface
8{
9
10	/**
11	 * Gets latest library versions that exists locally
12	 *
13	 * @return array Latest version of all local libraries
14	 */
15	public function getLatestLibraryVersions()
16	{
17		// Get latest version of local libraries
18		$major_versions_sql =
19			"SELECT hl.name,
20                MAX(hl.major_version) AS major_version
21           FROM tiki_h5p_libraries hl
22          WHERE hl.runnable = 1
23       GROUP BY hl.name";
24
25		$minor_versions_sql =
26			"SELECT hl2.name,
27                 hl2.major_version,
28                 MAX(hl2.minor_version) AS minor_version
29            FROM ({$major_versions_sql}) hl1
30            JOIN tiki_h5p_libraries hl2
31              ON hl1.name = hl2.name
32             AND hl1.major_version = hl2.major_version
33        GROUP BY hl2.name, hl2.major_version";
34
35		$results = TikiDb::get()->query(
36			"SELECT hl4.id,
37                hl4.name AS machine_name,
38                hl4.title,
39                hl4.major_version,
40                hl4.minor_version,
41                hl4.patch_version,
42                hl4.restricted,
43                hl4.has_icon
44           FROM ({$minor_versions_sql}) hl3
45           JOIN tiki_h5p_libraries hl4
46             ON hl3.name = hl4.name
47            AND hl3.major_version = hl4.major_version
48            AND hl3.minor_version = hl4.minor_version"
49		);
50
51		$out = [];
52
53		foreach ($results->result as $row) {
54			$out[] = json_decode(json_encode($row));	// convert to stdClass objects
55		}
56
57		return $out;
58	}
59
60	/**
61	 * Get locally stored Content Type Cache. If machine name is provided
62	 * it will only get the given content type from the cache
63	 *
64	 * @param $machineName
65	 *
66	 * @return array|object|null Returns results from querying the database
67	 */
68	public function getContentTypeCache($machineName = null)
69	{
70		$tiki_h5p_libraries_hub_cache = TikiDb::get()->table('tiki_h5p_libraries_hub_cache');
71
72		// Return info of only the content type with the given machine name
73		if ($machineName) {
74			$results = $tiki_h5p_libraries_hub_cache->fetchAll(
75				['id', 'is_recommended'],
76				['machine_name' => $machineName]
77			);
78		} else {
79			$results = $tiki_h5p_libraries_hub_cache->fetchAll(
80				$tiki_h5p_libraries_hub_cache->all()
81			);
82		}
83
84		foreach ($results as &$result) {
85			$result = json_decode(json_encode($result));	// convert to stdClass objects
86		}
87
88		return $results;
89	}
90
91	/**
92	 * Gets recently used libraries for the current author
93	 *
94	 * @return array machine names. The first element in the array is the
95	 * most recently used.
96	 */
97	public function getAuthorsRecentlyUsedLibraries()
98	{
99		// TODO (adapt for tiki action log
100
101		$recently_used = [];
102
103/*		$result = TikiDb::get()->query(
104			"SELECT library_name, max(created_at) AS max_created_at
105		 FROM tiki_h5p_events
106		WHERE type='content' AND sub_type = 'create' AND user_id = ?
107	 GROUP BY library_name
108	 ORDER BY max_created_at DESC",
109			get_current_user_id()
110		);
111
112		foreach ($result as $row) {
113			$recently_used[] = $row->library_name;
114		}
115*/
116
117		return $recently_used;
118	}
119
120	/**
121	 * Checks if the provided token is valid for this endpoint
122	 *
123	 * @param string $token
124	 *
125	 * @return bool True if successful validation
126	 */
127	public function validateEditorToken($token)
128	{
129		// TODO (with accesslib?)
130
131		return true;
132	}
133}
134