/** * Create new label module instance * @param array|stdClass $record * @param array $options * @return stdClass activity record with extra cmid field */ public function create_instance($record = null, array $options = null) { global $CFG; require_once("$CFG->dirroot/mod/label/lib.php"); $this->instancecount++; $i = $this->instancecount; $record = (object)(array)$record; $options = (array)$options; if (empty($record->course)) { throw new coding_exception('module generator requires $record->course'); } if (!isset($record->name)) { $record->name = get_string('pluginname', 'label').' '.$i; } if (!isset($record->intro)) { $record->intro = 'Test label '.$i; } if (!isset($record->introformat)) { $record->introformat = FORMAT_MOODLE; } $record->coursemodule = $this->precreate_course_module($record->course, $options); $id = label_add_instance($record, null); return $this->post_add_instance($id, $record->coursemodule); }
/** * Handle a file that has been uploaded * @param object $uploadinfo details of the file / content that has been uploaded * @return int instance id of the newly created mod */ function label_dndupload_handle($uploadinfo) { global $USER; // Gather the required info. $data = new stdClass(); $data->course = $uploadinfo->course->id; $data->name = $uploadinfo->displayname; $data->intro = ''; $data->introformat = FORMAT_HTML; $data->coursemodule = $uploadinfo->coursemodule; // Extract the first (and only) file from the file area and add it to the label as an img tag. if (!empty($uploadinfo->draftitemid)) { $fs = get_file_storage(); $draftcontext = context_user::instance($USER->id); $context = context_module::instance($uploadinfo->coursemodule); $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false); if ($file = reset($files)) { if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { // It is an image - resize it, if too big, then insert the img tag. $config = get_config('label'); $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight); } else { // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case. $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); $data->intro = html_writer::link($url, $file->get_filename()); } $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0, null, $data->intro); } } else { if (!empty($uploadinfo->content)) { $data->intro = $uploadinfo->content; if ($uploadinfo->type != 'text/html') { $data->introformat = FORMAT_PLAIN; } } } return label_add_instance($data, null); }
public function create_skillsmanagement($userid, $testing_mode) { global $DB, $CFG; require_once $CFG->dirroot . '/user/profile/lib.php'; require_once $CFG->dirroot . '/user/lib.php'; require_once $CFG->dirroot . '/blocks/exacomp/lib/lib.php'; $user = $DB->get_record('user', ['id' => $userid]); if (!$user) { throw new moodle_exception('user not found'); } /*CREATE COURSE CATEGORY*/ //check if course category has already been created $category_already_exists = false; $max_sort = 0; $categoryid = 0; $category_sortoder = 0; $categorycontext = 0; $categorycontextpath = ''; $course_categories = $DB->get_records('course_categories'); foreach ($course_categories as $category) { if (strcmp($category->name, 'Skillmanagement') == 0) { $category_already_exists = true; $categoryid = $category->id; $category_sortoder = $category->sortorder; $context = $DB->get_record('context', array('instanceid' => $categoryid, 'contextlevel' => 40)); $categorycontext = $context->id; $categorycontextpath = $context->path; } elseif ($category->sortorder > $max_sort) { $max_sort = $category->sortorder; } } //if not->create if (!$category_already_exists) { /*INSERT IN COURSE_CATEGORIES*/ $insert = new stdClass(); $insert->name = 'Skillmanagement'; $insert->description = '<p>Dieser Kursbereich wird für automatisch generierte Kurse von Skillmanagement verwendet.</p>'; $insert->descriptionformat = 1; $insert->sortorder = $max_sort + 10000; $insert->timemodified = time(); $insert->depth = 1; $insertedid = $DB->insert_record('course_categories', $insert); $inserted = $DB->get_record('course_categories', array('id' => $insertedid)); $categoryid = $inserted->id; $category_sortoder = $inserted->sortorder; $update = new stdClass(); $update->id = $inserted->id; $update->path = '/' . $inserted->id; $DB->update_record('course_categories', $update); /*INSERT IN CONTEXT*/ $insert = new stdClass(); $insert->contextlevel = 40; $insert->instanceid = $inserted->id; $insert->depth = 2; $insertedid = $DB->insert_record('context', $insert); $inserted = $DB->get_record('context', array('id' => $insertedid)); $categorycontext = $inserted->id; $categorycontextpath = '/1/' . $inserted->id; $update = new stdClass(); $update->id = $inserted->id; $update->path = '/1/' . $inserted->id; $DB->update_record('context', $update); } /*TESTED AND WORKING CORRECTLY TILL HERE*/ $user_course = $DB->get_record('course', ['shortname' => 'SKILLSMGMT-' . $user->id]); if (!$user_course) { $categorycourses = $DB->get_records_sql('SELECT * FROM {course} WHERE category=?', array($categoryid)); /*CREATE COURSE*/ //insert in course-table $insert = new stdClass(); $insert->category = $categoryid; $insert->sortorder = $category_sortoder + count($categorycourses) + 1; $insert->fullname = 'Skillsmanagement_' . $user->id . '_' . $user->lastname; $insert->shortname = 'SKILLSMGMT-' . $user->id; $insert->format = 'weeks'; $insert->summaryformat = 1; $insert->startdate = time(); $insert->timecreated = time(); $insert->timemodified = time(); $insert->cacherev = time(); $user_course = create_course($insert); /*CREATE EXABIS BLOCK INSTANCES IN COURSE*/ $page = new moodle_page(); $page->set_course($user_course); $page->blocks->add_region('side-post'); $page->blocks->add_block('exacomp', 'side-post', 4, false, 'course-view-*'); $page->blocks->add_block('exaport', 'side-post', 4, false, 'course-view-*'); $page->blocks->add_block('exastud', 'side-post', 4, false, 'course-view-*'); /*CREATE COURSE DESCRIPTION*/ $label = new stdClass(); $label->intro = get_string('course_description', 'auth_skillmanagement'); $label->intro .= html_writer::empty_tag('br'); $label->intro .= html_writer::empty_tag('img', array('src' => new moodle_url('/auth/skillmanagement/pix/intro.png'), 'alt' => 'intro')); $label->course = $user_course->id; $label->introformat = 1; $labelid = label_add_instance($label); /*CREATE COURSE MODULE*/ $label_module = $DB->get_record('modules', array('name' => 'label')); $section = $DB->get_record('course_sections', array('course' => $user_course->id, 'section' => 0)); $cm = new stdClass(); $cm->course = $user_course->id; $cm->module = $label_module->id; $cm->instance = $labelid; $cm->section = $section->id; $cm->added = time(); $cmid = $DB->insert_record('course_modules', $cm); course_add_cm_to_section($user_course->id, $cmid, 0); $course_context = context_course::instance($user_course->id); } $source = $DB->get_record(\block_exacomp\DB_DATASOURCES, ['source' => 'SKILLSMGMT-' . $user->id]); if (!$source) { // import new for this user \block_exacomp\data_importer::do_import_file(__DIR__ . '/skills_mgmt_' . ($user->lang == 'de' ? 'de' : 'en') . '.xml'); // last imported source $source = $DB->get_record_sql("SELECT * FROM {" . \block_exacomp\DB_DATASOURCES . "} ORDER BY id DESC LIMIT 1"); // change source $source->source = 'SKILLSMGMT-' . $user->id; $source->name = 'Skills Management for ' . fullname($user); $DB->update_record(\block_exacomp\DB_DATASOURCES, $source); } // last imported schooltypes $schooltype_ids = $DB->get_records_menu(\block_exacomp\DB_SCHOOLTYPES, ['source' => $source->id], null, 'sourceid AS id, id AS val'); block_exacomp_set_mdltype($schooltype_ids, $user_course->id); $subjects = block_exacomp_get_subjects_for_schooltype($user_course->id, 0); /* if ($user->lang == 'de') { $subjects = block_exacomp_get_subjects_for_schooltype($user_course->id, $schooltype_ids[72]) + block_exacomp_get_subjects_for_schooltype($user_course->id, $schooltype_ids[73]); } else { $subjects = block_exacomp_get_subjects_for_schooltype($user_course->id, $schooltype_ids[492]) + block_exacomp_get_subjects_for_schooltype($user_course->id, $schooltype_ids[493]); } */ $coursetopics = array(); foreach ($subjects as $subject) { $topics = block_exacomp_get_all_topics($subject->id); foreach ($topics as $topic) { $coursetopics[] = $topic->id; } } block_exacomp_set_coursetopics($user_course->id, $coursetopics); /*DELETE OTHER MODULES FROM COURSE*/ //delete module recent activity /*$recent_activity = $DB->get_record('block_instances', array('parentcontextid'=>$course_context->id, 'blockname'=>'recent_activity')); course_delete_module($recent_activity->id); $search_forum = $DB->get_record('block_instances', array('parentcontextid'=>$course_context->id, 'blockname'=>'search_forums')); course_delete_module($search_forum->id); $news_items = $DB->get_record('block_instances', array('parentcontextid'=>$course_context->id, 'blockname'=>'news_items')); course_delete_module($news_items->id); $calendar = $DB->get_record('block_instances', array('parentcontextid'=>$course_context->id, 'blockname'=>'calendar_upcoming')); course_delete_module($calendar->id); /*CREATE SECOND USER WITH SAME PW*/ $user_student = $DB->get_record('user', array('username' => 'employee_' . $user->username)); if (!$user_student) { $user_student = new stdClass(); $user_student->firstname = get_string('firstname', 'auth_skillmanagement'); $user_student->lastname = get_string('lastname', 'auth_skillmanagement'); $user_student->confirmed = 1; $user_student->password = '******'; // will be overwritten later $user_student->email = $user->email; $user_student->auth = 'skillmanagement'; $user_student->username = '******' . $user->username; $user_student->lang = $user->lang; $user_student->mnethostid = 1; $user_student->id = user_create_user($user_student, false, false); profile_save_data($user_student); } $DB->update_record('user', ['confirmed' => 1, 'password' => $user->password, 'id' => $user_student->id]); /* $DB->update_record('user', [ 'confirmed' => 1, // 'idnumber' => $user_student->id, // not sure why?!? connect student to teacher?!? 'id' => $user->id, ]); */ /*ENROL user_trainer AND user_trainee to $user_course */ $enrolment = array('roleid' => 3, 'userid' => $user->id, 'courseid' => $user_course->id, 'timestart' => time(), 'timeend' => 0, 'suspend' => 0); $enrol = enrol_get_plugin('manual'); $enrolinstances = enrol_get_instances($enrolment['courseid'], true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance = $courseenrolinstance; break; } } if (empty($instance)) { $errorparams = new stdClass(); $errorparams->courseid = $enrolment['courseid']; throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams); } // Check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin). if (!$enrol->allow_enrol($instance)) { $errorparams = new stdClass(); $errorparams->roleid = $enrolment['roleid']; $errorparams->courseid = $enrolment['courseid']; $errorparams->userid = $enrolment['userid']; throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams); } $enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid'], $enrolment['timestart'], $enrolment['timeend'], $enrolment['suspend']); //enrol participant $enrolment = array('roleid' => 5, 'userid' => $user_student->id, 'courseid' => $user_course->id, 'timestart' => time(), 'timeend' => 0, 'suspend' => 0); $enrol = enrol_get_plugin('manual'); $enrolinstances = enrol_get_instances($enrolment['courseid'], true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance = $courseenrolinstance; break; } } if (empty($instance)) { $errorparams = new stdClass(); $errorparams->courseid = $enrolment['courseid']; throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams); } // Check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin). if (!$enrol->allow_enrol($instance)) { $errorparams = new stdClass(); $errorparams->roleid = $enrolment['roleid']; $errorparams->courseid = $enrolment['courseid']; $errorparams->userid = $enrolment['userid']; throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams); } $enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid'], $enrolment['timestart'], $enrolment['timeend'], $enrolment['suspend']); // elove if (!$DB->record_exists(\block_exacomp\DB_EXTERNAL_TRAINERS, array('trainerid' => $user->id, 'studentid' => $user_student->id))) { $DB->insert_record(\block_exacomp\DB_EXTERNAL_TRAINERS, array('trainerid' => $user->id, 'studentid' => $user_student->id)); } }