Skip to content

Commit

Permalink
Bug #8, Add set_grade_grades(); reimplement `course_modules_complet…
Browse files Browse the repository at this point in the history
…e()`

* [iet:10314222][ci skip]
  • Loading branch information
nfreear committed Mar 9, 2018
1 parent dc370fa commit f5db8ed
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
1 change: 1 addition & 0 deletions classes/local/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class base {
* @return mixed Configuration item (object, array, string...)
*/
public static function config($key, $default = null) {
global $CFG; // Moodle global.
if (isset($CFG->{ 'auth_ouopenid_' . $key })) {
return $CFG->{ 'auth_ouopenid_' . $key };
}
Expand Down
63 changes: 53 additions & 10 deletions classes/local/conditional_embedded_survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class conditional_embedded_survey extends base {
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 GRADER_USER_ID = 2; // User ID, not '0' !!
// const EMBED_LIKE = '%</iframe>%'; // MySQL 'LIKE'
const EMBED_REGEXP = '(<\/iframe>|[?#!]-pre-survey-embed)'; // MySQL 'REGEXP'

protected $course_id; // 4,
protected $course_code; // 'FR',
protected $cmid; // 72,
protected $activity_id; // 'assign.id' = 13,
protected $grade_items_id; // 47,

protected $userid;
protected $config;
Expand All @@ -51,6 +53,7 @@ protected function set_config( $course_code, $userid = null ) {
$this->course_code = $course_code;
$this->cmid = $config->cmid;
$this->activity_id = $config->activity_id;
$this->grade_items_id = $config->grade_items_id;

$this->userid = $userid ? $userid : $USER->id;

Expand All @@ -66,14 +69,17 @@ public function make_complete() {
try {
$this->assign_grades();
$this->assign_submission();
$b_ok = $this->course_module_complete();
} catch (\dml_write_exception $ex) {
self::debug([ __FUNCTION__, 'dml_write_exception', $ex->getMessage(), $ex->debuginfo ]);

if (! preg_match('/Duplicate entry .+/', $ex->debuginfo)) {
throw $ex;
}
}
$b_ok = $this->course_modules_complete();

$this->set_grade_grades();

return $b_ok;
}
return false;
Expand All @@ -83,7 +89,7 @@ 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 $this->course_modules_un_complete();
}
return false;
}
Expand Down Expand Up @@ -119,7 +125,7 @@ protected function assign_grades() {
'userid' => $this->userid,
'timecreated' => time(), // UNIX_TIMESTAMP()
'timemodified' => time(),
'grader' => 0,
'grader' => self::GRADER_USER_ID,
'grade' => 100.00,
'attemptnumber' => 0,
], false);
Expand Down Expand Up @@ -160,31 +166,68 @@ protected function un_assign_submission() {
]);
}

protected function course_module_complete() {
protected function course_modules_complete() {
// https://docs.moodle.org/dev/Activity_completion_API#Notifying_the_completion_system
// https://github.com/moodle/moodle/blob/master/lib/completionlib.php#L532-L565
// https://github.com/moodle/moodle/blob/master/lib/modinfolib.php#L1835

$cminfo = \cm_info::create( (object) [ 'id' => $this->cmid, 'course' => $this->course_id ], $this->userid );

$completion = new \completion_info(\get_course($this->course_id));
// $result = $completion->update_state($cminfo, COMPLETION_COMPLETE, $this->userid); // , $override = true);

$data = $completion->get_data($cminfo, false, $this->userid);
$data->completionstate = COMPLETION_COMPLETE;
$data->timemodified = time();
$data->overrideby = $this->userid;
$completion->internal_set_data($cminfo, $data);

self::debug([ __FUNCTION__, 'completion->internal_set_data() (update_state)', $result ]);
}

protected function course_modules_complete_OLD() {
global $DB; // Moodle global.

$lastinsertid = $DB->insert_record('course_module_completion', (object) [
$lastinsertid = $DB->insert_record('course_modules_completion', (object) [
'coursemoduleid' => $this->cmid,
'userid' => $this->userid,
'compeletionstate' => 1, // 'true'
'completionstate' => 1, // 'true'
'viewed' => 0, // 'false'
'timemodified' => time(), // UNIX_TIMESTAMP()
], false);

self::debug([ __METHOD__, $lastinsertid ]);
}

protected function course_module_un_complete() {
protected function course_modules_un_complete() {
global $DB;
return $DB->delete_records('course_module_completion', [
return $DB->delete_records('course_modules_completion', [
'coursemoduleid' => $this->cmid,
'userid' => $this->userid,
]);
}

public function get_course_module_completion() {
protected function set_grade_grades() {
global $DB;
$grade = $DB->get_record('grade_grades', [
'itemid' => $this->grade_items_id,
'userid' => $this->userid,
]);
self::debug([ __METHOD__, 'get', $grade ]);
if ($grade) {
$grade->rawgrade = 100.00;
$grade->finalgrade = 100.00;
$grade->timemodified = time();

$DB->update_record('grade_grades', $grade);

self::debug([ __METHOD__, 'update', $grade ]);
}
}

public function get_course_modules_completion() {
global $DB;
return $DB->get_records('course_module_completion', [
return $DB->get_record('course_modules_completion', [
'coursemoduleid' => $this->cmid,
'userid' => $this->userid,
]);
Expand Down
10 changes: 7 additions & 3 deletions survey-end/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
require_once __DIR__ . '/../classes/local/conditional_embedded_survey.php';
require_once __DIR__ . '/../classes/local/tesla_consent.php';

require_once($CFG->dirroot . '/lib/completionlib.php');
require_once($CFG->dirroot . '/lib/modinfolib.php');

use auth_ouopenid\local\conditional_embedded_survey;
use auth_ouopenid\local\tesla_consent;
use auth_ouopenid\local\base;

define( 'OUOP_STRING', 'auth_ouopenid' );

// TODO: check if plugin is enabled or not !!

class Ou_Open_Id_Survey_End {
class Ou_Open_Id_Survey_End extends base {

const JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';

Expand Down Expand Up @@ -127,7 +131,7 @@ public static function complete_conditional() {
'course_code' => Ou_Open_Id_Survey_End::get_return_code(),
'cond_completed' => $conditional_completed,
'agreement_id' => $agreement_id,
'redirects' => $CFG->auth_ouopenid_redirects,
'redirects' => Ou_Open_Id_Survey_End::config('redirects'),
'hash' => '#section-3',
'timeout' => 3000,
'other' => 1,
Expand All @@ -140,7 +144,7 @@ public static function complete_conditional() {
<script src="/auth/ouopenid/user/ouop-analytics.js<?php Ou_Open_Id_Survey_End::versionParam() ?>"></script>
<script src="/auth/ouopenid/js/survey-end.js<?php Ou_Open_Id_Survey_End::versionParam() ?>"></script>
<script>
OUOP.analytics($, { config: { ga: <?php echo json_encode($CFG->auth_ouopenid_js_config[ 'ga' ]) ?> } });
OUOP.analytics($, { config: { ga: <?php echo json_encode(Ou_Open_Id_Survey_End::config('js_config')[ 'ga' ]) ?> } });
</script>
</body>
</html>

0 comments on commit f5db8ed

Please sign in to comment.