moodle/mod/lesson/action/addcluster.php
2008-06-12 11:52:15 +00:00

69 lines
2.3 KiB
PHP

<?php // $Id$
/**
* Action for adding a cluster page
*
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package lesson
**/
confirm_sesskey();
// first get the preceeding page
// if $pageid = 0, then we are inserting a new page at the beginning of the lesson
$pageid = required_param('pageid', PARAM_INT);
$timenow = time();
if ($pageid == 0) {
if (!$page = $DB->get_record("lesson_pages", array("prevpageid" => 0, "lessonid" => $lesson->id))) {
print_error('cannotfindpagerecord', 'lesson');
}
} else {
if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
print_error('cannotfindpagerecord', 'lesson');
}
}
$newpage = new stdClass;
$newpage->lessonid = $lesson->id;
$newpage->prevpageid = $pageid;
if ($pageid != 0) {
$newpage->nextpageid = $page->nextpageid;
} else {
$newpage->nextpageid = $page->id;
}
$newpage->qtype = LESSON_CLUSTER;
$newpage->timecreated = $timenow;
$newpage->title = get_string("clustertitle", "lesson");
$newpage->contents = get_string("clustertitle", "lesson");
if (!$newpageid = $DB->insert_record("lesson_pages", $newpage)) {
print_error('cannotinsertpage', 'lesson');
}
// update the linked list...
if ($pageid != 0) {
if (!$DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid))) {
print_error('cannotupdatelink', 'lesson');
}
}
if ($pageid == 0) {
$page->nextpageid = $page->id;
}
if ($page->nextpageid) {
// the new page is not the last page
if (!$DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid))) {
print_error('cannotupdatelink', 'lesson');
}
}
// ..and the single "answer"
$newanswer = new stdClass;
$newanswer->lessonid = $lesson->id;
$newanswer->pageid = $newpageid;
$newanswer->timecreated = $timenow;
$newanswer->jumpto = LESSON_CLUSTERJUMP;
if(!$newanswerid = $DB->insert_record("lesson_answers", $newanswer)) {
print_error('cannotinsertanswer', 'lesson');
}
lesson_set_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
?>