MDL-38814 behat: Move filepicker steps definitions to repository/
[moodle.git] / course / publish / metadata.php
blob298e5f3e1d468f6cc72d1c16a0a21c0348fa6b64
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // This file is part of Moodle - http://moodle.org/ //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // //
8 // Moodle is free software: you can redistribute it and/or modify //
9 // it under the terms of the GNU General Public License as published by //
10 // the Free Software Foundation, either version 3 of the License, or //
11 // (at your option) any later version. //
12 // //
13 // Moodle is distributed in the hope that it will be useful, //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
16 // GNU General Public License for more details. //
17 // //
18 // You should have received a copy of the GNU General Public License //
19 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. //
20 // //
21 ///////////////////////////////////////////////////////////////////////////
24 * @package course
25 * @subpackage publish
26 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
28 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
30 * This page display the publication metadata form
33 require_once('../../config.php');
34 require_once($CFG->dirroot . '/course/publish/forms.php');
35 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
36 require_once($CFG->dirroot . '/course/publish/lib.php');
37 require_once($CFG->libdir . '/filelib.php');
40 //check user access capability to this page
41 $id = required_param('id', PARAM_INT);
43 $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
44 require_login($course);
46 //page settings
47 $PAGE->set_url('/course/publish/metadata.php', array('id' => $course->id));
48 $PAGE->set_pagelayout('course');
49 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
50 $PAGE->set_heading($course->fullname);
52 //check that the PHP xmlrpc extension is enabled
53 if (!extension_loaded('xmlrpc')) {
54 $errornotification = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
55 $errornotification .= get_string('xmlrpcdisabledpublish', 'hub');
56 $context = context_course::instance($course->id);
57 $shortname = format_string($course->shortname, true, array('context' => $context));
58 echo $OUTPUT->header();
59 echo $OUTPUT->heading(get_string('publishcourse', 'hub', $shortname), 3, 'main');
60 echo $OUTPUT->notification($errornotification);
61 echo $OUTPUT->footer();
62 die();
65 if (has_capability('moodle/course:publish', context_course::instance($id))) {
67 //retrieve hub name and hub url
68 $huburl = optional_param('huburl', '', PARAM_URL);
69 $hubname = optional_param('hubname', '', PARAM_TEXT);
70 if (empty($huburl) or !confirm_sesskey()) {
71 throw new moodle_exception('missingparameter');
74 //set the publication form
75 $advertise = optional_param('advertise', false, PARAM_BOOL);
76 $share = optional_param('share', false, PARAM_BOOL);
77 $coursepublicationform = new course_publication_form('',
78 array('huburl' => $huburl, 'hubname' => $hubname, 'sesskey' => sesskey(),
79 'course' => $course, 'advertise' => $advertise, 'share' => $share,
80 'id' => $id, 'page' => $PAGE));
81 $fromform = $coursepublicationform->get_data();
83 //retrieve the token to call the hub
84 $registrationmanager = new registration_manager();
85 $registeredhub = $registrationmanager->get_registeredhub($huburl);
87 //setup web service xml-rpc client
88 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
89 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
90 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
92 if (!empty($fromform)) {
94 $publicationmanager = new course_publish_manager();
96 //retrieve the course information
97 $courseinfo = new stdClass();
98 $courseinfo->fullname = $fromform->name;
99 $courseinfo->shortname = $fromform->courseshortname;
100 $courseinfo->description = $fromform->description;
101 $courseinfo->language = $fromform->language;
102 $courseinfo->publishername = $fromform->publishername;
103 $courseinfo->publisheremail = $fromform->publisheremail;
104 $courseinfo->contributornames = $fromform->contributornames;
105 $courseinfo->coverage = $fromform->coverage;
106 $courseinfo->creatorname = $fromform->creatorname;
107 $courseinfo->licenceshortname = $fromform->licence;
108 $courseinfo->subject = $fromform->subject;
109 $courseinfo->audience = $fromform->audience;
110 $courseinfo->educationallevel = $fromform->educationallevel;
111 $creatornotes = $fromform->creatornotes;
112 $courseinfo->creatornotes = $creatornotes['text'];
113 $courseinfo->creatornotesformat = $creatornotes['format'];
114 $courseinfo->sitecourseid = $id;
115 if (!empty($fromform->deletescreenshots)) {
116 $courseinfo->deletescreenshots = $fromform->deletescreenshots;
118 if ($share) {
119 $courseinfo->demourl = $fromform->demourl;
120 $courseinfo->enrollable = false;
121 } else {
122 $courseinfo->courseurl = $fromform->courseurl;
123 $courseinfo->enrollable = true;
126 //retrieve the outcomes of this course
127 require_once($CFG->libdir . '/grade/grade_outcome.php');
128 $outcomes = grade_outcome::fetch_all_available($id);
129 if (!empty($outcomes)) {
130 foreach ($outcomes as $outcome) {
131 $sentoutcome = new stdClass();
132 $sentoutcome->fullname = $outcome->fullname;
133 $courseinfo->outcomes[] = $sentoutcome;
137 //retrieve the content information from the course
138 $coursecontext = context_course::instance($course->id);
139 $courseblocks = $publicationmanager->get_block_instances_by_context($coursecontext->id, 'blockname');
141 if (!empty($courseblocks)) {
142 $blockname = '';
143 foreach ($courseblocks as $courseblock) {
144 if ($courseblock->blockname != $blockname) {
145 if (!empty($blockname)) {
146 $courseinfo->contents[] = $content;
149 $blockname = $courseblock->blockname;
150 $content = new stdClass();
151 $content->moduletype = 'block';
152 $content->modulename = $courseblock->blockname;
153 $content->contentcount = 1;
154 } else {
155 $content->contentcount = $content->contentcount + 1;
158 $courseinfo->contents[] = $content;
161 $activities = get_fast_modinfo($course, $USER->id);
162 foreach ($activities->instances as $activityname => $activitydetails) {
163 $content = new stdClass();
164 $content->moduletype = 'activity';
165 $content->modulename = $activityname;
166 $content->contentcount = count($activities->instances[$activityname]);
167 $courseinfo->contents[] = $content;
170 //save into screenshots field the references to the screenshot content hash
171 //(it will be like a unique id from the hub perspective)
172 if (!empty($fromform->deletescreenshots) or $share) {
173 $courseinfo->screenshots = 0;
174 } else {
175 $courseinfo->screenshots = $fromform->existingscreenshotnumber;
177 if (!empty($fromform->screenshots)) {
178 $screenshots = $fromform->screenshots;
179 $fs = get_file_storage();
180 $files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $screenshots);
181 if (!empty($files)) {
182 $courseinfo->screenshots = $courseinfo->screenshots + count($files) - 1; //minus the ./ directory
186 // PUBLISH ACTION
188 //publish the course information
189 $function = 'hub_register_courses';
190 $params = array('courses' => array($courseinfo));
191 try {
192 $courseids = $xmlrpcclient->call($function, $params);
193 } catch (Exception $e) {
194 throw new moodle_exception('errorcoursepublish', 'hub',
195 new moodle_url('/course/view.php', array('id' => $id)), $e->getMessage());
198 if (count($courseids) != 1) {
199 throw new moodle_exception('errorcoursewronglypublished', 'hub');
202 //save the record into the published course table
203 $publication = $publicationmanager->get_publication($courseids[0], $huburl);
204 if (empty($publication)) {
205 //if never been published or if we share, we need to save this new publication record
206 $publicationmanager->add_course_publication($registeredhub->huburl, $course->id, !$share, $courseids[0]);
207 } else {
208 //if we update the enrollable course publication we update the publication record
209 $publicationmanager->update_enrollable_course_publication($publication->id);
212 // SEND FILES
213 $curl = new curl();
215 // send screenshots
216 if (!empty($fromform->screenshots)) {
218 if (!empty($fromform->deletescreenshots) or $share) {
219 $screenshotnumber = 0;
220 } else {
221 $screenshotnumber = $fromform->existingscreenshotnumber;
223 foreach ($files as $file) {
224 if ($file->is_valid_image()) {
225 $screenshotnumber = $screenshotnumber + 1;
226 $params = array();
227 $params['filetype'] = HUB_SCREENSHOT_FILE_TYPE;
228 $params['file'] = $file;
229 $params['courseid'] = $courseids[0];
230 $params['filename'] = $file->get_filename();
231 $params['screenshotnumber'] = $screenshotnumber;
232 $params['token'] = $registeredhub->token;
233 $curl->post($huburl . "/local/hub/webservice/upload.php", $params);
238 //redirect to the backup process page
239 if ($share) {
240 $params = array('sesskey' => sesskey(), 'id' => $id, 'hubcourseid' => $courseids[0],
241 'huburl' => $huburl, 'hubname' => $hubname);
242 $backupprocessurl = new moodle_url("/course/publish/backup.php", $params);
243 redirect($backupprocessurl);
244 } else {
245 //redirect to the index publis page
246 redirect(new moodle_url('/course/publish/index.php',
247 array('sesskey' => sesskey(), 'id' => $id, 'published' => true,
248 'hubname' => $hubname, 'huburl' => $huburl)));
252 /////// OUTPUT SECTION /////////////
254 echo $OUTPUT->header();
255 echo $OUTPUT->heading(get_string('publishcourseon', 'hub', !empty($hubname) ? $hubname : $huburl), 3, 'main');
257 //display hub information (logo, name, description)
258 $function = 'hub_get_info';
259 $params = array();
260 try {
261 $hubinfo = $xmlrpcclient->call($function, $params);
262 } catch (Exception $e) {
263 //only print error log in apache (for backward compatibility)
264 error_log(print_r($e->getMessage(), true));
266 $renderer = $PAGE->get_renderer('core', 'publish');
267 if (!empty($hubinfo)) {
268 echo $renderer->hubinfo($hubinfo);
271 //display metadata form
272 $coursepublicationform->display();
273 echo $OUTPUT->footer();