<commit_before><?php defined('BASEPATH') or exit('No direct script access allowed');

class Module_Polls extends Module {

	public $version = '0.5';

	public function info()
	{
		return array(
			'name' => array(
				'en' => 'Polls'
			),
			'description' => array(
				'en' => 'Create totally awesome polls.'
			),
			'frontend' => TRUE,
			'backend' => TRUE,
			'menu' => FALSE
		);
	}

	public function install()
	{
		// Create polls table
		$this->db->query("	
			CREATE TABLE IF NOT EXISTS `polls` (
			`id` tinyint(11) unsigned NOT NULL AUTO_INCREMENT,
			`slug` varchar(64) NOT NULL,
			`title` varchar(64) NOT NULL,
			`type` enum('single','multiple') NOT NULL DEFAULT 'single',
			`description` text,
			`open_date` int(16) unsigned DEFAULT NULL,
			`close_date` int(16) unsigned DEFAULT NULL,
			`created` int(16) unsigned NOT NULL,
			`last_updated` int(16) unsigned DEFAULT NULL,
			`comments_enabled` tinyint(1) NOT NULL DEFAULT '0',
			`members_only` tinyint(1) NOT NULL DEFAULT '0',
			PRIMARY KEY (`id`)
			) ENGINE=InnoDB DEFAULT CHARSET=latin1;
		");
		
		// Create poll_options table
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `poll_options` (
			`id` smallint(11) unsigned NOT NULL AUTO_INCREMENT,
			`poll_id` tinyint(11) unsigned NOT NULL,
			`type` enum('defined','other') NOT NULL DEFAULT 'defined',
			`title` varchar(64) NOT NULL,
			`order` tinyint(2) unsigned DEFAULT NULL,
			`votes` mediumint(11) unsigned NOT NULL DEFAULT '0',
			PRIMARY KEY (`id`),
			KEY `poll_id` (`poll_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
		");
		
		// Create poll_other_votes table
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `poll_other_votes` (
			`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
			`parent_id` smallint(11) unsigned NOT NULL,
			`text` tinytext NOT NULL,
			`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (`id`),
			KEY `parent_id` (`parent_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
		");
		
		// Create poll_voters table
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `poll_voters` (
			`id` mediumint(32) unsigned NOT NULL AUTO_INCREMENT,
			`poll_id` tinyint(11) unsigned NOT NULL,
			`user_id` smallint(5) unsigned DEFAULT NULL,
			`session_id` varchar(40) NOT NULL,
			`ip_address` varchar(16) NOT NULL,
			`timestamp` int(11) unsigned NOT NULL,
			PRIMARY KEY (`id`),
			KEY `poll_id` (`poll_id`),
			KEY `user_id` (`user_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
		");
		
		// Referental integrity fo' sho
		$this->db->query("
			ALTER TABLE `poll_options`
			ADD CONSTRAINT `poll_options_ibfk_1`
			FOREIGN KEY (`poll_id`)
			REFERENCES `polls` (`id`)
			ON DELETE CASCADE
			ON UPDATE CASCADE;
		");
		
		$this->db->query("
			ALTER TABLE `poll_other_votes`
			ADD CONSTRAINT `poll_other_votes_ibfk_1`
			FOREIGN KEY (`parent_id`)
			REFERENCES `poll_options` (`id`)
			ON DELETE CASCADE
			ON UPDATE CASCADE;
		");
		
		$this->db->query("
			ALTER TABLE `poll_voters`
			ADD CONSTRAINT `poll_votes_ibfk_1`
			FOREIGN KEY (`poll_id`)
			REFERENCES `polls` (`id`)
			ON DELETE CASCADE
			ON UPDATE CASCADE;
		");
		
		// It worked!
		return TRUE;
	}

	public function uninstall()
	{
		// Drop some tables
		$this->db->query("DROP TABLE `poll_voters`, `poll_other_votes`, `poll_options`, `polls`");
		return TRUE;
	}

	public function upgrade($old_version)
	{
		// Version 0.4 (the first official release)
		if ($old_version == '0.4')
		{
			// Update polls table
			$this->db->query("
				ALTER TABLE  `polls` ADD  `type` enum('single','multiple') NOT NULL DEFAULT 'single'
			");
			
			// Update poll_options table
			$this->db->query("
				ALTER TABLE  `poll_options` ADD  `type` enum('defined','other') NOT NULL DEFAULT 'defined'
			");
			
			// Add poll_other_votes table
			$this->db->query("
				CREATE TABLE IF NOT EXISTS `poll_other_votes` (
				`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
				`parent_id` smallint(11) unsigned NOT NULL,
				`text` tinytext NOT NULL,
				`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
				PRIMARY KEY (`id`),
				KEY `parent_id` (`parent_id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
			");
		
			// Add poll_voters table
			$this->db->query("
				CREATE TABLE IF NOT EXISTS `poll_voters` (
				`id` mediumint(32) unsigned NOT NULL AUTO_INCREMENT,
				`poll_id` tinyint(11) unsigned NOT NULL,
				`user_id` smallint(5) unsigned DEFAULT NULL,
				`session_id` varchar(40) NOT NULL,
				`ip_address` varchar(16) NOT NULL,
				`timestamp` int(11) unsigned NOT NULL,
				PRIMARY KEY (`id`),
				KEY `poll_id` (`poll_id`),
				KEY `user_id` (`user_id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
			");
			
			// Referental integrity fo' sho
			$this->db->query("
				ALTER TABLE `poll_other_votes`
				ADD CONSTRAINT `poll_other_votes_ibfk_1`
				FOREIGN KEY (`parent_id`)
				REFERENCES `poll_options` (`id`)
				ON DELETE CASCADE
				ON UPDATE CASCADE;
			");
			
			$this->db->query("
				ALTER TABLE `poll_voters`
				ADD CONSTRAINT `poll_votes_ibfk_1`
				FOREIGN KEY (`poll_id`)
				REFERENCES `polls` (`id`)
				ON DELETE CASCADE
				ON UPDATE CASCADE;
			");
		}
		return TRUE;
	}<commit_msg>Update SQL with multiple votes code<commit_after><?php defined('BASEPATH') or exit('No direct script access allowed');

class Module_Polls extends Module {

	public $version = '0.5';

	public function info()
	{
		return array(
			'name' => array(
				'en' => 'Polls'
			),
			'description' => array(
				'en' => 'Create totally awesome polls.'
			),
			'frontend' => TRUE,
			'backend' => TRUE,
			'menu' => FALSE
		);
	}

	public function install()
	{
		// Create polls table
		$this->db->query("	
			CREATE TABLE IF NOT EXISTS `polls` (
			`id` tinyint(11) unsigned NOT NULL AUTO_INCREMENT,
			`slug` varchar(64) NOT NULL,
			`title` varchar(64) NOT NULL,
			`type` enum('single','multiple') NOT NULL DEFAULT 'single',
			`description` text,
			`open_date` int(16) unsigned DEFAULT NULL,
			`close_date` int(16) unsigned DEFAULT NULL,
			`created` int(16) unsigned NOT NULL,
			`last_updated` int(16) unsigned DEFAULT NULL,
			`multiple_votes` tinyint(1) unsigned NOT NULL DEFAULT '0',
			`comments_enabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
			`members_only` tinyint(1) unsigned NOT NULL DEFAULT '0',
			PRIMARY KEY (`id`)
			) ENGINE=InnoDB DEFAULT CHARSET=latin1;
		");
		
		// Create poll_options table
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `poll_options` (
			`id` smallint(11) unsigned NOT NULL AUTO_INCREMENT,
			`poll_id` tinyint(11) unsigned NOT NULL,
			`type` enum('defined','other') NOT NULL DEFAULT 'defined',
			`title` varchar(64) NOT NULL,
			`order` tinyint(2) unsigned DEFAULT NULL,
			`votes` mediumint(11) unsigned NOT NULL DEFAULT '0',
			PRIMARY KEY (`id`),
			KEY `poll_id` (`poll_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
		");
		
		// Create poll_other_votes table
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `poll_other_votes` (
			`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
			`parent_id` smallint(11) unsigned NOT NULL,
			`text` tinytext NOT NULL,
			`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (`id`),
			KEY `parent_id` (`parent_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
		");
		
		// Create poll_voters table
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `poll_voters` (
			`id` mediumint(32) unsigned NOT NULL AUTO_INCREMENT,
			`poll_id` tinyint(11) unsigned NOT NULL,
			`user_id` smallint(5) unsigned DEFAULT NULL,
			`session_id` varchar(40) NOT NULL,
			`ip_address` varchar(16) NOT NULL,
			`timestamp` int(11) unsigned NOT NULL,
			PRIMARY KEY (`id`),
			KEY `poll_id` (`poll_id`),
			KEY `user_id` (`user_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
		");
		
		// Referental integrity fo' sho
		$this->db->query("
			ALTER TABLE `poll_options`
			ADD CONSTRAINT `poll_options_ibfk_1`
			FOREIGN KEY (`poll_id`)
			REFERENCES `polls` (`id`)
			ON DELETE CASCADE
			ON UPDATE CASCADE;
		");
		
		$this->db->query("
			ALTER TABLE `poll_other_votes`
			ADD CONSTRAINT `poll_other_votes_ibfk_1`
			FOREIGN KEY (`parent_id`)
			REFERENCES `poll_options` (`id`)
			ON DELETE CASCADE
			ON UPDATE CASCADE;
		");
		
		$this->db->query("
			ALTER TABLE `poll_voters`
			ADD CONSTRAINT `poll_votes_ibfk_1`
			FOREIGN KEY (`poll_id`)
			REFERENCES `polls` (`id`)
			ON DELETE CASCADE
			ON UPDATE CASCADE;
		");
		
		// It worked!
		return TRUE;
	}

	public function uninstall()
	{
		// Drop some tables
		$this->db->query("DROP TABLE `poll_voters`, `poll_other_votes`, `poll_options`, `polls`");
		return TRUE;
	}

	public function upgrade($old_version)
	{
		// Version 0.4 (the first official release)
		if ($old_version == '0.4')
		{
			// Update polls table
			$this->db->query("
				ALTER TABLE  `polls` ADD  `type` enum('single','multiple') NOT NULL DEFAULT 'single'
			");
			
			// Update poll_options table
			$this->db->query("
				ALTER TABLE  `poll_options` ADD  `type` enum('defined','other') NOT NULL DEFAULT 'defined'
			");
			
			// Add poll_other_votes table
			$this->db->query("
				CREATE TABLE IF NOT EXISTS `poll_other_votes` (
				`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
				`parent_id` smallint(11) unsigned NOT NULL,
				`text` tinytext NOT NULL,
				`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
				PRIMARY KEY (`id`),
				KEY `parent_id` (`parent_id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
			");
		
			// Add poll_voters table
			$this->db->query("
				CREATE TABLE IF NOT EXISTS `poll_voters` (
				`id` mediumint(32) unsigned NOT NULL AUTO_INCREMENT,
				`poll_id` tinyint(11) unsigned NOT NULL,
				`user_id` smallint(5) unsigned DEFAULT NULL,
				`session_id` varchar(40) NOT NULL,
				`ip_address` varchar(16) NOT NULL,
				`timestamp` int(11) unsigned NOT NULL,
				PRIMARY KEY (`id`),
				KEY `poll_id` (`poll_id`),
				KEY `user_id` (`user_id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
			");
			
			// Referental integrity fo' sho
			$this->db->query("
				ALTER TABLE `poll_other_votes`
				ADD CONSTRAINT `poll_other_votes_ibfk_1`
				FOREIGN KEY (`parent_id`)
				REFERENCES `poll_options` (`id`)
				ON DELETE CASCADE
				ON UPDATE CASCADE;
			");
			
			$this->db->query("
				ALTER TABLE `poll_voters`
				ADD CONSTRAINT `poll_votes_ibfk_1`
				FOREIGN KEY (`poll_id`)
				REFERENCES `polls` (`id`)
				ON DELETE CASCADE
				ON UPDATE CASCADE;
			");
		}
		// Version 0.5
		elseif ($old_version == '0.5')
		{
			$this->db->query("
				ALTER TABLE  `polls` ADD  `multiple_votes` TINYINT(1) NOT NULL DEFAULT  '0' AFTER  `last_updated`
			");
		}
		return TRUE;
	}
{'fraction_non_alphanumeric': 0.13345882352941177, 'fraction_numerical': 0.014494117647058824, 'mean_word_length': 3.481653310839308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '4456645', 'n_tokens_mistral': 4714, 'n_tokens_neox': 3796, 'n_words': 1046}