Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Web services function UI
  20   *
  21   * @package   webservice
  22   * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  require_once('../../config.php');
  26  require_once($CFG->libdir . '/adminlib.php');
  27  require_once($CFG->dirroot . '/webservice/lib.php');
  28  require_once ('forms.php');
  29  
  30  $serviceid = required_param('id', PARAM_INT);
  31  $functionid = optional_param('fid', 0, PARAM_INT);
  32  $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  33  $confirm = optional_param('confirm', 0, PARAM_BOOL);
  34  
  35  admin_externalpage_setup('externalservicefunctions');
  36  
  37  //define nav bar
  38  $PAGE->set_url('/' . $CFG->admin . '/webservice/service_functions.php', array('id' => $serviceid));
  39  $node = $PAGE->settingsnav->find('externalservices', navigation_node::TYPE_SETTING);
  40  if ($node) {
  41      $node->make_active();
  42  }
  43  $PAGE->set_primary_active_tab('siteadminnode');
  44  $PAGE->navbar->add(get_string('externalservices', 'webservice'),
  45     new moodle_url('/admin/settings.php', ['section' => 'externalservices']));
  46  $PAGE->navbar->add(get_string('functions', 'webservice'),
  47          new moodle_url('/' . $CFG->admin . '/webservice/service_functions.php', array('id' => $serviceid)));
  48  
  49  $service = $DB->get_record('external_services', array('id' => $serviceid), '*', MUST_EXIST);
  50  $webservicemanager = new webservice();
  51  $renderer = $PAGE->get_renderer('core', 'webservice');
  52  $functionlisturl = new moodle_url('/' . $CFG->admin . '/webservice/service_functions.php',
  53          array('id' => $serviceid));
  54  
  55  // Add or Delete operations
  56  switch ($action) {
  57      case 'add':
  58          $PAGE->navbar->add(get_string('addfunctions', 'webservice'));
  59          /// Add function operation
  60          if (confirm_sesskey() and $service and empty($service->component)) {
  61              $mform = new external_service_functions_form(null,
  62                      array('action' => 'add', 'id' => $service->id));
  63  
  64              //cancelled add operation, redirect to function list page
  65              if ($mform->is_cancelled()) {
  66                  redirect($functionlisturl);
  67              }
  68  
  69              //add the function to the service then redirect to function list page
  70              if ($data = $mform->get_data()) {
  71                  ignore_user_abort(true); // no interruption here!
  72                  foreach ($data->fids as $fid) {
  73                      $function = $webservicemanager->get_external_function_by_id(
  74                              $fid, MUST_EXIST);
  75                      // make sure the function is not there yet
  76                      if (!$webservicemanager->service_function_exists($function->name,
  77                              $service->id)) {
  78                          $webservicemanager->add_external_function_to_service(
  79                                  $function->name, $service->id);
  80                      }
  81                  }
  82                  redirect($functionlisturl);
  83              }
  84  
  85              //Add function operation page output
  86              echo $OUTPUT->header();
  87              echo $OUTPUT->heading($service->name);
  88              $mform->display();
  89              echo $OUTPUT->footer();
  90              die;
  91          }
  92  
  93          break;
  94  
  95      case 'delete':
  96          $PAGE->navbar->add(get_string('removefunction', 'webservice'));
  97          /// Delete function operation
  98          if (confirm_sesskey() and $service and empty($service->component)) {
  99              //check that the function to remove exists
 100              $function = $webservicemanager->get_external_function_by_id(
 101                              $functionid, MUST_EXIST);
 102  
 103              //display confirmation page
 104              if (!$confirm) {
 105                  echo $OUTPUT->header();
 106                  echo $renderer->admin_remove_service_function_confirmation($function, $service);
 107                  echo $OUTPUT->footer();
 108                  die;
 109              }
 110  
 111              //or remove the function from the service, then redirect to the function list
 112              $webservicemanager->remove_external_function_from_service($function->name,
 113                     $service->id);
 114              redirect($functionlisturl);
 115          }
 116          break;
 117  }
 118  
 119  /// OUTPUT function list page
 120  echo $OUTPUT->header();
 121  echo $OUTPUT->heading(get_string('addservicefunction', 'webservice', $service->name));
 122  $functions = $webservicemanager->get_external_functions(array($service->id));
 123  echo $renderer->admin_service_function_list($functions, $service);
 124  echo $OUTPUT->footer();