MDL-40050 webservice: Replace add_to_log() with new events
[moodle.git] / admin / webservice / service.php
1 <?php
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/>.
18 /**
19  * Web services admin 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('forms.php');
28 require_once($CFG->dirroot . '/webservice/lib.php');
30 admin_externalpage_setup('externalservice');
32 //define nav bar
33 $node = $PAGE->settingsnav->find('externalservice', navigation_node::TYPE_SETTING);
34 $newnode = $PAGE->settingsnav->find('externalservices', navigation_node::TYPE_SETTING);
35 if ($node && $newnode) {
36     $node->display = false;
37     $newnode->make_active();
38 }
39 $PAGE->navbar->add(get_string('externalservice', 'webservice'));
41 //Retrieve few general parameters
42 $id = required_param('id', PARAM_INT);
43 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
44 $confirm = optional_param('confirm', 0, PARAM_BOOL);
45 $webservicemanager = new webservice;
46 $renderer = $PAGE->get_renderer('core', 'webservice');
47 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
48 $service = $id ? $webservicemanager->get_external_service_by_id($id, MUST_EXIST) : null;
50 /// DELETE operation
51 if ($action == 'delete' and confirm_sesskey() and $service and empty($service->component)) {
52     //Display confirmation Page
53     if (!$confirm) {
54         echo $OUTPUT->header();
55         echo $renderer->admin_remove_service_confirmation($service);
56         echo $OUTPUT->footer();
57         die;
58     }
59     //The user has confirmed the deletion, delete and redirect
60     $webservicemanager->delete_service($service->id);
61     $params = array(
62         'objectid' => $service->id
63     );
64     $event = \core\event\webservice_service_deleted::create($params);
65     $event->add_record_snapshot('external_services', $service);
66     $event->trigger();
67     redirect($returnurl);
68 }
70 /// EDIT/CREATE/CANCEL operations => at the end redirect to add function page / main service page
71 $mform = new external_service_form(null, $service);
72 if ($mform->is_cancelled()) {
73     redirect($returnurl);
74 } else if ($servicedata = $mform->get_data()) {
75     $servicedata = (object) $servicedata;
76     if (!empty($servicedata->requiredcapability) && $servicedata->requiredcapability == "norequiredcapability") {
77         $servicedata->requiredcapability = "";
78     }
80     //create operation
81     if (empty($servicedata->id)) {
82         $servicedata->id = $webservicemanager->add_external_service($servicedata);
83         $params = array(
84             'objectid' => $servicedata->id
85         );
86         $event = \core\event\webservice_service_updated::create($params);
87         $event->add_record_snapshot('external_services', $servicedata);
88         $event->trigger();
90         //redirect to the 'add functions to service' page
91         $addfunctionpage = new moodle_url(
92                         $CFG->wwwroot . '/' . $CFG->admin . '/webservice/service_functions.php',
93                         array('id' => $servicedata->id));
94         $returnurl = $addfunctionpage->out(false);
95     } else {
96         //update operation
97         $webservicemanager->update_external_service($servicedata);
98         $params = array(
99             'objectid' => $servicedata->id
100         );
101         $event = \core\event\webservice_service_created::create($params);
102         $event->add_record_snapshot('external_services', $servicedata);
103         $event->trigger();
104     }
106     redirect($returnurl);
109 //OUTPUT edit/create form
110 echo $OUTPUT->header();
111 $mform->display();
112 echo $OUTPUT->footer();