Merge branch 'MDL-33554' of git://github.com/mouneyrac/moodle
authorSam Hemelryk <sam@moodle.com>
Wed, 13 Jun 2012 23:20:16 +0000 (11:20 +1200)
committerSam Hemelryk <sam@moodle.com>
Wed, 13 Jun 2012 23:20:16 +0000 (11:20 +1200)
1  2 
course/externallib.php

diff --combined course/externallib.php
@@@ -66,7 -66,7 +66,7 @@@ class core_course_external extends exte
       * @return array
       * @since Moodle 2.2
       */
 -    public static function get_course_contents($courseid, $options) {
 +    public static function get_course_contents($courseid, $options = array()) {
          global $CFG, $DB;
          require_once($CFG->dirroot . "/course/lib.php");
  
                  $sectionvalues = array();
                  $sectionvalues['id'] = $section->id;
                  $sectionvalues['name'] = get_section_name($course, $section);
 -                $summary = file_rewrite_pluginfile_urls($section->summary, 'webservice/pluginfile.php', $context->id, 'course', 'section', $section->id);
                  $sectionvalues['visible'] = $section->visible;
 -                $sectionvalues['summary'] = format_text($summary, $section->summaryformat);
 +                list($sectionvalues['summary'], $sectionvalues['summaryformat']) =
 +                        external_format_text($section->summary, $section->summaryformat,
 +                                $context->id, 'course', 'section', $section->id);
                  $sectioncontents = array();
  
                  //for each module of the section
                      'name' => new external_value(PARAM_TEXT, 'Section name'),
                      'visible' => new external_value(PARAM_INT, 'is the section visible', VALUE_OPTIONAL),
                      'summary' => new external_value(PARAM_RAW, 'Section description'),
 +                    'summaryformat' => new external_format_value('summary'),
                      'modules' => new external_multiple_structure(
                              new external_single_structure(
                                  array(
       * @return array
       * @since Moodle 2.2
       */
 -    public static function get_courses($options) {
 +    public static function get_courses($options = array()) {
          global $CFG, $DB;
          require_once($CFG->dirroot . "/course/lib.php");
  
              $courseinfo['fullname'] = $course->fullname;
              $courseinfo['shortname'] = $course->shortname;
              $courseinfo['categoryid'] = $course->category;
 -            $courseinfo['summary'] = $course->summary;
 -            $courseinfo['summaryformat'] = $course->summaryformat;
 +            list($courseinfo['summary'], $courseinfo['summaryformat']) =
 +                external_format_text($course->summary, $course->summaryformat, $context->id, 'course', 'summary', 0);
              $courseinfo['format'] = $course->format;
              $courseinfo['startdate'] = $course->startdate;
              $courseinfo['numsections'] = $course->numsections;
                              'fullname' => new external_value(PARAM_TEXT, 'full name'),
                              'idnumber' => new external_value(PARAM_RAW, 'id number', VALUE_OPTIONAL),
                              'summary' => new external_value(PARAM_RAW, 'summary'),
 -                            'summaryformat' => new external_value(PARAM_INT,
 -                                    'the summary text Moodle format'),
 +                            'summaryformat' => new external_format_value('summary'),
                              'format' => new external_value(PARAM_PLUGIN,
                                      'course format: weeks, topics, social, site,..'),
                              'showgrades' => new external_value(PARAM_INT,
                              'categoryid' => new external_value(PARAM_INT, 'category id'),
                              'idnumber' => new external_value(PARAM_RAW, 'id number', VALUE_OPTIONAL),
                              'summary' => new external_value(PARAM_RAW, 'summary', VALUE_OPTIONAL),
 -                            'summaryformat' => new external_value(PARAM_INT,
 -                                    'the summary text Moodle format', VALUE_DEFAULT, FORMAT_MOODLE),
 +                            'summaryformat' => new external_format_value('summary', VALUE_DEFAULT),
                              'format' => new external_value(PARAM_PLUGIN,
                                      'course format: weeks, topics, social, site,..',
                                      VALUE_DEFAULT, $courseconfig->format),
  
              $course['category'] = $course['categoryid'];
  
 +            // Summary format.
 +            $course['summaryformat'] = external_validate_format($course['summaryformat']);
 +
              //Note: create_course() core function check shortname, idnumber, category
              $course['id'] = create_course((object) $course)->id;
  
       * @return array New course info
       * @since Moodle 2.3
       */
 -    public static function duplicate_course($courseid, $fullname, $shortname, $categoryid, $visible, $options) {
 +    public static function duplicate_course($courseid, $fullname, $shortname, $categoryid, $visible = 1, $options = array()) {
          global $CFG, $USER, $DB;
          require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
          require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
                      $categoryinfo = array();
                      $categoryinfo['id'] = $category->id;
                      $categoryinfo['name'] = $category->name;
 -                    $categoryinfo['description'] = file_rewrite_pluginfile_urls($category->description,
 -                            'webservice/pluginfile.php', $context->id, 'coursecat', 'description', null);
 -                    $options = new stdClass;
 -                    $options->noclean = true;
 -                    $options->para = false;
 -                    $categoryinfo['description'] = format_text($categoryinfo['description'],
 -                            $category->descriptionformat, $options);
 +                    list($categoryinfo['description'], $categoryinfo['descriptionformat']) =
 +                        external_format_text($category->description, $category->descriptionformat,
 +                                $context->id, 'coursecat', 'description', null);
                      $categoryinfo['parent'] = $category->parent;
                      $categoryinfo['sortorder'] = $category->sortorder;
                      $categoryinfo['coursecount'] = $category->coursecount;
                      'name' => new external_value(PARAM_TEXT, 'category name'),
                      'idnumber' => new external_value(PARAM_RAW, 'category id number', VALUE_OPTIONAL),
                      'description' => new external_value(PARAM_RAW, 'category description'),
 +                    'descriptionformat' => new external_format_value('description'),
                      'parent' => new external_value(PARAM_INT, 'parent category id'),
                      'sortorder' => new external_value(PARAM_INT, 'category sorting order'),
                      'coursecount' => new external_value(PARAM_INT, 'number of courses in this category'),
                              array(
                                  'name' => new external_value(PARAM_TEXT, 'new category name'),
                                  'parent' => new external_value(PARAM_INT,
 -                                        'the parent category id inside which the new category will be created'),
 +                                        'the parent category id inside which the new category will be created
 +                                         - set to 0 for a root category',
 +                                        VALUE_DEFAULT, 0),
                                  'idnumber' => new external_value(PARAM_RAW,
                                          'the new category idnumber', VALUE_OPTIONAL),
                                  'description' => new external_value(PARAM_RAW,
                                          'the new category description', VALUE_OPTIONAL),
 +                                'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
                                  'theme' => new external_value(PARAM_THEME,
                                          'the new category theme. This option must be enabled on moodle',
                                          VALUE_OPTIONAL),
              self::validate_context($context);
              require_capability('moodle/category:manage', $context);
  
 -            // Check id number.
 -            if (!empty($category['idnumber'])) { // Same as in course/editcategory_form.php .
 -                if (textlib::strlen($category['idnumber'])>100) {
 -                    throw new moodle_exception('idnumbertoolong');
 -                }
 -                if ($existing = $DB->get_record('course_categories', array('idnumber' => $category['idnumber']))) {
 -                    if ($existing->id) {
 -                        throw new moodle_exception('idnumbertaken');
 -                    }
 -                }
 -            }
              // Check name.
              if (textlib::strlen($category['name'])>255) {
                  throw new moodle_exception('categorytoolong');
              $newcategory = new stdClass();
              $newcategory->name = $category['name'];
              $newcategory->parent = $category['parent'];
 -            $newcategory->idnumber = $category['idnumber'];
              $newcategory->sortorder = 999; // Same as in the course/editcategory.php .
              // Format the description.
              if (!empty($category['description'])) {
                  $newcategory->description = $category['description'];
              }
 -            $newcategory->descriptionformat = FORMAT_HTML;
 +            $newcategory->descriptionformat = external_validate_format($category['descriptionformat']);
              if (isset($category['theme']) and !empty($CFG->allowcategorythemes)) {
                  $newcategory->theme = $category['theme'];
              }
 +            // Check id number.
 +            if (!empty($category['idnumber'])) { // Same as in course/editcategory_form.php .
 +                if (textlib::strlen($category['idnumber'])>100) {
 +                    throw new moodle_exception('idnumbertoolong');
 +                }
 +                if ($existing = $DB->get_record('course_categories', array('idnumber' => $category['idnumber']))) {
 +                    if ($existing->id) {
 +                        throw new moodle_exception('idnumbertaken');
 +                    }
 +                }
 +                $newcategory->idnumber = $category['idnumber'];
 +            }
  
              $newcategory = create_course_category($newcategory);
              // Populate special fields.
                              'idnumber' => new external_value(PARAM_RAW, 'category id number', VALUE_OPTIONAL),
                              'parent' => new external_value(PARAM_INT, 'parent category id', VALUE_OPTIONAL),
                              'description' => new external_value(PARAM_RAW, 'category description', VALUE_OPTIONAL),
 +                            'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
                              'theme' => new external_value(PARAM_THEME,
                                      'the category theme. This option must be enabled on moodle', VALUE_OPTIONAL),
                          )
              }
              if (!empty($cat['description'])) {
                  $category->description = $cat['description'];
 -                $category->descriptionformat = FORMAT_HTML;
 +                $category->descriptionformat = external_validate_format($cat['descriptionformat']);
              }
              if (!empty($cat['theme'])) {
                  $category->theme = $cat['theme'];
                  // Finally move the category.
                  move_category($category, $parent_cat);
                  $category->parent = $cat['parent'];
+                 // Get updated path by move_category().
+                 $category->path = $DB->get_field('course_categories', 'path',
+                         array('id' => $category->id));
              }
              $DB->update_record('course_categories', $category);
          }