extra_field.lib.php 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. /**
  5. * Class ExtraField
  6. */
  7. class ExtraField extends Model
  8. {
  9. public $columns = array(
  10. 'id',
  11. 'field_type',
  12. 'variable',
  13. 'display_text',
  14. 'default_value',
  15. 'field_order',
  16. 'visible',
  17. 'changeable',
  18. 'filter',
  19. 'extra_field_type',
  20. /* Enable this when field_loggeable is introduced as a table field (2.0)
  21. 'field_loggeable',
  22. */
  23. 'created_at'
  24. );
  25. public $ops = array(
  26. 'eq' => '=', //equal
  27. 'ne' => '<>', //not equal
  28. 'lt' => '<', //less than
  29. 'le' => '<=', //less than or equal
  30. 'gt' => '>', //greater than
  31. 'ge' => '>=', //greater than or equal
  32. 'bw' => 'LIKE', //begins with
  33. 'bn' => 'NOT LIKE', //doesn't begin with
  34. 'in' => 'LIKE', //is in
  35. 'ni' => 'NOT LIKE', //is not in
  36. 'ew' => 'LIKE', //ends with
  37. 'en' => 'NOT LIKE', //doesn't end with
  38. 'cn' => 'LIKE', //contains
  39. 'nc' => 'NOT LIKE' //doesn't contain
  40. );
  41. const FIELD_TYPE_TEXT = 1;
  42. const FIELD_TYPE_TEXTAREA = 2;
  43. const FIELD_TYPE_RADIO = 3;
  44. const FIELD_TYPE_SELECT = 4;
  45. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  46. const FIELD_TYPE_DATE = 6;
  47. const FIELD_TYPE_DATETIME = 7;
  48. const FIELD_TYPE_DOUBLE_SELECT = 8;
  49. const FIELD_TYPE_DIVIDER = 9;
  50. const FIELD_TYPE_TAG = 10;
  51. const FIELD_TYPE_TIMEZONE = 11;
  52. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  53. const FIELD_TYPE_CHECKBOX = 13;
  54. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  55. const FIELD_TYPE_INTEGER = 15;
  56. const FIELD_TYPE_FILE_IMAGE = 16;
  57. const FIELD_TYPE_FLOAT = 17;
  58. const FIELD_TYPE_FILE = 18;
  59. const FIELD_TYPE_VIDEO_URL = 19;
  60. const FIELD_TYPE_LETTERS_ONLY = 20;
  61. const FIELD_TYPE_ALPHANUMERIC = 21;
  62. const FIELD_TYPE_LETTERS_SPACE = 22;
  63. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;
  64. const FIELD_TYPE_GEOLOCALIZATION = 24;
  65. public $type = 'user';
  66. public $pageName;
  67. public $pageUrl;
  68. public $extraFieldType = 0;
  69. public $table_field_options;
  70. public $table_field_values;
  71. public $table_field_tag;
  72. public $table_field_rel_tag;
  73. public $handler_id;
  74. public $primaryKey;
  75. /**
  76. * @param string $type
  77. */
  78. public function __construct($type)
  79. {
  80. parent::__construct();
  81. $this->type = $type;
  82. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD);
  83. $this->table_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  84. $this->table_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  85. $this->table_field_tag = Database::get_main_table(TABLE_MAIN_TAG);
  86. $this->table_field_rel_tag = Database::get_main_table(TABLE_MAIN_EXTRA_FIELD_REL_TAG);
  87. $this->handler_id = 'item_id';
  88. switch ($this->type) {
  89. case 'calendar_event':
  90. $this->extraFieldType = EntityExtraField::CALENDAR_FIELD_TYPE;
  91. break;
  92. case 'course':
  93. $this->extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  94. $this->primaryKey = 'id';
  95. break;
  96. case 'user':
  97. $this->extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  98. $this->primaryKey = 'id';
  99. break;
  100. case 'session':
  101. $this->extraFieldType = EntityExtraField::SESSION_FIELD_TYPE;
  102. $this->primaryKey = 'id';
  103. break;
  104. case 'question':
  105. $this->extraFieldType = EntityExtraField::QUESTION_FIELD_TYPE;
  106. break;
  107. case 'lp':
  108. $this->extraFieldType = EntityExtraField::LP_FIELD_TYPE;
  109. break;
  110. case 'lp_item':
  111. $this->extraFieldType = EntityExtraField::LP_ITEM_FIELD_TYPE;
  112. break;
  113. case 'skill':
  114. $this->extraFieldType = EntityExtraField::SKILL_FIELD_TYPE;
  115. }
  116. $this->pageUrl = 'extra_fields.php?type='.$this->type;
  117. // Example QuestionFields
  118. $this->pageName = get_lang(ucwords($this->type).'Fields');
  119. }
  120. /**
  121. * @return int
  122. */
  123. public function getExtraFieldType()
  124. {
  125. return $this->extraFieldType;
  126. }
  127. /**
  128. * @return array
  129. */
  130. public static function getValidExtraFieldTypes()
  131. {
  132. return array(
  133. 'user',
  134. 'course',
  135. 'session',
  136. 'question',
  137. 'lp',
  138. 'calendar_event',
  139. 'lp_item',
  140. 'skill'
  141. );
  142. }
  143. /**
  144. * @return int
  145. */
  146. public function get_count()
  147. {
  148. $em = Database::getManager();
  149. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  150. $query->select('count(e.id)');
  151. $query->where('e.extraFieldType = :type');
  152. $query->setParameter('type', $this->getExtraFieldType());
  153. return $query->getQuery()->getScalarResult();
  154. }
  155. /**
  156. * @param string $sidx
  157. * @param string $sord
  158. * @param int $start
  159. * @param int $limit
  160. *
  161. * @return array
  162. */
  163. public function getAllGrid($sidx, $sord, $start, $limit)
  164. {
  165. switch ($sidx) {
  166. case 'field_order':
  167. $sidx = 'e.fieldOrder';
  168. break;
  169. case 'variable':
  170. $sidx = 'e.variable';
  171. break;
  172. case 'display_text':
  173. $sidx = 'e.displayText';
  174. break;
  175. case 'changeable':
  176. $sidx = 'e.changeable';
  177. break;
  178. case 'visible':
  179. $sidx = 'e.visible';
  180. break;
  181. case 'filter':
  182. $sidx = 'e.filter';
  183. break;
  184. case 'display_text':
  185. $sidx = 'e.fieldType';
  186. break;
  187. }
  188. $em = Database::getManager();
  189. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  190. $query->select('e')
  191. ->where('e.extraFieldType = :type')
  192. ->setParameter('type', $this->getExtraFieldType())
  193. ->orderBy($sidx, $sord)
  194. ->setFirstResult($start)
  195. ->setMaxResults($limit);
  196. //echo $query->getQuery()->getSQL();
  197. return $query->getQuery()->getArrayResult();
  198. }
  199. /**
  200. * @param array $conditions
  201. * @param null $order_field_options_by
  202. *
  203. * @return array
  204. */
  205. public function get_all($conditions = array(), $order_field_options_by = null)
  206. {
  207. $conditions = Database::parse_conditions(array('where' => $conditions));
  208. if (empty($conditions)) {
  209. $conditions .= " WHERE extra_field_type = ".$this->extraFieldType;
  210. } else {
  211. $conditions .= " AND extra_field_type = ".$this->extraFieldType;
  212. }
  213. $sql = "SELECT * FROM $this->table
  214. $conditions
  215. ORDER BY field_order ASC
  216. ";
  217. $result = Database::query($sql);
  218. $extraFields = Database::store_result($result, 'ASSOC');
  219. $option = new ExtraFieldOption($this->type);
  220. if (!empty($extraFields)) {
  221. foreach ($extraFields as &$extraField) {
  222. $extraField['display_text'] = self::translateDisplayName(
  223. $extraField['variable'],
  224. $extraField['display_text']
  225. );
  226. $extraField['options'] = $option->get_field_options_by_field(
  227. $extraField['id'],
  228. false,
  229. $order_field_options_by
  230. );
  231. }
  232. }
  233. return $extraFields;
  234. }
  235. /**
  236. * @param string $variable
  237. *
  238. * @return array|bool
  239. */
  240. public function get_handler_field_info_by_field_variable($variable)
  241. {
  242. $variable = Database::escape_string($variable);
  243. $sql = "SELECT * FROM {$this->table}
  244. WHERE
  245. variable = '$variable' AND
  246. extra_field_type = $this->extraFieldType";
  247. $result = Database::query($sql);
  248. if (Database::num_rows($result)) {
  249. $row = Database::fetch_array($result, 'ASSOC');
  250. $row['display_text'] = ExtraField::translateDisplayName($row['variable'], $row['display_text']);
  251. // All the options of the field
  252. $sql = "SELECT * FROM $this->table_field_options
  253. WHERE field_id='".intval($row['id'])."'
  254. ORDER BY option_order ASC";
  255. $result = Database::query($sql);
  256. while ($option = Database::fetch_array($result)) {
  257. $row['options'][$option['id']] = $option;
  258. }
  259. return $row;
  260. } else {
  261. return false;
  262. }
  263. }
  264. /**
  265. * Get all the field info for tags
  266. * @param string $variable
  267. *
  268. * @return array|bool
  269. */
  270. public function get_handler_field_info_by_tags($variable)
  271. {
  272. $variable = Database::escape_string($variable);
  273. $sql = "SELECT * FROM {$this->table}
  274. WHERE
  275. variable = '$variable' AND
  276. extra_field_type = $this->extraFieldType";
  277. $result = Database::query($sql);
  278. if (Database::num_rows($result)) {
  279. $row = Database::fetch_array($result, 'ASSOC');
  280. $row['display_text'] = ExtraField::translateDisplayName($row['variable'], $row['display_text']);
  281. // All the tags of the field
  282. $sql = "SELECT * FROM $this->table_field_tag
  283. WHERE field_id='".intval($row['id'])."'
  284. ORDER BY id ASC";
  285. $result = Database::query($sql);
  286. while ($option = Database::fetch_array($result, 'ASSOC')) {
  287. $row['options'][$option['id']] = $option;
  288. }
  289. return $row;
  290. } else {
  291. return false;
  292. }
  293. }
  294. /**
  295. * @param int $fieldId
  296. *
  297. * @return array|bool
  298. */
  299. public function getFieldInfoByFieldId($fieldId)
  300. {
  301. $fieldId = intval($fieldId);
  302. $sql = "SELECT * FROM {$this->table}
  303. WHERE
  304. field_id = '$fieldId' AND
  305. extra_field_type = $this->extraFieldType";
  306. $result = Database::query($sql);
  307. if (Database::num_rows($result)) {
  308. $row = Database::fetch_array($result, 'ASSOC');
  309. // All the options of the field
  310. $sql = "SELECT * FROM $this->table_field_options
  311. WHERE field_id='".$fieldId."'
  312. ORDER BY option_order ASC";
  313. $result = Database::query($sql);
  314. while ($option = Database::fetch_array($result)) {
  315. $row['options'][$option['id']] = $option;
  316. }
  317. return $row;
  318. } else {
  319. return false;
  320. }
  321. }
  322. /**
  323. * @return int
  324. */
  325. public function get_max_field_order()
  326. {
  327. $sql = "SELECT MAX(field_order)
  328. FROM {$this->table}
  329. WHERE
  330. extra_field_type = '.$this->extraFieldType.'";
  331. $res = Database::query($sql);
  332. $order = 0;
  333. if (Database::num_rows($res) > 0) {
  334. $row = Database::fetch_row($res);
  335. $order = $row[0] + 1;
  336. }
  337. return $order;
  338. }
  339. /**
  340. * @param string $handler
  341. *
  342. * @return array
  343. */
  344. public static function get_extra_fields_by_handler($handler)
  345. {
  346. $types= array();
  347. $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  348. $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  349. $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  350. $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  351. $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  352. $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  353. $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  354. $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  355. $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  356. $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  357. $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  358. $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  359. $types[self::FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  360. $types[self::FIELD_TYPE_CHECKBOX] = get_lang('FieldTypeCheckbox');
  361. $types[self::FIELD_TYPE_INTEGER] = get_lang('FieldTypeInteger');
  362. $types[self::FIELD_TYPE_FILE_IMAGE] = get_lang('FieldTypeFileImage');
  363. $types[self::FIELD_TYPE_FLOAT] = get_lang('FieldTypeFloat');
  364. $types[self::FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  365. $types[self::FIELD_TYPE_VIDEO_URL] = get_lang('FieldTypeVideoUrl');
  366. $types[self::FIELD_TYPE_LETTERS_ONLY] = get_lang('FieldTypeOnlyLetters');
  367. $types[self::FIELD_TYPE_ALPHANUMERIC] = get_lang('FieldTypeAlphanumeric');
  368. $types[self::FIELD_TYPE_LETTERS_SPACE] = get_lang(
  369. 'FieldTypeLettersSpaces'
  370. );
  371. $types[self::FIELD_TYPE_ALPHANUMERIC_SPACE] = get_lang(
  372. 'FieldTypeAlphanumericSpaces'
  373. );
  374. $types[self::FIELD_TYPE_GEOLOCALIZATION] = get_lang(
  375. 'Geolocalization'
  376. );
  377. switch ($handler) {
  378. case 'course':
  379. // no break
  380. case 'session':
  381. // no break
  382. case 'user':
  383. // no break
  384. case 'skill':
  385. break;
  386. }
  387. return $types;
  388. }
  389. /**
  390. * Add elements to a form
  391. *
  392. * @param FormValidator $form
  393. * @param int $itemId
  394. * @param array $exclude variables of extra field to exclude
  395. * @param bool $filter
  396. *
  397. * @return array|bool
  398. */
  399. public function addElements(
  400. $form,
  401. $itemId = 0,
  402. $exclude = [],
  403. $filter = false,
  404. $useTagAsSelect = false,
  405. $showOnlyThisFields = [],
  406. $orderFields = []
  407. ) {
  408. if (empty($form)) {
  409. return false;
  410. }
  411. $itemId = intval($itemId);
  412. $form->addHidden('item_id', $itemId);
  413. $extraData = false;
  414. if (!empty($itemId)) {
  415. $extraData = self::get_handler_extra_data($itemId);
  416. if ($form) {
  417. $form->setDefaults($extraData);
  418. }
  419. }
  420. $conditions = [];
  421. if ($filter) {
  422. $conditions = ['filter = ?' => 1];
  423. }
  424. $extraFields = $this->get_all($conditions, 'option_order');
  425. $extra = $this->set_extra_fields_in_form(
  426. $form,
  427. $extraData,
  428. false,
  429. $extraFields,
  430. $itemId,
  431. $exclude,
  432. $useTagAsSelect,
  433. $showOnlyThisFields,
  434. $orderFields
  435. );
  436. return $extra;
  437. }
  438. /**
  439. * @param int $itemId (session_id, question_id, course id)
  440. *
  441. * @return array
  442. */
  443. public function get_handler_extra_data($itemId)
  444. {
  445. if (empty($itemId)) {
  446. return array();
  447. }
  448. $extra_data = array();
  449. $fields = self::get_all();
  450. $field_values = new ExtraFieldValue($this->type);
  451. if (!empty($fields) > 0) {
  452. foreach ($fields as $field) {
  453. $field_value = $field_values->get_values_by_handler_and_field_id(
  454. $itemId, $field['id']
  455. );
  456. if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  457. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  458. $extra_data['extra_'.$field['variable']] = $tags;
  459. continue;
  460. }
  461. if ($field_value) {
  462. $field_value = $field_value['value'];
  463. switch ($field['field_type']) {
  464. case ExtraField::FIELD_TYPE_TAG:
  465. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  466. $extra_data['extra_'.$field['variable']] = $tags;
  467. break;
  468. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  469. $selected_options = explode(
  470. '::',
  471. $field_value
  472. );
  473. $firstOption = isset($selected_options[0]) ? $selected_options[0] : '';
  474. $secondOption = isset($selected_options[1]) ? $selected_options[1] : '';
  475. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $firstOption;
  476. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable'].'_second'] = $secondOption;
  477. break;
  478. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  479. $field_value = explode(';', $field_value);
  480. $extra_data['extra_'.$field['variable']] = $field_value;
  481. break;
  482. case ExtraField::FIELD_TYPE_RADIO:
  483. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $field_value;
  484. break;
  485. default:
  486. $extra_data['extra_'.$field['variable']] = $field_value;
  487. break;
  488. }
  489. } else {
  490. // Set default values
  491. if (isset($field['field_default_value']) && !empty($field['field_default_value'])) {
  492. $extra_data['extra_'.$field['variable']] = $field['field_default_value'];
  493. }
  494. }
  495. }
  496. }
  497. return $extra_data;
  498. }
  499. /**
  500. * @param string $field_type
  501. *
  502. * @return array
  503. */
  504. public function get_all_extra_field_by_type($field_type)
  505. {
  506. // all the information of the field
  507. $sql = "SELECT * FROM {$this->table}
  508. WHERE
  509. field_type = '".Database::escape_string($field_type)."' AND
  510. extra_field_type = $this->extraFieldType
  511. ";
  512. $result = Database::query($sql);
  513. $return = array();
  514. while ($row = Database::fetch_array($result)) {
  515. $return[] = $row['id'];
  516. }
  517. return $return;
  518. }
  519. /**
  520. * @return array
  521. */
  522. public function get_field_types()
  523. {
  524. return self::get_extra_fields_by_handler($this->type);
  525. }
  526. /**
  527. * @param int $id
  528. *
  529. * @return null
  530. */
  531. public function get_field_type_by_id($id)
  532. {
  533. $types = self::get_field_types();
  534. if (isset($types[$id])) {
  535. return $types[$id];
  536. }
  537. return null;
  538. }
  539. /**
  540. * Converts a string like this:
  541. * France:Paris;Bretagne;Marseille;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  542. * into
  543. * array(
  544. * 'France' =>
  545. * array('Paris', 'Bregtane', 'Marseille'),
  546. * 'Belgique' =>
  547. * array('Namur', 'Liège')
  548. * ), etc
  549. * @param string $string
  550. *
  551. * @return array
  552. */
  553. public static function extra_field_double_select_convert_string_to_array($string)
  554. {
  555. $options = explode('|', $string);
  556. $options_parsed = array();
  557. $id = 0;
  558. if (!empty($options)) {
  559. foreach ($options as $sub_options) {
  560. $options = explode(':', $sub_options);
  561. $sub_sub_options = explode(';', $options[1]);
  562. $options_parsed[$id] = array(
  563. 'label' => $options[0],
  564. 'options' => $sub_sub_options,
  565. );
  566. $id++;
  567. }
  568. }
  569. return $options_parsed;
  570. }
  571. /**
  572. * @param array $options
  573. *
  574. * @return array
  575. */
  576. public static function extra_field_double_select_convert_array_to_ordered_array($options)
  577. {
  578. $options_parsed = array();
  579. if (!empty($options)) {
  580. foreach ($options as $option) {
  581. if ($option['option_value'] == 0) {
  582. $options_parsed[$option['id']][] = $option;
  583. } else {
  584. $options_parsed[$option['option_value']][] = $option;
  585. }
  586. }
  587. }
  588. return $options_parsed;
  589. }
  590. /**
  591. * @param array $options the result of the get_field_options_by_field() array
  592. *
  593. * @return string
  594. */
  595. public static function extra_field_double_select_convert_array_to_string($options)
  596. {
  597. $string = null;
  598. $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
  599. if (!empty($options_parsed)) {
  600. foreach ($options_parsed as $option) {
  601. foreach ($option as $key => $item) {
  602. $string .= $item['display_text'];
  603. if ($key == 0) {
  604. $string .= ':';
  605. } else {
  606. if (isset($option[$key + 1])) {
  607. $string .= ';';
  608. }
  609. }
  610. }
  611. $string .= '|';
  612. }
  613. }
  614. if (!empty($string)) {
  615. $string = substr($string, 0, strlen($string) - 1);
  616. }
  617. return $string;
  618. }
  619. /**
  620. * @param array $params
  621. *
  622. * @return array
  623. */
  624. public function clean_parameters($params)
  625. {
  626. if (!isset($params['variable']) || empty($params['variable'])) {
  627. $params['variable'] = $params['display_text'];
  628. }
  629. $params['variable'] = trim(strtolower(str_replace(" ", "_", $params['variable'])));
  630. if (!isset($params['field_order'])) {
  631. $max_order = self::get_max_field_order();
  632. $params['field_order'] = $max_order;
  633. } else {
  634. $params['field_order'] = (int) $params['field_order'];
  635. }
  636. return $params;
  637. }
  638. /**
  639. * @param array $params
  640. * @param bool $show_query
  641. *
  642. * @return bool
  643. */
  644. public function save($params, $show_query = false)
  645. {
  646. $fieldInfo = self::get_handler_field_info_by_field_variable($params['variable']);
  647. $params = self::clean_parameters($params);
  648. $params['extra_field_type'] = $this->extraFieldType;
  649. if ($fieldInfo) {
  650. return $fieldInfo['id'];
  651. } else {
  652. $id = parent::save($params, $show_query);
  653. if ($id) {
  654. $session_field_option = new ExtraFieldOption($this->type);
  655. $params['field_id'] = $id;
  656. $session_field_option->save($params);
  657. }
  658. return $id;
  659. }
  660. }
  661. /**
  662. * @param array $params
  663. *
  664. * @return bool|void
  665. */
  666. public function update($params)
  667. {
  668. $params = self::clean_parameters($params);
  669. if (isset($params['id'])) {
  670. $field_option = new ExtraFieldOption($this->type);
  671. $params['field_id'] = $params['id'];
  672. $field_option->save($params);
  673. }
  674. parent::update($params);
  675. }
  676. /**
  677. * @param $id
  678. *
  679. * @return bool|void
  680. */
  681. public function delete($id)
  682. {
  683. $em = Database::getManager();
  684. $items = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findBy(['field' => $id]);
  685. if ($items) {
  686. foreach ($items as $item) {
  687. $em->remove($item);
  688. }
  689. $em->flush();
  690. }
  691. $field_option = new ExtraFieldOption($this->type);
  692. $field_option->delete_all_options_by_field_id($id);
  693. $session_field_values = new ExtraFieldValue($this->type);
  694. $session_field_values->delete_all_values_by_field_id($id);
  695. parent::delete($id);
  696. }
  697. /**
  698. * Add an element that matches the given extra field to the given $form object
  699. * @param FormValidator $form
  700. * @param array $extraData
  701. * @param bool $admin_permissions
  702. * @param array $extra
  703. * @param int $itemId
  704. * @param array $exclude variables of extra field to exclude
  705. * @param bool $useTagAsSelect
  706. * @param array $showOnlyThisFields
  707. * @param array $orderFields
  708. * @return array If relevant, returns a one-element array with JS code to be added to the page HTML headers
  709. */
  710. public function set_extra_fields_in_form(
  711. $form,
  712. $extraData,
  713. $admin_permissions = false,
  714. $extra = array(),
  715. $itemId = null,
  716. $exclude = [],
  717. $useTagAsSelect = false,
  718. $showOnlyThisFields = [],
  719. $orderFields = []
  720. ) {
  721. $type = $this->type;
  722. $jquery_ready_content = null;
  723. if (!empty($extra)) {
  724. $newOrder = [];
  725. if (!empty($orderFields)) {
  726. foreach ($orderFields as $order) {
  727. foreach ($extra as $field_details) {
  728. if ($order == $field_details['variable']) {
  729. $newOrder[] = $field_details;
  730. }
  731. }
  732. }
  733. $extra = $newOrder;
  734. }
  735. foreach ($extra as $field_details) {
  736. if (!empty($showOnlyThisFields)) {
  737. if (!in_array($field_details['variable'], $showOnlyThisFields)) {
  738. continue;
  739. }
  740. }
  741. // Getting default value id if is set
  742. $defaultValueId = null;
  743. if (isset($field_details['options']) && !empty($field_details['options'])) {
  744. $valueToFind = null;
  745. if (isset($field_details['field_default_value'])) {
  746. $valueToFind = $field_details['field_default_value'];
  747. }
  748. // If a value is found we override the default value
  749. if (isset($extraData['extra_'.$field_details['variable']])) {
  750. $valueToFind = $extraData['extra_'.$field_details['variable']];
  751. }
  752. foreach ($field_details['options'] as $option) {
  753. if ($option['option_value'] == $valueToFind) {
  754. $defaultValueId = $option['id'];
  755. }
  756. }
  757. }
  758. if (!$admin_permissions) {
  759. if ($field_details['visible'] == 0) {
  760. continue;
  761. }
  762. if (in_array($field_details['variable'], $exclude)) {
  763. continue;
  764. }
  765. }
  766. switch ($field_details['field_type']) {
  767. case ExtraField::FIELD_TYPE_TEXT:
  768. $form->addElement(
  769. 'text',
  770. 'extra_'.$field_details['variable'],
  771. $field_details['display_text'],
  772. array()
  773. );
  774. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  775. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  776. if (!$admin_permissions) {
  777. if ($field_details['visible'] == 0) {
  778. $form->freeze(
  779. 'extra_'.$field_details['variable']
  780. );
  781. }
  782. }
  783. break;
  784. case ExtraField::FIELD_TYPE_TEXTAREA:
  785. $form->addHtmlEditor(
  786. 'extra_'.$field_details['variable'],
  787. $field_details['display_text'],
  788. false,
  789. false,
  790. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  791. );
  792. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  793. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  794. if (!$admin_permissions) {
  795. if ($field_details['visible'] == 0) {
  796. $form->freeze(
  797. 'extra_'.$field_details['variable']
  798. );
  799. }
  800. }
  801. break;
  802. case ExtraField::FIELD_TYPE_RADIO:
  803. $group = array();
  804. if (isset($field_details['options']) && !empty($field_details['options'])) {
  805. foreach ($field_details['options'] as $option_details) {
  806. $options[$option_details['option_value']] = $option_details['display_text'];
  807. $group[] = $form->createElement(
  808. 'radio',
  809. 'extra_'.$field_details['variable'],
  810. $option_details['option_value'],
  811. $option_details['display_text'].'<br />',
  812. $option_details['option_value']
  813. );
  814. }
  815. }
  816. $form->addGroup(
  817. $group,
  818. 'extra_'.$field_details['variable'],
  819. $field_details['display_text'],
  820. ''
  821. );
  822. if (!$admin_permissions) {
  823. if ($field_details['visible'] == 0) {
  824. $form->freeze(
  825. 'extra_'.$field_details['variable']
  826. );
  827. }
  828. }
  829. break;
  830. case ExtraField::FIELD_TYPE_CHECKBOX:
  831. $group = array();
  832. if (isset($field_details['options']) && !empty($field_details['options'])) {
  833. foreach ($field_details['options'] as $option_details) {
  834. $options[$option_details['option_value']] = $option_details['display_text'];
  835. $group[] = $form->createElement(
  836. 'checkbox',
  837. 'extra_'.$field_details['variable'],
  838. $option_details['option_value'],
  839. $option_details['display_text'].'<br />',
  840. $option_details['option_value']
  841. );
  842. }
  843. } else {
  844. $fieldVariable = "extra_{$field_details['variable']}";
  845. $checkboxAttributes = array();
  846. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  847. if (!empty($extraData[$fieldVariable])) {
  848. $checkboxAttributes['checked'] = 1;
  849. }
  850. }
  851. // We assume that is a switch on/off with 1 and 0 as values
  852. $group[] = $form->createElement(
  853. 'checkbox',
  854. 'extra_'.$field_details['variable'],
  855. null,
  856. //$field_details['display_text'].'<br />',
  857. get_lang('Yes'),
  858. $checkboxAttributes
  859. );
  860. }
  861. $form->addGroup(
  862. $group,
  863. 'extra_'.$field_details['variable'],
  864. $field_details['display_text'],
  865. ''
  866. );
  867. if (!$admin_permissions) {
  868. if ($field_details['visible'] == 0) {
  869. $form->freeze(
  870. 'extra_'.$field_details['variable']
  871. );
  872. }
  873. }
  874. break;
  875. case ExtraField::FIELD_TYPE_SELECT:
  876. $get_lang_variables = false;
  877. if (in_array(
  878. $field_details['variable'],
  879. array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message')
  880. )
  881. ) {
  882. $get_lang_variables = true;
  883. }
  884. // Get extra field workflow
  885. $userInfo = api_get_user_info();
  886. $addOptions = array();
  887. $optionsExists = false;
  888. global $app;
  889. // Check if exist $app['orm.em'] object
  890. if (isset($app['orm.em']) && is_object($app['orm.em'])) {
  891. $optionsExists = $app['orm.em']
  892. ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  893. ->findOneBy(array('fieldId' => $field_details['id']));
  894. }
  895. if ($optionsExists) {
  896. if (isset($userInfo['status']) && !empty($userInfo['status'])) {
  897. $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  898. ->findBy(
  899. array(
  900. 'fieldId' => $field_details['id'],
  901. 'relatedFieldOptionId' => $defaultValueId,
  902. 'roleId' => $userInfo['status']
  903. )
  904. );
  905. foreach ($fieldWorkFlow as $item) {
  906. $addOptions[] = $item->getFieldOptionId();
  907. }
  908. }
  909. }
  910. $options = array();
  911. if (empty($defaultValueId)) {
  912. $options[''] = get_lang('SelectAnOption');
  913. }
  914. $optionList = array();
  915. if (!empty($field_details['options'])) {
  916. foreach ($field_details['options'] as $option_details) {
  917. $optionList[$option_details['id']] = $option_details;
  918. if ($get_lang_variables) {
  919. $options[$option_details['option_value']] = $option_details['display_text'];
  920. } else {
  921. if ($optionsExists) {
  922. // Adding always the default value
  923. if ($option_details['id'] == $defaultValueId) {
  924. $options[$option_details['option_value']] = $option_details['display_text'];
  925. } else {
  926. if (isset($addOptions) && !empty($addOptions)) {
  927. // Parsing filters
  928. if (in_array($option_details['id'], $addOptions)) {
  929. $options[$option_details['option_value']] = $option_details['display_text'];
  930. }
  931. }
  932. }
  933. } else {
  934. // Normal behaviour
  935. $options[$option_details['option_value']] = $option_details['display_text'];
  936. }
  937. }
  938. }
  939. if (isset($optionList[$defaultValueId])) {
  940. if (isset($optionList[$defaultValueId]['option_value']) &&
  941. $optionList[$defaultValueId]['option_value'] == 'aprobada'
  942. ) {
  943. // @todo function don't exists api_is_question_manager
  944. /*if (api_is_question_manager() == false) {
  945. $form->freeze();
  946. }*/
  947. }
  948. }
  949. // Setting priority message
  950. if (isset($optionList[$defaultValueId]) &&
  951. isset($optionList[$defaultValueId]['priority'])
  952. ) {
  953. if (!empty($optionList[$defaultValueId]['priority'])) {
  954. $priorityId = $optionList[$defaultValueId]['priority'];
  955. $option = new ExtraFieldOption($this->type);
  956. $messageType = $option->getPriorityMessageType($priorityId);
  957. $form->addElement(
  958. 'label',
  959. null,
  960. Display::return_message(
  961. $optionList[$defaultValueId]['priority_message'],
  962. $messageType
  963. )
  964. );
  965. }
  966. }
  967. }
  968. // chzn-select doesn't work for sessions??
  969. $form->addElement(
  970. 'select',
  971. 'extra_' . $field_details['variable'],
  972. $field_details['display_text'],
  973. $options,
  974. array('id' => 'extra_'.$field_details['variable'])
  975. );
  976. /* Enable this when field_loggeable is introduced as a table field (2.0)
  977. if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) {
  978. $form->addElement(
  979. 'textarea',
  980. 'extra_' . $field_details['variable'] . '_comment',
  981. $field_details['display_text'] . ' ' . get_lang('Comment')
  982. );
  983. $extraFieldValue = new ExtraFieldValue($this->type);
  984. $repo = $app['orm.em']->getRepository($extraFieldValue->entityName);
  985. $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry');
  986. $newEntity = $repo->findOneBy(
  987. array(
  988. $this->handlerEntityId => $itemId,
  989. 'fieldId' => $field_details['id']
  990. )
  991. );
  992. // @todo move this in a function inside the class
  993. if ($newEntity) {
  994. $logs = $repoLog->getLogEntries($newEntity);
  995. if (!empty($logs)) {
  996. $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />';
  997. $table = new HTML_Table(array('class' => 'data_table'));
  998. $table->setHeaderContents(0, 0, get_lang('Value'));
  999. $table->setHeaderContents(0, 1, get_lang('Comment'));
  1000. $table->setHeaderContents(0, 2, get_lang('ModifyDate'));
  1001. $table->setHeaderContents(0, 3, get_lang('Username'));
  1002. $row = 1;
  1003. foreach ($logs as $log) {
  1004. $column = 0;
  1005. $data = $log->getData();
  1006. $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null;
  1007. $comment = isset($data['comment']) ? $data['comment'] : null;
  1008. $table->setCellContents($row, $column, $fieldValue);
  1009. $column++;
  1010. $table->setCellContents($row, $column, $comment);
  1011. $column++;
  1012. $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s')));
  1013. $column++;
  1014. $table->setCellContents($row, $column, $log->getUsername());
  1015. $row++;
  1016. }
  1017. $form->addElement('label', null, $html.$table->toHtml());
  1018. }
  1019. }
  1020. }
  1021. */
  1022. if (!$admin_permissions) {
  1023. if ($field_details['visible'] == 0) {
  1024. $form->freeze('extra_' . $field_details['variable']);
  1025. }
  1026. }
  1027. break;
  1028. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  1029. $options = array();
  1030. foreach ($field_details['options'] as $option_id => $option_details) {
  1031. $options[$option_details['option_value']] = $option_details['display_text'];
  1032. }
  1033. $form->addElement(
  1034. 'select',
  1035. 'extra_'.$field_details['variable'],
  1036. $field_details['display_text'],
  1037. $options,
  1038. array('multiple' => 'multiple', 'id' => 'extra_'.$field_details['variable'])
  1039. );
  1040. if (!$admin_permissions) {
  1041. if ($field_details['visible'] == 0) {
  1042. $form->freeze('extra_'.$field_details['variable']);
  1043. }
  1044. }
  1045. break;
  1046. case ExtraField::FIELD_TYPE_DATE:
  1047. $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']);
  1048. if (!$admin_permissions) {
  1049. if ($field_details['visible'] == 0) {
  1050. $form->freeze('extra_'.$field_details['variable']);
  1051. }
  1052. }
  1053. $form->applyFilter('theme', 'trim');
  1054. break;
  1055. case ExtraField::FIELD_TYPE_DATETIME:
  1056. $form->addDateTimePicker(
  1057. 'extra_'.$field_details['variable'],
  1058. $field_details['display_text']
  1059. );
  1060. $defaults['extra_'.$field_details['variable']] = api_get_local_time();
  1061. if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) {
  1062. $form->setDefaults($defaults);
  1063. }
  1064. if (!$admin_permissions) {
  1065. if ($field_details['visible'] == 0) {
  1066. $form->freeze('extra_'.$field_details['variable']);
  1067. }
  1068. }
  1069. $form->applyFilter('theme', 'trim');
  1070. break;
  1071. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  1072. $first_select_id = 'first_extra_'.$field_details['variable'];
  1073. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  1074. $jquery_ready_content .= '
  1075. $("#'.$first_select_id.'").on("change", function() {
  1076. var id = $(this).val();
  1077. if (id) {
  1078. $.ajax({
  1079. url: "'.$url.'&a=get_second_select_options",
  1080. dataType: "json",
  1081. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  1082. success: function(data) {
  1083. $("#second_extra_'.$field_details['variable'].'").empty();
  1084. $.each(data, function(index, value) {
  1085. $("#second_extra_'.$field_details['variable'].'").append($("<option/>", {
  1086. value: index,
  1087. text: value
  1088. }));
  1089. });
  1090. $("#second_extra_'.$field_details['variable'].'").selectpicker("refresh");
  1091. },
  1092. });
  1093. } else {
  1094. $("#second_extra_'.$field_details['variable'].'").empty();
  1095. }
  1096. });';
  1097. $first_id = null;
  1098. if (!empty($extraData)) {
  1099. if (isset($extraData['extra_'.$field_details['variable']])) {
  1100. $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']];
  1101. }
  1102. }
  1103. $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
  1104. $field_details['options']
  1105. );
  1106. $values = array('' => get_lang('Select'));
  1107. $second_values = array();
  1108. if (!empty($options)) {
  1109. foreach ($options as $option) {
  1110. foreach ($option as $sub_option) {
  1111. if ($sub_option['option_value'] == '0') {
  1112. $values[$sub_option['id']] = $sub_option['display_text'];
  1113. } else {
  1114. if ($first_id === $sub_option['option_value']) {
  1115. $second_values[$sub_option['id']] = $sub_option['display_text'];
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. $group = array();
  1122. $group[] = $form->createElement(
  1123. 'select',
  1124. 'extra_'.$field_details['variable'],
  1125. null,
  1126. $values,
  1127. array('id' => $first_select_id)
  1128. );
  1129. $group[] = $form->createElement(
  1130. 'select',
  1131. 'extra_'.$field_details['variable'].'_second',
  1132. null,
  1133. $second_values,
  1134. array('id' => 'second_extra_'.$field_details['variable'])
  1135. );
  1136. $form->addGroup(
  1137. $group,
  1138. 'extra_'.$field_details['variable'],
  1139. $field_details['display_text'],
  1140. '&nbsp;'
  1141. );
  1142. if (!$admin_permissions) {
  1143. if ($field_details['visible'] == 0) {
  1144. $form->freeze('extra_'.$field_details['variable']);
  1145. }
  1146. }
  1147. break;
  1148. case ExtraField::FIELD_TYPE_DIVIDER:
  1149. $form->addElement(
  1150. 'static',
  1151. $field_details['variable'],
  1152. '<br /><strong>'.$field_details['display_text'].'</strong>'
  1153. );
  1154. break;
  1155. case ExtraField::FIELD_TYPE_TAG:
  1156. $variable = $field_details['variable'];
  1157. $field_id = $field_details['id'];
  1158. $tagsSelect = $form->addSelect(
  1159. "extra_{$field_details['variable']}",
  1160. $field_details['display_text']
  1161. );
  1162. if ($useTagAsSelect == false) {
  1163. $tagsSelect->setAttribute('class', null);
  1164. }
  1165. $tagsSelect->setAttribute('id', "extra_{$field_details['variable']}");
  1166. $tagsSelect->setMultiple(true);
  1167. if ($this->type == 'user') {
  1168. // The magic should be here
  1169. $user_tags = UserManager::get_user_tags($itemId, $field_details['id']);
  1170. if (is_array($user_tags) && count($user_tags) > 0) {
  1171. foreach ($user_tags as $tag) {
  1172. $tagsSelect->addOption(
  1173. $tag['tag'],
  1174. $tag['tag'],
  1175. ['selected' => 'selected', 'class' => 'selected']
  1176. );
  1177. }
  1178. }
  1179. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1180. } else {
  1181. $em = Database::getManager();
  1182. $fieldTags = $em
  1183. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1184. ->findBy([
  1185. 'fieldId' => $field_id,
  1186. 'itemId' => $itemId
  1187. ]);
  1188. foreach ($fieldTags as $fieldTag) {
  1189. $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
  1190. if (empty($tag)) {
  1191. continue;
  1192. }
  1193. $tagsSelect->addOption(
  1194. $tag->getTag(),
  1195. $tag->getTag(),
  1196. ['selected' => 'selected', 'class' => 'selected']
  1197. );
  1198. }
  1199. if ($useTagAsSelect) {
  1200. $fieldTags = $em
  1201. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1202. ->findBy([
  1203. 'fieldId' => $field_id
  1204. ]);
  1205. $tagsAdded = [];
  1206. foreach ($fieldTags as $fieldTag) {
  1207. $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
  1208. if (empty($tag)) {
  1209. continue;
  1210. }
  1211. $tagText = $tag->getTag();
  1212. if (in_array($tagText, $tagsAdded)) {
  1213. continue;
  1214. }
  1215. $tagsSelect->addOption(
  1216. $tag->getTag(),
  1217. $tag->getTag(),
  1218. []
  1219. );
  1220. $tagsAdded[] = $tagText;
  1221. }
  1222. }
  1223. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php';
  1224. }
  1225. if ($useTagAsSelect == false) {
  1226. $complete_text = get_lang('StartToType');
  1227. //if cache is set to true the jquery will be called 1 time
  1228. $jquery_ready_content .= <<<EOF
  1229. $("#extra_$variable").fcbkcomplete({
  1230. json_url: "$url?a=search_tags&field_id=$field_id&type={$this->type}",
  1231. cache: false,
  1232. filter_case: true,
  1233. filter_hide: true,
  1234. complete_text:"$complete_text",
  1235. firstselected: false,
  1236. filter_selected: true,
  1237. newel: true
  1238. });
  1239. EOF;
  1240. }
  1241. break;
  1242. case ExtraField::FIELD_TYPE_TIMEZONE:
  1243. $form->addElement(
  1244. 'select',
  1245. 'extra_'.$field_details['variable'],
  1246. $field_details['display_text'],
  1247. api_get_timezones(),
  1248. ''
  1249. );
  1250. if ($field_details['visible'] == 0) {
  1251. $form->freeze(
  1252. 'extra_'.$field_details['variable']
  1253. );
  1254. }
  1255. break;
  1256. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  1257. // get the social network's favicon
  1258. $extra_data_variable = isset($extraData['extra_'.$field_details['variable']]) ? $extraData['extra_'.$field_details['variable']] : null;
  1259. $field_default_value = isset($field_details['field_default_value']) ? $field_details['field_default_value'] : null;
  1260. $icon_path = UserManager::get_favicon_from_url(
  1261. $extra_data_variable,
  1262. $field_default_value
  1263. );
  1264. // special hack for hi5
  1265. $leftpad = '1.7';
  1266. $top = '0.4';
  1267. $domain = parse_url($icon_path, PHP_URL_HOST);
  1268. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  1269. $leftpad = '3';
  1270. $top = '0';
  1271. }
  1272. // print the input field
  1273. $form->addElement(
  1274. 'text',
  1275. 'extra_'.$field_details['variable'],
  1276. $field_details['display_text'],
  1277. array(
  1278. 'size' => 60,
  1279. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  1280. )
  1281. );
  1282. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1283. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1284. if ($field_details['visible'] == 0) {
  1285. $form->freeze('extra_'.$field_details['variable']);
  1286. }
  1287. break;
  1288. case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
  1289. $form->addElement(
  1290. 'text',
  1291. 'extra_'.$field_details[1],
  1292. $field_details[3]." (".get_lang('CountryDialCode').")",
  1293. array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx')
  1294. );
  1295. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  1296. $form->applyFilter('extra_'.$field_details[1], 'trim');
  1297. $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter');
  1298. $form->addRule(
  1299. 'extra_'.$field_details[1],
  1300. get_lang('MobilePhoneNumberWrong'),
  1301. 'mobile_phone_number'
  1302. );
  1303. if ($field_details['visible'] == 0) {
  1304. $form->freeze('extra_'.$field_details['variable']);
  1305. }
  1306. break;
  1307. case ExtraField::FIELD_TYPE_INTEGER:
  1308. $form->addElement(
  1309. 'number',
  1310. 'extra_'.$field_details['variable'],
  1311. $field_details['display_text'],
  1312. array('class' => 'span1', 'step' => 1)
  1313. );
  1314. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1315. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1316. $form->applyFilter('extra_'.$field_details['variable'], 'intval');
  1317. if (!$admin_permissions) {
  1318. if ($field_details['visible'] == 0) {
  1319. $form->freeze(
  1320. 'extra_'.$field_details['variable']
  1321. );
  1322. }
  1323. }
  1324. break;
  1325. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1326. $fieldVariable = "extra_{$field_details['variable']}";
  1327. $fieldTexts = [
  1328. $field_details['display_text']
  1329. ];
  1330. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  1331. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1332. $fieldTexts[] = Display::img(
  1333. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1334. $field_details['display_text'],
  1335. ['width' => '300']
  1336. );
  1337. }
  1338. }
  1339. if ($fieldTexts[0] === 'Image') {
  1340. $fieldTexts[0] = get_lang($fieldTexts[0]);
  1341. }
  1342. $form->addElement(
  1343. 'file',
  1344. $fieldVariable,
  1345. $fieldTexts,
  1346. ['accept' => 'image/*', 'id' => 'extra_image']
  1347. );
  1348. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1349. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1350. $allowed_picture_types = ['jpg', 'jpeg', 'png', 'gif'];
  1351. $form->addRule(
  1352. 'extra_'.$field_details['variable'],
  1353. get_lang('OnlyImagesAllowed') . ' ('.implode(',', $allowed_picture_types).')',
  1354. 'filetype',
  1355. $allowed_picture_types
  1356. );
  1357. if (!$admin_permissions) {
  1358. if ($field_details['visible'] == 0) {
  1359. $form->freeze(
  1360. 'extra_'.$field_details['variable']
  1361. );
  1362. }
  1363. }
  1364. break;
  1365. case ExtraField::FIELD_TYPE_FLOAT:
  1366. $form->addElement(
  1367. 'number',
  1368. 'extra_'.$field_details['variable'],
  1369. $field_details['display_text'],
  1370. array('class' => 'span1', 'step' => '0.01')
  1371. );
  1372. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1373. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1374. $form->applyFilter('extra_'.$field_details['variable'], 'floatval');
  1375. if (!$admin_permissions) {
  1376. if ($field_details['visible'] == 0) {
  1377. $form->freeze(
  1378. 'extra_'.$field_details['variable']
  1379. );
  1380. }
  1381. }
  1382. break;
  1383. case ExtraField::FIELD_TYPE_FILE:
  1384. $fieldVariable = "extra_{$field_details['variable']}";
  1385. $fieldTexts = array(
  1386. $field_details['display_text']
  1387. );
  1388. if (is_array($extraData) &&
  1389. array_key_exists($fieldVariable, $extraData)
  1390. ) {
  1391. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1392. $fieldTexts[] = Display::url(
  1393. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1394. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1395. array(
  1396. 'title' => $field_details['display_text'],
  1397. 'target' => '_blank'
  1398. )
  1399. );
  1400. }
  1401. }
  1402. $form->addElement(
  1403. 'file',
  1404. $fieldVariable,
  1405. $fieldTexts,
  1406. array()
  1407. );
  1408. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1409. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1410. if (!$admin_permissions) {
  1411. if ($field_details['visible'] == 0) {
  1412. $form->freeze(
  1413. 'extra_'.$field_details['variable']
  1414. );
  1415. }
  1416. }
  1417. break;
  1418. case ExtraField::FIELD_TYPE_VIDEO_URL:
  1419. $form->addUrl(
  1420. "extra_{$field_details['variable']}",
  1421. $field_details['display_text'],
  1422. false,
  1423. ['placeholder' => 'https://']
  1424. );
  1425. break;
  1426. case ExtraField::FIELD_TYPE_LETTERS_ONLY:
  1427. $form->addTextLettersOnly(
  1428. "extra_{$field_details['variable']}",
  1429. $field_details['display_text']
  1430. );
  1431. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1432. if (!$admin_permissions) {
  1433. if ($field_details['visible'] == 0) {
  1434. $form->freeze(
  1435. 'extra_' . $field_details['variable']
  1436. );
  1437. }
  1438. }
  1439. break;
  1440. case ExtraField::FIELD_TYPE_ALPHANUMERIC:
  1441. $form->addTextAlphanumeric(
  1442. "extra_{$field_details['variable']}",
  1443. $field_details['display_text']
  1444. );
  1445. $form->applyFilter(
  1446. 'extra_' . $field_details['variable'],
  1447. 'stripslashes'
  1448. );
  1449. if (!$admin_permissions) {
  1450. if ($field_details['visible'] == 0) {
  1451. $form->freeze(
  1452. 'extra_' . $field_details['variable']
  1453. );
  1454. }
  1455. }
  1456. break;
  1457. case ExtraField::FIELD_TYPE_LETTERS_SPACE:
  1458. $form->addTextLettersAndSpaces(
  1459. "extra_{$field_details['variable']}",
  1460. $field_details['display_text']
  1461. );
  1462. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1463. if (!$admin_permissions) {
  1464. if ($field_details['visible'] == 0) {
  1465. $form->freeze(
  1466. 'extra_' . $field_details['variable']
  1467. );
  1468. }
  1469. }
  1470. break;
  1471. case ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
  1472. $form->addTextAlphanumericAndSpaces(
  1473. "extra_{$field_details['variable']}",
  1474. $field_details['display_text']
  1475. );
  1476. $form->applyFilter(
  1477. 'extra_' . $field_details['variable'],
  1478. 'stripslashes'
  1479. );
  1480. if (!$admin_permissions) {
  1481. if ($field_details['visible'] == 0) {
  1482. $form->freeze(
  1483. 'extra_' . $field_details['variable']
  1484. );
  1485. }
  1486. }
  1487. break;
  1488. case ExtraField::FIELD_TYPE_GEOLOCALIZATION:
  1489. $dataValue = isset($extraData['extra_'.$field_details['variable']])
  1490. ? $extraData['extra_'.$field_details['variable']]
  1491. : '';
  1492. $form->addElement(
  1493. 'text',
  1494. 'extra_'.$field_details['variable'],
  1495. $field_details['display_text'],
  1496. ['id' => 'extra_'.$field_details['variable']]
  1497. );
  1498. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1499. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1500. if (!$admin_permissions) {
  1501. if ($field_details['visible'] == 0) {
  1502. $form->freeze(
  1503. 'extra_'.$field_details['variable']
  1504. );
  1505. }
  1506. }
  1507. $form->addHtml(
  1508. '<script>
  1509. $(document).ready(function() {
  1510. var address = "' . $dataValue . '";
  1511. initializeGeo'.$field_details['variable'].'(address, false);
  1512. $("#geolocalization_extra_'.$field_details['variable'].'").on("click", function() {
  1513. var address = $("#extra_'.$field_details['variable'].'").val();
  1514. initializeGeo'.$field_details['variable'].'(address, false);
  1515. return false;
  1516. });
  1517. $("#myLocation_extra_'.$field_details['variable'].'").on("click", function() {
  1518. myLocation'.$field_details['variable'].'();
  1519. return false;
  1520. });
  1521. $("#extra_'.$field_details['variable'].'").keypress(function (event) {
  1522. if (event.which == 13) {
  1523. $("#geolocalization_extra_'.$field_details['variable'].'").click();
  1524. return false;
  1525. }
  1526. });
  1527. });
  1528. function myLocation'.$field_details['variable'].'() {
  1529. if (navigator.geolocation) {
  1530. var geoPosition = function(position) {
  1531. var lat = position.coords.latitude;
  1532. var lng = position.coords.longitude;
  1533. var latLng = new google.maps.LatLng(lat, lng);
  1534. initializeGeo'.$field_details['variable'].'(false, latLng)
  1535. };
  1536. var geoError = function(error) {
  1537. alert("Geocode ' . get_lang('Error') . ': " + error);
  1538. };
  1539. var geoOptions = {
  1540. enableHighAccuracy: true
  1541. };
  1542. navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions);
  1543. }
  1544. }
  1545. function initializeGeo'.$field_details['variable'].'(address, latLng) {
  1546. var geocoder = new google.maps.Geocoder();
  1547. var latlng = new google.maps.LatLng(-34.397, 150.644);
  1548. var myOptions = {
  1549. zoom: 15,
  1550. center: latlng,
  1551. mapTypeControl: true,
  1552. mapTypeControlOptions: {
  1553. style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  1554. },
  1555. navigationControl: true,
  1556. mapTypeId: google.maps.MapTypeId.ROADMAP
  1557. };
  1558. map_'.$field_details['variable'].' = new google.maps.Map(document.getElementById("map_extra_'.$field_details['variable'].'"), myOptions);
  1559. var parameter = address ? { "address": address } : latLng ? { "latLng": latLng } : false;
  1560. if (geocoder && parameter) {
  1561. geocoder.geocode(parameter, function(results, status) {
  1562. if (status == google.maps.GeocoderStatus.OK) {
  1563. if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
  1564. map_'.$field_details['variable'].'.setCenter(results[0].geometry.location);
  1565. if (!address) {
  1566. $("#extra_'.$field_details['variable'].'").val(results[0].formatted_address);
  1567. }
  1568. var infowindow = new google.maps.InfoWindow({
  1569. content: "<b>" + $("#extra_'.$field_details['variable'].'").val() + "</b>",
  1570. size: new google.maps.Size(150, 50)
  1571. });
  1572. var marker = new google.maps.Marker({
  1573. position: results[0].geometry.location,
  1574. map: map_'.$field_details['variable'].',
  1575. title: $("#extra_'.$field_details['variable'].'").val()
  1576. });
  1577. google.maps.event.addListener(marker, "click", function() {
  1578. infowindow.open(map_'.$field_details['variable'].', marker);
  1579. });
  1580. } else {
  1581. alert("' . get_lang("NotFound") . '");
  1582. }
  1583. } else {
  1584. alert("Geocode ' . get_lang('Error') . ': " + status);
  1585. }
  1586. });
  1587. }
  1588. }
  1589. </script>
  1590. ');
  1591. $form->addHtml('
  1592. <div class="form-group">
  1593. <label for="geolocalization_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"></label>
  1594. <div class="col-sm-8">
  1595. <button class="null btn btn-default " id="geolocalization_extra_'.$field_details['variable'].'" name="geolocalization_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').'</button>
  1596. <button class="null btn btn-default " id="myLocation_extra_'.$field_details['variable'].'" name="myLocation_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-crosshairs"></em> '.get_lang('MyLocation').'</button>
  1597. </div>
  1598. </div>
  1599. ');
  1600. $form->addHtml('
  1601. <div class="form-group">
  1602. <label for="map_extra_'.$field_details['variable'].'" class="col-sm-2 control-label">
  1603. '.$field_details['display_text'].' - '.get_lang('Map').'
  1604. </label>
  1605. <div class="col-sm-8">
  1606. <div name="map_extra_'.$field_details['variable'].'" id="map_extra_'.$field_details['variable'].'" style="width:100%; height:300px;">
  1607. </div>
  1608. </div>
  1609. </div>
  1610. ');
  1611. break;
  1612. }
  1613. }
  1614. }
  1615. $return = array();
  1616. $return['jquery_ready_content'] = $jquery_ready_content;
  1617. return $return;
  1618. }
  1619. /**
  1620. * @param $breadcrumb
  1621. * @param $action
  1622. */
  1623. public function setupBreadcrumb(&$breadcrumb, $action)
  1624. {
  1625. if ($action == 'add') {
  1626. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1627. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  1628. } elseif ($action == 'edit') {
  1629. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1630. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  1631. } else {
  1632. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  1633. }
  1634. }
  1635. /**
  1636. * Displays the title + grid
  1637. */
  1638. public function display()
  1639. {
  1640. // action links
  1641. echo '<div class="actions">';
  1642. echo '<a href="../admin/index.php">'.Display::return_icon(
  1643. 'back.png',
  1644. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  1645. '',
  1646. ICON_SIZE_MEDIUM
  1647. ).'</a>';
  1648. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  1649. 'add_user_fields.png',
  1650. get_lang('Add'),
  1651. '',
  1652. ICON_SIZE_MEDIUM
  1653. ).'</a>';
  1654. echo '</div>';
  1655. echo Display::grid_html($this->type.'_fields');
  1656. }
  1657. /**
  1658. * @return array
  1659. */
  1660. public function getJqgridColumnNames()
  1661. {
  1662. return array(
  1663. get_lang('Name'),
  1664. get_lang('FieldLabel'),
  1665. get_lang('Type'),
  1666. get_lang('FieldChangeability'),
  1667. get_lang('Visibility'),
  1668. get_lang('Filter'),
  1669. get_lang('FieldOrder'),
  1670. get_lang('Actions')
  1671. );
  1672. }
  1673. /**
  1674. * @return array
  1675. */
  1676. public function getJqgridColumnModel()
  1677. {
  1678. return array(
  1679. array(
  1680. 'name' => 'display_text',
  1681. 'index' => 'display_text',
  1682. 'width' => '180',
  1683. 'align' => 'left',
  1684. ),
  1685. array(
  1686. 'name' => 'variable',
  1687. 'index' => 'variable',
  1688. 'width' => '',
  1689. 'align' => 'left',
  1690. 'sortable' => 'true',
  1691. ),
  1692. array(
  1693. 'name' => 'field_type',
  1694. 'index' => 'field_type',
  1695. 'width' => '',
  1696. 'align' => 'left',
  1697. 'sortable' => 'true',
  1698. ),
  1699. array(
  1700. 'name' => 'changeable',
  1701. 'index' => 'changeable',
  1702. 'width' => '50',
  1703. 'align' => 'left',
  1704. 'sortable' => 'true',
  1705. ),
  1706. array(
  1707. 'name' => 'visible',
  1708. 'index' => 'visible',
  1709. 'width' => '40',
  1710. 'align' => 'left',
  1711. 'sortable' => 'true',
  1712. ),
  1713. array(
  1714. 'name' => 'filter',
  1715. 'index' => 'filter',
  1716. 'width' => '30',
  1717. 'align' => 'left',
  1718. 'sortable' => 'true',
  1719. ),
  1720. array(
  1721. 'name' => 'field_order',
  1722. 'index' => 'field_order',
  1723. 'width' => '40',
  1724. 'align' => 'left',
  1725. 'sortable' => 'true',
  1726. ),
  1727. array(
  1728. 'name' => 'actions',
  1729. 'index' => 'actions',
  1730. 'width' => '100',
  1731. 'align' => 'left',
  1732. 'formatter' => 'action_formatter',
  1733. 'sortable' => 'false',
  1734. ),
  1735. );
  1736. }
  1737. /**
  1738. * @param string $url
  1739. * @param string $action
  1740. * @return FormValidator
  1741. */
  1742. public function return_form($url, $action)
  1743. {
  1744. $form = new FormValidator($this->type.'_field', 'post', $url);
  1745. $form->addElement('hidden', 'type', $this->type);
  1746. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  1747. $form->addElement('hidden', 'id', $id);
  1748. // Setting the form elements
  1749. $header = get_lang('Add');
  1750. $defaults = array();
  1751. if ($action == 'edit') {
  1752. $header = get_lang('Modify');
  1753. // Setting the defaults
  1754. $defaults = $this->get($id, false);
  1755. }
  1756. $form->addElement('header', $header);
  1757. if ($action == 'edit') {
  1758. $translateUrl = api_get_path(WEB_CODE_PATH) . 'extrafield/translate.php?' . http_build_query([
  1759. 'extra_field' => $id
  1760. ]);
  1761. $translateButton = Display::toolbarButton(get_lang('TranslateThisTerm'), $translateUrl, 'language', 'link');
  1762. $form->addText(
  1763. 'display_text',
  1764. [get_lang('Name'), $translateButton]
  1765. );
  1766. } else {
  1767. $form->addElement('text', 'display_text', get_lang('Name'));
  1768. }
  1769. // Field type
  1770. $types = self::get_field_types();
  1771. $form->addElement(
  1772. 'select',
  1773. 'field_type',
  1774. get_lang('FieldType'),
  1775. $types,
  1776. array('id' => 'field_type')
  1777. );
  1778. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  1779. $form->addElement('text', 'variable', get_lang('FieldLabel'), array('class' => 'span5'));
  1780. $form->addElement(
  1781. 'text',
  1782. 'field_options',
  1783. get_lang('FieldPossibleValues'),
  1784. array('id' => 'field_options', 'class' => 'span6')
  1785. );
  1786. $fieldWithOptions = array(
  1787. ExtraField::FIELD_TYPE_RADIO,
  1788. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  1789. ExtraField::FIELD_TYPE_SELECT,
  1790. ExtraField::FIELD_TYPE_TAG,
  1791. ExtraField::FIELD_TYPE_DOUBLE_SELECT,
  1792. );
  1793. if ($action == 'edit') {
  1794. if (in_array($defaults['field_type'], $fieldWithOptions)) {
  1795. $url = Display::url(
  1796. get_lang('EditExtraFieldOptions'),
  1797. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  1798. );
  1799. $form->addElement('label', null, $url);
  1800. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_SELECT) {
  1801. $urlWorkFlow = Display::url(
  1802. get_lang('EditExtraFieldWorkFlow'),
  1803. 'extra_field_workflow.php?type='.$this->type.'&field_id='.$id
  1804. );
  1805. $form->addElement('label', null, $urlWorkFlow);
  1806. }
  1807. $form->freeze('field_options');
  1808. }
  1809. }
  1810. $form->addElement(
  1811. 'text',
  1812. 'default_value',
  1813. get_lang('FieldDefaultValue'),
  1814. array('id' => 'default_value')
  1815. );
  1816. $group = array();
  1817. $group[] = $form->createElement('radio', 'visible', null, get_lang('Yes'), 1);
  1818. $group[] = $form->createElement('radio', 'visible', null, get_lang('No'), 0);
  1819. $form->addGroup($group, '', get_lang('Visible'), '', false);
  1820. $group = array();
  1821. $group[] = $form->createElement('radio', 'changeable', null, get_lang('Yes'), 1);
  1822. $group[] = $form->createElement('radio', 'changeable', null, get_lang('No'), 0);
  1823. $form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
  1824. $group = array();
  1825. $group[] = $form->createElement('radio', 'filter', null, get_lang('Yes'), 1);
  1826. $group[] = $form->createElement('radio', 'filter', null, get_lang('No'), 0);
  1827. $form->addGroup($group, '', get_lang('FieldFilter'), '', false);
  1828. /* Enable this when field_loggeable is introduced as a table field (2.0)
  1829. $group = array();
  1830. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('Yes'), 1);
  1831. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('No'), 0);
  1832. $form->addGroup($group, '', get_lang('FieldLoggeable'), '', false);
  1833. */
  1834. $form->addElement('text', 'field_order', get_lang('FieldOrder'));
  1835. if ($action == 'edit') {
  1836. $option = new ExtraFieldOption($this->type);
  1837. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1838. $form->freeze('field_options');
  1839. }
  1840. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  1841. $form->addButtonUpdate(get_lang('Modify'));
  1842. } else {
  1843. $defaults['visible'] = 0;
  1844. $defaults['changeable'] = 0;
  1845. $defaults['filter'] = 0;
  1846. $form->addButtonCreate(get_lang('Add'));
  1847. }
  1848. /*if (!empty($defaults['created_at'])) {
  1849. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  1850. }
  1851. if (!empty($defaults['updated_at'])) {
  1852. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  1853. }*/
  1854. $form->setDefaults($defaults);
  1855. // Setting the rules
  1856. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  1857. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  1858. return $form;
  1859. }
  1860. /**
  1861. * @param $token
  1862. * @return string
  1863. */
  1864. public function getJqgridActionLinks($token)
  1865. {
  1866. //With this function we can add actions to the jgrid (edit, delete, etc)
  1867. $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
  1868. $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  1869. $confirmMessage = addslashes(
  1870. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  1871. );
  1872. $editButton = <<<JAVASCRIPT
  1873. <a href="?action=edit&type={$this->type}&id=' + options.rowId + '" class="btn btn-link btn-xs">\
  1874. $editIcon\
  1875. </a>
  1876. JAVASCRIPT;
  1877. $deleteButton = <<<JAVASCRIPT
  1878. <a \
  1879. onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \
  1880. href="?sec_token=$token&type={$this->type}&id=' + options.rowId + '&action=delete" \
  1881. class="btn btn-link btn-xs">\
  1882. $deleteIcon\
  1883. </a>
  1884. JAVASCRIPT;
  1885. return "function action_formatter(cellvalue, options, rowObject) {
  1886. console.log(options);
  1887. return '$editButton $deleteButton';
  1888. }";
  1889. }
  1890. /**
  1891. * @param array $columns
  1892. * @param array $column_model
  1893. * @param array $extraFields
  1894. * @return array
  1895. */
  1896. public function getRules(&$columns, &$column_model, $extraFields = array(), $checkExtraFieldExistence = false)
  1897. {
  1898. $fields = $this->get_all(
  1899. array(
  1900. 'visible = ? AND filter = ?' => array(1, 1)
  1901. ),
  1902. 'display_text'
  1903. );
  1904. $extraFieldOption = new ExtraFieldOption($this->type);
  1905. $rules = array();
  1906. if (!empty($fields)) {
  1907. foreach ($fields as $field) {
  1908. $search_options = array();
  1909. $type = 'text';
  1910. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  1911. $type = 'select';
  1912. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  1913. } else {
  1914. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  1915. }
  1916. $search_options['searchhidden'] = 'true';
  1917. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  1918. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  1919. //Add 2 selects
  1920. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  1921. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  1922. $first_options = array();
  1923. if (!empty($options)) {
  1924. foreach ($options as $option) {
  1925. foreach ($option as $sub_option) {
  1926. if ($sub_option['option_value'] == 0) {
  1927. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['display_text'];
  1928. }
  1929. }
  1930. }
  1931. }
  1932. $search_options['value'] = implode(';', $first_options);
  1933. $search_options['dataInit'] = 'fill_second_select';
  1934. // First
  1935. $column_model[] = array(
  1936. 'name' => 'extra_'.$field['variable'],
  1937. 'index' => 'extra_'.$field['variable'],
  1938. 'width' => '100',
  1939. 'hidden' => 'true',
  1940. 'search' => 'true',
  1941. 'stype' => 'select',
  1942. 'searchoptions' => $search_options,
  1943. );
  1944. $columns[] = $field['display_text'].' (1)';
  1945. $rules[] = array(
  1946. 'field' => 'extra_'.$field['variable'],
  1947. 'op' => 'cn',
  1948. );
  1949. //Second
  1950. $search_options['value'] = $field['id'].':';
  1951. $search_options['dataInit'] = 'register_second_select';
  1952. $column_model[] = array(
  1953. 'name' => 'extra_'.$field['variable'].'_second',
  1954. 'index' => 'extra_'.$field['variable'].'_second',
  1955. 'width' => '100',
  1956. 'hidden' => 'true',
  1957. 'search' => 'true',
  1958. 'stype' => 'select',
  1959. 'searchoptions' => $search_options,
  1960. );
  1961. $columns[] = $field['display_text'].' (2)';
  1962. $rules[] = array('field' => 'extra_'.$field['variable'].'_second', 'op' => 'cn');
  1963. continue;
  1964. } else {
  1965. $search_options['value'] = $extraFieldOption->getFieldOptionsToString(
  1966. $field['id'],
  1967. false,
  1968. 'display_text'
  1969. );
  1970. }
  1971. $column_model[] = array(
  1972. 'name' => 'extra_'.$field['variable'],
  1973. 'index' => 'extra_'.$field['variable'],
  1974. 'width' => '100',
  1975. 'hidden' => 'true',
  1976. 'search' => 'true',
  1977. 'stype' => $type,
  1978. 'searchoptions' => $search_options
  1979. );
  1980. $columns[] = $field['display_text'];
  1981. $rules[] = array('field' => 'extra_'.$field['variable'], 'op' => 'cn');
  1982. }
  1983. }
  1984. return $rules;
  1985. }
  1986. /**
  1987. * @param array $options
  1988. * @return array
  1989. */
  1990. public function parseConditions($options)
  1991. {
  1992. $inject_extra_fields = null;
  1993. $extraFieldOption = new ExtraFieldOption($this->type);
  1994. $double_fields = array();
  1995. if (isset($options['extra'])) {
  1996. $extra_fields = $options['extra'];
  1997. if (!empty($extra_fields)) {
  1998. $counter = 1;
  1999. foreach ($extra_fields as &$extra) {
  2000. $extra_field_obj = new ExtraField($this->type);
  2001. $extra_field_info = $extra_field_obj->get($extra['id']);
  2002. $extra['extra_field_info'] = $extra_field_info;
  2003. if (isset($extra_field_info['field_type']) && in_array(
  2004. $extra_field_info['field_type'],
  2005. array(
  2006. ExtraField::FIELD_TYPE_SELECT,
  2007. ExtraField::FIELD_TYPE_SELECT,
  2008. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  2009. )
  2010. )
  2011. ) {
  2012. $inject_extra_fields .= " fvo$counter.display_text as {$extra['field']}, ";
  2013. } else {
  2014. $inject_extra_fields .= " fv$counter.value as {$extra['field']}, ";
  2015. }
  2016. if (isset($extra_fields_info[$extra['id']])) {
  2017. $info = $extra_fields_info[$extra['id']];
  2018. } else {
  2019. $info = $this->get($extra['id']);
  2020. $extra_fields_info[$extra['id']] = $info;
  2021. }
  2022. if (isset($info['field_type']) && $info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  2023. $double_fields[$info['id']] = $info;
  2024. }
  2025. $counter++;
  2026. }
  2027. }
  2028. }
  2029. $options_by_double = array();
  2030. foreach ($double_fields as $double) {
  2031. $my_options = $extraFieldOption->get_field_options_by_field(
  2032. $double['id'],
  2033. true
  2034. );
  2035. $options_by_double['extra_'.$double['variable']] = $my_options;
  2036. }
  2037. $field_value_to_join = array();
  2038. //filter can be all/any = and/or
  2039. $inject_joins = null;
  2040. $inject_where = null;
  2041. $where = null;
  2042. if (!empty($options['where'])) {
  2043. if (!empty($options['extra'])) {
  2044. // Removing double 1=1
  2045. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  2046. // Always OR
  2047. $counter = 1;
  2048. foreach ($extra_fields as $extra_info) {
  2049. $extra_field_info = $extra_info['extra_field_info'];
  2050. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter
  2051. ON (s." . $this->primaryKey . " = fv$counter." . $this->handler_id . ") ";
  2052. // Add options
  2053. if (isset($extra_field_info['field_type']) && in_array(
  2054. $extra_field_info['field_type'],
  2055. array(
  2056. ExtraField::FIELD_TYPE_SELECT,
  2057. ExtraField::FIELD_TYPE_SELECT,
  2058. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  2059. )
  2060. )
  2061. ) {
  2062. $options['where'] = str_replace(
  2063. $extra_info['field'],
  2064. 'fv' . $counter . '.field_id = ' . $extra_info['id'] . ' AND fvo' . $counter . '.option_value',
  2065. $options['where']
  2066. );
  2067. $inject_joins .= "
  2068. INNER JOIN $this->table_field_options fvo$counter
  2069. ON (
  2070. fv$counter.field_id = fvo$counter.field_id AND
  2071. fv$counter.value = fvo$counter.option_value
  2072. )
  2073. ";
  2074. } else if (isset($extra_field_info['field_type']) &&
  2075. $extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG
  2076. ) {
  2077. $options['where'] = str_replace(
  2078. $extra_info['field'],
  2079. 'tag' . $counter . '.tag ',
  2080. $options['where']
  2081. );
  2082. $inject_joins .= "
  2083. INNER JOIN $this->table_field_rel_tag tag_rel$counter
  2084. ON (tag_rel$counter.field_id = ".$extra_info['id']." AND tag_rel$counter.item_id = s." . $this->primaryKey.")
  2085. INNER JOIN $this->table_field_tag tag$counter
  2086. ON (tag$counter.id = tag_rel$counter.tag_id)
  2087. ";
  2088. } else {
  2089. //text, textarea, etc
  2090. $options['where'] = str_replace(
  2091. $extra_info['field'],
  2092. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.value',
  2093. $options['where']
  2094. );
  2095. }
  2096. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  2097. $counter++;
  2098. }
  2099. if (!empty($field_value_to_join)) {
  2100. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  2101. }
  2102. }
  2103. $where .= ' AND '.$options['where'];
  2104. }
  2105. $order = null;
  2106. if (!empty($options['order'])) {
  2107. $order = " ORDER BY ".$options['order'];
  2108. }
  2109. $limit = null;
  2110. if (!empty($options['limit'])) {
  2111. $limit = " LIMIT ".$options['limit'];
  2112. }
  2113. return array(
  2114. 'order' => $order,
  2115. 'limit' => $limit,
  2116. 'where' => $where,
  2117. 'inject_where' => $inject_where,
  2118. 'inject_joins' => $inject_joins,
  2119. 'field_value_to_join' => $field_value_to_join,
  2120. 'inject_extra_fields' => $inject_extra_fields,
  2121. );
  2122. }
  2123. //@todo move this in the display_class or somewhere else
  2124. /**
  2125. * @param $col
  2126. * @param $oper
  2127. * @param $val
  2128. * @return string
  2129. */
  2130. public function get_where_clause($col, $oper, $val)
  2131. {
  2132. if (empty($col)) {
  2133. return '';
  2134. }
  2135. if ($oper == 'bw' || $oper == 'bn') {
  2136. $val .= '%';
  2137. }
  2138. if ($oper == 'ew' || $oper == 'en') {
  2139. $val = '%'.$val;
  2140. }
  2141. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  2142. if (is_array($val)) {
  2143. $result = '"%'.implode(';', $val).'%"';
  2144. foreach ($val as $item) {
  2145. $result .= ' OR '.$col.' LIKE "%'.$item.'%"';
  2146. }
  2147. $val = $result;
  2148. return " $col {$this->ops[$oper]} $val ";
  2149. } else {
  2150. $val = '%'.$val.'%';
  2151. }
  2152. }
  2153. $val = \Database::escape_string($val);
  2154. return " $col {$this->ops[$oper]} '$val' ";
  2155. }
  2156. /**
  2157. * @param $filters
  2158. * @param string $stringToSearch
  2159. * @return array
  2160. */
  2161. public function getExtraFieldRules($filters, $stringToSearch = 'extra_')
  2162. {
  2163. $extra_fields = array();
  2164. // Getting double select if exists
  2165. $double_select = array();
  2166. foreach ($filters->rules as $rule) {
  2167. if (strpos($rule->field, '_second') === false) {
  2168. } else {
  2169. $my_field = str_replace('_second', '', $rule->field);
  2170. $double_select[$my_field] = $rule->data;
  2171. }
  2172. }
  2173. $condition_array = array();
  2174. foreach ($filters->rules as $rule) {
  2175. if (strpos($rule->field, $stringToSearch) === false) {
  2176. // normal fields
  2177. $field = $rule->field;
  2178. if (isset($rule->data) && is_string($rule->data) && $rule->data != -1) {
  2179. $condition_array[] = $this->get_where_clause($field, $rule->op, $rule->data);
  2180. }
  2181. } else {
  2182. // Extra fields
  2183. if (strpos($rule->field, '_second') === false) {
  2184. //No _second
  2185. $original_field = str_replace($stringToSearch, '', $rule->field);
  2186. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  2187. if ($field_option['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  2188. if (isset($double_select[$rule->field])) {
  2189. $data = explode('#', $rule->data);
  2190. $rule->data = $data[1].'::'.$double_select[$rule->field];
  2191. } else {
  2192. // only was sent 1 select
  2193. if (is_string($rule->data)) {
  2194. $data = explode('#', $rule->data);
  2195. $rule->data = $data[1];
  2196. }
  2197. }
  2198. if (!isset($rule->data)) {
  2199. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  2200. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  2201. }
  2202. } else {
  2203. if (isset($rule->data)) {
  2204. if ($rule->data == -1) {
  2205. continue;
  2206. }
  2207. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  2208. $extra_fields[] = array(
  2209. 'field' => $rule->field,
  2210. 'id' => $field_option['id'],
  2211. 'data' => $rule->data
  2212. );
  2213. }
  2214. }
  2215. } else {
  2216. $my_field = str_replace('_second', '', $rule->field);
  2217. $original_field = str_replace($stringToSearch, '', $my_field);
  2218. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  2219. $extra_fields[] = array(
  2220. 'field' => $rule->field,
  2221. 'id' => $field_option['id']
  2222. );
  2223. }
  2224. }
  2225. }
  2226. return array(
  2227. 'extra_fields' => $extra_fields,
  2228. 'condition_array' => $condition_array
  2229. );
  2230. }
  2231. /**
  2232. * Get the extra fields and their formatted values
  2233. * @param int|string $itemId The item ID (It could be a session_id, course_id or user_id)
  2234. * @return array The extra fields data
  2235. */
  2236. public function getDataAndFormattedValues($itemId)
  2237. {
  2238. $valuesData = array();
  2239. $fields = $this->get_all();
  2240. foreach ($fields as $field) {
  2241. if ($field['visible'] != '1') {
  2242. continue;
  2243. }
  2244. $fieldValue = new ExtraFieldValue($this->type);
  2245. $valueData = $fieldValue->get_values_by_handler_and_field_id($itemId, $field['id'], true);
  2246. if (!$valueData) {
  2247. continue;
  2248. }
  2249. $displayedValue = get_lang('None');
  2250. switch ($field['field_type']) {
  2251. case ExtraField::FIELD_TYPE_CHECKBOX:
  2252. if ($valueData !== false && $valueData['value'] == '1') {
  2253. $displayedValue = get_lang('Yes');
  2254. } else {
  2255. $displayedValue = get_lang('No');
  2256. }
  2257. break;
  2258. case ExtraField::FIELD_TYPE_DATE:
  2259. if ($valueData !== false && !empty($valueData['value'])) {
  2260. $displayedValue = api_format_date($valueData['value'], DATE_FORMAT_LONG_NO_DAY);
  2261. }
  2262. break;
  2263. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  2264. if ($valueData === false || empty($valueData['value'])) {
  2265. break;
  2266. }
  2267. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  2268. break;
  2269. }
  2270. $image = Display::img(
  2271. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2272. $field['display_text'],
  2273. array('width' => '300')
  2274. );
  2275. $displayedValue = Display::url(
  2276. $image,
  2277. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2278. array('target' => '_blank')
  2279. );
  2280. break;
  2281. case ExtraField::FIELD_TYPE_FILE:
  2282. if ($valueData === false || empty($valueData['value'])) {
  2283. break;
  2284. }
  2285. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  2286. break;
  2287. }
  2288. $displayedValue = Display::url(
  2289. get_lang('Download'),
  2290. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2291. array(
  2292. 'title' => $field['display_text'],
  2293. 'target' => '_blank'
  2294. )
  2295. );
  2296. break;
  2297. default:
  2298. $displayedValue = $valueData['value'];
  2299. break;
  2300. }
  2301. $valuesData[] = array(
  2302. 'text' => $field['display_text'],
  2303. 'value' => $displayedValue
  2304. );
  2305. }
  2306. return $valuesData;
  2307. }
  2308. /**
  2309. * Gets an element
  2310. * @param int $id
  2311. * @param bool $translateDisplayText Optional
  2312. * @return array
  2313. */
  2314. public function get($id, $translateDisplayText = true)
  2315. {
  2316. $info = parent::get($id);
  2317. if ($translateDisplayText) {
  2318. $info['display_text'] = self::translateDisplayName($info['variable'], $info['display_text']);
  2319. }
  2320. return $info;
  2321. }
  2322. /**
  2323. * Translate the display text for a extra field
  2324. * @param string $variable
  2325. * @param string $defaultDisplayText
  2326. * @return string
  2327. */
  2328. public static function translateDisplayName($variable, $defaultDisplayText)
  2329. {
  2330. $camelCase = api_underscore_to_camel_case($variable);
  2331. return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $defaultDisplayText;
  2332. }
  2333. /**
  2334. * @param int $fieldId
  2335. * @param string $tag
  2336. *
  2337. * @return array
  2338. */
  2339. public function getAllUserPerTag($fieldId, $tag)
  2340. {
  2341. $tagRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  2342. $tag = Database::escape_string($tag);
  2343. $fieldId = (int) $fieldId;
  2344. $sql = "SELECT user_id
  2345. FROM {$this->table_field_tag} f INNER JOIN $tagRelUserTable ft
  2346. ON tag_id = f.id
  2347. WHERE tag = '$tag' AND f.field_id = $fieldId;
  2348. ";
  2349. $result = Database::query($sql);
  2350. return Database::store_result($result, 'ASSOC');
  2351. }
  2352. }