public function get_content() { global $COURSE, $USER; if ($this->content !== null) { return $this->content; } $this->content = new stdClass(); $this->content->text = var_export($this->context, true); $coursecontext = context_course::instance($COURSE->id); if (has_capability('block/demostudent:addinstance', $coursecontext)) { // If DemoStudent has not yet been enrolled, allow user to create/enrol one. $demostudentusername = generate_demostudent_name($USER->username); $demostudentuser = get_complete_user_data('username', $demostudentusername); if (!$demostudentuser || !is_enrolled($coursecontext, $demostudentuser)) { $this->render_view('firstuse'); } else { $this->render_view('instructor'); } } else { if (has_capability('block/demostudent:seedemostudentblock', $coursecontext)) { $this->render_view('demostudent'); } else { // If the user does not need to see the block, do not display it at all. $this->content->text = ''; $this->content->footer = ''; } } return $this->content; }
require_login($courseid); $PAGE->set_url('/blocks/demostudent/remove.php', array('viewrole' => $viewrole, 'courseid' => $courseid, 'confirm' => $confirm)); $coursecontext = context_course::instance($courseid); if ($viewrole != 'instructor') { print get_string('errorremovenotinstructor', 'block_demostudent'); require_logout(); redirect(new moodle_url($url, array('redirect' => 1))); } // If we do not have the capability to add this block, we likely landed here by accident or malice. if (!has_capability('block/demostudent:addinstance', $coursecontext)) { // Test this as above, but start from a student account instead of DemoStudent. print get_string('errormissingaddinstancecapability', 'block_demostudent'); require_logout(); redirect(new moodle_url($url, array('redirect' => 1))); } $demostudentusername = generate_demostudent_name($username); $demostudentuser = get_complete_user_data('username', $demostudentusername); if (!$demostudentuser) { redirect(new moodle_url($url, array('redirect' => 1))); } if ($confirm) { // Unenrol DemoStudent from the course. if (!enrol_is_enabled('manual')) { redirect(new moodle_url($url, array('redirect' => 1))); } if (!($enrol = enrol_get_plugin('manual'))) { redirect(new moodle_url($url, array('redirect' => 1))); } if (!($instances = $DB->get_records('enrol', array('enrol' => 'manual', 'courseid' => $courseid, 'status' => ENROL_INSTANCE_ENABLED), 'sortorder,id ASC'))) { redirect(new moodle_url($url, array('redirect' => 1))); }