<commit_before><?php
/**
 * OpenEyes
 *
 * (C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011
 * (C) OpenEyes Foundation, 2011-2012
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust
 * @copyright Copyright (c) 2011-2012, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

/*
 * This controller class provides functionality to load elements via ajax, and supports the parent/child relationship between event elements to allow them to be displayed in a nested format.
 * 
 * It also adheres to a more dyanmic convention for view files. it will look for the $action_[elementclassname] files, but if they do not exist, it will render using the form_[elementclassname] files 
 * (i.e. allowing the same form to be used for create and update). 
 */

class NestedElementsEventTypeController extends BaseEventTypeController {
	
	protected function beforeAction($action)
	{
		$res = parent::beforeAction($action);
		
		if (!Yii::app()->getRequest()->getIsAjaxRequest()) {
			Yii::app()->getClientScript()->registerScript('nestedElementJS', 'var moduleName = "' . $this->getModule()->name . '";', CClientScript::POS_HEAD);
			Yii::app()->getClientScript()->registerCssFile(Yii::app()->createUrl('css/nested_elements.css'));
			Yii::app()->getClientScript()->registerScriptFile(Yii::app()->createUrl('js/nested_elements.js'));
		}
		
		return $res;
	}
	
	/*
	 * abstraction of element initialisation to allow custom extension in overrides of controller
	 */
	protected function getElementForElementForm($element_type, $import_previous) {
		$element_class = $element_type->class_name;
		$element = new $element_class;
		$element->setDefaultOptions();
		if($import_previous && $element->canCopy() && $episode = $this->episode) {
			$previous_element = $this->getPreviousElement($element_class, $episode);
			$element->loadFromExisting($previous_element);
		}
		return $element;
	}
	
	/**
	 * Can an element can be copied?
	 * @param string $element_class
	 * @return boolean
	 */
	public function canCopy($element_class) {
		if($episode = $this->episode) {
			return ($element_class::model()->canCopy() && $this->getPreviousElement($element_class, $episode));
		} else {
			return false;
		}
	}
	
	/**
	 * Fetches the latest instance of an element in an episode
	 * @param string $element_class
	 * @param Episode $episode
	 * @return BaseEventTypeElement
	 */
	protected function getPreviousElement($element_class, $episode) {
		$episode_id = $episode->id;
		$criteria = new CDbCriteria();
		$criteria->condition = 'event.episode_id = :episode_id';
		$criteria->params = array(':episode_id' => $episode_id);
		$criteria->order = 't.id DESC';
		$criteria->join = 'JOIN event ON event.id = t.event_id';
		$element = $element_class::model()->find($criteria);
		return $element;
	}
	
	/*
	 * Ajax method for loading an individual element (and its children)
	 */
	public function actionElementForm($id, $patient_id, $import_previous) {
		// first prevent invalid requests
		$element_type = ElementType::model()->findByPk($id);
		if(!$element_type) {
			throw new CHttpException(404, 'Unknown ElementType');
		}
		$patient = Patient::model()->findByPk($patient_id);<commit_msg>Change name of getPreviousElement method<commit_after><?php
/**
 * OpenEyes
 *
 * (C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011
 * (C) OpenEyes Foundation, 2011-2012
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust
 * @copyright Copyright (c) 2011-2012, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

/*
 * This controller class provides functionality to load elements via ajax, and supports the parent/child relationship between event elements to allow them to be displayed in a nested format.
 * 
 * It also adheres to a more dyanmic convention for view files. it will look for the $action_[elementclassname] files, but if they do not exist, it will render using the form_[elementclassname] files 
 * (i.e. allowing the same form to be used for create and update). 
 */

class NestedElementsEventTypeController extends BaseEventTypeController {
	
	protected function beforeAction($action)
	{
		$res = parent::beforeAction($action);
		
		if (!Yii::app()->getRequest()->getIsAjaxRequest()) {
			Yii::app()->getClientScript()->registerScript('nestedElementJS', 'var moduleName = "' . $this->getModule()->name . '";', CClientScript::POS_HEAD);
			Yii::app()->getClientScript()->registerCssFile(Yii::app()->createUrl('css/nested_elements.css'));
			Yii::app()->getClientScript()->registerScriptFile(Yii::app()->createUrl('js/nested_elements.js'));
		}
		
		return $res;
	}
	
	/*
	 * abstraction of element initialisation to allow custom extension in overrides of controller
	 */
	protected function getElementForElementForm($element_type, $import_previous) {
		$element_class = $element_type->class_name;
		$element = new $element_class;
		$element->setDefaultOptions();
		if($import_previous && $element->canCopy() && $episode = $this->episode) {
			$previous_element = $this->getLatestElement($element_class, $episode);
			$element->loadFromExisting($previous_element);
		}
		return $element;
	}
	
	/**
	 * Can an element can be copied?
	 * @param string $element_class
	 * @return boolean
	 */
	public function canCopy($element_class) {
		if($episode = $this->episode) {
			return ($element_class::model()->canCopy() && $this->getLatestElement($element_class, $episode));
		} else {
			return false;
		}
	}
	
	/**
	 * Fetches the latest instance of an element in an episode
	 * @param string $element_class
	 * @param Episode $episode
	 * @return BaseEventTypeElement
	 */
	protected function getLatestElement($element_class, $episode) {
		$episode_id = $episode->id;
		$criteria = new CDbCriteria();
		$criteria->condition = 'event.episode_id = :episode_id';
		$criteria->params = array(':episode_id' => $episode_id);
		$criteria->order = 't.id DESC';
		$criteria->join = 'JOIN event ON event.id = t.event_id';
		$element = $element_class::model()->find($criteria);
		return $element;
	}
	
	/*
	 * Ajax method for loading an individual element (and its children)
	 */
	public function actionElementForm($id, $patient_id, $import_previous) {
		// first prevent invalid requests
		$element_type = ElementType::model()->findByPk($id);
		if(!$element_type) {
			throw new CHttpException(404, 'Unknown ElementType');
		}
		$patient = Patient::model()->findByPk($patient_id);
{'fraction_non_alphanumeric': 0.12259585556520661, 'fraction_numerical': 0.009926789924308227, 'mean_word_length': 4.672061928219564, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5561349', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2297, 'n_words': 749}