extra_field.lib.php 132 KB

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