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 static function authorizeCourseAccess(GcrMdlCourse $course) { global $CFG; $eschool = $course->getApp(); $open_access = $eschool->getConfigVar('forcelogin') == 0; $visible = $course->isVisible(); $authorized = false; if ($CFG->current_app->hasPrivilege('Student')) { $current_user = $CFG->current_app->getCurrentUser(); if ($current_user->hasAccess($eschool)) { $authorized = $CFG->current_app->hasPrivilege('GCStaff'); if (!$authorized && $CFG->current_app->hasPrivilege('EschoolStaff')) { $authorized = $eschool->getInstitution()->getId() == $CFG->current_app->getInstitution()->getId(); } if (!$authorized) { $authorized = $visible; if (!$authorized) { $mdl_user = $current_user->getUserOnEschool($eschool); $authorized = $mdl_user && $course->isInstructor($mdl_user); } } } } else { $authorized = $visible && $open_access; } return $authorized; }