moodle/course/publish/index.php

164 lines
6.7 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* @package course
* @subpackage publish
* @author Jerome Mouneyrac <jerome@mouneyrac.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
*
* The user selects if he wants to publish the course on Moodle.org hub or
* on a specific hub. The site must be registered on a hub to be able to
* publish a course on it.
*/
require('../../config.php');
require_once($CFG->dirroot.'/lib/hublib.php');
$id = optional_param('id', 0, PARAM_INT);
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
require_login($course);
if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $id))) {
$hubmanager = new hub();
$confirmmessage = '';
//update the courses status
$updatestatusid = optional_param('updatestatusid', false, PARAM_INT);
if (!empty($updatestatusid) and confirm_sesskey()) {
//get the communication token from the publication
$hub = $hubmanager->get_registeredhub_by_publication($updatestatusid);
if (empty($hub)) {
$confirmmessage = $OUTPUT->notification(get_string('nocheckstatusfromunreghub', 'hub'));
} else {
//get all site courses registered on this hub
$function = 'hub_get_courses';
$params = array('', 1, 1, array( 'allsitecourses' => 1));
$serverurl = $hub->huburl."/local/hub/webservice/webservices.php";
require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
$xmlrpcclient = new webservice_xmlrpc_client();
$sitecourses = $xmlrpcclient->call($serverurl, $hub->token, $function, $params);
//update status for all these course
foreach ($sitecourses as $sitecourse) {
//get the publication from the hub course id
$publication = $hubmanager->get_publication($sitecourse['id']);
if (!empty($publication)) {
$publication->status = $sitecourse['privacy'];
$publication->timechecked = time();
$hubmanager->update_publication($publication);
} else {
$msgparams = new stdClass();
$msgparams->id = $sitecourse['id'];
$msgparams->hubname = html_writer::tag('a', $hub->hubname, array('href' => $hub->huburl));
$confirmmessage .= $OUTPUT->notification(
get_string('detectednotexistingpublication', 'hub', $msgparams));
}
}
}
}
$PAGE->set_url('/course/publish/index.php', array('id' => $course->id));
$PAGE->set_pagelayout('course');
$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
$PAGE->set_heading($course->fullname);
//if the site os registered on no hub display an error page
$registeredhubs = $hubmanager->get_registered_on_hubs();
if (empty($registeredhubs)) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('publishon', 'hub'), 3, 'main');
echo $OUTPUT->box(get_string('notregisteredonhub', 'hub'));
echo $OUTPUT->footer();
die();
}
$renderer = $PAGE->get_renderer('core', 'publish');
/// UNPUBLISH
$cancel = optional_param('cancel', 0, PARAM_BOOL);
if (!empty($cancel) and confirm_sesskey()) {
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$hubname = optional_param('hubname', 0, PARAM_TEXT);
$huburl = optional_param('huburl', 0, PARAM_URL);
$hubcourseid = optional_param('hubcourseid', 0, PARAM_INT);
$publicationid = optional_param('publicationid', 0, PARAM_INT);
$timepublished = optional_param('timepublished', 0, PARAM_INT);
$publication->courseshortname = $course->shortname;
$publication->courseid = $course->id;
$publication->hubname = $hubname;
$publication->huburl = $huburl;
$publication->hubcourseid = $hubcourseid;
$publication->timepublished = $timepublished;
if (empty($publication->hubname)) {
$publication->hubname = $huburl;
}
$publication->id = $publicationid;
if($confirm) {
//unpublish the publication by web service
$registeredhub = $hubmanager->get_registeredhub($huburl);
$function = 'hub_unregister_courses';
$params = array(array( $publication->hubcourseid));
$serverurl = $huburl."/local/hub/webservice/webservices.php";
require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
$xmlrpcclient = new webservice_xmlrpc_client();
$result = $xmlrpcclient->call($serverurl, $registeredhub->token, $function, $params);
//delete the publication from the database
$hubmanager->delete_publication($publicationid);
//display confirmation message
$confirmmessage = $OUTPUT->notification(get_string('courseunpublished', 'hub', $publication), 'notifysuccess');
} else {
//display confirmation page for unpublishing
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('unpublishcourse', 'hub', $course->shortname), 3, 'main');
echo $renderer->confirmunpublishing($publication);
echo $OUTPUT->footer();
die();
}
}
//check if a course was published
if (optional_param('published', 0, PARAM_TEXT)) {
$confirmmessage = $OUTPUT->notification(get_string('coursepublished', 'hub'), 'notifysuccess');
}
/// OUTPUT
echo $OUTPUT->header();
echo $confirmmessage;
echo $OUTPUT->heading(get_string('publishcourse', 'hub', $course->shortname), 3, 'main');
echo $renderer->publicationselector($course->id);
$publications = $hubmanager->get_course_publications($course->id);
if (!empty($publications)) {
echo $OUTPUT->heading(get_string('publishedon', 'hub'), 3, 'main');
echo $renderer->registeredonhublisting($course->id, $publications);
}
echo $OUTPUT->footer();
}