|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00027 require_once(dirname(__FILE__) . '/../config.php'); 00028 require_once(dirname(__FILE__) . '/editlib.php'); 00029 require_once($CFG->libdir . '/filelib.php'); 00030 require_once($CFG->libdir . '/formslib.php'); 00031 00032 // Read URL parameters telling us which question to edit. 00033 $id = optional_param('id', 0, PARAM_INT); // question id 00034 $qtype = optional_param('qtype', '', PARAM_FILE); 00035 $categoryid = optional_param('category', 0, PARAM_INT); 00036 $cmid = optional_param('cmid', 0, PARAM_INT); 00037 $courseid = optional_param('courseid', 0, PARAM_INT); 00038 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA); 00039 $movecontext = optional_param('movecontext', 0, PARAM_BOOL); // Switch to make 00040 // question uneditable - form is displayed to edit category only 00041 $originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL); 00042 $appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA); 00043 $inpopup = optional_param('inpopup', 0, PARAM_BOOL); 00044 $scrollpos = optional_param('scrollpos', 0, PARAM_INT); 00045 00046 $url = new moodle_url('/question/question.php'); 00047 if ($id !== 0) { 00048 $url->param('id', $id); 00049 } 00050 if ($qtype !== '') { 00051 $url->param('qtype', $qtype); 00052 } 00053 if ($categoryid !== 0) { 00054 $url->param('category', $categoryid); 00055 } 00056 if ($cmid !== 0) { 00057 $url->param('cmid', $cmid); 00058 } 00059 if ($courseid !== 0) { 00060 $url->param('courseid', $courseid); 00061 } 00062 if ($wizardnow !== '') { 00063 $url->param('wizardnow', $wizardnow); 00064 } 00065 if ($movecontext !== 0) { 00066 $url->param('movecontext', $movecontext); 00067 } 00068 if ($originalreturnurl !== 0) { 00069 $url->param('returnurl', $originalreturnurl); 00070 } 00071 if ($appendqnumstring !== '') { 00072 $url->param('appendqnumstring', $appendqnumstring); 00073 } 00074 if ($inpopup !== 0) { 00075 $url->param('inpopup', $inpopup); 00076 } 00077 if ($scrollpos) { 00078 $url->param('scrollpos', $scrollpos); 00079 } 00080 $PAGE->set_url($url); 00081 00082 if ($originalreturnurl) { 00083 if (strpos($originalreturnurl, '/') !== 0) { 00084 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given."); 00085 } 00086 $returnurl = new moodle_url($originalreturnurl); 00087 } else if ($cmid) { 00088 $returnurl = new moodle_url('/question/edit.php', array('cmid' => $cmid)); 00089 } else { 00090 $returnurl = new moodle_url('/question/edit.php', array('courseid' => $courseid)); 00091 } 00092 if ($scrollpos) { 00093 $returnurl->param('scrollpos', $scrollpos); 00094 } 00095 00096 if ($movecontext && !$id){ 00097 print_error('questiondoesnotexist', 'question', $returnurl); 00098 } 00099 00100 if ($cmid){ 00101 list($module, $cm) = get_module_from_cmid($cmid); 00102 require_login($cm->course, false, $cm); 00103 $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid); 00104 } elseif ($courseid) { 00105 require_login($courseid, false); 00106 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid); 00107 $module = null; 00108 $cm = null; 00109 } else { 00110 print_error('missingcourseorcmid', 'question'); 00111 } 00112 $contexts = new question_edit_contexts($thiscontext); 00113 $PAGE->set_pagelayout('admin'); 00114 00115 if (optional_param('addcancel', false, PARAM_BOOL)) { 00116 redirect($returnurl); 00117 } 00118 00119 if ($id) { 00120 if (!$question = $DB->get_record('question', array('id' => $id))) { 00121 print_error('questiondoesnotexist', 'question', $returnurl); 00122 } 00123 get_question_options($question, true); 00124 00125 } else if ($categoryid && $qtype) { // only for creating new questions 00126 $question = new stdClass(); 00127 $question->category = $categoryid; 00128 $question->qtype = $qtype; 00129 00130 // Check that users are allowed to create this question type at the moment. 00131 if (!question_bank::qtype_enabled($qtype)) { 00132 print_error('cannotenable', 'question', $returnurl, $qtype); 00133 } 00134 00135 } else if ($categoryid) { 00136 // Category, but no qtype. They probably came from the addquestion.php 00137 // script without choosing a question type. Send them back. 00138 $addurl = new moodle_url('/question/addquestion.php', $url->params()); 00139 $addurl->param('validationerror', 1); 00140 redirect($addurl); 00141 00142 } else { 00143 print_error('notenoughdatatoeditaquestion', 'question', $returnurl); 00144 } 00145 00146 $qtypeobj = question_bank::get_qtype($question->qtype); 00147 00148 // Validate the question category. 00149 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) { 00150 print_error('categorydoesnotexist', 'question', $returnurl); 00151 } 00152 00153 // Check permissions 00154 $question->formoptions = new stdClass(); 00155 00156 $categorycontext = get_context_instance_by_id($category->contextid); 00157 $addpermission = has_capability('moodle/question:add', $categorycontext); 00158 00159 if ($id) { 00160 $canview = question_has_capability_on($question, 'view'); 00161 if ($movecontext){ 00162 $question->formoptions->canedit = false; 00163 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add')); 00164 $question->formoptions->cansaveasnew = false; 00165 $question->formoptions->repeatelements = false; 00166 $question->formoptions->movecontext = true; 00167 $formeditable = true; 00168 question_require_capability_on($question, 'view'); 00169 } else { 00170 $question->formoptions->canedit = question_has_capability_on($question, 'edit'); 00171 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission); 00172 $question->formoptions->cansaveasnew = (($canview ||question_has_capability_on($question, 'edit')) && $addpermission); 00173 $question->formoptions->repeatelements = ($question->formoptions->canedit || $question->formoptions->cansaveasnew); 00174 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove; 00175 $question->formoptions->movecontext = false; 00176 if (!$formeditable){ 00177 question_require_capability_on($question, 'view'); 00178 } 00179 } 00180 00181 } else { // creating a new question 00182 require_capability('moodle/question:add', $categorycontext); 00183 $formeditable = true; 00184 $question->formoptions->canedit = question_has_capability_on($question, 'edit'); 00185 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission); 00186 $question->formoptions->repeatelements = true; 00187 $question->formoptions->movecontext = false; 00188 } 00189 00190 // Validate the question type. 00191 $PAGE->set_pagetype('question-type-' . $question->qtype); 00192 00193 // Create the question editing form. 00194 if ($wizardnow !== '' && !$movecontext){ 00195 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable); 00196 } else { 00197 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable); 00198 } 00199 $toform = fullclone($question); // send the question object and a few more parameters to the form 00200 $toform->category = "$category->id,$category->contextid"; 00201 $toform->scrollpos = $scrollpos; 00202 if ($formeditable && $id){ 00203 $toform->categorymoveto = $toform->category; 00204 } 00205 00206 $toform->appendqnumstring = $appendqnumstring; 00207 $toform->returnurl = $originalreturnurl; 00208 $toform->movecontext = $movecontext; 00209 if ($cm !== null){ 00210 $toform->cmid = $cm->id; 00211 $toform->courseid = $cm->course; 00212 } else { 00213 $toform->courseid = $COURSE->id; 00214 } 00215 00216 $toform->inpopup = $inpopup; 00217 00218 $mform->set_data($toform); 00219 00220 if ($mform->is_cancelled()) { 00221 if ($inpopup) { 00222 close_window(); 00223 } else { 00224 redirect($returnurl); 00225 } 00226 00227 } else if ($fromform = $mform->get_data()) { 00229 if (!empty($fromform->makecopy)) { 00230 $question->id = 0; 00231 $question->hidden = 0; // Copies should not be hidden 00232 } 00233 00236 if (!empty($fromform->usecurrentcat)) { 00237 // $fromform->category is the right category to save in. 00238 } else { 00239 if (!empty($fromform->categorymoveto)) { 00240 $fromform->category = $fromform->categorymoveto; 00241 } else { 00242 // $fromform->category is the right category to save in. 00243 } 00244 } 00245 00248 list($newcatid) = explode(',', $fromform->category); 00249 if (!empty($question->id) && $newcatid != $question->category) { 00250 question_require_capability_on($question, 'move'); 00251 } 00252 00253 // Ensure we redirect back to the category the question is being saved into. 00254 $returnurl->param('category', $fromform->category); 00255 00256 if ($movecontext) { 00257 // We are just moving the question to a different context. 00258 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto); 00259 require_capability('moodle/question:add', get_context_instance_by_id($tocontextid)); 00260 question_move_questions_to_category(array($question->id), $tocatid); 00261 00262 } else { 00263 // We are acutally saving the question. 00264 $question = $qtypeobj->save_question($question, $fromform); 00265 if (!empty($CFG->usetags) && isset($fromform->tags)) { 00266 // A wizardpage from multipe pages questiontype like calculated may not 00267 // allow editing the question tags, hence the isset($fromform->tags) test. 00268 require_once($CFG->dirroot.'/tag/lib.php'); 00269 tag_set('question', $question->id, $fromform->tags); 00270 } 00271 } 00272 00273 if (($qtypeobj->finished_edit_wizard($fromform)) || $movecontext) { 00274 if ($inpopup) { 00275 echo $OUTPUT->notification(get_string('changessaved'), ''); 00276 close_window(3); 00277 } else { 00278 $returnurl->param('lastchanged', $question->id); 00279 if ($appendqnumstring) { 00280 $returnurl->param($appendqnumstring, $question->id); 00281 $returnurl->param('sesskey', sesskey()); 00282 $returnurl->param('cmid', $cmid); 00283 } 00284 redirect($returnurl); 00285 } 00286 00287 } else { 00288 $nexturlparams = array( 00289 'returnurl' => $originalreturnurl, 00290 'appendqnumstring' => $appendqnumstring, 00291 'scrollpos' => $scrollpos); 00292 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){ 00293 //useful for passing data to the next page which is not saved in the database. 00294 $nexturlparams += $fromform->nextpageparam; 00295 } 00296 $nexturlparams['id'] = $question->id; 00297 $nexturlparams['wizardnow'] = $fromform->wizard; 00298 $nexturl = new moodle_url('/question/question.php', $nexturlparams); 00299 if ($cmid){ 00300 $nexturl->param('cmid', $cmid); 00301 } else { 00302 $nexturl->param('courseid', $COURSE->id); 00303 } 00304 redirect($nexturl); 00305 } 00306 00307 } else { 00308 $streditingquestion = $qtypeobj->get_heading(); 00309 $PAGE->set_title($streditingquestion); 00310 $PAGE->set_heading($COURSE->fullname); 00311 if ($cm !== null) { 00312 $strmodule = get_string('modulename', $cm->modname); 00313 $streditingmodule = get_string('editinga', 'moodle', $strmodule); 00314 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course))); 00315 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id))); 00316 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){ 00317 //don't need this link if returnurl returns to view.php 00318 $PAGE->navbar->add($streditingmodule, $returnurl); 00319 } 00320 $PAGE->navbar->add($streditingquestion); 00321 echo $OUTPUT->header(); 00322 00323 } else { 00324 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion; 00325 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl); 00326 $PAGE->navbar->add($streditingquestion); 00327 echo $OUTPUT->header(); 00328 } 00329 00330 // Display a heading, question editing form and possibly some extra content needed for 00331 // for this question type. 00332 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow); 00333 echo $OUTPUT->footer(); 00334 }