<commit_before><?
defined('C5_EXECUTE') or die("Access Denied.");
class Concrete5_Model_Conversation_Message_List_Threaded extends ItemList {

	protected $sortBy = 'date';
	protected $sortByDirection = 'asc';
	protected $cnvID;

	public function __construct(Conversation $cnv) {
		$this->cnvID = $cnv->getConversationID();
		$this->populateMessages();
	}

	public function sortByDateDescending() {
		$this->sortBy('date', 'desc');
	}
	
	public function sortByDateAscending() {
		$this->sortBy('date', 'asc');
	}
	
	public function sortByRating() {
		$this->sortBy('date', 'asc');
	}
	
	public function get($num = 0, $offset = 0) {
		usort($this->items, array($this, 'sortItems'));
		return parent::get($num, $offset);
	}

	protected function populateMessages($cnvMessageParentID = 0) {
		$db = Loader::db();
		$v = array($this->cnvID, $cnvMessageParentID);
		$r = $db->Execute('select cnvMessageID from ConversationMessages where cnvID = ? and cnvMessageParentID = ?', $v);
		while ($row = $r->FetchRow()) {
			$msg = ConversationMessage::getByID($row['cnvMessageID']);
			if (is_object($msg)) {
				$this->items[] = $msg;
				$this->populateMessages($msg->getConversationMessageID());
			}
		}
	}

	protected function sortItems($a, $b) {
		if ($this->sortBy == 'date') {
			$atime = strtotime($a->getConversationMessageDateTime());
			$btime = strtotime($b->getConversationMessageDateTime());
			if ($this->sortByDirection == 'asc') {
				if ($atime > $btime) {
					return 1;
				} else if ($atime < $btime) {
					return -1;
				} else {
					return 0;
				}<commit_msg>fix threaded messages<commit_after><?
defined('C5_EXECUTE') or die("Access Denied.");
class Concrete5_Model_Conversation_Message_List_Threaded extends ItemList {

	protected $sortBy = 'date';
	protected $sortByDirection = 'asc';
	protected $cnvID;

	public function __construct(Conversation $cnv) {
		$this->cnvID = $cnv->getConversationID();
		$this->messages = $this->getMessages();
	}

	public function sortByDateDescending() {
		$this->sortBy('date', 'desc');
	}
	
	public function sortByDateAscending() {
		$this->sortBy('date', 'asc');
	}
	
	public function sortByRating() {
		$this->sortBy('date', 'asc');
	}
	
	public function get($num = 0, $offset = 0) {
		$messages = $this->sortThreadedArrays($this->messages);
		// now we turn the threaded messages array into the flat items array
		$this->flattenMessages($messages);
		return parent::get($num, $offset);
	}

	protected function sortThreadedArrays($messages) {
		usort($messages, array($this, 'sortItems'));
		foreach($messages as $m) {
			if (is_array($m->messages) && count($m->messages)) {
				$m->messages = $this->sortThreadedArrays($m->messages);
			}
		}
		return $messages;
	}

	protected function flattenMessages($messages) {
		foreach($messages as $m) {
			$this->items[] = $m;
			if (is_array($m->messages) && count($m->messages)) {
				$this->flattenMessages($m->messages);
			}
		}
	}

	protected function getMessages($cnvMessageParentID = 0) {
		$db = Loader::db();
		$v = array($this->cnvID, $cnvMessageParentID);
		$r = $db->Execute('select cnvMessageID from ConversationMessages where cnvID = ? and cnvMessageParentID = ?', $v);
		$messages = array();
		while ($row = $r->FetchRow()) {
			$msg = ConversationMessage::getByID($row['cnvMessageID']);
			if (is_object($msg)) {
				$msg->messages = $this->getMessages($msg->getConversationMessageID());
				$messages[] = $msg;
			}
		}
		return $messages;
	}

	protected function sortItems($a, $b) {
		if ($this->sortBy == 'date') {
			$atime = strtotime($a->getConversationMessageDateTime());
			$btime = strtotime($b->getConversationMessageDateTime());
			if ($this->sortByDirection == 'asc') {
				if ($atime > $btime) {
					return 1;
				} else if ($atime < $btime) {
					return -1;
				} else {
					return 0;
				}
{'fraction_non_alphanumeric': 0.17617801047120418, 'fraction_numerical': 0.004188481675392671, 'mean_word_length': 4.443019943019943, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5628238', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1286, 'n_words': 264}