Thank you for downloading question.php
Your download should start in 3 seconds... If not please use the download link below
| 1 | <?php
|
|---|---|
| 2 | /**
|
| 3 | * JQuarks Component Question Model |
| 4 | * |
| 5 | * @version $Id: question.php 74 2010-05-19 09:16:39Z fnaccache $ |
| 6 | * @author IP-Tech Labs <labs@iptech-offshore.com> |
| 7 | * @copyright 2009-2010 IP-Tech |
| 8 | * @package JQuarks-Back-Office |
| 9 | * @subpackage Models |
| 10 | * @link http://www.iptechinside.com/labs/projects/show/jquarks |
| 11 | * @since 0.1 |
| 12 | * @license GNU/GPL2 |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; version 2 |
| 17 | * of the License. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | * or see <http://www.gnu.org/licenses/> |
| 28 | */ |
| 29 | |
| 30 | defined('_JEXEC') or die(); |
| 31 | |
| 32 | jimport('joomla.application.component.model');
|
| 33 | |
| 34 | |
| 35 | class JQuarksModelQuestion extends JModel |
| 36 | {
|
| 37 | |
| 38 | var $_id ;
|
| 39 | var $_question ;
|
| 40 | var $_categories ;
|
| 41 | var $_propositions ;
|
| 42 | var $_lists ;
|
| 43 | var $_propositionsTable ;
|
| 44 | var $_questionsTable ;
|
| 45 | |
| 46 | function __construct()
|
| 47 | {
|
| 48 | parent::__construct(); |
| 49 | |
| 50 | $array = JRequest::getVar('cid', 0, '', 'array') ; |
| 51 | $this->_id = (int)$array[0] ; |
| 52 | } |
| 53 | |
| 54 | /**
|
| 55 | * Get the selected question |
| 56 | * |
| 57 | * @return object |
| 58 | */ |
| 59 | function &getQuestion()
|
| 60 | {
|
| 61 | if (empty($this->_question)) |
| 62 | {
|
| 63 | $query = 'SELECT q.*'.
|
| 64 | ' FROM #__jquarks_questions AS q'.
|
| 65 | ' WHERE q.id='. $this->_id ; |
| 66 | |
| 67 | $this->_db->setQuery($query) ;
|
| 68 | $this->_question = $this->_db->loadObject() ; |
| 69 | } |
| 70 | |
| 71 | if (!$this->_question) |
| 72 | {
|
| 73 | |
| 74 | $this->_question = new stdClass() ; |
| 75 | $this->_question->id = 0 ; |
| 76 | $this->_question->statement = '' ; |
| 77 | $this->_question->category_id = 0 ; |
| 78 | $this->_question->type_id = 0 ; |
| 79 | } |
| 80 | |
| 81 | return $this->_question ; |
| 82 | } |
| 83 | |
| 84 | function getId()
|
| 85 | {
|
| 86 | return $this->_id ; |
| 87 | } |
| 88 | |
| 89 | /**
|
| 90 | * Get the propositions for the retrieved question |
| 91 | * |
| 92 | * @return array of propositions |
| 93 | */ |
| 94 | function &getPropositions()
|
| 95 | {
|
| 96 | if ($this->_question) |
| 97 | {
|
| 98 | $query = 'SELECT *' .
|
| 99 | ' FROM #__jquarks_propositions' .
|
| 100 | ' WHERE question_id = ' . $this->_id . |
| 101 | ' order by id ASC' ;
|
| 102 | |
| 103 | $this->_db->setQuery($query) ;
|
| 104 | $this->_propositions = $this->_db->loadObjectList(); |
| 105 | } |
| 106 | |
| 107 | return $this->_propositions ; |
| 108 | } |
| 109 | |
| 110 | /**
|
| 111 | * Creating a drop down menu for categories and types |
| 112 | * |
| 113 | * @return array |
| 114 | */ |
| 115 | function &getLists()
|
| 116 | {
|
| 117 | if (empty($this->_categories)) |
| 118 | {
|
| 119 | $query = 'SELECT *' .
|
| 120 | ' FROM #__jquarks_categories' ;
|
| 121 | |
| 122 | $this->_db->setQuery($query) ;
|
| 123 | $categoriesList = $this->_db->loadObjectList();
|
| 124 | } |
| 125 | |
| 126 | // building the drop down list for categories
|
| 127 | $categories[] = JHTML::_('select.option', '0', '- '. JText::_('SELECT_CATEGORY') . ' -') ; |
| 128 | foreach( $categoriesList as $obj) { |
| 129 | $categories[] = JHTML::_('select.option', $obj->id, $obj->title );
|
| 130 | } |
| 131 | |
| 132 | if(!$this->_question->category_id) { |
| 133 | // new question
|
| 134 | $this->_lists['categories'] = JHTML::_('select.genericlist', $categories, 'category_id', 'class="inputbox" size="1" ' ) ; |
| 135 | } else {
|
| 136 | $this->_lists['categories'] = JHTML::_('select.genericlist', $categories, 'category_id', 'class="inputbox" size="1" ', 'value', 'text', (int) $this->_question->category_id) ; |
| 137 | } |
| 138 | |
| 139 | if (empty($this->_types)) |
| 140 | {
|
| 141 | $query = 'SELECT *' .
|
| 142 | 'FROM #__jquarks_types' ;
|
| 143 | |
| 144 | $this->_db->setQuery($query) ;
|
| 145 | $typesList = $this->_db->loadObjectList() ;
|
| 146 | } |
| 147 | |
| 148 | // building the drop down list for types
|
| 149 | $types[] = JHTML::_('select.option', '0', '- '. JText::_('SELECT_TYPE') . ' -') ; |
| 150 | foreach( $typesList as $obj) { |
| 151 | $types[] = JHTML::_('select.option', $obj->id, $obj->title ) ;
|
| 152 | } |
| 153 | |
| 154 | $this->_lists['types'] = JHTML::_('select.genericlist', $types, 'type_id', 'class="inputbox" size="1" ', 'value', 'text', (int) $this->_question->type_id ) ; |
| 155 | |
| 156 | return $this->_lists ; |
| 157 | } |
| 158 | |
| 159 | /**
|
| 160 | * Get the category of the question |
| 161 | * |
| 162 | * @param $questionId |
| 163 | * @return array |
| 164 | */ |
| 165 | function getCategoryOfQuestion($questionId)
|
| 166 | {
|
| 167 | $query = 'SELECT categories.*' .
|
| 168 | ' FROM #__jquarks_questions AS questions' .
|
| 169 | ' LEFT JOIN #__jquarks_categories AS categories ON categories.id = questions.category_id' .
|
| 170 | ' WHERE questions.id = ' . $questionId ;
|
| 171 | |
| 172 | $this->_db->setQuery($query) ;
|
| 173 | return $this->_db->loadAssoc() ; |
| 174 | } |
| 175 | |
| 176 | /**
|
| 177 | * Check if the question is assigned to the set |
| 178 | * |
| 179 | * @param $set |
| 180 | * @param $question |
| 181 | * @return int id of the assignation |
| 182 | */ |
| 183 | function questionAssignedToSet($questionId, $setId)
|
| 184 | {
|
| 185 | $query = 'SELECT soqh.id' .
|
| 186 | ' FROM #__jquarks_setsofquestions_questions AS soqh' .
|
| 187 | ' WHERE soqh.setofquestions_id = ' . $setId .
|
| 188 | ' AND soqh.question_id = ' . $questionId ;
|
| 189 | |
| 190 | $this->_db->setQuery($query) ;
|
| 191 | return $this->_db->loadResult() ; |
| 192 | } |
| 193 | |
| 194 | /**
|
| 195 | * Storing the question and it's propositions |
| 196 | * |
| 197 | * @return boolean |
| 198 | */ |
| 199 | function store()
|
| 200 | {
|
| 201 | $this->_row =& $this->getTable(); |
| 202 | $this->_propositionsTable =& JTable::getInstance('proposition', 'Table'); |
| 203 | |
| 204 | $data = JRequest::get( 'post', JREQUEST_ALLOWRAW ) ;
|
| 205 | $data['statement'] = JRequest::getVar( 'statement', '', 'post', 'string', JREQUEST_ALLOWHTML ); |
| 206 | |
| 207 | if ( ! (int)$data['category_id']) { |
| 208 | $data['category_id'] = NULL ;
|
| 209 | } |
| 210 | |
| 211 | $this->_id = $data['id'] ; |
| 212 | $correctCount = 0 ;
|
| 213 | |
| 214 | if ( ! $this->_row->save($data)) { // saving the question |
| 215 | return false ; |
| 216 | } |
| 217 | |
| 218 | $new = 0;
|
| 219 | if ( $data['id'] == 0 ) // case of a new question get the id of the inserted row |
| 220 | {
|
| 221 | $data['id'] = $this->_id = $this->_db->insertid() ; |
| 222 | $new = 1;
|
| 223 | } |
| 224 | |
| 225 | $propNumber = 1 ;
|
| 226 | |
| 227 | while ( array_key_exists('prop'.$propNumber, $data ) ) // while we have propositions |
| 228 | {
|
| 229 | if (array_key_exists('propid'.$propNumber, $data) && ! $new) { // existant proposition |
| 230 | $propdata['id'] = $data['propid'.$propNumber]; |
| 231 | } else { // new proposition |
| 232 | $propdata['id'] = 0 ; |
| 233 | } |
| 234 | |
| 235 | $propdata['answer'] = $data['prop'.$propNumber] ; |
| 236 | |
| 237 | if (array_key_exists('propcorrect'.$propNumber, $data)) // the proposition is set to correct |
| 238 | {
|
| 239 | $propdata['correct'] = 1 ; |
| 240 | $correctCount++ ; |
| 241 | } else { // the proposition is false |
| 242 | $propdata['correct'] = 0 ; |
| 243 | } |
| 244 | |
| 245 | $propdata['question_id'] = $data['id'] ; |
| 246 | |
| 247 | if(array_key_exists('propdelete'.$propNumber, $data) ) // the proposition is checked to be deleted |
| 248 | {
|
| 249 | if (array_key_exists('propid'.$propNumber, $data) && !$new) // case of an already existant proposition |
| 250 | {
|
| 251 | if (!$this->_propositionsTable->delete($propdata['id'])) |
| 252 | {
|
| 253 | $this->getErrors($this->_db->getErrorMsg()) ; |
| 254 | return false ; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if ($propdata['correct']) { |
| 259 | $correctCount-- ; |
| 260 | } |
| 261 | } |
| 262 | else // the proposition is not set to be deleted |
| 263 | {
|
| 264 | if (!$this->_propositionsTable->save($propdata)) { |
| 265 | return false ; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | $propNumber++ ; |
| 270 | } |
| 271 | |
| 272 | switch ($correctCount) // according to the number of correct proposition define the type of question |
| 273 | {
|
| 274 | case 0 : // input |
| 275 | $data['type_id'] = '1' ; |
| 276 | break ;
|
| 277 | case 1 : // radio |
| 278 | $data['type_id'] = '2' ; |
| 279 | break ;
|
| 280 | default: // checkbox |
| 281 | $data['type_id'] = '3' ; |
| 282 | } |
| 283 | |
| 284 | if (array_key_exists('asCheck', $data) && $data['type_id'] == 2) { // radio as checkbox |
| 285 | $data['type_id'] = '4' ; |
| 286 | } |
| 287 | |
| 288 | if (!$this->_row->save($data)) { // updating the type of the question |
| 289 | return false ; |
| 290 | } |
| 291 | |
| 292 | return true ; |
| 293 | } |
| 294 | |
| 295 | } |