Commit c477e196 authored by Francois Gannaz's avatar Francois Gannaz
Browse files

LnB auto-login for members of a team

parent 449e2092
......@@ -106,10 +106,19 @@ class mod_labnbook_session
return $DB->get_field("labnbook", "labnbook_missionid", ['id' => $instanceId]);
}
public function getReportId($missionId)
/**
* @param int $instanceId
* @return int
*/
public function getReportId($instanceId)
{
$userId = $this->getUserId();
return $this->fetch('GET', "/report", null, ['userid' => $userId, 'missionid' => $missionId]);
global $DB, $USER;
return $DB->get_field_sql(
"SELECT labnbook_reportid"
. " FROM {labnbook_team} t JOIN {labnbook_teamconfig} tc ON t.teamconfigid = tc.id JOIN {labnbook_teammember} tm ON tm.teamid = t.id"
. " WHERE tc.instanceid = :instanceid AND tm.userid = :userid",
['instanceid' => $instanceId, 'userid' => $USER->id]
);
}
/**
......@@ -119,12 +128,15 @@ class mod_labnbook_session
* @return string URL with added parameters
*/
public function createAuthenticatedUrl($urlPath) {
$userId = $this->getUserId();
$params = [
'key' => $this->secret,
'userid' => $this->getUserId(),
'userid' => $userId,
'institution' => substr($this->secret, 0, 4),
'redirect' => $urlPath,
];
return "{$this->url}'/auth/api/login.php?" . http_build_query($params);
$ip = $_SERVER['REMOTE_ADDR'];
$params['signature'] = password_hash("$ip $userId {$this->secret}", PASSWORD_DEFAULT);
return $this->url . "/auth/token/login.php?" . http_build_query($params, null, "&");
}
/**
......
<?php
use \mod_labnbook_session as session;
require(__DIR__.'/../../config.php');
require_once(__DIR__.'/lib.php');
$reportId = optional_param('reportId', 0, PARAM_INT);
$lnb = session::load();
var_dump($reportId);
if ($reportId) {
header("Location: " . $lnb->createAuthenticatedUrl('/pages/report.php?idr=' . $reportId));
exit();
}
......@@ -34,13 +34,13 @@ $id = optional_param('id', 0, PARAM_INT);
$l = optional_param('l', 0, PARAM_INT);
if ($id) {
$cm = get_coursemodule_from_id('labnbook', $id, 0, false, MUST_EXIST);
$cm = get_coursemodule_from_id(LABNBOOK_TABLE, $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$moduleinstance = $DB->get_record(LABNBOOK_TABLE, array('id' => $cm->instance), '*', MUST_EXIST);
} else if ($l) {
$moduleinstance = $DB->get_record(LABNBOOK_TABLE, array('id' => $n), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $moduleinstance->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('labnbook', $moduleinstance->id, $course->id, false, MUST_EXIST);
$cm = get_coursemodule_from_instance(LABNBOOK_TABLE, $moduleinstance->id, $course->id, false, MUST_EXIST);
} else {
print_error(get_string('missingidandcmid', mod_labnbook));
}
......@@ -59,6 +59,7 @@ $event->add_record_snapshot(LABNBOOK_TABLE, $moduleinstance);
$event->trigger();
*/
/* @var $PAGE moodle_page */
$PAGE->set_url('/mod/labnbook/view.php', array('id' => $cm->id));
$PAGE->set_title(format_string($moduleinstance->name));
$PAGE->set_heading(format_string($course->fullname));
......@@ -72,13 +73,13 @@ if (has_capability('moodle/course:manageactivities', context_course::instance($c
// ...
} else {
// student view
$reportId = $lnb->getReportId($moduleinstance->labnbook_missionid);
$reportId = $lnb->getReportId($cm->id);
$data = [
'title' => $moduleinstance->name,
'description' => format_text($moduleinstance->intro, $moduleinstance->introformat, ['context' => $modulecontext]),
'url' => $lnb->createAuthenticatedUrl('/page/report.php?idr=' . $reportId),
'url' => new moodle_url('/mod/labnbook/redirect.php', ['reportId' => $reportId]),
];
$OUTPUT->render_from_template('mod_labnbook/view_student', $data);
echo $OUTPUT->render_from_template('mod_labnbook/view_student', $data);
}
echo $OUTPUT->footer();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment