/**
 * Common setup for all pages for editing questions.
 * @param string $edittab code for this edit tab
 * @param boolean $requirecmid require cmid? default false
 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 */
function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true)
{
    global $COURSE, $QUESTION_EDITTABCAPS;
    //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
    //of parameters that are passed from page to page.
    $thispageurl = new moodle_url();
    if ($requirecmid) {
        $cmid = required_param('cmid', PARAM_INT);
    } else {
        $cmid = optional_param('cmid', 0, PARAM_INT);
    }
    if ($cmid) {
        list($module, $cm) = get_module_from_cmid($cmid);
        $courseid = $cm->course;
        $thispageurl->params(compact('cmid'));
        require_login($courseid, false, $cm);
        $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
    } else {
        $module = null;
        $cm = null;
        if ($requirecourseid) {
            $courseid = required_param('courseid', PARAM_INT);
        } else {
            $courseid = optional_param('courseid', 0, PARAM_INT);
        }
        if ($courseid) {
            $thispageurl->params(compact('courseid'));
            require_login($courseid, false);
            $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
        } else {
            $thiscontext = null;
        }
    }
    if ($thiscontext) {
        $contexts = new question_edit_contexts($thiscontext);
        $contexts->require_one_edit_tab_cap($edittab);
    } else {
        $contexts = null;
    }
    $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
    //pass 'cat' from page to page and when 'category' comes from a drop down menu
    //then we also reset the qpage so we go to page 1 of
    //a new cat.
    $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE);
    // if empty will be set up later
    if ($category = optional_param('category', 0, PARAM_SEQUENCE)) {
        if ($pagevars['cat'] != $category) {
            // is this a move to a new category?
            $pagevars['cat'] = $category;
            $pagevars['qpage'] = 0;
        }
    }
    if ($pagevars['cat']) {
        $thispageurl->param('cat', $pagevars['cat']);
    }
    if ($pagevars['qpage'] > -1) {
        $thispageurl->param('qpage', $pagevars['qpage']);
    } else {
        $pagevars['qpage'] = 0;
    }
    $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
    if ($pagevars['qperpage'] > -1) {
        $thispageurl->param('qperpage', $pagevars['qperpage']);
    } else {
        $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
    }
    $sortoptions = array('alpha' => 'name, qtype ASC', 'typealpha' => 'qtype, name ASC', 'age' => 'id ASC');
    if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) {
        $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
        $pagevars['qsortorder'] = $sortorder;
        $thispageurl->param('qsortorder', $sortorder);
    } else {
        $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
        $pagevars['qsortorder'] = 'typealpha';
    }
    $defaultcategory = question_make_default_categories($contexts->all());
    $contextlistarr = array();
    foreach ($contexts->having_one_edit_tab_cap($edittab) as $context) {
        $contextlistarr[] = "'{$context->id}'";
    }
    $contextlist = join($contextlistarr, ' ,');
    if (!empty($pagevars['cat'])) {
        $catparts = explode(',', $pagevars['cat']);
        if (!$catparts[0] || FALSE !== array_search($catparts[1], $contextlistarr) || !count_records_select("question_categories", "id = '" . $catparts[0] . "' AND contextid = {$catparts['1']}")) {
            print_error('invalidcategory', 'quiz');
        }
    } else {
        $category = $defaultcategory;
        $pagevars['cat'] = "{$category->id},{$category->contextid}";
    }
    if (($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
        $pagevars['recurse'] = $recurse;
        $thispageurl->param('recurse', $recurse);
    } else {
        $pagevars['recurse'] = 1;
    }
    if (($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
        $pagevars['showhidden'] = $showhidden;
        $thispageurl->param('showhidden', $showhidden);
    } else {
        $pagevars['showhidden'] = 0;
    }
    if (($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
        $pagevars['showquestiontext'] = $showquestiontext;
        $thispageurl->param('showquestiontext', $showquestiontext);
    } else {
        $pagevars['showquestiontext'] = 0;
    }
    //category list page
    $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
    if ($pagevars['cpage'] < 1) {
        $pagevars['cpage'] = 1;
    }
    if ($pagevars['cpage'] != 1) {
        $thispageurl->param('cpage', $pagevars['cpage']);
    }
    return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
}
Exemple #2
0
if ($originalreturnurl) {
    if (strpos($originalreturnurl, '/') !== 0) {
        throw new coding_exception("returnurl must be a local URL starting with '/'. {$originalreturnurl} was given.");
    }
    $returnurl = new moodle_url($originalreturnurl);
} else {
    $returnurl = $questionbankurl;
}
if ($scrollpos) {
    $returnurl->param('scrollpos', $scrollpos);
}
if ($movecontext && !$id) {
    print_error('questiondoesnotexist', 'question', $returnurl);
}
if ($cmid) {
    list($module, $cm) = get_module_from_cmid($cmid);
    require_login($cm->course, false, $cm);
    $thiscontext = context_module::instance($cmid);
} elseif ($courseid) {
    require_login($courseid, false);
    $thiscontext = context_course::instance($courseid);
    $module = null;
    $cm = null;
} else {
    print_error('missingcourseorcmid', 'question');
}
$contexts = new question_edit_contexts($thiscontext);
$PAGE->set_pagelayout('admin');
if (optional_param('addcancel', false, PARAM_BOOL)) {
    redirect($returnurl);
}
Exemple #3
0
list($catid, $catcontext) = explode(',', $pagevars['cat']);
if (!($category = $DB->get_record("question_categories", array('id' => $catid)))) {
    print_error('nocategory', 'question');
}
$categorycontext = context::instance_by_id($category->contextid);
$category->context = $categorycontext;
//this page can be called without courseid or cmid in which case
//we get the context from the category object.
if ($contexts === null) {
    // need to get the course from the chosen category
    $contexts = new question_edit_contexts($categorycontext);
    $thiscontext = $contexts->lowest();
    if ($thiscontext->contextlevel == CONTEXT_COURSE) {
        require_login($thiscontext->instanceid, false);
    } elseif ($thiscontext->contextlevel == CONTEXT_MODULE) {
        list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
        require_login($cm->course, false, $cm);
    }
    $contexts->require_one_edit_tab_cap($edittab);
}
$PAGE->set_url($thispageurl);
$import_form = new question_import_form($thispageurl, array('contexts' => $contexts->having_one_edit_tab_cap('import'), 'defaultcategory' => $pagevars['cat']));
if ($import_form->is_cancelled()) {
    redirect($thispageurl);
}
//==========
// PAGE HEADER
//==========
$PAGE->set_title($txt->importquestions);
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();
/**
 * Common setup for all pages for editing questions.
 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
 * @param string $edittab code for this edit tab
 * @param bool $requirecmid require cmid? default false
 * @param bool $requirecourseid require courseid, if cmid is not given? default true
 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 */
function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true)
{
    global $DB, $PAGE;
    $thispageurl = new moodle_url($baseurl);
    $thispageurl->remove_all_params();
    // We are going to explicity add back everything important - this avoids unwanted params from being retained.
    if ($requirecmid) {
        $cmid = required_param('cmid', PARAM_INT);
    } else {
        $cmid = optional_param('cmid', 0, PARAM_INT);
    }
    if ($cmid) {
        list($module, $cm) = get_module_from_cmid($cmid);
        $courseid = $cm->course;
        $thispageurl->params(compact('cmid'));
        require_login($courseid, false, $cm);
        $thiscontext = context_module::instance($cmid);
    } else {
        $module = null;
        $cm = null;
        if ($requirecourseid) {
            $courseid = required_param('courseid', PARAM_INT);
        } else {
            $courseid = optional_param('courseid', 0, PARAM_INT);
        }
        if ($courseid) {
            $thispageurl->params(compact('courseid'));
            require_login($courseid, false);
            $thiscontext = context_course::instance($courseid);
        } else {
            $thiscontext = null;
        }
    }
    if ($thiscontext) {
        $contexts = new question_edit_contexts($thiscontext);
        $contexts->require_one_edit_tab_cap($edittab);
    } else {
        $contexts = null;
    }
    $PAGE->set_pagelayout('admin');
    $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
    //pass 'cat' from page to page and when 'category' comes from a drop down menu
    //then we also reset the qpage so we go to page 1 of
    //a new cat.
    $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE);
    // if empty will be set up later
    if ($category = optional_param('category', 0, PARAM_SEQUENCE)) {
        if ($pagevars['cat'] != $category) {
            // is this a move to a new category?
            $pagevars['cat'] = $category;
            $pagevars['qpage'] = 0;
        }
    }
    if ($pagevars['cat']) {
        $thispageurl->param('cat', $pagevars['cat']);
    }
    if (strpos($baseurl, '/question/') === 0) {
        navigation_node::override_active_url($thispageurl);
    }
    if ($pagevars['qpage'] > -1) {
        $thispageurl->param('qpage', $pagevars['qpage']);
    } else {
        $pagevars['qpage'] = 0;
    }
    $pagevars['qperpage'] = question_get_display_preference('qperpage', DEFAULT_QUESTIONS_PER_PAGE, PARAM_INT, $thispageurl);
    for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
        $param = 'qbs' . $i;
        if (!($sort = optional_param($param, '', PARAM_TEXT))) {
            break;
        }
        $thispageurl->param($param, $sort);
    }
    $defaultcategory = question_make_default_categories($contexts->all());
    $contextlistarr = array();
    foreach ($contexts->having_one_edit_tab_cap($edittab) as $context) {
        $contextlistarr[] = "'{$context->id}'";
    }
    $contextlist = join($contextlistarr, ' ,');
    if (!empty($pagevars['cat'])) {
        $catparts = explode(',', $pagevars['cat']);
        if (!$catparts[0] || false !== array_search($catparts[1], $contextlistarr) || !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
            print_error('invalidcategory', 'question');
        }
    } else {
        $category = $defaultcategory;
        $pagevars['cat'] = "{$category->id},{$category->contextid}";
    }
    // Display options.
    $pagevars['recurse'] = question_get_display_preference('recurse', 1, PARAM_BOOL, $thispageurl);
    $pagevars['showhidden'] = question_get_display_preference('showhidden', 0, PARAM_BOOL, $thispageurl);
    $pagevars['qbshowtext'] = question_get_display_preference('qbshowtext', 0, PARAM_BOOL, $thispageurl);
    // Category list page.
    $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
    if ($pagevars['cpage'] != 1) {
        $thispageurl->param('cpage', $pagevars['cpage']);
    }
    return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
}
 private function get_categories()
 {
     $cmid = optional_param('cmid', 0, PARAM_INT);
     $categoryparam = optional_param('category', '', PARAM_TEXT);
     $courseid = optional_param('courseid', 0, PARAM_INT);
     if ($cmid) {
         list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) = question_edit_setup('editq', '/mod/quiz/edit.php', true);
         if ($pagevars['cat']) {
             $categoryparam = $pagevars['cat'];
         }
     }
     if ($categoryparam) {
         $catandcontext = explode(',', $categoryparam);
         $cats = question_categorylist($catandcontext[0]);
         return $cats;
     } else {
         if ($cmid) {
             list($module, $cm) = get_module_from_cmid($cmid);
             $courseid = $cm->course;
             require_login($courseid, false, $cm);
             $thiscontext = context_module::instance($cmid);
         } else {
             $module = null;
             $cm = null;
             if ($courseid) {
                 $thiscontext = context_course::instance($courseid);
             } else {
                 $thiscontext = null;
             }
         }
     }
     $cats = get_categories_for_contexts($thiscontext->id);
     return array_keys($cats);
 }
function addRandomQuizQuestions($cmid, $cat_id)
{
    global $QTYPES;
    $result = true;
    $recurse = 1;
    list($quiz, $cm) = get_module_from_cmid($cmid);
    if ($rs = get_records('question_categories', 'parent', $cat_id, 'sortorder')) {
        $course = get_record('course', 'id', $quiz->course);
        foreach ($rs as $cat) {
            $categoryid = $cat->id;
            $randomcount = 1;
            // load category
            if (!($category = get_record('question_categories', 'id', $categoryid))) {
                error('Category ID is incorrect');
            }
            $catcontext = get_context_instance_by_id($category->contextid);
            require_capability('moodle/question:useall', $catcontext);
            $category->name = addslashes($category->name);
            // Find existing random questions in this category that are not used by any quiz.
            if ($existingquestions = get_records_sql("SELECT * FROM " . $CFG->prefix . "question q\n                        WHERE qtype = '" . RANDOM . "'\n                            AND category = {$category->id}\n                            AND " . sql_compare_text('questiontext') . " = '{$recurse}'\n                            AND NOT EXISTS (SELECT * FROM " . $CFG->prefix . "quiz_question_instances WHERE question = q.id)\n                        ORDER BY id")) {
                // Take as many of these as needed.
                while ($existingquestion = array_shift($existingquestions) and $randomcount > 0) {
                    if (!quiz_add_quiz_question($existingquestion->id, $quiz)) {
                        $result = false;
                    }
                    $randomcount--;
                }
            }
            // If more are needed, create them.
            if ($randomcount > 0) {
                //echo "NOT EXISTING:".$cat->id."<br/>";
                $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)) {
                        error('Could not insert new random question!');
                        $result = false;
                    }
                    //quiz_add_quiz_question($question->id, $quiz);
                    if (!quiz_add_quiz_question($question->id, $quiz)) {
                        $result = false;
                    }
                }
            }
            $significantchangemade = true;
        }
    }
    return $result;
    /*
        $result = true;
        $recurse = 1;
       list($quiz, $cm) = get_module_from_cmid($cmid);
       if ($rs = get_recordset('question_categories','parent',$cat_id)) {
               $course = get_record('course','id',$quiz->course);
                foreach ($rs as $category) {
                        echo $category->name."<br/>";
                        /// Add random questions to the quiz
                        // - Source from: /mod/quiz/edit.php
                        $randomcount = 1;
                        // load category
                       //$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 = 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);
                                if(!quiz_add_quiz_question($existingquestion->id, $quiz)){
                                    $result = false;
                                }
                                $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');
                                    $result = false;
                                }
                                //quiz_add_quiz_question($question->id, $quiz);
                                if(!quiz_add_quiz_question($question->id, $quiz)){
                                    $result = false;
                                }
                            }
                        }
                       quiz_update_sumgrades($quiz);
                        quiz_delete_previews($quiz);
               }
                //$rs->close(); /// Don't forget to close the recordset!
        }
        return $result;
    */
}
/**
 * Common setup for all pages for editing questions.
 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
 * @param string $edittab code for this edit tab
 * @param bool $requirecmid require cmid? default false
 * @param bool $requirecourseid require courseid, if cmid is not given? default true
 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 */
function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true)
{
    global $DB, $PAGE;
    $thispageurl = new moodle_url($baseurl);
    $thispageurl->remove_all_params();
    // We are going to explicity add back everything important - this avoids unwanted params from being retained.
    if ($requirecmid) {
        $cmid = required_param('cmid', PARAM_INT);
    } else {
        $cmid = optional_param('cmid', 0, PARAM_INT);
    }
    if ($cmid) {
        list($module, $cm) = get_module_from_cmid($cmid);
        $courseid = $cm->course;
        $thispageurl->params(compact('cmid'));
        require_login($courseid, false, $cm);
        $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
    } else {
        $module = null;
        $cm = null;
        if ($requirecourseid) {
            $courseid = required_param('courseid', PARAM_INT);
        } else {
            $courseid = optional_param('courseid', 0, PARAM_INT);
        }
        if ($courseid) {
            $thispageurl->params(compact('courseid'));
            require_login($courseid, false);
            $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
        } else {
            $thiscontext = null;
        }
    }
    if ($thiscontext) {
        $contexts = new question_edit_contexts($thiscontext);
        $contexts->require_one_edit_tab_cap($edittab);
    } else {
        $contexts = null;
    }
    $PAGE->set_pagelayout('admin');
    $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
    //pass 'cat' from page to page and when 'category' comes from a drop down menu
    //then we also reset the qpage so we go to page 1 of
    //a new cat.
    $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE);
    // if empty will be set up later
    if ($category = optional_param('category', 0, PARAM_SEQUENCE)) {
        if ($pagevars['cat'] != $category) {
            // is this a move to a new category?
            $pagevars['cat'] = $category;
            $pagevars['qpage'] = 0;
        }
    }
    if ($pagevars['cat']) {
        $thispageurl->param('cat', $pagevars['cat']);
    }
    if (strpos($baseurl, '/question/') === 0) {
        navigation_node::override_active_url($thispageurl);
    }
    if ($pagevars['qpage'] > -1) {
        $thispageurl->param('qpage', $pagevars['qpage']);
    } else {
        $pagevars['qpage'] = 0;
    }
    $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
    if ($pagevars['qperpage'] > -1) {
        $thispageurl->param('qperpage', $pagevars['qperpage']);
    } else {
        $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
    }
    $sortoptions = array('alpha' => 'name, qtype ASC', 'typealpha' => 'qtype, name ASC', 'age' => 'id ASC');
    if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) {
        $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
        $pagevars['qsortorder'] = $sortorder;
        $thispageurl->param('qsortorder', $sortorder);
    } else {
        $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
        $pagevars['qsortorder'] = 'typealpha';
    }
    $defaultcategory = question_make_default_categories($contexts->all());
    $contextlistarr = array();
    foreach ($contexts->having_one_edit_tab_cap($edittab) as $context) {
        $contextlistarr[] = "'{$context->id}'";
    }
    $contextlist = join($contextlistarr, ' ,');
    if (!empty($pagevars['cat'])) {
        $catparts = explode(',', $pagevars['cat']);
        if (!$catparts[0] || false !== array_search($catparts[1], $contextlistarr) || !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
            print_error('invalidcategory', 'question');
        }
    } else {
        $category = $defaultcategory;
        $pagevars['cat'] = "{$category->id},{$category->contextid}";
    }
    if (($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
        $pagevars['recurse'] = $recurse;
        $thispageurl->param('recurse', $recurse);
    } else {
        $pagevars['recurse'] = 1;
    }
    if (($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
        $pagevars['showhidden'] = $showhidden;
        $thispageurl->param('showhidden', $showhidden);
    } else {
        $pagevars['showhidden'] = 0;
    }
    if (($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
        $pagevars['showquestiontext'] = $showquestiontext;
        $thispageurl->param('showquestiontext', $showquestiontext);
    } else {
        $pagevars['showquestiontext'] = 0;
    }
    //category list page
    $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
    if ($pagevars['cpage'] != 1) {
        $thispageurl->param('cpage', $pagevars['cpage']);
    }
    return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
}