MDL-17444: Backup hooks for local plugin
[moodle.git] / course / publish / index.php
blobd401e5c345b9e6d5f60312a9ed702ef2542a405d
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.
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/>.
18 * @package course
19 * @subpackage publish
20 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
22 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
24 * The user selects if he wants to publish the course on Moodle.org hub or
25 * on a specific hub. The site must be registered on a hub to be able to
26 * publish a course on it.
29 require('../../config.php');
30 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
31 require_once($CFG->dirroot . '/course/publish/lib.php');
33 $id = required_param('id', PARAM_INT);
34 $hubname = optional_param('hubname', 0, PARAM_TEXT);
35 $huburl = optional_param('huburl', 0, PARAM_URL);
37 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
39 require_login($course);
40 $context = context_course::instance($course->id);
41 $shortname = format_string($course->shortname, true, array('context' => $context));
43 $PAGE->set_url('/course/publish/index.php', array('id' => $course->id));
44 $PAGE->set_pagelayout('course');
45 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
46 $PAGE->set_heading($course->fullname);
48 //check that the PHP xmlrpc extension is enabled
49 if (!extension_loaded('xmlrpc')) {
50 $notificationerror = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
51 $notificationerror .= get_string('xmlrpcdisabledpublish', 'hub');
52 echo $OUTPUT->header();
53 echo $OUTPUT->heading(get_string('publishcourse', 'hub', $shortname), 3, 'main');
54 echo $OUTPUT->notification($notificationerror);
55 echo $OUTPUT->footer();
56 die();
59 if (has_capability('moodle/course:publish', context_course::instance($id))) {
61 $publicationmanager = new course_publish_manager();
62 $confirmmessage = '';
64 //update the courses status
65 $updatestatusid = optional_param('updatestatusid', false, PARAM_INT);
66 if (!empty($updatestatusid) and confirm_sesskey()) {
67 //get the communication token from the publication
68 $hub = $publicationmanager->get_registeredhub_by_publication($updatestatusid);
69 if (empty($hub)) {
70 $confirmmessage = $OUTPUT->notification(get_string('nocheckstatusfromunreghub', 'hub'));
71 } else {
72 //get all site courses registered on this hub
73 $function = 'hub_get_courses';
74 $params = array('search' => '', 'downloadable' => 1,
75 'enrollable' => 1, 'options' => array( 'allsitecourses' => 1));
76 $serverurl = $hub->huburl."/local/hub/webservice/webservices.php";
77 require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
78 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
79 $result = $xmlrpcclient->call($function, $params);
80 $sitecourses = $result['courses'];
82 //update status for all these course
83 foreach ($sitecourses as $sitecourse) {
84 //get the publication from the hub course id
85 $publication = $publicationmanager->get_publication($sitecourse['id'], $hub->huburl);
86 if (!empty($publication)) {
87 $publication->status = $sitecourse['privacy'];
88 $publication->timechecked = time();
89 $publicationmanager->update_publication($publication);
90 } else {
91 $msgparams = new stdClass();
92 $msgparams->id = $sitecourse['id'];
93 $msgparams->hubname = html_writer::tag('a', $hub->hubname, array('href' => $hub->huburl));
94 $confirmmessage .= $OUTPUT->notification(
95 get_string('detectednotexistingpublication', 'hub', $msgparams));
101 //if the site os registered on no hub display an error page
102 $registrationmanager = new registration_manager();
103 $registeredhubs = $registrationmanager->get_registered_on_hubs();
104 if (empty($registeredhubs)) {
105 echo $OUTPUT->header();
106 echo $OUTPUT->heading(get_string('publishon', 'hub'), 3, 'main');
107 echo $OUTPUT->box(get_string('notregisteredonhub', 'hub'));
108 echo $OUTPUT->footer();
109 die();
112 $renderer = $PAGE->get_renderer('core', 'publish');
114 /// UNPUBLISH
115 $cancel = optional_param('cancel', 0, PARAM_BOOL);
116 if (!empty($cancel) and confirm_sesskey()) {
117 $confirm = optional_param('confirm', 0, PARAM_BOOL);
118 $hubcourseid = optional_param('hubcourseid', 0, PARAM_INT);
119 $publicationid = optional_param('publicationid', 0, PARAM_INT);
120 $timepublished = optional_param('timepublished', 0, PARAM_INT);
121 $publication->courseshortname = $course->shortname;
122 $publication->courseid = $course->id;
123 $publication->hubname = $hubname;
124 $publication->huburl = $huburl;
125 $publication->hubcourseid = $hubcourseid;
126 $publication->timepublished = $timepublished;
127 if (empty($publication->hubname)) {
128 $publication->hubname = $huburl;
130 $publication->id = $publicationid;
131 if($confirm) {
132 //unpublish the publication by web service
133 $registeredhub = $registrationmanager->get_registeredhub($huburl);
134 $function = 'hub_unregister_courses';
135 $params = array('courseids' => array( $publication->hubcourseid));
136 $serverurl = $huburl."/local/hub/webservice/webservices.php";
137 require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
138 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
139 $result = $xmlrpcclient->call($function, $params);
141 //delete the publication from the database
142 $publicationmanager->delete_publication($publicationid);
144 //display confirmation message
145 $confirmmessage = $OUTPUT->notification(get_string('courseunpublished', 'hub', $publication), 'notifysuccess');
147 } else {
148 //display confirmation page for unpublishing
150 echo $OUTPUT->header();
151 echo $OUTPUT->heading(get_string('unpublishcourse', 'hub', $shortname), 3, 'main');
152 echo $renderer->confirmunpublishing($publication);
153 echo $OUTPUT->footer();
154 die();
158 //check if a course was published
159 if (optional_param('published', 0, PARAM_TEXT)) {
160 $confirmmessage = $OUTPUT->notification(get_string('coursepublished', 'hub',
161 empty($hubname)?$huburl:$hubname), 'notifysuccess');
165 /// OUTPUT
166 echo $OUTPUT->header();
167 echo $confirmmessage;
169 echo $OUTPUT->heading(get_string('publishcourse', 'hub', $shortname), 3, 'main');
170 echo $renderer->publicationselector($course->id);
172 $publications = $publicationmanager->get_course_publications($course->id);
173 if (!empty($publications)) {
174 echo $OUTPUT->heading(get_string('publishedon', 'hub'), 3, 'main');
175 echo $renderer->registeredonhublisting($course->id, $publications);
178 echo $OUTPUT->footer();