]> git.mjollnir.org Git - moodle.git/commitdiff
Moved all mod.php functions from modules into lib.php, and
authormartin <martin>
Sat, 3 Aug 2002 02:29:21 +0000 (02:29 +0000)
committermartin <martin>
Sat, 3 Aug 2002 02:29:21 +0000 (02:29 +0000)
updated course/mod.php to use them there.  No longer need module/mod.php

12 files changed:
course/mod.php
mod/README [new file with mode: 0644]
mod/assignment/lib.php
mod/assignment/mod.php [deleted file]
mod/choice/lib.php
mod/choice/mod.php [deleted file]
mod/forum/lib.php
mod/forum/mod.php [deleted file]
mod/journal/lib.php
mod/journal/mod.php [deleted file]
mod/survey/lib.php
mod/survey/mod.php [deleted file]

index 193e4501503a622e6b4f9b251e2e73cf35b8d813..adcf63d8bb14888c87354a93d0ae6cf6b4cb8ae8 100644 (file)
             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 (file)
index 0000000..a517ff4
--- /dev/null
@@ -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()
+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..6a8c9958c72ad3f2012bbf89387c6ab03bc8db6c 100644 (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;
+}
+
+
+?>
diff --git a/mod/assignment/mod.php b/mod/assignment/mod.php
deleted file mode 100644 (file)
index 8e588b5..0000000
+++ /dev/null
@@ -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;
-}
-
-
-?>
index a459beb68426e6caffe5bbe93fbe2eef3ba3d67b..25c97ad5fd70e6a643b9df9fa49813475614c99b 100644 (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;
+}
+
+
 ?>
 
diff --git a/mod/choice/mod.php b/mod/choice/mod.php
deleted file mode 100644 (file)
index 702a682..0000000
+++ /dev/null
@@ -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;
-}
-
-
-?>
index cbb62a09ce19e123ac5d50843e990249bef964c8..07c5c00ca35af37c6aea3bd7b53cf9ea7fea4150 100644 (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
diff --git a/mod/forum/mod.php b/mod/forum/mod.php
deleted file mode 100644 (file)
index 440d4f8..0000000
+++ /dev/null
@@ -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;
-}
-
-
-?>
index 1b25dedd0d34ec90f3944472eae940425e6c53a8..32bc4e5138f69013ffe22e7d5fad4d6024f5cd02 100644 (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;
+
+}
+
+
 ?>
diff --git a/mod/journal/mod.php b/mod/journal/mod.php
deleted file mode 100644 (file)
index a22e7de..0000000
+++ /dev/null
@@ -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;
-
-}
-
-
-?>
index c4f0856eb1fe6e750a904e1e2b8a23fc44d5f668..f8e2faf36521ba5cf8853fc201b63fed0b5b995f 100644 (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'");
 }
diff --git a/mod/survey/mod.php b/mod/survey/mod.php
deleted file mode 100644 (file)
index 9534d54..0000000
+++ /dev/null
@@ -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;
-}
-
-
-?>