* @return int
*/
function choice_add_instance($choice) {
- global $DB;
+ global $DB, $CFG;
+ require_once($CFG->dirroot.'/mod/choice/locallib.php');
$choice->timemodified = time();
}
}
+ // Add calendar events if necessary.
+ choice_set_events($choice);
+
return $choice->id;
}
* @return bool
*/
function choice_update_instance($choice) {
- global $DB;
+ global $DB, $CFG;
+ require_once($CFG->dirroot.'/mod/choice/locallib.php');
$choice->id = $choice->instance;
$choice->timemodified = time();
$option->id=$choice->optionid[$key];
if (isset($value) && $value <> '') {
$DB->update_record("choice_options", $option);
- } else { //empty old option - needs to be deleted.
- $DB->delete_records("choice_options", array("id"=>$option->id));
+ } else {
+ // Remove the empty (unused) option.
+ $DB->delete_records("choice_options", array("id" => $option->id));
+ // Delete any answers associated with this option.
+ $DB->delete_records("choice_answers", array("choiceid" => $choice->id, "optionid" => $option->id));
}
} else {
if (isset($value) && $value <> '') {
}
}
+ // Add calendar events if necessary.
+ choice_set_events($choice);
+
return $DB->update_record('choice', $choice);
}
if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
$result = false;
}
+ // Remove old calendar events.
+ if (! $DB->delete_records('event', array('modulename' => 'choice', 'instance' => $choice->id))) {
+ $result = false;
+ }
return $result;
}
// Choice is available.
return array($available, $warnings);
}
+
+/**
+ * This standard function will check all instances of this module
+ * and make sure there are up-to-date events created for each of them.
+ * If courseid = 0, then every chat event in the site is checked, else
+ * only chat events belonging to the course specified are checked.
+ * This function is used, in its new format, by restore_refresh_events()
+ *
+ * @param int $courseid
+ * @return bool
+ */
+function choice_refresh_events($courseid = 0) {
+ global $DB, $CFG;
+ require_once($CFG->dirroot.'/mod/choice/locallib.php');
+
+ if ($courseid) {
+ if (! $choices = $DB->get_records("choice", array("course" => $courseid))) {
+ return true;
+ }
+ } else {
+ if (! $choices = $DB->get_records("choice")) {
+ return true;
+ }
+ }
+
+ foreach ($choices as $choice) {
+ choice_set_events($choice);
+ }
+ return true;
+}
+