Search in sources :

Example 31 with QuestionItemView

use of org.olat.modules.qpool.QuestionItemView in project OpenOLAT by OpenOLAT.

the class QItemQueriesDAO method getItemsOfPool.

public List<QuestionItemView> getItemsOfPool(SearchQuestionItemParams params, Collection<Long> inKeys, int firstResult, int maxResults, SortKey... orderBy) {
    StringBuilder sb = new StringBuilder();
    sb.append("select item,").append(" (select count(sgmi.key) from ").append(SecurityGroupMembershipImpl.class.getName()).append(" as sgmi").append("   where sgmi.identity.key=:identityKey and sgmi.securityGroup=ownerGroup").append(" ) as owners,").append(" (select count(competence.key) from ctaxonomycompetence competence").append("   where taxonomyLevel.materializedPathKeys like concat(competence.taxonomyLevel.materializedPathKeys, '%')").append("     and competence.identity.key=:identityKey").append("     and competence.type='").append(TaxonomyCompetenceTypes.teach).append("'").append(" ) as teacher,").append(" (select count(competence.key) from ctaxonomycompetence competence").append("   where taxonomyLevel.materializedPathKeys like concat(competence.taxonomyLevel.materializedPathKeys, '%')").append("     and competence.identity.key=:identityKey").append("     and competence.type='").append(TaxonomyCompetenceTypes.manage).append("'").append(" ) as manager,").append("pool2item.editable,").append(" (select count(mark.key) from ").append(MarkImpl.class.getName()).append(" as mark ").append("   where mark.creator.key=:identityKey and mark.resId=item.key and mark.resName='QuestionItem'").append(" ) as marks,").append(" (select count(rating.key) from userrating as rating").append("   where rating.resId=item.key and rating.resName='QuestionItem'").append("     and rating.creator.key=:identityKey").append(" ) as numberOfRatingsIdentity,").append(" (select avg(rating.rating) from userrating as rating").append("   where rating.resId=item.key and rating.resName='QuestionItem'").append(" ) as rating,").append(" (select count(rating.key) from userrating as rating").append("   where rating.resId=item.key and rating.resName='QuestionItem'").append(" ) as numberOfRatingsTotal").append(" from qpool2item pool2item").append(" inner join pool2item.item item").append(" inner join fetch item.ownerGroup ownerGroup").append(" left join fetch item.type itemType").append(" left join fetch item.taxonomyLevel taxonomyLevel").append(" left join fetch item.educationalContext educationalContext").append(" where pool2item.pool.key=:poolKey");
    if (inKeys != null && inKeys.size() > 0) {
        sb.append(" and item.key in (:inKeys)");
    }
    if (StringHelper.containsNonWhitespace(params.getFormat())) {
        sb.append(" and item.format=:format");
    }
    appendOrderBy(sb, "item", "taxonomyLevel", orderBy);
    TypedQuery<Object[]> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Object[].class).setParameter("poolKey", params.getPoolKey()).setParameter("identityKey", params.getIdentity().getKey());
    if (inKeys != null && inKeys.size() > 0) {
        query.setParameter("inKeys", inKeys);
    }
    appendParameters(params, query);
    if (firstResult >= 0) {
        query.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
    List<Object[]> results = query.getResultList();
    List<QuestionItemView> views = new ArrayList<>();
    for (Object[] result : results) {
        ItemWrapper itemWrapper = ItemWrapper.builder((QuestionItemImpl) result[0]).setAuthor((Number) result[1]).setTeacher((Number) result[2]).setManager((Number) result[3]).setEditableInPool((Boolean) result[4]).setMarked((Number) result[5]).setRater((Number) result[6]).setRating((Double) result[7]).setNumberOfRatings((Number) result[8]).create();
        views.add(itemWrapper);
    }
    return views;
}
Also used : QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) ArrayList(java.util.ArrayList) MarkImpl(org.olat.core.commons.services.mark.impl.MarkImpl) ItemWrapper(org.olat.modules.qpool.model.ItemWrapper) SecurityGroupMembershipImpl(org.olat.basesecurity.SecurityGroupMembershipImpl) QuestionItemView(org.olat.modules.qpool.QuestionItemView)

Example 32 with QuestionItemView

use of org.olat.modules.qpool.QuestionItemView in project OpenOLAT by OpenOLAT.

the class QItemQueriesDAO method getFavoritItems.

public List<QuestionItemView> getFavoritItems(SearchQuestionItemParams params, Collection<Long> inKeys, int firstResult, int maxResults, SortKey... orderBy) {
    StringBuilder sb = new StringBuilder();
    sb.append("select item, ").append(" (select count(sgmi.key) from ").append(SecurityGroupMembershipImpl.class.getName()).append(" as sgmi").append("   where sgmi.identity.key=:identityKey and sgmi.securityGroup=ownerGroup").append(" ) as owners,").append(" (select count(competence.key) from ctaxonomycompetence competence").append("   where taxonomyLevel.materializedPathKeys like concat(competence.taxonomyLevel.materializedPathKeys, '%')").append("     and competence.identity.key=:identityKey").append("     and competence.type='").append(TaxonomyCompetenceTypes.teach).append("'").append(" ) as teacher,").append(" (select count(competence.key) from ctaxonomycompetence competence").append("   where taxonomyLevel.materializedPathKeys like concat(competence.taxonomyLevel.materializedPathKeys, '%')").append("     and competence.identity.key=:identityKey").append("     and competence.type='").append(TaxonomyCompetenceTypes.manage).append("'").append(" ) as manager,").append(" (select count(pool2item.key) from qpool2item pool2item").append("    where pool2item.item.key=item.key").append("      and pool2item.editable is true").append(" ) as pools,").append(" (select count(shareditem.key) from qshareitem shareditem").append("    where shareditem.item.key=item.key").append("      and shareditem.editable is true").append(" ) as groups,").append(" (select count(rating.key) from userrating as rating").append("   where rating.resId=item.key and rating.resName='QuestionItem'").append("     and rating.creator.key=:identityKey").append(" ) as numberOfRatingsIdentity,").append(" (select avg(rating.rating) from userrating as rating").append("   where rating.resId=item.key and rating.resName='QuestionItem'").append(" ) as rating,").append(" (select count(rating.key) from userrating as rating").append("   where rating.resId=item.key and rating.resName='QuestionItem'").append(" ) as numberOfRatingsTotal").append(" from questionitem item").append(" inner join fetch item.ownerGroup ownerGroup").append(" left join fetch item.type itemType").append(" left join fetch item.taxonomyLevel taxonomyLevel").append(" left join fetch item.educationalContext educationalContext").append(" where exists (").append("   select mark.key from ").append(MarkImpl.class.getName()).append(" as mark ").append("   where mark.creator.key=:identityKey and mark.resId=item.key and mark.resName='QuestionItem'").append(" )");
    if (inKeys != null && inKeys.size() > 0) {
        sb.append(" and item.key in (:inKeys)");
    }
    if (StringHelper.containsNonWhitespace(params.getFormat())) {
        sb.append(" and item.format=:format");
    }
    if (orderBy != null && orderBy.length > 0 && orderBy[0] != null && !OrderBy.marks.name().equals(orderBy[0].getKey())) {
        appendOrderBy(sb, "item", "taxonomyLevel", orderBy);
    }
    TypedQuery<Object[]> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Object[].class).setParameter("identityKey", params.getIdentity().getKey());
    if (inKeys != null && inKeys.size() > 0) {
        query.setParameter("inKeys", inKeys);
    }
    appendParameters(params, query);
    if (firstResult >= 0) {
        query.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
    List<Object[]> results = query.getResultList();
    List<QuestionItemView> views = new ArrayList<>();
    for (Object[] result : results) {
        ItemWrapper itemWrapper = ItemWrapper.builder((QuestionItemImpl) result[0]).setAuthor((Number) result[1]).setTeacher((Number) result[2]).setManager((Number) result[3]).setEditableInPool((Number) result[4]).setEditableInShare((Number) result[5]).setRater((Number) result[6]).setRating((Double) result[7]).setNumberOfRatings((Number) result[8]).setMarked(true).create();
        views.add(itemWrapper);
    }
    return views;
}
Also used : ItemWrapper(org.olat.modules.qpool.model.ItemWrapper) SecurityGroupMembershipImpl(org.olat.basesecurity.SecurityGroupMembershipImpl) ArrayList(java.util.ArrayList) QuestionItemView(org.olat.modules.qpool.QuestionItemView)

Example 33 with QuestionItemView

use of org.olat.modules.qpool.QuestionItemView in project OpenOLAT by OpenOLAT.

the class QuestionDAOTest method shareItems_removeFromBusinessGroups_paranoid.

@Test
public void shareItems_removeFromBusinessGroups_paranoid() {
    // create a group to share 2 items
    QItemType mcType = qItemTypeDao.loadByType(QuestionType.MC.name());
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("Share-item-" + UUID.randomUUID().toString());
    BusinessGroup group1 = businessGroupDao.createAndPersist(id, "gdao-b", "gdao-desc", -1, -1, false, false, false, false, false);
    BusinessGroup group2 = businessGroupDao.createAndPersist(id, "gdao-c", "gdao-desc", -1, -1, false, false, false, false, false);
    QuestionItem item1 = questionDao.createAndPersist(id, "Share-Item-Dup-1", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    QuestionItem item2 = questionDao.createAndPersist(id, "Share-Item-Dup-1", QTI21Constants.QTI_21_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    questionDao.share(item1, group1.getResource());
    questionDao.share(item1, group2.getResource());
    questionDao.share(item2, group1.getResource());
    questionDao.share(item2, group2.getResource());
    dbInstance.commit();
    // check them
    int numOfItems_1 = questionDao.countSharedItemByResource(group1.getResource(), null);
    Assert.assertEquals(2, numOfItems_1);
    int numOfItems_2 = questionDao.countSharedItemByResource(group2.getResource(), null);
    Assert.assertEquals(2, numOfItems_2);
    // remove
    questionDao.removeFromShare(Collections.<QuestionItemShort>singletonList(item2), group1.getResource());
    dbInstance.commitAndCloseSession();
    // check
    int numOfStayingItems_1 = questionDao.countSharedItemByResource(group1.getResource(), null);
    Assert.assertEquals(1, numOfStayingItems_1);
    int numOfStayingItems_2 = questionDao.countSharedItemByResource(group2.getResource(), null);
    Assert.assertEquals(2, numOfStayingItems_2);
    List<QuestionItemView> items_1 = qItemQueriesDao.getSharedItemByResource(id, group1.getResource(), null, null, 0, -1);
    Assert.assertEquals(1, items_1.size());
    Assert.assertEquals(item1.getKey(), items_1.get(0).getKey());
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) Identity(org.olat.core.id.Identity) QuestionItemView(org.olat.modules.qpool.QuestionItemView) QuestionItem(org.olat.modules.qpool.QuestionItem) QItemType(org.olat.modules.qpool.model.QItemType) Test(org.junit.Test)

Example 34 with QuestionItemView

use of org.olat.modules.qpool.QuestionItemView in project OpenOLAT by OpenOLAT.

the class QuestionDAOTest method shareItems_avoidDuplicates.

@Test
public void shareItems_avoidDuplicates() {
    // create a group to share 2 items
    QItemType mcType = qItemTypeDao.loadByType(QuestionType.MC.name());
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("QShare-3-" + UUID.randomUUID());
    BusinessGroup group = businessGroupDao.createAndPersist(id, "gdao", "gdao-desc", -1, -1, false, false, false, false, false);
    QuestionItem item = questionDao.createAndPersist(id, "Share-Item-Dup-1", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    dbInstance.commit();
    // share them
    questionDao.share(item, group.getResource());
    questionDao.share(item, group.getResource());
    questionDao.share(item, group.getResource());
    // retrieve them
    List<QuestionItemView> sharedItems = qItemQueriesDao.getSharedItemByResource(id, group.getResource(), null, null, 0, -1);
    Assert.assertNotNull(sharedItems);
    Assert.assertEquals(1, sharedItems.size());
    Assert.assertEquals(item.getKey(), sharedItems.get(0).getKey());
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) Identity(org.olat.core.id.Identity) QuestionItemView(org.olat.modules.qpool.QuestionItemView) QuestionItem(org.olat.modules.qpool.QuestionItem) QItemType(org.olat.modules.qpool.model.QItemType) Test(org.junit.Test)

Example 35 with QuestionItemView

use of org.olat.modules.qpool.QuestionItemView in project OpenOLAT by OpenOLAT.

the class QuestionDAOTest method removeFromShare.

@Test
public void removeFromShare() {
    // create a group to share 2 items
    QItemType mcType = qItemTypeDao.loadByType(QuestionType.MC.name());
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("Share-rm-" + UUID.randomUUID().toString());
    BusinessGroup group = businessGroupDao.createAndPersist(id, "gdrm", "gdrm-desc", -1, -1, false, false, false, false, false);
    QuestionItem item = questionDao.createAndPersist(id, "Share-item-rm-1", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    dbInstance.commit();
    // share them
    questionDao.share(item, group.getResource());
    // retrieve them as a check
    List<QuestionItemView> shared = qItemQueriesDao.getSharedItemByResource(id, group.getResource(), null, null, 0, -1);
    Assert.assertEquals(1, shared.size());
    // and remove the items
    List<QuestionItemShort> toDelete = Collections.<QuestionItemShort>singletonList(shared.get(0));
    int count = questionDao.removeFromShares(toDelete);
    Assert.assertEquals(1, count);
    // make sure that changes are committed
    dbInstance.commit();
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) QuestionItemShort(org.olat.modules.qpool.QuestionItemShort) Identity(org.olat.core.id.Identity) QuestionItemView(org.olat.modules.qpool.QuestionItemView) QuestionItem(org.olat.modules.qpool.QuestionItem) QItemType(org.olat.modules.qpool.model.QItemType) Test(org.junit.Test)

Aggregations

QuestionItemView (org.olat.modules.qpool.QuestionItemView)118 Test (org.junit.Test)78 QuestionItem (org.olat.modules.qpool.QuestionItem)76 Identity (org.olat.core.id.Identity)72 SearchQuestionItemParams (org.olat.modules.qpool.model.SearchQuestionItemParams)62 ArrayList (java.util.ArrayList)34 QItemType (org.olat.modules.qpool.model.QItemType)18 BusinessGroup (org.olat.group.BusinessGroup)16 QuestionItemImpl (org.olat.modules.qpool.model.QuestionItemImpl)14 ItemWrapper (org.olat.modules.qpool.model.ItemWrapper)12 QItemViewEvent (org.olat.modules.qpool.ui.events.QItemViewEvent)12 SortKey (org.olat.core.commons.persistence.SortKey)10 MarkImpl (org.olat.core.commons.services.mark.impl.MarkImpl)10 Pool (org.olat.modules.qpool.Pool)10 QuestionItemCollection (org.olat.modules.qpool.QuestionItemCollection)10 Taxonomy (org.olat.modules.taxonomy.Taxonomy)8 TaxonomyLevel (org.olat.modules.taxonomy.TaxonomyLevel)8 SecurityGroupMembershipImpl (org.olat.basesecurity.SecurityGroupMembershipImpl)6 QuestionItemShort (org.olat.modules.qpool.QuestionItemShort)6 OLATResource (org.olat.resource.OLATResource)6