Skip to content

Commit

Permalink
Bug #8, Add un_consent un_complete get..completion [iet:10314222]
Browse files Browse the repository at this point in the history
* Add PHP methods; fix `debug()` calls [iet:10314221][iet:10314088][ci skip]
  • Loading branch information
nfreear committed Mar 8, 2018
1 parent 1e0d559 commit dafbb18
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
2 changes: 2 additions & 0 deletions classes/local/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class base {

const BASE_KEY = 'auth_ouopenid';

/** Output arbitrary data, eg. to HTTP header.
*/
public static function debug($obj) {
Expand Down
68 changes: 56 additions & 12 deletions classes/local/conditional_embedded_survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '%</iframe>%'; // MySQL 'LIKE'
Expand All @@ -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 );
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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() {
Expand All @@ -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) [
Expand All @@ -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,
]);
}
}
18 changes: 17 additions & 1 deletion classes/local/tesla_consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,34 @@ 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()
'version' => $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
Expand Down

0 comments on commit dafbb18

Please sign in to comment.