Compare word count of onlinetext submission to word limit, and return result.

@param string $submissiontext Onlinetext submission text from editor
@return string Error message if limit is enabled and exceeded, otherwise null
180470
public function check_word_count($submissiontext) {
        global $OUTPUT;

        $wordlimitenabled = $this->get_config('wordlimitenabled');
        $wordlimit = $this->get_config('wordlimit');

        if ($wordlimitenabled == 0) {
            return null;
        }

        // Count words and compare to limit.
        $wordcount = count_words($submissiontext);
        if ($wordcount <= $wordlimit) {
            return null;
        } else {
            $errormsg = get_string('wordlimitexceeded', 'assignsubmission_onlinetext',
                    array('limit' => $wordlimit, 'count' => $wordcount));
            return $OUTPUT->error_text($errormsg);
        }
    }
888623
public function findEmailsToSend($limit = null)
    {
        $qb = $this->createQueryBuilder('se')
            ->where('se.status = :status AND (se.sentAt IS NULL OR se.sentAt <= :sentAt)')
            ->orderBy('se.sentAt', 'ASC')
            ->setParameter('status', SpoolEmailStatus::STATUS_WAITING)
            ->setParameter('sentAt', new \DateTime())
        ;

        if ($limit > 0) {
            $qb->setMaxResults($limit);
        }

        return $qb->getQuery()->getResult();
    }
1366743
function getSubmissionName($submissionTicket) {
		$submission = $this->getSubmission($submissionTicket);
		if (isset($submission)) {
			return $submission->submissionInfo->name;
		} else {
			throw new Exception("Invalid submission ticket");
		}
	}
1241738
public function wordCount($strText = false)
    {
        $strText = $this->setText($strText);

        return Text::wordCount($strText, $this->strEncoding);
    }
1223476
public function submitText(string $submitText)
    {
        /** @var Button $btn */
        $btn = $this->getButton($this->bottomLocation, $this->submitButtonKey);
        $btn->text($submitText);

        return $this;
    }
1233149
public function getMessageLines($text)
    {
        $message = $text;
        $wrapLength = 40;
        // wrap the message to max chars
        $message = wordwrap($message, $wrapLength - 2);
        // split into array of lines
        return explode("\n", $message);
    }
635847
public static int word_count(EvaluationContext ctx, Object text, @BooleanDefault(false) Object bySpaces) {
        String _text = Conversions.toString(text, ctx);
        boolean _bySpaces = Conversions.toBoolean(bySpaces, ctx);
        return getWords(_text, _bySpaces).size();
    }
554851
public static function getWordsWithinCharLimit($sentence, $limit = 150) {
        $str = '';
        $i = 1;

        if(strlen($sentence) < $limit) {
            return $sentence;
        }

        while (strlen($current = self::getWords($sentence, $i++)) < $limit) {
            $str = $current;
        }

        return $str;
    }
1039205
public function waitForVisibleText($text)
    {
        $this->waitFor(function ($context) use ($text) {
            try {
                /** @var Web $context */
                $context->assertPageContainsText($text);
                return true;
            } catch (ResponseTextException $e) {

            }
        });
    }
575651
function getSubmissionId($submissionTicket) {
		$submission = $this->getSubmission($submissionTicket);
		if (isset($submission)) {
			return $submission->submissionId;
		} else {
			throw new Exception("Invalid submission ticket");
		}
	}
653466
private static function countCode($text)
    {
        // remove code attribute content, like from href="" or d=""
        $text = preg_replace('/"[^"]*"/i', '', $text);
        // get markdown code
        $regex = '/(?<=(?<!`))`([^`\n\r]+)`(?=(?!`))|```[a-zA-Z]*([^`]*)```/i';
        $markdownCount = preg_match_all($regex, $text, $markdownMatches, PREG_PATTERN_ORDER);
        // Remove markdown code from text, as to not double count
        $text = trim(preg_replace('/(?<=(?<!`))`[^`\n\r]+`(?=(?!`))|```[a-zA-Z]*[^`]*```/i', ' ', $text));

        // get html code elements
        $regex = '/<code>([\s\S]*?)<\/code>/i';
        $htmlCount = preg_match_all($regex, $text, $htmlMatches, PREG_PATTERN_ORDER);

        // check if any matches exist
        if ($markdownCount === 0 && $htmlCount === 0) {
            return 0;
        }
        // concat all code
        $code = implode($markdownMatches[1], ' ').' '.implode($markdownMatches[2], ' ').' '.implode($htmlMatches[1], ' ');

        // replace multiple spaces
        $code = preg_replace(['/\s+/', '/^\s/'], [' ', ''], $code);

        // return the number words in the code blocks
        return count(array_filter(explode(' ', $code)));
    }
449566
public function compareLength($stringOne, $stringTwo) {
    $lengthOne = strlen($stringOne);
    $lengthTwo = strlen($stringTwo);
    if ($lengthOne > $lengthTwo) {
      return -1;
    } elseif ($lengthOne < $lengthTwo) {
      return 1;
    } else {
      return strcmp($stringOne, $stringTwo);
    }
  }
1149012
public static function wordCount($strText, $strEncoding = '')
    {
        if (strlen(trim($strText)) == 0) {
            return 0;
        }

        // Will be tripped by em dashes with spaces either side, among other similar characters
        $intWords = 1 + self::textLength(preg_replace('`[^ ]`', '', preg_replace('`\s+`', ' ', $strText)), $strEncoding); // Space count + 1 is word count

        return $intWords;
    }
919258
public function is_empty(stdClass $submission) {
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
        $wordcount = 0;

        if (isset($onlinetextsubmission->onlinetext)) {
            $wordcount = count_words(trim($onlinetextsubmission->onlinetext));
        }

        return $wordcount == 0;
    }
784954
public function sentenceCount($strText = false)
    {
        $strText = $this->setText($strText);

        return Text::sentenceCount($strText, $this->strEncoding);
    }
349265
private function getSubmittedVersionNumber(RequestInterface $request)
    {
        $accept  = $request->getHeader('Accept');
        $matches = array();

        preg_match('/^application\/vnd\.([a-z.-_]+)\.v([\d]+)\+([a-z]+)$/', $accept, $matches);

        return isset($matches[2]) ? $matches[2] : null;
    }
330202
public static function substringToWord($text, $maxLength, $suffix = '...')
    {
        $effect = $text;

        $textLength = mb_strlen($text, 'utf-8');
        $suffixLength = mb_strlen($suffix, 'utf-8');

        $maxLength -= $suffixLength;

        if ($textLength > $maxLength) {
            $effect = mb_substr($text, 0, $maxLength, 'utf-8');
            $lastSpacePosition = mb_strrpos($effect, ' ', 'utf-8');

            if (false !== $lastSpacePosition) {
                $effect = mb_substr($effect, 0, $lastSpacePosition, 'utf-8');
            }

            $effect .= $suffix;
        }

        return $effect;
    }