function copyFromBlock(GcrMdlBlockCourseProfile $parent_block, GcrMdlCourse $course) { $obj = $parent_block->getObject(); // If the instructor set on the parent block also has a teaching role // on this course, copy that instructorid. Otherwise, set it to the // default instructor of this course. $instructorid = 0; $courseid = $course->getObject()->id; $course_context_id = $course->getContext()->id; $instructor = $parent_block->getApp()->getUserById($obj->instructorid); if ($instructor && $course->isInstructor($instructor)) { $instructorid = $obj->instructorid; } else { $instructor = $course->getInstructor(); if ($instructor) { $instructorid = $instructor->getObject()->id; } else { return false; } } $fs = get_file_storage(); $existing_file = $fs->get_file($course_context_id, 'block_course_profile', 'courseicon', 0, '/', $courseid); if ($existing_file) { $existing_file->delete(); } $file = $fs->get_file($parent_block->getContext()->id, 'block_course_profile', 'courseicon', 0, '/', $obj->courseid); $file_record = array('contextid' => $course_context_id, 'component' => 'block_course_profile', 'filearea' => 'courseicon', 'itemid' => 0, 'filepath' => '/', 'filename' => $courseid, 'userid' => $instructorid); $fs->create_file_from_storedfile($file_record, $file); $data = array('courseid' => $courseid, 'instructorid' => $instructorid, 'courseicon' => $obj->courseicon); $course->getApp()->upsertIntoMdlTable('block_course_profile', $data, array('courseid' => $courseid)); }
public function checkConstraints() { global $CFG, $COURSE; if ($CFG->current_app->isMoodle()) { if (isset($COURSE->id)) { $course = new GcrMdlCourse($COURSE, $CFG->current_app); $context = $course->getContext(); if ($context) { return $CFG->current_app->hasPrivilege('EschoolAdmin') || $CFG->current_app->hasCapability('moodle/site:viewreports', $context); } } } return false; }