<commit_before><?PHP // $Id$

//  Moves, adds, updates or deletes modules in a course

    require("../config.php");
    require("lib.php");

    require_login();

    if (isset($SESSION->modform)) {   // Variables are stored in the session
        $mod = $SESSION->modform;
        unset($SESSION->modform);
    } else {
        $mod = (object)$_POST;
    }

    if (isset($cancel)) {  
        if (!empty($SESSION->returnpage)) {
            $return = $SESSION->returnpage;
            unset($SESSION->returnpage);
            redirect($return);
        } else {
            redirect("view.php?id=$mod->course");
        }
    }


    if (isset($_POST["course"])) {    // add or update form submitted

        if (!isteacheredit($mod->course)) {
            error("You can't modify this course!");
        }

        $modlib = "../mod/$mod->modulename/lib.php";
        if (file_exists($modlib)) {
            include_once($modlib);
        } else {
            error("This module is missing important code! ($modlib)");
        }
        $addinstancefunction    = $mod->modulename."_add_instance";
        $updateinstancefunction = $mod->modulename."_update_instance";
        $deleteinstancefunction = $mod->modulename."_delete_instance";
        $moderr = "$CFG->dirroot/mod/$mod->modulename/moderr.html";

        switch ($mod->mode) {
            case "update":
                $return = $updateinstancefunction($mod);
                if (!$return) {
                    if (file_exists($moderr)) {
                        $form = $mod;
                        include_once($moderr);
                        die;
                    }
                    error("Could not update the $mod->modulename", "view.php?id=$mod->course");
                }
                if (is_string($return)) {
                    error($return, "view.php?id=$mod->course");
                }
                add_to_log($mod->course, "course", "update mod", 
                           "../mod/$mod->modulename/view.php?id=$mod->coursemodule", 
                           "$mod->modulename $mod->instance"); 
                break;

            case "add":
                $return = $addinstancefunction($mod);
                if (!$return) {
                    if (file_exists($moderr)) {
                        $form = $mod;
                        include_once($moderr);
                        die;
                    }
                    error("Could not add a new instance of $mod->modulename", "view.php?id=$mod->course");
                }
                if (is_string($return)) {
                    error($return, "view.php?id=$mod->course");
                }
                $mod->instance = $return;
                // course_modules and course_sections each contain a reference 
                // to each other, so we have to update one of them twice.

                if (! $mod->coursemodule = add_course_module($mod) ) {
                    error("Could not add a new course module");
                }
                if (! $sectionid = add_mod_to_section($mod) ) {
                    error("Could not add the new course module to that section");
                }
                if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
                    error("Could not update the course module with the correct section");
                }   
                add_to_log($mod->course, "course", "add mod", 
                           "../mod/$mod->modulename/view.php?id=$mod->coursemodule", 
                           "$mod->modulename $mod->instance"); 
                break;
            case "delete":
                if (! $deleteinstancefunction($mod->instance)) {
                    notify("Could not delete the $mod->modulename (instance)");
                }
                if (! delete_course_module($mod->coursemodule)) {
                    notify("Could not delete the $mod->modulename (coursemodule)");
                }
                if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
                    notify("Could not delete the $mod->modulename from that section");
                }
                add_to_log($mod->course, "course", "delete mod", 
                           "view.php?id=$mod->course", 
                           "$mod->modulename $mod->instance"); <commit_msg>Bug 801 reopened. Problem occur when adding a new activity to a hidden section, it's visibility defaults to 1. Now, default visibility is "inherited" from the section visibility.<commit_after><?PHP // $Id$

//  Moves, adds, updates or deletes modules in a course

    require("../config.php");
    require("lib.php");

    require_login();

    if (isset($SESSION->modform)) {   // Variables are stored in the session
        $mod = $SESSION->modform;
        unset($SESSION->modform);
    } else {
        $mod = (object)$_POST;
    }

    if (isset($cancel)) {  
        if (!empty($SESSION->returnpage)) {
            $return = $SESSION->returnpage;
            unset($SESSION->returnpage);
            redirect($return);
        } else {
            redirect("view.php?id=$mod->course");
        }
    }


    if (isset($_POST["course"])) {    // add or update form submitted

        if (!isteacheredit($mod->course)) {
            error("You can't modify this course!");
        }

        $modlib = "../mod/$mod->modulename/lib.php";
        if (file_exists($modlib)) {
            include_once($modlib);
        } else {
            error("This module is missing important code! ($modlib)");
        }
        $addinstancefunction    = $mod->modulename."_add_instance";
        $updateinstancefunction = $mod->modulename."_update_instance";
        $deleteinstancefunction = $mod->modulename."_delete_instance";
        $moderr = "$CFG->dirroot/mod/$mod->modulename/moderr.html";

        switch ($mod->mode) {
            case "update":
                $return = $updateinstancefunction($mod);
                if (!$return) {
                    if (file_exists($moderr)) {
                        $form = $mod;
                        include_once($moderr);
                        die;
                    }
                    error("Could not update the $mod->modulename", "view.php?id=$mod->course");
                }
                if (is_string($return)) {
                    error($return, "view.php?id=$mod->course");
                }
                add_to_log($mod->course, "course", "update mod", 
                           "../mod/$mod->modulename/view.php?id=$mod->coursemodule", 
                           "$mod->modulename $mod->instance"); 
                break;

            case "add":
                $return = $addinstancefunction($mod);
                if (!$return) {
                    if (file_exists($moderr)) {
                        $form = $mod;
                        include_once($moderr);
                        die;
                    }
                    error("Could not add a new instance of $mod->modulename", "view.php?id=$mod->course");
                }
                if (is_string($return)) {
                    error($return, "view.php?id=$mod->course");
                }
                $mod->instance = $return;
                // course_modules and course_sections each contain a reference 
                // to each other, so we have to update one of them twice.

                if (! $mod->coursemodule = add_course_module($mod) ) {
                    error("Could not add a new course module");
                }
                if (! $sectionid = add_mod_to_section($mod) ) {
                    error("Could not add the new course module to that section");
                }
                //We get the section's visible field status
                $visible = get_field("course_sections","visible","id",$sectionid);

                if (! set_field("course_modules", "visible", $visible, "id", $mod->coursemodule)) {
                    error("Could not update the course module with the correct visibility");
                }   

                if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
                    error("Could not update the course module with the correct section");
                }   
                add_to_log($mod->course, "course", "add mod", 
                           "../mod/$mod->modulename/view.php?id=$mod->coursemodule", 
                           "$mod->modulename $mod->instance"); 
                break;
            case "delete":
                if (! $deleteinstancefunction($mod->instance)) {
                    notify("Could not delete the $mod->modulename (instance)");
                }
                if (! delete_course_module($mod->coursemodule)) {
                    notify("Could not delete the $mod->modulename (coursemodule)");
                }
                if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
                    notify("Could not delete the $mod->modulename from that section");
                }
                add_to_log($mod->course, "course", "delete mod", 
                           "view.php?id=$mod->course", 
                           "$mod->modulename $mod->instance"); 
{'fraction_non_alphanumeric': 0.1361674579197238, 'fraction_numerical': 0.00043159257660768235, 'mean_word_length': 1.4809957173447537, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '54811', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2298, 'n_words': 520}