Skip to content

Commit

Permalink
MDL-20636 Preserve scroll position when doing action on the quiz edit…
Browse files Browse the repository at this point in the history
… page.
  • Loading branch information
timhunt committed Feb 11, 2011
1 parent f9b0500 commit fd214b5
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 90 deletions.
22 changes: 8 additions & 14 deletions mod/quiz/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@

// Initialise everything on the quiz edit/order and paging page.
var quiz_edit = {};
function quiz_edit_init() {
function quiz_edit_init(Y) {
M.core_scroll_manager.scroll_to_saved_pos(Y);
Y.on('submit', function(e) {
M.core_scroll_manager.save_scroll_pos(Y, 'id_existingcategory');
}, '#mform1');
Y.on('submit', function(e) {
M.core_scroll_manager.save_scroll_pos(Y, e.target.get('firstChild'));
}, '.quizsavegradesform');

// Add random question dialogue --------------------------------------------

var randomquestiondialog = YAHOO.util.Dom.get('randomquestiondialog');
if (randomquestiondialog) {
YAHOO.util.Dom.get(document.body).appendChild(randomquestiondialog);
Expand Down Expand Up @@ -148,15 +154,3 @@ function quiz_settings_init() {
}, 50);
});
}

// Depending on which page this is, do the appropriate initialisation.
function quiz_edit_generic_init() {
switch (document.body.id) {
case 'page-mod-quiz-edit':
quiz_edit_init();
break;
case 'page-mod-quiz-mod':
quiz_settings_init();
}
}
YAHOO.util.Event.onDOMReady(quiz_edit_generic_init);
31 changes: 19 additions & 12 deletions mod/quiz/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
//this page otherwise they would go in question_edit_setup
$quiz_reordertool = optional_param('reordertool', -1, PARAM_BOOL);
$quiz_qbanktool = optional_param('qbanktool', -1, PARAM_BOOL);
$scrollpos = optional_param('scrollpos', '', PARAM_INT);

list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
question_edit_setup('editq', '/mod/quiz/edit.php', true);
Expand Down Expand Up @@ -183,18 +184,22 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
}
}

$afteractionurl = new moodle_url($thispageurl);
if ($scrollpos) {
$afteractionurl->param('scrollpos', $scrollpos);
}
if (($up = optional_param('up', false, PARAM_INT)) && confirm_sesskey()) {
$quiz->questions = quiz_move_question_up($quiz->questions, $up);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if (($down = optional_param('down', false, PARAM_INT)) && confirm_sesskey()) {
$quiz->questions = quiz_move_question_down($quiz->questions, $down);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
Expand All @@ -203,7 +208,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
$quiz->questions = quiz_repaginate($quiz->questions, $questionsperpage );
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
Expand All @@ -213,7 +218,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
$thispageurl->param('lastchanged', $addquestion);
redirect($thispageurl);
redirect($afteractionurl);
}

if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
Expand All @@ -227,7 +232,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
}
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
Expand All @@ -240,7 +245,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo

quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if (optional_param('addnewpagesafterselected', null, PARAM_CLEAN) && !empty($selectedquestionids) && confirm_sesskey()) {
Expand All @@ -249,31 +254,31 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
}
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

$addpage = optional_param('addpage', false, PARAM_INT);
if ($addpage !== false && confirm_sesskey()) {
$quiz->questions = quiz_add_page_break_at($quiz->questions, $addpage);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

$deleteemptypage = optional_param('deleteemptypage', false, PARAM_INT);
if (($deleteemptypage !== false) && confirm_sesskey()) {
$quiz->questions = quiz_delete_empty_page($quiz->questions, $deleteemptypage);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

$remove = optional_param('remove', false, PARAM_INT);
if (($remove = optional_param('remove', false, PARAM_INT)) && confirm_sesskey()) {
quiz_remove_question($quiz, $remove);
quiz_update_sumgrades($quiz);
quiz_delete_previews($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if (optional_param('quizdeleteselected', false, PARAM_BOOL) && !empty($selectedquestionids) && confirm_sesskey()) {
Expand All @@ -282,7 +287,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
}
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
redirect($thispageurl);
redirect($afteractionurl);
}

if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
Expand Down Expand Up @@ -375,7 +380,7 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
quiz_update_all_final_grades($quiz);
quiz_update_grades($quiz, 0, true);
}
redirect($thispageurl);
redirect($afteractionurl);
}

$questionbank->process_actions($thispageurl, $cm);
Expand Down Expand Up @@ -403,7 +408,9 @@ function module_specific_controls($totalnumber, $recurse, $category, $cmid, $cmo
$quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
}
$PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
$PAGE->requires->js('/question/qengine.js');
$PAGE->requires->js('/mod/quiz/edit.js');
$PAGE->requires->js_init_call('quiz_edit_init');

// Print the tabs to switch mode.
if ($quiz_reordertool) {
Expand Down
62 changes: 31 additions & 31 deletions mod/quiz/editlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,12 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, $inc
}

// More random questions are needed, create them.
$form->questiontext = array('text' => $includesubcategories, 'format' => 0);
$form->defaultmark = 1;
$form->hidden = 1;
for ($i = 0; $i < $number; $i += 1) {
$form = new stdClass();
$form->questiontext = array('text' => $includesubcategories, 'format' => 0);
$form->category = $category->id . ',' . $category->contextid;
$form->defaultmark = 1;
$form->hidden = 1;
$form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question = new stdClass;
$question->qtype = 'random';
Expand Down Expand Up @@ -256,7 +257,8 @@ function quiz_save_new_layout($quiz) {
* @param integer $quizid The id of the quiz to update / add the instances for.
*/
function quiz_update_question_instance($grade, $questionid, $quiz) {
$instance = $DB->get_record('quiz_question_instances', array('quiz' => $quizid,
global $DB;
$instance = $DB->get_record('quiz_question_instances', array('quiz' => $quiz->id,
'question' => $questionid));
$slot = quiz_get_slot_for_question($quiz, $questionid);

Expand Down Expand Up @@ -467,7 +469,7 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
$questions[$qnum] = $fakequestion;
$quiz->grades[$qnum] = 0;

} else if ($qnum and question_bank::qtype_exists($questions[$qnum]->qtype)) {
} else if ($qnum && !question_bank::qtype_exists($questions[$qnum]->qtype)) {
$questions[$qnum]->qtype = 'missingtype';
}

Expand All @@ -489,10 +491,10 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
echo '</div>';
if ($allowdelete) {
echo '<div class="quizpagedelete">';
echo '<a title="' . get_string('removeemptypage', 'quiz') . '" href="' .
$pageurl->out(true, array('deleteemptypage' => $count - 1, 'sesskey'=>sesskey())) .
'"><img src="' . $OUTPUT->pix_url('t/delete') . '" ' .
'class="iconsmall" alt="' . $strremove . '" /></a>';
echo $OUTPUT->action_icon($pageurl->out(true, array('deleteemptypage' => $count - 1, 'sesskey'=>sesskey())),
new pix_icon('t/delete', $strremove),
new component_action('click', 'M.core_scroll_manager.save_scroll_action'),
array('title' => $strremove));
echo '</div>';
}
}
Expand Down Expand Up @@ -552,36 +554,36 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
if ($count >= $lastindex - 1) {
$upbuttonclass = 'upwithoutdown';
}
echo "<a title=\"$strmoveup\" href=\"" .
$pageurl->out(true, array('up' => $question->id, 'sesskey'=>sesskey())) . "\"><img
src=\"" . $OUTPUT->pix_url('t/up') . "\" class=\"iconsmall
$upbuttonclass\" alt=\"$strmoveup\" /></a>";
echo $OUTPUT->action_icon($pageurl->out(true, array('up' => $question->id, 'sesskey'=>sesskey())),
new pix_icon('t/up', $strmoveup),
new component_action('click', 'M.core_scroll_manager.save_scroll_action'),
array('title' => $strmoveup));
}

}
if ($count < $lastindex - 1) {
if (!$hasattempts) {
echo "<a title=\"$strmovedown\" href=\"" .
$pageurl->out(true, array('down' => $question->id, 'sesskey'=>sesskey())) . "\"><img
src=\"" . $OUTPUT->pix_url('t/down') . "\" class=\"iconsmall\"" .
" alt=\"$strmovedown\" /></a>";
echo $OUTPUT->action_icon($pageurl->out(true, array('down' => $question->id, 'sesskey'=>sesskey())),
new pix_icon('t/down', $strmovedown),
new component_action('click', 'M.core_scroll_manager.save_scroll_action'),
array('title' => $strmovedown));
}
}
if ($allowdelete && (empty($question->id) || question_has_capability_on($question, 'use', $question->category))) {
// remove from quiz, not question delete.
if (!$hasattempts) {
echo "<a title=\"$strremove\" href=\"" .
$pageurl->out(true, array('remove' => $question->id, 'sesskey'=>sesskey())) . "\">
<img src=\"" . $OUTPUT->pix_url('t/delete') . "\" " .
"class=\"iconsmall\" alt=\"$strremove\" /></a>";
echo $OUTPUT->action_icon($pageurl->out(true, array('remove' => $question->id, 'sesskey'=>sesskey())),
new pix_icon('t/delete', $strremove),
new component_action('click', 'M.core_scroll_manager.save_scroll_action'),
array('title' => $strremove));
}
}
?>
</div><?php
if ($question->qtype != 'description' && !$reordertool) {
?>
<div class="points">
<form method="post" action="edit.php"><div>
<form method="post" action="edit.php" class="quizsavegradesform"><div>
<fieldset class="invisiblefieldset" style="display: block;">
<label for="<?php echo "inputq$question->id" ?>"><?php echo $strgrade; ?></label>:<br />
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
Expand Down Expand Up @@ -660,7 +662,9 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
if (!$reordertool && !$quiz->shufflequestions) {
echo $OUTPUT->container_start('addpage');
$url = new moodle_url($pageurl->out_omit_querystring(), array('cmid' => $quiz->cmid, 'courseid' => $quiz->course, 'addpage' => $count, 'sesskey' => sesskey()));
echo $OUTPUT->single_button($url, get_string('addpagehere', 'quiz'), 'get', array('disabled'=>$hasattempts));
echo $OUTPUT->single_button($url, get_string('addpagehere', 'quiz'), 'post',
array('disabled' => $hasattempts,
'actions' => array(new component_action('click', 'M.core_scroll_manager.save_scroll_action'))));
echo $OUTPUT->container_end();
}
$pageopen = false;
Expand Down Expand Up @@ -786,7 +790,7 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz, $quiz_qbanktoo
echo '<span class="questionpreview">' . quiz_question_preview_button($quiz, $question, true) . '</span>';
echo '</div>';

$questionids = question_bank::get_qtype('random')->get_usable_questions_from_category(
$questionids = question_bank::get_qtype('random')->get_available_questions_from_category(
$category->id, $question->questiontext == '1', '0');
$questioncount = count($questionids);

Expand All @@ -808,11 +812,7 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz, $quiz_qbanktoo
// Category has questions

// Get a sample from the database,
$toshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM);
$questionidstoshow = array();
foreach ($toshow as $a) {
$questionidstoshow[] = $a->id;
}
$questionidstoshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM);
$questionstoshow = $DB->get_records_list('question', 'id', $questionidstoshow,
'', 'id,qtype,name,questiontext,questiontextformat');

Expand Down Expand Up @@ -874,7 +874,7 @@ function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz) {
echo $OUTPUT->notification('Random question category not found!');
return;
}
$questioncount = count(question_bank::get_qtype('random')->get_usable_questions_from_category(
$questioncount = count(question_bank::get_qtype('random')->get_available_questions_from_category(
$category->id, $question->questiontext == '1', '0'));

$reordercheckboxlabel = '<label for="s' . $question->id . '">';
Expand Down Expand Up @@ -1145,7 +1145,7 @@ protected function display_options($recurse = 1, $showhidden = false, $showquest
function quiz_print_grading_form($quiz, $pageurl, $tabindex) {
global $USER, $OUTPUT;
$strsave = get_string('save', 'quiz');
echo "<form method=\"post\" action=\"edit.php\"><div>";
echo '<form method="post" action="edit.php" class="quizsavegradesform"><div>';
echo '<fieldset class="invisiblefieldset" style="display: block;">';
echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
echo html_writer::input_hidden_params($pageurl);
Expand Down

0 comments on commit fd214b5

Please sign in to comment.