diff --git a/.filter.php.swp b/.filter.php.swp new file mode 100644 index 0000000..d84e118 Binary files /dev/null and b/.filter.php.swp differ diff --git a/README.md b/README.md index 263ca06..e8a4e55 100644 --- a/README.md +++ b/README.md @@ -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 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. @@ -227,6 +228,19 @@ The {langx fr-CA}{/langx} filter will convert this into the following HTML Contenu +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) diff --git a/filter.php b/filter.php index f5ef948..20d3ee4 100644 --- a/filter.php +++ b/filter.php @@ -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. @@ -765,45 +787,78 @@ function ($matches) { $replace['/\{courseimage\}/i'] = ''; } - // 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'); } }