PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lti/service/toolsettings/classes/local/service/toolsettings.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 138 lines | 64 code | 19 blank | 55 comment | 15 complexity | a1198098ac47aac3d9458b56553a4fe4 MD5 | raw file
  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. * This file contains a class definition for the Tool Settings service
  18. *
  19. * @package ltiservice_toolsettings
  20. * @copyright 2014 Vital Source Technologies http://vitalsource.com
  21. * @author Stephen Vickers
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. namespace ltiservice_toolsettings\local\service;
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * A service implementing Tool Settings.
  28. *
  29. * @package ltiservice_toolsettings
  30. * @since Moodle 2.8
  31. * @copyright 2014 Vital Source Technologies http://vitalsource.com
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class toolsettings extends \mod_lti\local\ltiservice\service_base {
  35. /**
  36. * Class constructor.
  37. */
  38. public function __construct() {
  39. parent::__construct();
  40. $this->id = 'toolsettings';
  41. $this->name = 'Tool Settings';
  42. }
  43. /**
  44. * Get the resources for this service.
  45. *
  46. * @return array
  47. */
  48. public function get_resources() {
  49. if (empty($this->resources)) {
  50. $this->resources = array();
  51. $this->resources[] = new \ltiservice_toolsettings\local\resources\systemsettings($this);
  52. $this->resources[] = new \ltiservice_toolsettings\local\resources\contextsettings($this);
  53. $this->resources[] = new \ltiservice_toolsettings\local\resources\linksettings($this);
  54. }
  55. return $this->resources;
  56. }
  57. /**
  58. * Get the distinct settings from each level by removing any duplicates from higher levels.
  59. *
  60. * @param array $systemsettings System level settings
  61. * @param array $contextsettings Context level settings
  62. * @param array $linksettings Link level settings
  63. */
  64. public static function distinct_settings(&$systemsettings, &$contextsettings, $linksettings) {
  65. if (!empty($systemsettings)) {
  66. foreach ($systemsettings as $key => $value) {
  67. if ((!empty($contextsettings) && array_key_exists($key, $contextsettings)) ||
  68. (!empty($linksettings) && array_key_exists($key, $linksettings))) {
  69. unset($systemsettings[$key]);
  70. }
  71. }
  72. }
  73. if (!empty($contextsettings)) {
  74. foreach ($contextsettings as $key => $value) {
  75. if (!empty($linksettings) && array_key_exists($key, $linksettings)) {
  76. unset($contextsettings[$key]);
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * Get the JSON representation of the settings.
  83. *
  84. * @param array $settings Settings
  85. * @param boolean $simpleformat <code>true</code> if simple JSON is to be returned
  86. * @param string $type JSON-LD type
  87. * @param \mod_lti\local\ltiservice\resource_base $resource Resource handling the request
  88. *
  89. * @return string
  90. */
  91. public static function settings_to_json($settings, $simpleformat, $type, $resource) {
  92. $json = '';
  93. if (!empty($resource)) {
  94. $indent = '';
  95. if (!$simpleformat) {
  96. $json .= " {\n \"@type\":\"{$type}\",\n";
  97. $json .= " \"@id\":\"{$resource->get_endpoint()}\",\n";
  98. $json .= " \"custom\":{\n";
  99. $json .= " \"@id\":\"{$resource->get_endpoint()}/custom\"";
  100. $indent = ' ';
  101. }
  102. $isfirst = $simpleformat;
  103. if (!empty($settings)) {
  104. foreach ($settings as $key => $value) {
  105. if (!$isfirst) {
  106. $json .= ',';
  107. } else {
  108. $isfirst = false;
  109. }
  110. $json .= "\n{$indent} \"{$key}\":\"{$value}\"";
  111. }
  112. }
  113. if (!$simpleformat) {
  114. $json .= "\n{$indent}}\n }";
  115. }
  116. }
  117. return $json;
  118. }
  119. }