Search moodle.org's
Developer Documentation


/course/ -> modedit.php (source)
   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.'/conditionlib.php');
  32  require_once($CFG->libdir.'/plagiarismlib.php');
  33  require_once($CFG->dirroot . '/course/modlib.php');
  34  
  35  $add    = optional_param('add', '', PARAM_ALPHA);     // module name
  36  $update = optional_param('update', 0, PARAM_INT);
  37  $return = optional_param('return', 0, PARAM_BOOL);    //return to course/view.php if false or mod/modname/view.php if true
  38  $type   = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
  39  $sectionreturn = optional_param('sr', null, PARAM_INT);
  40  
  41  $url = new moodle_url('/course/modedit.php');
  42  $url->param('sr', $sectionreturn);
  43  if (!empty($return)) {
  44      $url->param('return', $return);
  45  }
  46  
  47  if (!empty($add)) {
  48      $section = required_param('section', PARAM_INT);
  49      $course  = required_param('course', PARAM_INT);
  50  
  51      $url->param('add', $add);
  52      $url->param('section', $section);
  53      $url->param('course', $course);
  54      $PAGE->set_url($url);
  55  
  56      $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
  57      require_login($course);
  58  
  59      // There is no page for this in the navigation. The closest we'll have is the course section.
  60      // If the course section isn't displayed on the navigation this will fall back to the course which
  61      // will be the closest match we have.
  62      navigation_node::override_active_url(course_get_url($course, $section));
  63  
  64      list($module, $context, $cw) = can_add_moduleinfo($course, $add, $section);
  65  
  66      $cm = null;
  67  
  68      $data = new stdClass();
  69      $data->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
  70      $data->visible          = $cw->visible;
  71      $data->course           = $course->id;
  72      $data->module           = $module->id;
  73      $data->modulename       = $module->name;
  74      $data->groupmode        = $course->groupmode;
  75      $data->groupingid       = $course->defaultgroupingid;
  76      $data->id               = '';
  77      $data->instance         = '';
  78      $data->coursemodule     = '';
  79      $data->add              = $add;
  80      $data->return           = 0; //must be false if this is an add, go back to course view on cancel
  81      $data->sr               = $sectionreturn;
  82  
  83      if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
  84          $draftid_editor = file_get_submitted_draft_itemid('introeditor');
  85          file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
  86          $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
  87      }
  88  
  89      if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
  90              and has_capability('moodle/grade:managegradingforms', $context)) {
  91          require_once($CFG->dirroot.'/grade/grading/lib.php');
  92  
  93          $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
  94          $areas = grading_manager::available_areas('mod_'.$module->name);
  95  
  96          foreach ($areas as $areaname => $areatitle) {
  97              $data->_advancedgradingdata['areas'][$areaname] = array(
  98                  'title'  => $areatitle,
  99                  'method' => '',
 100              );
 101              $formfield = 'advancedgradingmethod_'.$areaname;
 102              $data->{$formfield} = '';
 103          }
 104      }
 105  
 106      if (!empty($type)) { //TODO: hopefully will be removed in 2.0
 107          $data->type = $type;
 108      }
 109  
 110      $sectionname = get_section_name($course, $cw);
 111      $fullmodulename = get_string('modulename', $module->name);
 112  
 113      if ($data->section && $course->format != 'site') {
 114          $heading = new stdClass();
 115          $heading->what = $fullmodulename;
 116          $heading->to   = $sectionname;
 117          $pageheading = get_string('addinganewto', 'moodle', $heading);
 118      } else {
 119          $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
 120      }
 121      $navbaraddition = $pageheading;
 122  
 123  } else if (!empty($update)) {
 124  
 125      $url->param('update', $update);
 126      $PAGE->set_url($url);
 127  
 128      // Select the "Edit settings" from navigation.
 129      navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
 130  
 131      // Check the course module exists.
 132      $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
 133  
 134      // Check the course exists.
 135      $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 136  
 137      // require_login
 138      require_login($course, false, $cm); // needed to setup proper $COURSE
 139  
 140      list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
 141  
 142      $data->coursemodule       = $cm->id;
 143      $data->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
 144      $data->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
 145      $data->cmidnumber         = $cm->idnumber;          // The cm IDnumber
 146      $data->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
 147      $data->groupingid         = $cm->groupingid;
 148      $data->course             = $course->id;
 149      $data->module             = $module->id;
 150      $data->modulename         = $module->name;
 151      $data->instance           = $cm->instance;
 152      $data->return             = $return;
 153      $data->sr                 = $sectionreturn;
 154      $data->update             = $update;
 155      $data->completion         = $cm->completion;
 156      $data->completionview     = $cm->completionview;
 157      $data->completionexpected = $cm->completionexpected;
 158      $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
 159      $data->showdescription    = $cm->showdescription;
 160      if (!empty($CFG->enableavailability)) {
 161          $data->availabilityconditionsjson = $cm->availability;
 162      }
 163  
 164      if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
 165          $draftid_editor = file_get_submitted_draft_itemid('introeditor');
 166          $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
 167          $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
 168      }
 169  
 170      if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
 171              and has_capability('moodle/grade:managegradingforms', $context)) {
 172          require_once($CFG->dirroot.'/grade/grading/lib.php');
 173          $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
 174          $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
 175          $areas = $gradingman->get_available_areas();
 176  
 177          foreach ($areas as $areaname => $areatitle) {
 178              $gradingman->set_area($areaname);
 179              $method = $gradingman->get_active_method();
 180              $data->_advancedgradingdata['areas'][$areaname] = array(
 181                  'title'  => $areatitle,
 182                  'method' => $method,
 183              );
 184              $formfield = 'advancedgradingmethod_'.$areaname;
 185              $data->{$formfield} = $method;
 186          }
 187      }
 188  
 189      if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
 190                                               'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
 191          // add existing outcomes
 192          foreach ($items as $item) {
 193              if (!empty($item->outcomeid)) {
 194                  $data->{'outcome_'.$item->outcomeid} = 1;
 195              }
 196          }
 197  
 198          // set category if present
 199          $gradecat = false;
 200          foreach ($items as $item) {
 201              if ($gradecat === false) {
 202                  $gradecat = $item->categoryid;
 203                  continue;
 204              }
 205              if ($gradecat != $item->categoryid) {
 206                  //mixed categories
 207                  $gradecat = false;
 208                  break;
 209              }
 210          }
 211          if ($gradecat !== false) {
 212              // do not set if mixed categories present
 213              $data->gradecat = $gradecat;
 214          }
 215      }
 216  
 217      $sectionname = get_section_name($course, $cw);
 218      $fullmodulename = get_string('modulename', $module->name);
 219  
 220      if ($data->section && $course->format != 'site') {
 221          $heading = new stdClass();
 222          $heading->what = $fullmodulename;
 223          $heading->in   = $sectionname;
 224          $pageheading = get_string('updatingain', 'moodle', $heading);
 225      } else {
 226          $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
 227      }
 228      $navbaraddition = null;
 229  
 230  } else {
 231      require_login();
 232      print_error('invalidaction');
 233  }
 234  
 235  $pagepath = 'mod-' . $module->name . '-';
 236  if (!empty($type)) { //TODO: hopefully will be removed in 2.0
 237      $pagepath .= $type;
 238  } else {
 239      $pagepath .= 'mod';
 240  }
 241  $PAGE->set_pagetype($pagepath);
 242  $PAGE->set_pagelayout('admin');
 243  
 244  $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
 245  if (file_exists($modmoodleform)) {
 246      require_once($modmoodleform);
 247  } else {
 248      print_error('noformdesc');
 249  }
 250  
 251  $mformclassname = 'mod_'.$module->name.'_mod_form';
 252  $mform = new $mformclassname($data, $cw->section, $cm, $course);
 253  $mform->set_data($data);
 254  
 255  if ($mform->is_cancelled()) {
 256      if ($return && !empty($cm->id)) {
 257          redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
 258      } else {
 259          redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
 260      }
 261  } else if ($fromform = $mform->get_data()) {
 262  
 263      if (!empty($fromform->update)) {
 264          list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
 265      } else if (!empty($fromform->add)) {
 266          $fromform = add_moduleinfo($fromform, $course, $mform);
 267      } else {
 268          print_error('invaliddata');
 269      }
 270  
 271      if (isset($fromform->submitbutton)) {
 272          if (empty($fromform->showgradingmanagement)) {
 273              redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
 274          } else {
 275              $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule));
 276              redirect($fromform->gradingman->get_management_url($returnurl));
 277          }
 278      } else {
 279          redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
 280      }
 281      exit;
 282  
 283  } else {
 284  
 285      $streditinga = get_string('editinga', 'moodle', $fullmodulename);
 286      $strmodulenameplural = get_string('modulenameplural', $module->name);
 287  
 288      if (!empty($cm->id)) {
 289          $context = context_module::instance($cm->id);
 290      } else {
 291          $context = context_course::instance($course->id);
 292      }
 293  
 294      $PAGE->set_heading($course->fullname);
 295      $PAGE->set_title($streditinga);
 296      $PAGE->set_cacheable(false);
 297  
 298      if (isset($navbaraddition)) {
 299          $PAGE->navbar->add($navbaraddition);
 300      }
 301  
 302      echo $OUTPUT->header();
 303  
 304      if (get_string_manager()->string_exists('modulename_help', $module->name)) {
 305          echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
 306      } else {
 307          echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
 308      }
 309  
 310      $mform->display();
 311  
 312      echo $OUTPUT->footer();
 313  }

Search This Site: