2007-03-19 17:22:46 +00:00
|
|
|
<?php // $Id$
|
2006-07-06 09:45:02 +00:00
|
|
|
/**
|
2007-04-11 21:21:06 +00:00
|
|
|
* Page for editing questions using the new form library.
|
2006-07-06 09:45:02 +00:00
|
|
|
*
|
2007-04-11 21:21:06 +00:00
|
|
|
* @author T.J.Hunt@open.ac.uk
|
2006-07-06 09:45:02 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
2007-03-19 17:22:46 +00:00
|
|
|
* @package questionbank
|
2006-07-06 09:45:02 +00:00
|
|
|
*//** */
|
2007-01-10 06:40:42 +00:00
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
// Includes.
|
|
|
|
require_once(dirname(__FILE__) . '/../config.php');
|
|
|
|
require_once(dirname(__FILE__) . '/editlib.php');
|
|
|
|
require_once($CFG->libdir . '/filelib.php');
|
|
|
|
require_once($CFG->libdir . '/formslib.php');
|
|
|
|
|
|
|
|
// Read URL parameters telling us which question to edit.
|
|
|
|
$id = optional_param('id', 0, PARAM_INT); // question id
|
|
|
|
$qtype = optional_param('qtype', '', PARAM_FILE);
|
|
|
|
$categoryid = optional_param('category', 0, PARAM_INT);
|
2007-08-09 21:51:09 +00:00
|
|
|
|
|
|
|
$cmid = optional_param('cmid', 0, PARAM_INT);
|
|
|
|
$courseid = optional_param('courseid', 0, PARAM_INT);
|
2007-04-11 21:21:06 +00:00
|
|
|
$wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
|
2007-08-09 21:51:09 +00:00
|
|
|
$movecontext = optional_param('movecontext', 0, PARAM_BOOL);//switch to make question
|
2007-08-16 05:25:00 +00:00
|
|
|
//uneditable - form is displayed to edit category only
|
2007-08-09 21:51:09 +00:00
|
|
|
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
|
2008-11-20 06:59:11 +00:00
|
|
|
$appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2007-08-17 01:06:02 +00:00
|
|
|
$inpopup = optional_param('inpopup', 0, PARAM_BOOL);
|
|
|
|
|
2007-08-09 21:51:09 +00:00
|
|
|
if ($movecontext && !$id){
|
|
|
|
print_error('questiondoesnotexist', 'question', $returnurl);
|
|
|
|
}
|
2007-04-11 21:21:06 +00:00
|
|
|
|
2007-05-04 05:47:59 +00:00
|
|
|
if ($cmid){
|
2007-08-09 21:51:09 +00:00
|
|
|
list($module, $cm) = get_module_from_cmid($cmid);
|
|
|
|
require_login($cm->course, false, $cm);
|
|
|
|
$thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
|
|
|
|
} elseif ($courseid) {
|
|
|
|
require_login($courseid, false);
|
|
|
|
$thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
|
2007-05-04 05:47:59 +00:00
|
|
|
$module = null;
|
|
|
|
$cm = null;
|
2007-08-09 21:51:09 +00:00
|
|
|
} else {
|
2008-05-21 07:53:23 +00:00
|
|
|
print_error('missingcourseorcmid', 'question');
|
2007-05-04 05:47:59 +00:00
|
|
|
}
|
2007-08-09 21:51:09 +00:00
|
|
|
$contexts = new question_edit_contexts($thiscontext);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$returnurl) {
|
|
|
|
$returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-04 05:47:59 +00:00
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
if ($id) {
|
2008-06-06 14:43:15 +00:00
|
|
|
if (!$question = $DB->get_record('question', array('id' => $id))) {
|
2007-04-11 21:21:06 +00:00
|
|
|
print_error('questiondoesnotexist', 'question', $returnurl);
|
|
|
|
}
|
|
|
|
get_question_options($question);
|
|
|
|
} else if ($categoryid && $qtype) { // only for creating new questions
|
|
|
|
$question = new stdClass;
|
|
|
|
$question->category = $categoryid;
|
|
|
|
$question->qtype = $qtype;
|
|
|
|
} else {
|
|
|
|
print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the question category.
|
2008-06-06 14:43:15 +00:00
|
|
|
if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
|
2007-04-11 21:21:06 +00:00
|
|
|
print_error('categorydoesnotexist', 'question', $returnurl);
|
|
|
|
}
|
2007-08-09 21:51:09 +00:00
|
|
|
|
|
|
|
//permissions
|
|
|
|
$question->formoptions = new object();
|
|
|
|
|
|
|
|
$categorycontext = get_context_instance_by_id($category->contextid);
|
|
|
|
$addpermission = has_capability('moodle/question:add', $categorycontext);
|
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
$canview = question_has_capability_on($question, 'view');
|
|
|
|
if ($movecontext){
|
|
|
|
$question->formoptions->canedit = false;
|
|
|
|
$question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
|
|
|
|
$question->formoptions->cansaveasnew = false;
|
|
|
|
$question->formoptions->repeatelements = false;
|
|
|
|
$question->formoptions->movecontext = true;
|
|
|
|
$formeditable = true;
|
|
|
|
question_require_capability_on($question, 'view');
|
|
|
|
} else {
|
|
|
|
$question->formoptions->canedit = question_has_capability_on($question, 'edit');
|
|
|
|
$question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
|
|
|
|
$question->formoptions->cansaveasnew = (($canview ||question_has_capability_on($question, 'edit')) && $addpermission);
|
|
|
|
$question->formoptions->repeatelements = ($question->formoptions->canedit || $question->formoptions->cansaveasnew);
|
|
|
|
$formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
|
|
|
|
$question->formoptions->movecontext = false;
|
|
|
|
if (!$formeditable){
|
|
|
|
question_require_capability_on($question, 'view');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else { // creating a new question
|
|
|
|
require_capability('moodle/question:add', $categorycontext);
|
|
|
|
$formeditable = true;
|
|
|
|
$question->formoptions->repeatelements = true;
|
|
|
|
$question->formoptions->movecontext = false;
|
2007-04-11 21:21:06 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 05:25:00 +00:00
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
// Validate the question type.
|
|
|
|
if (!isset($QTYPES[$question->qtype])) {
|
|
|
|
print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
|
|
|
|
}
|
|
|
|
$CFG->pagepath = 'question/type/' . $question->qtype;
|
|
|
|
|
|
|
|
|
|
|
|
// Create the question editing form.
|
2007-08-09 21:51:09 +00:00
|
|
|
if ($wizardnow!=='' && !$movecontext){
|
2007-04-11 21:21:06 +00:00
|
|
|
if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
|
|
|
|
print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
|
2006-02-24 10:21:40 +00:00
|
|
|
} else {
|
2007-08-09 21:51:09 +00:00
|
|
|
$mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
|
2007-04-11 21:21:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-08-09 21:51:09 +00:00
|
|
|
$mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
|
2007-04-11 21:21:06 +00:00
|
|
|
}
|
|
|
|
if ($mform === null) {
|
|
|
|
print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
|
|
|
|
}
|
2007-08-16 05:25:00 +00:00
|
|
|
$toform = fullclone($question); // send the question object and a few more parameters to the form
|
|
|
|
$toform->category = "$category->id,$category->contextid";
|
|
|
|
if ($formeditable && $id){
|
|
|
|
$toform->categorymoveto = $toform->category;
|
|
|
|
}
|
2008-11-20 06:59:11 +00:00
|
|
|
|
|
|
|
$toform->appendqnumstring = $appendqnumstring;
|
2007-04-11 21:21:06 +00:00
|
|
|
$toform->returnurl = $returnurl;
|
2007-08-09 21:51:09 +00:00
|
|
|
$toform->movecontext = $movecontext;
|
2007-05-04 05:47:59 +00:00
|
|
|
if ($cm !== null){
|
|
|
|
$toform->cmid = $cm->id;
|
2007-08-09 21:51:09 +00:00
|
|
|
$toform->courseid = $cm->course;
|
|
|
|
} else {
|
|
|
|
$toform->courseid = $COURSE->id;
|
2007-05-04 05:47:59 +00:00
|
|
|
}
|
2007-09-27 08:17:09 +00:00
|
|
|
$toform->inpopup = $inpopup;
|
2007-04-11 21:21:06 +00:00
|
|
|
$mform->set_data($toform);
|
|
|
|
|
|
|
|
if ($mform->is_cancelled()){
|
2007-09-27 08:17:09 +00:00
|
|
|
if ($inpopup) {
|
|
|
|
close_window();
|
|
|
|
} else {
|
|
|
|
redirect($returnurl);
|
|
|
|
}
|
2007-08-16 05:25:00 +00:00
|
|
|
} elseif ($fromform = $mform->get_data()){
|
2007-08-09 21:51:09 +00:00
|
|
|
$returnurl = new moodle_url($returnurl);
|
|
|
|
//select category that question has been saved in / moved to when we return to question bank
|
2007-08-16 05:25:00 +00:00
|
|
|
if (!empty($fromform->categorymoveto)){
|
|
|
|
$returnurl->param('category', $fromform->categorymoveto);
|
|
|
|
} else if (!empty($fromform->category)){
|
|
|
|
$returnurl->param('category', $fromform->category);
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
$returnurl = $returnurl->out();
|
2007-08-16 05:25:00 +00:00
|
|
|
if (!empty($fromform->makecopy)) {
|
2007-04-11 21:21:06 +00:00
|
|
|
$question->id = 0; // causes a new question to be created.
|
|
|
|
$question->hidden = 0; // Copies should not be hidden
|
|
|
|
}
|
2007-08-09 21:51:09 +00:00
|
|
|
if ($movecontext){
|
2007-08-16 05:25:00 +00:00
|
|
|
list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
|
2007-08-09 21:51:09 +00:00
|
|
|
$tocontext = get_context_instance_by_id($tocontextid);
|
|
|
|
require_capability('moodle/question:add', $tocontext);
|
|
|
|
if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
|
|
|
|
$movecontexturl = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
|
|
|
|
array('returnurl' => $returnurl,
|
|
|
|
'ids'=>$question->id,
|
|
|
|
'tocatid'=> $tocatid));
|
|
|
|
if ($cmid){
|
|
|
|
$movecontexturl->param('cmid', $cmid);
|
|
|
|
} else {
|
|
|
|
$movecontexturl->param('courseid', $COURSE->id);
|
|
|
|
}
|
|
|
|
redirect($movecontexturl->out());
|
|
|
|
}
|
|
|
|
}
|
2007-08-16 05:25:00 +00:00
|
|
|
|
|
|
|
$question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow);
|
2007-08-17 01:06:02 +00:00
|
|
|
if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext){
|
|
|
|
if ($inpopup) {
|
2007-04-11 21:21:06 +00:00
|
|
|
notify(get_string('changessaved'), '');
|
|
|
|
close_window(3);
|
2006-02-24 10:21:40 +00:00
|
|
|
} else {
|
2008-11-20 06:59:11 +00:00
|
|
|
if($appendqnumstring){
|
|
|
|
$returnurl_object=new moodle_url($returnurl);
|
|
|
|
$returnurl=$returnurl_object->out(false,array($appendqnumstring=>($question->id), "sesskey"=>sesskey(), "cmid"=>$cmid));
|
|
|
|
}
|
2007-05-07 06:35:33 +00:00
|
|
|
redirect($returnurl);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
2007-04-11 21:21:06 +00:00
|
|
|
} else {
|
2008-11-20 06:59:11 +00:00
|
|
|
$nexturlparams = array('returnurl'=>$returnurl, 'appendqnumstring'=>$appendqnumstring);
|
2007-08-17 01:06:02 +00:00
|
|
|
if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
|
|
|
|
$nexturlparams += $fromform->nextpageparam;//useful for passing data to the next page which is not saved in the database
|
2007-04-11 21:21:06 +00:00
|
|
|
}
|
2007-08-17 01:06:02 +00:00
|
|
|
$nexturlparams['id'] = $question->id;
|
2007-08-16 05:25:00 +00:00
|
|
|
$nexturlparams['wizardnow'] = $fromform->wizard;
|
2007-08-09 21:51:09 +00:00
|
|
|
$nexturl = new moodle_url('question.php', $nexturlparams);
|
2007-08-17 01:06:02 +00:00
|
|
|
if ($cmid){
|
|
|
|
$nexturl->param('cmid', $cmid);
|
|
|
|
} else {
|
|
|
|
$nexturl->param('courseid', $COURSE->id);
|
|
|
|
}
|
|
|
|
redirect($nexturl->out());
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
2007-04-11 21:21:06 +00:00
|
|
|
} else {
|
|
|
|
|
2007-05-04 05:47:59 +00:00
|
|
|
list($streditingquestion,) = $QTYPES[$question->qtype]->get_heading();
|
|
|
|
if ($cm !== null) {
|
2007-09-11 08:23:00 +00:00
|
|
|
$strmodule = get_string('modulename', $cm->modname);
|
2007-08-09 21:51:09 +00:00
|
|
|
$strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
|
2007-09-11 08:23:00 +00:00
|
|
|
? update_module_button($cm->id, $cm->course, $strmodule)
|
2007-05-04 05:47:59 +00:00
|
|
|
: "";
|
2007-09-11 08:23:00 +00:00
|
|
|
|
|
|
|
$streditingmodule = get_string('editinga', 'moodle', $strmodule);
|
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
$navlinks = array();
|
2007-08-10 10:00:06 +00:00
|
|
|
$navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
|
2007-08-15 11:36:01 +00:00
|
|
|
$navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
|
2007-09-11 08:23:00 +00:00
|
|
|
if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
|
|
|
|
//don't need this link if returnurl returns to view.php
|
|
|
|
$navlinks[] = array('name' => $streditingmodule, 'link' => $returnurl, 'type' => 'title');
|
|
|
|
}
|
2007-07-05 04:40:48 +00:00
|
|
|
$navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
|
|
|
|
$navigation = build_navigation($navlinks);
|
2007-05-04 05:47:59 +00:00
|
|
|
print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2007-01-10 06:46:12 +00:00
|
|
|
} else {
|
2007-07-05 04:40:48 +00:00
|
|
|
$navlinks = array();
|
|
|
|
$navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
|
|
|
|
$navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
|
2007-08-14 02:56:43 +00:00
|
|
|
$strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.
|
2007-04-11 21:21:06 +00:00
|
|
|
get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
|
2007-07-05 04:40:48 +00:00
|
|
|
$navigation = build_navigation($navlinks);
|
2007-05-04 05:47:59 +00:00
|
|
|
print_header_simple($streditingquestion, '', $navigation);
|
2007-01-10 06:46:12 +00:00
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
// Display a heading, question editing form and possibly some extra content needed for
|
|
|
|
// for this question type.
|
|
|
|
$QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
|
|
|
|
print_footer($COURSE);
|
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
?>
|