Skip to content

Commit

Permalink
Merge branch 'MDL-62822-sqlbyguidelines' into develop4
Browse files Browse the repository at this point in the history
  • Loading branch information
muqiuq committed Feb 6, 2019
2 parents 8d05fd3 + 2eb67a6 commit d017643
Show file tree
Hide file tree
Showing 12 changed files with 584 additions and 567 deletions.
42 changes: 21 additions & 21 deletions backup/moodle2/backup_studentquiz_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ protected function define_structure() {
array('id' => backup::VAR_ACTIVITYID, 'coursemodule' => backup::VAR_MODID));

// StudentQuiz Question meta of this StudentQuiz.
$questionsql = 'SELECT question.*'
.' FROM {studentquiz} sq'
.' JOIN {context} con ON( con.instanceid = sq.coursemodule )'
.' JOIN {question_categories} qc ON( qc.contextid = con.id )'
.' LEFT JOIN {question} q ON (q.category = qc.id )'
.' JOIN {studentquiz_question} question ON (question.questionid = q.id)'
.' WHERE sq.id = :studentquizid';
$questionsql = "SELECT question.*
FROM {studentquiz} sq
JOIN {context} con ON con.instanceid = sq.coursemodule
JOIN {question_categories} qc ON qc.contextid = con.id
LEFT JOIN {question} q ON q.category = qc.id
JOIN {studentquiz_question} question ON question.questionid = q.id
WHERE sq.id = :studentquizid";
$question->set_source_sql($questionsql, array('studentquizid' => backup::VAR_PARENTID));

// Define data sources with user data.
Expand All @@ -112,23 +112,23 @@ protected function define_structure() {
array('studentquizid' => backup::VAR_PARENTID));

// Only select rates to questions of this StudentQuiz.
$ratesql = 'SELECT rate.*'
.' FROM {studentquiz} sq'
.' JOIN {context} con ON( con.instanceid = sq.coursemodule )'
.' JOIN {question_categories} qc ON( qc.contextid = con.id )'
.' LEFT JOIN {question} q ON (q.category = qc.id )'
.' JOIN {studentquiz_rate} rate ON (rate.questionid = q.id)'
.' WHERE sq.id = :studentquizid';
$ratesql = "SELECT rate.*
FROM {studentquiz} sq
JOIN {context} con ON con.instanceid = sq.coursemodule
JOIN {question_categories} qc ON qc.contextid = con.id
LEFT JOIN {question} q ON q.category = qc.id
JOIN {studentquiz_rate} rate ON rate.questionid = q.id
WHERE sq.id = :studentquizid";
$rate->set_source_sql($ratesql, array('studentquizid' => backup::VAR_PARENTID));

// Only select comments to questions of this StudentQuiz.
$commentsql = 'SELECT comment.*'
.' FROM {studentquiz} sq'
.' JOIN {context} con ON( con.instanceid = sq.coursemodule )'
.' JOIN {question_categories} qc ON( qc.contextid = con.id )'
.' LEFT JOIN {question} q ON (q.category = qc.id )'
.' JOIN {studentquiz_comment} comment ON (comment.questionid = q.id)'
.' WHERE sq.id = :studentquizid';
$commentsql = "SELECT comment.*
FROM {studentquiz} sq
JOIN {context} con ON con.instanceid = sq.coursemodule
JOIN {question_categories} qc ON qc.contextid = con.id
LEFT JOIN {question} q ON q.category = qc.id
JOIN {studentquiz_comment} comment ON comment.questionid = q.id
WHERE sq.id = :studentquizid";
$comment->set_source_sql($commentsql, array('studentquizid' => backup::VAR_PARENTID));
}

Expand Down
122 changes: 69 additions & 53 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static function get_contexts_for_userid(int $userid) : contextlist {

// Get activity context if user created/modified the question or their data exist in these table
// base on user ID field: rate, comment, progress, practice, attempt.
$sql = 'SELECT DISTINCT ctx.id
$sql = "SELECT DISTINCT ctx.id
FROM {context} ctx
JOIN {studentquiz} sq ON sq.coursemodule = ctx.instanceid
AND contextlevel = :contextmodule
Expand All @@ -126,19 +126,23 @@ public static function get_contexts_for_userid(int $userid) : contextlist {
LEFT JOIN {studentquiz_practice} practice ON practice.studentquizcoursemodule = sq.coursemodule
LEFT JOIN {studentquiz_attempt} attempt ON attempt.categoryid = ca.id
AND attempt.studentquizid = sq.id
WHERE (question.id IS NOT NULL
OR rate.id IS NOT NULL
OR comment.id IS NOT NULL
OR progress.questionid IS NOT NULL
OR practice.id IS NOT NULL
OR attempt.id IS NOT NULL)
AND (q.createdby = :createduser
OR q.modifiedby = :modifieduser
OR rate.userid = :rateuser
OR comment.userid = :commentuser
OR progress.userid = :progressuser
OR practice.userid = :practiceuser
OR attempt.userid = :attemptuser)';
WHERE (
question.id IS NOT NULL
OR rate.id IS NOT NULL
OR comment.id IS NOT NULL
OR progress.questionid IS NOT NULL
OR practice.id IS NOT NULL
OR attempt.id IS NOT NULL
)
AND (
q.createdby = :createduser
OR q.modifiedby = :modifieduser
OR rate.userid = :rateuser
OR comment.userid = :commentuser
OR progress.userid = :progressuser
OR practice.userid = :practiceuser
OR attempt.userid = :attemptuser
)";

$params = [
'contextmodule' => CONTEXT_MODULE,
Expand Down Expand Up @@ -175,19 +179,19 @@ public static function export_user_data(approved_contextlist $contextlist) {

list($contextsql, $contextparam) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);

$sql = "SELECT DISTINCT ctx.id as contextid,
q.id as questionid, q.name as questionname, question.approved as questionapproved,
q.createdby as questioncreatedby, q.modifiedby as questionmodifiedby,
rate.id as rateid, rate.rate as raterate, rate.questionid as ratequestionid, rate.userid as rateuserid,
comment.id as commentid, comment.comment as commentcomment, comment.questionid as commentquestionid,
comment.userid as commentuserid, comment.created as commentcreate,
progress.questionid as progressquestionid, progress.userid as progressuserid,
progress.studentquizid as progressstudentquizid, progress.lastanswercorrect as progresslastanswercorrect,
progress.attempts as progressattempts, progress.correctattempts as progresscorrectattempts,
practice.id as practiceid, practice.quizcoursemodule as practicequizcoursemodule,
practice.studentquizcoursemodule as practicestudentquizcoursemodule, practice.userid as practiceuserid,
attempt.id as attemptid, attempt.studentquizid as attempstudentquizid,attempt.userid as attemptuserid,
attempt.questionusageid as attemptquestionusageid, attempt.categoryid as attemptcategoryid
$sql = "SELECT DISTINCT ctx.id AS contextid,
q.id AS questionid, q.name AS questionname, question.approved AS questionapproved,
q.createdby AS questioncreatedby, q.modifiedby AS questionmodifiedby,
rate.id AS rateid, rate.rate AS raterate, rate.questionid AS ratequestionid, rate.userid AS rateuserid,
comment.id AS commentid, comment.comment AS commentcomment, comment.questionid AS commentquestionid,
comment.userid AS commentuserid, comment.created AS commentcreate,
progress.questionid AS progressquestionid, progress.userid AS progressuserid,
progress.studentquizid AS progressstudentquizid, progress.lastanswercorrect AS progresslastanswercorrect,
progress.attempts AS progressattempts, progress.correctattempts AS progresscorrectattempts,
practice.id AS practiceid, practice.quizcoursemodule AS practicequizcoursemodule,
practice.studentquizcoursemodule AS practicestudentquizcoursemodule, practice.userid AS practiceuserid,
attempt.id AS attemptid, attempt.studentquizid AS attempstudentquizid,attempt.userid AS attemptuserid,
attempt.questionusageid AS attemptquestionusageid, attempt.categoryid AS attemptcategoryid
FROM {context} ctx
JOIN {studentquiz} sq ON sq.coursemodule = ctx.instanceid
AND contextlevel = :contextmodule
Expand All @@ -201,19 +205,23 @@ public static function export_user_data(approved_contextlist $contextlist) {
LEFT JOIN {studentquiz_practice} practice ON practice.studentquizcoursemodule = sq.coursemodule
LEFT JOIN {studentquiz_attempt} attempt ON attempt.categoryid = ca.id
AND attempt.studentquizid = sq.id
WHERE (question.id IS NOT NULL
OR rate.id IS NOT NULL
OR comment.id IS NOT NULL
OR progress.questionid IS NOT NULL
OR practice.id IS NOT NULL
OR attempt.id IS NOT NULL)
AND (q.createdby = :createduser
OR q.modifiedby = :modifieduser
OR rate.userid = :rateuser
OR comment.userid = :commentuser
OR progress.userid = :progressuser
OR practice.userid = :practiceuser
OR attempt.userid = :attemptuser)
WHERE (
question.id IS NOT NULL
OR rate.id IS NOT NULL
OR comment.id IS NOT NULL
OR progress.questionid IS NOT NULL
OR practice.id IS NOT NULL
OR attempt.id IS NOT NULL
)
AND (
q.createdby = :createduser
OR q.modifiedby = :modifieduser
OR rate.userid = :rateuser
OR comment.userid = :commentuser
OR progress.userid = :progressuser
OR practice.userid = :practiceuser
OR attempt.userid = :attemptuser
)
AND ctx.id {$contextsql}
ORDER BY ctx.id ASC";

Expand Down Expand Up @@ -335,11 +343,13 @@ public static function delete_data_for_all_users_in_context(\context $context) {
}

// Query to get all question ID belong to this module context.
$sql = 'SELECT q.id
$sql = "SELECT q.id
FROM {question} q
WHERE q.category IN (SELECT id
FROM {question_categories} c
WHERE c.contextid = :contextid)';
WHERE q.category IN (
SELECT id
FROM {question_categories} c
WHERE c.contextid = :contextid
)";

$params = [
'contextid' => $context->id
Expand Down Expand Up @@ -393,9 +403,11 @@ public static function delete_data_for_all_users_in_context(\context $context) {

// Delete attempts belong to this context.
$DB->execute("DELETE FROM {studentquiz_attempt}
WHERE studentquizid IN (SELECT id
FROM {studentquiz}
WHERE coursemodule = :coursemodule)", [
WHERE studentquizid IN (
SELECT id
FROM {studentquiz}
WHERE coursemodule = :coursemodule
)", [
'coursemodule' => $context->instanceid
]);
}
Expand Down Expand Up @@ -423,9 +435,11 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
// Query to get all question ID belong to the course modules.
$sql = "SELECT q.id
FROM {question} q
WHERE q.category IN (SELECT id
FROM {question_categories} c
WHERE c.contextid {$contextsql})";
WHERE q.category IN (
SELECT id
FROM {question_categories} c
WHERE c.contextid {$contextsql}
)";

$records = $DB->get_records_sql($sql, $contextparam);

Expand Down Expand Up @@ -483,9 +497,11 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
// Delete attempts belong to user within approved context.
$DB->execute("DELETE FROM {studentquiz_attempt}
WHERE userid = :userid
AND studentquizid IN (SELECT id
FROM {studentquiz}
WHERE coursemodule {$studentquizsql})", [
AND studentquizid IN (
SELECT id
FROM {studentquiz}
WHERE coursemodule {$studentquizsql}
)", [
'userid' => $userid
] + $sudentquizparams);
}
Expand Down
5 changes: 4 additions & 1 deletion classes/question/bank/approved_column.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ protected function display_content($question, $rowclasses) {
* @return array modified select left join
*/
public function get_extra_joins() {
return array('ap' => ' LEFT JOIN (SELECT questionid, approved FROM {studentquiz_question}) ap ON ap.questionid = q.id');
return array('ap' => " LEFT JOIN (
SELECT questionid, approved
FROM {studentquiz_question}
) ap ON ap.questionid = q.id");
}

/**
Expand Down
8 changes: 5 additions & 3 deletions classes/question/bank/comments_column.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ protected function display_content($question, $rowclasses) {
* @return array modified select left join
*/
public function get_extra_joins() {
return array('co' => 'LEFT JOIN ('
. 'SELECT COUNT(comment) as comment'
. ', questionid FROM {studentquiz_comment} GROUP BY questionid) co ON co.questionid = q.id');
return array('co' => "LEFT JOIN (
SELECT COUNT(comment) AS comment, questionid
FROM {studentquiz_comment}
GROUP BY questionid
) co ON co.questionid = q.id");
}

/**
Expand Down
Loading

0 comments on commit d017643

Please sign in to comment.