From dafbb18956955be69bf506de15ddf89349b81a3b Mon Sep 17 00:00:00 2001 From: Nick Freear Date: Thu, 8 Mar 2018 10:49:47 +0000 Subject: [PATCH] Bug #8, Add `un_consent` `un_complete` `get..completion` [iet:10314222] * Add PHP methods; fix `debug()` calls [iet:10314221][iet:10314088][ci skip] --- classes/local/base.php | 2 + classes/local/conditional_embedded_survey.php | 68 +++++++++++++++---- classes/local/tesla_consent.php | 18 ++++- 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/classes/local/base.php b/classes/local/base.php index 8199834..3172e41 100644 --- a/classes/local/base.php +++ b/classes/local/base.php @@ -13,6 +13,8 @@ class base { + const BASE_KEY = 'auth_ouopenid'; + /** Output arbitrary data, eg. to HTTP header. */ public static function debug($obj) { diff --git a/classes/local/conditional_embedded_survey.php b/classes/local/conditional_embedded_survey.php index 094303f..e59a80e 100644 --- a/classes/local/conditional_embedded_survey.php +++ b/classes/local/conditional_embedded_survey.php @@ -15,7 +15,7 @@ class conditional_embedded_survey extends base { - const CONFIG_KEY = 'auth_ouopenid_conditional_survey_activity'; + const CONFIG_KEY = self::BASE_KEY . '_conditional_survey_activity'; const MOD_TYPE_NAME = 'assign'; // 'mod/assign' const MOD_TYPE_ID = 1; // ID in 'mdl_modules' table. const EMBED_LIKE = '%%'; // MySQL 'LIKE' @@ -28,6 +28,10 @@ class conditional_embedded_survey extends base { protected $userid; protected $config; + /** + * @param string $course_code Course shortname, example, 'FR' or 'TPT'. + * @param int $userid Moodle user ID (or username). + */ public function __construct( $course_code, $userid = null ) { $this->set_config( $course_code, $userid ); } @@ -49,17 +53,27 @@ protected function set_config( $course_code, $userid = null ) { $this->userid = $userid ? $userid : $USER->id; - self::_debug([ __METHOD__, $config, $this->userid ]); + self::debug([ __METHOD__, $config, $this->userid ]); + } else { + throw new \Exception(sprintf( 'Missing course code in configuration: \$CFG->%s, %s', self::CONFIG_KEY, $course_code )); } } public function make_complete() { if ($this->is_valid_module() && $this->activity_has_embed()) { - // return false; $this->assign_grades(); $this->assign_submission(); - return $this->course_module_completion(); + return $this->course_module_complete(); + } + return false; + } + + public function un_complete() { + if ($this->is_valid_module() && $this->activity_has_embed()) { + $this->un_assign_grades(); + $this->un_assign_submission(); + return $this->course_module_un_complete(); } return false; } @@ -70,8 +84,7 @@ protected function is_valid_module() { $count = $DB->count_records( 'course_modules', [ 'id' => $this->cmid, 'module' => self::MOD_TYPE_ID, 'course' => $this->course_id, 'instance' => $this->activity_id ]); - self::_debug([ __METHOD__, $count ]); // '1' ?? - + self::debug([ __METHOD__, $count ]); // '1' ?? return 1 === $count; } @@ -81,8 +94,7 @@ protected function activity_has_embed() { $result = $DB->get_record_sql( 'SELECT * FROM {assign} WHERE ' . $DB->sql_like( 'intro', ':intro' ) . ' AND id = :id ', [ 'intro' => self::EMBED_LIKE, 'id' => $this->activity_id ]); - self::_debug([ __METHOD__, $result ]); - + self::debug([ __METHOD__, $result ]); return $result; } @@ -99,7 +111,15 @@ protected function assign_grades() { 'attemptnumber' => 0, ], false); - self::_debug([ __METHOD__, $lastinsertid ]); + self::debug([ __METHOD__, $lastinsertid ]); + } + + protected function un_assign_grades() { + global $DB; + return $DB->delete_records('assign_grades', [ + 'assignment' => $this->activity_id, + 'userid' => $this->userid, + ]); } protected function assign_submission() { @@ -116,10 +136,18 @@ protected function assign_submission() { 'latest' => 1, // 'true' ], false); - self::_debug([ __METHOD__, $lastinsertid ]); + self::debug([ __METHOD__, $lastinsertid ]); } - protected function course_module_completion() { + protected function un_assign_submission() { + global $DB; + return $DB->delete_records('assign_submission', [ + 'assignment' => $this->activity_id, + 'userid' => $this->userid, + ]); + } + + protected function course_module_complete() { global $DB; // Moodle global. $lastinsertid = $DB->insert_record('course_module_completion', (object) [ @@ -130,6 +158,22 @@ protected function course_module_completion() { 'timemodified' => time(), // UNIX_TIMESTAMP() ], false); - self::_debug([ __METHOD__, $lastinsertid ]); + self::debug([ __METHOD__, $lastinsertid ]); + } + + protected function course_module_un_complete() { + global $DB; + return $DB->delete_records('course_module_completion', [ + 'coursemoduleid' => $this->cmid, + 'userid' => $this->userid, + ]); + } + + public function get_course_module_completion() { + global $DB; + return $DB->get_records('course_module_completion', [ + 'coursemoduleid' => $this->cmid, + 'userid' => $this->userid, + ]); } } diff --git a/classes/local/tesla_consent.php b/classes/local/tesla_consent.php index a351544..15e62fd 100644 --- a/classes/local/tesla_consent.php +++ b/classes/local/tesla_consent.php @@ -41,6 +41,11 @@ public static function agree( $userid = null, $version = self::AGREE_VERSION ) { $userid = $userid ? $userid : $USER->id; + if (self::has_agreed( $userid )) { + self::debug([ __METHOD__, 'Already agreed.', $userid ]); + return false; + } + $lastinsertid = $DB->insert_record(self::DB_TABLE, (object) [ 'userid' => $userid, 'timesubmitted' => time(), // UNIX_TIMESTAMP() @@ -48,11 +53,22 @@ public static function agree( $userid = null, $version = self::AGREE_VERSION ) { 'accepted' => true, ], false); - self::_debug([ __METHOD__, $lastinsertid ]); + self::debug([ __METHOD__, $lastinsertid ]); return $lastinsertid; } + /** Delete the consent / agreement. + * @param int $userid + */ + public static function un_consent( $userid = null ) { + global $DB, $USER; // Moodle global. + + $userid = $userid ? $userid : $USER->id; + + return $DB->delete_records(self::DB_TABLE, [ 'userid' => $userid ]); + } + /* [1241] => stdClass Object ( [id] => 1241 [userid] => 5