extra_field.lib.php 108 KB

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