function lockdownbrowser_set_settings($quizid, $reqquiz, $reqreview, $exitpass, $monitor)
{
    $ldbopt = new stdClass();
    $ldbopt->attempts = $reqquiz;
    $ldbopt->reviews = $reqreview;
    $ldbopt->password = $exitpass;
    $ldbopt->monitor = $monitor;
    return lockdownbrowser_set_quiz_options($quizid, $ldbopt);
}
function lockdownbrowser_MonitorActionChangeSettings($parameters)
{
    global $DB;
    if (!isloggedin()) {
        lockdownbrowser_MonitorServiceError(2004, "Must be logged in to perform the requested action");
    }
    if (!is_siteadmin()) {
        lockdownbrowser_MonitorServiceError(2024, "Must be logged in as admin to perform the requested action");
    }
    if (!isset($parameters["courseRefId"]) || strlen($parameters["courseRefId"]) == 0) {
        lockdownbrowser_MonitorServiceError(2025, "No courseRefId parameter was specified");
    }
    if (!isset($parameters["examId"]) || strlen($parameters["examId"]) == 0) {
        lockdownbrowser_MonitorServiceError(2026, "No examId parameter was specified");
    }
    if (!isset($parameters["enableLDB"]) || strlen($parameters["enableLDB"]) == 0) {
        lockdownbrowser_MonitorServiceError(2040, "No enableLDB parameter was specified");
    }
    if (!isset($parameters["enableMonitor"]) || strlen($parameters["enableMonitor"]) == 0) {
        lockdownbrowser_MonitorServiceError(2041, "No enableMonitor parameter was specified");
    }
    if (!isset($parameters["exitPassword"])) {
        lockdownbrowser_MonitorServiceError(2042, "No exitPassword parameter was specified");
    }
    if (!isset($parameters["xdata"])) {
        lockdownbrowser_MonitorServiceError(2043, "No xdata parameter was specified");
    }
    $course_id = intval($parameters["courseRefId"]);
    $exam_id = intval($parameters["examId"]);
    $enable_ldb = $parameters["enableLDB"];
    if ($enable_ldb == "0" || strcasecmp($enable_ldb, "false") == 0) {
        $enable_ldb = FALSE;
    } else {
        $enable_ldb = TRUE;
    }
    $enable_monitor = $parameters["enableMonitor"];
    if ($enable_monitor == "0" || strcasecmp($enable_monitor, "false") == 0) {
        $enable_monitor = FALSE;
    } else {
        $enable_monitor = TRUE;
    }
    $exit_password = $parameters["exitPassword"];
    $xdata = $parameters["xdata"];
    if ($enable_monitor) {
        $monitor = $xdata;
    } else {
        $monitor = "";
    }
    $course_module = $DB->get_record("course_modules", array("id" => $exam_id));
    if ($course_module === FALSE) {
        lockdownbrowser_MonitorServiceError(2027, "The specified examId is invalid: {$exam_id}");
    }
    $modrec = $DB->get_record("modules", array("id" => $course_module->module));
    if ($modrec === FALSE) {
        lockdownbrowser_MonitorServiceError(2034, "Could not find the specified quiz (module error)");
    }
    $quiz = $DB->get_record($modrec->name, array("id" => $course_module->instance));
    if ($quiz === FALSE) {
        lockdownbrowser_MonitorServiceError(2035, "Could not find the specified quiz (instance error)");
    }
    // Moodle browser security
    //   popup (0=none, 1=full screen pop-up with some JavaScript security)
    // Moodle 2.2.0+ (quiz module 2011100600+)
    //   browsersecurity ('-', 'securewindow', 'safebrowser')
    // if this setting is not disabled, it will interfere with the LDB integration
    if ($enable_ldb) {
        $quiz->popup = 0;
        $quiz->browsersecurity = "-";
    }
    $ldb_decoration = get_string("requires_ldb", "block_lockdownbrowser");
    $monitor_decoration = get_string("requires_webcam", "block_lockdownbrowser");
    // must be in this order, since the first decoration usually contains the second
    $quiz->name = str_replace($monitor_decoration, "", $quiz->name);
    $quiz->name = str_replace($ldb_decoration, "", $quiz->name);
    if ($enable_ldb) {
        if ($enable_monitor) {
            $quiz->name .= $monitor_decoration;
        } else {
            $quiz->name .= $ldb_decoration;
        }
    }
    $settings = lockdownbrowser_get_quiz_options($quiz->id);
    if ($settings === FALSE) {
        if ($enable_ldb) {
            $ok = lockdownbrowser_set_settings($quiz->id, 0, 0, $exit_password, $monitor);
            if (!$ok) {
                lockdownbrowser_MonitorServiceError(2036, "Quiz settings changes failed (block error)");
            }
        }
    } else {
        // settings found
        if ($enable_ldb) {
            $settings->password = $exit_password;
            $settings->monitor = $monitor;
            $ok = lockdownbrowser_set_quiz_options($quiz->id, $settings);
            if (!$ok) {
                lockdownbrowser_MonitorServiceError(2036, "Quiz settings changes failed (block error)");
            }
        } else {
            lockdownbrowser_delete_options($quiz->id);
        }
    }
    $ok = $DB->update_record($modrec->name, $quiz);
    if (!$ok) {
        lockdownbrowser_MonitorServiceError(2036, "Quiz settings changes failed (module error)");
    }
    rebuild_course_cache($course_id);
    lockdownbrowser_MonitorServiceStatus(1003, "Quiz settings changes succeeded");
}