weekly release 3.1dev
[moodle.git] / mod / lti / service / toolsettings / classes / local / service / toolsettings.php
CommitLineData
e3f69b58 1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains a class definition for the Tool Settings service
19 *
20 * @package ltiservice_toolsettings
21 * @copyright 2014 Vital Source Technologies http://vitalsource.com
22 * @author Stephen Vickers
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27namespace ltiservice_toolsettings\local\service;
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * A service implementing Tool Settings.
33 *
34 * @package ltiservice_toolsettings
35 * @since Moodle 2.8
36 * @copyright 2014 Vital Source Technologies http://vitalsource.com
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class toolsettings extends \mod_lti\local\ltiservice\service_base {
40
41 /**
42 * Class constructor.
43 */
44 public function __construct() {
45
46 parent::__construct();
47 $this->id = 'toolsettings';
48 $this->name = 'Tool Settings';
49
50 }
51
52 /**
53 * Get the resources for this service.
54 *
55 * @return array
56 */
57 public function get_resources() {
58
59 if (empty($this->resources)) {
60 $this->resources = array();
61 $this->resources[] = new \ltiservice_toolsettings\local\resource\systemsettings($this);
62 $this->resources[] = new \ltiservice_toolsettings\local\resource\contextsettings($this);
63 $this->resources[] = new \ltiservice_toolsettings\local\resource\linksettings($this);
64 }
65
66 return $this->resources;
67
68 }
69
70 /**
71 * Get the distinct settings from each level by removing any duplicates from higher levels.
72 *
73 * @param array $systemsettings System level settings
74 * @param array $contextsettings Context level settings
75 * @param array $linksettings Link level settings
76 */
77 public static function distinct_settings(&$systemsettings, &$contextsettings, $linksettings) {
78
79 if (!empty($systemsettings)) {
80 foreach ($systemsettings as $key => $value) {
81 if ((!empty($contextsettings) && array_key_exists($key, $contextsettings)) ||
82 (!empty($linksettings) && array_key_exists($key, $linksettings))) {
83 unset($systemsettings[$key]);
84 }
85 }
86 }
87 if (!empty($contextsettings)) {
88 foreach ($contextsettings as $key => $value) {
89 if (!empty($linksettings) && array_key_exists($key, $linksettings)) {
90 unset($contextsettings[$key]);
91 }
92 }
93 }
94 }
95
96 /**
97 * Get the JSON representation of the settings.
98 *
99 * @param array $settings Settings
100 * @param boolean $simpleformat <code>true</code> if simple JSON is to be returned
101 * @param string $type JSON-LD type
102 * @param \mod_lti\local\ltiservice\resource_base $resource Resource handling the request
103 *
104 * @return string
105 */
106 public static function settings_to_json($settings, $simpleformat, $type, $resource) {
107
108 $json = '';
109 if (!empty($resource)) {
110 $indent = '';
111 if (!$simpleformat) {
112 $json .= " {\n \"@type\":\"{$type}\",\n";
113 $json .= " \"@id\":\"{$resource->get_endpoint()}\",\n";
114 $json .= ' "custom":';
115 $json .= "{";
116 $indent = ' ';
117 }
118 $isfirst = true;
119 if (!empty($settings)) {
120 foreach ($settings as $key => $value) {
121 if (!$isfirst) {
122 $json .= ",";
123 } else {
124 $isfirst = false;
125 }
126 $json .= "\n{$indent} \"{$key}\":\"{$value}\"";
127 }
128 }
129 if (!$simpleformat) {
130 $json .= "\n{$indent}}\n }";
131 }
132 }
133
134 return $json;
135
136 }
137
138}