/**
  * Performs userset_enrolment deletion
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error deleting the association.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function userset_enrolment_delete(array $data)
 {
     global $DB, $USER;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::userset_enrolment_delete_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Parse Userset
     if (empty($data->userset_name) || !($clstid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->userset_name)))) {
         throw new data_object_exception('ws_userset_enrolment_delete_fail_invalid_userset', 'local_datahub', '', $data);
     }
     if (empty($data->plugin)) {
         $data->plugin = 'manual';
     }
     // Capability checking.
     require_capability('local/elisprogram:userset_enrol', \local_elisprogram\context\userset::instance($clstid));
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $userparams = array();
     $userid = $importplugin->get_userid_from_record($data, '', $userparams);
     if ($userid == false) {
         $a = new stdClass();
         if (empty($userparams)) {
             $a->userparams = '{empty}';
         } else {
             $a->userparams = '';
             foreach ($userparams as $userfield => $uservalue) {
                 $subfield = strpos($userfield, '_');
                 $userfield = substr($userfield, $subfield === false ? 0 : $subfield + 1);
                 if (!empty($a->userparams)) {
                     $a->userparams .= ', ';
                 }
                 $a->userparams .= "{$userfield}: '{$uservalue}'";
             }
         }
         throw new data_object_exception('ws_userset_enrolment_delete_fail_invalid_user', 'local_datahub', '', $a);
     }
     $id = $DB->get_field(clusterassignment::TABLE, 'id', array('clusterid' => $clstid, 'userid' => $userid, 'plugin' => $data->plugin));
     // Respond.
     if (!empty($id) && ($clstass = new clusterassignment($id))) {
         $clstass->delete();
         return array('messagecode' => get_string('ws_userset_enrolment_delete_success_code', 'local_datahub'), 'message' => get_string('ws_userset_enrolment_delete_success_msg', 'local_datahub'));
     } else {
         throw new data_object_exception('ws_userset_enrolment_delete_fail', 'local_datahub');
     }
 }
Ejemplo n.º 2
0
 /**
  * Unassign the usersets from the user.
  *
  * @param array $elements An array containing information on usersets to unassign from the user.
  * @param bool $bulkaction Whether this is a bulk-action or not.
  * @return array An array to format as JSON and return to the Javascript.
  */
 protected function _respond_to_js(array $elements, $bulkaction)
 {
     global $DB;
     $userid = required_param('id', PARAM_INT);
     // Permissions.
     $upage = new userpage();
     if ($upage->_has_capability('local/elisprogram:user_view', $userid) !== true) {
         return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram'));
     }
     foreach ($elements as $usersetid => $label) {
         if ($this->can_unassign($userid, $usersetid) === true) {
             $assignrec = $DB->get_record(clusterassignment::TABLE, array('userid' => $userid, 'clusterid' => $usersetid));
             if (!empty($assignrec) && $assignrec->plugin === 'manual') {
                 $usertrack = new clusterassignment($assignrec);
                 $usertrack->delete();
             }
         }
     }
     return array('result' => 'success', 'msg' => 'Success');
 }
Ejemplo n.º 3
0
function cluster_deassign_all_user($userid)
{
    if (!is_numeric($userid) || $userid <= 0) {
        return false;
    }
    return clusterassignment::delete(new field_filter('userid', $userid));
}
Ejemplo n.º 4
0
 /**
  * Delete a cluster (user set) enrolment
  *
  * @param object $record One record of import data
  * @param string $filename The import file name, used for logging
  * @param string $name The name of the cluster / user set
  *
  * @return boolean true on success, otherwise false
  */
 function cluster_enrolment_delete($record, $filename, $name)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/userset.class.php');
     if (!($clusterid = $DB->get_field(userset::TABLE, 'id', array('name' => $name)))) {
         $this->fslogger->log_failure("instance value of \"{$name}\" does not refer to a valid instance of a user set context.", 0, $filename, $this->linenumber, $record, "enrolment");
         return false;
     }
     $userid = $this->get_userid_from_record($record, $filename);
     //string to describe the user
     $user_descriptor = $this->get_user_descriptor($record, false, 'user_');
     if (!$DB->record_exists(clusterassignment::TABLE, array('clusterid' => $clusterid, 'userid' => $userid))) {
         $this->fslogger->log_failure("User with {$user_descriptor} is not enrolled in user set \"{$name}\".", 0, $filename, $this->linenumber, $record, "enrolment");
         return false;
     }
     if (!$this->validate_cluster_enrolment_data('delete', $record, $filename)) {
         return false;
     }
     //obtain the cluster / userset id
     $clusterid = $DB->get_field(userset::TABLE, 'id', array('name' => $name));
     //delete the association
     $clusterassignmentid = $DB->get_field(clusterassignment::TABLE, 'id', array('userid' => $userid, 'clusterid' => $clusterid, 'plugin' => 'manual'));
     $clusterassignment = new clusterassignment($clusterassignmentid);
     $clusterassignment->delete();
     //log success
     $success_message = "User with {$user_descriptor} successfully unenrolled from user set \"{$name}\".";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }
Ejemplo n.º 5
0
 /**
  * Unassign the user from the userset.
  *
  * @param array $elements An array of elements to perform the action on.
  * @param bool $bulkaction Whether this is a bulk-action or not.
  * @return array An array to format as JSON and return to the Javascript.
  */
 protected function _respond_to_js(array $elements, $bulkaction)
 {
     global $DB;
     $usersetid = required_param('id', PARAM_INT);
     $userset = new userset($usersetid);
     // Permissions.
     if (usersetpage::can_enrol_into_cluster($userset->id) !== true) {
         return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram'));
     }
     $unassignnotpermitted = false;
     foreach ($elements as $userid => $label) {
         if ($this->can_unassign($usersetid, $userid)) {
             $assignrec = $DB->get_record(clusterassignment::TABLE, array('userid' => $userid, 'clusterid' => $usersetid));
             if (!empty($assignrec) && $assignrec->plugin === 'manual') {
                 $curstu = new clusterassignment($assignrec);
                 $curstu->delete();
             }
         } else {
             $unassignnotpermitted = true;
         }
     }
     if ($unassignnotpermitted) {
         return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram'));
     } else {
         return array('result' => 'success', 'msg' => 'Success');
     }
 }