From 32e4c582f0df9640a3e532553a4335a69c6d9a59 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 16 Aug 2019 18:24:08 +1000 Subject: [PATCH] MDL-66269 mod_forum: AMD module to fetch and format users Part of MDL-66075. --- mod/forum/amd/build/form-user-selector.min.js | 2 + .../amd/build/form-user-selector.min.js.map | 1 + mod/forum/amd/src/form-user-selector.js | 79 +++++++++++++++++++ .../form-user-selector-suggestion.mustache | 52 ++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 mod/forum/amd/build/form-user-selector.min.js create mode 100644 mod/forum/amd/build/form-user-selector.min.js.map create mode 100644 mod/forum/amd/src/form-user-selector.js create mode 100644 mod/forum/templates/form-user-selector-suggestion.mustache diff --git a/mod/forum/amd/build/form-user-selector.min.js b/mod/forum/amd/build/form-user-selector.min.js new file mode 100644 index 0000000000000..4de61b175ca4c --- /dev/null +++ b/mod/forum/amd/build/form-user-selector.min.js @@ -0,0 +1,2 @@ +define ("mod_forum/form-user-selector",["jquery","core/ajax","core/templates"],function(a,b,c){return{processResults:function processResults(b,c){var d=[];a.each(c,function(a,b){d.push({value:b.id,label:b._label})});return d},transport:function transport(d,e,f,g){var h,i=a(d).attr("courseid");h=b.call([{methodname:"core_enrol_search_users",args:{courseid:i,search:e,searchanywhere:!0,page:0,perpage:30}}]);h[0].then(function(b){var d=[],e=0;a.each(b,function(a,b){d.push(c.render("mod_forum/form-user-selector-suggestion",b))});return a.when.apply(a.when,d).then(function(){var c=arguments;a.each(b,function(a,b){b._label=c[e];e++});f(b)})}).fail(g)}}}); +//# sourceMappingURL=form-user-selector.min.js.map diff --git a/mod/forum/amd/build/form-user-selector.min.js.map b/mod/forum/amd/build/form-user-selector.min.js.map new file mode 100644 index 0000000000000..3d1a5ffe5cc63 --- /dev/null +++ b/mod/forum/amd/build/form-user-selector.min.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../src/form-user-selector.js"],"names":["define","$","Ajax","Templates","processResults","selector","results","users","each","index","user","push","value","id","label","_label","transport","query","success","failure","promise","courseid","attr","call","methodname","args","search","searchanywhere","page","perpage","then","promises","i","render","when","apply","arguments","fail"],"mappings":"AAyBAA,OAAM,gCAAC,CAAC,QAAD,CAAW,WAAX,CAAwB,gBAAxB,CAAD,CAA4C,SAASC,CAAT,CAAYC,CAAZ,CAAkBC,CAAlB,CAA6B,CAC3E,MAAyD,CACrDC,cAAc,CAAE,wBAASC,CAAT,CAAmBC,CAAnB,CAA4B,CACxC,GAAIC,CAAAA,CAAK,CAAG,EAAZ,CACAN,CAAC,CAACO,IAAF,CAAOF,CAAP,CAAgB,SAASG,CAAT,CAAgBC,CAAhB,CAAsB,CAClCH,CAAK,CAACI,IAAN,CAAW,CACPC,KAAK,CAAEF,CAAI,CAACG,EADL,CAEPC,KAAK,CAAEJ,CAAI,CAACK,MAFL,CAAX,CAIH,CALD,EAMA,MAAOR,CAAAA,CACV,CAVoD,CAYrDS,SAAS,CAAE,mBAASX,CAAT,CAAmBY,CAAnB,CAA0BC,CAA1B,CAAmCC,CAAnC,CAA4C,IAC/CC,CAAAA,CAD+C,CAE/CC,CAAQ,CAAGpB,CAAC,CAACI,CAAD,CAAD,CAAYiB,IAAZ,CAAiB,UAAjB,CAFoC,CAInDF,CAAO,CAAGlB,CAAI,CAACqB,IAAL,CAAU,CAAC,CACjBC,UAAU,CAAE,yBADK,CAEjBC,IAAI,CAAE,CACFJ,QAAQ,CAAEA,CADR,CAEFK,MAAM,CAAET,CAFN,CAGFU,cAAc,GAHZ,CAIFC,IAAI,CAAE,CAJJ,CAKFC,OAAO,CAAE,EALP,CAFW,CAAD,CAAV,CAAV,CAWAT,CAAO,CAAC,CAAD,CAAP,CAAWU,IAAX,CAAgB,SAASxB,CAAT,CAAkB,CAC9B,GAAIyB,CAAAA,CAAQ,CAAG,EAAf,CACIC,CAAC,CAAG,CADR,CAIA/B,CAAC,CAACO,IAAF,CAAOF,CAAP,CAAgB,SAASG,CAAT,CAAgBC,CAAhB,CAAsB,CAClCqB,CAAQ,CAACpB,IAAT,CAAcR,CAAS,CAAC8B,MAAV,CAAiB,yCAAjB,CAA4DvB,CAA5D,CAAd,CACH,CAFD,EAKA,MAAOT,CAAAA,CAAC,CAACiC,IAAF,CAAOC,KAAP,CAAalC,CAAC,CAACiC,IAAf,CAAqBH,CAArB,EAA+BD,IAA/B,CAAoC,UAAW,CAClD,GAAIL,CAAAA,CAAI,CAAGW,SAAX,CACAnC,CAAC,CAACO,IAAF,CAAOF,CAAP,CAAgB,SAASG,CAAT,CAAgBC,CAAhB,CAAsB,CAClCA,CAAI,CAACK,MAAL,CAAcU,CAAI,CAACO,CAAD,CAAlB,CACAA,CAAC,EACJ,CAHD,EAIAd,CAAO,CAACZ,CAAD,CAEV,CARM,CAUV,CApBD,EAoBG+B,IApBH,CAoBQlB,CApBR,CAqBH,CAhDoD,CAoD5D,CArDK,CAAN","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Enrolled user selector module.\n *\n * @module mod_forum/form-user-selector\n * @class form-user-selector\n * @package mod_forum\n * @copyright 2019 Shamim Rezaie\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\ndefine(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {\n return /** @alias module:mod_forum/form-user-selector */ {\n processResults: function(selector, results) {\n var users = [];\n $.each(results, function(index, user) {\n users.push({\n value: user.id,\n label: user._label\n });\n });\n return users;\n },\n\n transport: function(selector, query, success, failure) {\n var promise;\n var courseid = $(selector).attr('courseid');\n\n promise = Ajax.call([{\n methodname: 'core_enrol_search_users',\n args: {\n courseid: courseid,\n search: query,\n searchanywhere: true,\n page: 0,\n perpage: 30\n }\n }]);\n\n promise[0].then(function(results) {\n var promises = [],\n i = 0;\n\n // Render the label.\n $.each(results, function(index, user) {\n promises.push(Templates.render('mod_forum/form-user-selector-suggestion', user));\n });\n\n // Apply the label to the results.\n return $.when.apply($.when, promises).then(function() {\n var args = arguments;\n $.each(results, function(index, user) {\n user._label = args[i];\n i++;\n });\n success(results);\n return;\n });\n\n }).fail(failure);\n }\n\n };\n\n});\n"],"file":"form-user-selector.min.js"} \ No newline at end of file diff --git a/mod/forum/amd/src/form-user-selector.js b/mod/forum/amd/src/form-user-selector.js new file mode 100644 index 0000000000000..58191219e4239 --- /dev/null +++ b/mod/forum/amd/src/form-user-selector.js @@ -0,0 +1,79 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Enrolled user selector module. + * + * @module mod_forum/form-user-selector + * @class form-user-selector + * @package mod_forum + * @copyright 2019 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) { + return /** @alias module:mod_forum/form-user-selector */ { + processResults: function(selector, results) { + var users = []; + $.each(results, function(index, user) { + users.push({ + value: user.id, + label: user._label + }); + }); + return users; + }, + + transport: function(selector, query, success, failure) { + var promise; + var courseid = $(selector).attr('courseid'); + + promise = Ajax.call([{ + methodname: 'core_enrol_search_users', + args: { + courseid: courseid, + search: query, + searchanywhere: true, + page: 0, + perpage: 30 + } + }]); + + promise[0].then(function(results) { + var promises = [], + i = 0; + + // Render the label. + $.each(results, function(index, user) { + promises.push(Templates.render('mod_forum/form-user-selector-suggestion', user)); + }); + + // Apply the label to the results. + return $.when.apply($.when, promises).then(function() { + var args = arguments; + $.each(results, function(index, user) { + user._label = args[i]; + i++; + }); + success(results); + return; + }); + + }).fail(failure); + } + + }; + +}); diff --git a/mod/forum/templates/form-user-selector-suggestion.mustache b/mod/forum/templates/form-user-selector-suggestion.mustache new file mode 100644 index 0000000000000..377cf093fe43d --- /dev/null +++ b/mod/forum/templates/form-user-selector-suggestion.mustache @@ -0,0 +1,52 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template mod_forum/form-user-selector-suggestion + + Moodle template for the list of valid options in an autocomplate form element. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * fullname string Users full name + * email string user email field + + Example context (json): + { + "fullname": "Admin User", + "extrafields": [ + { + "name": "email", + "value": "admin@example.com" + }, + { + "name": "phone1", + "value": "0123456789" + } + ] + } +}} + + {{fullname}} + {{#extrafields}} + {{value}} + {{/extrafields}} +