From: martin Date: Sat, 3 Aug 2002 02:29:21 +0000 (+0000) Subject: Moved all mod.php functions from modules into lib.php, and X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=04eba58f57ee5975543c8b0a99f135dabcbab89c;p=moodle.git 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 --- diff --git a/course/mod.php b/course/mod.php index 193e450150..adcf63d8bb 100644 --- a/course/mod.php +++ b/course/mod.php @@ -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)) { diff --git a/mod/README b/mod/README new file mode 100644 index 0000000000..a517ff4f25 --- /dev/null +++ b/mod/README @@ -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() + diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index e69de29bb2..6a8c9958c7 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -0,0 +1,50 @@ +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; +} + + +?> diff --git a/mod/assignment/mod.php b/mod/assignment/mod.php deleted file mode 100644 index 8e588b5468..0000000000 --- a/mod/assignment/mod.php +++ /dev/null @@ -1,59 +0,0 @@ -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; -} - - -?> diff --git a/mod/choice/lib.php b/mod/choice/lib.php index a459beb684..25c97ad5fd 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -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; +} + + ?> diff --git a/mod/choice/mod.php b/mod/choice/mod.php deleted file mode 100644 index 702a682bf0..0000000000 --- a/mod/choice/mod.php +++ /dev/null @@ -1,59 +0,0 @@ -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; -} - - -?> diff --git a/mod/forum/lib.php b/mod/forum/lib.php index cbb62a09ce..07c5c00ca3 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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 diff --git a/mod/forum/mod.php b/mod/forum/mod.php deleted file mode 100644 index 440d4f803a..0000000000 --- a/mod/forum/mod.php +++ /dev/null @@ -1,125 +0,0 @@ -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; -} - - -?> diff --git a/mod/journal/lib.php b/mod/journal/lib.php index 1b25dedd0d..32bc4e5138 100644 --- a/mod/journal/lib.php +++ b/mod/journal/lib.php @@ -201,4 +201,53 @@ function journal_print_user_entry($course, $user, $entry, $teachers, $ratings) { echo "
\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; + +} + + ?> diff --git a/mod/journal/mod.php b/mod/journal/mod.php deleted file mode 100644 index a22e7de5aa..0000000000 --- a/mod/journal/mod.php +++ /dev/null @@ -1,60 +0,0 @@ -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; - -} - - -?> diff --git a/mod/survey/lib.php b/mod/survey/lib.php index c4f0856eb1..f8e2faf365 100644 --- a/mod/survey/lib.php +++ b/mod/survey/lib.php @@ -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'"); } diff --git a/mod/survey/mod.php b/mod/survey/mod.php deleted file mode 100644 index 9534d546c5..0000000000 --- a/mod/survey/mod.php +++ /dev/null @@ -1,74 +0,0 @@ -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; -} - - -?>