Skip to content

Commit

Permalink
Merge pull request #78 from Wunderbyte-GmbH/dev_christian
Browse files Browse the repository at this point in the history
Dev christian
  • Loading branch information
cbadusch authored Mar 12, 2024
2 parents 9911500 + 979e0ce commit 136edee
Show file tree
Hide file tree
Showing 20 changed files with 481 additions and 51 deletions.
2 changes: 1 addition & 1 deletion classes/completion/completion_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function get_completion_challenges_array($mooduellinstance): array
// Date until the challenge needs to be done.
$challenge->targetdate = $mooduellinstance->cm->completionexpected ?? null;

// TODO: Calculate a user's rank within a challenge. - Will be done in a future release.
// Calculate a user's rank within a challenge. - Will be done in a future release.
$challenge->challengerank = null;

// Add an array of objects containing localized language strings needed by the app.
Expand Down
7 changes: 4 additions & 3 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use mod_mooduell\manage_tokens;
use mod_mooduell\mooduell;
use mod_mooduell\completion\completion_utils;
use mod_mooduell\utils\wb_payment;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -298,7 +299,7 @@ public static function get_quizzes_with_caps_parameters() {
public static function get_mooduell_support() {
global $DB;
$url = get_config('mooduell', 'supporturl');
$pay = get_config('mooduell', 'unlockplatform');
$pay = wb_payment::pro_version_is_activated();
$badges = get_config('mooduell', 'disablebadges');
$themeimg = get_config('mod_mooduell', 'companylogo');
$themeimgalt = get_config('mod_mooduell', 'companylogoalternative');
Expand Down Expand Up @@ -375,11 +376,11 @@ public static function get_mooduell_purchases_returns() {
'purchases' => new external_multiple_structure(new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'id'),
'productid' => new external_value(PARAM_INT, 'productid'),
'productid' => new external_value(PARAM_TEXT, 'productid'),
'purchasetoken' => new external_value(PARAM_TEXT, 'purchasetoken'),
'receipt' => new external_value(PARAM_TEXT, 'receipt', VALUE_OPTIONAL, ''),
'signature' => new external_value(PARAM_TEXT, 'signature', VALUE_OPTIONAL, ''),
'orderid' => new external_value(PARAM_INT, 'orderid', VALUE_OPTIONAL, ''),
'orderid' => new external_value(PARAM_TEXT, 'orderid', VALUE_OPTIONAL, ''),
'free' => new external_value(PARAM_INT, 'free', VALUE_OPTIONAL, 0),
'userid' => new external_value(PARAM_INT, 'userid'),
'mooduellid' => new external_value(PARAM_INT, 'mooduellid', VALUE_OPTIONAL, 0),
Expand Down
10 changes: 5 additions & 5 deletions classes/game_finished.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public static function update_highscores_table(int $cmid) {
'gamesplayed' => $entry->played,
'gameswon' => $entry->won,
'gameslost' => $entry->lost,
'gamesstarted' => 0, // TODO: this will be added later.
'gamesfinished' => 0, // TODO: this will be added later.
'gamesstarted' => 0,
'gamesfinished' => 0,
'score' => $entry->score,
'qcorrect' => $entry->correct,
'qplayed' => $entry->qplayed,
'qplayed' => $entry->qplayed,
'qcpercentage' => $entry->correctpercentage,
'timecreated' => time(),
'timemodified' => time(),
Expand All @@ -87,8 +87,8 @@ public static function update_highscores_table(int $cmid) {
$entry = (object) $entry;

// Let's have a look if the entry already exists in the DB.
$sql = 'select * from {mooduell_highscores} where mooduellid = '.
$entry->mooduellid.' and userid = '.$entry->userid;
$sql = 'select * from {mooduell_highscores} where mooduellid = ' .
$entry->mooduellid . ' and userid = ' . $entry->userid;
$data = $DB->get_record_sql($sql);

// If the entry could be found in the database.
Expand Down
135 changes: 121 additions & 14 deletions classes/mooduell.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ public function return_list_of_all_questions_in_quiz() {
$cachetime = get_config('mooduell', 'cachetime');

if ($cachetime > 0) {

// Next we take a look in the cache.
$cache = cache::make('mod_mooduell', 'questionscache');

Expand All @@ -336,7 +335,6 @@ public function return_list_of_all_questions_in_quiz() {
$this->questions = $questions;
return $questions;
}

}

$questions = [];
Expand All @@ -347,7 +345,7 @@ public function return_list_of_all_questions_in_quiz() {
$newquestion = new question_control($entry, $listofanswers);

// Add empty combined feedback (for ddwtos questions) to prevent webservice errors.
$combinedfeedback = new stdClass;
$combinedfeedback = new stdClass();
$combinedfeedback->correctfeedback = null;
$combinedfeedback->partiallycorrectfeedback = null;
$combinedfeedback->incorrectfeedback = null;
Expand Down Expand Up @@ -418,6 +416,117 @@ private function return_list_of_questions() {
}
return $listofquestions;
}

/**
* Updates the platform subscription.
*
*
*/
public static function update_all_subscriptions() {

global $DB, $CFG;

// Get Subscriptions.
list($insqlplatform, $inparams1) = $DB->get_in_or_equal($CFG->wwwroot);
list($insqlproduct, $inparams2) = $DB->get_in_or_equal('unlockplatformsubscription');

$params = array_merge($inparams1, $inparams2);

$sql = "SELECT * FROM {mooduell_purchase}
WHERE platformid $insqlplatform
AND productid $insqlproduct";

$allpurchases = $DB->get_records_sql($sql, $params);
foreach ($allpurchases as $returnitem) {
$result = self::verify_purchase($returnitem);

// Logic to determine if subscription is okay or not
// Request was ok.
if ($result->ok === true) {
$allproductsinreceipt = $result->data->collection;
foreach ($allproductsinreceipt as $singleproduct) {
if ($singleproduct->id === 'unlockplatformsubscription') {
// Subscription item.
if ($singleproduct->isExpired === false) {
// Extend validity for a day.
$udpatedentry = $returnitem;
$udpatedentry->validuntil = time() + (60 * 60 * 24);
$DB->update_record('mooduell_purchase', $udpatedentry);
return;
} else if ($singleproduct->isExpired === true) {
// Delete Cancel etc.
$udpatedentry = $returnitem;
$udpatedentry->productid = 'notvalid';
$DB->update_record('mooduell_purchase', $udpatedentry);
}
}
}
} else {
// Failed verification.
// Android expired.
if ($result->code === 6778003) {
$udpatedentry = $returnitem;
$udpatedentry->productid = 'notvalid';
$DB->update_record('mooduell_purchase', $udpatedentry);
}
return;
}
}
}

/**
* Verifies a single purchase.
*
* @param mixed $purchase
* @return object
*/
public static function verify_purchase($purchase) {
// If sub has been purchases on ios.
if ($purchase->store === 'ios') {
$payload = [
'id' => 'at.wunderbyte.mooduellapp',
'type' => 'application',
'transaction' => [
'id' => 'at.wunderbyte.mooduellapp',
'type' => 'ios-appstore',
'appStoreReceipt' => $purchase->purchasetoken,
],
];
} else {
$payload = [
'id' => $purchase->productid,
'type' => $purchase->productid === 'unlockplatformsubscription' ? 'paid subscription' : 'consumeable',
'transaction' => [
'type' => 'android-playstore',
'id' => $purchase->orderid,
'purchaseToken' => $purchase->purchasetoken,
'signature' => $purchase->signature,
'receipt' => $purchase->receipt,
],
];
}

$url = "https://validator.iaptic.com/v1/validate";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER,
[
"Authorization: Basic " . base64_encode('at.wunderbyte.mooduellapp:4575a924-9af6-4a88-95d1-9c80aa1444b1'),
"Content-Type: application/json",
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responsedata = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return json_decode($responsedata);
}



/**
* Returns List of relevant Purchases
*
Expand All @@ -442,17 +551,12 @@ public static function get_purchases($courses, $quizzes) {
$returnitems = ['purchases' => []];
return $returnitems;
}
list($insqlcourses, $inparams) = $DB->get_in_or_equal($courseids);
list($insqlquizzes, $inparams2) = $DB->get_in_or_equal($quizids);
list($insqlplatform, $inparams3) = $DB->get_in_or_equal($CFG->wwwroot);

$params = array_merge($inparams, $inparams2, $inparams3);

$leeway = time() - (60 * 60 * 24 * 4);
list($insqlplatform, $inparams1) = $DB->get_in_or_equal($CFG->wwwroot);
$params = $inparams1;
$params[] = $leeway;
$sql = "SELECT * FROM {mooduell_purchase}
WHERE userid = {$userid}
OR courseid $insqlcourses
OR mooduellid $insqlquizzes AND ispublic = 1
OR platformid $insqlplatform";
WHERE platformid $insqlplatform AND validuntil > ? AND NOT productid = 'notvalid'";

$returnitems = ['purchases' => $DB->get_records_sql($sql, $params)];
return $returnitems;
Expand All @@ -471,8 +575,9 @@ public static function purchase_item($purchase) {
case 'unlockplatformsubscription':
if ($purchase['store'] == 'ios') {
// Ios.
$purchasetokenformatted = str_replace('~', '+', $purchase['purchasetoken']);
$existingsub = $DB->get_records('mooduell_purchase', [
'productid' => $purchase['productid'],
'purchasetoken' => $purchasetokenformatted,
'store' => 'ios',
]);
} else {
Expand Down Expand Up @@ -512,6 +617,8 @@ public static function purchase_item($purchase) {
}
$newdata = $purchase;
$newdata['timecreated'] = time();
// We check subscription every day.
$newdata['validuntil'] = time() + (60 * 60 * 24);
$manipulatedstring = $newdata['purchasetoken'];
if ($newdata['signature']) {
$manipulatedsignature = $newdata['signature'];
Expand Down
2 changes: 1 addition & 1 deletion classes/output/list_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function export_for_template(renderer_base $output) {
* @param int $counter
* @return array
*/
private function render_questions_table_for_game(stdClass $game, mooduell $mooduell, int $counter = null):array {
private function render_questions_table_for_game(stdClass $game, mooduell $mooduell, int $counter = null): array {
global $PAGE;

$tablename = bin2hex(random_bytes(12));
Expand Down
8 changes: 4 additions & 4 deletions classes/output/overview_student.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function export_for_template(renderer_base $output) {
* @param mooduell $mooduell
* @return string
*/
private function render_open_games_table(mooduell $mooduell):string {
private function render_open_games_table(mooduell $mooduell): string {
return $this->render_games_table($mooduell, 'opengames');
}

Expand All @@ -107,7 +107,7 @@ private function render_open_games_table(mooduell $mooduell):string {
* @param mooduell $mooduell
* @return string
*/
private function render_finished_games_table(mooduell $mooduell):string {
private function render_finished_games_table(mooduell $mooduell): string {
return $this->render_games_table($mooduell, 'finishedgames');
}

Expand All @@ -118,7 +118,7 @@ private function render_finished_games_table(mooduell $mooduell):string {
* @param string $action
* @return string
*/
private function render_games_table(mooduell $mooduell, string $action):string {
private function render_games_table(mooduell $mooduell, string $action): string {
$gamestable = new table_games($action, $mooduell->cm->id);

$finishedgames = $action == 'finishedgames' ? true : false;
Expand Down Expand Up @@ -150,7 +150,7 @@ private function render_games_table(mooduell $mooduell, string $action):string {
* @param mooduell $mooduell
* @return string
*/
private function render_highscores_table(mooduell $mooduell):string {
private function render_highscores_table(mooduell $mooduell): string {

$highscorestable = new table_highscores('highscores', $mooduell->cm->id);
// Sort the table by descending score by default.
Expand Down
10 changes: 5 additions & 5 deletions classes/output/overview_teacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function export_for_template(renderer_base $output) {
* @param mooduell $mooduell
* @return string
*/
private function render_open_games_table(mooduell $mooduell):string {
private function render_open_games_table(mooduell $mooduell): string {
return $this->render_games_table($mooduell, 'opengames');
}

Expand All @@ -101,7 +101,7 @@ private function render_open_games_table(mooduell $mooduell):string {
* @param mooduell $mooduell
* @return string
*/
private function render_finished_games_table(mooduell $mooduell):string {
private function render_finished_games_table(mooduell $mooduell): string {
return $this->render_games_table($mooduell, 'finishedgames');
}

Expand All @@ -112,7 +112,7 @@ private function render_finished_games_table(mooduell $mooduell):string {
* @param string $action
* @return string
*/
private function render_games_table(mooduell $mooduell, $action):string {
private function render_games_table(mooduell $mooduell, $action): string {

$tablename = bin2hex(random_bytes(12));

Expand Down Expand Up @@ -152,7 +152,7 @@ private function render_games_table(mooduell $mooduell, $action):string {
* @param mooduell $mooduell
* @return string
*/
private function render_highscores_table(mooduell $mooduell):string {
private function render_highscores_table(mooduell $mooduell): string {

$tablename = bin2hex(random_bytes(12));
$highscorestable = new table_highscores($tablename, $mooduell->cm->id);
Expand Down Expand Up @@ -191,7 +191,7 @@ private function render_highscores_table(mooduell $mooduell):string {
* @param mooduell $mooduell
* @return string
*/
private function render_questions_table(mooduell $mooduell):string {
private function render_questions_table(mooduell $mooduell): string {

$tablename = bin2hex(random_bytes(12));

Expand Down
4 changes: 2 additions & 2 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class provider implements
* @param collection $collection a reference to the collection to use to store the metadata.
* @return collection the updated collection of metadata items.
*/
public static function get_metadata(collection $collection) : collection {
public static function get_metadata(collection $collection): collection {
// Stores the mooduell game progress.
$collection->add_database_table(
'mooduell_games',
Expand Down Expand Up @@ -133,7 +133,7 @@ public static function get_metadata(collection $collection) : collection {
* @param int $userid the userid.
* @return contextlist the list of contexts containing user info for the user.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
public static function get_contexts_for_userid(int $userid): contextlist {

$contextlist = new contextlist();

Expand Down
8 changes: 4 additions & 4 deletions classes/question_control.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function __construct($data = null, $listofanswers = null) {
$this->questionid = $data->id;
$this->name = $data->name;
if ($data->qtype == 'ddwtos') {
$this->questiontext = trim(strip_tags(html_entity_decode($data->questiontext)));
$this->questiontext = trim(strip_tags(html_entity_decode($data->questiontext, ENT_QUOTES)));
} else {
$this->questiontext = $data->questiontext;
}
Expand Down Expand Up @@ -143,17 +143,17 @@ public function __construct($data = null, $listofanswers = null) {
// Remove HTML and decode HTML entities like " ".
if (!empty($combinedfeedback->correctfeedback)) {
$combinedfeedback->correctfeedback =
trim(strip_tags(html_entity_decode($combinedfeedback->correctfeedback)));
trim(strip_tags(html_entity_decode($combinedfeedback->correctfeedback, ENT_QUOTES)));
}

if (!empty($combinedfeedback->partiallycorrectfeedback)) {
$combinedfeedback->partiallycorrectfeedback =
trim(strip_tags(html_entity_decode($combinedfeedback->partiallycorrectfeedback)));
trim(strip_tags(html_entity_decode($combinedfeedback->partiallycorrectfeedback, ENT_QUOTES)));
}

if (!empty($combinedfeedback->incorrectfeedback)) {
$combinedfeedback->incorrectfeedback =
trim(strip_tags(html_entity_decode($combinedfeedback->incorrectfeedback)));
trim(strip_tags(html_entity_decode($combinedfeedback->incorrectfeedback, ENT_QUOTES)));
}
}
} else {
Expand Down
Loading

0 comments on commit 136edee

Please sign in to comment.