Example #1
0
/**
 * Submits a view or collection for assessment by a remote service
 *
 * @param string $username
 * @param int $id The ID of the view or collection to be submitted
 * @param boolean $iscollection Indicates whether it's a view or a collection
 * @return array An array of data for the web service to consume
 */
function submit_view_for_assessment($username, $id, $iscollection = false)
{
    global $REMOTEWWWROOT;
    list($user, $authinstance) = find_remote_user($username, $REMOTEWWWROOT);
    if (!$user) {
        return false;
    }
    $id = (int) $id;
    if (!$id) {
        return false;
    }
    require_once 'view.php';
    $remotehost = $authinstance->config['wwwroot'];
    $userid = $user->get('id');
    db_begin();
    if ($iscollection) {
        require_once 'collection.php';
        $collection = new Collection($id);
        $title = $collection->get('name');
        $description = $collection->get('description');
        // Check whether the collection is already submitted
        if ($collection->is_submitted()) {
            // If this is already submitted to something else, throw an exception
            if ($collection->get('submittedgroup') || $collection->get('submittedhost') !== $REMOTEWWWROOT) {
                throw new CollectionSubmissionException(get_string('collectionalreadysubmitted', 'view'));
            }
            // It may have been submitted to a different assignment in the same remote
            // site, but there's no way we can tell. So we'll just send the access token
            // back.
            $access = $collection->get_invisible_token();
        } else {
            $collection->submit(null, $remotehost, $userid);
            $access = $collection->new_token(false);
        }
        // If the collection is empty, $access will be false
        if (!$access) {
            throw new CollectionSubmissionException(get_string('cantsubmitemptycollection', 'view'));
        }
    } else {
        $view = new View($id);
        $title = $view->get('title');
        $description = $view->get('description');
        if ($view->is_submitted()) {
            // If this is already submitted to something else, throw an exception
            if ($view->get('submittedgroup') || $view->get('submittedhost') !== $REMOTEWWWROOT) {
                throw new ViewSubmissionException(get_string('viewalreadysubmitted', 'view'));
            }
            // It may have been submitted to a different assignment in the same remote
            // site, but there's no way we can tell. So we'll just send the access token
            // back.
            $access = View::get_invisible_token($id);
        } else {
            View::_db_submit(array($id), null, $remotehost, $userid);
            $access = View::new_token($id, false);
        }
    }
    $data = array('id' => $id, 'title' => $title, 'description' => $description, 'fullurl' => get_config('wwwroot') . 'view/view.php?mt=' . $access->token, 'url' => '/view/view.php?mt=' . $access->token, 'accesskey' => $access->token);
    // Provide each artefact plugin the opportunity to handle the remote submission and
    // provide return data for the webservice caller
    foreach (plugins_installed('artefact') as $plugin) {
        safe_require('artefact', $plugin->name);
        $classname = generate_class_name('artefact', $plugin->name);
        if (is_callable($classname . '::view_submit_external_data')) {
            $data[$plugin->name] = call_static_method($classname, 'view_submit_external_data', $id, $iscollection);
        }
    }
    db_commit();
    return $data;
}