Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/quiz/addrandom.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00028 require_once('../../config.php');
00029 require_once($CFG->dirroot . '/mod/quiz/editlib.php');
00030 require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
00031 require_once($CFG->dirroot . '/question/category_class.php');
00032 
00033 list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
00034         question_edit_setup('editq', '/mod/quiz/addrandom.php', true);
00035 
00036 // These params are only passed from page request to request while we stay on
00037 // this page otherwise they would go in question_edit_setup
00038 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
00039 $addonpage = optional_param('addonpage', 0, PARAM_INT);
00040 $category = optional_param('category', 0, PARAM_INT);
00041 $scrollpos = optional_param('scrollpos', 0, PARAM_INT);
00042 
00043 // Get the course object and related bits.
00044 if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
00045     print_error('invalidcourseid');
00046 }
00047 //you need mod/quiz:manage in addition to question capabilities to access this page.
00048 require_capability('mod/quiz:manage', $contexts->lowest());
00049 
00050 $PAGE->set_url($thispageurl);
00051 
00052 if ($returnurl) {
00053     $returnurl = new moodle_url($returnurl);
00054 } else {
00055     $returnurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cmid));
00056 }
00057 if ($scrollpos) {
00058     $returnurl->param('scrollpos', $scrollpos);
00059 }
00060 
00061 $defaultcategoryobj = question_make_default_categories($contexts->all());
00062 $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
00063 
00064 $qcobject = new question_category_object(
00065     $pagevars['cpage'],
00066     $thispageurl,
00067     $contexts->having_one_edit_tab_cap('categories'),
00068     $defaultcategoryobj->id,
00069     $defaultcategory,
00070     null,
00071     $contexts->having_cap('moodle/question:add'));
00072 
00073 $mform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts);
00074 
00075 if ($mform->is_cancelled()) {
00076     redirect($returnurl);
00077 }
00078 
00079 if ($data = $mform->get_data()) {
00080     if (!empty($data->existingcategory)) {
00081         list($categoryid) = explode(',', $data->category);
00082         $includesubcategories = !empty($data->includesubcategories);
00083         $returnurl->param('cat', $data->category);
00084 
00085     } else if (!empty($data->newcategory)) {
00086         list($parentid, $contextid) = explode(',', $data->parent);
00087         $categoryid = $qcobject->add_category($data->parent, $data->name, '', true);
00088         $includesubcategories = 0;
00089         add_to_log($quiz->course, 'quiz', 'addcategory',
00090                 'view.php?id=' . $cm->id, $categoryid, $cm->id);
00091         $returnurl->param('cat', $categoryid . ',' . $contextid);
00092 
00093     } else {
00094         throw new coding_exception(
00095                 'It seems a form was submitted without any button being pressed???');
00096     }
00097 
00098     quiz_add_random_questions($quiz, $addonpage, $categoryid, 1, $includesubcategories);
00099     quiz_delete_previews($quiz);
00100     quiz_update_sumgrades($quiz);
00101     redirect($returnurl);
00102 }
00103 
00104 $mform->set_data(array(
00105     'addonpage' => $addonpage,
00106     'returnurl' => $returnurl,
00107     'cmid' => $cm->id,
00108     'category' => $category,
00109 ));
00110 
00111 // Setup $PAGE.
00112 $streditingquiz = get_string('editinga', 'moodle', get_string('modulename', 'quiz'));
00113 $PAGE->navbar->add($streditingquiz);
00114 $PAGE->set_title($streditingquiz);
00115 $PAGE->set_heading($course->fullname);
00116 echo $OUTPUT->header();
00117 
00118 if (!$quizname = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance))) {
00119             print_error('invalidcoursemodule');
00120 }
00121 
00122 echo $OUTPUT->heading(get_string('addrandomquestiontoquiz', 'quiz', $quizname), 2, 'mdl-left');
00123 $mform->display();
00124 echo $OUTPUT->footer();
00125 
 All Data Structures Namespaces Files Functions Variables Enumerations