| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 4 | //
 | 
        
           |  |  | 5 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 6 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 7 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 8 | // (at your option) any later version.
 | 
        
           |  |  | 9 | //
 | 
        
           |  |  | 10 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 13 | // GNU General Public License for more details.
 | 
        
           |  |  | 14 | //
 | 
        
           |  |  | 15 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 16 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 | * Adds or updates modules in a course using new formslib
 | 
        
           |  |  | 20 | *
 | 
        
           |  |  | 21 | * @package    moodlecore
 | 
        
           |  |  | 22 | * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
 | 
        
           |  |  | 23 | * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 | */
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | require_once("../config.php");
 | 
        
           |  |  | 27 | require_once("lib.php");
 | 
        
           |  |  | 28 | require_once($CFG->libdir.'/filelib.php');
 | 
        
           |  |  | 29 | require_once($CFG->libdir.'/gradelib.php');
 | 
        
           |  |  | 30 | require_once($CFG->libdir.'/completionlib.php');
 | 
        
           |  |  | 31 | require_once($CFG->libdir.'/plagiarismlib.php');
 | 
        
           |  |  | 32 | require_once($CFG->dirroot . '/course/modlib.php');
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 | $add    = optional_param('add', '', PARAM_ALPHANUM);     // Module name.
 | 
        
           |  |  | 35 | $update = optional_param('update', 0, PARAM_INT);
 | 
        
           |  |  | 36 | $return = optional_param('return', 0, PARAM_BOOL);    //return to course/view.php if false or mod/modname/view.php if true
 | 
        
           |  |  | 37 | $type   = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
 | 
        
           |  |  | 38 | $sectionreturn = optional_param('sr', null, PARAM_INT);
 | 
        
           |  |  | 39 | $beforemod = optional_param('beforemod', 0, PARAM_INT);
 | 
        
           |  |  | 40 | $showonly = optional_param('showonly', '', PARAM_TAGLIST); // Settings group to show expanded and hide the rest.
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | // Force it to be null if it's not a valid section number.
 | 
        
           |  |  | 43 | if ($sectionreturn < 0) {
 | 
        
           |  |  | 44 |     $sectionreturn = null;
 | 
        
           |  |  | 45 | }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | $url = new moodle_url('/course/modedit.php');
 | 
        
           |  |  | 48 | if (!is_null($sectionreturn)) {
 | 
        
           |  |  | 49 |     $url->param('sr', $sectionreturn);
 | 
        
           |  |  | 50 | }
 | 
        
           |  |  | 51 | if (!empty($return)) {
 | 
        
           |  |  | 52 |     $url->param('return', $return);
 | 
        
           |  |  | 53 | }
 | 
        
           |  |  | 54 | if (!empty($showonly)) {
 | 
        
           |  |  | 55 |     $url->param('showonly', $showonly);
 | 
        
           |  |  | 56 | }
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | if (!empty($add)) {
 | 
        
           |  |  | 59 |     $section = required_param('section', PARAM_INT);
 | 
        
           |  |  | 60 |     $course  = required_param('course', PARAM_INT);
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |     $url->param('add', $add);
 | 
        
           |  |  | 63 |     $url->param('section', $section);
 | 
        
           |  |  | 64 |     $url->param('course', $course);
 | 
        
           |  |  | 65 |     $PAGE->set_url($url);
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |     $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
 | 
        
           |  |  | 68 |     require_login($course);
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     // There is no page for this in the navigation. The closest we'll have is the course section.
 | 
        
           |  |  | 71 |     // If the course section isn't displayed on the navigation this will fall back to the course which
 | 
        
           |  |  | 72 |     // will be the closest match we have.
 | 
        
           |  |  | 73 |     navigation_node::override_active_url(course_get_url($course, $section));
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     // MDL-69431 Validate that $section (url param) does not exceed the maximum for this course / format.
 | 
        
           |  |  | 76 |     // If too high (e.g. section *id* not number) non-sequential sections inserted in course_sections table.
 | 
        
           |  |  | 77 |     // Then on import, backup fills 'gap' with empty sections (see restore_rebuild_course_cache). Avoid this.
 | 
        
           |  |  | 78 |     $courseformat = course_get_format($course);
 | 
        
           |  |  | 79 |     $maxsections = $courseformat->get_max_sections();
 | 
        
           |  |  | 80 |     if ($section > $maxsections) {
 | 
        
           |  |  | 81 |         throw new \moodle_exception('maxsectionslimit', 'moodle', '', $maxsections);
 | 
        
           |  |  | 82 |     }
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |     list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $add, $section);
 | 
        
           |  |  | 85 |     $data->return = 0;
 | 
        
           |  |  | 86 |     if (!is_null($sectionreturn)) {
 | 
        
           |  |  | 87 |         $data->sr = $sectionreturn;
 | 
        
           |  |  | 88 |     }
 | 
        
           |  |  | 89 |     $data->add = $add;
 | 
        
           |  |  | 90 |     $data->beforemod = $beforemod;
 | 
        
           |  |  | 91 |     if (!empty($type)) { //TODO: hopefully will be removed in 2.0
 | 
        
           |  |  | 92 |         $data->type = $type;
 | 
        
           |  |  | 93 |     }
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |     $sectionname = get_section_name($course, $cw);
 | 
        
           |  |  | 96 |     $fullmodulename = get_string('modulename', $module->name);
 | 
        
           |  |  | 97 |     $pageheading = $pagetitle = get_string('addinganew', 'moodle', $fullmodulename);
 | 
        
           |  |  | 98 |     $navbaraddition = $pageheading;
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | } else if (!empty($update)) {
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |     $url->param('update', $update);
 | 
        
           |  |  | 103 |     $PAGE->set_url($url);
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     // Select the "Edit settings" from navigation.
 | 
        
           |  |  | 106 |     navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 |     // Check the course module exists.
 | 
        
           |  |  | 109 |     $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |     // Check the course exists.
 | 
        
           |  |  | 112 |     $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 |     // require_login
 | 
        
           |  |  | 115 |     require_login($course, false, $cm); // needed to setup proper $COURSE
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |     list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
 | 
        
           |  |  | 118 |     $data->return = $return;
 | 
        
           |  |  | 119 |     if (!is_null($sectionreturn)) {
 | 
        
           |  |  | 120 |         $data->sr = $sectionreturn;
 | 
        
           |  |  | 121 |     }
 | 
        
           |  |  | 122 |     $data->update = $update;
 | 
        
           |  |  | 123 |     if (!empty($showonly)) {
 | 
        
           |  |  | 124 |         $data->showonly = $showonly;
 | 
        
           |  |  | 125 |     }
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |     $sectionname = get_section_name($course, $cw);
 | 
        
           |  |  | 128 |     $fullmodulename = get_string('modulename', $module->name);
 | 
        
           |  |  | 129 |     $pageheading = get_string('editsettings', 'moodle');
 | 
        
           |  |  | 130 |     $pagetitle = get_string('edita', 'moodle', $fullmodulename) . ': ' . $cm->name;
 | 
        
           |  |  | 131 |     $navbaraddition = null;
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 | } else {
 | 
        
           |  |  | 134 |     require_login();
 | 
        
           |  |  | 135 |     throw new \moodle_exception('invalidaction');
 | 
        
           |  |  | 136 | }
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 | $pagepath = 'mod-' . $module->name . '-';
 | 
        
           |  |  | 139 | if (!empty($type)) { //TODO: hopefully will be removed in 2.0
 | 
        
           |  |  | 140 |     $pagepath .= $type;
 | 
        
           |  |  | 141 | } else {
 | 
        
           |  |  | 142 |     $pagepath .= 'mod';
 | 
        
           |  |  | 143 | }
 | 
        
           |  |  | 144 | $PAGE->set_pagetype($pagepath);
 | 
        
           |  |  | 145 | $PAGE->set_pagelayout('admin');
 | 
        
           |  |  | 146 | $PAGE->add_body_class('limitedwidth');
 | 
        
           |  |  | 147 |   | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 | $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
 | 
        
           |  |  | 150 | if (file_exists($modmoodleform)) {
 | 
        
           |  |  | 151 |     require_once($modmoodleform);
 | 
        
           |  |  | 152 | } else {
 | 
        
           |  |  | 153 |     throw new \moodle_exception('noformdesc');
 | 
        
           |  |  | 154 | }
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 | $mformclassname = 'mod_'.$module->name.'_mod_form';
 | 
        
           |  |  | 157 | $mform = new $mformclassname($data, $cw->section, $cm, $course);
 | 
        
           |  |  | 158 | $mform->set_data($data);
 | 
        
           |  |  | 159 | if (!empty($showonly)) {
 | 
        
           |  |  | 160 |     $mform->filter_shown_headers(explode(',', $showonly));
 | 
        
           |  |  | 161 | }
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 | if ($mform->is_cancelled()) {
 | 
        
           |  |  | 164 |     if ($return && !empty($cm->id)) {
 | 
        
           |  |  | 165 |         $urlparams = [
 | 
        
           |  |  | 166 |             'id' => $cm->id, // We always need the activity id.
 | 
        
           |  |  | 167 |             'forceview' => 1, // Stop file downloads in resources.
 | 
        
           |  |  | 168 |         ];
 | 
        
           |  |  | 169 |         $activityurl = new moodle_url("/mod/$module->name/view.php", $urlparams);
 | 
        
           |  |  | 170 |         redirect($activityurl);
 | 
        
           | 1441 | ariadna | 171 |     } else if (plugin_supports('mod', $module->name, FEATURE_PUBLISHES_QUESTIONS)) {
 | 
        
           |  |  | 172 |         redirect(\core_question\local\bank\question_bank_helper::get_url_for_qbank_list($course->id));
 | 
        
           | 1 | efrain | 173 |     } else {
 | 
        
           |  |  | 174 |         $options = [];
 | 
        
           |  |  | 175 |         if (!is_null($sectionreturn)) {
 | 
        
           |  |  | 176 |             $options['sr'] = $sectionreturn;
 | 
        
           |  |  | 177 |         }
 | 
        
           |  |  | 178 |         redirect(course_get_url($course, $cw->section, $options));
 | 
        
           |  |  | 179 |     }
 | 
        
           |  |  | 180 | } else if ($fromform = $mform->get_data()) {
 | 
        
           |  |  | 181 |     // Mark that this is happening in the front-end UI. This is used to indicate that we are able to
 | 
        
           |  |  | 182 |     // do regrading with a progress bar and redirect, if necessary.
 | 
        
           |  |  | 183 |     $fromform->frontend = true;
 | 
        
           |  |  | 184 |     if (!empty($fromform->update)) {
 | 
        
           |  |  | 185 |         list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
 | 
        
           |  |  | 186 |     } else if (!empty($fromform->add)) {
 | 
        
           |  |  | 187 |         $fromform = add_moduleinfo($fromform, $course, $mform);
 | 
        
           |  |  | 188 |     } else {
 | 
        
           |  |  | 189 |         throw new \moodle_exception('invaliddata');
 | 
        
           |  |  | 190 |     }
 | 
        
           |  |  | 191 |   | 
        
           |  |  | 192 |     if (isset($fromform->submitbutton)) {
 | 
        
           |  |  | 193 |         $url = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule, 'forceview' => 1));
 | 
        
           |  |  | 194 |         if (!empty($fromform->showgradingmanagement)) {
 | 
        
           |  |  | 195 |             $url = $fromform->gradingman->get_management_url($url);
 | 
        
           |  |  | 196 |         }
 | 
        
           | 1441 | ariadna | 197 |     } else if (plugin_supports('mod', $fromform->modulename, FEATURE_PUBLISHES_QUESTIONS)) {
 | 
        
           |  |  | 198 |         $url = \core_question\local\bank\question_bank_helper::get_url_for_qbank_list($course->id);
 | 
        
           | 1 | efrain | 199 |     } else {
 | 
        
           |  |  | 200 |         $options = [];
 | 
        
           |  |  | 201 |         if (!is_null($sectionreturn)) {
 | 
        
           |  |  | 202 |             $options['sr'] = $sectionreturn;
 | 
        
           |  |  | 203 |         }
 | 
        
           |  |  | 204 |         $url = course_get_url($course, $cw->section, $options);
 | 
        
           |  |  | 205 |     }
 | 
        
           |  |  | 206 |   | 
        
           |  |  | 207 |     redirect($url);
 | 
        
           |  |  | 208 |     exit;
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 | } else {
 | 
        
           |  |  | 211 |     if (!empty($cm->id)) {
 | 
        
           |  |  | 212 |         $context = context_module::instance($cm->id);
 | 
        
           |  |  | 213 |     } else {
 | 
        
           |  |  | 214 |         $context = context_course::instance($course->id);
 | 
        
           |  |  | 215 |     }
 | 
        
           |  |  | 216 |   | 
        
           |  |  | 217 |     $PAGE->set_heading($course->fullname);
 | 
        
           |  |  | 218 |     if ($course->id !== $SITE->id) {
 | 
        
           |  |  | 219 |         $pagetitle = $pagetitle . moodle_page::TITLE_SEPARATOR . $course->shortname;
 | 
        
           |  |  | 220 |     }
 | 
        
           |  |  | 221 |     $PAGE->set_title($pagetitle);
 | 
        
           |  |  | 222 |     $PAGE->set_cacheable(false);
 | 
        
           |  |  | 223 |   | 
        
           |  |  | 224 |     if (isset($navbaraddition)) {
 | 
        
           |  |  | 225 |         $PAGE->navbar->add($navbaraddition);
 | 
        
           |  |  | 226 |     }
 | 
        
           |  |  | 227 |     $PAGE->activityheader->disable();
 | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 |     echo $OUTPUT->header();
 | 
        
           |  |  | 230 |     echo $OUTPUT->heading_with_help($pageheading, '', $module->name);
 | 
        
           |  |  | 231 |   | 
        
           |  |  | 232 |     $mform->display();
 | 
        
           |  |  | 233 |   | 
        
           |  |  | 234 |     echo $OUTPUT->footer();
 | 
        
           |  |  | 235 | }
 |