/** * Performs track_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 track_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::track_enrolment_delete_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Parse track. if (empty($data->track_idnumber) || !($trackid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->track_idnumber)))) { throw new data_object_exception('ws_track_enrolment_delete_fail_invalid_track', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:track_enrol', \local_elisprogram\context\track::instance($trackid)); // 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_track_enrolment_delete_fail_invalid_user', 'local_datahub', '', $a); } $retval = $DB->get_record(usertrack::TABLE, array('userid' => $userid, 'trackid' => $trackid)); // Respond. if (!empty($retval->id)) { $usertrack = new usertrack($retval->id); $usertrack->unenrol(); return array('messagecode' => get_string('ws_track_enrolment_delete_success_code', 'local_datahub'), 'message' => get_string('ws_track_enrolment_delete_success_msg', 'local_datahub')); } else { throw new data_object_exception('ws_track_enrolment_delete_fail', 'local_datahub'); } }
/** * Disassociates a cluster from a track. */ public function delete() { global $CURMAN; $return = $this->data_delete_record(); if ($return && $this->autounenrol) { $db = $CURMAN->db; // Unenrol all users in the cluster from the track (unless they are // in another cluster associated with the track and autoenrolled by // that cluster). Only work on users that were autoenrolled in the // track by the cluster. // $filter selects all users enrolled in the track due to being in // a(nother) cluster associated with the track. We will left-join // with it, and only select non-matching records. $filter = 'SELECT u.userid ' . 'FROM ' . $db->prefix_table(CLSTUSERTABLE) . ' u ' . 'INNER JOIN ' . $db->prefix_table(USRTRKTABLE) . ' ut ON u.userid = ut.userid ' . 'WHERE ut.trackid = \'' . $this->trackid . '\' AND u.autoenrol=\'1\''; $sql = 'SELECT usrtrk.id ' . 'FROM ' . $db->prefix_table(CLSTUSERTABLE) . ' cu ' . 'INNER JOIN ' . $db->prefix_table(USRTRKTABLE) . ' usrtrk ON cu.userid = usrtrk.userid AND usrtrk.trackid = \'' . $this->trackid . '\' ' . 'LEFT OUTER JOIN (' . $filter . ') f ON f.userid = cu.userid ' . 'WHERE cu.clusterid = \'' . $this->clusterid . '\' AND cu.autoenrol=\'1\' AND f.userid IS NULL'; $usertracks = $db->get_records_sql($sql); if ($usertracks) { foreach ($usertracks as $usertrack) { $ut = new usertrack($usertrack->id); $ut->unenrol(); } } } return $return; }
/** * Disassociates a cluster from a track. */ public function delete() { if ($this->autounenrol) { // ELIS-7582 @set_time_limit(0); // Unenrol all users in the cluster from the track (unless they are // in another cluster associated with the track and autoenrolled by // that cluster). Only work on users that were autoenrolled in the // track by the cluster. // $filter selects all users enrolled in the track due to being in // a(nother) cluster associated with the track. We will left-join // with it, and only select non-matching records. $params = array(); $filter = 'SELECT u.userid ' . 'FROM {' . clusterassignment::TABLE . '} u ' . 'INNER JOIN {' . usertrack::TABLE . '} ut ON u.userid = ut.userid ' . 'WHERE ut.trackid = :trackid AND u.autoenrol=\'1\''; $params['trackid'] = $this->trackid; $sql = 'SELECT usrtrk.id ' . 'FROM {' . clusterassignment::TABLE . '} cu ' . 'INNER JOIN {' . usertrack::TABLE . '} usrtrk ON cu.userid = usrtrk.userid AND usrtrk.trackid = \'' . $this->trackid . '\' ' . 'LEFT OUTER JOIN (' . $filter . ') f ON f.userid = cu.userid ' . 'WHERE cu.clusterid = :clusterid AND cu.autoenrol=\'1\' AND f.userid IS NULL'; $params['clusterid'] = $this->clusterid; $usertracks = $this->_db->get_recordset_sql($sql, $params); foreach ($usertracks as $usertrack) { $ut = new usertrack($usertrack->id); $ut->unenrol(); } unset($usertracks); } parent::delete(); }