Skip to content

Commit

Permalink
quiz editing MDL-24750 The Add random button should also let you add …
Browse files Browse the repository at this point in the history
…questions from an existing category.

Also includes a fix for MDL-24749 by Sam Hemelryk. Thanks Sam.

This commit also includes a certain amount of code cleaning up.
  • Loading branch information
timhunt committed Nov 22, 2010
1 parent d2e66a6 commit 94dbfb3
Show file tree
Hide file tree
Showing 23 changed files with 266 additions and 294 deletions.
104 changes: 55 additions & 49 deletions mod/quiz/addrandom.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,82 @@
*/
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/quiz/editlib.php');
require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
require_once($CFG->dirroot . '/question/category_class.php');

list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
question_edit_setup('editq', '/mod/quiz/addrandom.php', true);

// These params are only passed from page request to request while we stay on
// this page otherwise they would go in question_edit_setup
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
$category = optional_param('category', 0, PARAM_INT);

// Get the course object and related bits.
if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
print_error('invalidcourseid');
}
//you need mod/quiz:manage in addition to question capabilities to access this page.
require_capability('mod/quiz:manage', $contexts->lowest());

$PAGE->set_url($thispageurl);

if ($returnurl) {
$returnurl = new moodle_url($returnurl);
} else {
$returnurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cmid));
}

$defaultcategoryobj = question_make_default_categories($contexts->all());
$defaultcategoryid = $defaultcategoryobj->id;
$defaultcategorycontext = $defaultcategoryobj->contextid;
$defaultcategory = "$defaultcategoryid, $defaultcategorycontext";
$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;

$qcobject = new question_category_object(
$pagevars['cpage'],
$thispageurl,
$contexts->having_one_edit_tab_cap('categories'),
$defaultcategoryid,
$defaultcategoryobj->id,
$defaultcategory,
null,
$contexts->having_cap('moodle/question:add'));

//setting the second parameter of process_randomquestion_formdata to true causes it to redirect on success
$newquestioninfo = quiz_process_randomquestion_formdata($qcobject);
if ($newquestioninfo == 'cancelled') {
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
if ($returnurl) {
redirect($CFG->wwwroot . $returnurl);
} else {
redirect($CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $cmid);
}
}
if ($newquestioninfo) {
$newrandomcategory = $newquestioninfo->newrandomcategory;
if (!$newrandomcategory) {
print_error('cannotcreatecategory');
} else {
add_to_log($quiz->course, 'quiz', 'addcategory',
"view.php?id = $cm->id", "$newrandomcategory", $cm->id);
redirect($CFG->wwwroot . "/mod/quiz/edit.php?cmid=$cmid&addonpage=$newquestioninfo->addonpage&addrandom=1&categoryid=$newquestioninfo->newrandomcategory&randomcount=1&sesskey=" . sesskey());
}
$mform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts);

if ($mform->is_cancelled()) {
redirect($returnurl);
}

//these params are only passed from page request to request while we stay on this page
//otherwise they would go in question_edit_setup
$quiz_page = optional_param('quiz_page', 0, PARAM_SEQUENCE);
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
if ($data = $mform->get_data()) {
if (!empty($data->existingcategory)) {
list($categoryid) = explode(',', $data->category);
$includesubcategories = !empty($data->includesubcategories);
$returnurl->param('cat', $data->category);

$url = new moodle_url('/mod/quiz/addrandom.php');
if ($quiz_page != 0) {
$url->param('quiz_page', $quiz_page);
}
if ($returnurl != 0) {
$url->param('returnurl', $returnurl);
}
$PAGE->set_url($url);
} else if (!empty($data->newcategory)) {
list($parentid, $contextid) = explode(',', $data->parent);
$categoryid = $qcobject->add_category($data->parent, $data->name, '', true);
$includesubcategories = 0;
add_to_log($quiz->course, 'quiz', 'addcategory',
'view.php?id=' . $cm->id, $categoryid, $cm->id);
$returnurl->param('cat', $categoryid . ',' . $contextid);

$strquizzes = get_string('modulenameplural', 'quiz');
$strquiz = get_string('modulename', 'quiz');
$streditingquestions = get_string('editquestions', 'quiz');
$streditingquiz = get_string('editinga', 'moodle', $strquiz);
} else {
throw new coding_exception('It seems a form was submitted without any button being pressed???');
}

// Get the course object and related bits.
if (! $course = $DB->get_record('course', array('id' => $quiz->course))) {
print_error('invalidcourseid');
quiz_add_random_questions($quiz, $addonpage, $categoryid, 1, $includesubcategories);
redirect($returnurl);
}
//you need mod/quiz:manage in addition to question capabilities to access this page.
require_capability('mod/quiz:manage', $contexts->lowest());

// Print basic page layout.
$mform->set_data(array(
'addonpage' => $addonpage,
'returnurl' => $returnurl,
'cmid' => $cm->id,
'category' => $category,
));

// Setup $PAGE.
$streditingquiz = get_string('editinga', 'moodle', get_string('modulename', 'quiz'));
$PAGE->navbar->add($streditingquiz);
$PAGE->set_title($streditingquiz);
$PAGE->set_heading($course->fullname);
Expand All @@ -87,9 +96,6 @@
}

echo $OUTPUT->heading(get_string('addrandomquestiontoquiz', 'quiz', $quizname), 2, 'mdl-left');

$addonpage = optional_param('addonpage_form', 0, PARAM_SEQUENCE);
$qcobject->display_randomquestion_user_interface($addonpage);

$mform->display();
echo $OUTPUT->footer();

57 changes: 57 additions & 0 deletions mod/quiz/addrandomform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

require_once($CFG->libdir.'/formslib.php');

class quiz_add_random_form extends moodleform {

function definition() {
global $CFG, $DB;
$mform =& $this->_form;

$contexts = $this->_customdata;

//--------------------------------------------------------------------------------
$mform->addElement('header', 'categoryheader', get_string('randomfromexistingcategory', 'quiz'));

$mform->addElement('questioncategory', 'category', get_string('category'),
array('contexts' => $contexts->all(), 'top' => false));

$mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));

$mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));

//--------------------------------------------------------------------------------
$mform->addElement('header', 'categoryheader', get_string('randomquestionusinganewcategory', 'quiz'));

$mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
$mform->setType('name', PARAM_MULTILANG);

$mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
array('contexts' => $contexts->all(), 'top' => true));
$mform->addHelpButton('parent', 'parentcategory', 'question');

$mform->addElement('submit', 'newcategory', get_string('createcategoryandaddrandomquestion', 'quiz'));

//--------------------------------------------------------------------------------
$mform->addElement('cancel');
$mform->closeHeaderBefore('cancel');

$mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
$mform->setType('addonpage', PARAM_SEQUENCE);
$mform->addElement('hidden', 'cmid', 0);
$mform->setType('cmid', PARAM_INT);
$mform->addElement('hidden', 'returnurl', 0);
$mform->setType('returnurl', PARAM_LOCALURL);
}

function validation($fromform, $files) {
$errors = parent::validation($fromform, $files);

if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
$errors['name'] = get_string('categorynamecantbeblank', 'quiz');
}

return $errors;
}
}

20 changes: 19 additions & 1 deletion mod/quiz/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function quiz_edit_init() {

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

var randomquestiondialog = YAHOO.util.Dom.get('randomquestiondialog');
YAHOO.util.Dom.get(document.body).appendChild(randomquestiondialog);

quiz_edit.randomquestiondialog = new YAHOO.widget.Dialog('randomquestiondialog', {
modal: true,
width: '100%',
Expand All @@ -28,7 +31,7 @@ function quiz_edit_init() {
// Transfer the page number from the button form to the pop-up form.
var addrandombutton = YAHOO.util.Event.getTarget(e);
var addpagehidden = YAHOO.util.Dom.getElementsByClassName('addonpage_formelement', 'input', addrandombutton.form);
document.getElementById('rform_qpage').value = addpagehidden.value;
document.getElementById('rform_qpage').value = addpagehidden[0].value;

// Show the dialogue and stop the default action.
quiz_edit.randomquestiondialog.show();
Expand All @@ -46,6 +49,10 @@ function quiz_edit_init() {
YAHOO.util.Event.preventDefault(e);
});

YAHOO.util.Event.addListener('id_existingcategory', 'click', quiz_yui_workaround);

YAHOO.util.Event.addListener('id_newcategory', 'click', quiz_yui_workaround);

// Repaginate dialogue -----------------------------------------------------
quiz_edit.repaginatedialog = new YAHOO.widget.Dialog('repaginatedialog', {
modal: true,
Expand Down Expand Up @@ -88,6 +95,17 @@ function quiz_edit_init() {
}
}

function quiz_yui_workaround(e) {
// YUI does not send the button pressed with the form submission, so copy
// the button name to a hidden input.
var submitbutton = YAHOO.util.Event.getTarget(e);
var input = document.createElement('input');
input.type = 'hidden';
input.name = submitbutton.name;
input.value = 1;
submitbutton.form.appendChild(input);
}

// Initialise everything on the quiz settings form.
function quiz_settings_init() {
var repaginatecheckbox = document.getElementById('id_repaginatenow');
Expand Down
Loading

0 comments on commit 94dbfb3

Please sign in to comment.