extra_field.lib.php 94 KB

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