Merge branch 'wip-MDL-44287-master' of git://github.com/marinaglancy/moodle
authorDan Poltawski <dan@moodle.com>
Wed, 5 Mar 2014 06:18:21 +0000 (14:18 +0800)
committerDan Poltawski <dan@moodle.com>
Wed, 5 Mar 2014 06:18:21 +0000 (14:18 +0800)
1  2 
course/modlib.php

diff --combined course/modlib.php
@@@ -85,9 -85,6 +85,9 @@@ function add_moduleinfo($moduleinfo, $c
          $newcm->showdescription = 0;
      }
  
 +    // From this point we make database changes, so start transaction.
 +    $transaction = $DB->start_delegated_transaction();
 +
      if (!$moduleinfo->coursemodule = add_course_module($newcm)) {
          print_error('cannotaddcoursemodule');
      }
      }
  
      $addinstancefunction    = $moduleinfo->modulename."_add_instance";
 -    $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
 +    try {
 +        $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
 +    } catch (moodle_exception $e) {
 +        $returnfromfunc = $e;
 +    }
      if (!$returnfromfunc or !is_number($returnfromfunc)) {
 -        // Undo everything we can.
 +        // Undo everything we can. This is not necessary for databases which
 +        // support transactions, but improves consistency for other databases.
          $modcontext = context_module::instance($moduleinfo->coursemodule);
          context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
          $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
  
 -        if (!is_number($returnfromfunc)) {
 +        if ($e instanceof moodle_exception) {
 +            throw $e;
 +        } else if (!is_number($returnfromfunc)) {
              print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
          } else {
              print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
                 "$moduleinfo->instance", $moduleinfo->coursemodule);
  
      $moduleinfo = edit_module_post_actions($moduleinfo, $course);
 +    $transaction->allow_commit();
  
      return $moduleinfo;
  }
@@@ -354,6 -343,9 +354,9 @@@ function set_moduleinfo_defaults($modul
      if (!isset($moduleinfo->completionview)) {
          $moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED;
      }
+     if (!isset($moduleinfo->completionexpected)) {
+         $moduleinfo->completionexpected = 0;
+     }
  
      // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
      if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) {
          $moduleinfo->completiongradeitemnumber = null;
      }
  
+     if (!isset($moduleinfo->conditiongradegroup)) {
+         $moduleinfo->conditiongradegroup = array();
+     }
+     if (!isset($moduleinfo->conditionfieldgroup)) {
+         $moduleinfo->conditionfieldgroup = array();
+     }
      return $moduleinfo;
  }