From 2903b367f150c9fc722836e766faf88788ecc581 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Thu, 18 Aug 2011 04:15:06 +0200 Subject: [PATCH] Add new model for program parameters --- application/controllers/ajax.php | 8 +- application/models/parameter.php | 140 +++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 application/models/parameter.php diff --git a/application/controllers/ajax.php b/application/controllers/ajax.php index fdbe9bd..c89a3d3 100644 --- a/application/controllers/ajax.php +++ b/application/controllers/ajax.php @@ -58,12 +58,10 @@ class Ajax extends CI_Controller { /** * Sorts a programs parameters. - * - * @param string $id */ - public function sort_parameters($programId) { - $this->load->model('program'); - $this->program->sortParameters($this->input->post('parameters'), $programId); + public function sort_parameters() { + $this->load->model('parameter'); + $this->parameter->sort($this->input->post('parameters')); } } \ No newline at end of file diff --git a/application/models/parameter.php b/application/models/parameter.php new file mode 100644 index 0000000..ac519c9 --- /dev/null +++ b/application/models/parameter.php @@ -0,0 +1,140 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Model for parameters. + * + * Each program has many parameters used for configuration of trials. + * + * @author Eike Foken + */ +class Parameter extends CI_Model { + + /** + * Contains the possible types for parameters. + * + * @var array + */ + private $availableTypes = array('boolean', 'integer', 'float', 'string', 'array'); + + /** + * Calls the parent constructor. + */ + public function __construct() { + parent::__construct(); + } + + /** + * Gets all available parameters for a program. + * + * @param string $programId + * @return array + */ + public function getAll($programId) { + return $this->db->order_by('sort_number ASC') + ->get_where('parameters', array('program_id' => $programId))->result_array(); + } + + /** + * Gets a specific parameter. + * + * @param string $id + */ + public function getById($id) { + return $this->db->get_where('parameters', array('id' => $id))->row_array(); + } + + /** + * Gets the possible parameter types. + */ + public function getTypes() { + return $this->availableTypes; + } + + /** + * Creates a new parameter. + * + * @param array $data + * @return mixed Returns the ID of the created parameter, or FALSE if + * the insert was unsuccessful. + */ + public function create($data) { + $this->load->helper('hash'); + + if (!isset($data['program_id'])) { + return false; + } + + do { // generate unique hash + $data['id'] = random_hash('16'); + } while ($this->db->where('id', $data['id'])->from('parameters')->count_all_results() > 0); + + // put new parameter to the end + $data['sort_number'] = $this->db->select_max('sort_number') + ->get_where('parameters', array('program_id' => $data['program_id'])) + ->row()->sort_number + 1; + + $this->db->insert('parameters', $data); + return $this->db->affected_rows() > 0 ? $data['id'] : false; + } + + /** + * Updates a parameter. + * + * @param array $data + * @param string $id + * @return boolean Returns TRUE if the update was successful. + */ + public function update($data, $id) { + $this->db->update('parameters', $data, array('id' => $id)); + return $this->db->affected_rows() == 1; + } + + /** + * Deletes a specified parameter. + * + * @param string $id + * @return boolean Returns TRUE if the deletion was successful. + */ + public function delete($id) { + $this->db->delete('parameters', array('id' => $id)); + return $this->db->affected_rows() == 1; + } + + /** + * Saves the order of an array of parameters. + * + * @param array $parameters + * @return boolean Returns TRUE if the new order was successfully saved. + */ + public function sort($parameters) { + foreach ($parameters as $number => $id) { + $this->db->update('parameters', array('sort_number' => $number), array('id' => $id)); + } + + return $this->db->affected_rows() > 0; + } +} + +/* End of file parameter.php */ +/* Location: ./application/models/parameter.php */