public static function execute_rule($rule_id, $user_id, $variables = array()) { $pl = block_playlyfe_sdk::get_pl(); try { $pl->post('/admin/rules/' . $rule_id, array(), array('data' => array(array('player_ids' => array('' . $user_id), 'variables' => (object) $variables)))); } catch (Exception $e) { if ($e->name != 'rule_not_found') { throw $e; } } }
<?php require_once dirname(dirname(dirname(__FILE__))) . '/config.php'; require_login(); $can_modify = has_capability('block/playlyfe:addinstance', context_user::instance($USER->id)); $method = $_GET['method']; $route = $_GET['route']; $query = array(); if (stripos($route, '/design') === 0 && !$can_modify) { echo 'You need to be authorized to make this request'; } else { if (stripos($route, '/admin') === 0 && !$can_modify) { echo 'You need to be authorized to make this request'; } else { if (stripos($route, '/runtime') === 0) { $query['player_id'] = $USER->id; } header('Content-Type: application/json'); try { $data = json_decode(file_get_contents('php://input'), false); $pl = block_playlyfe_sdk::get_pl(); echo json_encode($pl->api($method, $route, $query, $data)); $pl->post('/design/versions/latest/deploy'); } catch (Exception $e) { http_response_code(400); echo json_encode($e); } } }
public function get_content() { global $USER, $DB; $this->content = new stdClass(); $this->content->footer = 'Powered by Playlyfe'; if ($USER->id == 0) { return $this->content; } $pl = block_playlyfe_sdk::get_pl(); $firewall = array(0, 1, 2, 3); // prevent users who don't have site config permission from accessing these type of blocks $isadmin = has_capability('moodle/site:config', context_user::instance($USER->id)); if (in_array($this->config->type, $firewall) && !$isadmin) { $this->content->text = "You need to be an admin to use this type of the Playlyfe block"; return; } switch ($this->config->type) { case 0: $this->title = 'Points'; $point = null; try { $point = $pl->get('/design/versions/latest/metrics/point'); } catch (Exception $e) { if ($e->name == 'metric_not_found') { $point = $pl->post('/design/versions/latest/metrics', array(), array('id' => 'point', 'name' => 'point', 'type' => 'point', 'constraints' => array('default' => '0', 'max' => 'Infinity', 'min' => '0'), 'tags' => ['point'])); $pl->post('/design/versions/latest/leaderboards', array(), array('id' => 'point', 'name' => 'point', 'type' => 'regular', 'description' => '', 'entity_type' => 'players', 'scope' => array('type' => 'game'), 'metric' => array('id' => 'point', 'type' => 'point'))); } else { throw $e; } } $this->content->text = '<div id="pl_point_block"></div>'; $this->page->requires->js_init_call('init_point_block', array($point)); break; case 1: $this->title = 'Badges'; $badges = $pl->get('/design/versions/latest/metrics', array('fields' => 'id,name,description,type,image', 'tags' => 'badge')); $this->content->text = '<div id="pl_badge_block"></div>'; $this->page->requires->js_init_call('init_badge_list', array($badges)); break; case 2: $this->title = 'Levels'; try { $base = $pl->get('/design/versions/latest/metrics/point'); $state = $pl->get('/design/versions/latest/metrics/level'); $level_rule = $pl->get('/design/versions/latest/rules/level'); } catch (Exception $e) { if ($e->name == 'metric_not_found') { $state = $pl->post('/design/versions/latest/metrics', array(), array('id' => 'level', 'name' => 'level', 'type' => 'state', 'image' => 'default-state-metric', 'constraints' => array('states' => array(array('name' => 'none1', 'image' => 'default-state-metric'), array('name' => 'none2', 'image' => 'default-state-metric'))))); } else { if ($e->name == 'rule_not_found') { $level_rule = $pl->post('/design/versions/latest/rules', array(), array('id' => 'level', 'name' => 'level', 'type' => 'level', 'level_metric' => 'level', 'base_metric' => 'point', 'levels' => array(array('rank' => 'none1', 'threshold' => '5'), array('rank' => 'none2')))); } else { throw $e; } } } $this->content->text = '<div id="pl_level_block"></div>'; $this->page->requires->js_init_call('init_level_list', array(array('state' => $state, 'base' => $base, 'rule' => $level_rule))); break; case 3: switch ($this->config->event) { case 0: $rule_id = "log_in"; $this->title = 'Logged In Rule'; break; case 1: $rule_id = "log_out"; $this->title = 'Logged Out Rule'; break; case 2: // Find context if not course then display this block must be shown only on course page $currentcontext = $this->page->context->get_course_context(false); if (empty($currentcontext)) { $this->content->text = 'This block must be present on a course page'; return; } $this->title = 'Course Completed Rule'; $rule_id = "course_completed_" . $this->page->course->id; break; case 3: // Find context if not course then display this block must be shown only on course page $currentcontext = $this->page->context->get_course_context(false); if (empty($currentcontext)) { $this->content->text = 'This block must be present on a course page'; return; } $this->title = 'Forum Post Created Rule'; $rule_id = "forum_post_created_" . $this->page->course->id; break; case 4: $currentcontext = $this->page->context->get_course_context(false); if (empty($currentcontext)) { $this->content->text = 'This block must be present on a course page'; return; } $this->title = 'Forum Discussion Created Rule'; $rule_id = "forum_discussion_created_" . $this->page->course->id; break; case 5: $id = optional_param('id', null, PARAM_INT); if (!$id) { $this->content->text = 'This block must be present on the quiz page'; return; } $currentcontext = $this->page->context->get_course_context(false); if (empty($currentcontext)) { $this->content->text = 'This block must be present on a course page'; return; } $this->title = 'Quiz Submitted Rule'; $rule_id = "quiz_attempt_submitted_" . $id; break; } $point = $pl->get('/design/versions/latest/metrics/point'); $badges = $pl->get('/design/versions/latest/metrics', array('fields' => 'id,name,description,type,image', 'tags' => 'badge')); // We then fetch the rule and if it does not exist we create it try { $rule = $pl->get('/design/versions/latest/rules/' . $rule_id); } catch (Exception $e) { if ($e->name == 'rule_not_found') { $rule = $pl->post('/design/versions/latest/rules', array(), array('id' => $rule_id, 'name' => $rule_id, 'type' => 'custom', 'rules' => array(array('rewards' => array(), 'requires' => new StdClass())), 'variables' => array(array('name' => 'score', 'type' => 'int', 'required' => false, 'default' => 0), array('name' => 'time_completed', 'type' => 'int', 'required' => false, 'default' => 0), array('name' => 'course_id', 'type' => 'int', 'required' => false, 'default' => 0)))); } else { throw $e; } } $this->content->text = '<div id="pl_' . $rule_id . '_block"></div>'; $this->page->requires->js_init_call('init_rule_list', array(array('rule' => $rule, 'point' => $point, 'badges' => $badges))); break; case 4: $this->title = 'Profile'; $profile = null; try { $profile = $pl->get('/runtime/player', array('player_id' => '' . $USER->id)); $notifications = $pl->get('/runtime/notifications', array('player_id' => $USER->id, 'start' => date('Y-m-d', time() - 7 * (24 * 60 * 60)), 'end' => date('Y-m-d', time() + 24 * 60 * 60))); $logs = array(); if ($notifications) { $notifications['data'] = array_reverse($notifications['data']); foreach ($notifications['data'] as $notification) { if ($notification['seen'] == false) { if ($notification['event'] == 'custom_rule') { if ($notification['rule']['id'] == 'log_in') { $notification['title'] = 'Logged In'; } else { if ($notification['rule']['id'] == 'log_out') { $notification['title'] = 'Logged Out'; } else { $split = explode('_', $notification['rule']['id']); $course_id = array_pop($split); $course = new StdClass(); $course->name = ''; $notification['title'] = $split[0]; try { $course = $DB->get_record('course', array('id' => $course_id), 'id, name', MUST_EXIST); } catch (Exception $e) { } if ($split[0] == 'forum' && $split[1] == 'post') { $notification['title'] = 'Forum Comment Posted in Course ' . $course->name; } if ($split[0] == 'forum' && $split[1] == 'discussion') { $notification['title'] = 'Forum Discussion Created in Course ' . $course->name; } if ($split[0] == 'course') { $notification['title'] = 'Course ' . $course->name . ' Completed'; } if ($split[0] == 'quiz') { $notification['title'] = 'Quiz Submitted in ' . $course->name; } } } } if ($notification['event'] == 'level') { $notification['title'] = 'New Level Attained'; } array_push($logs, $notification); } } } } catch (Exception $e) { if ($e->name == 'player_not_found') { $profile = $pl->post('/admin/players', array(), array('id' => $USER->id, 'alias' => $USER->firstname . ' ' . $USER->lastname)); } else { throw $e; } } $this->content->text = '<div id="pl_profile_block"></div><div id="pl_notification_block"></div><div id="pl_dialog"></div>'; $this->page->requires->js_init_call('show_profile', array(array('profile' => $profile, 'notifications' => $logs))); break; case 5: $this->title = "Leaderboard"; $leaderboard = $pl->get('/runtime/leaderboards/point', array('player_id' => '' . $USER->id, 'cycle' => 'alltime', 'skip' => 0, 'limit' => 10)); $this->content->text = '<div id="pl_leaderboard"></div>'; $this->page->requires->js_init_call('show_leaderboard', array($leaderboard)); break; } return $this->content; }