Exemplo n.º 1
0
 /**
  * Create the fixture
  *
  * This method must be safe to call multiple times.
  *
  * @return void
  * @throws moodle_exception
  */
 public function build()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/coursecatlib.php';
     if (!$this->exists()) {
         $course = (object) $this->get_options();
         // Clean course table - can happen when unit tests fail...
         if (!empty($course->shortname) and $record = $DB->get_record('course', array('shortname' => $course->shortname))) {
             delete_course($record, false);
         }
         if (!empty($course->idnumber) and $record = $DB->get_record('course', array('idnumber' => $course->idnumber))) {
             delete_course($record, false);
         }
         // Try to help folks out...
         if (!property_exists($course, 'category')) {
             $course->category = coursecat::get_default()->id;
         }
         if (!property_exists($course, 'fullname')) {
             $course->fullname = '';
         }
         $course = create_course($course);
         $this->set_results($DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST));
     }
 }
Exemplo n.º 2
0
 /**
  * Returns the category where this course request should be created
  *
  * Note that we don't check here that user has a capability to view
  * hidden categories if he has capabilities 'moodle/site:approvecourse' and
  * 'moodle/course:changecategory'
  *
  * @return coursecat
  */
 public function get_category()
 {
     global $CFG;
     require_once $CFG->libdir . '/coursecatlib.php';
     // If the category is not set, if the current user does not have the rights to change the category, or if the
     // category does not exist, we set the default category to the course to be approved.
     // The system level is used because the capability moodle/site:approvecourse is based on a system level.
     if (empty($this->properties->category) || !has_capability('moodle/course:changecategory', context_system::instance()) || !($category = coursecat::get($this->properties->category, IGNORE_MISSING, true))) {
         $category = coursecat::get($CFG->defaultrequestcategory, IGNORE_MISSING, true);
     }
     if (!$category) {
         $category = coursecat::get_default();
     }
     return $category;
 }
 /**
  * Add new course without a category.
  */
 public function test_course_add_default_category()
 {
     global $DB, $CFG;
     require_once $CFG->libdir . '/coursecatlib.php';
     $this->imsplugin->set_config('createnewcategories', false);
     // Delete the default category, to ensure the plugin handles this gracefully.
     $defaultcat = coursecat::get_default();
     $defaultcat->delete_full(false);
     // Create an course with the IMS plugin without a category.
     $course1 = new stdClass();
     $course1->idnumber = 'id1';
     $course1->imsshort = 'id1';
     $course1->category = '';
     $this->set_xml_file(false, array($course1));
     $this->imsplugin->cron();
     // Check the course has been created.
     $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber), '*', MUST_EXIST);
     // Check that it belongs to a category which exists.
     $this->assertTrue($DB->record_exists('course_categories', array('id' => $dbcourse->category)));
 }
Exemplo n.º 4
0
 /**
  * Renders HTML to display particular course category - list of it's subcategories and courses
  *
  * Invoked from /course/index.php
  *
  * @param int|stdClass|coursecat $category
  */
 public function course_category($category)
 {
     global $CFG;
     require_once $CFG->libdir . '/coursecatlib.php';
     $coursecat = coursecat::get(is_object($category) ? $category->id : $category);
     $site = get_site();
     $output = '';
     if (can_edit_in_category($coursecat->id)) {
         // Add 'Manage' button if user has permissions to edit this category.
         $managebutton = $this->single_button(new moodle_url('/course/management.php', array('categoryid' => $coursecat->id)), get_string('managecourses'), 'get');
         $this->page->set_button($managebutton);
     }
     if (!$coursecat->id) {
         if (coursecat::count_all() == 1) {
             // There exists only one category in the system, do not display link to it
             $coursecat = coursecat::get_default();
             $strfulllistofcourses = get_string('fulllistofcourses');
             $this->page->set_title("{$site->shortname}: {$strfulllistofcourses}");
         } else {
             $strcategories = get_string('categories');
             $this->page->set_title("{$site->shortname}: {$strcategories}");
         }
     } else {
         $this->page->set_title("{$site->shortname}: " . $coursecat->get_formatted_name());
         // Print the category selector
         $output .= html_writer::start_tag('div', array('class' => 'categorypicker'));
         $select = new single_select(new moodle_url('/course/index.php'), 'categoryid', coursecat::make_categories_list(), $coursecat->id, null, 'switchcategory');
         $select->set_label(get_string('categories') . ':');
         $output .= $this->render($select);
         $output .= html_writer::end_tag('div');
         // .categorypicker
     }
     // Print current category description
     $chelper = new coursecat_helper();
     if ($description = $chelper->get_category_formatted_description($coursecat)) {
         $output .= $this->box($description, array('class' => 'generalbox info'));
     }
     // Prepare parameters for courses and categories lists in the tree
     $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)->set_attributes(array('class' => 'category-browse category-browse-' . $coursecat->id));
     $coursedisplayoptions = array();
     $catdisplayoptions = array();
     $browse = optional_param('browse', null, PARAM_ALPHA);
     $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
     $page = optional_param('page', 0, PARAM_INT);
     $baseurl = new moodle_url('/course/index.php');
     if ($coursecat->id) {
         $baseurl->param('categoryid', $coursecat->id);
     }
     if ($perpage != $CFG->coursesperpage) {
         $baseurl->param('perpage', $perpage);
     }
     $coursedisplayoptions['limit'] = $perpage;
     $catdisplayoptions['limit'] = $perpage;
     if ($browse === 'courses' || !$coursecat->has_children()) {
         $coursedisplayoptions['offset'] = $page * $perpage;
         $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
         $catdisplayoptions['nodisplay'] = true;
         $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
         $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories');
     } else {
         if ($browse === 'categories' || !$coursecat->has_courses()) {
             $coursedisplayoptions['nodisplay'] = true;
             $catdisplayoptions['offset'] = $page * $perpage;
             $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
             $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
             $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
         } else {
             // we have a category that has both subcategories and courses, display pagination separately
             $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
             $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
         }
     }
     $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
     // Add course search form.
     $output .= $this->course_search_form();
     // Display course category tree.
     $output .= $this->coursecat_tree($chelper, $coursecat);
     // Add action buttons
     $output .= $this->container_start('buttons');
     $context = get_category_or_system_context($coursecat->id);
     if (has_capability('moodle/course:create', $context)) {
         // Print link to create a new course, for the 1st available category.
         if ($coursecat->id) {
             $url = new moodle_url('/course/edit.php', array('category' => $coursecat->id, 'returnto' => 'category'));
         } else {
             $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
         }
         $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
     }
     ob_start();
     if (coursecat::count_all() == 1) {
         print_course_request_buttons(context_system::instance());
     } else {
         print_course_request_buttons($context);
     }
     $output .= ob_get_contents();
     ob_end_clean();
     $output .= $this->container_end();
     return $output;
 }
Exemplo n.º 5
0
    /**
     * Get the default category id (often known as 'Miscellaneous'),
     * statically cached to avoid multiple DB lookups on big imports.
     *
     * @return int id of default category.
     */
    private function get_default_category_id() {
        global $CFG;
        require_once($CFG->libdir.'/coursecatlib.php');

        static $defaultcategoryid = null;

        if ($defaultcategoryid === null) {
            $category = coursecat::get_default();
            $defaultcategoryid = $category->id;
        }

        return $defaultcategoryid;
    }
Exemplo n.º 6
0
require_once "../config.php";
require_once $CFG->dirroot . '/course/lib.php';
require_once $CFG->libdir . '/coursecatlib.php';
$categoryid = optional_param('categoryid', 0, PARAM_INT);
// Category id
$site = get_site();
if ($categoryid) {
    $PAGE->set_category_by_id($categoryid);
    $PAGE->set_url(new moodle_url('/course/index.php', array('categoryid' => $categoryid)));
    $PAGE->set_pagetype('course-index-category');
    // And the object has been loaded for us no need for another DB call
    $category = $PAGE->category;
} else {
    // Check if there is only one category, if so use that.
    if (coursecat::count_all() == 1) {
        $category = coursecat::get_default();
        $categoryid = $category->id;
        $PAGE->set_category_by_id($categoryid);
        $PAGE->set_pagetype('course-index-category');
    } else {
        $PAGE->set_context(context_system::instance());
    }
    $PAGE->set_url('/course/index.php');
}
$PAGE->set_pagelayout('coursecategory');
$courserenderer = $PAGE->get_renderer('core', 'course');
if ($CFG->forcelogin) {
    require_login();
}
if ($categoryid && !$category->visible && !has_capability('moodle/category:viewhiddencategories', $PAGE->context)) {
    throw new moodle_exception('unknowncategory');
Exemplo n.º 7
0
 /**
  * Get the default category id (often known as 'Miscellaneous'),
  * statically cached to avoid multiple DB lookups on big imports.
  *
  * @return int id of default category.
  */
 private function get_default_category_id()
 {
     global $CFG;
     require_once $CFG->libdir . '/coursecatlib.php';
     if ($this->defaultcategoryid === null) {
         $category = coursecat::get_default();
         $this->defaultcategoryid = $category->id;
     }
     return $this->defaultcategoryid;
 }
Exemplo n.º 8
0
 /**
  * Test that category with idnumber is not created if name is missing (nested categories not activated).
  */
 public function test_category_idnumber_missing_name_no_nested()
 {
     global $DB;
     $this->imsplugin->set_config('categoryidnumber', true);
     $this->imsplugin->set_config('categoryseparator', '|');
     $catsep = trim($this->imsplugin->get_config('categoryseparator'));
     $catidnumber = '01';
     $course = new StdClass();
     $course->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
     $course->idnumber = 'id1';
     $course->imsshort = 'description_short';
     $course->imslong = 'description_long';
     $course->imsfull = 'description_full';
     $course->category[] = '' . $catsep . $catidnumber;
     $this->set_xml_file(false, array($course));
     $this->imsplugin->cron();
     // Check category was not created.
     $catid = $DB->get_record('course_categories', array('idnumber' => $catidnumber));
     $this->assertFalse($catid);
     // Check course was put in default category.
     $defaultcat = coursecat::get_default();
     $dbcourse = $DB->get_record('course', array('idnumber' => $course->idnumber), '*', MUST_EXIST);
     $this->assertEquals($dbcourse->category, $defaultcat->id);
 }
Exemplo n.º 9
0
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * CLI Bulk course registration script from a comma separated file.
 *
 * @package    tool_uploadcourse
 * @copyright  2012 Piers Harding
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('CLI_SCRIPT', true);
require __DIR__ . '/../../../../config.php';
require_once $CFG->libdir . '/clilib.php';
require_once $CFG->libdir . '/coursecatlib.php';
require_once $CFG->libdir . '/csvlib.class.php';
$courseconfig = get_config('moodlecourse');
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array('help' => false, 'mode' => '', 'updatemode' => 'nothing', 'file' => '', 'delimiter' => 'comma', 'encoding' => 'UTF-8', 'shortnametemplate' => '', 'templatecourse' => false, 'restorefile' => false, 'allowdeletes' => false, 'allowrenames' => false, 'allowresets' => false, 'reset' => false, 'category' => coursecat::get_default()->id), array('h' => 'help', 'm' => 'mode', 'u' => 'updatemode', 'f' => 'file', 'd' => 'delimiter', 'e' => 'encoding', 't' => 'templatecourse', 'r' => 'restorefile', 'g' => 'format'));
if ($unrecognized) {
    $unrecognized = implode("\n  ", $unrecognized);
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
$help = "Execute Course Upload.\n\nOptions:\n-h, --help                 Print out this help\n-m, --mode                 Import mode: createnew, createall, createorupdate, update\n-u, --updatemode           Update mode: nothing, dataonly, dataordefaults¸ missingonly\n-f, --file                 CSV file\n-d, --delimiter            CSV delimiter: colon, semicolon, tab, cfg, comma\n-e, --encoding             CSV file encoding: utf8, ... etc\n-t, --templatecourse       Shortname of the course to restore after import\n-r, --restorefile          Backup file to restore after import\n--reset                    Run the course reset after each course import\n--allowdeletes             Allow courses to be deleted\n--allowrenames             Allow courses to be renamed\n--allowresets              Allow courses to be reset\n--shortnametemplate        Template to generate the shortname from\n--category                 ID of default category (--updatemode dataordefaults will use this value)\n\n\nExample:\n\$sudo -u www-data /usr/bin/php admin/tool/uploadcourse/cli/uploadcourse.php --mode=createnew \\\n       --updatemode=dataonly --file=./courses.csv --delimiter=comma\n";
if ($options['help']) {
    echo $help;
    die;
}
echo "Moodle course uploader running ...\n";
$processoroptions = array('allowdeletes' => $options['allowdeletes'], 'allowrenames' => $options['allowrenames'], 'allowresets' => $options['allowresets'], 'reset' => $options['reset'], 'shortnametemplate' => $options['shortnametemplate']);
// Confirm that the mode is valid.
$modes = array('createnew' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'createall' => tool_uploadcourse_processor::MODE_CREATE_ALL, 'createorupdate' => tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE, 'update' => tool_uploadcourse_processor::MODE_UPDATE_ONLY);
if (!isset($options['mode']) || !isset($modes[$options['mode']])) {
    echo get_string('invalidmode', 'tool_uploadcourse') . "\n";