Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 36] [Versions 28 and 37]

       1  <?php
       2  // This file is part of Moodle - http://moodle.org/
       3  //
       4  // Moodle is free software: you can redistribute it and/or modify
       5  // it under the terms of the GNU General Public License as published by
       6  // the Free Software Foundation, either version 3 of the License, or
       7  // (at your option) any later version.
       8  //
       9  // Moodle is distributed in the hope that it will be useful,
      10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12  // GNU General Public License for more details.
      13  //
      14  // You should have received a copy of the GNU General Public License
      15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
      16  
      17  /**
      18   * Performs course category management ajax actions.
      19   *
      20   * Please note functions may throw exceptions, please ensure your JS handles them as well as the outcome objects.
      21   *
      22   * @package    core_course
      23   * @copyright  2013 Sam Hemelryk
      24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      25   */
      26  
      27  define('AJAX_SCRIPT', true);
      28  
      29  require_once('../../config.php');
      30  require_once($CFG->dirroot.'/lib/coursecatlib.php');
      31  require_once($CFG->dirroot.'/course/lib.php');
      32  
      33  $action = required_param('action', PARAM_ALPHA);
      34  require_sesskey(); // Gotta have the sesskey.
      35  require_login(); // Gotta be logged in (of course).
      36  $PAGE->set_context(context_system::instance());
      37  
      38  // Prepare an outcome object. We always use this.
      39  $outcome = new stdClass;
      40  $outcome->error = false;
      41  $outcome->outcome = false;
      42  
      43  echo $OUTPUT->header();
      44  
      45  switch ($action) {
      46      case 'movecourseup' :
      47          $courseid = required_param('courseid', PARAM_INT);
      48          $outcome->outcome = \core_course\management\helper::action_course_change_sortorder_up_one_by_record($courseid);
      49          break;
      50      case 'movecoursedown' :
      51          $courseid = required_param('courseid', PARAM_INT);
      52          $outcome->outcome = \core_course\management\helper::action_course_change_sortorder_down_one_by_record($courseid);
      53          break;
      54      case 'movecourseintocategory':
      55          $courseid = required_param('courseid', PARAM_INT);
      56          $categoryid = required_param('categoryid', PARAM_INT);
      57          $course = get_course($courseid);
      58          $oldcategory = coursecat::get($course->category);
      59          $category = coursecat::get($categoryid);
      60          $outcome->outcome = \core_course\management\helper::move_courses_into_category($category, $courseid);
      61          $perpage = (int)get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
      62          $totalcourses = $oldcategory->get_courses_count();
      63          $totalpages = ceil($totalcourses / $perpage);
      64          if ($totalpages == 0) {
      65              $str = get_string('nocoursesyet');
      66          } else if ($totalpages == 1) {
      67              $str = get_string('showingacourses', 'moodle', $totalcourses);
      68          } else {
      69              $a = new stdClass;
      70              $a->start = ($page * $perpage) + 1;
      71              $a->end = min((($page + 1) * $perpage), $totalcourses);
      72              $a->total = $totalcourses;
      73              $str = get_string('showingxofycourses', 'moodle', $a);
      74          }
      75          $outcome->totalcatcourses = $category->get_courses_count();
      76          $outcome->fromcatcoursecount = $totalcourses;
      77          $outcome->paginationtotals = $str;
      78          break;
      79      case 'movecourseafter' :
      80          $courseid = required_param('courseid', PARAM_INT);
      81          $moveaftercourseid = required_param('moveafter', PARAM_INT);
      82          $outcome->outcome = \core_course\management\helper::action_course_change_sortorder_after_course(
      83              $courseid, $moveaftercourseid);
      84          break;
      85      case 'hidecourse' :
      86          $courseid = required_param('courseid', PARAM_INT);
      87          $outcome->outcome = \core_course\management\helper::action_course_hide_by_record($courseid);
      88          break;
      89      case 'showcourse' :
      90          $courseid = required_param('courseid', PARAM_INT);
      91          $outcome->outcome = \core_course\management\helper::action_course_show_by_record($courseid);
      92          break;
      93      case 'movecategoryup' :
      94          $categoryid = required_param('categoryid', PARAM_INT);
      95          $outcome->outcome = \core_course\management\helper::action_category_change_sortorder_up_one_by_id($categoryid);
      96          break;
      97      case 'movecategorydown' :
      98          $categoryid = required_param('categoryid', PARAM_INT);
      99          $outcome->outcome = \core_course\management\helper::action_category_change_sortorder_down_one_by_id($categoryid);
     100          break;
     101      case 'hidecategory' :
     102          $categoryid = required_param('categoryid', PARAM_INT);
     103          $selectedcategoryid = optional_param('selectedcategory', null, PARAM_INT);
     104          $outcome->outcome = \core_course\management\helper::action_category_hide_by_id($categoryid);
     105          $outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
     106          $outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
     107          if ($selectedcategoryid !== null) {
     108              $outcome->coursevisibility = array_merge(
     109                  $outcome->coursevisibility,
     110                  \core_course\management\helper::get_category_courses_visibility($selectedcategoryid)
     111              );
     112          }
     113          break;
     114      case 'showcategory' :
     115          $categoryid = required_param('categoryid', PARAM_INT);
     116          $selectedcategoryid = optional_param('selectedcategory', null, PARAM_INT);
     117          $outcome->outcome = \core_course\management\helper::action_category_show_by_id($categoryid);
     118          $outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
     119          $outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
     120          if ($selectedcategoryid !== null) {
     121              $outcome->coursevisibility = array_merge(
     122                  $outcome->coursevisibility,
     123                  \core_course\management\helper::get_category_courses_visibility($selectedcategoryid)
     124              );
     125          }
     126          break;
     127      case 'expandcategory':
     128          $categoryid = required_param('categoryid', PARAM_INT);
     129          $coursecat = coursecat::get($categoryid);
     130          \core_course\management\helper::record_expanded_category($coursecat);
     131          $outcome->outcome = true;
     132          break;
     133      case 'collapsecategory':
     134          $categoryid = required_param('categoryid', PARAM_INT);
     135          $coursecat = coursecat::get($categoryid);
     136          \core_course\management\helper::record_expanded_category($coursecat, false);
     137          $outcome->outcome = true;
     138          break;
     139      case 'getsubcategorieshtml' :
     140          $categoryid = required_param('categoryid', PARAM_INT);
     141          /* @var core_course_management_renderer $renderer */
     142          $renderer = $PAGE->get_renderer('core_course', 'management');
     143          $outcome->html = html_writer::start_tag('ul',
     144              array('class' => 'ml', 'role' => 'group', 'id' => 'subcategoriesof'.$categoryid));
     145          $coursecat = coursecat::get($categoryid);
     146          foreach ($coursecat->get_children() as $subcat) {
     147              $outcome->html .= $renderer->category_listitem($subcat, array(), $subcat->get_children_count());
     148          }
     149          $outcome->html .= html_writer::end_tag('ul');
     150          $outcome->outcome = true;
     151          break;
     152  }
     153  
     154  echo json_encode($outcome);
     155  echo $OUTPUT->footer();
     156  // Thats all folks.
     157  // Don't ever even consider putting anything after this. It just wouldn't make sense.
     158  // But you already knew that, you smart developer you.
     159  exit;
    

    Search This Site: