Fetches a random MFA question for a user

@param  int $iUserId The user's ID

@return boolean|\stdClass
431079
public function mfaQuestionGet($iUserId)
    {
        $oDb    = Factory::service('Database');
        $oInput = Factory::service('Input');

        $oDb->where('user_id', $iUserId);
        $oDb->order_by('last_requested', 'DESC');
        $aQuestions = $oDb->get(NAILS_DB_PREFIX . 'user_auth_two_factor_question')->result();

        if (!$aQuestions) {
            $this->setError('No security questions available for this user.');
            return false;
        }

        // --------------------------------------------------------------------------

        //  Choose a question to return
        if (count($aQuestions) == 1) {

            //  No choice, just return the lonely question
            $oOut = reset($aQuestions);

        } elseif (count($aQuestions) > 1) {

            /**
             * Has the most recently asked question been asked in the last 10 minutes?
             * If so, return that one again (to make harvesting all the user's questions
             * a little more time consuming). If not randomly choose one.
             */

            $oOut = reset($aQuestions);
            if (strtotime($oOut->last_requested) < strtotime('-10 MINS')) {
                $oOut = $aQuestions[array_rand($aQuestions)];
            }

        } else {
            $this->setError('Could not determine security question.');
            return false;
        }

        //  Decode the question
        $oEncrypt       = Factory::service('Encrypt');
        $oOut->question = $oEncrypt->decode($oOut->question, APP_PRIVATE_KEY . $oOut->salt);

        //  Update the last requested details
        $oDb->set('last_requested', 'NOW()', false);
        $oDb->set('last_requested_ip', $oInput->ipAddress());
        $oDb->where('id', $oOut->id);
        $oDb->update(NAILS_DB_PREFIX . 'user_auth_two_factor_question');

        return $oOut;
    }
19493
public function getRulesAsString($iGroupId)
    {
        $aRules = $this->getRulesAsArray($iGroupId);

        if (empty($aRules)) {
            return '';
        }

        $sStr = 'Passwords must ' . strtolower(implode(', ', $aRules)) . '.';
        return str_lreplace(', ', ' and ', $sStr);
    }
748384
public static function get_contexts_for_userid(int $userid) : contextlist {
        $sql = "SELECT ctx.id
                  FROM {context} ctx
            INNER JOIN {cohort} c ON c.contextid = ctx.id
            INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
                 WHERE cm.userid = :userid AND (ctx.contextlevel = :contextlevel1 OR ctx.contextlevel = :contextlevel2)";
        $params = [
            'userid'        => $userid,
            'contextlevel1' => CONTEXT_SYSTEM,
            'contextlevel2' => CONTEXT_COURSECAT,
        ];
        $contextlist = new contextlist();
        $contextlist->add_from_sql($sql, $params);

        return $contextlist;
    }
437438
static function fetchListForUser( $userID )
    {
        return eZPersistentObject::fetchObjectList( eZContentBrowseRecent::definition(),
                                                    null, array( 'user_id' => $userID ),
                                                    array( 'created' => 'desc' ), null, true );
    }
543524
public function get_user($userid) {
        global $DB;

        if (empty(self::$cachedusers[$userid])) {
            $fields = get_all_user_name_fields(true);
            self::$cachedusers[$userid] = $DB->get_record('user', array('id' => $userid), 'id, ' . $fields);
        }
        return self::$cachedusers[$userid];
    }
131142
protected function _getUserEntity($userId) {
		if (is_a($userId, EntityInterface::class)) {
			return $userId;
		}

		if (is_string($userId) || is_integer($userId)) {
			$entity = $this->UserTable->newEntity();

			return $this->UserTable->patchEntity(
				$entity,
				[$this->UserTable->primaryKey() => $userId],
				['guard' => false]
			);
		}

		if (is_array($userId)) {
			$entity = $this->UserTable->newEntity();

			return $this->UserTable->patchEntity(
				$entity,
				$userId,
				['guard' => false]
			);
		}
	}
734366
protected function fetchUserData($userId)
    {
        $query = $this->dbHandler->createSelectQuery();
        $query
            ->select(
                $this->dbHandler->quoteColumn('contentobject_id', 'ezuser'),
                $this->dbHandler->quoteColumn('login', 'ezuser'),
                $this->dbHandler->quoteColumn('email', 'ezuser'),
                $this->dbHandler->quoteColumn('password_hash', 'ezuser'),
                $this->dbHandler->quoteColumn('password_hash_type', 'ezuser')
            )
            ->from($this->dbHandler->quoteTable('ezuser'))
            ->where(
                $query->expr->eq(
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezuser'),
                    $query->bindValue($userId)
                )
            );

        $stmt = $query->prepare();
        $stmt->execute();

        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        return isset($rows[0]) ? $this->convertColumnsToProperties($rows[0]) : array();
    }
303661
public static function can_read_user_in_course($userid, $courseid) {
        $capability = 'moodle/competency:usercompetencyview';
        return has_capability($capability, context_course::instance($courseid))
            || static::can_read_user($userid);
    }
236804
public static function is_user_in_conversation(int $userid, int $conversationid) : bool {
        global $DB;

        return $DB->record_exists('message_conversation_members', ['conversationid' => $conversationid,
            'userid' => $userid]);
    }
399766
public function fetch_grade_or_default($item, $userid) {
        $grade = grade_grade::fetch(array(
            'itemid' => $item->id, 'userid' => $userid
        ));

        if (!$grade) {
            $default = new stdClass;

            $default->userid = $userid;
            $default->itemid = $item->id;
            $default->feedback = '';

            $grade = new grade_grade($default, false);
        }

        $grade->grade_item = $item;

        return $grade;
    }
765255
public static function get_conversation_between_users(array $userids) {
        global $DB;

        $conversations = self::get_individual_conversations_between_users([$userids]);
        $conversation = $conversations[0];

        if ($conversation) {
            return $conversation->id;
        }

        return false;
    }
501283
public static function convertUserIdToVisitorIdBin($userId)
    {
        require_once PIWIK_INCLUDE_PATH . '/libs/PiwikTracker/PiwikTracker.php';
        $userIdHashed = \PiwikTracker::getUserIdHashed($userId);

        return self::convertVisitorIdToBin($userIdHashed);
    }
205073
protected function get_graders($userid) {
        // Potential graders should be active users only.
        $potentialgraders = get_enrolled_users($this->context, "mod/assign:grade", null, 'u.*', null, null, null, true);

        $graders = array();
        if (groups_get_activity_groupmode($this->get_course_module()) == SEPARATEGROUPS) {
            if ($groups = groups_get_all_groups($this->get_course()->id, $userid, $this->get_course_module()->groupingid)) {
                foreach ($groups as $group) {
                    foreach ($potentialgraders as $grader) {
                        if ($grader->id == $userid) {
                            // Do not send self.
                            continue;
                        }
                        if (groups_is_member($group->id, $grader->id)) {
                            $graders[$grader->id] = $grader;
                        }
                    }
                }
            } else {
                // User not in group, try to find graders without group.
                foreach ($potentialgraders as $grader) {
                    if ($grader->id == $userid) {
                        // Do not send self.
                        continue;
                    }
                    if (!groups_has_membership($this->get_course_module(), $grader->id)) {
                        $graders[$grader->id] = $grader;
                    }
                }
            }
        } else {
            foreach ($potentialgraders as $grader) {
                if ($grader->id == $userid) {
                    // Do not send self.
                    continue;
                }
                // Must be enrolled.
                if (is_enrolled($this->get_course_context(), $grader->id)) {
                    $graders[$grader->id] = $grader;
                }
            }
        }
        return $graders;
    }
1261476
public function isExpired($iUserId)
    {
        if (empty($iUserId)) {
            return false;
        }

        $oDb = Factory::service('Database');
        $oDb->select('u.password_changed,ug.password_rules');
        $oDb->where('u.id', $iUserId);
        $oDb->join(NAILS_DB_PREFIX . 'user_group ug', 'ug.id = u.group_id');
        $oDb->limit(1);
        $oResult = $oDb->get(NAILS_DB_PREFIX . 'user u');

        if ($oResult->num_rows() !== 1) {
            return false;
        }

        //  Decode the password rules
        $oGroupPwRules = json_decode($oResult->row()->password_rules);

        if (empty($oGroupPwRules->expiresAfter)) {
            return false;
        }

        $sChanged = $oResult->row()->password_changed;

        if (is_null($sChanged)) {

            return true;

        } else {

            $oThen     = new \DateTime($sChanged);
            $oNow      = new \DateTime();
            $oInterval = $oNow->diff($oThen);

            return $oInterval->days >= $oGroupPwRules->expiresAfter;
        }
    }
1052960
public function getIdCardPath($userId)
    {
        $avatarPath = $this->getSetting('idCardPath') . '/' . $this->getIdSavePath($userId);
        if (!is_dir($avatarPath)) {
            FileHelper::createDirectory($avatarPath);
        }
        return $avatarPath . substr($userId, -2);
    }
556758
public function getSpecificQuestion($questionId)
    {
        $configuration = $this->getUserConfiguration();
        $questions = $configuration->getQuestions();

        if (!is_null($questionId) &&
            isset($questions[$questionId]) &&
            !is_null($this->getUserConfiguration()->getTimeout())
        ) {
            $this->setQuestion($questionId);

            return true;
        }

        return false;
    }
137728
public static function get_contact_requests_between_users(int $userid, int $requesteduserid) : array {
        global $DB;

        $sql = "SELECT *
                  FROM {message_contact_requests} mcr
                 WHERE (mcr.userid = ? AND mcr.requesteduserid = ?)
                    OR (mcr.userid = ? AND mcr.requesteduserid = ?)";
        return $DB->get_records_sql($sql, [$userid, $requesteduserid, $requesteduserid, $userid]);
    }