Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 672af3c102
Fetching contributors…

Cannot retrieve contributors at this time

file 274 lines (240 sloc) 12.169 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
<?php

///////////////////////////////////////////////////////////////////////////
// //
// This file is part of Moodle - http://moodle.org/ //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// //
// 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
*
* This page display the publication metadata form
*/

require_once('../../config.php');
require_once($CFG->dirroot . '/course/publish/forms.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
require_once($CFG->dirroot . '/course/publish/lib.php');
require_once($CFG->libdir . '/filelib.php');


//check user access capability to this page
$id = required_param('id', PARAM_INT);

$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
require_login($course);

//page settings
$PAGE->set_url('/course/publish/metadata.php', array('id' => $course->id));
$PAGE->set_pagelayout('course');
$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
$PAGE->set_heading($course->fullname);

//check that the PHP xmlrpc extension is enabled
if (!extension_loaded('xmlrpc')) {
    $errornotification = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
    $errornotification .= get_string('xmlrpcdisabledpublish', 'hub');
    $context = get_context_instance(CONTEXT_COURSE, $course->id);
    $shortname = format_string($course->shortname, true, array('context' => $context));
    echo $OUTPUT->header();
    echo $OUTPUT->heading(get_string('publishcourse', 'hub', $shortname), 3, 'main');
    echo $OUTPUT->notification($errornotification);
    echo $OUTPUT->footer();
    die();
}

if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $id))) {

    //retrieve hub name and hub url
    $huburl = optional_param('huburl', '', PARAM_URL);
    $hubname = optional_param('hubname', '', PARAM_TEXT);
    if (empty($huburl) or !confirm_sesskey()) {
        throw new moodle_exception('missingparameter');
    }

    //set the publication form
    $advertise = optional_param('advertise', false, PARAM_BOOL);
    $share = optional_param('share', false, PARAM_BOOL);
    $coursepublicationform = new course_publication_form('',
                    array('huburl' => $huburl, 'hubname' => $hubname, 'sesskey' => sesskey(),
                        'course' => $course, 'advertise' => $advertise, 'share' => $share,
                        'id' => $id, 'page' => $PAGE));
    $fromform = $coursepublicationform->get_data();

    //retrieve the token to call the hub
    $registrationmanager = new registration_manager();
    $registeredhub = $registrationmanager->get_registeredhub($huburl);

    //setup web service xml-rpc client
    $serverurl = $huburl . "/local/hub/webservice/webservices.php";
    require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
    $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);

    if (!empty($fromform)) {

        $publicationmanager = new course_publish_manager();

        //retrieve the course information
        $courseinfo = new stdClass();
        $courseinfo->fullname = $fromform->name;
        $courseinfo->shortname = $fromform->courseshortname;
        $courseinfo->description = $fromform->description;
        $courseinfo->language = $fromform->language;
        $courseinfo->publishername = $fromform->publishername;
        $courseinfo->publisheremail = $fromform->publisheremail;
        $courseinfo->contributornames = $fromform->contributornames;
        $courseinfo->coverage = $fromform->coverage;
        $courseinfo->creatorname = $fromform->creatorname;
        $courseinfo->licenceshortname = $fromform->licence;
        $courseinfo->subject = $fromform->subject;
        $courseinfo->audience = $fromform->audience;
        $courseinfo->educationallevel = $fromform->educationallevel;
        $creatornotes = $fromform->creatornotes;
        $courseinfo->creatornotes = $creatornotes['text'];
        $courseinfo->creatornotesformat = $creatornotes['format'];
        $courseinfo->sitecourseid = $id;
        if (!empty($fromform->deletescreenshots)) {
            $courseinfo->deletescreenshots = $fromform->deletescreenshots;
        }
        if ($share) {
            $courseinfo->demourl = $fromform->demourl;
            $courseinfo->enrollable = false;
        } else {
            $courseinfo->courseurl = $fromform->courseurl;
            $courseinfo->enrollable = true;
        }

        //retrieve the outcomes of this course
        require_once($CFG->libdir . '/grade/grade_outcome.php');
        $outcomes = grade_outcome::fetch_all_available($id);
        if (!empty($outcomes)) {
            foreach ($outcomes as $outcome) {
                $sentoutcome = new stdClass();
                $sentoutcome->fullname = $outcome->fullname;
                $courseinfo->outcomes[] = $sentoutcome;
            }
        }

        //retrieve the content information from the course
        $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
        $courseblocks = $publicationmanager->get_block_instances_by_context($coursecontext->id, 'blockname');

        if (!empty($courseblocks)) {
            $blockname = '';
            foreach ($courseblocks as $courseblock) {
                if ($courseblock->blockname != $blockname) {
                    if (!empty($blockname)) {
                        $courseinfo->contents[] = $content;
                    }

                    $blockname = $courseblock->blockname;
                    $content = new stdClass();
                    $content->moduletype = 'block';
                    $content->modulename = $courseblock->blockname;
                    $content->contentcount = 1;
                } else {
                    $content->contentcount = $content->contentcount + 1;
                }
            }
            $courseinfo->contents[] = $content;
        }

        $activities = get_fast_modinfo($course, $USER->id);
        foreach ($activities->instances as $activityname => $activitydetails) {
            $content = new stdClass();
            $content->moduletype = 'activity';
            $content->modulename = $activityname;
            $content->contentcount = count($activities->instances[$activityname]);
            $courseinfo->contents[] = $content;
        }

        //save into screenshots field the references to the screenshot content hash
        //(it will be like a unique id from the hub perspective)
        if (!empty($fromform->deletescreenshots) or $share) {
            $courseinfo->screenshots = 0;
        } else {
            $courseinfo->screenshots = $fromform->existingscreenshotnumber;
        }
        if (!empty($fromform->screenshots)) {
            $screenshots = $fromform->screenshots;
            $fs = get_file_storage();
            $files = $fs->get_area_files(get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'draft', $screenshots);
            if (!empty($files)) {
                $courseinfo->screenshots = $courseinfo->screenshots + count($files) - 1; //minus the ./ directory
            }
        }

        // PUBLISH ACTION

        //publish the course information
        $function = 'hub_register_courses';
        $params = array('courses' => array($courseinfo));
        try {
            $courseids = $xmlrpcclient->call($function, $params);
        } catch (Exception $e) {
            throw new moodle_exception('errorcoursepublish', 'hub',
                    new moodle_url('/course/view.php', array('id' => $id)), $e->getMessage());
        }

        if (count($courseids) != 1) {
            throw new moodle_exception('errorcoursewronglypublished', 'hub');
        }

        //save the record into the published course table
        $publication = $publicationmanager->get_publication($courseids[0], $huburl);
        if (empty($publication)) {
            //if never been published or if we share, we need to save this new publication record
            $publicationmanager->add_course_publication($registeredhub->huburl, $course->id, !$share, $courseids[0]);
        } else {
            //if we update the enrollable course publication we update the publication record
            $publicationmanager->update_enrollable_course_publication($publication->id);
        }

        // SEND FILES
        $curl = new curl();

        // send screenshots
        if (!empty($fromform->screenshots)) {

            if (!empty($fromform->deletescreenshots) or $share) {
                $screenshotnumber = 0;
            } else {
                $screenshotnumber = $fromform->existingscreenshotnumber;
            }
            foreach ($files as $file) {
                if ($file->is_valid_image()) {
                    $screenshotnumber = $screenshotnumber + 1;
                    $params = array();
                    $params['filetype'] = HUB_SCREENSHOT_FILE_TYPE;
                    $params['file'] = $file;
                    $params['courseid'] = $courseids[0];
                    $params['filename'] = $file->get_filename();
                    $params['screenshotnumber'] = $screenshotnumber;
                    $params['token'] = $registeredhub->token;
                    $curl->post($huburl . "/local/hub/webservice/upload.php", $params);
                }
            }
        }

        //redirect to the backup process page
        if ($share) {
            $params = array('sesskey' => sesskey(), 'id' => $id, 'hubcourseid' => $courseids[0],
                'huburl' => $huburl, 'hubname' => $hubname);
            $backupprocessurl = new moodle_url("/course/publish/backup.php", $params);
            redirect($backupprocessurl);
        } else {
            //redirect to the index publis page
            redirect(new moodle_url('/course/publish/index.php',
                            array('sesskey' => sesskey(), 'id' => $id, 'published' => true,
                                'hubname' => $hubname, 'huburl' => $huburl)));
        }
    }

    /////// OUTPUT SECTION /////////////

    echo $OUTPUT->header();
    echo $OUTPUT->heading(get_string('publishcourseon', 'hub', !empty($hubname) ? $hubname : $huburl), 3, 'main');

    //display hub information (logo, name, description)
    $function = 'hub_get_info';
    $params = array();
    try {
        $hubinfo = $xmlrpcclient->call($function, $params);
    } catch (Exception $e) {
        //only print error log in apache (for backward compatibility)
        error_log(print_r($e->getMessage(), true));
    }
    $renderer = $PAGE->get_renderer('core', 'publish');
    if (!empty($hubinfo)) {
        echo $renderer->hubinfo($hubinfo);
    }

    //display metadata form
    $coursepublicationform->display();
    echo $OUTPUT->footer();
}
Something went wrong with that request. Please try again.