Index: question/question.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/question.php,v retrieving revision 1.84 diff -u -r1.84 question.php --- question/question.php 11 Nov 2010 21:54:22 -0000 1.84 +++ question/question.php 19 Nov 2010 21:18:00 -0000 @@ -248,7 +248,7 @@ } else { // We are acutally saving the question. - $question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow, true); + $question = $QTYPES[$question->qtype]->save_question($question, $fromform); if (!empty($CFG->usetags) && isset($fromform->tags)) { // A wizardpage from multipe pages questiontype like calculated may not // allow editing the question tags, hence the isset($fromform->tags) test. Index: question/category_class.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/category_class.php,v retrieving revision 1.74 diff -u -r1.74 category_class.php --- question/category_class.php 11 Nov 2010 21:54:22 -0000 1.74 +++ question/category_class.php 19 Nov 2010 21:18:00 -0000 @@ -10,9 +10,8 @@ // number of categories to display on page define("QUESTION_PAGE_LENGTH", 25); -require_once("$CFG->libdir/listlib.php"); -require_once("$CFG->dirroot/question/category_form.php"); -require_once("$CFG->dirroot/question/category_form_randomquestion.php"); +require_once($CFG->libdir . '/listlib.php'); +require_once($CFG->dirroot . '/question/category_form.php'); require_once('move_form.php'); class question_category_list extends moodle_list { @@ -111,10 +110,6 @@ * @var question_category_edit_form Object representing form for adding / editing categories. */ var $catform; - /** - * @var question_category_edit_form_randomquestion Object representing simplified form for adding a category in order to add it into a quiz as a random question. - */ - var $catform_rand; /** * Constructor @@ -176,10 +171,8 @@ list($paged, $count) = $this->editlists[$key]->list_from_records($paged, $count); } $this->catform = new question_category_edit_form($this->pageurl, compact('contexts', 'currentcat')); - $this->catform_rand = new question_category_edit_form_randomquestion($this->pageurl, compact('contexts', 'currentcat')); if (!$currentcat){ $this->catform->set_data(array('parent'=>$defaultcategory)); - $this->catform_rand->set_data(array('parent'=>$defaultcategory)); } } /** @@ -198,16 +191,6 @@ echo '
'; } - /** - * Displays the user interface - * - */ - function display_randomquestion_user_interface($addonpage=0) { - $this->catform_rand->set_data(array('addonpage'=>$addonpage)); - /// Interface for adding a new category: - $this->output_new_randomquestion_table(); - - } /** * Outputs a table to allow entry of a new category @@ -217,14 +200,6 @@ } /** - * Outputs a table to allow entry of a new category - */ - function output_new_randomquestion_table() { - $this->catform_rand->display(); - } - - - /** * Outputs a list to allow editing/rearranging of existing categories * * $this->initialize() must have already been called @@ -365,7 +340,7 @@ /** * Creates a new category with given params */ - public function add_category($newparent, $newcategory, $newinfo, $return=false) { + public function add_category($newparent, $newcategory, $newinfo, $return = false) { global $DB; if (empty($newcategory)) { print_error('categorynamecantbeblank', 'quiz'); Index: question/category_form_randomquestion.php =================================================================== RCS file: question/category_form_randomquestion.php diff -N question/category_form_randomquestion.php --- question/category_form_randomquestion.php 2 Aug 2010 21:24:39 -0000 1.6 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,38 +0,0 @@ -libdir.'/formslib.php'); - -class question_category_edit_form_randomquestion extends moodleform { - - function definition() { - global $CFG, $DB; - $mform =& $this->_form; - - $contexts = $this->_customdata['contexts']; - $currentcat = $this->_customdata['currentcat']; -//-------------------------------------------------------------------------------- - $mform->addElement('header', 'categoryheader', get_string('createcategoryfornewrandomquestion', 'quiz')); - - $questioncategoryel = $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'), - array('contexts'=>$contexts, 'top'=>true, 'currentcat'=>$currentcat, 'nochildrenof'=>$currentcat)); - $mform->setType('parent', PARAM_SEQUENCE); - $mform->addHelpButton('parent', 'parentcategory', 'question'); - - $mform->addElement('text','name', get_string('name'),'maxlength="254" size="50"'); - $mform->setDefault('name', ''); - $mform->addRule('name', get_string('categorynamecantbeblank', 'quiz'), 'required', null, 'client'); - $mform->setType('name', PARAM_MULTILANG); - - $mform->addElement('hidden', 'info', ''); - $mform->setType('info', PARAM_MULTILANG); - -//-------------------------------------------------------------------------------- - $this->add_action_buttons(true, get_string('addrandomquestion', 'quiz')); -//-------------------------------------------------------------------------------- - $mform->addElement('hidden', 'id', 0); - $mform->setType('id', PARAM_INT); - $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"'); - $mform->setType('addonpage', PARAM_SEQUENCE); - } -} - Index: question/type/description/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/description/questiontype.php,v retrieving revision 1.23 diff -u -r1.23 questiontype.php --- question/type/description/questiontype.php 11 Nov 2010 17:32:27 -0000 1.23 +++ question/type/description/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -45,11 +45,11 @@ return false; } - function save_question($question, $form, $course) { + function save_question($question, $form) { // Make very sure that descriptions can'e be created with a grade of // anything other than 0. $form->defaultgrade = 0; - return parent::save_question($question, $form, $course); + return parent::save_question($question, $form); } function get_question_options(&$question) { Index: question/type/random/edit_random_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/random/edit_random_form.php,v retrieving revision 1.11 diff -u -r1.11 edit_random_form.php --- question/type/random/edit_random_form.php 10 Aug 2010 09:56:53 -0000 1.11 +++ question/type/random/edit_random_form.php 19 Nov 2010 21:18:02 -0000 @@ -34,7 +34,7 @@ $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'), array('contexts' => $this->contexts->having_cap('moodle/question:useall'))); - $mform->addElement('advcheckbox', 'questiontext', get_string("recurse", "quiz"), null, null, array(0, 1)); + $mform->addElement('advcheckbox', 'questiontext[text]', get_string('recurse', 'quiz'), null, null, array(0, 1)); $mform->addElement('hidden', 'name'); $mform->setType('name', PARAM_ALPHA); @@ -73,13 +73,18 @@ $mform->setDefault('returnurl', 0); $buttonarray = array(); - $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges')); - - $buttonarray[] = &$mform->createElement('cancel'); + $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges')); + $buttonarray[] = $mform->createElement('cancel'); $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); $mform->closeHeaderBefore('buttonar'); } + function set_data($question) { + $question->questiontext = array('text' => $question->questiontext); + // We don't want the complex stuff in the base class to run. + moodleform::set_data($question); + } + function validation($fromform, $files) { //validation of category //is not relevant for this question type Index: question/type/random/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/random/questiontype.php,v retrieving revision 1.39 diff -u -r1.39 questiontype.php --- question/type/random/questiontype.php 24 Oct 2010 10:43:46 -0000 1.39 +++ question/type/random/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -117,11 +117,11 @@ return get_string($string, 'qtype_random', $category->name); } - function save_question($question, $form, $course) { + function save_question($question, $form) { $form->name = ''; // Name is not a required field for random questions, but parent::save_question // Assumes that it is. - return parent::save_question($question, $form, $course); + return parent::save_question($question, $form); } function save_question_options($question) { Index: question/type/multianswer/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/multianswer/questiontype.php,v retrieving revision 1.94 diff -u -r1.94 questiontype.php --- question/type/multianswer/questiontype.php 12 Nov 2010 11:29:53 -0000 1.94 +++ question/type/multianswer/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -130,8 +130,7 @@ $wrapped->parent = $question->id; $previousid = $wrapped->id ; $wrapped->category = $question->category . ',1'; // save_question strips this extra bit off again. - $wrapped = $QTYPES[$wrapped->qtype]->save_question($wrapped, - clone($wrapped), $question->course); + $wrapped = $QTYPES[$wrapped->qtype]->save_question($wrapped, clone($wrapped)); $sequence[] = $wrapped->id; if ($previousid != 0 && $previousid != $wrapped->id ) { // for some reasons a new question has been created @@ -160,7 +159,7 @@ } } - function save_question($authorizedquestion, $form, $course) { + function save_question($authorizedquestion, $form) { $question = qtype_multianswer_extract_question($form->questiontext); if (isset($authorizedquestion->id)) { $question->id = $authorizedquestion->id; @@ -176,7 +175,7 @@ $form->questiontextformat = 0; $form->options = clone($question->options); unset($question->options); - return parent::save_question($question, $form, $course); + return parent::save_question($question, $form); } function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) { @@ -694,7 +693,7 @@ $course = $DB->get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } } Index: mod/quiz/editlib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/editlib.php,v retrieving revision 1.149 diff -u -r1.149 editlib.php --- mod/quiz/editlib.php 18 Oct 2010 15:28:59 -0000 1.149 +++ mod/quiz/editlib.php 19 Nov 2010 21:18:00 -0000 @@ -82,13 +82,13 @@ * Adds a question to a quiz by updating $quiz as well as the * quiz and quiz_question_instances tables. It also adds a page break * if required. - * @return boolean false if the question was already in the quiz - * @param int $id The id of the question to be added - * @param object $quiz The extended quiz object as used by edit.php - * This is updated by this function - * @param int $page Which page in quiz to add the question on; if 0 (default), add at the end + * @param int $id The id of the question to be added + * @param object $quiz The extended quiz object as used by edit.php + * This is updated by this function + * @param int $page Which page in quiz to add the question on. If 0 (default), add at the end + * @return boolean false if the question was already in the quiz */ -function quiz_add_quiz_question($id, &$quiz, $page = 0) { +function quiz_add_quiz_question($id, $quiz, $page = 0) { global $DB; $questions = explode(',', quiz_clean_layout($quiz->questions)); if (in_array($id, $questions)) { @@ -148,6 +148,54 @@ return true; } +function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, $includesubcategories) { + global $DB, $QTYPES; + + $category = $DB->get_record('question_categories', array('id' => $categoryid)); + if (!$category) { + print_error('invalidcategoryid', 'error'); + } + + $catcontext = get_context_instance_by_id($category->contextid); + require_capability('moodle/question:useall', $catcontext); + + // Find existing random questions in this category that are + // not used by any quiz. + if ($existingquestions = $DB->get_records_sql( + "SELECT q.id,q.qtype FROM {question} q + WHERE qtype = '" . RANDOM . "' + AND category = ? + AND " . $DB->sql_compare_text('questiontext') . " = ? + AND NOT EXISTS (SELECT * FROM {quiz_question_instances} WHERE question = q.id) + ORDER BY id", array($category->id, $includesubcategories))) { + // Take as many of these as needed. + while (($existingquestion = array_shift($existingquestions)) && $number > 0) { + quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage); + $number -= 1; + } + } + + if ($number <= 0) { + return; + } + + // More random questions are needed, create them. + $form->questiontext = array('text' => $includesubcategories, 'format' => 0); + $form->defaultgrade = 1; + $form->hidden = 1; + for ($i = 0; $i < $number; $i += 1) { + $form->category = $category->id . ',' . $category->contextid; + $form->stamp = make_unique_id_code(); // Set the unique code (not to be changed) + $question = new stdClass; + $question->qtype = RANDOM; + $question = $QTYPES[RANDOM]->save_question($question, $form); + if (!isset($question->id)) { + print_error('cannotinsertrandomquestion', 'quiz'); + } + quiz_add_quiz_question($question->id, $quiz, $addonpage); + } +} + /** * Add a page break after at particular position$. * @param string $layout the existinng layout, $quiz->questions. @@ -551,8 +599,6 @@ '" tabindex="' . ($lastindex + $qno) . '" />'; ?> - all()); + list($defaultcategoryid) = explode(',', $pageurl->param('cat')); // Create the url the question page will return to $returnurladdtoquiz = new moodle_url($pageurl, array('addonpage' => $page)); @@ -649,7 +695,7 @@ $returnurladdtoquiz = str_replace($CFG->wwwroot, '', $returnurladdtoquiz->out(false)); $newquestionparams = array('returnurl' => $returnurladdtoquiz, 'cmid' => $quiz->cmid, 'appendqnumstring' => 'addquestion'); - create_new_question_button($defaultcategory->id, $newquestionparams, get_string('addaquestion', 'quiz'), + create_new_question_button($defaultcategoryid, $newquestionparams, get_string('addaquestion', 'quiz'), get_string('createquestionandadd', 'quiz'), $hasattempts); if ($hasattempts) { @@ -661,10 +707,11 @@
- + - + + />
@@ -673,44 +720,6 @@ "; } -/** - * Process submitted form data to create a new category for a random question - * This is used by edit.php and addrandom.php - * cmid - * - * @param object $qcobject - * @return object an object with properties newrandomcategory and addonpage if operation successful. - * if operation failed, returns false. - */ -function quiz_process_randomquestion_formdata(&$qcobject) { - global $CFG, $DB; - $newrandomcategory = 0; - $addonpage = 0; - $newquestioninfo = false; - if ($qcobject->catform_rand->is_cancelled()) { - return 'cancelled'; - } else if ($catformdata = $qcobject->catform_rand->get_data()) { - $newquestioninfo = new stdClass; - $addonpage = $catformdata->addonpage; - $newquestioninfo->addonpage = $catformdata->addonpage; - if (!$catformdata->id) {//new category - $newrandomcategory = $qcobject->add_category($catformdata->parent, - $catformdata->name, $catformdata->info, true); - if (!is_null($newrandomcategory)) { - $newquestioninfo->newrandomcategory = $newrandomcategory; - if (! $newcategory = $DB->get_record('question_categories', - array('id' => $newrandomcategory))) { - $newquestioninfo->newrandomcategory = false; - } - } else { - $newquestioninfo->newrandomcategory = false; - } - } else { - $newquestioninfo->newrandomcategory = false; - } - } - return $newquestioninfo; -} /** * Print a given single question in quiz for the edit tab of edit.php. Index: mod/quiz/addrandom.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/addrandom.php,v retrieving revision 1.19 diff -u -r1.19 addrandom.php --- mod/quiz/addrandom.php 21 May 2010 03:15:49 -0000 1.19 +++ mod/quiz/addrandom.php 19 Nov 2010 21:17:59 -0000 @@ -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); - } +$mform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts); + +if ($mform->is_cancelled()) { + redirect($returnurl); } -if ($newquestioninfo) { - $newrandomcategory = $newquestioninfo->newrandomcategory; - if (!$newrandomcategory) { - print_error('cannotcreatecategory'); - } else { + +if ($data = $mform->get_data()) { + if (!empty($data->existingcategory)) { + list($categoryid) = explode(',', $data->category); + $includesubcategories = !empty($data->includesubcategories); + $returnurl->param('cat', $data->category); + + } 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", "$newrandomcategory", $cm->id); - redirect($CFG->wwwroot . "/mod/quiz/edit.php?cmid=$cmid&addonpage=$newquestioninfo->addonpage&addrandom=1&categoryid=$newquestioninfo->newrandomcategory&randomcount=1&sesskey=" . sesskey()); + 'view.php?id=' . $cm->id, $categoryid, $cm->id); + $returnurl->param('cat', $categoryid . ',' . $contextid); + + } else { + throw new coding_exception('It seems a form was submitted without any button being pressed???'); } -} -//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); - -$url = new moodle_url('/mod/quiz/addrandom.php'); -if ($quiz_page != 0) { - $url->param('quiz_page', $quiz_page); + quiz_add_random_questions($quiz, $addonpage, $categoryid, 1, $includesubcategories); + redirect($returnurl); } -if ($returnurl != 0) { - $url->param('returnurl', $returnurl); -} -$PAGE->set_url($url); -$strquizzes = get_string('modulenameplural', 'quiz'); -$strquiz = get_string('modulename', 'quiz'); -$streditingquestions = get_string('editquestions', 'quiz'); -$streditingquiz = get_string('editinga', 'moodle', $strquiz); +$mform->set_data(array( + 'addonpage' => $addonpage, + 'returnurl' => $returnurl, + 'cmid' => $cm->id, + 'category' => $category, +)); -// 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()); - -// Print basic page layout. +// Setup $PAGE. +$streditingquiz = get_string('editinga', 'moodle', get_string('modulename', 'quiz')); $PAGE->navbar->add($streditingquiz); $PAGE->set_title($streditingquiz); $PAGE->set_heading($course->fullname); @@ -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(); Index: mod/quiz/edit.js =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/edit.js,v retrieving revision 1.12 diff -u -r1.12 edit.js --- mod/quiz/edit.js 6 Aug 2010 17:55:33 -0000 1.12 +++ mod/quiz/edit.js 19 Nov 2010 21:17:59 -0000 @@ -28,7 +28,7 @@ // 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(); @@ -46,6 +46,10 @@ 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, @@ -88,6 +92,17 @@ } } +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'); Index: mod/quiz/edit.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/edit.php,v retrieving revision 1.193 diff -u -r1.193 edit.php --- mod/quiz/edit.php 15 Nov 2010 17:00:38 -0000 1.193 +++ mod/quiz/edit.php 19 Nov 2010 21:17:59 -0000 @@ -49,6 +49,7 @@ 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'); /** @@ -63,8 +64,7 @@ } else { $disabled = ''; } - $straddtoquiz = get_string('addtoquiz', 'quiz'); - $out = '\n"; return $out; } @@ -79,7 +79,7 @@ $catcontext = get_context_instance_by_id($category->contextid); if (has_capability('moodle/question:useall', $catcontext)) { if ($cmoptions->hasattempts) { - $disabled = 'disabled="disabled"'; + $disabled = ' disabled="disabled"'; } else { $disabled = ''; } @@ -93,19 +93,22 @@ for ($i = 20; $i <= min(100, $maxrand); $i += 10) { $randomcount[$i] = $i; } - $straddtoquiz = get_string('addtoquiz', 'quiz'); - $out = '
'; - $attributes = array(); - $attributes['disabled'] = $cmoptions->hasattempts ? 'disabled' : null; - $select = html_writer::select($randomcount, 'randomcount', '1', null, $attributes); - $out .= get_string('addrandom', 'quiz', $select); - $out .= ''; - $out .= ''; - $out .= ' '; - $out .= $OUTPUT->help_icon('addarandomquestion', 'quiz'); + } else { + $randomcount[0] = 0; + $disabled = ' disabled="disabled"'; } + + $out = '
'; + $attributes = array(); + $attributes['disabled'] = $disabled ? 'disabled' : null; + $select = html_writer::select($randomcount, 'randomcount', '1', null, $attributes); + $out .= get_string('addrandom', 'quiz', $select); + $out .= ''; + $out .= ''; + $out .= ' '; + $out .= $OUTPUT->help_icon('addarandomquestion', 'quiz'); } return $out; } @@ -119,9 +122,7 @@ question_edit_setup('editq', '/mod/quiz/edit.php', true); $defaultcategoryobj = question_make_default_categories($contexts->all()); -$defaultcategoryid = $defaultcategoryobj->id; -$defaultcategorycontext = $defaultcategoryobj->contextid; -$defaultcategory = $defaultcategoryid . ',' . $defaultcategorycontext; +$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; if ($quiz_qbanktool > -1) { $thispageurl->param('qbanktool', $quiz_qbanktool); @@ -227,80 +228,16 @@ } $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, - $contexts->having_one_edit_tab_cap('categories'), $defaultcategoryid, + $contexts->having_one_edit_tab_cap('categories'), $defaultcategoryobj->id, $defaultcategory, null, $contexts->having_cap('moodle/question:add')); -$newrandomcategory = false; -$newquestioninfo = quiz_process_randomquestion_formdata($qcobject); -if ($newquestioninfo && $newquestioninfo != 'cancelled') { - $newrandomcategory = $newquestioninfo->newrandomcategory; - if (!$newrandomcategory) { - print_error('cannotcreatecategory'); - } else { - add_to_log($quiz->course, 'quiz', 'addcategory', - "view.php?id=$cm->id", $newrandomcategory, $cm->id); - } -} - -if ((optional_param('addrandom', false, PARAM_BOOL) || $newrandomcategory) && confirm_sesskey()) { - - /// Add random questions to the quiz +if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) { + // Add random questions to the quiz $recurse = optional_param('recurse', 0, PARAM_BOOL); $addonpage = optional_param('addonpage', 0, PARAM_INT); - if ($newrandomcategory) { - $categoryid = $newrandomcategory; - $randomcount = optional_param('randomcount', 1, PARAM_INT); - } else { - $categoryid = required_param('categoryid', PARAM_INT); - $randomcount = required_param('randomcount', PARAM_INT); - } - // load category - $category = $DB->get_record('question_categories', array('id' => $categoryid)); - if (!$category) { - print_error('invalidcategoryid', 'error'); - } - $catcontext = get_context_instance_by_id($category->contextid); - require_capability('moodle/question:useall', $catcontext); - $category->name = $category->name; - // Find existing random questions in this category that are - // not used by any quiz. - if ($existingquestions = $DB->get_records_sql( - "SELECT q.id,q.qtype FROM {question} q - WHERE qtype = '" . RANDOM . "' - AND category = ? - AND " . $DB->sql_compare_text('questiontext') . " = ? - AND NOT EXISTS (SELECT * FROM {quiz_question_instances} WHERE question = q.id) - ORDER BY id", array($category->id, $recurse))) { - // Take as many of these as needed. - while (($existingquestion = array_shift($existingquestions)) && $randomcount > 0) { - quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage); - $randomcount--; - } - } - - // If more are needed, create them. - if ($randomcount > 0) { - $form->questiontext = $recurse; // we use the questiontext field - // to store the info on whether to include - // questions in subcategories - $form->questiontextformat = 0; - $form->image = ''; - $form->defaultgrade = 1; - $form->hidden = 1; - for ($i = 0; $i < $randomcount; $i++) { - $form->category = $category->id . ',' . $category->contextid; - $form->stamp = make_unique_id_code(); // Set the unique - //code (not to be changed) - $question = new stdClass; - $question->qtype = RANDOM; - $question = $QTYPES[RANDOM]->save_question($question, $form, - $course); - if(!isset($question->id)) { - print_error('cannotinsertrandomquestion', 'quiz'); - } - quiz_add_quiz_question($question->id, $quiz, $addonpage); - } - } + $categoryid = required_param('categoryid', PARAM_INT); + $randomcount = required_param('randomcount', PARAM_INT); + quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse); quiz_update_sumgrades($quiz); quiz_delete_previews($quiz); @@ -546,7 +483,7 @@ } if ($quiz_reordertool) { - $perpage= array(); + $perpage = array(); $perpage[0] = get_string('allinone', 'quiz'); for ($i = 1; $i <= 50; ++$i) { $perpage[$i] = $i; @@ -585,7 +522,12 @@ echo '
'; if (!$quiz_reordertool) { - // display category adding UI + $randomform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts); + $randomform->set_data(array( + 'category' => $pagevars['cat'], + 'returnurl' => str_replace($CFG->wwwroot, '', $thispageurl->out(false)), + 'cmid' => $cm->id, + )); ?>
name); ?> @@ -593,7 +535,7 @@
display_randomquestion_user_interface(); +$randomform->display(); ?>
get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } function move_files($questionid, $oldcontextid, $newcontextid) { Index: mod/quiz/lang/en/quiz.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/lang/en/quiz.php,v retrieving revision 1.46 diff -u -r1.46 quiz.php --- mod/quiz/lang/en/quiz.php 19 Nov 2010 11:11:33 -0000 1.46 +++ mod/quiz/lang/en/quiz.php 19 Nov 2010 21:18:00 -0000 @@ -215,7 +215,7 @@ $string['countdownfinished'] = 'The quiz is closing, you should submit your answers now.'; $string['countdowntenminutes'] = 'The quiz will be closing in ten minutes.'; $string['coursetestmanager'] = 'Course Test Manager format'; -$string['createcategoryfornewrandomquestion'] = 'Create a new question category for the new random question'; +$string['createcategoryandaddrandomquestion'] = 'Create category and add random question'; $string['createfirst'] = 'You must create some short-answer questions first.'; $string['createmultiple'] = 'Add several random questions to quiz'; $string['createnewquestion'] = 'Create new question'; @@ -663,7 +663,9 @@ $string['random'] = 'Random question'; $string['randomcreate'] = 'Create Random Questions'; $string['randomfromcategory'] = 'Random question from category:'; +$string['randomfromexistingcategory'] = 'Random question from an existing category'; $string['randomnosubcat'] = 'Questions from this category only, not its subcategories.'; +$string['randomquestionusinganewcategory'] = 'Random question using a new category'; $string['randomsamatch'] = 'Random Short-Answer Matching'; $string['randomsamatchcreate'] = 'Create Random Short-Answer Matching questions'; $string['randomsamatchintro'] = 'For each of the following questions, select the matching answer from the menu.'; @@ -672,7 +674,7 @@ $string['readytosend'] = 'You are about to send your whole quiz to be graded. Are you sure you want to continue?'; $string['reattemptquiz'] = 'Re-attempt quiz'; $string['recentlyaddedquestion'] = 'Recently added question!'; -$string['recurse'] = 'Display questions from sub-categories too'; +$string['recurse'] = 'Include questions from sub-categories too'; $string['regrade'] = 'Regrade all attempts'; $string['regradecomplete'] = 'All attempts have been regraded'; $string['regradecount'] = '{$a->changed} out of {$a->attempt} grades were changed'; Index: question/type/calculatedmulti/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/calculatedmulti/questiontype.php,v retrieving revision 1.13 diff -u -r1.13 questiontype.php --- question/type/calculatedmulti/questiontype.php 16 Nov 2010 03:44:59 -0000 1.13 +++ question/type/calculatedmulti/questiontype.php 19 Nov 2010 21:18:01 -0000 @@ -526,7 +526,7 @@ $course = $DB->get_record('course', array('id'=> $courseid)); } - $new_question = $this->save_question($question, $form, $course); + $new_question = $this->save_question($question, $form); $dataset_form = new stdClass(); $dataset_form->nextpageparam["forceregeneration"]= 1; Index: question/type/multichoice/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/multichoice/questiontype.php,v retrieving revision 1.70 diff -u -r1.70 questiontype.php --- question/type/multichoice/questiontype.php 19 Nov 2010 11:36:15 -0000 1.70 +++ question/type/multichoice/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -488,7 +488,7 @@ $course = $DB->get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } function move_files($questionid, $oldcontextid, $newcontextid) { Index: question/type/numerical/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/numerical/questiontype.php,v retrieving revision 1.80 diff -u -r1.80 questiontype.php --- question/type/numerical/questiontype.php 15 Nov 2010 14:09:19 -0000 1.80 +++ question/type/numerical/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -1318,7 +1318,7 @@ $course = $DB->get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } function move_files($questionid, $oldcontextid, $newcontextid) { Index: question/type/calculatedsimple/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/calculatedsimple/questiontype.php,v retrieving revision 1.20 diff -u -r1.20 questiontype.php --- question/type/calculatedsimple/questiontype.php 16 Nov 2010 03:46:31 -0000 1.20 +++ question/type/calculatedsimple/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -321,7 +321,7 @@ $course = $DB->get_record('course', array('id'=> $courseid)); } - $new_question = $this->save_question($question, $form, $course); + $new_question = $this->save_question($question, $form); $dataset_form = new stdClass(); $dataset_form->nextpageparam["forceregeneration"]= 1; Index: question/type/match/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/match/questiontype.php,v retrieving revision 1.70 diff -u -r1.70 questiontype.php --- question/type/match/questiontype.php 12 Nov 2010 17:11:34 -0000 1.70 +++ question/type/match/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -493,7 +493,7 @@ $course = $DB->get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } function move_files($questionid, $oldcontextid, $newcontextid) { Index: question/type/calculated/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/calculated/questiontype.php,v retrieving revision 1.107 diff -u -r1.107 questiontype.php --- question/type/calculated/questiontype.php 16 Nov 2010 15:50:50 -0000 1.107 +++ question/type/calculated/questiontype.php 19 Nov 2010 21:18:01 -0000 @@ -667,10 +667,10 @@ * @param int $course * @param PARAM_ALPHA $wizardnow should be added as we are coming from question2.php */ - function save_question($question, $form, $course) { + function save_question($question, $form) { global $DB; if ($this->wizard_pages_number() == 1 ){ - $question = parent::save_question($question, $form, $course); + $question = parent::save_question($question, $form); return $question ; } @@ -689,7 +689,7 @@ case '' : case 'question': // coming from the first page, creating the second if (empty($form->id)) { // for a new question $form->id is empty - $question = parent::save_question($question, $form, $course); + $question = parent::save_question($question, $form); //prepare the datasets using default $questionfromid $this->preparedatasets($form); $form->id = $question->id; @@ -699,7 +699,7 @@ } } else if (!empty($form->makecopy)){ $questionfromid = $form->id ; - $question = parent::save_question($question, $form, $course); + $question = parent::save_question($question, $form); //prepare the datasets $this->preparedatasets($form,$questionfromid); $form->id = $question->id; @@ -708,7 +708,7 @@ $this->addnamecategory($question); } } else {// editing a question - $question = parent::save_question($question, $form, $course); + $question = parent::save_question($question, $form); //prepare the datasets $this->preparedatasets($form,$question->id); $form->id = $question->id; @@ -2084,7 +2084,7 @@ $course = $DB->get_record('course', array('id'=> $courseid)); } - $new_question = $this->save_question($question, $form, $course); + $new_question = $this->save_question($question, $form); $dataset_form = new stdClass(); $dataset_form->nextpageparam["forceregeneration"]= 1; Index: question/type/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/questiontype.php,v retrieving revision 1.170 diff -u -r1.170 questiontype.php --- question/type/questiontype.php 12 Nov 2010 20:30:09 -0000 1.170 +++ question/type/questiontype.php 19 Nov 2010 21:18:01 -0000 @@ -305,7 +305,7 @@ * redisplayed with validation errors, from validation_errors field, which * is itself an object, shown next to the form fields. (I don't think this is accurate any more.) */ - function save_question($question, $form, $course) { + function save_question($question, $form) { global $USER, $DB, $OUTPUT; list($question->category) = explode(',', $form->category); @@ -364,10 +364,10 @@ $question->modifiedby = $USER->id; $question->timemodified = time(); - if (!empty($question->questiontext)) { + if (!empty($question->questiontext) && !empty($form->questiontext['itemid'])) { $question->questiontext = file_save_draft_area_files($form->questiontext['itemid'], $context->id, 'question', 'questiontext', (int)$question->id, $this->fileoptions, $question->questiontext); } - if (!empty($question->generalfeedback)) { + if (!empty($question->generalfeedback) && !empty($form->generalfeedback['itemid'])) { $question->generalfeedback = file_save_draft_area_files($form->generalfeedback['itemid'], $context->id, 'question', 'generalfeedback', (int)$question->id, $this->fileoptions, $question->generalfeedback); } $DB->update_record('question', $question); @@ -392,10 +392,7 @@ } if (!empty($result->noticeyesno)) { - echo $OUTPUT->confirm($result->noticeyesno, "question.php?id=$question->id&courseid={$course->id}", - "edit.php?courseid={$course->id}"); - echo $OUTPUT->footer(); - exit; + throw new coding_exception('$result->noticeyesno no longer supported in save_question.'); } // Give the question a unique version stamp determined by question_hash() Index: question/type/shortanswer/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/shortanswer/questiontype.php,v retrieving revision 1.62 diff -u -r1.62 questiontype.php --- question/type/shortanswer/questiontype.php 12 Nov 2010 17:11:34 -0000 1.62 +++ question/type/shortanswer/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -372,7 +372,7 @@ $course = $DB->get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } function check_file_access($question, $state, $options, $contextid, $component, Index: question/type/truefalse/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/truefalse/questiontype.php,v retrieving revision 1.40 diff -u -r1.40 questiontype.php --- question/type/truefalse/questiontype.php 12 Nov 2010 17:11:35 -0000 1.40 +++ question/type/truefalse/questiontype.php 19 Nov 2010 21:18:02 -0000 @@ -306,7 +306,7 @@ $course = $DB->get_record('course', array('id' => $courseid)); } - return $this->save_question($question, $form, $course); + return $this->save_question($question, $form); } } //// END OF CLASS //// Index: mod/quiz/addrandomform.php =================================================================== RCS file: mod/quiz/addrandomform.php diff -N mod/quiz/addrandomform.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ mod/quiz/addrandomform.php 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,57 @@ +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; + } +} +