diff --git a/quiz.admin.inc b/quiz.admin.inc index 22c57e0..48de0d6 100755 --- a/quiz.admin.inc +++ b/quiz.admin.inc @@ -757,7 +757,7 @@ function _quiz_search_terms($start, $all = FALSE) { * @return * HTML output to create page. */ -function quiz_questions_form($form, $form_state, $quiz) { +function quiz_questions_form($form, &$form_state, $quiz) { if ($form_state['rebuild']) { // Save the active filters in $_SESSION $filters = $form_state['values']['browser']['table']['header']['filters']; @@ -815,12 +815,34 @@ function quiz_questions_form($form, $form_state, $quiz) { } // We add the browser and allows the browser to give us information on what questions are displayed in the browser... - $hidden_questions = array(); - $form['question_list']['browser'] = _quiz_question_browser_form($hidden_questions, $questions_to_add, $form_state, $quiz, $types); - // We add the questions from the browser as hidden question rows in the question list. Doing this we can have - // the question show up in the question list instantly when a question is chosen in the browser(using js). - _quiz_add_hidden_questions($questions, $hidden_questions, $form_state, $quiz); - // We add the questions to the form array + $new_questions = array(); + // Save all triggered questions in form_state to have access after page change. + if (strpos($form_state['triggering_element']['#name'], 'browser[table][body][titles]') !== FALSE) { + $form_state['questions'][$form_state['triggering_element']['#return_value']] = $form_state['triggering_element']['#value']; + } + if ($form_state['triggering_element']['#name'] == 'browser[table][header][filters][all]' && !empty($form_state['values']['browser']['table']['body']['titles'])) { + foreach ($form_state['values']['browser']['table']['body']['titles'] as $qid => $value) { + if ($form_state['triggering_element']['#value'] == 1) { + $form_state['questions'][$qid] = $qid; + } + else { + unset($form_state['questions'][$qid]); + } + } + } + // Build new added questions array and keep only checked items. + // Unchecked items will return 0 value. + if (!empty($form_state['questions'])) { + foreach ($form_state['questions'] as $qid => $value) { + if ($value != 0) { + $new_questions[] = $qid; + } + } + } + $form['question_list']['browser'] = _quiz_question_browser_form($questions_to_add, $form_state, $quiz, $types); + // Add new questions to all questions list. + _quiz_add_hidden_questions($questions, $new_questions, $form_state, $quiz); + // Add questions to form, so we will have selected items on form save. _quiz_add_questions_to_form($form, $questions, $quiz, $types); // Show the number of questions in the table header. @@ -1043,7 +1065,7 @@ function _quiz_add_hidden_questions(&$questions, &$hidden_questions, &$form_stat $vids[] = $cur_question->vid = $vid; $cur_question->weight = 0; $cur_question->question_status = ($quiz->randomization == 2) ? QUESTION_RANDOM : QUESTION_ALWAYS; - $cur_question->staying = isset($form_state['values']['stayers'][$id]) ? $form_state['values']['stayers'][$id] === '1' : FALSE; + $cur_question->staying = TRUE; $cur_questions[$cur_question->nid] = $cur_question; } if (count($vids) > 0) { @@ -1063,7 +1085,9 @@ function _quiz_add_hidden_questions(&$questions, &$hidden_questions, &$form_stat $cur_questions[$res_o->nid]->title = $res_o->title; $cur_questions[$res_o->nid]->max_score = $res_o->type == 'scale' ? 0 : $res_o->max_score; $cur_questions[$res_o->nid]->latest_vid = $res_o->latest_vid; - $questions[] = $cur_questions[$res_o->nid]; + } + foreach ($cur_questions as $cur_qid => $cur_q) { + $questions[] = $cur_q; } } } @@ -1227,8 +1251,6 @@ function _quiz_add_revision_checkbox(&$form, &$quiz) { /** * Creates the browser part of the quiz_questions_form * - * @param $hidden_questions - * Array where we add the questions in the browser * @param $questions * Questions already added to the question list(array) * @param $form_state @@ -1238,7 +1260,7 @@ function _quiz_add_revision_checkbox(&$form, &$quiz) { * @return form * FAPI form(array) */ -function _quiz_question_browser_form(&$hidden_questions, $questions, $form_state, $quiz, $question_types) { +function _quiz_question_browser_form($questions, &$form_state, $quiz, $question_types) { if (!is_array($question_types) || count($question_types) == 0) { return $form['no_questions'] = array( '#markup' => t('No question types are enabled'), @@ -1331,7 +1353,6 @@ function _quiz_question_browser_form(&$hidden_questions, $questions, $form_state } } // Clone query for hidden questions block. - $hidden_questions_query = clone $query; $query = $query->extend('PagerDefault')->extend('TableSort') ->orderByHeader($browser['header']['#header']) ->limit(20); @@ -1344,11 +1365,14 @@ function _quiz_question_browser_form(&$hidden_questions, $questions, $form_state $browser['body']['types'][$id]['#value'] = $question_types[$res_o->type]['name']; $browser['body']['names'][$id]['#value'] = check_plain($res_o->name); } - // Fill in hidden questions block. - foreach ($hidden_questions_query->execute() as $res_ob) { - $id = $res_ob->nid . '-' . $res_ob->vid; - // Add $id to hidden_questions, this way quiz_questions_form knows that it has to add a invisible row for this question. - $hidden_questions[] = $id; + + // Mark checkboxes as checked for items that are already added. + if (!empty($form_state['questions'])) { + foreach ($form_state['questions'] as $qid => $value) { + if ($value != 0) { + $form_state['input']['browser']['table']['body']['titles'][$qid] = $qid; + } + } } $browser['body']['titles'] = array( @@ -1357,6 +1381,13 @@ function _quiz_question_browser_form(&$hidden_questions, $questions, $form_state '#options' => $options, '#attributes' => array('class' => array('quiz-browser-checkbox')), '#default_value' => $questions, + '#ajax' => array( + 'callback' => '_quiz_question_browser_select_item_callback', + 'wrapper' => 'question-list', + 'progress' => array( + 'message' => '', + ), + ), ); $browser['pager'] = array( @@ -1365,6 +1396,51 @@ function _quiz_question_browser_form(&$hidden_questions, $questions, $form_state return $form; } +/** + * Custom AJAX callback for checkboxes on question row. + * + * @param $form + * @param $form_state + * @return string + */ +function _quiz_question_browser_select_item_callback($form, $form_state) { + $data = $form['question_list']; + $headers = array( + t('Question'), + t('Type'), + t('Actions'), + t('Update'), + t('Max score'), + t('Auto update max score') + ); + if (isset($form['compulsories'])) { + $headers[] = t('Compulsory'); + } + $headers[] = t('Weight'); + + // Building table body + $rows = array(); + if (!empty($data['titles'])) { + foreach (element_children($data['titles']) as $id) { + $data['weights'][$id]['#attributes']['class'] = array('question-list-weight'); + + $rows[] = _quiz_get_question_row($data, $id); + } + } + + $html_attr = array('id' => 'question-list'); + + $table = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => $html_attr)); + $newest_id = ''; + if (strpos($form_state['triggering_element']['#name'], 'browser[table][body][titles]') !== FALSE && !is_null($form_state['triggering_element']['#value'])) { + $newest_id = $form_state['triggering_element']['#value']; + } + $commands = array(); + $commands[] = ajax_command_remove('.tabledrag-toggle-weight'); + $commands[] = ajax_command_html('#question-list', $table); + $commands[] = ajax_command_settings(array('newest_question' => $newest_id), FALSE); + return array('#type' => 'ajax', '#commands' => $commands); +} /** * adds filter fields to the question browser form @@ -1390,6 +1466,10 @@ function _quiz_question_browser_add_filter_fields(&$browser, &$question_types, $ $filters['all'] = array( '#type' => 'checkbox', + '#ajax' => array( + 'callback' => '_quiz_question_browser_select_item_callback', + 'wrapper' => 'question-list', + ), ); $pre = 'quiz_question_browser_'; diff --git a/theme/quiz_question_browser.js b/theme/quiz_question_browser.js index f70c1d3..8a6cc5e 100755 --- a/theme/quiz_question_browser.js +++ b/theme/quiz_question_browser.js @@ -40,14 +40,6 @@ Drupal.behaviors.quizQuestionBrowserBehavior = { } }); - // Select previously selected questions after AJAX. - $('.quiz-question-browser-row', context).each(function() { - var idToShow = Quiz.findNidVidString(this.id); - if ($('#q-' + idToShow).not('.hidden-question').length) { - $(this).click(); - } - }); - // Question rows in the browser $('.quiz-question-browser-row') .once() @@ -63,7 +55,7 @@ Drupal.behaviors.quizQuestionBrowserBehavior = { if (event.target.type !== 'checkbox') { $(':checkbox', this).attr('checked', function() { return !this.checked; - }); + }).trigger('change'); } var idToShow = Quiz.findNidVidString(this.id); var oldHeight = $(document).height(); @@ -71,17 +63,16 @@ Drupal.behaviors.quizQuestionBrowserBehavior = { // Show the question in the question list $('#question-list').css('display', 'table'); $('#no-questions').hide(); - $('#q-' + idToShow).removeClass('hidden-question'); - } else { - // Hide the question in the question list - $('#q-' + idToShow).addClass('hidden-question'); } $('#edit-stayers-' + idToShow).attr('checked', ($('#q-' + idToShow).hasClass('hidden-question')) ? false : true); - Quiz.fixColorAndWeight($('#q-' + idToShow)); var toScroll = $(document).height() - oldHeight; window.scrollBy(0, toScroll); }); + if ($('#question-list', context).length && typeof settings.newest_question != 'undefined' && settings.newest_question != '') { + Quiz.fixColorAndWeight($('#q-' + settings.newest_question)); + } + // Filter row in the browser // Mark all button @@ -89,16 +80,13 @@ Drupal.behaviors.quizQuestionBrowserBehavior = { .once() .click(function(event) { var ch = $(this).attr('checked'); - $('.quiz-question-browser-row').each(function() { + // Questions will be loaded using AJAX. We need just to check items as selected. + $('.quiz-browser-checkbox').each(function() { if (!ch) { - $(this).filter(':has(:checkbox:checked)').each(function() { - $(this).click(); - }); + $(this).not(':not(:checked)').click(); } else { - $(this).filter(':has(:checkbox:not(:checked))').each(function() { - $(this).click(); - }); + $(this).not(':checked').click(); } }); }); @@ -341,7 +329,7 @@ Quiz.fixColorAndWeight = function(newest) { }); - if (numQRows < 2) return; + if (numQRows < 1) return; if (!newest.hasClass(nextClass)) newest.removeClass(lastClass).addClass(nextClass); var newestId = Quiz.findNidVidString(newest.attr('id'));