1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * File containing the Question class.
- * @package chamilo.exercise
- * @author Olivier Brouckaert
- * @author Julio Montoya <gugli100@gmail.com> lot of bug fixes
- * Modified by hubert.borderiou@grenet.fr - add question categories
- */
- /**
- * Code
- */
- /**
- * QUESTION CLASS
- *
- * This class allows to instantiate an object of type Question
- *
- * @author Olivier Brouckaert, original author
- * @author Patrick Cool, LaTeX support
- * @package chamilo.exercise
- */
- abstract class Question
- {
- public $id;
- public $question;
- public $description;
- public $weighting;
- public $position;
- public $type;
- public $level;
- public $picture;
- public $exerciseList; // array with the list of exercises which this question is in
- public $category_list;
- public $parent_id;
- public $isContent;
- public $course;
- public static $typePicture = 'new_question.png';
- public static $explanationLangVar = '';
- public $question_table_class = 'table table-striped';
- public $editionMode = 'normal';
- public $exercise; // exercise obj
- public $setDefaultValues = false;
- public $submitClass;
- public $submitText;
- public $setDefaultQuestionValues = false;
- public $c_id = null;
- public static $questionTypes = array(
- UNIQUE_ANSWER => array('unique_answer.class.php', 'UniqueAnswer'),
- MULTIPLE_ANSWER => array('multiple_answer.class.php', 'MultipleAnswer'),
- FILL_IN_BLANKS => array('fill_blanks.class.php', 'FillBlanks'),
- MATCHING => array('matching.class.php', 'Matching'),
- FREE_ANSWER => array('freeanswer.class.php', 'FreeAnswer'),
- ORAL_EXPRESSION => array('oral_expression.class.php', 'OralExpression'),
- HOT_SPOT => array('hotspot.class.php', 'HotSpot'),
- HOT_SPOT_DELINEATION => array('hotspot.class.php', 'HotspotDelineation'),
- MULTIPLE_ANSWER_COMBINATION => array('multiple_answer_combination.class.php', 'MultipleAnswerCombination' ),
- UNIQUE_ANSWER_NO_OPTION => array('unique_answer_no_option.class.php', 'UniqueAnswerNoOption'),
- MULTIPLE_ANSWER_TRUE_FALSE => array('multiple_answer_true_false.class.php', 'MultipleAnswerTrueFalse'),
- MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => array('multiple_answer_combination_true_false.class.php', 'MultipleAnswerCombinationTrueFalse'),
- GLOBAL_MULTIPLE_ANSWER => array('global_multiple_answer.class.php', 'GlobalMultipleAnswer'),
- MEDIA_QUESTION => array('media_question.class.php', 'MediaQuestion'),
- UNIQUE_ANSWER_IMAGE => array('unique_answer_image.class.php', 'UniqueAnswerImage'),
- DRAGGABLE => array('draggable.class.php', 'Draggable')
- );
- /**
- * constructor of the class
- *
- * @author - Olivier Brouckaert
- */
- public function Question()
- {
- $this->id = 0;
- $this->question = '';
- $this->description = '';
- $this->weighting = 0;
- $this->position = 1;
- $this->picture = '';
- $this->level = 1;
- // This variable is used when loading an exercise like an scenario
- //with an special hotspot: final_overlap, final_missing, final_excess
- $this->extra = '';
- $this->exerciseList = array();
- $this->course = api_get_course_info();
- $this->category_list = array();
- $this->parent_id = 0;
- $this->editionMode = 'normal';
- }
- public function getIsContent()
- {
- $isContent = null;
- if (isset($_REQUEST['isContent'])) {
- $isContent = intval($_REQUEST['isContent']);
- }
- return $this->isContent = $isContent;
- }
- /**
- * @param string $title
- * @param int $course_id
- * @return mixed|bool
- */
- public function readByTitle($title, $course_id = null)
- {
- if (!empty($course_id)) {
- $course_info = api_get_course_info_by_id($course_id);
- } else {
- $course_info = api_get_course_info();
- }
- $course_id = $course_info['real_id'];
- $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $title = Database::escape_string($title);
- $sql = "SELECT iid FROM $TBL_QUESTIONS WHERE question = '$title' AND c_id = $course_id ";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result);
- return self::read($row['iid'], $course_id);
- }
- return false;
- }
- /**
- * Reads question informations from the data base
- *
- * @author Olivier Brouckaert
- * @param int $id - question ID
- * @param int $course_id
- * @param Exercise
- *
- * @return boolean - true if question exists, otherwise false
- */
- public static function read($id, $course_id = null, Exercise $exercise = null)
- {
- $id = intval($id);
- if (!empty($course_id)) {
- $course_info = api_get_course_info_by_id($course_id);
- } else {
- $course_info = api_get_course_info();
- }
- $course_id = $course_info['real_id'];
- if (empty($course_id) || $course_id == -1) {
- //return false;
- }
- $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $sql = "SELECT * FROM $TBL_QUESTIONS WHERE iid = $id";
- $result = Database::query($sql);
- // if the question has been found
- if ($object = Database::fetch_object($result)) {
- $objQuestion = Question::getInstance($object->type);
- if (!empty($objQuestion)) {
- $objQuestion->id = $id;
- $objQuestion->question = $object->question;
- $objQuestion->description = $object->description;
- $objQuestion->weighting = $object->ponderation;
- $objQuestion->position = $object->position;
- $objQuestion->type = $object->type;
- $objQuestion->picture = $object->picture;
- $objQuestion->level = (int)$object->level;
- $objQuestion->extra = $object->extra;
- $objQuestion->course = $course_info;
- $objQuestion->parent_id = $object->parent_id;
- $objQuestion->category_list = Testcategory::getCategoryForQuestion($id);
- $objQuestion->exercise = $exercise;
- $objQuestion->c_id = $object->c_id;
- $sql = "SELECT exercice_id FROM $TBL_EXERCICE_QUESTION WHERE question_id = $id";
- $result_exercise_list = Database::query($sql);
- // fills the array with the exercises which this question is in
- if ($result_exercise_list) {
- while ($obj = Database::fetch_object($result_exercise_list)) {
- $objQuestion->exerciseList[] = $obj->exercice_id;
- }
- }
- return $objQuestion;
- }
- }
- // question not found
- return false;
- }
- public function setEditionMode($mode)
- {
- $this->editionMode = $mode;
- }
- public function getEditionMode()
- {
- return $this->editionMode;
- }
- /**
- * Returns the question ID
- *
- * @author - Olivier Brouckaert
- * @return int - question ID
- */
- public function selectId()
- {
- return $this->id;
- }
- /**
- * Returns the question title
- *
- * @author - Olivier Brouckaert
- * @return string - question title
- */
- public function selectTitle()
- {
- return $this->question;
- }
- /**
- * returns the question description
- *
- * @author - Olivier Brouckaert
- * @return string - question description
- */
- public function selectDescription()
- {
- $this->description = $this->description;
- return $this->description;
- }
- /**
- * returns the question weighting
- *
- * @author - Olivier Brouckaert
- * @return int - question weighting
- */
- public function selectWeighting()
- {
- return $this->weighting;
- }
- /**
- * returns the question position
- *
- * @author - Olivier Brouckaert
- * @return int - question position
- */
- public function selectPosition()
- {
- return $this->position;
- }
- /**
- * returns the answer type
- *
- * @author - Olivier Brouckaert
- * @return int - answer type
- */
- public function selectType()
- {
- return $this->type;
- }
- /**
- * returns the level of the question
- *
- * @author - Nicolas Raynaud
- * @return int - level of the question, 0 by default.
- */
- public function selectLevel()
- {
- return $this->level;
- }
- /**
- * returns the picture name
- *
- * @author - Olivier Brouckaert
- * @return string - picture name
- */
- public function selectPicture()
- {
- return $this->picture;
- }
- public function selectPicturePath()
- {
- if (!empty($this->picture)) {
- return api_get_path(WEB_COURSE_PATH).$this->course['path'].'/document/images/'.$this->picture;
- }
- return false;
- }
- /**
- * returns the array with the exercise ID list
- *
- * @author - Olivier Brouckaert
- * @return array - list of exercise ID which the question is in
- */
- public function selectExerciseList()
- {
- return $this->exerciseList;
- }
- /**
- * returns the number of exercises which this question is in
- *
- * @author - Olivier Brouckaert
- * @return integer - number of exercises
- */
- public function selectNbrExercises()
- {
- return sizeof($this->exerciseList);
- }
- /**
- * @param $course_id
- */
- public function updateCourse($course_id)
- {
- $this->course = api_get_course_info_by_id($course_id);
- }
- /**
- * changes the question title
- *
- * @author - Olivier Brouckaert
- * @param - string $title - question title
- */
- public function updateTitle($title)
- {
- $this->question = $title;
- }
- public function updateParentId($id)
- {
- $this->parent_id = intval($id);
- }
- /**
- * changes the question description
- *
- * @author - Olivier Brouckaert
- * @param - string $description - question description
- */
- public function updateDescription($description)
- {
- $this->description = $description;
- }
- /**
- * changes the question weighting
- *
- * @author - Olivier Brouckaert
- * @param - integer $weighting - question weighting
- */
- public function updateWeighting($weighting)
- {
- $this->weighting = $weighting;
- }
- /**
- * @author - Hubert Borderiou 12-10-2011
- * @param - array of category $in_category
- */
- public function updateCategory($category_list)
- {
- $this->category_list = $category_list;
- }
- /**
- * @author - Hubert Borderiou 12-10-2011
- * @param - interger $in_positive
- */
- public function updateScoreAlwaysPositive($in_positive)
- {
- $this->scoreAlwaysPositive = $in_positive;
- }
- /**
- * @author - Hubert Borderiou 12-10-2011
- * @param - interger $in_positive
- */
- public function updateUncheckedMayScore($in_positive)
- {
- $this->uncheckedMayScore = $in_positive;
- }
- /**
- * Save category of a question
- *
- * A question can have n categories
- * if category is empty, then question has no category then delete the category entry
- *
- * @param - int $in_positive
- * @author - Julio Montoya - Adding multiple cat support
- */
- public function saveCategories($category_list)
- {
- $course_id = $this->course['real_id'];
- if (!empty($category_list)) {
- $this->deleteCategory();
- $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
- $category_list = array_filter($category_list);
- // update or add category for a question
- foreach ($category_list as $category_id) {
- if (empty($category_id)) {
- continue;
- }
- $category_id = intval($category_id);
- $question_id = Database::escape_string($this->id);
- $sql = "SELECT count(*) AS nb FROM $TBL_QUESTION_REL_CATEGORY
- WHERE category_id = $category_id AND question_id = $question_id AND c_id=".$course_id;
- $res = Database::query($sql);
- $row = Database::fetch_array($res);
- if ($row['nb'] > 0) {
- //DO nothing
- //$sql = "UPDATE $TBL_QUESTION_REL_CATEGORY SET category_id = $category_id WHERE question_id=$question_id AND c_id=".api_get_course_int_id();
- //$res = Database::query($sql);
- } else {
- $sql = "INSERT INTO $TBL_QUESTION_REL_CATEGORY (c_id, question_id, category_id) VALUES (".$course_id.", $question_id, $category_id)";
- Database::query($sql);
- }
- }
- }
- }
- /**
- * @author - Hubert Borderiou 12-10-2011
- * @param - interger $in_positive
- * in this version, a question can only have 1 category
- * if category is 0, then question has no category then delete the category entry
- */
- public function saveCategory($in_category)
- {
- if ($in_category <= 0) {
- $this->deleteCategory();
- } else {
- // update or add category for a question
- $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
- $category_id = Database::escape_string($in_category);
- $question_id = Database::escape_string($this->id);
- $sql = "SELECT count(*) AS nb FROM $TBL_QUESTION_REL_CATEGORY WHERE question_id= $question_id AND c_id=".api_get_course_int_id(
- );
- $res = Database::query($sql);
- $row = Database::fetch_array($res);
- if ($row['nb'] > 0) {
- $sql = "UPDATE $TBL_QUESTION_REL_CATEGORY SET category_id= $category_id WHERE question_id=$question_id AND c_id=".api_get_course_int_id(
- );
- $res = Database::query($sql);
- } else {
- $sql = "INSERT INTO $TBL_QUESTION_REL_CATEGORY VALUES (".api_get_course_int_id(
- ).", $question_id, $category_id)";
- $res = Database::query($sql);
- }
- }
- }
- /**
- * @author hubert borderiou 12-10-2011
- * delete any category entry for question id
- * @param : none
- * delte the category for question
- */
- public function deleteCategory()
- {
- $course_id = $this->course['real_id'];
- $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
- $question_id = Database::escape_string($this->id);
- $sql = "DELETE FROM $TBL_QUESTION_REL_CATEGORY WHERE question_id = $question_id AND c_id = ".$course_id;
- Database::query($sql);
- }
- /**
- * changes the question position
- *
- * @author - Olivier Brouckaert
- * @param - integer $position - question position
- */
- public function updatePosition($position)
- {
- $this->position = $position;
- }
- /**
- * changes the question level
- *
- * @author - Nicolas Raynaud
- * @param - integer $level - question level
- */
- public function updateLevel($level)
- {
- $this->level = $level;
- }
- /**
- * changes the answer type. If the user changes the type from "unique answer" to "multiple answers"
- * (or conversely) answers are not deleted, otherwise yes
- *
- * @author - Olivier Brouckaert
- * @param - integer $type - answer type
- */
- public function updateType($type)
- {
- $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
- $course_id = $this->course['real_id'];
- if (empty($course_id)) {
- $course_id = api_get_course_int_id();
- }
- // if we really change the type
- if ($type != $this->type) {
- // if we don't change from "unique answer" to "multiple answers" (or conversely)
- if (!in_array($this->type, array(UNIQUE_ANSWER, MULTIPLE_ANSWER)) || !in_array(
- $type,
- array(UNIQUE_ANSWER, MULTIPLE_ANSWER)
- )
- ) {
- // removes old answers
- $sql = "DELETE FROM $TBL_REPONSES WHERE question_id='".Database::escape_string($this->id)."'";
- Database::query($sql);
- }
- $this->type = $type;
- }
- }
- /**
- * Adds a picture to the question
- *
- * @author - Olivier Brouckaert
- * @param - string $Picture - temporary path of the picture to upload
- * @param - string $PictureName - Name of the picture
- * @return - boolean - true if uploaded, otherwise false
- */
- public function uploadPicture($Picture, $PictureName, $picturePath = null)
- {
- if (empty($picturePath)) {
- global $picturePath;
- }
- if (!file_exists($picturePath)) {
- if (mkdir($picturePath, api_get_permissions_for_new_directories())) {
- // document path
- $documentPath = api_get_path(SYS_COURSE_PATH).$this->course['path']."/document";
- $path = str_replace($documentPath, '', $picturePath);
- $title_path = basename($picturePath);
- $doc_id = FileManager::add_document($this->course, $path, 'folder', 0, $title_path);
- api_item_property_update($this->course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', api_get_user_id());
- }
- }
- // if the question has got an ID
- if ($this->id) {
- $extension = pathinfo($PictureName, PATHINFO_EXTENSION);
- $this->picture = 'quiz-'.$this->id.'.jpg';
- $o_img = new Image($Picture);
- $o_img->send_image($picturePath.'/'.$this->picture, -1, 'jpg');
- $document_id = FileManager::add_document(
- $this->course,
- '/images/'.$this->picture,
- 'file',
- filesize($picturePath.'/'.$this->picture),
- $this->picture
- );
- if ($document_id) {
- return api_item_property_update(
- $this->course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- api_get_user_id()
- );
- }
- }
- return false;
- }
- /**
- * Resizes a picture || Warning!: can only be called after uploadPicture, or if picture is already available in object.
- *
- * @author - Toon Keppens
- * @param - string $Dimension - Resizing happens proportional according to given dimension: height|width|any
- * @param - integer $Max - Maximum size
- * @return - boolean - true if success, false if failed
- */
- function resizePicture($Dimension, $Max)
- {
- global $picturePath;
- // if the question has an ID
- if ($this->id) {
- // Get dimensions from current image.
- $my_image = new Image($picturePath.'/'.$this->picture);
- $current_image_size = $my_image->get_image_size();
- $current_width = $current_image_size['width'];
- $current_height = $current_image_size['height'];
- if ($current_width < $Max && $current_height < $Max) {
- return true;
- } elseif ($current_height == "") {
- return false;
- }
- // Resize according to height.
- if ($Dimension == "height") {
- $resize_scale = $current_height / $Max;
- $new_height = $Max;
- $new_width = ceil($current_width / $resize_scale);
- }
- // Resize according to width
- if ($Dimension == "width") {
- $resize_scale = $current_width / $Max;
- $new_width = $Max;
- $new_height = ceil($current_height / $resize_scale);
- }
- // Resize according to height or width, both should not be larger than $Max after resizing.
- if ($Dimension == "any") {
- if ($current_height > $current_width || $current_height == $current_width) {
- $resize_scale = $current_height / $Max;
- $new_height = $Max;
- $new_width = ceil($current_width / $resize_scale);
- }
- if ($current_height < $current_width) {
- $resize_scale = $current_width / $Max;
- $new_width = $Max;
- $new_height = ceil($current_height / $resize_scale);
- }
- }
- $my_image->resize($new_width, $new_height);
- $result = $my_image->send_image($picturePath.'/'.$this->picture);
- if ($result) {
- return true;
- } else {
- return false;
- }
- }
- }
- /**
- * deletes the picture
- *
- * @author - Olivier Brouckaert
- * @return - boolean - true if removed, otherwise false
- */
- function removePicture()
- {
- global $picturePath;
- // if the question has got an ID and if the picture exists
- if ($this->id) {
- $picture = $this->picture;
- $this->picture = '';
- return @unlink($picturePath.'/'.$picture) ? true : false;
- }
- return false;
- }
- /**
- * Exports a picture to another question
- *
- * @author - Olivier Brouckaert
- * @param - integer $questionId - ID of the target question
- * @return - boolean - true if copied, otherwise false
- */
- function exportPicture($questionId, $course_info)
- {
- $course_id = $course_info['real_id'];
- $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $destination_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/images';
- $source_path = api_get_path(SYS_COURSE_PATH).$this->course['path'].'/document/images';
- // if the question has got an ID and if the picture exists
- if ($this->id && !empty($this->picture)) {
- $picture = explode('.', $this->picture);
- $extension = $picture[sizeof($picture) - 1];
- $picture = 'quiz-'.$questionId.'.'.$extension;
- $result = @copy($source_path.'/'.$this->picture, $destination_path.'/'.$picture) ? true : false;
- //If copy was correct then add to the database
- if ($result) {
- $sql = "UPDATE $TBL_QUESTIONS SET picture='".Database::escape_string(
- $picture
- )."' WHERE c_id = $course_id AND iid='".intval($questionId)."'";
- Database::query($sql);
- $document_id = FileManager::add_document(
- $course_info,
- '/images/'.$picture,
- 'file',
- filesize($destination_path.'/'.$picture),
- $picture
- );
- if ($document_id) {
- return api_item_property_update(
- $course_info,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- api_get_user_id()
- );
- }
- }
- return $result;
- }
- return false;
- }
- /**
- * Saves the picture coming from POST into a temporary file
- * Temporary pictures are used when we don't want to save a picture right after a form submission.
- * For example, if we first show a confirmation box.
- *
- * @author - Olivier Brouckaert
- * @param - string $Picture - temporary path of the picture to move
- * @param - string $PictureName - Name of the picture
- */
- function setTmpPicture($Picture, $PictureName)
- {
- global $picturePath;
- $PictureName = explode('.', $PictureName);
- $Extension = $PictureName[sizeof($PictureName) - 1];
- // saves the picture into a temporary file
- @move_uploaded_file($Picture, $picturePath.'/tmp.'.$Extension);
- }
- /**
- Sets the title
- */
- public function setTitle($title)
- {
- $this->question = $title;
- }
- /**
- Sets the title
- */
- public function setExtra($extra)
- {
- $this->extra = $extra;
- }
- /**
- * Moves the temporary question "tmp" to "quiz-$questionId"
- * Temporary pictures are used when we don't want to save a picture right after a form submission.
- * For example, if we first show a confirmation box.
- *
- * @author - Olivier Brouckaert
- * @return - boolean - true if moved, otherwise false
- */
- function getTmpPicture()
- {
- global $picturePath;
- // if the question has got an ID and if the picture exists
- if ($this->id) {
- if (file_exists($picturePath.'/tmp.jpg')) {
- $Extension = 'jpg';
- } elseif (file_exists($picturePath.'/tmp.gif')) {
- $Extension = 'gif';
- } elseif (file_exists($picturePath.'/tmp.png')) {
- $Extension = 'png';
- }
- $this->picture = 'quiz-'.$this->id.'.'.$Extension;
- return @rename($picturePath.'/tmp.'.$Extension, $picturePath.'/'.$this->picture) ? true : false;
- }
- return false;
- }
- /**
- * updates the question in the data base
- * if an exercise ID is provided, we add that exercise ID into the exercise list
- *
- * @author - Olivier Brouckaert
- * @param - integer $exerciseId - exercise ID if saving in an exercise
- */
- function save($exerciseId = 0)
- {
- $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $id = $this->id;
- $question = $this->question;
- $description = $this->description;
- $weighting = $this->weighting;
- $position = $this->position;
- $type = $this->type;
- $picture = $this->picture;
- $level = $this->level;
- $extra = $this->extra;
- $c_id = $this->course['real_id'];
- $category_list = $this->category_list;
- // Question already exists
- if (!empty($id)) {
- $sql = "UPDATE $TBL_QUESTIONS SET
- question ='".Database::escape_string($question)."',
- description ='".Database::escape_string($description)."',
- ponderation ='".Database::escape_string($weighting)."',
- position ='".Database::escape_string($position)."',
- type ='".Database::escape_string($type)."',
- picture ='".Database::escape_string($picture)."',
- extra ='".Database::escape_string($extra)."',
- level ='".Database::escape_string($level)."',
- parent_id = ".$this->parent_id."
- WHERE iid = '".Database::escape_string($id)."'";
- //WHERE c_id = $c_id AND iid = '".Database::escape_string($id)."'";
- Database::query($sql);
- $this->saveCategories($category_list);
- if (!empty($exerciseId)) {
- api_item_property_update($this->course, TOOL_QUIZ, $id, 'QuizQuestionUpdated', api_get_user_id());
- if (api_get_setting('search_enabled') == 'true') {
- $this->search_engine_edit($exerciseId);
- }
- }
- } else {
- // Creates a new question
- $sql = "SELECT max(position) FROM $TBL_QUESTIONS as question, $TBL_EXERCICE_QUESTION as test_question
- WHERE question.iid = test_question.question_id AND
- test_question.exercice_id = '".Database::escape_string($exerciseId)."' AND
- question.c_id = $c_id AND
- test_question.c_id = $c_id ";
- $result = Database::query($sql);
- $current_position = Database::result($result, 0, 0);
- $this->updatePosition($current_position + 1);
- $position = $this->position;
- $sql = "INSERT INTO $TBL_QUESTIONS (c_id, question, description, ponderation, position, type, picture, extra, level, parent_id) VALUES ( ".
- " $c_id, ".
- " '".Database::escape_string($question)."', ".
- " '".Database::escape_string($description)."', ".
- " '".Database::escape_string($weighting)."', ".
- " '".Database::escape_string($position)."', ".
- " '".Database::escape_string($type)."', ".
- " '".Database::escape_string($picture)."', ".
- " '".Database::escape_string($extra)."', ".
- " '".Database::escape_string($level)."', ".
- " '".$this->parent_id."' ".
- " )";
- Database::query($sql);
- $this->id = Database::insert_id();
- api_item_property_update($this->course, TOOL_QUIZ, $this->id, 'QuizQuestionAdded', api_get_user_id());
- // If hotspot, create first answer
- if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
- $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
- $sql = "INSERT INTO $TBL_ANSWERS (question_id , answer , correct , comment , ponderation , position , hotspot_coordinates , hotspot_type )
- VALUES ('".Database::escape_string($this->id)."', '', NULL , '', '10' , '1', '0;0|0|0', 'square')";
- Database::query($sql);
- }
- if ($type == HOT_SPOT_DELINEATION) {
- $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
- $sql = "INSERT INTO $TBL_ANSWERS (question_id , answer , correct , comment , ponderation , position , hotspot_coordinates , hotspot_type )
- VALUES ('".Database::escape_string($this->id)."', '', NULL , '', '10' , '1', '0;0|0|0', 'delineation')";
- Database::query($sql);
- }
- if (api_get_setting('search_enabled') == 'true') {
- if ($exerciseId != 0) {
- $this->search_engine_edit($exerciseId, true);
- }
- }
- }
- // if the question is created in an exercise
- if ($exerciseId) {
- // adds the exercise into the exercise list of this question
- $this->addToList($exerciseId, true);
- }
- }
- function search_engine_edit($exerciseId, $addQs = false, $rmQs = false)
- {
- // update search engine and its values table if enabled
- if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
- $course_id = api_get_course_id();
- // get search_did
- $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
- if ($addQs || $rmQs) {
- //there's only one row per question on normal db and one document per question on search engine db
- $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=%s LIMIT 1';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
- } else {
- $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%s LIMIT 1';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
- }
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0 || $addQs) {
- require_once(api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php');
- require_once(api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php');
- $di = new ChamiloIndexer();
- if ($addQs) {
- $question_exercises = array((int)$exerciseId);
- } else {
- $question_exercises = array();
- }
- isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
- $di->connectDb(null, null, $lang);
- // retrieve others exercise ids
- $se_ref = Database::fetch_array($res);
- $se_doc = $di->get_document((int)$se_ref['search_did']);
- if ($se_doc !== false) {
- if (($se_doc_data = $di->get_document_data($se_doc)) !== false) {
- $se_doc_data = unserialize($se_doc_data);
- if (isset($se_doc_data[SE_DATA]['type']) && $se_doc_data[SE_DATA]['type'] == SE_DOCTYPE_EXERCISE_QUESTION) {
- if (isset($se_doc_data[SE_DATA]['exercise_ids']) && is_array(
- $se_doc_data[SE_DATA]['exercise_ids']
- )
- ) {
- foreach ($se_doc_data[SE_DATA]['exercise_ids'] as $old_value) {
- if (!in_array($old_value, $question_exercises)) {
- $question_exercises[] = $old_value;
- }
- }
- }
- }
- }
- }
- if ($rmQs) {
- while (($key = array_search($exerciseId, $question_exercises)) !== false) {
- unset($question_exercises[$key]);
- }
- }
- // build the chunk to index
- $ic_slide = new IndexableChunk();
- $ic_slide->addValue("title", $this->question);
- $ic_slide->addCourseId($course_id);
- $ic_slide->addToolId(TOOL_QUIZ);
- $xapian_data = array(
- SE_COURSE_ID => $course_id,
- SE_TOOL_ID => TOOL_QUIZ,
- SE_DATA => array(
- 'type' => SE_DOCTYPE_EXERCISE_QUESTION,
- 'exercise_ids' => $question_exercises,
- 'question_id' => (int)$this->id
- ),
- SE_USER => (int)api_get_user_id(),
- );
- $ic_slide->xapian_data = serialize($xapian_data);
- $ic_slide->addValue("content", $this->description);
- //TODO: index answers, see also form validation on question_admin.inc.php
- $di->remove_document((int)$se_ref['search_did']);
- $di->addChunk($ic_slide);
- //index and return search engine document id
- if (!empty($question_exercises)) { // if empty there is nothing to index
- $did = $di->index();
- unset($di);
- }
- if ($did || $rmQs) {
- // save it to db
- if ($addQs || $rmQs) {
- $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=\'%s\'';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
- } else {
- $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\' AND ref_id_second_level=\'%s\'';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
- }
- Database::query($sql);
- if ($rmQs) {
- if (!empty($question_exercises)) {
- $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
- VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
- $sql = sprintf(
- $sql,
- $tbl_se_ref,
- $course_id,
- TOOL_QUIZ,
- array_shift($question_exercises),
- $this->id,
- $did
- );
- Database::query($sql);
- }
- } else {
- $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
- VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id, $did);
- Database::query($sql);
- }
- }
- }
- }
- }
- /**
- * adds an exercise into the exercise list
- *
- * @author - Olivier Brouckaert
- * @param - integer $exerciseId - exercise ID
- * @param - boolean $fromSave - comming from $this->save() or not
- */
- function addToList($exerciseId, $fromSave = false)
- {
- $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $id = $this->id;
- // checks if the exercise ID is not in the list
- if (!in_array($exerciseId, $this->exerciseList)) {
- $this->exerciseList[] = $exerciseId;
- $new_exercise = new Exercise();
- $new_exercise->read($exerciseId);
- $count = $new_exercise->selectNbrQuestions();
- $count++;
- $sql = "INSERT INTO $TBL_EXERCICE_QUESTION (c_id, question_id, exercice_id, question_order) VALUES
- ({$this->course['real_id']}, '".Database::escape_string($id)."','".Database::escape_string(
- $exerciseId
- )."', '$count' )";
- Database::query($sql);
- // we do not want to reindex if we had just saved adnd indexed the question
- if (!$fromSave) {
- $this->search_engine_edit($exerciseId, true);
- }
- }
- }
- /**
- * removes an exercise from the exercise list
- *
- * @author - Olivier Brouckaert
- * @param - integer $exerciseId - exercise ID
- * @return - boolean - true if removed, otherwise false
- */
- function removeFromList($exerciseId)
- {
- $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $id = $this->id;
- // searches the position of the exercise ID in the list
- $pos = array_search($exerciseId, $this->exerciseList);
- $course_id = api_get_course_int_id();
- // exercise not found
- if ($pos === false) {
- return false;
- } else {
- // deletes the position in the array containing the wanted exercise ID
- unset($this->exerciseList[$pos]);
- //update order of other elements
- $sql = "SELECT question_order FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string(
- $id
- )."' AND exercice_id='".Database::escape_string($exerciseId)."'";
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0) {
- $row = Database::fetch_array($res);
- if (!empty($row['question_order'])) {
- $sql = "UPDATE $TBL_EXERCICE_QUESTION SET question_order = question_order-1
- WHERE c_id = $course_id AND exercice_id='".Database::escape_string(
- $exerciseId
- )."' AND question_order > ".$row['question_order'];
- $res = Database::query($sql);
- }
- }
- $sql = "DELETE FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string(
- $id
- )."' AND exercice_id='".Database::escape_string($exerciseId)."'";
- Database::query($sql);
- return true;
- }
- }
- /**
- * Deletes a question from the database
- * the parameter tells if the question is removed from all exercises (value = 0),
- * or just from one exercise (value = exercise ID)
- *
- * @author - Olivier Brouckaert
- * @param - integer $deleteFromEx - exercise ID if the question is only removed from one exercise
- */
- function delete($deleteFromEx = 0)
- {
- $course_id = api_get_course_int_id();
- $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
- $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
- $id = $this->id;
- if ($this->type == MEDIA_QUESTION) {
- // Removing media for attached questions
- $sql = "UPDATE $TBL_QUESTIONS SET parent_id = '' WHERE parent_id = $id";
- Database::query($sql);
- $sql = "DELETE FROM $TBL_QUESTIONS WHERE c_id = $course_id AND iid='".Database::escape_string($id)."'";
- Database::query($sql);
- return true;
- }
- // if the question must be removed from all exercises
- if (!$deleteFromEx) {
- //update the question_order of each question to avoid inconsistencies
- $sql = "SELECT exercice_id, question_order FROM $TBL_EXERCICE_QUESTION
- WHERE c_id = $course_id AND question_id='".Database::escape_string($id)."'";
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0) {
- while ($row = Database::fetch_array($res)) {
- if (!empty($row['question_order'])) {
- $sql = "UPDATE $TBL_EXERCICE_QUESTION
- SET question_order = question_order - 1
- WHERE c_id = $course_id AND
- exercice_id='".Database::escape_string($row['exercice_id'])."' AND
- question_order > ".$row['question_order'];
- Database::query($sql);
- }
- }
- }
- $sql = "DELETE FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string($id)."'";
- Database::query($sql);
- $sql = "DELETE FROM $TBL_QUESTIONS WHERE c_id = $course_id AND iid='".Database::escape_string($id)."'";
- Database::query($sql);
- $sql = "DELETE FROM $TBL_REPONSES WHERE question_id='".Database::escape_string($id)."'";
- Database::query($sql);
- // remove the category of this question in the question_rel_category table
- $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY
- WHERE c_id = $course_id AND question_id='".Database::escape_string($id)."' AND c_id=".api_get_course_int_id();
- Database::query($sql);
- api_item_property_update($this->course, TOOL_QUIZ, $id, 'QuizQuestionDeleted', api_get_user_id());
- $this->removePicture();
- // resets the object
- $this->Question();
- } else {
- // just removes the exercise from the list
- $this->removeFromList($deleteFromEx);
- if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
- // disassociate question with this exercise
- $this->search_engine_edit($deleteFromEx, false, true);
- }
- api_item_property_update($this->course, TOOL_QUIZ, $id, 'QuizQuestionDeleted', api_get_user_id());
- }
- }
- /**
- * Duplicates the question
- *
- * @author Olivier Brouckaert
- * @param array Course info of the destination course
- * @return int ID of the new question
- */
- function duplicate($course_info = null)
- {
- if (empty($course_info)) {
- $course_info = $this->course;
- } else {
- $course_info = $course_info;
- }
- $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
- $question = $this->question;
- $description = $this->description;
- $weighting = $this->weighting;
- $position = $this->position;
- $type = $this->type;
- $level = intval($this->level);
- $extra = $this->extra;
- //Using the same method used in the course copy to transform URLs
- if ($this->course['id'] != $course_info['id']) {
- $description = DocumentManager::replace_urls_inside_content_html_from_copy_course(
- $description,
- $this->course['id'],
- $course_info['id']
- );
- $question = DocumentManager::replace_urls_inside_content_html_from_copy_course(
- $question,
- $this->course['id'],
- $course_info['id']
- );
- }
- $course_id = $course_info['real_id'];
- //Read the source options
- $options = self::readQuestionOption($this->id, $this->course['real_id']);
- //Inserting in the new course db / or the same course db
- $params = array(
- 'c_id' => $course_id,
- 'question' => $question,
- 'description' => $description,
- 'ponderation' => $this->weighting,
- 'position' => $this->position,
- 'type' => $this->type,
- 'level' => $this->level,
- 'extra' => $this->extra,
- 'parent_id' => $this->parent_id,
- );
- $new_question_id = Database::insert($TBL_QUESTIONS, $params);
- if (!empty($options)) {
- //Saving the quiz_options
- foreach ($options as $item) {
- $item['question_id'] = $new_question_id;
- $item['c_id'] = $course_id;
- unset($item['iid']);
- Database::insert($TBL_QUESTION_OPTIONS, $item);
- }
- }
- $this->duplicate_category_question($new_question_id, $course_id);
- // Duplicates the picture of the hotspot
- $this->exportPicture($new_question_id, $course_info);
- return $new_question_id;
- }
- function get_categories_from_question()
- {
- return Testcategory::getCategoryForQuestion($this->id);
- }
- function duplicate_category_question($question_id, $course_id)
- {
- $question = Question::read($question_id, $course_id);
- $categories = $this->get_categories_from_question();
- if (!empty($categories)) {
- $question->saveCategories($categories);
- }
- }
- function get_question_type_name()
- {
- $key = self::$questionTypes[$this->type];
- return get_lang($key[1]);
- }
- static function get_question_type($type)
- {
- if ($type == ORAL_EXPRESSION && api_get_setting('enable_nanogong') != 'true') {
- return null;
- }
- return self::$questionTypes[$type];
- }
- static function get_question_type_list()
- {
- if (api_get_setting('enable_nanogong') != 'true') {
- self::$questionTypes[ORAL_EXPRESSION] = null;
- unset(self::$questionTypes[ORAL_EXPRESSION]);
- }
- return self::$questionTypes;
- }
- /**
- * Returns an instance of the class corresponding to the type
- * @param integer $type the type of the question
- * @return an instance of a Question subclass (or of Questionc class by default)
- */
- static function getInstance($type, Exercise $exercise = null)
- {
- if (!is_null($type)) {
- list($file_name, $class_name) = self::get_question_type($type);
- if (!empty($file_name)) {
- include_once $file_name;
- if (class_exists($class_name)) {
- $obj = new $class_name();
- $obj->exercise = $exercise;
- return $obj;
- } else {
- echo 'Can\'t instanciate class '.$class_name.' of type '.$type;
- }
- }
- }
- return null;
- }
- /**
- * Creates the form to create / edit a question
- * A subclass can redifine this function to add fields...
- *
- * @param FormValidator $form the formvalidator instance (by reference)
- */
- public function createForm(&$form, $fck_config = 0)
- {
- $url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?1=1';
- $js = null;
- if ($this->type != MEDIA_QUESTION) {
- $js = '<script>
- function check() {
- $("#category_id option:selected").each(function() {
- var id = $(this).val();
- var name = $(this).text();
- if (id != "" ) {
- $.ajax({
- async: false,
- url: "'.$url.'&a=exercise_category_exists",
- data: "id="+id,
- success: function(return_value) {
- if (return_value == 0 ) {
- alert("'.get_lang('CategoryDoesNotExists').'");
- //Deleting select option tag
- $("#parent_id").find("option").remove();
- $(".holder li").each(function () {
- if ($(this).attr("rel") == id) {
- $(this).remove();
- }
- });
- }
- },
- });
- }
- });
- }
- $(function() {
- $("#category_id").fcbkcomplete({
- json_url: "'.$url.'&a=search_category_parent&type=all&",
- maxitems: 20,
- addontab: false,
- input_min_size: 1,
- cache: false,
- complete_text:"'.get_lang('StartToType').'",
- firstselected: false,
- onselect: check,
- filter_selected: true,
- newel: true
- });
- });
- </script>';
- }
- $js .= '<script>
- // hack to hide http://cksource.com/forums/viewtopic.php?f=6&t=8700
- function FCKeditor_OnComplete( editorInstance ) {
- if (document.getElementById ( \'HiddenFCK\' + editorInstance.Name )) {
- HideFCKEditorByInstanceName (editorInstance.Name);
- }
- }
- function HideFCKEditorByInstanceName ( editorInstanceName ) {
- if (document.getElementById ( \'HiddenFCK\' + editorInstanceName ).className == "HideFCKEditor" ) {
- document.getElementById ( \'HiddenFCK\' + editorInstanceName ).className = "media";
- }
- }
- function show_media(){
- var my_display = document.getElementById(\'HiddenFCKquestionDescription\').style.display;
- if (my_display== \'none\' || my_display == \'\') {
- document.getElementById(\'HiddenFCKquestionDescription\').style.display = \'block\';
- document.getElementById(\'media_icon\').innerHTML=\' '.Display::return_icon('looknfeelna.png').' '.get_lang('EnrichQuestion').'\';
- } else {
- document.getElementById(\'HiddenFCKquestionDescription\').style.display = \'none\';
- document.getElementById(\'media_icon\').innerHTML=\' '.Display::return_icon('looknfeel.png').' '.get_lang('EnrichQuestion').'\';
- }
- }
- // hub 13-12-2010
- function visiblerDevisibler(in_id) {
- if (document.getElementById(in_id)) {
- if (document.getElementById(in_id).style.display == "none") {
- document.getElementById(in_id).style.display = "block";
- if (document.getElementById(in_id+"Img")) {
- document.getElementById(in_id+"Img").html = "'.addslashes(Display::return_icon('div_hide.gif')).'";
- }
- } else {
- document.getElementById(in_id).style.display = "none";
- if (document.getElementById(in_id+"Img")) {
- document.getElementById(in_id+"Img").html = "dsdsds'.addslashes(Display::return_icon('div_show.gif')).'";
- }
- }
- }
- }
- </script>';
- $form->addElement('html', $js);
- // question name
- $form->addElement('text', 'questionName', get_lang('Question'), array('class' => 'span6'));
- $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
- // default content
- $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
- // Question type
- $answerType = isset($_REQUEST['answerType']) ? intval($_REQUEST['answerType']) : $this->selectType();
- $form->addElement('hidden', 'answerType', $answerType);
- // html editor
- $editor_config = array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '150');
- if (is_array($fck_config)) {
- $editor_config = array_merge($editor_config, $fck_config);
- }
- if (!api_is_allowed_to_edit(null, true)) {
- $editor_config['UserStatus'] = 'student';
- }
- if ($this->exercise->fastExerciseEdition == false) {
- $form->addElement(
- 'advanced_settings',
- '<a href="javascript://" onclick=" return show_media()"><span id="media_icon">
- '.Display::return_icon('looknfeel.png').' '.get_lang('EnrichQuestion').'</span></a>
- '
- );
- $form->addElement('html', '<div class="HideFCKEditor" id="HiddenFCKquestionDescription" >');
- }
- $form->add_html_editor('questionDescription', get_lang('QuestionDescription'), false, false, $editor_config);
- if ($this->exercise->fastExerciseEdition == false) {
- $form->addElement('html', '</div>');
- }
- // hidden values
- $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null;
- $form->addElement('hidden', 'myid', $my_id);
- if ($this->type != MEDIA_QUESTION) {
- if ($this->exercise->fastExerciseEdition == false) {
- // Advanced parameters
- $form->addElement('advanced_settings', '<a class="btn btn-show advanced_parameters" id="advanced_params" href="javascript://">'.get_lang('AdvancedParameters').'</a>');
- $form->addElement('html', '<div id="advanced_params_options" style="display:none;">');
- }
- $select_level = Question::get_default_levels();
- $form->addElement('select', 'questionLevel', get_lang('Difficulty'), $select_level);
- $categoryJS = null;
- if (!empty($this->category_list)) {
- $trigger = '';
- foreach ($this->category_list as $category_id) {
- if (!empty($category_id)) {
- $cat = new Testcategory($category_id);
- if ($cat->id) {
- $trigger .= '$("#category_id").trigger("addItem",[{"title": "'.$cat->parent_path.'", "value": "'.$cat->id.'"}]);';
- }
- }
- }
- $categoryJS .= '<script>$(function() { '.$trigger.' });</script>';
- }
- $form->addElement('html', $categoryJS);
- $form->addElement(
- 'select',
- 'questionCategory',
- get_lang('Category'),
- array(),
- array('id' => 'category_id')
- );
- // Categories
- //$tabCat = Testcategory::getCategoriesIdAndName();
- //$form->addElement('select', 'questionCategory', get_lang('Category'), $tabCat);
- //Medias
- $course_medias = Question::prepare_course_media_select(api_get_course_int_id());
- $form->addElement('select', 'parent_id', get_lang('AttachToMedia'), $course_medias);
- // Inject question extra fields!
- $extraFields = new ExtraField('question');
- $extraFields->add_elements($form, $this->id);
- if ($this->exercise->fastExerciseEdition == false) {
- $form->addElement('html', '</div>');
- }
- }
- // @todo why we need this condition??
- if ($this->setDefaultQuestionValues) {
- switch ($answerType) {
- case 1:
- $this->question = get_lang('DefaultUniqueQuestion');
- break;
- case 2:
- $this->question = get_lang('DefaultMultipleQuestion');
- break;
- case 3:
- $this->question = get_lang('DefaultFillBlankQuestion');
- break;
- case 4:
- $this->question = get_lang('DefaultMathingQuestion');
- break;
- case 5:
- $this->question = get_lang('DefaultOpenQuestion');
- break;
- case 9:
- $this->question = get_lang('DefaultMultipleQuestion');
- break;
- }
- }
- // default values
- $defaults = array();
- $defaults['questionName'] = $this->question;
- $defaults['questionDescription'] = $this->description;
- $defaults['questionLevel'] = $this->level;
- $defaults['questionCategory'] = $this->category_list;
- $defaults['parent_id'] = $this->parent_id;
- //Came from he question pool
- if (isset($_GET['fromExercise'])) {
- $form->setDefaults($defaults);
- }
- if (!empty($_REQUEST['myid'])) {
- $form->setDefaults($defaults);
- } else {
- if ($isContent == 1) {
- $form->setDefaults($defaults);
- }
- }
- if ($this->setDefaultValues) {
- $form->setDefaults($defaults);
- }
- }
- /**
- * function which process the creation of questions
- * @param FormValidator $form the formvalidator instance
- * @param Exercise $objExercise the Exercise instance
- */
- function processCreation($form, $objExercise = null)
- {
- $this->updateParentId($form->getSubmitValue('parent_id'));
- $this->updateTitle($form->getSubmitValue('questionName'));
- $this->updateDescription($form->getSubmitValue('questionDescription'));
- $this->updateLevel($form->getSubmitValue('questionLevel'));
- $this->updateCategory($form->getSubmitValue('questionCategory'));
- // Save normal question if NOT media
- if ($this->type != MEDIA_QUESTION) {
- $this->save($objExercise->id);
- $field_value = new ExtraFieldValue('question');
- $params = $form->exportValues();
- $params['question_id'] = $this->id;
- $field_value->save_field_values($params);
- if ($objExercise) {
- // modify the exercise
- $objExercise->addToList($this->id);
- $objExercise->update_question_positions();
- }
- }
- }
- /**
- * abstract function which creates the form to create / edit the answers of the question
- * @param the formvalidator instance
- */
- abstract function createAnswersForm($form);
- /**
- * abstract function which process the creation of answers
- * @param the formvalidator instance
- */
- abstract function processAnswersCreation($form);
- /**
- * Displays the menu of question types
- */
- static function display_type_menu($objExercise)
- {
- $feedback_type = $objExercise->feedback_type;
- $exerciseId = $objExercise->id;
- // 1. by default we show all the question types
- $question_type_custom_list = self::get_question_type_list();
- if (!isset($feedback_type)) {
- $feedback_type = 0;
- }
- if ($feedback_type == 1) {
- //2. but if it is a feedback DIRECT we only show the UNIQUE_ANSWER type that is currently available
- $question_type_custom_list = array(
- UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER],
- HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION]
- );
- } else {
- unset($question_type_custom_list[HOT_SPOT_DELINEATION]);
- }
- echo '<div class="actionsbig">';
- echo '<ul class="question_menu">';
- foreach ($question_type_custom_list as $i => $a_type) {
- // include the class of the type
- require_once $a_type[0];
- // get the picture of the type and the langvar which describes it
- $img = $explanation = '';
- eval('$img = '.$a_type[1].'::$typePicture;');
- eval('$explanation = get_lang('.$a_type[1].'::$explanationLangVar);');
- echo '<li>';
- echo '<div class="icon_image_content">';
- if ($objExercise->exercise_was_added_in_lp == true) {
- $img = pathinfo($img);
- $img = $img['filename'].'_na.'.$img['extension'];
- echo Display::return_icon($img, $explanation, array(), ICON_SIZE_BIG);
- } else {
- echo '<a href="admin.php?'.api_get_cidreq(
- ).'&newQuestion=yes&answerType='.$i.'&exerciseId='.$exerciseId.'">'.Display::return_icon(
- $img,
- $explanation,
- array(),
- ICON_SIZE_BIG
- ).'</a>';
- }
- echo '</div>';
- echo '</li>';
- }
- echo '<li>';
- echo '<div class="icon_image_content">';
- if ($objExercise->exercise_was_added_in_lp == true) {
- echo Display::return_icon('database_na.png', get_lang('GetExistingQuestion'));
- } else {
- if ($feedback_type == 1) {
- //echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&type=1&fromExercise='.$exerciseId.'">';
- } else {
- //echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">';
- }
- echo $url = '<a href="'.api_get_path(WEB_PUBLIC_PATH).'courses/'.api_get_course_path().'/'.api_get_session_id().'/exercise/'.$exerciseId.'/question-pool">';
- echo Display::return_icon('database.png', get_lang('GetExistingQuestion'));
- }
- echo '</a>';
- echo '</div></li>';
- echo '</ul>';
- echo '</div>';
- }
- static function saveQuestionOption($question_id, $name, $course_id, $position = 0)
- {
- $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
- $params['question_id'] = intval($question_id);
- $params['name'] = $name;
- $params['position'] = $position;
- $params['c_id'] = $course_id;
- //$result = self::readQuestionOption($question_id, $course_id);
- $last_id = Database::insert($TBL_EXERCICE_QUESTION_OPTION, $params);
- return $last_id;
- }
- static function deleteAllQuestionOptions($question_id, $course_id)
- {
- $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
- Database::delete(
- $TBL_EXERCICE_QUESTION_OPTION,
- array('c_id = ? AND question_id = ?' => array($course_id, $question_id))
- );
- }
- static function updateQuestionOption($id, $params, $course_id)
- {
- $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
- $result = Database::update(
- $TBL_EXERCICE_QUESTION_OPTION,
- $params,
- array('c_id = ? AND id = ?' => array($course_id, $id))
- );
- return $result;
- }
- static function readQuestionOption($question_id, $course_id)
- {
- $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
- $result = Database::select(
- '*',
- $TBL_EXERCICE_QUESTION_OPTION,
- array(
- 'where' => array('c_id = ? AND question_id = ?' => array($course_id, $question_id)),
- 'order' => 'iid ASC'
- )
- );
- if (!empty($result)) {
- $new_result = array();
- foreach ($result as $item) {
- $new_result[$item['iid']] = $item;
- }
- return $new_result;
- }
- return array();
- }
- /**
- * Shows question title an description
- *
- * @param type $feedback_type
- * @param type $counter
- * @param type $score
- *
- * @return string
- */
- function return_header($feedback_type = null, $counter = null, $score = null, $show_media)
- {
- $counterLabel = '';
- if (!empty($counter)) {
- $counterLabel = $counter;
- }
- $score_label = get_lang('Wrong');
- $class = 'error';
- if ($score['pass'] == true) {
- $score_label = get_lang('Correct');
- $class = 'success';
- }
- if ($this->type == FREE_ANSWER || $this->type == ORAL_EXPRESSION) {
- if ($score['revised'] == true) {
- $score_label = get_lang('Revised');
- $class = '';
- } else {
- $score_label = get_lang('NotRevised');
- $class = 'error';
- }
- }
- $question_title = $this->question;
- $header = null;
- // Display question category, if any
- if ($show_media) {
- $header .= $this->show_media_content();
- }
- $header .= Display::page_subheader2($counterLabel.". ".$question_title);
- $header .= Display::div(
- '<div class="rib rib-'.$class.'"><h3>'.$score_label.'</h3></div> <h4>'.$score['result'].' </h4>',
- array('class' => 'ribbon')
- );
- $header .= Display::div($this->description, array('id' => 'question_description'));
- return $header;
- }
- /**
- * Create a question from a set of parameters
- * @param int Quiz ID
- * @param string Question name
- * @param int Maximum result for the question
- * @param int Type of question (see constants at beginning of question.class.php)
- * @param int Question level/category
- */
- public function create_question($quiz_id, $question_name, $max_score = 0, $type = 1, $level = 1)
- {
- $course_id = api_get_course_int_id();
- $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $quiz_id = intval($quiz_id);
- $max_score = (float)$max_score;
- $type = intval($type);
- $level = intval($level);
- // Get the max position
- $sql = "SELECT max(position) as max_position"
- ." FROM $tbl_quiz_question q INNER JOIN $tbl_quiz_rel_question r"
- ." ON q.iid = r.question_id"
- ." AND exercice_id = $quiz_id AND q.c_id = $course_id AND r.c_id = $course_id";
- $rs_max = Database::query($sql);
- $row_max = Database::fetch_object($rs_max);
- $max_position = $row_max->max_position + 1;
- // Insert the new question
- $sql = "INSERT INTO $tbl_quiz_question (c_id, question, ponderation, position, type, level)
- VALUES ($course_id, '".Database::escape_string(
- $question_name
- )."', '$max_score', $max_position, $type, $level)";
- Database::query($sql);
- // Get the question ID
- $question_id = Database::get_last_insert_id();
- // Get the max question_order
- $sql = "SELECT max(question_order) as max_order "
- ."FROM $tbl_quiz_rel_question WHERE c_id = $course_id AND exercice_id = $quiz_id ";
- $rs_max_order = Database::query($sql);
- $row_max_order = Database::fetch_object($rs_max_order);
- $max_order = $row_max_order->max_order + 1;
- // Attach questions to quiz
- $sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, exercice_id, question_order)"
- ." VALUES($course_id, $question_id, $quiz_id, $max_order)";
- Database::query($sql);
- return $question_id;
- }
- /**
- * return the image filename of the question type
- *
- */
- public function get_type_icon_html()
- {
- $type = $this->selectType();
- // [0]=file to include [1]=type name
- $tabQuestionList = Question::get_question_type_list();
- require_once $tabQuestionList[$type][0];
- eval('$img = '.$tabQuestionList[$type][1].'::$typePicture;');
- eval('$explanation = get_lang('.$tabQuestionList[$type][1].'::$explanationLangVar);');
- return array($img, $explanation);
- }
- /**
- * Get course medias
- * @param int course id
- */
- static function get_course_medias(
- $course_id,
- $start = 0,
- $limit = 100,
- $sidx = "question",
- $sord = "ASC",
- $where_condition = array()
- ) {
- $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $default_where = array('c_id = ? AND parent_id = 0 AND type = ?' => array($course_id, MEDIA_QUESTION));
- if (!empty($where_condition)) {
- //$where_condition
- }
- $result = Database::select(
- '*',
- $table_question,
- array(
- 'limit' => " $start, $limit",
- 'where' => $default_where,
- 'order' => "$sidx $sord"
- )
- );
- return $result;
- }
- /**
- * Get count course medias
- * @param int course id
- */
- static function get_count_course_medias($course_id)
- {
- $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $result = Database::select(
- 'count(*) as count',
- $table_question,
- array('where' => array('c_id = ? AND parent_id = 0 AND type = ?' => array($course_id, MEDIA_QUESTION))),
- 'first'
- );
- if ($result && isset($result['count'])) {
- return $result['count'];
- }
- return 0;
- }
- static function prepare_course_media_select($course_id)
- {
- $medias = self::get_course_medias($course_id);
- $media_list = array();
- $media_list[0] = get_lang('NoMedia');
- if (!empty($medias)) {
- foreach ($medias as $media) {
- $media_list[$media['iid']] = empty($media['question']) ? get_lang('Untitled') : $media['question'];
- }
- }
- return $media_list;
- }
- static function get_default_levels()
- {
- $select_level = array(
- 1 => 1,
- 2 => 2,
- 3 => 3,
- 4 => 4,
- 5 => 5
- );
- return $select_level;
- }
- function show_media_content()
- {
- $html = null;
- if ($this->parent_id != 0) {
- $parent_question = Question::read($this->parent_id);
- $html = $parent_question->show_media_content();
- } else {
- $html .= Display::page_subheader($this->selectTitle());
- $html .= $this->selectDescription();
- }
- return $html;
- }
- public static function question_type_no_review()
- {
- return array(
- HOT_SPOT,
- HOT_SPOT_ORDER,
- HOT_SPOT_DELINEATION
- );
- }
- public static function getMediaLabels()
- {
- // Shows media questions
- $courseMedias = Question::prepare_course_media_select(api_get_course_int_id());
- $labels = null;
- if (!empty($courseMedias)) {
- $labels .= get_lang('MediaQuestion').' ';
- foreach ($courseMedias as $mediaId => $media) {
- $editLink = '<a href="'.api_get_self().'?'.api_get_cidreq().'&type='.MEDIA_QUESTION.'&myid=1&editQuestion='.$mediaId.'">'.Display::return_icon('edit.png',get_lang('Modify'), array(), ICON_SIZE_SMALL).'</a>';
- $deleteLink = '<a id="delete_'.$mediaId.'" class="opener" href="'.api_get_self().'?'.api_get_cidreq().'&deleteQuestion='.$mediaId.'" >'.Display::return_icon('delete.png',get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
- if (!empty($mediaId)) {
- $labels .= self::getMediaLabel($media).''.$editLink.$deleteLink.' ';
- }
- }
- }
- return $labels;
- }
- /**
- * Get question columns needed for the new question pool page
- * @param int course code
- * @return array
- */
- public static function getQuestionColumns($courseCode = null, $questionFields = array())
- {
- // The order is important you need to check the the $column variable in the model.ajax.php file
- //$columns = array('id', get_lang('Name'), get_lang('Description'));
- $columns = array('id', get_lang('Name'));
- // Column config.
- $columnModel = array(
- array(
- 'name' => 'iid',
- 'index' => 'iid',
- 'width' => '20',
- 'align' => 'left'
- ),
- array(
- 'name' => 'question',
- 'index' => 'question',
- 'width' => '200',
- 'align' => 'left'
- ),
- /* array(
- 'name' => 'description',
- 'index' => 'description',
- 'width' => '100',
- 'align' => 'left',
- 'sortable' => 'false'
- )*/
- );
- // Extra field rules.
- $extraField = new \ExtraField('question');
- $rules = $extraField->getRules($columns, $columnModel);
- // Exercise rules.
- self::getRules($courseCode, $rules, $columns, $columnModel, $questionFields);
- $columns[] = get_lang('Actions');
- $columnModel[] = array(
- 'name' => 'actions',
- 'index' => 'actions',
- 'width' => '30',
- 'align' => 'left',
- 'formatter' => 'action_formatter',
- 'sortable' => 'false'
- );
- foreach ($columnModel as $col) {
- $simple_column_name[] = $col['name'];
- }
- $return_array = array(
- 'columns' => $columns,
- 'column_model' => $columnModel,
- 'rules' => $rules,
- 'simple_column_name' => $simple_column_name
- );
- return $return_array;
- }
- /**
- * Get all questions
- * @param array $options
- * @return array
- */
- public static function getQuestions($categoryId, $exerciseId, $courseId, $options, $get_count = false)
- {
- $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION);
- $questionPoolFields = array(
- 'question_session_id' => array(
- 'innerjoin' => " INNER JOIN ".Database::get_course_table(TABLE_QUIZ_TEST_QUESTION)." as quiz_rel_question_session ON (quiz_rel_question_session.question_id = s.iid)
- INNER JOIN ".Database::get_course_table(TABLE_QUIZ_TEST)." as quizsession ON (quizsession.iid = quiz_rel_question_session.exercice_id)
- INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)." session ON (session.id = quizsession.session_id)",
- 'where' => 'session_id',
- 'inject_fields' => 'session.name as question_session_id, ',
- ),
- 'question_category_id' => array(
- 'innerjoin' => " INNER JOIN ".Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY)." as quiz_rel_cat ON (quiz_rel_cat.question_id = s.iid)
- INNER JOIN ".Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY)." as cat ON (cat.iid = quiz_rel_cat.category_id)
- ",
- 'where' => 'quiz_rel_cat.category_id',
- 'inject_fields' => 'cat.title as question_category_id, ',
- ),
- 'question_exercise_id' => array(
- 'innerjoin' => " INNER JOIN ".Database::get_course_table(TABLE_QUIZ_TEST_QUESTION)." as quiz_rel_question ON (quiz_rel_question.question_id = s.iid)
- INNER JOIN ".Database::get_course_table(TABLE_QUIZ_TEST)." as quizexercise ON (quizexercise.iid = quiz_rel_question.exercice_id)
- ",
- 'where' => 'quiz_rel_question.exercice_id',
- 'inject_fields' => 'quizexercise.title as question_exercise_id, ',
- ),
- 'question_c_id' => array(
- 'where' => 's.c_id',
- 'innerjoin' => " INNER JOIN ".Database::get_main_table(TABLE_MAIN_COURSE)." as course ON (course.id = s.c_id) ",
- 'inject_fields' => 'course.title as question_c_id, '
- ),
- 'question_question_type' => array(
- 'where' => 's.type ',
- 'inject_fields' => 's.type as question_question_type,'
- ),
- 'question_difficulty' => array(
- 'where' => 's.level',
- 'inject_fields' => 's.level as question_difficulty, '
- )
- );
- $inject_extra_fields = null;
- $inject_joins = null;
- $where = $options['where'];
- $newQuestionPoolField = array();
- if (isset($options['question'])) {
- foreach ($options['question'] as $question) {
- if (isset($questionPoolFields[$question['field']])) {
- $newQuestionPoolField[$question['field']] = $questionPoolFields[$question['field']];
- }
- }
- }
- $inject_question_fields = null;
- $questionPoolFields = $newQuestionPoolField;
- foreach ($questionPoolFields as $field => $option) {
- $where = str_replace($field, $option['where'], $where);
- if (isset($option['innerjoin']) && !empty($option['innerjoin'])) {
- $inject_joins .= $option['innerjoin'];
- }
- if (isset($option['inject_fields']) && !empty($option['inject_fields'])) {
- $inject_question_fields .= $option['inject_fields'];
- }
- }
- $options['where'] = $where;
- $extra_field = new ExtraField('question');
- $conditions = $extra_field->parseConditions($options);
- $inject_joins .= $conditions['inject_joins'];
- $where = $conditions['where'];
- $inject_where = $conditions['inject_where'];
- $inject_extra_fields = $conditions['inject_extra_fields'];
- $order = $conditions['order'];
- $limit = $conditions['limit'];
- if ($get_count == true) {
- $select = " SELECT count(*) as total_rows";
- } else {
- $select = " SELECT s.*, $inject_extra_fields $inject_question_fields 1 ";
- }
- $extraCondition = null;
- if (!empty($categoryId)) {
- $categoryRelQuestionTable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
- $extraCondition = " INNER JOIN $categoryRelQuestionTable c ON (s.iid = c.question_id)";
- $categoryId = intval($categoryId);
- $where .= " AND category_id = $categoryId ";
- }
- if (!empty($exerciseId)) {
- $exerciseRelQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
- $extraCondition .= " INNER JOIN $exerciseRelQuestionTable e ON (s.iid = e.question_id)";
- $exerciseId = intval($exerciseId);
- $where .= " AND exercice_id = $exerciseId ";
- }
- if (!empty($courseId)) {
- $courseId = intval($courseId);
- $where .= " AND s.c_id = $courseId ";
- }
- $query = " $select FROM $questionTable s $inject_joins $extraCondition WHERE 1=1 $where $inject_where $order $limit";
- //echo $query.'<br />';
- $result = Database::query($query);
- $questions = array();
- if (Database::num_rows($result)) {
- $questions = Database::store_result($result, 'ASSOC');
- if ($get_count) {
- return $questions[0]['total_rows'];
- }
- }
- return $questions;
- }
- public static function getMediaLabel($title)
- {
- return Display::label($title, 'warning');
- }
- /**
- * @param $courseCode
- * @param $rules
- * @param $columns
- * @param $column_model
- * @return array
- */
- public static function getRules($courseCode, &$rules, &$columns, &$column_model, $questionFields)
- {
- // sessions
- // course
- // categories
- // exercises
- // difficult
- // type
- if (empty($courseCode)) {
- // Session
- $sessionList = SessionManager::get_sessions_by_general_coach(api_get_user_id());
- $fields = array();
- if (!empty($sessionList)) {
- $new_options = array();
- $new_options[] = "-1:".get_lang('All');
- foreach ($sessionList as $session) {
- $new_options[] = "{$session['id']}:{$session['name']}";
- }
- $string = implode(';', $new_options);
- $fields[] = array(
- 'field_display_text' => get_lang('Session'),
- 'field_variable' => 'session_id',
- 'field_type' => ExtraField::FIELD_TYPE_SELECT,
- 'field_default_value' => null,
- 'field_options' => $string
- );
- }
- // Courses
- $courseList = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id());
- if (!empty($courseList)) {
- $new_options = array();
- $new_options[] = "-1:".get_lang('All');
- foreach ($courseList as $course) {
- $new_options[] = "{$course['id']}:{$course['title']}";
- }
- $string = implode(';', $new_options);
- $fields[] = array(
- 'field_display_text' => get_lang('Course'),
- 'field_variable' => 'c_id',
- 'field_type' => ExtraField::FIELD_TYPE_SELECT,
- 'field_default_value' => null,
- 'field_options' => $string
- );
- }
- } else {
- $courseList = array(api_get_course_info());
- }
- // Categories
- $string = null;
- if (!empty($courseList)) {
- $new_options = array();
- $new_options[] = "-1:".get_lang('All');
- // Global categories
- // @todo use tree view
- $categories = Testcategory::getCategoriesIdAndName(0);
- if (!empty($categories)) {
- foreach ($categories as $id => $category) {
- if (!empty($id)) {
- $new_options[] = "$id:[Global] - ".$category;
- }
- }
- }
- foreach ($courseList as $course) {
- $categories = Testcategory::getCategoriesIdAndName($course['real_id']);
- if (!empty($categories)) {
- foreach ($categories as $id => $category) {
- if (!empty($id)) {
- $new_options[] = "$id:".$course['title']." - ".$category;
- }
- }
- }
- }
- $string = implode(';', $new_options);
- $fields[] = array(
- 'field_display_text' => get_lang('Category'),
- 'field_variable' => 'category_id',
- 'field_type' => ExtraField::FIELD_TYPE_SELECT,
- 'field_default_value' => null,
- 'field_options' => $string
- );
- }
- $course = api_get_course_int_id();
- $sessionId = api_get_session_id();
- // Exercises
- $exerciseList = ExerciseLib::get_all_exercises_for_course_id($sessionId, $course);
- if (!empty($exerciseList)) {
- $new_options = array();
- $new_options[] = "-1:".get_lang('All');
- $new_options[] = "0:".get_lang('Orphan');
- foreach ($exerciseList as $exercise) {
- $new_options[] = "{$exercise['iid']}:{$exercise['title']}";
- }
- $string = implode(';', $new_options);
- $fields[] = array(
- 'field_display_text' => get_lang('Exercise'),
- 'field_variable' => 'exercise_id',
- 'field_type' => ExtraField::FIELD_TYPE_SELECT,
- 'field_default_value' => null,
- 'field_options' => $string
- );
- }
- // Question type
- $questionList = Question::get_question_type_list();
- if (!empty($questionList)) {
- $new_options = array();
- $new_options[] = "-1:".get_lang('All');
- foreach ($questionList as $key => $question) {
- $new_options[] = "{$key}:".get_lang($question['1']);
- }
- $string = implode(';', $new_options);
- $fields[] = array(
- 'field_display_text' => get_lang('AnswerType'),
- 'field_variable' => 'question_type',
- 'field_type' => ExtraField::FIELD_TYPE_SELECT,
- 'field_default_value' => null,
- 'field_options' => $string
- );
- }
- // Difficult
- $levels = Question::get_default_levels();
- if (!empty($levels)) {
- $new_options = array();
- $new_options[] = "-1:".get_lang('All');
- foreach ($levels as $key => $level) {
- $new_options[] ="{$key}:{$level}";
- }
- $string = implode(';', $new_options);
- $fields[] = array(
- 'field_display_text' => get_lang('Difficulty'),
- 'field_variable' => 'difficulty',
- 'field_type' => ExtraField::FIELD_TYPE_SELECT,
- 'field_default_value' => null,
- 'field_options' => $string
- );
- }
- $questionFieldsKeys = array();
- if (!empty($questionFields)) {
- foreach ($questionFields as $question) {
- $questionFieldsKeys[] = $question['field'];
- }
- }
- if (!empty($fields)) {
- foreach ($fields as $field) {
- if (isset($questionFields) && !empty($questionFields)) {
- if (!in_array('question_'.$field['field_variable'], $questionFieldsKeys)) {
- continue;
- }
- }
- $search_options = array();
- $type = 'text';
- if (in_array($field['field_type'], array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT))) {
- $type = 'select';
- $search_options['sopt'] = array('eq', 'ne'); //equal not equal
- //$search_options['sopt'] = array('cn', 'nc'); //contains not contains
- } else {
- $search_options['sopt'] = array('cn', 'nc'); //contains not contains
- }
- $search_options['searchhidden'] = 'true';
- $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
- $search_options['value'] = $field['field_options'];
- $column_model[] = array(
- 'name' => 'question_'.$field['field_variable'],
- 'index' => 'question_'.$field['field_variable'],
- 'width' => '100',
- 'hidden' => 'true',
- 'search' => 'true',
- 'stype' => $type,
- 'searchoptions' => $search_options
- );
- $columns[] = $field['field_display_text'];
- $rules[] = array(
- 'field' => 'question_'.$field['field_variable'],
- 'op' => 'eq'
- );
- }
- }
- return $rules;
- }
- }
|