Skip to content

Commit

Permalink
Merge branch 'refactoring_optimization'
Browse files Browse the repository at this point in the history
  • Loading branch information
srobotta committed Dec 13, 2024
2 parents dcde1b1 + 80eac59 commit 4d7c139
Show file tree
Hide file tree
Showing 27 changed files with 729 additions and 415 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/moodle-plugin-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ jobs:
- php: 8.1
moodle-branch: MOODLE_404_STABLE
database: pgsql
- php: 8.1
moodle-branch: MOODLE_405_STABLE
database: mariadb
- php: 8.1
moodle-branch: MOODLE_405_STABLE
database: pgsql
- php: 8.2
moodle-branch: MOODLE_402_STABLE
database: mariadb
Expand All @@ -85,12 +91,24 @@ jobs:
- php: 8.2
moodle-branch: MOODLE_404_STABLE
database: pgsql
- php: 8.2
moodle-branch: MOODLE_405_STABLE
database: mariadb
- php: 8.2
moodle-branch: MOODLE_405_STABLE
database: pgsql
- php: 8.3
moodle-branch: MOODLE_404_STABLE
database: mariadb
- php: 8.3
moodle-branch: MOODLE_404_STABLE
database: pgsql
- php: 8.3
moodle-branch: MOODLE_405_STABLE
database: mariadb
- php: 8.3
moodle-branch: MOODLE_405_STABLE
database: pgsql

steps:
- name: Check out repository code
Expand Down
72 changes: 48 additions & 24 deletions backup/moodle2/backup_verbalfeedback_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,35 @@ protected function define_structure() {
$category = new backup_nested_element('category', ['id'], ['instanceid', 'paramtemplatecategoryid', 'position', 'weight']);

$categoryheaders = new backup_nested_element('categoryheaders');
$categoryheader = new backup_nested_element('categoryheader', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$categoryheader = new backup_nested_element('categoryheader', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$criteria = new backup_nested_element('criteria');
$criterion = new backup_nested_element('criterion', ['id'],
['paramtemplatecriterionid', 'categoryid', 'position', 'weight']);

$criteriontexts = new backup_nested_element('criteriontexts');
$criteriontext = new backup_nested_element('criteriontext', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$criteriontext = new backup_nested_element('criteriontext', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$subratings = new backup_nested_element('subratings');
$subrating = new backup_nested_element('subrating', ['id'], ['criterionid']);

$subratingtitles = new backup_nested_element('titles');
$subratingtitle = new backup_nested_element('title', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$subratingtitle = new backup_nested_element('title', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$subratingdescriptions = new backup_nested_element('descriptions');
$subratingdescription = new backup_nested_element('description', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$subratingdescription = new backup_nested_element('description', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$subratingverynegatives = new backup_nested_element('verynegatives');
$subratingverynegative = new backup_nested_element('verynegative', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$subratingverynegative = new backup_nested_element('verynegative', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$subratingnegatives = new backup_nested_element('negatives');
$subratingnegative = new backup_nested_element('negative', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$subratingnegative = new backup_nested_element('negative', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$subratingpositives = new backup_nested_element('positives');
$subratingpositive = new backup_nested_element('positive', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$subratingpositive = new backup_nested_element('positive', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$subratingverypositives = new backup_nested_element('verypositives');
$subratingverypositive = new backup_nested_element('verypositive', ['id'], ['foreignkey', 'type', 'languageid', 'string']);
$subratingverypositive = new backup_nested_element('verypositive', ['id'], ['foreignkey', 'typeid', 'languageid', 'string']);

$submissions = new backup_nested_element('submissions');
$submission = new backup_nested_element('submission', ['id'], ['instanceid', 'fromuserid', 'touserid', 'status',
Expand Down Expand Up @@ -144,26 +144,50 @@ protected function define_structure() {
$language->set_source_table(tables::LANGUAGE_TABLE, []);

$category->set_source_table(tables::INSTANCE_CATEGORY_TABLE, ['instanceid' => backup::VAR_PARENTID], 'id ASC');
$categoryheader->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_CATEGORY_HEADER), ]);
$categoryheader->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_CATEGORY_HEADER)
), ]);

$criterion->set_source_table(tables::INSTANCE_CRITERION_TABLE, ['categoryid' => backup::VAR_PARENTID], 'id ASC');
$criteriontext->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_CRITERION), ]);
$criteriontext->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_CRITERION)
), ]);

$subrating->set_source_table(tables::INSTANCE_SUBRATING_TABLE, ['criterionid' => backup::VAR_PARENTID], 'id ASC');
$subratingtitle->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_SUBRATING_TITLE), ]);
$subratingdescription->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_SUBRATING_DESCRIPTION), ]);
$subratingverynegative->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_SUBRATING_VERY_NEGATIVE), ]);
$subratingnegative->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_SUBRATING_NEGATIVE), ]);
$subratingpositive->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_SUBRATING_POSITIVE), ]);
$subratingverypositive->set_source_table(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => backup::VAR_PARENTID,
'type' => backup_helper::is_sqlparam(localized_string_type::INSTANCE_SUBRATING_VERY_POSITIVE), ]);
$subratingtitle->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_SUBRATING_TITLE)
), ]);
$subratingdescription->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_SUBRATING_DESCRIPTION)
), ]);
$subratingverynegative->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_SUBRATING_VERY_NEGATIVE)
), ]);
$subratingnegative->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_SUBRATING_NEGATIVE)
), ]);
$subratingpositive->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_SUBRATING_POSITIVE)
), ]);
$subratingverypositive->set_source_table(tables::LOCALIZED_STRING_TABLE, [
'foreignkey' => backup::VAR_PARENTID,
'typeid' => backup_helper::is_sqlparam(
localized_string_type::str2id(localized_string_type::INSTANCE_SUBRATING_VERY_POSITIVE)
), ]);

// All the rest of elements only happen if we are including user info.
if ($userinfo) {
Expand Down
14 changes: 11 additions & 3 deletions backup/moodle2/restore_verbalfeedback_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use mod_verbalfeedback\repository\model\localized_string_type;
use mod_verbalfeedback\repository\tables;

/**
Expand All @@ -33,6 +34,13 @@
*/
class restore_verbalfeedback_activity_structure_step extends restore_activity_structure_step {

/**
* The instance id when the item is newly inserted.
*
* @var int
*/
private $instanceid;

/**
* Function that will return the structure to be processed by this restore_step.
* Must return one array of @restore_path_element elements
Expand Down Expand Up @@ -108,9 +116,9 @@ protected function process_verbalfeedback($data) {
}

// Insert the verbal feedback record.
$newitemid = $DB->insert_record(tables::INSTANCE_TABLE, $data);
$this->instanceid = $DB->insert_record(tables::INSTANCE_TABLE, $data);
// Immediately after inserting "activity" record, call this.
$this->apply_activity_instance($newitemid);
$this->apply_activity_instance($this->instanceid);
}

/**
Expand Down Expand Up @@ -326,8 +334,8 @@ private function process_localized_string($foreigenkeymapping, $data) {

$data = (object) $data;
$data->foreignkey = $this->get_mappingid($foreigenkeymapping, $data->foreignkey);

$data->languageid = $this->get_mappingid('language', $data->languageid);
$data->instanceid = $this->instanceid;
$DB->insert_record('verbalfeedback_local_string', $data);
}
}
122 changes: 53 additions & 69 deletions classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,14 @@
use dml_exception;
use Exception;
use moodle_exception;
use stdClass;
use core_date;
use mod_verbalfeedback\api\gradebook;
use mod_verbalfeedback\model\response;
use mod_verbalfeedback\model\submission;
use mod_verbalfeedback\model\submission_status;
use mod_verbalfeedback\repository\instance_repository;
use mod_verbalfeedback\repository\submission_repository;
use mod_verbalfeedback\repository\tables;
use mod_verbalfeedback\utils\user;
use mod_verbalfeedback\utils\instance;
use mod_verbalfeedback\utils\user_utils;

// Quick hack for issue #43.
ini_set('memory_limit', '256M');

/**
* Class for performing DB actions for the verbal feedback activity module.
Expand All @@ -53,13 +46,6 @@
*/
class api {

/** The instance database table. */
const DB_INSTANCE = "verbalfeedback";
/** The response database table. */
const DB_RESPONSE = "verbalfeedback_response";
/** The submission database table. */
const DB_SUBMISSION = "verbalfeedback_submission";

/** Status when a user has not yet provided feedback to another user. */
const STATUS_PENDING = 0;
/** Status when a user has begun providing feedback to another user. */
Expand Down Expand Up @@ -101,8 +87,8 @@ public static function get_instance($verbalfeedbackid) {
global $DB;

$id = (int)$verbalfeedbackid;
if (!array_key_exists($id, static::$instances)) {
static::$instances[$id] = $DB->get_record('verbalfeedback', ['id' => $id], '*', MUST_EXIST);
if (!array_key_exists($id, static::$instances) && !PHPUNIT_TEST) {
static::$instances[$id] = $DB->get_record(tables::INSTANCE_TABLE, ['id' => $id], '*', MUST_EXIST);
}
return static::$instances[$id];
}
Expand All @@ -127,14 +113,19 @@ public static function is_ready($verbalfeedbackid) {
*/
public static function get_instance_by_itemid($itemid) {
global $DB;
return $DB->get_record_sql("SELECT *
FROM {" . tables::INSTANCE_TABLE .
"} WHERE id = (SELECT instanceid
FROM {" . tables::INSTANCE_CATEGORY_TABLE .
"} WHERE id = (SELECT categoryid
FROM {" . tables::INSTANCE_CRITERION_TABLE .
"} WHERE id = ?))",
[$itemid], IGNORE_MISSING);
$id = (int)$itemid;
static $instances = [];
if (isset($instances[$id])) {
return $instances[$id];
}
$instances[$id] = $DB->get_record_sql(sprintf('
SELECT vf.* FROM {%s} vf
INNER JOIN {%s} cat ON cat.instanceid = vf.id
INNER JOIN {%s} crit ON crit.categoryid = cat.id
WHERE crit.id = ?',
tables::INSTANCE_TABLE, tables::INSTANCE_CATEGORY_TABLE, tables::INSTANCE_CRITERION_TABLE),
[$id], IGNORE_MISSING);
return $instances[$id];
}

/**
Expand All @@ -146,12 +137,18 @@ public static function get_instance_by_itemid($itemid) {
*/
public static function get_instance_by_categoryid($categoryid) {
global $DB;
return $DB->get_record_sql("SELECT *
FROM {" . tables::INSTANCE_TABLE .
"} WHERE id = (SELECT instanceid
FROM {" . tables::INSTANCE_CATEGORY_TABLE .
"} WHERE id = ?)",
[$categoryid], IGNORE_MISSING);
$id = (int)$categoryid;
static $instances = [];
if (isset($instances[$id])) {
return $instances[$id];
}
$instances[$id] = $DB->get_record_sql(sprintf('
SELECT vf.* FROM {%s} vf
INNER JOIN {%s} cat ON cat.instanceid = vf.id
WHERE cat.id = ?',
tables::INSTANCE_TABLE, tables::INSTANCE_CATEGORY_TABLE),
[$id], IGNORE_MISSING);
return $instances[$id];
}

/**
Expand Down Expand Up @@ -237,7 +234,7 @@ public static function generate_verbalfeedback_feedback_states($verbalfeedbackid
global $DB;
$submissionrepo = new submission_repository();

$verbalfeedback = $DB->get_record(self::DB_INSTANCE, ['id' => $verbalfeedbackid], '*', MUST_EXIST);
$verbalfeedback = self::get_instance($verbalfeedbackid);
$wheres = [
'u.id NOT IN (
SELECT fs.touserid
Expand Down Expand Up @@ -309,42 +306,29 @@ function($user) {
*/
public static function get_scales() {

$s0 = new stdClass();
$s0->scale = null;
$s0->scalelabel = get_string('notapplicableabbr', 'verbalfeedback');
$s0->description = get_string('scalenotapplicable', 'mod_verbalfeedback');

$s1 = new stdClass();
$s1->scale = 0;
$s1->scalelabel = '0';
$s1->description = get_string('scalestronglydisagree', 'mod_verbalfeedback');

$s2 = new stdClass();
$s2->scale = 1;
$s2->scalelabel = '1';
$s2->description = get_string('scaledisagree', 'mod_verbalfeedback');

$s3 = new stdClass();
$s3->scale = 2;
$s3->scalelabel = '2';
$s3->description = get_string('scalesomewhatdisagree', 'mod_verbalfeedback');

$s4 = new stdClass();
$s4->scale = 3;
$s4->scalelabel = '3';
$s4->description = get_string('scalesomewhatagree', 'mod_verbalfeedback');

$s5 = new stdClass();
$s5->scale = 4;
$s5->scalelabel = '4';
$s5->description = get_string('scaleagree', 'mod_verbalfeedback');

$s6 = new stdClass();
$s6->scale = 5;
$s6->scalelabel = '5';
$s6->description = get_string('scalestronglyagree', 'mod_verbalfeedback');

return [$s1, $s2, $s3, $s4, $s5, $s6, $s0];
$wordscale = [
'scalestronglydisagree',
'scaledisagree',
'scalesomewhatdisagree',
'scalesomewhatagree',
'scaleagree',
'scalestronglyagree',
];
$scales = [];
for ($i = 0; $i <= 5; $i++) {
$scales[] = (object)[
'scale' => $i,
'scalelabel' => '' . $i,
'description' => get_string($wordscale[$i], 'mod_verbalfeedback'),
];
}
$scales[] = (object)[
'scale' => null,
'scalelabel' => get_string('notapplicableabbr', 'verbalfeedback'),
'description' => get_string('scalenotapplicable', 'mod_verbalfeedback'),
];

return $scales;
}

/**
Expand Down Expand Up @@ -463,7 +447,7 @@ public static function count_users_awaiting_feedback($verbalfeedbackid, $user) {

// Check first if the user can write feedback to other participants.
if (user_utils::can_respond($verbalfeedback, $user) === true) {
if (!$DB->record_exists(self::DB_SUBMISSION, ['instance' => $verbalfeedback->id, 'fromuser' => $user])) {
if (!$DB->record_exists(tables::SUBMISSION_TABLE, ['instance' => $verbalfeedback->id, 'fromuser' => $user])) {
// Generate submission records if there are no submission records yet.
self::generate_verbalfeedback_feedback_states($verbalfeedback->id, $user, $verbalfeedback->with_self_review);
}
Expand All @@ -473,7 +457,7 @@ public static function count_users_awaiting_feedback($verbalfeedbackid, $user) {
$select = "instance = :verbalfeedback AND fromuser = :fromuser AND status $insql";
$params['verbalfeedback'] = $verbalfeedbackid;
$params['fromuser'] = $user;
return $DB->count_records_select(self::DB_SUBMISSION, $select, $params);
return $DB->count_records_select(tables::SUBMISSION_TABLE, $select, $params);
}

return 0;
Expand Down
Loading

0 comments on commit 4d7c139

Please sign in to comment.