/** * 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; }
/** * Submit this collection to a group or a remote host (but only one or the other!) * @param object $group * @param string $submittedhost * @param int $owner The owner of the collection (if not just $USER) * @throws SystemException */ public function submit($group = null, $submittedhost = null, $owner = null) { global $USER; if ($this->is_submitted()) { throw new CollectionSubmissionException(get_string('collectionalreadysubmitted', 'view')); } // Gotta provide one or the other if (!$group && !$submittedhost) { return false; } $viewids = $this->get_viewids(); if (!$viewids) { throw new CollectionSubmissionException(get_string('cantsubmitemptycollection', 'view')); } $idstr = join(',', array_map('intval', $viewids)); $owner = $owner == null ? $USER->get('id') : $owner; // Check that none of the views is submitted to some other group. This is bound to happen to someone, // because collection submission is being introduced at a time when it is still possible to submit // individual views in a collection. $sql = "SELECT title FROM {view} WHERE id IN ({$idstr}) AND (submittedhost IS NOT NULL OR (submittedgroup IS NOT NULL"; $params = array(); // To ease the transition, if you've submitted one page of the collection to this group already, you // can submit the rest as well if ($group) { $sql .= ' AND submittedgroup != ?'; $params[] = $group->id; } $sql .= '))'; $submittedtitles = get_column_sql($sql, $params); if (!empty($submittedtitles)) { throw new CollectionSubmissionException(get_string('collectionviewsalreadysubmitted', 'view', implode('", "', $submittedtitles))); } if ($group) { $group->roles = get_column('grouptype_roles', 'role', 'grouptype', $group->grouptype, 'see_submitted_views', 1); } db_begin(); View::_db_submit($viewids, $group, $submittedhost, $owner); if ($group) { $this->set('submittedgroup', $group->id); $this->set('submittedhost', null); } else { $this->set('submittedgroup', null); $this->set('submittedhost', $submittedhost); } $this->set('submittedtime', time()); $this->set('submittedstatus', self::SUBMITTED); $this->commit(); db_commit(); if ($group) { activity_occurred('groupmessage', array('group' => $group->id, 'roles' => $group->roles, 'url' => $this->get_url(false), 'strings' => (object) array('urltext' => (object) array('key' => 'Collection', 'section' => 'collection'), 'subject' => (object) array('key' => 'viewsubmittedsubject1', 'section' => 'activity', 'args' => array($group->name)), 'message' => (object) array('key' => 'viewsubmittedmessage1', 'section' => 'activity', 'args' => array(display_name($USER, null, false, true), $this->name, $group->name))))); } }