Thank you for downloading quiz.php
Your download should start in 3 seconds... If not please use the download link below
| 1 | <?php
|
|---|---|
| 2 | /**
|
| 3 | * JQuarks Component Quiz Model |
| 4 | * |
| 5 | * @version $Id: quiz.php 59 2010-03-02 08:40:27Z 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 | * |
| 13 | */ |
| 14 | |
| 15 | defined('_JEXEC') or die(); |
| 16 | |
| 17 | jimport('joomla.application.component.model');
|
| 18 | |
| 19 | |
| 20 | class JQuarksModelQuiz extends JModel |
| 21 | {
|
| 22 | var $_id ;
|
| 23 | |
| 24 | var $_quizTable ;
|
| 25 | var $_users_quizzesTable ;
|
| 26 | var $_quizzes_setsofquestionsTable ;
|
| 27 | |
| 28 | var $_quiz ;
|
| 29 | var $_setsOfQuestions ;
|
| 30 | var $_assignedSetsOfQuestions ;
|
| 31 | var $_assignedUsers ;
|
| 32 | var $_users ;
|
| 33 | |
| 34 | var $_filter_order_Dir;
|
| 35 | var $_filter_order;
|
| 36 | |
| 37 | var $_pageNavUsers ;
|
| 38 | var $_pageNavSets ;
|
| 39 | var $_lists ;
|
| 40 | |
| 41 | function __construct()
|
| 42 | {
|
| 43 | parent::__construct(); |
| 44 | |
| 45 | $array = JRequest::getVar('cid', 0, '', 'array') ; |
| 46 | $this->_id = (int)$array[0] ; |
| 47 | } |
| 48 | |
| 49 | /**
|
| 50 | * Get the current quiz or create a new one |
| 51 | * |
| 52 | * @param $quizId |
| 53 | * @return array |
| 54 | */ |
| 55 | function &getQuiz($quizId = null) |
| 56 | {
|
| 57 | if ($quizId) {
|
| 58 | $this->_id = $quizId ;
|
| 59 | } |
| 60 | |
| 61 | if (empty($this->_quiz)) |
| 62 | {
|
| 63 | $query = 'SELECT quiz.*' .
|
| 64 | ' FROM #__jquarks_quizzes AS quiz' .
|
| 65 | ' WHERE quiz.id = ' . $this->_id ; |
| 66 | |
| 67 | $this->_db->setQuery($query) ;
|
| 68 | if ($this->_quiz = $this->_db->loadObject()) |
| 69 | {
|
| 70 | if ( $this->_quiz->publish_down == "00-00-00 00:00:00") { |
| 71 | $this->_quiz->publish_down = JText::_('Never') ; |
| 72 | } |
| 73 | |
| 74 | // creating the key
|
| 75 | $paginate = array('use_pagination', 'use_slide', 'question_page') ; |
| 76 | |
| 77 | // making paginate an associative array containg the key created and the explode value it contained
|
| 78 | $this->_quiz->paginate = array_combine($paginate, explode(' ', $this->_quiz->paginate)) ; |
| 79 | |
| 80 | // slicing the attribute we only keep the value
|
| 81 | foreach ($this->_quiz->paginate as &$attrib) |
| 82 | {
|
| 83 | list($att, $value) = explode('=', $attrib) ; |
| 84 | $attrib = $value ; |
| 85 | } |
| 86 | |
| 87 | unset($attrib) ;
|
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | if (!$this->_quiz) |
| 93 | {
|
| 94 | $config =& JFactory::getConfig(); |
| 95 | $jnow =& JFactory::getDate(); |
| 96 | $jnow->setOffset( $config->getValue('config.offset' ));
|
| 97 | $now = $jnow->toMySQL(true);
|
| 98 | |
| 99 | $this->_quiz = new stdClass() ; |
| 100 | $this->_quiz->id = 0; |
| 101 | $this->_quiz->title = '' ; |
| 102 | $this->_quiz->description = null ; |
| 103 | $this->_quiz->published = false ; |
| 104 | $this->_quiz->access_id = -1 ; |
| 105 | $this->_quiz->time_limit = null ; |
| 106 | $this->_quiz->unique_session = false ; |
| 107 | $this->_quiz->paginate['use_pagination'] = 2 ; // 0 no pagination 1 yes paginate 2 use_global |
| 108 | $this->_quiz->paginate['use_slide'] = 0 ; |
| 109 | $this->_quiz->paginate['question_page'] = 0 ; |
| 110 | $this->_quiz->publish_up = $now ;
|
| 111 | $this->_quiz->publish_down = JText::_('Never'); |
| 112 | $this->_quiz->notify_message = JText::_('GREETING') . ' [userName]' . ",\n\n" . |
| 113 | JText::_('QUIZ_IS_AVAILABLE') . "\n\n" . |
| 114 | JText::_('QUIZ') . ' : "[quizTitle]"' . "\n" . |
| 115 | JText::_('DESCRIPTION') . ' : "[quizDescription]"' . "\n" . |
| 116 | JText::_('WILL_BE_UNPUBLISHED') . ' : "[unpublishDate]"' . "\n\n" . |
| 117 | JText::_('CONNECT_TO_ACCESS') . ' [quizLink]' . "\n\n" . |
| 118 | JText::_('SEE_YOU_SOON') ;
|
| 119 | $this->_quiz->show_results = false; |
| 120 | } |
| 121 | |
| 122 | return $this->_quiz ; |
| 123 | } |
| 124 | |
| 125 | function &getId()
|
| 126 | {
|
| 127 | return $this->_id ; |
| 128 | } |
| 129 | |
| 130 | /**
|
| 131 | * Get the list of sets of questions |
| 132 | * |
| 133 | * @return array |
| 134 | */ |
| 135 | function getSetsOfQuestions()
|
| 136 | {
|
| 137 | global $mainframe ;
|
| 138 | |
| 139 | $context = 'com_jquarks.quiz.sets.list.' ;
|
| 140 | $this->filter_assigned = $mainframe->getUserStateFromRequest( $context.'filter_assigned_sets', 'filter_assigned_sets', '', 'string') ; |
| 141 | $this->_filter_set_type = $mainframe->getUserStateFromRequest( $context.'filter_setType', 'filter_setType', '', 'string') ; |
| 142 | $search = $mainframe->getUserStateFromRequest( $context.'search', 'search', '', 'string') ; |
| 143 | $search = JString::strtolower($search) ; |
| 144 | $this->_filter_order = $mainframe->getUserStateFromRequest( $context.'filter_order', 'filter_order', 'title', 'cmd' ) ; |
| 145 | $this->_filter_order_Dir = $mainframe->getUserStateFromRequest( $context.'filter_order_Dir', 'filter_order_Dir', '', 'word') ; |
| 146 | |
| 147 | $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' ); |
| 148 | $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int') ; |
| 149 | |
| 150 | $where = array();
|
| 151 | |
| 152 | $orderBy = '' ;
|
| 153 | |
| 154 | if ($this->_filter_order_Dir){ |
| 155 | $orderBy = ' ORDER BY '.$this->_filter_order.' '. $this->_filter_order_Dir ; |
| 156 | } |
| 157 | |
| 158 | if ($this->filter_assigned) |
| 159 | {
|
| 160 | if ($this->filter_assigned == 'Y') |
| 161 | {
|
| 162 | $where[] = ' soq.id IN (SELECT distinct(quizzes_setsofquestions.setofquestions_id)' .
|
| 163 | ' FROM #__jquarks_quizzes_setsofquestions as quizzes_setsofquestions' .
|
| 164 | ' WHERE quizzes_setsofquestions.quiz_id = ' . $this->_id . ' )' ; |
| 165 | } |
| 166 | elseif ($this->filter_assigned == 'N') |
| 167 | {
|
| 168 | $where[] = ' soq.id NOT IN (SELECT distinct(quizzes_setsofquestions.setofquestions_id)' .
|
| 169 | ' FROM #__jquarks_quizzes_setsofquestions as quizzes_setsofquestions' .
|
| 170 | ' WHERE quizzes_setsofquestions.quiz_id = ' . $this->_id . ' )' ; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if ($this->_filter_set_type) |
| 175 | {
|
| 176 | if ($this->_filter_set_type == 1 ) { |
| 177 | $where[] = ' soq.type = "custom" ' ;
|
| 178 | } |
| 179 | |
| 180 | if ($this->_filter_set_type == 2) { |
| 181 | $where[] = ' soq.type = "random" ' ;
|
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if ($search) {
|
| 186 | $where[] = ' LOWER(soq.title) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ); |
| 187 | } |
| 188 | |
| 189 | $where = count( $where ) ? ' WHERE ' . implode( ' AND ', $where) : '' ; |
| 190 | |
| 191 | $query = 'SELECT soq.*, (SELECT count(setsofquestions_questions.id)
|
| 192 | FROM #__jquarks_setsofquestions_questions AS setsofquestions_questions |
| 193 | WHERE setsofquestions_questions.setofquestions_id = soq.id ) AS count, |
| 194 | randomSets.nquestions, randomSets.category_id, |
| 195 | (SELECT count(id) |
| 196 | FROM #__jquarks_quizzes_setsofquestions AS quizzes_setsofquestions |
| 197 | WHERE quizzes_setsofquestions.quiz_id = ' . $this ->_id . |
| 198 | ' AND quizzes_setsofquestions.setofquestions_id = soq.id) AS assigned' .
|
| 199 | ' FROM #__jquarks_setsofquestions AS soq' .
|
| 200 | ' LEFT JOIN #__jquarks_customsets AS customSets ON customSets.set_id = soq.id' .
|
| 201 | ' LEFT JOIN #__jquarks_randomsets AS randomSets ON randomSets.set_id = soq.id' .
|
| 202 | $where ; |
| 203 | |
| 204 | $total = $this->_getListCount($query) ;
|
| 205 | |
| 206 | jimport('joomla.html.pagination') ;
|
| 207 | $this->_pageNavSets = new JPagination($total, $limitstart, $limit) ; |
| 208 | |
| 209 | $query .= $orderBy ; // adding the ordering
|
| 210 | |
| 211 | $this->_setsOfQuestions = $this->_getList($query, $this->_pageNavSets->limitstart, $this->_pageNavSets->limit) ; |
| 212 | |
| 213 | if($this->_db->getErrorNum()) { |
| 214 | return false ; |
| 215 | } |
| 216 | |
| 217 | return $this->_setsOfQuestions ; |
| 218 | } |
| 219 | |
| 220 | /**
|
| 221 | * Get the lists of sets of questions that are assigned to the current quiz |
| 222 | * |
| 223 | * @return array |
| 224 | */ |
| 225 | function &getAssignedSetsOfQuestions($quizId = null) |
| 226 | {
|
| 227 | if($quizId) {
|
| 228 | $this->_quiz->id = $quizId ;
|
| 229 | } |
| 230 | |
| 231 | if (empty($this->assignedSetsOfQuestions)) |
| 232 | {
|
| 233 | $query = 'SELECT quizzes_setsofquestions.setofquestions_id' .
|
| 234 | ' FROM #__jquarks_quizzes_setsofquestions AS quizzes_setsofquestions' .
|
| 235 | ' WHERE quizzes_setsofquestions.quiz_id = ' . $this->_quiz->id ; |
| 236 | |
| 237 | $this->_assignedSetsOfQuestions = $this->_getlist($query) ; |
| 238 | if ($this->_db->getErrorNum()) { |
| 239 | return false ; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return $this->_assignedSetsOfQuestions ; |
| 244 | } |
| 245 | |
| 246 | /**
|
| 247 | * Get all the registered users |
| 248 | * |
| 249 | * @return array |
| 250 | */ |
| 251 | function &getUsers($blocked = null) |
| 252 | {
|
| 253 | if(empty($this->_users)) |
| 254 | {
|
| 255 | $query = 'SELECT id, name, username, email' .
|
| 256 | ' FROM #__users' ;
|
| 257 | |
| 258 | if ($blocked) {
|
| 259 | $query .= ' WHERE block = 0' ;
|
| 260 | } |
| 261 | |
| 262 | $this->_users = $this->_getList($query) ; |
| 263 | if ($this->_db->getErrorNum()) { |
| 264 | return false ; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return $this->_users ; |
| 269 | } |
| 270 | |
| 271 | /**
|
| 272 | * Get the list of user that are assigned to the quiz |
| 273 | * |
| 274 | * @return array |
| 275 | */ |
| 276 | function getAssignedUsers()
|
| 277 | {
|
| 278 | global $mainframe ;
|
| 279 | |
| 280 | $context = 'com_jquarks.quiz.users.list.' ;
|
| 281 | $this->filter_assigned = $mainframe->getUserStateFromRequest( $context.'filter_assigned_users', 'filter_assigned_users', '', 'string') ; |
| 282 | $search = $mainframe->getUserStateFromRequest( $context.'search', 'search', '', 'string') ; |
| 283 | $search = JString::strtolower($search) ; |
| 284 | $this->_filter_order = $mainframe->getUserStateFromRequest( $context.'filter_order', 'filter_order', 'name', 'cmd' ) ; |
| 285 | $this->_filter_order_Dir = $mainframe->getUserStateFromRequest( $context.'filter_order_Dir', 'filter_order_Dir', '', 'word') ; |
| 286 | |
| 287 | $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' ); |
| 288 | $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int') ; |
| 289 | |
| 290 | $where = array();
|
| 291 | |
| 292 | $orderBy = '' ;
|
| 293 | |
| 294 | if ($this->_filter_order_Dir){ |
| 295 | $orderBy = ' ORDER BY '.$this->_filter_order.' '. $this->_filter_order_Dir ; |
| 296 | } |
| 297 | |
| 298 | if ($this->filter_assigned) |
| 299 | {
|
| 300 | if ($this->filter_assigned == 'Y') |
| 301 | {
|
| 302 | $where[] = ' (SELECT users_quizzes.id
|
| 303 | FROM #__jquarks_users_quizzes AS users_quizzes |
| 304 | LEFT JOIN #__jquarks_quizzes AS quizzes ON quizzes.id = users_quizzes.quiz_id |
| 305 | WHERE quizzes.id = ' . $this->_quiz->id . ' |
| 306 | AND users_quizzes.user_id = users.id ) IS NOT NULL' ;
|
| 307 | } |
| 308 | elseif ($this->filter_assigned == 'N') |
| 309 | {
|
| 310 | $where[] = ' (SELECT users_quizzes.id
|
| 311 | FROM #__jquarks_users_quizzes AS users_quizzes |
| 312 | LEFT JOIN #__jquarks_quizzes AS quizzes ON quizzes.id = users_quizzes.quiz_id |
| 313 | WHERE quizzes.id = ' . $this->_quiz->id . ' |
| 314 | AND users_quizzes.user_id = users.id ) IS NULL' ;
|
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if ($search) {
|
| 319 | $where[] = ' LOWER(users.name) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ); |
| 320 | } |
| 321 | |
| 322 | $where = count( $where ) ? ' AND ' . implode( ' AND ', $where) : '' ; |
| 323 | |
| 324 | $query = 'SELECT users.*, (SELECT users_quizzes.id
|
| 325 | FROM #__jquarks_users_quizzes AS users_quizzes |
| 326 | LEFT JOIN #__jquarks_quizzes AS quizzes ON quizzes.id = users_quizzes.quiz_id |
| 327 | WHERE quizzes.id = ' . $this->_quiz->id . ' |
| 328 | AND users_quizzes.user_id = users.id ) AS assigned_id, ' .
|
| 329 | '(SELECT archived
|
| 330 | FROM #__jquarks_users_quizzes AS users_quizzes |
| 331 | WHERE users_quizzes.id = assigned_id) AS archived' .
|
| 332 | ' FROM #__users AS users' .
|
| 333 | ' WHERE users.block = 0' .
|
| 334 | $where . |
| 335 | $orderBy ; |
| 336 | |
| 337 | $total = $this->_getListCount($query) ;
|
| 338 | |
| 339 | jimport('joomla.html.pagination') ;
|
| 340 | $this->_pageNavUsers = new JPagination($total, $limitstart, $limit) ; |
| 341 | |
| 342 | $this->_assignedUsers = $this->_getList($query, $this->_pageNavUsers->limitstart, $this->_pageNavUsers->limit) ; |
| 343 | if($this->_db->getErrorNum()) { |
| 344 | return false ; |
| 345 | } |
| 346 | |
| 347 | return $this->_assignedUsers ; |
| 348 | } |
| 349 | |
| 350 | /**
|
| 351 | * Generate the drop down list for the list of users and the set of questions assigned status |
| 352 | * |
| 353 | * @return array |
| 354 | */ |
| 355 | function getLists()
|
| 356 | {
|
| 357 | global $mainframe ;
|
| 358 | |
| 359 | $context = 'com_jquarks.quiz.sets.list.' ;
|
| 360 | $assignedSet = $mainframe->getUserStateFromRequest( $context.'filter_assigned_sets', 'filter_assigned_sets', '', 'string') ; |
| 361 | $assignedUsers = $mainframe->getUserStateFromRequest( 'com_jquarks.quiz.users.list.filter_assigned_users', 'filter_assigned_users', '', 'string') ; |
| 362 | $profiles =$mainframe->getUserStateFromRequest( 'com_jquarks.quiz.users.list.filter_profiles', 'filter_profiles', '', 'string') ; |
| 363 | $this->_filter_set_type = $mainframe->getUserStateFromRequest( $context.'filter_setType', 'filter_setType', '', 'string') ; |
| 364 | |
| 365 | $javascript = ' onchange="document.adminForm.submit();" ' ;
|
| 366 | |
| 367 | // generating the list of users
|
| 368 | if (empty($this->_user)) { |
| 369 | $this->getUsers(1) ; |
| 370 | } |
| 371 | |
| 372 | $users[] = JHTML::_('select.option', '', '- '.JText::_('SELECT_USER').' -'); |
| 373 | $users[] = JHTML::_('select.option', '-1', JText::_('ALL_REGISTRED')); |
| 374 | |
| 375 | foreach( $this->_users as $user) { |
| 376 | $users[] = JHTML::_('select.option', $user->id, $user->name ) ;
|
| 377 | } |
| 378 | |
| 379 | $this->_lists['users'] = JHTML::_('select.genericlist', $users, 'filter_user', 'class="inputbox" size="1"' ); // onclick="document.getElementById(\'task\').value=\'\'; document.form.submit();"') ; |
| 380 | |
| 381 | // generating the drop down menu for the sets assignation status
|
| 382 | $status[] = JHTML::_('select.option', '', '- '.JText::_('SELECT_STATUS').' -'); |
| 383 | $status[] = JHTML::_('select.option', 'Y', JText::_('ASSIGNED')) ; |
| 384 | $status[] = JHTML::_('select.option', 'N', JText::_('NOT_ASSIGNED')) ; |
| 385 | |
| 386 | $this->_lists['statusSets'] = JHTML::_('select.genericlist', $status, 'filter_assigned_sets', 'class="inputbox" size="1"' . $javascript , 'value', 'text', $assignedSet) ; |
| 387 | $this->_lists['statusUsers'] = JHTML::_('select.genericlist', $status, 'filter_assigned_users', 'class="inputbox" size="1"' . $javascript , 'value', 'text', $assignedUsers) ; |
| 388 | |
| 389 | $setType[] = JHTML::_('select.option', '', '- '.JText::_('SELECT_SET_TYPE').' -'); |
| 390 | $setType[] = JHTML::_('select.option', 1, JText::_('CUSTOM')); |
| 391 | $setType[] = JHTML::_('select.option', 2, JText::_('RANDOM')); |
| 392 | $this->_lists['setType'] = JHTML::_('select.genericlist', $setType, 'filter_setType', 'class="inputbox" size="1"' . $javascript , 'value', 'text', $this->_filter_set_type) ; |
| 393 | |
| 394 | $this->_lists['order_Dir'] = $this->_filter_order_Dir ; |
| 395 | $this->_lists['order'] = $this->_filter_order ; |
| 396 | |
| 397 | return $this->_lists ; |
| 398 | } |
| 399 | |
| 400 | function getPageNavUsers()
|
| 401 | {
|
| 402 | return $this->_pageNavUsers ; |
| 403 | } |
| 404 | |
| 405 | function getPageNavSets()
|
| 406 | {
|
| 407 | return $this->_pageNavSets ; |
| 408 | } |
| 409 | |
| 410 | /**
|
| 411 | * For the assigned custom sets of questions get the duplicated questions |
| 412 | * |
| 413 | * @param $quizId |
| 414 | * @return array of duplicated questions |
| 415 | */ |
| 416 | function getDuplicateQuestions($quizId)
|
| 417 | {
|
| 418 | // building the list of set for the query
|
| 419 | $assignedSets = $this->getAssignedSetsOfQuestions($quizId) ;
|
| 420 | |
| 421 | $assigned = array() ;
|
| 422 | foreach ($assignedSets as $assignedSet) { |
| 423 | $assigned[] = $assignedSet->setofquestions_id ; |
| 424 | } |
| 425 | $listSets = implode(",", $assigned );
|
| 426 | |
| 427 | $query = 'SELECT soq.question_id, q.statement, soq.setofquestions_id, sets.title, (SELECT count(question_id)
|
| 428 | FROM #__jquarks_setsofquestions_questions |
| 429 | WHERE question_id = soq.question_id |
| 430 | AND setofquestions_id IN (' . $listSets . ')) AS count' . |
| 431 | ' FROM #__jquarks_setsofquestions_questions AS soq' .
|
| 432 | ' LEFT JOIN #__jquarks_questions AS q ON soq.question_id = q.id' .
|
| 433 | ' LEFT JOIN #__jquarks_setsofquestions AS sets ON soq.setofquestions_id = sets.id' .
|
| 434 | ' WHERE setofquestions_id IN (' . $listSets . ')' . |
| 435 | ' ORDER BY soq.question_id' ;
|
| 436 | |
| 437 | $this->_db->setQuery($query) ;
|
| 438 | $duplicate = $this->_db->loadAssocList() ;
|
| 439 | if ($this->_db->getErrorNum()) { |
| 440 | return false ; |
| 441 | } |
| 442 | |
| 443 | // removing non duplicate associations
|
| 444 | foreach ($duplicate as $key => $value) |
| 445 | {
|
| 446 | if ($value['count'] == 1 ) { |
| 447 | unset($duplicate[$key]) ;
|
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return $duplicate ;
|
| 452 | } |
| 453 | |
| 454 | /**
|
| 455 | * Get all the questions of a specific category that are affected to a given quiz |
| 456 | * |
| 457 | * @param $categoryId |
| 458 | * @param $quizId |
| 459 | * @return array associative |
| 460 | */ |
| 461 | function getQuestionsOfCategoryAffectedToQuiz($quizId, $categoryId = NULL)
|
| 462 | {
|
| 463 | $query = 'SELECT questions.*' .
|
| 464 | ' FROM #__jquarks_quizzes_setsofquestions AS quiHaveSoq' .
|
| 465 | ' JOIN #__jquarks_setsofquestions AS setsOfQuestions ON setsOfQuestions.id = quiHaveSoq.setofquestions_id' .
|
| 466 | ' JOIN #__jquarks_setsofquestions_questions AS soqHaveQ ON soqHaveQ.setofquestions_id = setsOfQuestions.id' .
|
| 467 | ' JOIN #__jquarks_questions AS questions ON questions.id = soqHaveQ.question_id' .
|
| 468 | ' WHERE quiHaveSoq.quiz_id = ' . $quizId ;
|
| 469 | |
| 470 | if ($categoryId) {
|
| 471 | $query .= ' AND questions.category_id = ' . $categoryId ;
|
| 472 | } else {
|
| 473 | $query .= ' AND questions.category_id IS NULL' ;
|
| 474 | } |
| 475 | |
| 476 | $this->_db->setQuery($query) ;
|
| 477 | return $this->_db->loadAssocList() ; |
| 478 | } |
| 479 | |
| 480 | /**
|
| 481 | * Get the number of random questions affected to a quiz |
| 482 | * |
| 483 | * @param $quizId |
| 484 | * @return int |
| 485 | */ |
| 486 | function getNbrRandomQuestionsAffectedToQuiz($quizId)
|
| 487 | {
|
| 488 | $query = 'SELECT sum(nquestions)' .
|
| 489 | ' FROM #__jquarks_quizzes_setsofquestions AS quizzes_setsofquestions' .
|
| 490 | ' LEFT JOIN #__jquarks_setsofquestions AS setsofquestions ON setsofquestions.id = quizzes_setsofquestions.setofquestions_id' .
|
| 491 | ' JOIN #__jquarks_randomsets AS randomSets ON randomSets.set_id = setsofquestions.id' .
|
| 492 | ' WHERE quizzes_setsofquestions.quiz_id = ' . $quizId ;
|
| 493 | |
| 494 | $this->_db->setQuery($query) ;
|
| 495 | return $this->_db->loadResult() ; |
| 496 | } |
| 497 | |
| 498 | /**
|
| 499 | * Fetch the list of questions affected to a given quiz |
| 500 | * |
| 501 | * @param $quizId |
| 502 | * @return array associative |
| 503 | */ |
| 504 | function getQuestionsAffectedToQuiz($quizId)
|
| 505 | {
|
| 506 | $query = 'SELECT soqHaveQ.question_id' .
|
| 507 | ' FROM #__jquarks_quizzes_setsofquestions AS quiHaveSoq' .
|
| 508 | ' JOIN #__jquarks_setsofquestions AS setsOfQuestions ON setsOfQuestions.id = quiHaveSoq.setofquestions_id' .
|
| 509 | ' JOIN #__jquarks_setsofquestions_questions AS soqHaveQ ON soqHaveQ.setofquestions_id = setsOfQuestions.id' .
|
| 510 | ' JOIN #__jquarks_questions AS questions ON questions.id = soqHaveQ.question_id' .
|
| 511 | ' WHERE quiHaveSoq.quiz_id = ' . $quizId ;
|
| 512 | |
| 513 | $this->_db->setQuery($query) ;
|
| 514 | return $this->_db->loadAssocList() ; |
| 515 | } |
| 516 | |
| 517 | /**
|
| 518 | * Get the number of time a user has taken a quiz |
| 519 | * |
| 520 | * @param $assignationId |
| 521 | * @return int |
| 522 | */ |
| 523 | function tookQuiz($assignationId)
|
| 524 | {
|
| 525 | $query = 'SELECT *' .
|
| 526 | ' FROM #__jquarks_quizsession AS quizSession' .
|
| 527 | ' WHERE quizSession.affected_id = ' . $assignationId ;
|
| 528 | |
| 529 | $this->_db->setQuery($query) ;
|
| 530 | |
| 531 | $taken = $this->_getListCount($query) ;
|
| 532 | if ($this->_db->getErrorNum()) { |
| 533 | return false ; |
| 534 | } |
| 535 | |
| 536 | return $taken ;
|
| 537 | } |
| 538 | |
| 539 | /**
|
| 540 | * If a user has already taken a quiz, when he is unassigned from it his assignation is archived |
| 541 | * |
| 542 | * @param $assignationId |
| 543 | * @return boolean |
| 544 | */ |
| 545 | function archiveAssignation($assignationId)
|
| 546 | {
|
| 547 | $query = 'UPDATE #__jquarks_users_quizzes AS users_quizzes' .
|
| 548 | ' SET archived = 1' .
|
| 549 | ' WHERE users_quizzes.id = ' . $assignationId ;
|
| 550 | |
| 551 | $this->_db->execute($query) ;
|
| 552 | if ($this->_db->getErrorNum()) { |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | return true ; |
| 557 | } |
| 558 | |
| 559 | /**
|
| 560 | * Check if the affected set is random and if a set from his category isn't already affected to the quiz |
| 561 | * |
| 562 | * @return set of question |
| 563 | */ |
| 564 | function randomExist()
|
| 565 | {
|
| 566 | $setOfQuestionsModel =& JModel::getInstance('setofquestions','JQuarksModel'); |
| 567 | |
| 568 | $line['quiz_id'] = $this->_id ; |
| 569 | $line['setofquestions_id'] = JRequest::getVar('setId') ; |
| 570 | |
| 571 | $set['id'] = 0 ; |
| 572 | |
| 573 | $setOfQuestion =& JModel::getInstance('setofquestions','JQuarksModel', $line['setofquestions_id']); |
| 574 | $setOfQuestion->getSetOfQuestions() ; |
| 575 | |
| 576 | $set = $setOfQuestionsModel->getRandomSetOfCategoryAffectedToQuiz($line['quiz_id'], $setOfQuestion->_setofquestions->category_id) ;
|
| 577 | |
| 578 | if ($setOfQuestion->_setofquestions->type == 'random') { |
| 579 | return $set ;
|
| 580 | } else {
|
| 581 | return null ; |
| 582 | } |
| 583 | |
| 584 | } |
| 585 | |
| 586 | /**
|
| 587 | * Get the title of a category |
| 588 | * |
| 589 | * @TODO replace this with a method from JQuarksModelCategory |
| 590 | * @param $categoryId |
| 591 | * @return unknown_type |
| 592 | */ |
| 593 | function getCategoryTitle($categoryId)
|
| 594 | {
|
| 595 | $query = 'SELECT title' .
|
| 596 | ' FROM #__jquarks_categories' .
|
| 597 | ' WHERE id = ' . $categoryId ;
|
| 598 | |
| 599 | $this->_db->setQuery($query) ;
|
| 600 | $title = $this->_db->loadResult() ;
|
| 601 | |
| 602 | if($title) {
|
| 603 | return $title ;
|
| 604 | } else {
|
| 605 | return JText::_('UNCATEGORIZED') ; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | /**
|
| 610 | * Check if the quiz and the questions it contain are coherent |
| 611 | * |
| 612 | * @TODO see if it's possible to improve this method |
| 613 | * @return array |
| 614 | */ |
| 615 | function checkCoherentSets()
|
| 616 | {
|
| 617 | $categoryModel =& JModel::getInstance('category','JQuarksModel'); |
| 618 | $setOfQuestionsModel =& JModel::getInstance('setofquestions','JQuarksModel'); |
| 619 | |
| 620 | $line['quiz_id'] = $this->_id ; |
| 621 | $line['setofquestions_id'] = JRequest::getVar('setId') ; |
| 622 | $line['id'] = 0 ; |
| 623 | |
| 624 | $setOfQuestions =& JModel::getInstance('setofquestions','JQuarksModel', $line['setofquestions_id']); |
| 625 | $setOfQuestions->getSetOfQuestions($line['setofquestions_id']) ;
|
| 626 | |
| 627 | if ( $setOfQuestions->_setofquestions->type == 'random' ) |
| 628 | {
|
| 629 | $setStat[0]['category_id'] = $setOfQuestions->_setofquestions->category_id ; |
| 630 | $setStat[0]['categoryName'] = $this->getCategoryTitle($setStat[0]['category_id']) ; |
| 631 | |
| 632 | // getting the number of questions affected to this set
|
| 633 | $setStat[0]['affectedToThisSet'] = (int)$setOfQuestions->_setofquestions->nquestions ; |
| 634 | |
| 635 | // getting the number of questions affected to this quiz
|
| 636 | if ($setOfQuestions->_setofquestions->category_id) {
|
| 637 | $setStat[0]['affectedToThisQuiz'] = count($this->getQuestionsOfCategoryAffectedToQuiz($line['quiz_id'], $setOfQuestions->_setofquestions->category_id)) ; |
| 638 | } else {
|
| 639 | $setStat[0]['affectedToThisQuiz'] = count($this->getQuestionsOfCategoryAffectedToQuiz($line['quiz_id'])) ; |
| 640 | } |
| 641 | |
| 642 | // getting the total number of questions of this random set's category
|
| 643 | $setStat[0]['totalNumber'] = count($categoryModel->getQuestionsOfCategory('actived', $setOfQuestions->_setofquestions->category_id)) ; |
| 644 | |
| 645 | $setStat['coherent'] = true ; |
| 646 | if ($setStat[0]['affectedToThisQuiz'] + $setStat[0]['affectedToThisSet'] > $setStat[0]['totalNumber'] ) { |
| 647 | $setStat['coherent'] = false ; |
| 648 | } |
| 649 | |
| 650 | } |
| 651 | elseif ( $setOfQuestions->_setofquestions->type == 'custom' ) |
| 652 | {
|
| 653 | $setStat[] = array() ;
|
| 654 | |
| 655 | // getting the questions's categories and for each category the number of questions affected to the set
|
| 656 | $query = 'SELECT questions.category_id, count(soqHaveQ.question_id) AS count' .
|
| 657 | ' FROM #__jquarks_questions AS questions' .
|
| 658 | ' JOIN #__jquarks_setsofquestions_questions AS soqHaveQ ON soqHaveQ.question_id = questions.id' .
|
| 659 | ' WHERE soqHaveQ.setofquestions_id = ' . $line['setofquestions_id'] . |
| 660 | ' GROUP BY questions.category_id' ;
|
| 661 | |
| 662 | $this->_db->setQuery($query) ;
|
| 663 | echo $this->_db->getQuery() ; |
| 664 | $array = $this->_db->loadObjectList() ;
|
| 665 | |
| 666 | if (count($array) > 0) |
| 667 | {
|
| 668 | $i = 0 ;
|
| 669 | foreach ($array as $obj) |
| 670 | {
|
| 671 | $setStat[$i]["category_id"] = (int)$obj->category_id ;
|
| 672 | $setStat[$i]["categoryName"] = $this->getCategoryTitle($setStat[$i]["category_id"]) ; |
| 673 | $setStat[$i]["affectedToThisSet"] = (int)$obj->count ;
|
| 674 | |
| 675 | $i++ ; |
| 676 | } |
| 677 | |
| 678 | // for each category getting the number of question affected for that category and the total number of question
|
| 679 | foreach ($setStat AS &$category)
|
| 680 | {
|
| 681 | // getting the number of question affected to this quiz for this category
|
| 682 | $category['affectedToThisQuiz'] = count($this->getQuestionsOfCategoryAffectedToQuiz($line['quiz_id'], $category['category_id'])) ; |
| 683 | |
| 684 | // adding the number of question affected to the random set for this category
|
| 685 | $randomSet = $setOfQuestionsModel->getRandomSetOfCategoryAffectedToQuiz($line['quiz_id'], $category['category_id']) ; |
| 686 | $category['affectedToThisQuiz'] += $randomSet['nquestions'] ; |
| 687 | |
| 688 | // getting the total number of questions available
|
| 689 | $category["totalNumber"] = count($categoryModel->getQuestionsOfCategory('actived', $category['category_id'])) ; |
| 690 | } |
| 691 | unset($category) ;
|
| 692 | |
| 693 | $setStat['coherent'] = true ; |
| 694 | // checking if one of the category isn't out of bound
|
| 695 | foreach ($setStat AS $category)
|
| 696 | {
|
| 697 | if ($category['affectedToThisSet'] + $category['affectedToThisQuiz'] > $category['totalNumber']) { |
| 698 | $setStat['coherent'] = false ; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | } else {
|
| 703 | $setStat['coherent'] = true ; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return $setStat ;
|
| 708 | } |
| 709 | |
| 710 | /**
|
| 711 | * Assign the selected set to the current quiz |
| 712 | * |
| 713 | * @return boolean |
| 714 | */ |
| 715 | function assignSet()
|
| 716 | {
|
| 717 | $this->_quizzes_setsofquestionsTable =& JTable::getInstance('quizzes_setsofquestions', 'Table') ; |
| 718 | |
| 719 | $line['quiz_id'] = $this->_id ; |
| 720 | $line['setofquestions_id'] = JRequest::getVar('setId') ; |
| 721 | $line['id'] = 0 ; |
| 722 | |
| 723 | // checking whether if the set is empty or not
|
| 724 | $query = 'SELECT soq.id, customSets.id AS custom_id, count(setsofquestions_questions.id) AS custom_count,
|
| 725 | randomSets.id AS random_id, randomSets.nquestions AS random_count' .
|
| 726 | ' FROM #__jquarks_setsofquestions AS soq' .
|
| 727 | ' LEFT JOIN #__jquarks_customsets AS customSets ON soq.id = customSets.set_id' .
|
| 728 | ' LEFT JOIN #__jquarks_randomsets AS randomSets ON soq.id = randomSets.set_id' .
|
| 729 | ' LEFT JOIN #__jquarks_setsofquestions_questions AS setsofquestions_questions ON setsofquestions_questions.setofquestions_id = soq.id' .
|
| 730 | ' WHERE soq.id = ' . $line['setofquestions_id'] . |
| 731 | ' GROUP BY soq.id' ;
|
| 732 | |
| 733 | $this->_db->setQuery($query) ;
|
| 734 | $set = $this->_db->loadObject() ;
|
| 735 | |
| 736 | |
| 737 | if ($set->random_count != 0 || $set->custom_count != 0) |
| 738 | {
|
| 739 | if (!$this->_quizzes_setsofquestionsTable->save($line)) { |
| 740 | return false ; |
| 741 | } |
| 742 | |
| 743 | return true ; |
| 744 | } |
| 745 | |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | /**
|
| 750 | * Unassign the selected set from the current quiz |
| 751 | * |
| 752 | * @param $setId |
| 753 | * @param $quizId |
| 754 | * @return boolean |
| 755 | */ |
| 756 | function unassignSet($setId = null, $quizId = null) |
| 757 | {
|
| 758 | $this->_quizzes_setsofquestionsTable =& JTable::getInstance('quizzes_setsofquestions', 'Table') ; |
| 759 | |
| 760 | if ($quizId) {
|
| 761 | $line['quiz_id'] = $quizId ;
|
| 762 | } else {
|
| 763 | $line['quiz_id'] = $this->_id ; |
| 764 | } |
| 765 | |
| 766 | if ($setId) {
|
| 767 | $line['setofquestions_id'] = $setId ;
|
| 768 | } else {
|
| 769 | $line['setofquestions_id'] = JRequest::getVar('setId') ; |
| 770 | } |
| 771 | |
| 772 | $query = 'SELECT quizzes_setsofquestions.id' .
|
| 773 | ' FROM #__jquarks_quizzes_setsofquestions AS quizzes_setsofquestions' .
|
| 774 | ' WHERE quizzes_setsofquestions.quiz_id = ' . $line['quiz_id'] . |
| 775 | ' AND quizzes_setsofquestions.setofquestions_id = ' . $line['setofquestions_id'] ; |
| 776 | |
| 777 | $this->_db->setQuery($query) ;
|
| 778 | $id = $this->_db->loadResult() ;
|
| 779 | |
| 780 | if (!$this->_quizzes_setsofquestionsTable->delete( $id )) |
| 781 | {
|
| 782 | $this->setError($this->_db->getErrorMsg() ) ; |
| 783 | return false ; |
| 784 | } |
| 785 | |
| 786 | return true ; |
| 787 | } |
| 788 | |
| 789 | /**
|
| 790 | * Unassign the given set from all the quizzes it was affected to |
| 791 | * @param $setId |
| 792 | * @return boolean |
| 793 | */ |
| 794 | function unassignSetFromQuizzes($setId)
|
| 795 | {
|
| 796 | $this->_quizzes_setsofquestionsTable =& JTable::getInstance('quizzes_setsofquestions', 'Table') ; |
| 797 | //$quizzesModel =& JModel::getInstance('quizzes','JQuarksModel');
|
| 798 | |
| 799 | //$quizzes = $quizzesModel->getQuizzesThatHaveSet($setId) ;
|
| 800 | |
| 801 | $query = 'SELECT *' .
|
| 802 | ' FROM #__jquarks_quizzes_setsofquestions AS quiHaveSoq' .
|
| 803 | ' WHERE quiHaveSoq.setofquestions_id = ' . $setId ;
|
| 804 | |
| 805 | $setAssignations = $this->_getList($query) ;
|
| 806 | |
| 807 | foreach ($setAssignations AS $setAssignation)
|
| 808 | {
|
| 809 | if (!$this->_quizzes_setsofquestionsTable->delete( $setAssignation->id )) |
| 810 | {
|
| 811 | $this->setError($this->_db->getErrorMsg() ) ; |
| 812 | return false ; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | return true ; |
| 817 | } |
| 818 | |
| 819 | /**
|
| 820 | * AssignUser Assign one user to the current quiz |
| 821 | * @todo make this check in controller and call a new method assign allRegistredUserThatAreNotBlocked |
| 822 | * |
| 823 | * @param $userId |
| 824 | * @param $quizId |
| 825 | * @return boolean |
| 826 | */ |
| 827 | function assignUser($userId = null, $quizId = null) |
| 828 | {
|
| 829 | $this->_users_quizzesTable =& JTable::getInstance('users_quizzes', 'Table') ; |
| 830 | |
| 831 | if ($quizId) {
|
| 832 | $line['quiz_id'] = $this->_id = $quizId ; |
| 833 | } else {
|
| 834 | $line['quiz_id'] = $this->_id ; |
| 835 | } |
| 836 | |
| 837 | if ($userId) {
|
| 838 | $line['user_id'] = $userId ;
|
| 839 | } else {
|
| 840 | $line['user_id'] = JRequest::getVar('selected_user') ; |
| 841 | } |
| 842 | |
| 843 | if ($line['user_id'] != '') |
| 844 | {
|
| 845 | // checking if we must add one or all the registered user
|
| 846 | if($line['user_id'] == -1) |
| 847 | {
|
| 848 | // affecting all registered user
|
| 849 | if(!$this->assignUsers()) { |
| 850 | return false ; |
| 851 | } |
| 852 | } |
| 853 | else
|
| 854 | {
|
| 855 | // affecting only one user
|
| 856 | $query = 'SELECT users_quizzes.id, users_quizzes.archived' .
|
| 857 | ' FROM #__jquarks_users_quizzes AS users_quizzes' .
|
| 858 | ' WHERE users_quizzes.quiz_id = ' . $line['quiz_id'] . |
| 859 | ' AND users_quizzes.user_id = ' . $line['user_id'] ; |
| 860 | |
| 861 | $this->_db->setQuery($query) ;
|
| 862 | $affect = $this->_db->loadObject() ;
|
| 863 | |
| 864 | if ($affect == "") // user notfound we affect him |
| 865 | {
|
| 866 | $line['id'] = 0 ; |
| 867 | $line['archived'] = 0 ; |
| 868 | } |
| 869 | elseif ($affect != "" && $affect->archived ) // user was archived we unarchieve him |
| 870 | {
|
| 871 | $line['id'] = $affect->id ;
|
| 872 | $line['archived'] = 0 ; |
| 873 | } |
| 874 | |
| 875 | if (!$this->_users_quizzesTable->save($line)) { |
| 876 | return false ; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | return true ; |
| 881 | } |
| 882 | |
| 883 | return false ; |
| 884 | } |
| 885 | |
| 886 | /**
|
| 887 | * Assign all registered user to the quiz |
| 888 | * |
| 889 | * @return boolean |
| 890 | */ |
| 891 | function assignUsers()
|
| 892 | {
|
| 893 | $line['quiz_id'] = $this->_id ; |
| 894 | |
| 895 | // getting the list of the registered users
|
| 896 | if(empty($this->users)) { |
| 897 | $this->getUsers();
|
| 898 | } |
| 899 | |
| 900 | foreach($this->_users as $user) |
| 901 | {
|
| 902 | $this->_users_quizzesTable =& JTable::getInstance('users_quizzes', 'Table') ; |
| 903 | |
| 904 | $line['user_id'] = $user->id ;
|
| 905 | |
| 906 | // checking if the user isn't already affected to this quiz
|
| 907 | $query = 'SELECT *' .
|
| 908 | ' FROM #__jquarks_users_quizzes AS users_quizzes' .
|
| 909 | ' WHERE users_quizzes.quiz_id = ' . $line['quiz_id'] . |
| 910 | ' AND users_quizzes.user_id = ' . $line['user_id'] ; |
| 911 | |
| 912 | $this->_db->setQuery($query) ;
|
| 913 | $affect = $this->_db->loadObject() ;
|
| 914 | |
| 915 | if ($affect->id && $affect->archived != 1) { |
| 916 | continue ;
|
| 917 | } |
| 918 | else
|
| 919 | {
|
| 920 | if ($affect->archived) // case the affectation was archived |
| 921 | {
|
| 922 | $line['archived'] = 0 ; |
| 923 | $line['id'] = $affect->id ;
|
| 924 | } else { // case of a new affectation |
| 925 | $line['id'] = 0 ; |
| 926 | } |
| 927 | |
| 928 | if (!$this->_users_quizzesTable->save($line)) { |
| 929 | return false ; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | return true ; |
| 935 | } |
| 936 | |
| 937 | /**
|
| 938 | * AssignAllUsers make the quiz assigned to all registered and unregistered users |
| 939 | * for unregistered user only one affectation is used it's user_id is -1 |
| 940 | * |
| 941 | * @return boolean |
| 942 | */ |
| 943 | function assignAllUsers()
|
| 944 | {
|
| 945 | $this->_users_quizzesTable =& JTable::getInstance('users_quizzes', 'Table') ; |
| 946 | |
| 947 | //assigning a common unregistered user affectation to the quiz
|
| 948 | $line['quiz_id'] = $this->_id ; |
| 949 | $line['user_id'] = -1 ; |
| 950 | |
| 951 | // checking if the user isn't already affected to this quiz
|
| 952 | $query = 'SELECT *' .
|
| 953 | ' FROM #__jquarks_users_quizzes AS users_quizzes' .
|
| 954 | ' WHERE users_quizzes.quiz_id = ' . $line['quiz_id'] . |
| 955 | ' AND users_quizzes.user_id = ' . $line['user_id'] ; |
| 956 | |
| 957 | $this->_db->setQuery($query) ;
|
| 958 | $affect = $this->_db->loadObject() ;
|
| 959 | if($this->_db->getErrorNum()){ |
| 960 | return false ; |
| 961 | } |
| 962 | |
| 963 | if ($affect->archived) // case the affectation was archived |
| 964 | {
|
| 965 | $line['archived'] = 0 ; |
| 966 | $line['id'] = $affect->id ;
|
| 967 | } else { // case of a new affectation |
| 968 | $line['id'] = 0 ; |
| 969 | } |
| 970 | |
| 971 | if (!$this->_users_quizzesTable->save($line)) { |
| 972 | return false ; |
| 973 | } |
| 974 | |
| 975 | // assign registered users
|
| 976 | if(!$this->assignUsers()) { |
| 977 | return false ; |
| 978 | } |
| 979 | |
| 980 | return true ; |
| 981 | } |
| 982 | |
| 983 | /**
|
| 984 | * UnassignUser unassign a specific user if the user have already took the quiz the assignation will be archived |
| 985 | * |
| 986 | * @return boolean |
| 987 | */ |
| 988 | function unassignUser($userId = null, $quizId = null) |
| 989 | {
|
| 990 | $this->_users_quizzesTable =& JTable::getInstance('users_quizzes', 'Table') ; |
| 991 | |
| 992 | if ($quizId) {
|
| 993 | $line['quiz_id'] = $this->_id = $quizId; |
| 994 | } else {
|
| 995 | $line['quiz_id'] = $this->_id ; |
| 996 | } |
| 997 | |
| 998 | if ($userId) {
|
| 999 | $line['user_id'] = $userId ;
|
| 1000 | } else {
|
| 1001 | $line['user_id'] = JRequest::getVar('selected_user') ; |
| 1002 | } |
| 1003 | |
| 1004 | // getting the id of the record to delete / archive
|
| 1005 | $query = 'SELECT users_quizzes.id' .
|
| 1006 | ' FROM #__jquarks_users_quizzes AS users_quizzes' .
|
| 1007 | ' WHERE users_quizzes.quiz_id = ' . $line['quiz_id'] . |
| 1008 | ' AND users_quizzes.user_id = ' . $line['user_id'] ; |
| 1009 | |
| 1010 | $this->_db->setQuery($query) ;
|
| 1011 | |
| 1012 | $assignationId = $this->_db->loadResult() ;
|
| 1013 | if ($this->_db->getErrorNum()) { |
| 1014 | return false ; |
| 1015 | } |
| 1016 | |
| 1017 | if ($assignationId) {
|
| 1018 | $taken = $this->tookQuiz($assignationId) ; // checking if the user has already took the quiz |
| 1019 | } else {
|
| 1020 | $taken = null ;
|
| 1021 | } |
| 1022 | |
| 1023 | if ($taken) {
|
| 1024 | $this->archiveAssignation($assignationId) ; // we archive the assignation |
| 1025 | } |
| 1026 | else
|
| 1027 | {
|
| 1028 | if (!$this->_users_quizzesTable->delete( $assignationId )) // deleting the assignation |
| 1029 | {
|
| 1030 | $this->setError( $this->_db->getErrorMsg() ) ; |
| 1031 | return false ; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | return true ; |
| 1036 | } |
| 1037 | |
| 1038 | /**
|
| 1039 | * Unassign all the user that have been added indiviually to the quiz |
| 1040 | * |
| 1041 | * @return boolean |
| 1042 | */ |
| 1043 | function unassignUsers()
|
| 1044 | {
|
| 1045 | $this->_users_quizzesTable =& JTable::getInstance('users_quizzes', 'Table') ; |
| 1046 | |
| 1047 | $query = 'SELECT *' .
|
| 1048 | ' FROM #__jquarks_users_quizzes AS users_quizzes' .
|
| 1049 | ' WHERE quiz_id = ' . $this->_id ; |
| 1050 | |
| 1051 | // getting the old affectation to this quiz
|
| 1052 | $affectationToDelete = $this->_getList($query) ;
|
| 1053 | if ($this->_db->getErrorNum()) { |
| 1054 | return false ; |
| 1055 | } |
| 1056 | |
| 1057 | // deleting the affectations or archieving them
|
| 1058 | foreach($affectationToDelete as $item) |
| 1059 | {
|
| 1060 | $taken = $this->tookQuiz($item->id) ; // checking if the quiz have been taken |
| 1061 | |
| 1062 | if ($taken)
|
| 1063 | {
|
| 1064 | $this->archiveAssignation($item->id) ; // archive the assignation |
| 1065 | } |
| 1066 | else
|
| 1067 | {
|
| 1068 | if (!$this->_users_quizzesTable->delete( $item->id )) // delete assignation |
| 1069 | {
|
| 1070 | $this->setError( $this->_db->getErrorMsg() ) ; |
| 1071 | return false ; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | return true ; |
| 1077 | } |
| 1078 | |
| 1079 | /**
|
| 1080 | * UnassignAllUsers remove all the registered user's assignation and the special unregistered user assignation |
| 1081 | * |
| 1082 | * @return boolean |
| 1083 | */ |
| 1084 | function unassignAllUsers()
|
| 1085 | {
|
| 1086 | $this->_users_quizzesTable =& JTable::getInstance('users_quizzes', 'Table') ; |
| 1087 | |
| 1088 | // unassign the unregistered users
|
| 1089 | $query = 'SELECT *' .
|
| 1090 | ' FROM #__jquarks_users_quizzes AS users_quizzes' .
|
| 1091 | ' WHERE user_id = -1' .
|
| 1092 | ' AND quiz_id = ' . $this->_id ; |
| 1093 | |
| 1094 | $this->_db->setQuery($query) ;
|
| 1095 | $unaffectAll = $this->_db->loadObject() ;
|
| 1096 | |
| 1097 | // checking if it has already been taken
|
| 1098 | $taken = $this->tookQuiz($unaffectAll->id) ;
|
| 1099 | |
| 1100 | if($taken) {
|
| 1101 | $this->archiveAssignation($unaffectAll->id) ; // archieve it |
| 1102 | } |
| 1103 | else
|
| 1104 | {
|
| 1105 | if (!$this->_users_quizzesTable->delete( $unaffectAll->id )) // delete it |
| 1106 | {
|
| 1107 | $this->setError( $this->_db->getErrorMsg() ) ; |
| 1108 | return false ; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | if(!$this->unassignUsers()) { // unassign all registered users |
| 1113 | return false ; |
| 1114 | } |
| 1115 | |
| 1116 | return true ; |
| 1117 | } |
| 1118 | |
| 1119 | /**
|
| 1120 | * Send a notification message to the user |
| 1121 | * |
| 1122 | * @param $quizId |
| 1123 | * @param $userId |
| 1124 | * @return boolean |
| 1125 | */ |
| 1126 | function notifyUser($quizId = null, $userId = null) |
| 1127 | {
|
| 1128 | if ($quizId) {
|
| 1129 | $this->_id = $quizId ;
|
| 1130 | } |
| 1131 | |
| 1132 | $link = JURI::root() . 'index.php?option=com_jquarks&controller=quiz&id=' . $this->_id ; |
| 1133 | |
| 1134 | if ($userId) {
|
| 1135 | $uid = $userId ; |
| 1136 | } else {
|
| 1137 | $uid = (int)JRequest::getVar('selected_user') ;// $uids[0] ; |
| 1138 | } |
| 1139 | |
| 1140 | $this->_quiz = $this->getQuiz() ; |
| 1141 | |
| 1142 | $user = JFactory::getUser($uid) ; |
| 1143 | $message =& JFactory::getMailer() ; |
| 1144 | |
| 1145 | $message->addRecipient($user->email) ; |
| 1146 | $message->setSubject( '"' . $this->_quiz->title . '" ' . JText::_('QUIZ_IS_AVAILABLE')) ; |
| 1147 | |
| 1148 | $notify_message = $this->_quiz->notify_message ;
|
| 1149 | |
| 1150 | $notify_message = str_replace("[userName]", $user->name, $notify_message) ;
|
| 1151 | $notify_message = str_replace("[quizTitle]", $this->_quiz->title, $notify_message) ; |
| 1152 | $notify_message = str_replace("[quizDescription]", $this->_quiz->description, $notify_message) ; |
| 1153 | |
| 1154 | if ($this->_quiz->publish_down == '0000-00-00 00:00:00') { |
| 1155 | $notify_message = str_replace("[unpublishDate]", JText::_('Never'), $notify_message) ; |
| 1156 | } else {
|
| 1157 | $notify_message = str_replace("[unpublishDate]", $this->_quiz->publish_down, $notify_message) ; |
| 1158 | } |
| 1159 | |
| 1160 | $notify_message = str_replace("[quizLink]", $link, $notify_message) ;
|
| 1161 | |
| 1162 | $body = $notify_message ; |
| 1163 | $message->setBody($body) ; |
| 1164 | |
| 1165 | $sent = $message->send(); |
| 1166 | if ($sent != 1) { |
| 1167 | return false ; |
| 1168 | } |
| 1169 | |
| 1170 | return true ; |
| 1171 | } |
| 1172 | |
| 1173 | /**
|
| 1174 | * Notify all the users assigned to a quiz |
| 1175 | * |
| 1176 | * @return boolean |
| 1177 | */ |
| 1178 | function notifyAll()
|
| 1179 | {
|
| 1180 | if (empty($this->_quiz)) { |
| 1181 | $this->getQuiz();
|
| 1182 | } |
| 1183 | |
| 1184 | if(empty($this->_assignedUsers)) { |
| 1185 | $this->getAssignedUsers();
|
| 1186 | } |
| 1187 | |
| 1188 | foreach ($this->_assignedUsers as $user) |
| 1189 | {
|
| 1190 | if ($user->assigned_id)
|
| 1191 | {
|
| 1192 | if (!$this->notifyUser($this->_quiz->id, $user->id)) { |
| 1193 | return false; |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | function storeNotif()
|
| 1202 | {
|
| 1203 | $this->_row =& $this->getTable(); |
| 1204 | |
| 1205 | $post = JRequest::get('post') ;
|
| 1206 | $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' ) ; |
| 1207 | |
| 1208 | $data['id'] = $cids[0] ; |
| 1209 | $data['published'] = $post['published'] ; |
| 1210 | $data['notify_message'] = $post['notifMess'] ; |
| 1211 | |
| 1212 | return $this->_row->save($data) ; |
| 1213 | } |
| 1214 | |
| 1215 | function store()
|
| 1216 | {
|
| 1217 | $this->_row =& $this->getTable(); |
| 1218 | |
| 1219 | $post = JRequest::get('post') ;
|
| 1220 | |
| 1221 | $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' ) ; |
| 1222 | $details = JRequest::getVar( 'details', array(0), 'post', 'array' ) ; |
| 1223 | |
| 1224 | $data['id'] = $cids[0] ; |
| 1225 | $data['title'] = $post['title'] ; |
| 1226 | $data['description'] = $post['description'] ; |
| 1227 | $data['access_id'] = $post['access'] ; |
| 1228 | $data['published'] = $post['published'] ; |
| 1229 | $data['publish_up'] = $details['publish_up'] ; |
| 1230 | $data['time_limit'] = $post['time_limit'] ; |
| 1231 | $data['show_results'] = isset($post['show_results']) ? 1 : 0 ; |
| 1232 | |
| 1233 | if (array_key_exists('unique_session', $post) && 1 == $data['access_id']) { |
| 1234 | $data['unique_session'] = 1 ; |
| 1235 | } |
| 1236 | |
| 1237 | if ($data['time_limit'] == '') { |
| 1238 | $data['time_limit'] = null ; |
| 1239 | } |
| 1240 | |
| 1241 | $data['paginate'] = 'use_pagination=' . $details['paginate'] |
| 1242 | . ' use_slide=' . $details['slide'] |
| 1243 | . ' question_page=' . $details['questionPage'] ; |
| 1244 | |
| 1245 | if ($details['publish_down'] == JText::_('Never') || $details['publish_down'] == "") { |
| 1246 | $data['publish_down'] = "0000-00-00 00:00:00" ; |
| 1247 | } else {
|
| 1248 | $data['publish_down'] = $details['publish_down'] ; |
| 1249 | } |
| 1250 | |
| 1251 | // we have a new quiz
|
| 1252 | $oldAccess = '-1' ;
|
| 1253 | |
| 1254 | if ( $data['id'] != 0 ) // case of an old quiz |
| 1255 | {
|
| 1256 | $query = 'SELECT qui.access_id' .
|
| 1257 | ' FROM #__jquarks_quizzes AS qui' .
|
| 1258 | ' where qui.id = ' . $data['id'] ; |
| 1259 | |
| 1260 | $this->_db->setQuery($query) ;
|
| 1261 | $oldAccess = $this->_db->loadResult() ;
|
| 1262 | } |
| 1263 | |
| 1264 | $data['notify_message'] = $post['notify_message'] ; |
| 1265 | |
| 1266 | if (!$this->_row->bind($data)) |
| 1267 | {
|
| 1268 | echo $this->_db->getErrorMsg() ; |
| 1269 | return false ; |
| 1270 | } |
| 1271 | |
| 1272 | if (!$this->_row->store(true)) // true to update the null values |
| 1273 | {
|
| 1274 | echo $this->_db->getErrorMsg() ; |
| 1275 | return false ; |
| 1276 | } |
| 1277 | |
| 1278 | if ($oldAccess == -1 ) { |
| 1279 | $this->_id = $this->_db->insertid() ; |
| 1280 | } |
| 1281 | |
| 1282 | // updating the table users_quizzes
|
| 1283 | if($data['access_id'] != $oldAccess ) |
| 1284 | {
|
| 1285 | // if this is the first save then oldAccess->access_id will be null
|
| 1286 | if ( $data['access_id'] == 0 ) |
| 1287 | {
|
| 1288 | // we are switching to public quiz from registred quiz
|
| 1289 | // assign all user registered and not registered (user_id -1)
|
| 1290 | if(!$this->assignAllUsers()) { |
| 1291 | return false ; |
| 1292 | } |
| 1293 | } |
| 1294 | elseif ( $data['access_id'] == 1 ) |
| 1295 | {
|
| 1296 | // we are switching to a non public quiz from a public quiz
|
| 1297 | // unassigning all users
|
| 1298 | if(!$this->unassignAllUsers()) { |
| 1299 | return false; |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | return true ; |
| 1305 | } |
| 1306 | |
| 1307 | } |