Merge branch 'MDL-53803_m32v1' of https://github.com/sbourget/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 12 Jul 2016 16:03:08 +0000 (18:03 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 12 Jul 2016 16:03:08 +0000 (18:03 +0200)
1  2 
mod/choice/lib.php

diff --combined mod/choice/lib.php
@@@ -112,8 -112,7 +112,8 @@@ function choice_user_complete($course, 
   * @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);
  
  }
@@@ -600,10 -595,6 +603,10 @@@ function choice_delete_instance($id) 
      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;
  }
@@@ -1081,34 -1072,3 +1084,34 @@@ function choice_get_availability_status
      // 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;
 +}
 +