Skip to content

Commit

Permalink
Bug #8, conditional_embedded_survey class [iet:10306790]
Browse files Browse the repository at this point in the history
  • Loading branch information
nfreear committed Feb 19, 2018
1 parent c0534db commit 16c747e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
142 changes: 142 additions & 0 deletions classes/local/conditional_embedded_survey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* "Complete" a "mod/assign" conditional activity containing a survey embedded via <IFRAME>.
*
* @package auth_ouopenid
* @author Nick Freear, 19-February-2018.
* @copyright © 2018 The Open University.
*
* @link https://docs.moodle.org/dev/Data_manipulation_API#Inserting_Records
*/

namespace auth_ouopenid\local;

class conditional_embedded_survey {

const CONFIG_KEY = 'auth_ouopenid_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'

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

protected $userid;
protected $config;

public function __construct( $course_code, $userid = null ) {
$this->set_config( $course_code, $userid );
}

protected function set_config( $course_code, $userid = null ) {
global $CFG, $USER; // Moodle globals;

if (! isset( $CFG->{ self::CONFIG_KEY } )) {
throw new \Exception( 'Missing configuration array: \$CFG->' . self::CONFIG_KEY );
}

if (isset( $CFG->{ self::CONFIG_KEY }[ $course_code ] )) {
$this->config = $config = $CFG->{ self::CONFIG_KEY }[ $course_code ];

$this->course_id = $config->course_id;
$this->course_code = $course_code;
$this->cmid = $config->cmid;
$this->activity_id = $config->activity_id;

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

self::_debug([ __METHOD__, $config, $this->userid ]);
}
}

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 false;
}

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

$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' ??

return 1 === $count;
}

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

$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 ]);

return $result;
}

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

$lastinsertid = $DB->insert_record('assign_grades', (object) [
'assignment' => $this->activity_id,
'userid' => $this->userid,
'timecreated' => time(), // UNIX_TIMESTAMP()
'timemodified' => time(),
'grader' => 0,
'grade' => 100.00,
'attemptnumber' => 0,
], false);

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

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

$lastinsertid = $DB->insert_record('assign_submission', (object) [
'assignment' => $this->activity_id,
'userid' => $this->userid,
'timecreated' => time(), // UNIX_TIMESTAMP()
'timemodified' => time(),
'status' => 'submitted',
'groupid' => 0,
'attemptnumber' => 0,
'latest' => 1, // 'true'
], false);

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

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

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

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

/** Output arbitrary data, eg. to HTTP header.
*/
protected static function _debug($obj)
{
static $count = 0;
header(sprintf('X-auth-ou-cond-%02d: %s', $count, json_encode($obj)));
$count++;
}
}
2 changes: 1 addition & 1 deletion lang/en/auth_ouopenid.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
// Form validation - title attribute - 'login_field_help'.
$string[ 'login_field_help' ] = 'Your OUCU — 2 to 4 letters, followed by 1 to 7 numbers.';
$string[ 'login_submit' ] = 'Sign in';
$string[ 'login_footer' ] = '© 2017 The Open University';
$string[ 'login_footer' ] = '© 2018 The Open University';
$string[ 'login_footer_link' ] = 'https://www.open.ac.uk/';

$string[ 'survey_end_title' ] =
Expand Down
25 changes: 24 additions & 1 deletion survey-end/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

// For 'print_string()' language support!
require_once __DIR__ . '/../../../config.php';
require_once __DIR__ . '/../classes/local/conditional_embedded_survey.php';

use auth_ouopenid\local\conditional_embedded_survey;

define( 'OUOP_STRING', 'auth_ouopenid' );

Expand All @@ -30,9 +33,27 @@ public static function checkMaintenanceMode() {
header('Location: '. $CFG->wwwroot, true, 302);
}
}

public static function get_return_code() {
$code = preg_replace_callback(
'/(?:return-code-)?(?:\d-)?(?P<code>\w+)/',
function ($matches) { return $matches[ 'code' ]; },
// filter_input( INPUT_GET, 'return-code' ) );
required_param( 'return-code', PARAM_ALPHANUMEXT ) );

header( 'X-getreturncode: ' . $code );

return $code;
}

public static function complete_conditional() {
$conditional = new conditional_embedded_survey( self::get_return_code() );
return $conditional->make_complete();
}
}
Ou_Open_Id_Survey_End::checkMaintenanceMode();

$conditional_completed = Ou_Open_Id_Survey_End::complete_conditional();

header('Content-Language: en');
header('X-Frame-Options: sameorigin');
Expand Down Expand Up @@ -99,11 +120,13 @@ public static function checkMaintenanceMode() {
<script id="survey-end-config" type="application/json">
<?php
echo json_encode([
'course_code' => Ou_Open_Id_Survey_End::getReturnCode(),
'cond_completed' => $conditional_completed,
'redirects' => $CFG->auth_ouopenid_redirects,
'hash' => '#section-3',
'timeout' => 3000,
'other' => 1,
]);
], JSON_PRETTY_PRINT);
?>
</script>

Expand Down

0 comments on commit 16c747e

Please sign in to comment.