Example #1
0
/**
 * Given an ID of an instance of this module,
 * this function will permanently delete the instance
 * and any data that depends on it.
 *
 * @param int $id Id of the module instance
 * @return boolean Success/Failure
 **/
function activequiz_delete_instance($id)
{
    global $DB, $CFG;
    require_once $CFG->dirroot . '/mod/activequiz/locallib.php';
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->dirroot . '/question/editlib.php';
    try {
        // make sure the record exists
        $activequiz = $DB->get_record('activequiz', array('id' => $id), '*', MUST_EXIST);
        // go through each session and then delete them (also deletes all attempts for them)
        $sessions = $DB->get_records('activequiz_sessions', array('activequizid' => $activequiz->id));
        foreach ($sessions as $session) {
            \mod_activequiz\activequiz_session::delete($session->id);
        }
        // delete all questions for this quiz
        $DB->delete_records('activequiz_questions', array('activequizid' => $activequiz->id));
        // finally delete the activequiz object
        $DB->delete_records('activequiz', array('id' => $activequiz->id));
    } catch (Exception $e) {
        return false;
    }
    return true;
}