Skip to content

Commit

Permalink
New {UserFirstaccess} FilterCode (#115)
Browse files Browse the repository at this point in the history
* New tag (user->firstaccess) + datetime tags now take strftime
* Added info to README
  • Loading branch information
petermAprende authored Sep 30, 2020
1 parent d87a45d commit e552c46
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
Binary file added .filter.php.swp
Binary file not shown.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Moodle metadata filters
* {userpictureurl X} : Display the user's profile picture URL. X indicates the size and can be **sm** (small), **md** (medium) or **lg** (large). If the user does not have a profile picture or is logged out, the default faceless profile photo URL will be shown instead.
* {userpictureimg X} : Generates an <img> html tag containing the user's profile picture. X indicates the size and can be **sm** (small), **md** (medium) or **lg** (large). If the user does not have profile picture or is logged out, the default faceless profile photo will be used instead.
* {usercount} : Count total number of registered users on the site. Does not included deleted users, primary admin or guest.
* {userfirstaccess} : Date that the user first accessd the site
* {usersactive} : Count total number of registered users on the site. Does not included deleted users, disabled users, primary admin or guest.
* {usersonline} : Total number of users who were online in the last 5 minutes.
* {siteyear} : 4-digit current year.
Expand Down Expand Up @@ -227,6 +228,19 @@ The {langx fr-CA}{/langx} filter will convert this into the following HTML

<span lang="fr-CA">Contenu</span>

Date strftime Arguments

The 4 datetime codes can optionally take a strftime string to define the format. This string must be enclosed with double quotes.

* {userfirstaccess}
* {coursestartdate}
* {courseenddate}
* {coursecompletiondate}

Example:

{userfirstaccess "%m/%d/%Y, %H:%M:%S"}

## FilterCodes in a custom menu

Here are a couple of examples of how to create really useful custom menus using FilterCodes. Just copy and paste the code into the **Custom menu items** field (Site administration > Appearance > Theme settings)
Expand Down
83 changes: 69 additions & 14 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,28 @@ function ($matches) {
}
}

// Tag: {userfirstaccess} or {userfirstaccess "strftime"}
if (stripos($text, '{userfirstaccess') !== false) {
if (isloggedin() && !isguestuser()) {
$user = $DB->get_record('user', array('id' => $USER->id), 'firstaccess', MUST_EXIST);
$newtext = preg_replace_callback('/{userfirstaccess(\s+"(.*)")?}/i',
function($matches) use ($user) {
if(sizeof($matches) == 1 && $matches[0] == "{userfirstaccess}"){
return userdate($user->firstaccess, get_string('strftimedatefullshort'));
}else if(sizeof($matches) == 3){
return userdate($user->firstaccess, $matches[2]);
}
}, $text);
if ($newtext !== false) {
$text = $newtext;
$changed = true;
}
unset($user);
} else {
$replace['/\{userfirstaccess(\s+"(.*)")?\}/i'] = get_string('none');
}
}

// Tag: {usercount}.
if (stripos($text, '{usercount}') !== false) {
// Count total number of current users on the site.
Expand Down Expand Up @@ -765,45 +787,78 @@ function ($matches) {
$replace['/\{courseimage\}/i'] = '<img src="' . $imgurl . '" class="img-responsive">';
}

// Tag: {coursestartdate}. The course start date.
if (stripos($text, '{coursestartdate}') !== false) {
// Tag: {coursestartdate} or {coursestartdate "strftime"}. The course start date.
if (stripos($text, '{coursestartdate') !== false) {
if (empty($PAGE->course->startdate)) {
$PAGE->course->startdate = $DB->get_field_select('course', 'startdate', 'id = :id', ['id' => $course->id]);
}
if ($PAGE->course->startdate > 0) {
$replace['/\{coursestartdate\}/i'] = userdate($PAGE->course->startdate, get_string('strftimedatefullshort'));
$newtext = preg_replace_callback('/{coursestartdate(\s+"(.*)")?}/i',
function($matches) use ($PAGE) {
if(sizeof($matches) == 1 && $matches[0] == "{coursestartdate}"){
return userdate($PAGE->course->startdate, get_string('strftimedatefullshort'));
}else if(sizeof($matches) == 3){
return userdate($PAGE->course->startdate, $matches[2]);
}
}, $text);
if ($newtext !== false) {
$text = $newtext;
$changed = true;
}
} else {
$replace['/\{coursestartdate\}/i'] = get_string('none');
$replace['/\{coursestartdate(\s+"(.*)")?\}/i'] = get_string('none');
}
}

// Tag: {courseenddate}. The course end date.
if (stripos($text, '{courseenddate}') !== false) {
// Tag: {courseenddate} or {coursesenddate "strftime"}. The course end date.
if (stripos($text, '{courseenddate') !== false) {
if (empty($PAGE->course->enddate)) {
$PAGE->course->enddate = $DB->get_field_select('course', 'enddate', 'id = :id', ['id' => $course->id]);
}
if ($PAGE->course->enddate > 0) {
$replace['/\{courseenddate\}/i'] = userdate($PAGE->course->enddate, get_string('strftimedatefullshort'));
$newtext = preg_replace_callback('/{courseenddate(\s+"(.*)")?}/i',
function($matches) use ($PAGE) {
if(sizeof($matches) == 1 && $matches[0] == "{courseenddate}"){
return userdate($PAGE->course->enddate, get_string('strftimedatefullshort'));
}else if(sizeof($matches) == 3){
return userdate($PAGE->course->enddate, $matches[2]);
}
}, $text);
if ($newtext !== false) {
$text = $newtext;
$changed = true;
}
} else {
$replace['/\{courseenddate\}/i'] = get_string('none');
$replace['/\{courseenddate(\s+"(.*)")?\}/i'] = get_string('none');
}
}

// Tag: {coursecompletiondate}. The course completion date.
if (stripos($text, '{coursecompletiondate}') !== false) {

// Tag: {coursecompletiondate} or {coursecompletiondate "strftime"}. The course completion date.
if (stripos($text, '{coursecompletiondate') !== false) {
if ($PAGE->course
&& isset($CFG->enablecompletion)
&& $CFG->enablecompletion == COMPLETION_ENABLED
&& $PAGE->course->enablecompletion) {
$ccompletion = new completion_completion(['userid' => $USER->id, 'course' => $PAGE->course->id]);
if ($ccompletion->timecompleted) {
$replace['/\{coursecompletiondate\}/i'] = userdate($ccompletion->timecompleted,
get_string('strftimedatefullshort'));
$newtext = preg_replace_callback('/{coursecompletiondate(\s+"(.*)")?}/i',
function($matches) use ($ccompletion) {
if(sizeof($matches) == 1 && $matches[0] == "{coursecompletiondate}"){
return userdate($ccompletion->timecompleted, get_string('strftimedatefullshort'));
}else if(sizeof($matches) == 3){
return userdate($ccompletion->timecompleted, $matches[2]);
}
}, $text);
if ($newtext !== false) {
$text = $newtext;
$changed = true;
}
} else {
$replace['/\{coursecompletiondate\}/i'] = get_string('notcompleted', 'completion');
$replace['/\{coursecompletiondate(\s+"(.*)")?\}/i'] = get_string('notcompleted', 'completion');
}
} else {
$replace['/\{coursecompletiondate\}/i'] = get_string('completionnotenabled', 'completion');
$replace['/\{coursecompletiondate(\s+"(.*)")?\}/i'] = get_string('completionnotenabled', 'completion');
}
}

Expand Down

0 comments on commit e552c46

Please sign in to comment.