Skip to content
Browse files

Clean up - modernised choice mod.php

  • Loading branch information...
1 parent 0e30c98 commit 96c245363402f29e95069c0ce1eec9ba92d111af martin committed
Showing with 23 additions and 45 deletions.
  1. +23 −45 mod/choice/mod.php
View
68 mod/choice/mod.php
@@ -9,56 +9,27 @@
//
/////////////////////////////////////////////////////////////
-function add_instance($form) {
+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.
-//
- GLOBAL $db;
-
- $timenow = time();
-
- if (!$rs = $db->Execute("INSERT into choice
- SET course = '$form->course',
- name = '$form->name',
- text = '$form->text',
- answer1 = '$form->answer1',
- answer2 = '$form->answer2',
- timemodified = '$timenow'")) {
- return 0;
- }
-
- // Get it out again - this is the most compatible way to determine the ID
- if ($rs = $db->Execute("SELECT id FROM choice
- WHERE course = $form->course AND timemodified = '$timenow'")) {
- return $rs->fields[0];
- } else {
- return 0;
- }
+
+ $choice->timemodified = time();
+
+ return insert_record("choice", $choice);
}
-function update_instance($form) {
+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.
-//
- GLOBAL $db;
-
- $timenow = time();
-
- if (!$rs = $db->Execute("UPDATE choice
- SET course = '$form->course',
- name = '$form->name',
- text = '$form->text',
- answer1 = '$form->answer1',
- answer2 = '$form->answer2',
- timemodified = '$timenow'
- WHERE id = '$form->instance' ")) {
- return false;
- }
- return true;
+
+ $choice->id = $choice->instance;
+ $choice->timemodified = time();
+
+ return update_record("choice", $choice);
}
@@ -66,15 +37,22 @@ 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 $db;
- if (!$rs = $db->Execute("DELETE from choice WHERE id = '$id' ")) {
+ if (! $choice = get_record("choice", "id", "$id")) {
return false;
}
- return true;
-
+ $result = true;
+
+ if (! delete_records("choice_answers", "choice", "$choice->id")) {
+ $result = false;
+ }
+
+ if (! delete_records("choice", "id", "$choice->id")) {
+ $result = false;
+ }
+
+ return $result;
}

0 comments on commit 96c2453

Please sign in to comment.
Something went wrong with that request. Please try again.