这是我的SQL查询的一部分。
SELECT
c.fullname AS Course,
gi.itemname AS Activity,
CONCAT(ROUND(gg.finalgrade,1) , '%') AS Grade,
cmc.completionstate,
gi.itemmodule,
concat('<a target="_new" href="localhost/moodle/mod/quiz/view.php?id=',cm.id,'">View this Activity</a>') AS 'Link'
我想在gi.itemmodule =测验时显示以上链接,并在gi.itemmodule = scorm等时显示不同的链接(.. moodle / mod / scorm ..)
我认为这需要一个CASE
声明,但不知道该怎么做。
答案 0 :(得分:3)
case gi.itemmodule
when 'quiz'
then concat(<onelink>)
when 'scorm'
then concat(<oneotherlink>)
else concat(<defaultlink>)
end as 'Link'
如果在concat中,如果只应该更改concat的一部分,你也可以这样做。
类似
concat('<a target="_new" href="',
(case gi.itemmodule
when 'quiz'
then 'value of href'
when 'scorm'
then 'another value of href'
else 'defaultvalue of href'
end),
cm.id,
'">View this Activity</a>') as 'Link'
这将避免代码重复,但可能更难阅读......
给出样本,似乎你可以做(但不确定)
concat('<a target="_new" href="localhost/moodle/mod/',gi.itemmodule, '/view.php?id=',cm.id,'">View this Activity</a>') AS 'Link'
或者某些itemmodule对链接有用,但不是全部
concat('<a target="_new" href="localhost/moodle/mod/',
(case when gi.itemmodule in ('scorm', 'quiz', 'asdf')
then gi.itemmodule
else 'defaultValue'
end),
'/view.php?id=',
cm.id,
'">View this Activity</a>') AS 'Link'
答案 1 :(得分:0)
您不应该在SQL查询中构建链接。请改用moodle_url()函数
$sql = "SELECT
c.fullname AS Course,
gi.itemname AS Activity,
" . $DB->sql_concat('ROUND(gg.finalgrade,1)' , '%') . " AS Grade,
cmc.completionstate,
gi.itemmodule,
...
";
$ DB-&gt; sql_concat()是Moodle中使用concat的跨数据库函数。
$activities = $DB->get_records_sql($sql);
foreach ($activities as $activity) {
if ($activity->itemmodule == 'quiz') {
// Custom url
$url = new moodle_url('/mod/quiz/view.php', array('id' => $activity->cmid));
} else {
// Module url
$url = new moodle_url('/mod/' . $activity->itemmodule . '/view.php', array('id' => $activity->cmid));
}
$link = html_writer::link($url, get_string('view', 'mod_' . $activity->itemmodule));
echo $link;
}