Moved all mod.php functions from modules into lib.php, and

updated course/mod.php to use them there.  No longer need module/mod.php
This commit is contained in:
martin
2002-08-03 02:29:21 +00:00
parent 8c3c848114
commit 04eba58f57
12 changed files with 355 additions and 384 deletions
+10 -7
View File
@@ -14,23 +14,26 @@
error("You can't modify this course!");
}
$modcode = "../mod/$mod->modulename/mod.php";
if (file_exists($modcode)) {
include($modcode);
$modlib = "../mod/$mod->modulename/lib.php";
if (file_exists($modlib)) {
include($modlib);
} else {
error("This module is missing important code! (mod.php)");
error("This module is missing important code! ($modlib)");
}
$addinstancefunction = $mod->modulename."_add_instance";
$updateinstancefunction = $mod->modulename."_update_instance";
$deleteinstancefunction = $mod->modulename."_delete_instance";
switch ($mod->mode) {
case "update":
if (! update_instance($mod)) {
if (! $updateinstancefunction($mod)) {
error("Could not update the $mod->modulename");
}
add_to_log($mod->course, "course", "update mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
break;
case "add":
if (! $mod->instance = add_instance($mod)) {
if (! $mod->instance = $addinstancefunction($mod)) {
error("Could not add a new instance of $mod->modulename");
}
// course_modules and course_sections each contain a reference
@@ -48,7 +51,7 @@
add_to_log($mod->course, "course", "add mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
break;
case "delete":
if (! delete_instance($mod->instance)) {
if (! $deleteinstancefunction($mod->instance)) {
notify("Could not delete the $mod->modulename (instance)");
}
if (! delete_course_module($mod->coursemodule)) {
+25
View File
@@ -0,0 +1,25 @@
This directory contains all the learning modules.
Standard components expected of each module:
mod.html: a form to setup/update a module instance
version.php: defines some meta-info and provides upgrading code
icon.gif: a 16x16 icon for the module
db/mysql.sql: an SQL dump of all the required db tables and data
index.php: a page to list all instances in a course
view.php: a page to view a particular instance
lib.php: any/all functions defined by the module should be in here.
constants should be defined using MODULENAME_xxxxxx
functions should be defined using modulename_xxxxxx
There are a number of standard functions:
modulename_add_instance()
modulename_update_instance()
modulename_delete_instance()
modulename_user_complete()
modulename_user_outline()
modulename_cron()
+50
View File
@@ -0,0 +1,50 @@
<?PHP // $Id$
function assignment_add_instance($assignment) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
$assignment->timemodified = time();
return insert_record("assignment", $assignment);
}
function assignment_update_instance($assignment) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$assignment->timemodified = time();
$assignment->id = $assignment->instance;
return update_record("assignment", $assignment);
}
function assignment_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $assignment = get_record("assignment", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
$result = false;
}
if (! delete_records("assignment", "id", "$assignment->id")) {
$result = false;
}
return $result;
}
?>
-59
View File
@@ -1,59 +0,0 @@
<?PHP // $Id$
/////////////////////////////////////////////////////////////
//
// MOD.PHP - contains functions to add, update and delete
// an instance of this module
//
// Generally called from /course/mod.php
//
/////////////////////////////////////////////////////////////
function add_instance($assignment) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
$assignment->timemodified = time();
return insert_record("assignment", $assignment);
}
function update_instance($assignment) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$assignment->timemodified = time();
$assignment->id = $assignment->instance;
return update_record("assignment", $assignment);
}
function delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $assignment = get_record("assignment", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
$result = false;
}
if (! delete_records("assignment", "id", "$assignment->id")) {
$result = false;
}
return $result;
}
?>
+48
View File
@@ -35,5 +35,53 @@ function choice_user_complete($course, $user, $mod, $choice) {
}
}
function choice_add_instance($choice) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
$choice->timemodified = time();
return insert_record("choice", $choice);
}
function choice_update_instance($choice) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$choice->id = $choice->instance;
$choice->timemodified = time();
return update_record("choice", $choice);
}
function choice_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $choice = get_record("choice", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("choice_answers", "choice", "$choice->id")) {
$result = false;
}
if (! delete_records("choice", "id", "$choice->id")) {
$result = false;
}
return $result;
}
?>
-59
View File
@@ -1,59 +0,0 @@
<?PHP // $Id$
/////////////////////////////////////////////////////////////
//
// MOD.PHP - contains functions to add, update and delete
// an instance of this module
//
// Generally called from /course/mod.php
//
/////////////////////////////////////////////////////////////
function add_instance($choice) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
$choice->timemodified = time();
return insert_record("choice", $choice);
}
function update_instance($choice) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$choice->id = $choice->instance;
$choice->timemodified = time();
return update_record("choice", $choice);
}
function delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $choice = get_record("choice", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("choice_answers", "choice", "$choice->id")) {
$result = false;
}
if (! delete_records("choice", "id", "$choice->id")) {
$result = false;
}
return $result;
}
?>
+109
View File
@@ -500,6 +500,115 @@ function forum_user_complete($course, $user, $mod, $forum) {
}
function forum_add_instance($forum) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
global $CFG;
$forum->timemodified = time();
if (! $forum->id = insert_record("forum", $forum)) {
return false;
}
if ($forum->type == "single") { // Create related discussion.
$discussion->course = $forum->course;
$discussion->forum = $forum->id;
$discussion->name = $forum->name;
$discussion->intro = $forum->intro;
$discussion->assessed = $forum->assessed;
if (! forum_add_discussion($discussion)) {
error("Could not add the discussion for this forum");
}
}
add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
return $forum->id;
}
function forum_update_instance($forum) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$forum->timemodified = time();
$forum->id = $forum->instance;
if ($forum->type == "single") { // Update related discussion and post.
if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
notify("Warning! There is more than one discussion in this forum - using the most recent");
$discussion = array_pop($discussions);
} else {
error("Could not find the discussion in this forum");
}
}
if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
error("Could not find the first post in this forum discussion");
}
$post->subject = $forum->name;
$post->message = $forum->intro;
$post->modified = $forum->timemodified;
if (! update_record("forum_posts", $post)) {
error("Could not update the first post");
}
$discussion->name = $forum->name;
if (! update_record("forum_discussions", $discussion)) {
error("Could not update the discussion");
}
}
if (update_record("forum", $forum)) {
add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
return true;
} else {
return false;
}
}
function forum_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $forum = get_record("forum", "id", "$id")) {
return false;
}
$result = true;
if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
foreach ($discussions as $discussion) {
if (! forum_delete_discussion($discussion)) {
$result = false;
}
}
}
if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
$result = false;
}
if (! delete_records("forum", "id", "$forum->id")) {
$result = false;
}
return $result;
}
function forum_cron () {
// Function to be run periodically according to the moodle cron
// Finds all posts that have yet to be mailed out, and mails them
-125
View File
@@ -1,125 +0,0 @@
<?PHP // $Id$
/////////////////////////////////////////////////////////////
//
// MOD.PHP - contains functions to add, update and delete
// an instance of this module
//
// Generally called from /course/mod.php
//
/////////////////////////////////////////////////////////////
function add_instance($forum) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
global $CFG;
$forum->timemodified = time();
if (! $forum->id = insert_record("forum", $forum)) {
return false;
}
if ($forum->type == "single") { // Create related discussion.
include_once("$CFG->dirroot/mod/forum/lib.php");
$discussion->course = $forum->course;
$discussion->forum = $forum->id;
$discussion->name = $forum->name;
$discussion->intro = $forum->intro;
$discussion->assessed = $forum->assessed;
if (! forum_add_discussion($discussion)) {
error("Could not add the discussion for this forum");
}
}
add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
return $forum->id;
}
function update_instance($forum) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$forum->timemodified = time();
$forum->id = $forum->instance;
if ($forum->type == "single") { // Update related discussion and post.
if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
notify("Warning! There is more than one discussion in this forum - using the most recent");
$discussion = array_pop($discussions);
} else {
error("Could not find the discussion in this forum");
}
}
if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
error("Could not find the first post in this forum discussion");
}
$post->subject = $forum->name;
$post->message = $forum->intro;
$post->modified = $forum->timemodified;
if (! update_record("forum_posts", $post)) {
error("Could not update the first post");
}
$discussion->name = $forum->name;
if (! update_record("forum_discussions", $discussion)) {
error("Could not update the discussion");
}
}
if (update_record("forum", $forum)) {
add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
return true;
} else {
return false;
}
}
function delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
global $CFG;
include_once("$CFG->dirroot/mod/forum/lib.php");
if (! $forum = get_record("forum", "id", "$id")) {
return false;
}
$result = true;
if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
foreach ($discussions as $discussion) {
if (! forum_delete_discussion($discussion)) {
$result = false;
}
}
}
if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
$result = false;
}
if (! delete_records("forum", "id", "$forum->id")) {
$result = false;
}
return $result;
}
?>
+49
View File
@@ -201,4 +201,53 @@ function journal_print_user_entry($course, $user, $entry, $teachers, $ratings) {
echo "</TABLE><BR CLEAR=ALL>\n";
}
function journal_add_instance($journal) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
$journal->timemodified = time();
return insert_record("journal", $journal);
}
function journal_update_instance($journal) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$journal->timemodified = time();
$journal->id = $journal->instance;
return update_record("journal", $journal);
}
function journal_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $journal = get_record("journal", "id", $id)) {
return false;
}
$result = true;
if (! delete_records("journal_entries", "journal", $journal->id)) {
$result = false;
}
if (! delete_records("journal", "id", $journal->id)) {
$result = false;
}
return $result;
}
?>
-60
View File
@@ -1,60 +0,0 @@
<?PHP // $Id$
/////////////////////////////////////////////////////////////
//
// MOD.PHP - contains functions to add, update and delete
// an instance of this module
//
// Generally called from /course/mod.php
//
/////////////////////////////////////////////////////////////
function add_instance($journal) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
$journal->timemodified = time();
return insert_record("journal", $journal);
}
function update_instance($journal) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
$journal->timemodified = time();
$journal->id = $journal->instance;
return update_record("journal", $journal);
}
function delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $journal = get_record("journal", "id", $id)) {
return false;
}
$result = true;
if (! delete_records("journal_entries", "journal", $journal->id)) {
$result = false;
}
if (! delete_records("journal", "id", $journal->id)) {
$result = false;
}
return $result;
}
?>
+64
View File
@@ -14,6 +14,70 @@ $SURVEY_QTYPE = array (
"3" => "Actual and Preferred",
);
// FUNCTIONS ////////////////////////////////////////////////////////
function survey_add_instance($survey) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
if (!$template = get_record("survey", "id", $survey->template)) {
return 0;
}
$survey->questions = $template->questions;
$survey->timecreated = time();
$survey->timemodified = $survey->timecreated;
return insert_record("survey", $survey);
}
function survey_update_instance($survey) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
if (!$template = get_record("survey", "id", $survey->template)) {
return 0;
}
$survey->id = $survey->instance;
$survey->questions = $template->questions;
$survey->timemodified = time();
return update_record("survey", $survey);
}
function survey_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $survey = get_record("survey", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("survey_analysis", "survey", "$survey->id")) {
$result = false;
}
if (! delete_records("survey_answers", "survey", "$survey->id")) {
$result = false;
}
if (! delete_records("survey", "id", "$survey->id")) {
$result = false;
}
return $result;
}
function survey_already_done($survey, $user) {
return record_exists_sql("SELECT * FROM survey_answers WHERE survey='$survey' AND user='$user'");
}
-74
View File
@@ -1,74 +0,0 @@
<?PHP // $Id$
/////////////////////////////////////////////////////////////
//
// MOD.PHP - contains functions to add, update and delete
// an instance of this module
//
// Generally called from /course/mod.php
//
/////////////////////////////////////////////////////////////
function add_instance($survey) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
if (!$template = get_record("survey", "id", $survey->template)) {
return 0;
}
$survey->questions = $template->questions;
$survey->timecreated = time();
$survey->timemodified = $survey->timecreated;
return insert_record("survey", $survey);
}
function update_instance($survey) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
if (!$template = get_record("survey", "id", $survey->template)) {
return 0;
}
$survey->id = $survey->instance;
$survey->questions = $template->questions;
$survey->timemodified = time();
return update_record("survey", $survey);
}
function delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $survey = get_record("survey", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("survey_analysis", "survey", "$survey->id")) {
$result = false;
}
if (! delete_records("survey_answers", "survey", "$survey->id")) {
$result = false;
}
if (! delete_records("survey", "id", "$survey->id")) {
$result = false;
}
return $result;
}
?>