Example #1
0
 /**
  * Makes a literal copy of a list of collections for this user.
  *
  * @param array $templateids A list of collectionids to copy.
  */
 public function copy_collections($templateids, $checkviewaccess = true)
 {
     if (!$templateids) {
         // Nothing to do
         return;
     }
     if (!is_array($templateids)) {
         throw new SystemException('User->copy_collections: templateids must be a list of templates to copy for the user');
     }
     require_once get_config('libroot') . 'collection.php';
     $collections = array();
     $results = get_records_select_array('collection', 'id IN (' . implode(', ', db_array_to_ph($templateids)) . ')', $templateids, '', 'id, name');
     foreach ($results as $result) {
         $collections[$result->id] = $result;
     }
     db_begin();
     foreach ($templateids as $tid) {
         Collection::create_from_template(array('owner' => $this->get('id'), 'title' => $collections[$tid]->name), $tid, $this->get('id'), $checkviewaccess);
     }
     db_commit();
 }
Example #2
0
/**
 * Creates a group.
 *
 * All group creation should be done through this function, as the
 * implementation of group creation may change over time.
 *
 * @param array $data Data required to create the group. The following
 * key/value pairs can be specified:
 *
 * - name: The group name [required, must be unique]
 * - description: The group description [optional, defaults to empty string]
 * - grouptype: The grouptype for the new group. Must be an installed grouptype.
 * - open (jointype): anyone can join the group
 * - controlled (jointype): admin adds members; members cannot leave the group
 * - request: allows membership requests
 * - ctime: The unix timestamp of the time the group will be recorded as having
 *          been created. Defaults to the current time.
 * - members: Array of users who should be in the group, structured like this:
 *            array(
 *                userid => role,
 *                userid => role,
 *                ...
 *            )
 * @return int The ID of the created group
 */
function group_create($data)
{
    if (!is_array($data)) {
        throw new InvalidArgumentException("group_create: data must be an array, see the doc comment for this " . "function for details on its format");
    }
    if (!isset($data['name'])) {
        throw new InvalidArgumentException("group_create: must specify a name for the group");
    }
    if (get_records_sql_array('SELECT id FROM {group} WHERE LOWER(TRIM(name)) = ?', array(strtolower(trim($data['name']))))) {
        throw new UserException(get_string('groupalreadyexists', 'group') . ': ' . $data['name']);
    }
    if (!isset($data['grouptype']) || !in_array($data['grouptype'], group_get_grouptypes())) {
        throw new InvalidArgumentException("group_create: grouptype specified must be an installed grouptype");
    }
    safe_require('grouptype', $data['grouptype']);
    if (!empty($data['open'])) {
        if (!empty($data['controlled'])) {
            throw new InvalidArgumentException("group_create: a group cannot have both open and controlled membership");
        }
        if (!empty($data['request'])) {
            throw new InvalidArgumentException("group_create: open-membership groups don't accept membership requests");
        }
        $jointype = 'open';
    } else {
        if (!empty($data['controlled'])) {
            $jointype = 'controlled';
        } else {
            $jointype = 'approve';
        }
    }
    if (isset($data['jointype'])) {
        log_warn("group_create: ignoring supplied jointype");
    }
    if (!isset($data['ctime'])) {
        $data['ctime'] = time();
    }
    $data['ctime'] = db_format_timestamp($data['ctime']);
    $data['public'] = isset($data['public']) ? intval($data['public']) : 0;
    $data['hidden'] = isset($data['hidden']) ? intval($data['hidden']) : 0;
    $data['hidemembers'] = isset($data['hidemembers']) ? intval($data['hidemembers']) : 0;
    $data['hidemembersfrommembers'] = isset($data['hidemembersfrommembers']) ? intval($data['hidemembersfrommembers']) : 0;
    $data['groupparticipationreports'] = isset($data['groupparticipationreports']) ? intval($data['groupparticipationreports']) : 0;
    $data['usersautoadded'] = isset($data['usersautoadded']) ? intval($data['usersautoadded']) : 0;
    $data['quota'] = get_config_plugin('artefact', 'file', 'defaultgroupquota');
    if (!empty($data['invitefriends']) && !empty($data['suggestfriends'])) {
        throw new InvalidArgumentException("group_create: a group cannot enable both invitefriends and suggestfriends");
    }
    $data['invitefriends'] = isset($data['invitefriends']) ? intval($data['invitefriends']) : 0;
    $data['suggestfriends'] = isset($data['suggestfriends']) ? intval($data['suggestfriends']) : 0;
    if (isset($data['shortname']) && strlen($data['shortname'])) {
        // This is a group whose details and membership can be updated automatically, using a
        // webservice api or possibly csv upload.
        // On updates to this group, it will be identified using the institution and shortname
        // which must be unique.
        // The $USER object will be set to someone with at least institutional admin permission.
        global $USER;
        if (empty($data['institution'])) {
            throw new SystemException("group_create: a group with a shortname must have an institution; shortname: " . $data['shortname']);
        }
        if (!$USER->can_edit_institution($data['institution'])) {
            throw new AccessDeniedException("group_create: cannot create a group in this institution");
        }
        if (!preg_match('/^[a-zA-Z0-9_.-]{2,255}$/', $data['shortname'])) {
            $message = get_string('invalidshortname', 'group') . ': ' . $data['shortname'];
            $message .= "\n" . get_string('shortnameformat', 'group');
            throw new UserException($message);
        }
        if (record_exists('group', 'shortname', $data['shortname'], 'institution', $data['institution'])) {
            throw new UserException('group_create: group with shortname ' . $data['shortname'] . ' and institution ' . $data['institution'] . ' already exists');
        }
        if (empty($data['members'])) {
            $data['members'] = array($USER->get('id') => 'admin');
        }
    } else {
        if (!empty($data['institution'])) {
            throw new SystemException("group_create: group institution only available for api-controlled groups");
        }
        $data['shortname'] = null;
    }
    if (get_config('cleanurls') && (!isset($data['urlid']) || strlen($data['urlid']) == 0)) {
        $data['urlid'] = generate_urlid($data['name'], get_config('cleanurlgroupdefault'), 3, 30);
        $data['urlid'] = group_get_new_homepage_urlid($data['urlid']);
    }
    if (!is_array($data['members']) || count($data['members']) == 0) {
        throw new InvalidArgumentException("group_create: at least one member must be specified for adding to the group");
    }
    if (!isset($data['submittableto'])) {
        $data['submittableto'] = $data['grouptype'] != 'standard';
    }
    if (!isset($data['editroles'])) {
        $data['editroles'] = $data['grouptype'] == 'standard' ? 'all' : 'notmember';
    } else {
        if (!in_array($data['editroles'], array_keys(group_get_editroles_options()))) {
            throw new InvalidArgumentException("group_create: invalid option for page editroles setting");
        }
    }
    if (!isset($data['editwindowstart'])) {
        $data['editwindowstart'] = null;
    }
    if (!isset($data['editwindowend'])) {
        $data['editwindowend'] = null;
    }
    if (!isset($data['sendnow'])) {
        $data['sendnow'] = null;
    }
    db_begin();
    $id = insert_record('group', (object) array('name' => $data['name'], 'description' => isset($data['description']) ? $data['description'] : null, 'urlid' => isset($data['urlid']) ? $data['urlid'] : null, 'grouptype' => $data['grouptype'], 'category' => isset($data['category']) ? intval($data['category']) : null, 'jointype' => $jointype, 'ctime' => $data['ctime'], 'mtime' => $data['ctime'], 'public' => $data['public'], 'usersautoadded' => $data['usersautoadded'], 'quota' => $data['quota'], 'institution' => !empty($data['institution']) ? $data['institution'] : null, 'shortname' => $data['shortname'], 'request' => isset($data['request']) ? intval($data['request']) : 0, 'submittableto' => intval($data['submittableto']), 'allowarchives' => !empty($data['submittableto']) && !empty($data['allowarchives']) ? intval($data['allowarchives']) : 0, 'editroles' => $data['editroles'], 'hidden' => $data['hidden'], 'hidemembers' => $data['hidemembers'], 'hidemembersfrommembers' => $data['hidemembersfrommembers'], 'groupparticipationreports' => $data['groupparticipationreports'], 'invitefriends' => $data['invitefriends'], 'suggestfriends' => $data['suggestfriends'], 'editwindowstart' => $data['editwindowstart'], 'editwindowend' => $data['editwindowend'], 'sendnow' => isset($data['sendnow']) ? $data['sendnow'] : null, 'viewnotify' => isset($data['viewnotify']) ? $data['viewnotify'] : null, 'feedbacknotify' => isset($data['feedbacknotify']) ? $data['feedbacknotify'] : null), 'id', true);
    foreach ($data['members'] as $userid => $role) {
        insert_record('group_member', (object) array('group' => $id, 'member' => $userid, 'role' => $role, 'ctime' => $data['ctime']));
    }
    // Copy views for the new group
    $templates = get_column('view_autocreate_grouptype', 'view', 'grouptype', $data['grouptype']);
    $templates = get_records_sql_array("\n        SELECT v.id, v.title, v.description\n        FROM {view} v\n        INNER JOIN {view_autocreate_grouptype} vag ON vag.view = v.id\n        LEFT JOIN {collection_view} cv ON v.id = cv.view\n        WHERE vag.grouptype = 'standard'\n            AND cv.view IS NULL", array());
    if ($templates) {
        require_once get_config('libroot') . 'view.php';
        foreach ($templates as $template) {
            list($view) = View::create_from_template(array('group' => $id, 'title' => $template->title, 'description' => $template->description), $template->id, null, false);
            $view->set_access(array(array('type' => 'group', 'id' => $id, 'startdate' => null, 'stopdate' => null, 'role' => null)));
        }
    }
    // Copy collections for the new group
    $templates = get_records_sql_array("\n        SELECT DISTINCT c.id, c.name\n        FROM {view} v\n        INNER JOIN {view_autocreate_grouptype} vag ON vag.view = v.id\n        INNER JOIN {collection_view} cv ON v.id = cv.view\n        INNER JOIN {collection} c ON cv.collection = c.id\n        WHERE vag.grouptype = ?", array($data['grouptype']));
    if ($templates) {
        require_once 'collection.php';
        foreach ($templates as $template) {
            Collection::create_from_template(array('group' => $id), $template->id, null, false, true);
        }
    }
    $data['id'] = $id;
    // install the homepage
    if ($t = get_record('view', 'type', 'grouphomepage', 'template', 1, 'owner', 0)) {
        require_once 'view.php';
        $template = new View($t->id, (array) $t);
        list($homepage) = View::create_from_template(array('group' => $id, 'title' => $template->get('title'), 'description' => $template->get('description'), 'type' => 'grouphomepage'), $t->id, 0, false);
    }
    insert_record('view_access', (object) array('view' => $homepage->get('id'), 'accesstype' => $data['public'] ? 'public' : 'loggedin', 'ctime' => db_format_timestamp(time())));
    handle_event('creategroup', $data);
    db_commit();
    return $id;
}
Example #3
0
/**
 * Copy a view via a 'copy' url
 * Currently for copying a page via a 'copy' button on view/view.php
 *
 * @param integer $id           View id
 * @param bool $istemplate      (optional) If you want to mark as template
 * @param integer $groupid      (optional) The group to copy the view to
 * @param integer $collectionid (optional) Provide the collection id to indicate we want
 *                                         to copy collection the view belongs to
 */
function copyview($id, $istemplate = false, $groupid = null, $collectionid = null)
{
    global $USER, $SESSION;
    // check that the user can copy view
    $view = new View($id);
    if (!$view->is_copyable()) {
        throw new AccessDeniedException(get_string('thisviewmaynotbecopied', 'view'));
    }
    // set up a packet of values to send to the create_from_template function
    $values = array('new' => 1, 'owner' => $USER->get('id'), 'template' => (int) $istemplate);
    if (!empty($groupid) && is_int($groupid)) {
        $values['group'] = $groupid;
    }
    if (!empty($collectionid)) {
        require_once get_config('libroot') . 'collection.php';
        list($collection, $template, $copystatus) = Collection::create_from_template($values, $collectionid);
        if (isset($copystatus['quotaexceeded'])) {
            $SESSION->add_error_msg(get_string('collectioncopywouldexceedquota', 'collection'));
            redirect(get_config('wwwroot') . 'view/view.php?id=' . $id);
        }
        $SESSION->add_ok_msg(get_string('copiedpagesblocksandartefactsfromtemplate', 'collection', $copystatus['pages'], $copystatus['blocks'], $copystatus['artefacts'], $template->get('name')));
        redirect(get_config('wwwroot') . 'collection/edit.php?copy=1&id=' . $collection->get('id'));
    } else {
        list($view, $template, $copystatus) = View::create_from_template($values, $id);
        if (isset($copystatus['quotaexceeded'])) {
            $SESSION->add_error_msg(get_string('viewcopywouldexceedquota', 'view'));
            redirect(get_config('wwwroot') . 'view/view.php?id=' . $id);
        }
        $SESSION->add_ok_msg(get_string('copiedblocksandartefactsfromtemplate', 'view', $copystatus['blocks'], $copystatus['artefacts'], $template->get('title')));
        redirect(get_config('wwwroot') . 'view/edit.php?new=1&id=' . $view->get('id'));
    }
}
Example #4
0
function createview_submit(Pieform $form, $values)
{
    global $SESSION;
    $values['template'] = !empty($values['istemplate']) ? 1 : 0;
    // Named 'istemplate' in the form to prevent confusion with 'usetemplate'
    if (!empty($values['submitcollection'])) {
        require_once get_config('libroot') . 'collection.php';
        $templateid = $values['copycollection'];
        unset($values['copycollection']);
        unset($values['usetemplate']);
        list($collection, $template, $copystatus) = Collection::create_from_template($values, $templateid);
        if (isset($copystatus['quotaexceeded'])) {
            $SESSION->add_error_msg(get_string('collectioncopywouldexceedquota', 'collection'));
            redirect(get_config('wwwroot') . 'view/choosetemplate.php');
        }
        $SESSION->add_ok_msg(get_string('copiedpagesblocksandartefactsfromtemplate', 'collection', $copystatus['pages'], $copystatus['blocks'], $copystatus['artefacts'], $template->get('name')));
        redirect(get_config('wwwroot') . 'collection/edit.php?copy=1&id=' . $collection->get('id'));
    } else {
        if (isset($values['usetemplate'])) {
            $templateid = $values['usetemplate'];
            unset($values['usetemplate']);
            list($view, $template, $copystatus) = View::create_from_template($values, $templateid);
            if (isset($copystatus['quotaexceeded'])) {
                $SESSION->add_error_msg(get_string('viewcopywouldexceedquota', 'view'));
                redirect(get_config('wwwroot') . 'view/choosetemplate.php');
            }
            $SESSION->add_ok_msg(get_string('copiedblocksandartefactsfromtemplate', 'view', $copystatus['blocks'], $copystatus['artefacts'], $template->get('title')));
        } else {
            $view = View::create($values);
        }
    }
    redirect(get_config('wwwroot') . 'view/edit.php?new=1&id=' . $view->get('id'));
}