permissions_functions.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. /**
  3. * This files contains the common functions for the permissions
  4. *
  5. * A list of all the functions (in no particular order)
  6. * ----------------------------------------------------
  7. * store_permissions($content,$id)
  8. * get_permissions($content,$id)
  9. * limited_or_full($current_permissions)
  10. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  11. * @package chamilo.permissions
  12. */
  13. /**
  14. * This function stores the permissions in the correct table.
  15. * Since Checkboxes are used we do not know which ones are unchecked.
  16. * That's why we first delete them all (for the given user/group/role
  17. * and afterwards we store the checked ones only.
  18. * @param $content are we storing rights for a user, a group or a role (the database depends on it)
  19. * @param $id the id of the user, group or role
  20. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  21. * @version 1.0
  22. */
  23. function store_permissions($content, $id) {
  24. $course_id = api_get_course_int_id();
  25. // Which database are we using (depending on the $content parameter)
  26. if($content=='user')
  27. {
  28. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  29. $id_field = user_id;
  30. }
  31. if($content=='group')
  32. {
  33. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  34. $id_field = group_id;
  35. }
  36. if($content=='role')
  37. {
  38. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  39. $id_field = role_id;
  40. }
  41. // We first delete all the existing permissions for that user/group/role
  42. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."'";
  43. $result=Database::query($sql);
  44. // looping through the post values to find the permission (containing the string permission* )
  45. foreach ($_POST as $key => $value)
  46. {
  47. if(strstr($key,"permission*"))
  48. {
  49. list($brol,$tool,$action)=explode("*",$key);
  50. $sql="INSERT INTO $table (c_id, $id_field,tool,action) VALUES ($course_id, '".Database::escape_string($id)."','".Database::escape_string($tool)."','".Database::escape_string($action)."')";
  51. $result=Database::query($sql);
  52. }
  53. }
  54. return get_lang('PermissionsStored');
  55. }
  56. /**
  57. * This function stores one permission in the correct table.
  58. * @param $content are we storing rights for a user, a group or a role (the database depends on it)
  59. * @param $action are we granting or revoking a permission?
  60. * @param $id the id of the user, group or role
  61. * @param $tool the tool
  62. * @param $permission the permission the user, group or role has been granted or revoked
  63. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  64. * @version 1.0
  65. */
  66. function store_one_permission($content, $action, $id, $tool,$permission) {
  67. global $rights_full;
  68. $course_id = api_get_course_int_id();
  69. // for some reason I don't know, he can't get to the $rights_full array, so commented the following lines out.
  70. // check
  71. //if(!in_array($permission, $rights_full))
  72. //{
  73. // return get_lang('Error');
  74. //}
  75. // Which database are we using (depending on the $content parameter)
  76. if($content=='user')
  77. {
  78. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  79. $id_field = user_id;
  80. }
  81. if($content=='group')
  82. {
  83. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  84. $id_field = group_id;
  85. }
  86. if($content=='role')
  87. {
  88. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  89. $id_field = role_id;
  90. }
  91. // grating a right
  92. if($action=='grant') {
  93. $sql="INSERT INTO $table (c_id, $id_field,tool,action) VALUES ($course_id, '".Database::escape_string($id)."','".Database::escape_string($tool)."','".Database::escape_string($permission)."')";
  94. $result=Database::query($sql);
  95. if($result)
  96. {
  97. $result_message=get_lang('PermissionGranted');
  98. }
  99. }
  100. if($action=='revoke')
  101. {
  102. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."' AND tool='".Database::escape_string($tool)."' AND action='".Database::escape_string($permission)."'";
  103. $result=Database::query($sql);
  104. if($result) {
  105. $result_message=get_lang('PermissionRevoked');
  106. }
  107. }
  108. return $result_message;
  109. }
  110. /**
  111. * This function retrieves the existing permissions of a user, group or role.
  112. * @param $content are we retrieving the rights of a user, a group or a role (the database depends on it)
  113. * @param $id the id of the user, group or role
  114. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  115. * @version 1.0
  116. */
  117. function get_permissions($content, $id) {
  118. $course_id = api_get_course_int_id();
  119. $currentpermissions=array();
  120. // Which database are we using (depending on the $content parameter)
  121. $course_id_condition = " c_id = $course_id AND ";
  122. if($content == 'user')
  123. {
  124. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  125. $id_field = 'user_id';
  126. }
  127. elseif($content == 'group')
  128. {
  129. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  130. $id_field = 'group_id';
  131. }
  132. elseif($content == 'role')
  133. {
  134. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  135. $id_field = 'role_id';
  136. }
  137. elseif($content == 'platform_role')
  138. {
  139. $table=Database::get_main_table(TABLE_ROLE_PERMISSION);
  140. $id_field = 'role_id';
  141. $course_id_condition = '';
  142. }
  143. elseif($content == 'task')
  144. {
  145. $table=Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
  146. $id_field = 'task_id';
  147. }
  148. // finding all the permissions. We store this in a multidimensional array
  149. // where the first dimension is the tool.
  150. $sql="
  151. SELECT * FROM " . $table . "
  152. WHERE $course_id_condition " . $id_field . "='" . Database::escape_string($id) . "'";
  153. $result = Database::query($sql);
  154. while($row = Database::fetch_array($result))
  155. $currentpermissions[$row['tool']][] = $row['action'];
  156. return $currentpermissions;
  157. }
  158. /**
  159. * the array that contains the current permission a user, group or role has will now be changed depending on
  160. * the Dokeos Config Setting for the permissions (limited [add, edit, delete] or full [view, add, edit, delete, move, visibility]
  161. * @param $content are we retrieving the rights of a user, a group or a role (the database depends on it)
  162. * @param $id the id of the user, group or role
  163. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  164. * @version 1.0
  165. * @todo currently there is a setting user_permissions and group_permissions. We should merge this in one config setting.
  166. */
  167. function limited_or_full($current_permissions)
  168. {
  169. if(api_get_setting('permissions')=='limited')
  170. {
  171. foreach ($current_permissions as $tool=>$tool_rights)
  172. {
  173. // we loop through the possible permissions of a tool and unset the entry if it is view
  174. // if it is visibility or move we have to grant the edit right
  175. foreach ($tool_rights as $key=>$value)
  176. {
  177. if($value=='View')
  178. {
  179. unset($current_permissions[$tool][$key]);
  180. }
  181. if($value=='Visibility' OR $value=='Move')
  182. {
  183. if(!in_array('Edit',$current_permissions[$tool]))
  184. {
  185. $current_permissions[$tool][]='Edit';
  186. }
  187. unset($current_permissions[$tool][$key]);
  188. }
  189. //else
  190. //{
  191. // $current_permissions[$tool][]=$value;
  192. //}
  193. }
  194. }
  195. return $current_permissions;
  196. }
  197. if(api_get_setting('permissions')=='full')
  198. {
  199. return $current_permissions;
  200. }
  201. }
  202. /**
  203. * This function displays a checked or unchecked checkbox. The checkbox will be checked if the
  204. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  205. * does not have the right
  206. * @param $permission_array the array that contains all the permissions of the user, group, role
  207. * @param $tool the tool we want to check a permission for
  208. * @param $permission the permission we want to check for
  209. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  210. * @version 1.0
  211. */
  212. function display_checkbox_matrix($permission_array, $tool, $permission, $inherited_permissions=array())
  213. {
  214. $checked="";
  215. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  216. {
  217. $checked="checked";
  218. }
  219. echo "\t\t\t<input type=\"checkbox\" name=\"permission*$tool*$permission\" $checked>\n";
  220. }
  221. /**
  222. * This function displays a checked or unchecked image. The image will be checked if the
  223. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  224. * does not have the right
  225. * @param $permission_array the array that contains all the permissions of the user, group, role
  226. * @param $tool the tool we want to check a permission for
  227. * @param $permission the permission we want to check for
  228. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  229. * @version 1.0
  230. */
  231. function display_image_matrix($permission_array, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  232. {
  233. if ($course_admin)
  234. {
  235. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  236. }
  237. else
  238. {
  239. if(in_array($permission,$inherited_permissions[$tool]))
  240. {
  241. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  242. }
  243. else
  244. {
  245. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  246. {
  247. if($editable)
  248. {
  249. $url=api_get_self();
  250. foreach($_GET as $key=>$value)
  251. {
  252. $parameter[$key]=$value;
  253. }
  254. $parameter['action']='revoke';
  255. $parameter['permission']=$permission;
  256. $parameter['tool']=$tool;
  257. foreach ($parameter as $key=>$value)
  258. {
  259. $urlparameters.=$key.'='.$value.'&amp;';
  260. }
  261. $url=$url.'?'.$urlparameters;
  262. echo "\t\t\t <a href=\"".$url."\">";
  263. }
  264. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/>";
  265. if($editable)
  266. {
  267. echo "</a>";
  268. }
  269. }
  270. else
  271. {
  272. if($editable)
  273. {
  274. $url=api_get_self();
  275. foreach($_GET as $key=>$value)
  276. {
  277. $parameter[$key]=$value;
  278. }
  279. $parameter['action']='grant';
  280. $parameter['permission']=$permission;
  281. $parameter['tool']=$tool;
  282. foreach ($parameter as $key=>$value)
  283. {
  284. $urlparameters.=$key.'='.$value.'&amp;';
  285. }
  286. $url=$url.'?'.$urlparameters;
  287. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  288. echo "\t\t\t <a href=\"".$url."\">";
  289. }
  290. echo "<img src=\"../img/wrong.gif\" border=\"0\"/>";
  291. if($editable)
  292. {
  293. echo "</a>";
  294. }
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * Slightly modified: Toon Keppens
  301. * This function displays a checked or unchecked image. The image will be checked if the
  302. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  303. * does not have the right
  304. * @param $permission_array the array that contains all the permissions of the user, group, role
  305. * @param $tool the tool we want to check a permission for
  306. * @param $permission the permission we want to check for
  307. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  308. * @version 1.0
  309. */
  310. function display_image_matrix_for_blogs($permission_array, $user_id, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  311. {
  312. if ($course_admin)
  313. {
  314. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  315. }
  316. else
  317. {
  318. if(!empty($inherited_permissions) and in_array($permission,$inherited_permissions[$tool]))
  319. {
  320. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  321. }
  322. else
  323. {
  324. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  325. {
  326. if($editable)
  327. {
  328. $url=api_get_self();
  329. foreach($_GET as $key=>$value)
  330. {
  331. $parameter[$key]=$value;
  332. }
  333. $parameter['action']='manage_rights';
  334. $parameter['do']='revoke';
  335. $parameter['permission']=$permission;
  336. $parameter['tool']=$tool;
  337. $parameter['user_id']=$user_id;
  338. foreach ($parameter as $key=>$value)
  339. {
  340. $urlparameters.=$key.'='.$value.'&amp;';
  341. }
  342. $url=$url.'?'.$urlparameters;
  343. echo "\t\t\t <a href=\"".$url."\">";
  344. }
  345. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermission')."\">";
  346. if($editable)
  347. {
  348. echo "</a>";
  349. }
  350. }
  351. else
  352. {
  353. if($editable)
  354. {
  355. $url=api_get_self();
  356. foreach($_GET as $key=>$value)
  357. {
  358. $parameter[$key]=$value;
  359. }
  360. $parameter['action']='manage_rights';
  361. $parameter['do']='grant';
  362. $parameter['permission']=$permission;
  363. $parameter['tool']=$tool;
  364. $parameter['user_id']=$user_id;
  365. foreach ($parameter as $key=>$value)
  366. {
  367. $urlparameters.=$key.'='.$value.'&amp;';
  368. }
  369. $url=$url.'?'.$urlparameters;
  370. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  371. echo "\t\t\t <a href=\"".$url."\">";
  372. }
  373. echo "<img src=\"../img/wrong.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermissionNot')."\">";
  374. if($editable)
  375. {
  376. echo "</a>";
  377. }
  378. }
  379. }
  380. }
  381. }
  382. /**
  383. * This function displays a list off all the roles of the course (and those defined by the platform admin)
  384. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  385. * @version 1.0
  386. */
  387. function display_role_list($current_course_roles, $current_platform_roles)
  388. {
  389. global $setting_visualisation;
  390. $course_id = api_get_course_int_id();
  391. $coures_roles_table=Database::get_course_table(TABLE_ROLE);
  392. $platform_roles_table=Database::get_main_table(TABLE_ROLE);
  393. /*
  394. // platform roles
  395. $sql="SELECT * FROM $platform_roles_table";
  396. $result=Database::query($sql);
  397. while ($row=Database::fetch_array($result))
  398. {
  399. if(in_array($row['role_id'], $current_platform_roles))
  400. {
  401. $checked='checked';
  402. $image='checkbox_on2.gif';
  403. $action='revoke';
  404. }
  405. else
  406. {
  407. $checked='';
  408. $image='wrong.gif';
  409. $action='grant';
  410. }
  411. if($setting_visualisation=='checkbox')
  412. {
  413. echo "<input type=\"checkbox\" name=\"role*platform*".$row['role_id']."\" $checked>";
  414. }
  415. if($setting_visualisation=='image')
  416. {
  417. echo "<a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=$action&amp;role=".$row['role_id']."&amp;scope=platform\"><img src=\"../img/".$image."\" border=\"0\"/></a>";
  418. }
  419. echo $row['role_name']."<br />\n";
  420. echo $row['role_comment']."<br />\n";
  421. }
  422. */
  423. // course roles
  424. $sql="SELECT * FROM $coures_roles_table WHERE c_id = $course_id ";
  425. $result=Database::query($sql);
  426. while ($row=Database::fetch_array($result))
  427. {
  428. if(in_array($row['role_id'], $current_course_roles))
  429. {
  430. $checked='checked';
  431. $image='checkbox_on2.gif';
  432. $action='revoke';
  433. }
  434. else
  435. {
  436. $checked='';
  437. $image='wrong.gif';
  438. $action='grant';
  439. }
  440. if($setting_visualisation=='checkbox')
  441. {
  442. echo "<input type=\"checkbox\" name=\"role*course*".$row['role_id']."\" $checked>";
  443. }
  444. if($setting_visualisation=='image')
  445. {
  446. echo "<a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=$action&amp;role=".$row['role_id']."&amp;scope=course\"><img src=\"../img/".$image."\" border=\"0\"/></a>";
  447. }
  448. echo $row['role_name']." <a href=\"../permissions/roles.php?role_id=".$row['role_id']."&amp;scope=course\"><img src=\"../img/edit.gif\" /></a><br />\n";
  449. echo $row['role_comment']."<br />\n";
  450. }
  451. }
  452. /**
  453. * This function gets all the current roles of the user or group
  454. * @param $content are we finding the roles for a user or a group (the database depends on it)
  455. * @param $id the id of the user or group
  456. * @return array that contains the name of the roles the user has
  457. * @todo consider having a separate table that contains only an id and a name of the role.
  458. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  459. * @version 1.0
  460. */
  461. function get_roles($content,$id, $scope='course') {
  462. $course_id = api_get_course_int_id();
  463. if($content=='user')
  464. {
  465. $table=Database::get_course_table(TABLE_ROLE_USER);
  466. $id_field = user_id;
  467. }
  468. if($content=='group')
  469. {
  470. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  471. $id_field = 'group_id';
  472. }
  473. $table_role=Database::get_course_table(TABLE_ROLE);
  474. $current_roles=array();
  475. //$sql="SELECT role.role_id FROM $table role_group_user, $table_role role WHERE role_group_user.$id_field = '$id' AND role_group_user.role_id=role.role_id AND role_group_user.scope='".$scope."'";$sql="SELECT role.role_id FROM $table role_group_user, $table_role role WHERE role_group_user.$id_field = '$id' AND role_group_user.role_id=role.role_id AND role_group_user.scope='".$scope."'";
  476. $sql="SELECT role_id FROM $table WHERE c_id = $course_id AND $id_field = '$id' AND scope='".$scope."'";
  477. $result=Database::query($sql);
  478. while ($row=Database::fetch_array($result))
  479. {
  480. $current_roles[]=$row['role_id'];
  481. }
  482. return $current_roles;
  483. }
  484. /**
  485. * This function gets all the current roles of the user or group
  486. * @return array that contains the name of the roles the user has
  487. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  488. * @version 1.0
  489. */
  490. function get_all_roles($content='course') {
  491. $course_id = api_get_course_int_id();
  492. $course_id_condition = " WHERE c_id = $course_id ";
  493. if($content=='course')
  494. {
  495. $table_role=Database::get_course_table(TABLE_ROLE);
  496. }
  497. if($content=='platform')
  498. {
  499. $table_role=Database::get_main_table(TABLE_ROLE);
  500. $course_id_condition = '';
  501. }
  502. $current_roles=array();
  503. $sql="SELECT * FROM $table_role $course_id_condition ";
  504. $result=Database::query($sql);
  505. while ($row=Database::fetch_array($result))
  506. {
  507. $roles[]=$row;
  508. }
  509. return $roles;
  510. }
  511. /**
  512. * This function gets all the roles that are defined
  513. * @param $content are we finding the roles for a user or a group (the database depends on it)
  514. * @param $id the id of the user or group
  515. * @param string Deprecated parameter allowing use of 'platform' scope - the corresponding tables don't exist anymore so the scope is always set to 'course'
  516. * @return array that contains the name of the roles the user has
  517. * @todo consider having a separate table that contains only an id and a name of the role.
  518. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  519. * @version 1.0
  520. */
  521. function get_roles_permissions($content,$id, $scope='course') {
  522. $course_id = api_get_course_int_id();
  523. if($content == 'user')
  524. {
  525. $table=Database::get_course_table(TABLE_ROLE_USER);
  526. $id_field = 'user_id';
  527. }
  528. if($content == 'group')
  529. {
  530. $table = Database::get_course_table(TABLE_ROLE_GROUP);
  531. $id_field = 'group_id';
  532. }
  533. // course roles or platform roles
  534. $scope = 'course';
  535. if($scope == 'course')
  536. {
  537. $table_role = Database::get_course_table(TABLE_ROLE);
  538. $table_role_permissions = Database::get_course_table(TABLE_ROLE_PERMISSION);
  539. $role_condition = " role.c_id = $course_id AND role_permissions.c_id = $course_id AND ";
  540. }
  541. if($scope == 'platform')
  542. {
  543. $table_role = Database::get_main_table(TABLE_ROLE);
  544. $table_role_permissions = Database::get_main_table(TABLE_ROLE_PERMISSION);
  545. $role_condition = '';
  546. }
  547. $current_roles = array();
  548. $sql = "
  549. SELECT *
  550. FROM
  551. " . $table . " role_group_user,
  552. " . $table_role . " role,
  553. " . $table_role_permissions . " role_permissions
  554. WHERE
  555. role_group_user.c_id = $course_id AND
  556. $role_condition
  557. role_group_user.scope = '" . $scope . "' AND
  558. role_group_user." . $id_field . " = '" . $id . "' AND
  559. role_group_user.role_id = role.role_id AND
  560. role.role_id = role_permissions.role_id";
  561. $result = Database::query($sql);
  562. while($row=Database::fetch_array($result))
  563. $current_role_permissions[$row['tool']][]=$row['action'];
  564. return $current_role_permissions;
  565. }
  566. /**
  567. * This function is called when we assign a role to a user or a group
  568. * @param $content are we assigning a role to a group or a user
  569. * @param $action we can grant a role to a group or user or revoke it
  570. * @param $id the user_id of the user or the group_id of the group
  571. * @param $role_id the id of the role we are giving to a user or a group.
  572. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  573. */
  574. function assign_role($content, $action, $id, $role_id, $scope='course') {
  575. $course_id = api_get_course_int_id();
  576. // Which database are we using (depending on the $content parameter)
  577. if($content=='user')
  578. {
  579. $table=Database::get_course_table(TABLE_ROLE_USER);
  580. $id_field = 'user_id';
  581. }
  582. elseif($content=='group')
  583. {
  584. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  585. $id_field = 'group_id';
  586. }
  587. else
  588. {
  589. return get_lang('Error');
  590. }
  591. // grating a right
  592. if($action=='grant')
  593. {
  594. $sql="INSERT INTO $table (c_id, role_id, scope, $id_field) VALUES ($course_id, '".Database::escape_string($role_id)."','".Database::escape_string($scope)."','".Database::escape_string($id)."')";
  595. $result=Database::query($sql);
  596. if($result)
  597. {
  598. $result_message=get_lang('RoleGranted');
  599. }
  600. }
  601. if($action=='revoke')
  602. {
  603. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."' AND role_id='".Database::escape_string($role_id)."'";
  604. $result=Database::query($sql);
  605. if($result)
  606. {
  607. $result_message=get_lang('RoleRevoked');
  608. }
  609. }
  610. return $result_message;
  611. }
  612. /**
  613. * This function merges permission arrays. Each permission array has the following structure
  614. * a permission array has a tool contanst as a key and an array as a value. This value array consists of all the permissions that are granted in that tool.
  615. */
  616. function permission_array_merge($array1, $array2)
  617. {
  618. foreach ($array2 as $tool=>$permissions)
  619. {
  620. foreach ($permissions as $permissionkey=>$permissionvalue)
  621. {
  622. $array1[$tool][]=$permissionvalue;
  623. }
  624. }
  625. return $array1;
  626. }
  627. function my_print_r($array)
  628. {
  629. echo '<pre>';
  630. print_r($array);
  631. echo '</pre>';
  632. }
  633. ?>