Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
[FEATURE] support all report types for Report API via passing raw JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kochnev committed Nov 23, 2018
1 parent 1897143 commit 3ea2ebe
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 50 deletions.
76 changes: 38 additions & 38 deletions classes/Learnosity/Shortcodes/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,43 @@
class Generator
{

public function __construct()
{
add_shortcode('lrn-items', array(&$this, 'render_items'));
add_shortcode('lrn-item', array(&$this, 'render_item'));
add_shortcode('lrn-submit', array(&$this, 'render_submit'));
add_shortcode('lrn-assess', array(&$this, 'render_assess'));
add_shortcode('lrn-report', array(&$this, 'render_report'));
}

public function render_item($attrs)
{
$item_embed = new ItemEmbed($attrs);
return $item_embed->render();
}

public function render_items($attrs)
{
$items_embed = new ItemsEmbed($attrs,'inline');
return $items_embed->render();
}

public function render_submit($attrs)
{
$submit_embed = new SubmitEmbed($attrs);
return $submit_embed->render();
}

public function render_assess($attrs)
{
$assess_embed = new ItemsEmbed($attrs,'assess');
return $assess_embed->render();
}

public function render_report($attrs)
{
$report_embed = new ReportEmbed($attrs);
return $report_embed->render();
}
public function __construct()
{
add_shortcode('lrn-items', array(&$this, 'render_items'));
add_shortcode('lrn-item', array(&$this, 'render_item'));
add_shortcode('lrn-submit', array(&$this, 'render_submit'));
add_shortcode('lrn-assess', array(&$this, 'render_assess'));
add_shortcode('lrn-report', array(&$this, 'render_report'));
}

public function render_item($attrs)
{
$item_embed = new ItemEmbed($attrs);
return $item_embed->render();
}

public function render_items($attrs)
{
$items_embed = new ItemsEmbed($attrs, 'inline');
return $items_embed->render();
}

public function render_submit($attrs)
{
$submit_embed = new SubmitEmbed($attrs);
return $submit_embed->render();
}

public function render_assess($attrs)
{
$assess_embed = new ItemsEmbed($attrs, 'assess');
return $assess_embed->render();
}

public function render_report($attrs, $content)
{
$report_embed = new ReportEmbed($attrs, $content);
return $report_embed->render();
}

}
53 changes: 41 additions & 12 deletions classes/Learnosity/Shortcodes/ReportEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class ReportEmbed

private $student_prefix;

private $contentProvided = false;

private $supported_reports = array(
'sessions-list',
'session-detail-by-item');

public function __construct($options)
public function __construct($options, $content)
{
$this->report_id = \UUID::generateUuid();
$this->student_prefix = get_option('lrn_student_prefix', 'student_');
Expand All @@ -43,7 +44,7 @@ public function __construct($options)
$lrnRepUserId = get_current_user_id();
if ($lrnRepUserId == 0 && $lrnuid != '') {
// get only id without prefix as prefix will be added later
$lrnRepUserId = str_replace($this->student_prefix,"",$lrnuid);
$lrnRepUserId = str_replace($this->student_prefix, "", $lrnuid);
}

$defaults = array(
Expand All @@ -65,20 +66,36 @@ public function __construct($options)
'show_correct_answers' => 'true',
);

$this->config = array_merge($defaults, $options);
//supporting JSON to be passed inside short code: [lrn-report] <pre>{...}]}</pre>[/lrn-report]
//using preformatted text <pre> to avoid replacing " for “
if ($options == '' && $content != '') {
// set up a flag for compatibility
$this->contentProvided = true;
//clean out <pre>, </pre> stuff
$content = str_replace(["<pre>", "</pre>"], ["", ""], $content);
$content = json_decode(sanitize_text_field($content), TRUE);

if (is_null($content)) {
$this->render_error("Invalid JSON for Learnosity Reports API provided.");
} else {
$content["id"] = $this->report_id;
$this->config = $content;
}
} else {
$this->config = array_merge($defaults, $options);
}
}

public function render()
{
ob_start();

//Check this is a supported report
if (!in_array($this->config['type'], $this->supported_reports)) {
if (!$this->contentProvided && !in_array($this->config['type'], $this->supported_reports)) {
$this->render_error("Unsupported report type: {$this->config['type']}");
} else {
$this->render_init_js($this->config);

$this->render_report($this->report_id);
$this->render_init_js($this->config);
$this->render_report($this->report_id);
}
return ob_get_clean();
}
Expand All @@ -96,7 +113,6 @@ private function get_user_name($user_id)
private function get_users_array($users_list)
{
$user_array = array();

foreach (explode(',', $users_list) as $key => $value) {
array_push($user_array,
array(
Expand Down Expand Up @@ -137,17 +153,31 @@ private function generate_signed_request()
$report['limit'] = (int)$this->config['limit'];
$report['display_user'] = $this->parse_boolean($this->config['display_user']);
$report['display_activity'] = $this->parse_boolean($this->config['display_activity']);
$report['users'] = $this->get_users_array($this->config['users']);
if ($this->config['activities'] != "") {

//support string (default via $attrs) and array (via $content) as input parameters
if (gettype($this->config['users']) == "string" && $this->config['users'] != "") {
$report['users'] = $this->get_users_array($this->config['users']);
} elseif (gettype($this->config['users']) == "array") {
$report['users'] = $this->config['users'];
}

//support string (default via $attrs) and array (via $content) as input parameters
if (gettype($this->config['activities']) == "string" && $this->config['activities'] != "") {
$report['activities'] = $this->get_activities_array($this->config['activities']);
} elseif (gettype($this->config['activities']) == "array") {
$report['activities'] = $this->config['activities'];
}

break;
case "session-detail-by-item":
$report['session_id'] = $this->config['session_id'];
$report['user_id'] = $this->student_prefix . $this->config['user_id'];
break;
default:

//grab all the parameters from $content JSON
if ($this->contentProvided) {
$report = $this->config;
}
break;
}

Expand All @@ -161,7 +191,6 @@ private function generate_signed_request()
$request
);
$signed_request = $request_helper->generateRequest();

return $signed_request;
}

Expand Down

0 comments on commit 3ea2ebe

Please sign in to comment.