public static function observer(\core\event\base $event)
 {
     global $DB;
     $unlocked_content = false;
     if (!block_game_content_unlock_helper::is_student($event->userid)) {
         return;
     }
     $uss = $DB->get_records_sql("SELECT * FROM {content_unlock_system} WHERE deleted = ? AND " . $DB->sql_compare_text('conditions') . " = " . $DB->sql_compare_text('?'), array('deleted' => 0, 'conditions' => $event->eventname));
     foreach ($uss as $unlocksystem) {
         $ccm = get_course_and_cm_from_cmid($unlocksystem->coursemoduleid);
         if ($event->courseid != $ccm[0]->id) {
             continue;
         }
         if (!(content_unlock_satisfies_conditions($unlocksystem->restrictions, $event->courseid, $event->userid) && content_unlock_satisfies_advanced_conditions($unlocksystem, $event) && content_unlock_satisfies_block_conditions($unlocksystem, $event->courseid, $event->userid))) {
             continue;
         }
         $blockcontextid = $DB->get_field('block_instances', 'parentcontextid', array('id' => $unlocksystem->blockinstanceid));
         if (!$blockcontextid) {
             continue;
         }
         $blockcontext = context::instance_by_id($blockcontextid);
         $context = context::instance_by_id($event->contextid);
         if (strpos($context->path, $blockcontext->path) !== 0 && $blockcontext->instanceid != SITEID) {
             continue;
         }
         $sql = "SELECT count(c.id)\n\t\t\t\tFROM {content_unlock_log} c\n\t\t\t\t\tINNER JOIN {logstore_standard_log} l ON c.logid = l.id\n\t\t\t\tWHERE l.userid = :userid\n\t\t\t\t\tAND c.unlocksystemid = :unlocksystemid";
         $params['userid'] = $event->userid;
         $params['unlocksystemid'] = $unlocksystem->id;
         if ($DB->count_records_sql($sql, $params) > 0) {
             continue;
         }
         $manager = get_log_manager();
         $selectreaders = $manager->get_readers('\\core\\log\\sql_reader');
         if ($selectreaders) {
             $reader = reset($selectreaders);
         }
         $selectwhere = "eventname = :eventname\n\t\t\t\tAND component = :component\n\t\t\t\tAND action = :action\n\t\t\t\tAND target = :target\n\t\t\t\tAND crud = :crud\n\t\t\t\tAND edulevel = :edulevel\n\t\t\t\tAND contextid = :contextid\n\t\t\t\tAND contextlevel = :contextlevel\n\t\t\t\tAND contextinstanceid = :contextinstanceid\n\t\t\t\tAND userid = :userid \n\t\t\t\tAND anonymous = :anonymous\n\t\t\t\tAND timecreated = :timecreated";
         $params['eventname'] = $event->eventname;
         $params['component'] = $event->component;
         $params['action'] = $event->action;
         $params['target'] = $event->target;
         $params['crud'] = $event->crud;
         $params['edulevel'] = $event->edulevel;
         $params['contextid'] = $event->contextid;
         $params['contextlevel'] = $event->contextlevel;
         $params['contextinstanceid'] = $event->contextinstanceid;
         $params['userid'] = $event->userid;
         $params['anonymous'] = $event->anonymous;
         $params['timecreated'] = $event->timecreated;
         $logid = $reader->get_events_select($selectwhere, $params, '', 0, 0);
         $logid = array_keys($logid)[0];
         $record = new stdClass();
         $record->logid = $logid;
         $record->unlocksystemid = $unlocksystem->id;
         $DB->insert_record('content_unlock_log', $record);
         $unlocked_content = true;
         if ($unlocksystem->mode == 0) {
             if ($unlocksystem->coursemodulevisibility == 1) {
                 set_section_visible($event->courseid, $ccm[1]->sectionnum, 1);
             }
             set_coursemodule_visible($unlocksystem->coursemoduleid, $unlocksystem->coursemodulevisibility);
         } else {
             groups_add_member($unlocksystem->groupid, $event->userid);
         }
     }
     if ($unlocked_content) {
         $params = array('context' => $context);
         $event = \block_game_content_unlock\event\content_unlocked::create($params);
         $event->trigger();
     }
 }
 public function get_content()
 {
     global $DB, $USER;
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     if (user_has_role_assignment($USER->id, 5)) {
         $eventsarray = content_unlock_generate_events_list();
         $us = $DB->get_records('content_unlock_system', array('deleted' => 0, 'blockinstanceid' => $this->instance->id));
         if (!empty($us)) {
             $unlocklist = '';
             $course = $DB->get_record('course', array('id' => $this->page->course->id));
             $info = get_fast_modinfo($course);
             foreach ($us as $unlocksystem) {
                 $sql = "SELECT *\n\t\t\t\t\t\t\tFROM {content_unlock_log} c\n\t\t\t\t\t\t\t\tINNER JOIN {logstore_standard_log} l ON c.logid = l.id\n\t\t\t\t\t\t\tWHERE l.userid = :userid\n\t\t\t\t\t\t\t\tAND c.unlocksystemid = :unlocksystemid";
                 $params['userid'] = $USER->id;
                 $params['unlocksystemid'] = $unlocksystem->id;
                 $unlocked_content = $DB->record_exists_sql($sql, $params);
                 if ($unlocked_content) {
                     continue;
                 }
                 $ccm = get_course_and_cm_from_cmid($unlocksystem->coursemoduleid);
                 if ($this->page->course->id != $ccm[0]->id) {
                     continue;
                 }
                 if (!(content_unlock_satisfies_conditions($unlocksystem->restrictions, $this->page->course->id, $USER->id) && (in_array($unlocksystem->conditions, self::$resource_events) || content_unlock_satisfies_block_conditions($unlocksystem, $this->page->course->id, $USER->id)))) {
                     continue;
                 }
                 $cm = $info->get_cm($unlocksystem->coursemoduleid);
                 $eventdescription = is_null($unlocksystem->eventdescription) ? $eventsarray[$unlocksystem->conditions] : $unlocksystem->eventdescription;
                 $unlocklist = $unlocklist . '<li>' . $cm->name . ' (' . get_string('modulename', $cm->modname) . ') por ' . (in_array($unlocksystem->conditions, self::$resource_events) ? content_unlock_get_block_conditions_text($unlocksystem) : $eventdescription) . '</li>';
             }
             if (strlen($unlocklist) > 0) {
                 $this->content->text = '<p>VocĂȘ pode desbloquear:<ul>' . $unlocklist . '</ul></p>';
             }
         }
         if (isset($this->config)) {
             $lastunlocksnumber = isset($this->config->lastunlocksnumber) ? $this->config->lastunlocksnumber : 1;
         } else {
             $lastunlocksnumber = 0;
         }
         if ($lastunlocksnumber > 0) {
             $sql = "SELECT c.id as id, s.coursemoduleid as coursemoduleid, s.eventdescription as eventdescription, s.conditions as conditions, s.connective as connective\n\t\t\t\t\tFROM\n\t\t\t\t\t\t{content_unlock_log} c\n\t\t\t\t\tINNER JOIN {logstore_standard_log} l ON c.logid = l.id\n\t\t\t\t\tINNER JOIN {content_unlock_system} s ON c.unlocksystemid = s.id\n\t\t\t\t\tWHERE l.userid = :userid\n\t\t\t\t\t\tAND l.courseid = :courseid\n\t\t\t\t\t\tAND s.blockinstanceid = :blockinstanceid\n\t\t\t\t\tORDER BY c.id DESC";
             $params['userid'] = $USER->id;
             $params['courseid'] = $this->page->course->id;
             $params['blockinstanceid'] = $this->instance->id;
             $lastunlocks = $DB->get_records_sql($sql, $params, 0, $lastunlocksnumber);
             if (!empty($lastunlocks)) {
                 $lastunlockslist = '';
                 foreach ($lastunlocks as $lu) {
                     $ccm = get_course_and_cm_from_cmid($lu->coursemoduleid);
                     $course = $DB->get_record('course', array('id' => $ccm[0]->id));
                     $info = get_fast_modinfo($course);
                     $cm = $info->get_cm($lu->coursemoduleid);
                     $eventdescription = is_null($lu->eventdescription) ? $eventsarray[$lu->conditions] : $lu->eventdescription;
                     $lastunlockslist = $lastunlockslist . '<li>' . $cm->name . ' (' . get_string('modulename', $cm->modname) . ') por ' . (in_array($lu->conditions, self::$resource_events) ? content_unlock_get_block_conditions_text($lu) : $eventdescription) . '</li>';
                 }
                 $this->content->text = $this->content->text . '<p>VocĂȘ desbloqueou recentemente:<ul>' . $lastunlockslist . '</ul></p>';
             }
         }
     }
     return $this->content;
 }