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');
|
|
|
|
|
2007-05-07 15:16:07 +00:00
|
|
|
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
|
2007-05-07 07:13:14 +00:00
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
// Read URL parameters telling us which question to edit.
|
|
|
|
$id = optional_param('id', 0, PARAM_INT); // question id
|
2007-05-04 05:47:59 +00:00
|
|
|
$cmid = optional_param('cmid', 0, PARAM_INT);
|
2007-04-11 21:21:06 +00:00
|
|
|
$qtype = optional_param('qtype', '', PARAM_FILE);
|
|
|
|
$categoryid = optional_param('category', 0, PARAM_INT);
|
|
|
|
$wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
|
|
|
|
|
2007-05-04 05:47:59 +00:00
|
|
|
if ($cmid){
|
|
|
|
list($module, $cm) = get_module_from_cmid($cmid);
|
|
|
|
} else {
|
|
|
|
$module = null;
|
|
|
|
$cm = null;
|
|
|
|
}
|
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
// Validate the URL parameters.
|
|
|
|
if ($id) {
|
|
|
|
if (!$question = get_record('question', 'id', $id)) {
|
|
|
|
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.
|
|
|
|
if (!$category = get_record('question_categories', 'id', $question->category)) {
|
|
|
|
print_error('categorydoesnotexist', 'question', $returnurl);
|
|
|
|
}
|
|
|
|
if (!$returnurl) {
|
|
|
|
$returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$category->course}";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the question type.
|
|
|
|
if (!isset($QTYPES[$question->qtype])) {
|
|
|
|
print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
|
|
|
|
}
|
|
|
|
$CFG->pagepath = 'question/type/' . $question->qtype;
|
|
|
|
|
|
|
|
// Check the user is logged in and has enough premissions.
|
|
|
|
require_login($category->course, false);
|
|
|
|
$coursecontext = get_context_instance(CONTEXT_COURSE, $category->course);
|
|
|
|
require_capability('moodle/question:manage', $coursecontext);
|
|
|
|
|
|
|
|
// Create the question editing form.
|
|
|
|
if ($wizardnow!==''){
|
|
|
|
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-04-11 21:21:06 +00:00
|
|
|
$mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow);
|
|
|
|
}
|
|
|
|
} else {
|
2007-04-23 13:55:22 +00:00
|
|
|
$mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question);
|
2007-04-11 21:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($mform === null) {
|
|
|
|
print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
|
|
|
|
}
|
|
|
|
$toform = $question; // send the question object and a few more parameters to the form
|
|
|
|
$toform->returnurl = $returnurl;
|
2007-05-04 05:47:59 +00:00
|
|
|
if ($cm !== null){
|
|
|
|
$toform->cmid = $cm->id;
|
|
|
|
}
|
2007-04-11 21:21:06 +00:00
|
|
|
$mform->set_data($toform);
|
|
|
|
|
|
|
|
if ($mform->is_cancelled()){
|
|
|
|
redirect($returnurl);
|
|
|
|
} elseif ($data = $mform->get_data()){
|
|
|
|
if (!empty($data->makecopy)) {
|
|
|
|
$question->id = 0; // causes a new question to be created.
|
|
|
|
$question->hidden = 0; // Copies should not be hidden
|
|
|
|
}
|
|
|
|
$question = $QTYPES[$question->qtype]->save_question($question, $data, $COURSE, $wizardnow);
|
|
|
|
if ($QTYPES[$qtype]->finished_edit_wizard($data)){
|
|
|
|
if (optional_param('inpopup', 0, PARAM_BOOL)) {
|
|
|
|
notify(get_string('changessaved'), '');
|
|
|
|
close_window(3);
|
2006-02-24 10:21:40 +00:00
|
|
|
} else {
|
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
|
|
|
die;
|
|
|
|
} else {
|
|
|
|
//useful for passing data to the next page which is not saved in the database
|
|
|
|
$queryappend = '';
|
|
|
|
if (isset($data->nextpageparam)){
|
|
|
|
foreach ($data->nextpageparam as $key => $param){
|
|
|
|
$queryappend .= "&".urlencode($key).'='.urlencode($param);
|
2006-10-31 12:06:52 +00:00
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
2007-04-11 21:21:06 +00:00
|
|
|
if ($question->id) {
|
|
|
|
$nexturl = "question.php?id=$question->id&returnurl=" . urlencode($returnurl);
|
|
|
|
} else { // only for creating new questions
|
|
|
|
$nexturl = "question.php?category=$question->category&qtype=$question->qtype&returnurl=".urlencode($returnurl);
|
|
|
|
}
|
|
|
|
redirect($nexturl.'&wizardnow='.$data->wizard.$queryappend, '', 20);
|
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) {
|
|
|
|
$strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $category->course))
|
|
|
|
? update_module_button($cm->id, $category->course, get_string('modulename', $cm->modname))
|
|
|
|
: "";
|
2007-07-05 04:40:48 +00:00
|
|
|
$navlinks = array();
|
|
|
|
$navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$category->course", 'type' => 'activity');
|
|
|
|
$navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title');
|
|
|
|
$navlinks[] = array('name' => get_string('editingquiz', 'quiz'), 'link' => $returnurl, 'type' => 'title');
|
|
|
|
$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-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-04-11 21:21:06 +00:00
|
|
|
$strediting = '<a href="edit.php?courseid='.$category->course.'">'.
|
|
|
|
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-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);
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2007-04-11 21:21:06 +00:00
|
|
|
print_footer($COURSE);
|
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
?>
|