function create_category_pditt($nama,$deskripsi=''){ global $DB, $CFG; $x = $DB->get_record('course_categories', array('name' => 'PDITT-' . $nama), '*'); if (!$x){ $data = new stdClass(); $data->name='PDITT-' . $nama; $data->description=$deskripsi; $data->descriptionformat=0; $data->parent=0; $category = coursecat::create($data); return $category->id; } else { return $x->id; } }
/** * Create categories * * @param array $categories - see create_categories_parameters() for the array structure * @return array - see create_categories_returns() for the array structure * @since Moodle 2.3 */ public static function create_categories($categories) { global $CFG, $DB; require_once($CFG->libdir . "/coursecatlib.php"); $params = self::validate_parameters(self::create_categories_parameters(), array('categories' => $categories)); $transaction = $DB->start_delegated_transaction(); $createdcategories = array(); foreach ($params['categories'] as $category) { if ($category['parent']) { if (!$DB->record_exists('course_categories', array('id' => $category['parent']))) { throw new moodle_exception('unknowcategory'); } $context = context_coursecat::instance($category['parent']); } else { $context = context_system::instance(); } self::validate_context($context); require_capability('moodle/category:manage', $context); // this will validate format and throw an exception if there are errors external_validate_format($category['descriptionformat']); $newcategory = coursecat::create($category); $createdcategories[] = array('id' => $newcategory->id, 'name' => $newcategory->name); } $transaction->allow_commit(); return $createdcategories; }
/** * Create a test course category * @param array|stdClass $record * @param array $options * @return coursecat course category record */ public function create_category($record = null, array $options = null) { global $DB, $CFG; require_once "{$CFG->libdir}/coursecatlib.php"; $this->categorycount++; $i = $this->categorycount; $record = (array) $record; if (!isset($record['name'])) { $record['name'] = 'Course category ' . $i; } if (!isset($record['description'])) { $record['description'] = "Test course category {$i}\n{$this->loremipsum}"; } if (!isset($record['idnumber'])) { $record['idnumber'] = ''; } return coursecat::create($record); }
public function test_overview_files() { global $CFG; $this->setAdminUser(); $cat1 = coursecat::create(array('name' => 'Cat1')); // Create course c1 with one image file. $dratid1 = $this->fill_draft_area(array('filename.jpg' => 'Test file contents1')); $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 1', 'overviewfiles_filemanager' => $dratid1)); // Create course c2 with two image files (only one file will be added because of settings). $dratid2 = $this->fill_draft_area(array('filename21.jpg' => 'Test file contents21', 'filename22.jpg' => 'Test file contents22')); $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 2', 'overviewfiles_filemanager' => $dratid2)); // Create course c3 without files. $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3')); // Change the settings to allow multiple files of any types. $CFG->courseoverviewfileslimit = 3; $CFG->courseoverviewfilesext = '*'; // Create course c5 with two image files. $dratid4 = $this->fill_draft_area(array('filename41.jpg' => 'Test file contents41', 'filename42.jpg' => 'Test file contents42')); $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 4', 'overviewfiles_filemanager' => $dratid4)); // Create course c6 with non-image file. $dratid5 = $this->fill_draft_area(array('filename51.zip' => 'Test file contents51')); $c5 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 5', 'overviewfiles_filemanager' => $dratid5)); // Reset default settings. $CFG->courseoverviewfileslimit = 1; $CFG->courseoverviewfilesext = '.jpg,.gif,.png'; $courses = $cat1->get_courses(); $this->assertTrue($courses[$c1->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c2->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c3->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c4->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Does not validate the filetypes. $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles())); $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles())); $this->assertEquals(1, count($courses[$c4->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Validate the filetypes. // Overview files are not allowed, all functions return empty values. $CFG->courseoverviewfileslimit = 0; $this->assertFalse($courses[$c1->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c2->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c3->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c4->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c5->id]->has_course_overviewfiles()); $this->assertEquals(0, count($courses[$c1->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c2->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c4->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Multiple overview files are allowed but still limited to images. $CFG->courseoverviewfileslimit = 3; $this->assertTrue($courses[$c1->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c2->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c3->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c4->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Still does not validate the filetypes. $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles())); $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); // Only 1 file was actually added. $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles())); $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Multiple overview files of any type are allowed. $CFG->courseoverviewfilesext = '*'; $this->assertTrue($courses[$c1->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c2->id]->has_course_overviewfiles()); $this->assertFalse($courses[$c3->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c4->id]->has_course_overviewfiles()); $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles())); $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles())); $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles())); $this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles())); }
* @copyright 2015 Skylar Kelty <*****@*****.**> * @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->dirroot . '/lib/phpunit/classes/util.php'; cli_writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); cli_writeln('Welcome to the school generator!'); cli_writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); cli_writeln(''); $category = $DB->get_record('course_categories', array('name' => 'School of Witchcraft and Wizardry')); if (!$category) { cli_writeln('Creating category...'); $category = \coursecat::create(array('name' => 'School of Witchcraft and Wizardry', 'description' => 'A school like no other!', 'descriptionformat' => 1, 'parent' => 0, 'sortorder' => 520000, 'coursecount' => 0, 'visible' => 1, 'visibleold' => 1, 'timemodified' => 0, 'depth' => 1, 'path' => '/2', 'theme' => '')); } else { cli_writeln('Using existing category.'); } $courses = array('Astronomy', 'Charms', 'Dark Arts', 'Defence Against the Dark Arts', 'Flying', 'Herbology', 'History of Magic', 'Muggle Studies', 'Potions', 'Transfiguration', 'Alchemy', 'Apparition', 'Arithmancy', 'Care of Magical Creatures', 'Divination', 'Study of Ancient Runes', 'Extra-curricular subjects', 'Ancient Studies', 'Art', 'Frog Choir', 'Ghoul Studies', 'Magical Theory', 'Muggle Art', 'Music', 'Muggle Music', 'Orchestra', 'Xylomancy'); cli_writeln('Creating courses...'); $generator = \phpunit_util::get_data_generator(); $id = 1000; foreach ($courses as $course) { if ($DB->record_exists('course', array('fullname' => $course))) { continue; } $courserecord = array('shortname' => "WZ{$id}", 'fullname' => $course, 'numsections' => 10, 'startdate' => usergetmidnight(time()), 'category' => $category->id); $generator->create_course($courserecord, array('createsections' => true)); $id += 100; }
$PAGE->set_heading($fullname); $mform = new core_course_editcategory_form(null, array('categoryid' => $id, 'parent' => $category->parent, 'context' => $context, 'itemid' => $itemid)); $mform->set_data(file_prepare_standard_editor($category, 'description', $mform->get_description_editor_options(), $context, 'coursecat', 'description', $itemid)); $manageurl = new moodle_url('/course/management.php'); if ($mform->is_cancelled()) { if ($id) { $manageurl->param('categoryid', $id); } else { if ($parent) { $manageurl->param('categoryid', $parent); } } redirect($manageurl); } else { if ($data = $mform->get_data()) { if (isset($coursecat)) { if ((int) $data->parent !== (int) $coursecat->parent && !$coursecat->can_change_parent($data->parent)) { print_error('cannotmovecategory'); } $coursecat->update($data, $mform->get_description_editor_options()); } else { $category = coursecat::create($data, $mform->get_description_editor_options()); } $manageurl->param('categoryid', $category->id); redirect($manageurl); } } echo $OUTPUT->header(); echo $OUTPUT->heading($strtitle); $mform->display(); echo $OUTPUT->footer();
/** * Find the category using idnumber or name. * * @param array $categories List of categories * * @return int id of category found. */ private function get_category_from_group($categories) { global $DB; if (empty($categories)) { $catid = $this->get_default_category_id(); } else { $createnewcategories = $this->get_config('createnewcategories'); $categoryseparator = trim($this->get_config('categoryseparator')); $nestedcategories = trim($this->get_config('nestedcategories')); $searchbyidnumber = trim($this->get_config('categoryidnumber')); if (!empty($categoryseparator)) { $sep = '{\\' . $categoryseparator . '}'; } $catid = 0; $fullnestedcatname = ''; foreach ($categories as $categoryinfo) { if ($searchbyidnumber) { $values = preg_split($sep, $categoryinfo, -1, PREG_SPLIT_NO_EMPTY); if (count($values) < 2) { $this->log_line('Category ' . $categoryinfo . ' missing name or idnumber. Using default category instead.'); $catid = $this->get_default_category_id(); break; } $categoryname = $values[0]; $categoryidnumber = $values[1]; } else { $categoryname = $categoryinfo; $categoryidnumber = null; if (empty($categoryname)) { $this->log_line('Category ' . $categoryinfo . ' missing name. Using default category instead.'); $catid = $this->get_default_category_id(); break; } } if (!empty($fullnestedcatname)) { $fullnestedcatname .= ' / '; } $fullnestedcatname .= $categoryname; $parentid = $catid; // Check if category exist. $params = array(); if ($searchbyidnumber) { $params['idnumber'] = $categoryidnumber; } else { $params['name'] = $categoryname; } if ($nestedcategories) { $params['parent'] = $parentid; } if ($catid = $DB->get_field('course_categories', 'id', $params)) { continue; // This category already exists. } // If we're allowed to create new categories, let's create this one. if ($createnewcategories) { $newcat = new stdClass(); $newcat->name = $categoryname; $newcat->visible = 0; $newcat->parent = $parentid; $newcat->idnumber = $categoryidnumber; $newcat = coursecat::create($newcat); $catid = $newcat->id; $this->log_line("Created new (hidden) category '{$fullnestedcatname}'"); } else { // If not found and not allowed to create, stick with default. $this->log_line('Category ' . $categoryinfo . ' not found in Moodle database. Using default category instead.'); $catid = $this->get_default_category_id(); break; } } } return $catid; }
public function create_category($category) { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; return \coursecat::create($category); }
if ($parent) { redirect($CFG->wwwroot . '/course/manage.php?categoryid=' . $parent); } else { redirect($CFG->wwwroot . '/course/manage.php'); } } } else { if ($data = $mform->get_data()) { if ($id) { $newcategory = coursecat::get($id); if ($data->parent != $category->parent && !$newcategory->can_change_parent($data->parent)) { print_error('cannotmovecategory'); } $newcategory->update($data, $editoroptions); } else { $newcategory = coursecat::create($data, $editoroptions); } redirect('manage.php?categoryid=' . $newcategory->id); } } // Page "Add new category" (with "Top" as a parent) does not exist in navigation. // We pretend we are on course management page. if (empty($id) && empty($parent)) { navigation_node::override_active_url(new moodle_url('/course/manage.php')); } $PAGE->set_title($title); $PAGE->set_heading($fullname); echo $OUTPUT->header(); echo $OUTPUT->heading($strtitle); $mform->display(); echo $OUTPUT->footer();
public function test_course_contacts() { global $DB, $CFG; $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher')); $managerrole = $DB->get_record('role', array('shortname'=>'manager')); $studentrole = $DB->get_record('role', array('shortname'=>'student')); $oldcoursecontact = $CFG->coursecontact; $CFG->coursecontact = $managerrole->id. ','. $teacherrole->id; /** * User is listed in course contacts for the course if he has one of the * "course contact" roles ($CFG->coursecontact) AND is enrolled in the course. * If the user has several roles only the highest is displayed. */ // Test case: // // == Cat1 (user2 has teacher role) // == Cat2 // -- course21 (user2 is enrolled as manager) | [Expected] Manager: F2 L2 // -- course22 (user2 is enrolled as student) | [Expected] Teacher: F2 L2 // == Cat4 (user2 has manager role) // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | [Expected] Manager: F5 L5, Teacher: F4 L4 // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2 // == Cat3 (user3 has manager role) // -- course31 (user3 is enrolled as student) | [Expected] Manager: F3 L3 // -- course32 | [Expected] // -- course11 (user1 is enrolled as teacher) | [Expected] Teacher: F1 L1 // -- course12 (user1 has teacher role) | [Expected] // also user4 is enrolled as teacher but enrolment is not active $category = $course = $enrol = $user = array(); $category[1] = coursecat::create(array('name' => 'Cat1'))->id; $category[2] = coursecat::create(array('name' => 'Cat2', 'parent' => $category[1]))->id; $category[3] = coursecat::create(array('name' => 'Cat3', 'parent' => $category[1]))->id; $category[4] = coursecat::create(array('name' => 'Cat4', 'parent' => $category[2]))->id; foreach (array(1,2,3,4) as $catid) { foreach (array(1,2) as $courseid) { $course[$catid][$courseid] = $this->getDataGenerator()->create_course(array('idnumber' => 'id'.$catid.$courseid, 'category' => $category[$catid]))->id; $enrol[$catid][$courseid] = $DB->get_record('enrol', array('courseid'=>$course[$catid][$courseid], 'enrol'=>'manual'), '*', MUST_EXIST); } } foreach (array(1,2,3,4,5) as $userid) { $user[$userid] = $this->getDataGenerator()->create_user(array('firstname' => 'F'.$userid, 'lastname' => 'L'.$userid))->id; } $manual = enrol_get_plugin('manual'); // Cat1 (user2 has teacher role) role_assign($teacherrole->id, $user[2], context_coursecat::instance($category[1])); // course21 (user2 is enrolled as manager) $manual->enrol_user($enrol[2][1], $user[2], $managerrole->id); // course22 (user2 is enrolled as student) $manual->enrol_user($enrol[2][2], $user[2], $studentrole->id); // Cat4 (user2 has manager role) role_assign($managerrole->id, $user[2], context_coursecat::instance($category[4])); // course41 (user4 is enrolled as teacher, user5 is enrolled as manager) $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id); $manual->enrol_user($enrol[4][1], $user[5], $managerrole->id); // course42 (user2 is enrolled as teacher) $manual->enrol_user($enrol[4][2], $user[2], $teacherrole->id); // Cat3 (user3 has manager role) role_assign($managerrole->id, $user[3], context_coursecat::instance($category[3])); // course31 (user3 is enrolled as student) $manual->enrol_user($enrol[3][1], $user[3], $studentrole->id); // course11 (user1 is enrolled as teacher) $manual->enrol_user($enrol[1][1], $user[1], $teacherrole->id); // -- course12 (user1 has teacher role) // also user4 is enrolled as teacher but enrolment is not active role_assign($teacherrole->id, $user[1], context_course::instance($course[1][2])); $manual->enrol_user($enrol[1][2], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED); $allcourses = coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1))); // Simplify the list of contacts for each course (similar as renderer would do) $contacts = array(); foreach (array(1,2,3,4) as $catid) { foreach (array(1,2) as $courseid) { $tmp = array(); foreach ($allcourses[$course[$catid][$courseid]]->get_course_contacts() as $contact) { $tmp[] = $contact['rolename']. ': '. $contact['username']; } $contacts[$catid][$courseid] = join(', ', $tmp); } } // Assert: // -- course21 (user2 is enrolled as manager) | Manager: F2 L2 $this->assertEquals('Manager: F2 L2', $contacts[2][1]); // -- course22 (user2 is enrolled as student) | Teacher: F2 L2 $this->assertEquals('Teacher: F2 L2', $contacts[2][2]); // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | Manager: F5 L5, Teacher: F4 L4 $this->assertEquals('Manager: F5 L5, Teacher: F4 L4', $contacts[4][1]); // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2 $this->assertEquals('Manager: F2 L2', $contacts[4][2]); // -- course31 (user3 is enrolled as student) | Manager: F3 L3 $this->assertEquals('Manager: F3 L3', $contacts[3][1]); // -- course32 | $this->assertEquals('', $contacts[3][2]); // -- course11 (user1 is enrolled as teacher) | Teacher: F1 L1 $this->assertEquals('Teacher: F1 L1', $contacts[1][1]); // -- course12 (user1 has teacher role) | $this->assertEquals('', $contacts[1][2]); $CFG->coursecontact = $oldcoursecontact; }
/** * make a course category for the teacher and give full control to it * * */ function local_ent_installer_make_teacher_category($user, $unassignteachercategoryrole = null) { global $DB, $CFG; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/lib/coursecatlib.php'; try { $institutionid = get_config('local_ent_installer', 'institution_id'); $teacherstubcategory = get_config('local_ent_installer', 'teacher_stub_category'); if (!$teacherstubcategory) { mtrace("No stub"); return; } $teachercategoryrole = $DB->get_record('role', array('shortname' => ENT_TEACHER_CATEGORY_ROLE)); $teachercatidnum = $institutionid . '$' . $user->idnumber . '$CAT'; $existingcategory = $DB->get_record('course_categories', array('idnumber' => $teachercatidnum)); if ($existingcategory) { $categorycontext = $DB->get_record('context', array('contextlevel' => CONTEXT_COURSECAT, 'instanceid' => $existingcategory->id)); } if (!empty($unassignteachercategoryrole)) { $rolestounassign = explode(',', $unassignteachercategoryrole); foreach ($rolestounassign as $myrole) { $roletounassign = $DB->get_record('role', array('shortname' => $myrole)); if (isset($roletounassign)) { role_unassign($roletounassign->id, $user->id, $categorycontext->id); } } } if (isset($categorycontext)) { $roleassignedtoteachercategory = $DB->get_records('role_assignments', array('roleid' => $teachercategoryrole->id, 'contextid' => $categorycontext->id, 'userid' => $user->id, 'component' => "", 'itemid' => 0), 'id'); if (!$roleassignedtoteachercategory) { role_assign($teachercategoryrole->id, $user->id, $categorycontext->id); } } else { if (!$existingcategory) { $newcategory = new StdClass(); $newcategory->name = fullname($user); $newcategory->idnumber = $teachercatidnum; $newcategory->parent = $teacherstubcategory; $newcategory->visible = 1; $newcategory = coursecat::create($newcategory); fix_course_sortorder(); $categorycontext = context_coursecat::instance($newcategory->id); } else { context_helper::create_instances(CONTEXT_COURSECAT, true); $categorycontext = context_coursecat::instance($existingcategory->id); } role_assign($teachercategoryrole->id, $user->id, $categorycontext->id); } } catch (Exception $e) { mtrace("ERROR : exception into make_teacher_category"); mtrace('ERROR MSG : ' . $e->getMessage()); if ($options['verbose']) { mtrace(' ###### '); mtrace("ERROR FILE : {$e->getFile()} : {$e->getLine()}"); mtrace('ERROR TRACE : ' . $e->getTraceAsString()); mtrace(' ###### '); } } }
/** * Proceed with the import of the course category. * * @return void */ public function proceed() { if (!$this->prepared) { throw new coding_exception('The course has not been prepared.'); } else { if ($this->has_errors()) { throw new moodle_exception('Cannot proceed, errors were detected.'); } else { if ($this->processstarted) { throw new coding_exception('The process has already been started.'); } } } $this->processstarted = true; if ($this->do === self::DO_DELETE) { if ($this->delete()) { $this->set_status('coursecategorydeleted', new lang_string('coursecategorydeleted', 'tool_uploadcoursecategory')); } else { $this->error('errordeletingcategory', new lang_string('errordeletingcategory', 'tool_uploadcoursecategory')); } return true; } else { if ($this->do === self::DO_CREATE) { try { $newcat = coursecat::create($this->finaldata); } catch (moodle_exception $e) { $this->error('errorcreatingcategory', new lang_string('errorcreatingcategory', 'tool_uploadcoursecategory')); } $this->id = $newcat->id; $this->set_status('coursecategoriescreated', new lang_string('coursecategoriescreated', 'tool_uploadcoursecategory')); } else { if ($this->do === self::DO_UPDATE) { $cat = coursecat::get($this->existing->id, IGNORE_MISSING, true); try { $cat->update($this->finaldata); } catch (moodle_exception $e) { $this->error('errorupdatingcategory', new lang_string('errorupdatingcategory', 'tool_uploadcoursecategory')); } $this->id = $cat->id; $this->set_status('coursecategoryupdated', new lang_string('coursecategoryupdated', 'tool_uploadcoursecategory')); } } } }